From b57f79f43882bbc0268999647890bd621bd22c94 Mon Sep 17 00:00:00 2001 From: Changbin Park Date: Wed, 31 Aug 2022 10:11:19 +0900 Subject: [PATCH 0001/1222] Detecting client disconnection (#1373) * SocketStream need to check connectivity for writability. When the stream is used for SSE server which works via chunked content provider it's not possible to check connectivity over writability because it's wrapped by DataSink. It could make the server stalled, when the provider wants to only keep connection without send data and certain amount of clients consumes entire threadpool. * add unittest for SocketStream::is_writable() SocketStream::is_writable() should return false if peer is disconnected. * revise broken unittest ServerTest.ClientStop DataSink could be unwritable if it's backed by connection based. Because it could be disconnected by client right after enter centent provider. Co-authored-by: Changbin Park --- httplib.h | 7 +++--- test/test.cc | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 0aa1c55fcd..16310e0648 100644 --- a/httplib.h +++ b/httplib.h @@ -4722,7 +4722,8 @@ inline bool SocketStream::is_readable() const { } inline bool SocketStream::is_writable() const { - return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0; + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_); } inline ssize_t SocketStream::read(char *ptr, size_t size) { @@ -7345,8 +7346,8 @@ inline bool SSLSocketStream::is_readable() const { } inline bool SSLSocketStream::is_writable() const { - return detail::select_write(sock_, write_timeout_sec_, write_timeout_usec_) > - 0; + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_); } inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { diff --git a/test/test.cc b/test/test.cc index aa7eeca5d8..0f61ed3b26 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1643,7 +1643,8 @@ class ServerTest : public ::testing::Test { res.set_content_provider( size_t(-1), "text/plain", [](size_t /*offset*/, size_t /*length*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); + if (!sink.is_writable()) return false; + sink.os << "data_chunk"; return true; }); @@ -5366,4 +5367,66 @@ TEST_F(UnixSocketTest, abstract) { t.join(); } #endif + +TEST(SocketStream, is_writable_UNIX) { + int fd[2]; + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fd)); + + const auto asSocketStream = + [&] (socket_t fd, std::function func) { + return detail::process_client_socket(fd, 0, 0, 0, 0, func); + }; + asSocketStream(fd[0], [&] (Stream &s0) { + EXPECT_EQ(s0.socket(), fd[0]); + EXPECT_TRUE(s0.is_writable()); + + EXPECT_EQ(0, close(fd[1])); + EXPECT_FALSE(s0.is_writable()); + + return true; + }); + EXPECT_EQ(0, close(fd[0])); +} + +TEST(SocketStream, is_writable_INET) { + sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT+1); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + int disconnected_svr_sock = -1; + std::thread svr {[&] { + const int s = socket(AF_INET, SOCK_STREAM, 0); + ASSERT_LE(0, s); + ASSERT_EQ(0, ::bind(s, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, listen(s, 1)); + ASSERT_LE(0, disconnected_svr_sock = accept(s, nullptr, nullptr)); + ASSERT_EQ(0, close(s)); + }}; + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + std::thread cli {[&] { + const int s = socket(AF_INET, SOCK_STREAM, 0); + ASSERT_LE(0, s); + ASSERT_EQ(0, connect(s, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, close(s)); + }}; + cli.join(); + svr.join(); + ASSERT_NE(disconnected_svr_sock, -1); + + const auto asSocketStream = + [&] (socket_t fd, std::function func) { + return detail::process_client_socket(fd, 0, 0, 0, 0, func); + }; + asSocketStream(disconnected_svr_sock, [&] (Stream &ss) { + EXPECT_EQ(ss.socket(), disconnected_svr_sock); + EXPECT_FALSE(ss.is_writable()); + + return true; + }); + + ASSERT_EQ(0, close(disconnected_svr_sock)); +} #endif // #ifndef _WIN32 From 8e10d4e8e7febafce0632810262e81e853b2065f Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 12 Sep 2022 11:36:14 -0400 Subject: [PATCH 0002/1222] Release v0.11.2 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 16310e0648..d380af0c0b 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.11.1" +#define CPPHTTPLIB_VERSION "0.11.2" /* * Configuration From cae5a8be1c9ef8399f45b89f6fc55afb122808cb Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 24 Sep 2022 08:27:54 -0400 Subject: [PATCH 0003/1222] Added more fuzzing corpus --- ...se-minimized-server_fuzzer-5886572146327552 | Bin 0 -> 974479 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/fuzzing/corpus/clusterfuzz-testcase-minimized-server_fuzzer-5886572146327552 diff --git a/test/fuzzing/corpus/clusterfuzz-testcase-minimized-server_fuzzer-5886572146327552 b/test/fuzzing/corpus/clusterfuzz-testcase-minimized-server_fuzzer-5886572146327552 new file mode 100644 index 0000000000000000000000000000000000000000..797165c50b2ce9fd62fc71f5ca322aeac8ac3bf4 GIT binary patch literal 974479 zcmeIv(Q4CB7y#g24GoR>0%9+xEStk!XxT1mTdZD4(e74=O{SH$DQ$`je1*M+*S?wH z3)Ez7H^F%|3Y&g^5>C#alRxMG(mn3g8nsTZ*WKISeG&#|t0UTp!l0Rr^K_g?z0*lr zAI*pPU=mOBy_0M@ijp{wKVHQ$M#b8#_up2_+i8*|X>$2~d)&{G!JCiuBt03% zc^U>`@a!yje!o+Byd6|3i}Ul&R%5YPeD3$tNgj3My#FaYs)u24epQR{UU|R0oR|4| zSRX#fgY~A~wHt4nouWde(n^PU9KDEVY2`2RaGh`b_PsLqvfZm4)H>z!{oVaAxcmLj zD8IAJ7L7zv+5M{Z)?hZtW`lf?jqBBFa2Z8sW&FIzG={_M%X;>@xH&AMd)zCB!^4A% zcv{T9t`n Date: Fri, 14 Oct 2022 16:49:48 +0200 Subject: [PATCH 0004/1222] Support compilation on AIX (#1402) Disable the ifaddrs.h include statement and the USE_IF2IP flag on AIX. AIX is a Unix OS but does not offer the ifaddrs.h header. --- httplib.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index d380af0c0b..12c7b85d70 100644 --- a/httplib.h +++ b/httplib.h @@ -172,7 +172,9 @@ using socket_t = SOCKET; #else // not _WIN32 #include +#ifndef _AIX #include +#endif #include #include #include @@ -2720,7 +2722,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { return ret; } -#if !defined _WIN32 && !defined ANDROID +#if !defined _WIN32 && !defined ANDROID && !defined _AIX #define USE_IF2IP #endif From ad7edc7b2798c8363e30e38df519172a7d9f86d3 Mon Sep 17 00:00:00 2001 From: Changbin Park Date: Thu, 3 Nov 2022 23:23:45 +0900 Subject: [PATCH 0005/1222] avoid lockup in `ThreadPool::shutdown()` on legacy host (#1417) It is found on CentOS 7.0.1406 which is very early version of CentOS 7. Co-authored-by: Changbin Park --- httplib.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 12c7b85d70..761481b518 100644 --- a/httplib.h +++ b/httplib.h @@ -560,10 +560,9 @@ class ThreadPool : public TaskQueue { { std::unique_lock lock(mutex_); shutdown_ = true; + cond_.notify_all(); } - cond_.notify_all(); - // Join... for (auto &t : threads_) { t.join(); From 93a51979c4dedbd49885f502c01d09fac070f449 Mon Sep 17 00:00:00 2001 From: Changbin Park Date: Thu, 3 Nov 2022 23:25:18 +0900 Subject: [PATCH 0006/1222] Get client process id over ip/port when server runs on UNIX socket. (#1418) * handle socket options for UNIX socket same as others * set FD_CLOEXEC by default * invoke `socket_options` callback if set * Offer Client info even on UNIX socket based Server HTTP Request header "REMOTE_PORT" contains client process id if possible when Server works on UNIX socket. * retrigger checks * retrigger checks * add support macOS Co-authored-by: Changbin Park --- httplib.h | 21 +++++++++++++++++++++ test/test.cc | 25 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/httplib.h b/httplib.h index 761481b518..05732d51b1 100644 --- a/httplib.h +++ b/httplib.h @@ -2601,6 +2601,9 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_addrlen = static_cast( sizeof(addr) - sizeof(addr.sun_path) + addrlen); + fcntl(sock, F_SETFD, FD_CLOEXEC); + if (socket_options) { socket_options(sock); } + if (!bind_or_connect(sock, hints)) { close_socket(sock); sock = INVALID_SOCKET; @@ -2871,6 +2874,24 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { if (!getpeername(sock, reinterpret_cast(&addr), &addr_len)) { +#ifndef _WIN32 + if (addr.ss_family == AF_UNIX) { +#if defined(__linux__) + struct ucred ucred; + socklen_t len = sizeof(ucred); + if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { + port = ucred.pid; + } +#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ + pid_t pid; + socklen_t len = sizeof(pid); + if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { + port = pid; + } +#endif + return; + } +#endif get_remote_ip_and_port(addr, addr_len, ip, port); } } diff --git a/test/test.cc b/test/test.cc index 0f61ed3b26..266eebcd5e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5344,6 +5344,31 @@ TEST_F(UnixSocketTest, pathname) { t.join(); } +#if defined(__linux__) \ + || /* __APPLE__ */ (defined(SOL_LOCAL) && defined(SO_PEERPID)) +TEST_F(UnixSocketTest, PeerPid) { + httplib::Server svr; + std::string remote_port_val; + svr.Get(pattern_, [&](const httplib::Request &req, httplib::Response &res) { + res.set_content(content_, "text/plain"); + remote_port_val = req.get_header_value("REMOTE_PORT"); + }); + + std::thread t {[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); }}; + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + ASSERT_TRUE(svr.is_running()); + + client_GET(pathname_); + EXPECT_EQ(std::to_string(getpid()), remote_port_val); + + svr.stop(); + t.join(); +} +#endif + #ifdef __linux__ TEST_F(UnixSocketTest, abstract) { constexpr char svr_path[] {"\x00httplib-server.sock"}; From 26196b70af0b5ae731c5d052977025bf7676ac04 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 3 Nov 2022 13:07:04 -0400 Subject: [PATCH 0007/1222] Add test case for 'Issue 52666 in oss-fuzz: cpp-httplib:server_fuzzer: Timeout in server_fuzzer' --- ...se-minimized-server_fuzzer-5042094968537088 | Bin 0 -> 516575 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 diff --git a/test/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 b/test/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 new file mode 100644 index 0000000000000000000000000000000000000000..0325729036eeef76902e138b0f0edad8fe884429 GIT binary patch literal 516575 zcmeI* Lb>?x8R58&7qwYsVmMVRbN-pFVOyW8nAxK3R6%k1*HnT9!C1aY{){T=k zhy}A8sYv|?MjEyM0P7=mbk(YBBxcclyJMDJc)rJWj?WV(Pfn0ee(|sizyIgMj}Bit{PE$>55Ir-=fl4pZXMn_+&sK__@~2T zhx5bV9{%I-2Zw(>9RB+7<-+1RKul?}m`Rk{zT)lnu2RGlm@ygZnx1WFg_*H$J-`eBf)bU&JVZOEZXYc=wzk2!A zA78(9@#Nykvp+kzaN*9~KOXzsH}BlJ^P_9OzIE;P=P%s-lAC+_^rf@gx3AxL^^GrH zJh}XvU!7dOc>HVr=go`H9KSq$<;srapWJ@@+WpZ33@)9>E8ef{Q*H}20qm4E&F`^}Xr$8Y%Q>A(Np$0g_8$gj(=;%FIR8fdhOod+MoXFZ_ZvjdnKF4PEP*t z_=ile!AI*nPpML!GsmD*hare*1;_L6!Pu~5=@z2MP9RGZ;Jr3Oa!2|DXIm{5O{$xgXPyT=@xvpH3eB$>5Xu z(+T(TG2P1}k$&y>BbdxD@gFAa-bdp7f`>jnpUkKH694`JG5OPMviV%jhyUg5{;$6I z#W$bJx$n-q{}S*1!29;|$^0rlxzIoPzJG_G#DJeCKBk{1j=$IaMg4x4-|Ien8=uVI z>puMLe54J5IResX`2 zG5E%RibhaR5PYY3)c@oZ^-yo10_qKx^aw^!?;Ysmuc-Ig`Im!!QQxy4(J$=#Sw?R@ zutvQPT+t&LK)t$&UZ5W8nfnTpX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq z^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs z3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1# z+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lm zp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU); ze203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{ zx?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2X zX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8 z>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?B zIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XN zdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4 zpdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOy zpUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0; zuVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOp zFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203v zy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j z>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eA zcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^ zS2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;I zM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)> z4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX( zlM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiit zr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs= z>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4 z$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)Q zO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-Z zlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^ z+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOP zo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#h zhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9 zUZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+ zG@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}= zP_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)F zxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=; zy1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU4 z9_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c z%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rS zV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTN znMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClb ztq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8 znUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0O zsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWG zHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV z^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq z^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs z3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1# z+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lm zp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU); ze203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{ zx?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2X zX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8 z>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?B zIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XN zdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4 zpdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOy zpUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0; zuVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOp zFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203v zy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j z>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eA zcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^ zS2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;I zM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)> z4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX( zlM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiit zr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs= z>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4 z$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)Q zO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-Z zlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^ z+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOP zo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#h zhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9 zUZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+ zG@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}= zP_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)F zxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=; zy1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU4 z9_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c z%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rS zV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTN znMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClb ztq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8 znUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0O zsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWG zHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV z^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq z^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs z3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1# z+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lm zp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU); ze203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{ zx?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2X zX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8 z>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?B zIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XN zdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4 zpdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOy zpUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0; zuVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOp zFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203v zy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j z>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eA zcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^ zS2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;I zM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs=>Y<)FxiFbV^T~XNdb+)> z4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4$$W=;y1lIq^-8*4pdRX( zlM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)QO1fU49_pEs3zKOypUiit zr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-ZlW8=c%y+1#+uQ0;uVYs= z>Y<)FxiFbV^T~XNdb+)>4)sd9UZ5W8nUf2XX*8eAcc`b^+v-rSV^=lmp`JOpFqua4 z$$W=;y1lIq^-8*4pdRX(lM9n+G@s0OsHfZ8>QJv^S2gOPo;kTNnMU);e203vy{!)Q zO1fU49_pEs3zKOypUiitr`y}=P_JWGHR_?BIk_;IM)S#hhkClbtq%1{x?Z3j>Y0-Z zlW8=c%y+1#+uQ0;uVYs=>U|2-d#{^__xj-behg*acQubRK$()I7bx>7P^N!}nfe!% zJzJF&kNP{iA87RE18da#z!g1`0o0QNc&B;Phj8=8?Xw%VPhYrk?dB`jZ@hZ>)gNEK zb@All$+JH@xp3jm-9H}t+&Aysx$~oIzrJY0-Z zlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^ z+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmne zo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx1?r)mIk_;IM)S#h zhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>A zRihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+ zG@s0OsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}= zP_Lxx1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)F zxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=; zy1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~ z9_pEs3zKOypUiitr`y}=P_Lxx1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c z%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rS zr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTN znMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx1?r)mIk_;IM)S#hhkClb ztq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2 znUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0O zsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx z1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV z^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq z^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs z3zKOypUiitr`y}=P_Lxx1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1# z+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIh zp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU); ze203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx1?r)mIk_;IM)S#hhkClbtq%1% zc2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2X zX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8 z>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx1?r)m zIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XN zdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{ zqaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOy zpUiitr`y}=P_Lxx1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0; zucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOp zFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU);e203v zy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu z>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eA zcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww z>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx1?r)mIk_;I zM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)> z4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+ zlM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiit zr`y}=P_Lxx1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe* z>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4 z$$W=;y1lIq^*VM{qaNy+lM9n+G@s0OsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)Q zI(Ai~-bbO{!#}A1=;04N^pSsg=p!Hc1NHr!#k0~?!w-Rv;)lT5D;JJm9y>Yt!;_O* zy)T}75MO#w+UggTenj8d{YPZ<<^yYf;`_iAJ(2-_;;WnJ1?r)mxvwypM)S#hhkClb ztq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV^T~XNdb+)>4)r>ARihs2 znUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq^*VM{qaNy+lM9n+G@s0O zsHfZ8>QJww>jmneo;kTNnMU);e203vy{!)QI(Ai~9_pEs3zKOypUiitr`y}=P_Lxx z1?r)mIk_;IM)S#hhkClbtq%1%c2%Pu>Y0-ZlW8=c%y+1#+uQ0;ucYe*>Y<)FxiFbV z^T~XNdb+)>4)r>ARihs2nUf2XX*8eAcc`b^+v-rSr0WIhp`JOpFqua4$$W=;y1lIq z^*VM{KWg>*cVG181M3fU2>KY5?I=ile!pNS9u4j#jA_5Jo) z+0%ddt^V%o(Nm9~KK=OVHy%Gd7Ej*&+gKz7(H`$^6NKm+&;T;`}Bny z*KWRY{l=@8U;Xj*TNh6*o;>@rlM5H_-2LOR&wcaGojX6e_Ul{MZht=i7l|%h_|D5O zzxed&OJ}!lAK$$3#fvAGfAg!8%NLJ-&Huc4@tNb7r;l>|*MI-?Tlc?_FTZ{3?DV^zkzf1#?cqNTe{lHM z!{M(FUq1Z9;qNZxPQUi#SD(0e^1Z9izxtP#fAHe>{_N6I_rGJZ{^h-qC%*c`{m~~c zo_y=O^8Ke{$GC0o^6PKDcKiCRtLL}B_QRX!ub;kh_4d^t+V(0%wMr^FOTY8K27{S;-4(| zLG-A85IrDt`oGxt;05TGwN-%bPagaLIaa^pzs^2x;1{(I?xoJ8icjXf)HiRZ_^f=o z;6rSujv$NA)*Ze}1>h&-vc}ZTz>dzj5p4 z8`p1Nzj@>G7rt=v;LF|L<;xAw#Qz`b^he+*hbr@XuPE&dN^9()y_t>2Xb{Sia{wSfmeyx*(3e=iSytUUdo z?3MnW?R#fGJUc%-&w=;*^j&}apnf#J+W1O+g{%MW)wA_qbNTX%fAR9AFWvhWAfC#9 zN8;u4t2f>_F5$tyxpM7)ym{lV&R)rXdFAxEC!c)gsc$^{%m01t$>Zar4HDPyc@uheSF6 literal 0 HcmV?d00001 From 42feb7e8bee726e2d568572108fea964be70719b Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 10 Nov 2022 08:27:30 -0500 Subject: [PATCH 0008/1222] Updated README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 316457c677..f6db7c0875 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ httplib::SSLServer svr("./cert.pem", "./key.pem"); // Client httplib::Client cli("https://localhost:1234"); // scheme + host httplib::SSLClient cli("localhost:1234"); // host +httplib::SSLClient cli("localhost", 1234); // host, port // Use your CA bundle cli.set_ca_cert_path("./ca-bundle.crt"); From 87994811a1321ac03947eaf53e91357928dee2ca Mon Sep 17 00:00:00 2001 From: Pavel Artemkin Date: Mon, 14 Nov 2022 07:49:49 +0500 Subject: [PATCH 0009/1222] undef poll at the end if CPPHTTPLIB_USE_POLL (#1427) * undef poll at the end if CPPHTTPLIB_USE_POLL * win32 only --- httplib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/httplib.h b/httplib.h index 05732d51b1..3809828aa5 100644 --- a/httplib.h +++ b/httplib.h @@ -8405,4 +8405,8 @@ inline SSL_CTX *Client::ssl_context() const { } // namespace httplib +#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL) +#undef poll +#endif + #endif // CPPHTTPLIB_HTTPLIB_H From 07c6e58951931f8c74de8291ff35a3298fe481c4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 15 Nov 2022 11:53:06 -0500 Subject: [PATCH 0010/1222] Fix #1421 --- httplib.h | 38 +++++++---- test/test.cc | 184 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 133 insertions(+), 89 deletions(-) diff --git a/httplib.h b/httplib.h index 3809828aa5..e96b561d2d 100644 --- a/httplib.h +++ b/httplib.h @@ -902,6 +902,7 @@ class ClientImpl { Result Head(const std::string &path, const Headers &headers); Result Post(const std::string &path); + Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type); Result Post(const std::string &path, const Headers &headers, const char *body, @@ -1263,6 +1264,7 @@ class Client { Result Head(const std::string &path, const Headers &headers); Result Post(const std::string &path); + Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type); Result Post(const std::string &path, const Headers &headers, const char *body, @@ -2877,19 +2879,19 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { #ifndef _WIN32 if (addr.ss_family == AF_UNIX) { #if defined(__linux__) - struct ucred ucred; - socklen_t len = sizeof(ucred); - if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { - port = ucred.pid; - } -#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ - pid_t pid; - socklen_t len = sizeof(pid); - if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { - port = pid; - } + struct ucred ucred; + socklen_t len = sizeof(ucred); + if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { + port = ucred.pid; + } +#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ + pid_t pid; + socklen_t len = sizeof(pid); + if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { + port = pid; + } #endif - return; + return; } #endif get_remote_ip_and_port(addr, addr_len, ip, port); @@ -4745,7 +4747,7 @@ inline bool SocketStream::is_readable() const { inline bool SocketStream::is_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); + is_socket_alive(sock_); } inline ssize_t SocketStream::read(char *ptr, size_t size) { @@ -6752,6 +6754,11 @@ inline Result ClientImpl::Post(const std::string &path) { return Post(path, std::string(), std::string()); } +inline Result ClientImpl::Post(const std::string &path, + const Headers &headers) { + return Post(path, headers, nullptr, 0, std::string()); +} + inline Result ClientImpl::Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type) { @@ -7369,7 +7376,7 @@ inline bool SSLSocketStream::is_readable() const { inline bool SSLSocketStream::is_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); + is_socket_alive(sock_); } inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { @@ -8075,6 +8082,9 @@ inline Result Client::Head(const std::string &path, const Headers &headers) { } inline Result Client::Post(const std::string &path) { return cli_->Post(path); } +inline Result Client::Post(const std::string &path, const Headers &headers) { + return cli_->Post(path, headers); +} inline Result Client::Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type) { diff --git a/test/test.cc b/test/test.cc index 266eebcd5e..a92d0a5e35 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1715,6 +1715,22 @@ class ServerTest : public ::testing::Test { EXPECT_EQ("0", req.get_header_value("Content-Length")); res.set_content("empty-no-content-type", "text/plain"); }) + .Post("/path-only", + [&](const Request &req, Response &res) { + EXPECT_EQ(req.body, ""); + EXPECT_EQ("", req.get_header_value("Content-Type")); + EXPECT_EQ("0", req.get_header_value("Content-Length")); + res.set_content("path-only", "text/plain"); + }) + .Post("/path-headers-only", + [&](const Request &req, Response &res) { + EXPECT_EQ(req.body, ""); + EXPECT_EQ("", req.get_header_value("Content-Type")); + EXPECT_EQ("0", req.get_header_value("Content-Length")); + EXPECT_EQ("world", req.get_header_value("hello")); + EXPECT_EQ("world2", req.get_header_value("hello2")); + res.set_content("path-headers-only", "text/plain"); + }) .Post("/post-large", [&](const Request &req, Response &res) { EXPECT_EQ(req.body, LARGE_DATA); @@ -2125,6 +2141,21 @@ TEST_F(ServerTest, PostEmptyContentWithNoContentType) { ASSERT_EQ("empty-no-content-type", res->body); } +TEST_F(ServerTest, PostPathOnly) { + auto res = cli_.Post("/path-only"); + ASSERT_TRUE(res); + ASSERT_EQ(200, res->status); + ASSERT_EQ("path-only", res->body); +} + +TEST_F(ServerTest, PostPathAndHeadersOnly) { + auto res = cli_.Post("/path-headers-only", + Headers({{"hello", "world"}, {"hello2", "world2"}})); + ASSERT_TRUE(res); + ASSERT_EQ(200, res->status); + ASSERT_EQ("path-headers-only", res->body); +} + TEST_F(ServerTest, PostLarge) { auto res = cli_.Post("/post-large", LARGE_DATA, "text/plain"); ASSERT_TRUE(res); @@ -4181,7 +4212,8 @@ class ServerTestWithAI_PASSIVE : public ::testing::Test { res.set_content("Hello World!", "text/plain"); }); - t_ = thread([&]() { ASSERT_TRUE(svr_.listen(std::string(), PORT, AI_PASSIVE)); }); + t_ = thread( + [&]() { ASSERT_TRUE(svr_.listen(std::string(), PORT, AI_PASSIVE)); }); while (!svr_.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -4753,7 +4785,7 @@ TEST(SendAPI, SimpleInterface_Online) { TEST(ClientImplMethods, GetSocketTest) { httplib::Server svr; - svr.Get( "/", [&](const httplib::Request& /*req*/, httplib::Response& res) { + svr.Get("/", [&](const httplib::Request & /*req*/, httplib::Response &res) { res.status = 200; }); @@ -5067,7 +5099,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); svr.Post("/post_customboundary", [&](const Request &req, Response & /*res*/, - const ContentReader &content_reader) { + const ContentReader &content_reader) { if (req.is_multipart_form_data()) { MultipartFormDataItems files; content_reader( @@ -5127,7 +5159,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { } TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { - + std::this_thread::sleep_for(std::chrono::seconds(1)); std::string data(1024 * 1024 * 2, '&'); @@ -5141,12 +5173,12 @@ TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { {"hello", "world", "", ""}, }; - for (const char& c: " \t\r\n") { - auto res = cli.Post("/invalid_boundary", {}, items, string("abc123").append(1, c)); + for (const char &c : " \t\r\n") { + auto res = + cli.Post("/invalid_boundary", {}, items, string("abc123").append(1, c)); ASSERT_EQ(Error::UnsupportedMultipartBoundaryChars, res.error()); ASSERT_FALSE(res); } - } TEST(MultipartFormDataTest, PutFormData) { @@ -5215,37 +5247,38 @@ TEST(MultipartFormDataTest, PutFormData) { TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); - svr.Put("/put_customboundary", [&](const Request &req, const Response & /*res*/, - const ContentReader &content_reader) { - if (req.is_multipart_form_data()) { - MultipartFormDataItems files; - content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); - return true; - }, - [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); - return true; - }); - - EXPECT_TRUE(std::string(files[0].name) == "document"); - EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size()); - EXPECT_TRUE(files[0].filename == "2MB_data"); - EXPECT_TRUE(files[0].content_type == "application/octet-stream"); + svr.Put("/put_customboundary", + [&](const Request &req, const Response & /*res*/, + const ContentReader &content_reader) { + if (req.is_multipart_form_data()) { + MultipartFormDataItems files; + content_reader( + [&](const MultipartFormData &file) { + files.push_back(file); + return true; + }, + [&](const char *data, size_t data_length) { + files.back().content.append(data, data_length); + return true; + }); - EXPECT_TRUE(files[1].name == "hello"); - EXPECT_TRUE(files[1].content == "world"); - EXPECT_TRUE(files[1].filename == ""); - EXPECT_TRUE(files[1].content_type == ""); - } else { - std::string body; - content_reader([&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; - }); - } - }); + EXPECT_TRUE(std::string(files[0].name) == "document"); + EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size()); + EXPECT_TRUE(files[0].filename == "2MB_data"); + EXPECT_TRUE(files[0].content_type == "application/octet-stream"); + + EXPECT_TRUE(files[1].name == "hello"); + EXPECT_TRUE(files[1].content == "world"); + EXPECT_TRUE(files[1].filename == ""); + EXPECT_TRUE(files[1].content_type == ""); + } else { + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + } + }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); while (!svr.is_running()) { @@ -5291,7 +5324,7 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { {"hello", "world", "", ""}, }; - for (const char& c: " \t\r\n") { + for (const char &c : " \t\r\n") { auto res = cli.Put("/put", {}, items, string("abc123").append(1, c)); ASSERT_EQ(Error::UnsupportedMultipartBoundaryChars, res.error()); ASSERT_FALSE(res); @@ -5303,9 +5336,7 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { #ifndef _WIN32 class UnixSocketTest : public ::testing::Test { protected: - void TearDown() override { - std::remove(pathname_.c_str()); - } + void TearDown() override { std::remove(pathname_.c_str()); } void client_GET(const std::string &addr) { httplib::Client cli{addr}; @@ -5320,9 +5351,9 @@ class UnixSocketTest : public ::testing::Test { EXPECT_EQ(resp.body, content_); } - const std::string pathname_ {"./httplib-server.sock"}; - const std::string pattern_ {"/hi"}; - const std::string content_ {"Hello World!"}; + const std::string pathname_{"./httplib-server.sock"}; + const std::string pattern_{"/hi"}; + const std::string content_{"Hello World!"}; }; TEST_F(UnixSocketTest, pathname) { @@ -5331,8 +5362,9 @@ TEST_F(UnixSocketTest, pathname) { res.set_content(content_, "text/plain"); }); - std::thread t {[&] { - ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); }}; + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); + }}; while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5344,8 +5376,8 @@ TEST_F(UnixSocketTest, pathname) { t.join(); } -#if defined(__linux__) \ - || /* __APPLE__ */ (defined(SOL_LOCAL) && defined(SO_PEERPID)) +#if defined(__linux__) || \ + /* __APPLE__ */ (defined(SOL_LOCAL) && defined(SO_PEERPID)) TEST_F(UnixSocketTest, PeerPid) { httplib::Server svr; std::string remote_port_val; @@ -5354,8 +5386,9 @@ TEST_F(UnixSocketTest, PeerPid) { remote_port_val = req.get_header_value("REMOTE_PORT"); }); - std::thread t {[&] { - ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); }}; + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); + }}; while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5371,16 +5404,17 @@ TEST_F(UnixSocketTest, PeerPid) { #ifdef __linux__ TEST_F(UnixSocketTest, abstract) { - constexpr char svr_path[] {"\x00httplib-server.sock"}; - const std::string abstract_addr {svr_path, sizeof(svr_path) - 1}; + constexpr char svr_path[]{"\x00httplib-server.sock"}; + const std::string abstract_addr{svr_path, sizeof(svr_path) - 1}; httplib::Server svr; svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) { res.set_content(content_, "text/plain"); }); - std::thread t {[&] { - ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80)); }}; + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80)); + }}; while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5394,58 +5428,58 @@ TEST_F(UnixSocketTest, abstract) { #endif TEST(SocketStream, is_writable_UNIX) { - int fd[2]; - ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fd)); + int fds[2]; + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); - const auto asSocketStream = - [&] (socket_t fd, std::function func) { - return detail::process_client_socket(fd, 0, 0, 0, 0, func); - }; - asSocketStream(fd[0], [&] (Stream &s0) { - EXPECT_EQ(s0.socket(), fd[0]); + const auto asSocketStream = [&](socket_t fd, + std::function func) { + return detail::process_client_socket(fd, 0, 0, 0, 0, func); + }; + asSocketStream(fds[0], [&](Stream &s0) { + EXPECT_EQ(s0.socket(), fds[0]); EXPECT_TRUE(s0.is_writable()); - EXPECT_EQ(0, close(fd[1])); + EXPECT_EQ(0, close(fds[1])); EXPECT_FALSE(s0.is_writable()); return true; }); - EXPECT_EQ(0, close(fd[0])); + EXPECT_EQ(0, close(fds[0])); } TEST(SocketStream, is_writable_INET) { sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_port = htons(PORT+1); + addr.sin_port = htons(PORT + 1); addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); int disconnected_svr_sock = -1; - std::thread svr {[&] { + std::thread svr{[&] { const int s = socket(AF_INET, SOCK_STREAM, 0); ASSERT_LE(0, s); - ASSERT_EQ(0, ::bind(s, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, ::bind(s, reinterpret_cast(&addr), sizeof(addr))); ASSERT_EQ(0, listen(s, 1)); ASSERT_LE(0, disconnected_svr_sock = accept(s, nullptr, nullptr)); ASSERT_EQ(0, close(s)); }}; std::this_thread::sleep_for(std::chrono::milliseconds(100)); - std::thread cli {[&] { + std::thread cli{[&] { const int s = socket(AF_INET, SOCK_STREAM, 0); ASSERT_LE(0, s); - ASSERT_EQ(0, connect(s, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, connect(s, reinterpret_cast(&addr), sizeof(addr))); ASSERT_EQ(0, close(s)); }}; cli.join(); svr.join(); ASSERT_NE(disconnected_svr_sock, -1); - const auto asSocketStream = - [&] (socket_t fd, std::function func) { - return detail::process_client_socket(fd, 0, 0, 0, 0, func); - }; - asSocketStream(disconnected_svr_sock, [&] (Stream &ss) { + const auto asSocketStream = [&](socket_t fd, + std::function func) { + return detail::process_client_socket(fd, 0, 0, 0, 0, func); + }; + asSocketStream(disconnected_svr_sock, [&](Stream &ss) { EXPECT_EQ(ss.socket(), disconnected_svr_sock); EXPECT_FALSE(ss.is_writable()); @@ -5454,4 +5488,4 @@ TEST(SocketStream, is_writable_INET) { ASSERT_EQ(0, close(disconnected_svr_sock)); } -#endif // #ifndef _WIN32 +#endif // #ifndef _WIN32 From e7eadc3605bc4532e139771ae0ac42d6979dfd50 Mon Sep 17 00:00:00 2001 From: Ray Beck <115stingray@gmail.com> Date: Thu, 24 Nov 2022 22:23:11 -0500 Subject: [PATCH 0011/1222] add SYSTEM to include to prevent warnings (#1428) --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8b73a4586..9e526a019d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,7 +193,7 @@ target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11 ) -target_include_directories(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} +target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC} $ $ ) From 5758769ad3c892782fb8f7fbbe733f1ddbf0cdc3 Mon Sep 17 00:00:00 2001 From: TheMarpe Date: Fri, 25 Nov 2022 04:51:55 +0100 Subject: [PATCH 0012/1222] Windows CMake directory install fix (#1434) * Fixed install convention pre 3.25 * Simplified CMAKEDIR installation directory --- CMakeLists.txt | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e526a019d..709390a70b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,15 +221,8 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_OPENSSL_SUPPORT> ) -# Cmake's find_package search path is different based on the system -# See https://cmake.org/cmake/help/latest/command/find_package.html for the list -if(CMAKE_SYSTEM_NAME STREQUAL "Windows") - set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_PREFIX}/cmake/${PROJECT_NAME}") -else() - # On Non-Windows, it should be /usr/lib/cmake//Config.cmake - # NOTE: This may or may not work for macOS... - set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") -endif() +# CMake configuration files installation directory +set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") include(CMakePackageConfigHelpers) From 9d0a9d4e23f022bc3fb0f3d6bf98bd401674be4c Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 27 Nov 2022 10:05:30 -0500 Subject: [PATCH 0013/1222] Fix #1437 --- httplib.h | 11 ++++++++++- test/test.cc | 27 ++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index e96b561d2d..ff11e36544 100644 --- a/httplib.h +++ b/httplib.h @@ -5855,7 +5855,16 @@ Server::process_request(Stream &strm, bool close_connection, routed = true; } else { res.status = 500; - res.set_header("EXCEPTION_WHAT", e.what()); + std::string val; + auto s = e.what(); + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case '\r': val += "\\r"; break; + case '\n': val += "\\n"; break; + default: val += s[i]; break; + } + } + res.set_header("EXCEPTION_WHAT", val); } } catch (...) { if (exception_handler_) { diff --git a/test/test.cc b/test/test.cc index a92d0a5e35..9e665619da 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3839,9 +3839,12 @@ TEST(MountTest, Unmount) { TEST(ExceptionTest, ThrowExceptionInHandler) { Server svr; - svr.Get("/hi", [&](const Request & /*req*/, Response & /*res*/) { + svr.Get("/exception", [&](const Request & /*req*/, Response & /*res*/) { throw std::runtime_error("exception..."); - // res.set_content("Hello World!", "text/plain"); + }); + + svr.Get("/unknown", [&](const Request & /*req*/, Response & /*res*/) { + throw std::runtime_error("exception\r\n..."); }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); @@ -3854,11 +3857,21 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { Client cli("localhost", PORT); - auto res = cli.Get("/hi"); - ASSERT_TRUE(res); - EXPECT_EQ(500, res->status); - ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); - EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT")); + { + auto res = cli.Get("/exception"); + ASSERT_TRUE(res); + EXPECT_EQ(500, res->status); + ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); + EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT")); + } + + { + auto res = cli.Get("/unknown"); + ASSERT_TRUE(res); + EXPECT_EQ(500, res->status); + ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); + EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT")); + } svr.stop(); listen_thread.join(); From 74fe5a50294ebd670d2256d2bb9362583748fed7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 27 Nov 2022 10:53:11 -0500 Subject: [PATCH 0014/1222] Fix #1426 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ff11e36544..04a4c4ee48 100644 --- a/httplib.h +++ b/httplib.h @@ -1536,7 +1536,7 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { auto usec = std::chrono::duration_cast( duration - std::chrono::seconds(sec)) .count(); - callback(sec, usec); + callback(static_cast(sec), static_cast(usec)); } template From c0b461a3b7b15877b5c8047b409c124aceec8fd9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 29 Nov 2022 19:26:10 -0500 Subject: [PATCH 0015/1222] Release v0.11.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 04a4c4ee48..31a5431405 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.11.2" +#define CPPHTTPLIB_VERSION "0.11.3" /* * Configuration From 9f512acb427314fe1d1cc7d134f02933568fec91 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 1 Dec 2022 13:38:58 -0500 Subject: [PATCH 0016/1222] Removed code for upsupported OpenSSL --- httplib.h | 49 ------------------------------------------------- 1 file changed, 49 deletions(-) diff --git a/httplib.h b/httplib.h index 31a5431405..3b069b0853 100644 --- a/httplib.h +++ b/httplib.h @@ -257,12 +257,6 @@ using socket_t = int; #error Sorry, OpenSSL versions prior to 1.1.1 are not supported #endif -#if OPENSSL_VERSION_NUMBER < 0x10100000L -#include -inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) { - return M_ASN1_STRING_data(asn1); -} -#endif #endif #ifdef CPPHTTPLIB_ZLIB_SUPPORT @@ -7313,55 +7307,12 @@ process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, return callback(strm); } -#if OPENSSL_VERSION_NUMBER < 0x10100000L -static std::shared_ptr> openSSL_locks_; - -class SSLThreadLocks { -public: - SSLThreadLocks() { - openSSL_locks_ = - std::make_shared>(CRYPTO_num_locks()); - CRYPTO_set_locking_callback(locking_callback); - } - - ~SSLThreadLocks() { CRYPTO_set_locking_callback(nullptr); } - -private: - static void locking_callback(int mode, int type, const char * /*file*/, - int /*line*/) { - auto &lk = (*openSSL_locks_)[static_cast(type)]; - if (mode & CRYPTO_LOCK) { - lk.lock(); - } else { - lk.unlock(); - } - } -}; - -#endif - class SSLInit { public: SSLInit() { -#if OPENSSL_VERSION_NUMBER < 0x1010001fL - SSL_load_error_strings(); - SSL_library_init(); -#else OPENSSL_init_ssl( OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); -#endif } - - ~SSLInit() { -#if OPENSSL_VERSION_NUMBER < 0x1010001fL - ERR_free_strings(); -#endif - } - -private: -#if OPENSSL_VERSION_NUMBER < 0x10100000L - SSLThreadLocks thread_init_; -#endif }; // SSL socket stream implementation From c8c1c3d376715666eb665c6011933b783876201d Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 1 Dec 2022 13:38:58 -0500 Subject: [PATCH 0017/1222] Fix #1442 --- httplib.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3b069b0853..c32e2dee41 100644 --- a/httplib.h +++ b/httplib.h @@ -255,6 +255,8 @@ using socket_t = int; #if OPENSSL_VERSION_NUMBER < 0x1010100fL #error Sorry, OpenSSL versions prior to 1.1.1 are not supported +#elif OPENSSL_VERSION_NUMBER < 0x30000000L +#define SSL_get1_peer_certificate SSL_get_peer_certificate #endif #endif @@ -7716,7 +7718,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return false; } - auto server_cert = SSL_get_peer_certificate(ssl2); + auto server_cert = SSL_get1_peer_certificate(ssl2); if (server_cert == nullptr) { error = Error::SSLServerVerification; From 8f32271e8cd7fc294721e2166ab334d5b2c0c9b3 Mon Sep 17 00:00:00 2001 From: Ingo Bauersachs Date: Tue, 6 Dec 2022 14:23:09 +0100 Subject: [PATCH 0018/1222] Support LOCAL_ADDR and LOCAL_PORT header in client Request (#1450) Having the local address/port is useful if the server is bound to all interfaces, e.g. to serve different content for developers on localhost only. --- httplib.h | 44 ++++++++++++++++++++++++++++++----- test/fuzzing/server_fuzzer.cc | 7 ++++-- test/test.cc | 20 ++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index c32e2dee41..c7bf81092f 100644 --- a/httplib.h +++ b/httplib.h @@ -413,6 +413,8 @@ struct Request { std::string remote_addr; int remote_port = -1; + std::string local_addr; + int local_port = -1; // for server std::string version; @@ -514,6 +516,7 @@ class Stream { virtual ssize_t read(char *ptr, size_t size) = 0; virtual ssize_t write(const char *ptr, size_t size) = 0; virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; + virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; virtual socket_t socket() const = 0; template @@ -1778,6 +1781,7 @@ class BufferStream : public Stream { ssize_t read(char *ptr, size_t size) override; ssize_t write(const char *ptr, size_t size) override; void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; socket_t socket() const override; const std::string &get_buffer() const; @@ -2446,6 +2450,7 @@ class SocketStream : public Stream { ssize_t read(char *ptr, size_t size) override; ssize_t write(const char *ptr, size_t size) override; void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; socket_t socket() const override; private: @@ -2475,6 +2480,7 @@ class SSLSocketStream : public Stream { ssize_t read(char *ptr, size_t size) override; ssize_t write(const char *ptr, size_t size) override; void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; socket_t socket() const override; private: @@ -2843,9 +2849,9 @@ inline socket_t create_client_socket( return sock; } -inline bool get_remote_ip_and_port(const struct sockaddr_storage &addr, - socklen_t addr_len, std::string &ip, - int &port) { +inline bool get_ip_and_port(const struct sockaddr_storage &addr, + socklen_t addr_len, std::string &ip, + int &port) { if (addr.ss_family == AF_INET) { port = ntohs(reinterpret_cast(&addr)->sin_port); } else if (addr.ss_family == AF_INET6) { @@ -2866,6 +2872,15 @@ inline bool get_remote_ip_and_port(const struct sockaddr_storage &addr, return true; } +inline void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (!getsockname(sock, reinterpret_cast(&addr), + &addr_len)) { + get_ip_and_port(addr, addr_len, ip, port); + } +} + inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { struct sockaddr_storage addr; socklen_t addr_len = sizeof(addr); @@ -2890,7 +2905,7 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { return; } #endif - get_remote_ip_and_port(addr, addr_len, ip, port); + get_ip_and_port(addr, addr_len, ip, port); } } @@ -4517,8 +4532,8 @@ inline void hosted_at(const std::string &hostname, *reinterpret_cast(rp->ai_addr); std::string ip; int dummy = -1; - if (detail::get_remote_ip_and_port(addr, sizeof(struct sockaddr_storage), - ip, dummy)) { + if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), + ip, dummy)) { addrs.push_back(ip); } } @@ -4808,6 +4823,11 @@ inline void SocketStream::get_remote_ip_and_port(std::string &ip, return detail::get_remote_ip_and_port(sock_, ip, port); } +inline void SocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + return detail::get_local_ip_and_port(sock_, ip, port); +} + inline socket_t SocketStream::socket() const { return sock_; } // Buffer stream implementation @@ -4833,6 +4853,9 @@ inline ssize_t BufferStream::write(const char *ptr, size_t size) { inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, int & /*port*/) const {} +inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + inline socket_t BufferStream::socket() const { return 0; } inline const std::string &BufferStream::get_buffer() const { return buffer; } @@ -5812,6 +5835,10 @@ Server::process_request(Stream &strm, bool close_connection, req.set_header("REMOTE_ADDR", req.remote_addr); req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); + strm.get_local_ip_and_port(req.local_addr, req.local_port); + req.set_header("LOCAL_ADDR", req.local_addr); + req.set_header("LOCAL_PORT", std::to_string(req.local_port)); + if (req.has_header("Range")) { const auto &range_header_value = req.get_header_value("Range"); if (!detail::parse_range_header(range_header_value, req.ranges)) { @@ -7409,6 +7436,11 @@ inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, detail::get_remote_ip_and_port(sock_, ip, port); } +inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + detail::get_local_ip_and_port(sock_, ip, port); +} + inline socket_t SSLSocketStream::socket() const { return sock_; } static SSLInit sslinit_; diff --git a/test/fuzzing/server_fuzzer.cc b/test/fuzzing/server_fuzzer.cc index 9fb4d4b61b..41f710f431 100644 --- a/test/fuzzing/server_fuzzer.cc +++ b/test/fuzzing/server_fuzzer.cc @@ -22,8 +22,6 @@ class FuzzedStream : public httplib::Stream { ssize_t write(const std::string &s) { return write(s.data(), s.size()); } - std::string get_remote_addr() const { return ""; } - bool is_readable() const override { return true; } bool is_writable() const override { return true; } @@ -33,6 +31,11 @@ class FuzzedStream : public httplib::Stream { port = 8080; } + void get_local_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 8080; + } + socket_t socket() const override { return 0; } private: diff --git a/test/test.cc b/test/test.cc index 9e665619da..10bec8dbb6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1521,6 +1521,17 @@ class ServerTest : public ::testing::Test { std::stoi(req.get_header_value("REMOTE_PORT"))); res.set_content(remote_addr.c_str(), "text/plain"); }) + .Get("/local_addr", + [&](const Request &req, Response &res) { + EXPECT_TRUE(req.has_header("LOCAL_PORT")); + EXPECT_TRUE(req.has_header("LOCAL_ADDR")); + auto local_addr = req.get_header_value("LOCAL_ADDR"); + auto local_port = req.get_header_value("LOCAL_PORT"); + EXPECT_EQ(req.local_addr, local_addr); + EXPECT_EQ(req.local_port, std::stoi(local_port)); + res.set_content(local_addr.append(":").append(local_port), + "text/plain"); + }) .Get("/endwith%", [&](const Request & /*req*/, Response &res) { res.set_content("Hello World!", "text/plain"); @@ -2810,6 +2821,15 @@ TEST_F(ServerTest, GetMethodRemoteAddr) { EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1"); } +TEST_F(ServerTest, GetMethodLocalAddr) { + auto res = cli_.Get("/local_addr"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_TRUE(res->body == std::string("::1:").append(to_string(PORT)) || + res->body == std::string("127.0.0.1:").append(to_string(PORT))); +} + TEST_F(ServerTest, HTTPResponseSplitting) { auto res = cli_.Get("/http_response_splitting"); ASSERT_TRUE(res); From 58cffd3223d11e93f7e639cf21d54cdb563d3543 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 10 Dec 2022 07:37:48 +0900 Subject: [PATCH 0019/1222] std::condition_variable::notify_one/all() should be called after unlocking mutex (#1448) * Move next job in task queue rather than copy * Notify waiting thread after unlocking mutex * Add unit test for TaskQueue * Don't use C++14 feature in test code --- httplib.h | 12 ++++++++---- test/test.cc | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index c7bf81092f..67fa1dd398 100644 --- a/httplib.h +++ b/httplib.h @@ -549,8 +549,11 @@ class ThreadPool : public TaskQueue { ~ThreadPool() override = default; void enqueue(std::function fn) override { - std::unique_lock lock(mutex_); - jobs_.push_back(std::move(fn)); + { + std::unique_lock lock(mutex_); + jobs_.push_back(std::move(fn)); + } + cond_.notify_one(); } @@ -559,9 +562,10 @@ class ThreadPool : public TaskQueue { { std::unique_lock lock(mutex_); shutdown_ = true; - cond_.notify_all(); } + cond_.notify_all(); + // Join... for (auto &t : threads_) { t.join(); @@ -583,7 +587,7 @@ class ThreadPool : public TaskQueue { if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } - fn = pool_.jobs_.front(); + fn = std::move(pool_.jobs_.front()); pool_.jobs_.pop_front(); } diff --git a/test/test.cc b/test/test.cc index 10bec8dbb6..32eb127602 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -5522,3 +5523,18 @@ TEST(SocketStream, is_writable_INET) { ASSERT_EQ(0, close(disconnected_svr_sock)); } #endif // #ifndef _WIN32 + +TEST(TaskQueueTest, IncreaseAtomicInteger) { + static constexpr unsigned int number_of_task{1000000}; + std::atomic_uint count{0}; + std::unique_ptr task_queue{ + new ThreadPool{CPPHTTPLIB_THREAD_POOL_COUNT}}; + + for (unsigned int i = 0; i < number_of_task; ++i) { + task_queue->enqueue( + [&count] { count.fetch_add(1, std::memory_order_relaxed); }); + } + + EXPECT_NO_THROW(task_queue->shutdown()); + EXPECT_EQ(number_of_task, count.load()); +} From 93e53c91f741d2393a12ba043366ac6f55f56cc7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 10 Dec 2022 11:45:56 -0500 Subject: [PATCH 0020/1222] Updated unit test --- test/test.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/test.cc b/test/test.cc index 32eb127602..23a695648b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1325,7 +1325,12 @@ TEST(NoContentTest, ContentLength) { } TEST(RoutingHandlerTest, PreRoutingHandler) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); +#else Server svr; +#endif svr.set_pre_routing_handler([](const Request &req, Response &res) { if (req.path == "/routing_handler") { @@ -1356,7 +1361,12 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { std::this_thread::sleep_for(std::chrono::seconds(1)); { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); +#else Client cli(HOST, PORT); +#endif auto res = cli.Get("/routing_handler"); ASSERT_TRUE(res); @@ -1369,7 +1379,12 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { } { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); +#else Client cli(HOST, PORT); +#endif auto res = cli.Get("/hi"); ASSERT_TRUE(res); @@ -1380,7 +1395,12 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { } { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); +#else Client cli(HOST, PORT); +#endif auto res = cli.Get("/aaa"); ASSERT_TRUE(res); From 227d2c20509f85a394133e2be6d0b0fc1fda54b2 Mon Sep 17 00:00:00 2001 From: yukun Date: Wed, 21 Dec 2022 08:34:51 +0800 Subject: [PATCH 0021/1222] Add EINTR and EAGAIN judge for accept (#1438) * Add EINTR and EAGAIN judge for accept * Add EINTR signal tests * Cancel win32 and win64 compile on signal unittest Co-authored-by: yukun.yu --- httplib.h | 2 ++ test/test.cc | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/httplib.h b/httplib.h index 67fa1dd398..751aa92b69 100644 --- a/httplib.h +++ b/httplib.h @@ -5507,6 +5507,8 @@ inline bool Server::listen_internal() { // Try to accept new connections after a short sleep. std::this_thread::sleep_for(std::chrono::milliseconds(1)); continue; + } else if (errno == EINTR || errno == EAGAIN) { + continue; } if (svr_sock_ != INVALID_SOCKET) { detail::close_socket(svr_sock_); diff --git a/test/test.cc b/test/test.cc index 23a695648b..88937ce852 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,4 +1,5 @@ #include +#include #include @@ -943,6 +944,44 @@ TEST(UrlWithSpace, Redirect_Online) { #endif +#if !defined(_WIN32) && !defined(_WIN64) +TEST(ReceiveSignals, Signal) { + auto setupSignalHandlers = []() { + struct sigaction act; + + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO; + act.sa_sigaction = [](int sig, siginfo_t *, void *) { + switch (sig) { + case SIGINT: + default: break; + } + }; + ::sigaction(SIGINT, &act, nullptr); + }; + + Server svr; + int port = 0; + auto thread = std::thread([&]() { + setupSignalHandlers(); + port = svr.bind_to_any_port("localhost"); + svr.listen_after_bind(); + }); + + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + ASSERT_TRUE(svr.is_running()); + pthread_kill(thread.native_handle(), SIGINT); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + ASSERT_TRUE(svr.is_running()); + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); +} +#endif + TEST(RedirectToDifferentPort, Redirect) { Server svr1; svr1.Get("/1", [&](const Request & /*req*/, Response &res) { From 7e420aeed361f89d041af997c9a89895043846d0 Mon Sep 17 00:00:00 2001 From: Ray Beck <115stingray@gmail.com> Date: Sun, 8 Jan 2023 18:38:14 -0500 Subject: [PATCH 0022/1222] add support for requests with both MultipartFormDataItems and Content Providers (#1454) * add support for requests with both MultipartFormDataItems and ContentProviders * rework implementation * use const auto & and fix offset calculation * fix zero items * snake case variables * clang-format * commonize get_multipart_content_provider, add Put() with MultipartFormDataProviderItems * fix linker multiple definition error * add test MultipartFormDataTest.DataProviderItems --- httplib.h | 189 +++++++++++++++++++++++++++++++++++++++--------- test/test.cc | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 355 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index 751aa92b69..1d0e628d77 100644 --- a/httplib.h +++ b/httplib.h @@ -369,6 +369,14 @@ using ContentProviderWithoutLength = using ContentProviderResourceReleaser = std::function; +struct MultipartFormDataProvider { + std::string name; + ContentProviderWithoutLength provider; + std::string filename; + std::string content_type; +}; +using MultipartFormDataProviderItems = std::vector; + using ContentReceiverWithProgress = std::function; @@ -934,6 +942,9 @@ class ClientImpl { const MultipartFormDataItems &items); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items, const std::string &boundary); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); Result Put(const std::string &path); Result Put(const std::string &path, const char *body, size_t content_length, @@ -963,6 +974,9 @@ class ClientImpl { const MultipartFormDataItems &items); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, @@ -1201,6 +1215,9 @@ class ClientImpl { ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, const std::string &content_type); + ContentProviderWithoutLength get_multipart_content_provider( + const std::string &boundary, const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); std::string adjust_host_string(const std::string &host) const; @@ -1296,6 +1313,10 @@ class Client { const MultipartFormDataItems &items); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items, const std::string &boundary); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + Result Put(const std::string &path); Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type); @@ -1324,6 +1345,10 @@ class Client { const MultipartFormDataItems &items); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type); @@ -2854,8 +2879,7 @@ inline socket_t create_client_socket( } inline bool get_ip_and_port(const struct sockaddr_storage &addr, - socklen_t addr_len, std::string &ip, - int &port) { + socklen_t addr_len, std::string &ip, int &port) { if (addr.ss_family == AF_INET) { port = ntohs(reinterpret_cast(&addr)->sin_port); } else if (addr.ss_family == AF_INET6) { @@ -4129,29 +4153,48 @@ inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { return valid; } +template +inline std::string +serialize_multipart_formdata_item_begin(const T &item, + const std::string &boundary) { + std::string body = "--" + boundary + "\r\n"; + body += "Content-Disposition: form-data; name=\"" + item.name + "\""; + if (!item.filename.empty()) { + body += "; filename=\"" + item.filename + "\""; + } + body += "\r\n"; + if (!item.content_type.empty()) { + body += "Content-Type: " + item.content_type + "\r\n"; + } + body += "\r\n"; + + return body; +} + +inline std::string serialize_multipart_formdata_item_end() { return "\r\n"; } + +inline std::string +serialize_multipart_formdata_finish(const std::string &boundary) { + return "--" + boundary + "--\r\n"; +} + +inline std::string +serialize_multipart_formdata_get_content_type(const std::string &boundary) { + return "multipart/form-data; boundary=" + boundary; +} + inline std::string serialize_multipart_formdata(const MultipartFormDataItems &items, - const std::string &boundary, - std::string &content_type) { + const std::string &boundary, bool finish = true) { std::string body; for (const auto &item : items) { - body += "--" + boundary + "\r\n"; - body += "Content-Disposition: form-data; name=\"" + item.name + "\""; - if (!item.filename.empty()) { - body += "; filename=\"" + item.filename + "\""; - } - body += "\r\n"; - if (!item.content_type.empty()) { - body += "Content-Type: " + item.content_type + "\r\n"; - } - body += "\r\n"; - body += item.content + "\r\n"; + body += serialize_multipart_formdata_item_begin(item, boundary); + body += item.content + serialize_multipart_formdata_item_end(); } - body += "--" + boundary + "--\r\n"; + if (finish) body += serialize_multipart_formdata_finish(boundary); - content_type = "multipart/form-data; boundary=" + boundary; return body; } @@ -4536,8 +4579,8 @@ inline void hosted_at(const std::string &hostname, *reinterpret_cast(rp->ai_addr); std::string ip; int dummy = -1; - if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), - ip, dummy)) { + if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, + dummy)) { addrs.push_back(ip); } } @@ -6647,6 +6690,49 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, return true; } +inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( + const std::string &boundary, const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + size_t cur_item = 0, cur_start = 0; + // cur_item and cur_start are copied to within the std::function and maintain + // state between successive calls + return [&, cur_item, cur_start](size_t offset, + DataSink &sink) mutable -> bool { + if (!offset && items.size()) { + sink.os << detail::serialize_multipart_formdata(items, boundary, false); + return true; + } else if (cur_item < provider_items.size()) { + if (!cur_start) { + const auto &begin = detail::serialize_multipart_formdata_item_begin( + provider_items[cur_item], boundary); + offset += begin.size(); + cur_start = offset; + sink.os << begin; + } + + DataSink cur_sink; + bool has_data = true; + cur_sink.write = sink.write; + cur_sink.done = [&]() { has_data = false; }; + cur_sink.is_writable = sink.is_writable; + + if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) + return false; + + if (!has_data) { + sink.os << detail::serialize_multipart_formdata_item_end(); + cur_item++; + cur_start = 0; + } + return true; + } else { + sink.os << detail::serialize_multipart_formdata_finish(boundary); + sink.done(); + return true; + } + }; +} + inline bool ClientImpl::process_socket(const Socket &socket, std::function callback) { @@ -6869,9 +6955,10 @@ inline Result ClientImpl::Post(const std::string &path, inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items) { - std::string content_type; - const auto &body = detail::serialize_multipart_formdata( - items, detail::make_multipart_data_boundary(), content_type); + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); return Post(path, headers, body, content_type.c_str()); } @@ -6882,12 +6969,25 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } - std::string content_type; - const auto &body = - detail::serialize_multipart_formdata(items, boundary, content_type); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); return Post(path, headers, body, content_type.c_str()); } +inline Result +ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "POST", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type); +} + inline Result ClientImpl::Put(const std::string &path) { return Put(path, std::string(), std::string()); } @@ -6964,9 +7064,10 @@ inline Result ClientImpl::Put(const std::string &path, inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items) { - std::string content_type; - const auto &body = detail::serialize_multipart_formdata( - items, detail::make_multipart_data_boundary(), content_type); + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); return Put(path, headers, body, content_type); } @@ -6977,12 +7078,24 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } - std::string content_type; - const auto &body = - detail::serialize_multipart_formdata(items, boundary, content_type); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); return Put(path, headers, body, content_type); } +inline Result +ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "PUT", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type); +} inline Result ClientImpl::Patch(const std::string &path) { return Patch(path, std::string(), std::string()); } @@ -7443,7 +7556,7 @@ inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, } inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, - int &port) const { + int &port) const { detail::get_local_ip_and_port(sock_, ip, port); } @@ -8147,6 +8260,12 @@ inline Result Client::Post(const std::string &path, const Headers &headers, const std::string &boundary) { return cli_->Post(path, headers, items, boundary); } +inline Result +Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + return cli_->Post(path, headers, items, provider_items); +} inline Result Client::Put(const std::string &path) { return cli_->Put(path); } inline Result Client::Put(const std::string &path, const char *body, size_t content_length, @@ -8210,6 +8329,12 @@ inline Result Client::Put(const std::string &path, const Headers &headers, const std::string &boundary) { return cli_->Put(path, headers, items, boundary); } +inline Result +Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + return cli_->Put(path, headers, items, provider_items); +} inline Result Client::Patch(const std::string &path) { return cli_->Patch(path); } diff --git a/test/test.cc b/test/test.cc index 88937ce852..e592ed1a5c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5146,6 +5146,204 @@ TEST(MultipartFormDataTest, LargeData) { t.join(); } +TEST(MultipartFormDataTest, DataProviderItems) { + + std::random_device seed_gen; + std::mt19937 random(seed_gen()); + + std::string rand1; + rand1.resize(1000); + std::generate(rand1.begin(), rand1.end(), [&]() { return random(); }); + + std::string rand2; + rand2.resize(3000); + std::generate(rand2.begin(), rand2.end(), [&]() { return random(); }); + + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + + svr.Post("/post-none", [&](const Request &req, Response & /*res*/, + const ContentReader &content_reader) { + ASSERT_FALSE(req.is_multipart_form_data()); + + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + EXPECT_EQ(body, ""); + }); + + svr.Post("/post-items", [&](const Request &req, Response & /*res*/, + const ContentReader &content_reader) { + ASSERT_TRUE(req.is_multipart_form_data()); + MultipartFormDataItems files; + content_reader( + [&](const MultipartFormData &file) { + files.push_back(file); + return true; + }, + [&](const char *data, size_t data_length) { + files.back().content.append(data, data_length); + return true; + }); + + ASSERT_TRUE(files.size() == 2); + + EXPECT_EQ(std::string(files[0].name), "name1"); + EXPECT_EQ(files[0].content, "Testing123"); + EXPECT_EQ(files[0].filename, "filename1"); + EXPECT_EQ(files[0].content_type, "application/octet-stream"); + + EXPECT_EQ(files[1].name, "name2"); + EXPECT_EQ(files[1].content, "Testing456"); + EXPECT_EQ(files[1].filename, ""); + EXPECT_EQ(files[1].content_type, ""); + }); + + svr.Post("/post-providers", [&](const Request &req, Response & /*res*/, + const ContentReader &content_reader) { + ASSERT_TRUE(req.is_multipart_form_data()); + MultipartFormDataItems files; + content_reader( + [&](const MultipartFormData &file) { + files.push_back(file); + return true; + }, + [&](const char *data, size_t data_length) { + files.back().content.append(data, data_length); + return true; + }); + + ASSERT_TRUE(files.size() == 2); + + EXPECT_EQ(files[0].name, "name3"); + EXPECT_EQ(files[0].content, rand1); + EXPECT_EQ(files[0].filename, "filename3"); + EXPECT_EQ(files[0].content_type, ""); + + EXPECT_EQ(files[1].name, "name4"); + EXPECT_EQ(files[1].content, rand2); + EXPECT_EQ(files[1].filename, "filename4"); + EXPECT_EQ(files[1].content_type, ""); + }); + + svr.Post("/post-both", [&](const Request &req, Response & /*res*/, + const ContentReader &content_reader) { + ASSERT_TRUE(req.is_multipart_form_data()); + MultipartFormDataItems files; + content_reader( + [&](const MultipartFormData &file) { + files.push_back(file); + return true; + }, + [&](const char *data, size_t data_length) { + files.back().content.append(data, data_length); + return true; + }); + + ASSERT_TRUE(files.size() == 4); + + EXPECT_EQ(std::string(files[0].name), "name1"); + EXPECT_EQ(files[0].content, "Testing123"); + EXPECT_EQ(files[0].filename, "filename1"); + EXPECT_EQ(files[0].content_type, "application/octet-stream"); + + EXPECT_EQ(files[1].name, "name2"); + EXPECT_EQ(files[1].content, "Testing456"); + EXPECT_EQ(files[1].filename, ""); + EXPECT_EQ(files[1].content_type, ""); + + EXPECT_EQ(files[2].name, "name3"); + EXPECT_EQ(files[2].content, rand1); + EXPECT_EQ(files[2].filename, "filename3"); + EXPECT_EQ(files[2].content_type, ""); + + EXPECT_EQ(files[3].name, "name4"); + EXPECT_EQ(files[3].content, rand2); + EXPECT_EQ(files[3].filename, "filename4"); + EXPECT_EQ(files[3].content_type, ""); + }); + + auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + std::this_thread::sleep_for(std::chrono::seconds(1)); + + { + Client cli("https://localhost:8080"); + cli.enable_server_certificate_verification(false); + + MultipartFormDataItems items{ + {"name1", "Testing123", "filename1", "application/octet-stream"}, + {"name2", "Testing456", "", ""}, // not a file + }; + + { + auto res = cli.Post("/post-none", {}, {}, {}); + ASSERT_TRUE(res); + ASSERT_EQ(200, res->status); + } + + MultipartFormDataProviderItems providers; + + { + auto res = + cli.Post("/post-items", {}, items, providers); // empty providers + ASSERT_TRUE(res); + ASSERT_EQ(200, res->status); + } + + providers.push_back({"name3", + [&](size_t offset, httplib::DataSink &sink) -> bool { + // test the offset is given correctly at each step + if (!offset) + sink.os.write(rand1.data(), 30); + else if (offset == 30) + sink.os.write(rand1.data() + 30, 300); + else if (offset == 330) + sink.os.write(rand1.data() + 330, 670); + else if (offset == rand1.size()) + sink.done(); + return true; + }, + "filename3", + {}}); + + providers.push_back({"name4", + [&](size_t offset, httplib::DataSink &sink) -> bool { + // test the offset is given correctly at each step + if (!offset) + sink.os.write(rand2.data(), 2000); + else if (offset == 2000) + sink.os.write(rand2.data() + 2000, 1); + else if (offset == 2001) + sink.os.write(rand2.data() + 2001, 999); + else if (offset == rand2.size()) + sink.done(); + return true; + }, + "filename4", + {}}); + + { + auto res = cli.Post("/post-providers", {}, {}, providers); + ASSERT_TRUE(res); + ASSERT_EQ(200, res->status); + } + + { + auto res = cli.Post("/post-both", {}, items, providers); + ASSERT_TRUE(res); + ASSERT_EQ(200, res->status); + } + } + + svr.stop(); + t.join(); +} + TEST(MultipartFormDataTest, WithPreamble) { Server svr; svr.Post("/post", [&](const Request & /*req*/, Response &res) { From 7992b148969fbf8093230b37a6a36c2f61135937 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Jan 2023 17:08:11 -0500 Subject: [PATCH 0023/1222] Release v0.11.4 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 1d0e628d77..7431dea6cd 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.11.3" +#define CPPHTTPLIB_VERSION "0.11.4" /* * Configuration From 51607ec7520af5c1450ad97b91bb71e0639f0d9f Mon Sep 17 00:00:00 2001 From: Ray Beck <115stingray@gmail.com> Date: Thu, 19 Jan 2023 07:01:34 -0500 Subject: [PATCH 0024/1222] add to_human_string (#1467) * add to_human_string * replace to_string with to_human_string * fix test --- httplib.h | 26 +++++++++++++------------- test/test.cc | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index 7431dea6cd..e4a1dbab1b 100644 --- a/httplib.h +++ b/httplib.h @@ -1665,20 +1665,20 @@ Server::set_idle_interval(const std::chrono::duration &duration) { inline std::string to_string(const Error error) { switch (error) { - case Error::Success: return "Success"; - case Error::Connection: return "Connection"; - case Error::BindIPAddress: return "BindIPAddress"; - case Error::Read: return "Read"; - case Error::Write: return "Write"; - case Error::ExceedRedirectCount: return "ExceedRedirectCount"; - case Error::Canceled: return "Canceled"; - case Error::SSLConnection: return "SSLConnection"; - case Error::SSLLoadingCerts: return "SSLLoadingCerts"; - case Error::SSLServerVerification: return "SSLServerVerification"; + case Error::Success: return "Success (no error)"; + case Error::Connection: return "Could not establish connection"; + case Error::BindIPAddress: return "Failed to bind IP address"; + case Error::Read: return "Failed to read connection"; + case Error::Write: return "Failed to write connection"; + case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; + case Error::Canceled: return "Connection handling canceled"; + case Error::SSLConnection: return "SSL connection failed"; + case Error::SSLLoadingCerts: return "SSL certificate loading failed"; + case Error::SSLServerVerification: return "SSL server verification failed"; case Error::UnsupportedMultipartBoundaryChars: - return "UnsupportedMultipartBoundaryChars"; - case Error::Compression: return "Compression"; - case Error::ConnectionTimeout: return "ConnectionTimeout"; + return "Unsupported HTTP multipart boundary characters"; + case Error::Compression: return "Compression failed"; + case Error::ConnectionTimeout: return "Connection timed out"; case Error::Unknown: return "Unknown"; default: break; } diff --git a/test/test.cc b/test/test.cc index e592ed1a5c..01fe2e5da9 100644 --- a/test/test.cc +++ b/test/test.cc @@ -596,7 +596,7 @@ TEST(ConnectionErrorTest, InvalidHostCheckResultErrorToString) { ASSERT_TRUE(!res); stringstream s; s << "error code: " << res.error(); - EXPECT_EQ("error code: Connection (2)", s.str()); + EXPECT_EQ("error code: Could not establish connection (2)", s.str()); } TEST(ConnectionErrorTest, InvalidPort) { From 0ff2e16d69a315990c7232be06a636dac3c467e5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 21 Jan 2023 01:07:21 -0500 Subject: [PATCH 0025/1222] Issue 52666: cpp-httplib:server_fuzzer: Timeout in server_fuzzer --- httplib.h | 3 ++ ...e-minimized-server_fuzzer-5042094968537088 | Bin test/test.cc | 43 ++++++++++++++++++ 3 files changed, 46 insertions(+) rename test/{ => fuzzing/corpus}/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 (100%) diff --git a/httplib.h b/httplib.h index e4a1dbab1b..3c01e16c17 100644 --- a/httplib.h +++ b/httplib.h @@ -3960,6 +3960,9 @@ class MultipartFormDataParser { if (std::regex_match(header, m, re_content_disposition)) { file_.name = m[1]; file_.filename = m[2]; + } else { + is_valid_ = false; + return false; } } buf_erase(pos + crlf_.size()); diff --git a/test/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 b/test/fuzzing/corpus/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 similarity index 100% rename from test/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 rename to test/fuzzing/corpus/clusterfuzz-testcase-minimized-server_fuzzer-5042094968537088 diff --git a/test/test.cc b/test/test.cc index 01fe2e5da9..2c5ad4f55d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5344,6 +5344,49 @@ TEST(MultipartFormDataTest, DataProviderItems) { t.join(); } +TEST(MultipartFormDataTest, BadHeader) { + Server svr; + svr.Post("/post", [&](const Request & /*req*/, Response &res) { + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + const std::string body = + "This is the preamble. It is to be ignored, though it\r\n" + "is a handy place for composition agents to include an\r\n" + "explanatory note to non-MIME conformant readers.\r\n" + "\r\n" + "\r\n" + "--simple boundary\r\n" + "Content-Disposition: form-data; name=\"field1\"\r\n" + ": BAD...\r\n" + "\r\n" + "value1\r\n" + "--simple boundary\r\n" + "Content-Disposition: form-data; name=\"field2\"; " + "filename=\"example.txt\"\r\n" + "\r\n" + "value2\r\n" + "--simple boundary--\r\n" + "This is the epilogue. It is also to be ignored.\r\n"; + + std::string content_type = + R"(multipart/form-data; boundary="simple boundary")"; + + Client cli(HOST, PORT); + auto res = cli.Post("/post", body, content_type.c_str()); + + ASSERT_TRUE(res); + EXPECT_EQ(400, res->status); + + svr.stop(); + t.join(); +} + TEST(MultipartFormDataTest, WithPreamble) { Server svr; svr.Post("/post", [&](const Request & /*req*/, Response &res) { From 20cba2ecd9009b0c1c1a32f9c47845a7d90d3075 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 21 Jan 2023 17:43:22 +0900 Subject: [PATCH 0026/1222] Support CTest (#1468) * Add test/CMakeLists.txt to enable testing with CTest Add HTTPLIB_TEST option Downlaod GoogleTest source using FetchContent module Generate cert files to test httplib with OpenSSL * Generate cert2.pem with a new certificate request * Use the latest GoogleTest library --- CMakeLists.txt | 8 +++- test/CMakeLists.txt | 93 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 709390a70b..e5e2ae3d0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_COMPILE (default off) + * HTTPLIB_TEST (default off) * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed). * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed). @@ -88,7 +89,7 @@ option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a c if(HTTPLIB_COMPILE) set(HTTPLIB_IS_COMPILED TRUE) endif() - +option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) # Defaults to static library @@ -278,3 +279,8 @@ install(EXPORT httplibTargets NAMESPACE ${PROJECT_NAME}:: DESTINATION ${_TARGET_INSTALL_CMAKEDIR} ) + +if(HTTPLIB_TEST) + include(CTest) + add_subdirectory(test) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000000..eb97912166 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,93 @@ +cmake_policy(SET CMP0135 NEW) + +include(FetchContent) +include(GoogleTest) + +set(BUILD_GMOCK OFF) +set(INSTALL_GTEST OFF) +set(gtest_force_shared_crt ON) + +FetchContent_Declare( + gtest + URL https://github.com/google/googletest/archive/main.tar.gz +) +FetchContent_MakeAvailable(gtest) + +add_executable(httplib-test test.cc) +target_compile_options(httplib-test PRIVATE "$<$:/utf-8>") +target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main) +gtest_discover_tests(httplib-test) + +execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www www + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www2 www2 + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www3 www3 + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_LIST_DIR}/ca-bundle.crt ca-bundle.crt + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_LIST_DIR}/image.jpg image.jpg + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY +) + +if(HTTPLIB_IS_USING_OPENSSL) + find_program(OPENSSL_COMMAND + NAMES openssl + PATHS ${OPENSSL_INCLUDE_DIR}/../bin + REQUIRED + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa 2048 + OUTPUT_FILE key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem + COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key.pem + OUTPUT_FILE cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -x509 -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa 2048 + OUTPUT_FILE rootCA.key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -x509 -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024 + OUTPUT_FILE rootCA.cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa 2048 + OUTPUT_FILE client.key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key client.key.pem + COMMAND ${OPENSSL_COMMAND} x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial + OUTPUT_FILE client.cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa -passout pass:test123! 2048 + OUTPUT_FILE key_encrypted.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key_encrypted.pem + COMMAND ${OPENSSL_COMMAND} x509 -days 3650 -req -signkey key_encrypted.pem + OUTPUT_FILE cert_encrypted.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) +endif() From c4ba43ca6f80b831487841e35dda5a8ee5cef19f Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jan 2023 09:07:42 -0500 Subject: [PATCH 0027/1222] Removed incorrect comment --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3c01e16c17..4dcfbac8ba 100644 --- a/httplib.h +++ b/httplib.h @@ -6381,7 +6381,7 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm, return detail::write_content(strm, req.content_provider_, 0, req.content_length_, is_shutting_down, error); } -} // namespace httplib +} inline bool ClientImpl::write_request(Stream &strm, Request &req, bool close_connection, Error &error) { From 439caf5b798bd07497e70265383c8bc4e2c448c5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 1 Feb 2023 19:11:11 -0500 Subject: [PATCH 0028/1222] Fix #1479 --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f6db7c0875..db7b162fec 100644 --- a/README.md +++ b/README.md @@ -493,6 +493,10 @@ auto res = cli.Get("/hi", headers); ``` or ```c++ +auto res = cli.Get("/hi", {{"Accept-Encoding", "gzip, deflate"}}); +``` +or +```c++ cli.set_default_headers({ { "Accept-Encoding", "gzip, deflate" } }); From d663588491a16a416c11a0102a7db37ebae5ad7b Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 4 Feb 2023 13:53:42 -0500 Subject: [PATCH 0029/1222] Removed is_writable() from DataSink (Resolve #1478, too) (#1483) --- example/ssesvr.cc | 2 +- httplib.h | 44 +++++++++++++++++++++++--------------------- test/test.cc | 9 --------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/example/ssesvr.cc b/example/ssesvr.cc index d18aed355e..5b0e0b93bf 100644 --- a/example/ssesvr.cc +++ b/example/ssesvr.cc @@ -19,7 +19,7 @@ class EventDispatcher { unique_lock lk(m_); int id = id_; cv_.wait(lk, [&] { return cid_ == id; }); - if (sink->is_writable()) { sink->write(message_.data(), message_.size()); } + sink->write(message_.data(), message_.size()); } void send_event(const string &message) { diff --git a/httplib.h b/httplib.h index 4dcfbac8ba..8ac01bc52f 100644 --- a/httplib.h +++ b/httplib.h @@ -340,7 +340,6 @@ class DataSink { std::function write; std::function done; - std::function is_writable; std::ostream os; private: @@ -3632,7 +3631,7 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, data_sink.write = [&](const char *d, size_t l) -> bool { if (ok) { - if (write_data(strm, d, l)) { + if (strm.is_writable() && write_data(strm, d, l)) { offset += l; } else { ok = false; @@ -3641,14 +3640,14 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, return ok; }; - data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; - while (offset < end_offset && !is_shutting_down()) { - if (!content_provider(offset, end_offset - offset, data_sink)) { + if (!strm.is_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, end_offset - offset, data_sink)) { error = Error::Canceled; return false; - } - if (!ok) { + } else if (!ok) { error = Error::Write; return false; } @@ -3680,18 +3679,21 @@ write_content_without_length(Stream &strm, data_sink.write = [&](const char *d, size_t l) -> bool { if (ok) { offset += l; - if (!write_data(strm, d, l)) { ok = false; } + if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; } } return ok; }; data_sink.done = [&](void) { data_available = false; }; - data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; - while (data_available && !is_shutting_down()) { - if (!content_provider(offset, 0, data_sink)) { return false; } - if (!ok) { return false; } + if (!strm.is_writable()) { + return false; + } else if (!content_provider(offset, 0, data_sink)) { + return false; + } else if (!ok) { + return false; + } } return true; } @@ -3720,7 +3722,10 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, // Emit chunked response header and footer for each chunk auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; } + if (!strm.is_writable() || + !write_data(strm, chunk.data(), chunk.size())) { + ok = false; + } } } else { ok = false; @@ -3759,14 +3764,14 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, } }; - data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; - while (data_available && !is_shutting_down()) { - if (!content_provider(offset, 0, data_sink)) { + if (!strm.is_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, 0, data_sink)) { error = Error::Canceled; return false; - } - if (!ok) { + } else if (!ok) { error = Error::Write; return false; } @@ -6544,8 +6549,6 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( return ok; }; - data_sink.is_writable = [&](void) { return ok && true; }; - while (ok && offset < content_length) { if (!content_provider(offset, content_length - offset, data_sink)) { error = Error::Canceled; @@ -6717,7 +6720,6 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( bool has_data = true; cur_sink.write = sink.write; cur_sink.done = [&]() { has_data = false; }; - cur_sink.is_writable = sink.is_writable; if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) return false; diff --git a/test/test.cc b/test/test.cc index 2c5ad4f55d..10ff9f847f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1650,7 +1650,6 @@ class ServerTest : public ::testing::Test { [&](const Request & /*req*/, Response &res) { res.set_chunked_content_provider( "text/plain", [](size_t /*offset*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); sink.os << "123"; sink.os << "456"; sink.os << "789"; @@ -1664,7 +1663,6 @@ class ServerTest : public ::testing::Test { res.set_chunked_content_provider( "text/plain", [i](size_t /*offset*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); switch (*i) { case 0: sink.os << "123"; break; case 1: sink.os << "456"; break; @@ -1694,7 +1692,6 @@ class ServerTest : public ::testing::Test { res.set_content_provider( data->size(), "text/plain", [data](size_t offset, size_t length, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); size_t DATA_CHUNK_SIZE = 4; const auto &d = *data; auto out_len = @@ -1714,8 +1711,6 @@ class ServerTest : public ::testing::Test { res.set_content_provider( size_t(-1), "text/plain", [](size_t /*offset*/, size_t /*length*/, DataSink &sink) { - if (!sink.is_writable()) return false; - sink.os << "data_chunk"; return true; }); @@ -2952,7 +2947,6 @@ TEST_F(ServerTest, PutWithContentProvider) { auto res = cli_.Put( "/put", 3, [](size_t /*offset*/, size_t /*length*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); sink.os << "PUT"; return true; }, @@ -2979,7 +2973,6 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLength) { auto res = cli_.Put( "/put", [](size_t /*offset*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); sink.os << "PUT"; sink.done(); return true; @@ -3006,7 +2999,6 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) { auto res = cli_.Put( "/put", 3, [](size_t /*offset*/, size_t /*length*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); sink.os << "PUT"; return true; }, @@ -3035,7 +3027,6 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) { auto res = cli_.Put( "/put", [](size_t /*offset*/, DataSink &sink) { - EXPECT_TRUE(sink.is_writable()); sink.os << "PUT"; sink.done(); return true; From 4e6ded1f36ec24d6428ab560f8c0ddfb653651e6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 7 Feb 2023 10:27:40 -0500 Subject: [PATCH 0030/1222] Release v0.12.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 8ac01bc52f..709ca2b1ad 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.11.4" +#define CPPHTTPLIB_VERSION "0.12.0" /* * Configuration From 0e7d2f9f939a5e6d54b8d5dad538dd322eb5e835 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 14 Feb 2023 11:40:07 -0500 Subject: [PATCH 0031/1222] Resolve #1482 --- httplib.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 709ca2b1ad..8ca935094a 100644 --- a/httplib.h +++ b/httplib.h @@ -5598,11 +5598,7 @@ inline bool Server::listen_internal() { #endif } -#if __cplusplus > 201703L - task_queue->enqueue([=, this]() { process_and_close_socket(sock); }); -#else - task_queue->enqueue([=]() { process_and_close_socket(sock); }); -#endif + task_queue->enqueue([this, sock]() { process_and_close_socket(sock); }); } task_queue->shutdown(); From 88f6245c840df1faa82e7782ddfb54bf53241d48 Mon Sep 17 00:00:00 2001 From: jingTian-z Date: Fri, 17 Feb 2023 10:51:06 +0800 Subject: [PATCH 0032/1222] feat: Add Request::get_file_multi_value func. (#1495) Support to get multiple values of a key. perf: Rename function names, variable names etc. --- httplib.h | 10 ++++++++++ test/test.cc | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/httplib.h b/httplib.h index 8ca935094a..528c838b47 100644 --- a/httplib.h +++ b/httplib.h @@ -454,6 +454,7 @@ struct Request { bool has_file(const std::string &key) const; MultipartFormData get_file_value(const std::string &key) const; + std::vector get_file_values(const std::string &key) const; // private members... size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; @@ -4690,6 +4691,15 @@ inline MultipartFormData Request::get_file_value(const std::string &key) const { return MultipartFormData(); } +inline std::vector Request::get_file_values(const std::string &key) const { + std::vector values; + auto rng = files.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second); + } + return values; +} + // Response implementation inline bool Response::has_header(const std::string &key) const { return headers.find(key) != headers.end(); diff --git a/test/test.cc b/test/test.cc index 10ff9f847f..83d7997e7b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1767,6 +1767,40 @@ class ServerTest : public ::testing::Test { EXPECT_EQ("application/json tmp-string", file.content_type); } }) + .Post("/multipart/multi_file_values", + [&](const Request &req, Response & /*res*/) { + EXPECT_EQ(5u, req.files.size()); + ASSERT_TRUE(!req.has_file("???")); + ASSERT_TRUE(req.body.empty()); + + { + const auto &text_value = req.get_file_values("text"); + EXPECT_EQ(text_value.size(), 1); + auto &text = text_value[0]; + EXPECT_TRUE(text.filename.empty()); + EXPECT_EQ("defalut text", text.content); + } + { + const auto &text1_values = req.get_file_values("multi_text1"); + EXPECT_EQ(text1_values.size(), 2); + EXPECT_EQ("aaaaa", text1_values[0].content); + EXPECT_EQ("bbbbb", text1_values[1].content); + } + + { + const auto &file1_values = req.get_file_values("multi_file1"); + EXPECT_EQ(file1_values.size(), 2); + auto file1 = file1_values[0]; + EXPECT_EQ(file1.filename, "hello.txt"); + EXPECT_EQ(file1.content_type, "text/plain"); + EXPECT_EQ("h\ne\n\nl\nl\no\n", file1.content); + + auto file2 = file1_values[1]; + EXPECT_EQ(file2.filename, "world.json"); + EXPECT_EQ(file2.content_type, "application/json"); + EXPECT_EQ("{\n \"world\", true\n}\n", file2.content); + } + }) .Post("/empty", [&](const Request &req, Response &res) { EXPECT_EQ(req.body, ""); @@ -2611,6 +2645,23 @@ TEST_F(ServerTest, MultipartFormData) { EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, MultipartFormDataMultiFileValues) { + MultipartFormDataItems items = { + {"text", "defalut text", "", ""}, + + {"multi_text1", "aaaaa", "", ""}, + {"multi_text1", "bbbbb", "", ""}, + + {"multi_file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, + {"multi_file1", "{\n \"world\", true\n}\n", "world.json", "application/json"}, + }; + + auto res = cli_.Post("/multipart/multi_file_values", items); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); +} + TEST_F(ServerTest, CaseInsensitiveHeaderName) { auto res = cli_.Get("/hi"); ASSERT_TRUE(res); From 6d963fbe8d415399d65e94db7910bbd22fe3741c Mon Sep 17 00:00:00 2001 From: Sergey Kazmin <43613813+yerseg@users.noreply.github.com> Date: Fri, 17 Feb 2023 20:06:55 +0300 Subject: [PATCH 0033/1222] Support loading system certs from Keychein on MacOS (#1474) * Support loading system certs from Keychein on MacOS * review improvements: add deps to meson.build and improve conditional expressions in cmake * fix tabs * fix tabs * review improvements * fix after review * additionally load root certs from the system root keychain * cmake fix * fix * small refactoring * small refactoring --------- Co-authored-by: Sergey Kazmin --- CMakeLists.txt | 2 + httplib.h | 109 ++++++++++++++++++++++++++++++++++++++++++++++--- meson.build | 3 ++ test/Makefile | 7 ++++ 4 files changed, 115 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5e2ae3d0b..618fb5cdd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -206,6 +206,8 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:ws2_32> $<$:crypt32> $<$:cryptui> + # Needed for API from MacOS Security framework + "$<$,$>:-framework CoreFoundation -framework Security>" # Can't put multiple targets in a single generator expression or it bugs out. $<$:Brotli::common> $<$:Brotli::encoder> diff --git a/httplib.h b/httplib.h index 528c838b47..8266ec8b80 100644 --- a/httplib.h +++ b/httplib.h @@ -239,7 +239,10 @@ using socket_t = int; #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "cryptui.lib") #endif -#endif //_WIN32 +#elif defined(__APPLE__) // _WIN32 +#include +#include +#endif // __APPLE__ #include #include @@ -4388,15 +4391,15 @@ inline std::string SHA_512(const std::string &s) { } #endif -#ifdef _WIN32 #ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef _WIN32 // NOTE: This code came up with the following stackoverflow post: // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store inline bool load_system_certs_on_windows(X509_STORE *store) { auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); - if (!hStore) { return false; } + auto result = false; PCCERT_CONTEXT pContext = NULL; while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != nullptr) { @@ -4407,16 +4410,107 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { if (x509) { X509_STORE_add_cert(store, x509); X509_free(x509); + result = true; } } CertFreeCertificateContext(pContext); CertCloseStore(hStore, 0); + return result; +} +#elif defined(__APPLE__) +template +using CFObjectPtr = + std::unique_ptr::type, void (*)(CFTypeRef)>; + +inline void cf_object_ptr_deleter(CFTypeRef obj) { + if (obj) { CFRelease(obj); } +} + +inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { + CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; + CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, + kCFBooleanTrue}; + + CFObjectPtr query( + CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, + sizeof(keys) / sizeof(keys[0]), + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks), + cf_object_ptr_deleter); + + if (!query) { return false; } + + CFTypeRef security_items = nullptr; + if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || + CFArrayGetTypeID() != CFGetTypeID(security_items)) { + return false; + } + + certs.reset(reinterpret_cast(security_items)); + return true; +} + +inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { + CFArrayRef root_security_items = nullptr; + if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { + return false; + } + + certs.reset(root_security_items); return true; } + +inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { + auto result = false; + for (int i = 0; i < CFArrayGetCount(certs); ++i) { + const auto cert = reinterpret_cast( + CFArrayGetValueAtIndex(certs, i)); + + if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } + + CFDataRef cert_data = nullptr; + if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != + errSecSuccess) { + continue; + } + + CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); + + auto encoded_cert = static_cast( + CFDataGetBytePtr(cert_data_ptr.get())); + + auto x509 = + d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); + + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + return result; +} + +inline bool load_system_certs_on_apple(X509_STORE *store) { + auto result = false; + CFObjectPtr certs(nullptr, cf_object_ptr_deleter); + if (retrieve_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store); + } + + if (retrieve_root_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store) || result; + } + + return result; +} +#endif #endif +#ifdef _WIN32 class WSInit { public: WSInit() { @@ -7842,11 +7936,14 @@ inline bool SSLClient::load_certs() { ret = false; } } else { + auto loaded = false; #ifdef _WIN32 - detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#else - SSL_CTX_set_default_verify_paths(ctx_); + loaded = + detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); +#elif defined(__APPLE__) + loaded = detail::load_system_certs_on_apple(SSL_CTX_get_cert_store(ctx_)); #endif + if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } } }); diff --git a/meson.build b/meson.build index c879e25681..cc81eb1166 100644 --- a/meson.build +++ b/meson.build @@ -34,6 +34,9 @@ openssl_dep = dependency('openssl', version: '>=1.1.1', required: get_option('cp if openssl_dep.found() deps += openssl_dep args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' + if host_machine.system() == 'darwin' + deps += dependency('appleframeworks', modules: ['CoreFoundation', 'Security']) + endif endif zlib_dep = dependency('zlib', required: get_option('cpp-httplib_zlib')) diff --git a/test/Makefile b/test/Makefile index 4e72360600..cb7605c59c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,6 +8,13 @@ OPENSSL_DIR = $(PREFIX)/opt/openssl@1.1 #OPENSSL_DIR = $(PREFIX)/opt/openssl@3 OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto +ifneq ($(OS), Windows_NT) + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Darwin) + OPENSSL_SUPPORT += -framework CoreFoundation -framework Security + endif +endif + ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli From ae3a6dd2a9d1e2209afb9107a24ea6b9a41cb520 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 21 Feb 2023 22:00:10 -0500 Subject: [PATCH 0034/1222] Fixed an issue with example/Makefile on MacOS --- example/Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/example/Makefile b/example/Makefile index f37cc2464b..76366db235 100644 --- a/example/Makefile +++ b/example/Makefile @@ -8,12 +8,19 @@ OPENSSL_DIR = $(PREFIX)/opt/openssl@1.1 #OPENSSL_DIR = $(PREFIX)/opt/openssl@3 OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto +ifneq ($(OS), Windows_NT) + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Darwin) + OPENSSL_SUPPORT += -framework CoreFoundation -framework Security + endif +endif + ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark issue server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) From 75053bf8558a1ebfd3a249d9b9c55ad11fe09b31 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 22 Feb 2023 13:19:36 -0500 Subject: [PATCH 0035/1222] Fix #1498 (#1501) * Fix #1498 * Fixed build error --- httplib.h | 57 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 8266ec8b80..9bf9d64cd1 100644 --- a/httplib.h +++ b/httplib.h @@ -311,6 +311,34 @@ struct ci { } }; +// This is based on +// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". + +template struct scope_exit { + explicit scope_exit(EF &&f) + : exit_function(std::move(f)), execute_on_destruction{true} {} + + scope_exit(scope_exit &&rhs) + : exit_function(std::move(rhs.exit_function)), + execute_on_destruction{rhs.execute_on_destruction} { + rhs.release(); + } + + ~scope_exit() { + if (execute_on_destruction) { this->exit_function(); } + } + + void release() { this->execute_on_destruction = false; } + +private: + scope_exit(const scope_exit &) = delete; + void operator=(const scope_exit &) = delete; + scope_exit &operator=(scope_exit &&) = delete; + + EF exit_function; + bool execute_on_destruction; +}; + } // namespace detail using Headers = std::multimap; @@ -4785,13 +4813,14 @@ inline MultipartFormData Request::get_file_value(const std::string &key) const { return MultipartFormData(); } -inline std::vector Request::get_file_values(const std::string &key) const { - std::vector values; - auto rng = files.equal_range(key); - for (auto it = rng.first; it != rng.second; it++) { - values.push_back(it->second); - } - return values; +inline std::vector +Request::get_file_values(const std::string &key) const { + std::vector values; + auto rng = files.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second); + } + return values; } // Response implementation @@ -6307,13 +6336,11 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { } } + auto ret = false; auto close_connection = !keep_alive_; - auto ret = process_socket(socket_, [&](Stream &strm) { - return handle_request(strm, req, res, close_connection, error); - }); - // Briefly lock mutex in order to mark that a request is no longer ongoing - { + auto se = detail::scope_exit>([&]() { + // Briefly lock mutex in order to mark that a request is no longer ongoing std::lock_guard guard(socket_mutex_); socket_requests_in_flight_ -= 1; if (socket_requests_in_flight_ <= 0) { @@ -6327,7 +6354,11 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { shutdown_socket(socket_); close_socket(socket_); } - } + }); + + ret = process_socket(socket_, [&](Stream &strm) { + return handle_request(strm, req, res, close_connection, error); + }); if (!ret) { if (error == Error::Success) { error = Error::Unknown; } From 016838fd101f068b9dcf7ce2ebf3be1bcc534416 Mon Sep 17 00:00:00 2001 From: Alexandre Bouvier Date: Sat, 25 Feb 2023 15:56:56 +0100 Subject: [PATCH 0036/1222] cmake: support components (#1504) --- CMakeLists.txt | 6 ++---- httplibConfig.cmake.in | 13 +++++++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 618fb5cdd6..9dcf738adc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,8 @@ ------------------------------------------------------------------------------- - After installation with Cmake, a find_package(httplib) is available. - This creates a httplib::httplib target (if found). + After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL ZLIB Brotli) is available. + This creates a httplib::httplib target (if found and if listed components are supported). It can be linked like so: target_link_libraries(your_exe httplib::httplib) @@ -235,8 +235,6 @@ configure_package_config_file("${PROJECT_NAME}Config.cmake.in" INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}" # Passes the includedir install path PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR - # There aren't any components, so don't use the macro - NO_CHECK_REQUIRED_COMPONENTS_MACRO ) if(HTTPLIB_COMPILE) diff --git a/httplibConfig.cmake.in b/httplibConfig.cmake.in index 4cd5ebf72e..b1345235f1 100644 --- a/httplibConfig.cmake.in +++ b/httplibConfig.cmake.in @@ -42,8 +42,17 @@ set_and_check(HTTPLIB_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@") # This is helpful if you're using Cmake's pre-compiled header feature set_and_check(HTTPLIB_HEADER_PATH "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@/httplib.h") -# Brings in the target library -include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake") +# Consider each library support as a "component" +set(httplib_OpenSSL_FOUND @HTTPLIB_IS_USING_OPENSSL@) +set(httplib_ZLIB_FOUND @HTTPLIB_IS_USING_ZLIB@) +set(httplib_Brotli_FOUND @HTTPLIB_IS_USING_BROTLI@) + +check_required_components(httplib) + +# Brings in the target library, but only if all required components are found +if(NOT DEFINED httplib_FOUND OR httplib_FOUND) + include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake") +endif() # Ouputs a "found httplib /usr/include/httplib.h" message when using find_package(httplib) include(FindPackageMessage) From bab5c0e907f84d0af2ed038eca6b9fdbc5af314e Mon Sep 17 00:00:00 2001 From: Alexandre Bouvier Date: Wed, 1 Mar 2023 14:38:58 +0100 Subject: [PATCH 0037/1222] cmake: fix find_dependency (#1509) --- httplibConfig.cmake.in | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/httplibConfig.cmake.in b/httplibConfig.cmake.in index b1345235f1..fa473ac746 100644 --- a/httplibConfig.cmake.in +++ b/httplibConfig.cmake.in @@ -12,19 +12,19 @@ set(HTTPLIB_VERSION @PROJECT_VERSION@) include(CMakeFindDependencyMacro) # We add find_dependency calls here to not make the end-user have to call them. -find_dependency(Threads REQUIRED) +find_dependency(Threads) if(@HTTPLIB_IS_USING_OPENSSL@) # OpenSSL COMPONENTS were added in Cmake v3.11 if(CMAKE_VERSION VERSION_LESS "3.11") - find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ REQUIRED) + find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@) else() # Once the COMPONENTS were added, they were made optional when not specified. # Since we use both, we need to search for both. - find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ COMPONENTS Crypto SSL REQUIRED) + find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ COMPONENTS Crypto SSL) endif() endif() if(@HTTPLIB_IS_USING_ZLIB@) - find_dependency(ZLIB REQUIRED) + find_dependency(ZLIB) endif() if(@HTTPLIB_IS_USING_BROTLI@) @@ -32,7 +32,7 @@ if(@HTTPLIB_IS_USING_BROTLI@) # Note that the FindBrotli.cmake file is installed in the same dir as this file. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") set(BROTLI_USE_STATIC_LIBS @BROTLI_USE_STATIC_LIBS@) - find_dependency(Brotli COMPONENTS common encoder decoder REQUIRED) + find_dependency(Brotli COMPONENTS common encoder decoder) endif() # Mildly useful for end-users From cdaa5c48dbbcb6e44420869587106ee692b85d2b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 3 Mar 2023 22:41:57 -0500 Subject: [PATCH 0038/1222] Code cleanup --- httplib.h | 4 ++-- test/test.cc | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index 9bf9d64cd1..ca29cc2f76 100644 --- a/httplib.h +++ b/httplib.h @@ -6309,7 +6309,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { if (is_ssl()) { auto &scli = static_cast(*this); if (!proxy_host_.empty() && proxy_port_ != -1) { - bool success = false; + auto success = false; if (!scli.connect_with_proxy(socket_, res, success, error)) { return success; } @@ -7789,7 +7789,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { }, [](SSL * /*ssl2*/) { return true; }); - bool ret = false; + auto ret = false; if (ssl) { ret = detail::process_server_socket_ssl( svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, diff --git a/test/test.cc b/test/test.cc index 83d7997e7b..298aa8e98e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4026,7 +4026,7 @@ TEST(KeepAliveTest, ReadTimeout) { cli.set_read_timeout(std::chrono::seconds(1)); auto resa = cli.Get("/a"); - ASSERT_TRUE(!resa); + ASSERT_FALSE(resa); EXPECT_EQ(Error::Read, resa.error()); auto resb = cli.Get("/b"); @@ -4040,33 +4040,31 @@ TEST(KeepAliveTest, ReadTimeout) { } TEST(KeepAliveTest, Issue1041) { - const auto resourcePath = "/hi"; - Server svr; svr.set_keep_alive_timeout(3); - svr.Get(resourcePath, [](const httplib::Request &, httplib::Response &res) { + svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) { res.set_content("Hello World!", "text/plain"); }); - auto a2 = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); + auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); std::this_thread::sleep_for(std::chrono::milliseconds(200)); Client cli(HOST, PORT); cli.set_keep_alive(true); - auto result = cli.Get(resourcePath); + auto result = cli.Get("/hi"); ASSERT_TRUE(result); EXPECT_EQ(200, result->status); std::this_thread::sleep_for(std::chrono::seconds(5)); - result = cli.Get(resourcePath); + result = cli.Get("/hi"); ASSERT_TRUE(result); EXPECT_EQ(200, result->status); svr.stop(); - a2.wait(); + f.wait(); } TEST(ClientProblemDetectionTest, ContentProvider) { From ba5884e779c77db7bda3ad26e4d7820ea724464c Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 3 Mar 2023 23:45:19 -0500 Subject: [PATCH 0039/1222] Fix #1481 (#1513) --- httplib.h | 9 +++++++- test/test.cc | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ca29cc2f76..1b7f5f84a7 100644 --- a/httplib.h +++ b/httplib.h @@ -6289,6 +6289,13 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { auto is_alive = false; if (socket_.is_open()) { is_alive = detail::is_socket_alive(socket_.sock); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl() && is_alive) { + char buf[1]; + auto n = SSL_peek(socket_.ssl, buf, 1); + if (n <= 0) { is_alive = false; } + } +#endif if (!is_alive) { // Attempt to avoid sigpipe by shutting down nongracefully if it seems // like the other side has already closed the connection Also, there @@ -6339,7 +6346,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { auto ret = false; auto close_connection = !keep_alive_; - auto se = detail::scope_exit>([&]() { + auto se = detail::scope_exit>([&]() { // Briefly lock mutex in order to mark that a request is no longer ongoing std::lock_guard guard(socket_mutex_); socket_requests_in_flight_ -= 1; diff --git a/test/test.cc b/test/test.cc index 298aa8e98e..9446a708e4 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3871,6 +3871,32 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { ASSERT_FALSE(svr.is_running()); } +TEST(ServerStopTest, ClientAccessAfterServerDown) { + httplib::Server svr; + svr.Post("/hi", [&](const httplib::Request & /*req*/, httplib::Response &res) { + res.status = 200; + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + + Client cli(HOST, PORT); + + auto res = cli.Post("/hi", "data", "text/plain"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + + res = cli.Post("/hi", "data", "text/plain"); + ASSERT_FALSE(res); +} + TEST(StreamingTest, NoContentLengthStreaming) { Server svr; @@ -4067,6 +4093,39 @@ TEST(KeepAliveTest, Issue1041) { f.wait(); } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(KeepAliveTest, SSLClientReconnection) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + svr.set_keep_alive_timeout(1); + + svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_keep_alive(true); + + auto result = cli.Get("/hi"); + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); + + std::this_thread::sleep_for(std::chrono::seconds(2)); + + result = cli.Get("/hi"); + + svr.stop(); + f.wait(); + + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); +} +#endif + TEST(ClientProblemDetectionTest, ContentProvider) { Server svr; From c7e959a9489bd37fe7d796e679e48894f272e22a Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 4 Mar 2023 18:08:18 -0500 Subject: [PATCH 0040/1222] Fix #1481 --- httplib.h | 33 ++++++++++++++++++++++++--------- test/test.cc | 14 +++++++++++--- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index 1b7f5f84a7..2317276c6e 100644 --- a/httplib.h +++ b/httplib.h @@ -854,6 +854,9 @@ enum class Error { UnsupportedMultipartBoundaryChars, Compression, ConnectionTimeout, + + // For internal use only + SSLPeerCouldBeClosed_, }; std::string to_string(const Error error); @@ -1126,8 +1129,6 @@ class ClientImpl { bool is_open() const { return sock != INVALID_SOCKET; } }; - Result send_(Request &&req); - virtual bool create_and_connect_socket(Socket &socket, Error &error); // All of: @@ -1228,6 +1229,9 @@ class ClientImpl { Logger logger_; private: + bool send_(Request &req, Response &res, Error &error); + Result send_(Request &&req); + socket_t create_client_socket(Error &error) const; bool read_response_line(Stream &strm, const Request &req, Response &res); bool write_request(Stream &strm, Request &req, bool close_connection, @@ -6278,7 +6282,15 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, inline bool ClientImpl::send(Request &req, Response &res, Error &error) { std::lock_guard request_mutex_guard(request_mutex_); + auto ret = send_(req, res, error); + if (error == Error::SSLPeerCouldBeClosed_) { + assert(!ret); + ret = send_(req, res, error); + } + return ret; +} +inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { { std::lock_guard guard(socket_mutex_); @@ -6289,13 +6301,6 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { auto is_alive = false; if (socket_.is_open()) { is_alive = detail::is_socket_alive(socket_.sock); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl() && is_alive) { - char buf[1]; - auto n = SSL_peek(socket_.ssl, buf, 1); - if (n <= 0) { is_alive = false; } - } -#endif if (!is_alive) { // Attempt to avoid sigpipe by shutting down nongracefully if it seems // like the other side has already closed the connection Also, there @@ -6757,6 +6762,16 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, // Send request if (!write_request(strm, req, close_connection, error)) { return false; } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl()) { + char buf[1]; + if (SSL_peek(socket_.ssl, buf, 1) == 0) { + error = Error::SSLPeerCouldBeClosed_; + return false; + } + } +#endif + // Receive response and headers if (!read_response_line(strm, req, res) || !detail::read_headers(strm, res.headers)) { diff --git a/test/test.cc b/test/test.cc index 9446a708e4..02279eac5e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4114,15 +4114,23 @@ TEST(KeepAliveTest, SSLClientReconnection) { ASSERT_TRUE(result); EXPECT_EQ(200, result->status); + result = cli.Get("/hi"); + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); + std::this_thread::sleep_for(std::chrono::seconds(2)); + // Recoonect result = cli.Get("/hi"); + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); - svr.stop(); - f.wait(); - + result = cli.Get("/hi"); ASSERT_TRUE(result); EXPECT_EQ(200, result->status); + + svr.stop(); + f.wait(); } #endif From 7b69999c37368ceaf41e79fa7d3b05ab4bd1e8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= Date: Mon, 6 Mar 2023 03:35:35 +0100 Subject: [PATCH 0041/1222] Reuse gtest on system (#1493) * Reuse gtest on the system Try to use gtest on the system if found. Avoid using recent CMake features. * Set CMP0135 only when using FetchContent * Add /bigobj option for MSVC * Support also cmake between 3.14 and 3.20 Older versions provided only GTest::Main target, not currently used gtest_main. Provide backward compatibility also to old cmake versions. * Remove redundant variable checking --------- Co-authored-by: Jiwoo Park --- test/CMakeLists.txt | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index eb97912166..60e410c100 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,20 +1,31 @@ -cmake_policy(SET CMP0135 NEW) +find_package(GTest) -include(FetchContent) -include(GoogleTest) +if(GTest_FOUND) + if(NOT TARGET GTest::gtest_main AND TARGET GTest::Main) + # CMake <3.20 + add_library(GTest::gtest_main INTERFACE IMPORTED) + target_link_libraries(GTest::gtest_main INTERFACE GTest::Main) + endif() +else() + if(POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) + endif() -set(BUILD_GMOCK OFF) -set(INSTALL_GTEST OFF) -set(gtest_force_shared_crt ON) + include(FetchContent) -FetchContent_Declare( - gtest - URL https://github.com/google/googletest/archive/main.tar.gz -) -FetchContent_MakeAvailable(gtest) + set(BUILD_GMOCK OFF) + set(INSTALL_GTEST OFF) + set(gtest_force_shared_crt ON) + + FetchContent_Declare( + gtest + URL https://github.com/google/googletest/archive/main.tar.gz + ) + FetchContent_MakeAvailable(gtest) +endif() add_executable(httplib-test test.cc) -target_compile_options(httplib-test PRIVATE "$<$:/utf-8>") +target_compile_options(httplib-test PRIVATE "$<$:/utf-8;/bigobj>") target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main) gtest_discover_tests(httplib-test) From 1ebb8412c594a091750f1b614d6aae3964c0db44 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 7 Mar 2023 08:15:16 -0500 Subject: [PATCH 0042/1222] Use SSL_ERROR_ZERO_RETURN to check if the SSL peer is closed. --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 2317276c6e..699c1fb4a1 100644 --- a/httplib.h +++ b/httplib.h @@ -6765,7 +6765,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_ssl()) { char buf[1]; - if (SSL_peek(socket_.ssl, buf, 1) == 0) { + if (SSL_peek(socket_.ssl, buf, 1) == 0 && + SSL_get_error(socket_.ssl, 0) == SSL_ERROR_ZERO_RETURN) { error = Error::SSLPeerCouldBeClosed_; return false; } From 9f7ae0737a9a0afd58808a98603fb8f33d15a9c7 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 8 Mar 2023 23:03:20 +0100 Subject: [PATCH 0043/1222] Fix typos (#1517) --- CMakeLists.txt | 2 +- httplib.h | 20 +++++++++---------- httplibConfig.cmake.in | 2 +- .../fuzzing/standalone_fuzz_target_runner.cpp | 2 +- test/test.cc | 18 ++++++++--------- test/test_proxy.cc | 2 +- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dcf738adc..c98daf5edc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,7 +79,7 @@ set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1") # Allow for a build to require OpenSSL to pass, instead of just being optional option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF) option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF) -# Allow for a build to casually enable OpenSSL/ZLIB support, but silenty continue if not found. +# Allow for a build to casually enable OpenSSL/ZLIB support, but silently continue if not found. # Make these options so their automatic use can be specifically disabled (as needed) option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable HTTPS support." ON) option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON) diff --git a/httplib.h b/httplib.h index 699c1fb4a1..cc39270d35 100644 --- a/httplib.h +++ b/httplib.h @@ -800,7 +800,7 @@ class Server { ContentReceiver multipart_receiver); bool read_content_core(Stream &strm, Request &req, Response &res, ContentReceiver receiver, - MultipartContentHeader mulitpart_header, + MultipartContentHeader multipart_header, ContentReceiver multipart_receiver); virtual bool process_and_close_socket(socket_t sock); @@ -1150,7 +1150,7 @@ class ClientImpl { void copy_settings(const ClientImpl &rhs); - // Socket endoint information + // Socket endpoint information const std::string host_; const int port_; const std::string host_and_port_; @@ -5542,7 +5542,7 @@ inline bool Server::read_content_with_content_receiver( inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, ContentReceiver receiver, - MultipartContentHeader mulitpart_header, + MultipartContentHeader multipart_header, ContentReceiver multipart_receiver) { detail::MultipartFormDataParser multipart_form_data_parser; ContentReceiverWithProgress out; @@ -5562,14 +5562,14 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, while (pos < n) { auto read_size = (std::min)(1, n - pos); auto ret = multipart_form_data_parser.parse( - buf + pos, read_size, multipart_receiver, mulitpart_header); + buf + pos, read_size, multipart_receiver, multipart_header); if (!ret) { return false; } pos += read_size; } return true; */ return multipart_form_data_parser.parse(buf, n, multipart_receiver, - mulitpart_header); + multipart_header); }; } else { out = [receiver](const char *buf, size_t n, uint64_t /*off*/, @@ -6512,7 +6512,7 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm, auto is_shutting_down = []() { return false; }; if (req.is_chunked_content_provider_) { - // TODO: Brotli suport + // TODO: Brotli support std::unique_ptr compressor; #ifdef CPPHTTPLIB_ZLIB_SUPPORT if (compress_) { @@ -7399,7 +7399,7 @@ inline void ClientImpl::stop() { return; } - // Otherwise, sitll holding the mutex, we can shut everything down ourselves + // Otherwise, still holding the mutex, we can shut everything down ourselves shutdown_ssl(socket_, true); shutdown_socket(socket_); close_socket(socket_); @@ -8142,7 +8142,7 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { if (alt_names) { auto dsn_matched = false; - auto ip_mached = false; + auto ip_matched = false; auto count = sk_GENERAL_NAME_num(alt_names); @@ -8158,14 +8158,14 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { case GEN_IPADD: if (!memcmp(&addr6, name, addr_len) || !memcmp(&addr, name, addr_len)) { - ip_mached = true; + ip_matched = true; } break; } } } - if (dsn_matched || ip_mached) { ret = true; } + if (dsn_matched || ip_matched) { ret = true; } } GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names); diff --git a/httplibConfig.cmake.in b/httplibConfig.cmake.in index fa473ac746..93dff323de 100644 --- a/httplibConfig.cmake.in +++ b/httplibConfig.cmake.in @@ -54,7 +54,7 @@ if(NOT DEFINED httplib_FOUND OR httplib_FOUND) include("${CMAKE_CURRENT_LIST_DIR}/httplibTargets.cmake") endif() -# Ouputs a "found httplib /usr/include/httplib.h" message when using find_package(httplib) +# Outputs a "found httplib /usr/include/httplib.h" message when using find_package(httplib) include(FindPackageMessage) if(TARGET httplib::httplib) set(HTTPLIB_FOUND TRUE) diff --git a/test/fuzzing/standalone_fuzz_target_runner.cpp b/test/fuzzing/standalone_fuzz_target_runner.cpp index 733737bcfa..945ce175ef 100644 --- a/test/fuzzing/standalone_fuzz_target_runner.cpp +++ b/test/fuzzing/standalone_fuzz_target_runner.cpp @@ -11,7 +11,7 @@ #include // Forward declare the "fuzz target" interface. -// We deliberately keep this inteface simple and header-free. +// We deliberately keep this interface simple and header-free. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); // It reads all files passed as parameters and feeds their contents diff --git a/test/test.cc b/test/test.cc index 02279eac5e..bbb9d0d525 100644 --- a/test/test.cc +++ b/test/test.cc @@ -46,7 +46,7 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files, return *it; #else if (it != files.end()) { return *it; } - throw std::runtime_error("invalid mulitpart form data name error"); + throw std::runtime_error("invalid multipart form data name error"); #endif } @@ -779,7 +779,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { } // NOTE: Until httpbin.org fixes issue #46, the following test is commented - // out. Plese see https://httpbin.org/digest-auth/auth/hello/world + // out. Please see https://httpbin.org/digest-auth/auth/hello/world // cli.set_digest_auth("bad", "world"); // for (auto path : paths) { // auto res = cli.Get(path.c_str()); @@ -1778,7 +1778,7 @@ class ServerTest : public ::testing::Test { EXPECT_EQ(text_value.size(), 1); auto &text = text_value[0]; EXPECT_TRUE(text.filename.empty()); - EXPECT_EQ("defalut text", text.content); + EXPECT_EQ("default text", text.content); } { const auto &text1_values = req.get_file_values("multi_text1"); @@ -2647,7 +2647,7 @@ TEST_F(ServerTest, MultipartFormData) { TEST_F(ServerTest, MultipartFormDataMultiFileValues) { MultipartFormDataItems items = { - {"text", "defalut text", "", ""}, + {"text", "default text", "", ""}, {"multi_text1", "aaaaa", "", ""}, {"multi_text1", "bbbbb", "", ""}, @@ -3185,7 +3185,7 @@ TEST(GzipDecompressor, ChunkedDecompression) { { httplib::detail::gzip_decompressor decompressor; - // Chunk size is chosen specificaly to have a decompressed chunk size equal + // Chunk size is chosen specifically to have a decompressed chunk size equal // to 16384 bytes 16384 bytes is the size of decompressor output buffer size_t chunk_size = 130; for (size_t chunk_begin = 0; chunk_begin < compressed_data.size(); @@ -3334,7 +3334,7 @@ TEST_F(ServerTest, PostContentReceiver) { ASSERT_EQ("content", res->body); } -TEST_F(ServerTest, PostMulitpartFilsContentReceiver) { +TEST_F(ServerTest, PostMultipartFileContentReceiver) { MultipartFormDataItems items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, @@ -3349,7 +3349,7 @@ TEST_F(ServerTest, PostMulitpartFilsContentReceiver) { EXPECT_EQ(200, res->status); } -TEST_F(ServerTest, PostMulitpartPlusBoundary) { +TEST_F(ServerTest, PostMultipartPlusBoundary) { MultipartFormDataItems items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, @@ -3765,10 +3765,10 @@ TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) { "&&&%%%"); } -TEST(ServerRequestParsingTest, ExcessiveWhitespaceInUnparseableHeaderLine) { +TEST(ServerRequestParsingTest, ExcessiveWhitespaceInUnparsableHeaderLine) { // Make sure this doesn't crash the server. // In a previous version of the header line regex, the "\r" rendered the line - // unparseable and the regex engine repeatedly backtracked, trying to look for + // unparsable and the regex engine repeatedly backtracked, trying to look for // a new position where the leading white space ended and the field value // began. // The crash occurs with libc++ but not libstdc++. diff --git a/test/test_proxy.cc b/test/test_proxy.cc index 3adf0a6acb..f44ec63c10 100644 --- a/test/test_proxy.cc +++ b/test/test_proxy.cc @@ -193,7 +193,7 @@ void DigestAuthTestFromHTTPWatch(T& cli) { } // NOTE: Until httpbin.org fixes issue #46, the following test is commented - // out. Plese see https://httpbin.org/digest-auth/auth/hello/world + // out. Please see https://httpbin.org/digest-auth/auth/hello/world // cli.set_digest_auth("bad", "world"); // for (auto path : paths) { // auto res = cli.Get(path.c_str()); From df74526f910e6ad23d4d4ce608b5eb747d230b62 Mon Sep 17 00:00:00 2001 From: Mathieu Gaillard Date: Wed, 8 Mar 2023 20:57:17 -0800 Subject: [PATCH 0044/1222] Fix multipart Content-Type headers with both boundary and charset parameters (#1516) * Fix multipart Content-Type headers with both boundary and charset parameters * Improve code readability * Add missing forward declaration --------- Co-authored-by: Mathieu Gaillard --- httplib.h | 9 +++++++-- test/test.cc | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index cc39270d35..67e551665c 100644 --- a/httplib.h +++ b/httplib.h @@ -1823,6 +1823,8 @@ std::string params_to_query_str(const Params ¶ms); void parse_query_text(const std::string &s, Params ¶ms); +bool parse_multipart_boundary(const std::string &content_type, std::string &boundary); + bool parse_range_header(const std::string &s, Ranges &ranges); int close_socket(socket_t sock); @@ -3888,9 +3890,12 @@ inline void parse_query_text(const std::string &s, Params ¶ms) { inline bool parse_multipart_boundary(const std::string &content_type, std::string &boundary) { - auto pos = content_type.find("boundary="); + auto boundary_keyword = "boundary="; + auto pos = content_type.find(boundary_keyword); if (pos == std::string::npos) { return false; } - boundary = content_type.substr(pos + 9); + auto end = content_type.find(';', pos); + auto beg = pos + strlen(boundary_keyword); + boundary = content_type.substr(beg, end - beg); if (boundary.length() >= 2 && boundary.front() == '"' && boundary.back() == '"') { boundary = boundary.substr(1, boundary.size() - 2); diff --git a/test/test.cc b/test/test.cc index bbb9d0d525..173a1a4144 100644 --- a/test/test.cc +++ b/test/test.cc @@ -169,6 +169,39 @@ TEST(ParamsToQueryTest, ConvertParamsToQuery) { EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1&key2=val2&key3=val3"); } +TEST(ParseMultipartBoundaryTest, DefaultValue) { + string content_type = "multipart/form-data; boundary=something"; + string boundary; + auto ret = detail::parse_multipart_boundary(content_type, boundary); + EXPECT_TRUE(ret); + EXPECT_EQ(boundary, "something"); +} + +TEST(ParseMultipartBoundaryTest, ValueWithQuote) { + string content_type = "multipart/form-data; boundary=\"gc0pJq0M:08jU534c0p\""; + string boundary; + auto ret = detail::parse_multipart_boundary(content_type, boundary); + EXPECT_TRUE(ret); + EXPECT_EQ(boundary, "gc0pJq0M:08jU534c0p"); +} + +TEST(ParseMultipartBoundaryTest, ValueWithCharset) { + string content_type = "multipart/mixed; boundary=THIS_STRING_SEPARATES;charset=UTF-8"; + string boundary; + auto ret = detail::parse_multipart_boundary(content_type, boundary); + EXPECT_TRUE(ret); + EXPECT_EQ(boundary, "THIS_STRING_SEPARATES"); +} + +TEST(ParseMultipartBoundaryTest, ValueWithQuotesAndCharset) { + string content_type = + "multipart/mixed; boundary=\"cpp-httplib-multipart-data\"; charset=UTF-8"; + string boundary; + auto ret = detail::parse_multipart_boundary(content_type, boundary); + EXPECT_TRUE(ret); + EXPECT_EQ(boundary, "cpp-httplib-multipart-data"); +} + TEST(GetHeaderValueTest, DefaultValue) { Headers headers = {{"Dummy", "Dummy"}}; auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); From d1b616286f0921efa9e1c5b8e619c9fd7078e348 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 10 Mar 2023 17:46:50 -0500 Subject: [PATCH 0045/1222] Add note for macOS regarding system certs --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index db7b162fec..f1c6c5b7d2 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ SSL Support SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. NOTE: cpp-httplib currently supports only version 1.1.1 and 3.0. +NOTE for macOS: cpp-httplib now uses system certs. `CoreFoundation` and `Security` should be linked with `-framework`. ```c++ #define CPPHTTPLIB_OPENSSL_SUPPORT From f2f47284890e9ed1ab1750a21c06441bdd5fcb6c Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 10 Mar 2023 17:53:19 -0500 Subject: [PATCH 0046/1222] Release v0.12.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 67e551665c..269680b2d9 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.0" +#define CPPHTTPLIB_VERSION "0.12.1" /* * Configuration From 9bb3ca81698ecb5c3b1f6ebb7780d8c89aa07d61 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 10 Mar 2023 22:21:42 -0500 Subject: [PATCH 0047/1222] Fix #1459 (#1523) --- httplib.h | 30 +++++++++++++++++++++++------- test/test.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 269680b2d9..abb5719e17 100644 --- a/httplib.h +++ b/httplib.h @@ -1823,7 +1823,8 @@ std::string params_to_query_str(const Params ¶ms); void parse_query_text(const std::string &s, Params ¶ms); -bool parse_multipart_boundary(const std::string &content_type, std::string &boundary); +bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary); bool parse_range_header(const std::string &s, Ranges &ranges); @@ -3391,6 +3392,14 @@ inline const char *get_header_value(const Headers &headers, return def; } +inline bool compare_case_ignore(const std::string &a, const std::string &b) { + if (a.size() != b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + } + return true; +} + template inline bool parse_header(const char *beg, const char *end, T fn) { // Skip trailing spaces and tabs. @@ -3414,7 +3423,11 @@ inline bool parse_header(const char *beg, const char *end, T fn) { } if (p < end) { - fn(std::string(beg, key_end), decode_url(std::string(p, end), false)); + auto key = std::string(beg, key_end); + auto val = compare_case_ignore(key, "Location") + ? std::string(p, end) + : decode_url(std::string(p, end), false); + fn(std::move(key), std::move(val)); return true; } @@ -6463,11 +6476,11 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { return false; } - auto location = detail::decode_url(res.get_header_value("location"), true); + auto location = res.get_header_value("location"); if (location.empty()) { return false; } const static std::regex re( - R"((?:(https?):)?(?://(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); + R"((?:(https?):)?(?://(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); std::smatch m; if (!std::regex_match(location, m, re)) { return false; } @@ -6479,6 +6492,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { if (next_host.empty()) { next_host = m[3].str(); } auto port_str = m[4].str(); auto next_path = m[5].str(); + auto next_query = m[6].str(); auto next_port = port_; if (!port_str.empty()) { @@ -6491,22 +6505,24 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { if (next_host.empty()) { next_host = host_; } if (next_path.empty()) { next_path = "/"; } + auto path = detail::decode_url(next_path, true) + next_query; + if (next_scheme == scheme && next_host == host_ && next_port == port_) { - return detail::redirect(*this, req, res, next_path, location, error); + return detail::redirect(*this, req, res, path, location, error); } else { if (next_scheme == "https") { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT SSLClient cli(next_host.c_str(), next_port); cli.copy_settings(*this); if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } - return detail::redirect(cli, req, res, next_path, location, error); + return detail::redirect(cli, req, res, path, location, error); #else return false; #endif } else { ClientImpl cli(next_host.c_str(), next_port); cli.copy_settings(*this); - return detail::redirect(cli, req, res, next_path, location, error); + return detail::redirect(cli, req, res, path, location, error); } } } diff --git a/test/test.cc b/test/test.cc index 173a1a4144..09f88a741e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5978,3 +5978,34 @@ TEST(TaskQueueTest, IncreaseAtomicInteger) { EXPECT_NO_THROW(task_queue->shutdown()); EXPECT_EQ(number_of_task, count.load()); } + +TEST(RedirectTest, RedirectToUrlWithQueryParameters) { + Server svr; + + svr.Get("/", [](const Request & /*req*/, Response &res) { + res.set_redirect(R"(/hello?key=val%26key2%3Dval2)"); + }); + + svr.Get("/hello", [](const Request &req, Response &res) { + res.set_content(req.get_param_value("key"), "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + { + Client cli(HOST, PORT); + cli.set_follow_location(true); + + auto res = cli.Get("/"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ("val&key2=val2", res->body); + } + + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); +} + From 88a9278872c3a06702c0c3af99b5deb5e6c0442f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 11 Mar 2023 16:57:51 -0500 Subject: [PATCH 0048/1222] Fix #1486 --- README.md | 21 ++++++++++ httplib.h | 56 ++++++++++++++++++++++----- test/test.cc | 106 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 137 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index f1c6c5b7d2..9825772165 100644 --- a/README.md +++ b/README.md @@ -347,6 +347,27 @@ svr.Get("/chunked", [&](const Request& req, Response& res) { }); ``` +With trailer: + +```cpp +svr.Get("/chunked", [&](const Request& req, Response& res) { + res.set_header("Trailer", "Dummy1, Dummy2"); + res.set_chunked_content_provider( + "text/plain", + [](size_t offset, DataSink &sink) { + sink.write("123", 3); + sink.write("345", 3); + sink.write("789", 3); + sink.done_with_trailer({ + {"Dummy1", "DummyVal1"}, + {"Dummy2", "DummyVal2"} + }); + return true; + } + ); +}); +``` + ### 'Expect: 100-continue' handler By default, the server sends a `100 Continue` response for an `Expect: 100-continue` header. diff --git a/httplib.h b/httplib.h index abb5719e17..1125f595d2 100644 --- a/httplib.h +++ b/httplib.h @@ -371,6 +371,7 @@ class DataSink { std::function write; std::function done; + std::function done_with_trailer; std::ostream os; private: @@ -3525,7 +3526,8 @@ inline bool read_content_without_length(Stream &strm, return true; } -inline bool read_content_chunked(Stream &strm, +template +inline bool read_content_chunked(Stream &strm, T &x, ContentReceiverWithProgress out) { const auto bufsiz = 16; char buf[bufsiz]; @@ -3551,15 +3553,29 @@ inline bool read_content_chunked(Stream &strm, if (!line_reader.getline()) { return false; } - if (strcmp(line_reader.ptr(), "\r\n")) { break; } + if (strcmp(line_reader.ptr(), "\r\n")) { return false; } if (!line_reader.getline()) { return false; } } - if (chunk_len == 0) { - // Reader terminator after chunks - if (!line_reader.getline() || strcmp(line_reader.ptr(), "\r\n")) - return false; + assert(chunk_len == 0); + + // Trailer + if (!line_reader.getline()) { return false; } + + while (strcmp(line_reader.ptr(), "\r\n")) { + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Exclude line terminator + constexpr auto line_terminator_len = 2; + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + parse_header(line_reader.ptr(), end, + [&](std::string &&key, std::string &&val) { + x.headers.emplace(std::move(key), std::move(val)); + }); + + if (!line_reader.getline()) { return false; } } return true; @@ -3629,7 +3645,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, auto exceed_payload_max_length = false; if (is_chunked_transfer_encoding(x.headers)) { - ret = read_content_chunked(strm, out); + ret = read_content_chunked(strm, x, out); } else if (!has_header(x.headers, "Content-Length")) { ret = read_content_without_length(strm, out); } else { @@ -3785,7 +3801,7 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, return ok; }; - data_sink.done = [&](void) { + auto done_with_trailer = [&](const Headers *trailer) { if (!ok) { return; } data_available = false; @@ -3803,16 +3819,36 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, if (!payload.empty()) { // Emit chunked response header and footer for each chunk auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!write_data(strm, chunk.data(), chunk.size())) { + if (!strm.is_writable() || + !write_data(strm, chunk.data(), chunk.size())) { ok = false; return; } } - static const std::string done_marker("0\r\n\r\n"); + static const std::string done_marker("0\r\n"); if (!write_data(strm, done_marker.data(), done_marker.size())) { ok = false; } + + // Trailer + if (trailer) { + for (const auto &kv : *trailer) { + std::string field_line = kv.first + ": " + kv.second + "\r\n"; + if (!write_data(strm, field_line.data(), field_line.size())) { + ok = false; + } + } + } + + static const std::string crlf("\r\n"); + if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; } + }; + + data_sink.done = [&](void) { done_with_trailer(nullptr); }; + + data_sink.done_with_trailer = [&](const Headers &trailer) { + done_with_trailer(&trailer); }; while (data_available && !is_shutting_down()) { diff --git a/test/test.cc b/test/test.cc index 09f88a741e..ce921c14fe 100644 --- a/test/test.cc +++ b/test/test.cc @@ -186,7 +186,8 @@ TEST(ParseMultipartBoundaryTest, ValueWithQuote) { } TEST(ParseMultipartBoundaryTest, ValueWithCharset) { - string content_type = "multipart/mixed; boundary=THIS_STRING_SEPARATES;charset=UTF-8"; + string content_type = + "multipart/mixed; boundary=THIS_STRING_SEPARATES;charset=UTF-8"; string boundary; auto ret = detail::parse_multipart_boundary(content_type, boundary); EXPECT_TRUE(ret); @@ -1710,6 +1711,30 @@ class ServerTest : public ::testing::Test { delete i; }); }) + .Get("/streamed-chunked-with-trailer", + [&](const Request & /*req*/, Response &res) { + auto i = new int(0); + res.set_header("Trailer", "Dummy1, Dummy2"); + res.set_chunked_content_provider( + "text/plain", + [i](size_t /*offset*/, DataSink &sink) { + switch (*i) { + case 0: sink.os << "123"; break; + case 1: sink.os << "456"; break; + case 2: sink.os << "789"; break; + case 3: { + sink.done_with_trailer( + {{"Dummy1", "DummyVal1"}, {"Dummy2", "DummyVal2"}}); + } break; + } + (*i)++; + return true; + }, + [i](bool success) { + EXPECT_TRUE(success); + delete i; + }); + }) .Get("/streamed", [&](const Request & /*req*/, Response &res) { res.set_content_provider( @@ -1801,39 +1826,39 @@ class ServerTest : public ::testing::Test { } }) .Post("/multipart/multi_file_values", - [&](const Request &req, Response & /*res*/) { - EXPECT_EQ(5u, req.files.size()); - ASSERT_TRUE(!req.has_file("???")); - ASSERT_TRUE(req.body.empty()); + [&](const Request &req, Response & /*res*/) { + EXPECT_EQ(5u, req.files.size()); + ASSERT_TRUE(!req.has_file("???")); + ASSERT_TRUE(req.body.empty()); - { + { const auto &text_value = req.get_file_values("text"); EXPECT_EQ(text_value.size(), 1); auto &text = text_value[0]; EXPECT_TRUE(text.filename.empty()); EXPECT_EQ("default text", text.content); - } - { - const auto &text1_values = req.get_file_values("multi_text1"); - EXPECT_EQ(text1_values.size(), 2); - EXPECT_EQ("aaaaa", text1_values[0].content); - EXPECT_EQ("bbbbb", text1_values[1].content); - } - - { - const auto &file1_values = req.get_file_values("multi_file1"); - EXPECT_EQ(file1_values.size(), 2); - auto file1 = file1_values[0]; - EXPECT_EQ(file1.filename, "hello.txt"); - EXPECT_EQ(file1.content_type, "text/plain"); - EXPECT_EQ("h\ne\n\nl\nl\no\n", file1.content); - - auto file2 = file1_values[1]; - EXPECT_EQ(file2.filename, "world.json"); - EXPECT_EQ(file2.content_type, "application/json"); - EXPECT_EQ("{\n \"world\", true\n}\n", file2.content); - } - }) + } + { + const auto &text1_values = req.get_file_values("multi_text1"); + EXPECT_EQ(text1_values.size(), 2); + EXPECT_EQ("aaaaa", text1_values[0].content); + EXPECT_EQ("bbbbb", text1_values[1].content); + } + + { + const auto &file1_values = req.get_file_values("multi_file1"); + EXPECT_EQ(file1_values.size(), 2); + auto file1 = file1_values[0]; + EXPECT_EQ(file1.filename, "hello.txt"); + EXPECT_EQ(file1.content_type, "text/plain"); + EXPECT_EQ("h\ne\n\nl\nl\no\n", file1.content); + + auto file2 = file1_values[1]; + EXPECT_EQ(file2.filename, "world.json"); + EXPECT_EQ(file2.content_type, "application/json"); + EXPECT_EQ("{\n \"world\", true\n}\n", file2.content); + } + }) .Post("/empty", [&](const Request &req, Response &res) { EXPECT_EQ(req.body, ""); @@ -2680,13 +2705,14 @@ TEST_F(ServerTest, MultipartFormData) { TEST_F(ServerTest, MultipartFormDataMultiFileValues) { MultipartFormDataItems items = { - {"text", "default text", "", ""}, + {"text", "default text", "", ""}, - {"multi_text1", "aaaaa", "", ""}, - {"multi_text1", "bbbbb", "", ""}, + {"multi_text1", "aaaaa", "", ""}, + {"multi_text1", "bbbbb", "", ""}, - {"multi_file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, - {"multi_file1", "{\n \"world\", true\n}\n", "world.json", "application/json"}, + {"multi_file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, + {"multi_file1", "{\n \"world\", true\n}\n", "world.json", + "application/json"}, }; auto res = cli_.Post("/multipart/multi_file_values", items); @@ -2920,6 +2946,15 @@ TEST_F(ServerTest, GetStreamedChunked2) { EXPECT_EQ(std::string("123456789"), res->body); } +TEST_F(ServerTest, GetStreamedChunkedWithTrailer) { + auto res = cli_.Get("/streamed-chunked-with-trailer"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ(std::string("123456789"), res->body); + EXPECT_EQ(std::string("DummyVal1"), res->get_header_value("Dummy1")); + EXPECT_EQ(std::string("DummyVal2"), res->get_header_value("Dummy2")); +} + TEST_F(ServerTest, LargeChunkedPost) { Request req; req.method = "POST"; @@ -3906,9 +3941,8 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { TEST(ServerStopTest, ClientAccessAfterServerDown) { httplib::Server svr; - svr.Post("/hi", [&](const httplib::Request & /*req*/, httplib::Response &res) { - res.status = 200; - }); + svr.Post("/hi", [&](const httplib::Request & /*req*/, + httplib::Response &res) { res.status = 200; }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); From 5f186422718ca57a1176d0a3e68f4c2b957c8582 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 20 Mar 2023 11:45:21 -0400 Subject: [PATCH 0049/1222] Close #1531 --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index ce921c14fe..23aa20711b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4616,7 +4616,7 @@ TEST(SSLClientTest, ServerNameIndication_Online) { } TEST(SSLClientTest, ServerCertificateVerification1_Online) { - SSLClient cli("google.com"); + Client cli("https://google.com"); auto res = cli.Get("/"); ASSERT_TRUE(res); ASSERT_EQ(301, res->status); From 5745eabe69ad7edcd2f9a43dbd94b8743037e0ff Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 21 Mar 2023 18:54:54 -0400 Subject: [PATCH 0050/1222] Updated test.yaml to use ctions/checkout@v3 --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 2bf0c3dfd7..79b8de79a7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -17,7 +17,7 @@ jobs: git config --global core.autocrlf false git config --global core.eol lf - name: checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: install brotli library on ubuntu if: matrix.os == 'ubuntu-latest' run: sudo apt update && sudo apt-get install -y libbrotli-dev From d262033ded4c6a466ada474b813f31826d01e1e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Fl=C3=BCgel?= <122094924+JohannesFluegelHighyag@users.noreply.github.com> Date: Wed, 22 Mar 2023 19:16:32 +0100 Subject: [PATCH 0051/1222] Prevent overflow in hash function str2tag_core() (#1529) * str2tag_core(): prevent overflow * Update httplib.h works for all sizes of unsigned int and if there exists a #define for max --- httplib.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 1125f595d2..50c77090b4 100644 --- a/httplib.h +++ b/httplib.h @@ -2978,9 +2978,13 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { inline constexpr unsigned int str2tag_core(const char *s, size_t l, unsigned int h) { - return (l == 0) ? h - : str2tag_core(s + 1, l - 1, - (h * 33) ^ static_cast(*s)); + return (l == 0) + ? h + : str2tag_core( + s + 1, l - 1, + //unsets the 6 high bits of h, therefore no overflow happens + (((std::numeric_limits::max)() >> 6) & h * 33) ^ + static_cast(*s)); } inline unsigned int str2tag(const std::string &s) { From 8f96b69a258a227796c907c23a73b34e03d6f67c Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 23 Mar 2023 18:53:27 -0400 Subject: [PATCH 0052/1222] Update test.yaml to use microsoft/setup-msbuild@v1.1 --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 79b8de79a7..173daa8c89 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -32,7 +32,7 @@ jobs: run: cd test && make fuzz_test - name: setup msbuild on windows if: matrix.os == 'windows-latest' - uses: microsoft/setup-msbuild@v1.0.2 + uses: microsoft/setup-msbuild@v1.1 - name: make-windows if: matrix.os == 'windows-latest' run: | From 4cf218643e990383f0a037de96df6839aa9d030f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Mar 2023 21:12:40 -0400 Subject: [PATCH 0053/1222] Code format --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 50c77090b4..c12a5a208a 100644 --- a/httplib.h +++ b/httplib.h @@ -2982,8 +2982,9 @@ inline constexpr unsigned int str2tag_core(const char *s, size_t l, ? h : str2tag_core( s + 1, l - 1, - //unsets the 6 high bits of h, therefore no overflow happens - (((std::numeric_limits::max)() >> 6) & h * 33) ^ + // Unsets the 6 high bits of h, therefore no overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ static_cast(*s)); } From f4b02dfdc199a3a704ca66f2debf1347f81e5547 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Mar 2023 21:13:07 -0400 Subject: [PATCH 0054/1222] Fix #1533 --- httplib.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index c12a5a208a..2fdf6095de 100644 --- a/httplib.h +++ b/httplib.h @@ -6826,11 +6826,14 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_ssl()) { - char buf[1]; - if (SSL_peek(socket_.ssl, buf, 1) == 0 && - SSL_get_error(socket_.ssl, 0) == SSL_ERROR_ZERO_RETURN) { - error = Error::SSLPeerCouldBeClosed_; - return false; + auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; + if (!is_proxy_enabled) { + char buf[1]; + if (SSL_peek(socket_.ssl, buf, 1) == 0 && + SSL_get_error(socket_.ssl, 0) == SSL_ERROR_ZERO_RETURN) { + error = Error::SSLPeerCouldBeClosed_; + return false; + } } } #endif From a66a013ed78dee11701d6075c6b713307004a126 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Mar 2023 21:47:14 -0400 Subject: [PATCH 0055/1222] Release v0.12.2 --- README.md | 2 +- httplib.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9825772165..12566cf71b 100644 --- a/README.md +++ b/README.md @@ -841,7 +841,7 @@ Note: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin on Windows are License ------- -MIT license (© 2022 Yuji Hirose) +MIT license (© 2023 Yuji Hirose) Special Thanks To ----------------- diff --git a/httplib.h b/httplib.h index 2fdf6095de..c7e5d406b5 100644 --- a/httplib.h +++ b/httplib.h @@ -1,14 +1,14 @@ // // httplib.h // -// Copyright (c) 2022 Yuji Hirose. All rights reserved. +// Copyright (c) 2023 Yuji Hirose. All rights reserved. // MIT License // #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.1" +#define CPPHTTPLIB_VERSION "0.12.2" /* * Configuration From 76230db97ff8945438ec4bb5eb7fd6630d0311a7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Mar 2023 21:52:39 -0400 Subject: [PATCH 0056/1222] Simplified scope_exit --- httplib.h | 8 ++++---- test/test.cc | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index c7e5d406b5..43c70bb150 100644 --- a/httplib.h +++ b/httplib.h @@ -314,8 +314,8 @@ struct ci { // This is based on // "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". -template struct scope_exit { - explicit scope_exit(EF &&f) +struct scope_exit { + explicit scope_exit(std::function &&f) : exit_function(std::move(f)), execute_on_destruction{true} {} scope_exit(scope_exit &&rhs) @@ -335,7 +335,7 @@ template struct scope_exit { void operator=(const scope_exit &) = delete; scope_exit &operator=(scope_exit &&) = delete; - EF exit_function; + std::function exit_function; bool execute_on_destruction; }; @@ -6410,7 +6410,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { auto ret = false; auto close_connection = !keep_alive_; - auto se = detail::scope_exit>([&]() { + auto se = detail::scope_exit([&]() { // Briefly lock mutex in order to mark that a request is no longer ongoing std::lock_guard guard(socket_mutex_); socket_requests_in_flight_ -= 1; diff --git a/test/test.cc b/test/test.cc index 23aa20711b..7d070cc60a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1220,7 +1220,7 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { ASSERT_FALSE(svr.is_running()); } -TEST(BindServerTest, BindDualStack) { +TEST(BindServerTest, DISABLED_BindDualStack) { Server svr; svr.Get("/1", [&](const Request & /*req*/, Response &res) { @@ -6026,6 +6026,12 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&](void) { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -6037,9 +6043,5 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { EXPECT_EQ(200, res->status); EXPECT_EQ("val&key2=val2", res->body); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } From b33aa52dc2de91ae8d966c1e2d9e3ca9ccaab75f Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Tue, 28 Mar 2023 13:01:34 +0900 Subject: [PATCH 0057/1222] Fix lifetime issues in test using detail::scope_exit() (#1535) * Fix lifetime issues in test using detail::scope_exit() * Remove checking joinable threads --- test/test.cc | 475 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 286 insertions(+), 189 deletions(-) diff --git a/test/test.cc b/test/test.cc index 7d070cc60a..c1e30c1f64 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1001,6 +1001,11 @@ TEST(ReceiveSignals, Signal) { port = svr.bind_to_any_port("localhost"); svr.listen_after_bind(); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -1010,9 +1015,6 @@ TEST(ReceiveSignals, Signal) { pthread_kill(thread.native_handle(), SIGINT); std::this_thread::sleep_for(std::chrono::milliseconds(100)); ASSERT_TRUE(svr.is_running()); - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } #endif @@ -1038,6 +1040,14 @@ TEST(RedirectToDifferentPort, Redirect) { svr2_port = svr2.bind_to_any_port("localhost"); svr2.listen_after_bind(); }); + auto se = detail::scope_exit([&] { + svr2.stop(); + thread2.join(); + svr1.stop(); + thread1.join(); + ASSERT_FALSE(svr2.is_running()); + ASSERT_FALSE(svr1.is_running()); + }); while (!svr1.is_running() || !svr2.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -1053,13 +1063,6 @@ TEST(RedirectToDifferentPort, Redirect) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); EXPECT_EQ("Hello World!", res->body); - - svr1.stop(); - svr2.stop(); - thread1.join(); - thread2.join(); - ASSERT_FALSE(svr1.is_running()); - ASSERT_FALSE(svr2.is_running()); } TEST(RedirectFromPageWithContent, Redirect) { @@ -1075,6 +1078,11 @@ TEST(RedirectFromPageWithContent, Redirect) { }); auto th = std::thread([&]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + th.join(); + ASSERT_FALSE(svr.is_running()); + }); while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); @@ -1111,10 +1119,6 @@ TEST(RedirectFromPageWithContent, Redirect) { EXPECT_EQ(302, res->status); EXPECT_EQ("___", body); } - - svr.stop(); - th.join(); - ASSERT_FALSE(svr.is_running()); } TEST(RedirectFromPageWithContentIP6, Redirect) { @@ -1135,6 +1139,11 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { }); auto th = std::thread([&]() { svr.listen("::1", 1234); }); + auto se = detail::scope_exit([&] { + svr.stop(); + th.join(); + ASSERT_FALSE(svr.is_running()); + }); // When IPV6 support isn't available svr.listen("::1", 1234) never // actually starts anything, so the condition !svr.is_running() will @@ -1178,10 +1187,6 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { EXPECT_EQ(302, res->status); EXPECT_EQ("___", body); } - - svr.stop(); - th.join(); - ASSERT_FALSE(svr.is_running()); } TEST(PathUrlEncodeTest, PathUrlEncode) { @@ -1198,6 +1203,11 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1214,10 +1224,6 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { // into spaces. EXPECT_EQ("explicitly encoded", res->body); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(BindServerTest, DISABLED_BindDualStack) { @@ -1228,6 +1234,11 @@ TEST(BindServerTest, DISABLED_BindDualStack) { }); auto thread = std::thread([&]() { svr.listen("::", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1248,9 +1259,6 @@ TEST(BindServerTest, DISABLED_BindDualStack) { EXPECT_EQ(200, res->status); EXPECT_EQ("Hello World!", res->body); } - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(BindServerTest, BindAndListenSeparately) { @@ -1298,6 +1306,11 @@ TEST(ErrorHandlerTest, ContentLength) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1312,10 +1325,6 @@ TEST(ErrorHandlerTest, ContentLength) { EXPECT_EQ("26", res->get_header_value("Content-Length")); EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } #ifndef CPPHTTPLIB_NO_EXCEPTIONS @@ -1339,6 +1348,11 @@ TEST(ExceptionHandlerTest, ContentLength) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1366,10 +1380,6 @@ TEST(ExceptionHandlerTest, ContentLength) { EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); } } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } #endif @@ -1379,6 +1389,11 @@ TEST(NoContentTest, ContentLength) { svr.Get("/hi", [](const Request & /*req*/, Response &res) { res.status = 204; }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1391,10 +1406,6 @@ TEST(NoContentTest, ContentLength) { EXPECT_EQ(204, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(RoutingHandlerTest, PreRoutingHandler) { @@ -1429,6 +1440,11 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1482,10 +1498,6 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { EXPECT_EQ(0U, res->get_header_value_count("PRE_ROUTING")); EXPECT_EQ(0U, res->get_header_value_count("POST_ROUTING")); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(InvalidFormatTest, StatusCode) { @@ -1497,6 +1509,11 @@ TEST(InvalidFormatTest, StatusCode) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1507,10 +1524,6 @@ TEST(InvalidFormatTest, StatusCode) { auto res = cli.Get("/hi"); ASSERT_FALSE(res); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(URLFragmentTest, WithFragment) { @@ -1521,6 +1534,11 @@ TEST(URLFragmentTest, WithFragment) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -1535,10 +1553,6 @@ TEST(URLFragmentTest, WithFragment) { EXPECT_TRUE(res); EXPECT_EQ(404, res->status); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } class ServerTest : public ::testing::Test { @@ -2855,13 +2869,13 @@ TEST_F(ServerTest, GetStreamedEndless) { TEST_F(ServerTest, ClientStop) { std::vector threads; for (auto i = 0; i < 3; i++) { - threads.emplace_back(thread([&]() { + threads.emplace_back([&]() { auto res = cli_.Get("/streamed-cancel", [&](const char *, uint64_t) { return true; }); ASSERT_TRUE(!res); EXPECT_TRUE(res.error() == Error::Canceled || res.error() == Error::Read || res.error() == Error::Write); - })); + }); } std::this_thread::sleep_for(std::chrono::seconds(2)); @@ -3011,12 +3025,9 @@ TEST_F(ServerTest, HTTPResponseSplitting) { } TEST_F(ServerTest, SlowRequest) { - request_threads_.push_back( - std::thread([=]() { auto res = cli_.Get("/slow"); })); - request_threads_.push_back( - std::thread([=]() { auto res = cli_.Get("/slow"); })); - request_threads_.push_back( - std::thread([=]() { auto res = cli_.Get("/slow"); })); + request_threads_.emplace_back([this]() { auto res = cli_.Get("/slow"); }); + request_threads_.emplace_back([this]() { auto res = cli_.Get("/slow"); }); + request_threads_.emplace_back([this]() { auto res = cli_.Get("/slow"); }); } #if 0 @@ -3740,6 +3751,12 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) { }); thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -3752,8 +3769,6 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) { "\r\n"; ASSERT_TRUE(send_request(5, req)); - svr.stop(); - t.join(); EXPECT_EQ(header_value, "\v bar \x1B"); } @@ -3775,14 +3790,18 @@ static void test_raw_request(const std::string &req, svr.set_read_timeout(std::chrono::seconds(client_read_timeout_sec + 1)); bool listen_thread_ok = false; thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + EXPECT_TRUE(listen_thread_ok); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } ASSERT_TRUE(send_request(client_read_timeout_sec, req, out)); - svr.stop(); - t.join(); - EXPECT_TRUE(listen_thread_ok); } TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) { @@ -3927,16 +3946,15 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { "/events", headers, [](const char * /*data*/, size_t /*len*/) -> bool { return true; }); }); + auto se = detail::scope_exit([&] { + svr.stop(); + get_thread.join(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(2)); - - svr.stop(); - - listen_thread.join(); - get_thread.join(); - - ASSERT_FALSE(svr.is_running()); } TEST(ServerStopTest, ClientAccessAfterServerDown) { @@ -3945,7 +3963,6 @@ TEST(ServerStopTest, ClientAccessAfterServerDown) { httplib::Response &res) { res.status = 200; }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); - while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); } @@ -3953,6 +3970,7 @@ TEST(ServerStopTest, ClientAccessAfterServerDown) { Client cli(HOST, PORT); auto res = cli.Post("/hi", "data", "text/plain"); + ASSERT_TRUE(res); EXPECT_EQ(200, res->status); @@ -3979,6 +3997,12 @@ TEST(StreamingTest, NoContentLengthStreaming) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto listen_se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -3994,22 +4018,22 @@ TEST(StreamingTest, NoContentLengthStreaming) { }); EXPECT_EQ("aaabbb", s); }); + auto get_se = detail::scope_exit([&] { get_thread.join(); }); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::milliseconds(500)); - - svr.stop(); - - listen_thread.join(); - get_thread.join(); - - ASSERT_FALSE(svr.is_running()); } TEST(MountTest, Unmount) { Server svr; auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4044,10 +4068,6 @@ TEST(MountTest, Unmount) { res = cli.Get("/mount2/dir/test.html"); ASSERT_TRUE(res); EXPECT_EQ(404, res->status); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } #ifndef CPPHTTPLIB_NO_EXCEPTIONS @@ -4063,6 +4083,12 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4087,10 +4113,6 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT")); } - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } #endif @@ -4107,6 +4129,12 @@ TEST(KeepAliveTest, ReadTimeout) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4126,10 +4154,6 @@ TEST(KeepAliveTest, ReadTimeout) { ASSERT_TRUE(resb); EXPECT_EQ(200, resb->status); EXPECT_EQ("b", resb->body); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(KeepAliveTest, Issue1041) { @@ -4140,7 +4164,13 @@ TEST(KeepAliveTest, Issue1041) { res.set_content("Hello World!", "text/plain"); }); - auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); + auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); Client cli(HOST, PORT); @@ -4155,9 +4185,6 @@ TEST(KeepAliveTest, Issue1041) { result = cli.Get("/hi"); ASSERT_TRUE(result); EXPECT_EQ(200, result->status); - - svr.stop(); - f.wait(); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -4170,7 +4197,13 @@ TEST(KeepAliveTest, SSLClientReconnection) { res.set_content("Hello World!", "text/plain"); }); - auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); + auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); SSLClient cli(HOST, PORT); @@ -4195,9 +4228,6 @@ TEST(KeepAliveTest, SSLClientReconnection) { result = cli.Get("/hi"); ASSERT_TRUE(result); EXPECT_EQ(200, result->status); - - svr.stop(); - f.wait(); } #endif @@ -4219,6 +4249,12 @@ TEST(ClientProblemDetectionTest, ContentProvider) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4233,10 +4269,6 @@ TEST(ClientProblemDetectionTest, ContentProvider) { }); ASSERT_FALSE(res); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { @@ -4253,6 +4285,12 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4266,10 +4304,6 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { ASSERT_TRUE(res); EXPECT_EQ(404, res->status); EXPECT_EQ("helloworld", res->body); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(GetWithParametersTest, GetWithParameters) { @@ -4294,6 +4328,12 @@ TEST(GetWithParametersTest, GetWithParameters) { }); auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4329,10 +4369,6 @@ TEST(GetWithParametersTest, GetWithParameters) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); } - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(GetWithParametersTest, GetWithParameters2) { @@ -4344,6 +4380,12 @@ TEST(GetWithParametersTest, GetWithParameters2) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4364,10 +4406,6 @@ TEST(GetWithParametersTest, GetWithParameters2) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); EXPECT_EQ("world", body); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { @@ -4399,6 +4437,12 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4412,10 +4456,6 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) { EXPECT_EQ(200, res->status); EXPECT_EQ("ok", res->body); EXPECT_EQ("World", res->get_header_value("Hello")); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -4433,6 +4473,12 @@ TEST(KeepAliveTest, ReadTimeoutSSL) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -4453,10 +4499,6 @@ TEST(KeepAliveTest, ReadTimeoutSSL) { ASSERT_TRUE(resb); EXPECT_EQ(200, resb->status); EXPECT_EQ("b", resb->body); - - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } #endif @@ -4650,6 +4692,11 @@ TEST(SSLClientTest, ServerCertificateVerification4) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); }); + auto se = detail::scope_exit([&] { + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli("127.0.0.1", PORT); @@ -4660,8 +4707,6 @@ TEST(SSLClientTest, ServerCertificateVerification4) { auto res = cli.Get("/test"); ASSERT_TRUE(res); ASSERT_EQ(200, res->status); - - t.join(); } TEST(SSLClientTest, WildcardHostNameMatch_Online) { @@ -4720,6 +4765,11 @@ TEST(SSLClientServerTest, ClientCertPresent) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); @@ -4729,8 +4779,6 @@ TEST(SSLClientServerTest, ClientCertPresent) { auto res = cli.Get("/test"); ASSERT_TRUE(res); ASSERT_EQ(200, res->status); - - t.join(); } #if !defined(_WIN32) || defined(OPENSSL_USE_APPLINK) @@ -4792,6 +4840,11 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli(HOST, PORT, client_cert, client_private_key); @@ -4806,8 +4859,6 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { EVP_PKEY_free(server_private_key); X509_free(client_cert); EVP_PKEY_free(client_private_key); - - t.join(); } #endif @@ -4819,6 +4870,12 @@ TEST(SSLClientServerTest, ClientCertMissing) { svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli(HOST, PORT); @@ -4826,10 +4883,6 @@ TEST(SSLClientServerTest, ClientCertMissing) { cli.set_connection_timeout(30); ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLServerVerification, res.error()); - - svr.stop(); - - t.join(); } TEST(SSLClientServerTest, TrustDirOptional) { @@ -4842,6 +4895,11 @@ TEST(SSLClientServerTest, TrustDirOptional) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); @@ -4851,8 +4909,6 @@ TEST(SSLClientServerTest, TrustDirOptional) { auto res = cli.Get("/test"); ASSERT_TRUE(res); ASSERT_EQ(200, res->status); - - t.join(); } TEST(SSLClientServerTest, SSLConnectTimeout) { @@ -4885,6 +4941,13 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop_ = true; + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); @@ -4894,10 +4957,6 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { auto res = cli.Get("/test"); ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLConnection, res.error()); - - svr.stop_ = true; - svr.stop(); - t.join(); } TEST(SSLClientServerTest, CustomizeServerSSLCtx) { @@ -4956,6 +5015,11 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); @@ -4965,8 +5029,6 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { auto res = cli.Get("/test"); ASSERT_TRUE(res); ASSERT_EQ(200, res->status); - - t.join(); } // Disabled due to the out-of-memory problem on GitHub Actions Workflows @@ -4990,6 +5052,12 @@ TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5006,11 +5074,6 @@ TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { EXPECT_EQ(200, res->status); EXPECT_EQ(large_size_byte, res->body.size()); EXPECT_EQ(0, std::memcmp(binary.data(), res->body.data(), large_size_byte)); - - // cleanup - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } #endif @@ -5057,6 +5120,11 @@ TEST(ClientImplMethods, GetSocketTest) { }); auto thread = std::thread([&]() { svr.listen("127.0.0.1", 3333); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); @@ -5081,10 +5149,6 @@ TEST(ClientImplMethods, GetSocketTest) { EXPECT_EQ(200, res->status); ASSERT_TRUE(cli.socket() != INVALID_SOCKET); } - - svr.stop(); - thread.join(); - ASSERT_FALSE(svr.is_running()); } // Disabled due to out-of-memory problem on GitHub Actions @@ -5101,6 +5165,13 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { }); auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + if (content) free(content); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5113,11 +5184,6 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); EXPECT_EQ(content_size, res->body.length()); - - free(content); - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); } #endif @@ -5241,6 +5307,12 @@ TEST(HttpToHttpsRedirectTest, CertFile) { thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); }); thread t2 = thread([&]() { ASSERT_TRUE(ssl_svr.listen("127.0.0.1", 1235)); }); + auto se = detail::scope_exit([&] { + t2.join(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); Client cli("127.0.0.1", PORT); @@ -5252,9 +5324,6 @@ TEST(HttpToHttpsRedirectTest, CertFile) { auto res = cli.Get("/index"); ASSERT_TRUE(res); ASSERT_EQ(200, res->status); - - t.join(); - t2.join(); } TEST(MultipartFormDataTest, LargeData) { @@ -5293,6 +5362,12 @@ TEST(MultipartFormDataTest, LargeData) { }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5315,9 +5390,6 @@ TEST(MultipartFormDataTest, LargeData) { ASSERT_TRUE(res); ASSERT_EQ(200, res->status); } - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, DataProviderItems) { @@ -5440,6 +5512,12 @@ TEST(MultipartFormDataTest, DataProviderItems) { }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5513,9 +5591,6 @@ TEST(MultipartFormDataTest, DataProviderItems) { ASSERT_EQ(200, res->status); } } - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, BadHeader) { @@ -5525,6 +5600,12 @@ TEST(MultipartFormDataTest, BadHeader) { }); thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5556,9 +5637,6 @@ TEST(MultipartFormDataTest, BadHeader) { ASSERT_TRUE(res); EXPECT_EQ(400, res->status); - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, WithPreamble) { @@ -5568,6 +5646,12 @@ TEST(MultipartFormDataTest, WithPreamble) { }); thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5598,9 +5682,6 @@ TEST(MultipartFormDataTest, WithPreamble) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, PostCustomBoundary) { @@ -5639,6 +5720,12 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5661,9 +5748,6 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { ASSERT_TRUE(res); ASSERT_EQ(200, res->status); } - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { @@ -5725,6 +5809,12 @@ TEST(MultipartFormDataTest, PutFormData) { }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5747,9 +5837,6 @@ TEST(MultipartFormDataTest, PutFormData) { ASSERT_TRUE(res); ASSERT_EQ(200, res->status); } - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { @@ -5789,6 +5876,12 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5811,9 +5904,6 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { ASSERT_TRUE(res); ASSERT_EQ(200, res->status); } - - svr.stop(); - t.join(); } TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { @@ -5873,15 +5963,18 @@ TEST_F(UnixSocketTest, pathname) { std::thread t{[&] { ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } ASSERT_TRUE(svr.is_running()); client_GET(pathname_); - - svr.stop(); - t.join(); } #if defined(__linux__) || \ @@ -5897,6 +5990,12 @@ TEST_F(UnixSocketTest, PeerPid) { std::thread t{[&] { ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } @@ -5904,9 +6003,6 @@ TEST_F(UnixSocketTest, PeerPid) { client_GET(pathname_); EXPECT_EQ(std::to_string(getpid()), remote_port_val); - - svr.stop(); - t.join(); } #endif @@ -5923,15 +6019,18 @@ TEST_F(UnixSocketTest, abstract) { std::thread t{[&] { ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80)); }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + while (!svr.is_running()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } ASSERT_TRUE(svr.is_running()); client_GET(abstract_addr); - - svr.stop(); - t.join(); } #endif @@ -6025,8 +6124,7 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); - - auto se = detail::scope_exit([&](void) { + auto se = detail::scope_exit([&] { svr.stop(); thread.join(); ASSERT_FALSE(svr.is_running()); @@ -6044,4 +6142,3 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { EXPECT_EQ("val&key2=val2", res->body); } } - From 3956a2b79079ad7a8c337764726f8e9972a91527 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Thu, 30 Mar 2023 22:50:51 +0900 Subject: [PATCH 0058/1222] Fix ServerTest.ClientStop test case (#1542) --- test/test.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/test.cc b/test/test.cc index c1e30c1f64..6176e5353c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2867,11 +2867,16 @@ TEST_F(ServerTest, GetStreamedEndless) { } TEST_F(ServerTest, ClientStop) { + std::atomic_size_t count{4}; std::vector threads; - for (auto i = 0; i < 3; i++) { + + for (auto i = count.load(); i != 0; --i) { threads.emplace_back([&]() { auto res = cli_.Get("/streamed-cancel", [&](const char *, uint64_t) { return true; }); + + --count; + ASSERT_TRUE(!res); EXPECT_TRUE(res.error() == Error::Canceled || res.error() == Error::Read || res.error() == Error::Write); @@ -2879,7 +2884,7 @@ TEST_F(ServerTest, ClientStop) { } std::this_thread::sleep_for(std::chrono::seconds(2)); - while (cli_.is_socket_open()) { + while (count != 0) { cli_.stop(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); } From e5804d4a50eb2fa0412d449a2fee86d613ad7104 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 1 Apr 2023 22:26:30 +0900 Subject: [PATCH 0059/1222] Don't loading system certs from Keychain on iOS (#1546) --- httplib.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 43c70bb150..842a51e875 100644 --- a/httplib.h +++ b/httplib.h @@ -239,10 +239,13 @@ using socket_t = int; #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "cryptui.lib") #endif -#elif defined(__APPLE__) // _WIN32 +#elif defined(__APPLE__) +#include +#if TARGET_OS_OSX #include #include -#endif // __APPLE__ +#endif // TARGET_OS_OSX +#endif // _WIN32 #include #include @@ -4511,6 +4514,7 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { return result; } #elif defined(__APPLE__) +#if TARGET_OS_OSX template using CFObjectPtr = std::unique_ptr::type, void (*)(CFTypeRef)>; @@ -4585,7 +4589,7 @@ inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { return result; } -inline bool load_system_certs_on_apple(X509_STORE *store) { +inline bool load_system_certs_on_macos(X509_STORE *store) { auto result = false; CFObjectPtr certs(nullptr, cf_object_ptr_deleter); if (retrieve_certs_from_keychain(certs) && certs) { @@ -4598,8 +4602,9 @@ inline bool load_system_certs_on_apple(X509_STORE *store) { return result; } -#endif -#endif +#endif // TARGET_OS_OSX +#endif // _WIN32 +#endif // CPPHTTPLIB_OPENSSL_SUPPORT #ifdef _WIN32 class WSInit { @@ -8060,8 +8065,10 @@ inline bool SSLClient::load_certs() { loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); #elif defined(__APPLE__) - loaded = detail::load_system_certs_on_apple(SSL_CTX_get_cert_store(ctx_)); -#endif +#if TARGET_OS_OSX + loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); +#endif // TARGET_OS_OSX +#endif // _WIN32 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } } }); From ff34749572e433450263083771450a171a0055ff Mon Sep 17 00:00:00 2001 From: Octavio Valle Date: Mon, 3 Apr 2023 12:28:01 -0300 Subject: [PATCH 0060/1222] Initialize sockaddr_un to fix valgrind uninitialised byte message. (#1547) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 842a51e875..d7c808c053 100644 --- a/httplib.h +++ b/httplib.h @@ -2668,7 +2668,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); if (sock != INVALID_SOCKET) { - sockaddr_un addr; + sockaddr_un addr {}; addr.sun_family = AF_UNIX; std::copy(host.begin(), host.end(), addr.sun_path); From e62a4b02e5f74a45beb3f26f57cf44c0389364aa Mon Sep 17 00:00:00 2001 From: Sergey Kazmin <43613813+yerseg@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:12:15 +0300 Subject: [PATCH 0061/1222] fix (#1525) Co-authored-by: Sergey Kazmin --- CMakeLists.txt | 10 +++++++++- httplib.h | 10 +++++----- meson.build | 6 +++++- meson_options.txt | 1 + test/Makefile | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c98daf5edc..668abb7d22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ * HTTPLIB_REQUIRE_OPENSSL (default off) * HTTPLIB_REQUIRE_ZLIB (default off) * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) + * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_COMPILE (default off) * HTTPLIB_TEST (default off) @@ -43,6 +44,7 @@ * HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled. * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled. * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. + * HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled. * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only. * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include). * HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so). @@ -92,6 +94,7 @@ endif() option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) +option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON) # Defaults to static library option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) @@ -137,6 +140,10 @@ if(Brotli_FOUND) set(HTTPLIB_IS_USING_BROTLI TRUE) endif() +if(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) + set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN TRUE) +endif() + # Used for default, common dirs that the end-user can change (if needed) # like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR include(GNUInstallDirs) @@ -207,7 +214,7 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:crypt32> $<$:cryptui> # Needed for API from MacOS Security framework - "$<$,$>:-framework CoreFoundation -framework Security>" + "$<$,$, $>:-framework CoreFoundation -framework Security>" # Can't put multiple targets in a single generator expression or it bugs out. $<$:Brotli::common> $<$:Brotli::encoder> @@ -222,6 +229,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_BROTLI_SUPPORT> $<$:CPPHTTPLIB_ZLIB_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> + $<$,$, $>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> ) # CMake configuration files installation directory diff --git a/httplib.h b/httplib.h index d7c808c053..6da0a7299a 100644 --- a/httplib.h +++ b/httplib.h @@ -239,7 +239,7 @@ using socket_t = int; #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "cryptui.lib") #endif -#elif defined(__APPLE__) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) #include #if TARGET_OS_OSX #include @@ -2668,7 +2668,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); if (sock != INVALID_SOCKET) { - sockaddr_un addr {}; + sockaddr_un addr{}; addr.sun_family = AF_UNIX; std::copy(host.begin(), host.end(), addr.sun_path); @@ -4513,7 +4513,7 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { return result; } -#elif defined(__APPLE__) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) #if TARGET_OS_OSX template using CFObjectPtr = @@ -8064,9 +8064,9 @@ inline bool SSLClient::load_certs() { #ifdef _WIN32 loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(__APPLE__) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) #if TARGET_OS_OSX - loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); + loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); #endif // TARGET_OS_OSX #endif // _WIN32 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } diff --git a/meson.build b/meson.build index cc81eb1166..16362ad29a 100644 --- a/meson.build +++ b/meson.build @@ -35,7 +35,11 @@ if openssl_dep.found() deps += openssl_dep args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' if host_machine.system() == 'darwin' - deps += dependency('appleframeworks', modules: ['CoreFoundation', 'Security']) + macosx_keychain_dep = dependency('appleframeworks', modules: ['CoreFoundation', 'Security'], required: get_option('cpp-httplib_macosx_keychain')) + if macosx_keychain_dep.found() + deps += macosx_keychain_dep + args += '-DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN' + endif endif endif diff --git a/meson_options.txt b/meson_options.txt index d37c40db48..e15847d42f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -5,5 +5,6 @@ option('cpp-httplib_openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support') option('cpp-httplib_zlib', type: 'feature', value: 'auto', description: 'Enable zlib support') option('cpp-httplib_brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support') +option('cpp-httplib_macosx_keychain', type: 'feature', value: 'auto', description: 'Enable loading certs from the Keychain on Apple devices') option('cpp-httplib_compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires python3)') option('cpp-httplib_test', type: 'boolean', value: false, description: 'Build tests') diff --git a/test/Makefile b/test/Makefile index cb7605c59c..9feae74c65 100644 --- a/test/Makefile +++ b/test/Makefile @@ -11,7 +11,7 @@ OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPEN ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Darwin) - OPENSSL_SUPPORT += -framework CoreFoundation -framework Security + OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework CoreFoundation -framework Security endif endif From 985ceba525a7a1d566f2d8ba64930907bc60fef3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 4 Apr 2023 07:17:59 -0700 Subject: [PATCH 0062/1222] Updated README --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 12566cf71b..642bf8ade5 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ SSL Support SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. NOTE: cpp-httplib currently supports only version 1.1.1 and 3.0. -NOTE for macOS: cpp-httplib now uses system certs. `CoreFoundation` and `Security` should be linked with `-framework`. + +NOTE for macOS: cpp-httplib now can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`. ```c++ #define CPPHTTPLIB_OPENSSL_SUPPORT @@ -75,7 +76,7 @@ cli.set_ca_cert_path("./ca-bundle.crt"); cli.enable_server_certificate_verification(false); ``` -Note: When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. +NOTE: When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. Server ------ @@ -834,9 +835,9 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32 #include ``` -Note: cpp-httplib officially supports only the latest Visual Studio. It might work with former versions of Visual Studio, but I can no longer verify it. Pull requests are always welcome for the older versions of Visual Studio unless they break the C++11 conformance. +NOTE: cpp-httplib officially supports only the latest Visual Studio. It might work with former versions of Visual Studio, but I can no longer verify it. Pull requests are always welcome for the older versions of Visual Studio unless they break the C++11 conformance. -Note: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin on Windows are not supported. +NOTE: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin on Windows are not supported. License ------- From 21f9c5155647e15c6e17cb2f0e8fd8c7323f7d27 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 8 Apr 2023 04:40:52 +0900 Subject: [PATCH 0063/1222] Remove whitespace in CMake generator expression (#1552) --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 668abb7d22..c9ec3705e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -214,7 +214,7 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:crypt32> $<$:cryptui> # Needed for API from MacOS Security framework - "$<$,$, $>:-framework CoreFoundation -framework Security>" + "$<$,$,$>:-framework CoreFoundation -framework Security>" # Can't put multiple targets in a single generator expression or it bugs out. $<$:Brotli::common> $<$:Brotli::encoder> @@ -229,7 +229,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_BROTLI_SUPPORT> $<$:CPPHTTPLIB_ZLIB_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> - $<$,$, $>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> + $<$,$,$>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> ) # CMake configuration files installation directory From d587548250b169b1ffc5f36cad0169004df48ea7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Apr 2023 14:53:55 -0400 Subject: [PATCH 0064/1222] Fix #1545 --- httplib.h | 3 +-- test/test.cc | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 6da0a7299a..4d6b6639cc 100644 --- a/httplib.h +++ b/httplib.h @@ -4941,10 +4941,9 @@ inline void Response::set_content(const std::string &s, inline void Response::set_content_provider( size_t in_length, const std::string &content_type, ContentProvider provider, ContentProviderResourceReleaser resource_releaser) { - assert(in_length > 0); set_header("Content-Type", content_type); content_length_ = in_length; - content_provider_ = std::move(provider); + if (in_length > 0) { content_provider_ = std::move(provider); } content_provider_resource_releaser_ = resource_releaser; is_chunked_content_provider_ = false; } diff --git a/test/test.cc b/test/test.cc index 6176e5353c..72e73f80b5 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4253,6 +4253,16 @@ TEST(ClientProblemDetectionTest, ContentProvider) { [](bool success) { ASSERT_FALSE(success); }); }); + svr.Get("/empty", [&](const Request & /*req*/, Response &res) { + res.set_content_provider( + 0, "text/plain", + [&](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) -> bool { + EXPECT_TRUE(false); + return true; + }, + [](bool success) { ASSERT_FALSE(success); }); + }); + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); auto se = detail::scope_exit([&] { svr.stop(); @@ -4269,11 +4279,17 @@ TEST(ClientProblemDetectionTest, ContentProvider) { Client cli("localhost", PORT); - auto res = cli.Get("/hi", [&](const char * /*data*/, size_t /*data_length*/) { - return false; - }); + { + auto res = cli.Get("/hi", [&](const char * /*data*/, + size_t /*data_length*/) { return false; }); + ASSERT_FALSE(res); + } - ASSERT_FALSE(res); + { + auto res = cli.Get("/empty", [&](const char * /*data*/, + size_t /*data_length*/) { return false; }); + ASSERT_TRUE(res); + } } TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { From 7aba2938d3e7efb36aed8eba606200e949c20f03 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Apr 2023 15:31:47 -0400 Subject: [PATCH 0065/1222] Fix #1548 --- httplib.h | 23 +++++++--- test/test.cc | 127 ++++++++++++++++----------------------------------- 2 files changed, 57 insertions(+), 93 deletions(-) diff --git a/httplib.h b/httplib.h index 4d6b6639cc..73e7c1fcd6 100644 --- a/httplib.h +++ b/httplib.h @@ -743,6 +743,7 @@ class Server { bool listen(const std::string &host, int port, int socket_flags = 0); bool is_running() const; + void wait_until_ready() const; void stop(); std::function new_task_queue; @@ -752,7 +753,7 @@ class Server { bool &connection_closed, const std::function &setup_request); - std::atomic svr_sock_; + std::atomic svr_sock_{INVALID_SOCKET}; size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; @@ -816,7 +817,8 @@ class Server { }; std::vector base_dirs_; - std::atomic is_running_; + std::atomic is_running_{false}; + std::atomic done_{false}; std::map file_extension_and_mimetype_map_; Handler file_request_handler_; Handlers get_handlers_; @@ -5120,8 +5122,7 @@ inline const std::string &BufferStream::get_buffer() const { return buffer; } // HTTP server implementation inline Server::Server() : new_task_queue( - [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }), - svr_sock_(INVALID_SOCKET), is_running_(false) { + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif @@ -5334,15 +5335,25 @@ inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { return bind_internal(host, 0, socket_flags); } -inline bool Server::listen_after_bind() { return listen_internal(); } +inline bool Server::listen_after_bind() { + auto se = detail::scope_exit([&]() { done_ = true; }); + return listen_internal(); +} inline bool Server::listen(const std::string &host, int port, int socket_flags) { + auto se = detail::scope_exit([&]() { done_ = true; }); return bind_to_port(host, port, socket_flags) && listen_internal(); } inline bool Server::is_running() const { return is_running_; } +inline void Server::wait_until_ready() const { + while (!is_running() && !done_) { + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } +} + inline void Server::stop() { if (is_running_) { assert(svr_sock_ != INVALID_SOCKET); @@ -5733,6 +5744,7 @@ inline int Server::bind_internal(const std::string &host, int port, inline bool Server::listen_internal() { auto ret = true; is_running_ = true; + auto se = detail::scope_exit([&]() { is_running_ = false; }); { std::unique_ptr task_queue(new_task_queue()); @@ -5804,7 +5816,6 @@ inline bool Server::listen_internal() { task_queue->shutdown(); } - is_running_ = false; return ret; } diff --git a/test/test.cc b/test/test.cc index 72e73f80b5..59cf04bc1e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1007,9 +1007,7 @@ TEST(ReceiveSignals, Signal) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); ASSERT_TRUE(svr.is_running()); pthread_kill(thread.native_handle(), SIGINT); @@ -1084,9 +1082,7 @@ TEST(RedirectFromPageWithContent, Redirect) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -3762,9 +3758,7 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Only space and horizontal tab are whitespace. Make sure other whitespace- // like characters are not treated the same - use vertical tab and escape. @@ -3802,9 +3796,7 @@ static void test_raw_request(const std::string &req, EXPECT_TRUE(listen_thread_ok); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); ASSERT_TRUE(send_request(client_read_timeout_sec, req, out)); } @@ -3939,9 +3931,7 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); Client client(HOST, PORT); const Headers headers = {{"Accept", "text/event-stream"}}; @@ -3968,9 +3958,7 @@ TEST(ServerStopTest, ClientAccessAfterServerDown) { httplib::Response &res) { res.status = 200; }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + svr.wait_until_ready(); Client cli(HOST, PORT); @@ -3987,6 +3975,17 @@ TEST(ServerStopTest, ClientAccessAfterServerDown) { ASSERT_FALSE(res); } +TEST(ServerStopTest, ListenFailure) { + Server svr; + auto t = thread([&]() { + auto ret = svr.listen("????", PORT); + EXPECT_FALSE(ret); + }); + svr.wait_until_ready(); + svr.stop(); + t.join(); +} + TEST(StreamingTest, NoContentLengthStreaming) { Server svr; @@ -4008,9 +4007,7 @@ TEST(StreamingTest, NoContentLengthStreaming) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); Client client(HOST, PORT); @@ -4039,9 +4036,7 @@ TEST(MountTest, Unmount) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -4094,9 +4089,7 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -4140,9 +4133,7 @@ TEST(KeepAliveTest, ReadTimeout) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -4270,9 +4261,7 @@ TEST(ClientProblemDetectionTest, ContentProvider) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -4312,9 +4301,7 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -4355,9 +4342,7 @@ TEST(GetWithParametersTest, GetWithParameters) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -4407,9 +4392,7 @@ TEST(GetWithParametersTest, GetWithParameters2) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); Client cli("localhost", PORT); @@ -4464,9 +4447,7 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); Client cli("localhost", PORT); @@ -4500,9 +4481,7 @@ TEST(KeepAliveTest, ReadTimeoutSSL) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -5079,9 +5058,7 @@ TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // client POST SSLClient cli("localhost", PORT); @@ -5147,9 +5124,7 @@ TEST(ClientImplMethods, GetSocketTest) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } + svr.wait_until_ready(); { httplib::Client cli("http://127.0.0.1:3333"); @@ -5193,9 +5168,7 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); // Give GET time to get a few messages. std::this_thread::sleep_for(std::chrono::seconds(1)); @@ -5389,9 +5362,7 @@ TEST(MultipartFormDataTest, LargeData) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -5539,9 +5510,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -5627,9 +5596,7 @@ TEST(MultipartFormDataTest, BadHeader) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); const std::string body = "This is the preamble. It is to be ignored, though it\r\n" @@ -5673,9 +5640,7 @@ TEST(MultipartFormDataTest, WithPreamble) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); const std::string body = "This is the preamble. It is to be ignored, though it\r\n" @@ -5747,9 +5712,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -5836,9 +5799,7 @@ TEST(MultipartFormDataTest, PutFormData) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -5903,9 +5864,7 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); std::this_thread::sleep_for(std::chrono::seconds(1)); { @@ -5990,9 +5949,7 @@ TEST_F(UnixSocketTest, pathname) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); ASSERT_TRUE(svr.is_running()); client_GET(pathname_); @@ -6017,9 +5974,7 @@ TEST_F(UnixSocketTest, PeerPid) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); ASSERT_TRUE(svr.is_running()); client_GET(pathname_); @@ -6046,9 +6001,7 @@ TEST_F(UnixSocketTest, abstract) { ASSERT_FALSE(svr.is_running()); }); - while (!svr.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr.wait_until_ready(); ASSERT_TRUE(svr.is_running()); client_GET(abstract_addr); From c2e156e0e0e6af7df6971456255044197678a982 Mon Sep 17 00:00:00 2001 From: Oleg Shparber Date: Sun, 9 Apr 2023 12:18:44 -0400 Subject: [PATCH 0066/1222] Fix leaked handle in create_socket (#1554) Fixes resource leak problem detected by Coverity Scan. --- httplib.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 73e7c1fcd6..28746000cd 100644 --- a/httplib.h +++ b/httplib.h @@ -2728,7 +2728,10 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (sock == INVALID_SOCKET) { continue; } #ifndef _WIN32 - if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { continue; } + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { + close_socket(sock); + continue; + } #endif if (tcp_nodelay) { From f977558a28d6db893cf42131c33d025fa17458e1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 30 Apr 2023 10:44:36 +0900 Subject: [PATCH 0067/1222] Release v0.12.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 28746000cd..0da282bc25 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.2" +#define CPPHTTPLIB_VERSION "0.12.3" /* * Configuration From 5b397d455d25a391ba346863830c1949627b4d08 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 22 May 2023 22:56:16 +0900 Subject: [PATCH 0068/1222] Fix more CRLF injection problems. --- httplib.h | 38 +++++++++++++++++--------------------- test/test.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/httplib.h b/httplib.h index 0da282bc25..54f37a6ba3 100644 --- a/httplib.h +++ b/httplib.h @@ -5925,8 +5925,8 @@ inline void Server::apply_ranges(const Request &req, Response &res, res.headers.erase(it); } - res.headers.emplace("Content-Type", - "multipart/byteranges; boundary=" + boundary); + res.set_header("Content-Type", + "multipart/byteranges; boundary=" + boundary); } auto type = detail::encoding_type(req, res); @@ -6616,32 +6616,32 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, // Prepare additional headers if (close_connection) { if (!req.has_header("Connection")) { - req.headers.emplace("Connection", "close"); + req.set_header("Connection", "close"); } } if (!req.has_header("Host")) { if (is_ssl()) { if (port_ == 443) { - req.headers.emplace("Host", host_); + req.set_header("Host", host_); } else { - req.headers.emplace("Host", host_and_port_); + req.set_header("Host", host_and_port_); } } else { if (port_ == 80) { - req.headers.emplace("Host", host_); + req.set_header("Host", host_); } else { - req.headers.emplace("Host", host_and_port_); + req.set_header("Host", host_and_port_); } } } - if (!req.has_header("Accept")) { req.headers.emplace("Accept", "*/*"); } + if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } #ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT if (!req.has_header("User-Agent")) { auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; - req.headers.emplace("User-Agent", agent); + req.set_header("User-Agent", agent); } #endif @@ -6650,23 +6650,23 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, if (!req.is_chunked_content_provider_) { if (!req.has_header("Content-Length")) { auto length = std::to_string(req.content_length_); - req.headers.emplace("Content-Length", length); + req.set_header("Content-Length", length); } } } else { if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH") { - req.headers.emplace("Content-Length", "0"); + req.set_header("Content-Length", "0"); } } } else { if (!req.has_header("Content-Type")) { - req.headers.emplace("Content-Type", "text/plain"); + req.set_header("Content-Type", "text/plain"); } if (!req.has_header("Content-Length")) { auto length = std::to_string(req.body.size()); - req.headers.emplace("Content-Length", length); + req.set_header("Content-Length", length); } } @@ -6734,12 +6734,10 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, const std::string &content_type, Error &error) { - if (!content_type.empty()) { - req.headers.emplace("Content-Type", content_type); - } + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_) { req.headers.emplace("Content-Encoding", "gzip"); } + if (compress_) { req.set_header("Content-Encoding", "gzip"); } #endif #ifdef CPPHTTPLIB_ZLIB_SUPPORT @@ -6800,7 +6798,7 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( req.content_provider_ = detail::ContentProviderAdapter( std::move(content_provider_without_length)); req.is_chunked_content_provider_ = true; - req.headers.emplace("Transfer-Encoding", "chunked"); + req.set_header("Transfer-Encoding", "chunked"); } else { req.body.assign(body, content_length); ; @@ -7423,9 +7421,7 @@ inline Result ClientImpl::Delete(const std::string &path, req.headers = headers; req.path = path; - if (!content_type.empty()) { - req.headers.emplace("Content-Type", content_type); - } + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } req.body.assign(body, content_length); return send_(std::move(req)); diff --git a/test/test.cc b/test/test.cc index 59cf04bc1e..b57bb0bc39 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6116,3 +6116,49 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { EXPECT_EQ("val&key2=val2", res->body); } } + +TEST(VulnerabilityTest, CRLFInjection) { + Server svr; + + svr.Post("/test1", [](const Request &/*req*/, Response &res) { + res.set_content("Hello 1", "text/plain"); + }); + + svr.Delete("/test2", [](const Request &/*req*/, Response &res) { + res.set_content("Hello 2", "text/plain"); + }); + + svr.Put("/test3", [](const Request &/*req*/, Response &res) { + res.set_content("Hello 3", "text/plain"); + }); + + svr.Patch("/test4", [](const Request &/*req*/, Response &res) { + res.set_content("Hello 4", "text/plain"); + }); + + svr.set_logger([](const Request &req, const Response & /*res*/) { + for (const auto &x : req.headers) { + auto key = x.first; + EXPECT_STRNE("evil", key.c_str()); + } + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + { + Client cli(HOST, PORT); + + cli.Post("/test1", "A=B", + "application/x-www-form-urlencoded\r\nevil: hello1"); + cli.Delete("/test2", "A=B", "text/plain\r\nevil: hello2"); + cli.Put("/test3", "text", "text/plain\r\nevil: hello3"); + cli.Patch("/test4", "content", "text/plain\r\nevil: hello4"); + } +} From f8ef5fab6438898d55994fc8fcaa1e8e4bfcdfac Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 27 May 2023 00:57:43 +0900 Subject: [PATCH 0069/1222] Release v0.12.4 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 54f37a6ba3..e055dd443c 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.3" +#define CPPHTTPLIB_VERSION "0.12.4" /* * Configuration From 3409c00e6f5402eb4e300b45e44e11c3bdfe1a5c Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 27 May 2023 01:10:21 +0900 Subject: [PATCH 0070/1222] Fixed warnings --- test/test.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/test.cc b/test/test.cc index b57bb0bc39..f573cde9f7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1843,21 +1843,21 @@ class ServerTest : public ::testing::Test { { const auto &text_value = req.get_file_values("text"); - EXPECT_EQ(text_value.size(), 1); + EXPECT_EQ(1u, text_value.size()); auto &text = text_value[0]; EXPECT_TRUE(text.filename.empty()); EXPECT_EQ("default text", text.content); } { const auto &text1_values = req.get_file_values("multi_text1"); - EXPECT_EQ(text1_values.size(), 2); + EXPECT_EQ(2u, text1_values.size()); EXPECT_EQ("aaaaa", text1_values[0].content); EXPECT_EQ("bbbbb", text1_values[1].content); } { const auto &file1_values = req.get_file_values("multi_file1"); - EXPECT_EQ(file1_values.size(), 2); + EXPECT_EQ(2u, file1_values.size()); auto file1 = file1_values[0]; EXPECT_EQ(file1.filename, "hello.txt"); EXPECT_EQ(file1.content_type, "text/plain"); @@ -3921,9 +3921,10 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { res.set_header("Cache-Control", "no-cache"); res.set_chunked_content_provider("text/event-stream", [](size_t offset, DataSink &sink) { - char buffer[27]; - auto size = static_cast(sprintf(buffer, "data:%zd\n\n", offset)); - auto ret = sink.write(buffer, size); + std::string s = "data:"; + s += std::to_string(offset); + s += "\n\n"; + auto ret = sink.write(s.data(), s.size()); EXPECT_TRUE(ret); std::this_thread::sleep_for(std::chrono::seconds(1)); return true; From c54c71a3e56bad832507b0d627827bdbb8e84fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Iardella?= <49476401+rotolof@users.noreply.github.com> Date: Tue, 30 May 2023 09:08:58 +0200 Subject: [PATCH 0071/1222] Add HTTPLIB_INSTALL CMake option (#1575) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add HTTPLIB_INSTALL CMake option * Proper formatting of HTTPLIB_INSTALL block Co-authored-by: Jiwoo Park --------- Co-authored-by: Niccolò Iardella Co-authored-by: Jiwoo Park --- CMakeLists.txt | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9ec3705e0..de27d50ba8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_COMPILE (default off) + * HTTPLIB_INSTALL (default on) * HTTPLIB_TEST (default off) * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed). * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed). @@ -87,6 +88,8 @@ option(HTTPLIB_USE_OPENSSL_IF_AVAILABLE "Uses OpenSSL (if available) to enable H option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib compression support." ON) # Lets you compile the program as a regular library instead of header-only option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF) +# Lets you disable the installation (useful when fetched from another CMake project) +option(HTTPLIB_INSTALL "Enables the installation target" ON) # Just setting this variable here for people building in-tree if(HTTPLIB_COMPILE) set(HTTPLIB_IS_COMPILED TRUE) @@ -262,31 +265,33 @@ else() ) endif() -# Creates the export httplibTargets.cmake -# This is strictly what holds compilation requirements -# and linkage information (doesn't find deps though). -install(TARGETS ${PROJECT_NAME} - EXPORT httplibTargets -) +if(HTTPLIB_INSTALL) + # Creates the export httplibTargets.cmake + # This is strictly what holds compilation requirements + # and linkage information (doesn't find deps though). + install(TARGETS ${PROJECT_NAME} + EXPORT httplibTargets + ) -install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE) + install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE) -install(FILES + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" # Install it so it can be used later by the httplibConfig.cmake file. # Put it in the same dir as our config file instead of a global path so we don't potentially stomp on other packages. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindBrotli.cmake" - DESTINATION ${_TARGET_INSTALL_CMAKEDIR} -) + DESTINATION ${_TARGET_INSTALL_CMAKEDIR} + ) -# NOTE: This path changes depending on if it's on Windows or Linux -install(EXPORT httplibTargets - # Puts the targets into the httplib namespace - # So this makes httplib::httplib linkable after doing find_package(httplib) - NAMESPACE ${PROJECT_NAME}:: - DESTINATION ${_TARGET_INSTALL_CMAKEDIR} -) + # NOTE: This path changes depending on if it's on Windows or Linux + install(EXPORT httplibTargets + # Puts the targets into the httplib namespace + # So this makes httplib::httplib linkable after doing find_package(httplib) + NAMESPACE ${PROJECT_NAME}:: + DESTINATION ${_TARGET_INSTALL_CMAKEDIR} + ) +endif() if(HTTPLIB_TEST) include(CTest) From 27c0e1186c28f4f6a6864e0f8e14a2898a33a794 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 30 May 2023 16:09:42 +0900 Subject: [PATCH 0072/1222] Release v12.0.5 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index e055dd443c..fe5eafc605 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.4" +#define CPPHTTPLIB_VERSION "0.12.5" /* * Configuration From 698a1e51ecc6450b19757169736b173730c1d440 Mon Sep 17 00:00:00 2001 From: db-src Date: Fri, 2 Jun 2023 07:40:00 +0100 Subject: [PATCH 0073/1222] Move, not copy, Logger and Handler functors (#1576) * Explicitly #include for use of std::move * Move not copy Logger arg from Client to ClientImpl * Move not copy, set_error_handler Handler to lambda * Remove null statement in non-empty if/else block I guess it was a relic from a time before the other statement was added. --------- Co-authored-by: Daniel Boles --- httplib.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index fe5eafc605..ae32939781 100644 --- a/httplib.h +++ b/httplib.h @@ -223,6 +223,7 @@ using socket_t = int; #include #include #include +#include #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef _WIN32 @@ -5242,7 +5243,7 @@ inline Server &Server::set_error_handler(HandlerWithResponse handler) { } inline Server &Server::set_error_handler(Handler handler) { - error_handler_ = [handler](const Request &req, Response &res) { + error_handler_ = [handler = std::move(handler)](const Request &req, Response &res) { handler(req, res); return HandlerResponse::Handled; }; @@ -5272,7 +5273,6 @@ inline Server &Server::set_logger(Logger logger) { inline Server & Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { expect_100_continue_handler_ = std::move(handler); - return *this; } @@ -6801,7 +6801,6 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( req.set_header("Transfer-Encoding", "chunked"); } else { req.body.assign(body, content_length); - ; } } @@ -8750,7 +8749,9 @@ inline void Client::enable_server_certificate_verification(bool enabled) { } #endif -inline void Client::set_logger(Logger logger) { cli_->set_logger(logger); } +inline void Client::set_logger(Logger logger) { + cli_->set_logger(std::move(logger)); +} #ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, From 4f33637b43cc926a978e08409491d5fc08c5940d Mon Sep 17 00:00:00 2001 From: v1gnesh Date: Tue, 6 Jun 2023 10:44:06 +0530 Subject: [PATCH 0074/1222] Add support for zOS (#1581) Signed-off-by: v1gnesh --- httplib.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index ae32939781..0398253dbf 100644 --- a/httplib.h +++ b/httplib.h @@ -172,9 +172,15 @@ using socket_t = SOCKET; #else // not _WIN32 #include -#ifndef _AIX +#if !defined(_AIX) && !defined(__MVS__) #include #endif +#ifdef __MVS__ +#include +#ifndef NI_MAXHOST +#define NI_MAXHOST 1025 +#endif +#endif #include #include #include @@ -2805,7 +2811,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { return ret; } -#if !defined _WIN32 && !defined ANDROID && !defined _AIX +#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ #define USE_IF2IP #endif From 3e287b3a26cf8928dca6285176cfdba649691cc2 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 6 Jun 2023 00:56:26 -0700 Subject: [PATCH 0075/1222] Provide a CMake option to disable C++ exceptions (#1580) This allows disabling the use of C++ exceptions at CMake configure time. The value is encoded in the generated httplibTargets.cmake file and will be used by CMake projects that import it. --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index de27d50ba8..943b7a5ee1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,10 @@ string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}") project(httplib VERSION ${_httplib_version} LANGUAGES CXX) +# Lets you disable C++ exception during CMake configure time. +# The value is used in the install CMake config file. +option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF) + # Change as needed to set an OpenSSL minimum version. # This is used in the installed Cmake config file. set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1") @@ -229,6 +233,7 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} # Set the definitions to enable optional features target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} + $<$:CPPHTTPLIB_NO_EXCEPTIONS> $<$:CPPHTTPLIB_BROTLI_SUPPORT> $<$:CPPHTTPLIB_ZLIB_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> From eab5ea01d78feb08441049c08918e6f060112331 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Fri, 9 Jun 2023 16:34:51 +0900 Subject: [PATCH 0076/1222] Load in-memory CA certificates (#1579) * Load in-memory CA certs * Add test cases for in-memory cert loading * Don't use the IIFE style --- httplib.h | 41 +++++++++++++++++++++++++++++++++++++++++ test/test.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/httplib.h b/httplib.h index 0398253dbf..dd89dd2f50 100644 --- a/httplib.h +++ b/httplib.h @@ -1124,6 +1124,7 @@ class ClientImpl { void set_ca_cert_path(const std::string &ca_cert_file_path, const std::string &ca_cert_dir_path = std::string()); void set_ca_cert_store(X509_STORE *ca_cert_store); + X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size); #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1504,6 +1505,7 @@ class Client { const std::string &ca_cert_dir_path = std::string()); void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); long get_openssl_verify_result() const; @@ -1563,6 +1565,7 @@ class SSLClient : public ClientImpl { bool is_valid() const override; void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); long get_openssl_verify_result() const; @@ -7590,6 +7593,35 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { ca_cert_store_ = ca_cert_store; } } + +inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, + std::size_t size) { + auto mem = BIO_new_mem_buf(ca_cert, size); + if (!mem) return nullptr; + + auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); + if (!inf) { + BIO_free_all(mem); + return nullptr; + } + + auto cts = X509_STORE_new(); + if (cts) { + for (int first = 0, last = sk_X509_INFO_num(inf); first < last; ++first) { + auto itmp = sk_X509_INFO_value(inf, first); + if (!itmp) continue; + + if (itmp->x509) X509_STORE_add_cert(cts, itmp->x509); + + if (itmp->crl) X509_STORE_add_crl(cts, itmp->crl); + } + } + + sk_X509_INFO_pop_free(inf, X509_INFO_free); + BIO_free_all(mem); + + return cts; +} #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -7990,6 +8022,11 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { } } +inline void SSLClient::load_ca_cert_store(const char *ca_cert, + std::size_t size) { + set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); +} + inline long SSLClient::get_openssl_verify_result() const { return verify_result_; } @@ -8773,6 +8810,10 @@ inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { } } +inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { + set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); +} + inline long Client::get_openssl_verify_result() const { if (is_ssl_) { return static_cast(*cli_).get_openssl_verify_result(); diff --git a/test/test.cc b/test/test.cc index f573cde9f7..3fe0330e31 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4710,6 +4710,49 @@ TEST(SSLClientTest, ServerCertificateVerification4) { ASSERT_EQ(200, res->status); } +TEST(SSLClientTest, ServerCertificateVerification5_Online) { + std::string cert; + detail::read_file(CA_CERT_FILE, cert); + + SSLClient cli("google.com"); + cli.load_ca_cert_store(cert.data(), cert.size()); + const auto res = cli.Get("/"); + ASSERT_TRUE(res); + ASSERT_EQ(301, res->status); +} + +TEST(SSLClientTest, ServerCertificateVerification6_Online) { + // clang-format off + static constexpr char cert[] = + "GlobalSign Root CA\n" + "==================\n" + "-----BEGIN CERTIFICATE-----\n" + "MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkGA1UEBhMCQkUx\n" + "GTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jvb3QgQ0ExGzAZBgNVBAMTEkds\n" + "b2JhbFNpZ24gUm9vdCBDQTAeFw05ODA5MDExMjAwMDBaFw0yODAxMjgxMjAwMDBaMFcxCzAJBgNV\n" + "BAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMRAwDgYDVQQLEwdSb290IENBMRswGQYD\n" + "VQQDExJHbG9iYWxTaWduIFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDa\n" + "DuaZjc6j40+Kfvvxi4Mla+pIH/EqsLmVEQS98GPR4mdmzxzdzxtIK+6NiY6arymAZavpxy0Sy6sc\n" + "THAHoT0KMM0VjU/43dSMUBUc71DuxC73/OlS8pF94G3VNTCOXkNz8kHp1Wrjsok6Vjk4bwY8iGlb\n" + "Kk3Fp1S4bInMm/k8yuX9ifUSPJJ4ltbcdG6TRGHRjcdGsnUOhugZitVtbNV4FpWi6cgKOOvyJBNP\n" + "c1STE4U6G7weNLWLBYy5d4ux2x8gkasJU26Qzns3dLlwR5EiUWMWea6xrkEmCMgZK9FGqkjWZCrX\n" + "gzT/LCrBbBlDSgeF59N89iFo7+ryUp9/k5DPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV\n" + "HRMBAf8EBTADAQH/MB0GA1UdDgQWBBRge2YaRQ2XyolQL30EzTSo//z9SzANBgkqhkiG9w0BAQUF\n" + "AAOCAQEA1nPnfE920I2/7LqivjTFKDK1fPxsnCwrvQmeU79rXqoRSLblCKOzyj1hTdNGCbM+w6Dj\n" + "Y1Ub8rrvrTnhQ7k4o+YviiY776BQVvnGCv04zcQLcFGUl5gE38NflNUVyRRBnMRddWQVDf9VMOyG\n" + "j/8N7yy5Y0b2qvzfvGn9LhJIZJrglfCm7ymPAbEVtQwdpf5pLGkkeB6zpxxxYu7KyJesF12KwvhH\n" + "hm4qxFYxldBniYUr+WymXUadDKqC5JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveC\n" + "X4XSQRjbgbMEHMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" + "-----END CERTIFICATE-----\n"; + // clang-format on + + SSLClient cli("google.com"); + cli.load_ca_cert_store(cert, sizeof(cert)); + const auto res = cli.Get("/"); + ASSERT_TRUE(res); + ASSERT_EQ(301, res->status); +} + TEST(SSLClientTest, WildcardHostNameMatch_Online) { SSLClient cli("www.youtube.com"); From ed129f057fe0c1f035681c18907b1a27c5a08df8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 9 Jun 2023 20:49:46 +0900 Subject: [PATCH 0077/1222] Fixed C++11 warnings and code format --- httplib.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index dd89dd2f50..73730d2543 100644 --- a/httplib.h +++ b/httplib.h @@ -5252,7 +5252,7 @@ inline Server &Server::set_error_handler(HandlerWithResponse handler) { } inline Server &Server::set_error_handler(Handler handler) { - error_handler_ = [handler = std::move(handler)](const Request &req, Response &res) { + error_handler_ = [handler](const Request &req, Response &res) { handler(req, res); return HandlerResponse::Handled; }; @@ -7579,9 +7579,7 @@ inline void ClientImpl::set_proxy_digest_auth(const std::string &username, proxy_digest_auth_username_ = username; proxy_digest_auth_password_ = password; } -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, const std::string &ca_cert_dir_path) { ca_cert_file_path_ = ca_cert_file_path; @@ -7596,7 +7594,7 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, std::size_t size) { - auto mem = BIO_new_mem_buf(ca_cert, size); + auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); if (!mem) return nullptr; auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); @@ -7607,24 +7605,20 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, auto cts = X509_STORE_new(); if (cts) { - for (int first = 0, last = sk_X509_INFO_num(inf); first < last; ++first) { + for (auto first = 0, last = sk_X509_INFO_num(inf); first < last; ++first) { auto itmp = sk_X509_INFO_value(inf, first); - if (!itmp) continue; - - if (itmp->x509) X509_STORE_add_cert(cts, itmp->x509); + if (!itmp) { continue; } - if (itmp->crl) X509_STORE_add_crl(cts, itmp->crl); + if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } + if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); } } } sk_X509_INFO_pop_free(inf, X509_INFO_free); BIO_free_all(mem); - return cts; } -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline void ClientImpl::enable_server_certificate_verification(bool enabled) { server_certificate_verification_ = enabled; } From d3076f5a70695e35fd2c497e2e766ce72725dd69 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 10 Jun 2023 07:02:38 +0900 Subject: [PATCH 0078/1222] v0.12.6 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 73730d2543..05544a75b4 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.5" +#define CPPHTTPLIB_VERSION "0.12.6" /* * Configuration From 067890133c284ff8862eeeefdcecc540b0506014 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Fri, 16 Jun 2023 00:12:41 +0900 Subject: [PATCH 0079/1222] Use nghttp2-hosted httpbin.org (#1586) * Use nghttp2-hosted httpbin.org * Add CPPHTTPLIB_DEFAULT_HTTPBIN macro to choose the default httpbin.org --- test/test.cc | 154 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 109 insertions(+), 45 deletions(-) diff --git a/test/test.cc b/test/test.cc index 3fe0330e31..43d3f41c71 100644 --- a/test/test.cc +++ b/test/test.cc @@ -526,7 +526,13 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { } TEST(RangeTest, FromHTTPBin_Online) { +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; + auto path = std::string{"/range/32"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/range/32"}; +#endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; @@ -538,7 +544,7 @@ TEST(RangeTest, FromHTTPBin_Online) { cli.set_connection_timeout(5); { - auto res = cli.Get("/range/32"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ(200, res->status); @@ -546,7 +552,7 @@ TEST(RangeTest, FromHTTPBin_Online) { { Headers headers = {make_range_header({{1, -1}})}; - auto res = cli.Get("/range/32", headers); + auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ(206, res->status); @@ -554,7 +560,7 @@ TEST(RangeTest, FromHTTPBin_Online) { { Headers headers = {make_range_header({{1, 10}})}; - auto res = cli.Get("/range/32", headers); + auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ(206, res->status); @@ -562,7 +568,7 @@ TEST(RangeTest, FromHTTPBin_Online) { { Headers headers = {make_range_header({{0, 31}})}; - auto res = cli.Get("/range/32", headers); + auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ(200, res->status); @@ -570,7 +576,7 @@ TEST(RangeTest, FromHTTPBin_Online) { { Headers headers = {make_range_header({{0, -1}})}; - auto res = cli.Get("/range/32", headers); + auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ(200, res->status); @@ -578,7 +584,7 @@ TEST(RangeTest, FromHTTPBin_Online) { { Headers headers = {make_range_header({{0, 32}})}; - auto res = cli.Get("/range/32", headers); + auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ(416, res->status); } @@ -673,7 +679,13 @@ TEST(ConnectionErrorTest, Timeout_Online) { } TEST(CancelTest, NoCancel_Online) { +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; + auto path = std::string{"/range/32"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/range/32"}; +#endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; @@ -684,14 +696,20 @@ TEST(CancelTest, NoCancel_Online) { #endif cli.set_connection_timeout(std::chrono::seconds(5)); - auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; }); + auto res = cli.Get(path, [](uint64_t, uint64_t) { return true; }); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ(200, res->status); } TEST(CancelTest, WithCancelSmallPayload_Online) { +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; + auto path = std::string{"/range/32"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/range/32"}; +#endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; @@ -701,14 +719,20 @@ TEST(CancelTest, WithCancelSmallPayload_Online) { Client cli(host, port); #endif - auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; }); + auto res = cli.Get(path, [](uint64_t, uint64_t) { return false; }); cli.set_connection_timeout(std::chrono::seconds(5)); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } TEST(CancelTest, WithCancelLargePayload_Online) { +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; + auto path = std::string{"/range/65536"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/range/65536"}; +#endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; @@ -720,14 +744,20 @@ TEST(CancelTest, WithCancelLargePayload_Online) { cli.set_connection_timeout(std::chrono::seconds(5)); uint32_t count = 0; - auto res = cli.Get("/range/65536", - [&count](uint64_t, uint64_t) { return (count++ == 0); }); + auto res = + cli.Get(path, [&count](uint64_t, uint64_t) { return (count++ == 0); }); ASSERT_TRUE(!res); EXPECT_EQ(Error::Canceled, res.error()); } TEST(BaseAuthTest, FromHTTPWatch_Online) { +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; + auto path = std::string{"/basic-auth/hello/world"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/basic-auth/hello/world"}; +#endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT auto port = 443; @@ -738,14 +768,14 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { #endif { - auto res = cli.Get("/basic-auth/hello/world"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ(401, res->status); } { - auto res = cli.Get("/basic-auth/hello/world", - {make_basic_authentication_header("hello", "world")}); + auto res = + cli.Get(path, {make_basic_authentication_header("hello", "world")}); ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); @@ -754,7 +784,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { { cli.set_basic_auth("hello", "world"); - auto res = cli.Get("/basic-auth/hello/world"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); @@ -763,14 +793,14 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { { cli.set_basic_auth("hello", "bad"); - auto res = cli.Get("/basic-auth/hello/world"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ(401, res->status); } { cli.set_basic_auth("bad", "world"); - auto res = cli.Get("/basic-auth/hello/world"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ(401, res->status); } @@ -778,26 +808,39 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(DigestAuthTest, FromHTTPWatch_Online) { +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; + auto unauth_path = std::string{"/digest-auth/auth/hello/world"}; + auto paths = std::vector{ + "/digest-auth/auth/hello/world/MD5", + "/digest-auth/auth/hello/world/SHA-256", + "/digest-auth/auth/hello/world/SHA-512", + "/digest-auth/auth-int/hello/world/MD5", + }; +#else + auto host = "nghttp2.org"; + auto unauth_path = std::string{"/httpbin/digest-auth/auth/hello/world"}; + auto paths = std::vector{ + "/httpbin/digest-auth/auth/hello/world/MD5", + "/httpbin/digest-auth/auth/hello/world/SHA-256", + "/httpbin/digest-auth/auth/hello/world/SHA-512", + "/httpbin/digest-auth/auth-int/hello/world/MD5", + }; +#endif + auto port = 443; SSLClient cli(host, port); { - auto res = cli.Get("/digest-auth/auth/hello/world"); + auto res = cli.Get(unauth_path); ASSERT_TRUE(res); EXPECT_EQ(401, res->status); } { - std::vector paths = { - "/digest-auth/auth/hello/world/MD5", - "/digest-auth/auth/hello/world/SHA-256", - "/digest-auth/auth/hello/world/SHA-512", - "/digest-auth/auth-int/hello/world/MD5", - }; cli.set_digest_auth("hello", "world"); - for (auto path : paths) { + for (const auto &path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", @@ -806,7 +849,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { } cli.set_digest_auth("hello", "bad"); - for (auto path : paths) { + for (const auto &path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res); EXPECT_EQ(401, res->status); @@ -815,7 +858,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { // NOTE: Until httpbin.org fixes issue #46, the following test is commented // out. Please see https://httpbin.org/digest-auth/auth/hello/world // cli.set_digest_auth("bad", "world"); - // for (auto path : paths) { + // for (const auto& path : paths) { // auto res = cli.Get(path.c_str()); // ASSERT_TRUE(res); // EXPECT_EQ(400, res->status); @@ -3919,16 +3962,16 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { svr.Get("/events", [](const Request & /*req*/, Response &res) { res.set_header("Cache-Control", "no-cache"); - res.set_chunked_content_provider("text/event-stream", [](size_t offset, - DataSink &sink) { - std::string s = "data:"; - s += std::to_string(offset); - s += "\n\n"; - auto ret = sink.write(s.data(), s.size()); - EXPECT_TRUE(ret); - std::this_thread::sleep_for(std::chrono::seconds(1)); - return true; - }); + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + std::string s = "data:"; + s += std::to_string(offset); + s += "\n\n"; + auto ret = sink.write(s.data(), s.size()); + EXPECT_TRUE(ret); + std::this_thread::sleep_for(std::chrono::seconds(1)); + return true; + }); }); auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); @@ -4414,19 +4457,32 @@ TEST(GetWithParametersTest, GetWithParameters2) { } TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { - Client cli("httpbin.org"); +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN + auto host = "httpbin.org"; + auto path = std::string{"/range/32"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/range/32"}; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(host); +#else + Client cli(host); +#endif + cli.set_default_headers({make_range_header({{1, 10}})}); cli.set_connection_timeout(5); { - auto res = cli.Get("/range/32"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ(206, res->status); } { - auto res = cli.Get("/range/32"); + auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ(206, res->status); @@ -4652,8 +4708,16 @@ TEST(SSLClientTest, UpdateCAStore) { } TEST(SSLClientTest, ServerNameIndication_Online) { - SSLClient cli("httpbin.org", 443); - auto res = cli.Get("/get"); +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN + auto host = "httpbin.org"; + auto path = std::string{"/get"}; +#else + auto host = "nghttp2.org"; + auto path = std::string{"/httpbin/get"}; +#endif + + SSLClient cli(host, 443); + auto res = cli.Get(path); ASSERT_TRUE(res); ASSERT_EQ(200, res->status); } @@ -6164,19 +6228,19 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { TEST(VulnerabilityTest, CRLFInjection) { Server svr; - svr.Post("/test1", [](const Request &/*req*/, Response &res) { + svr.Post("/test1", [](const Request & /*req*/, Response &res) { res.set_content("Hello 1", "text/plain"); }); - svr.Delete("/test2", [](const Request &/*req*/, Response &res) { + svr.Delete("/test2", [](const Request & /*req*/, Response &res) { res.set_content("Hello 2", "text/plain"); }); - svr.Put("/test3", [](const Request &/*req*/, Response &res) { + svr.Put("/test3", [](const Request & /*req*/, Response &res) { res.set_content("Hello 3", "text/plain"); }); - svr.Patch("/test4", [](const Request &/*req*/, Response &res) { + svr.Patch("/test4", [](const Request & /*req*/, Response &res) { res.set_content("Hello 4", "text/plain"); }); From 4a61f68fa4a59ca97c0da1024a4b96d1aafbc0d2 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 17 Jun 2023 03:56:16 +0900 Subject: [PATCH 0080/1222] Don't overwrite the last redirected location (#1589) * Don't overwrite the last redirected location * Check the last redirected location --- httplib.h | 3 ++- test/test.cc | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 05544a75b4..ea42f14559 100644 --- a/httplib.h +++ b/httplib.h @@ -3920,7 +3920,8 @@ inline bool redirect(T &cli, Request &req, Response &res, if (ret) { req = new_req; res = new_res; - res.location = location; + + if (res.location.empty()) res.location = location; } return ret; } diff --git a/test/test.cc b/test/test.cc index 43d3f41c71..c182bb171d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -972,7 +972,7 @@ TEST(YahooRedirectTest, Redirect_Online) { res = cli.Get("/"); ASSERT_TRUE(res); EXPECT_EQ(200, res->status); - EXPECT_EQ("https://yahoo.com/", res->location); + EXPECT_EQ("https://www.yahoo.com/", res->location); } TEST(HttpsToHttpRedirectTest, Redirect_Online) { @@ -5301,7 +5301,7 @@ TEST(YahooRedirectTest2, SimpleInterface_Online) { res = cli.Get("/"); ASSERT_TRUE(res); EXPECT_EQ(200, res->status); - EXPECT_EQ("https://yahoo.com/", res->location); + EXPECT_EQ("https://www.yahoo.com/", res->location); } TEST(YahooRedirectTest3, SimpleInterface_Online) { From 8df5fedc35aa90f759a4652d07a9c99bba43aeea Mon Sep 17 00:00:00 2001 From: Andre Eisenbach Date: Fri, 16 Jun 2023 17:30:24 -0400 Subject: [PATCH 0081/1222] Fix -Wold-style-cast warnings (#1591) Removed multiple old-style (C) casts and replaced them with the appropriate _cast's. This makes httplib a better citizen inside projects that are compiled with all warnings enabled. Unfortunately one warning remains as a result of invoking an OpenSSL macro (at least on Linux), that would have to be fixed upstream. Also fixed a few casts for invocations of setsockopt. setsockopt takes a const void* for the value pointer, not a char*. Well, except on Windows ... Co-authored-by: Andre Eisenbach --- httplib.h | 81 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/httplib.h b/httplib.h index ea42f14559..f7acf2bcec 100644 --- a/httplib.h +++ b/httplib.h @@ -1675,17 +1675,17 @@ inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { inline void default_socket_options(socket_t sock) { int yes = 1; #ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), - sizeof(yes)); + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&yes), sizeof(yes)); setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&yes), sizeof(yes)); #else #ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), - sizeof(yes)); + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, + reinterpret_cast(&yes), sizeof(yes)); #else - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), - sizeof(yes)); + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&yes), sizeof(yes)); #endif #endif } @@ -2746,16 +2746,26 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (tcp_nodelay) { int yes = 1; - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&yes), - sizeof(yes)); +#ifdef _WIN32 + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, + reinterpret_cast(&yes), sizeof(yes)); +#else + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, + reinterpret_cast(&yes), sizeof(yes)); +#endif } if (socket_options) { socket_options(sock); } if (rp->ai_family == AF_INET6) { int no = 0; - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), - sizeof(no)); +#ifdef _WIN32 + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&no), sizeof(no)); +#else + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&no), sizeof(no)); +#endif } // bind or connect @@ -2898,13 +2908,14 @@ inline socket_t create_client_socket( #ifdef _WIN32 auto timeout = static_cast(read_timeout_sec * 1000 + read_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, - sizeof(timeout)); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(read_timeout_sec); tv.tv_usec = static_cast(read_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); #endif } { @@ -2912,13 +2923,14 @@ inline socket_t create_client_socket( #ifdef _WIN32 auto timeout = static_cast(write_timeout_sec * 1000 + write_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, - sizeof(timeout)); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(write_timeout_sec); tv.tv_usec = static_cast(write_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&tv), sizeof(tv)); #endif } @@ -3377,7 +3389,7 @@ inline bool brotli_decompressor::decompress(const char *data, return 0; } - const uint8_t *next_in = (const uint8_t *)data; + const uint8_t *next_in = reinterpret_cast(data); size_t avail_in = data_length; size_t total_out; @@ -4482,7 +4494,7 @@ inline std::string message_digest(const std::string &s, const EVP_MD *algo) { std::stringstream ss; for (auto i = 0u; i < hash_length; ++i) { ss << std::hex << std::setw(2) << std::setfill('0') - << (unsigned int)hash[i]; + << static_cast(hash[i]); } return ss.str(); @@ -5799,13 +5811,14 @@ inline bool Server::listen_internal() { #ifdef _WIN32 auto timeout = static_cast(read_timeout_sec_ * 1000 + read_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, - sizeof(timeout)); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(read_timeout_sec_); tv.tv_usec = static_cast(read_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); #endif } { @@ -5813,13 +5826,14 @@ inline bool Server::listen_internal() { #ifdef _WIN32 auto timeout = static_cast(write_timeout_sec_ * 1000 + write_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, - sizeof(timeout)); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(write_timeout_sec_); tv.tv_usec = static_cast(write_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&tv), sizeof(tv)); #endif } @@ -7852,8 +7866,9 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, // add default password callback before opening encrypted private key if (private_key_password != nullptr && (private_key_password[0] != '\0')) { - SSL_CTX_set_default_passwd_cb_userdata(ctx_, - (char *)private_key_password); + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, + reinterpret_cast(const_cast(private_key_password))); } if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || @@ -8167,6 +8182,10 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return true; }, [&](SSL *ssl2) { + // NOTE: With -Wold-style-cast, this can produce a warning, since + // SSL_set_tlsext_host_name is a macro (in OpenSSL), which contains + // an old style cast. Short of doing compiler specific pragma's + // here, we can't get rid of this warning. :'( SSL_set_tlsext_host_name(ssl2, host_.c_str()); return true; }); @@ -8267,8 +8286,9 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { for (decltype(count) i = 0; i < count && !dsn_matched; i++) { auto val = sk_GENERAL_NAME_value(alt_names, i); if (val->type == type) { - auto name = (const char *)ASN1_STRING_get0_data(val->d.ia5); - auto name_len = (size_t)ASN1_STRING_length(val->d.ia5); + auto name = + reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); + auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); switch (type) { case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; @@ -8286,7 +8306,8 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { if (dsn_matched || ip_matched) { ret = true; } } - GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names); + GENERAL_NAMES_free(const_cast( + reinterpret_cast(alt_names))); return ret; } From 7ab5fb65b2f17600ea84324e4b6e8300e3e6ac8f Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 16 Jun 2023 18:08:28 -0400 Subject: [PATCH 0082/1222] Code cleanup (#1593) --- httplib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index f7acf2bcec..0f7134bc04 100644 --- a/httplib.h +++ b/httplib.h @@ -2024,7 +2024,7 @@ inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, } inline std::string from_i_to_hex(size_t n) { - const char *charset = "0123456789abcdef"; + static const auto charset = "0123456789abcdef"; std::string ret; do { ret = charset[n & 15] + ret; @@ -2364,7 +2364,7 @@ inline int close_socket(socket_t sock) { } template inline ssize_t handle_EINTR(T fn) { - ssize_t res = false; + ssize_t res = 0; while (true) { res = fn(); if (res < 0 && errno == EINTR) { continue; } @@ -3389,7 +3389,7 @@ inline bool brotli_decompressor::decompress(const char *data, return 0; } - const uint8_t *next_in = reinterpret_cast(data); + auto next_in = reinterpret_cast(data); size_t avail_in = data_length; size_t total_out; From bd9612b81e6f39ab24a4be52fcda93658c0ca2dc Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 17 Jun 2023 02:18:34 -0400 Subject: [PATCH 0083/1222] Changed to use `auto` (#1594) --- httplib.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 0f7134bc04..d8b8c6d4d8 100644 --- a/httplib.h +++ b/httplib.h @@ -2013,7 +2013,7 @@ inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, val = 0; for (; cnt; i++, cnt--) { if (!s[i]) { return false; } - int v = 0; + auto v = 0; if (is_hex(s[i], v)) { val = val * 16 + v; } else { @@ -2074,8 +2074,8 @@ inline std::string base64_encode(const std::string &in) { std::string out; out.reserve(in.size()); - int val = 0; - int valb = -6; + auto val = 0; + auto valb = -6; for (auto c : in) { val = (val << 8) + static_cast(c); @@ -2206,7 +2206,7 @@ inline std::string decode_url(const std::string &s, for (size_t i = 0; i < s.size(); i++) { if (s[i] == '%' && i + 1 < s.size()) { if (s[i + 1] == 'u') { - int val = 0; + auto val = 0; if (from_hex_to_i(s, i + 2, 4, val)) { // 4 digits Unicode codes char buff[4]; @@ -2217,7 +2217,7 @@ inline std::string decode_url(const std::string &s, result += s[i]; } } else { - int val = 0; + auto val = 0; if (from_hex_to_i(s, i + 1, 2, val)) { // 2 digits hex codes result += static_cast(val); @@ -2468,7 +2468,7 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, if (poll_res == 0) { return Error::ConnectionTimeout; } if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { - int error = 0; + auto error = 0; socklen_t len = sizeof(error); auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &len); @@ -2500,7 +2500,7 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, if (ret == 0) { return Error::ConnectionTimeout; } if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { - int error = 0; + auto error = 0; socklen_t len = sizeof(error); auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &len); @@ -2745,7 +2745,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #endif if (tcp_nodelay) { - int yes = 1; + auto yes = 1; #ifdef _WIN32 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&yes), sizeof(yes)); @@ -2758,7 +2758,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (socket_options) { socket_options(sock); } if (rp->ai_family == AF_INET6) { - int no = 0; + auto no = 0; #ifdef _WIN32 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), sizeof(no)); @@ -3240,7 +3240,7 @@ inline bool gzip_compressor::compress(const char *data, size_t data_length, data += strm_.avail_in; auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; - int ret = Z_OK; + auto ret = Z_OK; std::array buff{}; do { @@ -3284,7 +3284,7 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length, Callback callback) { assert(is_valid_); - int ret = Z_OK; + auto ret = Z_OK; do { constexpr size_t max_avail_in = @@ -4587,7 +4587,7 @@ inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { auto result = false; - for (int i = 0; i < CFArrayGetCount(certs); ++i) { + for (auto i = 0; i < CFArrayGetCount(certs); ++i) { const auto cert = reinterpret_cast( CFArrayGetValueAtIndex(certs, i)); @@ -4805,7 +4805,7 @@ inline void hosted_at(const std::string &hostname, const auto &addr = *reinterpret_cast(rp->ai_addr); std::string ip; - int dummy = -1; + auto dummy = -1; if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, dummy)) { addrs.push_back(ip); @@ -7697,7 +7697,7 @@ bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, U ssl_connect_or_accept, time_t timeout_sec, time_t timeout_usec) { - int res = 0; + auto res = 0; while ((res = ssl_connect_or_accept(ssl)) != 1) { auto err = SSL_get_error(ssl, res); switch (err) { @@ -7778,7 +7778,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { auto ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret < 0) { auto err = SSL_get_error(ssl_, ret); - int n = 1000; + auto n = 1000; #ifdef _WIN32 while (--n >= 0 && (err == SSL_ERROR_WANT_READ || (err == SSL_ERROR_SYSCALL && @@ -7811,7 +7811,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); if (ret < 0) { auto err = SSL_get_error(ssl_, ret); - int n = 1000; + auto n = 1000; #ifdef _WIN32 while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || (err == SSL_ERROR_SYSCALL && From 18592e7f989685318493c14db14099f748d9b310 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Tue, 27 Jun 2023 17:09:42 -0700 Subject: [PATCH 0084/1222] Ability to turn off linking against OpenSSL if already detected. (#1602) We already detect OpenSSL in our tree for another project, but don't want to link httplib against OpenSSL or with SSL support. Allow us to `set(HTTPLIB_IS_USING_OPENSSL FALSE)`. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 943b7a5ee1..3515d77a9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,7 +120,7 @@ elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE) find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET) endif() # Just setting this variable here for people building in-tree -if(OPENSSL_FOUND) +if(OPENSSL_FOUND AND NOT DEFINED HTTPLIB_IS_USING_OPENSSL) set(HTTPLIB_IS_USING_OPENSSL TRUE) endif() From 50cba6db9fb96706a11195c76155141c661343c8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 27 Jun 2023 21:13:41 -0400 Subject: [PATCH 0085/1222] Fix #1603 --- httplib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index d8b8c6d4d8..d403b378c6 100644 --- a/httplib.h +++ b/httplib.h @@ -5853,7 +5853,7 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { } // File handler - bool is_head_request = req.method == "HEAD"; + auto is_head_request = req.method == "HEAD"; if ((req.method == "GET" || is_head_request) && handle_file_request(req, res, is_head_request)) { return true; @@ -7620,8 +7620,8 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, auto cts = X509_STORE_new(); if (cts) { - for (auto first = 0, last = sk_X509_INFO_num(inf); first < last; ++first) { - auto itmp = sk_X509_INFO_value(inf, first); + for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { + auto itmp = sk_X509_INFO_value(inf, i); if (!itmp) { continue; } if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } From fe9a1949a633985957404747fd773190a141e7ef Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 28 Jun 2023 11:28:08 -0400 Subject: [PATCH 0086/1222] Fixed an example in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 642bf8ade5..5cc674cb66 100644 --- a/README.md +++ b/README.md @@ -303,7 +303,7 @@ svr.Get("/stream", [&](const Request &req, Response &res) { res.set_content_provider( data->size(), // Content length "text/plain", // Content type - [data](size_t offset, size_t length, DataSink &sink) { + [&, data](size_t offset, size_t length, DataSink &sink) { const auto &d = *data; sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); return true; // return 'false' if you want to cancel the process. From f1daa5b88bfba652668e0acf79ada68e792c23ab Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 4 Jul 2023 20:27:11 -0400 Subject: [PATCH 0087/1222] Fix #1607 --- httplib.h | 42 +++++++++++++++++++++++++++--------------- test/test.cc | 20 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/httplib.h b/httplib.h index d403b378c6..5b42992072 100644 --- a/httplib.h +++ b/httplib.h @@ -1066,11 +1066,13 @@ class ClientImpl { bool send(Request &req, Response &res, Error &error); Result send(const Request &req); - size_t is_socket_open() const; + void stop(); - socket_t socket() const; + std::string host() const; + int port() const; - void stop(); + size_t is_socket_open() const; + socket_t socket() const; void set_hostname_addr_map(std::map addr_map); @@ -1439,11 +1441,13 @@ class Client { bool send(Request &req, Response &res, Error &error); Result send(const Request &req); - size_t is_socket_open() const; + void stop(); - socket_t socket() const; + std::string host() const; + int port() const; - void stop(); + size_t is_socket_open() const; + socket_t socket() const; void set_hostname_addr_map(std::map addr_map); @@ -7477,13 +7481,6 @@ inline Result ClientImpl::Options(const std::string &path, return send_(std::move(req)); } -inline size_t ClientImpl::is_socket_open() const { - std::lock_guard guard(socket_mutex_); - return socket_.is_open(); -} - -inline socket_t ClientImpl::socket() const { return socket_.sock; } - inline void ClientImpl::stop() { std::lock_guard guard(socket_mutex_); @@ -7507,6 +7504,17 @@ inline void ClientImpl::stop() { close_socket(socket_); } +inline std::string ClientImpl::host() const { return host_; } + +inline int ClientImpl::port() const { return port_; } + +inline size_t ClientImpl::is_socket_open() const { + std::lock_guard guard(socket_mutex_); + return socket_.is_open(); +} + +inline socket_t ClientImpl::socket() const { return socket_.sock; } + inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { connection_timeout_sec_ = sec; connection_timeout_usec_ = usec; @@ -8719,12 +8727,16 @@ inline bool Client::send(Request &req, Response &res, Error &error) { inline Result Client::send(const Request &req) { return cli_->send(req); } +inline void Client::stop() { cli_->stop(); } + +inline std::string Client::host() const { return cli_->host(); } + +inline int Client::port() const { return cli_->port(); } + inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } inline socket_t Client::socket() const { return cli_->socket(); } -inline void Client::stop() { cli_->stop(); } - inline void Client::set_hostname_addr_map(std::map addr_map) { cli_->set_hostname_addr_map(std::move(addr_map)); diff --git a/test/test.cc b/test/test.cc index c182bb171d..3f4a6f70bb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4693,6 +4693,26 @@ TEST_F(PayloadMaxLengthTest, ExceedLimit) { EXPECT_EQ(200, res->status); } +TEST(HostAndPortPropertiesTest, NoSSL) { + httplib::Client cli("www.google.com", 1234); + ASSERT_EQ("www.google.com", cli.host()); + ASSERT_EQ(1234, cli.port()); +} + +TEST(HostAndPortPropertiesTest, NoSSLWithSimpleAPI) { + httplib::Client cli("www.google.com:1234"); + ASSERT_EQ("www.google.com", cli.host()); + ASSERT_EQ(1234, cli.port()); +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(HostAndPortPropertiesTest, SSL) { + httplib::SSLClient cli("www.google.com"); + ASSERT_EQ("www.google.com", cli.host()); + ASSERT_EQ(443, cli.port()); +} +#endif + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(SSLClientTest, UpdateCAStore) { httplib::SSLClient httplib_client("www.google.com"); From 17fc522b757ae05e2296b15db00e750e726990ee Mon Sep 17 00:00:00 2001 From: bgs99 Date: Wed, 5 Jul 2023 14:44:19 +0300 Subject: [PATCH 0088/1222] Add named path parameters parsing (Implements #1587) (#1608) * Add named path parameters parsing * Select match mode based on pattern * Add examples and comments to README * Add documentation to matchers --- README.md | 9 +++ httplib.h | 212 +++++++++++++++++++++++++++++++++++++++++++++++---- test/test.cc | 152 ++++++++++++++++++++++++++++++++++++ 3 files changed, 357 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5cc674cb66..8560bcf6bf 100644 --- a/README.md +++ b/README.md @@ -94,11 +94,20 @@ int main(void) res.set_content("Hello World!", "text/plain"); }); + // Match the request path against a regular expression + // and extract its captures svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) { auto numbers = req.matches[1]; res.set_content(numbers, "text/plain"); }); + // Capture the second segment of the request path as "id" path param + svr.Get("/users/:id", [&](const Request& req, Response& res) { + auto user_id = req.path_params.at("id"); + res.set_content(user_id, "text/plain"); + }); + + // Extract values from HTTP headers and URL query params svr.Get("/body-header-param", [](const Request& req, Response& res) { if (req.has_header("Content-Length")) { auto val = req.get_header_value("Content-Length"); diff --git a/httplib.h b/httplib.h index 5b42992072..24d4f07889 100644 --- a/httplib.h +++ b/httplib.h @@ -229,6 +229,8 @@ using socket_t = int; #include #include #include +#include +#include #include #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -472,6 +474,7 @@ struct Request { MultipartFormDataMap files; Ranges ranges; Match matches; + std::unordered_map path_params; // for client ResponseHandler response_handler; @@ -665,6 +668,76 @@ using SocketOptions = std::function; void default_socket_options(socket_t sock); +namespace detail { + +class MatcherBase { +public: + virtual ~MatcherBase() = default; + + // Match request path and populate its matches and + virtual bool match(Request &request) const = 0; +}; + +/** + * Captures parameters in request path and stores them in Request::path_params + * + * Capture name is a substring of a pattern from : to /. + * The rest of the pattern is matched agains the request path directly + * Parameters are captured starting from the next character after + * the end of the last matched static pattern fragment until the next /. + * + * Example pattern: + * "/path/fragments/:capture/more/fragments/:second_capture" + * Static fragments: + * "/path/fragments/", "more/fragments/" + * + * Given the following request path: + * "/path/fragments/:1/more/fragments/:2" + * the resulting capture will be + * {{"capture", "1"}, {"second_capture", "2"}} + */ +class PathParamsMatcher : public MatcherBase { +public: + PathParamsMatcher(const std::string &pattern); + + bool match(Request &request) const override; + +private: + static constexpr char marker = ':'; + // Treat segment separators as the end of path parameter capture + // Does not need to handle query parameters as they are parsed before path + // matching + static constexpr char separator = '/'; + + // Contains static path fragments to match against, excluding the '/' after + // path params + // Fragments are separated by path params + std::vector static_fragments_; + // Stores the names of the path parameters to be used as keys in the + // Request::path_params map + std::vector param_names_; +}; + +/** + * Performs std::regex_match on request path + * and stores the result in Request::matches + * + * Note that regex match is performed directly on the whole request. + * This means that wildcard patterns may match multiple path segments with /: + * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". + */ +class RegexMatcher : public MatcherBase { +public: + RegexMatcher(const std::string &pattern) : regex_(pattern) {} + + bool match(Request &request) const override; + +private: + std::regex regex_; +}; + +} // namespace detail + class Server { public: using Handler = std::function; @@ -772,9 +845,14 @@ class Server { size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; private: - using Handlers = std::vector>; + using Handlers = + std::vector, Handler>>; using HandlersForContentReader = - std::vector>; + std::vector, + HandlerWithContentReader>>; + + static std::unique_ptr + make_matcher(const std::string &pattern); socket_t create_server_socket(const std::string &host, int port, int socket_flags, @@ -5147,6 +5225,99 @@ inline socket_t BufferStream::socket() const { return 0; } inline const std::string &BufferStream::get_buffer() const { return buffer; } +inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { + // One past the last ending position of a path param substring + std::size_t last_param_end = 0; + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + // Needed to ensure that parameter names are unique during matcher + // construction + // If exceptions are disabled, only last duplicate path + // parameter will be set + std::unordered_set param_name_set; +#endif + + while (true) { + const auto marker_pos = pattern.find(marker, last_param_end); + if (marker_pos == std::string::npos) { break; } + + static_fragments_.push_back( + pattern.substr(last_param_end, marker_pos - last_param_end)); + + const auto param_name_start = marker_pos + 1; + + auto sep_pos = pattern.find(separator, param_name_start); + if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } + + auto param_name = + pattern.substr(param_name_start, sep_pos - param_name_start); + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + if (param_name_set.find(param_name) != param_name_set.cend()) { + std::string msg = "Encountered path parameter '" + param_name + + "' multiple times in route pattern '" + pattern + "'."; + throw std::invalid_argument(msg); + } +#endif + + param_names_.push_back(std::move(param_name)); + + last_param_end = sep_pos + 1; + } + + if (last_param_end < pattern.length()) { + static_fragments_.push_back(pattern.substr(last_param_end)); + } +} + +inline bool PathParamsMatcher::match(Request &request) const { + request.matches = {}; + request.path_params.clear(); + request.path_params.reserve(param_names_.size()); + + // One past the position at which the path matched the pattern last time + std::size_t starting_pos = 0; + for (size_t i = 0; i < static_fragments_.size(); ++i) { + const auto &fragment = static_fragments_[i]; + + if (starting_pos + fragment.length() > request.path.length()) { + return false; + } + + // Avoid unnecessary allocation by using strncmp instead of substr + + // comparison + if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(), + fragment.length()) != 0) { + return false; + } + + starting_pos += fragment.length(); + + // Should only happen when we have a static fragment after a param + // Example: '/users/:id/subscriptions' + // The 'subscriptions' fragment here does not have a corresponding param + if (i >= param_names_.size()) { continue; } + + auto sep_pos = request.path.find(separator, starting_pos); + if (sep_pos == std::string::npos) { sep_pos = request.path.length(); } + + const auto ¶m_name = param_names_[i]; + + request.path_params.emplace( + param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); + + // Mark everythin up to '/' as matched + starting_pos = sep_pos + 1; + } + // Returns false if the path is longer than the pattern + return starting_pos >= request.path.length(); +} + +inline bool RegexMatcher::match(Request &request) const { + request.path_params.clear(); + return std::regex_match(request.path, request.matches, regex_); +} + } // namespace detail // HTTP server implementation @@ -5160,67 +5331,76 @@ inline Server::Server() inline Server::~Server() {} +inline std::unique_ptr +Server::make_matcher(const std::string &pattern) { + if (pattern.find("/:") != std::string::npos) { + return detail::make_unique(pattern); + } else { + return detail::make_unique(pattern); + } +} + inline Server &Server::Get(const std::string &pattern, Handler handler) { get_handlers_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Post(const std::string &pattern, Handler handler) { post_handlers_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Post(const std::string &pattern, HandlerWithContentReader handler) { post_handlers_for_content_reader_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Put(const std::string &pattern, Handler handler) { put_handlers_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Put(const std::string &pattern, HandlerWithContentReader handler) { put_handlers_for_content_reader_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Patch(const std::string &pattern, Handler handler) { patch_handlers_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Patch(const std::string &pattern, HandlerWithContentReader handler) { patch_handlers_for_content_reader_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Delete(const std::string &pattern, Handler handler) { delete_handlers_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Delete(const std::string &pattern, HandlerWithContentReader handler) { delete_handlers_for_content_reader_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } inline Server &Server::Options(const std::string &pattern, Handler handler) { options_handlers_.push_back( - std::make_pair(std::regex(pattern), std::move(handler))); + std::make_pair(make_matcher(pattern), std::move(handler))); return *this; } @@ -5930,10 +6110,10 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { inline bool Server::dispatch_request(Request &req, Response &res, const Handlers &handlers) { for (const auto &x : handlers) { - const auto &pattern = x.first; + const auto &matcher = x.first; const auto &handler = x.second; - if (std::regex_match(req.path, req.matches, pattern)) { + if (matcher->match(req)) { handler(req, res); return true; } @@ -6055,10 +6235,10 @@ inline bool Server::dispatch_request_for_content_reader( Request &req, Response &res, ContentReader content_reader, const HandlersForContentReader &handlers) { for (const auto &x : handlers) { - const auto &pattern = x.first; + const auto &matcher = x.first; const auto &handler = x.second; - if (std::regex_match(req.path, req.matches, pattern)) { + if (matcher->match(req)) { handler(req, res, content_reader); return true; } diff --git a/test/test.cc b/test/test.cc index 3f4a6f70bb..7750a511cb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4379,6 +4379,12 @@ TEST(GetWithParametersTest, GetWithParameters) { EXPECT_EQ("bar", req.get_param_value("param2")); }); + svr.Get("/users/:id", [&](const Request &req, Response &) { + EXPECT_EQ("user-id", req.path_params.at("id")); + EXPECT_EQ("foo", req.get_param_value("param1")); + EXPECT_EQ("bar", req.get_param_value("param2")); + }); + auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); auto se = detail::scope_exit([&] { svr.stop(); @@ -4419,6 +4425,15 @@ TEST(GetWithParametersTest, GetWithParameters) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); } + + { + Client cli(HOST, PORT); + + auto res = cli.Get("/users/user-id?param1=foo¶m2=bar"); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + } } TEST(GetWithParametersTest, GetWithParameters2) { @@ -6290,3 +6305,140 @@ TEST(VulnerabilityTest, CRLFInjection) { cli.Patch("/test4", "content", "text/plain\r\nevil: hello4"); } } + +TEST(PathParamsTest, StaticMatch) { + const auto pattern = "/users/all"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/all"; + ASSERT_TRUE(matcher.match(request)); + + std::unordered_map expected_params = {}; + + EXPECT_EQ(request.path_params, expected_params); +} + +TEST(PathParamsTest, StaticMismatch) { + const auto pattern = "/users/all"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/1"; + ASSERT_FALSE(matcher.match(request)); +} + +TEST(PathParamsTest, SingleParamInTheMiddle) { + const auto pattern = "/users/:id/subscriptions"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/42/subscriptions"; + ASSERT_TRUE(matcher.match(request)); + + std::unordered_map expected_params = {{"id", "42"}}; + + EXPECT_EQ(request.path_params, expected_params); +} + +TEST(PathParamsTest, SingleParamInTheEnd) { + const auto pattern = "/users/:id"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/24"; + ASSERT_TRUE(matcher.match(request)); + + std::unordered_map expected_params = {{"id", "24"}}; + + EXPECT_EQ(request.path_params, expected_params); +} + +TEST(PathParamsTest, SingleParamInTheEndTrailingSlash) { + const auto pattern = "/users/:id/"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/42/"; + ASSERT_TRUE(matcher.match(request)); + std::unordered_map expected_params = {{"id", "42"}}; + + EXPECT_EQ(request.path_params, expected_params); +} + +TEST(PathParamsTest, EmptyParam) { + const auto pattern = "/users/:id/"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users//"; + ASSERT_TRUE(matcher.match(request)); + + std::unordered_map expected_params = {{"id", ""}}; + + EXPECT_EQ(request.path_params, expected_params); +} + +TEST(PathParamsTest, FragmentMismatch) { + const auto pattern = "/users/:id/"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/admins/24/"; + ASSERT_FALSE(matcher.match(request)); +} + +TEST(PathParamsTest, ExtraFragments) { + const auto pattern = "/users/:id"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/42/subscriptions"; + ASSERT_FALSE(matcher.match(request)); +} + +TEST(PathParamsTest, MissingTrailingParam) { + const auto pattern = "/users/:id"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users"; + ASSERT_FALSE(matcher.match(request)); +} + +TEST(PathParamsTest, MissingParamInTheMiddle) { + const auto pattern = "/users/:id/subscriptions"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/subscriptions"; + ASSERT_FALSE(matcher.match(request)); +} + +TEST(PathParamsTest, MultipleParams) { + const auto pattern = "/users/:userid/subscriptions/:subid"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/users/42/subscriptions/2"; + ASSERT_TRUE(matcher.match(request)); + + std::unordered_map expected_params = { + {"userid", "42"}, {"subid", "2"}}; + + EXPECT_EQ(request.path_params, expected_params); +} + +TEST(PathParamsTest, SequenceOfParams) { + const auto pattern = "/values/:x/:y/:z"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/values/1/2/3"; + ASSERT_TRUE(matcher.match(request)); + + std::unordered_map expected_params = { + {"x", "1"}, {"y", "2"}, {"z", "3"}}; + + EXPECT_EQ(request.path_params, expected_params); +} From 5d8e7c761f596f402bd716380dadcc366b8a29d0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 5 Jul 2023 08:45:05 -0400 Subject: [PATCH 0089/1222] Updated README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8560bcf6bf..a0b183aa8c 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,8 @@ The followings are built-in mappings: | webm | video/webm | zip | application/zip | | mp3 | audio/mp3 | wasm | application/wasm | +NOTE: These static file server methods are not thread-safe. + ### File request handler ```cpp @@ -199,8 +201,6 @@ svr.set_file_request_handler([](const Request &req, Response &res) { }); ``` -NOTE: These static file server methods are not thread-safe. - ### Logging ```cpp From 0c17d172a25d5748407bb0e9ce4fc46beaaa1eab Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 5 Jul 2023 09:06:21 -0400 Subject: [PATCH 0090/1222] Code cleanup --- httplib.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 24d4f07889..ac68c6b3f7 100644 --- a/httplib.h +++ b/httplib.h @@ -895,17 +895,18 @@ class Server { virtual bool process_and_close_socket(socket_t sock); + std::atomic is_running_{false}; + std::atomic done_{false}; + struct MountPointEntry { std::string mount_point; std::string base_dir; Headers headers; }; std::vector base_dirs_; - - std::atomic is_running_{false}; - std::atomic done_{false}; std::map file_extension_and_mimetype_map_; Handler file_request_handler_; + Handlers get_handlers_; Handlers post_handlers_; HandlersForContentReader post_handlers_for_content_reader_; @@ -916,13 +917,15 @@ class Server { Handlers delete_handlers_; HandlersForContentReader delete_handlers_for_content_reader_; Handlers options_handlers_; + HandlerWithResponse error_handler_; ExceptionHandler exception_handler_; HandlerWithResponse pre_routing_handler_; Handler post_routing_handler_; - Logger logger_; Expect100ContinueHandler expect_100_continue_handler_; + Logger logger_; + int address_family_ = AF_UNSPEC; bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; SocketOptions socket_options_ = default_socket_options; From a1e56a567bf35a9dae1083ba9853589bec73f506 Mon Sep 17 00:00:00 2001 From: vmaffione <4920001+vmaffione@users.noreply.github.com> Date: Wed, 5 Jul 2023 18:05:29 +0200 Subject: [PATCH 0091/1222] Result: allow default constructor (#1609) --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ac68c6b3f7..9c65b91981 100644 --- a/httplib.h +++ b/httplib.h @@ -959,6 +959,7 @@ std::ostream &operator<<(std::ostream &os, const Error &obj); class Result { public: + Result() = default; Result(std::unique_ptr &&res, Error err, Headers &&request_headers = Headers{}) : res_(std::move(res)), err_(err), @@ -987,7 +988,7 @@ class Result { private: std::unique_ptr res_; - Error err_; + Error err_ = Error::Unknown; Headers request_headers_; }; From 82acdca638b64e0c00b43e2b192fbce0a4de9567 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 6 Jul 2023 18:32:42 -0400 Subject: [PATCH 0092/1222] Release v0.13.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 9c65b91981..9753439abb 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.12.6" +#define CPPHTTPLIB_VERSION "0.13.0" /* * Configuration From 3533503323b22e48ef4875ace082118e4b69949a Mon Sep 17 00:00:00 2001 From: bgs99 Date: Fri, 7 Jul 2023 20:17:19 +0300 Subject: [PATCH 0093/1222] Fix explicit constructor warning (#1614) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 9753439abb..e14bc7da62 100644 --- a/httplib.h +++ b/httplib.h @@ -5275,7 +5275,7 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { } inline bool PathParamsMatcher::match(Request &request) const { - request.matches = {}; + request.matches = std::smatch(); request.path_params.clear(); request.path_params.reserve(param_names_.size()); From c30906a541107df69cf09af008db749a92fa7d02 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 7 Jul 2023 19:43:37 -0400 Subject: [PATCH 0094/1222] Code cleanup --- httplib.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index e14bc7da62..ce15f404e6 100644 --- a/httplib.h +++ b/httplib.h @@ -6262,15 +6262,10 @@ Server::process_request(Stream &strm, bool close_connection, if (!line_reader.getline()) { return false; } Request req; - Response res; + Response res; res.version = "HTTP/1.1"; - - for (const auto &header : default_headers_) { - if (res.headers.find(header.first) == res.headers.end()) { - res.headers.insert(header); - } - } + res.headers = default_headers_; #ifdef _WIN32 // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). From 0f1b62c2b3d0898cbab7aa685c2593303ffdc1a2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Jul 2023 07:41:24 -0400 Subject: [PATCH 0095/1222] Release v0.13.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ce15f404e6..716cfa0208 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.13.0" +#define CPPHTTPLIB_VERSION "0.13.1" /* * Configuration From be07d2d7a99c0a54b00526f30f175e93c3588f34 Mon Sep 17 00:00:00 2001 From: Alexandre Bouvier Date: Sat, 8 Jul 2023 19:24:36 +0200 Subject: [PATCH 0096/1222] cmake: fix comment (#1616) --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3515d77a9e..4e3f50eef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,13 +256,13 @@ configure_package_config_file("${PROJECT_NAME}Config.cmake.in" if(HTTPLIB_COMPILE) write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" # Example: if you find_package(httplib 0.5.4) - # then anything >= 0.5 and <= 1.0 is accepted + # then anything >= 0.5.4 and < 0.6 is accepted COMPATIBILITY SameMinorVersion ) else() write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" # Example: if you find_package(httplib 0.5.4) - # then anything >= 0.5 and <= 1.0 is accepted + # then anything >= 0.5.4 and < 0.6 is accepted COMPATIBILITY SameMinorVersion # Tells Cmake that it's a header-only lib # Mildly useful for end-users :) From 52d8dd41f1dc239d8eceed1253631022653783cc Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Wed, 12 Jul 2023 00:32:41 +0200 Subject: [PATCH 0097/1222] build(meson): use C++14 with GTest >= 1.13.0 (#1618) GoogleTest, starting with vesion 1.13.0, requires C++14 to build. This patch enables C++14 if GoogleTest 1.13.0 or newer is detected when compiling the tests with Meson, making it possible to use new GTest versions. You can find GoogleTest's release notes at . --- test/meson.build | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/meson.build b/test/meson.build index 293ba05633..a83360d5ca 100644 --- a/test/meson.build +++ b/test/meson.build @@ -86,6 +86,12 @@ subdir(join_paths('www', 'dir')) subdir(join_paths('www2', 'dir')) subdir(join_paths('www3', 'dir')) +# GoogleTest 1.13.0 requires C++14 +test_options = [] +if gtest_dep.version().version_compare('>=1.13.0') + test_options += 'cpp_std=c++14' +endif + test( 'main', executable( @@ -94,7 +100,8 @@ test( dependencies: [ cpp_httplib_dep, gtest_dep - ] + ], + override_options: test_options ), depends: [ key_pem, From ee625232a413edb915797983cb32d8b95feff924 Mon Sep 17 00:00:00 2001 From: bdenhollander <44237618+bdenhollander@users.noreply.github.com> Date: Tue, 11 Jul 2023 18:35:27 -0400 Subject: [PATCH 0098/1222] Fix successful decompress reported as Error::Read (#1612) * Fix successful decompress reported as Error::Read Streams less than 4096 bytes are sometimes reported as failed reads because stream_.avail_in is not reduced to 0. The next iteration of the loop finds `prev_avail_in == strm_.avail_in` and return false. `ret = inflate(...)` returns Z_STREAM_END on the first iteration of the loop indicating that inflate is finished. This fix prevents the second iteration of the loop from failing. * Fix successful decompress reported as Error::Read - Add unit tests for raw deflate that illustrates the decompression failure when there are extra trailing bytes --- httplib.h | 6 +----- test/test.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 716cfa0208..2c5615c286 100644 --- a/httplib.h +++ b/httplib.h @@ -3384,16 +3384,12 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length, data += strm_.avail_in; std::array buff{}; - while (strm_.avail_in > 0) { + while (strm_.avail_in > 0 && ret == Z_OK) { strm_.avail_out = static_cast(buff.size()); strm_.next_out = reinterpret_cast(buff.data()); - auto prev_avail_in = strm_.avail_in; - ret = inflate(&strm_, Z_NO_FLUSH); - if (prev_avail_in - strm_.avail_in == 0) { return false; } - assert(ret != Z_STREAM_ERROR); switch (ret) { case Z_NEED_DICT: diff --git a/test/test.cc b/test/test.cc index 7750a511cb..93d2bdbf44 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3330,6 +3330,59 @@ TEST(GzipDecompressor, ChunkedDecompression) { ASSERT_EQ(data, decompressed_data); } +TEST(GzipDecompressor, DeflateDecompression) { + std::string original_text = "Raw deflate without gzip"; + unsigned char data[32] = {0x78, 0x9C, 0x0B, 0x4A, 0x2C, 0x57, 0x48, 0x49, + 0x4D, 0xCB, 0x49, 0x2C, 0x49, 0x55, 0x28, 0xCF, + 0x2C, 0xC9, 0xC8, 0x2F, 0x2D, 0x51, 0x48, 0xAF, + 0xCA, 0x2C, 0x00, 0x00, 0x6F, 0x98, 0x09, 0x2E}; + std::string compressed_data(data, data + sizeof(data) / sizeof(data[0])); + + std::string decompressed_data; + { + httplib::detail::gzip_decompressor decompressor; + + bool result = decompressor.decompress( + compressed_data.data(), compressed_data.size(), + [&](const char *decompressed_data_chunk, + size_t decompressed_data_chunk_size) { + decompressed_data.insert(decompressed_data.size(), + decompressed_data_chunk, + decompressed_data_chunk_size); + return true; + }); + ASSERT_TRUE(result); + } + ASSERT_EQ(original_text, decompressed_data); +} + +TEST(GzipDecompressor, DeflateDecompressionTrailingBytes) { + std::string original_text = "Raw deflate without gzip"; + unsigned char data[40] = {0x78, 0x9C, 0x0B, 0x4A, 0x2C, 0x57, 0x48, 0x49, + 0x4D, 0xCB, 0x49, 0x2C, 0x49, 0x55, 0x28, 0xCF, + 0x2C, 0xC9, 0xC8, 0x2F, 0x2D, 0x51, 0x48, 0xAF, + 0xCA, 0x2C, 0x00, 0x00, 0x6F, 0x98, 0x09, 0x2E, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + std::string compressed_data(data, data + sizeof(data) / sizeof(data[0])); + + std::string decompressed_data; + { + httplib::detail::gzip_decompressor decompressor; + + bool result = decompressor.decompress( + compressed_data.data(), compressed_data.size(), + [&](const char *decompressed_data_chunk, + size_t decompressed_data_chunk_size) { + decompressed_data.insert(decompressed_data.size(), + decompressed_data_chunk, + decompressed_data_chunk_size); + return true; + }); + ASSERT_TRUE(result); + } + ASSERT_EQ(original_text, decompressed_data); +} + #ifdef _WIN32 TEST(GzipDecompressor, LargeRandomData) { From afb0674ccb6282e458193b4d0386e057d8884eee Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 21 Jul 2023 20:32:03 -0400 Subject: [PATCH 0099/1222] Fix #1624 --- httplib.h | 146 +++++++++++++++++++++++++++--------------------------- 1 file changed, 74 insertions(+), 72 deletions(-) diff --git a/httplib.h b/httplib.h index 2c5615c286..590aa67a0c 100644 --- a/httplib.h +++ b/httplib.h @@ -668,6 +668,8 @@ using SocketOptions = std::function; void default_socket_options(socket_t sock); +const char *status_message(int status); + namespace detail { class MatcherBase { @@ -1776,6 +1778,76 @@ inline void default_socket_options(socket_t sock) { #endif } +inline const char *status_message(int status) { + switch (status) { + case 100: return "Continue"; + case 101: return "Switching Protocol"; + case 102: return "Processing"; + case 103: return "Early Hints"; + case 200: return "OK"; + case 201: return "Created"; + case 202: return "Accepted"; + case 203: return "Non-Authoritative Information"; + case 204: return "No Content"; + case 205: return "Reset Content"; + case 206: return "Partial Content"; + case 207: return "Multi-Status"; + case 208: return "Already Reported"; + case 226: return "IM Used"; + case 300: return "Multiple Choice"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 303: return "See Other"; + case 304: return "Not Modified"; + case 305: return "Use Proxy"; + case 306: return "unused"; + case 307: return "Temporary Redirect"; + case 308: return "Permanent Redirect"; + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 402: return "Payment Required"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 406: return "Not Acceptable"; + case 407: return "Proxy Authentication Required"; + case 408: return "Request Timeout"; + case 409: return "Conflict"; + case 410: return "Gone"; + case 411: return "Length Required"; + case 412: return "Precondition Failed"; + case 413: return "Payload Too Large"; + case 414: return "URI Too Long"; + case 415: return "Unsupported Media Type"; + case 416: return "Range Not Satisfiable"; + case 417: return "Expectation Failed"; + case 418: return "I'm a teapot"; + case 421: return "Misdirected Request"; + case 422: return "Unprocessable Entity"; + case 423: return "Locked"; + case 424: return "Failed Dependency"; + case 425: return "Too Early"; + case 426: return "Upgrade Required"; + case 428: return "Precondition Required"; + case 429: return "Too Many Requests"; + case 431: return "Request Header Fields Too Large"; + case 451: return "Unavailable For Legal Reasons"; + case 501: return "Not Implemented"; + case 502: return "Bad Gateway"; + case 503: return "Service Unavailable"; + case 504: return "Gateway Timeout"; + case 505: return "HTTP Version Not Supported"; + case 506: return "Variant Also Negotiates"; + case 507: return "Insufficient Storage"; + case 508: return "Loop Detected"; + case 510: return "Not Extended"; + case 511: return "Network Authentication Required"; + + default: + case 500: return "Internal Server Error"; + } +} + template inline Server & Server::set_read_timeout(const std::chrono::duration &duration) { @@ -3180,76 +3252,6 @@ find_content_type(const std::string &path, } } -inline const char *status_message(int status) { - switch (status) { - case 100: return "Continue"; - case 101: return "Switching Protocol"; - case 102: return "Processing"; - case 103: return "Early Hints"; - case 200: return "OK"; - case 201: return "Created"; - case 202: return "Accepted"; - case 203: return "Non-Authoritative Information"; - case 204: return "No Content"; - case 205: return "Reset Content"; - case 206: return "Partial Content"; - case 207: return "Multi-Status"; - case 208: return "Already Reported"; - case 226: return "IM Used"; - case 300: return "Multiple Choice"; - case 301: return "Moved Permanently"; - case 302: return "Found"; - case 303: return "See Other"; - case 304: return "Not Modified"; - case 305: return "Use Proxy"; - case 306: return "unused"; - case 307: return "Temporary Redirect"; - case 308: return "Permanent Redirect"; - case 400: return "Bad Request"; - case 401: return "Unauthorized"; - case 402: return "Payment Required"; - case 403: return "Forbidden"; - case 404: return "Not Found"; - case 405: return "Method Not Allowed"; - case 406: return "Not Acceptable"; - case 407: return "Proxy Authentication Required"; - case 408: return "Request Timeout"; - case 409: return "Conflict"; - case 410: return "Gone"; - case 411: return "Length Required"; - case 412: return "Precondition Failed"; - case 413: return "Payload Too Large"; - case 414: return "URI Too Long"; - case 415: return "Unsupported Media Type"; - case 416: return "Range Not Satisfiable"; - case 417: return "Expectation Failed"; - case 418: return "I'm a teapot"; - case 421: return "Misdirected Request"; - case 422: return "Unprocessable Entity"; - case 423: return "Locked"; - case 424: return "Failed Dependency"; - case 425: return "Too Early"; - case 426: return "Upgrade Required"; - case 428: return "Precondition Required"; - case 429: return "Too Many Requests"; - case 431: return "Request Header Fields Too Large"; - case 451: return "Unavailable For Legal Reasons"; - case 501: return "Not Implemented"; - case 502: return "Bad Gateway"; - case 503: return "Service Unavailable"; - case 504: return "Gateway Timeout"; - case 505: return "HTTP Version Not Supported"; - case 506: return "Variant Also Negotiates"; - case 507: return "Insufficient Storage"; - case 508: return "Loop Detected"; - case 510: return "Not Extended"; - case 511: return "Network Authentication Required"; - - default: - case 500: return "Internal Server Error"; - } -} - inline bool can_compress_content_type(const std::string &content_type) { using udl::operator""_t; @@ -5692,7 +5694,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, detail::BufferStream bstrm; if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, - detail::status_message(res.status))) { + status_message(res.status))) { return false; } @@ -6328,7 +6330,7 @@ Server::process_request(Stream &strm, bool close_connection, case 100: case 417: strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, - detail::status_message(status)); + status_message(status)); break; default: return write_response(strm, close_connection, req, res); } From aabf752a5101fa66d540d11fcf7a0b2c24eb88d0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 28 Jul 2023 23:26:55 -0400 Subject: [PATCH 0100/1222] Fix #1519 --- httplib.h | 76 +++++++++++++++++++++++++++++++++++++++++++--------- test/test.cc | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index 590aa67a0c..c69421ee8c 100644 --- a/httplib.h +++ b/httplib.h @@ -2428,6 +2428,13 @@ inline std::string trim_copy(const std::string &s) { return s.substr(r.first, r.second - r.first); } +inline std::string trim_double_quotes_copy(const std::string &s) { + if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { + return s.substr(1, s.size() - 2); + } + return s; +} + inline void split(const char *b, const char *e, char d, std::function fn) { size_t i = 0; @@ -4064,14 +4071,34 @@ inline bool parse_multipart_boundary(const std::string &content_type, if (pos == std::string::npos) { return false; } auto end = content_type.find(';', pos); auto beg = pos + strlen(boundary_keyword); - boundary = content_type.substr(beg, end - beg); - if (boundary.length() >= 2 && boundary.front() == '"' && - boundary.back() == '"') { - boundary = boundary.substr(1, boundary.size() - 2); - } + boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg)); return !boundary.empty(); } +inline void parse_disposition_params(const std::string &s, Params ¶ms) { + std::set cache; + split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(kv); + + std::string key; + std::string val; + split(b, e, '=', [&](const char *b2, const char *e2) { + if (key.empty()) { + key.assign(b2, e2); + } else { + val.assign(b2, e2); + } + }); + + if (!key.empty()) { + params.emplace(trim_double_quotes_copy((key)), + trim_double_quotes_copy((val))); + } + }); +} + #ifdef CPPHTTPLIB_NO_EXCEPTIONS inline bool parse_range_header(const std::string &s, Ranges &ranges) { #else @@ -4129,11 +4156,6 @@ class MultipartFormDataParser { bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, const MultipartContentHeader &header_callback) { - // TODO: support 'filename*' - static const std::regex re_content_disposition( - R"~(^Content-Disposition:\s*form-data;\s*name="(.*?)"(?:;\s*filename="(.*?)")?(?:;\s*filename\*=\S+)?\s*$)~", - std::regex_constants::icase); - buf_append(buf, n); while (buf_size() > 0) { @@ -4171,10 +4193,40 @@ class MultipartFormDataParser { if (start_with_case_ignore(header, header_name)) { file_.content_type = trim_copy(header.substr(header_name.size())); } else { + static const std::regex re_content_disposition( + R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", + std::regex_constants::icase); + std::smatch m; if (std::regex_match(header, m, re_content_disposition)) { - file_.name = m[1]; - file_.filename = m[2]; + Params params; + parse_disposition_params(m[1], params); + + auto it = params.find("name"); + if (it != params.end()) { + file_.name = it->second; + } else { + is_valid_ = false; + return false; + } + + it = params.find("filename"); + if (it != params.end()) { file_.filename = it->second; } + + it = params.find("filename*"); + if (it != params.end()) { + // Only allow UTF-8 enconnding... + static const std::regex re_rfc5987_encoding( + R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); + + std::smatch m2; + if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { + file_.filename = decode_url(m2[1], false); // override... + } else { + is_valid_ = false; + return false; + } + } } else { is_valid_ = false; return false; diff --git a/test/test.cc b/test/test.cc index 93d2bdbf44..e56e35a0a8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6105,6 +6105,69 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { } } +TEST(MultipartFormDataTest, AlternateFilename) { + Server svr; + svr.Post("/test", [&](const Request &req, Response &res) { + ASSERT_EQ(3u, req.files.size()); + + auto it = req.files.begin(); + ASSERT_EQ("file1", it->second.name); + ASSERT_EQ("A.txt", it->second.filename); + ASSERT_EQ("text/plain", it->second.content_type); + ASSERT_EQ("Content of a.txt.\r\n", it->second.content); + + ++it; + ASSERT_EQ("file2", it->second.name); + ASSERT_EQ("a.html", it->second.filename); + ASSERT_EQ("text/html", it->second.content_type); + ASSERT_EQ("Content of a.html.\r\n", + it->second.content); + + ++it; + ASSERT_EQ("text", it->second.name); + ASSERT_EQ("", it->second.filename); + ASSERT_EQ("", it->second.content_type); + ASSERT_EQ("text default", it->second.content); + + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + auto req = "POST /test HTTP/1.1\r\n" + "Content-Type: multipart/form-data;boundary=--------\r\n" + "Content-Length: 399\r\n" + "\r\n" + "----------\r\n" + "Content-Disposition: form-data; name=\"text\"\r\n" + "\r\n" + "text default\r\n" + "----------\r\n" + "Content-Disposition: form-data; filename*=\"UTF-8''\%41.txt\"; " + "filename=\"a.txt\"; name=\"file1\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Content of a.txt.\r\n" + "\r\n" + "----------\r\n" + "Content-Disposition: form-data; name=\"file2\" ;filename = " + "\"a.html\"\r\n" + "Content-Type: text/html\r\n" + "\r\n" + "Content of a.html.\r\n" + "\r\n" + "------------\r\n"; + + ASSERT_TRUE(send_request(1, req)); +} + #endif #ifndef _WIN32 From ec87b04aff6bde589e09f55610fb647b160fa81f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 29 Jul 2023 00:53:57 -0400 Subject: [PATCH 0101/1222] Fix #1619 --- httplib.h | 7 +++---- test/test.cc | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index c69421ee8c..9a26484391 100644 --- a/httplib.h +++ b/httplib.h @@ -4267,9 +4267,9 @@ class MultipartFormDataParser { buf_erase(crlf_.size()); state_ = 1; } else { - if (dash_crlf_.size() > buf_size()) { return true; } - if (buf_start_with(dash_crlf_)) { - buf_erase(dash_crlf_.size()); + if (dash_.size() > buf_size()) { return true; } + if (buf_start_with(dash_)) { + buf_erase(dash_.size()); is_valid_ = true; buf_erase(buf_size()); // Remove epilogue } else { @@ -4302,7 +4302,6 @@ class MultipartFormDataParser { const std::string dash_ = "--"; const std::string crlf_ = "\r\n"; - const std::string dash_crlf_ = "--\r\n"; std::string boundary_; std::string dash_boundary_crlf_; std::string crlf_dash_boundary_; diff --git a/test/test.cc b/test/test.cc index e56e35a0a8..5d85611865 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6106,6 +6106,8 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { } TEST(MultipartFormDataTest, AlternateFilename) { + auto handled = false; + Server svr; svr.Post("/test", [&](const Request &req, Response &res) { ASSERT_EQ(3u, req.files.size()); @@ -6130,6 +6132,8 @@ TEST(MultipartFormDataTest, AlternateFilename) { ASSERT_EQ("text default", it->second.content); res.set_content("ok", "text/plain"); + + handled = true; }); thread t = thread([&] { svr.listen(HOST, PORT); }); @@ -6137,6 +6141,7 @@ TEST(MultipartFormDataTest, AlternateFilename) { svr.stop(); t.join(); ASSERT_FALSE(svr.is_running()); + ASSERT_TRUE(handled); }); svr.wait_until_ready(); @@ -6168,6 +6173,52 @@ TEST(MultipartFormDataTest, AlternateFilename) { ASSERT_TRUE(send_request(1, req)); } +TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) { + auto handled = false; + + Server svr; + svr.Post("/test", [&](const Request &req, Response &) { + ASSERT_EQ(2u, req.files.size()); + + auto it = req.files.begin(); + ASSERT_EQ("text1", it->second.name); + ASSERT_EQ("text1", it->second.content); + + ++it; + ASSERT_EQ("text2", it->second.name); + ASSERT_EQ("text2", it->second.content); + + handled = true; + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + ASSERT_TRUE(handled); + }); + + svr.wait_until_ready(); + + auto req = "POST /test HTTP/1.1\r\n" + "Content-Type: multipart/form-data;boundary=--------\r\n" + "Content-Length: 146\r\n" + "\r\n----------\r\n" + "Content-Disposition: form-data; name=\"text1\"\r\n" + "\r\n" + "text1" + "\r\n----------\r\n" + "Content-Disposition: form-data; name=\"text2\"\r\n" + "\r\n" + "text2" + "\r\n------------"; + + std::string resonse; + ASSERT_TRUE(send_request(1, req, &resonse)); + ASSERT_EQ("200", resonse.substr(9, 3)); +} + #endif #ifndef _WIN32 From 961a9379d5dae931aa1ce21ff3e3f53889874c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ak=C4=B1n=20ELDEN?= <43653687+akinelden@users.noreply.github.com> Date: Sat, 29 Jul 2023 19:09:25 +0300 Subject: [PATCH 0102/1222] Fix #1590 (#1630) * ClientImpl: Connection=close header control moved from process_request to send_ * Connection=close header control moved from send_ to handle_request * SSLClient::connect_with_proxy error handling improved * to_string definition added for Error::ProxyConnection * Comment improvement --------- Co-authored-by: akinelden --- httplib.h | 65 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/httplib.h b/httplib.h index 9a26484391..1a6c212c83 100644 --- a/httplib.h +++ b/httplib.h @@ -950,6 +950,7 @@ enum class Error { UnsupportedMultipartBoundaryChars, Compression, ConnectionTimeout, + ProxyConnection, // For internal use only SSLPeerCouldBeClosed_, @@ -1888,6 +1889,7 @@ inline std::string to_string(const Error error) { return "Unsupported HTTP multipart boundary characters"; case Error::Compression: return "Compression failed"; case Error::ConnectionTimeout: return "Connection timed out"; + case Error::ProxyConnection: return "Proxy connection failed"; case Error::Unknown: return "Unknown"; default: break; } @@ -6748,6 +6750,21 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, if (!ret) { return false; } + if (res.get_header_value("Connection") == "close" || + (res.version == "HTTP/1.0" && res.reason != "Connection established")) { + // TODO this requires a not-entirely-obvious chain of calls to be correct + // for this to be safe. + + // This is safe to call because handle_request is only called by send_ + // which locks the request mutex during the process. It would be a bug + // to call it from a different thread since it's a thread-safety issue + // to do these things to the socket if another thread is using the socket. + std::lock_guard guard(socket_mutex_); + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + if (300 < res.status && res.status < 400 && follow_location_) { req = req_save; ret = redirect(req, res, error); @@ -7162,24 +7179,6 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } } - if (res.get_header_value("Connection") == "close" || - (res.version == "HTTP/1.0" && res.reason != "Connection established")) { - // TODO this requires a not-entirely-obvious chain of calls to be correct - // for this to be safe. Maybe a code refactor (such as moving this out to - // the send function and getting rid of the recursiveness of the mutex) - // could make this more obvious. - - // This is safe to call because process_request is only called by - // handle_request which is only called by send, which locks the request - // mutex during the process. It would be a bug to call it from a different - // thread since it's a thread-safety issue to do these things to the socket - // if another thread is using the socket. - std::lock_guard guard(socket_mutex_); - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); - } - // Log if (logger_) { logger_(req, res); } @@ -8287,14 +8286,14 @@ inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, bool &success, Error &error) { success = true; - Response res2; + Response proxy_res; if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { Request req2; req2.method = "CONNECT"; req2.path = host_and_port_; - return process_request(strm, req2, res2, false, error); + return process_request(strm, req2, proxy_res, false, error); })) { // Thread-safe to close everything because we are assuming there are no // requests in flight @@ -8305,12 +8304,12 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, return false; } - if (res2.status == 407) { + if (proxy_res.status == 407) { if (!proxy_digest_auth_username_.empty() && !proxy_digest_auth_password_.empty()) { std::map auth; - if (detail::parse_www_authenticate(res2, auth, true)) { - Response res3; + if (detail::parse_www_authenticate(proxy_res, auth, true)) { + proxy_res = Response(); if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { @@ -8321,7 +8320,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, req3, auth, 1, detail::random_string(10), proxy_digest_auth_username_, proxy_digest_auth_password_, true)); - return process_request(strm, req3, res3, false, error); + return process_request(strm, req3, proxy_res, false, error); })) { // Thread-safe to close everything because we are assuming there are // no requests in flight @@ -8332,12 +8331,24 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, return false; } } - } else { - res = res2; - return false; } } + // If status code is not 200, proxy request is failed. + // Set error to ProxyConnection and return proxy response + // as the response of the request + if (proxy_res.status != 200) + { + error = Error::ProxyConnection; + res = std::move(proxy_res); + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + return false; + } + return true; } From e699bd0730695eb56aac2e12093d0807e336906f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 29 Jul 2023 12:26:19 -0400 Subject: [PATCH 0103/1222] Release v0.13.2 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 1a6c212c83..9cb49ccfeb 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.13.1" +#define CPPHTTPLIB_VERSION "0.13.2" /* * Configuration From 01b90829bc8f353c90be01f0601cc1e68239dee8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 29 Jul 2023 23:01:47 -0400 Subject: [PATCH 0104/1222] Removed unnecessary CRLF at the end of multipart ranges data --- httplib.h | 2 +- test/test.cc | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 9cb49ccfeb..636dafe74f 100644 --- a/httplib.h +++ b/httplib.h @@ -4530,7 +4530,7 @@ bool process_multipart_ranges_data(const Request &req, Response &res, ctoken("--"); stoken(boundary); - ctoken("--\r\n"); + ctoken("--"); return true; } diff --git a/test/test.cc b/test/test.cc index 5d85611865..893080d091 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2886,9 +2886,9 @@ TEST_F(ServerTest, GetStreamedWithRangeMultipart) { cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}}); ASSERT_TRUE(res); EXPECT_EQ(206, res->status); - EXPECT_EQ("269", res->get_header_value("Content-Length")); + EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(269U, res->body.size()); + EXPECT_EQ(267U, res->body.size()); } TEST_F(ServerTest, GetStreamedEndless) { @@ -2978,9 +2978,9 @@ TEST_F(ServerTest, GetWithRangeMultipart) { auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}}); ASSERT_TRUE(res); EXPECT_EQ(206, res->status); - EXPECT_EQ("269", res->get_header_value("Content-Length")); + EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(269U, res->body.size()); + EXPECT_EQ(267U, res->body.size()); } TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { From 2e34a39673c820d332992dc9cb6eb38e6cfe3fcd Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 29 Jul 2023 23:02:58 -0400 Subject: [PATCH 0105/1222] Added StaticFileRanges test --- test/test.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test.cc b/test/test.cc index 893080d091..7369d8e258 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2501,6 +2501,19 @@ TEST_F(ServerTest, StaticFileRange) { EXPECT_EQ(std::string("cd"), res->body); } +TEST_F(ServerTest, StaticFileRanges) { + auto res = + cli_.Get("/dir/test.abcde", {{make_range_header({{1, 2}, {4, -1}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(206, res->status); + EXPECT_TRUE( + res->get_header_value("Content-Type") + .find( + "multipart/byteranges; boundary=--cpp-httplib-multipart-data-") == + 0); + EXPECT_EQ("266", res->get_header_value("Content-Length")); +} + TEST_F(ServerTest, InvalidBaseDirMount) { EXPECT_EQ(false, svr_.set_mount_point("invalid_mount_point", "./www3")); } From 00a8cb8e5d3a834d7f441706844d369b72272d61 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 30 Jul 2023 13:23:32 -0400 Subject: [PATCH 0106/1222] Code cleanup --- httplib.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 636dafe74f..7b2bf3715d 100644 --- a/httplib.h +++ b/httplib.h @@ -4111,7 +4111,7 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { if (std::regex_match(s, m, re_first_range)) { auto pos = static_cast(m.position(1)); auto len = static_cast(m.length(1)); - bool all_valid_ranges = true; + auto all_valid_ranges = true; split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { if (!all_valid_ranges) return; static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))"); @@ -6390,7 +6390,7 @@ Server::process_request(Stream &strm, bool close_connection, } // Rounting - bool routed = false; + auto routed = false; #ifdef CPPHTTPLIB_NO_EXCEPTIONS routed = routing(req, res, strm); #else @@ -7206,7 +7206,7 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( } DataSink cur_sink; - bool has_data = true; + auto has_data = true; cur_sink.write = sink.write; cur_sink.done = [&]() { has_data = false; }; @@ -8335,10 +8335,9 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, } // If status code is not 200, proxy request is failed. - // Set error to ProxyConnection and return proxy response + // Set error to ProxyConnection and return proxy response // as the response of the request - if (proxy_res.status != 200) - { + if (proxy_res.status != 200) { error = Error::ProxyConnection; res = std::move(proxy_res); // Thread-safe to close everything because we are assuming there are @@ -8353,7 +8352,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, } inline bool SSLClient::load_certs() { - bool ret = true; + auto ret = true; std::call_once(initialize_cert_, [&]() { std::lock_guard guard(ctx_mutex_); From 6bb580cda8fd3063e7141588d96db087b9e50420 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 30 Jul 2023 23:33:27 -0400 Subject: [PATCH 0107/1222] Fix #1559 --- httplib.h | 79 +- test/test.cc | 38 +- test/www/dir/1MB.txt | 8192 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 8286 insertions(+), 23 deletions(-) create mode 100644 test/www/dir/1MB.txt diff --git a/httplib.h b/httplib.h index 7b2bf3715d..22858ff177 100644 --- a/httplib.h +++ b/httplib.h @@ -90,6 +90,10 @@ #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) #endif +#ifndef CPPHTTPLIB_SEND_BUFSIZ +#define CPPHTTPLIB_SEND_BUFSIZ size_t(4096u) +#endif + #ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ #define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) #endif @@ -784,6 +788,7 @@ class Server { bool remove_mount_point(const std::string &mount_point); Server &set_file_extension_and_mimetype_mapping(const std::string &ext, const std::string &mime); + Server &set_default_file_mimetype(const std::string &mime); Server &set_file_request_handler(Handler handler); Server &set_error_handler(HandlerWithResponse handler); @@ -907,6 +912,7 @@ class Server { }; std::vector base_dirs_; std::map file_extension_and_mimetype_map_; + std::string default_file_mimetype_ = "application/octet-stream"; Handler file_request_handler_; Handlers get_handlers_; @@ -3197,9 +3203,10 @@ inline constexpr unsigned int operator"" _t(const char *s, size_t l) { } // namespace udl -inline const char * +inline std::string find_content_type(const std::string &path, - const std::map &user_data) { + const std::map &user_data, + const std::string &default_content_type) { auto ext = file_extension(path); auto it = user_data.find(ext); @@ -3208,7 +3215,8 @@ find_content_type(const std::string &path, using udl::operator""_t; switch (str2tag(ext)) { - default: return nullptr; + default: return default_content_type; + case "css"_t: return "text/css"; case "csv"_t: return "text/csv"; case "htm"_t: @@ -4489,12 +4497,13 @@ get_range_offset_and_length(const Request &req, size_t content_length, return std::make_pair(r.first, static_cast(r.second - r.first) + 1); } -inline std::string make_content_range_header_field(size_t offset, size_t length, - size_t content_length) { +inline std::string +make_content_range_header_field(const std::pair &range, + size_t content_length) { std::string field = "bytes "; - field += std::to_string(offset); + if (range.first != -1) { field += std::to_string(range.first); } field += "-"; - field += std::to_string(offset + length - 1); + if (range.second != -1) { field += std::to_string(range.second); } field += "/"; field += std::to_string(content_length); return field; @@ -4516,14 +4525,15 @@ bool process_multipart_ranges_data(const Request &req, Response &res, ctoken("\r\n"); } - auto offsets = get_range_offset_and_length(req, res.body.size(), i); - auto offset = offsets.first; - auto length = offsets.second; - ctoken("Content-Range: "); - stoken(make_content_range_header_field(offset, length, res.body.size())); + const auto &range = req.ranges[i]; + stoken(make_content_range_header_field(range, res.content_length_)); ctoken("\r\n"); ctoken("\r\n"); + + auto offsets = get_range_offset_and_length(req, res.content_length_, i); + auto offset = offsets.first; + auto length = offsets.second; if (!content(offset, length)) { return false; } ctoken("\r\n"); } @@ -5493,6 +5503,11 @@ Server::set_file_extension_and_mimetype_mapping(const std::string &ext, return *this; } +inline Server &Server::set_default_file_mimetype(const std::string &mime) { + default_file_mimetype_ = mime; + return *this; +} + inline Server &Server::set_file_request_handler(Handler handler) { file_request_handler_ = std::move(handler); return *this; @@ -5944,17 +5959,35 @@ inline bool Server::handle_file_request(const Request &req, Response &res, if (path.back() == '/') { path += "index.html"; } if (detail::is_file(path)) { - detail::read_file(path, res.body); - auto type = - detail::find_content_type(path, file_extension_and_mimetype_map_); - if (type) { res.set_header("Content-Type", type); } for (const auto &kv : entry.headers) { res.set_header(kv.first.c_str(), kv.second); } - res.status = req.has_header("Range") ? 206 : 200; + + auto fs = + std::make_shared(path, std::ios_base::binary); + + fs->seekg(0, std::ios_base::end); + auto size = static_cast(fs->tellg()); + fs->seekg(0); + + res.set_content_provider( + size, + detail::find_content_type(path, file_extension_and_mimetype_map_, + default_file_mimetype_), + [fs](size_t offset, size_t length, DataSink &sink) -> bool { + std::array buf{}; + length = std::min(length, CPPHTTPLIB_SEND_BUFSIZ); + + fs->seekg(static_cast(offset), std::ios_base::beg); + fs->read(buf.data(), static_cast(length)); + sink.write(buf.data(), length); + return true; + }); + if (!head && file_request_handler_) { file_request_handler_(req, res); } + return true; } } @@ -6202,10 +6235,10 @@ inline void Server::apply_ranges(const Request &req, Response &res, } else if (req.ranges.size() == 1) { auto offsets = detail::get_range_offset_and_length(req, res.content_length_, 0); - auto offset = offsets.first; length = offsets.second; + auto content_range = detail::make_content_range_header_field( - offset, length, res.content_length_); + req.ranges[0], res.content_length_); res.set_header("Content-Range", content_range); } else { length = detail::get_multipart_ranges_data_length(req, res, boundary, @@ -6228,13 +6261,15 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (req.ranges.empty()) { ; } else if (req.ranges.size() == 1) { + auto content_range = detail::make_content_range_header_field( + req.ranges[0], res.body.size()); + res.set_header("Content-Range", content_range); + auto offsets = detail::get_range_offset_and_length(req, res.body.size(), 0); auto offset = offsets.first; auto length = offsets.second; - auto content_range = detail::make_content_range_header_field( - offset, length, res.body.size()); - res.set_header("Content-Range", content_range); + if (offset < res.body.size()) { res.body = res.body.substr(offset, length); } else { diff --git a/test/test.cc b/test/test.cc index 7369d8e258..217991b2b3 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2511,7 +2511,43 @@ TEST_F(ServerTest, StaticFileRanges) { .find( "multipart/byteranges; boundary=--cpp-httplib-multipart-data-") == 0); - EXPECT_EQ("266", res->get_header_value("Content-Length")); + EXPECT_EQ("265", res->get_header_value("Content-Length")); +} + +TEST_F(ServerTest, StaticFileRangeHead) { + auto res = cli_.Head("/dir/test.abcde", {{make_range_header({{2, 3}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(206, res->status); + EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); + EXPECT_EQ("2", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); +} + +TEST_F(ServerTest, StaticFileRangeBigFile) { + auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{-1, 5}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(206, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("5", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("LAST\n", res->body); +} + +TEST_F(ServerTest, StaticFileRangeBigFile2) { + auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{1, 4097}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(206, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("4097", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); +} + +TEST_F(ServerTest, StaticFileBigFile) { + auto res = cli_.Get("/dir/1MB.txt"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("1048576", res->get_header_value("Content-Length")); } TEST_F(ServerTest, InvalidBaseDirMount) { diff --git a/test/www/dir/1MB.txt b/test/www/dir/1MB.txt new file mode 100644 index 0000000000..f5a9c204fe --- /dev/null +++ b/test/www/dir/1MB.txt @@ -0,0 +1,8192 @@ +9254835974458887629672873635789957411886024698554157393849494864228024962939550688297074527198420261051675205999609689838587412 +7948702662533481896767559573369920938242346354580061545409242090168773727371802699309443935396635866263937828773324526334321892 +7929250312741837331511829643632683169694074912332726993582394725302853411901337696207186358524323117172520907433878952968176465 +9486937364148093931718552300016332142708943190856638524388888569011747617956915519539025796115901484762122047712200094207683584 +0703675740855407318047361595661595146837376373951978537785605481083388906490085533348547865459237835407372374738389274773789264 +3524314516560200536698529022539598732463389124803873184044464663165630452635665559603483233341839268186056673186867104904449866 +3388466377320953222057779182433549144340237502432464295061371141084500222833875925546082542869030852833895137466510262849050187 +2359980877010447170873386178573828860442255448874794721230413368694441497441338856684036949118353204002591974711928301953002372 +6372613557152801003023836434406997863096739346637849381675791163293956729985652182807911615231198696051411047800847064484769940 +4555399892730282159333189930818248844009737851281434494736943722577947684829017806688345340403991974811107725657399196027995602 +3976607710681047393460981746843991626375149576917517874980586155555711990727221948467045177979554215577477183785345135535682148 +8981224019853812859833307577420310576654838103212699667682513798088777109119729024038590682109547975734687407898816774007038892 +9017123764104410282402525387418186790830943654104676998756681754083664849203556615995024922746652778319099999725942494552746687 +4602993085345245279681119328539229356959910969065035141679707233267342542524482900839390718291823532073464112692162110160887576 +8646564120617234541914353449310981315044240998894442066188871011899825887695202156119273906958004776559173081177243291448706570 +4866033151494706872899672197703352421058356661105030485577210664650513250767511742264039882291373723618030276740280451621184372 +9438899492601727676139376045138079061355198567322008270344297244790265795845160675322685239683610065522225253070076549915962270 +8389935622999431104891971065250599273560270041530059453015141663393829191008304620453278274234791447194116642959854046669653221 +4390066870930359126190473237338490788280116222078414392005864878004044572924012489353108185972058375678318527434060575546457283 +0571024164252835918003185534087097269093594036226831977994875139354124615791501602629221816855731144878875700135493432353006251 +5927821910139360288795697248742701824779517711136895716699422565064551330393130338804982871980774254446227613970394730940420871 +3742377272014443550088749485134437400845098911378170974616143540710504172620523980130687874417124218036934374655684373965565750 +1478423915766121994159683752043056020433068572256778826939436280481378432842397489866548450027752963017529841096517341127140258 +8566764493817154363962092792522076892737665735147823735202597353071978920287229637466345140359899735477359400126088124931881468 +8940255760337363163978793913265843793885760290540433771518954295210648595347128727773745692808704267887587662204824695198843990 +0899757006576147578169404543275069681042994730814241204020586377450354179962282687398746645565905502250948590090052580759060254 +6486974981051360582508378437652320531515639324364424491392656403886587101611394285202791775180181840494403192370521504242888310 +0832863857962097233772304981176182241414796661234070317886749751979535621508014316193777356696545211120669034308685700669047575 +1601920560631092775469657814901382577801938745924198963515833985856042317888652481510158158143597978988591350058772254540887509 +2132235148831014717469033542114409228818750252221985002159849994061876844567039235968616101425307518401162305005282076459564485 +3749005491254116003221964938075605607977980187695543266859111481060871108661145469674590183816636520815523589998805144183387552 +3454557776795930937812464849331806246436437524220402821725671887519345824588678830784061509096255092713692879753496348543679698 +6604711892156464428503873379368266697844691398807542828869396127474509499861163107833096454590997165019449027223124752455672795 +1464056065563451406292025100441586772081609300298880665395988771146415490692648836974652685280576941091682396423173897267646839 +0355383165630525414486907132932207574564820206814521900700480593494653392345246480522618317569197186894847097184780188176595384 +2234714652935409724403164799544553996270776652429105659472464101488105672623286713328942045106946821859926624587221567145826782 +0309935578008890373154226142294034025862773115052182464935233315361839771441799453095926400038395389556682588458216003237653863 +1061156071932484650516913321439215405043703716040614962270232640142284681169472791577978618224769535957766957086206741391709970 +7647351716205014294072195135184469100518783539374945598848168573993875333505334011212625117442647232720744618603822330582078365 +6190042304849971211815384939288709598033150233044255711655989057889996862762413082297341687788607689416591830790638752880761817 +3350993967482666602170935703513835999165561197161777938477157925978429670778105685120890113130859239045083522459241247368307415 +5460081669964922086720150722963115784946235169601550836667474277946931369187925788894576312382075479577915204545165252523196796 +9483728931707116749644793319165741326903913011722074179317419072713429472369240811255731316334667969479890293999075667048649131 +3479265891282260862086967904220845208360231245372151218473448305464967324382299780822943482467600298687513476936031155077963601 +5358607166888753271639287331221882667306965170073890751994864930247242443182503197751744519602399141153643026523167858853130538 +7454234112327473005509721991333469328638161495819890863131852055720398755713433050343878076412540696773945682790587599473898772 +1829500286670935139918198012253669665196719074234685642111242009658467409729732638466770339415767564217863874440642300491597266 +5585497029761221358413402963546931530898077983556347511671398790492531247609197801647309393017719703556474420667077635431137035 +8446511749461154540070308470454488783535318141900958816289412388611370858697615498746416636102261400237788596935028958347787028 +3125589335242227429164632721324285385335478344437654660712476162463759322956724441170491898597020350367163787012219584863841888 +6802337989299969519242898184367047999251583706804577726542893830886334416591927545136810611568115854449708934129157129519681281 +4502536799805954583144549032621959969753071050956941684132891200046324576389173182566153807220810192479638581311891427849367417 +6325541021938064574977797529368876421571303611445571554655335842928934553745122110842178236045825160364424732178843094315127909 +7416551206319999579172591987511489686313571898121634456963111089610744020435972487389528243640299599133169203384400119064268257 +5942558924368052900576494867848083529988242331335385049200017447913990221665638834941940132033114280719741451021709322013495656 +3034267296680277379429583962288582457402893052674209528241394670330734634693333770067544918231098413897817493181756172191761769 +4462568735945386723562623780129364617626340957792264088426814103842409805756856047587770203362285297182877719759239486173167134 +8181657858983878076788365457679611406051878073922935400724071002626732091440173091689286855430601190105542880854867957669944342 +1682764258327911438109776589215105175058720345720016147450773913501679216877023673201387960875767306750352156509386560915532694 +4168453046768399507263930483191895109159119018724369742162771101927944398681984030667327651300421681200981291741743361136642792 +5961214949092908589726506803431211408890378633711553535211419472586275853572210445315467622414789208830684013971300686277675328 +9269710543143964341467182430089608998710751826481978504336310277969086698368677287182389959721242273938854619109417313330166529 +8486459999966843090378171773263091325696386996545517566407679424699279968298703891528397394384066258625248890398189881141241156 +1449849546688513154852662654151484110306993368687380709636409815697889872688403677017676988174939843342948688833489247136945501 +3026821425662283243215241228426396875362999466310497066906176240972976192127107346129568606866349374029240104708104443230866441 +4888599832610923002904596161762304764008702325429097763470366526898011656507069583771589908698710318907474941536590688671464002 +2187786798670885931014137550230451723811271617853032443909991254363190383287294838940424051082728434647528986018234698035977262 +4010150476565439429491060857277549534561616385054516756344772776300804281913985317098584407945908254666687113999982299034911916 +9543106789274635042741853569705907409683466986445551664737067041728196021666244823741197574167419992584104093228192902092720801 +1102588771206288521669854794170734776396759389699841902844928003578465247527168100807066013305698109312645455547945036883046986 +3363256734764708042924448420459037705906529627671525144633382899415555101340497992824046104327698365969273385304329013580634534 +5814024400502542484138080303093675479908925878015957334513036393579080542663814740530799226052633254072603206379019404756963615 +8568459878266889844587337958272179244147420281407959235700418194754222652142770182396574372220705526302797889629403089542338729 +8226364273353855713782831639652716359809735693921459950536413341806490125092787610897542413754704628839004798504470781343226879 +9564108089806303912196187245286576464060313163695650258929860024346682416259472890719900060340274063102211811727469052257146328 +0723807096388817245239864356681843226170736144832701238842559718295427226555369395998370081796589238415142792914737246512392617 +4241424816471283201248965000004171003042756134627995493041482479770591013182623840449510466593158465328399081296605282376713554 +9973513220590332519460381924338347505856779256581376567327469745338170108702340101128824216183825500136464452346025348049667485 +8383897971629124447860057453350337067099519586684049058435585462779736095630666963739484221218567448740285624710665297561214099 +9964116268591382706460872763874878534297946361217165257274977145088020502651470013064060961213429959898581048432243027406481577 +4824315082578790346775943088343852549806522479147355182415674805204204673847884219703487656256651056490611337757236524094938348 +7062382447597254598729362880233858271292436951748099256624309342834053225940366427538355199347489811241328865453894894705029296 +9735693411211619628088603452483215272175364970550497151583596541206523461527332458408984429879086514102509879665768932131692226 +6961688387629152540756216546017259642317857065168508958573052496210655276891659756089714383999813015316305245290357709870563638 +3863034723721495784723589237391107262786771026496285596990386769945707595340965195448259401890929565886615037974745228156868323 +3287577904832967842610714371567414265151412481754216777139400930821306278453396427971928947503983582862250196984342590738386627 +1791128295449101351282899590128118669109794265058849951756999066974826947872800232214872176234126177766951584517353226892319930 +8805052099840613606036822579273082344123139982418321238213526119536401574939527633217151490658267375799590895845553195225375174 +1355928510541796297379079857922955866076933929404227257481153317061720856509437585235189609477388103725696248255380889259925943 +1402201875930840177564779603131963190398447445546273482826943610197406605496467698637182093544726028048446184407465904504733488 +1834037129267281181848805100644706716446934703366578946950288560270548606368029542736413168537166947783032181537827321728818305 +2399588088866368871684803227983829485917501647912319399696731866908470118289600881964063135888922021379521028829404789266368968 +5637489977535077950426899162426475829892339881113388204978816994197921201698140466319573566765752657389499556451516488232245559 +6112692169213605381302831305889438504096793315962284966459192757358537426473374519814657784899843476819443537896967951107338491 +7895505461382759098793722523795396339534176000429147383719128841803883949006205025948100090981495836498310874463726230844486437 +9522042614500829901321795525603265978871144309432521198633117362072879318547949126425751870262081563243482328537146079314556599 +5093542621599279420676140514142072884094585996707002095359240117940447064308608734332029368110977503867431654445743620282821742 +2074818355004815481495949283111185240895061522427508730372487670815127557063465146879286952537668866636765956480834805349247418 +0982616183770170277896101663961256388046096956303414645662042291995673037815635718874781061367093483701372208717092453548930045 +3197990341408426456860501773717322183556963874016603497159407408857792270849795326742052795163483613191779561704529979153717213 +0442381165605136382176204027769856937910886725474913822447353042136166362618403395513606165988761350028767809916274534709139581 +1546533500647539778292217072989397950692244341857345231008319214990106951110301762058019968223873956867021450221558460837271796 +1089583682950702039719953402978240768135379617758154644301927987331573523265837746123630494418924793748993674977892210713791382 +2010498095473532597719948269554932451554991652103201520706494302534512806373772862577948592827967992187111665796229241795272327 +4495947525239030494005205359163320911969546009892986032715554824732415447400580445227801191434206229755544175085033640445463730 +5799343664533857261737308836915890943267341995945607000250119768647338018370977116150224513412795224930773162810128318361091070 +2845165641335198012087900200223115487995666447653144922411381435396653532150893427712002942030941647409112450887593516206623257 +5995936189970345951128332927675055555031225056766626864377451946716279426300206796784320339236602175243416848009874418064376734 +2751388225971927284674719275737386426623723915170272496656834116124167598744380632642578314683658323664087930294748983479134152 +4463890675024338744201454760164930410895360686852249316115526602608580347281230566360947860956394767605509297995646286805155429 +3902535272296902694906404269870261097232828009262961508419387381919670436136425912908116240583462930917866834873246313946702767 +0171550221706917129701789274673785241823132172160837286626181116666263103300493781986590027327174868610919833041462749008446670 +1233446436949798212579086729206735034953949556045481891487880919889748751337486349345808929953562851746791383784762700811827813 +3273320169223863915019571833988285591541029709234409949795661766708317004888223115613777281294607726043777706813741640111212754 +2476840221289778122524588163601454015024966289209119530584971298419043747426874336591254749329149388984462650479516131162264547 +8321322112340265706034027413610131445579234122444443539849216325832257070066442940612976748450142222873158683339615189385385401 +2845301295555610107931163495080379964949955895954082606347174241430747973616106979000794362756653542557197433894470866065773098 +7002289110540946230118386265586199860767183813803128176222583812066464632835592487506968002253203796340562076570806978606547027 +0518614265711371822209002835579969441398099868205035333550662371029236766960492501859441092496004547436756442048667962205035734 +4662742623053069471722274771033535333247780680117662494001501801076484609461801093095512629224389725470873708072969419161134986 +8879423231727024904174405990021786946862967043559824446453539979244586561718968470449066018656109597862462444251052806647222165 +2462868109247823087920350263249330583062507248433983310105749602411182363633125913535849740456631462938827755069526554867046297 +0556317986368239387236732047607114173950737209007825151623731055762898522619516377121452352573813859619423710420648830645140602 +0693502619716624171936460417629499350461603844160041861287075322351538559343745415528939056080652464222483749575581089457335292 +4766904028429523888256867990946783862212788485169555738269492360138212802976905819698072914952002888319407814152289904290372830 +3063149485419553279766015944554584134585112055860455417890621924246024514671960271622075351843821409825893454426591239346175825 +4138071925467858356596514010874840756148724225545969356853427243328235466057330927837715365449101436095007553412409485033663473 +1718208277388066697955637942282188804022877425156297722332673824998500535568426990569492133060260476735245935911435663031104540 +7259896781146483425619092546253937377799155709431757305830799439617510543866635784180461435471514956432054478723110546701399601 +4212208190748800869765878332993087846354727233690197986460622890272309218632979045510485708917467310543076772888886969650697402 +5830555875156620379814707898244277917838097196097913596741388644704517369937389656755811401456911697429799824431768975340394565 +5218384019129708961023696421751573471619498755822698446106554346210228582416782419196987283985815988033363104751364948821321736 +1511408675805990625863697514171112598891753956197059919478438005392395089087719396999097527167894172833319566356364163919692543 +6760864061185222844062388472607262118669410742190580800609818458141651702885176634837319000673951554557807668749868767849900633 +4624693982782044541974663953840367719217355838087352148000020445830027606519679992919499646684217452676557562945022247829061975 +2885293059896283301736153794239325555898371313303885556968069349802848506212069388120502744705750493536808390340479824477084109 +4742565846910523167821445663540712005957374896244507720218360942226085109738527921622092099772083368812073433747682045449511884 +6164131243427554092783794384838205105688550227514505179310025715143470000166045228305059905780171994193190773288044579516190413 +6196746209214286300600159852801523856096528659384736317041341206058064233216653841144231397934288134905891354902685060365011048 +5888543435289034798476752259935453803961756168693460157400272434328483248778988626059542292033153708506682448981834226044225475 +3520105979519175798851693163134966793094384503981145911295918784323346445102768597120762535638265452619900970085082259502423098 +6023108133594199401032707688284772080067633191011227704160888217224741219472651134131857620995953537078628999627600457859691029 +7135355737374006279317910328897677569526320656280275576870747086734764513800879537763010892690268998504415888806085447064856828 +7067645951048993310751362481424429404370974532580270127897680271443323793244595619510531091473812713032955994538657114149409819 +4429631547566328842887647552139562050329710358137821590185444827892759275367380722401524911377817064992340751082835810723755618 +7949457580263990600187866192739809055130641391600646169338376355962933757309772135951086682561238922534649604316450790162767470 +6778696718174528127426896010544648198065903564137512038147927254912585276060875391066341248912566730098691603844487954144583289 +9567988530970013733492869819764508889770837509952069384446637448211216157716267753953316429209184585653933599673133926099458608 +3413492966631743395005504952764856687552734825362970605800032693220033767913422924066513158712050177343306017113090503548595910 +6782760373416377101401788671756172877579108677684185994928623883970442286211028728581037425407354811624955553337741266271223038 +4228769569080076083693265874897262718489495320015615486943453828550003614854151710616339504422293141400194809493593482338761720 +1275749976017493214306625272092330580872354604747422498275624636561377503079831465248194508227879655351358840776762816388383155 +4044611547062276323077587550347056464054608166550263311750875441257836393920920323092209759398994038142599127607649577377981910 +4521255521824521693676238300467676986666555950395922139641083951517880343036286546664953201729241686129223651162600610883998263 +4053431348518058012397818472045875672812077824208246071436792077005700411785602017912645840401853632467140385402691379589989961 +5296228207351306866595027867237475870719440724477597041700391443928732836085081541635185416372585087892465757549359102233343361 +5753047891155830333399279611743974102446066339908340811693445790222472707295688908858729491663915472040238337620182908302697924 +1508699849153464593476010736879669905272623521348097908417836104685217230447793112158735284635375208948973376371517941685224649 +0333224377446486130711894052961084313686306998844675785828086842465862992758130724435712277355911443550657464532883107599718794 +6835605634916383199967953082993703931572871914916373072477880100548805151438790628235173925034028725412169450381473208146789756 +5647596797800233744008641095874820885871714922036190240494729749620603436690380840330661337877272707920476284213555812599419547 +5513261332211064956118510684223202485323331553849116007081552356506786559518794150443263231676076605407451231256028775857402194 +0513233346964413426058663133294017428893643801135629939987380889256710071523164000264715556002787400893978524476668487881682249 +2238557041006601823583800792379249859430190860772025645499651325329407777431099926158122243379494002959281894688702064542524790 +5329597914788889055993063859757359411515855405549076867209561054324585069797183524577493085961561532791013967475565456058906425 +1727267651465421481105710991587303136944086691165624660300683125601416772368166283177444892976868345022274558919475820725390404 +9723590243421203442884921434798664645968065479222983928276913077586527562214486087189922112591555037425227530771384194025142882 +2491150360749458092334991769156796313818947278756674701926505201943095015129003490350920079450451251986198697962197375931585963 +3288323311745586996701741630221983319300390047571862205264349777484027201608059718085154305903308263735198400749807369566973087 +1513669962911297058622557295345513797998471626294474435337069062590410594233688469673875484253912558586263976338954760252676149 +5698941909248281308488717883921117273305117757034931046904729563167368375307728972002962702645828488935279252804415118514787991 +4226862654077359109954473957002792990601772994320272543965433892756039129932073592057677191275583708907760929447157089919000104 +5013015202975115790118348806423060224599193301855272198220793587287609415484559416236588999749109103840932649489845113026318936 +0096212754630289886604076288072412229982106747320501739034720205607227829189554712104511341545586461792092663001971581276075105 +1400830012525551115910295470153382076377587383792342868236429581401216573706230071139732339253271088253819425521718399148426972 +8130329129443141149308342972871751834898890054061408002066328617017958243761248655943163660366018684672224076482931610024551435 +1612470444029502611969134698674683976299405806211129021898957209691274647456212891723004609478869086828079731180673443147282673 +2207763612557030174078525394691981763423880246207823469307642881721011975478949627177448531469379452033830830547116487954748579 +8943550014526152971159374128630625318393391528677655228099671641521821415999666028671716937719971706453115362098995997004338463 +6633791090761145650463177053061879817237525424207069349153087084514135819202898699531517360951263097509259970351913296855368085 +3253556229437227815840782322932038842610282009077575504105765620857277275220518640524545781112378703944765595787111117088536490 +3612778048987899240807648365404713381232983899393777316948941796158037305227415310986733359795685540845368139757679718651388721 +0329656082303032301035960655149116897998069813522388783445067885544697734848160954106786926689147778838221235161040983098545023 +4949145925422482842946072091038603570358418034882170359973918018079551251028978485799644769497234033416966830413282850538365544 +9688715528548523732988559882524759509039740049260246259447423254924760566724656561201075170029641287177630144231062011355479091 +9326655743860681005816798142139776662356020296561383131825601205038606699757827396575776760705487871722516625005501108342115046 +4862493770761711413231246894538240278199950440029797832691068222332648670966923316771267216815856555063431665762362373752681261 +7759348913324715341816977430054410931106803880118579894939051307627827449748451051704548335839341424354678283555009009093334643 +5035504534504599464259968803145065603468459325562475588547886212145426779673200191888479107787688352706387961887120658923163596 +7064143605981003619608323632499268464142806140873807593464533126484595249138527994250140164063377300752448939069709418760740581 +3405460804222472492156715297971710492718005664789725902179902462523655008185987881883912400930038752420707768027434069222517179 +6201754337957471374021641947490208688904315801118730053700597398632018696041671232277781587402859428981324544384470959282694488 +6581501955752939978682011949909110724289175482558070539947143137365139752819830357605189104362596510417675677952395485738392629 +5415386438105335984223237131949793153812658394744575126207919072784736846230789712179591745207901814607314375532066326818332653 +3916132472388884065768704630042622537625667251509691545801542090771247404318802955070620862627476301969800427094218570764969521 +4673296443835065767694158631727161041961301871016507435883528289555722603890069782368970629525870136617529702747543702356107980 +3969736866659860369283152300636814307559050510391015595957669034065148689817184431746719346979216411505522453887905503375134993 +2033209673996359127998948182294581452192221728807476998740417776019537129643269360367751767407520936424856588589036363660084548 +8920359577043563701436228560312778025314413146903691717334802285060730546542075837699964573276987471415144914980259764316759773 +3627657012409360919929372833162804983136382545072275729679633045374976939117136265913209976809144657411039862524163682772934353 +7062667690768603813766580397495981633020198031283191319322060508875443418955017187814158494951274114076243932640382433951906100 +7089164714588749807180199878456232640504233132283953906601030144605887922753172274055475279148519962245834295034899100856266109 +6314588522281633268271129575999011547246790816567972537919142521521537511380072338334215014034228037819143744053883142669885047 +1533466041592069230562893355289214460798489775583299853132986367560533284715718053651634282346811547110422080041794519104713892 +4512970626101824923414591998781160477443695022763348614143799819070327746195782058463430126505518264262282025424239196981682678 +0616389983515682792619586855106396642118585620436668809283536133165065564894495800101984339481469054418070569566522216534870046 +3480404934288007401354060197932800798509063792079011642804930003377191431422069063134440047889535881274567178940747418743011151 +6923077833597121569133964233884887129884707102983051918568192593065049229148460460606709813558966649828440283419826988551789494 +7928629618912970441223379654942167876674509009329326197500704752789973531185610595644942798244043453568923441723724834770288101 +5686916225325175369639383018669733714398867749909229151589872339897763998251848983094518391124033198227341074940345064217565274 +6976813108342658294069521014494920511978448452850030848500379855545152314960261910035283177013574685072898109220517966532820914 +1235440485859974578507661524414185881273494203471328032423655298610228309217218895796957468358842309363210353318874365415441005 +4775757215064448647761305923181381380005564211124254441169985896444461359377488533806177746993363765363744834471832298666899659 +6549502546484198000053617666533373457701974326516846596770901748191355355442117405686714513545060992856554971022392931918431672 +7550521620433789157641377352042796699563142807992890238677899811145361588800069593324309664156547033893582449253982866608790396 +8335832703903786234598337454771069254394148676210696718885661839587950139009136903487221685179093872236368600881697661914186761 +6081423646547430855633909798819189360248786532594620983774708429259122445948885539867620755942089483824811916664871291112367965 +6763010129734054857751250818823088329080234337202140258490696882947562857997890963039131549439624749888135285626495741851799125 +3823814359862336359300824370629334246257087407174522366323576913384874982113357872199582879993086251942490130349296563220714459 +5201936494493880745125428463676510521663426683417020693977788479860674628237207703884747094987717754212968764024565059007572287 +5656800692578264688131164439239627529022443380780216659445516384845610830241501827976377913518196568744298647947111534785980646 +1465035810576162949068709350012676100595272189178329025511241633720229686245517256992739629981296918245960598907587127807526613 +3498649092457065189914681277177637039326561250640746889454833842625621406771442705555721525931725554548176228818511280348712812 +1526903631217370487596090586369687092431736602852047414002552907015226570166585529226164536775440508494117144839832365340311216 +7172800070700446830268660237981558246985408200103623671258540515214258800554645764441093416997150958224932045592907480540349584 +8145891984169127686107656905169532154735243264685794264058843466267191409417280492627793038318403119617408184518297221316412215 +3733585102819835157576236492205872180664881259553134950516961350305063296415930019908542524245430548400901559537444244647471710 +8919107817519795022023748231546623121376187758240479239499900553296636944913506176758776881811178925344276782657634277574411018 +0087408240115194690559689367771085179492160150159772172117583670772068640711181227707347785373406551286722418977845204918371862 +4087671496873156369239006050995430210075877655341012369255927938969111714405072240508066221092434986016434496634246773638497705 +3997764933956408083465951727938645280914517402927065500370134817839436837421504452664821784855528363909858322652715776945384928 +6358034140171396799781808478643680937510756584505346662094931360684566558016245219693809041873394197337373027746374913052226841 +3523525169184690915788330052438367441556380178099903982632705234888141866441214930597392851790874812046196446764134474357280196 +7912390044622498326943727559828557263667260605437809865126129166669886288145038860031004268429510310452514645934110806061259377 +8350389661397820495902304825219183306686077590455378114318515252557794852940943308375379300282537760761994418165168215848774199 +4335948571309322083199073879107610257555821153883090762370374143145191005083410872058977433580493717339726072170334542991808382 +0741068478360850873822704930167309345230781173492590875420659162725013692793497218747184011101436665668426379299561926131540226 +9684119382210299933488533355753954403421331470099036025961973755235128720357457186896140454721156015369426494838464841418224574 +2606550410151415944627360045368428365343042196701567415760981599314167562524281114850783881875311867997694821988388783550617647 +3906118902121264712371873625367711895188485445955724568729752600563482629569914020509331709891753567605376276818895453947988485 +1946035303735634882855442231649687139100528774921727985325887697840697252312164193587116219630593036960739462878472488751794288 +7553042934057050700594030020764349497635594011506182646429625749401961796662698874719407015328877341348471848266936805626731614 +3965083773308342246671432838925759649742042738645731254867213711401607696364783002153346490446057228458139827872431818981641456 +6290917282609648173484002432007182859707772380995362779354118181323028872604689080381882966273198128487451758287389558544864092 +5135412345654409193398015818933242323380089276961210614666090376540312839620184116491698184580875294639581135409937430385452967 +7814752714924949029739912516676992468879575093818117912716084850528773868618697654019734174399226706840162753982136880421595798 +8344230549785765490790122830425599688553302815508985004520791080241214227127540251939234623642895839337504147995870998311297157 +1312261536153577576475501063559792340822581967430971162124952202625290357772475691141555237352878643256411801046597748593662978 +5420113078044378326245454890074054926337548530109656935414953837202844128511917486560998089330806876653700112974245139455110655 +1446316293204894461498438056975498718142802987046646526458433588790266320884188848067627056955062757523352642280099730639892974 +8768410207973700878297712434460869178087366365740643368423050061014329802628323539796504787700469737820946806556379851425348409 +6903484946126406722104799612810275531016737884354336960835748710713062889887930835389522081146458095107803234373499389860789149 +6113798456809198989364723468291582407592480139088852307707256783165897446795918270682937158869421622443360894104220836046672216 +1297180429334742843324101908319081257574325368096788573404226330238491288173549545719251363488582787945804486932260991191153289 +6310060489009039954463983763054135671085760401926391779699811819750329003225671390731075907159344886944959170333579705985011394 +8928332308562582665591113793712076278529561597438667031252620919756801258278599576959931680328088876910755153375346545987026059 +6122799394738485298698282219069449469308864175448069521079405422190290510333027132055402559422290946776150886848955390869348557 +3298127120443406100122108547957183131539174057790900079286455780159808161628252012286085211125545510059619150351442374519647873 +7895547723969636832110890595697353438517639756208042458909186966958825057542753856885651750056062715953653156961335641588198051 +7242277604059435612451710438360700839539630420328128639238629662384680381167202360792624305492770524378775130253064094350170239 +6328910744252096963453975544360333319092492567496656064841933375445672717827688660311871137666274201081402086421470543791139037 +1263075806974163649200998435369614522606717804984408053096263479824896274273438278637073802016955266073407127338686266295417138 +3380472791463934939167548524939273036820154830243043930199471181363843017057589926197441097934643398001941245455025305275718778 +6053039097745341259474392045998222000183392630902077243215225896980913571132934688432219430448989534298905581203790323081204451 +9398618592749592673582297312578194153271726432191886003886455601016177152460729663034185278967994135744387261240974218700552874 +5660578626067455796320901716488860867842597868415619043285914590213707161427735661103212503097888685177044222466862770614738888 +7523554669658437443860955048187147707950298396691077287510802686031319997279006079308577219459278947413769480723164988814047848 +7728230896579900984081254526628677638403672817908586482917564357430378814260695955826428360637353362697491648099319657648279806 +1471506063396451021205086178083016198973053283515072407397794280863574093292850961361893442271480652011908263493969049518154719 +1271663389268841465526530211516124798508874084299647641127612963439336172485890973748223056840291177210000865573662838724859865 +1558003045612329244947071838328209194658672648215862136590055290358556159847830215232765640226279624286079243381635121169856392 +5235686772055013347630127336838697764880220575101940207293906989459723943700375632099865606791689918944793971999840022845627855 +3362608893556970872086765317357229622168187942063554148251194799624231946317410456787560024770536041305554794440402555379048363 +5400711312343982155393978124014413441498824140561950458826861214386401267011236483424009049988004220217832202185519723172983734 +4739966966771226209909624912450025675969022747113454805529677743180928548967947375791545038561055048316262023987945828187738406 +8806383430961455857801815047901978280522034447065757458634887588704123408758546678452281010485610787173222519794754395918755668 +1313032694820263690081002684124072214508820793025272277314613304407858220279657882431787747545642867929558977205831435007980692 +3821423807029286765485523471547535800355065560255771366935624333716101960889324812432465967939307158664437101875808531730514686 +0552828401027435473969844207309352000803571913860014776373394805498998489404828887974801011916107676224063398083299850490285852 +3252785594630414326772320135663653623232974740345887923006984684749440127689169558676316478796812920656203083787608388272541143 +0886829100701462879464273672433682081314775699581471366939184918943997271841997863805609882688632845996559926289731733309941773 +1588621110929737275611330049699746179538000510503020090443485760166202124848757232989323035029655555830280894064603577880623924 +9460393092105077041540924186306320293949908924617136654677663794806814239207321566509608838781326729111423409256038330560254768 +2457234134203393682333223194421969884096521962196051357781698196280191186003044231263105047089066125824374037625184294564841726 +1139020355104202113948476242624526315291276395095210708873598278111336130644448701938653730158050834519082575031500159059606323 +5508845274116902540996666349891489554638610844733883825695366107090243640688502082955261760220172087743405531646494097110373564 +6139164128879727447485151113660365410811051772254913947601863445614936478465117644061091010800284725353254306320280311126282508 +3691867968556520735423213939515627425003649240791159831598564334907098275550400308993911728901273767070664323401959466199812550 +4385285527314360012494827503644449196670340936661875727271329351754418864846693134514944138823292825239723190822861243721969511 +3358507325257641984977160116136111658256532073729689685425144586604492783647635836225848095839938544002404873700622644589011676 +0802250613087293781581129847507424374630865942136402236774163044580066760450481644654868348223850714885941401523519942001370346 +4033651642172702525694267019009943272304377112789600524789052835595443304754361088227651039992096267114223436584457492049115625 +1008702122009370555007456522799161340685826326521804374437361823776598365128302875870953921955751574077453333491368066562183540 +8297016982983877478208126579509560908805708870139177751490524014204560610778740303237644832086788493774257972604784310184677682 +6595999611158451837934955382339066842486744152636969464066220937412139592293869816659174158028425151865523439443968468476337116 +8189453034430471397425280708179774110697730501130625090567026833440338110187758034977661201862518966699911402128869207946665922 +2967301009332385012982602845125958037212145265901920840751275900004234804254302792404413397360100212415586648851954797337031843 +4621309795646253111797549932297844132545282162620476457572874699573469126157531277320612819758228011208417228396419529928921453 +8015689409373876308149147646698419734053674168732622753328286778805854267784590171527922720365829353861437404497654095122385810 +3928532819894452405285805315911246752486216644456327155370715615309647716023031992277092232949890587556921144870096925641170977 +8706990895052933118940892301863446619427769704201470123344557113277628075518257745853511560735206426541219526593186205677069195 +9323565048038756996967463636608859147371871703253988562924960678047445864387523330892542610569708364248757858563962167618047606 +7400110024724615888960177029937719273668926142229826804752461957263527604940620456908940268767529855383328656980509173310228171 +2961079316835291946492496420673777366632570317335815018560521820389694972648667220189874230520964455139599084822803458488491752 +2053325276467463411017022059798270225751157375504499399426618848961341529109405849259510473132610506746820731096435354555540869 +8022085653261752864363248353443911714595466837953116893617432029480864961559617145874891482509931363773686667586008577729590619 +9511531059462330034969446429670396910393858411286019648142180898913734931062268324002479006487407711948954747504728441451817299 +2402899664016344765585783952968436864461868976736500943447165022135283244224710415767003722013469693233516880183011359108783628 +3912070384615994999455640965424049848090544713337811655011624123059617272139004908000480810430015004300019350285545631530421459 +7040129421344930471792157420616841533490962354840490248534268666426427664847776443733801972606800005209711387678959754783758169 +4514744752921544867887197341433819468485319076219844212297689345457872671062595496333380534044033301910072934964202067332750929 +7744301249953061021059801665718252827169031698368805857673827901657376462120738712603816558346872095129968009454819539063410887 +6694959951385441493938996005094705323326365638007625755937236084671116883434472854253964886004560210329640317017985931432992507 +9830755088107356428137960573336610521897465624860840691251416368635190221352342520096074021567053917840102300469951496094532646 +0386151915867137989482068036180096232759486104299898530934407665433761886348452364475713105652919177827149093478062390353337982 +5026237611119968967543921268886408485214827943839163757065518470886816959612437396882015040264309844556286249899479340530273455 +3790585606726228419436731177762295480034329918020969162076958086843994331135466429174339242003665276457867681590462499745066166 +2010072822988771442333707800353630624045735130580706054631404185963369356383377732515576219388977808173711224814572042638811746 +7799862036041836103498954213010085023304912197271099528776247085017591855736447939941510010043072233516636490128682332435837718 +5444031310957599380550217779540801553000957068137470959947071825380695799288846148959315246034349773291226461068837384267657314 +2720831922046271298345192916201975278989464655822747470522487119845175483171802764985807888092086888555468865508558747670561030 +5037268239030155207644532217893862758398321608270496665238719184860604497280537678204406616537105359104541182047680859111844623 +4406453518829809819978030558459040108565356220631255519247977687593573153889331324518157228436273334635625807524985686484922311 +7999885151695902136577261421847256015439589971601084977870060297320803728853045230722764838994142711659330991658042253610623719 +7149325976325848648664235796296763337498794913699316628172271388813068172391895353748736320858783488531575015143960860926515250 +9988100123929774124197265328008141820710758495752123486181540545695574140507942678614702580850949656109084164982037643220132234 +0375655311441060929515636645306301671876166021260474227545267428349070029167644989380583812184485451805574601129180998157634256 +2128590561140195400524100610776393812855470570049174388092975612532917892277799041074366790055894057834557983976227857050864930 +3547444389508771568810075527864526064121655767293193457033048336615284399475478737509966968763107403398079684015244980874035979 +8519927849602061794266560801914694036469483013102023144973737181284943231700919734522079332185384045910178109811932848109276138 +3567893790780435626536700059643971391023938979188277544418757492427060624484762199641583855650960393640504524655219454811384399 +1735108417168233650163860054247929680595797171838825452321119827231787146560622246912322232224942016739093046379276365165998295 +2756695873865903361436041234030623662053438358924238813701743016813195378135003338279782291211315013357508842537428877943825850 +6550701509812921278785065189524058197762485207459987155095767921276575821627138362297358879947692406013849533700244092475658064 +1746417294003960060064872265575781963057635907396407260460748957479942537372418296161245217676807862873866767635894044939661427 +2752800199195253529712854754798642118106991591404644917342058968657791030752834046050164301961614256798982226629143783613672100 +0156432932446788502003627727688728508740309980174215347673828299641766130526832968939502574943042109833631744090112189289099293 +3949539215662926707735521667894130134427486982796357220482855624651559957655963754443694394481228836760254413314867991125537370 +0833694740794893624914563726494064663925162779653035815936273173351045298468454290966312347207985664544908635066940292067018215 +0068104260838913669470167249521001975928318786418160510709377212045134485178004263296128628173776672447600907854928188803969244 +5213186606414124173974080816812650470078589656434215381413340547874523982782714789910645183938314140377172417321651847315127943 +3831079213870153648590263375611549325132489817138611364392701108960231363751036773655982850004119077364131468986709426279928740 +0816735516514168218608953096598217254388061619583961807831532937684267549690578384517226873045647249368898189499267589557763196 +7614683199694761607567513771357320522896560891215582525317620218466204798420014498841517198000714225395529432437408592988810647 +6493791813371800888731383040408467219612486147146096499615909931691397312978653723829961950808111958324158561777510444489457609 +4673680562966535580522867179325650694242060572148395328716350087210015917993916484085808071728589768362522840828065262726854285 +9484597285206848463033004828740831651691831183702931423669671299973340807479819361399029066256705512283915649297270684923293120 +1714285317558703325551063623439102862882087552524008272992080310001454376923262343097189280399813634553559119888320098054640112 +6878492562141548781762151287184309582512789444294394903670308453724775992147414690794148922865626436020007876554089383158780819 +9151911784071444893456939300122542765400868095022473582458690303654492910301968711120089416900205538880607052645783814509878005 +1882377638793327163743947324933816937365677683669086577608915703348630259888777501270806753764247220631643679580537815123548411 +6272056421434812628524453496821198676269890972424881983781819205395093370536909259477553372703891212584194254740307122112225533 +9681870026283102041075727337579428100736459914910652574617863758549452931286799490040297351890738617979595875678774646214173033 +4185695124550535533485860637896723654154834853472854068710912474987668413470803105179616610921355620666097149488574561869489797 +3333789216162396397679204330436931257560768258425513267401621836728579265694014546537584859527037491359360764924746663506919770 +2081504393567373511045831331064517139188352171030907247345004840816407938277996907076471099879274974679268216987024065310558333 +4237450865130692858281322184224268516039222037739605355087614033374925113958908332718451900205100215886486089988991485524969556 +7929075290163499129098877300162688469599528046726233844196457006510599251899484746828484574667591829570772351512078305736672654 +9048657921163013454470683806356852138380411128268107788272256300715016144618475970710912738205043728268553865691241054834233600 +7029431095139361764456603528202380622931434502522594214209499225882330762428652317267948702623321489577844016464692915246359712 +1105356646640216883904536505154508223476242075189032231521222657818953039915135319911079058876034135094530224254031516149971890 +9040945596327638323723931073336767619159273652642403035784821558395849500879059623038153539774239738033911626241510146918039749 +5283073188137753905883236902928531799079492225443087557262624838420087547899455213109577396225125545248301841891417389453351792 +7168518951278682051937542342148124215459196543677153007216757499379420036959725544058917170458823956258437616459586004329579552 +5203183504391078070063471026373757432195663011756522268894506479810617330340549685777010011742133881536309636471645529762659089 +4854999787239813251707701121392165868052289429676407362096901868531485589830231940346048063593475645736706551014468995600111842 +4955763538748975069385515923948951672972674499411759026560381765340448515748581186486358853253830173480680063909301824002408127 +9640228631714593257928903905827590703526290377038235443447387587380382054598578561742893844220053769466029427853174638397427339 +7206862882358947361070854137919196014699723353543071743024301192848154276186984110546320724917862537765542130378492262094573104 +9132037902884622149627426018038745128067438979528182768862440252858820253995972446342211888009471423051493159698724414795710825 +4044729478097558603438562691316366082632115863305337165834502683246949426265600257894877978230675496217762990369003197560692400 +1914819988622085254720645203900712417397542695171367605811941681178792903669629925720096971822910923986744641852805301639652708 +2489879698662595408916009150319958484334649360641536805110449468835050783501013718256899380316883895243102069697127104742796718 +4988423693542042725154817443015091705766869067613353141088272355694386531680806067437655514967029170154529196712934789617792274 +9999952996574987268331116423939940247569869095269736111023291772144901858552922926558494336795934151397996022792048113880115874 +1453279672186008281192597269984891593458554070326190463606379023362133764652707672957322700161490484448540753367821464532839504 +8011800610118047251507565848810514239096971840458570325653839372359200258329325658866372171001058138732560340444368314621028592 +8193050742913890615185212677939019076499475354863399017606329114085259623153218920828787136128272822531407941969673998134066236 +6165032662878134088891598296730315347615307351518186514382605872591152954913301540364688295560996800081490449469152762525841628 +6574519924837827479315694440706135165231391744110344384147108151733208482344899812368417803491072197299164648305134049390042783 +4020778253016163495293946067531529520938183648436344477015365877950549136205835548741134429268197026979922853216037833912140076 +2560367110629307999607135522026567041549325633921424597293236948045008668100091755427842685667533245402093886980655601060106544 +8546203408136109675436916166218885966337393842202215612854795887800951672240495314500504099192631873269330473780375743830861472 +9788815056490265802740571039729926738317636325182129335295512619052159101153870121975439793577984106855088335077085584239297075 +0607830196004382404188902942190311303589870612777737359147435465863424442529799140567063628056860095478745600686399779891523221 +6451811221617351847394635729026502559474989696391694472537910902735717543356566711060840045900392940941333171363021277998101010 +5131780001821762931334985227662541898093578808808129086254438607279239181340438600040947973426848418990057822839591824881359023 +1781431927220093804299065870243095263893435123368341056371775420733487563878765425465471958566683414339834418118022714155686281 +7855784526694976383147640552245153733193000678558101047345867400675345449097722230036412101539440443445801673442840764573276547 +7277064664195765848534013590019916383557749169375559963951776576181466602296914352536749809247940247872302329782226925806468043 +7192135649991318277247808266477765244997880386860464765070023950465490749600254005312168230797058556941811885358619067264540155 +7745942254017380214048240376624237449522972214173349323588251697136718540827110673334670762835744617609275818957162829921297194 +9308025524075793350930587214905796399488049433256186463627589329393236892726854692113712455277309297788479077266643487682416715 +0884436213404481146144542589017779230742715119840784000653264872459751590076369675379177572780987987657953814990135600852985178 +3730124530298931373240431341115710398974380360265132276809345755075936226833617497570877144367390338090832665519341170114016004 +4487614165841966483390809601332500844989818602599615930254525814591372642009685306021109318297597415196166334778397096512192919 +9738135052767202788355804851260065753666213740432424577877433994081030576175406007197406894692674226624886870624390536370450115 +4972079333970854412711805268652812255100648740561624233549801666164086630826247751458218621070591048081789263645429911196722042 +6264477153602121971771346665126521727114186506364463613567938860398188315590497953212091430368302124372365408850328299694402187 +7096923163132078408222622634338258654575603652287595704602066710353019363339205986259588116309830845020074124235604674290929803 +8620319232582906151287500817533307221837336344013921731345438451489042597967502976685688377786979657248449082964352894633940456 +2338290774645460787964275902190755185934483199622353782037807590665967507775691698706742149485822727584225845398159573488959138 +9690411512559467787285374715845943612220011261976368220307963458032917206195128934924351124238497339947198608551639030163894645 +2522942273087160899325601687017261599086953489151039944965907240532011926664847298253352884442034273091440472287878013270485552 +1149024375555901235741559582840384810679491209306220816791833779666244374445454015124428345163452944670223052356961455086641846 +4911582198911887147216938492382715818668300238279079306283822841054803949022104243393368739761683072273825914099989412306146198 +9386705286204083264591504191247314048641049210231970001783669995174466309042384199881382505454205575494098905658585266777083412 +6597181522016787958122306510209743227930379650431716531860926286813430719462209935654406493573792531684209677728726902971780496 +7454713877871827759953186012939103462107245349313356776395316754209733644734837949073664445059496108161795213098031298387675255 +8608951097824554197185243937211248657800767047324365682759725200979621123783045604563185385990004228189141941283634284151650878 +5110742662179788336448957887870922104701629467080657819437644833165127617594897707891497065965495615130086622172410875337801320 +2393400609667414757827986008711153834467770780863121443973568354701559598965757992398836770293536284505657099974035105460765424 +5696578078800033701889220099620389665546740070731873354278406253021517777757298261519059116566359233204858968015644380662525991 +0719487243613892918578289524307174160805716406329931625803050285847534463223682481335815436403441752588538391995273084423051287 +6507583132575286651290896026205391515246827860101127710478694329951379556419208073957900692792049817183622267407642643093594278 +1377392380718866307096826670910831948575020464783957275980748872622490677834200043136176315679139636646826582155786548373039619 +6270395893591295820132189201839068186418319333704429754904087462921395570639063307809781688462251183623162844581418406608429879 +5043135604579355727135840436265306254474995931628655768285052525690960177800386175175072542969753822246288489006113161219380471 +2468870042718971405890990067722938578347935929978019468025646064389136067817188723296206441343678326386730657242344326326352583 +4850753963828793552300794217123706294629474328454245842829452531896699931041364932772984481884556840376569123447688057207356583 +7057042541510451935323810217032483925746383540989928451101483213770975125680075200108338616874937924879361446516778250185550748 +0832810498555353548220097752070786325088219810053096162098021215047558250904896030368053916311890910240442204298949146112466005 +6040761853883330139655359121858985041009457655416978338162675746762113717003221699855200571002086517873586911211646864010901906 +7256461701161490507120388412005575681572262509069280090655692691681966297281536854053796037221572903258227405845942489932658062 +8784119159896217889610742272823513831023300901198251714365380671379756902059751391678189474782942687418296669663414454093527211 +1713004342725426977027637006978967397759795215758374886596544583647960566243025979142738914314172995528789806147213520151545065 +6148932160477331319836946915602714548850688076504811022908366977684207197869071392739615383347277286078886115107459496844571114 +2443445647690017845788068401719782165485185661849045242867722200580611203950026796072201135775981201642969396670605705878495845 +1294371619212953132417647548708472622197636506856370676160451382994700324005284453279834375908065277166478082791508699598722770 +0053575986014268796134989286247216849523870686352637864684792640382519878279886269694672196689841813884729761559800125789968281 +4079017492141445585825743793710894771763718871198578437956220555470157513268225528361787107561094227364687598864249537846090985 +3335615421853085612471428388452383268898317706526014807014690533942186796242785098624189594771605450015209158202436565395838528 +6313851017796457232684831975868577197008285743175695149676285515896251363246115689779921987378115615609824156064808941150859547 +2765806218554516717364886537785391971358807028025829434420213828068755277959823238847926003687327479957197440099922200245318093 +0743837871847609090197592779267491068691643664874076633968061486254606691482824855693172784961663018432092989740850414577990750 +6025684120312451259686658587433694029989987324349789159690905104220972232002229613273265979326232104213906084669828970966380427 +6946689887211552778830098495751925653354785324514999092963991842666284889259310987400348941690948665027526678850288865496058905 +6762732711489921976841952967909655635081188142889946182277259409310226741889437458160117435942945024450654816897944448992096154 +5422044978529069360644478126257492572807446085424868838210351520631277115650603746786970481265582987466949165123354295111201812 +4211276624640084315612918421177060204230299421803486633824592006573340606615889580523464227765304350439006450951838160825473751 +1889210243603037535646067281045805960008570285172891727145769267831573879630019508324069996866828486890146852983707941528252170 +6433944896458400005722683036542919674416232401927258992043251760109983309622060221449241884161395267285324969554809613236559401 +5926828496890530602862162152396620975722541268836002211191264684743325984168877494942640619948750726132999624105124012640906283 +8437529190279374121273890161340277025282882572337700420322280964590810755716568643364840307879983400203857287989101011632690781 +5802276041061620122710926329386353739806320420710281218622737558209391349689462899753335081329268758815057487656715890733084223 +8915069186666442083922633115688459542249351209368361689010535548757066038294153139851377020698884275592572198938097053313533421 +3618393873597153740469607428584632822338908997205070091595456519416863149036488990139674096344263132753025929808588767591996177 +8414765535715090848046152050822658084473018911692025473276058620384118863100457752482347145023516317250246454398495927754913591 +4409298010504839698734131748484717641239465901635152376312170896985346043211365815426588368753012122770591860188057761705660735 +5937603186684235090583457539320837412342393238618817530913846014346149555309319385019839106489932216877734882102311404478731716 +7896182092904946306296521788524236999972318921384942071102716941043024956484573567729385887708261561271188936395359650621440543 +9381772394748371157597820184835066654518409316544350915121960781236322808659004922335381181699829103134517372303100027463245782 +2615260350961768130102419692323579521155005867350775325462551282112894826272075572040191002407099324856614539494210810480930402 +1838597774381264536500359028476517592435681239463143604712064430411599943084702827260512914320943321785685269655653280527187527 +6303497221483801786682166005068915785511014637493079986822276972242808097676862236960849777047832121671397043713182928176830651 +4198254431094795957177601291882860974299955452519675630067202852553365287181631847719984611318356336562220811004590160639271651 +2089050062014249099555113392986787327862396528284354504971691665000330719435672605372528268724943691397193582081997337212460855 +8717657741330116057938366732697730717547522872552360477855457981234742653137379848417305649320084599648948083741462795882532206 +9986804236567371664160082803763334644507241277854017370156980387812678877717978239570818728942909512587707736901227099845780710 +1187012467446764171881903676699687486363209718049359334271085642494010609014855350532206473984262423560106107436149534409159662 +2848539362765385732985316450544140366071147981646641820740677994697597799634870260463776941968992688323272637389950097251845343 +2181340059702615616436136969810778034448257219485660110664564303142589278244498041564775661983269433643752893242421364842071587 +6511475958145031560867412663572535560224504744829799500512261131519166232318015397419472829178929843115481631831351152969869230 +5907563295429072630722334129008428981723806690819380400545768764609783317272102244876794754684900328466037092277858030276039049 +6371121545042724094475375695524854871280733127714683231595927748253222937280791764025222016369882957071507439090065827190758754 +1326490274618829941454831367643360383905788459550667904893538378442057279259437234426288871318674319032806392619399812416955409 +2775275011488829091588325070565060813564196841900611728756018532857865700574781000487644896468359788252916194701281784442869238 +5475404321259151425741175250657761031096608313986258427026901467928877964154516924617882931075782136380173762818340243425882456 +8098506275539721604499982773326806644420333816118778369209316210417120818431663778002427348509174034183157653013145092651819004 +3184928471078890543345386093573923680647427432807794321314997911033358058808825224490515008557540951525337845495688169454435522 +2663993815050031607786993170355950506772886776860845172901406916363156294602197804936561148186445731375733299997098917913770308 +9652206542382536216286608096098963262843138133196068261154952924480494400928138047212132673039123404498669742532274932056703877 +0803886057269071094490544437295692503919136095718722047240291609927251689451657849380414877514143018375735518559305335464120670 +0151936480170888759767939879997438315844081206201757776198680712499035405650084830184555439755477818487911581690050566831394218 +6229367230673286153903588882341483871782570684557378319370079577894075314574849976163949953230620503498800993353619885098682722 +0775734990307483598683533921005381003594918538823910337693583925400749988570440308274068133400598419503446320537735625031687287 +2279164484478563946971740937436182665660391281989609876479617857522997660917074445342075564734477309946731477007826609533166522 +3758123683039815493345731508244661356741376276550143129350958441510802897891765417564525850067802663207435432004216770694226804 +5619370786333060777254420771287566622455383721762137194440628363338996839313233289419808657699323597544032060605130001017679348 +1117350732097568268814555936831091979923626150476679569711035279777989299558634893220687598099570940599334574372587146302188862 +7465078440908857228369280211059007709404158916443923372217270156208307015594931088483508995735817533317081734115339967063512432 +0277551433478001259984748926007853394677528181409165584238991766092184666080139589261333280556946872045412337141801061054086949 +0700754560549557192840941960533883683916477333049889110496053434388836650044089728945956844381845980096615663986249384912038818 +1729833498846421646108320742580046475400334773592611265395391377127688594599285540845684396076777993728243626683212706942573378 +4323511472129830807288869567946309250674678068275114664508496611904796208234885435751853338278854135085398181237686024172180393 +1312943548505486065871582599174831836548648084308970713256187466678464307897110260321658188877464727357499093515658589884692245 +7139584610276191442567513021000360376560327542398104492743671139951823728316211591532169872633220764756742780779371664542606277 +8013300125828426771757381146217871760111879193645766888697436656318066675243280906292568248974540959337990973627561398675008197 +5419373124232954726590126551016645137142696380830673731392642951542537238565961064580394978042654467730915963421765890733361548 +8922925533382884854249653772799918390864697404746308525228860651400300505857310156341812355891928439789989969029099008533806665 +0128219411137135309941467559688405403979836275303036131774092714874831957845954239946311223981403764141665925425006138193699503 +4023035360745761142363755560440975222622652382698883967356728120684964102122995320575139970015373461400690787473496703365503109 +2193654449177213661881030975620088565679413977376323484863510871705861613106991010472484286534395967108522932664799263272973605 +1651932770511765948726250021128129739307128956256839990590743197977037091662728274789578774568077274689428101918180304889077378 +5017924617218629587957879829522315523692604428645809008208520226544692314383658761425382226132125228054011492055441635314272210 +6749666157481037578188762106676697225815856020636806090419991079970372071289788209874954711658011019178097283297120172931963024 +3867624578607959056494862536265389313506274108084087969185322137028561490827818437756426384470803080867883480797295503488443754 +0266755732085811371635896735784706558323514361613693558482118358007540674486152236347627273339110326422500120960164943514979392 +5851591583094022546650594344855608619956936942520965617453776819147723734709318492888126759979035908706037791854416714517089588 +7488779820804386679237327683610073367094612676999550051056279635818725821831859618176236859057491859294403215135785905001415728 +3397867405536078184923760377846341551636533657104459673180071634565287960125731400588939475716477977856211995657191117602660471 +6368419292461966253403926350187375212915416793685707098775001247308325202429711397391995493844090131420300451991006099818565158 +3171884061798508018743336874067220372374917564318502076615548912819919624484325005878069566693150527749974672352053137075239471 +6765298415501192506923578604075674679460732604669110708523132941578157177568853063216936080050749029655642031375231708316371762 +3941656184595817536876952668926168956461631341364895778129612449325569155094729678326528659230305783051788455638170048468709384 +9344704062107618220092102502671070488066523904860951808964071896990152691743696929206393427492236495869384495623953241985933433 +2329954473357680885309244441022618801570174231106127708621760839070677029891079166854592191885991850284207932518637882898896696 +2253197905988134418498970803242763736997197997987724202146481273276883471048373064146605033007993208986500108914474101742235859 +4471673346443282932636516218151183887721959139730007987788845685281052854907139082610271978365656275535187015151917676999391516 +1490454357895502187362105850833658593130025904397280081248261243837433753996643256759896119495710470799362993945917928340275859 +4187480193910225053682324326478852139878506761849860931936128187212907303727615513834095500089467988300420504891523377586019041 +2552835341088538861109696869815032626256269959324139802181242249722241877212898021098564840649196902417063584333206994390607317 +1538928060054617317449920025668989345408124114562616395808759769278572186743963855933221653177548947045222630757626854233433755 +5185848360760696218717214816135397052477034052026614446970415328439572221368707488898468973353428786860987024947961924953899839 +2985590716831731870038599668250152561736463733069492744631154471689685148003517537536069537952391933185169671069870236035572482 +0858819385615354860719000425698572982937868475649379251912477519089804489107469755432874178918458772943168929318750385686530082 +4333886362870957848081259613648289163828064171655224322239708870842553708643760011978783227657321411272976905508646641961780912 +2833169683733846355069467452629978104797868384861004576520122658047677739451418977045812632716306848990938028931383957425588061 +6045777379942977654526927417023844498064089452756699867598470436037381168815462649457696613017471951809000988548875034178737874 +9160331824383077463591927113486291567227954453778702317129507491730495740733772073822607464672707613182092741352901404225499494 +2171629178354382148962537134449119312001738349076684871759101972177169632459401670960405086152157843086489006781537667287047896 +5465718260392150704046827088996611582230320415786650066647900151553492798388495126103785840394545507668150495076747239741124643 +1419544523318047856148530079232229449051190304992694484547233964026124081035121194889038106213961089629592355710172833338936041 +5735074574334557161220421706612819925561806730824425800383474349667304718298810588448596848386045836391940535442755152772209476 +4477743831927353467775816826229193246462805293097367869721250682503407993182691455177890744118461259199550137591872314970843570 +9614009267031215091375702771630372739884751484563943485109876681877458872527820916316860531787616767154557713108421464243252888 +5725244451595751926785755645396186934606833650722629688279447836080615920686865486122499155337740847620671316549690747377719416 +9886724243384291436035548475563689452903302006875551792856774087989888845839795026561561511158603968657727537570783565987096483 +4338269257625836943750609173139824275314999458619895874921046346622761766613817248643045610302260828228899586867769128173831852 +1927251435241586742325643335703829021262110618516121300640702167056777723519853174450793473043913595814696522118527504244796457 +3599578411448167894106533981801012494310659214850381649569414007781396645447499689729938374104814729448345077680781325687345087 +6129529633831819171439689515282408943585760656344118595138870514119273973014358876594274788901442968142994742135926265337385656 +2522744449484255252321589542733596606602045925005526885282459116039248444399754430448309551058351839870771919853241700908753171 +8095397674156020153025647112643503525783203561945051043117484225509753196255050991995918313572389826508756030729780202503607157 +0514732746209410237098921106270758705122238404358107693018072790601247661147289735400790334022315641411924147791473123654604067 +2183481353624169330708810250088442996270202852800382852132922090252387617856118236197486397057001588957700864557493511482566082 +0159554686938426672550427425422201760923684567404107605954807954862252513544057244716697622930133085009155635119267747535809775 +1603870374074406842079458547774759610733574985005768921583657069449807861035511349884403260777475545081121567326386609338253275 +2866478551802452645398860404273568244013340206033503958336009745540839717584645607628126532817660071722429335530485083943886111 +4372806755879104949087031501622642625790498233747556727536449464577828956704936323795941826197457122033168971930280473743209480 +3510521745467987614604622504324811929598409501462197588604391588467606320835512728358214802216357643065451820237801293132895101 +8869176817884529359989957422435201680723727230870849702923220814297956988224073755455624237386035731957272496882413670632312027 +4987041709615070926110617696999725204352784063869478762637421619822949085893727644701242995534552198616152209092394050930669848 +7400973738218421741284556096408600962957422728138542260382229658265717929340410249216494966480842797114656043729408022372140555 +5357622280419421430680077268491817179160449030679507850055675685514803114136633487732178706581280425610186327901499849008080553 +2063614935667238312237638365326101007587181929899519898020565403780654677915752203035463564958973934599571280127628650025231204 +7510339610097271890758575355760181313472729121504677949019841665976993380277166759244310422659174282752504901557558455360101043 +6704624590746564335785852066839780043541376118703961641315331313655337920280405840841448353766789935381787637277026193365826955 +5718584531080420228299782199614112550723022759714938401551695221278973774269123526563402857239118115571007974128747966574892058 +7406616537246908705363274038771943716880888382315653131428051669734785136535017075892856676521312977758275400592055721561155447 +8191030503034523458658590430731690106155340044400945735308786298326190302983231623869556707507267012762969572969229893222326020 +2781627756283482808592633115010135954787388098836449433663074354764942592857302479394320001436105974173300167981736974256889069 +0749889909665269146510382674032370824445149525831032970035921874308716508078712017288693000420564774687445800320014347768102977 +9500582955080504298493442003839451470653349230762999448413032412395803444854529134420395637374428892260393652466859653474793248 +5578683404816116099839255848388989533623282136956100112356341602163894219640627502458957004434668830525937659394378712613593339 +1328818081682559186294868249152029912791110338139114004470380993085670292745974492137182403037306610656137695392960137614180452 +9752978688889728250401766859342517990190156836045937377483717028864519242478723992053825465496992411244434140736698055778811865 +5764034558119091657996034001136138512460220698471156208113839665880299241195462026376651406117658603193270831455055313097779290 +8887190533917978952783942478895262358255953083684956216049958401478271139454139129915935858174062762845986202401574727546757519 +9965112384492455902741039666831644918325589401840520355227650968749201419553213336728867559952057316374095495633674649247673155 +6547477349678905420240149432846633096396537735337479158019850832661246628844505249719392841713290257003652108726047676880419068 +4709595063724483081528398865202332209399887790016225322294096330311019868576291990108551198704968033034014495019187100300731545 +3677983555491782274707240632393630038558613476413675438434221141668472183743225033730344641440692723378405837243484642959062746 +5604991163272359834040422515862505640037839519050818603112192411600677144216422234215431187611787844494127075718764452791848799 +4854476179924301594123281798645361490338402536310431288082224776520403651635476604940174850365038818289755315902382966023203577 +0810744335357702561567797304986145218660883831691187353402861163846458499952189536879044115452318681728624510324860015033788398 +3979771283995380891938289539381044182171976827997056764348788204187193621090041996206584024348644400402675325221299998818648561 +8560553235856018013965529605400965645682933039991913219712272084018424710938107420465401522172404020985846364794336906931979136 +4463662321945447989909156167276620401617659159053053338198477063314745749076987397606793494767276193523264007206178167936213596 +6945045570777520341473278974105523918485559285787445842507870137014286454851350178004328369233461521852484045058748849569462881 +7175394733133381132769602280613000317015438488548919976504824538838731947546613872200764083627777984989422833194236427886436510 +6841401583932488101726607361270514543381198032082148492961394207816779730163687527133913837138426969504533356370971942113680465 +7438297763442758000905394998453798098283316097089007654200747461809281986645383560583766595972712154827834692097585224878349332 +9365726487547590990433290508632050107564671461069290060500347861396555664994103630437491491318643117772003180597703910105848664 +6611643426610581687045714856705413399193072506247927181637882082436528718891021527134176567154185837899481317325358321239700678 +1557904502034099580005949862058754523860889792700842593217321816597173581797908357156234792521428404814333336278006477527454771 +5551300020237036342120897324580866490216118116991149735001002790662102209629270626643498774459628186650438561401066616780089887 +6490963755355736580386978526346909059361347064280111211229508805478817906115930155049278075845715671257641653870466615953910939 +5214778972551839841667874021257601195235129654518890488743898996335333591775608284908553206841411120044347642321931257964395096 +5963500480745033263837415489981452116756935833729630104233601643805643301868453891208377454731826714298746739883158478178624208 +8078825488224820778954240186836158374373671746556003072588357835036884385547573916766540198508057742745190671930027702184464479 +7807519854130748159210155879811678899722270728200599689770154189194726372412310007746266228667023082290285051427818296004435318 +8849779730758648974379073101178990211676693852201291929217145528640734853429497077571161674011066495817276757324157483849716817 +3827352998411927949530005101476246283441759897176637008058185334546170312583858564041585383266413483217734023667361663656510262 +4989459887640993845875939547459055523785574975836774839383442442772727790780196274204446558595371067248953971633261788681833422 +1430260652446882399259326117845029992780579944482914613560754920430392419419801174119306529200876322246749783122971752437529985 +1073402550916699040362018956704245437401832452382196705545206547444352132288265525961915924743830388330363555452775455714221746 +1446616034452999060536906307198084433181209978784238878045014349105221877679941993115397416711191417693894301857690745032510836 +2955390019036983033546377278387952218996955027373523655965784118029086980123579985775681000785897246210289721704232358897262443 +1116899333926038276430989136011993311064835969966384464160468735201645112827713375177166600414004561963808951457633980244174631 +3365167597668328545027411315066184915713562624842225290359331084124729347322094220584503855828183282959730835678676639554531692 +3703132630286521554741077336255711977522731641435705473997030102644612053700886241315456330580773390018414386585814426113751822 +9217494111509421272989272558690493322060649433443466464859990829891612802636240902558280843106767641519024486333577210250455758 +1991150751561946927232491146002439007601543254992434751580119323409611022604175456900099304707614992483865118197257172247468485 +4718289269042762658615396540607439436267108037664826892947523927247548349191063673761417193077849996352799507438421286155035753 +3670003174685490297784963434080516316824119943018705327982929605778412196093833118187456471893403114829335230172542023511761918 +3530299462397538994353624879221057016014513591355266971800473995195599575301873296387357918288523036075230002667564181209941862 +5652465756063919538830640228613516853681348031923008502389424015416629283480087157633179737963541694567718976270634283296698772 +0628398273786061322495772855081114777882254155541640367813109541283460927308493462443462004237680494256941289350455650052190389 +6530923425528750248516830645859529161548508001636146477492907827739674571658452569696309864671893300056851989854105052764500328 +6377838408857911124605696723808145124469491655188029699697020610066790779133275460162800601521823852930699312176096876169139587 +0939964825101402137367079294180090650010316165686145129739428563266232844352675969324123159076365905057635263730951535508592474 +1811804823447992196520518580949056544426238843325335935996032408627339563545875884333166832898448807558706376176980463476019811 +1853671195949445539340843650229575714750539986544526174232405759132358558190475375028485355385394868549681406717987759935904458 +0905656451763972780540932190579104438556329939472118207647554428546869843074735432587357981463196272333295652269129883611649971 +8081235587163561191593283995607470950948702816215035438010567011848029435878836700192088876498542980088266248768136664360732349 +9048395346539903256416069243232533610069691095414640231288922049229368354092313481960519237230458713935632653201847905841902926 +3586899375981532151071970206889742452371614510846757239619094223086588901204291404135214382914427118575565088049023826713070734 +2259183814776114935570816268571424671175423119794189492522671432476564901966405647187984087627618260356094922535155595764746633 +8775939779211866982932257974707044029673787248018990390269181484731175023868082868637570701885688930003111950730117366747034776 +7292037040878433775921745075599110933471784681597260046655966261287697859339714368407310137037826533774841074326027111292219013 +0255740507228750627788675142293353045696200562639503837065555027761345191438643278181859657112500994447007605647817675710357329 +2976075331061106900800437561657304517961915727589721564040507512613786451696450429048216474153539650176719572123959448008900012 +3408433706045747990792868143612079234278596536479367304972783738238238625919742682436942402201332632706821698633101418726786131 +8375660756604324838036937822438189146692980841058943655646096987446498055875487503182581651007976496878268593478897138465125407 +0302161625788138941756180586844882835162635784547013096553214201480525116709804815394793585124634598219195269074280104207728072 +4667838900407600665351205946634451166183916253892825653381319301710998507256107777186848085957403008238368100522944303632213848 +0568234978451633682200113830676859774853569376506354250671989943595147694543341125278943952146653132296950198184251693558754266 +0837997184654618266464229835853893240382511414642926848175904359357425914225875122515607126936089890130506925127703951001817058 +7698607971927804322206607636649779969047920064098738740542516881736563449825854806194526365935192809082449412467250570813092077 +7597321887811111154968364298503454292989382990719881362442172931187037500402048057328677657705369081382090672410230632333022578 +5702691344767100466202486216169170905325756285673048601355109036801723677816257513289352210067825399512675664902709627155754391 +9534899164623786842509061739505443727104012615679366121874758009252764781464695793453679090140639672566086270483355150124722338 +3629599380125790239429932208245186801604452556987851483665390481177058173633973798236252529147955843797090679456499291551397899 +1654839141753372209578242837530384383897899016697607163271741042796206564645006293555562819243718356635356102675076651792712069 +0330578183767799422174048823749345411130436371482070584047136107077445803065143356378310883097325525812461948426463818958359831 +0535837083229949247061916351323305805073161284351843348700635104127671026579915150619625693973299904452300157134992421628495782 +9135533592025422741289488252334208635046166354163256661447685205945401888767478319569656787569357811074256962118550453119004362 +9301751134736346294356523345255306657038167822211402293117443464784840025659169376501953957672727051530179579688902595680131766 +2105969846665539329882900056874716520798786759023512793122449365556804018453805656203040461898429839786920406056455733868681013 +8994359707951012841246179634189829024194191300675090705148350390247850109295388787831386650321294752877084504137894482370739618 +9241947230769626839032573755018849701961689378840022528468670515360403514925600595045782712624516468191893178057825723173315417 +0398704518600034083166938112120280977963993296425790225059839016038745051472686938943167918298644018861694001602670324180907739 +2829253174521666979009820015551029220993105373115142695444414232286946938928961493390386858301002697320226848958500348592987936 +4933376341875898717948810316592654389756790528913140902980810511129982071300880573916140408222881362220907631175682400695211957 +2728405831786952505365442116971677026094675638127255014104635321119617454191227198408175192550442007477181962648595382032863011 +3335513609023390087365324523236604049358730860336918649941016742564809500323096143070248679219333226141951672644858949079743290 +2564143013685722767288600853878709769356311565059463598251742499173323941411533573854915762183775988798690410191696639340413240 +0493448733483595346076056156947860868081377709049727106924154044343435604871736849228001533150906569243527614792377672885378205 +5757247991130415486385859064419869200094851432494013487858837812428585929428086640566084149796652609672519627454844564932256297 +4669558478660881121021903646645372020041434507907448258764267056511994202274610206718051374682457426295364768694292186976277202 +1564360996091464350826834839280932166253903015206256531264131271209638480781609315412345487344374301996139188255108960560840234 +9176534815648639973257109408451729832095062649822000160147163265683826755303899059727567592934572490764864402347275743547218198 +1866055495605517210298107031806201932208987305284317169767415130763125409722583393741250961695800822861253047933789380082578910 +7563809612558319699283127796207634523042191451596228205984495682290513031662522019531400901314878626864267750108819514332947951 +6013217317646435086018804719571761416592333129643806900438788455325630320446510012694647580590342912656641492584092556696246675 +0844610636935384963725961540456988648961187114742364338486232200258126457044861826644153851981432318719173242496404738484829592 +3080829363782082227074488901159023270312983944302028828324523976099006110195688729810726298266450333348633619065288392022236229 +8005690472921247866581261895577036104640057531331680489329654614207889686295106419176764455962025382611644995861348654828098933 +8502321967575148172006469677676125932579572402208147927471694132908710196626265784804209764246638587086749717612279459712731943 +8253507041283227562754413288994720899007749153170225893148651683553291625294974124954074490050789574119491844797138467527966227 +0130509196073119104402816135401821552059355465255575401457469499625193746950815086568670490180348072693785487568778765124602003 +0665248124119376664057125582265850335568981888916788294854156077917276025925880496268355143677289608433935345978812154208855268 +8995137970182597573167629723687168196388018417038021858197249313810312106750976993518701591284371821222304917627037046364482239 +3380163047424234535936046961259910243886634603953810906816744443879125914109276466757734603864050055610106148478096732039481501 +0050863632424997195774488430947153709833716798293363139764056905080948883372556055175360309201107604690863026027029304809129018 +3159045522676282622055741762907017038764983291766901157880220174438440331565498671114637842462602216932285417970008975756029800 +3552237063449291059014349162828946704972597219745549772402644156576458713144836954411568696563098211658803621586740322438123527 +8553792749167921622407818214329886287689794104816641507269131593430918574387850051670847355476478466155909359159406347347073422 +4529077230987714405406119382703861571668341576429308147647968381984164008665288474312273798880569173169482210427510225857144151 +6873466267196323702969428729542596817149488353193513581368049814798979078196465830515246880993440065883452528995023407220294628 +3423764900702625807926494346022071251875105005653050523263476206366063134157656968651581937910705560688575444683225925304492434 +2952113637739291573792177591055762311484591345740602655920022962868733639951191003783483933798477891209504511076833970415208477 +5918285063915228730571970492875778946493408022023485280196322884176264848374770469091671934052402742006902024842930375953314997 +0188283754328437589051211543538670804314085522712102214064193375644277314824255259794129098802312806481056628400349306174297818 +7400374465412082001463306963438819366261869661341067811137716763449961651069723995940239925017934226230532596999220744932707685 +6210672490680638537370704393760193333970297977253365672997212980801329671424210333810500664050546692669173806880821364438329547 +0441850655848799432631375117017035628999017015487913470494267041654835295714453029034648907258088418618148803149605119491595378 +0667709470097778550996561398901373747557627918533058120957567808028277879620594852014529739234370013800886373620787541449928329 +3125515897530266841998191196333295200100578758020333846745697177799969213733901430202219148194889600422499239512275441137216920 +5650415027342249979224454839045392096843643602822206652387425455763721764312986203030589819856297909492941454232871627676382600 +9334860923952422719693805188600329744718934283065130982713900165662458916981874159673444880197331666542991521443060535788337790 +2260464469972488038180467803562022523783671731570132720326212442908194344858555602668263231905684505806559421525279834670049624 +7792295078720351079690690721334629651981883246931635344717773193932081371713539221551527543695110154112122644215377568071412160 +9195473483543180179899660415512485164196998646076213127620507751563271324222304453084630241541418631528227182134165081602418516 +6107199131518105376224811112336028754695240510430182476876463353393744511821873843541677824666782764783493452855094114804483949 +4681701662176678506643507046144062278379351438997367570137464357422720669913574736380880061893275748247043675527436431955366100 +7149553832033242764660394022295041894654956152213899886346680985491273200086975419330405272293533518246991447255727068177450390 +3655198436587622677364860349928319378663047397528676907239140945275682820405657108502041392682979560846597921188888907265931036 +6476533442276806500783053564071177712515831376234094631885310570178318133342242810813665266902989059805523937906419013727894188 +5431372487274298724777517697230193875880937677668764659986523658830811826735084723524757461022654674192380627828684371745612348 +3820846618376083469895407196241963862417850743410179248545973893510278183277393709562656195366243761330452232662018800107769440 +2069720833072682115347026542393190511365870201586051736004598121210837565385863777323843786272509797134422300430192141780468920 +0442031082909423594641218252063553777303614020588281829250848330051744891712248697144729676289337492695365177253047051776547762 +7268639820077382077506959745238922291525171731315329144119549984484846531136030620064784882237444115815092112452964098100945835 +9872606636855509027099907122315750101939814965155251567403611737103452272009598076995597635176083234788901670535978209531433759 +1085327638195333244518594079295886331726523118261222009427508754702667381643585015276186491128806622593525832828640622269002176 +8000558812466886549021793247340135947691306446427709528028498200426365682563584527832696795214569479985794618339152398807896264 +6187988847312530289136504030984674599402925257102785831329312431749794271223626066515601194350356075536808657593446997124519277 +8221593778261506773470868068030158332034878924556789236087379527748372902142937754401272228850387450354124993708739623158789162 +0522225048132576688432285718958345124544338046135042578516691967468794904627442698787375554323557096004260191198734997094093259 +2462493092358156921232365706546429643442698260566011347797207644508552329524610339812245114706710736520675636593241151933904714 +8168806409481440220552654041925492552985114821706878419857282323004033916474011864732600044228285384350058041710798941857144196 +8456183927145192331984325229219292792322346278563254801756089348717336352552507588302857665733398806159165657708323657950532344 +7959027432654889449179694030583164431534165468437531025326379868797173494533488353046359898220027884040904102921916878818230634 +1572671474039612214962772842653777925720919829404267236601529791378862012346769450214240286963754895280462160430824808649254296 +2713235643749557564532205594282964657763877791499599519104896455051738725475670902079776954156280521778944415643098383791627273 +6105964254598123007279257593697900824845572590247120355844905046052760198392683039070579501029336629655707492386250995234644336 +2615762358163929247483738668566291188631481608731204506773561888479176007728316484171079619334578564058039995926257045933155309 +9915284962226636771930258072321745421222389817234966851925987744510291810977458517680205953227386190750476782698954854970577352 +5673578687374046860672064496277781044455548859373704110716891640260365042726694184132711815871762240294031220477588483951772686 +8668728817231201425334885333412781573848400038328099971193361147160493544335456580227648349461318031327984792185171580102807503 +0169567182553246331404084202997183149669206523460201655479386533242862563036582654644409484005992680312894606697548079677284436 +2408634459821807360135371085559754639951676453609356475891807606645289547821918227592956096670383760258316085039325249167270345 +0639753426126492725051857242781368140995333223012553263382165242568922620303770347983405643905206504900791961601071942284936427 +0871588895354801816598052309754202243843057099627863238724184518284044817748295192358320828895054008513930794283973928514112196 +9798654324310664456635488952439998180995428408476235283937134417681364575355417817510233366035067348678178856527757300827180944 +0038123007290989063863175985592778519488056333171045377703085872568160778246582018586168915869948568385646644298618780983546355 +7869384600764512734048062223566504717806018441987433044570169614606144975153300488119826197625737171672582011169222743563965980 +6977293818241046999651425289984525444775996710580402650890897413638457082477468109575400763448366802872239506128418904706794072 +5799904603862986594106662322483455329279349540532965537255861860553280872874108710364305979053889159756036410545175996560632213 +1905113425852351684154357685504469533745516493501062212340862826870385326709406031686063551224557399310052952422838419329227931 +7546871961833002037605725905779115533289685288706841277287949763530450546184456445185142124516259820881277398739985868903009290 +5390552777664596373149764240028438950449670876087502765011571015373476441820757391918243018050536384628502031771242890575429423 +5602898175403670523511959645093602460733826479169195731008780332981500631244059144423791577718260284080812356359230721779826632 +0282070388225563293057775488479437333861437827295440192975141284220068554470734762243654683900921775316128776798676828649464561 +7044919729960370651911034098935618095727733916533962937362197410606374931103363062973825850629444853848346339446960413652519614 +7923852433886365419446901963455158024361204561948757781294971510735411634498000899993250692952771388573089449720039510778006556 +6448339128502198442312788117252393804187619178631365039260121533649897690132658417974431016238776431953884429473283621621760928 +4796673454480093007496675559950785558242785307210144883418877108437084521237518409035829359990440607609053004656855467826840969 +2539967222010309449705096804270772495367746726394308555148839954759386392982542739753442093370829925036983594533194718249508227 +5349079635800249074525176724894658939051245265345143033311987922668779171808073614407324686649849960572890952798590407394876104 +8881074380416317599148271017700792806364855765066986663556628051754736630602571628284291106232498605571402693983906331712030753 +9874937506608818776772074340810498838067037288381814044995123846119534641571274807743575095446827709625774292925956993651483030 +1351712538266765203986454072646816009684736475838189392745470191271779327758375268068178391759551780428792430541942252314370156 +8407859484892460620078868667802179357348492180685724246644370230014476781983275135684947107880402240406891839259873980451085903 +4906163533191850256268583682454988749837419823578079473319346296433799092047235338380155019552271132115175763259341940167374009 +0058465924482070913500147064727037054124622100326796664614046484792704959337155884709332716325316023891570424872580884952438266 +2436800291590505288180953642913700972896437288014085488105811040747638787748114199225450616098949678241069300416404785919318926 +1934599170792002326313718708164205938810092585230289681773510059324899654472028348104884160918328871998687720554532160558923223 +2833310668093246151983045850009608661418327316614685260920047242208439507520352050936476946684777115573214252016051709311522733 +5806306154432125296361988853844471579270461201267503074861984238054921903008010783021332277527690547871326604862832931571535476 +6953887201338700880983597387777946758689636635028887005208234910693065403037992011989492126099118102129494636864406808581750418 +8449828487903174265578895543349223421139970247142747092518020699819810023484608128979286359504168816826179123882092074937458466 +8429190917143583035383484835202769619898592015997875633392860989349020465819040052129531463007708159553740489427112502433350259 +0015541288911157030703391088218525575081861192862772160417721389983310098584001488671913042391958271599517521875772879817883670 +8715721723667422477987285388090627370795114420296274720333968016528373376502351404697409435351445641183117232448408900877983999 +8085119600169893659072473239891529571989304915800525658372770589231187553832358134049724486998092398100951195327740893124363904 +6825971614998820258858203237294363381385260403003703481866209020749805588161211696398124406863222077112906818160524737133236376 +4791287439396340287050946270382309108183116290058907007033269789030152170762597022242414148156450382489069745048352057183456698 +7699407910303700018163871163387977417734630895740976320842794125644044206230844211892365927545147294624099819954065586859396532 +3131672188768311610064467750350104984282723151135474654890501043106825950749943883966630006426594607652504022624213316206819824 +2381097970942205624530028578508606408894416081148567879895026246182144217645044116693226312513033539646866882479709987958089969 +8906558060289356651604127285059333055318644242901255378341192868378040612451560944688746385088366187655524363293058496259834503 +6045783943499511329380195312503328218913377235931966010737751685331040259612353101709972563764048165640905281453608759750462107 +2697778235378791219808122722229257971170129904297461026446932149333464400232434190909889478146390656698937276924878791295257123 +2803907707992452534631676946969649948134841465957319668276815925648168701088565169944440306293458637327427324437179025248962792 +4731713616143649458304003973798123909714421863335575637937266819992047643342146589405067966192841049385855050988705354679370809 +5372002945727744156997261317197525282324231196500161732336217784763019914950265863384886596392669097800628799874734609235337063 +0503789478310331174915570738552836640716185310681333898360502850585012405514745303614418697185463616197057663173866523087604838 +0675037169916204490298120588020089851962810662807481941976840354009840553886735946987601156791667665686995459100468477453975696 +6834794049663185275577105102245452812771387897018848470724588835525127031721746222497385677737902506111966509521044773531500357 +5329174285407427305661763207997349560549428910454568831186538339414510256938492615597532037011523362904523476314189986503671291 +2718934812220309630578226399896956440076865897466517017205703733213982937662658920267819470286608114781834913036578565065677437 +0292082835411024840424424503720769844545589088751119682651139453690649901159394940442554457210310520610468908232856172200322893 +2168944494860484191151489757452268440986053558799687398611861491809004454116677153114993411395893222172780575772632409234094898 +0022046237220608888434835342573839194475118566063006131560443847533169495969846042538404623478526559762435571079702487933385421 +2451919192203771937523596048769324041291130793137730493766482430142235410357050813575163787479425148259744336129889104511577631 +4142550759732390528563707063152034344178846568888223418447224601319163578533878251799463243956174732623278426810354689051255154 +3970640202120158350260730117870456696154229567633606514596475612129033212370234978048801831781364493706668262862093481931515716 +6561924140099494620364526936808552193114041143469574838371934269300545161105733392695711211423415123016324534422409169998476243 +2456534483029530345127697481939835893243537886692901492470290039455511175154304300500928667293566968811328935461117311498383274 +7770989718625939764088550940976599710169478296655836026852130278698487150688281647540447530622442916288092929799874814879443957 +9848763764198437026743206591847265528425238321090574928951078930719540533808196681934811602035009442951951047723353414154858302 +5787946077159159168774450433330444862738778849768023490388074911496672265084989382578533753038885946437129868548176233233930301 +8580017107399342108923739552269357825947129658651850931301283981215557531809779053507136238066452267893979878894161383251981836 +9913766525707178278575810543038685579648619267169136094328824697618419625108889858389235878109542078438921757522374605654744287 +4829229647563208502985033637245591132219509835319418276937992741995767258780935271942567981080789563695276467555561800638017448 +1643138233933525491385701115282638884356620775214978523597941747167932566311974729439511528404991306505854174907409034566035713 +6143827997778747391148759316186369744390378766441792907186485522361511087226349625577289271875444080879557771349971812896783921 +8151037464162282243454432112145953184331053916340169687740205103102595400733446898088691425582734935373310066807019088114280263 +8729904943075565576008704186063345542320788733990798433124383054387825974302546390620173375209936756748717665874520884924479185 +9888909645307351789982700065033105216388788398262734159940626037880383673919937047828435841493103609687935677331602519191613414 +8850737384099163846794178899558085730334694261907207404022320796803770107876986360807427894048245601008966306481139963465178636 +9603978263464022093387313902323305847024620528667574592309994157800256182557220046954796119376040712963053063896476690374286989 +3405947975994172973640043088019552492100802303545308377567580689104604977659913539104938989315677959717470803854511462658064563 +3191162645413432233404183050706031855638390349798393321645475424393193065096426473922012746067572812705091078572123464528104522 +5754070330806062132016998542325199869791387417374085617357171312577717425296247007640670351991232446756043415004205160478899347 +1725194535387949369475223998736399458938442651506418282232173272224426998356393475616690357433350683532939697909185050724112276 +1531609961298179460965979011464443408544699180131732903518616680630569852510445519164321486062756903290742218453214665562625462 +4277461038526097907874522581743597670466704734455633690080575913829222964025366450929288253518473640288917518499198469245997685 +7420143434734074307744766434941598412413533864920547945213441905542326713457623437002740516606230037868321117285609425877483514 +5509703411252690324400121230820675240247540075031296148576782911392574907974824839505516956261324207298130826703943230789524466 +6235427271350178310739006810179034776823843871399578101065895786301705049659021548642844679899589499916930305008178597329009228 +8708376738514576473422684695199014327164139367786002985515660277291737951903297112627302613852418677997201436504553518932602363 +9576506417537859055584148204389221076679317163739081159093922729562344633399336320523856409926599959155324451664857304036979342 +7821519375657055506931717396389844553686714012907465431386872822303418480617283652851337727643122589592938283893016029851450741 +4046931470136635022512650864971382510668763279223722442081085741561189482186827034926915081819290103926309450155161557982601266 +4715454308438516658794722092196206636934007267582801367477453071665674490724227142539801920570519231994847446963269786268119916 +9168041401535492095223153393392138627537574429611826175866774713151956729343684934892542983754029148830727112711391973335239513 +6515970901191127073416539618442535739664555006336299291335206114647465786032988393861387837528101930282563813184667131516357399 +6965025115159934457674823672748261872585621777883115554117585206762204650484234255271173958058407125541367807442514328611570278 +4831064230665616198380860322957982656881003885916660535633639193463684058409926839010861838915803748651536627056017889319571392 +2850377483892889368048618651662404196455568541580082178826055899571765132140934322132515590245447090853398672580050380575567268 +3835035801788676880902622563977805200108581423258706942731469718506605351522050771195525463348620059942550507473397926975656075 +8170641249931936194409801838731827726032358290235160889838261562910412201128942409820711534658779929527668900047257962358847821 +3914141889006189906618830872826326907793134446571712010041037077410013305523598603376182060668984447359850821728080727595426748 +0524288363005399115153078597028593878897064055724890623451683531614421839812503951731500983120346087995189240459208148107096125 +4155453406508028056687037448768955586471685200246991147018630064209861444093780513311415335160348800209398873165630411940689888 +6507947702834259046810555837139361937816955993601288388902706347465220128545880169696095175041923698015462911423095653174399855 +3762547022058799188111929405215622052455625552628888943381861262479627747983891009023031983537685246888772136743333720092806316 +1214549690057757944760791464294073960482795674578197576015074847454616235022841464586043262062684847644609779311315421146732973 +8541338437296411555067873828147460205050118794445175546707442427376724688646895066999337376018755680130878200128406968859847082 +9053903102817499105181101245913670467308052365562812081988351351621302283720868904030918069105268441314194378171102376175885834 +7390879175657202991792721024454048282348887367327525251428650799473344325817058656761335824140200255463527106801978883972849023 +9114858631082305511415208423205161099795092979957887353079774012813446208145355233313207283275642795951235302177270570702859527 +9437270466650300884205636449007422609778006468400075137707364599220020468818147412256802671321760977139033664343323597425197602 +0302618076580610803854795974063518981909366239035024103586093127902225268380234904318024257226627138837362185766291678782029207 +3947830739969480621126671162942463986987058328256455824368521897685045168203062702288692030759860685197940546125045250168790201 +0514803654436570370324022741742361761085767189089724981006110323674867393784988265456553747645514420643289677440464150052019123 +5223236088122199447858422156052424410894574207990368063564291905403412016110145065374098953504598451976662453874666981008325424 +8373293703798267046749026333095880042002196112554036817370313956674653924174972808775910245525212215059736970917924397257890308 +2688275927997288192485396746573266295779844438202521819272050705543165472702597009969819708658965256689368204413111379773538064 +5084094239742086467203807611090720704742678798346329233433650521401666169943202745736920597870661200400002701183019652790391816 +9022357095836768187469229301323281507939417769215045474272231522798590708421786910823915726035891376298601448255283206715063088 +7293988737791411397897393109492140546674166871313197493347838700746175919251207836436115320719048184023218823514733017767389387 +7188783211699643174822974868235729901402276203893157987109644622416046761772620967723823455916506713613144614196470266830456333 +3793370001173936142455344055973062003252783611956485218268240522116090384604646513391487197827194186399975132117817107687082668 +3534299426891086746037016872943029880008565862091417500370811850667508670706496943938696062708498042156433801209832903518387378 +3612904603444715041436728628573199366360323396870491816934924984624193337784171063290043144432026274325167439913901462851257791 +8301153338470277907154944290430893973748596163892611069628643761048471308522954471189690031882490684526895423581669049412645780 +9532276724709021001315582137581907800630208033706211952374871095930264465397182584969831143514075753862615083949416688307055905 +8388236378283440896890187881320950399116639874885181013806820276432717798539692296166413239097562927693580121835296266376329989 +7227163697631743317865706478603326970676879690725149096075601615664615649563621319796915072035936935656249230727588009066864898 +4030193127531435008439718127581289588778728561167418433131262502679346011703797827534339742404719888498611315489306325294200918 +0822596041854995877122241367357937629640300673554434225279391124715516842541288461765835730255187409409295964047110843143360218 +8020810762143625695211304403500380226297593451193440072241550883222759127723312388393491431821749019440262383840735943032001195 +8318237479489554100626369597427649553987985282444811016365279010859228891440704149689826185729924642137537603090218575037482996 +5366847590778547088514990246112762636619967032116058526965593868078207507263589910269699995373744003756645473632293041062685541 +9610845056170949836472700450287286628803643135098265884778110832410350875689137416100768678349522724998316556986352522714663266 +1503726130267377258411268772164465860231491482249362263232011498460531343555282835085236658598989040170763967052694908347332552 +9494811816089738912922010509947751572303760145536902984045309394480942411502821839970060527331347541450194445987076121267053844 +0578064800062529101700072602908332148334858119123936457716348054959136175842817084891684097218981249238233949488256020176601607 +8655837084859592004656876506460130727405913211331057771278893395802267144808219629460783237220481671207469404413079770236276202 +2474357024202046544233057042050406320202093782109005795759352430295741395906001878748884723478079164300747108788119563090468194 +9729275772864119080887672349769974091030926766697899488139685369926038289554489717497100438993101868557613700320201349470541544 +9690121476368923069792412981332423045488814436625785624017824621004713254286195984412682695459029135822031307061078889173619865 +7378777371791906660635984844468930766958298884404755753328140351948727806677530013266236008432182713748064589578467419387451399 +5958115863312505525587296099572988051567717080297066945899741469832877228284562546192870097158128338501540449059947967484049806 +1525360297576645372782766370344139731393001666478941675207716176663153356185352097531952067180451056483850877049391450796637643 +0957180541118429393622724140553758348088782996643755097821436674434023381251140872906157549968145359578492278555879752671991774 +6216547228847936682579390485586625009476953902113956539812453553726263850745606267922068236416232274799633682965858910518609181 +9364762411101864870815658017284551581396524871993598349274914322101521556813705161096159781789698091708777511072561306773711005 +9372502462001712636254133123395734859574314940832644465721405982847453658518168263698099694805090947049141539874161892364044592 +2081215406680382103751369362572309567943724594570823605875011736925225634106628852644577321897191396835476563249653975626106030 +4349971249994089684132392481061788229278189139063384564147513903173876133019427848009965641301113400947275878225261721625731279 +0221661121010559617008273964451895647239826707650461656354872916410348912631911913485548662828141859786059545072528125011374221 +4423053386826713852263693496635804173362348029571154806846975163821408623542798104298751000786068795017683260714515841078366993 +8161127671355970070573405532186939626037976009760161070135615665381704277318637283166773287886342633146869956929128780714287331 +4724406673554759228867043031026142833731777584308969100843251135369865682593955020981079390823701606661557011686336153922127671 +6320174341401698137531688064847461308245007114188039688579142931690284251799997677320348583442367762054912291140917964811725137 +8202757825011219494192886746445679726841310275929718330630078072572216660349917135633404881654966136524906313940172149175596901 +4973259052196790674213907468121479716985489021755170094314094085515986880378978689367545392713051595516162518064260512413271805 +1589006020451052603264805726624921702203140540281094471735610296632014298925191276086205769087410850492910589262019474771202876 +5301512438781961299615681939122770744864098845659058825055415852695585895078281739508660406939859772275150311279745599614771126 +4288153404604175490400855789649207035885124038840820254085618553577028061229192741635233861987354455133991517534727391845052478 +6258955556704090814031626148938140929500657324155298474719121746871071846048890688770977276676240866370191127535467056926311394 +9986426460698287989606477786579619338308357811140009068302509875335795457387693847614672598016435455104290716649294340111672025 +5987575195605597807571132149058303080863960035941811196802177359118666488648716460574884370433012992324776906424681910784438914 +3212828929093079665577584834614675181147027766122500255332258719353578136517199306847607401381636227698622152320466280962873067 +4298697726297500762842637031315949284066464528353792900058970168170276550530718742305480231641988165044232612666352048235992340 +6528595274852601267138855685497658048124785482783676783853867479073934060681129115971059226209525138395957477851256838662435594 +6333253678417649760854615241319608105505866305579272971991684020242948061790638726814427247631657265086447512599362760504729659 +0050552281101516551597310571530814588813676939570968218933911952205870242379775297989531959401920856446210979962385790387518282 +8767289958783485919140054170755933223361121752964753574235200401049015889601531517259131960751322512615901642287582440268512260 +3451721805339224609531183820195682695971871204396323817335046115539455320833063731322682287797699412707522241064736997253801427 +9838095438421652946093122030638044112929662006537527680294782737078006325003341225082530500978086841673076833294743067222781678 +9343880589742192946487883613246357871516859764774916328816004546765194900049207055237803648799342279688804719689248327413730472 +0721971662815930264205326239766135366858450956630362245585637639859898001993676716328053273905567456704608882800064784850223773 +0571787432542029050900323916790197508546510517976343882254348928206749045986326975042209032481347983325984442399998495068210198 +6252008138898607709468342620809002496223471138515742069863151888069692871096460617937300581107228988473399264367167623204729419 +2797770513682558101443641688720044042871553905410373246365721075390692021825457052959920292945148726156660506042073326859993576 +9943199450035463537495910614331149365062244685626306093678223057245690588586113869249388996930139917906887631260478153521760102 +1950460426985566320913113822097771379876492753786278737646554639417059453209917138732302032256017931681335738155323404263671794 +2838332702415027792861677040542052438018840283865010592299818970494138350530560217854544968425238656792859418734080761867272852 +6010463746369529626569768506440616846423788144981864845682008740038225629815725166282753185272670640607060429451038753678650440 +3603428316928538153550803942955541314085414603075220414550257874931378714081625009020301955524753074400211685480928675879412454 +9843069053870715332522840750526561812419932672015609424516373833579891793837439080150576601064726524016745608185730860593580037 +4790338120349419657809887950279987231778879709683733319593125286240898651454362025515251496405484678061036071464115462928635106 +4812436174990135339020055889098969606774850114068102256726271424987296420443648067709292253692100724695627779636510134753572039 +8737165463831035630144985978697481580718872897801411679391604620458063323095158841009595789901639579062186093555920948550517282 +6684194497843892923374409974962663409557384721186689597945008117535697913915795350771799536776499659499378233933827449930665824 +7081582116471192658574426394048422901610749624872891986363877792779389085331435286663561611309448994925468965045010492951543901 +3680646340800173339218788077063204059241257097481947777145409084202800888856762009180382127277534304230302616826742040805136435 +8971885064485253090204122142512764196490013452340872579790701372873179615012971724582237981895972101835369658929642000295179467 +3378697466160221993719637822345980287088027594206423215053076460092623235649859970956549802453476198188280999027211336510921505 +2652711751748836279015697510746966396333400935969997511167766407036252126460465825405459671454621493581082715193532241502957665 +0853017906034458377960386056517204258262136080721053029202866891152909200067791240221407899628896204792320935757942824137109755 +1860549007099165197284140872678756594535459353075535851412365063078974696027557562375802944288504306767016261985686187927040312 +6976439902255838121317145194266076485068757990450059106045155783172858632535344129113251970702093415466811410640476211193415639 +0223864295074452283847305560600110714424512989818888930888094361164360320952698546375108408821282297940103191521862113682311023 +1062349856533324628662077342594233105268417141054370232872638557841382516201012169119621084906023902818960772903834214834404456 +6774964345965594961633616650657071025352025214474328041017041037485163730502274877992356272868337521422636796698369213270452116 +3863759791559426852045781221052728648632252466126078672480213084512794723010528131976170544126507023866299598547784848361822975 +6029193859923289086357438220756059069241393076481467455226491973473927966812070493726378405565631775752820893408930243604143389 +8322645429369121872596731546622494472770504292226778137537611049076304139170096181584862462563435300455143882748092410271989379 +5326365807733260201580801629433084263428632745627040818781791453399164548163815232430786017320082301681273560908524949863305250 +0127708812120756378948747377847983639315742195284498126386446424753766099623540522826797908737477298768890173160498925727449025 +2938525448223494002554985481215274772014195861665818544594199880542548809977177276693721985173829796357491362226406805154130568 +8969218337326188250536163628581836759650127208671515917790854163363077352794417610106050375787449708519147175922505361662892084 +9818570904194161360123724397774706465600642465735373239577049512865171271762814912171304543676985961805640424586977875904966200 +5504575938770959529220023577243022588499122259144158193746005510614090936240126010183628014128216077152330048541538274583951558 +1040854914239994126791549056393295346381943572230033663200164558894293446713116407420635913349055999596494259230893231105260703 +2206196014061006612340848198814409910794456229071521204012047591463330479752534343609457402515951279901699854399989958952429475 +7726196843265136180636204555752895156585913253117092477574090168474904085406088453119889514918675712220848883891583790692329162 +2932979781809321279966736276684170473868093894960203921581294390763273096543783962941941740885305313552176422059326344349963187 +7716303606885535604492911546074803956458411462697046763121884171908019143640932002882802971285171891988638807617682091180833424 +6415461043036191035529625558425951185907448251833755623616264167570352631169542482639741527208649286390063877760359771911261699 +2470130258381358062929085215957585938262216089039199788226601264737025509814505817029779107756588940453410080897136093992799084 +8707632659399021754985431789449236853018112947892026429638184953314257900880019675773825490255337783864219014358786450842397851 +5533932851049276649253727477601106534672571299541307151668502286973847937896722437084410916259029864044870523541806218077954614 +7765554877085157744714903505492619912731173476480412564678818534684847115861709661320561820994876787782948571629974821128726525 +4958648468242228861341995358858962307381366972784922066788447146487993304134477988625345942386711833780639553271598395688961584 +1195610154724201767373227499183087873970044260009821201260837039944092568903479517438262374212926002997007192595185854104973828 +1619364472726153539242937799692428699593667255284692675884347709702639988899768923848970457805127213116307471608615036594270036 +9983245059851562634241554611084024908123182475839572134087792577459483664336562817481606414919540980247553055009390347181343478 +6678203813070191242843917525205623295811411258351922528980269188922471356248950351834938277372740881478275242174992792567496074 +4143356634732380642139347869145766266914672583108623737574556686979737703412637109328384991661662700191639112734811399558605096 +1208518184191671627470570937029456222980556566946874634805220009038965108642318793927711482497435062563764442708954406150308502 +9023312095876743701169506435873830799968604505754154998579861924311377960500475654542205202570979796896262463835446621504771900 +1307938351217139321342488304983730107947723886674861520928741265078308049241789485663045316829261201640722069914290181463616514 +3662774552959414495906707805907185396346911240417542898771207054497319717662592108767592192807181086654802759806348618578837674 +1314737004679848142363776160351558775692327407524329957179113590512266999745499877585915362625510835561292623778541138597816039 +0237557760056059081432345998516737302912533244471786164779569231630397999149258259824248258872090314272651220971111732309368883 +6627477978424025488072609778273019056422203214910145966781125727030515618065001832071393756745595983343475497495207623260839724 +3342966216908293442342817978848639488604191273604917723840719378411000753792919755939336669416561658839344042868764649850515740 +4843734023345647692176295441180249482639581287413972097837107583698158034620661397371501344888180961705583629809207419852455696 +4443380077789857586353070023335893700728665808003439355680624696628418230215872552816714310195766329481088338871468994321119252 +9699439754749964974015300163681954912050318298048480045612220669380962262620582805353459791375378214164628533298845978013550599 +5105714350835688155081910384711576767489399113130569825483778697383765314872304014753426304269107388256780976698871683305123430 +7876749639965180153007689268468639912304158853044482532644498399416286758021429281245070574658924487014301816203665832428101533 +0785808281226435244861112112960701400035860271883263300268756398080488577467688265527817734331234380039587960227981962626600939 +3364281430281513574894870100381156896475296131097244720576919647275082387947394192830802845871083057512101147458345259973497437 +2352548478260815071941762637203003924467368412816659772061510861905396984310747338352315940227937768901334141297190271981978620 +7421497155149250772882271160825127041610751435166048440557936723442915599683580765855387190581730147042311189334715395286473607 +9469405090355283185848987802133089556363626275612421572132540664555746805117424373802421917096772214790891162942671883468819382 +1589417274385691408452755936967864047060787328586473526198572660582234669906554955441129992972950187613542002387015080229290084 +6570356377915672505761938151404215475617807815543585573009407518371861507851745477702147309937407690084987324419699405440825645 +0612011361559116532371585728822626979903698606786712794256913394582020905043090461211508518273342883931278463866340432838401156 +6760882395086214518227285005993629709889190889508417455761844997600135276044719721514847763212602718036483323892756763864051034 +9188515996754462022639889466629680669832215175040501200558563344456278764716592388326318156786971890684957626542747491478209927 +8987779229113917038185012511339563267826387201889188999106457112243660191988731669456871109523244231764354533555547239773405699 +6489667993193399907320056380302219372587627376810666191791189051092938301013845696380002825207303265588504074647639636023424359 +5972299897263342080767319345606033144822159869746010125714876643532020183420706758750193523599953077555495726481141414611132824 +5084488926501974380578938135730935789462111699625550813110845128956753975680460172778115855586076226024263960948204752675321380 +2481196609759725214010722486828189087368975741750919937794099496934509151874334791821893377695611203497436421390537165843568949 +5731231776297026471145248406339411301301972034255825750883890535679456779763247607668575953175369910078981695492172415999716775 +4579188403021146726016160128342968838235818738719371073259844121400672340573224465598262036627730489472180977859344538916635018 +4072803135751982897056486221080699066662076832459383392956264311706786163956015331215770418998634239350534320417157397046765970 +1201062381226118587752910736476250954770728984407318744902312031409265281863798225473350200683876473994417794886956931818743554 +4970302735301544243501924330552682090042743013078749961722848840311456198559231934700094783625178641374367971636230883796585194 +7528520099930871600504738859526542685454459947737830229008160696355714649737859829804718726706633142812661022345510571151124118 +8421496990755255433515708880870288195255467739826209142506221198099596830401844825962275951060431553735295340571732198670724017 +8936767354220444343191661460546264683941090169583805460925893859976635128662736335459378982086771782291262296836018682528094985 +2091822408378038044344571653062122443897076701230437493508375904957950547708046448886825832044918141237713293307448531246301325 +3809577056439308352524023850120731400555497421961772257434416551729487887916281181723448155709611102047050048200739035199609339 +3691135325949817192195090067443211343266627775946016712851865749742799362777412328641698096396095788341581675313236780103509391 +7728571205866997785438131913054881617576104163904406501238204339704104937888486853321561284077020503299289883355914615218277880 +1111888880906892863133264981628224969781528602979570581183128605546822078255116005933260161155631478832277334536041066268750461 +0079412603436706091984459336642210703864450821510237108480924013766858888811236465849216492858539373276328300134023520401083981 +9032711135936968864615808603415924938529404508326233015998723558019026312135753805702013665081865026922455487474085399902382380 +1925270181168851392094205156290625337733809503843149282248914199479920984244708969137863994130437975788618973591803506088013062 +5670224338144189138135191184201868809626683864671529567267492650290228767102161777058656146469480714765930525720049680179040530 +7230690280352425636146176530879993888167423730080287061127174186402752375468464868170738182852209838339454679149339529988407952 +5792512066685362806154726245973930382853685512688261188341000372263049656702777229779863118583825044769296407053427341340382043 +9064288699746230367766305172967912478546347888349770026780079432939827599619309710342120385387650986045115258506762134679333064 +0440596074705090759141142692292577561658123037036520266673482910698027486128518652316613594364723726387332531338654819095216165 +6586405699035556181676379611570196812319147084489164717015394853295535428378692362698265567241807453037131148384149957093387466 +9613627657031243653069021006489437859522461371334813124213550061172753326963975967858093098270917873153645156857962187738174651 +1136920419639517807349446907609250529925657860858085930986434051082566457439789550530915328824048952555143480521491002977432620 +9723285190605612751574356382595987410097040493290287438645066768564593720132821343589116869402530302205558307160327389977244390 +9364492329019171456987187525681281472110463501074399197748223709921114091308897093844593961700969890959904837552334397994454560 +3052838056301761186571362672662664380662089189821373489289767638693120179754848616628936578638481820222476538767896516701765186 +5650968841999476243265351662185220984505585094177664675790716289996388896832182868460012463094065416068468660526498381293391126 +9133890615055659775962704691788335028541094043193982847019337250184246131745395473435608325229593796115617156815138186553076713 +2129458246410892733861841029801352092648607760367150219892460311630359921628868730343711772190656001417014669173768985351029224 +1665772830534776231474157241609092342166046079774561656411248463883336203142778691223352454530469529359343352214726777533890124 +8331713345182730719576279806483123633260411891324365421657390996528436543606816818186451009421810586108827795803801756029033502 +1364037207944391495829014284717167206349222584053304291053816472070825719135329033244226893785498596526306122826589748158594087 +5166290726387661882217520391175991236803942524737711932725364262567914513184343152964650814367428507369519194246642124353850820 +0827084375654733169383428093676562471650179031999648254047083978351359301318312364388282400723012984805620892215650029052432539 +3941456090006715583530315193770635520541951076921761324088412023106545476103418543330517872032796586561611105128147089091776975 +1738857734263197422168561159954861725900337109733462635773133902960240600973009046170223803445433108730659547191448827977753217 +2930699447354813297267803165810088809944869393780407968089194813574507757841359950100327174854511476594508709317805723583889020 +7143123510044372032973820765560757174132206252079849711539375893206623268969901549499791778646394614259993009419795504303081399 +1805751816651443729216778302694093259724338799262345264750249439983832664270852123252647682716640466581767974713902148881103321 +8726470726559295990593930464431140413396151555993515614244807290113117258035592083195115356107951148992883803789239385817695980 +3921614672869622892796645795274584202330027636891305986908354816884938844639467554178364479042973665199423978535726526806853994 +1879696934764137925030122852767473404168885624623810742903886724883175975872670995314600387454933128918537822599476294544895031 +6035100939350635436051361083289099777664034736094529995818610825799950536521416106176327389907566363175789821848187634785837579 +9000534727303607788183270598866205796784604375642000912263136681354291802540390456321071569535579301082240092546052754082125140 +1036718094258942656960699558474884940333329012199962569808690163604756147670835128428656230386743308384472157735698077816035891 +6071364091704432824311172611510490775020204802190669441798310247002139209451209194509056421614671635249279135552841468497623698 +4388935023201010068076632458613446034376062184128689428896319432854524321580911853120662708261911385263651607882193115809188251 +7169742597495615052582018265793684790742777650121858370707225197495242787747124696863007018741360971574027831332907655096436810 +0422401456149077117264566129183248282220685815602963735110673297177754943137791219212563311879171990788150209340116758159767140 +2965862835354985730344339045722772376955873793360410786309094363480176270871774839639924049136780870709007238189898961666868275 +2861817902533679195525000340605947416733205573982037034681250485524002724047643727596821058644083422061496633576063860945664624 +2693808963859011255454082437160794171986401169181404558686995397058453911479501396355062702756373669284391335723793677898504528 +7653550559325583799147678302921854984957213549656349312970370601725315750255349048113259251600824306253788257181726835044331139 +2669198588461532129599972595504488308041821964826043635029559596161652526633086414778498340817442875389328897830361220046753460 +2464008866849845585427596306460289186014718930150794200485697147420894155928351507966633760981788686260275637265508592186109230 +8485080800308772606343366155948976547130610312843499421865489766584635822710314441085925235277117540126323440360094147819765072 +3284154124684872982729898726330863362356401302174583854830959819131037571458234265917476543549149369136036055331113709805046443 +6448518117737425864310082406263081225358862769781154236900286043306718901307704827192440877547054996291784365092391305036785917 +6937932081779624691771675203207782587255392167526245881985222450132700822827458180664476166098376639952639111122762216172733126 +4351428226863320079499951081487882664115198758192473811033557945313783704183329712942802900894464150489698398293843181571708391 +4008673285830233165375165239225902635382169451174378889470574573298752951780171507430341658864697744996790183195739971709305547 +3512291497844318467447674406573948587271898370849211096489030445902905335005315909627516994616160913716819045790842998532630215 +5522629798725110629827671497041697233370597035945650285606531591280806838835367751631589755667302233305861288279338186531242009 +4038072792258238775154639697267095650759428219766235775721809805667537846787229698544718121429627243715285966487077144477541907 +5934583620628679989854657650575271470278836721346368661646073065566282621355114101575479793022577251192819889432201758294108462 +4480564984351469882094948004921988867959440174254223793056014056869119031094103106976186436427858310194322489736576393894678612 +6908132113303990201784965196474475898344897609537622286517498106001289334026874927830753197509682053509801560482547631691338408 +9319954675495203754314132567507992211027517777636008035159464490278748417535817989110579370702464528694578198640300697978306801 +4761479515872183180982458008106034482409223233605055754850233921649363735633967021409784641062684672780275455212963103609820201 +2564622328254102204752883685136476152747302286902963024211546476863845751873654900973840371931652957066788857238841473470464835 +0397866053032166080589875219701391296400993039583315649582052801128849941725263617220137826074241870851980225112051857130648469 +8173011294494728142605327204039773210726054522101869637370867240485919811250566145341338527471719086407653593120324797075262800 +9276563059829040598662749592867289249306923572562392127787551507493862417784430100714677869547866344355462003505196714057035937 +3281114518664069193538073304543737277406653201799960331850340753228936196208065150992985287938425817855366760648223669372651097 +2101840795886669078503855800666686104431608113029414404488344962534581805317909553125776693807861855300451192405993773968098583 +6961316934095858314386858284332606626453618369372169846037717301254036577518610498824984225850477019844523165578520651885370736 +5674574044356298030636615794164061879134353864364329335297710010416084563498871752753647964277918366926080825456181880254954413 +1539836063070409136278143569348758593204080100329208721008698517483741296128676983168524689365145284941105915339931290444395066 +5870435438526474510831874542069716476806976094126759545431524918894521691790318139213265279009865120135936482961615836310836321 +2764563926650270576320235999861005882574146665848294584476765322933617549606955812826398821817999980232276039438569048353472002 +4681622264311366983045853037646736891614380849223091345272419576321258005567715693518336948951455962926248349277346106557405718 +8956877856487148384136234859130353374642270367915456930125213227196134161996004529594088595345226945903651779962975528996861250 +8929969605933523152819203271238663371252277051363612117118809528465712440154903065164514629518743371367297353604095710696093004 +7157581534746462952501632250334837099070591121491848742704449306300549604064489388204805910975177833999231196480535164260229721 +0485019965651431731437912577838502867419408108382589212055345268265256532718527981331506279373063176342764821347791812231845829 +7209745175723014456091749043194092299024100594164185069424964912279120968460389124726898963864775282217153874785467678332297469 +6065725443439620053836394925815928161931638271242410865193688681337289422879252019985383178533022928491349413174307090957339832 +4700128875271375466912640968373319232662860319940601106545249660353269122104137983304514576260940899048020820371598690778056227 +7730285573414592054013879878236163258148114953801223831916249666510471398921760574945529246282567489263612670937049902168944246 +9130285817898813367964618316152272977147568779205131506356566544347370722993791917334237866531951886887339933938523558239665651 +6324176517408834364349803908520408107012550850560156082064698531696664057881361151779666150135225685603149038329558618588277296 +8952307375129199935262885586527056730554419885668513403462367686079193825971303716999017662160537275706617381132201851911575059 +9097939471326158030329763386527360778246814576714519406324120582675890884898234797792200042538307916074737348191079713801271431 +3543558395840050080153435933135880595590305720081325097084551423268599148459068228435603649060705649929053840511309887473304515 +9764838954490408335606543556024437767253488401288444187782685927977837265995938637882104106333802927203512151240662538609957779 +2886694819124240943375973107339251767858923004989669776842476259662336587537006376705925903350515688363082070599436191295014286 +4373652465925317661858189411340138062597385913063993168012584788091469995881494867908911062927314918797351706279530078724029778 +2064997451678388426343449295809472909654204980492286328038224332702407992526584281413262450071832225709127884717383554674100721 +7539736814358108443787011442956856478411159063433502735600777534624427911961386455371172130290891500709946519469882478498227746 +6726050228227536316385485897543745820479149980410361188284581130022635250330849613983713225572081803265842903422696226592604564 +7194667876267325769087238008916325523655281922437602110657822708321617107090571364990182422106391457560890252879023326941272951 +8084067244599983014933199534424787256031530896533024441147175887371345390886210891424383817784357384520863399170708018540336261 +6756491974545573085147283999623619067108143004190487704098038956692693217789059190015343978239812814528286370354978428552071154 +7449780712548692282752907313327411787266027754878897309843460775599392567392973717717866266635813723308899529146352109375925165 +4589899290541494280337702591008141258629373350544520604291218705671399173981123075630121451176103241759370092474254377359262199 +6674505740657078583031897307527013973936077370107324719073133704686647880878285508608449940595668407790459295575945823755660650 +4398342537751131171214988271811959189579782716426212830903406974442463464257365397952526980356187741927953055943122410685033921 +0094907389935032343867848721405008172219352354700721989014087040463745373039712419507660947640188922848825854691917677041514246 +1508958586684510982818262867790308592204381251380001158886369026776930843982561684403451614590888186936500388011892045824866501 +5793657116945434882250299286723935630635472763043771513002492162136250578168782159984593375232995341440779322489270838200480406 +1528263927376996749090311110890532617677357745138138771708962614857985706783193215918275341078100860497124422089882672715412665 +8047862045380193284671628316345513578527766132655289631199372572297400350940186548315099505461109159626804827405877434015762079 +4497649985437114106619275583585240709212568170308750996103273375973255643809512987167799126855604592582070720993954457629607433 +4115202807426887388976298359856110123402007311986746450130132490988863002251413157990023529650615874660044346489668889392287152 +6927862701563682169262409452072639909156413247108366389659864970583248090206725794860315733927719571533833830245401473668394282 +6642155549542044431836970672852504846515539364245490047433571110732970388069323446548403285359639096781185191635964711812010789 +3758505500262572544806847683378518452890545811878275350915244958015902281380798955282380990829046266914505428380626717794710699 +8543173740869008063671194651581188030729498726094049008714991031352383511909957936902571701241692397622133551499445586152966454 +8558201709766887626106558682087493174247508346751479027758498057382070135663052096489680200478789301589881335174285636803583368 +4021046412548985259286576563116974009718144433360698911802741759233868394233863822972297994024437477811572624976113883122932672 +5381918891212001167216621281602456221735226414991801854696667000136111863626404844152191631832767778925457352443210000412625595 +5545489652781686362831418324972755141214447870296324256972282335912548489681306938005662008761939913552064594595349128397481698 +1089524707879456280414931183450523776892076608328015438392554074880142678157014194242067164456688702421957600153913700978618426 +8305117650117364060258714900356625435051024551440455926273115470939757929013176898347131589373525848240533670222323888613399302 +8471548092796793462446437655496090111004563242834114582368336961549825665258690557364306703617109594457055331363141186121145408 +2838670852038846380823335210551520468349670239701607224866707358833084563155287946007354912983177147283657585469121005219622389 +5789402261399916318681167359698620197070246205566424633235135391148334480479925501535906551354569207810065959398048065712057919 +7261986944279592176600370192037825062903228830667505762121478097517284917101606104242575975523312739096999493662697366954853560 +9155879998783130488358931786153797296783212124287955652349632245457365996542148509944356865191456587886152913113892875578626974 +9514039241881963923397516312505696923391431643577404694192615351139994947842245479233801981061807552927418310904314043203688694 +4614887134561403862531573523675942563015366589167507205886559178959551755768826801165790017789960028672209846259757000436195062 +6712756378314975481834508159845487505561014170818191973740730433116802799842074666829329785272494733308089058087919052323067808 +4343294134478763284253795500389053885558601278822120237277266312668433120502431917455874080753529426382802888951317455320771924 +2376278290758786366787499337556544479743289945243171688237218372051181305516529407353834157305915645651032848895215696121925178 +3277461207520288197080048258823569914990769314412444872094153856107817656040287107340131396038173497845042121242427193089822818 +0292406985223320173089591545488895576256166382750650552107291481212988120961054843360076945546651513119822979777064017606102333 +4155585975612603215642148373485509667928447809166188682093526614287990594479841858693261264552895318696996233565563963262273017 +3435051958110889869116245172555488932493678104217964596210912258968214519860568648927109250277169274770553939366194092044056400 +5580589234840522431968399852060022338678890148720329696753155804394492049184529967776995091977565256891476404577309506725891693 +7335500310521157522656023934693183042792687707804385279531540473816692277059506917018062802407212114266734977379325447178574076 +1771613660964024539818471437377426571013833744798577093945387086328616540439686380471290881092187502982926867793090143460960995 +2854310614938011180157233734163443946405910506035197343508210254223120818772340019254546366421372519708012585990732220553479817 +1257049653581939929259306981138171825335308854774522129783756061113569283775667916315356572397505175426434698580681752134340583 +0335808023777690523056395860773496290527877864032944037336174082899748170635967157534100283308552217210895819668943595660390141 +0793041940236886441234025927232106929006722186357450970274155143635586525537995503752589941375247670685666954666533793877981889 +5778831921482018016340449172209749049865029154730502990831586948434948616688177219302113345681124510479338999307531273610240254 +0006855309162866716967524960032543880915882390415328777432560766307982993890466192334347172872288555702927609941028091228916549 +7852480060739012387300892561223523799609034777867042713542472004721462831725545348869852796577495340595446292276187383478785958 +7959977096587753530877434044450370650017598273016702141156049535064683872702373865885204993323178473693440316927580843350832264 +5711255434828429627433858416873855046302807982520306738509536092504233877047237492422270856933123651986965389822895956478482440 +6861826464075028721520622101929991628946998804166738863536077306024363695385475918930843524639833318710346078946645157384213109 +3481268732060383732986016495680958177849607099558634081654518008026716403239770966732475944721762791451888118885285069414233963 +8027947110287197529845522807009685886470194904262452476975443162530080660974564750582146126181908103247560999247950332233807322 +7124796294345362297022824568300955114164662928041497399401450052218863148267186902693402640989732333727359655944286809966607247 +6673207022195790654496166749745020173181219341922393697843282922354805033819231485054350962234180921218558256719281039871046137 +9465616068877625982160091084356867655695114307535279252535857794074660077470905649500203254845487633889884403918357791957962609 +5439735864176545493786682348080515776951063552894497267405045766639566000474520462761676673185451169867195127044329565448901474 +4077128683528317505947720920671307017612997849640361880751590889728513950754717072470243203246494346250198972938371059265537757 +1744350487793132663117639969909905883135140378094636348689423577493817809958662291757247314354678873465246383300244542531431911 +4940086036767442787655065360411221974038277063722727918083568388107313711402640446340277014461054034344740215045925518041320283 +6681118829208967331321877862087707476476072276521025702668676229506021840555589912885589031283416146794110309804988142636941186 +2405582757418961820523544833021078187732693088350227346014916968640935524755515218365262141583526578487828351593275991676198367 +2826944841325179084380122395615301242526065149184196022148551459509194360014728846066978997793718470140240650758184827065285092 +2993193263125920746329597500608467852384071869621346018318097029329594882972558366328973665134862355812606060309541457130668022 +4276324020092753413044519728088179483413580580809338027992824022071273342649903541028827269643668077697566268171224024282173079 +3298668219738409681902878367215423318085688425654045440812819269643490157963034537370434474347979142141704582114328144364074548 +3331207723416487218945222041751113899922244456772815194220564328760009807415143846017951460454941724943113249362066483407691380 +6952116171208702291147492954583173797003314720166450613568401114973391179929136073418264108322111138297086137222772204563421106 +7600975435069311874768177860666996344740802287521757586619300995824931951698677022696322801985871429773970218124919477206966901 +3780912628061324155929062282119787356103921261553217430966183645130255649596951891774470507549609157651291460007364039518971745 +3378164561008341242097804088752883493567850412617992500593780122329644848355808639540910546811625908951376798420135130564695711 +1010690664052506424047390590878204998037797666671722315517689809912733797153349697357764209138638827429140983606619452818262145 +9300376547277559089699722018353829113301128665766029981150643108015654465357713345760938255089553063389045170395343438086345129 +5388574617722177598048137212627225252667251794017646238607267836072085293351506292982031628145496634572585511875570432312724337 +2949102931084425964911707872142235444671368657598878661624977383342922503912907622387561510392479663939836372865380891249214191 +4125450850629924708967370485579795410196136664949644795471576226816969360966521409006096786481214505471016659590219454123795063 +1052876846080196429866636468392224678653280363636952881423630013978399548705549755495939234226751546533383899311389616571394812 +4742555980388054447437326569075723750771475897619661025765632486508214470920970384046022812610698865095434736181621465694089157 +8053089116717921661140443537567547341019450706571788438437303262785839761518407332728972316994708287799676864196511854612689905 +9729609228443111962917503361177873795233721041675657438816948255161968110343038443890530223545306466481706693430767571885086143 +5688308264781028505036247702821583514666470671292172916350280224873269657299593653174145005772915708578064978186214186153683267 +2204744465132776025498386899741825318473763441099667987231878067919021578886769657107832299974638025812458213339029056421384587 +2941505567992035598339903647769230684834003209355669548196807243023989038563917277881744820709582631259852618961087166384306292 +8258024223124059927265905619693379087206675705618331236606891852566067360854244728445479329673501981619440816785961582356678690 +6012067901366243313604096658402361049393912691496270752442176761357212025454418262374116671287518874273805796136109140975751742 +1773101052368269774971707329316287503544657412042811119480548137588711677240886794469483314479010931714923252443537409707971115 +6673163431438354320152064250744844641052080216051967752388682876816416999627859168211126178210239748498710723469312339914116142 +5806996877521195102255361656866436538966669931939141317697197556942791721788110932577145668299881351690980272915197702021298156 +3837290410389303517128116353347856259353484969554892124616182205192313287433755106122400179494690010200326668121477882253979202 +4517266820218197081941589722796747209204288657893693887244618207431797169277035145695561271253339157663193312460041285285239201 +9619778362364427885644889878069243475033064627573715740274290928496163757241181156603865800841856421952950186816149922823346976 +2573799739461267334017539893388809186322465134575561495883735717608135099991766507451755547789018617434096932254961072365198882 +5105319049917763260343536285268328841500875349287045804710455434303605043562842410645690374572514107603618866501621709959657187 +7151556358315357415841531703618215410583915203575998268810030381640117079961254114904804786046065507943854733690644791284850608 +8204760879007736798651157366669090166543821998311590636584404376075249452086719646757979910467231964531015916233682304625227803 +7147573127581292166990486442215222962358403851938571275150035153036061725108691622876773795056511233146161151053768550862751540 +6784559304983902886590174698622574195988266824149602039534916660889368814614010342767085921580402597291566274193343471685906410 +7880113249001482900851888199979393787162065292926067710104923584703156779327643300580458887132435255065678627456912854660490039 +1350749751135031413626228320025567623412283741939627729115634082906593503710519533248918652855496972010878915554543211009367225 +0004322455171197190670578109680045148889288778920452116958832375230435697820421502377733900228528866659062527967209953101213734 +2911494504322931801753348544419729543749078880072226980969166346711819931480214331433779640233877327672317049984114641749562870 +2396528513460618041259531318301991286281693049268735199694175803321673323893916691134629842475605991446026813330356086230228330 +8981164060702124227875449453939249569083280933393885800497807219622718057632147355043854391102180378987437281964171835261740700 +4207084959173122310540889440071390175078165835125695056930143838697501277302913507401666310496334451392319525169760616015349520 +1210081234448921940981378280200458404260945592669718748037205896059223823726981351910671794263375996276697272684781727338912271 +0029168415827572351727213122061796310780992187543596747141643738703113178475108090159020344233281790182332311601803329178285993 +3777296404075934053174216751857062118469565443797038517380817156541529651488369770373411767683949249420334108704951789531604654 +0677478972172417855596910438873967997191564526032435944553932413671467038312980120163881839182106666575810117448701361229089668 +2906070251543291085920125260549397785656115356661440571177943054793731483489419102892567240649669779614310761600563474068066656 +8852394653036692892038076417261650066148179764783605637155006560905389777668215992491194523484007340075319390767759607804494192 +5467138831557823428775541689197797741887498255851810682591799669249141593216801183262480761866825456541084229589715596536290776 +7751352765591319462216158866914539056239902442050723154683435064019845231969816008347645334022334350198037412040594819924724593 +3021915992015948439763471750840891829476672656242373621059864369709314486194930162083489346397306741461969474457136306652660884 +3810149798323432950565012468358334053474435740399651150333513841619654716521117976746459459286758520929133377210685994781661011 +4783431510440557379639204082385220913253340902177656244399207972984745234341006126872839573292250089207868222847639289722379753 +7370438575710728204326451494061012215276912157251877502908542289047363288154325283991522374002173371912625950441590135539908011 +3066350412780717315157416276543477507139338679594873856557881443518665559120844101586947952288770461228629756299270909156776058 +0225437513080408599402290104732039265518603222598728765215485627157659513804399230415740373980117223389491501554877516067224969 +6906447098674865317783282853291741419024882827886344308680586123738306172623618313185104401579759220967002703474556768483264969 +7905688354362418737693115373990150326675244455866285863285017259126406290824772728738190085605025791061658351412473817132182003 +0647939692176384124068167344966453433916156302209336971942526032217331539621915101096729732974008611758025821638418841239335586 +8207612011209373495228760846993905535758093646702045474203894639020884032055233069612162464709714659219943117947340234145019206 +6685510574753704984128082407027684573335104766797904438216667655851315902097650094638260466258758906563426639941626773728838182 +2859725751851113943873234212381842264933438665075493160334098268584252964692288511136721035397358821669241681360772178239556283 +5410817225831141636647896001039576920297438613631278187702231335198482845481821124632157811564428311298381077638390915142518664 +5603082383902365441423743060726658870177085955695406252400018823076710944733837738608757044351867174051742821082475300834328708 +8478410686281696423470828758415498795393164508886616098722313596122431017415306773664281061410625509783895380848928242909573156 +1195618788186485122683528679683670597289650244657656876407897279500115335276331331011520690820997358881740005917663903217514124 +1469313024861648921401247135353735958071127626605430535836717325791385228578496115248025350543355198105506645012545576867598396 +4272483837056013890177205566878534986867897273483516131338967706119399325076036529394304714161849476343886942682280685872295940 +6971223873405564611431588388181753597862284579604627783908270991876509853907693857394999066866716324481756490631331601939564088 +8692753456851589867469578350330459046916706027508903379244973723114291207510039578602992677933126396343784500936297228340222600 +3596764762533798756045077959160866048043299983905049607435425529900440769757414376919342785743646389458775443875643589233861301 +9939277922617044929198368291152778217422406940588205287822196961674791811253436361915620783243520085430696637802610907731339802 +9987990017965498772438500667174890865212624587022291536612477503056361052627799082687991851074848222861818981269823302761583997 +8147838382210431535807715103935094271794068054691587562868334683152631170314268604516246803968415953907592500669227061338962852 +5229815249263579089803694020966160885001383014770540554119854860763174611534378849807683267399405523325826175066050422465261245 +3718808810539902175097435162693814660278935973946526145133557384972950132995651178561319774914749271123710684959722970432994100 +9959643168956842935132972702456973644474462349184166563719137832114600377784944150813295076399096859538689536653676932352320454 +3562313742471254053603374767998733448940422800590796399248016870732131873285978468485675936836421845482343165519443520207502472 +6212070810123317420939860915076451492474105989853395883410582273920108061505858814970010182505273303507369510379631126623445635 +0255217936242316574157324758578917396184805700986741727460755136645033816635143639164644674079087020280816581372010788765004026 +8089847944137656769780273194285160566134507215012061013579883469551314461311271302478380193931275275686279445049118291696625976 +5717365181365376852569097535965171977412985242766246364289767242179344339616335652390309582781287529277190209039953388689399517 +7041440160561255381091312862643936475613988744385378977312543669992877553324112750381702041222192086102659343479767405018249915 +2110051683946677346707980171411807954606467103478530131043035978033107947826868347943468663705645386376022529329242653151632937 +3379574154524802337331393517730441276946707038997138141465007355281465799683250899916025487713716291942082244114027843952458642 +5030312724644064165890328712593254449923448331621966613833157539215136234997090424298061395610381815885285290455631522924868317 +4994969373293084869354149476773798201740005462072247291096910619119410509333790774262072570461057954016819195195682237016634823 +4014617100181465701775054953697719420102573860376968588430229723536957418028396409863878695628364581200214130204435051219695552 +9995923662150908682939564279380158287700441111607726222291472481672861788611478655050339588555615320984584521234687823576281254 +7451463346941792082220143976579354392136627715379752529027471716966012381538802459409604897867012386933986500209811720725405141 +5447700282385988297313347229401494009078606483625633850809103068410968238448493407439403040224936061985927544263475630989480459 +1276956495071860259959641422731621493130581830343261790000300846232499293476144299954615667025912420799180581171607386310558493 +5072567626180671481702538305209899197776259094898315797269647260887831166441294066368414302707370453117631015362578331461379694 +1152681127657219064376008724117880126055071670561685874051473152694654802783634954044854697360607744867225915714681210070391625 +4963921877303857727536312369203728666152509364270556408792999159107647346567121427484742607873371889230959069964077737186080579 +2539117036539210376394880318723799879677288248068497337514544989379323165067241670457629058952981263712005132132192204500173772 +6813760519782616773952896216661044250831393786872842763541388164566337619519250295645333112400558661044316307967715017816016933 +2537593940009216399181185307950727066601997003943463162691283637037387944868769661465346762713541328987499170590124382561225808 +2775885197315288545943935069058879566010060299863101752754477420864624440405321494564586899592105748495227288314078895511070581 +5073590254176183749859074321316849201765757369989203202482670238574028719679448337609628523768999643298161243528454637871645997 +4654524903507892912886995134469509913539767744116890388991007614902224668150391846827264198768560243067636303891131378158227338 +0324734826151939630626306117202466456571054839140771329647402994885021118469683547773949974151585727386291916309232652285623776 +1825578079957905035730446040937650014976367810409736691475746813209385827351129150863065044732028040265416217520597270250595073 +3855654215693196726130528392601924163490164258356468808028166928343554047073423362885338439044483545871577569335583661986547054 +7793570955613127244841652451443245937038624291178210631123294830620894480548406463322651814043971289378075631025886964612359732 +2535751039273171851827163526482017707846468207349048105786558968816006070754676314714316763865097547820424829616783998998577881 +2844480806501005182020568712353744447818200854387376634450572497810731969576672980314852204195217762355809650187750497828976590 +0779679209849300532787708183193857610170295918273464149221680774855800423860839661690361920242424552243626034568783063475060030 +0715723560396890811677256708000602036818903166537471742271498254161510038321801193648871563911124555662395779532361864351019488 +0796637275901473841659277764647010680246421160415288649143217531307967149087210418850695712273237810379507829287822942082290828 +2503374558287484037225872566037347531382781922322502069198289211129714070730191986360007963273941619249637438751324038507743444 +1257552500220289953409007738059857241268432142060119245938800646680659562080913958426385782354999183544917531229451796428251407 +0627725746927523912542272514587614535547975158991752064909670083277315643027595358743280831654266470167938798514468394097718636 +8318980485800264189777412075113224818829974167411737699158537404003402647667510002613635249834073904159437111364326870286724933 +8736390761693005617348059895960699070609999931133577397987040270060440645814471436005094800905267014783073740415604380803498206 +5542024002024944354928529036523594800851472969896531771028010643441490945234844042008320398276632902810470392426566175025658836 +7031032484519885828134561441713514585691824451116624144085217816423547864679125010423917860268671111391465300826171047260397179 +8220783551779338773320989765886440391559510492663544076020535514257589749352990988564034320388602959557187208585104844145026439 +8020438087951116022977373383495534674635129594766601601387483705748972280627013851396530590121458524210171753315286548207527820 +8153810125605918943918826453140421737748512940456044065444292520385926730447939082216173702077609971171912049413332448203544586 +0886669881112435179401237673978622874656264303143999122578352317897806033193993865036799045024203333380525337357889563568831448 +3748676570310585965679075167577833926365276411040601559021494474638574451420952356408036362315997810486031449429712897242856220 +0680179178809075888645900355703866665848623566100723748395628721560414660071214664781095082893581316152736759533950009932003975 +0830073511229116156443164078334884149650348116571315260656550335371524295132896582152314795929439939447827419494358066689706173 +3038785789686019127115381229380791436051546935860047229508729818282744657378118473627065099573727853891404083449592265655152693 +7387679714798835215773140865910355708661822160467627107526217765214393654304740207983760504979185513943991488603347106187504836 +4959345618457216694880885929572868404119594959297665383045694122211724921300914575522725438253647266552305439505601459584619177 +0467037747326795908961510775114924268777468775918327644966642534544042676564308146590029193764372898938024544197186375500357840 +8285890907673262212647988009259010379723054727978121814340184295658072413264615156697842474728575585169324914900743103535246719 +8911662589073033021628854642652750279152366994152376376433694906661951714454207190114956328995857009825646477784015810868978338 +6105850627169563438884040914539421660727931317710596144077279839188308966168911142486835009941100149861369756674171355191560490 +2993431983320455272922883788672722733898158280538086533775288120030297726075605253307507669239723828996651212708874872223213527 +2383871203918607138972250186349539349163142546547549565946027359258489597478748862408648058675393278207850385093331126182621573 +9149363763947487276724850656041160743708661373179229186384365572255499194358074217364047819850970764281787263423869876082053909 +5827019270739581666492975342346328261479975112685223225619643483040396563898673337995672909483700478627244101352870984979237732 +6998285824391825859392150613924432428270654497491969804735086552708299672203935039153979762171440238901122660133544046138761976 +9253330844439113156612382643124565174653829532487393981769888635312756386927331059248556801164857640315799609889660186200071275 +5612390497528583434894505132668347409216226770741805847223173711771542665456899372500390460954330204857624315915587829701045748 +3597132578062559495102590042788395347495253649400865738509995118091093608521019322954235222572297567318636643867386469580449507 +8425515407736939284693677162137557410259923224830338231554842919234034510500493129925331580967001474100531060588153614511993338 +9601991297057389436503665001505251866564553593237174176487901010113004032749058341666775216970685449376017846133214652240838962 +1863773381844384208184694104858797647539515137932214592371546918752008772143433088976967184397882651789039128762093118018510138 +8436667156247339295567342218063358078249911156583296510351786615409192064411950727914656163850098232887122260394676405120111886 +7683313388786537579802951656666063632278774842670516448032010207156251926218292168775800210222391153956472045994503212219850049 +8209641417161775663979384085179876713787424866821458112247475298894421880117371699371735285979992519666751909878379569083863705 +5145614759445599831570000861590726100099887859826097034388914355367729544959518374514940678332048394297378845981970826760413852 +8761767120217732947982706113384060618324856798940745761981931692821487049372786616858603289668185012412091827806837321112104047 +9255297129664160944098264261547065105677964568103165093339743646349513755899758152316922927563473869566225326724560456097296052 +8120768832977744822631836021474402079390526097477803448973026451821196612519143995594879311933463926655409414749552006489267940 +7017154772060236322937445448043347707331947096778317016923510498198624836901896034874919545286197554142622789268469355512048260 +4180409321511968885143370514200618650235742405287713769661977834572569077057120974054874981770237845015640851329981775003008191 +2674007147813616401003929214576628097932821870524475792725335810151140502429235053500438517449175945097953478389066531072308154 +2478433215760250770512406042154394331540911914042172939884164584979797404189940441889258451913067147217070836370940898306980314 +1416691061574950251933857581530784660502363969157027453652188159283670553356947339926944472421121743170291855577403109297001923 +9846364489252978228078702013876774947354134510165101268064021721322665802683013959144338916544348159574015107727356473797086745 +5487580365242793847184389294498037980764980441864399341257536426029899353090372478661527651346349015000812904186890490170512422 +0317001187750772629199900746610544979042670580491329322209673449299816557793150957519344382208574308527216206006597313873698350 +3641287214039996737900834675182665853779449596351604582635503134884649233644728626688852457677654231608768591360986911023898427 +6414868015450446904060895609231719309486232701910101120865147273052264604064433289410480856555673255517305129013544577242362891 +0524147637426415811762515415524550268148125432394280267631328443360400295790051752698236429135771292548014257090019134991791562 +9227410989021340348714125220582535723860559138846179511512191891037914244234245126229571603923532773311387103298341477228375501 +4041581080402587294355936437638145699274625343476210354812865278292813236740766242635378473283614144250925302804707242340663339 +0724867859827673965725683173043844289022888248735884487800477685080844896245015920731492673699852061747889861151283779392092379 +5909276888786182094219448158615991460321912732697015860009933234510620643391119454097956755043612428806923128306819132701862789 +5863973408297121204616907602368353757254965361693796331036727921299882051062544545347637276552415740930878434572627384251645258 +2609867181490019825132280383807303218611438271972259358234476006167125808789720811193408861105326321587061359193074883011503250 +2053550365140441314774770422273440770268402358845803831349391590570354084402287239858709867448417600167758148787965827714400727 +7156914133575985423380170082307013620095209686338890246264669241382877333719091759478776099336392938215841439510174135934515390 +4831194527648288858829733221197151800308714018772578642206404911045931696147282373784233544435117870290316107286565703640910861 +3300965303400428799995252612196866095687126018105212822125411581947708785019993474053195560005697252054488325120896486904066095 +5685043938431323336868482886553258817526871549549389321696880672458619872963868160503420041705844727261813948644337640127844475 +8437858633093280150489850618743229089133908480393577494682517449743840953324665700848231841040716763791338769325607594504384201 +5092632498177396936492730922026153200701191354311511401621705191001018224605697557158822145660532720002740219521243300566753090 +0884695364629948365817606370132571392316227842180869160712531089788912972870842630687441550199592940590099216041036498458331435 +7030125885795773170445439886698551870214797580800884149092936799091801845209138388276070168782087225547258102113578759536143915 +8807215865608701057987377248776702944861046260230342503936276411275965656745488818885694005475308409675094846381271540973613956 +6869008153507650455021791206721217199858946589909935757514740552973399919912521587008600090842850454930346920106313925959869436 +8747302342382237757458390343882591560184411401946674013753631245032512422678856746902092711874356365404625893572768808561925896 +1816616401614372016489584185015076832989812362352963164309054653174766477426767816672229374164734973950570074316503361323429424 +1677262729710288413972124464110274430459973064690910792449284824422312855723898793069294265613363135307936039462077553033385323 +0904814204574628728334178723557725337676246503641550973591704771295724843533583760990530372617065538876854581885105724368370399 +5318034405787225786821497050581659615205871549939335953839157390829611482749151979571742480565357809673956661177238715716708421 +5050974915162203984880371984109598616980412684284404767484126024659745317958728060034806278671739980663056141392010690752871879 +3910704359349884254016812391071406772666793577154924194799659346664635964157482006918898999240501149700799097400684650477119203 +0922138102426301243886965897610590765176893150415296968278489084200960754236095696787157337789651225430707548067049568646321905 +9034532106902339573322445743023271918597056331210862099117907113669633072759130199009133076195985791779283506809079562559701881 +8961339360352958868430789580169262538709387737102199957461921100064868143051486287683576990974108237901843716454745741895109283 +3895519300319620380905559168583475674843131617139726705719237185238119833221024548683898215318859806049104445070815116282206901 +3205107673082159654286800880087962054713784047147807335322961266542952563116519811400145837980005316237797804651103815235446781 +4645211086556795731679627686638624553276782440503123281227932461374131645478394881186841410205689089468850242938656791425398127 +8527652116361873059872046061450470630778597394637649835734537239971727675715042761592888819669786191505268650105293343814467234 +4729851960520632361339165114787166123924351946142914574237488059076517558856540707604936767815825051810582920267228276662907034 +4922524498676764577221603274496474839043188887553906964245432382389882944444308052516105844797654740378920402689234115734188908 +5209597697295900954976185802188459574638626316316754990028635919463918798002460967253179124238772322245122243876066379489508079 +4508289933039159505453489164861720999217265104842659882479905333119333431390108947303039851217725991104705191344280222412086293 +8069168568134508776661939124641765187854999589971548041638843098203486147656735371454412494419404765242018465434846607552127178 +2927086453284193065980223266739988186690361081906781869605569767794118257589865157784413584255650425147523732344110338440580815 +1748049647890769557692421227727549481557775151410048868814718985045151061237011017099100528551045245586497019590105694556854099 +5925541326979395577276086387515399363166724218914221343444459910602377753889752768375789640169881596578910501727353216027189265 +0670300499827706283025120061244353545380543254798985391943205942681540582832008582661334249048815794102101737939011541318568554 +1807401946172440506670841188965007674553832019157741404977051650612570825055419487071388288683499429125090172081236081328128795 +1574208400432062514192736694804153587958619698259830513687209529110862276181211516095950235354568516040903749851761677661305637 +4121149896620030430814848794433445919918770329319490730619096312332656236065824412598168467443676868836685315118978623516376785 +7498488621509709129976594981432727530944618723741457356588286547102533914121132990321805599546945869002131411100756554367181741 +2582026542886480869734858959495104201677107827620049653093381222272376273695750172303884972292405628020932588645381010082785621 +4289447606077369793749546092089501817143072386741950941157247080345113014329756464038358626070033871947884567651488044623905631 +2542825564877135509885226287724296817068865459213301536696178082552402072123707396192496897302967815948042943248544343979991813 +2226470997115317096784601264496380718265218871198752715837658374418501244345114451866417686910079224568758319235433015428196960 +8662720543840647608329817530425275416788291903848716280076563958924728751343578053506896388100895823567897044421928057610378475 +4719351914296817015503837199703117466548446025449281302558021903566930798786374684451211437854302542934198900108681526080958767 +3447640954611982803365820977016952430036387460526610055495899354375691830575753318086903113772624764685535267168082007345740305 +1797213012073375178436558221355605782112077809669165249433528557235690262339894762165301931035587761808175461223039297448962130 +6950060736132243209446984212914140861957832417030579738364918001660026811196800334571314378298274260059705895036596817108824926 +9243829747951735931284002210476722019445511064984030772651417194277223668272106134988724834976681779756839767862454568373306618 +4177035873387789691442645602142232402697759139202124087044601161733861189197322918438352811683833924407803890384828993570282474 +4594126055178872656922618774167034354795564094725497124745719061896909861368756961278327080697512762579612758334700763137442983 +5295668887669717485226390123467973887358507953479829153694623729169375830523050788966597377361980793027344718171019365784257200 +9888285370457729318303943315282858750583399490083047557333008218529891417381026015175475841289968239416942724745429537867737999 +3215943066766119309004524839024701086312759322446215895076113158100056596170634794503064752363481133827922180405978563689758589 +5959944414411467463305736849422386192090664596675188756343645002538240622043092367067855848705606619349233737685916244645398642 +3415921462522850660711019463447810831161516098186857649733922254277645246597288489872824421913456035668900907466154510857353878 +0820770271351282269897729773480908180216209552917101530859717823023591831191252679348064887785178954716044181664367902784712050 +4837078657362119194233933526772924288995857318513107274934487922952892071107573280347076419494020490239809987105400407903583211 +4566520950673320089105623215464822027247924240083971798563728505499902175676964769999078225165976694000667473401833118202304025 +6860738161875565205941822290237567447513259073024464364524976929966835518792473395436546988594137222229184152759954076791794830 +3322947265984158060992158624663708425488918876383348951122905125836522038089874259881383269124957101134232722838965307585611944 +2514815461210894795746476799700740431234580399243562016925036790171527515771508119826976806830254109945111480181093741053447775 +5733504011133442490826886677777865571712956442700084777358932735387221216646199198889278425501261656719619101527490023033629983 +6816820665025994462583833453804933866931009904653183983658207938144775757074429801900721418490709180940459786270732207357575723 +8952696642268346283355069787962809893365964014926493914452473794524489554221368619837559042723108457610361497745820672215991860 +4686726618791630599619015305428791046600757619220375372642195506961581791667069381025833026424470965152344317512976704970780861 +7817535100493702359961847958187008692676255354707895369219620793484182638707169828174557995935583658475438688352992259510602626 +7874029548817067666626239631331985390475404826024086339339243583071276348104332609325698630718922399423989995185805657290031135 +2250809137006081215712604683009748641626154723738138936917095298080941105465629041740614734952469019573555032467949707456686827 +9015780860040863502309304151201467772727168204445161990634325579174702838004535428730659838209979201561616665321137059277802195 +7650185512550554196643188069669262493680280748343975919639135449289335914799374871448334387269516977974949025899212288538454112 +8893662379603774306702720358700430713868475310661351513479439829552220384722225613968805831874918699875259504799262267715048953 +6970047041565543406988313458814942522252185061892588920654751003945616453031191382091583403673677641380943374412110595010134653 +9871101512999920825593895837672777862350486799080847951285270490204712290524251977042420133385216867854292673426124017430550114 +4761750275074356557669956909154179090706612051478635506091941757942400068895124161242203046266249256221284982009220935209300242 +8708015446206142670623126732884105024418284851295914904738375122444129503796268180002105711502782904456582492253779456800326918 +0613210293625239057781585677639979950255289284707390038337154693725678298193172844000279478330485593483147704265272812526895779 +2716164189514060813320272610067774089300292554132648786501937337701166054949053895064937132503546260829302791728809439780621266 +7313188607076954454240146509025347232689443993903699487707438769611328865209183398377855897661913914996316621615825549781446347 +1784361684093575880030474001414660619024654155783784777347697077774197795864389624206377564266630250281594010870492871026068899 +1515638387019036333906018359538519902111800466842577317066502746814101599618916383605825219038858550204877062732542814333739145 +9220084992150752811004335287492444508585899566926596972214201784424729857471199982968054884285888711269768545472118962801716609 +3225729665605184922610258057387753797602084282592399424014307116412082926636660405157448703031528431483170813038850013412917589 +0896837209572767267939653527113544043129466184693048843785638628896239995824306624383959725609177449846462355536652421863726297 +6350549459745687058464464835566280558244152696093628407065101518502946110700562358545869437041517797874386126356678480069546762 +7437438880206605630925397821472561939196877744334362718590301519068712375301433711985727209285202108430567240861306104506294438 +3997013218626793371442945794751768102167128014691257491799130532817890787884088430906840628465718832537193051422942089432586522 +5767261809159521411906210527732505137517003452232973525892796163942363843586179566469412536807588910846358095073973053136806664 +3610420406657475104037145580225980872688624964111041381549594329404718245302720059446156211318636072750134363504069278172819616 +6203445793416289171078116228949526537345367573758934511473904862922585007326728074024526181186924979509692268351022430367531902 +6396903617968213357545062076755915835538061764765669442173658896006627193712410945197598872950553456423579139305216599382954077 +0380250815827704962815549731957552434336266476872009617222288199268494650352751383734703683895685962764217150385921949138345511 +8687569627504118278687806727964146837523838460065586633962532325989849682060922414609471208083225295893531164710651416235932804 +7419608622045901350837303269139752477969857182975189849619797931283834755645259685017775129366698872709262322484867769237487261 +2446574648357299934768068561355356119075191099390351323266829442188920299959413380468532819783426950868690325166385302101461953 +4690378580819708064900475904899019106498140395743178471011674929320871266333196561704778715716963538368801024314408279647276339 +2089232113041890885410086998151297290297216269518414003124742833718798363559663862672468722436254498875698485622178498338429914 +6550115139019878137719854465346267455296956376622672210869101826618943385242099068657285404811000189142469975735597865828978322 +5550336328979484745931555695487878550858216320770924583332798110114879836414978841309407295358874518788123614542792919395667790 +0067101966073227418786203809533671075851186310058420930636457857938955159488144881158165188969895896949949174391923269110027161 +8258211496051765933329722240348370716824091139227581167499651455166693356343053246387421021603939871942981451611393932490380081 +1143232998448167618984488431281685110452032295577636699995232387576558530063365443650214541186195345336376561664260667081107636 +9153874053518331378425571758003432585952239206770799268110257888073536748287123822972824857968518689632999374391956653833541643 +5746454771428065810576182766708428737462219904665952012954717596322305804801076045993811560187169208752861306869132853087960769 +9312948167988600676884786283729313554222793557251794907465964089218336400060941052967060271657136677975942167674352571570304004 +6999898093303477247632176216594681728525645905467929567402604064471637964034911185817952376210008390273053006469300379255866531 +1000632388106711156678341302292429215991586500122697419661097904387224375829260375948304920442750157857268831027577641937349960 +0561600453741069995026478401094735925203677529242298315347820295756013844421038599646676315356655360880496025651231310731701615 +2967967299264819933654047107088552569694325550743639578846283279457367587777819171468331383302126179626852498959303153806267304 +0993137404276474214149260182488025202243237418254946278036864473980040065830274036040054866953015031416924556900973301250401325 +0611401987313671825312920644599670850377089684826838922442681491359233283306670402386279700142617583474055662080523345059224691 +5939710996631567268204735821745742750433473750474145824948890280584535509141301239485088599010310179272128880118496394992466934 +3357460688499765569727929892732936606129832020628130664357730146150594561279515636082352905831482949527411745350155774277195108 +7138561333287682465057643618887421608272303910896561253448290571853039520359195699641420904384787139962581486218527455895010235 +5098065276517497532201660511210544476729427289544804994012760004734407796257346283172974549503877915741488118551582294732680387 +9918245303994525211620190522775746453648980603544567886385199869862202445674868474533709002221515555801308081350907650459178667 +8721390287975913893819238889792260446828081274233551710328471981963516819510944667867602212793558775770710289206007250029862895 +1280952122455041043530594319244152931145497436315530258653194357643115496210565410757613267156777417895715618326341142914815577 +7155758483619829149138641241642134167637824757268284393191488835351550697663275036059641871170325362834558782380774852273848821 +9812670602301209189800121141901793479005817704022886493550881027819499284489381378125534965938777548562514868791510874667018247 +0362874203793328000664031717659796956361927785166994482222033051420000873310315385500469809294516134684467763731164514267312822 +5076794324739899424289073581881385172449093717540058394689713271226423655501988369216296397971568827556688651950460247532081671 +2211691279142831153117347297369818334578478434502829176759746633980926915557525626263924160377780019382684610945724739361356924 +9219339881449560885105533284707871200679261887421531149034514599874409542178946360778835736536874753087132328398630153344208176 +1738323078966650030040964282424869456011617229198020575179773319844636313591780039750411848469791355249483448877218482996481058 +7662725242856974627520011178425574682734942204257582618062074645522175607162830607624141450906516065750218377963070171888324843 +4118042284044277227029777215730052173836438075783336538165120621022353806541561734384066342123951846452650796265263415816226504 +9577454228253953925531431429190317095770965424733462598360193026659796995068726322946365945449373804244913417484873047389958047 +2530447003714965976379236094037411278014717964765934254550861704945379408174897820528178412885512242072953354276409893858269606 +8161920683332117194434729439905850512943519462231619259091383414069915670154194411541846032268055950920748136328515080957772524 +4455001394112823835403683958904433147118332238523201500644925421808301905477707785391327088921953707802405649821266420465223561 +6446522347006213132203935166470869874136295692811490433897687621265910913874552752126752166818595258011990560857727236113871905 +9408818174849694926607806886600067803753381428797382723099805698620415041534972953179055866017711870699147517868454045402246335 +4135626065428509052074741682443536368971147858952760160985560880547769122981012756994845114634562103177568847660082033374741355 +6629483302659977998713743227246471116209038004192175569114510152669193408163108052671223081581651955384894038833735367319970945 +2019914369819713476232512996482094786229436696697562766992595975532622307992117773104105916821954959122045124593739046172932273 +1444770368011659028126820519729159232409884803442212877208399135578736969682832974964729860145229172744181374244786183467104294 +5285602663449673777986234552062489192832103368544537902433053581936823853505740285888319749907423551259460167786803225430704894 +0644806832972865011089771228494426041526498427256197014809301589227000243312341210098122668991020924989347084902763173739593342 +8397927825150753855181618641584202156450292633924809550510911217351590362959737037094558684222129548773410325105794129798697578 +2130742130526719507540424131716191473755652175799405630787752600339151530661377691130274509280084673031861647779131553466473489 +8949445645797314751748458206320095870152672752275260151260265707878555413048057771903238190200758654160960860864191150393477626 +1150155207111417862229200794999414320488018094694431946879621916439109486610009948685634866137188097525670175207351289611083094 +3459879841467541763732336521769192414341573430800023957612803685733924880739274791648569413015334203212412056584388583105695911 +4207107909576145869607620085461426361544959409669769028641359260476393776996951928885197026584290166920423018039379739461412804 +2471437021597594937922441016847924078362096363319644062767483944790539470640965768192086646725793415577156194561524796709562342 +2281835051710898850388364318434644955789879751829726732802303436684019874264646806723409567549761608858039525219541663231620839 +2007932711287625313296243751903200023005400873291491556838807519342296983784742524733445763971687078693207166472588686547509634 +7628738900800911617227705711930133205924637233313615650647097515124961199263711780307927527262674727498690057241315682922136505 +0493387404859274797132212785618181333173806092520644500400240017059008198178246301124430115868753110589721037771703980382573162 +1169775643061410030855660308847307357939679557417322697395889694976054211267593984893969933792226845025198715121269754643254572 +1728088919686607770305974714811474045083911324960307426887087611368014116902540202262357011208475439646884185432232498389602471 +9413509858059972641349538923181108900150612516616095667778909942463828217847216087159443807284569471198894585206492715160193699 +6728843665008266563029199158801993529576549886997200057517865461162768723429256920925422142043184781462681238742660537092344889 +1931599731390297037090231505612731169969829636622229470099002019655957459951531223643763907113518395449915860761987492535032339 +5034107674301251502545019517381773020837552557746624579624919732008873147762835295630900229992866373514578020722517390727520774 +8983019600230774694685639778625463724553591351398381775880431832781553934906986888770789153951456721760712116772577734503027958 +0722579888130638672105073329597090462417149082738030373661949401575813793082783579270436569787902655097641040401368636813371427 +7854245354370422693429496181630052949425191073877308843275368739572310979470712820505147110988781661197587183674125970895995542 +9743873036934859621564820909920160132335721185173809848423866981847229831790016569684947679721801847602901060080188329396305906 +3397337816832712119749817143233767742905059889912076656163289731022283191890667300707504214144077245819520343407565716275839778 +1635431146179533618869624816029047896887367730042516939278216433734900869272992829571445669607086349418935403567352277456520703 +6762725385281999872315155129365300727718531348063401770341896591345777984146395511533572557918080490707700609701643325567678181 +4555558906784569913680220888302706502231120206554070127879841389783669163314482027219519085496782129580409038602612227824819543 +4625553964652973015688630098326028308371988997070002772661734916972323600534194087341555003249709049635157603584665553841867805 +5469615145572204179649517877754736243473051237336198742915870594247954290354530866927776592105619674425818013502199456960100742 +0688295929947456127268347042202884584629259939771429635850789571181349570944477662818026788804604769581716692572546910654441173 +1781053017263760670442837500622338443773255350157040419311002126378320391255428265011649731680948453211623587016944241093429272 +4424881884807343597741531983608658269276205760340198024603987236049280398955549067330598233254907076437393855435114528768763737 +6402747544261459973943024136936806092529416389402993987239813694282531255632557296852083229095761340348625572700195393957513287 +1528386320984169104784676953231416483588713316281109988780398093348714641247346320098277064757682809088330401558069087928359793 +4836620934876383780592087107373057072272745160875663949786761387509864704698529847777036861011698566433232612309834845364039380 +3575337583851040701388845815054721473930456788213430385395140774983959957647809720764167375524924858966015899435919948180069180 +3727628368963430480869218342563326331182798677393171005831460902561217359479597768311004931085102290184621530025879576836660947 +3475800984573188366693910563609274312878602294132115102801727644998770488363543862638723091471157432089891694987852322137790631 +5023683657353497036212732071088147337120821028934424904568945720202766911488162370473709710449329167955152043455364639983165681 +4313458074953427422966761496213154547288857367926863109897916806997328848306557928981283734566043834603735209469550380544603011 +4769534552591442528339142072463485535584209788041390319662187991947284630203143273451030277553655546597381665676996289780792291 +8029659403865541723176070298858179302484672898090770785814054293418165410123040143090326214914018331298053460618863938939353357 +2504331261837289138046426704519324433487258219679900506483638261532541113835978749947580235779347318410118894160450785891787503 +9154332931976583483472163920590162126815140207546760699499178902132160947370072538150136322051659957346811166868803166980109050 +0222142484013400331297859576897114613785020913334195683798252363342695111135735081039945217905782047137997143182303582893239120 +9813120425348259494442258981684713118897639132890990099359593568005985530954284388860261356040562648449500126432442951864251191 +9491055336614888264152467342246996843863880300943314768116172425467094897669780084992632583894436406804533915530813948074710378 +5125880262000217074108251922916601396846452955978383367381037097928649700928330600429463193168987527289334840125068384753709726 +3250963913544985283516074025079436289530032965000091704733601456641384410110473920750483277883697522865698072624602352586775278 +6909533273650494192857472271669762600062165210442469141500817398520087420757013742072500281461753556164642072928308813935348280 +4807129228896091173010215184046472764333315290810434138260279373142369497015166184160402673619706065463269358547161491925056454 +7992119117554165493880872181423830045252323577778753354174393490633929436643377607848377532573742197373252323683113537849603683 +8841806293434459100805456108210596607134914383853585818153512788887296784056237782465358856155449499271949927041469376892328564 +1632152697497520229603928059316997370226452543375632335002549957119486189260078571706186257203470953055703949967851861921520731 +5864396596718182058160283419308429964136934231008821382389697128204242562370820069167753381761875728312424003987892871207159646 +3666084774918882500462621483992172725824109862896378199142479129987559328773744933469602552905906375001745899273316469658254151 +3498338535962144862096688449703329334091149790999664223656029226119319000718124150843673811902580542556051470065598276315050434 +2588642538822153267060185463367170199110995449008021906678609426788891572594252587471982661988824671301251499893937578926745391 +9319707795744627065170409664872689583830132047201026242383522246999299133923376128562725383185342894837098008031828134646969110 +0671702811161136021518386108886822361915633677386996222331112492876474384896788653692739997207795179082317779078270838374606210 +0529680562308620614160670757652951824510508056181488035417621544956335930822186390269360882386498654576366806627786546389748223 +7121344007162987446407144751532917923911603613369296361423444081791345960568825786153422255467670330488573438324604236300940295 +7856126749900534423962976560155869500794418586760356971148877729423323229897743690930063998855496652306786264023420507384957613 +1115472217398806310578221635364703110916537667405172225846214402717552527160481181622222803507823937807179294058810167262993525 +7733396618045594337624445678237438829647882527257597412143681872681853625778555809127382419530057648275810262641644934924071181 +3613929384294435871056622853422519700112176582857461049491276578382237331260522744886532614099866550813707901946900039411643678 +2471863854966531804604197010815017888282961247506056635263741803813648407515388558830396273074055989597305977778886391310447068 +5772682018682680181055188634541777077934406094474845232952841473576584969574909042981622739932442934509311768673906919851347223 +1971820335809415226886329944736554280331033759806916931583970783266805147633904060778157532814909009520404718412464333929846412 +9304789467519446866413824868897777103238294214707967726874145548954743749977770022421296628566823077488711722686581183789601459 +4520829387352906480220193366391017100275344244933837568936172706668016811057327408759076674979685576296077953383462420493700931 +8246906431343919288012465266462141639350962646993227082304804964566869105156074039984736745415005176673660764795387467919402546 +0186162998111273052854513690953012281012706484313894676418003816742150384731546652072390520748548343581415404883693106949598503 +7407453409468893649012012693295080963235028162714227398127099455397823529445343521263605978197734068303737481555026299674237511 +1229395187707069269668822522188040767339600015117257223444650130799671208076697307072988709360546665590067811729152682754853792 +2543283984004058807502208094435749036903952553879602762556029245920852075326946498509084553554503835502118795067551626235164267 +2007797747581357604343336945727914338356814330722751353631792234077965818274628997502349441503414970786616526353391356822676384 +5194656346701670632084824377074289402109258239302810982753490912710811175011533673266509733689435108026608580234617657393376969 +9037146495591802460947583581948869619219502922722697707400434384802310554421740985009894719409552319891457897233615461251631720 +3990697080273078766162376909117528749470352764935216917028329174981312070290561402015804824976866738329336998564205666524939715 +7848352778978178394189920640395834604512765967920164240559275230242580756053219581598506048812354481467677378211973577424576951 +9532477816991813127692436491685156204938010748592475096875883131377908630365761647389532075843048673325944084338736570456096797 +6164242821944626959915759190590818947629953857895701779138738262444662644883838599728736964664029753263536337011632748825080378 +2817015241236739478928849304183816788634913242150862112550226668153792859950263630837585979285928908928017849179569989536100198 +3188299380080735588065637018955883708261980780287989654193640067585824757814983887808561472404025858752559638368589207857289458 +2148159553153805396206627740954877447246881212999958661604167091735503989349952300666940107138834649908131455364478740204253214 +5183812867943299697018327767670320460762394440257519747758147645570356514775758990004394658301628734229570022912960712413921130 +2264795742893091605616321707447640168594382931899898384410937459110317820561426704161424031904340033048159158399357351652777596 +2139572365125527551258839222311219599103367463992672950511023648604600231272790630354961828455031402437496547839035832950304233 +5563173043827945218120288721053385228256759107947287607765533263965688120561373605092351078219345874822210656978425318969319770 +8518350618323036588967937820117667974812517069373788130975907844737917546000454481446588184420492343516807727330574227463796535 +2208092079415381688598883619322045535433174373692220944523386140424053570720154062384759984606718110454232154813303857905183459 +0403764445312406400743177177292964855962834587085077838226697948324657642954983884229554291774482754430895940798928630071520949 +1218078030268093244894387180566099112644808636942463725970271185753041625812549630461585448271241111674252623627152709755765524 +4640249030477405125378434884747349008061139769305596841781606951188241867017869136729477063722445205619856063985162910427240398 +2909590245662898324814954125183873527735419113049232565629860534526664160220755933343864533662307471463902405211409478015729764 +1532024154051612895466466141175266647835257739963433595273778351297990606120134172421391748505385924656782899507982823237116066 +7625676430217706141880242765715903813773188383257526213002250997251341961521914541749167347954676286972265765550795698115431540 +3971204449523415473814450392924406717851351635913252062373246080376997499866289003353070967905385783507116089156113219681812229 +7080951492315882179871809720207741914174481310077398153033680591523295504167580131795931815413662606081970038126846874489797367 +2964040579217611595547760234190477290346436427835968877980338437548172752209833653855504469466390943293257324156532489625950388 +3972616808815375498057586682316341344883202169607183819718037286789995473261003288017415010063837288806976576286819527639032681 +1290138051725002391039102512231630818525813814320415691425261417707087230623051869853213472687397101016144678883680930245168423 +3850834662586016207855419987088023334696460550057253060411826174208076438814838201928582833181083830814307068201908029863175971 +7944749902703521228274612063687297856087416799604592533936372400167341391775276233906976252493497295192482610468181988345091916 +7011096175582468584236690812621406307216715768647084636075068364924266221366474623044001441060540006895424513815585241978347109 +9612362696533734373153334851655466254197093486211639205861389987966709302035551682330490724447416578042696870553662285651851441 +0111552534765233442760444257765948564305822466469203165903339169326584537556214779726091179725355707166736526121645211226756106 +5885039259881836616302187141071464709494663689251644989212209295897461744225634984400183422383926438027326803031066696585257111 +5396579069429715251643225942479087285188610492026082322649552644481241838275988175206718743351678720472794668564948833054357449 +8688233606386980591001497882755114729508367160997540301693192893291539036951478433163921320714186025327605130043325851253234451 +4872214673472278544834646890891722960548978190052377730643946484747055518801507106492936171927846554906068388153908514485343068 +7214840374326612339276352609419398397127314205763710059777054421447567554750695002090543962570373028396055745121721373269382936 +4524482436241914482911352925071095921316760701089039169544052169552093783128384931032814298463081577630793680822839743941613795 +4223734008339327317027315036935919017365387705335808468598641390301926191406305147914145568155445735877807426352454856646393111 +5101170944379204574558835956241764204898906557725918071785044288986731351021322301083120247570462572136590876553271594974477165 +1498954940338463049076496039744592188429182308195708965464178219513192666612550201124332096109168498851228361187923717360547190 +2838593784210615507907908324852279814008797652951109874718831612968151452611605913542110950712021617053098075818112497307624799 +9381906333865113269293243176861228537211612376105763917319466803899016867981408631123044887142147183129506705177986309824780722 +5409043531218283220619747469806558139995337403975277796517573718341286548125289716392847076083882092014379916668361163684338425 +1904893321014375227674131798347455361695847285890504332145250525132622615658888584205703776156043946440538442055020240373771823 +1351830991497076875181168528408533516757044944191964801761366476485666222599596030732870772285002472325833568217236712614883027 +7315021210616605619597435420913698876375528968662848736161937624720303296895982028335232063555260929924269983942028091369465040 +5072200871530059327653441988927547294080741487165355628851444397293608964873155978474929308924899776875776153220999350338874781 +7297806144047991652107617589673777407092234699017295323732250434070279346790076112443512292202647551871470787944370705490593514 +8338680194506251068070541345811382066184901866943035469419313397753521282182668152084983612781310002659403231835343007351253055 +6114581775690037701739519471149107309520083450078485755172388779153808947468636573364766942355736298359796132744647242525151199 +7574059840087355237287159427555431111919013708861023222908038566164641505519732443177083128309239177653941203870945389834998157 +0673474392377001399825892148070125779937209491690893896089896421739180565135864053866800087116893052683453216209088258021309774 +4693630023596081716394525302256706650097913048822663453433130852833930985337805085115332988734054335391426675730296735748012109 +1093024463171151726057935490277564245957695353097684106836395483499057575855912524860400186656839768087931489404973303617192884 +7635877951810419014091014411375539957398983529537012020519286943703105851379947162885419241335326379003488660213681674994537920 +7313912009934610962402221459721660669217906503605255214027757570091733853488694769091622366303142874602414314687218525998341547 +1566050559171006939210753283371737704974135991991141945660050193578931524036467433476181347487862613613591250793557188528520720 +8176429431061693107924500762483197651311460029641913001176379201100638867103309766025948024030323914130084770751860668764418979 +1729648696441840023063291425166270194908270401060650039064514332458826292034060316832564595611388754933869606578691497545240684 +5096948893204927421293649874213362565576773971352082850459718459918977951318599802521678656926223913450782713343578245294043312 +3086541714606171194181739777107223773075014101828171109460641650405365226657056099487143258905930679574237317845176027548390939 +7740302926757091096782313708390498469422515280866564744352174072524950077317896546008807496266670386420521998084434771204151893 +5027934064208470666653625072824590692528078583512194513874229573957257382460729248431045517338285948895199087965965424240384379 +4571860754401274665671127939865476298997613160622025616980368067727347277864838846767268485621173877779887690613158095362529635 +6944876714447267999239762443038959101083236381747476636643523496718859843462703424789350147757356429745707937022796547995361159 +1608685279655308172831370710312433300754386599666997983340912162248117716366440489547493562293368632280675661325251959965144367 +0470115155279779686716584895721905617913807392365128663764858517955975388392843450401077964641050043314321327171303982059553970 +6215103349524529478271694130462996515027440018395285605756916821272681672383831564950333676633547276622946473458222960324229716 +5752661076880540890733587009248458903926085507327954333035749203073156883894621881819872469950016600034383209674501094959670531 +1945586770541986321354980385963397070039734408942633942296911586196737659678600361467268366772660352728384921986880900512427348 +0224298460720490332231265716874929497392701347632450314627063512555464017409367105505507132766860559659364230297395631822230539 +1702023132625842749699356356536784362975613947059773629978196487288877402990298821674431326788769352305708774873068640207819256 +0837874454109632180336808361233189153201698197837194960277668995755988025271886650717010166150668262322432532297058508033324759 +3169311626099247379274799980132948823347592107280599635475818048919814215035606030291543579920124429871014029266938736200412702 +1914619565140979302991699155094170839054951976790499833713849411741065459327090513567799966465721776223514774008243762437394273 +0284830262288056137733605741424808121279641702537891966081045603173960192239888264018200262442793489277094386722616393425116590 +1173151387211465492751444105592198529015440250049757464822160661232074007786672709170841572180341360083386765961004042180282066 +2747834197332941363146926817441453515366505571978984487189489275517487274745916049268403158626553738640115381387510687487708488 +1958762131814281375452347210594453320648975262319481726099981862965271971390001963271654018154213822420613244796687794506233768 +5861590138913305751605474142329109238465204802867089486028021810232894377402829167243953763417451369079334074531476305390636648 +6931736509544684644323440543056340368361669695138186889701847677102848670844548402669049251722131793379128120867567738961962646 +6821092152578736586590175476051687813270318648206546383329391485549308897384882743702278026541948728683530021908873051239269012 +3127870261792191598343155115866595959890760429348459173942873213248004394755219777755565345896164898508858847846745938219558572 +4104690546998169355396414252792133064383123628337383872945199098541769741499506855287626343383400914652705990644014540006261236 +8792100596328961741396764802811919634888917999468524631681735602589600555328679349075778614192998476904702613310979642799638155 +6734793674993705010403616483433595873080508573310753327845752574399663353072797746592366100412052447212721125322772808600316385 +0099515723538668132596125963466202152817452490708599517233532239221132293841708379481396652887673374586403485713968074564071453 +0083805755888358130747915433250146084164753782259744459712819051842998158461581619972310157769421457011089633907439164606444507 +7652876608005273562462370933960187647117519619868786042595158425921249718553500241273244226393132978251127763667423654542879946 +4405249132813172389850479368504130884719811118887711374371841091799443512801595585892341132692059703325567321860246290919547167 +6941199045895501309914547487559727954258599982156757774973817213033640726217789962907551298312065690327505399614397188477788050 +5358645453073717607933338901589045055393757684759513577031387645637409945004898936885098068574782470685593557377991753572387018 +4706013177574137609818422036943485331576839280415982210652357962891328244924452137995064340517319656166353370990119146719861836 +6329505987385677903166233247184756000826411332850477352758018273420949926789280244169127178212529891658087198774279941207714390 +1511370436371213645604523617727839098695940567432172783593724006252095799407942743832278211803447800402891197805298593983426545 +3022464264863164058488714249339098231034997516428008392896433897781383885524218363385806736382055438093644340647396074293733782 +4501101178543767329620885826562169899606635544112652117620964836027885674519772391619599940633712271697963657935450955634525900 +6375010018855545330452050093482150077359764898798182405456683026765532905675606074372556178229924063905296358938093169081598198 +8483257277634207523851089755598501951207216079577367620523344599612671928857960638762403403726285770712060711385777082614045533 +8256451384331378443868986535492407706512110875647453641525322621598498933275339800741719049869908400644823204415096675208523961 +9398321546440720870569872262936542811668490033035472870984698676634686438176528523891161540254323161470512628073841436944062388 +7797071423711013392162710663609589623668119033549706301015274145920933018984092472146696838533855941884096071158160204252589379 +4261414209517939560365542448209082777049757262017603674203052224119028323101718025778556206926756191607192650438859403548542695 +1466289085237622991161102650836935979121797669268051582725264475273671975785294342068745493084363600896894177743687712147368123 +9181597431483593985916614200861015600827448233462428138664038851972627461175903260548893373194383862378492912625568875647273959 +9257826929421951643418775488431746425388390160227055490497812088985726596130954189607412047036836095076045019637105980229125135 +0051088814685847441269344262748461773674703937437029964258894662753422844953047676499113437643607083551852374126649110465539183 +9719703193787962011022241398606941751940065229976379345648357311141518661729448144028369831751739729767389915436519514302519528 +4754705205114290966030322546625592332320235853494013451121235653943274561055976709436804717429731588488742125670116982095506624 +7915307785043160782062481393173682663021526076353055172054644485732609943157735268019683657488875580064373381138013382280006724 +1348120551409342824081918999927191374394809490457882851229963796819461455853157081977254041113267197827298870478281259789531341 +3812065821388690082091959389908490155933727232741583927718485507099116748923648769090644447688804257458798099398350465758474495 +2937863476645119756329442458144939324030284617554341982278719802555921010666415523070911564773110585363083165465312366738827231 +9179756558387423308702990244249813252311594585173041207944901196092166608366163256489844497165091829615643175666074910233765563 +6459378049219057988171618077989915820678435345205197081776728544947416949166191536837274889454949606246062308000247423136050336 +9590694274127077084581072678020481871509177233838867384578103147792423645131300534630220077747270144036126808318115046320973761 +0633314308759908977900550446703882605141401119276147159777634200043370016079844602659915203435729306675829065339742126589258345 +1889317316284541412519941066954231638054475095114130395821846044209964998882207473650227876029465587838652109009674311273487678 +1491201614697119361909884499515016131285970573442170426537294317539659125369759905519191669796894139802704690179128614455530965 +7312991894729010255958350913145958890797364015827373553097639087944419042322472672141767845335817123220952073258598944857610720 +1824787410780276399243095677626477613009119292679764840577779945279669981000204638159164277233130499426714911295485793124165881 +3599221696151384019695918691913331345308102473844702422853474390767392532790500021180165318933884607342905862054572886894689533 +7797578535955366187957171752724494492272818965402286074464704517072955420112819202222670121573771410655986788904078893257371168 +3476992235872213742357632857624847275225481432286353014243838356580086497628026465199448029895990288276361657184717108337186384 +7666621551208709893696388991140973902647787737649797506777580191326933697405674502023865297304974748640809764705859758691017754 +6544825495307606554357924631329747091007811292348124154567821317868772060770803350068486092232254680993513246201748613742385991 +6866165375349816659160016479507285079045466715841081115993221601429358066993308321907376618487248492349626318569998572862307928 +2954124850369537534496609519161469912394832203382444510628904117251662671751490677012621979800464348066614680001698354593940135 +0193499508454379205949576005154484505543366786538069430200252986165917146991374529470556848495722371613024998552648030029385354 +6300091436093722524653066330745577837674427424963685925900231612998930766803747751089009018089480544176580489192755004987058784 +0521904612577648015929532704786690696377674858853171421019639025087849558650954768145265982042719739087464082587058116910545924 +2995811242507764873679857631100733041164546962708369112790103328792294311269786039292022283070742012101949352297665947227182231 +0598910805263795572231168128611351576632001399408367899641814748409013513401170040370795100905848171920487777572798457368620639 +5688904469919555388263250672582090539068694827337359228605791133091700320455439594271216798892203930514646320638691425342088585 +8515424974655688512862245643897078195523156507477939116050565619183311712594098054858317849757764852624936019665610738326843069 +5113156995533534391502169817952742278838176530218998980784387996273419179922080679783505603663683376521615832986618021979492127 +1086319824275441488370793880965889728722603318901376609034648244802721809098732997770252918945517263258021059460064891744542026 +1590099478966776519298307952043122519324124336024594288380180094489716784777893529727657815697554594607079223393500535902031225 +7357641282650721485620638312466056185406173433791953981763404688139736645500991900862738958018055032882772556895285743906215162 +1650489828204176805947705046005072475080530548765982426627534295084219207983407930870882583588364915870597364960371942080426209 +4961456796449042167841011430036294492463136865448894885627513655748299627165496853898722132001299392052298889154231937053221092 +9292679093929935825812492666003758036634836903177775163898869080980492856814299385978432904050294518519563887081873640579238706 +3011512315941368814678516363524198263828364895022582836616309022291899256400386653229758118495144379060943664767132561955347093 +7994582030787878507087359159314617931782658556072425334054967106060186618004693028010903789497198319676911594575116984344480865 +6353605238667338186351983657599555627480760430658384201616896420757729940462911589836581183482303716623193619087127242146945823 +5587627832512661518234488087236957525879895860979271923639746468639388295216331627000259398878042734326931204663332399636686573 +0672965511904852366825996694902980930309250905369947812120763277890579450585046143580711709629358309790006863408567461111578107 +7579999047873643551313574662014234072727914277127742448350969386406545430650595860664380236476144348769728322529154001617830780 +7768720905776990806878980911933213548469388055876623124525549575902760321680062870410042191450149173795942693780209583496274718 +7500581851674961002356261345542689400915034872242480502581980604450197467020488142845109390383270729947278076491900579496982299 +2129690101587171429651661009917402506767801016536968984813683892465052790569498753467876499730105980696506174974591421872344509 +7047018820490212289369541715584056487037206133357581281018603240803633328043751106808274944483601431153046581012102597333076902 +8327884098495349573228357110403653478827808473415101824363258200250596339218554185491862934594792362506926494486378909931564949 +1740056330174856891125669279294923651309763604203532765617783458152533228430452541997637109088581841891669514331609715059758968 +4197367157825328214419156751555885819747937310581379204615061923091469679845199014292435432070508700023932922581376409143095993 +2165125379005342133424590972755612895768392827051354709297231877589314385116008084334202246281649011825694669304551295469244979 +4509728839111272144692019658264673559690649474179262646699331625117318888277703635865926266976720285188867979085670193580891875 +7275477166128052446741208082801962510905074668975005769114065221711777393514237422266031434604625793424024653464475330291262873 +0817046411197027445752205700663441451626769878011224256767794721900542448009071309280703954949009199107668829370562168637408040 +9787763619368655981881242490879775693879040221802840519661369579710229328600940826654427779387311550329699789056384531195194998 +2723448594543172549888888596962814780076107260643457934204621584814413545214573904777806106803313154411323713368897701260407449 +8000951726014956095448794689858919573269714659606606617498924039765372767236447524498222062645212525371056330185170891625286898 +7400634580995643373473457408678165048789850829161271772842260046851336944770097590727744202976687517001530085639423877373364847 +6131960772116097152434381501203781193864171006504601138647645912125860654370952251962787439280635180850121164508129160794934976 +8194796768491845741394186667260815254416308027847649620467735109219757947751470402197659639068665099777922925932497275155739813 +5209664008657449547795035193830830788192397569777052795318568835532699855624203558168754541268446171707037486010417690857467487 +8377976554519991606329945190868959375772837211176688945495596448296939525690240337764633216663866800325009183616688088580000528 +5192787146614999641495067302066620692218333148024412938301229255333067720338121642154391246693289222550082295681213541896998006 +8136852204722481664059335120578115477689164853072624921786692464359828604653937695373728935390680120663294293493176103289690254 +1144435038869128719771735108732844672639943553994309593207501141582954078007058121278827020008994616209652065957537243296195065 +0970656298377136326223895516660489631269402730038663872646787518014244370067321464425302758563853474605729399708299081361238244 +0240779629525661769222180612134943092596901392802155389763359378856115803129803025504659400407739325825079102512268146609621569 +1920020849630289059227598433172661405103577932866917079329997842631426173495463282180424424558897910324932519322784595681717233 +1144193327801538649747431966846093543676528159861420323208804005782998653152702827377592130780075570401214786834816747288844424 +7672000694647648348909906701159130777349487715893771185782393615180543175659863092467698777377397102350383295634648499503885338 +0495630306491592179663093479474155305629241895069310776977867647781537028272341913619281785471238537339471770501056143359192572 +7308837601725546836619910084602280257935558569169920782961917685472285154488319168403367545336906857013748237755825054290532894 +9661814404701500878628706398803486793202614438164625925828662516099882010742067149392493627954571934045651513234013334682059852 +0312168977557161126723210896500911203207572745509229840888786198868413208045541779692684493190694384662556740715047630385587973 +8520266389063839477339561111091620930884162729262785462465146299021058533677470756448900131157092740484913244873616221850614852 +2366892600338849323238354679971377016007007696850190096980341739336594207986799037588279474367471064265784394163814443326245256 +4911325423638475961054407270474262985635705119625522856108931080012973303196967168739739944802380176010145279834846779349851937 +4257810389850148158544144932315012034020829622092429506782620637342284858172415547207765264086812199822149360687311593339577078 +9708659858477940738903030251204629213359548332123829955153191520032561522000846708640602917979387525987407975336034837147466054 +8086930623135121532860695594083516763569498334428501641151038532550501591735570621442476271723263173645629791905305955098548029 +2777352200022791503781001509067591047738954816120020121065054036180446105221458464420759433938919480637654297994424050557216733 +4557327979787779116307153599001198923403057470538783746410924085722270113118125208362788921890249447415394699853853075390237309 +4365884384429048241521760483369109759203884801440475321881130286288789565265074980716663859447668954490193155283240957032437631 +2400761044210689572007006009552492603834440745618264315468884643834376009957185817870038205575856137576307667033965516109840180 +9737040417279915834911486990827520959476936502177184234720809847197612305989565105760154340868284644203521456078055612096029018 +9781808678278846885397125652129297898882169803910679237997846224593685450165476495607070948013373905740079438009246883252169515 +7739852920294363425633218123401420673829295163516060432640021373043583962110762867984429100483394773437741380363822754539046324 +0883431115446791272406513984118239461183841794345923431236238292749873478934884995894017966307053473725524062094771916898837907 +6139386014396316197310075826416043562421564641768733346779156742528616231256212666391070181857228107399708720583287723130892055 +8564495834035892079724353995520557131026025067703760714350598180164418444716168502414005579666385014431317699567383147791141861 +2311406885413424305003701677451722846677377710768986204839545491722958560065098823523147478261771908820851989126482817082569115 +9602340226869859614696378992497033778548537442058541618817057710375353486743749797455027651809309416058045995383235110379798461 +9628709222162680949961060074815226201933096277151174310696525731658668144988286362519156716805236829848685383992849148064465103 +9735346812240729849346428396581006547963793149866858949294858741576251512791660305611651687839731531035722282235292660633788691 +9538042061855430905977346502656421504814621398394569363683719060626568625659708532699356988708790883640172621307887407813836364 +6305019984976325257337569429166547690694053229542307273781369534773313407694544352991644928363693848808898178142342306260087117 +1805704646504085604801764484506100460470712673185793035853367748681894021658904030806253312188697070560965442489042489792077571 +2490985236219600794455119570755743690829793064352980492197865586952595298856978623488801799239792633697972577551080842006688049 +4451839628325290929728145285465077829042998419941805433824084767085375358192064592570165051059371791323138480645291403839150350 +2979438318932849641805788421308077401361414307061802172300131888869924632094305534351210554801463479850767128364891176166829465 +8872921500436141106816138238492673770588860037153870977927285412564501357209059187194899313386454964167921543412282921398037659 +7636485131279033828460677166640282160440679145358607334279812434544494120552175807283800350790566307285400625724030248887429340 +4680751592769547019498010899871228144441828285042700998685644394131763248290312908027689367430340488843863114128754770298553903 +7371408142591796865710653973046215109255786437157352313924442478531290092820677548020730647005559727973069973416903159076053104 +0296204614919758433624657596498376245793058852298819735885655373134544548701918947319286620768915718576221199382936906689136966 +4514861015660728180490886079334753171354215683899059435960727947953401618302867137487819801076786142685334483700384896695954037 +2862199178730357056510866292501440152986864077701674864131708177930481209373944027435441136206079828523990989033623289788521958 +3232125855905003070647322986167904784829417211608519129888545490992306400854811324029764732798696566611533605039116218287211064 +3328048087467522304174357185558580322486932881453149442008314359000540253074863751401069345083176437332910631591438430165816043 +6086110405144344067589331082608930355326779125945673110136783179380891079747539440654925994890579005806127474614509336396435324 +7215091301714414443069868776064064504395207482808517829405383379414684365187710912963053526301972343897304816484149722894728342 +2436471129882505774580140273945136647748090634204279933544155848974105586771383680141377892142181597367201114241681187884944773 +2048644482475865853030326358622959819676030401734261133906361439604280224652350627125083350512410927294034793877892580606553735 +3602935472072437248748601211519488988169731855279575778400706577254567463123219123896535430970244921999769114868734160215986062 +7783657628040022836086246510234417653915945131795351860293548640058155931582229824232337815003665382949403984933860425849161499 +7472148054410673792713655531708222900140217409627089428063743914621052166172358563633841329724840401144427912925448192355476917 +0347053733535476550133949622008506766494989314867137093489295258193336447136204549399148001892481820212852414088927422900419709 +9639078047884835538046942482094804161605891002036742318046907668105442784580061293938476627159719249453272549683221805416505790 +1032786228200415283294994910054729118848671075845281886209463073179728081317785205722424406468440642562912784973899656483223098 +6968595178017412521208376863115552570593292656764798517030930445070380603195697501931025439581550142699705302804245618724490111 +5453654356413064906966652124099905267347821364144623020142756809037695527255942562866173971391249170311659304510623511853516154 +2732536262071170918681622064325048337837520111884271935643287214208402090379942906531363266640024631740298714867728868944222573 +3147285446615157276563228822132897878376378847350594831988142529113309891139340761458000809351957235529902167556281922497506368 +4354478919601617063477169153215969873898414180925342349186710519974948744199229908589593712396666250201617868945303106857533139 +6882511013607261756622150246671069745871933132400620002505947164316958006187665439907383822926483113411891198332966708317599278 +5134964407461253261959522854559873663187547708050982034442691200555341283933873911454057728258698109359806239057894517156699836 +6450666103279081547887551782344908272320062944069880747023544207556937435206656509659651679103507702121341392065845820245893429 +0369780371537429488949966165797604230927289652620569227359744404503741546226260737989821878745670653730192856868459952515531054 +3093345869807819730548713734544643718538694047498494322996726007960107180939172720629651622711868833028333458719243526925076960 +7989599203565850572942150442657633965818968052176208211383992661670496993990824695668976291478797155039216428981397014999174383 +1307119242405531123217610464790254600437026788611070015681427406612197589802556879143899518155467294982590879340815458735590885 +7771734336463859132744468610337712969586326511610847632572701236574882685603506834926412287387742247547804989759024383729486919 +5271411219228882208040644078382620627789689028134461694434130394862996168758457553833289759810275955726724366779012750280404286 +9512730467284947060387240979546511277358650894416265360099607348235188428440048072383761268322342324845297508884228837212297345 +5520192598997086640709924048681358015575115024163991652287513881247554854286841200564378110057343645081513230448973403055902132 +7020240592700662201858752560247475343913953666111892278775820432145603140381127320826667055090149454135269196723432849201394471 +5046712344141591199667738249797803701007407397593500840656021101416075518278507896267944040576213675962509419570331142339223528 +5009173314040221242742971167778009936788547416346651832329001334847054317513290947080798012432976916063888435394522306337209156 +1366848096908460688950147484431870925661107449748979391978321496249695501280906214711588783547789400280700153426360634008817145 +5032320677216082097266187853807984797656213614647275198101803139086954569491022740666386321968921335192817122277500443048900363 +5004785469025981206660221297903847696398366951138307001168655587727211921402639926973173862035209940308951165885978641411045411 +8785398824958308479147867520302086574154227898640917802025901098713415060910391722736844682709559806414137007346900318504025652 +4829177039724412998798470463159506613910620057768809973408365008034259886542242550978070824664073713620021235802691404413458167 +9261226904113620784725763761828029554734562287674270893824453948307773514334280989553881807221303459863642843519335273228474128 +3311573735232406552245381370523373424363177967581228254334269537766809380288366330679697723673023553333593300509406940840178257 +5850353980111617800710390835159343131583882096987317262767588967118575624615788759557354550955443154249258491730584290096574346 +6058742867661098727133777961247323273097355895095662383648934730004254372357455111922489913009492093823561131792526688980983778 +7068373836217667990225856363415691473206022105532884987579074277226993152918608006384495892970483440333181267244554192632887695 +0375929236714973683664267906774009903162875936131255110025871545553310661098253000439303508717945000859600253447780364023263921 +3589236909622208939924919255987333064134666511205037102476749734554331712415072770445515704120917930961110839990243914307170158 +6795584768746356073781978623124211251870230688353778395309754364333319536742360245765209901147398777468379230149142523181377411 +2729948487677410474357762288343380948554393380782338431844676175646578459942584025062978984890983296713845183696219799180782039 +8853156389861615819589926505138052323380550759364020828050387019448769426662074170464533329827565869171293278882331249180195752 +5354617268287092363055229579924827016859348547054043349609066878817933385065188678139799271936447212380584467570750380867131343 +6669574812003056132820086160683395897628895296098843871784268115803999250621428898234509785779912888254113882189729913124138323 +1094710833263258542617692112795417986473641110466966207723302828445199198995397058382901854029835381782441946019335577334721714 +7084654187809403875061887873785967514836739155665927380077234732823032340675248478794617424267309726934964316167994411232825813 +8586810770368457480021270214945101098418804122645991758117744041940927633232985858835902473466474854825024480648895385708401183 +5596898921066046808429836093228050083092077217562084369964360104846761170405526779538921555848456229448715495096371173722462344 +5660195590159993019077789825004164832246820538076625410586718868028600352830449932992980717485232030870587444549082470943172077 +0821935416600837395349048851468072663127356309332941980901438793730151587795962214031501434310617749617534269164881521735791175 +3042072934705160703532059769445721283937901499649463824595439982424043872605434635393216517511777007087089174560510963232009367 +5464155662396168936608488040261163576038165196682279135320764015816698209508887367606848299565138365679605265583581996112669276 +6753095562768549798586933346781184629087517744898743057935941129179204236737611864578907720852163937289062427955626269555922129 +5716541079272109791401178359438691238957049044014297693164836859127158377764717929820749372762244199830728156908190474636597721 +6048348633690063166546832299456572409599753235001536610146572139811087134501881675589119526672410909520126024302026020371798099 +1171674402450497762501699459068107617561003253876215131387056282446344211385618131395030296408469586922564210025675216461987907 +9312371022806876440175927558224158477356755411023006311819103124252221341547172708634888791607917037276394431911343613744399883 +0922159956723896116809200304280546821592880397796462717485456950002852867129487923589665229233372270334774238944990617682919310 +7658709241655408916654408381196074058493866541161875193933692025333882777349795393761970679827330490202099389041851596493234215 +9234545178442642967566968436599668141535683259923279950462781030512766827071494181783393026832191674347556257505316419663487762 +0041257342453564273568929735796453624713683399848511617479340816490608890963355058355974856413150349382759095918170362098854394 +9592779304960316507865578967321355607760068698329045954497816675023927602619347677723250166350520531047186145206536424990428337 +4299316161376269795506896486995922307501710625151467203920818965364151628354223037516185750241653836563366411298238504451597789 +8841671421275735676647633262528681899530392131503705721695449652045887039670882496194478043622621695748874619557992717633215233 +2073059696519439089739646800449388322820261958926389800326850308700850275001529873409140035288149114816334962577827586268363961 +2490360047432436143344657376283381577226551875079136121637392911221073093866166045906272906769849919522908370281254710635409730 +4974848754591262059988003622570276863287575164608170178580822963208162146400835022943896925242423440936618793540028102479004702 +4372126595413379264526984770633204109208673880518625238463206225086878596008443858616399507133771134539070683384873925072702058 +0639171273649854719051709551224607779261350414555233764709993649110326667200717789312129165461963429321431418833445603440663689 +7186962822361873750181210266660607397813719262382253059434948056476254707273143420389570046116876618519383113357898774377253891 +4319529017033432347952439153798524883464591210553274851928717915148566352954737293867412627976883192261494139098284868083014649 +2622939111808623590774562258070588172127827495395694013730379591966886930134915200669202327099882207686441261515920355425526638 +1200973651071533307294108492132851108586204275141332635739794068695424575790972361088170979623763207458716935295604008671317983 +1251374850775260697268191392326213755565839271682628582402356668458562455145193885654924842502488021956636839087993338831884363 +2092679910172237700017761223090447686346621171455515944807634834063707579619074178681232780539980362324148874096831135359068248 +7059799025589703414119220067452626516006331839767724497907056535882707433149950178268364082722639297792594500281095862497786970 +4542905544332920101592075397986385645511891063856851014878633174032148021231620025882083670673312205077519194573640381350698919 +1312255205389657370322617251904525722465432117955235178041221031184821368449334584954069933722375723026079372281725777733909489 +8940234530803425309074547064952811642951856360280155612932727691967562499759113316867977300723413954039644096858319695187621995 +1667713304364140592650059312268785120415368020836414159057787321290823207568631669529330852196553357371986318924246623685192637 +0422089184234642438098080557484372830069143118691288742408958852833389685002956551956492581208496774233386094905265339874099483 +1677835356907666147956557529262732608196197501168021350247232245111279150809917571509855702652690905689682229523319549019678024 +3645155615476272052329623583943381582821256380614894465459876556622453836730749021655688760168012160726745987877981868221817319 +5647755640891329597098392215119820185325320967799221680909508299218493251878104661039386657485694867555315346235424508828545406 +2249500944046395955203873004372279572553883458895334843433140571043474669708074473381457065865063499709405786900325552248426583 +1230511579171467602223112925195512272958191569060831817009891427720423564097069405239471201694564617244826682098552152177059273 +8469866721000791525531110152439789939013289745066720962189402891838096512969001059723536174778699525713458075138245336864075519 +1296905270266717168150361331577154511349585629938472384230294812334459038517468323643620870384471376627488513753063864443527971 +8484849516117452587091720711551100043539596219785764133452403530020825880462282298393651618450654255243582938022557870602084123 +1944353914349184905366261486479354722961723711488366658700425467290309892874206605011693831609609440448499484127167958439545979 +1904342525350793098590501390656028964534129323617049186915074724786886755773131382892183343708636631327982725884104881408443775 +4047731221209068512782993439144246721190758070966515666060478086425710216870076434550244885851035049595368742260028400195490338 +3375747312957348876639595293489234210411377963219380216708693519698209655124852272292375830483071612875799792380567345928238534 +5110079232697543396393866568868109799067673989898575725771625847005572590499345721422620239447760726870678880204127450730489419 +1892883230935369730591390745517921975081616548082333381141791553938700817410468926449238904528671037503779324573001732065035519 +8395077376884962568002904176016991329810132526312963614723727626088091850321489438757392596606168596638103913778878129196298132 +3938927636631738411085232552592623008752473475775738216360990000223836611551924314564820835426069886741001153829625505334993487 +1299692449183332553204542348863840181665273246414104121624684776356638436277872632132123576236177751436546351690643540205371844 +9194356489958293805818668234623245496942584158879649161401045859846964460358471563904833418667460716546730472435403875780613403 +9275256836090272445757731146730052254532930338266683064726986679677894399506235508548851649563290194895324537517696553037056936 +6401315045565945782629076626066813595555038611734944119511046358367624104666208828448443470577765348785145599426526721185590575 +0586738484454531185743029468713911469676776854865676690022624209621140076731906638884334409090253652724218159918793965233464039 +3171209020852465810305033445747086308509991604961680580404820907479024353374887015602705890939142087700418451581794807689385348 +9424311150932643701645157338987499029528852262438813754400403069738023146915551968014972578206454343730343122265559473474927849 +7742146609950233423816671939328483772283214892322537597846916524424589673152890736557038920458731659652032921748569241119859976 +6687533180189782962288035952098447575892236672807319984532899523336137857362773056256776453970744401322852572058367357860586750 +5112973080188345114036362904396465766939815911981737447433859679102909113460170044475876180448426521815838110280974934802651453 +0141366660566071877771992446357332649851216791119528638672163150044856967323023760152081614645879961910911227845879654515023475 +2214123760185353539707121031098580866690882823675580600794715047520486504684525675655152270550399135886572417008928998130462848 +9223973656232181173590003292639341133004977532862200821270596930208889426180465089396056658959981954090935260888619248916061940 +6623578698318166136431412779326115216613003933902509874343492190835346614403255698238178964137991654202739789058288364219841871 +8291158969189181550110191401002122993337707939239499805961356987899937471416091612952104089354586248344316163672726189677077009 +2569911973118347742318591298716009672334442600647034815935927622454202539592391699029450660978546827587095337413939180709898411 +0899648100113650188435064875802395140965508050973328163902047496526339571954451436627138016940395531683176282436292339394573885 +3218116539223354243511329358754444736017570099304122222814440304223163469524526595019625005310578387024223128529473465323727623 +2903847495538482030544840551895363794476928236443136637630260470246965452066473119820100467512281726986329810864283236603867046 +4226586385557184936270267709088518321173404628666811633771213904835009960571786348833009556180773685058946093743512399598902672 +8536593915600232712348175092402812063615932808932457150254760365196790780020038688785606653890320304533826035350207715944668272 +7207307332779658122899268750088169215451053148485733154400660830345046074837509653982716901490942833776007996597381089382249055 +7354364139715042807616100729619187431380023428907586266528433249618278407719620456619422594117403277614144426686677487237152240 +7463017222778211045789643242204755641148330540930800826901784405753146374489626927527265867849180494980902054821139633747003818 +5151230162674659769071431499839596583910959703956345024479063722159584629356540852497161935430481786349464090482420412307760810 +9834488101241849496636150547202745300929538510953947644624667132590235877398283748992791388091916279125999482122365178401663101 +3799117737061812716778711014651789301773605485731280987694315010080752800132070053513995645594283497896886041897544784262244652 +7787706667573783951864085352707088626343810856713494836743287556709150843482692271254078127357988781374166954894723733919964351 +3850833609175684557920955809259004150766406752693874551692602542748508158101511651908521148570349768720243116655926752800025156 +0542333256075861408967459343438031423694963841751597298639419078331767590705140401244001636510150856151990464720632445051520472 +6303430699933496407768275797041897577889994294810383735293345544688537126863233465001327982318529841153751728223029693637728347 +5397568768462052741831355049132795323985421528118062025164276988946171985642590654143451292287062379578161283874235728279748519 +4640901822963272018953105436900070403241616145463711907565248533620946580204196365483196723416387548557874992063706490486368853 +2187665586968675889547896116950223441017696539693810802819756256217188102291391477467930773393197219210849036472348714939348493 +0530870138541801269405772003173391922440292694411582576482977240988452402062182324365937231322664832700142793000599915699446377 +3232764179605713808662558293727950897506959582972158149863534091415959234408922619772945504672047855130269189456164558749700864 +7976387712997851037838832161690498586021679479533477214488890351913481087239877288703362839723676811186594991567854176580725967 +8767096970476797579593159059506720944428573124512821132135870916546309651522551842044412513318859523946351494542237948488449896 +0718064204159475781113170590472579807866358541733298234646266805452325502205676982790448617382183104529620072495546016035798369 +6656888096932816475747398102488120987337320254949271905969490909227960204913812078261626642629009104843808070570261399892364887 +7197928827518406319220242665874355187232953000643123084932578105682719648228431872207900564999025910692470162420939628457406240 +9390054304943829673541677221189814900027963471043254648597886086990795714249446234111674340418457357710907778877224567169903636 +3047190706971324195888140547475741620358462920833528017528636158028581644462796976018091635090361132164631234849610385434473088 +1299169151001233913629789222426928990506746164121889744635941525381099603593944992448532482489564134843216370953945058018816372 +8022886634541463283701288620853613352158026264893049477075544241214340204041747957155846007535087712499320675089565208063975842 +8518974453812680488184297781261482966404248280615326837256707090751389480480211355208009414536208955423929547041219391948623885 +3604467721690594055924832819820324481035373705830034807313911053016213471052470202123999030910736617997566930544442854033269439 +0783963633104292458209399376532444272248160361826344666316224873723641667255667675127680227315908535163742986352235015659049712 +4004639628899564002311837850986616952858773518487977784208052701702476955042845648511721265262737849283604073030798668836277183 +0862408653011082563803132060254754932254909982790619138767168477417959797920610077885997837256974272446346326081237570982197007 +0940886136952306043500899870090754863364443344376526643707265847999796477272121932881155662360934605500536289044543909420287159 +9325468416880604404675394196786101658805312863903480479928341990160539724867416343519113232899201923506345096672360143197907320 +7602604379899247751883206653536848520843218205588950289513511227638017895858870330990465204694386615676242643330003217066315632 +0851199846447079085502271034855714656668178101657497639831079366789502032474686387227843449343312220724903806454747746782689424 +4098848471040002241791388735422021963275960354775258076923928679254165040693087908221190070756034164623860222301010088075103056 +3055192757629057465255631284293545235726639464473675957498678493846951810733281325747646636144880693592596312740548434768830767 +6731474905682791005984276464714844713588718904361218261812425686803684106123652431919970410078795392818111582997268429659349585 +7924410389518202685905806688713577803625532077879713115752972677622209346192681858566234259911076164694414416704969551453021886 +9355683562980634479815919874568756218379958083872036318959815690363034752884522901587704723069489938561066700856799286608658620 +0543220462343036193244858851552821480362000689940188453022451557344919396300863983139599553360452940548134852258624017234867371 +1067014527356580183151961289434647684360706506035600010429135039073802230973345888131418619960777100132646812026883383699827970 +5359069383361932605007503288578185157851793814788353694181498218990573049785293883804780055838358139459377587968253742572072043 +4573719136273381835268729540845328454994954362222395612112318303745157807658681724375200294326161779060096534923740884933625708 +3514458846264455850674231783280205705825729759163794635971944702176098831501306476593168602881301267771422854514181550204581778 +2814589926430879599930014687698610766303495462811227979670379853246555977897078815634814137435467174275073493314846601851573261 +9109412328394243903990091864894855454386078755925003658916610663445020722891205775398782367407839761338086985543481077581229638 +1491723826525165284311575346657279958318121038102570973778417404071686643040506005133105712555305338320966176072017676784923629 +5488218241739200538202629320385686355121297434909655261539604247574676965715827475716615737941607238002608903015929093309168488 +6222350309549145806267049598652344478433516262239158892817022906333942210072994901250459132730572231739319182532518293889630019 +8044072077285353283240535057997955717161542654506209603492744952552847242899140500411096612225095709028876598587942929972397072 +1885954490526928165806858149219735696470308645981279734404025197278823077339850867123073835557226495747843398970587652389656636 +3636990744440216604699149114584326945110057025978680402356462092308935861145193409488632679598241716104734129398885084544095857 +6069902893386332364062130016921517943943574898468298687528180638591233629526536463888804022514538460226447545474670300837773440 +0715242925233223925302043184620521073165032928562060926764419538572558168218946544243657541925010495652673050247406203961490742 +4163377094941935791929068796922863289449570870715728363127254502097952823099544701660693419650850191571787642058768071578191335 +2362149372830570291861457450525257727016337864275693314681112878356465000993320675510813633779009823896594571255519867010367760 +9335339409871128425332395511671431500329529218909754194982323897857550540930935004728114503428724192561034251245345901549129065 +0756506273973719264925319448914314432556471353593665242663682345436151888535592338801660197397976918922142540903105362128344221 +7296939654818049488767067190718755957947538340790260392001857532393622868997477129188922872359726386124323878376837617352814907 +4078579999111217939011699307760285871218819333972182125286903838312055662520500719903656687016759241817857273468272683917688574 +8726333004719198815434960721386959090224338905967680668744954339106442434132414526246207631805974218944345895924595868424575128 +8809968439523768418749746186037611280882890683322020074536396621763557542013569567593054212148112921735580176883820120783267348 +2537069038616920940158973428724068585048795501013293376681740631186566951229973336427931288891824421015980577224523633941036996 +7974808176187279818828936860482106588007126950454089943602952411737485839990023098877207814888492767150658782003308086338815157 +1588659669715002646102364063331711646691061797936643322785359527528662208821134749050337274899095692843563066296876088924734707 +4549130202526685872258479111021810868210332005793914145311147798812593113617843971880716197778981371491177307945521311901222788 +8560297570244477829694687977784621455987068942424983136944870380427057052887779787702603030649524780263375021654142024115901877 +2048774313776039429808088171712835186320571466269472744364892701622816406146475324290364012640387008030349061922998372137187474 +1958983930518040048400877463606239970518078952389442564587925608445408847664043055091202257080066087803944336882062127350978604 +4065348283423540496042896789726757274478123900698244046915478972359449728574613924262102889566496871444268574144572205450579978 +3629438124450409423602990222911509881985430704078927765904795404045938118161221397917848075421341126252805098426191320837834521 +8085534923914835313209677375813349364458599796275600866316693139878003025545882010477316042716780306226946838431532163667037127 +9898915134568321964545103764074604849670802062543079353666823915979509779221620608508897413582424170944308180311897168957048017 +5385323115399849956058889239701492340513864492761797303099763383900112373871513361204005145800948160917012448658958645510687464 +8637322858113048873422361029279103210946558131854412009103898341572774795544194370639320402689943405658836056138282160542993169 +3301482949893831756487465487869018898293856072780727019945397657149862423574816057140762598392679733606145316506298182753363157 +1989885295563430148607778298379580655148124775805236404809749482897588166275331919278290911157248463196182871139898936725797368 +8850340804158989499571102548588275214111587736824329752538481445797164191835592201791780416442465525643066854658921528683230761 +1421812723062059588410998478367550671697961940333154251595442093624642651562126033425248558504164225223871598921143098979968917 +2477995193068866106116650832975646638319811948342255800290956070421894468168681224145709091760495793451121243995417375669474096 +3150729646482352886032550446062928871539222609285098660402450856512727724053427921573737445175821849100475781418189174380995632 +7046750620615596575236877490285020288321290904270312430965440858488622217217317102253600290127052465317624607416177498383288106 +9945695686299042380678429305385240697039293151977430358157956385467140053019250720885367675692664936064594234820657654969335744 +5571751022714308898152825948152037875574841245733361004625842517428626308101356793475497881525020804091668118665745094943275801 +8608762448666074656196662829963227928222015907465782465504699762597437156318531429585635263586424089541092855892902609967867982 +1978685830753176969518395838040278058667840236176435782603960672407003909222439483538760352906908927302768494238355233209217253 +7074071920319953536604510843365153876170171633349491568232092272150542222706006804946809770825099582766752656052960983718114235 +3280496115751440895543554193849398007467023324006497516116534251153591572725745514291959829061315518724947165745450390160259001 +0601737046591989613120616719919874653529282948250689745032606820130735453268070027901333295387220734852357272804293331117523997 +6136958737937467562116926228263651346592589988767235293060122410789132148757897567424381946719333849272080001742385628150689944 +1891595701639675010992493605507619576155702842803896050077803395482631144748035073236453571286662875633494080865014296782405059 +6994138350962374748911276067098522195781999897499082597900135054301540799702368264182765273926228557006482725202228009397664985 +7788023609935244035749201882811852725625300846793646185552122933557882704452555648205100150340311907075711931570895497802594450 +2197653103798752067286172607092220822596982226247423576487125070944191009560709583893589326623076734376750057424185701225493266 +8863132327252007877822761700057804325184406486939470443703245734365105772001424789573894937476178130538608010471355446757855177 +8174190110749058109791874932401725786307915411703003043709908238217181652029602262116068758446055873309569311805547464496642142 +2490958747068677929992898459199669839140241121204680850335549077174567238373970308977898462513690749136086901700597984427253536 +5982357462083434284507903055152940486985159078049952746161034961672224995628192794860546561549979398991180455860037140923248338 +8926976775587697462174304166383030496921336111150196335640267063653243971405939737115496867091474614902122111713955464436671112 +2328015592752422584686538755738383160041067711192111211555671470860520931401458590260603642848665758765427324349701633338120285 +3238367054704111120178059574962944110115171398341904274585267575082605150464279484097073060481753901121878225454945359042570553 +3450237761306126967240618521993384597368444622149529677366929462149782561575513795963740446321127465963106959992420775690369312 +3448814725588587245567368751607825271329254275986408132599484784341832975302652140521158378926626072460485474580935278488412601 +9150780220021887436734602033408383688852530542191765389359753381850912455739194414678686576674855611507802645590418679170889751 +4835292344245385826099978982021385208338079311915794842181613685383986799333650292340449601221579759524765161463912861431586003 +8364091375971775721571844301438463624297900164446658688640976105663184901553656708363187917609334998277021278755741801258289094 +7084708048206234641953289421943297010794421803966656094223814335341165874897678668246105857901156960186097878571917343164253048 +1580091826012825586670694920435572709977363754508123881333362672498821977048126176623956391025928031884064853593246953559407401 +1343143393297933839473552543351834063051545756389274017156011730389906161525821950360013682370064447544881706475110217230969461 +3302514538150761859786668968377489515133804300482090273478961905590177116881654275768644498073647264106408418864420104945624333 +1455642109436975199690456185370628722356663838058282076416293074142932077562024273500214654758558303856335007961702080005015078 +8063031532965823241531821249811544926127012157501395220835632529512891602224510304171587094059291180949555426978545235398053558 +6792060710713434110654251175014016166953632439786838514140951524356192499726146571336067500056531834668571621898017480292338282 +1437695510093265077864080367192602710341444418961408220405302548941539890702354912153612054318858614633735009858764482449040861 +5181414865532338468286437374705876616670683310417100729198121447458573292151625305538384979133781114543503822301014850960361423 +0442509889959385737920714766510641471461284309092021412176656794047214218226892073311469169814612693044014585717564971417728218 +2325595356223658506960436649084028083363095352384800259697237546709925440654926883086116058532378827751785516901620186132035077 +5272017869427934924377297497212161665902064352062568107159660976294283182349751217848859566575424801083760720335160297859673425 +4927647302447341405487506286250768152116103802235113601574433911840768325925117428685709060725568588745725091307806563183578704 +6009173829226300709392490503960716312095637543299322573027471284752881663572097511651811613598957587604124624018835545870558611 +9874256128717490827444943646188644499417848153072863603454092200052312279880710501340245614763095671533551439274986414901802322 +0712116664532492284027602515056442554560965687688409213710573265303359796914199163349296608736358741384041138558244053035969858 +9693983191087651982342027390681525334777714170455444776207288727295047097906322701667199571113890318007616548536163726817679168 +6706523778360875477183129443881169868743369990013775836046887154518569535610947164990958856446973388802060554267647855673512269 +5862057420569669693068391429181291253874208771601116624815603880238130385298427887023332027652265710135551985724073597417285974 +1151313997638402328677281725415458206631525714244607205307139347412188214659490398369065819541976622293080801350270699351750349 +5489131825943276548129703872359273308252153537674522058679810049913479398153870812359217912596783251244700635734540612732369203 +3810123181289945953628771451906051867522552261199661987069529884844103683969181491185606510174695973406830793843872394354042162 +3346242314263710422512316433607417431578705396179309838772547308651613774189871178285455805352015354652802479496815910335754476 +5032059944587174601615131788722847272253927881072336845872496308794095355330608135957953660775615970673613290442946086962710905 +3407385896995114869865346803277241560730078573870063366755416777507927736959074993460049742600768481558296737228998922109024099 +0049052520096863687692070927701978011226142548782375715516453178031755305702747877694502859869129537906539333534520649523091710 +0991259574063512483397592194908683392249793361680299501959035992654866098958575700878621835330177239453007223378603834381079687 +1103177507554459538813745204571902700244177530864339036101223775180048578597548931452670546251696214644617674818880606435247986 +4019388020763636749355438098141001952281422670022244785922521428313256185545192914769702955966048643204981030086298329088086717 +1549239391810219717095199987772454895177623386763902046734720379092060908409042895882464384442634362486868518995850543330943745 +5263274887172899674152253042181056688293450964020128773142784973127071891058343458622006778652971493625047746423062870250661196 +2492300927820520335243072396524887229850551524757145264574209619185242101186100663595852135831547431534587842715149723823788454 +0239821705984933412196203741296576026949577895980132509651253316722691651055934018399445089497754941402505430183658650264000569 +4454480470480799484331207273048645656468167612178274351303856622687362159020726843169180682079344623827123515453025370828135970 +6142817374030811769491227966611452974299687874240073880283247099440633899922286466254504368749938286010203764066551001971357963 +9169371099648374900400474139097217260717487967105154156622253602791903705738661081630850020115390238371457432815879031763979788 +5560591117813511863922620504148281644660212448276865905578433181502848473767100143776780656617049797235772123812344362645447507 +7125601127543091335948203843174929841142906765885076202058923049770681372235738838189576737300366872775686393117885907972185950 +3813261893105186097817193647097680347436595910522013775725325978428043833139417789267610377229028363642103442485430761044994912 +3166100516390908023193075423476442023657276364366869002050485177701831320339505415669888608102030204859429175627661908828955978 +9494583112215459385851845969134725676775320276319447511947728695272882901489693777569547667119292451041697329224102769821139225 +7691777515996491255498611230620011375291757353871825879292337748953866037150788019954259501731583992956846734646612549716437169 +4674403391449814677760084246730522440833433795684947826674440906657230316614951017230444639014782979009506122819959837960185292 +0124355369267161692186629219979251945207193338015531285271750882020294586168274943975716404048435047057151424920598922068881892 +4259039872889979839195103377532319383985569925201669042943584292754218542022884377759029683915124413393419307275904131879991019 +1735669223208908684334277224397343396336961848404316449603849894027168642444303310792840158007116435560970914275701222063897926 +4461905512399641675513869110628312166872040099906116555152852234330620387342830937229572832948858700069818699059571737043133655 +2317566091257832110561031578973018527337285656514910017567896208598685109912802746001639928928505435442403312983994465299665281 +7100315234971800925514182498331922330287575769352512401970636582041663941571732261396082519132641394516031353060487765740093218 +9334108345710950934819716799864030596011645738530063495673802842261253827926601866855793893356025796840470669373855061371630452 +9938632519142602121728449903028585427271086908360215670337654867014624403654411514574585082225609074457979619906343109374814363 +8543685485409092161128411354085800792705928451605723409112677460751012429082851977989992688721655128865875431794910678786635771 +7286092726668793850700794820169050814700948490519583438078010079869324333131287831724247406778611721612540484173599614481822185 +4574626770701467258525092815137010178678255624898097602082268698327798870150981485054197815103745515005378748184630266101601233 +6996566956298539914158437280054873330111337840249130739283399579411421525420812145476940206933841167306808654076518530796691101 +8418268977140865314494741099167445020028244920345037125932530888607409097769183652295273473400544354104335924214164055029523702 +8653179301531871439574603212631671179611383475494609355467328013854637990353997237551580336715104250041179882078593885288177498 +6757096834181706624090949791934584516326051126444323415129234296822035888300651545387936037901447054120865724936805272897353272 +3719471070090385271298581540906250911443020297189549719410313905300350268160333497598330190662838247612158675798074496367653467 +7420546839654007142424912212795950734732168508855699265941182387494625335487354878640173067010282119575715050167662820992299427 +8999971488927790060730948508800281979058687995045352068109470869864835768781397747295222955822196732634803342790733054201967597 +6576480323890189237090109618880316708942469888066595108911237671288781962755118036472198419023002661197238821372958077703913188 +0030541257153278168215715641021711922462243607936192442241352641161941478602829260934238679472069149540374325351015851641339748 +2308535064234691761213175923993413124441933619616103905759452431159366446936203305785164404138919753229165645626046227853031827 +9274311478528593074600453989280243110143811549871941292371158642466286143077576823957560727090752083042241440289345317446749500 +1173344584450748636805732688311608479601983149113524409946668094326431976475189882181347248492781101482302481266734427266149348 +6919145353409181506683417908661000530570384104910516382895650835980412723990294313264588477866084060873540703108364930450237118 +5325748279790493246685409745464007302387808804042056122442284885720787803880628218787879564860434934892913815793329423983574457 +6453232382761168189611119093517182812167138336965744983593436983776900497001902946217406040709180061277827817549237791463409689 +5084186690867471312419665514325307095801242831316185457562663155798586037028408988187703062546161289787145296742552136882463426 +8870611874224684584815641540909217473850820482485529973916085066540438294865034479488663791703681082228740535939513197426568394 +3829342886981207956995705990631766515300196466846957375041259104048073176049856454632700433183856168388096468289412653196954791 +5561500767524576355366009997865394697845244227185393733933754620619491997202491015533470742012384145261633852618565246889880329 +6777844984653507188325486982839438152297369628419739121210603276755667853986365800165693953505799574103492924940759456778294070 +2214788746856145315547663790110177531050862742765653751073730521273798413515040644084618990342819820992718598911810035213540952 +8641245540354058813605493935866324134857506571449379897541736929598327494870227097960497570120553215541706501704428439721082223 +1700972466803583171420254007427289345043866532653318330426834471075577492940413011600530531542381075895357213715112384747031546 +4123673880740060770118806009141779021375877044026069684883815887385967786493701519196377144556351001736897028481481645941513408 +2662387772790420942409426074953672031980544055539694698764834339968798663047095391766181294233657281432203121874861027827377503 +3000685337077147654086746625753896686864534986031835702363775205640514651662788785826457436508900020141827996232430281971022030 +3024908258360292259670809665500668877816383415506517420287409766113445399746700536527732408780456759980633419485517682977905050 +7609884167715679957397855272558744514159873182477148797463658401514781751802711120892880095270644173320069885204433134067923423 +0014100455158046473463596055055311019866188559065450226001282429179589867255611231640796556419411721523548855378235369309657647 +9633011209397874287442661801560684815176053929256216322999478244794557838852723961384875065741787167826618848105863793521919749 +5428219888294008773249094303237486065991730028127620139643719301859855449270217745813940272539972852306476328220561773494939103 +4521537632739898925531910531504909038824924540851609471635849282948417316063275786486909546139512668787750779344864961591530650 +0285764732059028849862258838048464137495797174589639632732738487543884739338580280553036435526582504329979148309470441850901993 +3193744331770081476790157034764887938574991726583896870321256839758584043852505285610942913104942834651377543523173599555843275 +7417116084257040522072018694176431604709041564144995596676144624737792695410277079523480490262409478745371723464489814999444924 +0087200564277539488709914626916914001557170500699327915642055990350944184767970585140183066326636283502929299531353689075738930 +9142572663076266551791245195307340496553949814370587843903642543965453293671993338189766065010527351493298615638788367806472270 +3051117521339185890745759762693550854894691691802102995243849634880012010573871449566457109934687770743470466412945533888196288 +4158491602681925772695910708467090530134341332595220736564785569416215771589193350272179369165509097131151416084829009479740145 +1103853287929082360008599627015884781258703830958532256506076724299192350494313751000293453841630584783094561339455205657099017 +6669316988454995197629701325242327637911706551201433838323166461015137492273818007712193923739461699066479249555708713191227011 +2113634008934694195638896909191311796132499125975665209093925839375449400491235035672664100786917876149509780668632265414075514 +0126792384563810279878665722622429577480527747103593555729429055584348852183674740137123973052149872227487163624179929217676754 +0429035865573402523285948804441336965668748085575713483346016033116457998360922288296258888389027618101688101669309692510329301 +4327425318090553450075452219201170395108911828576562899226660479453870050953529030012653173886753094327406771671224243820895023 +5520772008782057192477714645125967892355088889936328957302124838926954775059515639556003228522011635026617680256545865946919398 +5091701805891827972817338370300267783615596474978619035655959815481776653782092336443749908777349150397039742672328322075757599 +5790089502501043722549854203882262801131152082075103693079735744546403353282880501020065196803852275180952587214373981006637291 +9796322418193084084142426874342342820014220995329829353218013607479946651207097461896332568716316883980489998209799290426511423 +2380307550005537574650358548246683232060877572246000949898947779365886734692122861965160733025000170093436288569747823376036955 +1082935962455046112099818942036953428151667543776795511944294244788427450015296441783391903107987172541886534905100146388519657 +8380489382633120509090625034073608336127658262734309778765867742476489991527857117594934853404329609845740341120593238746572358 +0633586457978329098387174811695256037738146638558272816343120362030421967763476640034051908578551893496813666553354240880945358 +5373098216766144042601726925416970539214195693276289721208185447267076741060153916852700809475592729479267755151190925441394762 +0437558873199426030384531618231327018631366219163826316141114912440580402838580312314548478509711368795320884000345769917178052 +5639423930316613774685180001977671844817204570499797609653987640525926925309564084656003072725353558374445587925969239833796829 +1210135977251946489323174272650887900867086890978166723808871820517325894679293066521051888521481265231417131690626664853728495 +0539462898410401286175566033379657439604282033567012883413399478842279156974842259796901368805265470365140797961756617874554244 +8867613639089241484274514628778139575529407010929548231466056716290533770512410612299882134379199476168691871070113322417352240 +4833451141363445314543208606007459133355986460223413209331799936622495058516670427933156376684533870032838911569984108849259357 +4227813092685468088212208364841572112496680733745196711345105782294865361331232982776705201607924522572931046426758698077037314 +3298920484699256928312414816543062270280813072616261084031990462638788100585936907797010649601422888227353971911450080527103430 +8677297821634129486433214400771371293863980023792460946229497228182272173859417196509074761854532992353908622744248599410546426 +6781620363930103389596792660725581404654123168036180688431615736861252151699433810032668453228299140054818608750957825124796595 +8122906022184441989524946520995601380863784338127264956306250128205898953341684941467474429559908562531894365353100792345598520 +0411240004527716081891095406341655736648076045216518268298111043258024354531159970495009766409513979584877788837540685928510096 +8110610731150417715423694936324187409013994345563727775716872413402896219029394300366269945656385788061342489896665541029423618 +3755150141442065122724114186286854845339699980415816422951602029278923264626041281480497555316143381278936597056953084064028022 +9997198501283363781790158682287440631540353151583792163912187892594225150451760679251039984200332891341466144376776153588743411 +8117278660244194155818259161232321467141273951036898758016163015261776236723767428479024530348209694127274271155128147667928805 +0936167378563389999940015556970429817708525142859666613441735283201399969921725036498088504412772933143791841603758820099231969 +1224164983307664154773003073878449221074064106296765689183370554165942272977254560731754387603794592725273703193699452227243648 +0945223186347621257574665017745770814803043072264123465865881332861490934432664674134678310921895127370062396231037904388566491 +5578757540264948131008953288936928142006135647005490612913881156326258717861846071301620618495929699330750590715333343381570370 +8766152138213724166896984751062466183413369758646701425025924527217037621942282338912065834670044626031344011383552551288896568 +3053487311706357675549287442389024758844523549667853410498690516184480612259764698839374235801509026206625335813997095967736808 +0878702145089270046099454388880300081151816870001049594773880813882330822460954086402671799804568587855965385027526964053573154 +4675804956846633737319204649690805027179329770063541964923158992410097311744090492573918964775287855196910884200372242948299414 +9493162547793763389979831614732404846665474684277822658912090008789913594349007683276852208398902139951370774691434150042517692 +7345730422542157756414350209374237744157462270676494668123673800976571405254566784526649041229590418069453350314530822807850416 +1964598394787617389494252382959463826235528571672258679974212194662236832005305369294264438079751870505403383373187745093885686 +8249944446042573821740194113388700642890510755384482816288425486723007601353010761537384611960083168099219424928983664383056396 +3337126174819661239061831213486837993026986769690178323635308622147431003839607372786324521326222541843059384926020501381139876 +3550125336344588588098791695947139914704682212688259894589141433972450950443922835504195392482891358587151935390940488550833886 +7749712101826086175922145824388719830543614893730681609524662863877518177721578349626789661635402530736054829364302745002369020 +7052231140791673822400576065079175225485411488088090330405642998541294857834801823806307475957421894499261138427284899379872160 +1843292586638145520787104685467083303977045950130084986120058743550949902430669077350548793220932862849879063881005931348774482 +1487754184031131275500703273842524528318488976791651801275003631033587218395976230647938697685183895874331282612016119932243681 +9443865463893265996646234621765924400556069796282223873874118161895663217586723720163197972926787252465189277090874867985305943 +0897354453697180183636362963351299526862015366834373289147517835099724224465741319067052046603751698502499127219361448464402391 +5704747041899601180624074915530540633028597706644514489974845238123048510465569828429534100611326273262355319658327363672387706 +8316996307878784269659645906887598359322978350217418291053466278096200407791776549885681670136754711514051366460206315185892069 +4722848610616728633370187439001656108169740264355894513804107426599369076853462061213905264840303368831514494760256744683488217 +3692367810726295806823047818393633700597950772879297341267237845287738595298116096169937443486203130496785989216239853131904645 +2667811751880900529395751251780403233472004508704751605995052039171718893677809009851286227285227934697321953881347551922850785 +6909280644240056221753293916337855667825910341176418724960272141306897770589964342155201798900696411923242239316269753447770119 +0999650808847959816984860421127802079006195715319055574680057716511026426618826912583374854994252722086849395905477181959336308 +6682428165643537701794115175085151859549462586484758294772142985146427091765603916714315410145159899524436580396471513525615313 +0287601495196877948543109041275994870460190783889596253917385102642407519701386785758190101150573237633840832279432894588540853 +8953039640516871936098610139590123074456039528834700886251621001651688622097083336798736244961699802438533721144359429291546962 +9090712409265567652916908423800558494486073728012853735882650836864723820428807816418262790577395429269519877994493798687893365 +1660175388130474812877157640023025667491901812382385258466335889928876299301453437354017152844077297406456989761054934994390342 +9934657654239690864626478281461559020855371564678749001531999572350068313050489634121119934441378970500982348402296361517516343 +6359086489575862410458280767790738382982543594459454924194612177893193423779790100018577922756855011303081967830570557786235066 +9627300155446988370785806212844429529453154177930814144914555470252140016234923123986998575098139584073958387735640106379021856 +1431322355214385540867355391544675004031897330982334142717560574603867105320387323557316017902469957357722433291210791176477230 +2894506203039713875024969465668820869708991702780944843854568441944017095189870471544531770235169544173059692732034123691090895 +9985087463071783533894010510378579436608410386134175083499511116916575290748570942191993555978994826375337547024070760036740444 +6261306132531269467443124232293674590069389935930085698373191334535132795190967480627467212037239181698443751352939224782894445 +2120942222786996279924477488386308186164427794112389492451650285444010405583566731254920895180572589153915684428013418667038463 +1430800636064677716455907326084896393465425867531959255704442690767435730899236888138775592453451717734356391997983296892276722 +6245887023168571979343600695324609549057431178812999538431044619282851448174071578424122279044132430998592688952289021834131681 +9674276988668745718949139392247616305459443871778443070291211471809698194880071407948810420513382171773670397147925755783987072 +8396960072488524616931349500116793025065793132740236508100883299116095620726458675309257154864629749532773543158061852267047338 +4708002781694658659895872218532749075225884113621393683821853677375696999740077965880387656889715948194663339565410394344347289 +5555870327192777337829881158886331270254424879662205835411641384434648950116158681537009743933205450514200494453854527945997900 +7477629425593340948832945986684831020436243430891488737957650425170704516063535499519492960538931301090427592749932503236484184 +8423876423875909116240287411777889735748877901032042101830839646783823015629217546200038215020201848216827890202734808048558954 +0807456230133883710789676060781653192722644571250120561750361830812875165496905115518629620725251203869040049714454953862738741 +5303451865971911125648060457840129182131488705975454818569283523145391216818437057554152537823305765736049257100852046148635621 +1261116302127532039373528605959266607418243283938981652100937053779793268699698358833723678904084230346993115051100507293779300 +8426944066403361349421019695464969608724905091684569968020665596067769625988182192985084507208329554219894020546830342373194912 +9836620096551496246720748973842622026481410313119306152287268352751048064398407415273133860155815754408933010180348795503748753 +4609338756911699625776639967309034003047928015509904888467025680057353339055998784262880963136510735792422530687128803685177960 +3794220657491630690779260465220147420857430753103256706506076703567632175129351004805264212916552247643663829004772139169350940 +7434241221131032055118140342385838086277269231509234123959703170231052929402882172699084758826526485094213048561191825363736939 +0904036155468367437756029547852649525568053491591721559536888529587622171812488273446127650656057211625693401063929504404510727 +8594239813337093390867462989975908087174105244731466049861585875653241923202495711120654077327719056153590078128352003621088112 +6550980939146114945715655019872060022635651047970499718424015515740106255804720532248662461951505100400404921280927158680583839 +6741404050546354494349489969507458195999859785965336806692031040435145352201853821004643353403220778871762826594034649052903693 +5050161106286616149825562707832775275340760357578872487646716309800665185049903572794595588208600449320559114964177323489795050 +7988668426398349050514452498533331732900030359537800440340518464659495823291497139184592287414133272356549392805616744235453149 +9266075338762306748845630416478232159198106165396768656800162018036519950819645985549720852139273704724379986117806399232175170 +5556988932197330115132604542704979085477903108338527639954740461587257180160289420796745117217674397152069207476864049579397740 +2173348172420527062990485233356407054265180085263061116024667765176353870469789794863489543966815025332527451848905334447400043 +2653295629344700441742575198328366052220017722996829827117810780652298604720896185508927603921863120335461979927552660891307644 +0066285895594775046166427188466940243950248922228033222148293913898651489477743932715196100030572189599370661700468278120150796 +9557220581453859816276169005234368536133383160153382159372481458293494645221000658600106273797363754949138070903261376602472730 +3805492311138707905865671027330890704509145113408439234581134791375788711343962876719164349282530634198140214550594966178264550 +1436926045326544742314469119616902325996600826173849367078861191857896132772006881206929709595222480758410968732831090627983535 +1444386260983859263563013696700221044836992640755935489313754181471481261753184503838050091200730279031416065367564840157446166 +2902149047863838396186395637335864504592375088936920775765866174611877001827681253531321463980847446832448377620176627133555161 +4791558390249335499772225358752569718358080171845842601137108723166037557543519329054572216953276760772287383669070062122544693 +3653096889815826831439877436629034472435210543189243295673175535474384447647824085044069839822212271287638158109858092072312186 +0748574544351788265285261914954044165932622710335856937231283900552445994528620171010664109729214838117784033934235805145199914 +5275894527081180465946457660332530286653207667782652877623355615488795343761394166684000047525776778529177571402318083997867530 +6467109418857304686760026137696284413841248686101454431645726165291910757652786986378480149853820055715870646485922893256858093 +6676215878298696393785441082359744916852977084427450114208277030042915360587042453548139647735771416607502451093388273522788649 +7671054706470997891661682369520143839129305382788490682363534839812789784359519127806453517900921204537766515545890569275307893 +1361042207361371441527086792533736691205317414847988400835638938786015770536113548366581806919926001472840811854920801842703777 +3513103534581691803636903931367504112887620002748826537288400922168108606124444688399865918353400722960622289440592437281395265 +7889386408564819031497565924192895193912656422791697412472168255473833561450568882333025985344661645666990267705103860364236866 +4138536431654239332667925068305482627915123154662709208870044077990559363503200410357003059370208164876016094801255094919175222 +8463391696176344840026735185830036495571599978819527559041511392555529527154011417043582657075386914403021244017762572439715299 +6162318218961162791019379824579408557803275130662824155987476456514181881477682742571553987831058318649000691189597142635269772 +0762970133488857814721372272166424064692356325030921377174309564434883115428450738561984323237757935361350713032425828308741123 +4714351982559489932236335028957017819906944707422172680018267561533474486226912008423131335876195206249325093723564894293512080 +2816500946584950779938046762589915909515704971018715869504032011106342871269261795583577290967260307340084652876419981696310087 +2895004364486838513889537109545822827678603850445221912466809759993491507875666091568956682275842225830234855225383245575493552 +2650099647818037615116956954341655646634963643235750472739733468509790968870462418042203958941068786618484819431710507936599802 +7533481270625343830151159727689285867369942408696652407015806894633372367355011778476566950994358633971847804532262002525341326 +6932860309305588424128816753568417315152512711756820693574982607856846643933251360676478873401876049759834996395397442053366771 +6368418934202491624332453410763014824694322271983857287392943327020965722037743354197029665328443075456374222598320483575376742 +4282586458471797802238473382178433687724169447427314412337761093512867942490521429740324461721167516599618126259885014134320111 +6677394682606606653900300409179091142196198876169170096213616729601352901860163965806655227312385454363306654134273762118341113 +7643573234932937551595703701897895283801028602846877296702625125220327139117379468939484216431057135643790896852072143295568124 +7835389220407382114018498622358193237717282193191181508848234026347203068182480302817923964772670420825100195578203841958774014 +0034680494236515185913110737330886189937723492058708152408147979713114102686546086347368499582981596157232052021983733385897893 +9959931900795867812310134476649406402976231824404571628106712844354935633442704883886435745579659139240624317737165693276684021 +7687653483916295211948300957846886688394120859824086212655110896478780014654869246794209169764271870284569926071280986536129686 +5389201304796699165935771717834181664385244359824248801827481372360749339940576454594000410357902778074011200874064037958889186 +2726930691205319100949599707692925525547420349048657283658532712722412081334373160958039199607974848481872853703890943042973921 +7254087398978104571457211604241039805468739560240953011032729779871018385163199958508492807104550542120182914215024552352099374 +0577915788776618918085672242537299536604399259342051743390425673863630842390420280176568564228989365174973256614738413491070038 +6830892093137210682654019600696831217364160874783778871977883927322860408992703263164973180938595498029716242874574648385670449 +5774283228582139254923100205140724149936447963980034063069342493084827233283799090727386532026590721266521634433539903773672657 +8489342339836233604189107412549422519166707342446243631744337172427688799797300485425305108241084447320396355017099790987982304 +5579039086144583780301772101084268454284211524135896968194358624225126732215388025582474957280102865197930121676550220972263090 +3389908879281737422526309759732063914252217622801815648828128392852505613143446838612931228798897371072027044252222238376051988 +3571583131629499703290168358948884937302565954157189422433659984916252703476429662770576623285862465399735505261324990848316694 +9423186355018278041568884367207433805709684366572651006506138520185260823230823570755489649379382727052958581331266615555167678 +6126444225712076374139461277391085492256424369818965208777930082776462483927136117628590877335005615182912506580467844672001776 +2323186582401204627524733122349298471005163415025134564249126647383086521203590734419048262603709759435997087128843414545136629 +1421280371866708001926278928901318288694248295906455469958649830300570517830927222281187526547437638030692046413330135004977193 +4201689423423687675024635448407327285565295542200018160703811061939134316388397382336397162201256369493787936488029791585101767 +1265877778847130342563633758037018164444481581498122824054055477037485608751037801526946223069038643514687690116234164514587643 +6465410554863527094647350795029177204941109231775452235798798372912591022108733306213831496272836341289051922220644943607370174 +4555709604071919166868299016817656945294419168762671940808165674695761877170460702219178063962280901485563918842254363745508105 +0013210837712837766354170359690869396554185818181209225721015219925108353810498389109811284979505591805687297968690234266672264 +3540066879463905504617792447842885657986052603128773981944573151451301111933677250259888942146768184470087035596773903177095378 +5526250295384032490751656419926139482444274162610961490517202317645513359417017299610996672882236901128269120411948696682951298 +1147928498844392902179227619628807696892271738030552398135463750372457058220840887917423404648021366673468670510142397314290816 +2022158104950164034675577261500173802135526124382488977804744045573154829053227880877248106935421683250032295002832455111826392 +8408395848784043250462314109208469565763023368052967204488715596813441318603486322106989843882974610129619714497130039489786739 +0214994088989310972413925550111779902382656332396702390689823464515504633177056625665996413210331231059059122014949611785985118 +5638643037266704666895115401268842632588292722777701463135298698439281447394022408579092978979422708178801226855288373349007705 +8326215915178072795379523340039085009563169070517125775679474479863606678664704404360480187047960485640111421757696617713004548 +7011793090984794876958820884434550749243106407550272966012525399145703465211673094494166304978135466245182409167311669917812035 +0646616697055433264306514791141300386873739412778313053422926573847874849306566154499488632029298944379782617421929711377514401 +9297384752555069074761141370510516463922575237795983591479132397584337422724053423993525662073888857524562744276272922807488244 +1675900193060201503933483483504265157956265823054822690157216718534136770295979836197494827206847083365905242438375193125219057 +1431083489513661032602020638770515681536739853677363483613015298018233407875797334225693452983166033993589897236847768729569036 +3412842920266990322594256412420427432972591970945387529406395749977856430023496367881365081011414676715347811993981995507454759 +5814144158348481510698013052752762134884248303529348153673229587940320030719465454064415732294560941869034046035698546780721067 +3694293937312762744233869259702927434016315955447360677690945403418821029256532787548058243155743890295919743842535871205350185 +8704385056392271497806649473016369833786145282019863224686988090496033200166054190979749241314009880507707963024916288921654292 +3157093902990992972624660747840907228349704051194371097267625916545775966277296334446546102566702460108399894331979790861994643 +6211141708601791603941445199717151439636201237385208163393264036156546491266459438443028650233309419884755515206198073403412631 +7609935217879726233731421923555596716242460372189351557516117275999330506730738459524772217152788164129973955538645698104965588 +9847824808961328798019186709647321679530290047204083126132050648182784705034270305452693472108814807665149578959595630309221057 +2336424726766825888588931956562955321343032371214572623650620016457504060828907233038750693736196960807811513112341914628133531 +9997503724107344142689181995651896815056436454295876248977561163594537358114242360861284248276610577578928061231272901007371912 +2013755472268039887671228509805013770238575064767125406070391538875450803622634669600191763159687914916440803301533482646017516 +5799981996920369911160876482866900908859555470100550461338722440379485926728401393736777675182480925560316384661340355950830168 +4131746820931543634876854491169439418699998977639325418048790558796894160516876451374044889060607036066881936080985211558198717 +8071462893187814119459304307555930509471933902061826760709069595504601694208191737856286699434878770893482761009503838844594596 +7181730908337307386590819037242463777795080011670425589895801753553643404874388537396185083285326538843075531064088612600268485 +7874226918011033625880877484768842271536701038369116084490113476976040281425980324472775723714911411152630252812320962593721057 +0414263983887707664017378501754926805713789236380580564348508099662975168539697702344553508537455836440798954696577079098427990 +5498272997168152869729083312401990047724554350008563240202328869525129660589507428406050419409546123885174117080636577540661417 +2321805983098426879552953451815149973073351984882483766093075912871745174124737636496091875961776384678278562342333189413446156 +7532047930885191527458749856036064187500060909405487710845907268217223665450938676158247000950256593423837838865029236735406065 +4326287344295297838879679103933460283015103512394382866450845885799711951741393486898208614864273534333346921541450681108663424 +8567495767845161425037712088656492510273651099963049803480674659806775332933320452431001042997191274150089937218301268747409008 +1892491166044214033213915009098534855928106088594182060850901874215590307570900626843004514029616915218847005161443909006676086 +3105173474201156958194039816160955452881468075916368040511971709028208813038518932050264340243550030216756906608101248802881284 +5437478863331533890468364414134511741947560918971502005022258558696334726057777326125552089511046269814814736730962698056311073 +4951554244048858298258123156812226947705933314291630536799208713986095914743504621255604040168912151175487398095913442501527517 +1612279415688868683265625113331706654838561784474685978138326507909792993504451472859581305912676732125668231491237540161852687 +1352117401098609459260904440848555453118498774861101544577811081137171667682199549961162875051222443028292136608108908775783291 +4668566015341904293075901042719184643021339885314119600028221153413672044545251474917661767760069310540295768155060202109602728 +2520334200236839012091693839792035322503068072072460065591632021254758742371667902074341537861212278319285847598815912703706243 +3175268051143981660033747106123282192419159048635214903634052864839951778255031756400562020446144172393222917501787673701002700 +6674523291734748122422773809147614700484318146175088254981107335895144969807349660283900516719746118645468637821636061386059378 +4188188277626583767786502184322569089569789412892621297176431619545847378700138574560707680259428721989759555839556974186233480 +7390532896972001905316907714622971854007628170763072962479615247643615622571558632228493177244362089074929594584800973845536848 +9849264610200952816901406520106384682505741852721568226048835135789532787094701294801848704745256861395407908649513920150263639 +7854370986367091444291966220372693018093730764321979930716154354135718272983430773038337101141108835455952869661500155683833074 +9808115836250545782744528663456612751035343126746511715032833222650951208562936847133894670903159578406935434899681616157157677 +5449933772771897148631641377562540641640474848389783754029100557570032554566006191465792063723092795517702238425793412416470001 +2937905058909924731783049378468003437910238645835264792097079136587087350442777867813702093887200643761896159097869017005937395 +7717133729012763683884090295651337537034023162131283487583227409327777370699292346651312872193008211062413650051860746533410200 +4095695950110108518764643338298928366182339223897361738328378044503564041168430011712729973754089101835569129650304327287404972 +1174584897511466194183788253926349029362049505536254582209201338252575420290164983455933289840278231035884030712260894346134552 +7761765877961955439161913415420485543948776016504802146528406434389546215574979243086360938551004600037990661153084242444222743 +4353062554304250531566068529474211745999775668076123932253052153101156786485645664269792342453816897835171506547074839704777091 +8193385707206057241016991296332616186095885707895014417445978963635638843413167453713147741250557894417823842378892716883564383 +4414701258878541571780503656512173574547903536405524255843710113697817741762054938128322216548020454736882148058230445988304376 +8363544420911047180282206501238156287142986596377518728647856328365181602387822102287895774281073353368828111014956911375392578 +5780430022747871965685716392391425352083934583867099701698503567269032511748940082003282777645389137257025389413017452674572537 +2606024393701221307694529871431154164540614164993497271619958131424791186768644684669356636777831879397734272673335705649633002 +4789715983933276471694982923444249864558464851612286765926229253919876317314926011029164829296322542092827015562884359904206642 +4213428153587165381335521523932052241841163300973801287138253226845490672931816978773941803990440278669979647051465348624079105 +3419168581475572983260190361701934633564914249990805271700921787735379441310149748846950460105295646015883522763651311765718139 +0643017584581435034370880134992336308617187531872808223433939732921528795419567765739615646487503731718827467059811489514462708 +8240926821672036119484675230474136343520006840157742512260506574754560864426113591474981060698558545401924390339201368669580151 +2180431409945375285121040004138946753923265119496541829137171528887590486854621739462433884861201148650039065347599969036034946 +1220794484575763660002325630268586558185893116790653322219525972500497727914800636242536069684827121496593553311794674511534108 +1435781606190730034271758074654507450242292934136120417582018758847558210750517197211614624177861297537749923102696299059068262 +0472496196020370020276610485121224239313415267755759579734483426439502592786733423472402917351281201878422928096327388672118427 +5664545446808704741371688000935805171339458791507069541059893057148797349299303009996748288669321167515451714205892864440743408 +3539189653746007637884781151821846130210806166359445842675385724193537198834368015119520365512286626859121785638758771673116936 +7228747311262908816869990638505266862436414983330364594232199408818881369887998094729811275091341031498735810192826600585848478 +0205817711643909986528303579098875467050350065610591795091066174715779956393527864740830714556714738435192795203670003124014759 +9197293866434029742096641108142225947219962019182256790387923893087254917713809995074288755692595202295289706002058647970706318 +5801028709225408662416729985040929435165270531028696258985082752391299922713342941633843800570338856731347298583819618303436452 +8554584149353583626116529209271190042567689752495849141906628210792514969913837494587223513509196504949988416482688661169416704 +1402557567228850127015457900783688564010064141030969351494178978419198543363884975731636660451573493841983931009293074401767946 +3030096749835569373371333947576923588432247404291305005146406298391452901679290153566364898274791521766815033709118309495674776 +5711698251707518922789190956913393869735623141004406498318629780886567378932420667481352170410684788247915724815490088304482484 +3076891999474110399724898312925906240059216711193634437769661170118805829580169752019184852464954183024109801959695130678652316 +5549698831705307512844145517641207234201273500862193514468462342310564088815360136277681192006596458241962992029526454841238730 +6411622599480809121036108851843289420809827360922656655734346202711742438003990222191272251022816208079113097900823895134384085 +8642218596518127375568301272202091207693967578318544987425186514107159773089906580986790334784827799489119999185876094206749612 +0920817351118635692704580562112859516048707763068110821230955827967708140350064043987570026933663880446183554485511010170742288 +6208781018246003491238792622187493762169792217017386868912187285653248963115594278272915569084370195923738619149855997663765716 +2725282513427928049648973662146630492856487618068183730067763515357833597788940914019736675580475997314248706127026547890194380 +0912098065082008833983417148631289829848433926717155189805055489641556714887595710925279228500591899600062727405015074732933183 +5895616117549258380703685066950990019495929210886386708101945826998030626417636766877015647925822567530097432835311405452851781 +3061662816058323226135782103379493145683394691280827163116839270882202901941521202335712663514020076981153846416619848715503675 +3704213022599283410938645199398840870678548274677230864636710751729466573816253072905154246398964963820890925958414003660031502 +4630926004977081614276851598024033198734821505047735799361816301169717640380369512425102474864033576302498992022080746889954455 +8300690807062719013236716321873014112140959900671073008576129360776883572345888774946590273926184154530838752149268349654124927 +3447912234767016528327462063192808290048544233618790028527492173810076784214345126091690768646666027973848294720038771161886178 +5457890600658613094704911994422834789695852986406564746776631261869186614541778685993675186223192485017919263967183543307324819 +5220338990377375643418843097310570471194246573225298484773451484736001523372710934922163010657077255170019875356114982042678628 +3627624529201855124798528837400884425278033564368552915095248122583917121126951781145851039073704399808416096566055979179720996 +6138215215437842405346946457258803271828305596332127664808930703097188386621950424243497121232196129953435128118553466623324068 +5920381194124220091735356882290501723032521874921419228504863143247804084726835859796865644318358484363167764149683837216715113 +9615036434361215242420376195665243628638184799766269042140156331651635843944014951375098750172039065463946417568449326243598402 +2848169979084951950982695318867752277301100025960923376831113692149385936857810916874319944880663028251022532340529140647508871 +9496200698078244875645144567349529530108679183666387943211721642572053750534478253516851058589093730399207634528878897274151362 +2928780676487223500661310045048476864951396027388478482164226318809641027170548978608486524888062863065934362652787878241515138 +6280140376322091582258435509210383633979192424770386485640747341583226684971908322655364759264755383681296473387197771187476598 +7993834385710389281257683754961049197160221786960013125265044154150064439440391405850545737192660609079271627770663100625583357 +6072190114323511020360786591498148692349156157375850747362774972374185937176303811511475665210532862701141979885066767496308496 +6809986728266957224821129384937931767139546739620244968569547174693124128355961928311295430759387663715690541708892900136725621 +8713302194814326105418406542612287987228114780045964567511737165272440568730052294690749670110950977545749571399774020053301568 +7216372973252354647766828421771297596834262561708247359782436050128141372353679212726164448161015103612893738264244644610968102 +5490158565324437536585310937019467679460278414170537463829112562515850928229747475007728895628087831498962809803771938282971625 +7508575669851979183836777758657815365989035447083239066619058708444094473391492540134970134555418935400444033568972945852753572 +2681051665685441434594259287440247146082524888648989023037549139977692642794573123372272447282566487899476245382067369090850227 +3516664349028783594652935471979981657724225297147289075312362875423752912404004312370632615551263897637065247961679885605652690 +9466216134195188009940680229231563219004828730503835830166900891639648098483491041893870240357033427709041628218884025561211450 +4848574180102172215721912201870496816515688173491866267900855346017402326765363320124474261873980008204988494006538252079431490 +0175523216285771758146998384814407339456495403123042145864725622594607160861096627528216923235632801268714390775921555811885643 +3472489136602420352765486727018974617090254641528435634640334942425890346384541194932965110314503859645146781896767507525832148 +3797532143892746774112201800929601204074380314303366575611389855222496601937222584569281001864863917059035591510412512996095572 +6540831912166873834974788036724952907724341969947205585823842409980011722639587514582627960169448155988074691268777322159243577 +8665277823830969067856126267665294223334216725032631778088124483157924821285251588520602045895388804877588758409591955378022156 +6073508435137376529408576041907918073738449051031545977935509368280423831551346121480713109837860058648169729195905582970167345 +3067341878979134838523394919669124325563435281537910461171499830549480992384643061189946199980549578258740383418503347009079843 +6535435940186811905537955923044494920097170641355108274375772163581055863892145187878626476077935978473720854007118125956368393 +7039488100826115931812511833016243383119511518291750301509797980336012158668128482275262947277553272940357496952990711563149829 +6786417640541461059254040553435360397444650993725507452127477820766859236159070974204514281362156361495968903161994222700825800 +2231234046323964842602361033236684311038633571563965762668970125159431168316364793191351729000958068187715242930876778118365126 +0967020148060045963966458121640283348948132407097784342111426011211692372274414489985481236783110642969061229593384194970583663 +0518863000584387454760957529314643812512823643579550158516331617073251164731368407116086238782619225015253603821594850218277314 +9500480699512272865605970060364505765826990178852739273720908237913877224017867775678563171957729835114722642208632758208282247 +9138815300684460564222550599375232818221500256241474012816587003367381334232108533309664763635997507997023730828738583077664669 +8788277264997082732015978733262501251796460091814731315709354222338224334551918351172940509755714499856609104221032539934398062 +5201268981606265439495692981057225390935909458631770334121087316181673897848115792408678690299457980695904707677321358225662858 +1753894444989509786731779001286291244699622264775676367978135041545186928742736879462156569826451402964970455781449463407937916 +0502111951096040580120210611615484977765606052460541731989539917916976249725653281972573849533515652151626503088059881052346467 +3880078154575290219658231687424062098862063067654268460424536569017374872824177081255701341783967148208402475928724455704261768 +4392256036256639333393028072709593018903870460743710545351823219115495656631234924513636506499865017761998475934414247147614073 +9879823269518650692917107025520590107178762204459762006129828636469652976104262657652867426597078219790167485035259125893130526 +4491505097353181156857564911252591317148670618490859288558887139051313551147429010258650026546020137477020225229299116254389267 +6985649712270923660498384674089852650962875340575045596604964662128408959309248980968270692814095597628820086983662450615030773 +6856393844414418123694511010327503283724147879193562418774164993500731367874976787177535887228795026180482876909284579667315939 +6933319890504711538399213021011090982613851985557386817639509779738587337666166285748238443831950809951451700040304637246662798 +8972237565184004138743415214250280650420991574154677063216686873786654244381615346208802365774879184715316421935088510122029692 +6620712954831004627906209308475373401688370427674946161939930227860450185388905612087218953668667006558048824549749639289702982 +2252979368901033426647735610872049783979382409247439935969647741186385842273253355085952551816588123321471149874812539836048112 +4296535534263007024565979896369915411490254323267398694533004344505948807352166933785377686738429645888455485219163542489241753 +3740204196364178691705296568637646938043711157073683455458837069839389554673644717418013786210053159123354215579725572298341488 +9402853270709855032434056268150751702914142055269816941745088351607954708943354365301302743628246326922883750858759728646811815 +7702984283703968225748705248163911320353463996186650929366431271533557842658056577524133407573715263399635592058138266058468047 +0714364441200044139354320382443415251209148277388628504182113543224388038221246572844030594506529807381529435302712895155562998 +0439249856337395882945730885613068512496256701244515252923706394988734280606704794447812748091162906835016774939625011625452587 +8701684848748737861671752322431955447724661967607517009813604790923987516158908955881849157986515334551799664962438730731109340 +1331695486624815425190823239475277990544123745272288937823732446789801006674641059010522745250663013852871097862751638189259629 +0016943722810447477028299392663967547809929491638369830715943306970577569121839921705532284065788504261357162084070598074698898 +8301370408134947453196729774027640676030589016291539644959237246218491449189371838582480346507102754824929019477369849852385466 +2141296007314745921128534279212951822200061786556429858733058886029876384472876002241385397319110771995753216118435172891077520 +9778033995372614700099511429265411943800576787023724043391866778854759353346452408608492285235768924255415140931221126895275706 +6825951183619818611745057594180574765846145064255544523604725057011807870654883273137368630387060066151211603237021909384345606 +6330524686796934177420756525152826921339672339044318925623881985016143335928837573713885535605018904858579244102576335089585010 +6476163553560279805504121578981537068558170447489713068606370855078339092366601341950660196338341527489856914865088585338744059 +9336566578276542977779339876759708356666986167810967015943735975240831465492474205336898814725322385265950743359319117386285955 +1003736656415305664502391530550831810874960043692083942495595913831106487788016650162073804988081719551806867379947943364891966 +4573789346107222063252149996048100164143705560186510044433851243141510350751823081763496713992637129016266999061673393254758192 +0532366470306788734368951439658028825164454629260106890282620855033982533015451546843353901652120510130789111458392221784617757 +3253852880221087799999614626290882768972817659832607861376923098885595639154736935283503251479471872499871522284582447822302040 +6285682722317206257124522813577367514121775302484636697810948128394195942996079881830875566043123925277038343926112033315427349 +3775226925665315936530276286731605375257672911376828473612175726312446039948354980701402997357293119532073936998664168131452515 +9033989479402366425600648174316259705202840220576987680353222246016330572206033521947195443476632332503203414941943726000075449 +3497459952076058462828783375726894554577596495151945532419592454939988268844456907659575363299273992966202955633347460395801503 +3425070812671430759768577458384150050234482462127557226191091262303333548668785285283704391650036078067518873006221217224813250 +1772566105841430765131974198596141137685703304393653624186537427347991201369580105964338526460016536799639038488848966636423096 +6770176829068426450217510320589346020757715335312260267538371974073805419798173049507293562584393319422383700569196675270010649 +8817107380879373164458511189191184591118659927137093795839429896643513855977855984835472389317216929700259038397397174527984545 +9182176191236023496232040243155433201447895622909243154991531577802143936144665255362367572046313372242799588041424257757065202 +6776868876338528332502171518394602792217092464624165442452676081627495348032268487133922812426314590277917132179444733559137458 +2894505940700668608956696393712351416879091173479440509500831009675626499007967467374789971290965256917798968433095199887640804 +2933556414955828193417995654373126786599609721238527340939127371332396349156901995803794360944816870450892560704118441975220867 +8553706670591731040619297656286157301262507708170329674504933402137245881090232925455937213553504830839742590855235282434383116 +9795978189578883639204205303262814903090980902741535992637415797011196591002116065669674171784371634354111486560182744790314795 +4119132103638186216017314646897106719838984255536083613903341238051086509058863316947216192276628486590741349320633205188806897 +5670698885913664671019828424294908157556037863822378418679559826760134301152250392322845741422553340998938758664406467756180798 +7800330341660310724142951110753435998016854748165017611258286920601003037645494210716339815788133485140202482683448446213209716 +8180605035942700911937960726293062276511453378796626425148211321573098417580088171144007867547815289216240544844126601589466966 +7821272307041782298994133562164496850666805515425583415056198384421212580796187662587737326754702942280674718201253410570348983 +1042240816866808878998548317060777151321655529169893824595029339588028328148179827446446643616423322057981572730612868764050015 +1252481771070461052389437601673649482439048233841755345725688472923079857771939132303119904339949419801565589936559095363036112 +9690992931777916802696331919514208983516033826094247916936120742724743873182994969726283601645253015955852055017097669830871832 +5020330232719825823684760306159154637346729706124058199917604069885798242234996370123942691457916122474221663868046994104598933 +3075139912987034850456516141324884611162536977850304751997334983975217864864834605427571083113425269670859785386338318467147612 +0588929507981854517277972648054247858977149919377574480251256787460673116218337687478964763305472549592601599187647039007311159 +8091499973422866102588159323886136790375349337327388200287148626779716184548302459458465936817938524771802569873742301251650117 +7114931231277684778417144640504976098202413303636808727117937611520910337365455884518757181692362707318939628346107928380267487 +4950235887683950624565704360578955553783208046483127644086525832912071037741169842458625817412278583829306158758057435134618696 +8199425955265652644399992914133529916068954186792582922740480043066932022166380883075792995446339961281184510898029007416993672 +9600726548050421231768431078919064425306219469503912968092554459127359674333330276775477837154780938957377807571696713004700625 +7295599369136487293154800206051955319915985280570186743302107975100582206721744471004980480977594771538574498512856521123216578 +6297798457301239668561079936581574605258366525996359269984364801565322122584365843225724581654618783605926367931879918225442452 +6533005631078137948109852121400341144965267577414971222703110484819122046478368287561575133762804019638669026806894198861867537 +9875087635268237028446411260770380453643015454432015279985047995719605753073640336797720945754884595273185611911133456865738261 +1289442162696753398201423924491482730765830984320927644525084976853458311031303193411990973143614781325043459014068347046089289 +5407587059442212092113638721767282263392344800170580359252927060347500966261590384053868270413268344224069405566567860000139705 +8360711621827142923632844470555921440996634627172367848310520969393805873503222609811062308946619951688125101610027026987846081 +0944723049476073716566027637737569127176067300129222186002435041592350356420231369282460258580648007773781925508162225156920162 +8475983626059931171149310298021646566523963848825823192493400102745845807108661239544077702836139330088359628977689491846874286 +9167471361458822389556369913209606973474148790242038515231761016355472551050907573614312829544645064709238887157780557241782741 +7014298247554687536486236050770971039248187073536082362810060976896826815113357515444053263548215504897920396644014795535941249 +0560999088691723682614108804238530988595549473233071643241045493910124491974900926784898185428410219894939857122959475910314078 +0706992135879838500099998224229773341412566822617554024599579881313516475013871821845580144033844736871959630619808210884015298 +3257284534077293882786787542863570138992146936142087051892058183105267975351672517356758026095332624891123361507959805527300464 +4074440343205139647276844490970010808052229358468787479224053850708044118534407822903344480715673998402733320764968913814756460 +7541792444051749131437550370667287159419463038893712135130829128921018730253201583423068101308427818790061823168162915307740555 +1517558628903132612936551305596370414201320454890625631435775037687907085643182588609436062805330988597026150223859036793476645 +6591140085443467002438387440547002194892167702007183254656681949652141186889776360123350307460795672547510863464622633652798129 +7586611318404573147896264171755514255213812732114705872658268827371541849326158561820698871403138855140301594042187495345343587 +8296969079616469879346007981374601146907653922591351081026313152884017339179855411584129459367820354848581380738645777750515335 +8731520753126192088044684618604970364839081511006806274832524720914395021711036520236353757973882463981033460910456305044738117 +8522729809358153390546384877141395094866223397312997581315815377215593011496352023911920483284381829852239261360027871178466434 +1761342609588366253125631150586581982113849076981180786682830283076537811478367306351425774976563602440271445970009753618288192 +2190966255981782407270090701820316391321596233574351800067587131465490412933509752425381764960420707947936543904169527628499764 +9513655081411394282745221856716659032453348163674130701896927299389413958771625248039619427975826651601280801943472380714643137 +0617514295978753057337209914173385759751936634152085643157967813155609425512280471102235144051790721763863647958187237685008587 +3639355491023500583172147594486316367021278514106274611239107265098592491987675254836995000835549078267373162122839134981244694 +3032030608995234928881439477961757003027720355891764881759463215674265954842081986273329762678139617217680974433220699552082278 +1162323720318256232198753258939220028085022540772566418699150206741508982674558652349158167206478285853624098497667218452569282 +5449383863141380776369123912608347050738601579475359170938129811063143046219515664348459639566915361115334890006657006031842097 +0352577524785516785715741803748070461694324222115473415680064156677430016348738412256655844328208932407174179588633693022145069 +3240824351920886083704779764410813380504193208597077988968552959949184540967132284829781189340533621232738907229081350464536533 +9963137564898255760485048365009768861444430624328428769392562608655048396702090989488644412080235346086820241525013306324575413 +6313120896902229376551555163469139342705609183907471905448131939609829452611107992213294654747615543541094148280055017927542670 +2193326928610645174628017287211712211408683079421721839813537852939457890561997238125898536070007981217325358023811789787011723 +8243869736268481951401784440593310926754240433539070013976127357814953175386685024013642745787559590618261612069606519053423542 +0179110493828168312231149822817828204338127248358983471224771340698766448231962762617835559498180861959832229135237920163613151 +4787386815157272287656433720722356482569671165537977997843012106191355180546649834098738516604856452092754246704403258051505371 +0634701773994997694376147550495812360818423051628930021511577756465075343321034591427451854134383867627150784580620161049855432 +0766676598258381037189024267219785521248498516810290597640526034223344261052531930836169758828566004912961881921058343147697444 +6482511785125094948376527886610692241647293945078604787929517099856009510498982203730650348969745313708621728477885759515338493 +5205196085155489849560658125068733947071196666550006357643461400687947338189762369344420067691692171235090223284004207347050790 +9809626775201561107404043309301598988061425083419682730787633868247584100867048954636838021112840948572297116645919603622359907 +0822943415366943321888436141210445288839456758217953163265370657515497810970631795622846252681104767475177503937782564413276889 +0099794234571476050102251924980186262893747705880229668101192044145818046711997376029940219465691893053496893208450200931362078 +7338980161503676399103395907119988361285887220551606875942870878016149229490400212073840391368478482151725734739290023095487410 +7899326915683751786630813196982092775796371026481722729931635095642264999233728389534905015911812126591048864589797672830096696 +2751118177471534985294175817313968889734356500489672942275914873801559720503551024017816293024629330339229860581633938440765263 +2579409952433198069628120504384999886282779583918285844878077794508256550262226622072460752726666294025165627679376513073930940 +1557518549380248705140615993858146675367318700594928368305022009826106738355680320673267541599976659073767016002316705812605118 +0706655774414337358133841048392051795234166451755892548200847217249090290629891786004054678210061272314883459040459769608941760 +1894242091014594788195514987048397793320863358408872612791768967887371355399390803287124928045545098723504490408131463760999333 +3060272094521186414251733828941054176318890330197109603090531231771409739699564694060282342629340263255630493316904735659002049 +1028815907660526325417480678219858187627208188992524766305787455603486273947607751634385261335594552079352038645485771077029993 +0683468157651545576501200944305620051155390867906355388005127965874177601579306536442110166029928489048442610212141268455617318 +0988081700313980006604864032962026305268670102163784987963985127384885587927642407664223367758558519122248185218742873483956807 +8435692954315269375984976106264988536738299966701776101047442064080401887709348151446333391726616352591853741366988778921917957 +8956328839854159854532673749984321247777028190876411555620913589749860701945785300696279975495557163768982684990843868186778431 +2520325381475441756157721367873710939223796803781885424158254911912476533014496257166233162377942526340788173572154209732321202 +8180099306765466453860728219641256442389031828386563717730489464926442454808958172470108839488494541309400369186661157251374638 +0628247148382602252920534144240581556722670156430583076620252695866327968997749969112676595494827895760196984828212360640034869 +1083401588117745555669152288505435653771338944376475705488767101152826312238533625308441022273664874833693060276780849703095095 +1156085057274283778724094356821366624982478225088443694900412624860954703963723162086763699442272343649549404340286403255643105 +7910121814653271873529362722249047841930063953194714561480180469523505989148782328178679381325533730833709002539304255163521040 +1625855113537607234401449098110230513476322963277630854102799933050565678807805089239388422442620697922678611315082884292068865 +4371620713456528072846857825011777710835705836437187394614071662418465730864540826899869210260117229659041798505650538477261368 +8985576750846387981996728278040998786610281073838965731512714567426037576813755214228497693635971824751946973019990502558550741 +3401249486943615979298276104790818107736649764729969044884395409904447217117866856126816079991915988836153438356752147841144170 +4762938795985970542856369222277414531064648102624695688826552014393694646959714635482411149329964264665288374841117934547255987 +8266183745098026650707351627828760547702165330658530132998099760473617506446329738701905894912433667123567916471538563863907720 +1749733602645901685261363936495062217041689057898136406969925562689807608184144495204588063954048062251421026224871438707736424 +8548102168448428644279598842714118177587767821370906237000294038845928905112282335030976267725288843840126116450813362024012439 +5938499597145637343173003399875431713125443282016722261731991805628239317480073706051380874344456692406363674536084190692646142 +0730500561374089587897509875647211800809704761282604727960645054003482860298243507208373227297725664305984934399424216469588628 +8419154111597746163254112932894914530620901371133608712199806376981269451117389112518987662727134188058972066300128519445766440 +7937806580759161251998478953470159484262859343028238192145225348422179576900021837242945100592904945106701907680168893307572302 +7516873429370795478978284132360296301129718931677825172903818753611017830921544550227392257770205145640364461713731347634995836 +4812445494938381664839724186613091405433366166945041207304600303147241282114042034521353097954231266436563888803937496834589712 +3378087498752524611421562976032943128888258876939559646852575538636633313807640898785156463287303741217790696080003514700600687 +2916828602935343245489574829087284839505734067476926451104496434541582983914416281292812699388088495707067652976139900162078153 +1962925444708479245069456050692574860960690835458249514553536707157865617717301444683893731781098151767222498616081872995504896 +8173903682609218746993851247188524280631112843215665762250628484523471921122641923241385617676960635551184550463145418449684690 +2919933149036430836155770474842583951453613631150982113752681626101371527818957365564134323888669717562193735500885632774199608 +1609040194815030093545138643057865379099082109649230423000768558709693305039551156969735710643672277845208035827493067182565724 +6908049177553254273158603203912637795720376483171212869602699514186725461068506421013630496115516175113680573200157037543353536 +7096806396941038667344905110986317666939904573544009284947400783005195693194103701914010433669991415780015087815639572682791514 +3680756978168727352707707927350530897842094443052210887958488018513662258743146529661558086974070289076864455883890457256934114 +1990854389059420349848505548169668954938843880071111806085356892226974097003547436768393216697877346132002814225746150600875921 +4374492277982018508835930955357380285574652568017468028145914088210165891776161047538281175076369924948661559170128551211440651 +6393090046195615488795758266312386404911848462625030168594274415704463604586947949625250757933039912135660379167156550736214014 +5728243934436503311699358540925223297549609990942386098315769782770878982264993140803173130768644963918006938211060093506696811 +8242993502087208931436768299544970974899425557012952209431499521399155627662162396837380424393929443574272383673835625796387465 +2907417733054963303298412175669470571774232019901402324926626651661243360954715973085766036500273697051651414938093042950467378 +6785894337286818807967188916007632157799934091531302525882785328964971157321099749451607078727217297793573097237856806946698841 +2698558719450879861393803875387234736047009367170703129862250880475805397892793129358764218650002148641764709592993056227677164 +0081971305438865752091446464085352177755237063190907377734114566649793616356658589817576855323559323530686449621884731840150788 +1826939540243947080817186653825740075444935631469715645767626335326860305087307127848152820195439244125563274241365606473310540 +8086243883958240578268878611599696204485799969418845316458163879163274209827292559109804125366908746820981499563030997204003905 +0386391010049409963011357239704626669689746915426124197111331235480408669200583724344735150698143659880232480059746405028850204 +7070621356175994235487518719187218494861976025281218293612423462113181646903473241591007825352754630207984555163787848738511210 +5746214941035113394857005157649321493185603337720914119128667915231061424163637568324581504110784614852811094476768652380182856 +9112517888906528267503001049949573135127874375506725576149307826638851599758326023611417494647191891545342374452874064341534379 +2427813650814077661287436979575407398921688136831196702246080880472345267198382221158754713144173467705458354166291752011017130 +3427037443878854401716449891823089902160247151232344993329995686205840347612271617101573796125148445827775208799719717001561364 +6663271105359323644532079372626792834540392816461024173260479629489907065371009161907255452160867279588979514640773444750654047 +5973386387254971355387042682264494417370956942797373987206113528047687059080572493246240752693790196755839336183495531383520500 +0786113322119991260636829390212550532753074849695602064584008995135867708856455125847634381213436388672553141147016113632022246 +5315245540985412852477846866194175059739189599584743384794049685678013782286205098025299864897730658726559776885622145372207093 +2947046237055804495388214736381241294012375243250351412075115454784594240484997746146442841037234277389231586332048832314609602 +4940560326735767175083614737052886039378277408442894735731934794992054788139312451587128497131293085948245032106415584594528368 +0132772291233611554088929401128017045937329980274933241381990578374018596495898631586186020586965351932668997510872004538098971 +6387651070790112849045385204698059096706128798635300671169181792420546539133767532896269735055635232257735434930885609585584847 +9016459024097607724588041340562451108743288014737496316921528251742165151671257409207041532182399287641327587310586187223956817 +7534883738445463926481692970148340291005837757179973259685921796789142441810657257829230854362459214897426800925224271446152113 +0560193061957298132549489635518745231685759631661778699250394638322691452161479836232887573811183208096513244778566157714733211 +1547430295477490963534296984638628258877576273566145152920369666682577588376652374130208299869130148553971805471715526321961195 +6131510592032893543765491267410178707297913936975805122290905928404238596549066769665229727337467790389337961037867643489553699 +0609065177988688342181444376437189336384085173474299503743870199156371216381879438127542687917993261590322648797772549606051885 +5486486858037007564853644084702405129390388809213365108089374357918264647019430516503970264903210163964653753490047274772902330 +0461300174226405350596681300475321223682365336534045385167141718398138848125986935473489411898994178547406946367385725330213907 +1293509274626976153463652826393079169386713361776443893666230924263947932847701762953659799461100689690446626123374959253884277 +1306373580520533113454583490110255646387329350405309931889447247185635126485724261288069862689556565591921946727772549017471108 +4575946994632942954732825619796920704805541065043930109567542878956936210283544264617080903536339854736947121491562648913229061 +3705434044613436537344272909123399762720035093491390444488810776264889843570633561219106570438196072991495500597843887820183050 +9436774296203905562085230110417550251074216179370977223886873788396051869595539699599654175540263842606847704783570758848289347 +8739544541060559735321933455321086474549444477666793232318769795339438758735396350156993223591143789407202109979058296632555075 +4292457420547255026544250001229816943682894619632431862491477990869504856164569154248439119907994750443837979614731379296026483 +7759359950784829354681821925764141180511597239315053998175532245470389364554510124781233539932768390441633522487434841829277108 +6460068597420725351564334958788443977520626552046510044910725038151710003467025913052244964411427404827820741380296783360351862 +5211054158075000733081570753562084145801231533068994378093380973748015206056523195741701721944088905307131000364624646104412959 +5581089920926259625216459931700561828077094118449960988240945610479754976194870306805076133937666171790939426378434449418282506 +6039057527913534217575393813423847928955750038761332266774182283692275076570170104686335201401167177077211434292507428463611621 +0405115165537284758792010216853186661681734615099607405177733845109910013152569350731884598363743840624814198617754273927011515 +1407618016023682414983073935592618659194377480743081488662206405541839291934958759003119610099774589701968015068594030177949217 +0790315691986911016674475309797543936300728937674591920985541138152579574295860682375269717640514911824203818306809998011275060 +4382117983381412972994275528147432373242673418215434465602805780063289348278856395267652713597654578622689358031371373016193682 +4971203890127140128708179164505005602256484886747097346823610758765747601150832737555748482400760746795103492345268624660081193 +1238474101549902789927134668369786012288633703563425355984226013256465916402333622807942597885223175366854363298056733781480165 +2475056989181989715935840837103963570756393472493139600699839539995767516688497969127775142807367604482540314805881080534085920 +5110483916952128964226635567750120557932419254741576165448818621280817929369091427028028668168593266857526132988629197479499338 +7382097529770364245616319813419439630747996451787212372784898440040645194051307376865641947314145623284843530906118562837024282 +1180719306858134644273288620402473339088866088558646666749328622134176944113672979075244216767608666882416653166422367766359934 +7454692494850748528168077614552490356358640467830959916873691009192683520419187156489031520981784955116022176529954332656159900 +5703126531777606205672012335960434796458742284617167876316278828807209603472901804370309819783462306443611963348173237324352923 +7622935915352142387734818536448742013947936195902370636105913778439778154319304174619967238291501475263405722167671985219327033 +4192469391918128902822325360387297654953797790226858555687025028458729211895927349803665945095655786671449147679921097155626406 +2780677237649339975622761955036413080116980723465856549727438471457957358521359815258203894427475417964365478268462653223640340 +6384355742271287800278025268983527483432389226283229170112845354988683227778986191910815226499442938779725664362022274661446656 +8983529286956750153487719136198257278584599062333134754547317517831680020077715886321603644059583997509690986046227389994861108 +7329782796097322937140185361853575699691754834694652931632748528792770060065607908729085140248378226282625852546158156687855610 +7542858690282896923167331772353246169388345238738453225427851985834084721866947550098641275435407170332769801857389664211235898 +5799583185812386717849837795175430854770749931463501139883970148364355569591352178627597816164651268936590016366615235430971843 +1626691744466309451182517845050907803525893409042451069687831523577743242687080029262942742968372360368621788807914146915526880 +5318459023686131069631243003764992320489381131276662425282581511713606116289353391248019203404378565007089996278162834011328054 +2940914263304203321901619997487124198689066488749388375440671721678381773728920019056941032982814050260828373786697860844945937 +0935106263610485636127316359361586585062460943315331019818757835533567856247177900197592421873824025425985298148601575872003838 +7678845567273084139170475285839065925039600025616987691701666903861540800590513965950744386259900437461673353720707990879400851 +8954549187260534626790030686600472676242184422173770737899202674956746463128211733652450269429272431346452882295888511514249530 +2678743312277245350760231139022995628706226321476561623183303095250671287742041881664219630594695703026351221927214229218759706 +8754746770599874955073426285210045733061478934256278154824723752843570542480374148666553082213624470594178380618511944338556770 +1258342170448502309847524118053510267304683961421264512188294654208652383435713573419843151024833136714917076044792376946554552 +2686790894179727162588194653073820095034068415445717821765878768235572220948743946189649565838289653662296142578461681042115408 +4979975702994756010947392018214486397764251254586716632357033001934850295130508675410334819960903160411122706650159720668896818 +3588969475934951426055953320940144471459990302441248110905750552714828775127383961567862284258836730043491152173444309138622805 +0885835602018492226235865580484440117123283431801851414462459989105042310153202450298431895243159623304698553633960555968674076 +2495404637338396778354332975443524901010204103490577360377282407257576116606492214532864036869380580423420924036214250605437552 +0273823801820228766960129999040441972228765926389245168282516822705610151382079832776017382538534107323345050070717318572440468 +3719693296470114077607298610532514071278447231443437795910351532332738448270051379404482177889663554053263746637709700053641967 +9828951429348008059079973550469791268813640030847883313731919419849614743360559828672182938947231250356355114573949483277719010 +8160147790535337222916241165963603517851503563087426894556730214593005608351479878086372777584308199478067490440060949884647159 +2948684894394971841029364986618911826288588400559264365297543464936912860908849309130216792677280193265598685884468366012698447 +3139146768567604514882268160880542261286448423327913447026774946542138981930662613526814753066924945383435581070740645979592320 +2648770517875200683345474598491811627153093170689853703611806314466352414707945002443742160480066256003350127740349411901099140 +3308592816806695381210143529051488111319581220479904092647583948868369653515446635908476209492392955841254295107008775348098134 +9546057832057661112548846778480873064251831646186943320977718022174333455691510572548784701482794043711785251965805930805124302 +9797598220825240487337675580868560618123083511865310119111306172185132317065154479238514833175902325985968356007574306322665172 +6169700545459665615556940263207457415925074513750097043236324790451429771342105725243301487094857682554118664272164353388631072 +2065773265591123476370674843120027879502951065822173174917589895449890375502164860452910893685397468281488262706240927914630538 +1707471421521563472417257512298478800552951214730430454446551411959701879859928077389843110236600965268158074916952323108191254 +2504020366275453319690764881376901720307904782764373433647976640867427413961893744590272111954111461916745192557787193817126040 +1588579151481338108756894573873934357158704669830936720767495898488860812251579113152660788061419535504615986627104261122375990 +7331623622011754370645778091243569467777379728540842719407347993337611435365175147413009114962875879124181378102542276566594108 +7484609326756980072187285780841286543812762762534587441056462410575222582806689121646585229111979073490526305371963936438467919 +5706586317647554978296224372749817614542161459685489294484897802898801747536653246958762657311576344225567199024088879682797895 +7982416710167868709570332702438980645861985176457876981357976675349506324625350574864591977896798566893422693217353865223434242 +4612810517637816971256206727690640808547458195070320671241318078156152911173576755031013652809873321281227880993755057868636257 +5869566853914426396407700744200965755799089377206196772845889331869496057116760154715122075783787728423594874902542137832225559 +9589225721722482637458883619679136109478999500918895037360018469933679740571642521146059558547268528193088694289457407297389417 +8114608149302113225536168603740452239048816211285413122470267658232185443153634579198429078537401989102386028340268733889611123 +1658771744647637356624539664960206812688702241127922503053926273465214516451739790546967335354319444825617690989800359892589856 +0053465651697302095039711511050038742810220818349852842019182724663703883278427036626489847521335203092583455303768771456368837 +9461783935995459315552854731516681231226811974225458662840484653357703794689886592991537310946084831023202899386375997693481512 +4533585921481186000403378289480817856084298244494770175994273545179036968044114231324393781909984894250047249639320078105176941 +0088337423065130496692628858394850230047403024636567775158827661121094757863694577077324592397639708861018849419374525563390572 +9433907825695888321692655143225283986904641901823924591220365135713855259383744336203797993519930945597887702113356326830645826 +6988446116797840398450853279454162958768243775646820798515687585868046231740714350466068297160555230698894793181661331483900976 +3228255598385373641636782196372646263654896275851591790742732186929071139789500558984105284755563028281035328678092579289714000 +4681011094010579149015425103883784622081638214345888273901960768244903621448040859732301956914911242855175944393888342553563087 +2560980412018398873410432462155957943116348971819529996430919042949095207925589612393187400930103605496273814170584912659092382 +9069469616610979589154787060025562176784632650316733720274832314981371020706161084351225782272365506817110974959084699634535788 +6765587755549143676480283034752182178980320308874937062805295265849032617917049285075020134633510431916668093086266525800515736 +1987129393090049213521579473208927794007157494973792406906256045288699283419276005662236119067885571670938877198469481410429981 +3722619269607651225319072582934109199126677644296125933160645018724047938190291886105626593185859522559253131444513872231301882 +3878903450400959596968323921819162239731394330071230682797573061816799122019281619186466630116704421984435128057686311537024574 +2171772674581440679441147914828465322671796713303729214917204516452594596558898159147461291026535836576291138961274078954494387 +1278996875658664588862768319875101010284199226028473900312140448349990579106956212227946883352757991736474264479730037979113425 +8251737524782800721142346398693390112514424073577724343296177123905498209033583895128953063498814237667366732979745537429555278 +0896743149526940034664390783553213882383502205749882093466354195761918368893140199641725113693740413564898871253806941096124190 +7796854830589872123486600630498401268330698814798157768931103798808711643064916232366065296644778046047073412187780619253278677 +6500537503531609708551372800808683958268146390074367413801774325497534682441389495398023074938743544207866592122064463211672009 +8374007659269318743410884141200642883663626493756879447578129358932170292700541285549708569643545027306452528039696051958912164 +5461313220523803049639223578843432913631643532704665479011316367050573380692644563882130994146341345324086855525040200238888685 +8089200828994642111859047971321583377842360402699515255215765930754030446170133309029861283548123601950771958367816928039501979 +4921861981592932540366839797291842409375656173168252866766746284441002285941098783812692417408647250610811303546299712956961745 +9167799975366484899208629064327399424255199740686280222254809555146344897012896217389558240697588684347940576269967072665683864 +9259388885200851860744997606520800745929512006692275733677005833780389985037437438896650034386964970221299725643606029624930990 +1923555984126235140744073661251739569863134027553494228555366490941034788475845541690058986834251129830243447779754071605873640 +8546321296959618681680784347875855095908171158196021873844044080735403609307093046577378976184646301996038465275795577308210631 +0292841602799520083981301964025702228115813520124006499666068950846030422607104364257191751077960220252620197225981403964787782 +1502322992487049171872430148257934075524270686852895414785698734102981748355080650539194240735935759095611779799454232906920170 +7645673745116807956634247517521534891766878215315481669195796109233157590171328181080639943010458633508897705148841450600988472 +5372622766288568973430030404326811200585106334044292931194542876927622884286129265334479806408440904927200146880239590676743160 +9737852886366125426992930022076166695307759649277919719824038921587655496096287480936269368326605698820270793854355939980396762 +3180790631743853089037422811844405317287386545411386410379668362987107109358079426452748318127402761332147799830440163526016129 +9051792458167155549317710812956107571797762247756516089487421498534083002847984096816239076561386593955153876706013822381927681 +4173095560231171732875361485159122658902814416236442472154640300288401422165407659115651941434514022078384640403200843998671972 +5966470556331046673745430553457883526280940892044422560744252802816860579980940340917982940500224988618235941234788610789509168 +0789375239650015659257828854233372959702664651151444414363942452653680312005125741160555543069598467797693116222658921071003474 +8906623899466461239060702070337224569185523906567051838309830757794131815206179504935357263945463042333252081048931892858013514 +2565946185596833054040544913117114680329109501977878093743966290348734278988799428312347727405020457211698124070822545220367599 +7887639027440626642608258462581197685627001806341140783071773706310063459682140433859377089948217599328491885205500920242752765 +9105042785087589492163764806502467978793005126850254340679666786999523605304678430709511644540488408967072195820451312364898977 +4806602335002501148210169758225301946374240051070758860058332659173580299590222924023022260764063882929324416573334085874519685 +8860484115578005776711551777005731510087855975894724843956874006078066036507250577776451260714027733005242056180700853839120022 +9972510062716845450929888769682540876602068670550887529330503339523242901354677312818632650357341649500497585776441604969459550 +2623606137079639076047135393717143616094073824102105929404170935516181957666178130675618698894069752882863773580174806425770058 +4036671892720176871088682314351824213287902597131301780764439176800136663620269901795969751693427552587437595932156820043424392 +0677294124292240149296066696637747776445858679559483174535961774585189431151122220911414705647847144029722579499169874681984001 +3515611659295600392682520429928899147250410034409210806506732173247308027181439461299331344368884698174936563300497600474483708 +0063773420552619484426050490136425686777123343387056930411442496315945289353053161802745807953399447036489317450479861853401831 +1025437802898670603956439520711493780674154126180902484921242469929281393551385642769157018539956452797615106532069322627218483 +8759796578568398063968102679425317647163292357375418469803494058822777510870684874739268000355169575539187937262850844553141671 +0520130761108166376660600783005143635657232142524978468353606381401953088627097253750920455395288123559132491257402067683088919 +2890887289996966535342242107315365601206035014159576380219737883962373302977227719789793402712556663655879816814402801231940391 +7075049290134330330911964129274745947329781151686133509827045508476840011833420977165532815261601339404747681713954176358695787 +4190862513692697255977164180664759574127822507052658315853043337010650269787647027656260221445450941719360532423677717036461514 +6908155746961875390092893184110336543104604423996158420042649680420697682015958655453176718660496277341803127921088518280061343 +8331309328194407889735199978235437818800306669005429636917360222653217191676841870581785131998034245906906399415366961802496511 +4025301862689940996141896458524993405457637230423945068131717803505952316603722440206101338781745994056920279938566008667957974 +8407156560172061727221211500538117499687747789630891734019549331980906693172176957439813668311313041124185359330130579817813671 +7466416959132373028000515440426004927338552129675365950832267054293724597329240325990928091976976797329108528262309484406803795 +7029882344252063506996455416881601769204928489785629548747090770306190718064516950313796925310576174715934481881905681054628070 +6880642279419324327941511209000580929409891074059158709439715032933825582097408872913695012908119133558817540571147731630417054 +0450976304961807996926719984418185237945644387146277128118939816965978556618267349869880710186417738649005061794411263100794703 +7430304052534898545934660709004803179571319216041424124846029027406133093839979267839656729246840664461817790155804024698154985 +7645544772776077953527747512204638297554270388027111651502874795299134795410937797070656228161921719694817879801901345933313665 +1313410076049663998827811921356048583498283359569530324863158193199961087225704771561935232487625934010979224924950123845221938 +5542450907649094312430747916423053695607417672358828335758664117685424978394617325307248658166202146920547925646776725087468720 +8125710483678181819120321493187443931435742819361342552711290450551262872707443639793745995943576289709507020621251828473114770 +5808995455056792083922234742431742082106369821885564948694790039533752228468465967393391425326441602867483086230714643155046641 +2407915004627569028579526596926110527896052886334582385573230907225818330304996992697006735418150830862621130034997989250568103 +4747836691209220024929494385208567863244371223110054782899030754719240501536232821233744154670656379727685527957774155672319475 +9632183735133215405359145903218054467186662532650851575633645907886287911612022173510068469813052313582577723168904869118582615 +1758443894147315526858202229285381300192667883978558926059924490571584269631902778078169815289434853467255533171050775547788250 +6827379725903576619807972215479471705793002981926808670091729392075163455575073588508765374074600361763199064144608650998814612 +1139348883195112842120618191199370895634072657781661691958053236883789482321308598352679539450892103726749423679416567117182472 +5628380449503846780627597266237186381426941668768981275727365655268389180764025467621494196092237996217033830813378898293373834 +6249982118767143075646051931844250281862161753132641089322013640269043322260625061753846454820580624883961230554153956989845603 +1506389246361534372134008198497771836659467625678409854914006570144648661265229365915719554541771143877999578976720337073985818 +7005263179523699177762675340194056182817188450058666203568903779623473238455975919469726862072009578002483873154457672262756142 +2974648493466373909300392298493641388947991203930909247567333382267233769728042131150238686456747730553128288781731025800074389 +5103801198075821222369482103926218823537252278659224466101143285542928579893855240568803378461857010249847636234366813910593321 +8255444368358914408018189587539831385866661807993390145935560791507824844153502737089683973595182396850151415917641412882278385 +6994623624086457510788033568938511176649748143257081559534249807952079088004873263643117220080770572410226081636499222544341556 +5868861827549149829709753995576140261303291609060728919357591890769431437067208298630183272025511291993873213366101172628545169 +3536668590020988868465523872067378130539719812107645831862475320339405404292929655894962610265222780223832442563481658864747632 +3855559579815655284020206856324714890689773172461142431850331038157366681406979208119201701019291754639995462477549126425391350 +8450291792039621077539436402960450989801386738795258875543765926494042577259109968714035988972694939518009619956412673656801521 +9433075795122748111293126771649210779744396506362417079033687912993766618349565561906099136133480366162159291355560623721579187 +3378797875509332216281558051343681937584540368139917115626876588840206579792879445323063696609803160334574023091211019696117960 +8286659856511129135392256673984667705469454940089344874655311260297659378259031305125448870028720171028350742267966605006358033 +1559883131888908511800248016547342952257309383028102249271256078844439208862517563884780187208616611438790446776997799461384713 +9213000700276768725565441947730287329227888200262127732583001595805481558214719061412862292967561082950848644790833398460564723 +1237463356367627789252505343790157426584837476951882453112990120548575795054118752744721410491509448371890573822392145256573546 +0705282244277315586839396249472907788988824858086668101214804950902161992883203183045693099700874860748969961552066568180233875 +3649804362910298143091615106831740058421008356128934335979284102172120855125136327683861035088765145754792315464262726624806783 +9434730144784579817482674138286237679514020123482574946846405367389289759453442305761634017866345369865209680919734485563877769 +7735810389258589718835809442124950108708303704945904199371845065484839372522859566124912994258804441548671551838820818437896075 +7383779025406555287701082383561184897642996999173556341153509465466385364878232088945280693653997350128318569872380406482923450 +0873625836034957145587001580511059464110231294763738687292419746897783167632026738408026265866915946594910670670203196220111099 +7591072259741426420367322902499121885937012842290323842004604562106110626665272233088406003591488022718144482545987747891823870 +9175117307974364573779518417573849521480439605413624062061689756346227400155445091604379572899069013192269044493766848399848392 +5240204070345240378009455038862456326732346785249576960346569277468135720674493354585769115025511723751059422380004118585409709 +3349173998694448270286501817135410477702569648480525937208264068896413487832339295364105296137401893118290448960552591536754124 +2721855377073042117183109540215181252714104113649070283807754222580162122302324470826848007123339852921541117150545915474905734 +4442237467667117475961916917266329385273400917266015718314637687934575915917583933649989627896013912399205012102463085560527584 +4091026285900629392132231811067909337191049861336953793421590084466340471776345574531068461460044327698926685026108185944785273 +5202568174541673346987530914965105932805689677456919213224331168071054852507039901870720345655716025476685968559834858758422493 +0614858796890361150654138608860068135509273139791153876597516417474596470901950497034522305572817639212080825359999795816120790 +4690019462662785935463711750437540580321430169890806355137538513325878535253326703788253684313085653375194280379587086774403636 +1192297531414960747356858854539457018921020998476941206542822005080405209643807598603349026957600933047420780702135587594220420 +6385879114957936148095665992720513226197859511089355764654052901972661068207137358088876405026746758171573905075146612191621724 +7225256074560068850195651541217829216290435321680622077492940113765770295891334808874940394348277026794361425754981697514840551 +6579386119350941004072171940171520374772062901476128261790580552298150743389764668933362002827319670377285437400224631386514541 +8225060930462866114265671430768226751533399557663700459188510439035058277166158167696828360029527807376628848000272330091576713 +0060985472860653127812993949032906458731116791182858234879179245998734711417931089452148679024170798661176089885161196837664106 +3722805008939449259124307009136610112592018063781886320098372656598331954054355261215122816645348365251690431718100984807641151 +7671563753475866652087409998956658850991352510305691362750704516857124752942784347614592502287785972007339688267613862027066735 +1058283061155393934892259910225706182663785747481861117691921311446649045132526185682558542054377226003665649454406375422089246 +9732227956822666337303879201449758438032098745926653063569926833474234804332766368153105341066282800778041496886005184241210550 +3671076559003594556531982936604847786145732585941552091406028722895252734130685947197552543811727116647815405252804851390263345 +6932108260738047486627928786730592183240725916073436786293871274003576099287405402166418657664483313714856186223383473022466647 +3109816045417430083537878210451471667742535838802468841651352736464574926057073048472886864367560530408263557356129708697457730 +5131047990674797494394562345612521219564146779615283892037401029449518064490250135341621616256327661631926681344727899774392825 +9793560539429965156147946048811944830887554283992612919784433839525971829092056423046384752108153956186882584119526879128063725 +4848894438740997315189560856906795393017681147097928684567412396811955257068230623390617279746246419592475583485164533349133972 +9247196347885177565475533963701634576057917406510305607776085697222055528129649096213092034549229625900854393776713393006598014 +0895574299458423626122169092017122327940337399418681543962293182652716019527292869659349372977261887962838657423721599638764034 +8069818312801265892359056947805299813404068292205524125399036272188934298155723014962611157660369164361085013693243392717540968 +1746313469892957608005716135870516847580996237870651257913684437466577959076954626426758903637186057505535214913540381448786350 +0826130380070802481888865468100577235559992056039751752075537240976306665243883422413804381659448597324700429367218879041686316 +8783969310605138136810926575209117557352903553802462523920408350875226887462880478648547408932195087998226093346126517623953622 +3655963588132204550439064715232312197339282921758181601943083078440247650220106399066615678880555199248431536315260442825292972 +2898418712339146920156618115849473564647025888554958680753487875973131958912468912448106432964749351366422577093050105438712807 +1852498129286540733225557917645869957332090278845330056842464816843083325044360873406582227952116884337704935341689726805650922 +3976284069253193914079828201683985526108157492968673193265258027640406409120387997580216531505549000204500196895696438933985023 +7344227386409800578949975082890327124630045606308364332447462178940410322375677488036092734345158152279018751575711953710675611 +1365860719506143953539467521806358327791893587420884033512557356449628421974197081525171097681451739962654458105945042338824044 +7339951041651291336434788317295707347631976285149811926440821662705811682404836418364754712250487175296474328282212659994274238 +0212161973979980092408896174977830808566631947896643996545492647236993898876722578200269918604906863319277003087190525085408577 +4872695328272197013637039406318385585335721591609046713028487734545081008963689929406578869624586103965589484238888590831714070 +3093781382852235095749474972285903266360882836791963595009076006750220113661602476802687021999748113402446232401462878936110934 +6435034693320558902105688910308538965196623309949466683973971294957981775446228096356658904951613108961843850602970532678592796 +9900412913465232057825708257994535036127184304885007026505543104350741668197223958911527343764741682299736861152999772201279599 +0514985184283479308437214277006973884479296109534451984897036545588323471166529442587769709637302971344236763633165248653048272 +2548677505362732783025625571327094753804197898599649990120724885114160782630110803234921618274764179324553264262464188071111602 +1490102219572319216164107062405778780633273426833137366869390517112371874174117264586848123441331500645236212846316718716883477 +6374557134532066647778099833443353940716636987739316642710720220881882944339283639205849477871629369917099561362221287484469397 +5537933700604880504714026554029166587597577826204845542181354963551493735767539318268321221914383282788245529093887530478824907 +4539736948571324006868573970323898689460869616752573795437663232396854974783326373421982301397494963660135114656842200560632231 +5417628879269942844019831356484605118567348015685889718110291011794547506306795984565230200245047127434730978214554859175571386 +5244295129306313578981901753289283565322938724988065200258490956019375699629296714989839545066652554067148655850643910689520282 +2971836240187409511885225271973597558472371014435493899110877817281998758924278138897958733963027723716372919312791817550414257 +3718036377547310037822620295119160273747183347935451508155041258644862468137971759991204189248362267356043409939280563151645203 +2059403653814048643066476915696010105560937121353588493219472721117585709084500577027269061641086537899045745026346755806963182 +4321247098580397470005864122314116779988792921164422171095663365610591552259489098505982208555327107941284015766148898178511334 +4129075760977557106793231123557041651779523250483585239932004312719534628408382643294636229121610127684366632908893383504173184 +2529072874533404417364878145806482135038499775952105421793815591456627903830117948809720387389717200574623969207051170720940841 +8319746694698707336604137199806790228573013581807699678418425604568242103303802544153333753105945256725487631110225791990763711 +3880160358558853965367146276153313624158725831047412244380280879089394407413109649993209520904697096729360273647396579418687376 +2091906829462709596208568404663223009844823381264480008433097662223468678702122339891481765380241818468489069582210313140199504 +7634167069455911451763369770809802453049016134384625283172635485682336911180471943157282346432504400241673963332048163371168819 +6945528084709716553738011980160563280642542751055915815221655609127228922438332498633425574184424076324567457335614537707491118 +6855985221224698806898182154405013337026345633363958698774698699505064139860586968819626692305575161911369355200549282693239574 +9661995161189339321012455875057211575581587182476156883528607442238967236926672903199283225630412167629967841892682077069456544 +5556977126081270383421853880021926196747677623279360503448724398127284807443362608046943892392030345708092629807650879718315338 +2008556770184273576766805643194174770708191017610672185545130733621276522353464775534219302741371801405182864900478774809576537 +3865082900785088869003731961311138273432940384506242628178208362272250891137755623037615929981020628600026015312604846649871045 +1810271541153370013506194385701474067688519311563701121092950023460871685900667973434789085894353689769155959515852286976707125 +9220855281244525680311255117572398240118776083069705246489217127641378521494501271090294888487837045515780398860147999941580644 +7341485956164711767365322167960284966203049807458284565556017984048646824943506319395879136232290301571995491168074801397213067 +5371405547126368328180403806382396944697802094247261778722922120527093199887586224288144259646007747572032786762234373440343768 +9641998676406552363362334096338343938226513712459780071504106769464358782102176567281764333076640338559714360625098972864983029 +5780224793125175832096440772460219675603258721147649221639684097269727175225573293185628479999177881598175668238919939024400140 +5871315749737173437146622211284749783435729572078139529891438562616324259909214610231102730312223921245301659630109254504380334 +6697637358599635782137313649853967748604644159075078379860391872979880640358744386814475406235163749464766557762029498100317344 +9260432917913444420430985635427292687992391436924828756222785655924726309271829922076362876173530631675597755661167920958784469 +8008272163146127952220122593079694523591964786341879588418492633364085713190449203278430539536800444410958318893570820694596913 +5865771428552651226808077349460469511350507824410126508014924137043903316541107301501243645730254790431504727160432598689746741 +0797866504326277547835398517451743711623314102885283925539211029404096358855485908679412360891989483761400122402649553985042303 +6315338269917337190802305575034003552890761126872174385267928282692687307003769845400327640284655468895977857113820663582843212 +5883143761391732179613778683464000760776952507578651405618216504779945736760856433526347237980076786027307666785673805176780505 +2608019170724255979187609288672429113478903050968752935506997075853793110328285209939065370268834945095553185289660445134585271 +5934178091636548622002305410563118241322636012045119794891759960583808782939464117247149044590174301040963274743429124451974081 +9822212841254646950102433569804383682318140936706127266382165162763534213426673482380144302560262856995513292329891211427952322 +4939074943058922639133209628604240951813644498408022254626546671648111368884617471264005460319454857408802054462873323814596304 +3567851550694955955743156890344607975932611999373517821856035756405925438219730313496771878294219292907299376903706456079314724 +4355891716120089571168867921156570013309057339164893294310027619061553640293850173132141828093723591781265620877693908024633891 +3432150765823697928455051347308237232788126878764759364421680111689906395962403729413891780224106047993476140159348719814489238 +0234684991575714928121012708547654894568292786765465546202194583822334911755447378931758555122045328265456840620648627956740582 +6213762564459648735386518381734716590458385722741140951208497160837103126054522292981022939593107580769282971517719856277138950 +9795430658880195944561326976463238641719113418421451290008508454179670774519228875316092692131997401436104108663905564902246214 +9652373982485787988119955149332007099514930250170109206849474570844779657182243727422415156736246473756296181666403081597210612 +6363221919442807657179269261813196315614323943076172547080772762082497876523976444410926957078802241443845971487934887307740781 +8442538716705496485993660458983193034639571775952652456130115624584796446966906201307091498198760859970990751619599499849168922 +7540755071266549940783200519568012630337075342163311989445430751720634069245959298519839175642543857453232073106471321794307960 +1699245059009156264426959609151370644710641713396754020166970217700932248280718651485571836633670709702375632170998942628345680 +0816396190588432801059693946646313280382672711087244012208759747817406868034578850546787526313820913242493648577159472787123381 +0759322746357530465987827894614798343895157467747058744481984104257507440485290934854164189380366013210043776529824930881371048 +6385658080000822018084506735695449199723297410443658699992360702264595362859622022028092450523165950738306668328519968101921178 +9290969633149036611961423129438616262578501454017560337962795366936304454531175225394994520495848281893733418969823834849646325 +4866905011511861701376543806753349151579844197278285044705460570867283644779510401658626041128258372474841328027560368075591684 +4097340967374048459759351247293757377219970623863320131555594933368226711740987383236414710324945367020743532195606500683770435 +8777289231702354648686196142357855572861820397757772835817870476148855333166772599103631182225390962911857675993162562793056451 +1643701292272316612682496492137369576459996136135417810378930005125534965897227946840522877611416279622753205256995171831362038 +0366738891350724663780781742018384952618867530073606447030185424566647732433327301660674858875240792034393577096349757765316310 +8289591408206074373434407333542355057011414872348953466323958988208406535831896578204946085981588003478321397316316325936083627 +9704718565559121039339280928457498417206123777131839564784077774260566019404908403357317188872113841441577298919813210986548633 +9293886303171570837540716535349361584600602170148405662530496205079833641395026663688854207586823003823193883976384903470625532 +1727675041270249998973772127828169098733155135266069947499638944765750528948036161984830730668217006998355034769138526948886812 +0092789759212121413625842309085282315688319684507180858726957280563112424218015623004324904038907228951254860936129174811193610 +7248502251808372373101361237612617365642190393287142805878594705588056326330546884029678052177636399750149538893582015648656691 +3367150919108114535725714981888781359653934702237037159199343298241562681613327166427987327289684634196866794561168570101129576 +3376393226054192039386172081865991731275655717454440752850294664224843539838937920883445641690602345087514795742186569107517414 +3623971382393048718491241619316545941650237621831224965329865224908967972818626887372855949653336237529478398264567160685068577 +4052813893095863733367924664141451148648087387776942749014403408537141959668060633169208644494300366994545481397333067992909358 +0667669402093903159518564734675653958529086910570255872008713355774046040785518290333034404789497402572158389475386895486868259 +7468963766629011848413788837165962853506507117471181320045844652746289086413801588758664510368868946415159144280007188633881726 +7244685543653493127669292847190750279258471265994953355225441242455255911176450067019362851931161106818923851255323971731656588 +5234890473800747514454607303289634698091529945347698407177688996229422965619449293992367330399443066079751703944482713291326343 +6459044264277571164204689989304404957960666476059760392612837445978755062781098058819661144475485054149528425812865761888665230 +4335062014272927898970122302854052906295227383275313276798239957308395793035503825814636802802824167294443473540831595897555019 +7272548964073946058176039524239703608785612474858328053422538963447385787828187985994849316214926394617015487472147004996421715 +5996250931763812814551030631847429127490485716717613966608339966170653265360723312151608339298980087240888475983661731432872181 +2297536852364764322872184823063403907099469015512172454623275616030363118664676670421610327708607480232788374190290021972622793 +6341746181410744070773088966325798253704849845010112635545932402181743821794042899160348845322610793015131752714444557452077603 +0974929188283489944704823132680006629007254931129889666199080416689925517182595848963795936091170595822025233402048475030835352 +6345563891720858751435127759306670956633219054037703933137927110019397801896389210768937954353107771885376564624836527498565777 +7406605189056950050564580620487220225704555865872099535962165458171814654057503675391062376917680202395298499820248250031013118 +9646002186451042668001138188414741520592517338749162144757726571293411846579111672780359450504540363461450479439757376293399965 +5620887367929571798109873216776069586763980872092222280624201174614361433514239488022226011723867237579885237366697226018636145 +7658816676965243906096876039909445364177324988679848256414790985301032302828151276736018655718069421805321528401722943260148087 +3504531887253350368429518151187892137924735559724530027941664978322897818820489478004391185810494126054267658700325358193119630 +6476950573687760010199531144942136546086284325150545828401223666989902218958065517474495615521528611225439578736181496123291235 +8141273699960775743878136458997315091877968088833966155435545803714212437024032432947661397098373986850161208575249088483734667 +7526564869781879172205819920641629162261070844597301726270751056013595355206013946667713217060767795442616244184317275433955181 +6807778755059399042252363801724898729347456888637350665052001794960468288523356956423878794195125458046129085019792922364372832 +9606605635909164393346308754559022246397895156754911940817409436883533684563312856180585298884265000503280045621922703129043223 +1353282688124152832117904166784884443448057703427122162928095824570260033806131301099883014390489674198929314751517488130229287 +3941712694041861559776002423763440625536805158907454771393762216105225325673929237051966211204216493810015246774275044495594616 +3694132772158289887311123798026659094603586436166517150338257882694213224057803554027693325965893134619716724281180373864668897 +0965421930380580362871564338408316626579345707567981028006568747619015807088576948951469852219385041334895298633260796214042764 +4365061209195177145333592267894941789748549729388820653503884365772532158665492262305741107905299439181875779535940222413616201 +7002692668339856374514202085788525299569973429859432424389034479629705876996023622182434169328690891870930429505413389490533136 +4295933444463189433951574721728951160043627069745406122977006157620687110134601604174649175489421916452229565760756130297102064 +1960368933597422050626608328849588309269037647433491188814133197810733941601294848194060829957660144812059760171213031036827489 +5163673581923411040228773859014781426438397455478270422462230943695383387244412044104565525899545629313341781559828594738866880 +9340062964014870509227099698887936675774112546104074153547361150335199188541340855227153532746778775047019132401967059701871242 +3486224254115155530038741441994666898680466167832566881940775772365423817200581937154559007068187829334117784945473428671685916 +6007443619236946702946484250753989318115938165448366527641686620981129998037010871500824063178061129082786722904909804168031283 +5077049104333703032159256335893142225775472800145797927383645074150506549583979834771372332786212436563421519736302401896553214 +9328253090460566002578048864828230612968529907510194421722878024618561333479693530779323192843841998003032594026649617397117033 +6902940885468724446328665796554679901999854957523032465138455983570952184108442966984143957137952183112915545991326476857170678 +1147940566264959316051371696041947904637481710293264721589819021588178200143221071704512368347198312678908673679523259812561827 +1664627873674389755627490418493705966611982979380988314739579220409030725477944088566536789756067951060713524593551031845158984 +3598081566704365022254984411009080907751257030857437625615151390400729972784657938585703465777027605245686537246903748184890274 +3743523412032108137414706768919475408652123860125336712812291505493576913938367683595349130153523703056997225766687176144402340 +0831940301479210349558229457276992358900461281585291044594812536347699257011182959431429011534075284989504481274394019519138849 +5294182590907051056305209676408046536522421503936187377406071503791132907536141109384530248717758304915645762984125835020638228 +2925263782209751558565689180869492270244285946019041108330056058905348740666223484108200048676063426609063511425234359377856803 +5066811579460770402822516077692569897555102184861090248580603480179709451447581598869819018321983813091599472628576647298283510 +6074763977940235151776420120127969582510026900568916108091774893522283047922950769687105424389690185426520673414305707753060144 +1829809239775426957360751626081187328513913192137296926783947378382044283754109795780048763911203026361961183033933012273700578 +6256148707335785289325167505290076615535370263567053348838880542975561767345020465640090418173271179915907484593965695502745441 +2416400711412329501943051474827325196149637971289654356289167838113336024091716837472756809029018726324544210527893716549434408 +2077670900805210878733285491095692143052856294682669529394939361462198474814099442765549871744436963471806447226559851872954947 +9082929602325709346337243734095265641562540772131425161193766826203999287761238275537747088073534676484013517045928815906520421 +5721840157618684926859754448303249283214882375060250302441861554867804071849460117284782240184817225436567120551507130457965054 +3640323862784940017360312940091490694469989530945043763136591022334344797040629558445856236949010602808244365211211804494530091 +6701192226260174575156745632984043284760428280836716963199179332091776863302070789290228857289693019520852953529236479717683160 +7117865720410815041230240613271283107726446255341876904224653305719269008887983911844926604802435682901331270611392890319564523 +6692783449614566649026808017316150504276465383033625466313163064737973819404507236614011588134611220037079763294452798973884759 +0914820111491787540225084826895277018554164459289529667607470230351077932208310255555375301186238522549530796497320559115264320 +5836662613656654780353360816866098001238641884282152191319830259179620144977780872729828947449487560906147944637380292807461055 +6561572680280173994959056296932010485999239763428796091258639640337313710982242786240160248574519338042692530967139936885572234 +2825509566854748807330632140109077953768464116764873006974740017020211637781546586566316927017884251448954820201170637677959988 +0832497323156260043765386414739221132824464852853436205409279663532624738981652928962852787063322728977472723705113895939816648 +3887836720640005567311856937351200913471468555895780989932698604802967755682558413334239668009044766569135994431992317817193615 +9488541867758126379374062288207207762501971013768400508156207999323493728384215408284344930017408986904527951285518045057193770 +1193456086093196883061110691053041347131507623254475732487481683124957212918787096719880296652189465479161772651216902388161935 +9023602670135872848924155417498176225830627494225163432154996996459879170929691453342113731711303819248475466023815067381174572 +2080493294756115922317931111866355542145263306791051695700043555158422272380979294286809277064163627702612725392431921737967789 +5506584820111354803017904304944193794950330337626863953844116710622843699200721933361001889997152111163384731679702997569228667 +4453953407265344555661480951607569587119336749659238396970668832430048807500071385571032957421002113212240607304287845329689873 +9400568404300352678168177542306869394633571699501805048687025644577243527012594817839631515308187464636399822139186673563109194 +0383996386779379889997394025888104236940479474160100383602505977089098334042031033748699121508022116963448387393566265472288965 +0847531693445140037287515174964594859379085364324835476807557964238229721804605800683718605401718393447954937423707976551995646 +8901279644569224520056558762822223149315014455057454435980383798016264269841766056870811144787284148299586773970705052525539895 +0256120889703056267161520334600912740468642740690884187820554569479971860126461193414499951106260376564440623032634236822171234 +9700910582776001785061958742334821043319568168457980526939228569330951842352229610659307009566518307187068255993195333178816924 +8084009439471986482548298108494644279210299285939700414080731141355145274821687079465639330167799278714667429384500519830074150 +7502762255088959326392982513450122047410180131292434743496089993750128169949631240918611287380175422097126458912263961350683605 +9825024574318602570230904976205086057242856859840660113304499069379218541308171998260227484149937812807157987838492362751492073 +5236013621279011602439272005713397632062733928777510296548649421999353087528124960783236699168282936749555076309059950538448044 +6316174264399599155501461815935271957641181470379962431535756895151732821144407472754664303419949275610161730902538833181412620 +4057262138394989003315257406621046570918156102356055537411042166653273066886196141130782850554134831168704085162214611295749827 +1728494807516097505065593608124398864640297391491113562997206711322054216710834884111534067711304959653237337706486015754839628 +5402941401919360128506367871554679221289967323748328989926516006333132440966780154068277893259475878724270654107817685397502294 +5331749302510962305291613004333202326722920832094875206002590142911609625146433305314091631233538797653208605364527924132244946 +6532771118816432093996083328883576769019322364788699903259151365484388584381218032915424365355589731770063912720956702509258388 +3882789530639893098899335537330679624810372563783381211488557543067129370526059579061942074775015254040779746533167078485769900 +3805020437690986060459807837031266916198079638277454221416718947378635541609908151334933790442634344916435987118766221380030868 +1492873283652410067454455812350782971829174602968575413579966330593167559586103530053302924740897504468176712345572924410751989 +1402681422012394852214397745855323854981769611609240399129690457712558487768725787104039715649959552365787249656873757201153712 +7034615208236270834171130441362610841044504351389336191409989539270321328989800821619371048375553928712514470949748975085093770 +4290878195796201049040671016672715493368576254144716365945552067883559930088219118750514982822693100430367769784775351909372687 +4911033872681823931983855759969195568792322301570737362071175550474999508803728901587248248809147697357896852570255543589500900 +1706971004869558605860088419347382200967993695904618287963224847080176675951145483137675636512072408162582506993137174472960694 +5573663486380813348973168761749599259962367833751791115712074445847171992391927789752756447549332777671869328971664763925290830 +3549115232652328982096333522858191070453865857146234126898896140406643330756492575988127393599824952457257137615690188707592890 +1525981890012719417710592274397314151142262744434946961995671692595224961001445969622314797497698000523423663899015096913051877 +5058249696800337974138653960205886721218275906496709288754537792787671454609339328245451684579097878838116584542322616579053187 +8462720851869420776547815997374420654356793047547706068426425982466357700923014078111329124212863212450480733486009748682342594 +8687099289779458763026259506520765785275805315714163404563447144528065795533004752634001703743154416316364697795177031670571395 +7862389886797621228571167458335190181377137309906389411269247283889070732381228453233824760431622453252798014077600435024553202 +9884611862481302466807813924732912660462964877829202613446787892509006450191623119538981968974508042576824347741032177030382425 +3275371888164672951716608075300420614160898739974183834737152799222716640347298255880801705875381815950138367820787575063612645 +9756093031018363423853254508819810837905492342390007409090686184553436757595124339753724654015162734521998021981012802729263275 +3427616385168365599989752741868814273183927471411894804297652680678085002922251517971806076560284843234235639397961184180625624 +1126099034992853577542894567722591646934072315106827450408752081909824273574814734547622072209980280969251694858199479989346866 +8324744163428589301098203537736106384602289116499145216551769186060030444438336222899526023763743834415207190662514180019708518 +9860319477002084962218177584666884101089885263708103715532250072465740590415811526551340778700948662719303811079801949032990730 +9442895797928578191430950680716135775881506952099465712614129706777971670822951940768332168877767587109595551931372470789023062 +9676406861977772053008009279919036888396717196766804819265649832410502497794545528956267262992497398344695416980354321065043902 +3430450368036376703613028439245508507778168871582511449294099014869582746432398446542812126358198966694091267937409209466063739 +3928978146389085853115437005111689057451518521401216175319263947818787743509560003599577334399518674836615112723186476720359894 +4447676769726440097553180938842032872197723469668279942479093598413513198335476789357764400869873866217837103053672831807359964 +9438439139166494727711797686854076602382501450775000543946895474100528401256910882752544466058353451178988269998012104071279388 +6796506900519353467866660536525382267232333410870940350598284296015115345501330091034294987205096937204489666809908368374224287 +2156069024262468587697645236488193119228612123037868983120993219205723939445417381491095019098927648926035392978843470867760614 +7821524721406495642677263766762922840188657758664683038649201210659149304382137313140079079190066786903508941027377579459577045 +2846438208088991099137843881611431007996513901288343001595074297103385844197112994321057890476648269334831505632974358825733750 +1231729513446989718094794150861322831069880594276720936717092445853573977523050237490193119260788535329095088882720971495287598 +6552792304258966749461747651069123750999083956204720301600561922121624443601785058830034234673249878353748610370038504030736047 +4350618220429461920496227812205841572163867546971596032105207072905191109123570652010474485250661135080242831754570708916683989 +3648963905128198988374985403745574043149005801382728738825016196793544318399117519577715497903566248991289200713376389062963733 +9079224255192675318799042558252967439629275533715026478752109034505102371733784051667760885774671501306079109448761994271014212 +1512159183483513720286072997122368280567403105173759926916954869853277451339001028186617037332009159449259382498277868408842981 +5560323178755830694146107056035341050323807669577457330458808700666012312546070466530820114714620579338594279601664840585122075 +1503852393417110615057020185323349829356846940778480270527833679702648854699330937650632301666517518142397830288536621388668145 +3710500442232649775196536006291799614616427004022039024161898799546077671371127704078583325133930080437162856065176543455363091 +9667028302235222877392627005222715604718844825560371250888081705125248380997161235203510947690674677658525524540413177350345055 +1651124581699981562124791312861972388295643691956825406823017359302629847041679857625842731954319702741028762561310737659882575 +7234973146912955319568057648697281752581304980681832088301794847222733894976160526360976917273669474157112988773823160903919773 +7236452528456462658486826400719408745377253190375111579049732814880300278553197889206203166333133501206064778109147665779882391 +7682375338032730875672864027350071637472437979382544031201751667224513438710165103834022186623616196654886225240056588996350179 +0853897451837131243025169955161135908558413022976148659863288496875873894764829106676107357256982339087954980489000376844916919 +8360603574420141848805577433559168689130304284043047944444385048148193572183817853604188280975779635124804366554607129505815592 +5620546941760373757081442569363535705009166704002199788324439031196454334794954999272772623824166956059123197810751221134756232 +8439866566091270892187388961159863817415914847036125600694319372852752155914048990490541073567566380258061226260248539927574844 +3029318850407273236073024405659942071591211229297986931569472429177060698779317028894343293006454758184946754805713329630249820 +2687678405796666588388227397973494711846620655765600330508932225839545844068520955016865194693435829894249625192750034081363961 +0826642719445444861688478619229100303043863369218122546764063288271554588608667295145429271395132923307142550915748046259216532 +0468573883399305338589054269280928735368128284996899601120318736730996079626128417354949909634397399033464297120851931573621836 +3410567460401768661525344506338831729399382001213762232285178082217011433200810491330007874531405259479829710109345021089321397 +4652171379208756546308171186440327278368902304830062446989533198226580273246735128676055302820200408293662986737676930578153746 +2150739333645551554065559172680067798805779396460259578671413082802669062176930977818789038399646027209326563780688854879437364 +5947149323195596075203471654735880096964253215673415065855952449622950653788545812330777657299303345667765138612882163698466977 +2308935140239438613941108953213493640813697893782475635075053824082277617694735156671731366529539638010664196515846143425205110 +8579861845970706058547725114635120976593327222009873742050798971923964666256857891243866172081317410728590091574945331275284201 +6824605749924142731964725200957618055202505975317938345133949930752079669713432776367891693447902213677009365812998801001720273 +9233059182009261025428591058709759978362385968915457411652886788541048883340218037203772730667179361924214082717915090369829831 +2313518106392278036401430877932921941234750269539064717318812526416633477902124303231275087598527988161235948020286385526463743 +3838476810771838510370563675061364260967203242139687527568417240328961372800287193798489828795160993501686735937478711295496291 +1519858055532723186083206022574209495517859134830555261865370844903580200999194798031588523243896559267197627522165919682799739 +9373974604821907639636032716761671130085088834596447375314712571767917652624045445822422980937123575808022402072804691330745798 +1179538903767384713618264919378659303577920743409425206952169277638505227558099310711226481165660388253070855325241107221381882 +1789290788643749161623308260296145948321831584631161915478987224126348775962249568727630631570400533850489485693171250304745466 +6433974998050768158826396562741859990471614366251286876757878787725794466582791436968613297526831824776182286993388108679611273 +6110623308937612049537824555231489221014173246666865428966654501963681680683054782880195354576356714786682961340119163196471017 +7065396261977852039518801715732959486507529242741135438482938793950709913003256496248997903954928882444470989824659787821204683 +9252764834270969572461432198412629509667322770306467304657216423067070489844755342237360066917220335677138645076622463524222641 +2863822477918798068216749348395676325852006154849615936810666235080045937062299799315963185719888255745874252084645488043646414 +7004033498629565919058730350299817723186869899372617107591275993421694639356397475575742082851089041949288607800654160534708316 +4190299271083535674187523239133194914262331993315335908926599556351517813335151871562105201268635100775530492262605262924734429 +9412474523205479108160235037378746252342301320435142535429384588026050874475919119800213524490595720796240406073642925771858154 +2913084106745765525951041794109785827673640207212541754087108553407118981892598453634040675583256537303820724542193091642590800 +6534476025728340988278166932729090239881218889572681282009000341783806869952125287383734393447529192404697677486779298438469322 +2829214412991708861478313576349293840205174852775802339069812401323737815710652651384534932732051408295532745074816386764349267 +7099631393624915897100017625766879968866232677413779562258136847735611193368355358377164844416154018729080207420593111993431521 +9679117084184583456113996444214687961259734088670459741393316532022602071869803421115792325270648228861169427886111180946003583 +9793622141632553469977231970499144123659775068372909299552862631976151882730222347477076919808461706462627308819362671717147651 +8286017842303147776294340423265613507963077365773181170674484922475010476392279295555883161702991597407378465228254838715678283 +7139981166981912432328248939664037584747665029041762831645786266577343138070260044935844528507385472563673503510303215480074901 +6952789894317576895242795097137355300336216988450939924501915977563360873810170551299455217264567889345472118057178061331257728 +1694193485067381935776175208128377412895577843695434073687471815717642677045054952476930977756497488385115713913555764776948263 +2231234433522295956531798985522781927112508008839457523109337600193193052076325806292222155384767900607744680674492935529295805 +7875818939048046279625548208487559794494231792862975497567734812838016356460114239167085114060525364762528245992233228644094327 +5021327908105698731349894818836329218148701613108177279681910593746940136925470307141167410364253643713010143547929247739518061 +9070977175221547245640857484222877237472955055880739878012461158071643167060432840330043982130511367475881160956788509827952621 +3413847694252380420294340031416724229995257917239755969134460592475590887547733435481300484633477930736275363875263891668995643 +6581430213176248276716948697514387283511867935196454364984608140452221982505162813367283133818168866214263555481101556955727420 +0078360325719735185976311476426092189177189796469613813653608015870137963763116020948507804890688620493269983909621885486957143 +5618073667256477516793467657312199638612399475873899685969826531992817695372605952175192740133201709617218497592077143464993595 +9686528263740598514413702973825670801929614448327903726180967797157579946183718568106283503561970266617924646393019216381949796 +7239480687825534075396618625255444191589146664116441837547061247242846107878949219756732241373337771333630723471570758001480280 +2126415066562760335451168640052256439621013367706107505889219639609857343988288214828754545448713238798703235273922756046005812 +0281498418252644767093639973704165385252675952065796869316299823450944375075913221341612192647317518382010547568017829174497579 +1057968057525848488438423398887379257534282986505898261044295544509349907137424691109525427205047296410961152317113953498675027 +8037990675600906293845109738467043233236225877257801109348395466430705164075214123065322814006154190811607337254708305938085250 +7210750787555252394437778367913891376849871377061775971887348544361550409572803722815881181222850658661431069707417398802536368 +3948215469610248733031315237243553696794016591253408216711885250561262516305255457076505242344244633141474145222490778655694723 +6617637025342831507631846476937783337058482960397621355078541340935495458638237325584632545957068938913331687735591931648307374 +8523372206558862816505119548118888594916440410353297374883874035661877591624592793870237386066600870369847319996357899821848884 +2706665246531670922861049993051412169291896643639078882262700663889473964251112689202969447822598990637782956377227324903867608 +4848299459820567898610921948177867341511272056149848749209193175952272681698383112955078617288403697439760095671946801857673079 +1356542472568516852079785586485413072353853487441642185000260635680413178133480855130718255056119475830508178140915142512386038 +6695877448601376560251771173382757011161242171039619918232438391997384640483554361477105518555170292200647684746099630559477876 +3728050473071484393708670159181207753885192509059591605951929761162380613874243004953769191772521075466929499282741107419333117 +7606734018487546777450475520310398974869459655169606480072758933265326533542081878074199816338268783853568241202511239916973258 +1633835831994903654810299535109070687398984404650301845605445387393310102422388713024846802629758902868815270339924811537344859 +8928571046801342106880818013492293682607259787490363620233716285093685649713330924728743681650501355104561967670012038426873625 +9125730098479474089046067867529096250921613432172766348896492595710398358975815806788579245423344223729093128465048555061302778 +3282236766431701005576011421891586267683372954054171332350103840612998244535378427127331925639537865313201470923709392166490250 +0060953065190643840375181038485605085116833098863859869877736596835368776079089936760138916464834786551291455693169964704950417 +7199761056487159527818318808980388404160177710661891595418127883387542011797165555082211982772990941128288012248134317959535274 +3151702008500412396300814816932022272142827209737059555958537947102106965289850740478085447869942031637107833939492398930644696 +5466978918720768258413199259613003866496795654264247159666752558539556973496507886748391341566752558990139978324598436936793928 +7875629494705039695595741518975797423129800053333398650746169290789301147238968164288511049704954808998463415048261417058627843 +3675014761845381637345570687526385375856012330298649384510964329070850231207976330807099687112778366295062201878036276787748707 +4186642715813159153197640948334883431130155154184492235081536887094317988019087470651733545171824399201735161775652002448517265 +4661100517204949080442753518341612732523752379970048081237708561434446413436675964518402865361477156495973338379138615279891674 +3104047228288563492721376497746751756302186258285396146017997376211045579689813619459200950120798719227167890408329425880640799 +8361120737152440781721610389840480574105404073477545436637229579684528270718993412816918987324414005130598864358095704485655816 +2635022304411690520549036888248912012839104866750669849182202670549996591599148236127250813538958602763034624939004690945598644 +7161693686069093177957527866565203934639363610988615731752113714098234889877144865056494208717095238090181742851725619039913679 +7926731919437873880030756176008264549114452715998481120029937345677673326677605995613852123446385084312717926880869469763207023 +2138495852046091769881664603146562927499752742224823433380060312750362684368906102817542844818392822621059272739767561472497448 +3487585927757204836571198808541456058695288628326581580540815649602826692062251298954681717253220499784757374087955966951533070 +4467286406109149470022526422047665420981641926169730080064026643952810388751979407214952628713107539097869076094466606508205808 +0329251645328585224993793572099403208481085245076448698022757086335438075955415420248533451214205820043135499362946801092902796 +0976117055234786919615481110380099853162437989837673156044051970682580784609511428623279845514623277521398030431160849037450740 +8697358783561542570626868871304762629606530282055602229180460161462702998318108546783149370269213126706148977779508293460733304 +5685401232626893164468498498310349947377219975132662081047738432680920912627837266028376099158214591714411685177672935669560017 +1991014855525662209159171891334726717439346791115150429082237201563017187579488089876803702248258879215762021955798945536843228 +3358779497231258973228903279307787602968091467986183211284685640196892409994105852728310998830904989480714242762651865855421586 +5348281152141939663360571564939207399533427205490276937846050282692131755481373457167966144301656498283646918663467663563230116 +0442219576879355438661813222738756969322895770177941048443578213219939881424295242319200710244313116242014329915750902609753298 +6880215129386746871145799105266743824027120004725079746962555401718867449525806260178086747751584880447035314395435465651034570 +5590819486053989106201536213480377379276227468376756122798396628016810991489235173054754920663273388709967201645610835786499004 +9939124283008389379680409146709896607754411605235108861950292912142332714272148567105156028757875660670111321491114463297009230 +2097548826892083113543893580713808393001576983724545670281722041343097928800752617116438879952016563025042978404320211936405203 +6082731184886898638758782020161455765775544117220425974704422223842521161418889571069655363278462600259872240060382048674290190 +2889620681353481574769346958935531281292735580442822115141047535414968598234054161580569343948696497621906027825367798221210232 +0975609392326975398368477771498401808897612300017559800926232766502316691201583773217274029458304062931758320244725915302928993 +2656060793106179140097207671393073878144623719159743370358884637219392258120064605880204498430784571518779561821815030688040645 +5261532754299991261842655852754335348627643855237017342819255089240352732014055394202970458645445920979909498339363437891353307 +2437642062562451324051185886722830951326647012880268770841964036842838215823451274151497274115272960818748924818830295293023164 +6427726746699539771500247174272715628837777269348318525014112099038948379809971189523179740600410427829712055609005231760641337 +2123549160662241455227582001148252334968910254759170985955722387116765936697038502260200822286774138322324033501219326909463806 +0821193700130194457116090490985590394866182284594574006494500813138155877966705322490599457628280712279954157952750704655060385 +3280391474844631197279842080980749627555722515522776772617462578270115902754706089142103829738218939451926197117850790818482759 +1294311329582945102206118794968530880988413255047180804846373165792608079444603588122636074961065406623564621335429415769062656 +8977139076449153307860216027207885939587708357239425724499000050811065643249463642868728678159848609219920477201880783130814232 +6033248813331250043643381559597258568598550852601730339407737331740193572873459479638405202147132462334764720678461849283231967 +5244706826495860145056785602766589976181173320167438082817365766859974115630195914700440671364272919565408159761417527099133015 +5244648245024168084347025892989203801674105350823195087890804798433093162301085522657402786073312465657131861678950736152896502 +7020998265858326008838932594312667517103689364315007929663379806725932670838335332530002316590322181497959301133051038639381171 +3173858772156448895864459261457987925785493055610741437909826143835126197998316368571824712159171580331713288750535891179500215 +2461289349520625956975133669730507522155477555105599302736241191876700580974296813158063100071375109901417730679844357856593047 +7673877787687493268983420698315905381260558449663590054448103444553449366277579160597491777336624754840213430260072450113764481 +2114017564926717940676432504294232477129802350412122813350795229854770687737407776153546967564248142801354985562159014016660409 +7639567903574424627121988012936196690968952173335045547572877439937623104537739475753166073603089257419976655666993295754279243 +5512010810515840583007802011584440810442039388430026796415104195890917308156926213851922793907826473532250186114497099571038088 +1090443322487112398586196562777035714693242567109619766467418285811180453954969091778067062185341924208029182725946749209676264 +9663462321929487207790152045012463956298339314526151434668136673612071349597053270833334450801647517402244793354108502290993012 +7465047517200443480317342789716535487720038254767800382013521445375492608318405493546080410946096145404154482214436078102318897 +3241343339884810074493047134584332154632775327907375423171218727031001503885369275032870180181698599353279959939076928392091092 +3566208126745756363429701058439360596278930537654250601183842796400352091131220495755538686838579076983412474092980588063067246 +4135280622573899686743659033428766204732217042537180031329938266218979406566474637509065330050474399644477629470645922989131608 +7243489726801527985565622807882110752891684081535249425920277363697188485817835521822988850811873532635201983418016593214492469 +8143017528502801938786431108109170005870927726570176129291655180782741998755590156671788445329312481442984040900285747284336895 +5565100360014106030272353666080827357535103165363399802552515662750922553611500390854921288593734087100792149327574407938377815 +4240318921296779247997230590857031671134516419731819431863267883435154179769593315054293181367248263035923946261410558920492931 +3065344926131172598974832603770803242453631170305261893085619634353617662701712503317939637731383890741691116709803904180847998 +9640635307154881695674450558215122808610162087865073261126743708282066313632270460083280371892143332906784831299555537946109939 +0645039315913940793949968013785570770789031780794104310782057130859521600859343415595288573910289771410236190253921849657190998 +2817116940059523621717397073394274259776453515913607417038665482821641102656240840561075406926383495205691566077252073640483438 +6876828215210014803339974347992473188369840859181281062746399956666218336920552262054357048613213862945863515439475503711072752 +1206178405392180823761458212656703549891855527651389518191357531805984535259972031066177734136716154598488260571189715523538978 +4990265766954047391630449528696946350646833442858182431131915610086870866589089866607870181211091256487223226710174280311854030 +6161821498017905298626518871081902325899313379230902725132275803649959728622670760552023317532682229232464170889758455581169141 +2416184842590677978922625659052895162548385411298782741233394356631320019382233371212037232115448059660819419247622831630454503 +8975030577721689746153882576268854827896373568358085985637110827886139292888578025729952622899410488518168089967179203288999038 +0077221628423351964468120440528719933928163566191359047160562478933063353123985611799118577768774930246047770557959247998776831 +1721135782157358288734205102478591394569267133297228431858377971925993589071779657700502192679741782178657102555324869244446359 +7869917278483084269980405860197759252589350638010408763257025389708116328761797581159680452123249311643134587279719495772936617 +8130672462738712860454774853313086658312258840823449860002088944658955778609509469061098214154416089413962879973327200377762688 +8704019840879290058261002736123709853095591291334597972059260229183193853550672628813904508834589255323455601476677738648315934 +3234707347670711573887321944356094703845291499731243740116195333742959154260950396149564697796845304852992191344817095433298019 +4556001053283765367945967785584518418747873282740299536406596053579536242035383267115157980628785476579854884310834210027387315 +3952399660098012461635956108592408478331533765191761940733106633178973207757390760027945616808844645166556348572120907508405281 +9620717181599113391181876705169833156639227504083941308593882555592005786644351550764764616222603704206370090556397818828816448 +1082150428965180471148236660642979232846204673076692680655912004876027643170213363695294926023526278443457817518033465638673749 +5350172689969152071949149368789531895369178652578561324968877842847433493285130074155790255799028895045897756190837543509860775 +8482306198938020753154634340975595193686061652677880586337892305604321362120530299145893988789835870281018804163394448979511499 +7488870885383052769468046156937025798795341295237597526911841052145121608390932943180983640617731865473642038426653712735002335 +9011612227214170561522052833657180847394409367216727282193072678838450183604374539578667642858779645471672950276478435834423943 +0042627888400396166577808028511674909710254329416152368725303977021338640330576476481308582201268866056584880471857573435293178 +9268879578435020158729300163317051184609265956929202720052876516659404370185319984314639348524347471621091689443144604058655054 +5852357719309697687694316961668030847489545691322959818739069805501234210837740692288860145679956618287254425865421435945272722 +7947183003454715593442541393266684657719476151125892217474788448864376694397034056195614730803146207684810002655419508268188406 +0212392708824827732017576889397138488570370042723523923833128481673477500325568912519958461261978143669880070457987816169424188 +1965090700335288348865794109082688653306331467742286624350702924857750689705564771477934006408695884251431946386364556639034176 +6475202723487206164676214732339055881658522754087991356304799053722702797567004357975452415295767243300651018833595864642447417 +4933751846153559955643592816488612142322342560545913045826692544542626150928640726729092043333739064265750508139974673275834713 +2427576527067163076789990338481516933446902541780767233056948866977560689363984682741766823172377515643032993191346658956321461 +7240324801931427740710853207105854320415521035336023346171992089619399728628105888007254180361185035803733126321368733856723085 +1503702321045090573654976317248889826950138261936742489712456263150062608622387827772363799121813242734818379753299053724286228 +5076095412377646585652542244868299631686826272014334350505998081702990345584728215380800687580035780236658004071510394469994071 +9933250676927268802066614172669848689165401909644815070604643321993975291059605403370117318954869373816750869659405704139511279 +1490487519262534395623800813247168885091994099496258142463552140723866858712657283749576144012562970919330051773400199590563572 +8265606609996094143464919138293434901647769730550988047626306005465014764814728223656959001378552960458572921902571777313119856 +0301901698751025073371353819870024706444286250847057963018585025053110220589734085318599463450687736352183212884401020231957087 +4456959908526601850236631528599915374848039675582388183944917813443948321814362879113998039695208714072018715891598727749400184 +7983572039054110543265312946188122716587941443406593149730482849110607363599698686119685785597056173835119438876589991354302327 +0157777305454082280716591110467065838967277498987788761274920512893356487762316080383017175970582934458813655903151031833258038 +6820568297362586686149767416323236376371752928724979488399337093603770184434877480032885947231535650930077949030676059598788698 +8108972963805156390445400882624903314064494634772981427763122632358977184343156109534055951999238879890515883719506599847044143 +5178486334616508289086769240536520705517396788807705586482419714104331671474772634945045486037353039747544411584344166691513962 +8343543694892040940224135766807400138072678909751563547074645700842179499138145819361180694589974178543789544801347334713872045 +9708545131525935820756667264787954116125739691093797584888706242100925330533736446977764101332858478855148268301440155677807429 +8363906279880507853775707450188819239306527160717217937065519073363784713378967477694756526571005707114651691767780955073859818 +5651083308096512159550753176200623584419958971162109603579598590262428369134046173758247656339396228143231420340091898293104843 +4302402395996304257050404831355580727247848379655438560342120245630874884033522016540414633929586663157231554069390318916865498 +2822530065271711681487170629161962249557718383135273443686990757846857063424817696146359531243469019949939521304535035359286967 +4074330918579124906980668220696463605660380759667033381676449310911291881680221895239419422515427861193964219305035700436309668 +3698935243696912496491762756802269973014707849066246048122302860046406677238810686973357352423777172732249145383686882436002629 +2170964033471347644711273905006241361125626168342597916481066003694402986372839549149179850327741026797871619199281396638967050 +3740578777458341247832731137954141104761544067127564384269992513879476807275980026551471675919580563967451050762083496336212971 +7234069436162575416758566662736313504628826119312240011212647893254386199394411434394192850902621006968244175625150393585420756 +0819505443586556219267270131584242241933754418772049304646166400049758424842433111724173775260453475529050201556290830854251309 +8694504279853225298817987700209881068598742151871017058490633074185456825579641586256200310473031416146096103034054724162968499 +1678422417003132262043356103744836118876129032014712571839148276942048121918851516621273265839886300177612597618998212159348375 +2526979362189085413641977979132635792430656327264789820332281372901762624494315668746921549021511066663733746233883697857770994 +0998995661761657851385372991367035240872501467476816162857262794643509591735415040438155899974797001865973911239317650890930815 +0189734426599237495702149267240934080052773268635893422129378135841786547348314154856133018855495625125449892981908291205858083 +1077322089308800545276469380562276682750368988122928137210856581036772006900205486477908581832538965073059726766472615915649559 +7595664231420716081656297104542741477179939201745233207227457362234020070465865805626851389405073254933773440235630530654929440 +4561326689796974026193323140357607026602878702428514108628434286473624453127013395116455824218643518830181373377505009554553648 +5175104916514700448781338224183451234036582197678358641053766734457042147404203339056493172875783393464733980674769043154128903 +9625053130166787806916253108452498110678784874573415439254136465274322837153616892957540974054903367967131773174510386204520192 +4708907023698793329968287054491892003035884220479471761449794976142360667172403302792534515419470234070842851218805657152102504 +5165815294615880424810698811972894519218327570376404414484865603951627216319921021460205245622341179697179353800333764741713858 +3375832459808523959735342163302977206974789134934574309888193442489681770595931407566150654834927560855722717629843449379732533 +0696873250701946777504053721122628669489758761941472800177248115996803185013999823071443413470671100299081264402635064626229095 +8025269049201568533557667082941257090329107268081485134499591411578665111542402290969079825247733361568938034890137999482482426 +8413571137578802277713985161717245739847221863546744280234586017009016274669615970285654793200770322063367655004275873714400764 +0799707402410991916198286689965775278824664119744877317308731118045964149958722032731308079677323234905411311426216785693702443 +4397846375057495792019955808977919911307473873070308655227892822037825751568866737076516050806909779275729314309462325310751161 +5878515311252357335941687228452716020488777361851148660668041257248641752627888839374533734775241618706657533250097255962909759 +7924103963089477116974416014956752830485444377913838660792557425488769129517146048639663168698011387132583363962130759916380103 +8854627858331158799334753893263279859800213644437374816465531701511843970386899153300191498875238863924442400115037208900760391 +6931077305054934679510399257137017692212293765671016977117885559987800112104522229358688690556795828786584220491722127422607686 +1161689224772384204986305992766453244735906304538266466175104130240871760142886069723737054629941552777930420547825827115890782 +1184882323411827620265622192040661772065557335415769691870166844416649885380327832148533301202237904520864333379649271453239928 +2427936123038564183450762355240415926776086706660362490151633725587501086167861442974904906015877742003232322938651897499721504 +7575735828907721001810713381085000197583517056663976862531026160720187808215284714716227545242555746984605470134836970810868686 +7690274030579086519662066594780770842382679015250578750543674207604336237491570959207826549369435808803521562398005006071237827 +1591235570232351829740515082573934645171861807176059621044839781347865804129548447089911918892970375115928184446756254541226685 +2615960213766977626603096139489045595459731295282305420627566186367919926990597793979775395219871115824222384407322704217828184 +7269890378837649922156673232477845852726935996940584696490442582150880559450498906939124023442999694271016368614887466665480687 +2492299648986452580995323839604907135499552296977074591849011346778726414701224990442027943081442825619102646860492552917507521 +1791849743502515995886083914654782544108637930226038487802556564179211481768559160141129157276978288393204371047755590042629364 +2476979194376897909780308753936290021359131405027064825271771828980164040364357335094434041723555965477129434483174161190644661 +8416023154844136801169958073248822091441560069684654659045233353502161929981208692256561137140555110119558013208216615513472071 +8088260724856516409808086622373800856820389920242427841076949364650960501816473316120878023138924750918248626057879988336142195 +8686116426096280530843620941389111299655341997827197141570086674404151699053951971920289530070752071000490045958486626799786432 +6549357623845124371617655875408646869879278421775346320373177073222854384021377119568271293703313232299103659136776571632693838 +6591886804108118051730248844194375009874244915537509136047158661056246282742880964703031154340229700891206754898355101039366979 +7268446901106376070498745112524561027602391892725887518917435705098580461006376837385629028194839014124169131891549609455012171 +2285131899533028794555938374512548939747904214953588004250694538667660139983891321230034509152717542342156366790660293952707870 +7433656872059365457021513060668191880971221287545847083471271690608548455879533083319220177538857519457319889888751900959653068 +9242848205306926609548172769597825608518368367889673409305233019839916718283755877093610440755381268416027885226989117854626319 +8543014996495123544046729990961256012932771211075541381188839964005105738775284593953696325073738652709607173130510895811417458 +1100281056074250163495442070917233405800039282178047058927490789657980634481405379098411400723381012395226453993698124096414583 +1497707784106353163476884094382224594079935684207379351580860836917141337699813295106036949622008098195962892923132082743352822 +0227639803444432864090266134712772762851750164363159986164200332551145986599690734005702172852457747000704439537884708919681464 +3297805927942628853699179019515733067501114704121488080361171310117107236329567894778609250864163044875868270159183178936230020 +5632160100910553597455671868785895773407892040726468337688933116567628214043646404787040937392191211462332257825317687874338725 +2296104873433207092085115555734422123208784998029195153841807872132818719198275132977492788603520666575735728468601968901331765 +8507664661851267844375807200747151263758160914796423476220162799279781135476440706809418288627923653896822426541736985486808537 +8804577755163112469882477676592666556395113447285604375941404675578061757730014398552781158003833295830499727417346600077947853 +5831914326747916706642066586312780276779650520761789440673709492335505292573240738337240722730985752273930258542997960743098240 +0068020053146252182779250786628431011150801984991349353593228652822215156799328870412166924618702218796377703357100331644399678 +8109188902158713215146842638263856459757440491023323555585167449490157093903282451159411558188534514053282926080246371635200972 +1083689238510925211444565358062724034393858074119865447872227462830480815166007221130324188970568012551002420122076677588668537 +8348566408824119448641450955381151117598257925347235252204110951120532544649854733446808547885490496232547849386772180981787081 +4560676276145414580213909440734926632475864478909927595616312861612204269485671554044668485757818928586611911258131650112268582 +6442754729881254198230970045300311192828701003356370115227215491493010569367089158620723231028014912810350816540331381099940995 +2865041663295338237509904105995789610569727200605222728767332785967873952215814407401152907766797309671425581760149058652925760 +4017820912206849712695013871311183547029336150750415340954915312119753021914893470116876327839429907255892388374033828544982922 +6915440546354439943941953788638484102813214756761743923515532313884486004173505190595536349294404018810183152351908266279594331 +2928244470990795144278024903861617956853603900951440210459677120550845092043556188794480813102688176816346489991301120405759270 +1186435866287315352826686449165308561598819563912734578580650689933009781657638259105653726859937343022833693021987646952723005 +8712542566252718904406054272317438859093977264257379132707981296910101101713844533871886378161442772031073848272459192591830784 +1058110999534770164492882010801967465978671453250245066874219221865094533085057504007502380425647847612715483754244102708112443 +1742666667868535721098914454487184166505673192687612783065836658057498575235907333228055857705494275550823062941047285844371213 +4854496575428807093051119901176270909165842784911651361479733219811770760153017103816762799215219103460967340988812865617828044 +1917561105792307812885764646858740340976693913696981626941718101122829669609931847477816007131803415395929587982744622533841587 +9667546614449025826471505952967761841638682554759010436234535597997076544716136876213634826200883843816631177929989595634824997 +9075236896089146108414434690485647098336762127620316996183949539422311636347004018452729032082455756224021955521110115243171929 +7022352504520236787549133408418756692328586206498166126138700604093881377702931259764322041155662852059678496015745189492835117 +7091800587591451755394603236816915477651467511713772374848670866752841854518597403280700140135675063538245814642015579501650431 +3475133641567131461159293976535068676192194831897897611380261311134218099223461446586431446131415308645022873899564559364425282 +3789965382702942084656483383865958903699057640653956948168423022124988840895696969117651267238844593486453639829783832289036588 +1651772364049655351227109534629863148340655489696054950189023140607819232747963074764923819690470465024081626771998338862194589 +8535421442026745607190566634422441543153174467912420591988970152959270609189415618141521453452531554132027545470737271909694248 +4266576066028018898848004953928316732798903447774832407967130237423961865629656870579836679541833228863492849112635373253768730 +9318620813778397458907750295204099889575365028367626038475757355404525059311775107234951920852829456542067713455662881377001712 +9660563316063581065664074432670545816492979485719464785114250983280531864483884025594604925138921571079067953012249664289286107 +4733006686229685087965270923863015490601420365854224908680910247314401508516820945614321593880289181386292705522860502467700146 +9595467875394655716250868523623848001720836247002935008259127820208612390799061964695134992188932480652490964335096936081240148 +1812489955447149372964288932685703059456499125726325849419752698359991222537673326627718506868098338620828381688669635638529284 +2277365318750471576552640984213353147488350298228497735938404810800516562727031985237579073707417430708173600319624539956749453 +8558865515990107426689500711926656694092773040192992037548490908808858324669008603563963750417300374317505710614289622964156217 +2457409596680804630516887336784028949728779145781319560216200606134423822190318186800036885710141555957030479473061434184735920 +9100569805069607595713477651532945125488629510591455102444809609662844669593112401315706794492448332303051585825039449951564890 +2934068975480357331702373570405176114572488785747724819226534222897314789801301206100799457068445592290174980265109976510186964 +6974436264361115896790352170073260547419486352304459996628335808264941225228110927615548124284539746249063361590443246656233810 +2022987197590236256417410387584617910709314684989417286318087666015073752226802244527631727840148104686143554769788772100971964 +3931314203567669848668014170818609543271827823841957939988437792149776879965413983671414699512124596470803895106449229017252962 +1646897058572253319802460817369397398067528887800488520057974357980724071070615260303277654527720835652573050782049698374251316 +0612949136289175984348022991134845911118529208723293255584972808307452158666230105816460375784187988904522880654792018341929152 +6230320383294577758148209875436786980246088588555018398742402423700033755813061790265512976391765915810238317924960933176044915 +6724326703636717615730930803229989144709304487765743249773045992631205387565818549594275877262188712201760168274734486308418567 +9793449160017973876181891146468573134118620184336387033315592292444997748610681437980592831049932965491128002640896459624743912 +4292773888056347657265747923515456543197563688072118373801968694939175467616839381912904118459023550508906592936405878500629369 +0726575398511990054804247570050536297610421442312874646372000460857738421341575986454091454129764640046210072544927787587872609 +6223497359073574286258812426783136238045789247117981734256875260415121593723556039489844073919163685845325053278772517808357882 +4805353931456319684552807410713836603840908704132137938671395582768618313934438378980539779778185823334069713366782891327854771 +6127074671224214243978383308821083055917752075116334068237357481944923919216084154202678626979129283869929664457025576182992309 +9095642603811777857467738034071749226163187428061948529437320054852470403172207781014210493833697169288095448800181203251014035 +4736481344099852996408105951657679269262847981157582390462101509329835494113215114942443801348682136828613264161536188227115466 +4003055863718708771225642500990071025833297545571403747979992978828908605722348616364122656556369792934466258936918430294297508 +0884716108700884651920742851823456839844922966806960079344733214332219603718719129110850475334654765668025057309956984350345154 +4885236271502923816983822161323233481126598003985902081480145736364185121032980985643061033399360057443356862260970037248156868 +9095109506937315402103438551736664529148909771957666146775454439817301965157339466426336556934667643823128218330998524081566484 +9554172468178139926024925040064147501158211448170728837042601105476806693933691272219558658718966118313273471191896511638062325 +9954369358233682690458005171274421730241048095790588467590844502525588085948164312745806267036443692262270244497152879755376313 +8540761990884030057918282213961331175575293790886064372121411245265558611444044299259702033859938015653338871057063134186390567 +2937295853774855369815055903708691070046531641376115545763368983982874877046797588826386810643172084809422213611867667864377575 +7456129086242673800167308357557286842740029636829456648911252509981444991813390655015368895052836323823418477931875989888413494 +7619903401290202996353920197163165845104216512134977822317453208086977782938058360962563863616931222677006031494617743246676098 +8619837607929254793532399964916863199570819754247459608169557658376643077622610923724449371091028101105519894212018425361113161 +0198232737234871539712619394554173033293046636313472444724663228704935678432306609229598127499957898688691002287353410535746129 +9160344825935915883479808009630827527538308033068458669327511758149831271072074138691690968888119139014429832927001663077896093 +6075520157463461341098226057137016444093714810741234441217965271334334440753561468859945005901907007749818800609799068435785758 +0546092246435123497159753904346692263278071121042508101131684892541656872975812647089730436015791483195690674123772749878087282 +6758449872382291050831212201949792798424861556134983601310627614120558124555112900385099751123167702480059628861813816928572678 +2294380967592455168636104873737766456888729486890507823276579714836828251933669783531511408873018452811541618386657467835047578 +9771964953738092240932562360117185595020635073758269035209960005817272212266420970453644822258523689081214708732161974226486482 +2078896042427351080221479050978223463317038184395717248909464597626365437809427793625231151897894098417132582150723722131356816 +2733651127665461066112679225653017158187022844835577848138599970119201959393371465576981148627618500453766449113619920556794832 +3098796406035170187586479893078215549068777188615428168013041309627717475700524281634638706348659216956344427373631234955931788 +8266607652077744997152631910857696622624195835857414262086680438204284388121731692389911117940177851680318293050972437430080491 +5263662912508589659968304863826115756592152285582835871560573052117732379831943224939230850768149596127803763098910581308889239 +1399259544384714811458348148272122374447108528351486026748375991519038080289258112233626298579932128994892803010854830711867003 +0120562601270662181088525907457246972630366755109340652778459673777734310903812432648671068725042051752322546900077059823883777 +1071975377081858251465844724729282629412278512504579575631776161691808267150394386844729953211628319505799528227342615586982667 +5142190253907427483211782117002935160362621628506088192922092212154140185826659904555543974037176549370222198088358512119912742 +7709122677369521964052899819214134971313049580616225982506654419124826044743127724976092244678406630989264402052401911579629855 +5241083294500875288635504487164898666527862404520007162909767182104068330196614730977680503144106835763690135404371824827295654 +6210246422372066059297630639990103921525353988425004032130811899385175353438423745075299078297982811812894692845392714907314914 +6639687154171290857100923298064297354897427312066201467165805288117452112109293854180789331988821851079333157010919824197308857 +2725453575267288288994357675260613771455378866560128031996095809604727908410382843600101001039972876558637948184365207490031492 +3022829447684501701771651529957077694566855215179433255048106574130176149607854191690519660804326926806696105896923466573489014 +2276500150756721393101309310240889850130747890241895231090329328679796493201088708899083812272168509412763541079565359287160502 +0441480433094880222135017379174984153891031352882663477623197838966247123264644246447696041370087767283894667315757217159981846 +6600209627389381111032923919248323966625789375205223637463959729585017236548352912218466643214096190786355523902607233701011308 +0879209770503108176937425508560832114089876275039410069927508813642206128497418765988069857902212785710790868994707145334746457 +1573213691519886100372421851125709104684878670999887084420803400476797705625591520633797149282934213606570900108998582064311163 +8676807063736715112005822408555267407647050728641117839753641320386536310057649950460984214505375532528456592558681408856748600 +0458633214841398749014431880145127111888700604235741500367092065118505389545560189317863466505700948103428117858933891140917600 +5132202176401630576193346962510769965520088034746560321030328807272795203005246723655044384670879356197070618995577734499503916 +6506939563329550580672846170456665825955418030610189100857896112174782009029170826779986506077333681379230762539236049071683000 +6370393487113254502942284063528147627172601717639450424195980075047464787096908959060881173708540962987133431304917906118868338 +9267746888420071021500735075663336736361717345091433424161991124952838871022030399503645263408791327784786086730026429717318657 +1672147391408891002327479016767689503494648768717682253138108661180075426978036916352251329255918145397491166698770599594009604 +1973656522713866421729264353952482643307862403936073686775977710379738331755646451684387173747099995521827339722863619385237499 +5408235756969453434913224193378329404979503566466468217232986076880098924660185616188785089771941834979789440134998311199906069 +2232778818840350786605080110308895419763433233707234872571290509162878857914634432096414764357358087088039057924071148091731175 +6015400792410124384152213047000568844879712268022351230585299428537516602169557527154990684330404034064121063393300034030430905 +2306055374539312465651241264233074065955242079361211680462145344554747450796349210759636113482252702608031612245403353997125476 +3803253691034432130798363413192595243576410393487613462409388440991106589264853952016023685661448176427389232941666704057678369 +8628747721532797463638681268551173019525973277356279608142072485533442125834239673920151827309537064739870526314899247429664781 +9025827660968538606553886333865132013576294269585884802265251611104643759277860774328243246331432389944537967831541391858902940 +1930816587488360252688732499160213070813273038333861895643529140656993608246150830322861238139209540926180077253222099793719072 +8959519042048720068153629627140256241685908963439182993973204720950662335620899080859042503691987552467007737450276337233761755 +6288965328635383011799009079228464765004787324704548708119617161002013632986577780042057914052383556753286042031709306907755664 +9921350932285357024268612614215975988614776231803905241441720250462994526451363159473639866331342141233807338499617054943944238 +0578049360816576019800945237402222592171684814088734743903979539564400529178521960883785173251341874550331866956766247772470369 +6083904254700701911630159329372053705191987684688500636724033475465777760911461588784110437867357713431148790643538763108521995 +0296263770538400456769346062348737783795801732870097577376952964730500440617573963135158364912865598645263299913463769215039071 +9483488255991073328145731534454837990839423394416473517085030613349147333787972449423197209621559003595529788900553100012406747 +6566884594766054381929553636693829055015384572992260492476307544253448277903698366356427152189807274375674035466963843196695551 +6323074679661834194813723642658721376145252523971723277400835325217197190050897668239821693181091121061162219401288326146881306 +1808318984353930429816050257400279035517547964016766257303295002919848213345027516727642354899576468006486504355233740324100211 +0373421583654094261005790222006220690243740321608605292144463259644370384862300052723119124037021639917903672109294422220446746 +0968367458627898019517765631253210766036998155063276402536135475040822567588712196840514962484791203840216962030611173316284918 +6730753402413401318179055308056338575836289967262947906669540139949663260817034924092055901733213326920487904492973772740252118 +6813399504142772729322264698007839309738863435169195740088479397131114450638902610593694986422730992468798340242051077542153965 +9094257292692469011371694239749935666279240094911204646416349941755315640280190692332906803301138668428433338486284139761451437 +4927696735702519573953727266824755919361553550569177388087634691983381789617481807188380885676894329271297374663349552211867549 +6214976348118306656535007501010457882239285954427884872811741042673638154912694956412208130004776408704809831151781445974935728 +0120629582751291565009219505554682954747176012178127728953108970378564330824283571745236870527703343674549433801104649142185888 +3235814158776446629647585300745371840827518705724793256416163401064594247074494980560274282618040988687418212805930332830675213 +9481691238337366800610093527436128723725108303743340453678113695806808341554588638276662497395500403037304969340369996972707032 +4082676737625354210464419813293767706297361241754982481007296169725698649583516782177058747437891980516222464440989464874961596 +6461961406394849542189779035893533584829634128015156718397540384299215958366542622049511074988966488475073248255117221338638960 +0142933164629079463406544918744481398396204306933357431841214352733485790844296470933212820449310522157583393441447538375216436 +3436698506912676403242337919492000906428711269510381173399936468041047249569064777837314598565623718618433285256806067639636763 +8460415932660725184337524946121929924205499042071794021186039050613143778222559008526610557979170270365704047948250328090311884 +8815703597819948360630939744751577068004694199231149936335126538352407733636909351409964233765563167198074923729461436493523959 +0713926842635856417640343723268323797389202591360887205782568996458244122502082280450763638212538223700620622044815212320168684 +9492871418408066338755969200124883796242190267635257673723366249954282292979482645457308347074754036142876356477739278675875440 +2183538168589813954235091042917215997797441009968993171615399263341109892414323128649997905799043937988646881424204624530006049 +7733957096488341212048475007904035717627062565794383079540365496869348281433928986222996400495215587152772750589584231009322321 +6967167721546910451512704467642578730007590171739084668065616508498732163764275594573615283985791091469103384452900988352107000 +6807413612465354023458660940481659053886101973060578770973513038430471528121342377174140575232152423951725002933862875035962993 +5491625968951170241792359320698419197951622254963187269169519754789130928703155856710378168285319433421425146672702276206900357 +6582314544521282948303668171056488977803778991914594073284490075274341682398796202525053587573368003492350674264590216915743030 +0229180917308822559423399370500197589688253700875858499871606888817412759424516191276640847554118760685723541416666202243509736 +9274208984682823732942979222927492539292245964003720945726994325795924579380001664666341956208236538074900737136200088724900215 +5814292572624892701632682972030886871485930705868231267185916589385811869703139406294452824977282139608709779376891002163708313 +4033763100173660376690182575379995726083861081926510802579785312647154456013703372795547129639766049095465742696104516613357933 +2568315805640894925038031436747677782703800879694535494046794349191040639680313707242709239136167699797774023104845097743799234 +6763080953224487156488442945555613963349180931811315907436001825754710792409694328952798340996992622252003347436512459358128857 +3954533989335010638156841066648473526011867397359480295694595402042037502438174479721797977373598413288286342098814213128179837 +2396433612158432510452891115965816912823381888049756989510546093247222344511226918341455435352327199403241839439420506108676964 +0795850610161143815844607948060775881656537958435083754755508315612517655376939244238451149176008256028242905067513932423707050 +3517402986146524008574508829340726577308717530546869694363472906429850377209335971237767032862995433224020904386089476753728648 +7922151222682324169544546785791039508552110735905147311483313421452486749631056709157326288104257728264776617306059521303067262 +9433265821676424821718658733270406951796862690935463256100330389699577509374904038151899940074906081844482171701461900939078601 +6186077218855804555456232441080881439587655776678203701080218698681846807414980313213113823373021577317623820418853903404980853 +2057729858963790488672801591735476430626710017313638370400064539747700089967564851982820372204387613607305955240060602392589527 +0383881550897793438940267029950019121382581949345028853214775595378737481122241954444306274659828618843592228475107234699152687 +5284798962706326315885441962112759776606317588723708150855167126722288961236786090248841992912344949773249220873179712041971922 +9678187960541674562218790198419395947752158216709587927827286969325482868753908887434457039802709422399284864987575179916747347 +5327838914777072714692085263368643404584146218804037383216974773265161119155906542869845216416813947477024304569196702936579095 +6268003933265575972147374394617649572960257762413423428779104569461013465964897200866208582709538990234194911357542427621601938 +8074876525846270711334089333528645517621667077382560706349198812216415510664481057225472908752257947691952856523071940619013694 +8017368030459649790293701927587307177007662644838805113977690349437913275410750363651007980050566471766249753077608880102272532 +1982797159364965178888531580373430260986522031868350608333752657707407931000254944024943348168140524874484514493735113742249981 +6288538584949755098193652659249807271937205648283720864311553381239244146100739200355642491916783137202534161705603274089556521 +9354739749045138253043275273268328367815186805183694069007675806235940663397422177024952828447038786641822001541524569479714913 +7698528117160783953905420058390896481011235446749390616223922774275291430828661119528466868454607218473820114505896776059375354 +1619019266174818706077047840193129831420498197596777970098691437675408999864116292704117680089265101055969410774137278714249866 +3704870678985840970113166302795462489950063082099943547515245655672648446079824717316402600594084559028770660910436751665528235 +4429408197253611572221699160867353954702111945889329273311821911416970564822252007768702453855504730931208682912670990901446785 +5056501125351085290367349611566587022633517744283896529455313187439470673480194626772000636195743336806541403680260332379718964 +4062991551971333412504544251310884322830607074386974123640580284992839293238984564989258127209764459890435786526862148574118080 +2625522665812777011614768650094518338934467618573508691312860257788451631146950375887371627144471854275002143114396671788841878 +2226696461215179698643658067033045890658991544538880319431666117259518073995053382607108900045909290904771038098705696699068324 +6726532088338857549906950143621062126429461929145317128221035122460444306074528944822699784500814789341052158726046904507897034 +6793037164285887325166583356411787362893236497739544799385622176244058491240539097656877256235845151279052346581850041111149908 +4093573044381701004333825958081420923185855648955716595548583153752167241574306747073123301312447687906770815027018933422227596 +6707271839374572454938209957923490292030537274459728356418336773458317286912917431092307541994164910938497543069009665010432516 +7241600538166073336929364870095691302789706058064621204209320909673557421090888354529446748882788614331747011940455844576764284 +7137143302702161225320499515565654830535431434870632910006167776519433451398361978770695152191140156154509207190258738746324525 +7177837461622149136708945368215880972643302158259696018853584758017706472295497707280871705217770629770641478542481671610250773 +5554013978114456033660343221358867604345109625580200393288313536168463029481940254044728394046188030569648323861618169629477000 +8748988786120543881188413407361779739580428614335192009085939766971185616502526635854198592751325209395171686208426924544564152 +6617729010736669611153048037379873649289970538055287963581277014784710903080627770559078145423490012308690390159447385760636617 +1396112919025930755072794022535421762089970913319020087082933809741223660404657644541118861471741622430838236195432385726159361 +7249197652757615950334012535738132798576509936889286629270864681020298782574659494783085110319815705595177485744225974776618676 +4858557343521566530159783491610288461205535017137620369901510860776505101408698469561089138749268742702921025433191359814405089 +1405508073047405874902803040100792069570430571449445778558936794822787960786336616853548170322337310425822658832018643837559079 +7255075818431908394615699437073631405701119134607024097267578921840611418622682373870814723883284591259922905738590072345115516 +4240694967339735201081972989105259182809574142617805837465112261091318429255228721568810181664317538777929819157212160575829023 +2748237669200464565992816533713130419647283186413179673011963172187269272224647869974262327569585442612854368077691269344188871 +1222760335817459965853292578856201576938118519312855304508610105281112231513426234814422228921425942855517206703964421120719762 +5224765636718330925202872259803187360837016560225525039097597027027310508724003400835919326880989701929146674585353105826375662 +4608686356759945389820261327877835956104857982859971343770150256738677287345081654437248462809898624047205228119203349515135144 +6762635128848980939500701402251709503914581106728361156687648893186497005364492516596632908393739083848785619442884628144162198 +9408219837678921746467927680318487696309519621970987584546154535257273988915610716263345779849862915767049642549659017655936977 +2878461866210176159858735856155729710212544372367962529421415383498942572742627275020994117127943578105501057737118382232121164 +6202970140395425040275648643808519609153095822076099220917958127812733404125781257612941188098229102786208245263355552763155862 +3412899983063072356219645639174430993848691176774827540248823520470123385464397586328853608924297363157128484769714643392030260 +7516335187749776239975100738544129195651952155894618035351972235899711941435830137791970183095848641340212385537760526454996975 +9326117473414458915105300842976184452176417489508025383214692364884363250764059076859137855740438113389750789393201920849913143 +8213713819060950631004232834986002902474872793703875917119551478078092258435100422531909399249521481966638648761188280089875780 +4759150755939784604117474362263502288999681621525995527689733138120086197663894427404346527959682389904515329738583325486736756 +9325978425209872464237977527958499931962722888446298443732493719892549369851302316811008601401750818556435135705126450083049922 +6347121675883203194927076244218929133933870815129366063255106247006180593262999464959953693728515646161688550961478583928288648 +1770297585530292141472219789662846895032928913171250259285237148388628954759446094661658933173846573153793706711301693991850474 +0792606603197595851399011444059807607822829889531316870539398964731417122727677982590799094547174678976545309066723508642538395 +7542251187115005568283732133757451228935803355815247833157647444961116649664568241618566131440468159056245333148920222556848970 +1686590915600101341570938724862037843381611006851337958368672209742162426192678307570447318588843817286911928534762475716887889 +5985898442600265569905129276649109155018541224177922670883691334985077196371351903258301592600475229763499481236752268688794381 +9908334038229937596478744640386393429233365920050089472460888907538665294038594595567391991275514366380037563916695381509760722 +6697176988394487279550132960940875716670256744139528862226095599316038741474608986574869723524996958686407456560239747388319182 +5272345146409276991368562911742268892736319679968918454777104809692672080906197146972033144703725807051487037561295160467451538 +3361581138752178146561886363234614574825214993383047665369152075131490893168140965966229773832573946751662445845244243875117807 +8497747825333018837277204487016088859935993376161231128402571833027091271675038762646104326773996163504827534262519513030402352 +2850247104178367791237021165640248127529302018809122924281078393572312164207913339929888292707995450264033189457174311609184073 +8924909933371581642458243436549195910810805622486372258791244747556044533591204446068293714215938334798650520367000310196349472 +4508128384366452869820524778886955415968833938807781198993008689473057745756213279992800316467476453813822465284911857999907387 +2023953430847273884483235864052375615320236609720705596526699723117087569499174093607783752368506950667179330339328300764225536 +2114055767363508202672868934173997107264743852817603394956504753530153541861401139621693806395357957937922300502759889840467545 +0621018093521434841220357944235687355230152336239627344725027296978480489999738827553268628247847786834042126152054295485896741 +1728177905254640772617413047936979080771772313470023078251325162582234445493894126314939244900306139362255151328277418578396047 +1792422884816727055970087290849551106652457784152258102530082023142369893647618177451355631227247379577671860849853688299849551 +1175037347149672092098074310747361251964753674884333700769847744830036555394374700856740987145924794681060765600644641697283847 +9174589667717240975144336198616364140632445180996668524271118863864525314244946497799850056518633949293612226613509484575056441 +3024489420602308444377762407441001512681502589345978196662197798484057230208048085644756143263157845710447555275495197040972213 +5990095737215350840694710792788564049039561363466293476205766559255673678361573392948770517450420686662337759686497763541080151 +5536095650185550909008740808818202276021308469567387829730017248451381382214473271435330569988489076831395673641296020100168963 +8691489605876605176450990544960425812802030262532258796192840475047025803288315858498575542856190938582743335067317105863072855 +8766959566097551451268355313989562707945501704157509911384107284566676448181401117485715830603954518469086610851712800284373857 +2622452939630203014872224939693671859800249164641618680985148159351811785234412541320836473437263872332827932949209391274805175 +6798235720998149184680985250493187047397164050502295623889415695762080775856311836882958794525078302171514915009308220188324579 +7404140736104785086437030648484406734008243907361526117865122472043884228991384547907143497695706699559582484787176857952820522 +8804256451428134606481551907263647977538193140987930551038510575189609463453621867637464848455610801693723759700181203650436839 +2454914648531519297448584075603475998203868332048006750382533505496359960760723675619846780966293281981298326598027774066580197 +7995861529357273876737015678397822856184868318896981667361295506177063218216975663372922918478316465965515092121524544927046347 +7075516111646427841984461388320881450094372462158722036978724530408790912720869813138049436688811195541657354372228803333585071 +6429445107361977048898774220057765467210817429170612447918756002833100352538329968035564836996813252177717820091122268457886430 +7174078917945962391527335388096356842601813618444449251961003226992399982772853124625379285502923443657958530018722098893816397 +4966136548214340660067711135963960273659031432856630667889996071606345454141323196469871177597395097849304859547230071866679596 +3726141431238995322616590363658851982624885785492321657227849247981548495242247508727680411775990290110420891478204977360853532 +1644317907061571688813615690182418379232646792079005297020445585096706983621152261491067330546246215457831060764875633375743540 +0260037498619368766791619780583839981870124397870392302778118229013764892065306663721631976910781105530817819103416467777655922 +1257160865985307861172892171513923812618825522832093610372208245587343312911128848417617475098350804052464802003464019493811163 +3766028847159301257741274325560482246090419451830573366629957679849625493927009484554531296839025641820596875488169323390321224 +8232388226007759579919663974563407506786051531271638328419810743863932368666194478382016978871056258046863096095307264426924960 +4549834654287891305590680957281622094960361243150054432811463994295196664383435297694301203486327605844830810715344529812950660 +5753311902123482227347291010505867923405362170620911512612976729652957268599586765352146177919457594239032123428411979879139233 +1756522666058959488542110121660842887596621268641851969762507171503624064440144225359791257651455405971257235743672383114242117 +3669744927951794530124706132421697781115421405857509254735757547057701496218013526040546290811670945415111481766033696065077908 +3743986898663416455838782244658330408591406810811925113737418608085893951934341548820971030074613077943621026895167359064570606 +9471978412930979202834282673808363212715221979769900548835760002508328425519756934140019511728229374555172954389643589129389588 +9455543651731906114795743817911783078297338344457513508249293073101170733009370971042675747320086133963697038647918247059612888 +8987238012717473365026512535048463787033301244418231940438539856074603360903070483058902057658066015083363248256775852399567354 +3364502373064620236820638615612559600453180311037005397530277541350185917242503038852432559174885033399595723295543954546558084 +9899331087165498351833150053347482195081440704956752507876144131532942935103436324218551370249739155812929164520262875021494783 +6908904643032400627041773579937621950623563067061899745276566450404645141582156514162192031414915906603166745603027411059622640 +6495757281070400862553880509625330137059949973388167457480215447233006890221821337932185784669081820552176614865399261551583475 +5214987707999082292941559306056717820096495439266384956685554643672988468125971218644583921446293636673793845912080991022068721 +9098075542650837679681095355197850211686123726685262897042587789900344672369748441583193738857149324437982999889236683375639656 +3454737499177458611505297150687929879276146562076664745364918792408073476880967147036390541767431864129948395331642282070602239 +6878237499611110425456520778926623547629857959652095556385934261653929146175223058776396057712180642186976052559776640567287939 +9117825842497962507655739795549675536045986355442960959176637564697703951624943028930326642942379066897205661256857648338706258 +5607249824040048715831055763915681630425710558796489860046207669351392908027930067134903067626066854044057840603625024212216072 +6132093035960157469992103741008683953459144927130308314821395510817197195888871912314613073270790771658400690547770532514734857 +2289766523163076031024159344583085122839051296194651927355671209701571188590931542840886950235609126079848243347538568462648984 +5766639281060869652151900461538022230554309323803322083408453315192390160247383316259588161796028371801133887539960085255308323 +9483650441565836194775795637439945189737766569777908677905305601319160962603730861873961369931413625520373523170442339205096446 +6397277751314562067770288602692729550017088466152598972362685935373861021657565974015373030623318967239986491125515528075018887 +5402601339014610197678055292860126657475597634829577844878918241260173909075027791997352414032212153167271561905364752619385786 +8491863948995147633786778542070447298998068007279647829967199136460489001972567744945790789456373114064713748773853626265799228 +2370209092117301003097542080527068882722669167429990641325299940901538545419032212913930826834352316812337709858765794889107909 +1398631884718147949515763634548480975677014453715972288924837802795200463595978496786857308537512388719226166051585776739717368 +6941189534316796817726911487247546420252712714875639017653467949442416805399033991555627055669486555645886651826109264153186819 +4940516166674720818220542130511334825103331157208156275492549414655335399338455554953498955587141484886410449146234826660024826 +6659792948006838082450272697160209259339168888357685416533244048793279607628259013570705943914832132649302220555803191155769678 +3450607770354143996892622745433805449122734203977711922327068576407370897444833149206827486642681899060356549533970828759001934 +5727275520263633514968976110225542746359061074870472786303946464842833869445090010825025879958856002949152638295200941043841906 +1327175710840731608371208629917959139504834096615996636184118498113010168900528468440699292352911245996048410672241669829930509 +5804872329617982647538232786144744032881485213965459136524837612293635959247462464117508493059931748287964717902656930987863246 +4277723315738568098484054986328302182182023008609946701266751039890388797338292226205365731416488922689962862442504898912370823 +0169167783207015850910326779335592201489472916428336283983741331168324385837589731443211700684040249551018329769335204931232483 +5298537282274168905097783252545295159931996045324669229561719918373117774516092756810950028198012718738992851067743291059615923 +9444851345487696935698137109913963152214976022384665735269814043959675847649595558507091607437770303094115672250928216923641250 +2149472592686716079768844857346451000286201002122320769034758020998927559101579776094371380910427183625784710226757782544230415 +0647876642293735785152692483992423954470320088754545909378609961100700126777198798362991474951795873563734734785888619665299138 +5849838488444430649533263787538811331357728938722568950445006777515651519234515285808516388251516110101450462227962034131202241 +7276104739525907372419126181186384546119321638501786879645354354875497667502927550069197442007577146458961731206493061524033007 +9422217503140335187736221742892478886207544102369268483447607791356773560830807813772377547585429918129160635826252418480968692 +4951724274446263780114357613049442315030582695389596800003586264063614032462365896162651936067640252887621519671734335954108739 +7454566191860852039559843736923834077952568697550937244831873949705841445761551247253789687164216518186201016215799884228535871 +8439893371971012000885553951535059948361302183174942319645796198457357286423328894929588756597087846522596101434099524110623679 +2208231333330844864465745363677908966068876448934931718761801046807820975248234034617078513450119384012451530340905431793537946 +2305572597375584599916270867173159433891169944691281508143382202813205881528811308590035206333627177252785229699333403132814188 +6581817307307772248849032636830370028521484024024459152842727518740380359881146898101434552487760886134249651169919754088451088 +9972253499037895070298514770518285390402914258598519025675691003304065496834156654347189749182550625905099172508363395356804877 +5758008808025624632351941198979018660594929305107322792450818499652772086629264781844489198081813129733546688788720899531192457 +2745675328488913480621264293304705435951452309540398654081411328496424730516137398740590505211615977997709945823736263422098774 +4319217301030265407335508264753089155109074707762624338354639950177679811707826932551029618948153019495825747590381756914645981 +9125342547489286004121333337059776054538539726153714180036823804029952720452919240063140345998373975940156807239544138619226508 +7242675874264000704878339896544038336920890771642735504973073959953113142490324395737352393349120127057115852252907686376626747 +3473321596354209308933007945651010752129252885281924230373167976919072240297072208573755175225599946410500654362886750707794528 +5727589543974524706691693259117706864773673979587751324354353742658357813584415514973239116989629743388388478173856874552697761 +1142418605095869987288446285147568178401344806595851658792922110020211362874700057924021743582095514290749364426026381491938886 +7017216586801442390706943294273005287782115442259090512088524168970016375416554459606914186390125156135455842825317699271637907 +0211233315441242280840462166926685973850749956345775689925242698347596240183158491250150173733543845430700552741593194555804750 +1797724078344241125893042060239537330994558786922162379383591805537659608535521283089375039292199790021670953442916099772863332 +6130652679697033488878720295305832453720948952785209088953761883766005724826476556165454243473790630572440696210857988190704713 +4744821686581324503785124383460438165434233854467259677279955943920755162775872557500679297287992727542016808831027373389642811 +8818850180176632238377551894120301699425047464434209428644888921239546400810384063448594908229208386662084553428371435016786566 +2565402723149127183692936818466510584042955514361987237908845951130503356391102529543147109435545190864583265738886354448729716 +2555888389438655691918145095510314610669012618591908132566801282747594112804817710376968735883323783155393749243633468461076212 +4037970808854790499072817736929400553034907676890898900783090149602127450205466513482819048073679969106848951996174948205730701 +0816030875372524859030697931442206128673990318458281042368684773151576092499288971063578724086458233384930941299441451767263066 +1664521612180858396451529620088930659358500352782541669595817923485051339758066634536721977274523994998146473981701939017205602 +2524104027099484717563367769361246592695182470261402965977193164101572566284710942794698915634141627566609476347965721168530986 +4359025426573155220274777012266852482447049345363636421786714781355224872652596955092178684933842367455487342490049229104686636 +1405127824749409497462184480237538519293466992317964479311951604897323056429690265101169427550747026874617333157171121236924703 +6018076627013550295381656119236991061160617457180144979226462876301063552180250667955529552359828561142316939674528337208856344 +4374830018868998904944977293261195517662546966324518234611103690143089662138017933662034152841911712239931096922875112835748433 +0236447221655510600424621887641222992324877860049632049076965940501963553949996895966286320389211270787434721126820780971624253 +1124118658886218410732481795333541469161905682578320893049989427979158879370075562912903672274463984603712619157580009287344826 +1490073439715132975375184719480763004789606934374075237219980119217402660147346676094869754304392997432951402325274930204871342 +7507088153751893726167389642050254715794421419546083151335746591084804700733010001661585164038391983336305667211157845088930512 +9454605864819889656904342091511833717539963300409957365660591244004512939427858955522107791635547956216918186634105330346773979 +0658593516322846162016300446203238142166026181710436615937557978420890524597354295972144917810824199008158678995080211427722929 +8980589661160288330719076634082892363742482807739228207426629330991685667550752678607146578792931470069849286956087928508404855 +0981420843325337105199162550260043516249035154228170027681913983488364730292863723816365277728171439162179656110789246164875870 +7235989772218476249654125554327002628917857770756363184283174214585030441232386973067535578285982100385381595377232095167172355 +8531968481709055316167180990447601481938541581517577911736493317452245372585792259012856477270057051421673563226983021870172320 +3966519268023261060863087111821082801296148611252172465597370204173800451852480272015584093876590792853690848377385063421097933 +4667059194375637666825712724740535719642601441210189891690016272378550854485280624499553958549215837331920055819015130335235748 +5180652276625067336617174370015460185337302241346649852510792894228685353669611675801310899772318114006268028233834332313504023 +1941303813100529733477333374209673305915978659212933725574700988773740701325826021523545062039757705261878074707241548966802269 +2746067443336418197240323253838643666106109625323987238695096142699993722607024221029583744572542375113816178166737688431737737 +1790082381839115387609873262982695752275538347088304949304931589485737690198187441832377091589179048950816796427590616885843542 +5552689266955858826481326722121453449315123458065490624658389457610909531278210069460670951157478754958271396713130819467585412 +8161144774814776141255994752061769287038765060690866140571952344698969957192475271081186838639007943816699743960034503332953194 +8789192141356424274503562578603345033063547574284980009602843989229909463071658256259561315235167835094522110712803401150014304 +2269120125327746733199585385420581800688978135760399189023545691870920243025811096318243711254873352522759304218250492540601301 +3220660009367443347579325094663071621014417170436158246461857082697811419327408184450532404128001876815109034413458694764955526 +4236810019612678173215181621403086283786465950563987195979676317579127299901400746488219293603220797858502005948349760508971512 +0842456999754267165377711495312735495780613161991580964945616998690779002045181519007002606802172925174094502143404195978695837 +4416749583760585438101952975396064089728080431113199590836008764153005621794368480547875309689843049490843783015113705286745691 +0795274682904383996694242263473123412015847505963674606830368047427506205910859669522416907398900276339485220277009294111636658 +0726660439712285671104280059752886748116228091939311944983809111670167487220776456879193368274819545300958924106260054971842623 +8150764216086897115298219326429987785009191738597276238467913392040684511353140756968540557235239878544391001829270153861846092 +4383222134341106409110971398373626248863354871646454326385144812872101996077996725153807549596115873770116755372384144360289549 +8170673501302124114120744895395545744756695363267571023745570418014263093634951684729982428233593809293841917967048459961199699 +9763136038244743451806991825890314924556837135102471133555361059824564665398157293669951119702184740695499289874076409711970682 +1422421956968116348857989939957276677533229421063047970708284154758036180433546990214632661390471026182712000805513222171655488 +0790454789270387069048408785812014404576158995323548751958890039523932351478622587142638056133836919868045535455845844392024399 +6828229983228262659292226957969701095193296498963956591476120614340834793473477949399153199148731691820887892496627505449096093 +8624492093374718033878604413583151815223479591147786368391299257097621796393142945746725700896309907581595691253852161958930992 +7107729406703783687280485106218574382700030054257355689626925093581880354626104182184797971451190514868852721286584413091561305 +8741987234103316893377380144344514006198840745678799814654988835056150781397439910157073114301452768509065135515533734967678598 +2735043292667576626222020726140529203825880750760734206750175908534901067729659839678099054130592189468939769616706044680647240 +2763006819383647925527431911808115681683993973822624367816274873450252222984650251537326892288304742295886859351709349425297650 +7134753100395351301348083365117683812003850761712390745630056031799089854945233811860729771699517948808663082113548033370410055 +2156718353193091760663949540389192861228968135157606923907872289234299014121301709409557692508414639141148641363055713300497167 +9323046502608972228620000366263176485575290223984881067717695579312796433503231136630486297616245185739116228913473000321724069 +3402413948021099348085449161508115950805641814124236561124067595528674925736958070838477983849773356429271615279624883406948406 +2586362309829746055325863292755259087415528960697154410026073785183365372343620398551768197389974234910915413966255239135166137 +6948141006945340339297784658800718813009858499271702100629421441689482806248257882748211373263608811093415305500022007093455945 +0842258683716404942736428616895358695656655266518974938995151040768012671515151979466768515865903424648012383400333040320383895 +5049269512019956300105561497239010604854186206683961343893764070138734690927147819767835829400814779254514025647466904224213470 +9569948375696342652641662102284605174592869950745185014008552719701570802715911338264166097617707320465336842190177333964575034 +9428680684767284183633020532362541285325879314896297017757679938122092115670160403478099811406505218708402491047782860144041267 +2048668363875460097326211666406188941409324970507852534507916458181293600129195542233794471499710031501087442970014489278431811 +3405993437193068750979763065800204417948705854493241428660974718747020128492308645680153785966021095932035295060527677641892559 +2814589083509652662902787080397228682431174828014560598681492336798258054680671382857806718386021239491601389601875295478518945 +5986699496572944083337074930929834075993621216590713141225309670719782429561351058677320246208472110574056384475102009208825158 +3701143845992716374497567663523592738178836225177899343548036997338523733500001300643656167477000925420983129066143726434092701 +7423291379038754262185514882391794604313384501082737513845672893062736061561920457702887522180551612186897189287428790772663945 +8038882462960387008399150903931548842040303337569963977574310341965170156166264463639669524288945874943197641758694114087360524 +6755427287648373311476510042437767363082433235534497669444705172475485394287772438817148371693824101932257743465051113814804574 +3302584529244780057393036854445554623243089576362550605055694156880682183315829897197741778897975736012282052319045886108088033 +8087176493289867561968800923375092924482419715512093174473694625106772042668885277666098646361898523510402733964872800445131037 +8320007995921770348498605128869111626930267035943426816435809551556518111290098800524644244409358410267473862764842446130048523 +1436098828025610017328979458386153232076446194265248731690143382189291992244935737904603822794261840119592775301158595671929956 +1859896675014716704798696047818739852245706249557223427847334266658566082156593745460337399765187932861285602937611319183698837 +8695387218535720582564065835479455701900416698839376472323627804365945785468346621682197696163783828936362978547295808542269445 +8279752484506847142094417985544033613129823835703913986703044915444548737906032583690923865665220094573089105884291792454719678 +1316169725449877686068579181522410565098719766530274437681078973112896904796812316498589687674134438686508004774642199962324204 +4690063619499878869524791692206588255254568407388459022366711747832387099104508881401463078787110967721065394141648606132537550 +1451429470869671002057789223158518659342906175930490990163086609677315590447420960904216338864582531646704745882645257996753003 +3594769224483393076962577578336775804994360716705648101110515582304099946203974654110102114903083901640907564773113403226112614 +7842811390980512417623967516307862595714647209118659868698680060437058188686064362842966029195804418885454481156401460457680810 +1353730331240432284758809177395482784888827256712163440645440749728289433010840369051942073911490213001477219496896397735829211 +4601945158211709423733169616586120219445506544455684681994448828365740758425856347182450418912396580393258871768216932273389644 +7546606883962070377792485250777570864737360035965255716590780907370097846008363755114568550428189781279182714576817370377989913 +5909871699164748481388103916215854782263584875652627145927520783000462057342075067627629231607264735052014948668522278967261465 +7789853062938105623444363261994088544665616630390123445507860288440231593034911635167838522597643333127458708364830418263896184 +1286144438352596484065325500754944308173042437315362632781599996019422261591416810474894175619846059414717720198222378370653571 +6971462477582470633206125394932199429053380382783796898399516803651438673139498531736021776634684941580638746309619120992683170 +8629432822993724963855361987882118654589234693568266212440633573606219295689682039033133123609808063033145394447533709295411896 +0281594967652261579794466637274919984182923757681634332365968310833409307348568763974412665592870158544818035335013012421932763 +7655826860692715418764005666669293766929001138134358352328235302135813053504743107776878655717896623753934484760219931500298692 +9290168663678775972562133999711785783179036668870829240738642007250789936063529444287722257668245891843786516368999364205453698 +1388094089899362163718773572230079214466119375080348978705038480095898940916060131319234067470176587422484787467679366538347868 +8017293664803927630769729168954945146012294840940686008911896485848777037133106837925465110245874730110174759001141830380040038 +0120439722450454617928179479243857705288134086458602893871161972846537382451740227055242495699985657918659780373398590814158015 +9185549346791665800883510913273745072991047690371888044204398878541170797025220849684634548447569944177523688037116487344544844 +3983330147768673814424552255794031561316294164196820680509267685360230979960997521617397764375675161021883986966830273621436840 +5559819786199323213875219658620019627595340113429135282821726501703289978583492131096279542835050405087347140325687712807079487 +9787946090092230362250195573096660493646419587912974580035697132589473417918325240116393969464922906025196729087774385693764553 +9860355320714920060965670358032280168554148297437945180860387882403591842300282814921233640661490871801402207762626482522032047 +3097025361491117451005377278198487212237099544908247111077372764356886263791448989161160949762415522639831639294698581458345985 +7119461403366321455704765813404706513096376229662175435103856904757065360840168635845572631764671801632765978886309611217770123 +2355725108730791813146700353437818727398062989969964100944171032459233591148110740093249920429111285758187817891195538479372333 +7717147589069034365041184332554553609756818941590171329038369697464604453126540943732615622700321916184110353125350100127752260 +5400719243883713007355805618610788516934976237500923767706894033322172234355015854525038569883308926393709084041999484656996753 +1283666031444516796129629651038339271512674202354596553588525167158551065999313761923723647718179391480101442800341777895133996 +0753973775383179955400385390016380992296547654995782315767306424246762945687075752145691419815405005932537449947304486849576643 +2979180958396224767297581462043320034127704743702992357878029772922817116999851784773690688789215232089766369127969016032601856 +3851573261415488209314332793330099314068046066211719044146103127781354079770099732602779302176734228288014045681315375260376275 +5613084622744138175415292879907528314309988380989805830104342547726499059288400078772029464667932988421246953056079406071204771 +4586730576036740853854978952480253788859203002939257757392716256983049436420156853771221502897572109716517239674204969203181474 +0982119350191052441755943123327258611832950316806139591389360751863567226397368531779203766930988225110737707276225086269268083 +1166609927459152345242074308503846915870742513191815496448329154263550697382001973332037653901875197894977220232463712982961473 +8742250538546251559619529217497793362557849446641056066254726761640756859174902719692841027963345440941200289292587371677456563 +1787473730619074666648808301286212846168364513813858291223488226257956569272198811926719725754005926219281926579718279777830444 +9964885779070155802556863992597887379404305287991864566420006473195804170299129414259096081893003248613986369776375193541122827 +8679261717535638702784929128089835277682571491318590535314932208130153018789998592505582846756436233025096155853923159667457622 +0064101062279216730164192200983393569735835140706754353527254528463606619322108164129797836707319607352924077829068257856221565 +4098966914598730293864163204728437998349124344700052324246741119119244973881766239805489297351925527481613098094358252765642656 +3256631812453399632987567720287450907847236484423613740997675264597104916297183865029408528402827703543717537820987540612308827 +9763193844988062420108359297687488837148328286540087218373263127565810612066788312087172343604520200144217517124505185828378183 +0839169941798971803467138716750724876136916222233258526467257984594030393990037357389464526262272705365500201964650768865692587 +6552250604997641261294087071350242489522328429130222914193744223497369981001341679952214443941383946458063259049876349424028513 +2213974436158368636025109248174911312580051245505113315795959437260475388392500593400331863318219524296806654096778830779216940 +4878685599516319690797457606707664358483790224825508827231099632903629838432588023623129489722939201221165328880348763488094404 +4425772854689128756512183299147748549960529805463272504802739617344094375960651486501133996216955411590283887952698362709773340 +3400068591062085249345178361745959828363146278918068111168494145377017234716441154409189698261322125169944980545699464089750536 +8846865398199885991570109728269877331973845988335052515573008133676441111176443077214835337189028637762538757833593083011292020 +5146028640720658045073881058022137119722140728994381646856170548927402303281330627742305686994941764560406751703346767750713886 +7209258975092391507951817856714755923161856176501059139093203502102911025009832983122251947751287786516716014787221574638939756 +4031047248735708744857275535082996528246946792080390060989384866585501142477546435169883331570932498501313613151748523824910672 +5636536308963660510951867259797466984603129916969819578084787382312187680895777499152465628258228314680156051454804934741689251 +0512085879302345402847429395015269085320583560263939898103230391420972595529857458723736782481282792113866713373594735837816637 +7989131244460061235927200538684991689192443600011394251966967305334141431951393722271395226793607939272662478632186079903315157 +4105861225129127048237596511466748710669457275423951266966241892139141936555720690437418563605034436351637331842274069357984239 +7714780490538701008702866992517751463558344819912101444037343071198346617249265664311758101747127784897043351442589412949637554 +0794058900554049165331519254294841158439399536633260010261808293322453935679510287405960977887161196498223064393352481611148599 +1300801876528225731545729262391681879452425698425569733040068970789105907778300649408653356394071182530765139660697696280006682 +9500403460028711452841481826687628011337250118712797471026118134978473459279189369396543791538899643830373887355243212938105191 +0873118171200026341001915337132116843336126642682077009436271005094613931653248785836179383736770204896797782028704969977499350 +0015763840495758341501065058600781266869591393444306380925176998016954972799133300711081355008885083487358653288976991291458433 +1182038281534129680784410839850542254891681906855217404657576532842299794168911463577129652659153953666202320168343676218151354 +4489515205537483086890747806185147028159038841561163491305751443503026033718219551299657596422184541173585136713789948319572208 +5427049518844513938239013971033113865769356316346953363872557559769111015332419532829697997722648255367995610629118024243751562 +2407474388820183495631279695253839995841338363502759887550630065930650395810552864219034911688436057782562693639597408091177372 +6597246835826333390472023469934087393575305915386587464255626374910540525559513620612895257369286541372428722814356277369790926 +0540482329932732167361734686964636330198342028336626252732092257904785101659859937597352536082164206650257784256989122547150860 +6958569549047812907458129176134743095874743438183862052347741885494624143736509130890665487847350948675120079666371405006768737 +8780431675373547960195603385932928721012510177772630104840609721475744144915357267790854338213160647630650329373129575638594956 +9496521524390891252020171500116808958864975665633521384560269916937417268750283135245520953900937858745308291830904258244389317 +0015367958131832930196538448945414897381505579147872992058659675192664335486653438990141847260783598099119731774723267313420147 +7668586967951300028516829535113315808229408687973080784960696295388250987543953394689424360452251573606982659417899250410280988 +0855610169950498351316129660470090096789046559977941298373823170600445713232790694833343261118257499105417238723309775430866295 +2330553865870284141777533356762036585595078938692432368606760459748846645365953536258322870084801797022785593339163737467833147 +6997226216417469287126591633487052682393389785830401431520389186301156111690874765557617527284900760279233718743071166517225051 +9560345000426728260399841368953209835209100487337639538936954570820031680886090876109665021023200759147113373797389077298422122 +8175155068276335352679806117326566700814346009488428499581701401590558929437455938924635624938224553855781741120788865291783407 +2295536946679503673362429092591476438921243024065802176610048872694278802256805275897901963915809617356852239830206442834187860 +1980560976965565165140981488552352620248220002599581191144636050647934285203553865821505084561426414945738261567792216102403148 +1151908591260497769379834919810177114427370448525005415075150280396616811812913926897078347185577604045023394052213732465977204 +5152624009308747654657296667409633669797187558140762249677298229159695566185953786763883703575683368256625993695568514289670471 +5898396239237604417219674285036697981388707533386023577734556605753254850219222927862511398982564982295186931657039590148578018 +5932796785525089331274908323688642021701636412080489905006446995970747450915290182470813258134915840332610387871533886024451368 +8138194206803987511384906150527820013265474338857553899093509868964543274546783521373587071739152356855552276377502435950664343 +6671221923018976396144288089085545597962326938710922261699996323322275457923099268605292778087753199123847741515682942101433343 +0553836973054122354894964985734607674790792609106461042226839499946875666977767744836458472114784382708675816804952000351499560 +2364997010884637465860014885474868656892220025469289156670808481103243684995557245150848863996066555128792257705475749161690025 +4110649532154387762250909130921439734079690118528643911754202049214907597563788601359791270067268169269560624171237553348861595 +5940616224028249469894892261151683448506705471885912971357007128279832881479611563158245452847817654715394431167451576558923691 +2701950548115303007534807474569747157173002064876638265966317419898895957065042960548048125830083650783906003217529190210069108 +9448438509033254495362865744281724512323137198768884725254104680523096628653622761003126111128301647813853623032837646549389708 +8800961571279179907403520239170262557803310568213744508717356574441764473623277516011904000012404016704294869157929203333991614 +5621867976685621217499877201966531190154493625522363719922688125680349437187226891224242675375445798588375405091275415995993660 +6776602064451444697704536229609651834393352676778641614610712731028707328894803760218379598569050464723477953936124087430950917 +8609350069628812265374110081821823052163446196500492527833170210965201939797385205224718774402651526872691305286672629217382035 +8488697066441224751559631066164022076293716903983024132971083317225244640782734589625800094058649006568726351629980984900472989 +5845746441409288197669763941632849306888745439411555999404218596367981143977695990224865577494042079971012624349908586273448094 +3944731944003065116712174476397673714373918870846441482703374457072218866041336694655582361341131880499845103050687313792470342 +7857292237476195260600973001695477364449307421802939526518179464787823934178232688755800049682688090660718430541383983270572304 +0611341821464316878794523833266573224644012837504558841156871904475125652999503438286924058574554384509785291956022252381864357 +0405626679943651292212678664558286589975871490755359605369624889948784641951887207653741241486219388021477385980747033128784838 +0349563685997861866328510113314436119201124870929479544327167409917885140426561670911266619220856732660355093308773694213795936 +3842271417777258599324767675283259950186722960735488702869088413460362934496727774403713216022454808579752649987147082209915211 +7262086846799716572297263071380322488276591389406786696601706848938742607301811413241169596156442149745813064652698654676224030 +5774876540806221409456713575924968982273367206175097023050483195829231424480633395579433434537883182331511499750784779245974304 +6819745509357317439429415625378554567362388569070215909977624507836378437106698876357999443207523376055325643289588788312580731 +7833148656904363694334569121139758945904938915660261426660062808527488257575416983895078937523942074734502850315306918181450849 +0874641305830980547277712020017148651915548171250649493853510014278214375521776229277631115079740388430042785920214556466053488 +8302249778400205215684880334891997915871809162392886542987069548288428411471154533517912205528047987025944754683578880543791606 +0358320982227642668267885208147099122293592290139450664586369209811129197752858004803010616081626472750567559363814707696436535 +0313506752837594147209139665385131531998918025147449755897149885365821721038971422038604629058413601987368885444084132780675032 +8698373618661979252204221364880007737028947769589369624595251994724054095272484618585409406539000761630748495654887039290015310 +0800818173175869261167775704612174999230343952148568401102639665953327107535357309312915124706207238389207300777172747117401698 +0640073150863859675544943154734483059715378564796905159762945298307365280446171698542814284025032267504158534248530901828343035 +0873201504993233632733239057534034773053022444340692497950449652566396669145334447109018793398061041589027430348848568737315985 +0702523531880691374404054678759180464956226260133696133658291458560416403819468440250477647013266610552428012647058409477074911 +2679888054788444461424298005407130238905755507645631966764120317200939342102226493489457200513433158963725232826211305616759400 +6659241756332251314783750854468182797311328498121933543014291040088680657858265588241030117849346100683667795040402222839296842 +8277427249362157833641670896337597700968219737912856460330555628526942419632625557211562429343845027815601523792180592076379859 +9754404607631948362062894438912954500557072195072644550642458309860663622333701157823267362150209715197912528880882392701349340 +9392967484360182578733939528902161701930343748675665636284278981486199253820383071398887821347218338053979003089742707812914379 +2904235361284953393462862403685101898073830804299574939556200135991635904384199077467177409693740584708928799458835571980205648 +8121614185438088992376830836290018683875444583668400234525664787649173577729566744205001928280399848363250846307170382670891227 +1379156561645180509829419293369128655964653490776933715927876086576622995107120980971557236204092369803081979966524323872401105 +8210634652239640140303181717302977441319086010060056823202877048062527278336721358234059108992008083109338742139836036323492605 +7384031376893772770300054085703713950658765604751229388016547464754511995220292088356538057488534591958960218276518555883565725 +4074775956662864334941163174462487475635543748972688834049926463696503634025310758502751556398690346342648412837461668031647927 +6365750302288055740676475554487791055706610049861824646116595120976061102018119073494765991803213702717674747175954421813209939 +8009331937511328151001411154015547213063838109162601512214845406533014265983012161008264010599651427488015290161524354573353234 +6470978945561557225050621564665550894387519941185513668617316911095236397590508616848692919250154411755818493336504839734750031 +3525111855377922515708294426372763399105358667430235504503228627410198905334837234851992023849271193012460177555030458178964902 +2832205291216922852095126506355099210740893308816704635379794133382523761361826241501772732710458333973916837834880210080414250 +9084082447350749526849475397900892283815818083030975632291741303292925823304366055091504295072069505478412237935230243114270458 +8733240614953524515745727759843129756538792646920469274157284227528661937418719037781765126256474601006075476554791323844619684 +1035243868690685092085007530104395247063420667025726455865466535541087734271159372906562129287612463548495628710375386966180600 +5440542501876451157868698528285357586655663503645304099842630588650168915815059917415278238955617758631802903768822104984447821 +2018849475721498910886528030895133588236820109040257215930892013189827568406433756483461049360024443491773721292902184969255790 +8822875627013591731684908728396697952056849491038175568000507526035874582989907019189676297477355884544173196568562596540184740 +6207027832469439408465681825635058880584562540284002435337574271598044807397129809411607354965616450909115426381853724556891033 +7221658030059861817521753782741366902139395897501991336285575875528980544038829626149666345302799699624831576475243396464744984 +1649540253799052538081391692469181324705607910980425515252483873483980593877409055481936471747872491282539042199279780879543216 +4236145733824367590860331474571745015558798839670826761096891349398910243527132904379844441054007921880393265550231971508677926 +9325201276405157061556423958329163005949768823959483768715570931105074676418085399720398268434277889429566019457186074673651582 +1556755082109554008455021604947841187746491317217027281220528571996910808491743715098726471698066342041982149950618417860098084 +9612051527440619756444098523237719085720236857461176953239899550769256325996619384467815272576473677824512425753715748473823644 +3293706412332532029368150574309147409666660955800738909802362577371939197344269404239579009973609254902956047445459161013030585 +3889810572293351817285095560307955387570122899356894847953368306309169743185951491528192204482516204225431961214922057114411745 +2466537795850541088028557343809549480793529110899163252329393397207265339963882361084755962562912216879860134066671514387618346 +7860354854395803331508980945959835169783983572284248855654573657487835485311040917221652836888254950796354469715116733559514443 +9649687850645361629683896013084952406507914922578933911589068814423571506614639956792270631464418346423540206296752844238915237 +6020288012912630828121914201273847417936995938147436941121877369382021368921214557882494996867880498498027992143690818597132057 +7464732232055318247290946075477625177092427701352448918522702341636498386700884661390140882325113012278493389309349523351396213 +9696483275515491260944768055252019253491922403588385357522280969636691193420388846988546363998678336385777443993831313531774583 +6282522032399446565965739736752150396232120722412203505719106262036358824681398508892551567229346012725815333843066908465157756 +9407628149379572734413508114861082667244920525693627019900560538557779908157912341364139167913811711711544677292025506854245811 +3464634373606321375959043076084467134891991975403488809906777158185809859079955799214529934428440394715803296141747865907907489 +9423993889790762040142434803114305323935291193328498665927373291227929076575533913306444776443880643225751481031179680675214923 +3368470912062297700011980465345274304044785588310672874869214201316579834207228249128013243107286248089143869069547088156308777 +1414590222708121116814503748513196371955339806257298864789960510451079590735116383580469454857965360874314128844784304143454069 +8766678394720390254448078037404671513243310605018703312189431738938607517130277658972087080448195787216475306011123823302381254 +3391550184839202307004590614178501644771630600861096494601392733266531850599404613838770697571965897048614790913072319558856284 +7803230787138777594218452988887094396749088370117209351285801556001096168575241228759515079363186428923678980339871398190852582 +4019696628533985636279505759337532628472390302841917324608852947709111244515112700445738982371253756187679740389512024539739520 +4770847899134941839230668652175028834007538603739876886601634289336067458758592877494727624461743546957277223347887135182085175 +9704365765485920770156655545282270424007413902597806432869012806155483311798101235310454409760720811406614955012826140011450968 +0313480515097495573412839063893028999786645804567919951099814820194282264604422806064547777371073192487100289094441987847150568 +1657543112009039941765308947835121413396501421019711136903259340105124014460634621836811024214292779775980478293474663062122820 +9705767650372955367904858685510440746508331239334572782478185604370866609254504064506288513924921428844442806366648997093161941 +3489214963851233247024856358669087681654387218345765317192165484089902190489810844772030255183904855574220071570304690043795231 +2065091959221855386411960322245401320421129454685844655947550889456851989959823948526569728196088066488438899595441765150072821 +4429114819428348855107669954324003559235452502739679008927728613312163086830142833511895866448006257958165946765071157730382695 +9882679827690443780755577550740154748999710142655073573245474660654485200341800425388615819101330831466686148830980748666684090 +7163405135812565179670235235614927720441373285793867065291904861238349709700184831060873375476942470039510660809272264903320798 +4462917767939061046448018044214701368685457860623889616757211734619696932095108903120448131130054208365085745531785639497736359 +8218165839330532616994196742532527473553555966673141549749982047712150106818828620285541917373915693522694719282613944281399835 +8674485087809037160526520795860814268331241546826667372648249043039257847644579120849321232434718628926179993145231262359579173 +6402705380929919120941181769537062173912605142768108201761013755197467816367902585985810911153925853453654100594647121313245913 +0666872833826033632401371436548685693080139022484100159022033636174658329127466366162032910540421919446428986161784748017789598 +4723199613767942411247930612753447325541505390129994382585659648148388692327147276519250190560063905452973361339890280400124358 +6913416888336681958297148217827338479648390575317941960750631878321348332412104016113215430987661026879407929196484877377640061 +8494663204235300224243052398541725825482919073092074355975063032796596426605495082752541621103997044605964809112021627153615430 +1932408491193512375077001954464823146678231602554522550896013165127235920096414910813761330938337552089472955671775628068540474 +8216732019553272236739723100215131535386354969462704414412180804694918899617675023795044921436432605559415902678545425651517812 +8025247190590822148102225680479409002058106823871348717740963812585351667254545007307100968605826752214724522314039708032085185 +2898242245907570507615621864533672777989484514162394933849338212142607118088404798508269488444104500180113716320167923336719325 +6087711919535546884547342110103435389748847458344462232091767124995184518172878034781269231230242533893228603793308922587510107 +4591474660599204864799066811331150365409997858447042372594886218949250820247279474918230135657009456785941687614114216570961562 +0712145092385555182611993058740320634997625075753366666542910024626577807784857468873721417913984050547176339602792705731039726 +5904226911569176720780206777188612328120845124057780320649670149526594811111907198960857717925291216166167928639613716137663067 +4307416456452371611701931195073322788105756639763524372948315105493615715372579618838296610933712476520491005806495650421658587 +8328103991782890592563008949384942150246156275152343753139757736003563406755966669814359044466955928045317799518086337670918431 +0138083133435376046718367707916060720252112445257291714225747183751131216668718597844623190537189131972377334685833227229634752 +6238721382625606157004023423898943602807707672999152909288657052128785368828226605301253816178238566671806542598902348311422328 +8320139586923410723937149546492213794676826048701166669784481756530412597915193651538414740148097847037639468257092287852640305 +5479022830982203377478476331811758576901670233527290215872232363518816501458927070562308753377018843087767143244104321568604135 +7369186061703793181459759961931363593408835426106781211446368072605366366571248851597783995945722926222426820630491316650527779 +8828573417201809585216027386210261861298732060253123609864528252840272478045882739663756003736716392199489082689850824432511971 +8111646983909651153366476446390221276039522740932580204249319217339578537772918571217444956420257260371077236650618481206170288 +2935054551970379324008798030984106801055948226939968902171687217907155340893582643366397894579369212912093597160438593641683669 +2485378257568528389277310889609277386833678172566456312002344737328227016110453362502908568225831332260414575845627976066579941 +3482852162228716106007258452673841400279159876124086723321209861347673279139378292982201754962295388660515249705755465339980807 +8586012495756212505119149505421333725699491270326513988475156468533804482583378661782959257850441126447111003717332538997317723 +1776086627675735007130894098661445494477133237650830139159997474669449285984774365441233470966013545796673243563388041724650925 +4871204375981896728514583939521811481161849278118060344334503543763509824336823270998963842004711517359429526921820434423765506 +4949119417418804810189631493303008698556136954410752013975187350631734375598695358460132019129899037320268439555839944615572946 +2352205983666568424098911534581381260867463768189900460175744875437666192115066390019086651996365032553554531097825841729484867 +6396761821871154925250940721334814793716957966097731930384514803099552848004844714015522077211735687647117734547230837853524693 +1596031648179998000220637673943792334209148422484797868537888852196932416293373918035158752836265069893009384789762509582269339 +9695024706050496247098862490788952376578622083442656866053965079277898118236124486093149364786379996025483226916387856904200464 +5207178534801739733741859262250257717063345689422848102283865760573174574242830519134626672907219548945944321186449133371889496 +8091522599148708961675484538617588113615426169855145369191110338586586013267131819054886276010519517578355159744969858769754154 +1409888192657898889323633959345553144675637666158270953101126274823625732454329591205013981131246606853736622675628645701807371 +9520905296639004266979383969455080705361676763621326243504360544161563837353776252465278766865244727756979870759572455920688699 +9581386171160687133121725147597447299189214610587624098233087649334093755829602691095533964193964555227483958747057992036054166 +8721053344317784071933338712749304229416314171955616978192540365793299631623144229526476496982594982691261090976385827573975900 +7821592310703338108652726889016577851582851275588755649801212540201927028394346407971389167170778763825909054893860501536766294 +1680216758688189041294684581042122535978201496230771039365667061380634389195094311357702877874169983055267901138828354564408227 +3161070157288336465271541894601581266294703722546655378330486847268915166615856440605517140340676687703279431660743947236511822 +7741994422834194190860106225475557703581613419594307564273066500384493526007784089212735294217591917854424698645245431154845570 +7453435530259083182485810144347944810673614929556646679467919668579911140259730381818980942598880746925296760172341499274790409 +2328005309406832991426090784212693827689769275846169654962563240745452602435209585710235753460014401490458336687380630135023071 +5366592739312999644456849006548399917819901894358497737731098338943033079178655254808256654806548883980302404456845406302737523 +7903862311453131512315977712627346676478344399846058399879029101474967706599628983635802084695117268292559579897221003598626395 +1226042367447825280036345293351132154926212820976803484997329381495188404459278925205516668444497544901886948777923893344531981 +6856828147900151315527595804653628550631963940129883617356848115429626055627960941822258407367220366681493979858804129725050531 +5342427449777364357294403483728929672993293331804282077540029002773232860603723492768689779320843055290078442091489307669129440 +2646993961014124319764296539370434958358249722078956842849671467698536721940846395173191197670777284048312934901527571955362838 +0198542559113109732314309858412233715215167131228324026800689688315670022802995393900835564840785241774828835077665262843753444 +9550183762744591451958276893980778053478437092327149970098767098796188062615518630585477187237482484844353645352242801527565339 +4554133259430221270883589344804016824319680145988501368070988742027201586296359667866696516024912143872664381709357471736498210 +5093801098192097809224518282804541968783581829389873304181998214634779593308644761544892301474532546795113168811568716153209669 +1410860371073926432124196875939453572716037044352794395231284292433400177009767688979893244784882963302195811252949131002020348 +1963827673323077855623143768105648991600618303185458508264555147286917125868559346485583695956446151749324530132278047444739067 +6874456031627491486993438537612795741124963717273593326065380547768578196708046010574823669488232340781667689355939847002297664 +9059011156256654537948571823030159169978023672567790424242785131594034312912382632898662220373433122635586320057460971136026042 +4324851680908243245093083193503431558790765585803419091388697495752260988154385332510991556891584925155976792883412406678432776 +5169890219592441180988843355694784180780002217839745830787310928598719082559628331291213329365294364549008762394123896795514907 +4029304916499522070046006091292088087865888798317559803991873835903034439822636248876206336683085957754970026069257438125567024 +5122833726152490145361691490840338977240905109443464507374230617046450541939205551391476680636725777819582830317640677472291259 +8887879121383290774133008351057610071339235010888965861277804156037797773751958774639837524541250075316907358014661336099985243 +8039061153806899718406603277773464956989757949717475056088907096202984062415028201715624897091896396812284772715615886727140592 +0697572299245622972158982736674917157478112408603912440899066443377237594315710291599368603222944799827489902803052151032150505 +7013603271652485049620063445001242816749693945247727354086779497318820787447165373787134099987577277741625609459410605718145900 +0792184428093364028147126779227116462983472771162905140500152025984437549037622220978921623538498863508150200422542729027788614 +4411728159030287378714419332373354590292666051672346415137756916116829485503628039118411108736302251425180386282192968531233608 +3508034331771430567756401746605566004814192531598982097023747313496871602413228109505959309225451717487757635602112764447022631 +3428939513944422073187530514913804210377096901814387277664055693937327821063211837805670103207586535472844416128884599982345779 +3765013258236529201610750246167509469405857415384290132571908547691847116538830356937781623544344792657443940779980096189839626 +9024565602139835552337325332903285437411134785405743434839219379066510469685518924233636921521087402359934033983973385991471002 +2649847962568274167726165208946386788572121523811142222528040146116227215917720262618074588389904637062666528544746561556536935 +2620030538510199023964637151697713281829456055441798954059894139613970616409824903577033806155241943903044175681019965795785283 +6461188416880024087073825761424480883587286299792763357082205040327994699188993856940062456204962545880583868705703598889787721 +5944413986797134250694896844973795638537262177510549522817774683727110576630405084332924025735617088037709220742387561124513292 +1615973751057225002195236737059699517585264539200525680337988614003524501832730480276366257598203042140690301023569755795697899 +9008200222469601333665860661780983941015300488620597776980944258674129973552734738060885268066420909348372616142102733523433267 +5003598890783029733738430164315966147920008508177297711130783619606431201578483617231981134136392404255762575061631480099367673 +4714080230392459893569173674100327560113467502678424574866012722633975629063693151545366570327641280682622355521718987259238801 +4739385038824895235096103000674296240229268577109938375885209481939108319170129231324096236414336990925757528939018270228944966 +2361210030668060864499350273214477998565800871680472680681932947260121232176894643185754378295528202618772690219192407686919753 +5156059412217536746848726448056144396547837914584560922069735882550291341190213498634134528385275982127813806960692781201779053 +7966885142214723811725348168549783205309168240872140544601559983829401790243561220385975487819401267336194223205730538148703619 +2086985085185073545036414851529838204481235650803097024838796015231330542654233028002529608154776678617456164886732320375739646 +0833105952434512974899609076351921948939569136092110228925816092118172001373677700416412487399777738494320581470807407424704877 +8185614688665309617727123329966574828512583312557787490171669059879359795468437113583190694186011613831079902014081217754134725 +7131783601536200912167420913195076409694857630710552837087267662216383925992631278943851251589580445676689967886327863559646484 +9888310805734900531289699098784350167801387862535778038559729898236704661594447329313866535642213694637004811239940871137589762 +8123715241748226553144892581245052850529932097715696995932059624924557269075540247991049472713937530723957720713793716760718395 +6093034172460809629440737053209815131819638726936540787299401716655839213840485847609031890782549934060830350478366179240604434 +6633327894940509763972045245237235718323112502675490128278367355639972323817271838806709048068724517131782516093900913433491465 +5582353019666679291426183344951264862654895560952921265198755899160545875749490172299073507227437486882523588711701507976499572 +7489002650231525223980434094738594856401532028510616419177773329613390523750901433006132084090755184848290450781826037290985462 +0539323854296447671459242043430937704073386171637825236119340677027183609466820564020370547139050173937355911835070350187485451 +6965582154750741810065288181652685394941853476132562873871859640024361243300296805283748325817168878484565585632754351908511680 +2886210467566660836736517150078351812871682359935951946868379376912994012664524203941072154686353570579681603704825530456774626 +9665362335544365210262820037867343620764616695075643840510926473275049084133707571284177399685539645626628023165977084111958392 +0177820463043321585863992470028736425683626513381632161117010851699245594406701811450201210813697392240877275493182279748367019 +7250981623715632368538564967386387747576695271655268440119068242579103997269027655460321699463398768768277613166901925556040578 +2730921762116366900155596821064099866827590787028023864778747307107976624095851829841563684215591447795700215410952638220148923 +8642501753068607515960160175361362264643558443610150876742595480796702902130477857992388072241713968623856298400849938588555032 +1187308644994319305723690746675187962892871980887310999328792568364489164541385679772588717510375815217942384186220430941913143 +7605854751867100384861067666196612302899049367936234596159545293447341885894031370313641111815396044152641244256403303821921174 +8006560725678932454993887223171401960145271946857211912769628951507047840598467703395747670926287903550867839502877316151413748 +5747061868485506527664044246509977629632520838572005928929416857628593497937173916677705472302579778727680532363758751491026938 +3023153622006915734476058970896478596388304728569523091701672491484313935627044876208043141561946195882862365574965158147452289 +8540852691422877019070250316861163162391778272197273383387116022030059687803664857766804536240988573448290819394431972776651275 +9021443311810736715901066501558981319880316837771114344162148017989800388690774710489918382047318311988709068050500771418272212 +7599513906333939122554708100671399521304710290565461919067937465641955962609388316053120501043224440969693966990639899533003437 +3190313864768728595063193176469208637792413542897116168001394022157595453909893711585707042678368387735204900683732630413423309 +6043003917729256108805619361421477310316625655437814740309514638761344462517487426501249646525209961899684542352037279524879981 +6813035487710494491438894246146083397270552071559892438473910394670225142289451782904407545380437720612171136664498766583513111 +5704425501897608539934816199146825064755643795555188152948142674213138361295684789361927194878162187162220359319096077044697338 +1170042273866788055113160203247185045523345121557337349823787766288071325865947069357672811750151752362658390610542086745537803 +0182650295086344086825098102966797093995388649163271139864646408250278380402342942834021881070263294572807189069134900984272374 +0906668841731928141540118014775032666661994283184008007242068962044782267000273516374045366478779586459658472979356032532424400 +9941559950477797261120283165043361871515058911345852440301289987592080477208449029732297262652194811701835630896804400526302181 +9586422168434769367306983212903836891536506488360440148109923788291834492143450307170521991049310812325101209435949795579328604 +8845618327364168114007785559410459235882830447965260478796725064238526247677648804067848274177910485949526784938820751326532317 +2508842138180368207641530038974478572707059622379262380563191970615374835570345785749767510398007047615953484581417608147080414 +6150921916969845997446844026937752931358496152526223613349123080269156525204842351030887676736715854630790139383298746726376238 +0031087628812333418550306049252849828620365883770156880965318518371047868891562748685920081560624079882621440888924178323074068 +8556547813934656680983629186991073869359264408784267940961601835440468005712703124932656473056207782384291231611143830784642169 +4764006946356475094019331519107580885428818699148255825445877812529420751612646315832517900095651843319599421976422128038327435 +6209251031135205676846184796239439073588798115331056299413787815047447544760251266043212878984345103105431840273907195809002871 +3654931993884850780377252970969582655676559449815451929264323578834644743068604576736541418957979028943061560532360261664725787 +1145330876015831126155307819990268909839073491920596329763420311698726840745194869089228905040694957988171983477254803375572985 +8257437969668449439729079801079379205727147223003815670652927114347162605507272520770488283403449339977650008075057150876981627 +8526350745722665602676982911174850303165259851752432760991481586280102205346134401601648981884690319798225175260769925489404224 +4638271898447020123724423300538195550321048052825314720561544239062181964769986570926083174181155377779882057584541395809301918 +6106817249634524848855511452769182264081765291797239837907787970553958190403210902425419707216288535844322802242224844682625560 +0987012833422099095295557450557916850795807069003361331869709388270541973078686998103429124404642889230735965872322730527661759 +0637705394090134454432592452488278594036085049343720666733736875565892704518648963039960363828210137149300479545174722900885474 +0438909745910598389431226270312843755719991811348892485728211852221096700168476622881940496574796916264512821293932878797623865 +5429545451401418793893535526425687987849470147455674595433976709030225358630706349020692453458568350635883987698939555558472630 +1147145098629672646090540159437091576295858909288901226882948677153091610088843747423989951237804279634707873305141146001310035 +1453067258355914230711608884942303119909820139748995292741299997309447939834224950618749891034712331269306844559292626045946290 +0350632260797112269078999525007760708741928859358707432624911128337604559525225243942115958112015615876335010570860522898738781 +1402978628312279477469329446037732401120697968328000813953667479029085290308524043626235410022744559135665074491331563473370347 +9337757755044278436081677756600946862635252212939271849678018763209718297572192696470355304192247185638719953361091249281203364 +5405880207322161294391822123973103190408090878564615457135856917654118957703067143223083955082019279925860379183853717628861577 +7799366841975673356259200026023839542055869990618462031135739235438635258865047764693545067810235010528954960166478294307447628 +2027467028086256924839310527875817129214597755080392073496545377860632428440004957401492609606750537328284161904594401425287572 +0946928415702129545673169191304792948477302426781915513863540380482590408165885780307409257801864775225156808756934724074392504 +6885597618942836774284752567772379904321306380471869189416734595292130350179577547119957964477389742383246612250778027835962842 +8375378564184742787926400597138354486910116541106048723078444753508902260690644823241797242756355368838174606975331846084479482 +3212968824505986851162688090961261899834237009593445525206396148505570975743205626589129717163778397732428341839974826649188402 +6250784433605236331523177619946160387836454523224441645894992796160653797716355919979290100324224314406791061386652716371890152 +4400989435392530316607884601954369502551234892963245103254821300716613610559994576020035792556553491043544497572846045958494663 +5437689141597501417826982456539318735177994543195003033035189307876601341936559142966278723819207128349590261078232012690448383 +2240565515565951781099043148895044873938264025267367187036427905278273314523689584565134739736805096906657362307932608184624022 +3576329018410785775670826940638079688618282322990520892260090365771287681416891560096002562633570097542699787911417555916230717 +2453291822907103982064112069025122300625453433920178638265846617355201994938801382944512864303440296743023682910689965580508346 +3275346241221900813564939920509014369583021411699796257800366335780055834288641882272217012847000156622653258224710526325808198 +1761823047477639207496107226016946214853880512328754599996142453344143869395811453079561049784458849323538032761380820785136205 +2258773098932947134988484220635633194802789720564239663961212480883691013756304617477696306260192353528126108628749643695576613 +6833244981505084786837439890389565039428783638198451342774797405426073205683183254178026538685346206428799444442723636136435573 +9080842459252171303872018490896413638818633597602353810855257937546412235233854346618680040369752485990328489156699457496819504 +5734956183309699603857071255358660306282081929816419207249476965467352584050195709593369245076183392383809049796820142290983389 +0663950400843838925251438784404925348605007257769726864605575147487057019983649102686292523010863667700171976798290888401737766 +3133554911232814826547253321321519762370231796859406653514455605268584403622162920523841843001241629651167179835787795168834729 +9915875102181929094696545619337694209374642713183714372607433054204210229685880508097695696888633628128273726076768020385810595 +2987095117021662238907432053186678763052465613665295736124815230360572136345177452861949998291445717212884676001873754746660396 +2589155650613427623626563286829473395485125944790254164155482476888221798281733050713864337019212875437294907722695910181965531 +8195108479458493218118347693154287549401864008165971140576262020179031350059154171267408221692360825699802778787719114529741055 +2994227854778559135194271668156980433699910463754412118741685322605625614010496732911474348010716497147621915462772990056635740 +9885103937597375812527288674502022238204092589207503781840088970784707532373479354579114899440649532568358342845263798451171893 +1120400390759648596081862269355864764100400822617213332109125827310532445408926542692499006220714769552811438530408840934868695 +9637352403752939344901814991765254063329438135234927589317161742716459240954818619945185528105201605559036407476820639345279573 +0967725725520221343652248725994878515580037117626550685849684690414565200284452932891511103030564811419601323232328459974123969 +5151667166514928638097099027082126182546856178488195923185506331245117707219875304117100373941938587261574361349595466933901648 +3122056019614029244775838489907181693402454194031630939340884628769127341314464687448572665248434305566402372170442289658220344 +7261444332642187051262725693834547125915901896179000263992637439997458766108204237403853459573909864356641282495892771557042023 +6977452997091657310159593635773017261586291897746068707373152247429150821576531204588421265308077483457611680883539505380780214 +3922860418447742388907173493206552834918162458673611316176697519544920126038326376534410690399328547067085820148163398020288911 +4131844303372153660043567296613439825919900174540571926796003444468128320273120297033737036976412016725245066800816469650561843 +0565592489986925533018758592782811877964316768421296500160427160146511217146432195095933744734673635804950107620352420106654863 +6645406729317349776240373018557665618972564221260681309297356903494539710063100566543717860365197783559220679304253096607594218 +8395581134093404449211217585032937525052183907591091053649490577579801329664352206258963567937765723904612138153463659172391420 +2858367788679710677058155486700833423902690944634848842142528244040019800469662248189870722506296487688337321695524703236140988 +9562335938642263288699025637860876864417622879634601222106055965060418025394207515941236354344664880329773638937529679471456359 +3869629127425084307826072099872319475018151572502025420028453624372211412720817771154291359747053407476074728284771551844057529 +1190185706672366345077026144989614660307994733718658435474012628808621940611349704306495070342196599139286091306151010932970462 +9675509101851273861946201556462769919002921112388488480221448504521451553534147109710141587683279463216951321945573166307639217 +6672697005271871616738575015318763914957713525828413258526916822948066768551357315714715932196912191677461856418144576263544333 +4907150429612203297659855554908990742757537394357200499303314336795187732766056993269698914296065846201908473023132752908744956 +6150083396086150456609926582054844276634569272470435640456573226419822868710777542210573165448531019738194685407995076708226492 +2678786284405544977160356904372320894983311525398796027674382539921414318300162458602130818377816924569056780073177178248997811 +1585303840969268907598807940355932272770894001504038659615940425154386157294260258784536698627961267225191113300660353374029144 +4866449090853757246935294981633041248861600653432843768265539085865624263955878975774708775312072236190746445124355521953038339 +0619885412382828558999175920193016334591402367369763220140054897008235791975355396141026004273210423619062237213937060537525842 +8976484958039104695613723977251336268561215466702146950426183444969792826975925369611735319989524615202694816932040830397174311 +9564951821673710247264553143623225237946783695563652359502341538660577772441234930760198015851060420035149363908475167289811549 +6152784468367404157734119802017549287296220448855141063864170092034156049229526992205564211402819767239639089903951267480899195 +1084349314003385023967647400823528491190700765335630550586981662116778098851626565057148602128362406840285312235675524872617927 +5940674859942896006453306875133525569054425493267698876245403823203215376527985578976643008721792339745957322665402353157011446 +1029563432263058113381593903721587363928336506888785654392441615160711392515188910321841843403492466442469739247826795396883496 +7172639941661127457144015058984534581536740542737103665437103773621233103380626067034160263026558390443198760839727420004712631 +9914873013707039718441199896815680469326923849342274027255430706708553663676894461369886145860075954707988128252410758852549672 +1551922530995012072301491677655182006822391780785787705916872826260151900149221019724939267128249948853451126842232399605725462 +6980483373104508461112547210954800274259585332622017453698311994963928004018795768275261593898964198667538969022507330787533788 +8940619208645823836214853361748146283829606466502798396069751370937123229891589445495370452889766339397264192786388083894708555 +7956574006856378561036342289467521536334236359576203113726730817643632648603038458281607027513534064039819509732524551295593096 +5939647576077639500586029620496592494701352335060993013958711501324112331829653462612315976140447947573865479895456805542744172 +8296423959232512165579460974983453693457497491285869113279013506974543084139634238126476293873284263151589338438860044240330996 +3782355276288558083473518137834514003242038994143031796041577911531844210313799581374155317783854201414730465792201716701206550 +7105406465035531748842630956573813909293101829709167100331101814849787778408887961527614275809837033921260676349397115972110690 +9643615107680266865840715334731751157919061855580521203957602157773380969437003086918579100512932764454793288187949797876169125 +3320437252833110321043772044039248321368660878095814104499448998676046985620748260108371162632325229365624907230743947154301806 +3552088981131531046172808385452694435204741530370783735986679874877536428004908486014008409001068550950540888673128998784481805 +7440503329453373467657006789991046671852958451887155184801369224781302834630349870843861530093008719949904514151022786314021716 +2709390621195321800279652002200952192418979261420927427122958490897500556999549527116421814590121223682055231817153659082402135 +8861992826208042869383343763880314283402643395875744203109296333659792215641368608715044344315779183133340656043459727198349780 +2734853139703811794155039071217208536677070354393864907407146911703003457454092806351920281851834356124440898064979131802059323 +1762234053491518617088128044036706357776327020285634889111113700954005526394521483443227952188980642686375398780347144135711592 +4027397751732022026309364751091606371427768246571192961054471672221120559486812200168063013361795376094462318094221786026438363 +0392038021743121561693528064882454352142444617962196680006981383051817202169379782864384242280286939946138589599414498933359730 +0182070858522164858867574874421689046928982559988292189961850204180007351631664423841067686122468503764793368632417463823286265 +0700191191614031195459835157848968083002627564348814468750050055060620960050671214092145749062318287857521361265309131257106421 +0309993332034066779054757057884677047198784197094514566927373158826040214788501636945490789604082561095662632988383214277496388 +8146430655840989789550968117320096504739356599535135443521661785643633703575650679430439992282453217709233862686803968241138826 +7498984628272507825242938050646337866580174070972879570659031107873223367860364402212183428942145402115290592836227443279445979 +6978276114539892419020829624768340474210713091442435775100177879638010349673639613552216581396012901447719087628078745824194120 +8522095336564609176473140966826492093656451125709888847406519188859548795021576488451306844892007565711047730272408121668647900 +8524767921694471514347227180326839537161579546977834075174921433329341884828393393398191832645952423890037661848882505051151043 +2944580067530192277404349067174032715610611074091485944569506105415953050308842454649185132382815370040650599793085560858695708 +1141032332340176333446374984947231196716478639118202128308413932621313966803046203929215874583358851861354789012133869833074749 +9480985593249065406361871874582636508161948209786505592954264261087303579799611153927773325602584185048591266936288024303976476 +7322835259534879703369663910307964057121123765236316839734559189401161415410496300232441653039670294405632163031483814775087095 +7887660928641822340331849719497888363217187995951806289777062854449559129016492535377000453799053164705981391283757823729876400 +6020813393115291018308864451756852473844549771230553274019875173525511163873500444811237248088951714603052752360262642567865853 +6201967072308522428055969416019910663232530141827188975690227560728650829855364963013282370083361622353100870401742620986225707 +2869802657723987122733883328526226736949680510879014974422147235169606191520933028844627505009715152702759247026991603640320467 +4241131697918560404418795016499808486429648304834823604182336854359989655705275261891160035233666029073733408580774645759107173 +6464198087662041784095710258475229347049766003901523983606673438365749432588484287371422883288614656304912471987516429205303532 +2029089639113957845701115097046043607676790299218866434022020078360708383436488957906770062559366765186955585351813372430895635 +6464504289920784076224333417714908863182256990967502428628619515039237321418833968508382684390570422398059844256244914931448759 +6529784435502409492045275382927262616866859838773431100600230894559679830532230924353989524323228472158682810144077054469603044 +1378563204070129032409938042343154208174485755861395799608824865872543930138962034925792279431758568493941395394754047686715676 +8867499345143102872671197738475892292486346568581939250404454964808538362896520373387072413955104371842316500056740851235207639 +2274346030168140429762079586632175197653839654847793978202115595048824365507532766797058257757822432716594523867292474757570615 +8116418268033688475580223253494948603537184329744599083311334953737050247127839879240255833527455107807664847505065047983484396 +2629458032033571400314592548790506246485500676837502841162113579334258598171571921210523629971864377357109335285657941548264619 +0524276024669364832317266608416143775893545796330992023551256594493130557067547024156422646526989759981543218213803553279470269 +7049845656048970965053022592938828183548869262256486091586477018991837255857719144475359084653581143521408001578287580561915936 +9529610539570129132154472887914287431871889778678946943105713104741010716248000242330469610192173796870030679714772937881344366 +0545264556901297919417188117578751509627881737016436472816406518703771084849520268761391513510275790321817000658619656930708957 +1126630442325657535785952978581421903165028332745161812379117711348541046986438165712940213350699237254925482467553006850919218 +5721411832634196862603413999653849373180502518946059378586343176647830400545625593156677745062236816423794840646924829283523696 +9566889818345214089079695756528442838502922581513940730675014653702918250796815796276266282500947845827720283326159797147319082 +9317560741024728849503640154852784294588264197172188120649331980986731376355567665746330692183280397742589194244588569542845631 +3577828329965372354724624706136258680035391223875847298805321085928193394693686066077198681054782793606636920380474314204706128 +3316822771568615892652610314018713244424614540516258058981790879447066097870072989300359291800915299516058342141748356513095635 +5061676595203394978368391202469283356586340799916518917181619890218937093078654617621002548811013022192072875054917226111499567 +2293159175827100175340065081112035701753220885751903235819929349733241991099088182499008327172200193308932710369190557345577793 +7287877879088510408061450710486988984638957628372307246581657037353807145126249736162916823230146250853818942298633104586163230 +7832024984477944259180948598164959684871867948848537597567339850852013498502229835859692193114794153895539003144519694217709429 +8852158774260606249546637223424939759148603134570091996953512424441839401422606873114052812717989308319825300637980593162367504 +4443868737007280155976070920335648272365458102007219456742917706885599061474160397874769568586074099945991122357965710810304405 +6905794745598083595623675442819199614676323588131811003338385631872023890221085694977414242799825541355735846812034684719203047 +0457011399186690441251124301781521567240706876614307067167646882762698974983127872249705063133235031055532098528898796399551640 +9652581539551646223682272835407450811885069347498654352393131589816331647166209087054200913809824196442611900896852634745826036 +6159069172552131589074592658415752799244670288018595097465086916954714517572062029793973193555030320732773860687445556056374048 +5349001165930930982732148212936887040004758525708856986464739067246746909509060364496603927468208068059374896651091078566710130 +6254982141720413268677308357696628123925721390150502950572640806864637896561429156063059840807574455370484872143372214422469569 +2175489787129382942852202790241971823638922108920609980574885762787573196139571654120370722438096597478384547338469999448759583 +5779677161907195059713244890541696081649739347821725617254499816041573948157136918495996244811587574062062152794567408848302587 +2529817818204143798110200493373689828340608796471631850589769739013205825657411890682974212480864416875107897957894840172709126 +8663943826538718474564967927014748693701498556781723819074861406711142155938775092841806948205850959520646877840367557220520678 +7792447422465445177106699668289442956655097062959653482152033676820332098951332376110889269353539366684304599313150351375802284 +0121046685822850120961765570267263832452267770816505513149214924261156051159905321694021977473033018289683251782668439030324894 +8869657128937432079954235872959567283205921860119040052090516147170061627077989260167387744379514761600478703309504193576305611 +5381561915778733491108127139487324618000049052071735127115368151009776782599438434551832876965146125033956869670863387976299721 +0949362683385697791271115461446907297573048650262795825751967646029677499691298037462050809938530416641818484223928338242122429 +9766820839983470977275951789763019956067562051439591397901411073621068841669094149499180916451056538781312613407170881646764399 +6524347269748594984863393554929474287488214701192085748591350202439606866037583191335302568028741994749726846505035406608976877 +2807315518903493816008029815670675803410749049697801438415798820359073479308467800153777926573033244523898966885887408576598556 +2253435971693966005243248611373406144205306206628977772254035623423592393288603285157497164481644751059433660694427679833172764 +2802621839144263944769655055452576122669485060013276462140874654840615169333830556593045220363613387004343762888697682047116295 +6079893975072940528060570984236651746363761678873252309931727588216926947288076839743652207728678149523270862367475616321714661 +6924992946415404337508448250054422067066309322273329866764351438985216249046429085623842949432537700067268248539671410894502789 +8240333706088312915806196572306355577223650119535205051323962302240268709606481125217437426747720173549677961990418013172593155 +6781963827534077258276934309689853954408488415498885594714929736965951139633336078246485722224486449322009472875775669311469575 +9358007010281073437748629060353526682504603164238126641900438090676758229061250503441387135871208008336678965627603224246244049 +5356708068957856933330346985639541006539430131114293957097055267024756313531830324556261486697384925986987181884646073628504968 +6088781492829096627570053299366443750840538654491246720498646497885473443660031433479078885663560146413659899024021823265546612 +4698755327188359177076358836200498034389012684305157808211750883923932482111066086636126223845187785757048164121301047675382008 +4187475464845783395113764420886751702440050366103999318308171948006157647282740757274766917041519563322385269555023048099709528 +0571195389552617367126852668538451980401879674162973150705011024259186076369903231002763667511763488135226006543931654677750804 +0619275562362999032336486727097255819748979447931027384044606019902431591438640635467977566427621673095319535200747603235302696 +4867604449981342383768009732025809614182230646572537976384930951101533738349930256172344711040076994914315737263468451025930022 +9527865696943210023342715736597334765710291291516121893348495349788313734498145749015674214471321122949483123539062028956769641 +9765176523721839937410174251025970316488964877877371126040698433360574265827174483074565633308845572364434646203497713132542776 +7378651477723436227947997821691317672343016210083663443777377182287912898006288444888091959123188074922030160615537818420033213 +8447189492947088531974966589067358589089364485475991918412867217338548681050492469752464750298772894247076136639226285280041834 +5454360318746876235873485912565983134779435530640178745325450114100396790737296942281564432762776663339965033474759509011312252 +1273643557029602462258772262515086262035743506025601400592823850215645680844288819770466248021756708616474262140257830085882080 +3749316335624058049683001015861489646556394155367973498744773935515065243970160613827515488862189910777219950612111041830333142 +8451480666232257772267491027370674681436846933893722515862731717525412660342458091112720313220482317659721562206466780128741773 +6590252632454850440238194247507400433748088629955607174486085725118788995595732312569896819608944355538496441101640124949497789 +1257684894384392174484251124503854845960148691603301944875996738800826596693185375085047729958430171553740436704869195063748372 +5735178593590752060263331193146970071579446804835972630866651058153595857973419849597143669821080829676089777998056446703836684 +9360907310859244502541304513473974522087020370096397612021384703925636952795854616525882877878108950663963286923299046586524224 +4736748114460524678435616682271235781989815828869520783049700875658488695718736121600271316948975177628944650186309894116256839 +0122265134749874764508072046641714144031116931952372994620287753392883858943809994533079487667165865233880719017940897178306317 +7314139776044966808676160030164847624119585864721750026892048897546015469276703858536945215709271741054424782569701147500084232 +7758065860697872155705967293772173137656465001477901257876428931468366029449777184812467116641975319314916341175480998066511678 +8213787357873005347083763974187729721493820664650580516006337664286359010259386041876040802392491656717630783835571533641496104 +1539726264552952565436503783866624559510200362458151625173082533487696535010112807775556729174429917223668593210128877186508887 +8321470309679685999475535888469479559195982218922599887296055650137375133750432460874145869352229391171673429325865805622380834 +2056541359048531447828515960692963929079449940672170706409337197174760052966541653050946881379320800572898168412293068662723053 +6860373992960297640329729816572073853997874011764757398797944036292846733897735820446934726176024024757470895603256455220029335 +4569926110072668348278664329257941057438032693980297647417188505086076585913708391932628924011735966680243663432137597450444007 +4011230494392556854932236273332089389934747612548401261998294903567663125297736568382740395489945640372131150054623072337132335 +6844595318925439547616931848005923901307170686601327728283791660587722758968622818978597514280133292570491476960507397213932185 +3642313707941792113315508020910489195204450745180517492522873324890987317406946100562361363175345008151634614618218325839525506 +2427692898230648928178646301964171781644216637535917410571457842164951317391990752953058484915364895813865613835337731245388877 +7010431184482412816244666073504621100130866578971286677458525711336280431605171233723698610913008947422033976946085031938114793 +0877515916454772093488878527862660314336276524158489551235818954456016892617227185085470522620693855915519816376238143161430319 +8799443736799287358483938545063592502641553678820618531061987381595925735003006045937462381053795139314188939327005235192961861 +6759340733723316487592044207138087167698990178309899096979902164464364089126370344350744470572331313727851480014896363857798812 +7052560097987652474276867365814450520379120040581256992867022098643876725081745248682691582583629292763400638018100065591005268 +3106774405492401515494177672517042073837303505772295474776502959089196445416987270369786947855131616223965006450641692994332025 +5855582574286277020845503779828089079164761761910842413066849248818854888626393445367304058754822066188520015897444131019700263 +9198472044843711981048071735258326011544302082682349755642269651044020224003915379747598044212429909878394016016714106491656407 +7815377808589332772544290119723706266101079820396212755543706404343739006942558946534655221133089320660150834947988585978442816 +5845893139549225714132593150193123229669033684893833205775336633241518807901847649594321897137880458298454663559120194776464523 +0400879116612606005027575801357996533825782630360057558670923510328348895013335023158934967383007297478557740608358705255176983 +7266683495403368714481550525556638200455893453737443660389337826017810731088543589057015038655883579293647540385262180527785917 +9515154749550773405200793344557941722519749405852747512969927332977178984307036495472318127241347248808326472175619457986406723 +9518368191888409210468669956186116750302181387390597344128917877355951593149853095697850106850745402883068932235000062455347402 +6771238670568019918070196745624606843885430053371888544619960477580892136283363206743364325496202732003423537704812210825777485 +6482929731992099113054720411204927077100453390882438863808021020983016716953161187024589380636187873278841457216002930842647147 +9086580408882584727439595283961649388079282398490521121307077379799427026353954996467076132654318458754445407020982965846766574 +9211933498567658239813690464315557516873798138696846826984940696375072209871675004339676866312551085733908702422475770451671522 +8116601759554721517887607233127079564923984623323605008410478991117346586220447102577578587384958136197341044972808673740184098 +7135146663669772129679997894669791690347234150535985440597324857772792781041329129123706810764665585549577867626836104138395091 +6171338134734949666403237923355751356314985925381428193696582138260452767497882006262979715518886220518431969713213052164146287 +0631430030464172255146123719714749762611746753803707576735960990848350628824677285326419791772891874510486806367078208015683527 +9792995724212035781617488647953282247761468865566020082106220151676123008979512284726786406279413459522157229217252165905352918 +2536766618340462486482122665210247152075138704341907443423031644884751559830028648873890163595496896608683668686938870179657834 +4207985050934314372862959059606329918091261977012574425026916852113090691158385845006538026326042605485030114114277066770366995 +0403083121980863784638665369135497943380554934143397132755814677874063414921737247611566476833724738395380308234640207731607371 +3017035318366003181883008020161130977715784016586015636467061595058652062869875350107267130020471568933409734170464250203156306 +7978597058687788859694752086622825044743069638461579429486476003366615826282892684888720644513019714307671128574626623134255754 +3225178060856930853571262377435203804205020419212526191171244624283460959473575918723283729545170705498401123341048944419740415 +9391613362743685159686501403436221835235931626375947101387495019232220333271032937655938586143908770909065649064710101953494614 +6134742532053652776330925214492259910738874571044952830222662538392022245336085949987893286559101791675451325204933710093660895 +3150091954112952712726674404458997796890860249938694466648235513572474974162093062356467502005784802581269257972393197375619808 +5518818055326965608441276354480052128774778908643152331498791242682676747511171669510551698928872813137576082004507231113018234 +1869442353797750668263237704646436989107574039444714294394574053634166211680018322567901718394177095139221750525981664718345874 +6533895638718548550755018968962814261467954303937753456123595613702977896395623181616704065331577434094624992851719491928609615 +0190398690201868871739512766894228210227318255924906586297140986714101681237768762073978231156784378903908446734627746278198818 +7031625744207762150055633900029138852849133531239672839021846058540394772383588961074862090538883314417964060795273709151209027 +9458808924606129446923894166510777560369040702494184195952257501705875876785777050661631817125173124207320372163502777664965913 +3571992627283246607384090417534713518631913006344389649603958134572814682981286787015904497353080342805307719694940285614675633 +2468021847965102322233545270789228088801077254585303970545296705237224231512698609584385993582329483075661032700231251161660423 +0192713398720255736106913269405387578211504390721637672765578917354808072397586108294538148319234789867624965197532780016350565 +3634551422633123938462201786680417505451646469018713382722643171537409460426340292141021655461958038710203141816197429713424286 +2547844227738650654059981449340163279595536790994781981453043554676804995664853696521066106987131049223085671186846778658607485 +8528870512517578936416926762107480564174061090150629794502095506665701012457777925443164598343164919674568006002249856403680494 +2627259489744462745552373437029184782032929858142986221112288622061305496355657681071521590855144706970497835664724912122006599 +6545391156443312660152544214378226475642958908136790116192718647739147551525217967757101506789062053773824320749961986081365820 +9807243716856995753119727006541625638999515827040154011379764506202860312616961954158869599676568905867509226176218277805150630 +2073942766462908233406965969896132634247796801676354098426177499898295515362604488579021522583028021857262554533983809115858178 +8347131968232485092731301472083865394183193666260253997648555077371404915951205500140212998582177455804377214724298329527661383 +1594405101691517143629153539833183982495777310394687166385933138800061962945009756356701243200756906586574077923344666352145020 +0197245967822981167194710233250484229803417371108743341686105467298388178153158024123847938905011673366872874417478036778662336 +7258512385396982458981727808434299320938547592708453258132441101938548683407696744955847650770224320115020029123283233005466171 +5842172497719189784583681309409508840094316855599405125810349460717428156132068389884811129531014665690641559952450142631134435 +3120882941134196144363251034342986015603525881548802680508329909569735321266175483509995349121263055144465010373367684170785550 +4158635639323481056067212351252293942485312028596582540399759383658505450921291350114073361212206891492359937785370602986743900 +3248674367436665982259747983227106329784209726320992248877150489251570771053020539956408865849029873307934125408634817191607873 +5454247998356721456240658773994350865596498454154991883699218368604475173834985657275975908620474089305230670921518592231403386 +7351062162946891390363227270397968233004651810177775309863584286429947693132180002624305405957468154601633114090431896806560396 +8578733043161456354664012245929168398575757646163138337526746639222181428391936995137008559247216584486677942670927998985802859 +9200314360845834380351667184778736242928721806415925103242410837162869501355174790557366226463713207146043374288777068201363073 +1884840380691470035098733943006470724355475360919052895346183170077857886208887006329282319454011758272954888828936243319780379 +5906121108044999002595816055500975401842454091232971454535066145382445630442880279299409925299375855072691591936952415415855608 +7427547736672177870270669416923472904176994726042270600865769556468182342137641250127961474267696372215125069986572926580684930 +1283565631209283359581543095011894542058022145233771315616803743285668523226044837411757706026740916352039927306329889089649349 +4270078841384513763036784211367232205576108266657423596077336951308763566593324577257390215814671195914760219863020884678789417 +1094797252284852472558752948074017934025826602284347674097641546447667688812462875419412834529814608042996511263599028929742238 +1967645060681138182820884931078366878249535027007771792225806188604957386423876036386080705880427884926800392921951044407013636 +3181566611581199324907272529442720663293855144200609139564151757908337725071341353176155541142076746946246524270499526229012582 +6539374062743440091085243475167651698361455560983223707251591133463685518000037795208788191244350973724875956194617703899137414 +1430821502040369247348912270128694893351423782952149316135243013570919313886614565161358637079094402119428463930234964822013013 +2907845492061431274313604396416192334514224786448855288597321564420931438182778758210128537309848899766746069430202195659612336 +4975634130177816916191624410591783555638901610254745557743055701024321622685828565315892206810167787457147305784539222040470909 +3426187794037654633853814952285180680406547839298481434679476618025289610702493051123252356926166123963245694601391065308079166 +1560694708414872555648575097110162943981198563540849019446854403562264846931619983425916923587136273462584821828465235074422917 +9261979271695865979574032703343588466946379682055875729180379739323087400969273393174411780309814669530363099378105348978942035 +6060070323917526453062047842877465074776912567111669196792162790002591482979059554932579116312700674668070694919873464603223754 +1894071532823340571620020715489409265478540234593867913381245139145940908648942949383027523114581382713294341230059675979933347 +0475001838386962980608799805753326787172142674741106499743819708928855603482127536974433291922816520616537305575646417283646800 +1407873654137191433607314317494815145886982224527312502679184216570574189580738684704012553837521804108283635888649341886347674 +0746298331021251518403002440748995086111379224255590828431961727677994867561042572035686044830391992128329363200090117946273786 +3394767633292754355096521352490384715569381533683416319072637548165685703657478638050012390155682264692286629389917053497837436 +4659095138863302858042866246863612004771584058240848977405503610643181773657781348444480569875240316081167661626347187543347285 +7911879124009359945066105568437670726515538210712672838972140937144499494198784053146413298370949806905102557089313540518536047 +2387001299041180490187017325756798839893484841126540625119691321202951445764540357103908077323391792392357443606419429134662703 +1311387664343269938420538390428981548672980222893983525849983661962674897972783374663857036358464131799985348402057774789061484 +8274461005931054626156020913275021215007841002783185917701579638808358640962009486666957046897388731341995785176631521816719220 +0104967747699292212229371707417874791998945791320492326714053322534677090261456035102963665329391198512545240166335302214953529 +6438295329258114463806507486440728873144766916788136148990852634546220929994618127547833759089567150425084541257262887419748944 +0763510822819190844430827630471119922358130707131670435790654181644862502449964467386506947657809851105256327118765427086936245 +2850131634530202363427136221800593088389014446941597649063703366121196898996686256705732620416208851855006823708510518951595042 +8751603030878422139220116631069675693727662503215348707642267571256820475012856064445690912370994140681017385311381651837184429 +8337808674514385942347760110022916345088925240957432454452424792146056100857933925448423669501901023480223134384961261737051238 +4413770733662528673309544847059059161282542697670213119975814848684886926540572546980353008826625285683672206383376561174283585 +2470179376522057019705141639334564346002921054122405868536589549939044185327352857788406922050956188186959812914331458433168181 +2179183768538040087965101925166314083741211822328135691819236749579736222030235331381929418960851621953749706426251383153679388 +6697638737127992949029219495456980219928837272379992149835670720157263752765848341694925460132961677719444757263352390287952092 +4093217437367696270862739890376907333913414784572650839225342564140670817115158483271922969611210426005659701735005263854191680 +0504868284114293941201391805305848644129473248052919285116546768968370400211720819698688057290313111673099410825833594935299522 +2193387285480585675810105037896679125123370546559641586098343943853768828725430307316784909701983568646060993578149869405653861 +2934591075689506327550526527978208181872636707486069235044785075612292358262482648814374144402086535106182407978781842722397427 +3764043018357659628728101790307107498150768318990432916850503907920600389618490690749145152636599605594698036095369786495988258 +6957155927715716313765479257750780421880426026172174055737331392383266819616843853222801559499877374057106670897236097150948636 +5427827682310804721340200045877249335498355065518235785095376215323629999016684789788057540796773407165234965827360566567682463 +0931247633662980846735922260231265500967979148521116776842985570907612389971711829938070907548905323760203422391349321697221760 +7882789193682587165711403864901445874796286061561018238012019307540878080647271091752229028944021330264150343818926048069144244 +3829234846305757977743751433953176195429230057570537421684266250781148509555390682508579644164157541082064669789683815095364787 +7604939164496265670478495045765555257915418238903189606069500034604107925625520169975834751123093859423683572718773062691097465 +9536565688839857043404968751102502068918401627692404156712144798677665827278179931457967293092502383995754188611043728483106278 +9597088053951878542145390264364113151329590157432302502341917977233379257169202042072712802096599668573120272798291944153363375 +7607153187541747861933137718609189822023639083176590969309128534131402001720049300131597786926920331444518080594310667529269598 +1927075329456693896562301483770530596686903351047943440879405951056391325510081534440975417269136288862213745763314617080452571 +4793088300460286258221203503876214260288694619740795021100096527276740956582412254816142704747725930689538001063488725674114094 +9964651944235263688993656306565044507502459642744800444126035956827838480470927964887269362894767336619648517065502062761621016 +6129875810846273188094595526245494198400294797450838878997940481886389039479255372079045644823922532826285987109316717080651452 +1158870497644577891842220310991400298247186591687353490986090963675370236688750155704648708257105452733670085922368068618952508 +3951586474153083065442516047977847063483146004542723388081748533379792081337808607910165722930346295608653437371334690508592931 +1348798137260798951844725481726752185101778730990513125012632961668430932041485231492712161518123487434868038690945449294611190 +6621726374054578571840509185550584952608718450715584685612358931363303426238432883301985179247942967408291991891183249754595852 +4638603540391846589823234253913403535906012903538328082937776681713183362324668976651509798060188640593955628086698902190682737 +1314136004568121579468174164410670114683609747309631647110337083615027244209788221757184880343776037559026360829925134683110860 +2743916110589248861852284646250771021137678508599837301158835778690463663553082493519868901155377573114305779992715972859595723 +2324106156194950900453760356879741607730365869344607490682695136931314492042609961479060353962654666877592314030242319785217157 +7037168535189901999950898708785124945709252827963059451445136357835564541820565609455192709322939082485154440865255543678569770 +9896528396971770400160528156211153078421941311469208430939294234515837583618887123400678449175339595403746664662044325400329517 +8261684885541466594581605303831216257932122772017718190045767780517645346911853652853978624682959833140344555022435113362676062 +7591075563360347900183625940068873872381827943795851765368024187038497412028598183964746465300474453614206176090307713158346489 +3374371123832409332000144159517441149498304308931146086397279524284139118811410468196667079250510555507523645907234786379143531 +0501327353064942573752656984727083020738199901757943687656464477159443799349546909245912076525128236088399431523118269843367482 +8613396151458427790408589367894556692924575676708650976493092298687011565830641071449135943255849354462437020422889320020232241 +9743887743932345248629409670762729564753828800124861494449000192569258881214737190612239921652995520457700328791744959299446113 +7181302058928852380763877100101757115044093853940723929209952955676211472249441292704903663335543383186649963257659728968301134 +0388255348661563721653151594456333702528830265445870529546222473972548233646402200780627751622560467191291529956825231887562559 +3526669851709736895997935055319638614407522325558293993145073133898821442020970359820377176459127571138998803488194418913023474 +8960318055715147737928173496291103297340011637615159200331936657806625012838282253984371770704650788787874077528191805071976481 +7124181681951361717176138891836878456339953961111533220751215226320942283255447821785246102801554680118941275936544271258524058 +6005709566240667298294089625210827175355499791073268532752944587487068732200218887976443970173147332812756704491687536620579417 +8523733324824779870890616722944885692254782796148238417811553337420218355790067774624307813310722411932853894530043351212104621 +4208314480849782328920489549263981924145677154566910149000272410262683233827903445768177081248117495913152923052663893776265183 +9942787773857699223811513925994334863462094677096830079955240744467436994912827703793978101340756041298804409547081408298801222 +9436460738640015479585458718857778968516432654013043215917075749663128588920464616671554576564466134592276012302780087016944245 +5682263919237157166619832653999221365676490237394196490562353201595416072759662040168607378605240879904649331264075658653944374 +3188760460625654041085468641256306887215807672289682883500357801180231951265973876967365453175824452139544361480252271534860563 +3489612971636788170285039991175495323686795856259626029428439316305263410995491370010061781397716962628385590075791887118786041 +5311327330508694662530309261695611418858308192623059112240071130884198746415297821301302430358551661316850076909001762997643991 +7081644935973288072050639441580584025029531691351232277986672643518925684337072256344249262810387549624475090299911463668556533 +2269997305729536278296569991307939885225827949212711278683260974334694618965859001446604029046584493463719653785353337281283459 +5669018591396191702482439366148219818778884802332484502882028894694251543216632761518648364549018220678109701293026473478656784 +5216611642715035439533559692613121139357309471021194983124318134640525984115565217254793009252347891534891058775512107043759248 +2075136627102497321632846769129196363043810766007725332614332304423370092169449259610020916402169233919813196199842388493441962 +5246508710864902175515674209765293421695856922741891032048768583212886538286815573096956696932708558353574597425799726517739782 +8036312671737074585001040104552777443725646270711497123342871845885341214930320018750607319709014145767035316932229927380726251 +0928548556204438876091471205246192546298228533150898999146677973522875037893164503792472130409496157160476844265385841682474739 +9233703771903675834839283547585483443926383504808637869079921199402511859863654798938168359018632590649252624952570683979540091 +1817221547279336881324191918747916105439761395909101897519754743532665651720607836479601826466707285601138161807075561528644357 +6507813758533570073431536118758351858756886547831285242021547001802317103965340085013359438004591048652288218222633904693766526 +5939230267672950104631374960137514687520839197558900123956846637455545802654473302741665702939840951606373658402834814254928601 +8899091999774572904211301414279903590789989799671058329810205159495566404297154849221520843334449344645035156970528552300030222 +6795649886498390391442973828383660673784035290138239807079635557460618362207456840361649348840778636144683493328779008559160150 +2312967328418841937893181097506584921132253955130661944022425054340098558673444397656011485902801524597729549862183491017801616 +8364392130149044788968141245875416756772094094121947877022476352773253705820364822300001844264751712000950605882778006252697459 +3173455702957684349309232875767407323242741753471239292204928826580888927330167241766284958168551480352850879501139241966892452 +3677753858072439789438100033278254013052115660199724677408997086225534466921024361672151595117485386004966423349406051295735713 +1033555069901179500855772626876468463729168610634688507726870943003645664213844575844814874933857419440136881531279514876328051 +6902431911053955232595350941009434993084829394137562877137241909400659809326832785642273291709998822359902244140526746390924526 +3598394230231172238038637497155035177663453584383356974229908339909881383831792443286626244348781344679035766390554002582686748 +1986522033280153369322100953745103819643767567342601672718065640720320991063977205827436045236651592119137181084201370425284749 +6005671239600468501156161076186950720461257574394357624099400895752471978287422522141370855745760936595412914180235254380961690 +1248432656688783102677099988123915947205672369413239452092262942922895352363751338938332396260328925306803441684111264080959934 +7043361968005295354938534781857472787124249512274641194624952254981027355960086055922235901916884500390183949326239914435749876 +5900769760705257904732141569222453308536443186947518812790214535222294555869866712002026766787105689583023017833746154510155688 +5602689240796761919744677404697558271913792067417789140292202440216539736230658588620970781000844620550456371994946471279052637 +6983815257060123566373254198705908764725007305820906210985164414734561111049679088421536404461082246253221142258100082860407483 +9787291977224975917546280204714841440857941451156318425600052380180319358748436390813045531423344721155836168161085909646309385 +4242317405737007113338681399774018133792533519976132559943781717607228562711266632582303687475696334211416518106220430942562440 +8001103370755032373531777436355301854609759653124445242732075706459884158212645376386153155459030396899392086527430085304137547 +7061352207839212087427452963168038388774060848015551117972221521512460122751953993512403134647980990954916284061921026824432536 +2639034110356917513777636397443311536328136804898636889890450733741495575783711662670202058347655591164148410498512181752116112 +8649347704311634713230582416874077536749261618798919119607870454970552954421518115988625118329175121493449500857931770229652333 +7998158841528743978260913418602919615786831528127648656825638874370061930628233997562266898051264651939263641927874506380487439 +1169319905978231587648398211672101766334393172127691275996797499245406750863796639040056909542250284633880406494091612313210866 +0317572128063301907416088057966656497607301508200217804677889135103287897506118599601843251561058377858126583470533794642274814 +4690147674695890988264540020570125563698789420115878750835681272646540842126992805764557197985389253255527910197264650144295636 +5798557941192958990661384529989091262630978788566879170732007097819131006336590152363030155153413704404260928053167322809868601 +6850987001199817141644762090308087270676753863496477045191783385382914059759989907264265980509298271071343239640773196627310409 +4389027546704283892663222266104915372633460042956234973983464977451929104395584092104280081159181521725125037452942372440981206 +6675905477554320026243055974304148345728455542853879050278442548087322107774875373656985055242105650999387599625064478196226162 +5421451962648003632206646262643905337837657920982111146977004270602922886008501562566012949545116457517171981566495094457953170 +2653749527617823956383177123455781619052217065408026747322738616321944929384445431993242259268884094539560852964142946672208945 +8593461023191475746268918563613277494098559522521079968069246435865433845367704112809898386514701303501214965468624316127309978 +1864190494247307693037266993358200097794635502447995520938928657994547718890764619887745240985736437934807473704761456024575292 +4959121097171972205063831718099245216438006228886790229146916547085014590026042332430672777678300137100806650898573540666242384 +3655898393459642632004541036291612193425851747783279795401104056847304510228417238922574433467131806129145867390315264166063875 +3665579208506648402815517926487551967245716952452342336509872884242496327353828450388024010049265610889985970803926589219237453 +1676400095916770050334175293677803863116549153471866784271105726012628596876429530293900006500470621643237024986843048814960581 +6303887230138382981362522022235864197895867379102358980372057655831897690287262572451017096062107356032913949946624176978299116 +5005151176823509060156202781958197462286719252108370751542425598689093613819219880986961622340717811406826396835254749852492414 +9810879199072089316354376552239141698257935918222963398002559226752773027118660428719265494441568442234264661966547139854597318 +7873099218250455002270603753696336287379126088262705830929170584620542532170428705348694262962278699570554235741942919271113853 +0853097378201921331418681617335834926523454159799372408812168044016229123296540289846574743942294543985242874224122532498334672 +1490594866688679908867416614545427520723745911612132408696699789261886952468259502210832466240663473598353736119026236378375966 +7870513434223037902717553793135590258545562641705185546860782749440609431080399391577848963291799745623426666134231053217546405 +9446201870461219368892707181895875357863356844636097852807754420432074309846001997057609966109454410826649254380546761753175861 +2761057480832813584246237846523502525922933474798627724801784019360724008460215340269094831023982649184534291701279975122991786 +6747259709148196213568727070749193520741364779274424639307959421349714799461578583982498969579857164144199424813392167578660320 +2903178417498559526330742642780679790900780361831699904848420604973586382034364851787093357442413598696765925470890327590522104 +1850819839601928427102795504066478105300817488452603819717168073205087377109510653761519361878295067555599575161123548496116519 +4527800014440544203257680309603709183834604758224679743597378438522340853371773245388147497516647805604078269186299047096706230 +6498133304861002880698895980139890714606455707071690798907848409609045542981039802321508092873754941535671659985239738644320953 +5988405032666743320254142929561597654494413403431947291535930103688912359712380841156852022207805099821357330225186728532082090 +5547768444839618341918220237864475011631317876613320699268333350020360283617553420136149953194413589978795210753934389762807735 +1597329107893662665482951708145282526843200385584016605115945831916977370397498490572333755725067314085934032787782477551454989 +8425608912417288932359069001529317862434139520899814966998471303722622773872512411876898983545914085663888123444680470167948505 +0434379133165396213883414761877309648582182115178764180968488658223225128338783723464562383576437566308911246233632408447704048 +9663996367177301008227471941256368607999821009483324708241288407336982656214907935931597532047696000543001055538357367423684843 +1080054249718909513852149584485289714580193379500564799748250805755830508642143351393106365614646908548224506838553676335276275 +3599779694595660288226660287150289204263669082115248096828431008225170485272045909181946378454893228880239576553739907190878426 +5885850036699891408378122906193395367309415591691241926759692588330412581784093438940668271882079466692153184239937668982303474 +7878175454828248795271539884473981581982185106585811627218428403581816331240364769920118073711196193240357920319448764087086718 +9383157433735677366088538314691739001878394272868474296524756761536638899101995873845026009993980779305151574599223381548532836 +3668859832227797331952981131294963911962283005597571432352599223356213488863688452877543275627906341710804574808946808990599277 +4561238331796792049632393877616772107198692076748218412457533127281687506143050581347703755397549089646358464889763941628738646 +4287040673613995225940479030356811166110299081667864088662964809883579567473005653984288637714354335572597312336813053771357145 +7067167172790181728147512916597493867072803164173683458648511730308061884633961295638170464699169569357249648229427546009552241 +2411562579887913980455816507682929933659712945876292627871263596090842188654178242939374681016381916432972311121058175582055725 +4171582020877122298082911042822107332503076877376941726903575210668249614607891518640837866322224789915987615341840992350582413 +4400620040237405249285698955673350511161843009038833792839643496604322064832266848854377214512824709405806535993728236489108631 +0795420398459113177519012283723869747981824916722015034363766243369266801152099213940570568038081676315926730528271844090048984 +8736235449371678977947912073661875499227319547901313273871106645387069884378420956151320072268281471236296252518071417869812398 +7190598905022008877003332225740349664805580192380729307470319849086922459614014566654582713365646665851557294353631593510036953 +6381552204220558217529826167930690334372579521383903206328505035888174852614555542283456959315756641772011982607288085297853686 +1349874783384878827773365548625629981523061054390679273761674753812182403462337719746346989770142792526923785272095298042279054 +8890444037528702976987014556298735324481383261103576223990679497009263015064472090831542516594399577027449370238294863246623636 +0336415324118608522678012840335823229090847361254516690870558705257294993006716462941193893800422308988102399798523789694443112 +1923359529945753787122624465725573807149022119989195390997561541611804328914617110055244794800371476974622186846094996027068986 +6556505900937451676724626863029277508817962303859724405355623034880805936586542708527460464895044273023827975806326940478307069 +8592969583414428893231495897818982129258755657382413367672348099201190652878055544751096757376276397486791497622706985221853529 +2908214067134856191896613437551307015999440525676444599333628882928075642102806483213033250837448355509306721853006604031173677 +5140214739575524129341946985893116412008106149871779041808900801409064849278864166852383463573647719274024968463791254976359299 +8847571119606652336355242986018768524604507411735194803338041421026584303361841828532589747052208612636774062068296366532983021 +8655354613872561646045351810915955927958378666435016019460717328781560793954138282423423334855186987151419088975164710160358441 +0049260799204162100696414303845623303274803491195157588921793903647526766706979672202295037308810197036167390246126788340972486 +6425819895447745350399433314412535917516434906996482513931003678441923085763619185652238830813722611071936854791527455947241273 +1522067822697883751451720158852174772439898925807262503934468921130385503850561421474472046621939053613032754307326343494446468 +0373529669975958588899685583408106767873002561071952257411549791159364132364256139331134266337194556486549929770686277721381623 +8699593258141567800940600698126100601323395696274203924322395263563919165964829715731584976772134953809087705318959395613991572 +4912970610760196588068419702343479920035578132234944550622679160245214714573304402887577273891607583288099465093463035431694610 +3905758432785910691737893861264914959634002377051354781210095863851654985098474884630562136246052330525659496807184083910344772 +6004796506916845030772132724200525430268203485146341239363234592583508467825890468047631822102162429469995405739029446103026786 +2936254882880449606977442748506803270167246097479977977827447584435796513203817330397221813580364844509174311882193765156654656 +6587840516915974691152625347076034414185867253449830309514472874859872212804040804281552578684775759016088925401886242253588410 +2136162082678649695650433358835719979490577896244051036608023014229544782516370023231616157535453095718156293168346470255374077 +7531511647981041419067438947595659704304601529630424537788677553031920743289991068705064547597248153946867352942473723492529159 +0056165493810107660174271077633956520600765977621759777545425851195592595759244720831020706108951012298629188421930844092200174 +6642191140454110070088900972029229475358599582028206307088525106506003848087788102905904900131739063517620939825620312143365317 +0312737363283798477941966380713988316123472457978241774897278441026567305606285826664686434652740990763564567342068867452842418 +0562084816143782025599524978888894444210155619376771675521151554796594041637061218094181094152665322539245369886205219847245940 +0029405789079825038952711651290857359659229927344229997950428696846444420150303484487905236128836752670607196713086924265912777 +9562937968844743614454574043893713056224816312429825464738549426759328345347674841205841958347360767579803402491577142236137115 +0533906692397009484027899629862953135204910641799594877569760336943034406380230261140675352675702660262675371335224685084951739 +1782949915965578201764231201307179274400901440982114344774078855036237671988151622145580473895458193522528339933303018706591388 +2624100076711766326191346479084275280944607050965314374143191066314104835834695035041295363869510686680641979042639548470149337 +7092183008459177550878010123635992307868170928762105253223870251579666192277376903965022436031647547645865700232025464698571152 +3958091554628717576397311569044621708700856680671753736603162545139430701751294444844592994780440200375396353670083705904818899 +1158314027781793782715830446196630725174972102720930236759032571130842037840584037988906905557214975416804249578167992887315936 +9206031432141826113994005343808438088835006886050009647865275969654889753018433197685269199002634601456210696285807629459472336 +4978328489062061200804108082671938204870858983057488740461332416429168822452529945581949038775475758752323372301506063733079805 +7738055653647518591404108351211994677622659177154884456890162864455435902069510745121823508583748140331565330826498449431014143 +0674653178829590152076432119041481195951363053106794226287750227106636545367810520707295978843237959311321898828560534345440055 +8038620990007430479290370430002421271720574168054663731040351369363896772189503927548376910184417619479114290519008175905383545 +7118330213438952390733597389988833345665275766596109498963112726494459652684601428898616858980054673653547857404417055361966561 +1587037091185968630608431240360245295772438686943763646925036642543820946055469856983284464996007864493360253900748905383563580 +8745104358873817076497065734218673552778861792932465886098953358042048623712145278501699290745480384052380874247143800362538370 +6251997645750683187872814541336341689788360883547457593458471177846675162375769376759272289262293759496680413378317765946726569 +9307054144320271279835934039640502070988256340708987512314880763049398513262419383032058404111149286657762258132166819869740391 +6208679111601546062938461491996426928719837110414091479969117767614499297075986816870114797028327349760763930021316392342344670 +6195338739848107103644806413130142942032473433624064988762100818410644021199176248704887358790343589713845228788844183335779186 +1206612776301905227273380391656909985775786692526546380296204581461396277505352115889617471623174433491331255641939009226704644 +2188221413330256108594133828534885890151834345250541767462459786862391647167810214086311680548340646481922395107065550293841987 +0422053725280997492804556037165046616311376881740818579026242231815216233417949738101116086586716392810175144442461269235250030 +1446402688506317648015492449267953446562377233978895904366659114613140191253374875252211719488638375761957759777329556920444079 +4742234209552120961058674900214350131325585573620862943651827639544267432934381233345762116672659741592229252195548093031985971 +2686838533752676886446680927399440863720061868372851834815109578907201299828776117754439315722227995606329153759209194448678185 +3375830903591078788002755338696975595441586005976260188707036346541857769978992173498220429575524475532930877665817876092460756 +9351987353933462923395374235495972847763917223789357531623204700947268441087397398182190787387926517802157283116535834305980686 +9492332135897364643664989358260446062172464533880475533980243165095186005367740562491795933632109829107385087893207785894397713 +6433768862702582156901779207532027093153001807004150434114821705300991777142257598550961217676112326241485864463713890885328141 +4370423630135664116118217486548726531069411489498186055038679029605744527511210064431632487827249384739496221986798075711280160 +4424010886610887880437216748921409021429656729228727372446155715321241952848523185619209556774687672654909818027324033748491821 +5647085557243826965464774431484515106691575517669395491646712200606275072075247550415886971532904779039992053369051837694948246 +4648536696394853995288135284563546131345084094639405448329957974626886591260968153163248670156108903690012016896947562864068947 +9335817025706673645248672808655206936988229988506868855512246059364735571373658692183981236508753893624162179393300884572562525 +8454550045708811850843967602772364889207411447386933113146884803720821905371224276398839301167373376393299604166530531671536817 +9469613971479940226446065344175803391558840869442680476409858979317446638566865770776832153252927434674599753345041690804354354 +6652209314699751202444785726308628207785053079762876873537555813669271605409315256570040001991644970781331622466129179499281915 +1961444894849498696928429106294385830964606511500699941607840002024216695044414216800293243595307021832292431154543081449089313 +1962091622249577961316459709116638715944104552499484128514903058588880092425282180366103208523455810521574844650965568635887113 +4368421159409460609897114428104781398719260897374658539676968461604386468189383954460163078825080336107872167576349496495408685 +9649536539795842583228826048018046721092374785553431643665884938389496673231436870435149786802809160883735413992798925929876704 +5235671264707293479931013726706891341489539237837731840177933783526465474894432047982015526419973235553573918270319559547403086 +4039735888376366609018757028506161014783678583356813250677778613982578318008203758267120384291005999279443777316382864592311739 +1217739163779382661260606469702245656794710548611797988738048091846590739807732824364552075368990683393959928284317293320069867 +4916615035875154768600732328677330949063202236875210951540534956404960395079564144372822026855682749788976197007630147709880516 +6877745432713411450666647170018266444463633969867263093231179811756276204043142296040101066040921365338143870829243201412736978 +5181332217031028793650384590762724542630876907198977592730043310368021901782721530053394250577461993296684728562642042745578204 +8059743569954838104598948856222744875810352375978155248926213114786009876969666524615446586588275065587752809296977777941299366 +4960623850950107986976011274245247425368704858396317272530283837294761704449561833295704741998045363670811422098828233894939307 +3391941685834457637274247913999521308752675840541536848418544996656831007389255133631777457133457716285009673478699826403200309 +4519130151833287959851246691876615538787874185186936205145712978930546123861640155395430711723899715676633532061887829247109590 +1676546877970973940392277995447472865522499015178996256227821980732651914936762181112328377073844485649563827098289006561421904 +4935465846415256812459944886042442740776629289289624483798607070666905637221701286036356829140962976615020452048228461943046845 +5324252244416512745242538847575585469682159371913734134263569496276780441518422322618507872746192259540742513974372774167973337 +4061947105362693262133270759088934938816594942342421443896871674922874493598103930292152723174036554995062078161445343075609331 +2764956618470182052214734830103729160245043907968784028181813618886443201986039333396356809943270121754709713915646568740151206 +3370863211220134665256513439944166456784396566762502427753466744044215577692010267595936563128872570057590311781066772952724628 +1434093271982951335221748052776429614044261679575670437872151960576974124250079774604982905427771724749469647670243244251475834 +1955236107353798168181042526280355939682930522521300010742221556820749544725933966041624888393993154502962490618839145486969576 +6290416028546495661266540757687391083370341491184427521635724821327522535027514382783824919995740288516481937621675853899158115 +1271233716574270260984632585239413670925887252984501086126979534236923438493445631129740540692858583194337904806381657298903618 +3300535021488010384891010505876121956740827701335944021678777354223576529240510607458801670448052514469687591418109583216371899 +3485417441989301906542262637698405742722722240100485896839477201597733807155335120248775000837222824525999750714790729423644465 +3411348746879651113262757268624543364109755847832677598538228869268434533167862688737780726486206863057323257234642767012961381 +2165814758833781311422742619981746792464126525574786109619341095871177233773859607236931880493129178559812250332066463408514532 +7726034975123054527871123751165334996022991283473243555448093241244802428165706217812562663617393968824053366244173854470750856 +6095264928940472527983481330401867205683258753243162756860090551147348417813873221392674935402798968661528287131244490650915944 +4846998428123345187768488062434480479219313046695253889842680691405315693254169660178067961065703759896641710892485894972873800 +1685615608144031413476448920807056587474093195656920132969415782126007882727463216577625078022336565533036012495150175153154793 +3655761747568001155088833871531145294046756206587264284578974630056937095900549928662096625015556765632408742933996850251895951 +0111954704076325038375601656210245491836721039674987643056715722213989286056097567862156918863302428121309646750177612394901330 +8415584072851574023474900634488379027144152280913594573298990276864512420566377677406310761330579752357658832083046689763512425 +5360991991088493887837058948075647761797269956784924198250160336333432026534756149206974204145691725795668303825556473066440146 +6308534942697410882776557732300657800112150355078878291669302663342797528117730903433654703116183867380240849386640798032108726 +1472159005438778436757327262797064431019220263757862409221343714482887707767271424863105342534371712556647665588514796238299461 +7911386503568215258552458522450938485043355503569972624134133652411157539448177529209200030556438822689089819514432813580599189 +2365679543813190286281972504483805742897733392239676734466218958339246807222637911353738424980809257547217933600526785327528737 +8206666715147505843269892459203855284050942439891047557154055608815102510800979065603928242478158845237310517156379667384978425 +8564365639950704666389094627317673103441553979461958229265682055180735168285879029143428791049454750378104825426147990982959332 +4829102499976584107337249497306375535937865662615510579219221293219668812136853030161705938471830225621587949094705819774556069 +5123783679768692703321434015951275136482214434898378095577232958805557078721200098045359198446613744587543829867662788092598136 +6929950567989350660377223378484336709625123004431484409297585830087971734242391343301362221372782329683381841402343240854937627 +3027430314645534840941417243615965839776590781088050407903488696621208213787375420065466391992905505392720776395628578270203968 +5561557545896908531566696290176040220677116087315281719465429326728445015278079287297460019260738872427151255654689514071849986 +1828538379287013993210377215462458395873947138158932974395044262987261065763556796656318930386507150893214229366028840942649836 +2063121041445493166725834460800448260568700963104181570946402318291105228818528098805135391601388005156357001178185451101272800 +3370800872347836800855502143109732296211615709327616533336180819117537969874343031967516246129400847851680499950308763513532698 +2261251707986576560067694894090522658489287994163408241552992222074332750687259975120713193581210288235466997453538082950570613 +5547333609592722700682015971929817349283991994515812388316385424977175852402318951978198762120074865379726347089860459367729850 +0402544338102912384265553136946539662518389179790301064640333604344279296879018653656722109731114774692564467347146643170192535 +8993673525857276993127971955065805434826324155448624528360859774191561165506877746667502530039089420897916681025323026574636061 +3901599392791219781553830625484740571991825540511807354859441733493239937466053566374517381465227962291796387571020809725382544 +7574422026948740113868646418886560822423385797395522913481336942061412395775696211559412859927044430059903817069457531153931671 +3666927872751409403909634814246127285265889024700225210198280286825210509773750861772507551910169158603017385059974490577083038 +6432266277247351230987806521470687675699096976848067098507903096237956371518053276015484587153777797643144785970196659271124201 +8805175031746648720776866745620376390338985825396413633102505797368825729966186841638401277578013322523402620704692665720430906 +8623786671348700175861755678175427019767971457395145977447813890266721740858466375677681653108760786751356531141090929913781526 +3195648380237469630693855838585465684458789117617495694729011729035913248141836338293691788762707190185659273515849140203833275 +0597402941284234251455344083137079916499435796408338558567042346638713911874856299057564768580158647502818908352952487514100356 +0970868650564875484386825809898953941148685658470222784359891344183429634142985332602653810091182805015332286021467238333362385 +4744217325804259157671625823323830396696824326946849733799160663706608011805733632306227156863544912264884279245494044730337851 +0084125575216079610672037870038502647338343776533232876650551512543864312081271558094734273891229896844012952524148474639077490 +3953838752044271045872553760612072547541462596253868540540395181295139378941954659771594736894686221229277026890071243182491089 +1570455075037097901528554345355410026197839647129067342374536771901052967778273115724862482455356615889491970844930623438114267 +1113425143150646440502818701009316634243943336310155606852136330886016200376990719743484314150050963993583980070237708157561869 +2850497678307680310432965409960545878246397108725852340253836659713228648432015357639072103145170515437738478376685162370275788 +3326664187179079200022042294919767104163644624649594061308737509813605056856101998334843842071022724849290393928923994480353310 +5498899160503467457596940573659595416916940383633806231810327042150722695852999362605105557507669885448657908253818192962915295 +7340742801283937313388827545354690848045358633204829321755059875964474925792234042792682433581007092726558257928146358188938017 +3788737611929951605093399738060446796551970972956586328702617346062935531918635358396384018139217900704517134300111877098173771 +6556088236400468643295262663772005261144129747959817416429151644418457406109514416151746327186627985567345268051259611187659664 +9265455725916581745797982714801820234080260053210177213938914050671185571980281286595729366143889579286214314682943150406601728 +0316466611292764222191535471075816173597528297677167091929792260801201488249926367457821134968651224754973624583045814966898991 +7240870813182642979734405654373311183972717838426383954790596720674570866884378092938010438980361304392635321123093916926377699 +1098769261784660364965755629864526449156402293650490776352458745073647855811975287018649518441512140869999035534937655163547223 +7318570673563180482204840468935262794048795191854962451038993504158655689641517360974735911839513301305317748540458018312631163 +0444738341332226086089060414609016104084271113844087095538006211914540457624942221411569773470678982159365460154412977671269730 +7214452687225977763650799051005862709711542484791447215218763058978813997928801482608042879518784727437915756166238817248827266 +6180378877242192988083619217841832407955378736448569454941330027911152317402955093457538120111385592095583433817050629741079340 +5629107620334905196700840557766414405147144002648539402508824797133924214385476958587576623195377053042403089302692454844253984 +3318554062977863170044352262420619308431178765567333623647941692615442475752129139395428889369180754179899164103774056228855177 +1724684496632837895268448877487345538070342201067523252247951553639414362390924781555578381060846820725528271757795755177666280 +3668536899528123873671104403554809934477255454000919778770432207141030247047304569687086172422809626838419879927821205317757198 +4281376208790055629568334266876725167967493231201019877577819781783383068774238210201096426961674885308966481435486534817275888 +7577712459257967273487604669410934219901389126865013214764147583539515778668315529487762810385482472159142915583111160208903283 +2092476036780853093515438486821087167147843243339127524062892009959789660882873649427006559335981769826686296878365166850602867 +3343172251567689761041900651298644580155321777412234282871319882978374041565615637937824367712999898962026386244164131814440583 +2523107617535787966332348997625949033914601433430397210697903069041012073594953603672415272693571514416319482962732360550203007 +2354311337820780136418582226046355285087018441007811818178174630131348892691010862349686382003869512529793089372921566890105315 +1170999065500729991421364651545352790769082161371433138736113416719659099975772914182032395782369105406892855739430280792570755 +1474708015575641879072739292610329996968899496702225715923818519908679748009454327877467225863186123383312506444593821059541320 +2519230317068471671990663024614062738285094805102560284217361596105049865919692357067522503590280759438003713619649669926160819 +8477593117311192072667283113407927363527106554972169624278711433088640030184820529565097341512033077210672824164404101920707697 +5928625658638382607550288328629457639763193426953494665267869864411489469568811023649446369996193687172148618677078694284120641 +1646459774821145583534736023501469753322215654392655710781868598002276354678420510784519315703507997622744075405271188494799548 +5604976454282399594837769165955324714117059445288436250408738807427315530171196984735484375626981702836635871509821590209332491 +1535805646302302016352504827863133089568000814854822798689081283591430708834238105002833054643250911507660006657799194089124227 +5554971708100609197716368094861383459498089313293558900360605415466184945588159656825590482066432370186681671169454435798038380 +3849073431364569441656271420494005561747519609440641325324675204133825604638188550112139476449622000662627992498117594394096623 +4191048491581987672722516757004987334900086209495744999112552193520096404679809481469054957100693375905822779214877756331765654 +3967826927260531495518865955487463010124395050606347758705435971911583515293152693887305102817702226005465633434328368776552721 +0431488077940753541035965504802229919911746684489825460131664577839381215244988219702491643455403072485117531612606199932108314 +9410075360076281631174487118204030545727872004911310355915647215932318325233997786962419782803117783722823043531791703707719546 +9979899286512850189031098302495325193753351855488331350658951722090011555997930787556662797780665452620638874492236108755984250 +0652520709290586245394513350104966676213533639227604453023649959625928591554435452582297988094318689720723128469217871904823318 +5154138924950867908591044177967760020371009497593629645137446770819235001454567589699623753759581616298742234368099793781827967 +3473470305718567484348272233839660441345062709145235638650446308235096448960992127705010353860654270452060145032963280540290644 +0926811026419214969150491345419181845874142492157134486078145329290631845342202476471135442659122079092431512088292118497802805 +9354612782399796245605885928858715130206027789349453872862552261402417530015360315458931199778666063128483794096976323329377872 +9050549362931470711847062862659110650055274307045585443522892077774880073956334195774718662309192081402741882514069042809280212 +3388599022582995822369209018970901943500870352576118339149315315976479926966165136544818295802570738444044829469770910136539354 +6564372944547448237407512632251508379320872549342100765184348768929229061655816110427490635344170087149155652249787176557737086 +9304726910793034348084164624446844756267779275838929154374342302538079381563499255177774157465080422281305646532991405754916981 +5079495135477953063596578815549589683659295982170939553637877137130071575921564130005951724009912627770427858856970687414583660 +6190459320672966822341653503087470382689354052580606552329874275327388332913948910878502521194902477705810238260537165258752153 +1741863988657791139389205030367955495311579034712194218460880036525975053623461402320730996642095747448443229185546281279794016 +4885628580837209961221029576497258511104739050257941549091025711288015307691459924906488083252587010968968824529177652581775801 +0763397108119317156892696282929270919177456927168774057189218655020688126030914415663313590555442617356391166888501048989509961 +5152773773826793584772678203560374056030587370542989568481592013810907285435722199937156448541686535030998055918516459522290076 +1305239048493423602262076973364789510528152271858803400583562336066393859447415629511735131940444974630579121650398527184965704 +6991327733592729480669828930382613428226140528036104065114673331048967012900401318314500711300298883332334369070527930012816139 +3452427216705740390119289172699570798671134046758624232397496740015824965414235594660630239994200610360038450127723765597890208 +5175730522105055786800808015853823181610779386663930878893825884556056314059074930365519187914921351319328315180621551748091601 +5714910102916106131414571159732353736678835156815504392107562506650484480968671954066774178137781734638792008562430877641109527 +5369362810620715036409904025968522864824364325482506902964133711312038294053559282742499233287959154156854383883148516152426070 +0230923585180985083743915234845529953134912525464096240500757424789301872897111680226897303301395426146920411464973830228585960 +2828878183082532630135919486366960583430058092282156338048743819662184574743797715878229822611358535700802659064501248149009579 +8877808967697355352896283886616902601338873980280498803907441157295597934158728665206373488691687917104704283343745205639622462 +3315755549989540644308207351384163575736340700335343877848915517455194885659684732058284726257857232622941934453881121004726782 +3002928241602670019659934407449689182082678896433504272332382719662522606641231869982252041696766531509399818750721950486163498 +7932472000788041651204426873094409270765907861252426634561030946897515784875744395710037381070458145660220907030271894709835605 +0606375828713960156150750275120802286593052131450659403286977242629895839703700510410871815684746087294738637398861554742127265 +0927164114710800842124744207418934599056137681039495980981311410777428870205405129624149605463522859752503128770836084491952252 +2931015631747347657009129018143774038849625686108045440606651923845534431071983109500879593401459457270781303275371634919948465 +0387570468949867854454498257661547651091256802128345749601432500529628510635061338338862610989893855279059054348321900538486115 +8602609964344200881430293688135456245345432239342905412584133371544661893209862399991442865006457440325878578391699643241518820 +0476780634258919055579958686158323045730786320160920071950074973087521728400869220442510810405620758553467843454120670874658001 +4611389884432116865691971525430161735365815156176932740053507703667082841676421694566703484160697073157865364859324160407900243 +7848340394578239725877088114075619051748487906575872632940285638137907126466280399912812673908953355586620975122613901023009092 +9989300098497815617534003740634757576144040056489564419015441642507944260710401355749819839918602861170379515547533183230261689 +2978159531889967729748526061427217809996789425967856884930963977509421860822261926488518659963801830737928324899351448743506636 +7739373703546853709971163541284486539624807396328266253157406157316793793115843617866707276779926556010088380028395990807722966 +5262595518780890084621315703653759393744008415536932695809891522083236025326969192829600448033930003810949789949558452866508165 +6709958307107045609075677759152351429582355756238261042407300818561928024091909129169346687529817528488405441096301840942497835 +7939846806218683995681953891663100594898796616302246789629809345268814966245162531052163166438108697094692065750286916247302592 +6376737923232528630569024819762238692074089057479592609265302063789788269041103846131589957468529143458603367428237467202691677 +1164704135075482622437354706246898083995113224930951841406301203169957259318901636559771075096487643893444519082342460614778341 +7571486167299822050740707597065176588093664070641995447134130614103674235649546121137008292829783010870775335672457433796269838 +0786000364088666248224822824647357946129181563136369891309362023665749511277214956791187825832905762312557124459238096857752088 +0224756578418586950260891776975306306690545499717256072289157907934175802791666439540405671709869057148319796761025632922961188 +4317800666800783644393485986576298736917563650897164304571720738720115271226750820847348337143207594067034889090129746637573061 +4992035733871695023603260755794332051946719932307664611429871720394241080075351265170984927835215008078524349098428854934511014 +7791275210894880460866040577708447085440157107534047266197077167838868317086743808903866856598023308194718747427381845326367182 +9440053603247145332820937897067937394376812799936768352012215925676355028026697201409962671443788824047418146714929473243964178 +4694097429621088759426437344831944205038778311615485599880533035443309753228841592516038076111818420993913946554412796041546011 +0591550477943256220239415601324460355602248522402154416815822080226505160015237485088369250676462054718340071691237191715854643 +3754782628510988470286589139121850755775569961153502324797466418949716647077195388880488321524207926209705534645689175931304390 +4293579894293631845176289384983754244067774813502510037057302019611329180738850594681778906215406264239719011424001198377631320 +9407877792875650325539404181214900230679005444970369243950847777718535539213759142124477926938032002586130641694624396314012506 +2378295522687601357905408069624354294555654221781438340067666919648970200524898545019520189688682193849864964964428853699551465 +7923565501350073769051751525297874085786148453411032926127268603914648170142127584914619282268063761170983684501064629044484389 +9840829700300902841805899697228329095218443610475399628627070805326825976451593789370050027841655892248443960659103428515769018 +7131899325280315528312838930296738263099442057104969325259438333829573217299432626262718217616786664813337230788868035555543551 +1613365891642882106953566519548821305418260262459032678151796367238313756913498686906585565640299870573937524634136703149075431 +9289249177275626622021358990349141206245802827466686634188302681734388914438573274476670571970132658340810522019108097685976679 +1999547679043388007539431538024419806104005321474063902499212939182657952169737390109212206767202350076020372910741798343053860 +0796380798378956338026387089364560910203624767639137927998294096484424294854533260048523991232484163916572752125119143647479084 +3312139471300109845789877486922941094495320192955690413961709171195278169767812527830359720778178037462244636938901054061989359 +6446495700380140348116004473224522745585963020017740642418321329101387653563805874445580561212083040164396327873544239022001568 +8221316129071405562026280838341093594207054207388991782744751735089766826502781865742057843922709134454840546015717044077633360 +8884079984504038495190808918953678045353356097676798230492279289061735280457857280913457619377057884683523850527395740839623992 +7553178237304616708890828475106185796221210507905405468874982714460646413397054645663825587287675092464040250385703334315092608 +4773590965589763472535194103574470410955536365729704648777555516672917330870335268784268577673152078292448804242825553568245947 +6215819528050303270969774851839361036143312160025453398613079612450766506304878967542263517886001609950018287965333798559597461 +2170641880151969257726247627112079520687196427192459411452224132231427774555020194976266757454256306589376845325708061001674166 +0808706168526831621497924679232450195651965661724083005213398910330404632324992737223126731122399064939390308377239660504899222 +3551818900906904056676616710573047283299775444456072812898768689067250392820375890588789819477340760813736549269624861064751893 +0906163675906901070688176210778473069828249578452605511950224714556564390138087101450788624170738723043479291100038259738176381 +8078282667536765535434830984808011662048300628237758542292327990288591220497965448597597894673766538440553768610799486540893085 +4239348796188542060619155351521608241401936444825596707153268722768490308653700045149495791462321531911996176024726025953987339 +4599085925561530429435581768120454911625188723603840101001827808010649207159239374191781479981311865586606666121770067310616172 +7104627786436327756821840719455701887978236563257187943017156401785911149438142292942970899875188032869983742209547431350943402 +3524068403178617373975067637980111980036408360680020858787573852832862969725456560160112278450433733839443039332972392923338691 +7361417128175114993261450629674608141894725708171510038678649037487846100911817687890240766440318417379790915067480509929873884 +4192563045095005960884279637612799513276543500165975164906153877957210268650229031342852943158004290795716146485254485943610979 +5527495850605424448537975358841328894172810067249914112763981461492106207569150714303562508512966364997576085520454249415595925 +7749567920199753428291369733514893066123672937987655345561072264405494064545300253825630928142777746597418333809686090132329222 +2347190729828800374979423249344630750463633445579902195914226451238506679798785890126552693482294276302768079721653123608475225 +0152265832264604743532240505688517479981918489770686750786602858928368893549157302414453807821089110756269039234757397043529808 +6533482130565176488100655388961819118126865617901005108218775489706817843329452026358354168685608881349897826461107138466301657 +1298673036456454794414764858082769061588499493558362877721489608505337579335664200621558841720543957496547716133300368539580407 +6422724558120550176442233652100893764139597343252110471726917934375351595018103999311490022927771281873120822851829841201522060 +8271532119297676915199374389042651216528118441258413260870365096855873207948565556782103615860771982841168346920959540519710060 +6184875439923588759440136066343058630113336838104030510817176224460276957688775424874821399346650313045479118572616668489003296 +2897490483936533080760701381789308980122333755911815060695347160990990030052801534871717538234667743972256918252210649169228903 +3655303333861985799802708736697214310308014845445976122762735834979387407451810326226050509201808567673064245381176975037733147 +9657194682720093768010430501297373816646166065617291768151706574173498067197264972548166688571305758669319121229890115185819057 +3103510865220902456203344829140970552995511866958564974902239021728149583173073409170941518663482580344763138822732758130711321 +2350688930344799523350604942490602629624759567547933020887009924743050812335493964555189623547661668513628841842940808414417517 +7886476590659571641831046458439879022628434419829165109717986460127979081642839919400368933401267954441646768446103515126565637 +1110171943300926097143589488105661141081024879644220948038639183302967863702059520939356184545779690411373217502283638534283105 +6427345179711846867068806487631152636131101160079927489950186169690439565446479010213897921064107507425078506360801250554414400 +9449471539328516576998441231176991412965843783160858329264144663894049487064693073174535162944480509103362252131259963289347661 +6448977729584408680822098278848838996541448014927561863895692613238880261580223887644439627676547583170203035028358611234118545 +5113424393568423046116537671317616837784363459294529414397028767453793820041115557136079295159075170242753593066820395440768961 +0357417280340583283641050541304196888459267811242609025317032935293813444330705702581551660482757892548218860632259447912758396 +8759325176891928932295819377731294216097307260061594553902071464538665097894582707057397147529275324299057601238957576630914051 +7741473646777114825234899359211272805439289189063644808522711602498268477174042815308766568204260129647306647359885729554656976 +9309329125552267123498982148765832181281490321112798033562587148057414223743472931562572470841497241968346307379573830843375864 +5689803719011022683494640589564528393539106647783122400405858218032186185463041208001667912281578057825422723357469418158669943 +4135592927602852287576618966571253116928765233133651892018881037343848794704599894714843595981667962009408311650894622157342917 +4255899203623494824224271003677968232634091967503097880844441755720576147137952557139636719564772738964156856519335241825543143 +3626415788714735175257556917344733999894086186508542273673131627361802049868502277018604195271807930087879067221107134172074310 +7958546281467926023449343568661101832138815620051349600621939712639392675220715111033777811540451328852498746778968470255323001 +5507407940398075053567751242109569783310327198481187906287038704370431459121242392319816826310134418784694594320070284603132253 +8500952612124145512973435903996934578404485074378319984371754538530064680891506823775364453662506914230333187130946894042814136 +0265665148790173017555028894292865406694173385138855281737615651927882128657965562089006870021634293021436977249028055567123606 +5915024874219893422458723773331806018064564044582475437209765195603359789772265483078502778595048000442697079452809908661825491 +9974005988977051807595272626225779862046223804794634926553502843630881070875123298869912614128545778960915871587536819171698115 +4134066090737503736509722416899089791584075227377094759873530554067748069082533106208531237181423743510501202120010884006504203 +2130661127717575197825945847274376219456565491166146558949366985214290031939401399735403160414646904595040915898139346351713694 +6324612167251656207707087624536097155449639896731425226748776831598133504224173609907231060039276944120772510687900080346884581 +6779168745096711200457711019042996058420593520557512225066879514823155886446534991145884583182566717602534510076749929656585763 +2645365584973687585388329644555202088022989110607367050144776537693714417352526613252012599647340542087230486009799844093446266 +3564846704113295525583758182164662009216216712683832484187612989237709031101278092921642250525269665257513687371728284688457875 +4269555170494408895577906466689197972679804550565920918614809933468304708581306933698168463484178291731316450813136298166976686 +7026547610376955852909877173580002842485362728362791349023862313373489864426541187364755041770087263084268647054320360965911322 +5647079585282992247752748756022346611926842984561046976951468976647765336240687959117772104657541464074764281634923169020133548 +6986017692163536989579104384975828200486159266813636401205178471888697579916647226641294586286142850042407781409124499578432265 +7557633520998487338874304841131912835862129914096167258884138035045133191763280844957441725806050932560746430226219247549692154 +2059549946818227720943211781526111003981250924876459433602235222747377311088585283118957625555745513105052583294040713888376552 +6312790236548842295363447664682859100743206688687480099078223631017589800121679494388116457487107923554186562511797306252062729 +9341961249268388400507893558690543848588537117611908256806356375473026278316829039260672100995233732139958201591529357001905122 +7622440629036046952745648171252074362090378664664601861125598525510174413032785951294400218362705244805632366004605852186841943 +5591402799903315324575404129541681549710964981665090508369906992078809669693743879553048600144250375936726479837822625004211533 +0078063724382242661885925388201522364426479724179540766386042648425409841158873221737967588237291116048440934261723138640261227 +7668975572438016377259095768317171394189060199749631603972553612209575383312005377253743770387375624576792404785414546299669225 +6702311775340274741303788836209727482858012234498189667215083952930215080983474097496335600683835003845218991908331419228161974 +5844861014709438146848025778890120727743982063500046236695299463240688553674143271469625051120980545361183599015488717404464199 +1417923931690244544999340897859985367002505948231379127672256195312588278402509195472407043750082438215262579893876014989408706 +0852865918577980935645894147403286685964996381094297648429648506192278067863183038777421574419694387317777907505729693297708412 +1477876485508630104652801640230580883493634454785840756974806839665717736338880683347432665781835143575902380330089255654038083 +1333574195103318490380293307280457872559202823396902837942959887393577658742570521479101307298105809134466724699237489092865507 +8231986011655928132234768712876079867027010505901813055232448036254614645338353115984374755578779894249199293331904818734171378 +5291198257745669262198453729172996916240029288161181812810389503650167962679364773038871747909175248874112616641407342566234542 +9827656930274121781454100476261391707245667711266140909114058056141317855910068833712824192869630959336254369672357008292856548 +5323877306540282063144065009953089367459505720096157841731259977506672262970125862602619551379971955654409939177527714243403057 +3533286954907536500237907021493132000340867715658050587581466366920687545465498963798769329374590369604149184427380755601882748 +2772451245387121398103687673837186367345979531432828052838695364605611707914643359500610696276887453665009271796449018655863587 +0698238146791660163022661497107232088142858713995181413650802098858185207125972232613318849001183174321522406825869523281989408 +8598554632732565974534400037249824032956321514858885348539735224820260572526802963756049116642833599682551125312131721936464816 +8777244479719589766354029439548567013317807112981262942768196141952117452742612964786718669561362298781178596889656657857438059 +5923903176580798552139931019374192553986137461354795191944498128759243022494357593594711086051327159717361960027267213667050902 +7713620888965717050672864796196500874403820011858648932283473719914877636081597753296264071668416201048071423687724652867885227 +1226026168424754948160701743250583052114587429759744035821120491264623002950973529144113414105707200986835221503669020815368719 +4301615723202468195623453039744894794833018107887889921748034816356630393443669846540007477652862272296203141137911353369865567 +3701305132741529846291547614776780687562767296077648285050793745319685122361946265802267010089656480989090754766793951985030256 +8873911333836369977427224870430320356503521006657767633627578973429639446804587551411591770027775298708856911679457385890346605 +1569544828599021231532143176442191013631066377985711192575263159095165119051577468547654466026788861257476052155683756383480477 +1570683253965182842006010673097287497172850525327662453055182206979693937570550092128731857505346441332074707878996538911947435 +7826422261424373910786428920393564290525228206911026303685173218951588837324122530045948404629473803092607834231045937648966971 +2638900097751822798368796230240210897707934867384226561665436456581464223307579261127638844885560109562036790807667670390644417 +4192581432555251427389363044896326951734847648428400345150360499669512737809145524003991223977279428592712065139442479960445332 +3942282183806384991949767715266661401310060486291440972343510267352817495541237489262806877081824812836064827724828303648923419 +9223072015234086234796090436383818568296143232241955872923081786551697504279591818923391084950971313678554302938168053766302337 +5040295146817500973605556893314901715843407186937767008367308307925199801486021624397893053770707845409279496052311813731054362 +1647014082822566848815080638989025226353949427396868720941862068805915405636344175116957293867502515550210403185422186297987101 +2299194118400856813156210708210682041625725297454205612569506042325388570654000072012600316753710747599029554756191357595113351 +9406123252615695607363852828573549803486496914881699175306276755265739836369938313041363024110056929623864023133434302918217876 +4012400092952627260881136088985623398889677549475347950880378929158923209470729028820934081657587476645444580153423943137551662 +7552074556799764537114289715930906335072549730926411552458159805302034148895295264056331531618830126090300007948842259905560480 +2034810936658458335936482208711459668407533754842615732173788255099444966005654249703291665433879743623245887203502319367176453 +2055826854912228554738248289886125490164761646368783638608966701408500405439745351176591154838568604792386814062201460103196028 +1704883259604470302949798965476058273811653448846315941108597287428362004683627667421915628743842382745845960365444382205614441 +8276093407998769926806805283999661528492618490261728950220171409852969042576745011296912080496971648424710213822281461166346433 +7954055459730823465679899905176049956104235683646988019468094197473567154076726095028250041409673376986848760985312614035795365 +3541288526820871183022785627606776202868249144458419432875958263707872906136295450220559956136362126175679096582857639739948725 +4938942457166622758688764402917253914836669353169972138509951712963418233559611193066044779423979184123891151058275415387004481 +0186693532983400032989768484986211599974215771878040648121115240600011282719832395142348174280309789242173034859607865458058836 +9693011226500711160574222437812022981161460007590617976243921827767353835606291414448435195278815539213466555449489307049996549 +9509118537609881549110785870137427018215479109414974529881125038407426313589072435487055670013225428915901526956102119247020213 +7469585110154725916486236087220807528982687428584047123899577899339622871539124688449741918490207266633825962457784296958609358 +2301098947514645707894405697878233655395086728296961354861714139621104988822145498752087551938260818588440041204929785479253335 +7195002791628422640170129886701270065601981205842819128758172220230767271007555891989535523140754390046908600193906165109046248 +2903714800754690075212414752414239844419261581404644710369810146988392192489474219249291646325601976712417376991598429990116224 +6657841194866308789835186691755481781807363317310203480516610021219245717136441915610841540149737012207330154276656421683007775 +6496393167312732418345038041143115847682160487308598451735321809667649213873941020106124895016322422646066096706495173559340273 +6190352286444996698010567042083025249630998980474746852300834243586692391358290885793710964591096599205367697354040455225748995 +3808084112830212575642888231806203048547309288414193348028410780142000603799742289999737003393558049032105240979729430950416734 +2055001806906934446231057752350125761310442613239626005937018173389749155377417284662314193484961591256165582045826903566815287 +1770910475516651085946142895420353886980271498962773028208448011256576219469415630937607816862320717646276543446021775437024856 +7317728070174544541281155663261784538153312462326658626789839195595942323601274570763560759947639040733318592217361764664122267 +5163497922629734181478583586735139172337995385407882365183581289857390438660661367597010604158349602523776584737697559311077546 +1232290524053006728473192557288773310853272463002083064021874545276391652038777534156787659533007696087737660972241935746823998 +4820797373925070252089423136204599368358477176708061478789645953897583587905165644362770501568086914083487441612509546270070041 +2710844937068669762717114936914428075765570984673461573362652998653398467217253506007120061287151708786386990808903587499291676 +1722239692416177387037629004556867021409013954231954955946248469232703827805268278720272112690685704443311442687874485358911812 +0696442461522918873003473056316397138103158503920758588244927506442635208935867088943853508413143951321019830428306333251201396 +5237490053651269730925587281174140512120043151663340911938612562084343250088788816613613533254540244890538135550921235569055235 +3652159735685467737870844804342436795620955533935479356452033442056822975104591188103222254197338188275216408256009062112739968 +7134994798879023357613115067249723046493042657115745023241318111461450746096281739377465898982050597074346498759749836646750845 +5435539953004427145646816313257688146440880667736243466005411373421754461366941715230523658976477672479751504847483760926576034 +1189232066364746161715297654667683387924489405288055983423309925216635045159783671997425077719999713937624968743960815866938439 +3458042402161473455431554634378956977066496820341904422146434258908727947177474383440746332129391825690966243498723403254667650 +8583588769346353411734495459871858141713721510897174001175951449320360912321580651780695549218819369972998610310409490019288172 +9334204527475060600189111850610268775609506754404225935699578359127242186303638934810994186504201384645847184324944047027889017 +9180203944559928543575350648779851174917364441299737433952437533215087774057147807226819191868491441590442039396662591808502513 +2164065534579878437699757090955883471447031879403082053258427608568820018565147466627486801491876503507730663614122066321703246 +1913587358432681308736116815399127159387589682764288441702400713256228014611237877207779478537639968530764623959147519370506561 +6235255735630858442517033591935698933960554844760005512206225607812740106611830794710056776637176558829867814913080368920324943 +5410646760661322485781048142156629014814968811323925705022272475371023640394527122828545405831491808608741154836760565505004928 +2033787078461763925201661733809116559798197426225447373091087213252755823492809148960852025727421510534412088471340160144342242 +4053406317407103820908867515275579307068390637321468991384194322089645112923727349244540751951979469134053290718220589958599078 +3285805403257089050405641613610899794397383496324785340103859171172319440876039603408630922409574370487373867533449326075083340 +2983815868763252323303322070522064853273357744420576029234853609842229699640881843769482230526051119476446187157115583149336835 +2361402646872012433986880843871560611945683230864891757700569780134284216871106221826645632387373922861192629580123373279881156 +3429998671996373176825887742582100895464377776504251588052739302345778540285554961799280437565093922440989079226166655301999596 +0886198944491737540229136497483278400920360475905860689648388002698383026711966163612310598810941558644533048630182341670726792 +3224790998309426141510868353814679975103849323867287287851298934668662832191399524482143116612939771691776186144211021701423317 +4793327096193585617231688937441621332297850907599875602232326960040073326201864681000919760060752937975322888247984232424151851 +0809471069558103959275275538388584472907830398322459954494433179596574461777867507710018713571052848608078849868820600263934936 +8295056327643509778568235977901421687599218957335038414287972297917509776356464814745628543893936365866982215021206066423916526 +0069175389099655621827463771703163016234772278712340002459468537149630573879420643113985870282366118625694647368265400040980694 +0185330675548785458492267177712810772887558224559469934081174511045410475070695699952516120170917198086628352522241811643698589 +3432240470257950810314775679834800845473620978487244838484905235351184086387166599707527028976915030731776500593580501517198631 +7221196580395629622683771151068414253703527355840765726766691589948635031125477445246126627685840897423930890628894585553998214 +0691851299476896761562326144030082656664005132402228246129440862040530711922127884942717942941321981606132599740710622690829340 +6387805954414104944165651355643971413515987568344486496225929165033526273702557885861628429072595508259134031569932407466556139 +1855370060311355098279801028503699206359780131940779590143641110125855998712927294283001777864930865688001454553164651367990176 +3395026397066375956526282504048669461521946610830298850923781230518236171386053536666469499692151678856847055919172630657325178 +3046849923385707165827109497171128710135811605592578271326373856764418443760349195924655728319788343802029428671675457416769456 +5261821042279220321036999746346900717107895012894353289951489320917708733388530368251182473743527841343292751660664418023250390 +8824252317744508579970406643025576979363288136493464811540150480851485119103480579103156985795448940762007262203877811330652870 +3204055616321408250954812676138625194951030184386810613892670161995606664768252471283913135456955545480933087872315934450481826 +0110599619290339021324056958678493559294456117665436489672772960474962839844256174658530179997209334133515553960451214172384426 +3123165163841681474367010429491835679876709672710839983993762107872490425945162980229932665274102783110995818120564672709048084 +2232035395926446390712487234338471590552947684994788621710474575654186707905435367680711475885293815543060949577152083169955046 +3350028292251658928286123588620947938740352179851517866242963626384508167950485172272074373493999323206780786225244298185666999 +4138239667749081340443940138823062129167164095346506708209137890234837357780622504863833649209158347489730999565991588612008781 +4664217286110698633018092758084011823559493288270590581425742146586929149871453186482768470820080489858199024057046533515257475 +1359038127174952081563588721035499113638671173404164924825530484539148472501653910385502837523160916070082537168043390775386612 +1436923728210391122200372504366645991744778858443244990595402772009992024820412617754275410349299293608939703285522116131987750 +7070475686255460177418547021473687579971194688396164244442299931146128832803904423667313289561781005593542663645306028982529133 +8234121263247251924212841508107137520536152642698580769661242285329043039771966387613256643214231439403217580461965950616775878 +5413104246474816660429811639531866004425878342745986000581091374331764631199000564830359002609727849512990017018293163945873976 +9271235506027321498393346844536657245544343198147802739604119444238511848559098921026630558834845589124777635275843923942487146 +6915026105020117228571904488841353205308577369931061433915716936274811913573096342546878245443525669417786920642396110509919680 +6739657651695868636149376334284237799146008295879768907577994879004415279621922483186516644311320818173589692203657526672858471 +9876510782436209693056682744986249105883882516043932138260023370268816469981968622627487020614107433825722570906101692145161092 +8947476576936475757290736092762279169953016586726757242391235235637432154172158901821960951603571049760362400377076037216546585 +2735502157822565784279289258228694712999190257163908804541511707522385665999422960150232894155188313507493238330302410605425296 +7940101946087505879550405973067848188833812225296523952905032878875050817047074123446017999571197850004230072472112763344801201 +6857142063692961406887982913011490273637829211932192499375592176104274764580285681098203860236999583984675192011342612589287803 +2211465020826460764614937570329924717346527293528447660776999654426823206473097384092520163892337570587601913303602876478051786 +5272016532080322083507311689593741698084734458418980219100525429302190098183573551213250917695957795596851135451614813138939428 +5194225250639639525068156399214932189538746207985167145356511964074580667075083175359149568969997574350520456153735083289658446 +1455560636188297319680331471565963722123437735473357325582592487841750254758281060767877781711667628353861132757458093517522296 +9590937580052253974622159164858580322392765535712215396895455529996073185392085906968002802221878982904887664044332656986715431 +9536212991471446618363328764237863492326635377747715439634427901843465449556160996406079848698687609807559175166455209276718369 +0962150554158891455114948237812845566152678283675857088563676954226626094608713709080121062121158536561703886898127526222898762 +8414892749163551177217490205399965970236826061221918459810953818518538232318451456451553658102845761329243527433596909757880804 +9825158812532083534774503272169600063323718458433952873310136391419269694767849055513012348666684447456130828029837154887287827 +4062509382360859964541627147185929761392475832974190883249337770970320052156457280268514718326951417528373787461693447362886601 +9802384514195519930363615938594105163103928246792737359775050266251933928460657553568168049147890604569802307087230082628479583 +5935332407364708394968146809867463424837289762388992341535259500067286459397511377634976747563308326968703921946360640378185893 +4569961338778055670213869684862788995924625160473819440389284613604453202763523485241482845106528449769880720371645531620509281 +4202560683681703431369153262516802980502196714639887848169937613326674387122996761898343353693436995583214395894151975156893071 +5332713363776927366695888962587179393484799105075109946348286416315933464459810857879632369155531524704654845441581407617245460 +1504145331569763505220832523847632508529957830567158287689592530569297300966404712183597770247546234814056520028432048220079902 +1149483717724666058290549158624685899733135096369862966589441778195807957291623190196664952975922610237417399523101511538417768 +0036082673459241242094083136368293175771615652653244493296672289353318399016588588663275146926170769838811055155231491519921092 +2671555423322348703897548437846131134228893160392027737396758641632859270434645917320325207654186499204544521705733245689882718 +2041837354468721403869880661989984614132154115011011100068745419042193490192096797533733270459341389726436281948139045659902298 +6287265676207191576589342058985373273830354712920659876843903556929428288343172794038913384089049546744993546672999661244015504 +7671119447680097893124176344032941210873441607294204020878536819662836852561652631058964593184756766213107639235733279668108014 +1503597944873319896726432783506968518844581130482208284211813252594573116879179483819354129815885419312761420721174077013349076 +2090575099698794920755209276799477214063322134870825766478922887601572992873375806104725864045048652936083491870937541080699093 +4276983494146036588052551210478424032928358829122542012906258904015903858342990969060460111480318597431250609578131562003677515 +9113189915457840484343399644582165727880755692052210886524470220995792925869536099894161015143779391596346691130912281801612173 +9319631208845800137369253760266414764140198516858735371828537522929558300406662683159046086394078157278265484925694452296859315 +8349237034176302850857262350204638637311808275176159745110236526981407472953488387189243977498778789698586980560479570319641349 +4582085067641332005668306046922050438876194786256238539902378554139808224133740244623740910175342259941720827473284689401445459 +7345697903120299815105938454414060402797077914470248272961589951135290709800128402376384567445478103311791196342258909767408438 +1687450542289596593441156541948105976556022804241843599203672068561696047317558154471118174352593930389361496742548511304322359 +6498489819476221747377877067530863799486747795199009866503783070729160667495759546595972752749803505545796148381282603736838562 +1116947692218975950285800049524721556053629253858269322416639882955425601685355611062823222776368062359326543474366782696365362 +1876190167722031024686917347027779070009896358771654643412669083054810506078866218417271851286420021860907338434817211204686529 +7526344148737332091666585836660697055361931941977353720336283268157952341498338628673162826343734542380046878989163995116270895 +0881257474139601407433704222324056930283355591807454770810061687372318085122818010133000433045729252354366007849932042280735696 +5414676249451280636930231638752958380804768673433252210159619334608464253621441055450445428123593855381938626029786836437726558 +1472864902858611995686957469529408189014561042091105708978737612763064100403995245584630738467933358860436751349270719679653915 +4154741684647123464440327863242393005522818718477064168160551024239993656696764829360125195215986845682299044548790324197334138 +3620127277527314655388754016922253350206555973782560191527034271929722748456430219013528002841930398470078939445946816067765376 +1722243978849829693580351795930973604730338879610427564825410468950337347468265193351290232895574144876817631258161159766300943 +7560827667474386459711757237346751568066838129529407855307314365209698298457841298119589066095648716960974765231576411494919727 +8647954673176727645450541867878958992452684388452058871232181090099976607265059152633077058376469186824930991444056731009872982 +2534991973200933430036026714058825873362205723979737750638944916727241098322793656977758591967127159597583155643556576943448135 +7155096296084695428953865082662825849460303226866978634425536379072621988021237204615220061198468810868414563936757985215986294 +0328355158636181420325206501518296045012302259181045466662110690600154986730477840865756276397313811576274178347247630894639601 +6488666618858920723988680761139802965152715427776544103393791345032803457282545151397254870010332315293514763142030945537580212 +8490792796508337321795619849060108983218521200268617175020002271232501077412217793566906763151880182142093233096870432790215644 +6497520222357920618391446425720257029473583117356819552842063897560547456381395533477059429635070000008137772848031720176668971 +8337012837324253697188573699623123203170708166950795276999966009577648906818828342730326847639721034032786616971040641824194318 +8936005757600971054320025253057644372313238443033194219596927715787049581460393528422411161050154583706800554645719354006707835 +4215727593152764210346931899527305466845340439034432649923099270817579403876601746194619933095404333412889852431784452755466843 +2088097487158522551752685495115047141137278634729729587648195891955992392424399063377394353752520259570862609415201408164480501 +3544474831228092443689968228780611679861586419457086393251353177701671796943515744580842421181721325756988105402798156643588821 +9778536795257051681439799124663145890473401144931092912457976024515106053860305458288529123695426810921506064347775651216836501 +8629179794286699832017474082041208054529672919556607496647432513709724969762646139135868191228785586347557306843431128687158014 +7106456703557605702232569911033196736598272397024373712296482733842097318351505429979256730289847489156750506335880609744614597 +9660558924487542734143121356331540689398560467085012517453390341732092436780513934245365179678624863881161450471818695381011735 +3612453516034902143796136205048511802780802772294479133906585232123112871839607661282061340027850740910859483338299107807956460 +7138724118583930096084405139356471004624454913317962414308274669136174812224878197906201370412577330455805945128690650516949333 +1730291821726950196206552159924266720782365385611242853448279354949948923432277012453511172303822029259754519038931979427529263 +0548575047242616318508715072515332204278272517306960109310804996719916332590161894788700070468159978743034429043329057033741956 +6780889499085964528861899269034340070621419803779139024411147795371750105312955245739852543595775978340360773771494478163557276 +8779458235824687171901362922249920581085853186181780124486013927395708657432957030172044779093473433280766667409407392636463029 +6492579411705822845905066201120652601308459859010659739650561022388466983198156117882306744218369432413488854001765063909641296 +8476320260631331607201248389753502857961661494677170435281685503191366884817616726638971291309647117085069726218509290967362859 +0810139343653776833125734038676243930878414416044335363929863448842581458674823745747264416650503948061405425537368655356993636 +5246772266327550199529567399996744188644213516458735587639817991224447177904904043235549503266714688854194981790228117033924563 +5461999291173972450770635507697104176866762020802521790899644748842731393628432841194003711839701487245460386154522697276378209 +5508101058683508523829079387008807816663732920071304290119836347773461806818814318630035261732455767269492882409702742637950576 +4599609444549117705097045766405200220153090927581307100167391516722145681413981100426906534790499705750570830947858845925010025 +1009651634984508835728715571045793550385433404603846489058904184111368085059460823145504327889561854662565906194985160507019989 +9357881999324778923494272780697498659881414033472270442304085245770043338219929173021287371666801815069695486701740310076529915 +4608279080766236606674170110085478654830337635040537744779366742642067739517988003498779451425063495418123468371481260706586708 +2700839483986172436829409192485873721128692643705335631914640430691426436305173498220746572777225402080014675196382731227699368 +0086136702746607717078625347234495013215002090165872533540039551264212687781885399476155438281141614343873696580481400589706298 +1705902722746414058890369373242227296762941970919549616605978259694120130539911344963309277369189903126883321693536398430654697 +5704737347400443449757566987270236468633966314353505101478851263083176174429030344023607680553018559157482487272502931014168203 +0929253853547791644998906236759598501768018124990991199353184644473283908513254590068607924929473380232470364424573291631346278 +7114456427076321654054593005258016944651062956332800845969220254804995756234489111294820165405880256529733135691041245584370134 +8553075127747556822577563288536391683930679292906640525736392953226223500696613649419343205087984527799731174525862937043354377 +7195787380049689624114711568372766827743368052980829823446372539589421589808988393401756593651612425875577606936738214349475949 +0181544697505469142362834711903740140759734959010974977126438624042186356553593244884210506676336566524109269413687671228874205 +2896686735795141476730158554607522934103668703053995570782216706155323744964613382384967846986182100960586040580173953854307263 +0286768549279297394586785263108063892918942908459235186623403809969114153282154910435594392237358132752498428484045697160025994 +5955615832427009939764606346259378692931215536161617155986791397037887173705219219646647075071165536665651942885935717666403172 +4276536421804556864922554676992233459439755861177568827029918375612999063128068548901725736512083925758415949098712083242055096 +1725062474106111957956927114735687646594918046204634711666975260667778901437761819647914937715436330233142512856404865653199627 +1291532637755334170692403451392823323540177224847012033326608939686606026939972956526341007272336243309559194347525857345975521 +4741600004754011316928740091902596508371266808717732973936254941000335221943249074812743110404969466723162046697491974991225004 +9085942759791031775731739206062635146158396893656161608173687371708987211298821225045306367484626014700868922039873009889166761 +2541340395518961317950998826785729636172355473248648132541946680948357368185423797382627281741493372551999786014593359188997484 +5815863362546532185070737662568520327034674676319716730206859041401224197894669598892125005912741187360523778711460345399659500 +5019261584110255017064950229895215051849707546749811603776445037494938571872346104053773024040213974077890562979334359709174595 +8333959703819799129975411673347762248495046754138982930023526377690018754531518740079491735091275401960400702618787889510076010 +4581333314409607030057543494344952121287231730028937956224706941567517833092451729648212810249480493474654543033507398453170912 +7852221502784071535646166335599143402394637641678513241517161191111016811759837825114453186940577300367319815188600287295217212 +2010730508881189649006207896145313350898015140508068016693226343978406558747353752836658017965545008809220423091595161424950648 +0496642867065050893136545829132837570390366632826974250747266589629788809643453838849392317508541173149110672931572489980731531 +5044656574615736195246481865299114510211631762807229391764826087155637720268154669986339615396192306621947657967460512382581862 +3895441416167225879629472693046552479446299590678533650004819143386099678646479751244788463147638567925169013110241886917506841 +2313203706796269432481996570645174678020808456857425673026366797683548661197668493887407113195800476443548512276825541513285096 +8842881585797604763305986235159142379357301472286513544030211227645270193019240287343188528847284806872333232004379253392212209 +2688409746134847438143178885250323822851284371547861492401669288415720100963691981973327453125586959600245064339903637953701615 +2157845883601129857682095395770985605599007238545319703595004924079346833281510603116153010794429037127659368861617341676777914 +4401447994414721274888256152671021736338924549726141907269816501834171190194745666679544881448528084111510223352357146390678748 +1598441987414496519030297478102458680201205753852279934039229453001433839642149793337137629917651258040708764206181523433254292 +8502235049697025555092849971402076674867464110183607528142090872909056796241616598564935589465706229822069262366265937628292137 +0055816249709410284431305701461181450452686881595061600026629505373443676670339321519731346337868758756244867546936430600344835 +2653221234894262976174352794968903515069018957828772393070009300269049849197795885320787480595346792949215526978359438371233230 +2573198395480105527983836130766181207518943018866225650015829519188519988960108721260257064720088842555685468531102293009893766 +7692694226464861550196190446675400547348832497307906496613188251375658889565533214610868096123194257835553142178169804139054021 +7725637998171173785445603097719118044612631181543666841703322233562489202164880099839601175940461517268783567897496005081890093 +1623476912740353658966890439792885600281612076194740147557413318421891935617196959684345685434629547787219047616918564873155095 +7025393340892141498709286165315521796432685307146924561201314330411100254047950443336104515273461423562157427134079178986568513 +3037051173501455568382307906695636378996155638857317043809314274578391987572790382804869815651855972786188467330565714827922500 +4074893387854201023525918388726764794855730871461446351512218023803177488574497502040250654764531910469820302411970861290475346 +4505685142400586418253034395353236203805450253479017801928704774791824048811941055335303586560686324885362705510247805420001343 +3089154219080968310703760534064569831009505236913263817236675143668487294264949278958590434251398242365729091805726174530517140 +0036394729791915199963247134854987262729782792900430729405932713576759938708525288045097625035200406357341027793528833641758308 +6352067888617175431411674423431768301646476262241768575619045274936408735985350498083851711439785845206739316196883719103192098 +4796246905383107193048562525408958439687461541231738438567101188983088993969984494129981508412118318349184613817013959589269737 +6278309646817389075935268193490507135536415367887824086765490270943493340352420279233033265958209197334685770879485799469719026 +6501644826983757753799478095020135205854302975462551896895597291920140799672078608383168757287535179491461326280401680688209269 +6878526040144420355891683769364803589616260114471471935609824723068550467740012749917856656207461894693384857525476785223522600 +9913533038664755944016342494625355616351591218888135528544319832829890186060691211822484503271002284261557646472978467535245524 +6548210843247451049422320528853823425570998217119732063096360026534131251690098102596630502775298567714836475115745947762170767 +8887884056401989307352068428168695321737823154308274924885994114161224059109503941221400198439058269599476104839310697936760947 +1499621302434101869959148741723191678491689696644090274728115214408462354277075304721665902694379966811617179086736989510478453 +4472180192689505631132593579798684787021607827033405644669077356547531106428251322335834054519860462918400592332567647993370324 +9048351300356674084399149618697303904545693491523593398209411550081390607196236295352104843258693200555492607047645055200473684 +3904033119579621911938196032662442953927339169769049079015529114126726770405396769752836907821633197786728940414757447592836029 +5698177775039384194905994353296325732870874582783505440992449252246418852133705888017732270642203555475460239299507823556669568 +4584762886535479770675275444567493237009137069030861149601485784515963297935895153471843170037858613325007001549210631837023209 +2986230798529869904729567059889517914570160421964137879893497645563877292347055745496295096825111567999357916375574494934291205 +7206470066816931559279891084092643447735267681920960477832257393262206090265312234429580381825327805715755696980240563671249333 +4975196191021868367044398019078519059684387790904063324517862970957096304027938042892083911602143438683752393325035772333502998 +1316065614559478287404397163400580119624302865064035828659428240183667221604845265166461282189820016614382937134407772850983969 +6172722824726265969836224691170018753182037736393837492582066024393428721754417142086323296047227886511163988288746175386143366 +3150098340574634288363424590978979671688129464997330011306067549875063142531548626552657667362582081599069751262734236839198246 +7320831408674583797739325661435931815687746601771458516299735930183747747803369077854960129285952199586802721391111341133770000 +9781649076575876416834626425635920411598588075542620127671675851163249442163030188821293485762987388143543674662920406406559708 +5939935563329383384304248329695829758747483676028520127505271048590567171170009956748249750276404393523654234793402435394531559 +0252668408043754647206021991183069125065854666835585971567158743285135342908676173306885768974453965943775589652707957703958897 +6941205372351396701250517551254482102589310971175662401605631499693168133844709297036538928165453987035113124871313193061665449 +9276802658913609967848115344605249848453532699908586062377147315012593156326148344297014110306443933718879254374138651819351013 +9358073205389217401007942156414789371666731131537345827743121944929460937083473364444624266082970336996459723671391030112788573 +1765411908158890001229050208942988203575712050271091972655376675898080722954958732806828573013490785204118792568609729781493352 +2380302146221749454886974990613671789801805162408638634096879268990438108120689272991357553079627023448340790990962415597084280 +4048096687228528057040549427950909321059124104059954691594133361792928522598487445722858314871353602305792037504518403367795101 +8791402885580812697264727719699410880547796108771833960539902147827417638408675493518018213254819498104814348795878620396366601 +0903924261354725492168290232167217374559865613331033632981304605379493456181688182582298294804821827770454840207003173905889439 +2319227785834038175693137220680318778454701304164120192954225359327542167556566637258412033259709183354710843065358146305555525 +8673917622829080548174920911891769587355766435165634787328419281721579009044997104872530471989859299305155593643127267879722422 +6682290955279007939673997760490906289041187907827046608623932479656002087546101920096135789193088482019418559812270396482968185 +2423495240985101912282160197226440146044901932777702865325863608790152621900419953459804469566930950992382020196553029673452779 +3436610407681610128962456711300906610774647139359864663150800708313215865508716982334525964257676579485350065609743793059790523 +8492723526317024179966715344536215615681812917282255110526575184184812878258269018344255337700911624857617809339928700208173945 +3637336636381056356070802698487403636259922727330027710606038599582455818066875278487848716217844022297313070587219989978367370 +0060775392327016214743079646392570709223503152196217613938079763344036433088567530057235155273916070612559581478927578508356337 +2980025169096765222953776328674278356394573802596587538549701911336821174109269755308129020228887624742741743790423872778133411 +4050821812671715448039700197323622949979651733365312936187264385591443824448095216098907529899820693216629857601750862463957125 +3142033473651787251598564785166706147991399595143208797800693772522221250290172822723884818192653997830457442015732513857367482 +2434227934816021841461320189923052035591521089170168079786149675686974463633607305584810474242944486308070058500812306124076610 +7715099107942733927512136192299373968619398158672679676087995950860231222104112656659877314698556942502196127845551909712373108 +4069114461731658732633727592893010579600135097271095735648535688306732518715682620335083055144068477571815102417990371455419096 +4513648336048758526599148974167943082379478315281923441623546714332454634302169701041016212788185513022911448815223921787570792 +3624469067047527512170727298754069896031789622645772484827952155406739967506695954077541651367850929296976343602998185574427928 +7941267068022945446692718780586230511573764935339043083147537297266971720289420223564631007244922385010298582167405543731618295 +6806852318072988521804655880278600099719660787055120319799397720389095148995402664025461591174050670273042539667806005860061500 +7207008027571345636661751333090766740919387017591058445443344334627123396031143577924484316316865139392646603641126628696936857 +3077564139261564206167503001531356981773542928052096642681673223984499732941393025220000573381152245673838328811500670802459159 +0948542799001533682410297780373013799791748346265940949566539377367731087302452174028989049580452869089162990037685957268273937 +0880681561126138584261480022891283215404751141789014380796409952816321543905013512146286826522138659411899727000462608467450535 +5880091566261256557011783504868257620770129211977999169370084785384096209807922714348396458806443353193445034311369641072095970 +0556656033549253864960039890721015883335372132680590369609602226419085993403091634173432095151442616233244407104672122152816903 +0104165883408627525444104591380562545622183558159751831249938218150151310276941351661814981521482974647621742382874619909936820 +7313501231477817301767451446702735705428503662442130539627888646852578831768325107739301527824363969429357121983438884878755269 +4393819179422351389843662394835319603450398553466130430331252873486799616761474948216299677793061438633632338236018231007238273 +7068094772817742176160590443426997804722265949259567119852638283512544860402755698118178658952516755701578554245375512654131546 +1929058509678608173068953824208364147161581285515961193245483567752314654578018433833005722739540799467152027333949229966280869 +6336730057716861143854645697201406300653307680945816167555425942108475384088151314617817926634797063108952284581676269870141278 +6513049997339587705695549544072595339278305253913271901919825663866146612507671581432953692087570028664604274026412331620371074 +8287494858287111672511595795429201499531492727612182441663719617940686643391436311349735908431637719847434127985592417043341276 +3683439281027866311263196237695664936547654237370870765012602749829825808040000867227133823847412406422822451359747985192576292 +8080745720999988790691750589159999510681935221965279452806039652949988754070864815196625235801191787296756531552141564212907151 +7193959478339407643118308465039555507133342674876808586706138734909756613087265895500010160295176435656450676824231321652261709 +0364925287385399527025737739685585498448664221812361858587254459582955070942164513804426958941634994463722930608065187062143205 +1510248350334956246451724384468817066713416535078509857129529545180481755289335535116009464206553367625156566677354615817653859 +7881465671702288772143540937915254400157725015862757578041600213873551218117006116952755764443062859739372284691117745149958234 +9088672142145404730278835893039966187590126325943635642412358143475145374782213403246198035026788070070178049812453881844272867 +0338668401687420146135473027030982411204915710337406365921420685513130184588295184291465724202435316806267038332889397443572598 +0839432579664022013756612378573508309364105293988945101718659897497738253028464952712017100629220365209543091962896609529479679 +3205960596777307708890129013560513946330763313564335478005273643120628824046289255031118424365183741647820073945909472936879735 +4620269579622752421433383654982653678424583328146692271652411473449919421964392617935515652881545262048907644967697133592449864 +4910971218311134291321139032124590602851436712963785978946343853100496072075735946195731359706831946181264874128557978861454843 +1011713617018211148846555458691095625992954032111035185714620653734574566700313717404927859717431081627049246861417661324499234 +5786110524543942705885896839017534087003537095772880970962648591315518710892525409209474091669813346069319857029093908507508520 +8853954861830714292944628596393329384568570114810794672937003168548384708307820733820193722197605410037476744326684584534487320 +8506177470614128104032179365484641505802789779285846461790237297261018747968281039195118146388862647365824837138881138267868859 +0627387322960768524871037409484459085875896325439489116621528742302285368082254073124593119410595219290194886865353532781155425 +6809698357496328576273024691628485594217747112697681983376999200098136183929376787562599199562809870612840417370015973807170130 +4180345838072561608049345847198586105129570485513244519517878528919531649387592676465860801665435795014078048815567745274248672 +3354246532695167434429089502120887783749962531689888071529106417054905309204892456124468634666345233412562908228212778433482865 +8025350198646920310690370174859009390241456508358264258699021020277939851249666503547743318751810595431556900870080845029269395 +5024287042925036023658377259430412863334903203075207016434236014370755084100466996153432751138550920390967655897565334875107985 +5140055633415509862050689677756134057716739901647883241169162784527852235725792016297971735177502489661133426961482215258022336 +8037402896490266221826083726250897311919802495381754225581047474087034884706618427848292889662240224463663278752931604522244404 +7043755425948194930791058610151488175614282166409617670305934153837246327320668633959913213103337798602482509159766659520973014 +8270324793209592396753604357772106280615543278837901728489074421806486555551552367821526140160754356205673704266414947144922428 +1799358966488711764366695558613904123247231097498677269788824167860157320461004934898696811210735916673092405907643647942161633 +2879051334510139198581358891368404052755328890835533066541217913121822938905792625249238474950146832344471977043224365781135389 +4792241156268785166424579695585412116561708898561202729978503519790831983242726846466172154727854320602695467825448826562117055 +6892572380716177090374320438112277052543379149328749706362112028296715205065873069869487739216648881329625482801871208985853567 +6373214964682547847971295488976163394711102004330268920420740567984995588029489900437674583804505907280376076761512694288533949 +5155665472119733684583254908202345934203146938766729952819115305923223221786312083916415057710647121628060022705929366547804604 +4979219068691159117673733260174579718275355270622127824048810725346461586407079827993239800649367405496829848031012026874839047 +5453326819721439241285068650662264327283806902040744362117833297041970217611203752467289267570303110513926750426867300123044678 +0552826491452886926336136137608744868267813981244854176639767832954709406334434073383918878421433694185303125198608436174948723 +8232804838617139276458679276053642834353480357742672915996943482707728711707722990226821986645110256211853787080947091570985448 +4530019597199416503606666321373773776661468897805635174486648284631882284913728030703703554479870389738113486662451982640555230 +7993338222145807140991352100542338400006637591961344296816299971822252698512532704506078851263452743784059409504687946969377470 +7050536882923788373466054136459774772814292349043369279413441771827357318562815124739087054575308713882488227553010491099772197 +6089909596225409017825449105685851204483516303044569069782656841637982677940745207225513250507661349031619934115869792030545534 +5623878938475449105483656355950820776480123283684344289877071474158657289092937164564108821983834915760732145489786160801928029 +4816804046415869731980026155380698391293302660706933603124933543082802974288753245590234004708950597141938648083597306991959548 +6285742091945356496670113754904503990748375484181907762641311090270050878330341468974330973619534202556739616012441626266016429 +9943182264167678058972498083665971330560261159232616995578303066019814909746442924781284268163340228187122534568268386331093065 +7152421562633124939028810156815250340969816324770306713467508016775028986746579511353296216202084745909078793312626258806625843 +1270211232086431110376182218366306020098067244906289836262668331857252429211967620796216066094322098532350592071668917821894836 +3845983140880841145716906938062904326623489852177531923802141898083667738472813358868563811996856141869924840103531518902763682 +6718003149619307421590918630236184183948486678786725981990313301354204177947073223184765980604578217913950986068011309908008603 +0928467335337181093723613708048071495790652643180583079182628211576154222901059273081503840088736880283912228917857747961420216 +2166551801482676965109266503314605693500879290753119500152110927158165273225738493344045059814681043424105403563256277265130147 +5602984870377735940956944951929383106107020907201382858387070933315397423879633043403789726957228735509198228676824257307906832 +4033785332705686886894233935205324983504447579271074658738235607798108047836471901273736322254888901735575902110174004577938609 +3402493343952267342344754443371491773245869528553143427384549726736187617570093360565939837511071410896535571400200988988962479 +0467384270228802505542012448307525976842449031499613137353277852822620338697586513075361354863064717799164245330603052561106892 +8842259346260183856170976867586760896719736933276441816021638614978497367736310097708762890208045248689587229972592272876387473 +3259167318440772369838882122021007960375389506141034220872992638686407702750133638151910546511598120704345383522282582904780462 +5442641858108090041720094339574637116216554948307838544047344475741751117118100567565238848037532377180455225898883679663375089 +9577142779120614628627441392665993338561362305904130927688924256501300629907368310744468665785794738752761659537261334710214003 +8036648554081009787955952496618722107214483438085877494384608581966978660817589075759692849806559360759821555215575625537328994 +6018332837847583031691963477349319171451400074031373390713717042745436212897138228273237021196246257873177739985799453366628180 +2105213797394649680881845019293842601907281739434203168302170068885354441461289080251599722778415734193180238772976142195797481 +8846734820754645249343903444980443492288225807913782286509632765370978666435328327850741457922220028478728899676269184885886857 +6241211337754447826883159708571545245319208827655835686933251362672810423079630739434073408653288828495537708526658804579257936 +6272761641480084516721023070052621557056643754854336674557368573875689625068313256900499409409105292963555938180247719262631692 +3370472297661265053196085477234369632895929368519546792630928565306505801718550038387422423219452220411081683199640562587649457 +3951837878713231474233482132735598466639480024677257398672432796607337555329635538613813318615303256000562507502749357845826125 +4701181391450803850270234592090970559917694412710413114961034587689766035663731463069166460549538653431143701866309605231883521 +5130066101866721176356898240445132440597653755333619647577387320361484004480200635865792415170670027337193740214918631143418262 +0808586953125247209833845490228639118624854066699258124848785165048528440583631961380212929557758370252911013779599196305478822 +5521493745858166529682225371969453760228060851280399420192708176554476472315839318021751905690807649055065100259488774569707365 +6158697507308922777896716663452883986126333631244914987739404979553039259206951208801722656773710392910263349735278566679339104 +5235852402635719195587338866742022182421541295397761869128302452896058848115769056351269147058952464962337808384290518791915445 +1541410356265877277120453080537595028913680044162125110791583969590059857305994592466475393255534238742387386696685456308459612 +0685998205305456113478494375669069867507303172663122574416432766684294168665147860680796606372059541989820238442687325132846277 +1106867923762001975234815885590982053876420748926951108789589029118783643675464846081587064829483161231369554788911064379449191 +5812396650186976221479400172298188629572727898176974211003232401377793666992976058114854815038333629709686365618467427463150786 +1384329026943982531591592399085803625354634016228493888015194258739954993856952956641410330220082208258103940975869674221124275 +0959625394390538490258630101489160874897384983747423071388118372125410613424612801559234232776779480366009970965890340120701045 +2667599636961907870724269931710369308622554291093494631427897956168577372077593727180133140549717498300507170013965255252092132 +1737339475235603869117715688789198413446311276950893765017909814539886259628403094769733766342691082854440546058895772248646612 +9197218961185262852748275379196855788331061390003979168135259363646749693751540875250641291188297420332987544882702271806564544 +5155345846357272505381130365306178806560153732924148036950818502559878423925856638459435711194956693467002588044226649914787035 +0339515088331009475687588955523954567747189933073011102332237830656095392839856108022616789694653613303860633782147528467869258 +0130375935586298049775383191407798098669127243484570035128574481514435908283974501169023065679639597582504503336586846159045241 +7077631278309415202815413204154287337150040264985085284229097360757947744868068694211304208527582045828778373828218901884299318 +9750268301116713110151838109753966293343621088077855224277949045570521139532153236249008822916991091102238861514323117478408697 +7155195645472829181908341253643812659814915997068241585467987778740169901860636348467280424257966646686476472265113081410661304 +3489215851493494198992712587668351276298911212514851702684901497030382245009336388185220072052801963314343348536393431861793824 +7433750102437336219240711853055477575713689475760799540383502439038522399381791672647025184537779822386053913810580122038174879 +3818754166719135070632269620576055650986227950584996452818776818068696309115722467830916362575260788613336625118633863774586835 +2178221238069038687341161131798367556148830622561463128345189400786005116148867892392990638096974925106944221425159061603022670 +5524790379877915422325982255364820388029995625235481186060304010456803568052174332647747736403777805245458180638361448671813038 +2561932270943099179760490101865479784716200934807733561691561384108933097340199742932516818080682737612322848740218966854945307 +9080116601255117121383041648878380955264484146894062161454022932953200141179550531168588724355886537447858132714146647197563370 +5582470235237288151308759910566681678927176948236130886004032182125845977161600117740547183580925791449145036400529238606663420 +7837033717084727015977259234033008100455368360038870286352550514822219714510463917848677542164460976523276256801252857727025894 +7130094419267655355009949375142847444276972876916939298289507233195397404513990497114810662137361054915518609388753530456478940 +5388394232615483120266638817714522427995662494049561316381620397442848359037023023824190702655956805505137789130807808295750670 +7728487145979981298300914926600023112696314252375455071809186812868701327870915067718357028716612327551252066278398582389353164 +7289773656445897637637801900198614044295973498998341687324987147175709042706190580765940871231724558042584698396691392833921161 +8657047381454227627618374146397069336202044856822409754902332810974380485722671719214725867693990943072348305834409116868276317 +2768386266640115552548464265203598491327266098674766891714414339959283543826573869249667218521183972556730897770799418342118136 +8189160219232646049490114563802577964294955901129433201250836705921020927053769046687178122894793267915461627258608458791264898 +0219476902780453916709467789696795567878369111867867605857135264741583700268708214119151342234570678050700247598575853332533925 +1642635008587172832360657829315478456525706956941480551250122576528755981779071461911390584857611720174162647873804473942063599 +4967685799379026281672422049403080588839491839093762962697369896023527084701530700159221622072624944257435732610627292851813751 +2044450251147619884660437582513822729870849508689036347657931033634388064328200140554626737296767003665386040440053850111272335 +9965191238699562685527207327106427173242624391995490667681457409129351701642801367806048618652102824703936861574242876103982267 +8167136500949575593721265813173669081199030452059803613088300447991271804667292549385675921231605005292459008477827396828396980 +6509840801282604124983554742769070687886591529251914833953844029743868619125159895774188014042708540332420119945175514865123729 +3692321786307153306613998657737637076488435978265420180472674598878806315670458550057067811482012301407908299677656750462270208 +3553454561634700149917505664871758279230385607072819176680565522193204640320948453398810380708601224770333475704117532694153974 +9739801831118688634434296635463933483606562392700839399728340475392493400599816097076262424865867680957927823749646379318466788 +8207485786494651396331129651303688013258032982874538569357681541288542572016965208057337584986821728683984473782485195031290559 +8109594698571552972881573904540885927360173123269475341745160895931951290769944326526035760450300948152494775976334153020903866 +1739268973385061826289687149889229189956753685868472054090051279828873221786295847959246538258862203254463138510475016160891729 +6472588945675080220223551738194925153786626106262423384260557370130433634861152559973124770729011313810073811203315002214957767 +6317904094534761856637133735479679945651270156668746266850573869850630241143818143990553888552953747865302101114447970138765661 +6050111904910844217007312990287283030132792651432799525258590083326102644255354794948740529248987597130140253327270495396985608 +7218279207220544832644649289313013019030346375149234023654949206605352146548635130815666273562950986058561700625454917451118233 +4591089295985815819108775507988478501559092837715809067331448086286924500664248339776158516774620654659269297333948779421651033 +0402389695369287676425439165311946884646996933164719659142031175373351707836712648952147263228223229642458870791042562919539663 +1663280408606348818705021370639699925355411336760403455952690546859354756234316710656534265824067538349235703012760994218438928 +7331947252166763395452202287514396143003916184577374680861816449247026988630652041781591100537950521820855164931956884346869156 +0167992341341885278124354492799177573677966259864026035120911925009272011711402759093945175436356249669782240856162554344304896 +4307297970448548637044676901410933338328417297377294558704310254387943692431491233869228400552419559003867252598836554809147959 +0400912691036342469718967629054695345164844205186330491298158594966799688436822836744333087245877645780438275466738783338029984 +1522719795609161172147875781283157496792619720043728981895906145341666050986634594495047649741152246939806507306609930123284981 +2105358148148398274033567712304765375367909977448660123752729616799410712586037852322616433905738649313626209308187391551045434 +8184050603718152729487919051152176341217780682835043432591401578282222377677030156609712538101741280977544353648960619498088312 +9780539921497865045552480951265192490326869876857256607893887668594821322177012473373977135212994227092237257692716960309499930 +8387530951877561556746885813088050059098237284649866946622347108029066952894002584214015925808803683837814966224683534722188788 +0980315065961365338422640630321175434205379462185984915822930473041804521924781482514510231643402826027992447752984590090815631 +8885215984427616592965846360943654237762984211426187791836287528989189066470173789608783423601639354776027707428728361921725733 +1713545761488733797112097100438352878478186093850974631628293903780866218943733017611561596620119895299713346283509695880279900 +7056694123692100328413045552536384469910897094450012862955826233283495356986105417266885152765210669772149818027501922143072697 +5044288331991180937483810266252299436099022518584143328384527105505463981298174129945339074070183868941494344473296478243433481 +4945558387028214187495664309036767910507538072289226775563326183770144119596728765797237537898375774364456608274929793323186859 +2322622760682958787774419794236441627708513520567224931868094563846705540632720743482044172976408519449985880794628778392723979 +7704734167762682724779119792543440260650654515136102295412021984201525761889238911775278953009856036983484297176809989345351898 +7538438504892134535588424111693601136717057553968849287415515571796218046474942306436018172239215803280193264125771352664266396 +7105221455394922182238669063185805229847494225913363125711170315190206996126469146603174629254473614090501455991736786538113046 +8157168667432314271904818365481089200297683424381896431649087725330131885687876217074390326660020019876852812089717929283102567 +5370154297245853419804188360942032243192631168128880436140877705786486797146832449934263947276552080089455965696884234934971523 +8611893045023888017310198960086212752413461499017699656702018998940525757023063840379418108573223175879013713671744193748767015 +1169024393509843272061563781622978325016779395605303924999369153283788827391502842037817134058668089767959752756676201396896441 +7712715405049463369575317556974267677998279848759269048895603544096481338768211601218741510541248300317436674252540127015114391 +4834430831243295519098671812863396000922057166207709070811235493720931115916983299692520931163380069504267039634742271991229413 +9287385557045725218161176454965763264083042136444478713449129334549410452033352200798030615759042237027156709457224173930955024 +9440754327156472541302944326043902553561785824204549770748860453197398189036670911169331773914895138408417009555757899645501252 +5832453847611197719596117303718224745254716796559500451686231076822545459079358621425609803998655831572289152307921157534370594 +6716617297863540993457906819123068181321563782640587142663699479729095230755403564453694626863314955875318305864016967285927449 +6751752585208130882471772146338918033811645841373896388668212294917320582400645793013617832138799866544853495551795042553770974 +7292062075462511235154219336433250111030971448383048960076039173673865773457243289124490997622252180509835724029497464904686356 +1703189532838988444479300672213603786659157307392869303094005510273480520540799882625565441527626460538592793551679614793917597 +5131469474921792991086626550504407821816558283404370799177646663722782715758886649389166252871185413421418269585717891891693033 +7009640276492618793739187876608008054508113875022453329081433650929497189681068056166559133270910556458288747450052550799593778 +3775026817449250262071158797005661647517869545017156045641434181332078515755353689003930497030264487465467676524742489128433074 +9510101337115987091584562788637829217924129844325702143952753831115106454935897717213086048464400730491846668741988349250815009 +0460642398157894924642248578767274436367563791950971068099635643161955689433389352445513881354534070645863359289997626236597102 +7422701275758347504612869999447090197900987293675125648746188541872170309069628366658720207642452150511782301406643296921604925 +5722148742542704368310731836532013521833785510008023772247459847917061172721772791747731176316362682231389997934194900072780568 +6833294401022288062004523032063979096666714062815023108654665205678873446818506366372748896978903704125253114795333767006805633 +0409503844899480726742485372066289542014116663615248040053134473634302073503009858583019797024883588714394669118201212140331049 +0801167094699960082079962007695468661043658656967418331551197267388919434693010620339366679098731235659707521841235334772751876 +6325143950028159251006278830542343903327924750282184108026273734472327418765244935721260982532015689685569244807990908472766627 +7594072557268049752783928609937336033485545218183637235406635759394992632389014311377829965838907592318760198667367240484412343 +5995370105199099659966025879437447340499996959986818273399469349454086733538293008250823125200112169460372394345464200255398128 +0278006855752348475839798168839058164956107248521078548181749550904031782397298429598863198195066959241173235250539525076363890 +2434406818995447305231609382502170967538696884871023912846712870860599713914006479152902091530717543673744437190057705732209450 +2049303158709878586595288112356673888760379702189055053773161541139894881284696826212357934526729889089168985110486922861784281 +1480496657855804936389615128385812000095228481966854328817796490269872553734151219601055960998715934877799282526137718284428232 +8638224569717329657108787802886111345753795000064618991042853502960039658407299390014418474102137277340588865538873397922966731 +2915504391345921079597753766165614369426311302871553494239227907927104755401277811135239511652090480469130728548314080193845714 +4662543498990058418814498281187250236958894998429926084397929019382124044722273292092351973221932689481754974738766719140738353 +4190409245555651289372817877351684664080842342410487693494611165285252514427042631380484145603968868053004939443493512729765220 +0119636369629430093021488300230179969441757666478935315483114992540843688913867607317956666306088259979412209337113112892142162 +0522344915049743379719646407210766405374162220565183535557053495876729008927029380341012939110046953015244548503515867668213581 +5545277258914067549098676802369675716209629211229823278440028400844384130766676038181293025311537464968008032689589725571813893 +7241917831784954432887951874113610869422570389407469506150472467208362227227524708480496950010505902724279188045395824562149045 +4536217193131427845613009167815485956442321803951137596941709381463070724725318338865720453796245623311051568325407640234609643 +4950000863730017668562367944404823933355943718044346155630933481082791520987136962663882167172001786426520967022022083855046918 +9948462347102121864710644896101448735320408682918327475914042821856145185407258575043429392954988610306816855047216892742882375 +7607463192368539597117886391111127498537601742711656094819081284338644518509840706151881140781291037342960935079170415736099697 +8170547034180787962131392617751093115453333934584190242119875787080041916215989194964046994229463391170868877473670939015394600 +4967067863801616048208692428984726982425267929231942089387972467090049190181520316912402532109719978404281158192349591335918000 +0182827511598856652325488782019351042712700704721955971553105284410000877214479216605042256842199795133210940783113951447780445 +2845448586343352856456977029480170139895515001400772533947791028664521178270989590999530566461508232129548565121491303912378773 +3186958087525040737796735368369363073980409705399259078085185801788329795473492605756831924612159181775569516279264645527914405 +3736227582287369825938099577236266512057139246043733096797986415476742434871074557767615701953628932757311718425644125724233706 +7173052386737821682078985950554267597062731307233678638341260916391599502956775154905191238402444341035490900454688563799626952 +5147641961470238255748645042116320141678230506441721302812017771332190399762353180889095100320685446894708468194573746305094928 +3469096230652662675602711465325424276546888133056279141284661309882996606616677713056958440903862720614419241163546636652676601 +4000176045790898594283018168562878609386243501149259496688481941480198035420169390431588952838073985685288362095448469635565752 +9472291341301030094187025146867336563674988987757598438413852630025766068250538948297399599973328446303590662904124689712678759 +5558918453683884045568116648619640982031099812596210893142179251292478971547413993326289821580230193948481450670535256195029562 +4928975689440726890177460501590676962108712941853309133182477027909307432937071388936253714931106477057738446899643424898539007 +8940244387920877438974129853648880036120882337129881652544245505645048735775452951748876363983239868801033134946650378020657016 +3701058033103724656733182793045707161803086804756890720227008032425755334349001190669627279911430274518624636020186433616060892 +5795048614649402340472644254299779685615196268626556186825992241948964494636129874107575230767378691185971658847979564205979820 +1236133846548113964258787812605512858795064801453710507748252434757524711761598464790831987938492943750974650361247007836117825 +8985069765244610015917016588371467517920803422724008738355002305222843175513547415525425623310272808519127269635589538855868669 +4866637881580384758530553032050127301271010383021829168699459836736308865544704254424297016793922754172114529453114659796809124 +3421197137282279783180092595776656118340512425979400729754872785822827951114365899638513626864234673281733350488813180030409671 +6116540261589173059806435182727298140303534088364505375974117730218290182662177594123794279119735348291936701406861588485281875 +0383388028538445842643371054045990749985217056001543711456417105050188186522673179186319252532355654228417953034579705914850138 +3589398737652268493665223622209314271095213796466114005222254738357264490629049957974234088293478306962011737255628545244120278 +9242006644961491221804556688452952473719954034253935879343918188942010496090705258446852742963029458182305909452754390287793730 +8783068031607497439341603800503578168008017096526972920750091440007773022446775696934539156855822636256531074692478856955931965 +8800823547223548709312492609422799167744315137636030763502283934241170303284109496261288800964950861563894408561284405190045969 +0236871653896771373931349637525093015261721424149322538581449425146949187109336492808412768678305259961228548229258526615055933 +6808296006076261647807027264924130647898638203490518417736498838185026931804266322409312178542687977048973852811639981510309567 +6711783212313531503601350937924384067635440627869411288664439772524317458074685185346606914709019186196743715338064452022403271 +1329461726620692353133812940387503225279782302791789101276394351197294472993751698133198738485236134603208512597461992464092534 +7712409812790062450698418919781788475967301395142101015147156004348821394973951402124816517835984222889123201533407163528809483 +8878866193884252627688867520926767781102452754391877549372235240772045942305940402248699505448319900090404392151869967674502128 +2483008370299951549301955588790277512316664972307031593695383033586616221934522732614016251454351893271188672805879216575868904 +8930668992950485150931537517355790279447532793020359504907584544928499659083716918240980733762266979363328381222043500384239731 +5449783313063957701679502215046202754515996947849788011011244404372242125949053935941173427778943061308870747330558976279745268 +0087442866677315281344307628368254568290685263159851300491709072441620656219856549470063530059686910247545618397733080267966459 +4275923776467144475919672663054261638444876359385278313589184049205522647886045588220547740175271213225571694335593813888699266 +5631076970562565479284442184748121529273149320713280238168313146627141356684124916604998541431548094125068927278902251637629771 +4049873854233479495273404123122985718709224211298472591495302249456292201698069758048348513785824572699432884088465158829077277 +6601450724720706904135791943277181797939378581152182984849501639363583354753791712537234399122836156611327530479415218232099189 +5823047280117680731777259023254262177630139266484401163220777646941723211983561493087099972956890435877152661438838138706007958 +3871525096685339470645850417153350859644241363604522957863652066029739101996436761253594694922069548250943637910124220796682359 +3789021225120290908842849945791566951268040039302726617891236371450848884685584694857899475642048623621033883942963333740640913 +3413392907322042998743788130668642581010260731658079311951196515199970920919298587850590756579979416052745401954692899254629962 +1834576186290812892686277778078188491493029363265052305832102758282145333131211542287672262899869561233857209736661910966320185 +8640618069436548861856298762841794920426604496493168277782652390652529026370866518863711574378563335650089395301480857411365309 +1062855694706861343683613721473054277513151272875708166067829305351279095564881057460121748427296483260938003342506900294399166 +6708351271129793412643921214770696882147428525507519544681246954889801321262715754608536636546303081680171501901796848282715773 +8170065360748835664225539865399039511547738151434626699419911223518847738407528346693417347838281049824633463358386526323379557 +9773283803239608948889044323843524447778250704201869489766199362127854167469574262448141201863158740882252497005327174568985690 +9629170341829493159540502281159419978757497174339132063688621315745991750554750919122418499757801567167615195540686775308036772 +6813737486648360157997154949184994770910737848857708594705159108226498818076724620595499835896899658135344576077658527954169440 +4916163367448740109143119472115171909713176429892100499193749017654102572161123098799425438195950481418848253249246794538567213 +1943648585194380435794184207380836831419456414164714608475465257187830071751668999365832434059345210749956754565801799757863175 +3687162724713568040156043932976576841434569787909011626375244300886516755159434297033774573216110685196819615175124148320455479 +3344419757198679638351706919063941845054040199124828713172593196435249592482470220242606177271518431150127043364815791934444495 +1400008065520008098162065199768125759577818782918099045365313282741980846275621548204708560469294053250492025107214712766610441 +4165080021198705915210786508550601653226551217888163799551833017968067846988341804528031533804959471453060256667445828562593413 +0316539921143167379344751397929218943090424282667061717675506492829683232765044382831375540458280475310930354270466628024378573 +4617204749069035253813713597661824629929473681227979210717594546338035842018470292109616982725985026372103084242143325201934501 +7628437040946835252535863570081482725836702391432238382009021257963849956941583278276159017021059243802411805222128391491555532 +7118276592277047948538579672067937116293934455417400255981648387252807293008272125197089405857447395662820745517911550198420495 +4817641036360381588057889324736649206689772293316553388319983835732003282890192751493367420760022776216834345866178180918425947 +4046762929816361002114402224273088310438847474234192226897299571581711857975564268312621600137920668387355607049886008638057179 +9349209580226030857313156556379575691164723527464611358492523658913876021766102463374095835678962118324213355237312840525047153 +4458386832215216090846608267034928117933000209670619365236847239547649406543272314067052926849866069213065858041288568227157782 +0323889725674406704675299432615839196837602006707700866051572397447613281818928460518366859474985370527579091269025122957262918 +8693066533952054034279975214015256982660363796184125831239795876267900616583623647519579472331003311428117914837252346650454796 +6978896914173575690746678387000060698232734806725458177123404206650082946025871125219582890983005045333389163168584620102734094 +0208343185195645077429621338459120990032615146936965171481121547620265509203707449471143882951101626839387108073423291107985197 +3806694565547588080124701297594558579857621291153844744163973871845300036486064506978169189349641947450555475680771354116721127 +7571370673293744335868791633492268450482890329783462471218711903016708619390949407758233183647172103050542587791975330523548939 +6895867004702083992837053115350359081392183348123057432804437516085126480426796559419343457573809103554769814374422319272987621 +0493741336702343083547043049060106406621621423866156744878463280624931631638757753907480473151395705318815364246326098575320029 +6546610394497897246218305493409122075422636049647756434404729742804710870898441487104017723846320578576625101570540009682146545 +1011863982645118776769923031005075048502898177517353081584252680645039932448249490068383719149012492191034530578466311073324350 +0940022141946663679361628444591959010381878575634668757912852707910261260983582304441366347012174417975464831931140858093450475 +0648068206853955526969735294213923796512233785864528730934169006767896899020079186828212231303729771906306615698951701782896040 +7705404267837637281673906483600597132329707232579985805240515664780686649377686946124141035632716982723408330472426681691722063 +5719449931034079305356439666263973565128713593058013796099786122163872295053911196817120195135137786906005302955333296169099953 +1565007759742421563846584208111412785562612780177121771600395311757381072903942514964351796547320888609281738762831301074727155 +4840914538717807330571774867536594427640229999158918266573255879619306849868398528582717693006558964074770743956975947230605506 +8434170706163547151705112191666162854461320116071520338539133193324696905732424568076770881016026396766250345270611437134977029 +7078829392513262023910437193357139649522860536333445198136202621793588265321546711781743395796073320028953300954811115494631443 +0358109781392845465184944557313417160881340633391419971812545527651450314512749202620144080637491419201752326439158460989691315 +6502021443239860929638929077452949976769441697009577432157105021271893283919677666379901876413440307841861569738215346309784385 +3775582600443257869423701993595504734055924868316007937741523410145241997102735147790121190918364977637486443176097526249923371 +4637116062887697972788560169232687166734847960365006931173635513798426534363094553967229788862861423059133919581108100325100963 +7530945856385218994386045042760138610248296085030904585153248074916348965900743636913197206787899808540051760130445456596005975 +0082651589864980975976353455996961471089119847766125021188045871260940417581575204228376995886410553255809049908515562710519369 +1760176651046607404619158827695196639809040360410321347276093208667279377323033464221881041272072298115933348047136279306992189 +4449900653089574942697877619922022124081013931968320543380925350254859461377805018823478776678186127822712476112142520129506611 +5360537273314488942383696459647239094488670283840872805432523968819547360255856276619301822761467290415157270983258323381158842 +4069470180647648022724489031315390330623675246416531426538288357194889582860078070141817901349890459635960185352818352346101741 +7844251807909360860529020528005655952345376045413861757127236316061244993838373872852044455049706576181912407467785016013363013 +9052782242469623627138305243693885375972159761715793565717369599576257075617300500868041008633881912456191819467065232604757474 +6257367548773919654525538912712606831341580701967173140576682703453195979358288347264315572356542410824096892368844905848969800 +0225531033632269859821449136659749178099595609866207288912288428688139123199517309089785003185562015618879502978274246919977195 +3412420395378666741269488926730591786905587902546167052955793049551854045851796515140844301739953615780488764468845360750173095 +5314569125098157310473890637519564038989130593324658307078574903372187794019987998391252363359372204089006276213106238012887639 +6663953749875926737127213800088216077686383881272422931892106536521390323291590624983492836817664288184016273391511477345251544 +8638673096407867791116469531963581105938614830552680798696429006418115851082700139624263097469936097502648484256024821946642846 +3261638416472717635495620507090713763983290274773473746283045337514051847664087913092670813304318998742601034170158553214610079 +8880759198069479182867513797991706527425937623927208476182273090404608783390855357409578977746370113730628533384722500327769010 +7964841863629197861004282115308229708980836184692237168958174288698996726561927516939282475998327300490824455643829853959878546 +4078550144875137620442196894251820235783690433882002285566789090860837373159507997956136455505729693351610325429624900264497493 +6050282912476085926950954499140111563620447106769829012752567207785593165136710295523078591701424407957863045561973215300362256 +7166661176552788865443610475451627720514979209097163741564119254519710530732032781304320231265321064373366029789454524964576034 +5731838949398822830420671258681703806386818639376155723888774861742977966282701086517327314375297715355669245126118723701881276 +1224558805186365369295742263213010659311742471260411491616914473284160243356280450620150570408750609913288411042993040473195864 +3965363040687291139829734658431286318462224708522925124414942719604612215357576250193871596416258769483377169469309016314582237 +6893425774786606424591335564336597467021697313261577560777519605605111465025332099784940659090512446671447989183060023172745349 +4463023117754025492704298169088346276715292811942385607520313996627188228077675005968319409041724080200894220147117291796736469 +2838367610734769148543248796176416656292040941138404572575878998139182976355595776291061040689581132913463002164503763638521768 +3585007037968899533646050958305472231296473689572926896126127396786068936357337467731293075452760614111507010520769025993887156 +3025256134722351463444782906552873128386686368275305328178049328442606382059696387241124580313700815579068965097895955058791706 +4888785355196252887855666442682481926655016587897905441501499624695729356509071382797002137717002280304447105900656692938689937 +7373767636755443505506466024295128010399690845976961083809222125377563226037530095320414396885247587842781981216438439176626761 +1467040097296217610154727986310524824930119662672041177376733844872215017687323322767179584613934704188382812120901100084067248 +0055261010703792927114582278798343530536332871891286962151403775103518820096790537756954678439988840466572934380472009105374277 +8745186553258777370952969850546252235196298235316976663904366268611878480055244838332857644576178557847556193922108941720480963 +7310291886872239579954842039199219595404202222951474277713284211951970030467647252785199038918806323250107496561396165925992981 +0269739665100112200470147709926216131752562563116766321854129961314938822821859829783043904077776487245854331065090150597583869 +6022034598022521394602277250974332652859372516801989113143038045232940829875622476147452690929605127893923207935810728978176813 +8336719184987758081413476270877983896287998313373933528191759149653154025750363065606417948489184358633308380877845477894994394 +2211461131853873156810302948765499385999974459433061226146423964769287619911252503115962272972255899290410107199334224556606196 +1402532784763926313823670409002705225482185592078872442217786835593632982577282903273472588503956076522785281354947952227775013 +4429564170373891691353053299293858952840011672714332817264920331107567378250030393312486800497701640449816183190591793478209061 +6696515680454660415306312055092012435204898424655900246268144415272389485588150485707845045690614308427142660270754614963190500 +2858971388307084324374434247865554208042477256400063151868591675576690659860039137955754547923321131572573666015058716528677560 +8082431519645786853787074095747644469767343409908775777981556502295146873300678603062293673505033125364497124824045616025095406 +7796338540952910648733467788030865929612271961440794230689497348772551503718356824778119200166125356293580937006896521596909494 +6623286777262305838866280436574493459341503722464731387299068764367690003490556552997864648359791608273875942027789979081747901 +2820762823681177751588883983434913077317012385982082727245344446265775140411273692313089468167013918322825570599659499017461253 +8261747834117161824733335000918969440857899686470868022119901936951959497914248555296472517607486195543086996762943719289544222 +9801799673583374329577712601117604429044210658931033348348701101269055569381455391166077387776701104476767458640618577366620088 +4812671524372167827325442166901095131227009588677251478248615586184246419258110774327904940522040756539440861199193515869861850 +8664636186261176234317788607818904273283245654628256017166703040583397980518196864696741148650137946657712844496351414683354360 +9233785042626900594870398424394786589093845861107111076865742457098507103631392813148996022790966610376381301013746723765671101 +5599585345939628248728616861490756773613146232759270658252490655694524473642364799716984131253156192355158951050342077829511934 +1018115413367504225955575347257464735373797464835594482025360169817251469418319729479090655145106287600720398462808509722342523 +6277929810316831402715379663360019513470321307271888286707156176722601290897267704973159272389968049881611196311224142002818283 +3935507653397356468132051961271044279593473480413318195705016813924079917284710536189206698836513833423232062305170527285530270 +5930541214728728710514258849354991541204981028873011695299038161284051747700208580498162480391834623057628739847088944321070540 +2131178112490366896971016249779880442716420462976316622931349965957507940779710818286842266030393060843328131735676772739856028 +5339433698747921036431228649318038784378241814872129036313002907877189828316605458939009159662654429547906907634851960924867766 +2725144295031618884966653121214453016640389429569724950019640175004023672074204183536974715285043574870846234625695507751624691 +1101023040899658835128880208200979236233784480432079242348382094125974376887755928733596001730428491870299057160651502141598807 +7529805201863368482438225714843876867695794742159176187308623091428831090441762886002331398279108448588579520003248961018841987 +8520639531359520492110038481387012497220545183327686786319255787574425654228049283708164761318908288816634942299623177967850311 +6808287068903794688768313922352685578188040716704660250946776133077382541811421559714303292759615754217647905753791800896114429 +5661937959933192849164004778980526700005624355990312125795014118394734147334017327033918908976463421944496702296931260093163478 +6866390898079223822127023182717821447021210719486206672090868302561371850042810557962915762631762160188811564644765871940463260 +1983784957124576912722518060888269293020694751991232003856484645400501387457614453894654158242083061986108255912461168024044362 +5023757063288290577909452112558045920010016969071712896564748738667312129264760935144195712725562634643903560619975222875598092 +0140266427651227095088902077296483470532188392053269670883135265668245606745264278532552540957167938303029227319042084398428852 +7212818706985477890259174108784143029357600406814727037979412733984989889987109358055090912357026404921211609781959966663957665 +3885309358663398776897062600124335478996053317484403001725157655044282776168453835403175508740651996708988152698983678588345737 +3549561496006579864153172902480324325137781423364931522958523431287424861594425297230970169266435951937028485126756644018421959 +2100522734881499893473520957404272588614489603694208305341205355969403603944823313607027770883562580199458807047182736184431355 +3558566826776678787162145637003470985297896846344237649664211727912542844807936273529611661291547660198761650362176216275159276 +1697273051970401823436818435327916444246809126780823246257873146180484087471375537086616586114897794676638646530157453530809297 +1877141731822379123458355749374949833686834219493808460922026220049948042459289931477172440353720942293259692771867444391010663 +0596114677690135744062023310258035439858771664427542203238516588128327906442218908975480077474481540939986337760369812161668739 +7140350695399428116941903267710113882542083290951701275369344655927910523637037185426739245384522097331905044898640671070017500 +7453482207139204385313210256702640687353020750058344407280878423180101848431141829884177527114462939147014281506261604724365378 +5747051408867537658614820169384534962462911220360124367460201651929905485919878430356918185750947336103425001412723806012420037 +7672418158298677361496394053891368709542765466425399242228084774006250993472472890952527381058062695609973430135097018848485375 +2721910797057123953879416244095325096151158592383520642643416351496904866623597785985406941433201519864328401275343395705908857 +0642983563496035432630122777955167888076028823131047975325063367604577001605053104073667764503079932854743114362110563627584697 +1737450520589955785122525243234607141022634008326290553045379074437558301788691892914015378729774742647378096844792864200227145 +8049477947243449894011256046326611511617306926576503154750985615770186729781372043096871431456514521234719945343529143211616132 +4060930164560164022219102032312173710730634459253495956116881900466752536260684001258460015562183214690371015163609148581576715 +4339672054138260157605312889148304084301210593319661500758836115820325521753635857005220500312010112221791048318710925629489360 +9098118884517110952549620567550824645268745886275953112452963794466205654973301959016969502644339312155829940467386423970146851 +2030422710629305679756784568188030405053813130034967510951876362126306529129246017456183221963883892572064857246348841309902207 +4014325197777088687588337561652558142332091500654763031260105487352116429628561906919380099545877339112314250562226830515084041 +3366554959465306038343578751837494403196208574534790117389855546409717054044483042532146409499847066640874035730246190276785027 +7784874865116514506998867941821004659533959527747303526874272093224628939120659783314979799979608075209344316698881835617720838 +6440603777916554902557821762272317634207749152011454338456295029524399535728380230269435936275288518444494136138485427687743672 +3697767429372731488197371169546304182440802429138205900920907076873491237616407487249087243231746614255961699354564621444166710 +1737116215657118949418096366707426664676852506304031637797388892083081543972250240515279155087924413491083737598642466601038186 +5943501997584026886556269657780895221836455524956899348998644491412008841758015117023754182242908647650511196372757530427844200 +7126098691386230123347928604422159107062602585844839531380975985325229120484080904014468470157441597070403077589128617082084491 +0277414078111008949169676840880976444791578133697866086476338429943906280786246925904505101785927908752510981120731146336711732 +5011921703942318242688882518650473013576715256957336272842712546671709676486379489552317943731327342059730625728957322661925103 +0879272542563908841343149899911945430473689164183260890378649854294324088836226655479509126988786763035689227450447066454969580 +3635797692910230406460094517526461454920959082159212413377990917407487877377494660930246389887432321322711771671714018210018244 +5496909568572645176488382324921312621177838539677336553279411765862833091060541060811106495286938550448561214644311877956532326 +1372848285800862888898722477263959718622329488994306400315469392140836614515990231729283982778367781183582185203884353672168533 +6002585963544294141850925971586001726265376264686412361898087738304610666926697454083310088708743790359728974336913706461189057 +9544994601782264143680659072037087516325897282651078850550946582804440990613134903404213391711941451230258293606634693846961977 +4294296739368718314807261871407488388139897913896057193551518458028526780686105950168593279489650803151515173510050942218207014 +4440222769538547178501749374936219442687195706297985586916321673062155148432380676822216859833618467359812508595764374585042802 +7090291929148830504311787542972555491835675686001439326827409601590115100758667523079283454747571560861565934370463671494879471 +6306207613636744929606340131323412825985507087453258431275338612289333774848983021189785244607159387364635876091430264215902551 +6978625947059991073203270373760209952614062079324674488242868605448441131389947105750784088354616643566884781392969415722350013 +6876895254868693830649346298281314621651851243964761424180528331393082774872122268180557991527876049313013835528942921722633863 +7994555257251266617368145187318488777925218911343096210380569645729516845387191015748935661281511118325824054860678678241487526 +0561712487759666786219959935849355827031795611802441457990195006279656115907452353501575006228849649556626967066612451740817401 +3065700444272921097020103095032561392189274167240364636985217260309148382438375007728649942200430100798750588348282135626863041 +6418290958339960432209597429802884987767056126860829761197583897408153172039938420304452769552704862796110442336483929988890230 +8411149220304441269312528476234638082926201849344380193272700895522547783655344727512614476846755120115181365390739989354909216 +0361827006206843386504436443633849307138803846456656207910374387223474485947474439419270873842802147250905121223284959187002960 +4561901942869500550331725452244098638243866570298342595884556094097612128702524209824571526031229091793929079092201659156014426 +2569784663225002575851412395224946857021846888745089626571792624047307039913201999491767773809090290291368272189546699726924662 +6874345054057523806453164208085928558404937408086499251394158950058605789150302667994062909598865360566447853006716951800116507 +1806892347177795837940597487685027448543886249836281858785204813002880073746279096873385938335081449362375954555853598273582800 +7280197984136582079744511307894765629896405494228558457447110951049222331477610433641974978714281803792968381573777298819652746 +1797088044067027854938520030542484156979879022746379761147066454374087806486473993175758842146367406488679492927645706548068169 +7653106688789014002295870215309142445613718219291306854675259348422471093504331308963443794325054200760084750801107789128152322 +9003923022817082834921721240788410357242843286036977355497738812382673303415071169601837346379966066053060156517838544048108090 +8927903196351477482460887787174517966173931425526452434853205934174795153124436599267973662874006233354646357728472499860554504 +3649137050137288600334652169002894564926317935052375328874723351133720379648102007752831671041946276927861278960706248758820299 +7881676548153486303936365019720006418330263108526672450012829414081940012783530554845066547004930367871816825750941800388750747 +5617499751970255706526710234532717339068075364068621300856559435412791685014748892438638038434662792832126729610856601828058978 +3609505162338640897172019393757051075698357002384746777463891780931828353562474692966266497051217520058712600221078906316845512 +4218942164849049619324701770389240864171286359070468568203895572380950389773235867347931266228357895243861158024586703415416725 +3286877811857113758575603255834974713137243819997694752214133530728139390482439967896436662426005413536915227299430206070281748 +2197090522718698504657769582076344098155286933671656794750871593655253531647336100969432683359373204499872727545324036411875127 +9266329520011965890398156813860326403682885444460637962832683571266360623432333340007962893574859386016733773045469648300215724 +9360218780816808197711206028145267393233058010562551036732947883280144516446467977991400621686114194941533328336858706301853920 +7975466727102526807928730597288066888928449183473417942716701022737828267407136547666947638756923213101355014521544864295487342 +5529807372773853881156331115996145600478892095295063408262502214227819442322854320893250029815358897498644974831455098851752144 +5217677466571168103885153319146030362086428120742099054171064034691470505077333070394007756131235288630142015715989289401952345 +3238751872112411965525172279795754140090657968922574492232135102357083597107656178253770512768121887161690539858563177051498031 +0621143235328817027584926551312004300826256116914067938452329926333908173348351478521275486940271058902772821984682443421901374 +8675029994765542582556780936437230200725353322681947577235834861538086815882331325782379515826004655685444830286018743065534290 +9009226484124905023296329465126310425464799999137557622919308471018246198151834136297559364600044830857980861753679005100073906 +4026810600660421971978121050945152770853655639964897333811108395111596792046176724058242259398267547036042717629901672105588823 +1701174760060091817941457573061071686086097383906657079983926029247083644956175901708832562604741667581501354921250883246878076 +9360121868675405580012858168094642670756925814930958722345840360155035939830719501362830202256115680429221284715437831407183230 +7627384360887381611002122676157337577850291681707758128451482725333215128574377639863228270429176470896652784523071172226399208 +5924717495388805188609979501739528551778957144375491268555383034743525334962407264117945701829683575948250702090310348063964292 +7285038571762987494406954123411340680613165769286304746181220668099890216085869650378284432589286853709759806525223094629118994 +9108371459503424585162428035918267612861106220506440110173672498423384456764491877643881942033565719022603374698498258614890281 +2119544835856779238923121210216240209691871364625727861760840643200609817224777039142791222829730821468075117365482012776292615 +0287625006985324113383256843550124431381565006783532108722669073077586017057966762263289920449445173729525567088605286164318499 +3802234646726929017573814267132203009806257343348973659561216577528599490870752429375293883854127262090614163544620710283467000 +7574267833171812189998339587827378487203820258567218738257171370030553714438406356225807045913475052445364294909346459733645424 +7057209974818106332836072380332490777736818456915022774135091028181738711236811811377666386715288877847647340595585098875240481 +9197215476954479756497624010576859326493604058234153493216522641384431342823687635555610194191608220091909666730522341780147856 +1119537820044774998504706322100346395055393450296026673688711639572331613882591716507869222690391102545737433635859812821586270 +2028121903848368987212538442391618102128444213522329584551212886903253244685970973210284229639689302832127513289597893485709871 +1139101840913818162477340055623563542434178020287614160741421605723686784042526943742745792272929328869556547712709877327596063 +9107408768006237961652791681788083610784715577214377191666576416371990600318918309907846332307177843177765073709224406749447169 +6733725726008450978073741092474719053157935773438091672305982444490248804288080650618594239161482780330596854243463318510550141 +4618707611170337857871421415147893285747404164961249246066409955331826510336908541776726066361511390478520619795261534651528646 +0029871423726184455790317017373341560382122160829511677232857909382425970016570928985722750518023888742942482674353399294113016 +5851677764630543407742209257890179166834894877003624901201481696534056706522332095998210032002750686323425734736641298781343895 +9906317458124808949504199178481097664951999312281141056844360268819241235749855328929404804574246937697891911753442746410513511 +0301178939436270473741478421267690206733306236983147974561800903978683929049444127508643906957999471564453966363638317098479091 +5695590670680910148994003236516725327853434675592633044599651978504490768012904279502213144296250305384458215615813612885360699 +1739488113025246606429350291878905105006718514335177675021282668693092738324431010591942084632165356255063331639570681777622822 +0452464599884896996261376953519588463160916217402746423924422154683861260191652961898971705302933158251417160490178433853439776 +4496351983750730650624656965901956261109512227868877661440793378743454026849129576683511149685514643006637302234104714172349199 +0650873766138283576167959756442091757660353703079228843249388294567585567331488037468607523624545996431350612623181744202876623 +9217884841612782253645748182653936757610046223059439143748083525756745987386187135434504743166765496977544377259895573270803669 +5233343431863155649799928568730055354198316775389815272387174921789883067246402571006054994197016258947251698092729387460016002 +6222471188835586754619063338314127078684770466144889169048987130424673328124774962008878054843463980934491177848530073946295466 +6092983987521059016788973113331515120050882840886284842856803861355854867424082016223127983920367739842939341557083529339357539 +9152847423135593002663232861455632334026991039822394086654099188131348343461957860123922177650787602787823148319677335222779269 +0688837540315420699495154888659745629289164825587079409113672585691726485783954257060693018031835646491154074592682796993759642 +1560331745546205131052821650865965767504353088368099838110467887791087910699782310016063819056506494246788726522788847044002972 +9843063757395851013539217226262322774482770252168515327930783086524549572291277963238718919614771432829552926078930464389873382 +5165503350814374626462147517304790280393184056104578516266158383058163824599464967401146483647756651293496532192439439145458877 +3334882919381143165661074051363092147447735703148534066554968559573383084492917940343234309775642206678952812922080261015609561 +4150090892422827778476783958648862075260361082863595155851728025658354185826522324370827070485157038772970922137249416500335139 +2270729610239360993359408455484013545432335012129825782822705159183112780581412024925958595707332590452541348227573704213342055 +8812698543571227460191878467610741003808872140196791243938096775905061169632652370417102797369863591037024610630210903737953654 +4650850571724314687005845773201562588529522607481288290365062000530992710204626283641702861862037299641210309359904939817933700 +1541143632255817063903213640253364698002930217355939377060114361273809860644552941254027110592936933650281526566113066201345129 +4904443218069061051090074486584977672087200091073688866301081198367118241651115030647481869824608967401802054144740280994709964 +6714819600988123781424031251846423957142678675252652218273564424036686308763026328451858255344930462603371376203101104448466961 +9509079835837547622180222314290215064732353288206452347849664411672496172109171750484558066698591320017336090478613367662057930 +4411722606999341112288605914814611482715323396030160357274723566603030739104369997016486251468461541345322418772128759607222696 +2948183785699572384084816055776039138878581148811752092336317365102276231176457487558011884744609026187961229782054619496985034 +6425095624545385534119498681567553265889108179726785664829530547971892685921754372850536788921833807429343926950424016846919855 +3908326069100657803588698889883675042505627231337237747168653314558267673583632726264839565017058848634863075879312565153073799 +0321367266050720934662774520376532661682212427124263069420126600109509081952403933716651001460064949865799766176759028464538738 +9741038058065551330442704073919666652946527397570055939551345839830820648016177629571594906346803356773604688620184511211636838 +5025402557710775296650014515224591309232698067998266520271098024189399721692010614452489061642157793190304607388980065831660161 +3865371756663396962706440110406172511579502225888432146828231479035598172810500698253040173626514931478221885791112009689705108 +2972286823839979161832311952971045738890113686974537517223372452946515286365656547601871718080007574659696313592727664057554794 +3671238913457833340723922452738592109309375009714705044237276003225833183922594035143870946960395880735557105535062577056078657 +5639824224660978396027300014663032753936090244783494976851962014859592152013834741021570506331633866559318037963618148063022083 +9565507482964988994686034476299289689477300610763650942914931588839058915298408270882723496065586551944274748829842524532900569 +2950174215698554483745941905548644387438215825109435969023495189379067472134512077000118600012988374494816155364721054534508820 +9518525904194943790633186346738672276318778362232864095452323156494968062204689793145701834333799109246633884847638921045253621 +6777489268860937675801194897856962853781999286279373198884776447439334574892992495036091872659389999472183722596570197112840487 +6940178173947556103657708676822035069381549630078448878860057242455764188710473335746627500154960475546070307022636234086868814 +9329686102999304427635999138246997518586345169303923543680994865014159368545564362656579126189568984209672180187596509411904289 +8400290546843033015441275084455799605870040808860666105081155326036596425585507607789010815662662324838353003534213828215099749 +6262731155067666934829202231823905666280056341904107277513506385716411353609764416145090669300953084810469016331307248958077392 +0982572667217714967858091868055542854621721843072461742877828288780619596753823464439165581485796328231801786913442847252641526 +4848655976411335420584575046674979122655105315200347515565901735274063028061587092460808297171218521824980966675211293241764354 +2938658446761945214970070404972488238286005305727629688975307477962234711112158327154676636619409638849544823620779316735831746 +9233723105727772276287999774922758635916582468201648663126732610340191585232715185516172590941978497752196971596838962424314214 +0715690795433694724565717518703035300373548926507910938888351542625042732566820561320308097626791527666632661894798286702091403 +4964320219506572216963601199129841376254539941692757336784083929771858734782219556315822673159169808692321828354292775097642159 +0335559611733921504268377474272853207366325430567729920137620204884173222566965027114331948190426982554186105784631841860371363 +8033508558384267940900957170710292773682470738089554833769290979485707252598489360936633550982291403202103393568639544279214517 +9104033203529399347159766145704434677301867369574628242547881760927798205454869510642633732888078982477565192220841911379788216 +7987660031763621257245807669247102344394837687835695594169011679441270384265326170270019240149525684618010449694008041756350062 +8151902440733357075969965129856431003770185569733670086864031515027203169324745948550070082363039150127359144961399154724592013 +3790327961179853157835883754649149214290489479319422066362773172577179735157530589191336297098156970651227770438081844185734307 +0727046673630137406560803707058042940068228879956058420026237531511228698088255979495039662974032046384364026969019919437568764 +4204578688546460366354226690592321231974552743860907666913839079477111780605991648504857092410645662298682793993079551732419876 +3756294405429570395559614676089322871628319405115443082409818445213875343993774082306018456892013141571878129826624772129903802 +1915879655542608031603048228973067523323417771894628549592585367737979812887089684899398996853836990904295238545782994463736003 +1862917064901509743356712523964887969381684280627429777002084722985720739317294453681605121270099743339673719320682461749376518 +5235629358000506017419756442938714321316730985284287276160530781176188912088650998156658168743552720877268116184356043543445755 +9443882656574676998601479646029201608533720364834332155558897088411735201080867283586900504743773546108777074700728937687647870 +1677493272605889337983961765432999426943110889282768474699096119469883217687878946342929523139815961885455260346054616901453447 +9942749715588817104901581207188857800673119328816832808571244811062495461360073125883981969697016073423707659358263862951164461 +7589849154636259255662987558451236625196817855393407896633655975124730683842916268495164065502043203437031707489979618651602416 +4877537573726160052953215845080536556910640218250727320033197474127755187683283945624270057808449225177808596236183681916214308 +6307674309759482316304083895463219733480617515657742063182272951154632706337489064696789783685482205360435876431996285794642684 +8643739931017169670588941204387320679922643218218094561807636190505893287798227669018730741719908408353128973453099243266062517 +9595878873331613803340153742041195527735780444717553915355354609332848505998881213100337678158719821578770031967558080136888152 +9826258075448305567391906666815695574763625260042694682673443923778013491658161518721464450500242508138338760627120693213553580 +5553946281220024617384962807582191448987025563460515052097885240434443584003120884005027893320006209592895012188176773812824397 +5351207991648894581591662853877103686069327469518295438143903554689747912914341445196157967066587607931175822601224101946313543 +4718020971952950496510335972370616351651238578779144226177776993578212091520634237018822715136461197024637982739115303152147863 +9502456279815158271330961578383078627663170821879138142148906444928946168505151984176109046790336568113096829851051335078064246 +6953240969636903319954311768057533966261755313567448194782711138760563765547507739230406557393058730942539222575666804143511119 +2069936646276438376676948110321617772893399556260252358509335079746651069969820285929369016951090713864814429938558082689945968 +1521371174145731236232976467624651809029170086344894245707746268123154057519935873219521049882236870962226595226944947282873198 +7819821085451992801895188229558460134173236266804505398682944828070031878272264470195089873005628963880175293100970223151449714 +5581893838568192200692899357118035029378571475716773858918510887575125236858310172913692992079179964473799145336381338174573764 +4769245956084317711076182507252518842194925936437279722455907758875435914157274173921143663890367325067229935879996805150485626 +3134946076248319258593699700301770098983689499295660906642311982752703674969335911328913636983282731424392564971128071529792186 +4653454953851844259383149899336223085116998954459207141602572224661191223836954928657358541085552778110770329785385935394034672 +1848527104180913477755119395683294511819189869818050532796513348646616780772576901429766012659310230873210004958312011164035296 +2958092156871189322732011639055425235756904126336215793291675441374713359747361020499385226744232860514052551561041374928704972 +2212834863497329670476581032210400927271310708293108228424935554688407632484416285274174812931270321948680850009808218617859705 +5759858970411718249018834966145654061840913712087092866138009251429104683711023685469060131756225674409696442152891897372343886 +9744590632172501257249804338242668855862458498369585009073080663432870189779358421204658237236159348444067179731646552147785093 +1487302286960634890703793800583916358993694233181958170067964772301899115902690629049952516027344747956086526751455372742400915 +8338876525824289493587802849949027999302785007312125533604872964433845704867828931952315117389601715858805797176205231916978863 +9771708561966315440823738672686475996359300091791133612159610896088743156170335643885163215910168604808694162494306049091846493 +4260246980990607411798428730055796631495823134395302589335824515261468702425017396399121744052394058025204383900792732372609031 +9971700068769073722960546542881880186840993544233005529300285659415785115249158554970710339057479784757811267863305208596232681 +2366740530644754206054437467535946438364093779279894170145358386159026725061074970741778654202207224942319105808241561149108640 +2131299570746273126264570482316141085118510380774029845237350007337531371596475771574171940133305454244596824947855809667756017 +5149174518393308544163272712602730988826876495407743839625377436405836969824290538418225557277170446225596736304939253815552458 +4121823510749511107366436348094783330267428964699228112245771169272372131739184081655990835742337126629817378548166241761588650 +7681892302838328396599913751062470874471102029977403036886581893796472818466025775981658918091981433719370508505357431231485600 +4066251794397726082230815863792863498887502338665772599923859722198014368910671442996024261604600584351270667582157099044750647 +2453319243126941865196218141928398513931210505017908632678271906294188175528244273588712226403525451579574075491526959814201424 +2165992720884571403675858895697135732268260556618466022757708781513389984131289376166641651464163701439701648503883511692095752 +0809953098069693344801057772787094818934098518685172639722513866511171301116371082267224802273430384579466957516125344869179896 +5481982401939122614289521472353637017126771394148320518526784723238206085981641590886685015768028048372274104457267692145673344 +2306442226244080959840391166046824665212018683995806330612693877379188175903847065416666586796553782166003940513860649019698449 +7839622245401117642004314710277578314230150041129278793586315748881510113272583096733556513373523207978534603403670731996254473 +8688617881383905361473235374450468814839809922748795968256897565635364988983320676159901781893744135189821124712699202769736139 +4654535121453878305432208653573696687720659331945282280482164376466643366264403854440410004602377849435849425047260830549050573 +5901354141130553186089991532215523080159611047438619487077531580624462005235622602720033994742060589665151566143947383285531360 +4404189049080879563477017517544794830939312853525609682809863259313640891722874703005379977055110211171023836236430564193209703 +7229494566794585131912570254848123871703487763830158261455284479338860024826943524410960337002804517176829321597236274083343832 +3895863444104818475102160062282466733057017625695711453552783055209411033854956586687106898952724582542873871156820426292736090 +8900114835631317986947314833806014993742344316565623294563018259706162592235301529753004421871816938486470617127313453996556895 +0571759514976034692556387423658377978720336611483511537576020419517170544073326612329641870061597096528288442406290336718658618 +5351965547349291678130305501394296379111242594063885471825143788366715324130022316640730177505334429253870301505302005277723169 +3916717817342560503418340716556896773392571317597234277462755462162266315055085507027881148045053452399408688937434362014420860 +1395787899840181514485743703752177228078579014380638280483962491760489212783979634460850686798659390084425828810746413759168217 +2135670387347630269927889910568863392759999570308499518950101510541542088393000686133918165399672601047703855590903413630319231 +5402756885639965603367270976348755948712799599490635951531639604227412641095043036804481527401401591963544493042050777198305908 +8529853370862945970036024025436607369800687358272778049961830381455619066198750162208786259673987188349784621840620855238037708 +0287160171661419657676846666977667625410411272514035315874978763125830094141664498713238305106795904657260444752711842879767089 +2429466318667908141926838367968922957966923427083942277132655834855250930739506120606993470384477050842837546690187842884625651 +0859078343456062742869073421013869747003592646292364644445549193378529859368317830004107789688729965524376290149979489394466571 +8035239664854439337661491633430612363668270550373199092404545748888325504908156365529288450637845828980854077097067322982777549 +3141023341289995552305686646515529412648513395018325323202158482204644747543553961230472458295893311161675174437937559470576749 +0847147233703839410907356117466248368234947493218430919253855840473886381053247491509943700747999091387943188137392889875056078 +9027561924408681103294230082697546064172946790782404069187548612190839473120786175725940158419889526634026805571973842865999326 +0264435567708660940819192568711718952454625290701063992984178907184987617554072919582117061061638475508286749372288750484810591 +3758876219081855315432362136232140221117526750802434000370402808679995451358803624973049460963970344708478212627401394392324314 +0737438132749076462394577606428117744360361527711020670066707010174337101948368515240770186600234895342764130573542724774509423 +9073624026312594849755836859839861773390790206872160912057185754009883091290659157623485315161215679742322663821620022079418138 +9512292856680689427009039672717710424584681275009917377139164236516131951125126982668253717705356666975977684408510117347264524 +1614244135791303269397434529358056292497647554306953362312956458703923199969180084694524758403500654442416064508250070732846804 +2519174085201627296034372830011366838603028175377849633378995386798621551863828217907888881997187873208215147973680227021049086 +1003834843003895903348617625671797354229539053440753237068065646834526921585552402882071062806329627104734866652786788079565348 +9629790405359972301542148043545627855421437234097522244411526361848299030888697549739412058687417924390455152178947938956747613 +1220334555078453153775416863741025226449115088431256895031662080047877074330240958104951191074477627437218425259277775462127879 +5330046177874455391020429152325102772616525806909792370732670726443060748075990477694277944732782581654693994482617923050984272 +3673634429035216620140695806733909073383802026068407079504394523147664262305135669486209989893714484828370038683949845972371071 +3708115456116104837959577481370904415573625213784335620684326306607428751455492651914598239687297810388689807875911114963544682 +6275078866641136241403805509431449819259498303907303571719921144438991991012377399643498948247855847742996423485083104296357966 +4819771171509939007698439213480827086866218725106376148904562704537146213203839362150271333692094168029380425896012121918182027 +2363813324380838896838697582331955348538206296702138518105826516120031079298034769024454364560040544650658193748994518844922761 +3584004325313624962617797426518359904557846265454464840046448902633251444504148633006804302389871871448461320891471777760471406 +7015140209188781023966520963877943044151556490498938297548114159848752913311985584190436463500622710986698476383835553548754022 +1839528400463687701674829536257273749291910270980698644016289075485436646251865330303505791679775487148849253452040305315948402 +3145765566550173415234672570838174821210069239627979798684879235063388281638855085067234894023172727303768990495108093511731814 +1483336663514861984292197260930161194717688304395281475857151745194230301582456092278030099203677396095385627051965960418965869 +2854504524032911241727059077016541586716790096641727786446371669834231656716324311341476302384390592511402123780357675520817756 +1896892000036214095213174483595939108751482199845439848320000109938495152786801921343185996467957081886861283697079441114823751 +1013835470696140967938560803552257866025556771879993212750495494783316985507407134125075415195171213975980403552113090097922014 +7515529295471634503543767654884408021479006972212507337901526368761975637401627838535557010674676502841359449244736451534979277 +6386148785910089568558317754358991974549956833481352102962872492406026696207774586778061183616357702706148878186924821100084521 +0786007793408177523760641643227040330556706874975301831960943949711205745890553620154847773954039608277686547389729917607939287 +6601065186724585869718286730900507361468222773067014813540248318802263981782312642062065066278538284353552795463948608367617264 +8710458632884006689061619016115234211820116439477889276589203755014121466655765492211482958675286327074722685854832241409593748 +9721061962175462432000129864627652688757814948302475172688435098625740068901878583888711851475071264973226898929358873311316683 +4174366427344989068552152924708427538956427143878190827546384412926545762544478791156448578168011030464548344644597978114749995 +7360922452061081711320164728509310349083463549987726136205083770590452623618881507969241810844776231293351721107321616807591790 +2550845118266461695500217220306235111000226441822451555820123914245682646337932826709632824811050296875427929721177340139400066 +0537242786569134793043366211458572528023713869510543400208763370329672141795438795279424345734151680580842314157723144187850297 +3691971167907808375655388036762185591921761050859435693988855587355710680373549562866421201047569372572737260475884900803372055 +1007691946206878968978529598838997374929069152901229662680626003917658157134099448777326028788327501120348629153653344792265032 +6789665338488546383152814125714262232923477122140534949024472459646083579525928155625268840134301323030935772657800235228310313 +4868298684968408834649240926318299142611007662477773878693177513443928825272512719998804017438032952107625783388477428012876908 +4309202213419885445857524725380221565049288409243087159437144931986513177158450201360308556789291053726105045930369784581421376 +4918602058560708246713982614793257938660065153424340512931090185661823200932451770267967499779255156975310512692435013776152000 +1110183674703280644653992907629380068789930715341891132020648458097673681456412608084277373854376969813158591776142390000280900 +0445144418946628205657456424237128264942955311965086380583598212294732021966933151948426543613378750502788733253601313302851775 +2374366628960975291734051077636704217339955444337088817734748627762386830985963672836557693273405067738105684469127912825361938 +2027506449534260776940737096467772483868296281393345078755987162674894542324936567113913899614190563606092238595769318736510014 +4601609459626193748838141934701193459914056226332390088463312165368986927729066528661188656389710831973546586554245180984726395 +8820641417137341791284188339243013012221967809583302454529489453970560570507033795822714618813232104921926392983794422966946716 +0917599289793082450351085959141123440985146982836878988204562375158569611940037966919396385952791842444219407364739497415459949 +9477416265026706394163032606799526520617258416558453349947010427060666346412817629143427886670549081801335758913070396959720115 +0666704035859146354165211453275694040361967505403921624893044875089069215309652369723945012113209887458307287755360555234673115 +0255440161315757825615697585801039525106439960924827488627325282298838888854341592966844608858745754258454784788330547612148950 +0874980077738428750262349348089941932765130462263947980854368723919615602845763694707934084120416138718231554631967803081258409 +8981164868907222146030105839525610946849494940635627162577865021292617220051659027789888586435753051650333546661672096178778980 +2620946901150871449077949420947077759285135192780733116251997439655441528374111538899479815498280668657556929167956071921060805 +9030260608320606358567118378046750979991657572292125217943471649264668431650823749458443029769965695844134676138947512285494656 +7911546964476705302214543959264572361084543166660831362636246720337948370592526588629297565497381903059061404007654352003582699 +8922075574627020723499343655693848239182605712427393497956424335482790840429881008002039906918138130808630673506435738463196394 +1030007563952008786387120762737476786971644512140825923263748032598127575949995761019498818566016398257609354069772389656925099 +4044422726596923902940860834745051450250943536191821985629944194928632102239101274072099618807513448554296724965250996986364924 +2354577685813636309146827729776859816069210424412883327405297035176569137490665953223528983078628177275666747947529655966331575 +5477299131598928984485290428357889109417137134680518227595002407302611193611761790018230013945563583265000510580955975339191446 +1619521324601562573756521193045609645344126519940152032470378539517720502293336186421186366939997498341179560168270447355950110 +4230306307869293075421899272149696843537151044956328729562855283491975528896006550623263787552206307775364239158496535813468780 +1611641802461249957884380478543784809385839965522996094364642447223105495756896318541934272292804367584843894271046287233761298 +1709756805199283313621618096481348781674012787506990084789188511001769795898381723775624988249883268986469361054705514036409475 +4042054305280705588082843156563390558519428096353822906407664058875561059644605482196547535261726903242582076817607377960964154 +4253860999746516433043460107715275174182573491654972680242324213294530205657678192111601960601634649905585928529798955700154076 +6676179068384501343541631038132044675296273720164020679619589492139981245074167783663095317254579357724910508792547067262588017 +6293884180622784793929874980896521285454501177558249542530377203281315652064795385736617567836645686619617679168909645709168097 +7743521550856066318163410468235782415369091035062393314878499152138458241235674642599783367386177248502506902767835896501146443 +0483794093371674252980588779386498050198274197464907995529503130294921702208337296889908183028360175355797035085155032755797134 +7360019231801705667141478490298073274830800662389220682649304818227754575701335947775670426238672520023046096705399683924718657 +1560373958818879850684156471739380073353650802319300382801718291634025935869130174150899754981641558303334317282125650661935271 +5092493350585336373714086054348812631829949972662091014907535609939068169965945723736778889287001972766703372105697934518100296 +4734257102525392916687039288087104774233931626636454683584533774360103845888157407165794111547584642769928525653912147278806542 +8822290823497972283964354496248019949369662678090787983018405519062148805449317563743875081526769207312318307346979280704726453 +8280283626279949899078861531461602097441638970306234787225062938541682486544091938804453769368426982357391305981842832099966845 +2217356720554468128605404764499852161966456502338368106620715847133295711653095826628517163144372167241765382757784888551945323 +3293984824292108860635484259719926152739335450946748798660592468126259449417195090268999500256639254414418461654750409028017288 +1675306289991237206693384004427250238261655808453706438418460194044272886240166511333039026240609926117492662156547022216935277 +3798683710262055280473341604637975853707715733359848491914615475081846941708688479067522988710484836724298291195075742855018190 +4581874216800089134728449476207100847781237572690983431502108191722970308605570690039979318708286697553630513952137757229594209 +9413803628857147469961583711007745023208883952507321209502032469098964348049914806057645578293300641835023243516073565578336233 +3421265017714445796604621284137835957308268098924387669789495349357687107880833828961898545436375036341306978848338980923182467 +9343222636188028392447876071807038373006670961664292112334770077234449906744690005047749541517081971030139237186089097495754677 +3197112368581263984569948101844452284212008325999133835851189833940770803601768180655910037983634430639371254776873860967944989 +7762428156063656052457140048976461668407455298961839410706956245596539166362271685772723545123147978715000469199290892122691240 +4878440976953031234401260269958754462435834653516282136155912652234992770322924693740152133726104201502258366755195941397981349 +5410566039278422394352444668313290032035427902945347706947962717669034134093986739640503933965401015646443482046077453432046184 +4739388878693555231586220110918786353416678121114400297535390976972658914130824675010539898056712966138526147925359029678913230 +5886358316255011245098615464017615182438190446564591019394412368509357681569397482568515821423340665852760995420364289038278484 +6311541503127681976206070117236931568310981280275285971795790489631417369196648946460067342315632515195328396495936094906795027 +8224055001487992245066819015245704964683485501844381492046566836373072408682020849321689488337660828971885175876650163928646965 +0254578793821590283140178222280457619069765363934967602749676534057099985483967828656474230988014165618526645571684624945648693 +0109665916508637725280874977575865530242997110923401414431409335639376452646452851515568924163798404754643876369338378812431753 +1218315801760060263870530314784967603071899867258461326183988209146000243519324558810891158838383484826158948667409186010480817 +7903203377696988414945216232421892536173370183150633896237853253925354364816894136252089105545529048726749085139508534886223860 +8148078797101770523716078377874332667044042558325146665083596936734559299430024125955358519934955785765198216278595449870598677 +1116539363983086722722201448783346777178486858969772190694450961667734803002682120934093453206868817575132463247817949468716367 +1637275206589839665316460804048412011573846372603155744795091025936193454883889805228527990919468358500076576823231506244498699 +0027621847956067398935694606560509614067216464874695482321462823676120479915649013012054108931749591716879497574932046754706928 +4550965358813639986179681154209780873107404530149950214180601478806568724333935291072801881538682786052141302073521529721130768 +1636835808005913985865949033373364692120504151732972595493618693947796782831116462924175961942781719381911750675622713009187832 +8144555119970713260660826605512619993205968791014438812251863090080817574324870955751027471809521051989383021792494139844493682 +1585931821612292754292414253292528827165768965683611240737666722015320896218326640019860760530517054982970340862863680710846055 +6536681595996347954437568694119184759342070677456835801973881107420392523103849305532249049602562479138323295711575175876515640 +3486443061013741439479986438866229511864021882422370923187276377158037668029544320414759066219016776345013936686773434186071692 +8652668214037751054395752355562459984977103091936105931037362668574357365575540736744748684242434859190554622399068882473374610 +8580093375714553964201167764812992676992679093090175449230495342109288752328986692009639151993677096258635370756684860954291469 +9964866699458956436463452444822527184213143978744043079921280767307287341419726065990023057954791661193301961189038570454182768 +5099791381798674690627429376704901489902412978407791536768610182213492340739402802196624950327768967894658802241237435134810216 +2644824029820305111520479567245295948221720974051912855629977767969256584700072624737697630748957013069006968156707232150642177 +4694014156265815259320275858444952794733981468869946419687206201356067556770447737925027827372686022309398793525531551762030508 +0692322520193661664579545021783669513925271024381519716157761278129858733071273401229450196329877841294994307214799190419756421 +2529925924690763185785959066401191385349387731434107358942442763904987402826148766868356311114428295259838898928666272489020921 +2079494677073945124622995106365273958525412410850501502163733379133554803716983972906533166904636077433873959124786425510691812 +5600448283577862312179623155268824754692832396408303848264207707120544301021495705368104362648456117090809527815889327217546635 +2343977389207774815138566905589371213682572216665369482458860895527700507201800331270809726620829481769123927780354816652809008 +3857604203317914309286408408516621032904248756858927009945259813983765983907916658284682954733130678932128039594166445571290526 +7783506576165023166951251072092917407173251744876214934915430447355544209919802690055207660666887916247859978935839016078615261 +7480703460843227445195199879222716960672528809567298037159358077924188280933697924269515298429095424116709060884750185401083226 +8717064476124069485152260308142874514219920880901182710267169211713742088964956637523570820108183570127357202528608391772392065 +0183762641286324428853776063863402233453633712505217167155150999046272831538702513532943824820006210590058738974696140094824531 +9112496681140545311761452054846654072950289614507391281868179377997604366151409215985425553727631766796511079991043874131555557 +4848466672408480575274846878733482332783913190570793476048801496992752228173129813319287139570412963303092066006377899503587208 +2004264846825219154033402067536124920004600149381068226608766267724446119288301005499297409144197426544971420258082942394022997 +0140320078386067723015618497818762945156815014583969278200840575341177014900417018827299383394638626203285185921061940008032865 +5962478195492396384491797383201234602669716423445444477699550016949258636112731978273213688262995924599283114955724957765374843 +8311990015515249068920407871121923281094596878515199867380540740586492915851905851867276732161963585489156521545932776243150499 +4592710682075197276504754152068206668838563346968654451648904363538719952128821973082368544846718432978965867108316589932428817 +3552163482893373255049569753046229304178905562057146740570805257427023711941189943044300807285165026750415508222049580394911287 +4596021173135544782173915901528349195173147664807647330846216356295629295015969380040305219923049284417801673254103299322068397 +3943524404368165203936926893260166313850279802117822440456770151714171409778904837544726372346574716859663114202887576413543801 +2969116285326615235890680734637608834483184611588075027596448992877129841445196831709848480423954210927050978003235143506132046 +0153071245386120635128957595884412738484038627307517440237760302728984006580096280602129668935036883924996553872412205753013658 +5621357475787817996023091217050008449865559719579810787563038126520104376758261418798236153692393962370203265309058313384060120 +5031974571476436448786217419749048851015183003367752606459920605017140928435866977820091890996541266633748940714886983142086133 +4562696779266374687250665129144508120768763103068669726719903362264420017240549417302081941962780048493798236161080017811934325 +4744441027688831600256636793504579543928756869184371127623452884135869811364793667736493944686083992892480103958881983194171487 +3993112528998853349337977120649249691278774025202103634228484270608254195748299845801769708524194520077695532568068092195962254 +2329576931503118880223192095103946522402558163495818627264589763485690044385772668639064805859345298816707780560479994433943363 +3218849814933855133187785331003935806771232881004238706522431511966088657254108552621744793383328388740747036182217129765309790 +7924052825089429554499360417535494167874442982520963688982831955472793462543386236218475310833559387701937594241052466519691317 +3120730387098471204669336934308519377296835486747153400369971745719675185172869103202802955453713945706425356726081359191431362 +5436861304614177525957519069526164638577548380287496823470807113570761415650401959363470580496567502232543408020661206474053266 +3120249944908567907826123413136257067078494146549111473184473022022655595636232845880738387277254397911665634549126629364411401 +4035843002062617386757564723259476788690970258752656414372739557004284974159497458955655696811845766981760932359601002427208626 +0779143969819344348002577455948300927630939121921547040685880544649850641595729872650264658050333293212488609440422250520307515 +1460705203029148369614787086632962281691805758071437499284040866802949156450922963941143029043912074471289441815938231327288667 +1549240739672667607945860219999638499322190035348564953406860715951588472937396572245559080251322668950418605324345241657646723 +6681600989234088565629106267156100053645378516406056877177467623317919074902879430656553383602319342476881970725454244483233156 +0192568263169645194866570376494013537343370202553601755904593727729559768993904298684689230055250228064310566719259142616384584 +2226698070135175270997371657677443976168753242911675302780266617573092075405498205119010526964247236701457645067756244640039560 +2411523124866541301070099168413071292074360672744530325093983003307829914552999628543032410966968534244029432252498124255869721 +2032148958117324274615176859919802414958436537913396718375158773684001583339324493639015775574550405787223527127839505198335838 +7873829779466960857655228261450384972633464601331098096364008012962199690499598210503782863578580915221775299630382906076523471 +1033617798236208613493443460691499481184470043787180389655899698166528830636916915480716417683921961582857940369278335021557721 +3570877773431086041166388678403654768190776468384892784896107473099172280071916265352211077124176187206981633533447143541466428 +9162337956051462461244081308639454192982414597589520706270659960902459420484545944118766172576927213645046647438580806130370976 +1679386001024741318115842266812215689927333532837813069949968390429571894735964678235628716093113797167330012932695772923859921 +8804168783787089493714870963570663419696958001331927763315516123670935571014222651056237882407840332672169785104960709919825245 +0497455776366854139775882322268590872937081090128717394425875756408043314452495438785765312873239203328867278937236653265323056 +6042409906538241575209979144212026252259767618647784026916367882392586982801406891358315499203056053321982052638011533498889270 +5665573721287635790869699668106163302621832934471963587290594671222971786422361537494624465628017639194877871599798228379718225 +7432584741479817366719910925645646541846360875945358928938495570674893693392242341873215005147647843928108951666723535877703443 +9626429137239096178611195842928172193314671253797016055524388530347610194243157336306488458466944807366840872517364192307199510 +8872554743012955139567705170988677634651238832225827058388116110755496007697973600003490415480515644505946637930188314840631970 +3992491991103767989877164315291928453885661238942961179632807297112992978760631966972677658662459086278724019257889314597469958 +6669007755891601840756776309575869340114031142955328946684465711503263349456097268207290469828978418533272473362080110971119500 +8033630970428066315841260643530102154382783569132687157611513276699429626479061706898110708339849619210276811595853043955446869 +9768070537230217029438684537825935225327148348372819431279340284261370618005491965480298943126300044774337991256135794900355667 +3657498279192720768668603975934793465689868735015232170607621866439453389115795388712792645934981304662545764256708338815562273 +4841652997648921227154354900004136760485517640549167386012657306424618486993618255961405066483542868908453734101505226047480448 +4390654153631921147807124887078770782524389315313338172327932505555766712042126014521672948387933863420403298182271156860089870 +3083699675349781049946715849913203260143681249018875435801318149856990157414011015651215483550403032096067120964528627647289771 +8136660832504574374548878451558509361941048418373428464399511971079202695299106137651197289051575362091446283996841312766684810 +3676236989934685487277460614965631287879882167402767160319121856718994264197563392677953816932994293095294646864998371569945650 +3049112960977618674809680978004704430920799039334511606915199457270782728770389854739859266964607398443304578468934206463864162 +4020319901840360451062762075624924661391286802372025413278593393390727656336661248466535685907382226627976102440776444559861524 +0166268182450996578449973258577137477283861283683233635300267518223499975003192269667287508860009509128730943175015750710725727 +1027094337033962463834235326303117245639527702316027545974140882034976571260270032549979726479017186330571896766878727628563084 +3735720519280562035416027535373646170800608954405500868661244053278717672230128154298018261865339340499855937060742071802179228 +4620909442780453001381207438741771581539126274686674694429903202199824281873555557550181182902700089108021432193493732079872184 +9458342180011256500005493355500078007350011604607303307732963471459637499551951642174991940916434214889720629725398539493831562 +1757418742866953045608344774690257090125548479597625280633090790836185148048976878560947512134108931123436802430288121604450838 +3439908217353866705662826605127180864607977834354266960510252404933627640585873170058624018410480482791416937234522273135497031 +2172788248827330880258621512418568877495834091216163584166603056260778255791670347944634914745463656259805211952460022859276063 +7062821874356395565274839950114991325558841496177484931811683058289009845398038339874364587482418953564120109998982642304471400 +3698346712015383293895452263862425925615594845734737899372976339855075694671913339207356770288208509103055437248670557197975416 +5731723926926739876475885511154531945948200793682997616742493659519964588297814245753251250515716831733713917292045440536017047 +2326213034490494256432217527368652272393723129996987975280491971552239548286262875444742346718137588580994530639159848308302488 +9664053981063208567695237243202681506536792559295775868533828506782791916794818915656227027388965683683700700077078525885858163 +5667625010472277345606381058073451841188387271842197689932469281333817846476853011214737420944488058943262260113307982047560323 +0376867635943376210756229949512175221086640268169558374511241311647850185329038766762763270981601390818540680562189733654333764 +4491518223112590691196826611428935302844272864063278173063473168207705976253698032183297470010465109985370388667131449602003018 +8802699827748011182168113988637297474486522040228085650948530427329245143454181410142916723537434758973564869412497956238710891 +0653015424586307910784932154517633495234468902775642895621562573878654050393509111804824221865060266482583193591594511748836623 +4094703884414114132180304529532880233548615595688135936066808466092184620343793991343848249436804401054149903884653644876865628 +2718370374189505315521628763517179672356494682994180239553818915110898807872150301279550476267769560111595428023012737770277809 +0983429088572418894311039932871928790123994971756838750348015397853262244219489154464202472581023076365814977799205726076421849 +2244581657719291754700709306659618001196719303854922152332583798578459941542447742263613733064435246505660395020395510382351759 +2371191845917148718985406337649548363315059193935226677398831831786300690640380203874437098924585872141781137278459673035181084 +6868095538827304671263059227883616117642085424440799227282508929248920781388666797060697845611588551792908329555898460456707821 +1192043306305230972108032344493739345654505538181410336224627870081996078504793311999388842368948858806146981869977786056189228 +1263564157432629856155576816827336520937607635620996781728277895852539790604285849253279394716497232033008275892868545867382681 +2337190119883945387644191032300914026172189976929618448527119917727894462635110227775485639758673866947701176216601761844984677 +1541725580846818676211322948610609824222332361334426723507920844056410476870968277509960759021044456134551342168799278225221031 +5808026746515835625867232790523509089624770846326913802756163972740615072842822135746471264238978131846218179577994033108366459 +7209194492748333961784669338759539252535168061991620450390411453076684475148292681430958283119646863377618784660018401847206388 +8164592842234101218973444204368622348477154717654721601252474279965258123105673927507866232148031414671644959483263113289777725 +9536506697075295060127905565119738004002490249111193299408907722018709064690369235720344207608369516256550493184611556526526774 +4560017960261564009218990204461419858845480963206874732488142599183336196461705771053123771553268404282868440226650446328616213 +0784979620602852570679351917990832385632423349482355239911158425540111723754138128518596135522138872178079847769857439148437200 +3065506422221326046922349934844525141835749289303921189806839691414799095653902814723672937020068076690809012734952484971091424 +7531639556490305149862508189711238391327019426642515502558468392326082589336113833462268524671249515158066960751008937821736410 +6252414527138677204351141055246475557495876614593229376289610072418141564580640343324426668169327302426458833085549654617933468 +8091658367794443766667336494599463493723421496560610375457662742614878721712215614199388402972271203457264117746009414601292472 +4908066830822438120188614160836549308604180994779262747990121233555704249867768081188552478880193430879502515548138613493765732 +5079293880539371206967935767115987717076965982796395512686484284292191272158775216705810765026584234552773076439527278292069704 +5164216628992440501162199298685532131413675395537593848742057924552014924010292705113677909421661221880801679108009584398377793 +4121418969859257717214261444608216079650826208617995799712962734646420115576080668425510442736110574190180643945326397724337600 +9884581544962427545759780318699856274991305713483502351242287264905051143111943618139482715140793163760250713362251924403750737 +5001181429052612807248790060512136898354370778167327719757330132811615985502470639866207141577372460429670627788129417134404406 +6178770653157619944192283152223652702640541977712224400210339303510432403323032560310805508239002669348396267104479169736023331 +1565506146888751623012576320991779119855864177643274705087893960131150916233068282254723459661688282729513190349028463435804527 +6845488200397241399013882436401994250911820745822241059508913459645459567274421896167243282273985655453126249904632199335837792 +6040683797579452107027216845343063524780614029426886935760791257030864245182939630838108471908303610357161096840181118609152193 +5867358422344899058837359139467062379239965703229659786120062948805736192330007221916713426793575162507137089954539396331620247 +9120519760679588392286105798236021559061854045122019266108300186277736886734969411204978788065566311792204801446141473355188648 +9729762487738362702158800023376627553944314549674371461896299198497056817939283617350417487984379973531521250384859390378780952 +5278728682099899168824382639886768910340224230494671557124611921288793461227571446099109231672351187529971910075105296077357641 +1730149007222759960804235202582288759192771252526372328644840575023003569331571124411309306137954892577214909905859437364845514 +6359722384817229880817656694157685568966235098606153131789136445509767178564237103959982917552413501317717319150118113725281168 +8331881891680259180368288807257087972744816443005083134263401722243486734946708091896751081947017218318643507133088446490502898 +1564672123742228001093857696640500357335628593848961899541997547484403147074041514838330205353223636545525633021798226061157975 +7577934224848992516208537526305362086756191009198436224096722971843979917310448903629548517759776409320845391838217575441202030 +5842023675100545856824292093806577194777854067803726567456782139496624136800515010149246416535077787552986449619329516458032493 +0373167808153255052885634740314278397690578953752487856466779556596486994777425942109945709709335720717497273570846620327341193 +5952530704523306889233492671665454303269886039975987299656234612350738474857467451882170147036566336312226535534145167519693735 +9607859559588540690534618223279700864577721437927651039992456828233467681820892987538385235563873550538138831743319323285816092 +1624406727909111147099641441681559414299104318980440722709754783222151032344202663364762643657625463452804121615256537142718557 +4340315496406729909696728430969897055002939519672286841554008064322610667892226345008617230358420658804502966997298469146129669 +0210625023915241926959138868143886129423564883883556915150345946486026490910420790564346970440390087533686122782420012907572301 +4721491005031549006749404748103949153294842924575427991607317267682247725581223721840333859898813958860082395329629494670433269 +8089333321693829301610669436388430107322213493559809633043551047784093889175488034859221042528763690657356439399119706918708269 +0081737607011167672164816311361636636595718748621290008823635691873373600228901339494989909273621908199424676593460415730887285 +4160520455923507310701763781457568843470746581847988497318956296776248312754405407533119008905656107514741346097573287623338123 +5386718227154603611476662828982111704301478482540378612558078862521675877095588029121004214011721936617184276464093409939658461 +1979208081789258362592059412017416272899419903367802069424612928998858612300695288310086571610845555893261171864325093713786916 +7319139333841813731995304531194319105663469219744020376897814143833856662869242932535917521586466923358318419175396557400347006 +5043921771833079685531325825310432807800223715751775658072562091646020835717475241682115604301178255769370561929846716138929898 +5795009540103191080406839616053879828656176434583781287476876321448232920090228345539065457933826027880057345811966575002039185 +8576153060956850521311077608763009793771506275720974549550420722202837108735329754654396372138014747847396055096136010954168085 +0509043015950572569270166347532451603412568949379675617083809175396443490772874366901432037667793377607725671409709046037414839 +5757807520296047868933446233650750653295440658940733452331643306076758589809542688132352958487696630197185077693771148210415488 +3493366273573202706880963127674882710949035853748406509260478202136097703296547394474710548706092731416204930859887260446245253 +1707539941222225775800658254639655291940922607284340086831234164821348634212665200587884346716985605251686416602230060503804746 +7119373075229566064030462457605177717171013246215489918839015056056006164706313041702800263980277171617607329962257981016326162 +6663813052725497188713444974040320318581428866821251405183260930030389032259911879414867761739508256774997369921307923003380224 +8543233997255243697045277479565692227756699494957952164631865806868110331658332715068585534381871375140656787606232565908699795 +1127161017080656837915924804407883971278108801828959672411183897706979458144168384325785293840525608900932906069101189576035960 +7746884054099676741518894156142307630763897600710799726835033615686726465390861737711082117067758069961050536090277623500309130 +5637631551447663652036022243883777403508274485804348060822361386569981574710776219228289288499523332852532323527986372935987772 +4799128874055082677319044677884361786887526377254867769810507962743180135340211433750487850964619355570684078365963703331785531 +0073682589254914802156389462761084682583678912826358978335933715271645287806405047356166648799977142475160038979372628423949010 +5037559719886724670132444119093240447382541873564218599313650731701908225623880448841585926824993839038111972019595929993742491 +3846935441857871521862191302228658206156991884533871767438656661539104499878809823997230034119838267962065754111334791148146756 +1506245754838722163589450414720903186576980445131544657545019758753225527419416112605350003805887953770147934624442363883801265 +7711965724693431682894274811945992120534708309601562503539815868676726704850890815034768784240539682384600476706665486329906626 +5956050270087533659778978762952993742209890928275529257391035690977242967921950083594415755279086778292240063613624273000623384 +5061387982251874414971585573609892535056862802605728035065909921406395624271235709374930413322182966464210152496167481038893673 +7793264955081367428964822852138933334974506780923885826935088471472828950440677885279984646325294353993045156334916727728658382 +1917731844388737433964393775321834810791126648863902100900130682254900663770885502300591056044829585219310561565900983432788348 +3993621103753371559559083065225969001381490024230870158740460349214225265354617541796726739208926003200650331011617584531364466 +6446975200293349558151403162258385505474942322796455463090381985898884153879810666921247678344495019592171989327800934238087607 +1116773012813841761179870425820266292137052144431441290909627977940969824030291459806507439794936788928214828707352139273391574 +6029232735235425403035383736905446200540026716844974571516139411975848154720481565143793111521425975198184192780712740114138327 +3438200310045736349450654856319455355471806503450945057852390828051199118282010495739738213447341204668213941183921897766832892 +9512957847613586351781261772535091586107882959218997758350989715296118346024725200133884012765597340862400507624462524830866480 +9478843365776113648646455826476363854336443473601804016310466451767185956320296981753166811182992550179769973329037834636002638 +1745421142947154461000688129964181745046963595109768527672990383210124171587972151958957748633651595231193536560491908213623383 +8381825139460307549572877031135183947681591383140425137518397676071788098018785187454915609863724531998066262732015118963304139 +9818630925621801216199389879103728134389152002296761687336537102178195168590959317811012194485775439363610537061412149495648268 +5576817033233193054333141148999572966012541792116109714940451926593836005748304065707002129553500879663495665687664101078961830 +4255249811578737819490016920102133439336759727576849642889727534022902117660385461887412305323198051682727105599743797538768483 +5664318936157823403995526721241574938566862585452228404066914834889748254113822291330168260243598542768358287033612921584775608 +0493408359374626915181620402845179233550927620380852963377274566085358451742815682206366209318504599243115355217504587070160767 +5099926458337087971802835525020183140934847289656680603935783200084783016975906879446974951582977123093907468193624294378414602 +4993211036037539293757401608261712055642333911428904305517543773653002323722539705273891988338327482438288308063770111207012273 +8875724448927612453412899117913683062983270974383158999932195926949387111141855398745328636343443301765332909049991525667183004 +6179446485196106673286301271613059516541037127280321853066460858041150601008945716021915010720764523897185471929927560029444239 +7849437984569538042541347312104898444874295714765888754190517892771505925662567504469022216257045253341087785885957187833718309 +9972288762936380366686950714951282499790474803745721728891585856096753079311064364081796195123806475478622506713826868777638697 +5194143783169339034131894869316961917794611019135131983101366014715857076410377619181633332553598229324447853163042312520962414 +7850854551539305001687198329060780010579189214774936469033426991594801002823836027328068965959941131816419243666581830678041716 +2062179831972241107526467338284038440066567441767013516710832064119073030278681905009991848241324570216334414105094116172288746 +4795251857046503954973485018229646234695366724410250031504983138843493652944866619750102836093665932368212191653037534272918686 +1825362974498797050137125577095725107017850998954189361286965048355333973664280494182300562942448127524039989758648090694962142 +7512994410145549835156679857014752505125401506747737470958447208937721056830032278112608932324610937928836512547690813007773206 +4386958625254464154906642431853347789932184826155599439488217117827733831342079710541861559451261895702289359997911170346503767 +9160582845194443225222601665399912946931088757458941216770145834719498890068834370787508728005299127862046720974369218083539582 +4881031377785621947067985825999914746359269429111016243690638440462655472434228579718379181809439967478754245522265321625889814 +7583391301685587265357562950095622866121830355803288828241373074939869852539741493888550714334780322503664271906152272539287229 +3706056275918240737054417094100085374094932331005897520832184041773254725650156202489186855782648043533132404772816727073155235 +8593562217884076811451277453364735377701544569033315203869164836320799479709306444027657854268527295924524538225997862678112859 +4570537859634341717395527618756161556420569519217630387646444203036568601265546935410843254883517475649241288633895824882290087 +1279603976070610116464640212199310166444520867245963242449012132718994226744403248200981139871780434324112903659101791778621066 +8744850384566117283317344846157583437411072049976031321951124766354609384768681179440522110424899002915456603055956128713981139 +2086242661312485180876173221212851654741063402662853918748324706267356051777749583559232529239969008936507197332763290031477120 +5741215520375853751719868036345389495144698592395029595732821924732143612679143914116460149908254801423378398341400612840255578 +3112678009802694849286559093313827681147182296039209347542924704088172807791962585025279313952354773038812296501897230865202710 +2464335074070148432312036647984428094369519448454963043288725321796755075626414550577697084871002645789785381682654641241100283 +4946700365473689782081952042866282556327651210752194953923594323433725250140138447685850292340132652464859222629234697220377811 +4116813834263078090553178959891950239969717708797371397856902146227502290357214553371450093318491311749632101080525636146301695 +2152016442288947698443136330933042135184223974200115803798042702098262975256411368585240157169341361695851434355296732786801088 +9469700239580500443632308541309983452163906017765379912144038775472527920834490565234287360219977902823838503195349727039831496 +2028576502159755428471922748239092215954614499408923132851493814161260002491760119779606365853853991681827617094579573055586297 +3640678920108595929213187711839271941417061119448282180155978731064884982006841462863880420761509974612500984423805360525276222 +9057118337335912039094779552592972943649024122835147704439083152493402341167911887606112405528052285968546122798145084807566159 +4304613049097528789015322647077710992450848025235239429386820053371823148160964657760192249248892087179211556970604610852657330 +1245963067779593226119382101475675796217312455063935339967592594066075071514347181089457321147516518204553844378465193273590500 +3560863140415397624008978961463819965678362699058645101497391867248986869781266285523093686811424022579360460735505488281520907 +2803810314481228865478572362393166113955034570401397318410902932552817985751479490450351950758398971928389088057335245044158094 +6904347745025056517093981246275608711548205826384671869194152526627350920208727208140128575295910227390194690418706771705356517 +6784445719916791606648643520294851397681933118673503958741728387886357271190905065762821681465016058740202568162846967243295973 +1820495748778970925066926664077661706850366307799032827973753165199895444172414611070770446635902894354703969177838887030507999 +0792366480532040444153327711554056488308633552915589047332411364711058132183546270866868018652349880999177379420796287072716088 +2231884404962985945388134094475613555762253572694848559208422137166239633417739879274457716224035685193748560132590653034477542 +9227302311116220471908055555125965124068362598930072116668063472712158786276348904469695948132587468164071579543544049342283942 +9611661683770276849635190630772488322125598419492636769106569145188958898006930644639421498885045868655337164693651551682789719 +8113170194159795409724804157277970216585110875118496676005194638884485314135820103479833056690555003499859437927213123666961409 +6776248629614262117061005539488708741664752240716973399951870993215840911122617246729216135676971155075389185544568368200715006 +8136621653681348621828391620654330376833126244814473075974634681828022721817501734663865372891778488609175456672709780494092424 +7509035477896901683836806362847630551058479464889625032860924968434340014421918549778761662492676219077042005549667470710873708 +2649270641258444022173705146602820338815565069036316996296828717613516424116147690610506944602495068800321856748967379303431711 +5231537094318273489411666006633698888790246462698166129109823556983405782712006150780252324827311704109848150151626541203089319 +5293239066624699492800648051071066181862007268310279648019183315312315154420858012591159812489763989269552995744386956174038764 +5180720261104219765835445462051189983679962609760132661239453035110340712611783326617205712026077627868902275865901764502746369 +4450386186789754245435591998462910276602998139642701382886884245787698610667584456364442583085157908011542453439257529663938908 +5226947020891494666123742174916211157087031239651884379910014927950876644487562604700114503798045681730239237952766587287565756 +3462320891328376981719619778327669975981463699579856289946252639770341221258397441894692753073255239973756891174514808164393544 +9266128730309970612623389424292152551017956433451773715730990286170012828117563840968654996015507568606746254308032751484010172 +9555122523637838569715536561794279389081447703619598960381475663155959964617699927258813445644496349054324074619586132556815203 +5221901221258703935673357600398356681976947446424967337708716326590353253394660639230448040100077013280765926948098493319213488 +7738313289310543436800301466124075949448435224583670124694327098481119456888840896351512543815188557251685118660772588456625224 +6115002819234894874533900222654850689485882307323315708374429887345170183105778321445910604513739852020875884337203588872923125 +4481043241227156956639553841037732426965917364470043436075190174090995043087977394594850901991029121373900633832712403510778230 +1122190232976475343331179295988355035324572960682589565916578492624347219825791452145522290660285362795514899273106419997278153 +1419535386080392454227188259532485710914908620105317213100011817889875764632911121751593887025644393163109071422951719422847110 +4026612711274029089236246041114948354793310083266181854689070097597525414068795108775947050469531123122537281180760454160254035 +2729212202223251132035733428110959851887971832113712091873626429082924950281013969697115103891338579818898846568239238607918122 +3944336153317752913161793886424178237035722936447760222478425805766217641600209813232221230340238786982394323446453582721990048 +7389193916919980057221104426235427350670234767999865432553198312811347361703697530913350075859737082487587469312736993623735804 +3544483515704686277538264527506961791275819582201516681389283444580314635355964056417380225809498367162186604237676145482322561 +8044600252747925830317760868421526571377975782889497428302869692743382713250361972424374300944696385157823203594995989063758674 +3762181316198228161833937930845505733610147834846550265056669736693282801787541130363631664735877621678425882651996823896571197 +4895759837384473598984792357851123553619562828621778670882704444499813426092540279529447104250292800981220546692084253328082420 +4664284555514464728238600094152484535233997991752935740228264651440639886242409459644164822259574135218995760166441434144147387 +3498021732532747332097779512063273078194846290742584872890698856916194513341236677884416300942002054144602101645686192153654576 +8855689629475186087358536061769600329981635356231999344927571715122048450556630571843667383074529954198709235538731487573715687 +0984316998018946399866843104618259659452795653432490384229487374720689309493558506727485555071518696965289338830868013877431679 +0444954460337393289812378506420855357215741077375742703669752012654590715324153275679920752247842054109194186158654335712157841 +2806861505564373354176276173563926841713871053400626160801979041772088503472612997224598155147542728678254353573523697935567654 +6201924274245126686318859488923564788418058568530778365444195042057133158737326264356888205129275667061366349202778458019010774 +2825177079603097543301363195546845866777243546459927001122927276132658961336248692066215766333309788747162494346240324193946246 +7328522607444197904276536531669234484641234327006749709725328245867037809150903161226855351918234564454937188245329779811527397 +1625377992337069927288579727970280388115743434164021261444485502332859966787759877144357204976585729746356637542057830900380879 +4756314028565636147037500545330319143265189233753100563686689461353510356901840785161069243491300815016067752040583292254841464 +0425017333645387838654294817531355494569985613245897735076448718279104158222119913920255342054852667882224523660815046839280651 +9062897569747184612738179958208393387812077029581124581459197624411283206076395360970022007778409057996215320186949888642491834 +2577588591174250273899408593063983438617592453792302359558473005206418002455294336176016723332413815891373160708758731967049062 +3790766334026767123109787766877375184100806711868340730414046922950551256206928850545228229670325982658231736019892656930961551 +5274543406306956723055597804421123472449821052046299950205504470231963135543548703265039753201973863356153745730092781664650899 +1020617263114480064430348590768589835423732129147820366131428529210137664442462332795234431300140813802687903923031421483099298 +3305606503022918455845722586154193433226668761115620627747430416848925633688922763380741392673785175993422375485098655310345526 +2853130953848078288453311632699255400113689129756388559283548184844635112023417531659479267970268571040812683475165664822794180 +2783222167931397034746003825257822695087707764404029816076797619920932960124572983887919479900468345097640119343412470086673322 +5063459185940735886394343828328918625032505938760162817991123725871771735016474675217009872180054875341819281047070252038306775 +0743585397605286027482664572990290216960312219499687628442325704541075906118067163819053997876546874075941147761887253181403133 +6865060056069818696891296915545023370983419980885799857058396262667622091719201948407642987224698259796247084724028705854417594 +5103827826152310417357275607217586307830343393236258610369278200854730301259212109525027296782910788706655437239982908254923137 +2959652192735329005701145454769790852470342092444456431448859896236135922540005403226570654335981197446157556763871319983952696 +0906069491801564153648838254163601062811138002404571553190646376754817601328289018602749191681199089353060650264442584766903530 +3785962322135501816792718661619323345882587573641791662855806452191640690223489872037123172150522718319643946771618053874607301 +2342704599377336428681571005102144288192803466969307063516540780329331225964749753260124769353818714585772490856102378132713276 +4346236542835966635872778222659462346707942811767870117146806978169322604045750387880280187005012616459999816635343308178563646 +2604219260434304257671433967264369756269802345023491459538543248359901859356108205297353830910989444923284454022447613985397264 +1822281949178892000423296707942037651472365683251029280064728588016134363373058610506594311454908111460545368768742973357987311 +2329670499086760430314104242828210716854904379725565610986919473143807393666799098016719544449077169306161547903571473642324181 +8934928646320239275117990276319738648757543739070170895539210951333884142223117539519009532014319073300363829125825739466521964 +5363386211310899141728954217940413318460520197666536176317994465965648033515827854532405714333529319178360920611572195490491843 +6452281693971710923703852296775814957821367584399432406445144322839651572894248840082293918339020670715780310479909368596480688 +6791949317913799774656284904153563675547018432823443924016298235065082832647707880299401729868016947630432394689913082273372425 +2712968598557679357916581334926350397464524640833937031346811806334561647348253048938805411658427143226876519485429485016393766 +3501555543617126115194082213265703065410803089224710636262686284845574699295863975355551722711478618786950261560124771603773061 +4778106677024357003045486275897639115668057170330533010873096634039731123238801834051871842868704307554039712015191582514954442 +5218459195255301650149970279364690486331464065970810654696223977244841051824600951397161627678662117382548039349844217280375193 +6369806374093253248520031657537241583648618465123745039819043766572718344368140639003289414418211436811709774570097952888487167 +3212569840247911007598018387084803647762488058555511319640016886915147136497977845568137947542869347226726490373484609221240787 +8115733543956480787117884715874295702599005706164646884593210924736152784838359510894224199377258864940900115663844969555460182 +2035968818201814843503316301816877369650620471604903993852843829395302301918155158237168629501277731356144310878037185323999107 +7016895419441126229181619830275636225832821520441765441532992753638148826237793875874434736200782222141244213180375828740996801 +5879711152250078980107885067914386728437059276590755707069830533352692084569773933300266007591174281283361187767672358396078896 +2103537195144138796197574043874859185879649977538344432422677102393984939164145250471907596342652013494773096173424264207508962 +0637450827987698515011845992398093937196147180755217329303542644463406470700019971836100495535322254679390620873420862364850762 +2242509562912546291760696517385259786276250406593124098460913531494124669973775469757781521279236307631066606896102762073078153 +8737688554624301439839538446862017884613896797755819281023174101498754986747558403862071766932594675733495440363990431783540137 +3643542527592425148722339107873667468772709136145667446671572809454163340131116640859776867285040887038441378816294697995941474 +4072206008606547388536544456067257224875289444010744788037863160664893445070574937489602184055019861155649531412106370948013649 +6183624181311379076807905566573341779391313360549851142539899794930473883081910943750299100861533261211231956101452244671916467 +4790552691647255291761946345206897704211085378644313837821311423673070233392146600975258580259558879486034230540380014801626096 +8493966397430159495762611418791059378501633459275242578770822154956094389019127421733416568122031957966837378624113082091125501 +0539497663700011903785013114734351822728368447618541431249895153026462750771721023669565811790420914410370442390790070707245463 +2527965030066432300857015457970368291786471653036513592059650961230415181073725654607900007084498257528783224577191374901431098 +5466092963804647249090725375187844347347973252108137441562290526430827908072841935677879142427224945383497721669674256500195118 +9408447458584058076150017650238543964665522204847767119461148283020725314993208318241105785737923682521127890161621980525163933 +7752816476751548314213662757572297632611588857400086488089795605852388674435780461993562784936271707018785480608983595358835686 +1627393802741824989025898363620676709684659912421866285079664625171969200262501658717867654051389804050637814037774619076628490 +9122449665202107140311875578301366613855230050319765976709818479408721612162782387682958793989254712472790259401394162587792903 +9264559273820216137804057886666471549044481178190332632113699758788705141611700544482989913381964978281793867160157791927781162 +7442806842403069859300613325533104981898816922110133255013384334075830779759933886533885733793824024671542171085144755776119332 +7374346360629846250873248611855740390376331086859870274251856769041575442564535703086946429812945653348220186072122845016274001 +8100158244663714389704533217051390472696914158612218153906542802690978033894796544874039962095788720080152937661496672600961913 +8620241055478925684985952252838411836616234675852362577897905957450930653087681187667293254151875334644179085362678160756183911 +7978254875299875225763061871403474457110245655385719997304830996169804380424203018555208820048813202142753962116252594621406974 +1364681806031729655441460904058555518845771287158086951978113218142925356256811479736849256168552051145501834683683179144053582 +2064837061784377038174422796078941972546986123715738128334902017019689407784830561790843652346745495974010607663653352384636987 +9087616048769273749416367719522093035127707930211676277159029972485006678648961483209994033836189245748110099847024086616545737 +2425064383233144306141397196556964739088709046393835346029594675076399505634895487000914867003535813058158157320486332684663652 +3641097021982425901903299759975001000185954507785071466003606303822049570876073457705939373716255467071337231807741213874106729 +2074354309025181421785407070622306037112442163872375219312465679573739444271473076859615411141496918260111756219972558400237414 +9209218175245343776137887607783882100053568632353169828579551734386860768832081473967959877027533280331064368843953279403791000 +7265829914895705780556070716875430476662014009364335116363868273301526498001345684876869560767489114942311375190271613882803603 +9073891388696315073189212242611726526377243973277573301675482213008892303352424672983354043608654230919446999369674504952411077 +2313808555572186833151976371065120343070868637724954931470642187595688232146689299510744666262728998553432846824564736716304195 +3769895001051711590090397709284289543423142627611293076964116196325536683515731394758243179761144577336943990391686351744541117 +1964012423430274286597296893918822905314758106425920575603004465793377045724816176059995632780349523671266024041147016652652055 +0559141307894322726836380567121144490621939523042182901839217710526781776038951549469639294413748250130589664480237146063632821 +2784427615240187672159066907559581170138189996990499287132279332580644422377771338142338877270406145886924306247277891647319839 +2650622840124168882146785089864582520075975814107450314378337065910303739372347881027629224527453006767239445485125470798573294 +3280112665923668502905138798645913537592166171499662307256909576761808318862002890184471250591748083711310397550032446473077789 +4839123297739060737950671029462682374736375690549900393153725695518603231578972526440776594614418742798429193873047497505681792 +2160244025080490634108186799676843921536142621408043650219395075800794132235618687434126288996783749492148526728267890467762275 +1151778651927614305188542555589072273825319471951036866487051470913760762080070986450018226884129812856690672034368570260461972 +9126667808863381235489349849691246557114377306076458702232805159967351257175416964221955677360994319567614203175199799743819984 +2789442446232943685597991603590204183390799819475475321518636276804097467590827293001641282750302064255269776064371623832516088 +9048597881455099698450226466082784483581892839976853703691711348479752784919013613635237082895964344395914096977829703979539035 +8807651873851261763689740610852384724724967281185663138373836249447037268804159569417135422054907833329936731832253007919797504 +9682114913920055259992108818897312911322686928213632559659016546514207889796765657343025470035022686294498921433293889021982807 +8817906539189722113102359692407543788732651010055394962852526688290033541790486992705883840600079101359528027208567808930305163 +9801041923433800395009066327257582729179435202758752590945790035180893649947139262456916818609204478389874916889421571462394032 +7712450051093466464431798727528046026835894856944238434402423528849559439733559946888997104896406027931037956349309616326923736 +7381318701427430852131274547674285844624653982181746045743742319196948480723763005752758284784444958915729789741525394299696626 +8012218598270213765952492120607042939337977067878086380794314818747633639449994732495580724511274417714861329582888877331906442 +2523903559053376560461999356914800460953183047966595580074070441846526092785101396532452282664986312641571216344907286853277587 +1243980803716723402043316042450038929890705999070870015541470833748745344879737850504389676782666386423839519311757255510240006 +0284528532586136580475966266604417479884520212063362364900502892825693493079279816397630592037794996982087214380723458893748385 +8491272727902175551882935376152929301347887913314101691264776996119552678466629354884721510455050616404890619695271496162337915 +4545341965713318896843475698632445949069586135305533687841423447686352614650216106958803696792920165610566274323872801678178779 +8253568664915132158211550322533453124118327929548882623091786963905677622475126883200587876894319717512008420758223683547396925 +4320222323998014931144025979173468123790086139354433220062005544015035220976182446830891814234515809043692298378909223491021578 +6069959232219660879097232566121384126364624848136634056377125102029351039021829959177114631501219753707218086851316623762802248 +1243816089430429483661271140945607300465854350055161784219266668283907250544685294916674983006906835039612884905549454140955379 +2940117682598404016293743763452820571055836767681621549318648097809089905651411870200800086998457828557152804031061902717793951 +0887454608619720066585775426385076124950271424460822511049331848391671487328356192926497422819817241814088663154002962024098217 +9444111199203225196678001748854632792810817288744330344130819478809411416720197937516411714390009340089903953898652786438479523 +7598508564858904067093707855688285940754985295788676773297431147947757643127210193954685014018553421494459901761944702436530765 +5554085843289054481441810390185341385291272744398367506999297552891321596456006697431455499740285520112057597547672427356435569 +0013578994633080338189924814677511395910036898885609486453589361725442884886155382037285922038189976648035996103250646536792347 +1883418355117695192895288397176362340054382375638457044029914555409870529840507997093953867543753807297378240486101911704147190 +2990020394219301141282427939313186733000711509741075546302432127423255702951507832353089573401235669455023017949653420450062207 +8315834094959817998047438269545055161942585493293325815723771181252407447320553420868080424394122133699301368250012158017016770 +9114429395330391270070591803725103499827663830583921855069504046749255370339126443177002479229837662039928889147633055859281868 +0939693955047378469672103416236905988750384823305338476710533963327706820167318110473623797427088982971507090747733464699600155 +8097749323633489883895572126941411948413387610953411492455863784508332803867377054591645781447379503035057961144435257072818355 +9192774536491861553570480071143726445650506118482951514380572367117987602662371433431531020944047345411439555055616659122572444 +2538272673224446791161100046059076794348690288664907501420986291128393194698926073716590796828226666658940692534781574652080599 +2296719356672708630298340102476096286223198315529342168688227248665999468122934752834636542066357602969267253948955801596039200 +6907553929212877794229661239340340054507529389112502857484240836627584226292443778415389852020999685448069205241033372399261985 +6092552080660661596415004237982747843962036754844362941207857067711980933061648692291784758051313128780610506614569982941900763 +6594390851938570829005416110125258734178056235062424322731777215369780823020661620803238711218728587137816612075854867828598400 +2369696331217866566447853409278304508453286888356285386844080814982619783652943626843027594633000036437577975836948042581832383 +0948885007237584172710171695745820012612068700229335944852613395850206862454317862915465465928827325249777743488489409096560811 +5495911603990067019527969794975740716100732404562122420639048240417867412633964511077857983711654955131598842753406211465747911 +5938576990907415666730959973171116867930776870120598849297289380213982472240243585128799882672536952585668000017332908127868142 +0055033948608802263653030525631019632385167453147504582749896613855370979829552517051177850239757173815628192095505002356755838 +1575006377951552426189315697593571157901434403253801500948115475871805154047796850271348643125694619380221648554282326550822126 +5246972276283601394823321357823533983734860648197127606415601325852813899044500843947933650765212255657169653289842270182408561 +2963795283400693969427927841410518920229478373496996799858803689356286312942605835353285929732637824507016592351551689198434905 +4524116354075005325609861714463374588743076763717669989113993414649241279740839454455938573607498636902929162122468763846383424 +2179849115762842803410844590988059861418931490923531363860515702620355825744742928541840867928398829472206209785674120570623101 +1132410047750617625576007081275570045071771592714539836172004355577617397790511943990307565673693827096029990088810389666652957 +3079203667265148591706387956264742397674809071114000619637965798572137055721279282178439992636758230593462657176883252897846130 +5344900606236775949063632708480760988922321343844454792539062870953446557548709705912512202634518627191679599405320534739710674 +4862620838291477498931509821859436769537924979388766030614053473312195673928520927507333262772931790461823017698325114264269689 +0377879474949535999373402143435358728101597553950835828260875850446040578864121150552326559433858342085551900119266465442357108 +4145382952202338626169521528905963930174893641795335885386219002691641231007259917968036502863440853204735639598211642320126801 +0053580536687636415170048134719028636721663473814357492351910729926031799390889508364212752500797332425619171871273560570543315 +8050888999124989277531284920879308536986968026163965044507397321505480674446271580916011349473678101309289578265591746813517726 +9127557909914237400136577868957543371638044740008283166851799275961511059368672159330761617818453900045874047935222126146402161 +4101481561011635460925901510238481810190290058999387022386961480571796642806735909558855831574016730200955930521599776711652022 +7452149234766310409197279968867850647491580237728049077688692166936015804308350068889158066597960097627065379539367797830770392 +6879895654050303365324517468233477011666079944449521630351405739029720149442284020545119121309386081120398471736260643772075320 +5115668578180735659993042798673877885823921120201277569914715190894516855160960884928665814998466370463245773786729580361126788 +8473676321663825127148201234179810956242584805477442480418075610556509294721684997709018216961441699698479559880791844442850980 +2548233794760268816189190401306088756408880212519056730724214290704225280819394151931727397286420914387903845669497115561372625 +3358354482836700948199493625980064917823071542164808097350543961229800125334834517703864655364546055166975880935305435123088821 +4029969129548193511513014303632741396672093205336941776475040363815521979058565558783096959704099250216961508907068930707743800 +1178228259948098596059380680409565881553234195364086559565498076952409655440130991639052974945372324956190156780171629355058755 +3717169015464986627001032179838523927735142460855957466743630432390818286505306791995379349323709241837876168544133279436428880 +0351514453291951960472553858319673428002898082287147987459621016579844258976392337653325512601241626465492005157367458123795393 +9131757913676253650480410533013952331883655576543674109012844727933443442812463625615079052305206439748070329783436962037461924 +2344960044770585240104572826430912626099098553793285442582298302002106108601045902079657749391794040816491373923940544837370962 +9188063701594180602103941124399234673973525989100931802175158910488942922514429931243842291939057260146437903218286048586526737 +3095726475590315515951883606439041451987645791724176078005404488548116509110488969805623639698399122683827452715501916665020036 +3403203657780121846439527513421356719526606981096988845264774034545984754667811956286997210908649753612024363260420418331459837 +6454363503953069085653959466382547762654112282647446898596667543948487517796676159860624750118940186171824534509979950086899125 +5534857215488234005757824538224541774649888325359087056984429790126545819871211147018201409980430263764262297169187413523924493 +9520167452539794941218775260774423444492261204451489938948286717754151689428746487424118400020813014135629700319029471552190487 +7649753857278617517384047446362214242830920706961544783469693002591616096695611398984815665331683937899607935227857197308530722 +7670636468635321903676766861271780561993826318676409654225250617146887843176296360469702345236373355189175304203166464435492803 +5456942248296553684603690989598415553795292969337585462669697131930121000520692716176831920728412650435826182698878423955991084 +2896202227362420166816298936702205787111522693592087473120377589659651412246131193859654870409149102874068068052947526362484606 +6206499177988103420772046456006763763163890633653756586913836205106440292257489426046140931638490258992453397047593430476263734 +8271727426541847240045919975482895895407596972876222349935866226855009736203327632117757403576107132386631830517560425145954967 +4982163925157138555939635994371164500846077564198212229817459032427881591645290463697023178851238007473163893076170963474781460 +5340106261127351887331618946742533731988940154583287036382074049074320856289664772105865760586792346984849386692407381048942810 +8539587910802062328556477423232706345676834247983911515809748455837801552264721412722228887311117555317778442883915233779797479 +1727978384302123967727306531882414560603798977850417620003458650086712045078303090511550979287673387880893466154163854646269635 +3674694578655904749986580598395421685146833662087197374056652325798896836233778806349388223093547068287833955056912724333066803 +2847637229990901535043145484946473452323017593046078412652932709449735113175295374406245481235052704111601101677970510368326922 +2791471564967916436181421827384161108238141389611296884460017859614820709694863977807620407661790121758211278587753750388535470 +1311682212671438808292446213587823526675524926735249896308087156166254699859676499961522597030531906137578453887072208144785351 +8861987970713845651946125749765842589051394459161283498525078704659636051445265425124710568264660637581231331333166906616781773 +3433773030784699549858220921642631039795495279596839707512577767723632096893086622754554818674187555230885034907077860457028892 +0456580053379968368017945695164763479833506838952019851181545387562584359601036032261663315698690958554540494006314322814695152 +0089934480486704619783110767294727013349926542271497056296250118282426029001430520413060977864809649544418723152448749796245164 +6374218433638081986774399897692332350976246009622586461188714603950733284096247572024844313234627483374886751903785440297788166 +1964624071497448806005939877571581081852368682713329198774704389508439970356078811686024907131424792844471710419031994307710495 +0920647722499203428391025133284362175932945797352824069655533852410541721344987923656955005597638885921881748026281505225146701 +3234051414875467310335184839402728147911090604477636334326355603404083470772369446633927746336105082555084803709873030579813182 +0797776807854225896647327655002411099428092825155001834564019066984990634531216819521296497699004639732085677368728818595315930 +7917485162442064658421046487728607879599371289976088757626588362451318559683586821768061531908102225442474938214076282479553260 +3281219759953705660325535461460598775637270570011648848817233301493712654518786509665608881437564458093540417006933366539835720 +1421309961668710980725741781573712344943061720229290458895090456489735933725778071381711038599743236066437961925604705889777444 +5346018484213473348778149540609702140487404833149084935132956734625173249282732560039608351835971014828152667004296001672327334 +7640982015534543149522949001287972675835062998825649759593191032854308472369740597155188057572429051521288244481472576347709112 +9082197142126709759172581677046792938596869866773476159806997279087695007866672568094682918889951065921351285356324461616422296 +1936662836367636814393593918150500159197921407091608653457518897602784067757706017281722377124420704953813373267377728149707235 +6165532554189463034890275736530147346375384892741034329027227244542906540148716547806611470041759808947343102903725834811540237 +9921590940060069893953324679767980790622906014798953371116800849894891003273141853411016879477894602450403366892842855960933978 +7315523767557595011955152358473653019805790165086637876204849356737581570450280098237545014539966107468231674446113357850428012 +0791096311925956512226765013630421811573099687378388596324590465120594475232654312603704526616319090075205150090415360289991719 +6209585520294606187513003055352209151687938901113926217849214203137326771247519247105616300171713950372822794825746012319922943 +9070419207282434237195104895273960005868745134822958234433238365905514670266360172103212277318139495451964580465731804042546085 +0338167338157532972668177758079386489083653270128069387600686406564818006155678752863600080494279043355117187180589834925431100 +6075857929007054805987361298779916735419742025429402324367885856941288459616727309405216361897524393233989298288583563658786859 +8295608945048628864124667559446303925571884604451666719475562297491635918267804843940029802361638899118153382320671609177821037 +2582167131471095203583988388775565537112236568527071226697464466863969482609071992299656322827744545682290545625251014139439495 +6711594101773826530320065101211025715415340393938808536071030195055575879765597974028194836866850448633781263352118203830334159 +1911464166497757327229347266491166768994040887477045432967042061773419808596553435219059712816623298485611786304164577509711093 +3950559965553312195375204426888631697136007142008473612408896359608938295800221018496826509159015008898585495437881710932385623 +9874188189105976892158996129334251306053998794609627074731940358792016931428037315501950874633599120688188498035110487709165641 +4173410887164627415991784444779118017795312676797160939963018529923226506062126935003504945358174169319649592822030532548902364 +8585536437904001642599877592181317351676828771494354438414987542591562928890147892012194379909967644778402105597291718886776458 +3150820435297838229930820366686733000243384455249980345366643081949584071763731857162948259797374460060553393333323596509856155 +2746510308210106138913961915766655928022321725147254596535903741506128248245569457512888067134893515592196316478265340857967148 +6054837822399734332369250046782008884610626357811684106025662192613352002782118381922758419491552146516596733810475017566505975 +6253444244691352274570036070483313882748278795425532524034702609588633374333913295809727607310412619066512211586460777396607177 +3167935626890486882612342937895694862011472784369829386553959207681859657350791878067764914715082125859929851130735487337849909 +5973912099248621614115761320919410184939653960557823675647875630413749948258747675478750350454945866579601771984960798194388016 +9736525221109913891380740014791803647462175891768280453619965529263318350088005137408675181210618289125588737889391858187625884 +5353369021308107059796622691343010568879776199002760578714065302617216068310095438159704609241785014319825155619864292170880710 +0495180914933316707105009248960814921465681956792191416760665299694180526760284922837494706424443747428740900047510907222820027 +4261167178860446527279234457781216359164057184535569399549566971774741437839602392078186827336187328588770123788484074481402098 +3638667122645479534954458961543608733857643210245423464371257036918768143627288762652953247716922315676522443004771243444008790 +3924515546743559568892457871009003770852048769643235894176832052809028875813896140562020415910166599360845331504914716199418304 +9539930208848580218543922990732130838884473458954810662182599563214576790823089949536669626168272941913640024171496275351316441 +6014586121604715045014833563011298445249992633905973084716393755263990314978408352185626630254184110544304520462563363100889663 +1480056209827971737404379030390423810733649879094610502529510552900145205746169635070607870308190199905737823014183804945008242 +0269907341608215559877018486447313197581053179139277368261940240899846162645640750726730556726252880111678045833842135360488520 +9452683639389882051701960541518755636274003173728067144992243393027569642308110984514593826650393132965688544590683455796310124 +7380252617100440281579567534384005518334110526934315682916800234325208500512344342386934373532140277510905603770493859349394883 +1640841298979540048287075269187639921568678590871873233480612103263772909669941767642148108401983790040311768132667513845995891 +0797228841202589730512673576721011566549518209931565777081305309555092032097475454967752699515363312040210695622598933787975405 +8668051227922162902176856358741680893763627061240389531754672702747011947735221029021238076999742096989024601711374129582948446 +1940517877834907351003862153234670764674347412348064152632718218140233582465116935278604919054339813155764852687263384383689198 +1258507121750424356317899666874328759136117897322624713333775442411946490404548479399911860814088039900005338783292400154353212 +3215026566497194921100428678498475477212368831513037002482731701747625115899231609161962690257412946823057745227816040916263135 +5553936624046126710513840693074241292678003250027162357678547764315313001417556750343221645935609713903330012474781806462752509 +0087160096352464233135718030584278086059148140723626725159134239250134423251753319043440628036779710209805517160099782393064078 +9318218498563579335348341700906218700126032521018782542069807548617891695409073329049001835630809969460708500370067751685336992 +4208076385552541906663566236770990347477457147392506695455104977958316161334777343709329582406111068678972797895379263213969728 +6469519015224118923867472889765223881558607274601638396572334489644055785283085612422941306760043896876352796674858662547224493 +7581545382840053688960159322695550231110088020621219817091426507061324686155465441741816208389678694769740607167732243171488972 +4513458865704194161496940570841680853750653077515467382681747134693802486964790650970100691348806372555667204130083389487834234 +8611747863343669889389552604708894518837779002018609033906535124466043777729170830388281007572831090596749539330676454829092268 +2614980773184238784409092097541705502166871511835967598046040179529035245968990886914099394023190993075959231506769917989928255 +5078404017663690714097396036195442424905417384596337491669874839339606038090162958546298191413554764614281624619030688715839038 +2641999347004416078674706237266340276545528202969242926658631976187485588763571283416465910176657402769591804490385811120607894 +1022069138109039758566977843736614783843237277066124548095396035264485535383579339344672494958836028783883577568562124831488748 +5036220302308126851203910148295691777409108647096999242270479109438402097077523126876642469999412297820631461204592271671208573 +3765990012871705693020962224239694872611037068970786438641717866334183625110030788667653078611800346467240202316395411589819234 +3782933301188992583378339987527190128332463154672014687853873904582770103495961273158433396887983077621367280840979561172209874 +3749199929332536186794255922364823419316096604597225233951718116228041916547239168206812159635533832324993459328359641335851160 +0949078067861678051433564861906686233060038981581529250289577929864989790385982845786299381823508035058298434160670694386088772 +8589603171909191258961043702444348933790044242468360895051180975009846570524502293795875915077460542906777905339916008769533025 +4696790001285278712597004232911565273828035212555295118585512821803987256554356248161862945364958160311031123882054615472804301 +2136763839544335242427199077661794515629181187002289822787279783807001282765862055208820154228904904403489678312786030757766407 +9614871264207972090235920749815254428999808582182551807788105640461738443833466880592889986393806973211194934814261892282034345 +1053682892737737957362804191027861071649489484546703968499730625323764928679998205513025454613668271807284174919717885935584552 +0107677739834015517323287238318659939175109001778305487593186279160892671682768468572465735391214037913879126299875773890935302 +1453192783717188719965961795655288342172822178539274975297334507529484119848624649135862288188339473719284880061285948938876451 +1104549929363155396273207050870222760506597442331198500643525825130190102095717730285667006432704158844993996831311067931052472 +0828794534801481162296313627002145147495573308897739685396596733882610495059540555269595443659894160127867987587635803471525887 +9824022973580621392509031573913634949593512565298787398230994893915219176490569710003371534856099636970419816555736516423169506 +9878869772576948330900142931880528301401489012367453237909670821957956278129934157555287021598067976133621454184066239649408611 +9449658706952732276667554753119961137301412966671524990966931464264571223338509226807775956572226631814170860570478467598493299 +6650524655470467246688640367290038855618731841663664529545949374735107916410687959478366394614227095766286594581538618985047713 +6086084723895749993359399578577946854255073324766404400181960974843071723540051597517023301307855898898894953430083847202358708 +4753978848939024247917094291054930385042551054372444060172056724142258138979831515270370976027807892767076899036493276499539268 +4888375835344223898032733332908158386194226863203069893727606532739301723911436149446573440019514355993413403576684311443713663 +8296174705195869577823179373277139184472357378750746123040139737885926265459733919518384739329034183096218738593256149149459218 +8744604285228489074099091055498838353604537087151030264581202847750393201247650508352763633370859358894270796201667489608183108 +4803757550419070833057341387873914307394706611793081307683075761521436160450412298883575182283341357874191269212976379223012176 +4435149804710266828206094765772134040146772381477199506975830561360349698395986931196641173574221626045221904844792851097031410 +0140901477445628979104022134212056527853288372076077175566962012356161842830489257088195443545165486451856025672191097292162212 +0064993358872969642733510540028442337052528489493327308542431944612538748472046808937048692371405185247438306806803215040744971 +5863180250517768423942955033295423284494978569038179153339219650484893674393537942959738721683811008504606623434622402463074587 +1057835094428515977317637880823368056821605598124838652727786262421298250352722448869673429472764531868054225289565474229567344 +3983238067434669068143443350182793359210352492378691479389127933138449974060689160199725860332928925347596191014918077016053014 +9909206404814481317875154662284644158413522012241871567063512207157398398808577747347614035401411443650498361772558269985219726 +7744531085141281581884696688318667987388073192993652661160923279555067568241657394706279105517300977887168465546838484164576246 +9611748410338839797298552575152326071882814435353560145135335186607329942289657269580962404267932188572721339477733166705898691 +7946378873728294158344802516673294879896454520313415594420541327407109273815189756012820920103635963852069701484919842956064247 +4874591841062298195410390631359369161750834793590724525846541010552709961171248502344146476896644307714211419632056930049968152 +0852013636597020207693222771451991722621240083418836922565030460173149090294177983564964216327697927947088353859239269055188074 +2400549504774664967997419613371907388057256285629514767888950107220270166626488534296499500671373046737811115964349503266901011 +7846949189277918675793279134349974486913260503583373441600579959916070999842932572749268240452265295033063113981992389359199980 +1807261813286492446582855762770791231541210822077548388510880369998042947014204967053843960432445968862263380028288322991265906 +2762645433260880544005259370505499891003063278739034550726117946384596067905756389481622864019774830490639904899687312014368603 +0877598939078117695418432932115534755404639291467885726434445252435059379183430846339103652602297046524638381772417363883263654 +8359730049870608112690328069372460928467332597727838745244400830648941526818883943362511766258737811338084111308530830389543560 +2410247474136168092178004494614768123474893065682802068994067272461583528237284734448787471492174143726629957921201303786784836 +3419244084207146670255645738668183101897287707313517219103923810981141387894446864639750358126083692639837682548305250192071549 +3436195357834369331471914469743643722462664355231433520254139779344070714371148189113613561574794491575113019678653016828154505 +4439128393642435861391888228862161582205771186938296499633571218847225435482241772976029740807151825694346388408247223423817081 +4171338859609192030035030504936231948955422469935021643277709598491047020926862755743386040228384976638566676326191183360208322 +3510862362886970176404175491659583359746924395962009940383204211293131363439339555697993521569537948812781342292800263161257581 +5726969821251892647951678196842942528264097032325730950425154233756415408461741348237674984518538018983212811201588754756679009 +4778823499024627173529601893322692810338013854983738107737211008580929848242105038076828712411467725165215358148210018424891037 +1496776225022474894349628318466731097093204838845621769757160832213029122420309244726997275518899114578190401052089674878889558 +2150627813505105989559012023523240177523105542484058646465606833626398767902073791909228024464129808047176621447241995834760361 +6052017973982660323612282677894938232580284277106494794233888932365369180563831962638387127741187828046331415436706723759407894 +5180894648613337149931873741726031450761346316713499884971063033267583243155357639761188673059015634957451632776491096066497095 +7359097724129074389636668836328680896804756391700054500433769926703593431467797993796585602417512809374217324038244052246360155 +0320127339112689809171849857331267778192107524355671645529083978652864384633410979534318842451250352236642968028403645196181521 +4118883887076525329129847815663385510560303081843081502700312066224535446821931779450723308317216723461024137619749777688133771 +6860925861993428345905718490095572908986315074234835981417267582301259888782018674341030424958141289788351602467046638469818924 +0143824935249223588362256342022083873670928848969341583470612068807101099916137780686058304339569283167254333748135808339800772 +9845042645815548051061661959342234028016893229602886364367950621055704775648720419681077874002883738984453187494339471937282961 +6185757617018491828131168967624272567910874050935111978401025105754755523134728391834040088198125315748002405260114662632225537 +0001356522238346600683651153474644012974963965580249070955634679932657691126596449770936543341015260416997826513343646145044283 +4828874167962259991499503673600129164632862005208870245173240873585329607224314026307889603968799682624300985376989091014318333 +4168596789573195113960639290119776051474016854378309232971533989042184885619296187870057410925857690307120789952188315701548184 +7815816015467871136014863206025674274535612873638378381437080759117487117954420502148760971346576864484963796596882357424854163 +7278776070084196458814159988413439534481041061939764299982929635630954177993909110795537307351830954498765311432170571984412940 +4866643127910829515007652463394937334032003939291631862785106424516937511685939908344347334073225050202328398177347110978277589 +8801678473026832588217149008639268101698714830248585119117348101801713106570301483277093069752495633755950440945309944552577536 +9617526174545226424763055218268088790352736241937230497521917254797890205799826169553805927031554132395993133419267021828416925 +0588595739388380595646159745519694245103070425108579498054959496392270944865180508024017295767451162258748775694293117545535076 +8702772928743292051022384687311462236447455075709507160040356608423402093661525464261088741775321017969842598962589897299430978 +6237136476697160214138655273076686746008630927947783830802379210660809805387482347921808311217785296709410669748246476297410657 +4956721962293257951828094615247811594706401567441027963919853253912294488061757086648296894406025064932611868738979275806148213 +9236140986639524620047147846000572917224921424609362686790157846221057427556655153843449488971368941339909014048314921826442675 +5366378038534835924469785482056822213525701444985254633404597205774596447015868437597513680309327466710395924280351223623339183 +9261759487436784234076954133371789265925256106206142125748755816777807349896143234487553341181444300315188799020606387261852065 +6449231339989879656134113568400091770993505914272783860814830316499113207580889761456971111735531955653301180535329421263128180 +9593132987957417462820592468596242875901632353771559986709766159903387546786798526051530972235857175987208754226458183961092508 +7166010270941368051527588903270384190676373547534758318035208065713808112773649467017099455130795910402044915843292687098488409 +1937469715418217414858420673158652244798733546985659728265347906685952080207655697652213278741491999837300249957163250199620558 +8228542398195856592161847035652366519403982135370687548941385579105164032051119406009514327346290711302155896619001736994882959 +3637688922930586118891425546901071630399830689323382607140358997300211646909715463890915726479319114588493474688432142961034974 +3846092487931514115420051603659542019430636869539365331821500084544038227936250365288930517356505814134292254191756084661422225 +7491630641568207270697424777290522332626816338241009039782131614678232351819731901409187134000842000166738975995925878926795417 +7070750968877352578247753117115107850387026880348329157736320326010941179590211312594408978595708610843851283204781916328035469 +9579425490712849875219973084577346692279033275981940434988363957383909743141776369454560267346747976256694629456374012164182136 +5715566001611830336152118124700907641876141203182109095013901373890817946278609009427237417191831719001170380251629245333691467 +3777911831700858846402803605902436628175309291503492499557547037759922326055839219601687167716391664999501580752878380637519910 +2345655297190543682328955031766959976133728834616422943753268441547260728586436316967787919247613972239277147037959837372479832 +1264917138428370036605146743753527478105136932962495735773676134552135849746496312485051160777893753074748519890909181403083444 +1019752269689676763134331558096188560591351764633604225395078892173960817806638672836991960044167937282088675278982806967963563 +1646009447412568269213391779579827953653829284427738902923460839464242874336523939561058096952551188341759036552715805796071087 +4514073980834947668053426086988984219335479873558292410478690803080693664498437799009591988658726439726875547569087833595254435 +9375888306479510358575648650751703046541164954438171217850843228076528589259852655316343249086105052230853543970799873699798265 +0497165101239642555970983822927664186264700494353584434852254137560612395432178721562325965038387864052788810184396252265391565 +9637587742861000445296706995627451301787108835157787393998256171661400401789319408568582769562951011299994449045177879050273430 +2769638085862574311449284998834663396793439445498698446119611616703979283028314824805123683773362437290195603306996434216889157 +9746240352274295235857973544138134283682808329702423798784675201114018293748073782256043515882634590777226313897991318556273667 +4701746730536780180098002090253702648694901490740529266632381315672027202981413355374788940709298520432491519903507229198264330 +8070148149525433117389602780506057744721609297582112109576333690538110175139908485162827329907610365996630627743121137187293998 +5104791573207818901442004558359267736381147580646586301522698902268860041597652832411905302035950359208853670857974818287006889 +8341316079342584982610662915730694074266548963632208359785240542883719157226848170269734749166834340935627403531307648991671361 +0980972555646214322144321439065894684434469993258702669138171566031352957356377641038155398195278908409758982420498643103410893 +4371601001168990355540734973104369357289155288304862691393888938344400557230712070949783924178880475136899894295722616103660902 +2650480054074532640486048424756932242389804525662345983096982594522899748828272298964877383316038674083325253436014966605240140 +3856519566975050055663208955514019483009113366052382153385562181732228724721105677879336134186949728875130780075511950816508916 +8078373724394599222427487550133729616625008462045077201670397039714132392823230257154531053190372696559721189464062559107218427 +5533555275076610788085420797340154399319379967237990930040901555785674670104205199936906742420190549652702261090687000720033243 +6220111323366364001766649253557628713894497231901067623752708357435827393686138195041002675760831950319558894058730884453262921 +2672277003207147557184524907281442713352109540367339513087567600977008089227949901291846746947778742548425956666175882640922513 +1341364992092721464190832131115386139839119999152547728030982443332120739263533376607896512917871745789498861904805336640005368 +7658494769549719799407250503812834345058648333149043237321838262779633765459673785919568562329102220327192186018538985265090758 +9891469225677893167626557199385256479464450147340948798817312917938176509424351376822942297600700655298360387396846679746727135 +5585419624514115780086956162546737430419445855772977848266264876064891998070796161296726855803937113862539594644902156882096859 +9369735292641205270247208512449851304357935258980017450069350537303506725253901558161348011161752800022469320895146408450548005 +0250792460158595347750598522833094847081172052676835161342784300572879217498951077354750791561533069799550859447965317570533329 +2215924262194811929293333565918894008757945953561673129802085846175034801490539407544733104736094736753035909391436637603434211 +3003828547025999647155773214469770001753156334255534694689533941988376812539304881256890976599139258146884644045613614613975749 +6800289970404751783592624836999186310592737935219914459902710041374388930994677919752362443845898391179354567028930335795630336 +8505759963363966237797610984127231223565214326257640335015223496586184791131912377414208061741646388077399037761412502229212389 +2367769534630970786705110465175915418118488516256049569042500049966228999665875815493320452276993289094558376973109778079159470 +6740493910513290939463082366133739451516673396242163998658427933891438988618879026121788841470475041492666218138874169322768351 +0357303411110414929271994301686020384849443986791255124503432672170825267728337655437754688169388290097504447359015466216468554 +8192240142847884347063706580178899765888936207019662263339819670804657575316021484690040614562949295841542317355852873318646994 +6622263158780477778507038872483270954479088649415292887077869603719840994025710565546888311161500807362774885446004022146890733 +5176307819234962298055575706826929453118700498832781969398229886642207165799263179818071674130902753482145042794765741675062383 +2751302275326116756435072977812989216925193208517438463427139377868288067976273977283764655073728859395428187073500573181560080 +1845833119022235738677681522695682631533441089840982284202257527507809093077654733824218731845673599302802574363126193666185391 +3166900131346963127277355530406427740614776135936937104096485949875917688047955787215898240293170537866227146364747857832752126 +4669645223062976703005533676266421855057200556768903274559944244985743499257659816488004923631139517399183550005987669212203452 +6297157628559223790562907965128004350109267209905657347407682698178194211365899319173140023292544735908289515011446104226422300 +8502054721545809152280155719799579471628904200160322267901551608506906168294064129630324866997658363940456422902104004108177137 +8611455694956943077268590839116881863962149614279260195002902444128093061737269752861412765172102748566330731758569318211290162 +2118077432186424807711637317365270035214176328074110410477259086405158562676552653953649314850972984909614735542131768665131395 +9130318975369645829745860603018652270421321400871933193989890358079853776989047665775272263249474986926107253991947206182591757 +4325629926598371530112760898633445089584186304063488458578614511450976053503391266403574144006236271752461276059535144816112805 +9310962691558816998084824217608875970240851404516558899699931821439837002656912679683111747809512293159335274051955163666587739 +3902386789985655245291475443305453851903636698632422663873769701394003436882941095444088054500028643416504275287739285414766428 +6731684059779922460344447840778233714899809573570342484286950672469381242333715669289828470432847009362616600780028432025743033 +2276600041430073716292091375271727222342465787797822281415644677410001912257463959430983836963509288786612692459635796134586826 +9782224784159961526251458006646854715817922665126779115397720973464372703397941849152840323345005890646175543131277561028472366 +6526167561528291467123805366888888939086346365110568387291388627679455761130295567840390067905361907699677554800553539940686367 +5318634933920742765367288875699774586510799452591417236786364826083325845867157040527831875773026806940962750032015566117391953 +7493598975572064199793468603272146721884132503560554819191261369489899329392181449153306365084506335645635211479940033483680033 +5666857577286998209607341637144223997808492861427974015972123407497307671199006287616123632355515817115708130939568369663610192 +1205647133800994070282776117664323692713443344045889466640364601504907846992739340462456897562574412472399343745023522153250853 +3330319212199788930516095800858918371890861157149091660451404495244272634139073531985568539542425087248882661841548689414190274 +9339962909966865014181214015249254327772461905399600921043142492498857240896128334661531817157722725029302364254696461600120855 +0275915123664954327692393323944686811113159039391999178218124646929218008261416648150717574114279175738099624550954907847794987 +7571438595941848638134927452374529897917662628583906227103605014116793319790628080501185855732090732338304609688113595225239617 +9130230309512350610529542991215174872450882806867068241227281773112095423160023040208987280292016395893990306260033588936149291 +3745658203217218584383948667226718968580182712181973947156555599253392240064780457939033740602796118129681262618905670233538489 +4412418271808065273371380680584774737632471892264742468899650839254371362867805781616736426407895133489629389395846145646724319 +8489210495984684333844268191310832723383161085211929007986759567690696951893569242504292424131665261321552489038411809330750578 +6574010925786704765333558400961864682658986685222505394734228960816617397028634600968531515638569668302469579752350128387282475 +7078834237219808682337835724461339925697109440081470255808977861161896407485691000442893960515108700590859410043250399284457162 +6826005017559754965121660973424054878627050600950197534543970583792709158920574211453104472613133226055927030752116724686804441 +3714997658728942284217722016417944347609973564690519305766123183125986230197933675112901682033357194516255841107152642532141304 +1057671755594723253182271537993425618933500230542903348744403011513962182314508888904498037845069539547674219152154472098348463 +4167698979868847784276420293320039606112408368549434641654634849595716003611837153708427260322734964264330233981684490094968636 +5842205187892994596332543353231430273153910962730263311288493948447613898642287416656513718409931065308355871917064045665758537 +9483067464396684153711784947324331651250593602460374159868530726671454450164268258494437853338327466263116015842104260460184775 +9918327983152500807062535153855232542858746567492036007943220587038833257219577445237712255513226337182088357385642452970244677 +7796979178949646284046620756567408638628381588842960380640158418933607236535455721736245193350129856456748190582611676103108397 +7474868470568488770868160856619425486013084529184753412392811134951027143914884376770407337322922148422893409126222728159404831 +4025970172900887552972543571216650363430374550536726543515591505239084581125341961497432836939375430662456975979411649814396281 +1376673671354389313333179969625064091561856231653660770117134131024861451548997992193443491241771601133879416546391689721119253 +7812382563778834433247309068407407067582779720037171842393136498269365501469642413788690972515921121053433102407538535462479581 +9706575053071949026852790205126415890557321745536935615519559505631029223517371185086529237733348358256190743027438758336144576 +9006414531022472111125196202814342278375571427110180645405421603811535358205031055882679708681076022342218218061708622772715466 +7014411937404524372862470846165036293398376604107823885481595132489354330681978934995519374885800444029796119672180751145597990 +2217635875626883098820646597303117864519547801664674186453314965253480085119898512655154794357106383983041271503641895125251110 +3512252235837181461231288315033867607629725036297297118913502488196313074376766607719647753171764776345290627421411392489636355 +9367329989873888631983207743830247822211335192876356701620415814984875713241717521999471919347104942898869901044085358319426464 +3886932632919049176756333953836633255259101098478704964812325499313834184343270599669768380535271527046781188364512125103089728 +9085400532850996923140563913725057699601728324308946983801623506065312434754540522710960789580013319731801582815683477412184104 +0482120489559053393661539069411739064074531226478250805786509477678364657275374151794004014104036094940006833641747026194721664 +6223030054108461763808112515000136243498515799834948548188322786124370768805042850586372228996897524679407164853720521651414447 +2045659821087414454414430804224189873085215197748925231475200893842348103097879724089151789730563882183249354356129862099874841 +1315828776287414001746118239879779806183081196189658013775774856212674965726153082362347461854112895332344854176341904165597409 +1213515137476784596057537684608135941219538557595828609988436459029859804112595309586038750020095414785054256852539919216535980 +4484140865975650257767631970278524142097436320367062170812973468859967284286099661751054315067110058820945519136720185944895374 +7742692644036659798892433853855040190699363942257261289132684570773772398269153067661790938476854192452194252168071064023197248 +0852041937859186893599452942342756186406516322350714595377388609314520833365297395472603829432190992959559426685189491282347057 +5433185143289951693194250541689460837628217616946850778491455755537650206858581434235195035172795441448516354831307931147174885 +0450942348007827481803050062279398301950579282155758680606572572109484512358059858330484516434220157350961537316843651345720503 +4380803663067690067136823238680695720389449212682650668097530448778728543739620300359368766106338322729357455486494754904098461 +3264642960305581246967575470210497486665490038206044077023048630121578302316138939937893860519106763145890200076403810118822241 +7107639168089983991587862527040150737165433796689810859573435301169366192576251898692883826389304101824844158348543802249127582 +0669598166832510431109943775944521732861864105283325341271770704933734103372804558821648326293865298432780241079718755089452707 +3568107971104085428061715527742724933050823192969880425640410593395489624255402189258495659521212800172822796344882503818407485 +4194972547530169348356862971069263077075953903800152306693089832494384056021498593498576727061441507427007792270872575143948044 +1172788991936383893646302947517752222941032857029795292428946787677455477081292410506859204482638074930525197495818984377516119 +8708727404823062734669585099177621030117278743659596151369099005394128728746425149879168020045121622056893025422397438542352826 +5536557575335193916360622332260209029723091397745281648820143235533928946294383911963312581783465260511469849131842805230470062 +9799128949323644060685917638538635211442541566486657515013727896653746677089071768007544683389875228688476415065723679786222857 +5615573863935839938823973115950563644124889855629743447770458847381603667537087961954383608440966294356327727154342428403717584 +6135719365304945156195903748702135001035781887797234160617684449358873860897697192990886253607384422197173334101390229305548708 +7355386834242660296627412137907269283682869702323709426719706244145140528071269497974044139621935732608883353990706800769609401 +4117824188478783409286690916041542923147052848706784080198351893505523979781915688089182588499549735693880215954821551279637108 +4670168752285459430877011145545186817108898834243886734027847064654587388165854257852169040893626949216503073312235415138548138 +8311486748387427946913806861679281620866414231742557923760474181682196182819449180979395793050862114181314437692082443568986323 +8048322367949325657177291617359572925594555792044926031542356556453504106573058251613848469630469333674263842694166478603019606 +2241419420107086119951172130953831261301246015464350985640311185360710868324911647982596380951605647282292847958812174415734380 +0207875370509633314857109554449234589264638078127939118196136645009058962335018366740029205503954796978369681506112588271652726 +5224125202089733016629542238144949897132285324338195131993745529968240465385054246763922445031407439625787648928055552616882109 +2793536617404132317781661381899391556171029177272445009604125493001682989300317084296686416290208482553512764370697922889547543 +8188433691033017668731418898412943576673580084814405684350547059200277266867531820522833168888834691511550304066275553413432816 +6069781060072723977215798057681530374205235352090719032425665651313169145801463663003622748057443073337332287692516062915866474 +0991144941963456299854456491439712458220412985735998277280486665724362417204461793739331274746630407706323572187551658131522029 +1128985384630705817449509424373236680198643102398192723995214101875334517122923435752591437827690137749416921444994412824571229 +0933149859869844034267514737213635247382994476023098139584524288805316943149032915781109345152673212416893459588048333327424649 +8431499800476711779747358632413776057189971388402676768615975922425552726845845057116844980552680355154340958763735731899549226 +8999746969039035522234132566990886859367771300359825363759150761208120895977530444860283076544729935781975800213516901282765869 +8578384564148446021966192696855640744733543364607326875346561423047230035981121889907856300901288650704791837601613641976512860 +4782737185453902550739147496508870329447551421595794641391271174654998339963075546317914475664543351090858707240517793471880344 +0532832780888888968522715514533732512924561999915854959906252640056974250958067417634581049729155174966017949852499049433553005 +1013570953208696598838952288133824762193269724513979589866416713036164401176767184475902330276700096926543877198993766233942302 +1558731482683686439823710758826362624215320659618968974586082717778992330445630506757688512508423035162517304559268454636939287 +0350643207571320038261933704850349754594287810435189119965241800581118358691680807766625340717143030794170306528880710869836401 +7542021244067320014248296046012617744009265286583021536650506535157160764351137973007620114482584140630719966828687210278471738 +3972594767241483050839240086921327315906881022442251582254034730627668558044239515436332898375100074941146541911588915186647677 +0697122022586499143860386882839929813751234590790881003673047769341255221587898768035294711863803910391010620206350075218304635 +0915512582808181894140392610882201933416172227163788895549745243551406730258634514282881847090763083210060265428731131498145196 +1501512625884967191270103224021116637844888067197984181082849059840793925774486252348044423174920237439188586936474269289045183 +9546757728181045784907692074408445085975750656689918729547020741600264958315888287557582915125013916814828453001978125724956413 +3762013269830490722798916645093224443451891592652341999436840113601558507630551284115296518475712532341410529593546552381421688 +0535075660768315265313017230395001783344059304089071262026544062275821507449160749765605530180854702975356649517426392492153632 +4462229922158652059017569592409947967925255644154395071833612619360534506981545280294824268624971434641426791536487863590137885 +4122860079882835156235088149203967906043976190683635109983922591971046404170885166646247578979128875141127938069850765269247014 +1129866849265718274907637855113066201183196338524085201233097892015346569822121339489136356302467977157281536251430774161642528 +4989929495806193845801738597980701228751796772240750395477795189154079611747433960930484756810247462060407330702869333431213592 +2584408285550889731205066850550890247663800179003073740079409662955268984464235081016863796902152306425169108972643255331837648 +1846495490902027499953838660575831876113795963871581292212984733841932116361051031722680807069640515568611052917719380714508500 +4730368154761060853591867016650935034777443474265562879400822197268954342492544791524055284219250660710445080227842874954141063 +2999165237216316240588784731941187402326266210523501276042349025855852110771327522344504598981978041650966736224822421754899433 +2237345937661408885897695793709671176319094949111986436639240560063478917538944076082940836147591761497957719533329126550091001 +7598999755791735199200904311392506066249434460145555306422386713523721542555336766166589674039359217816140532997872100172694264 +6085462229902194135463330821695657542854346332891821785703935541518889566865952336969577854576497261635473651422862788436766766 +9330926718781006335569837395334762725138935227106710853342363931868717426383038886351253414292607302835343171708502682390162568 +2759725885383603903963134803626246023341697535265756560586925977858745569648871684971845885034365173610581864076735566479105747 +1316975221277998480665738247654385735782940591833339656446094875845628844928541054371377477189441032462815404076598890156931227 +5084047857098057061549088214576495134500797236087647217879151791731034614882352668503598335757353547259171118367223224526235321 +0797097525215466425082977546510079918770199122108187284931098479825643355771575695825936128687263528517399780086601002282738464 +5882108977367740469432474363253310136350409797015594231680654761034810033039280285322415385168502267113255563957206593967018787 +3765499022319437900072126982943230712423709856742749800481145115762174065774560039072882469409389283337021169962653600686979665 +4601581997618072107980461792417647044978305789885381659804666757198975786036139854382381412304035407153678163339829269303413079 +4804720639771780250811522331538529037693400650190090078194406321327819886301852415216493527923345326577327202798642624789731981 +1262413264233591997549583133381940611046336903584987787247990058376502498817637376736904140547566821566316367905947287384644751 +5776817509537024183629053964111738625804577685331848301319393405578215972673272833105126341165662081776587402862223607527237841 +3028857137505052452119497280084550108942293030946862851209526606327291674235480503744471700882929025765105260116471991605614081 +9340100683840148792620022922546803909003134351687693370396127613819896251128556322124687119185535407506368947843781106277912265 +5837860085277971472343245356960717599185735265281223333386501908378356024658012834775741482445665420888316355875215276412206400 +1493889817668204302273257693995210600140074382228307911629842034054862116792315868260039297702147104111933826341459504324514560 +4753667010660505615221578575743422389046538837003732162433830910088895626144829160559920876420293005201234062697339903600869729 +6354021869352141987691776069755076073591529011311969753213483772524805097648788519425735403544963778868397356812064242294599252 +3007002284988957442402223796792807680185699679494949671140500739304141518373349678385232543712822588498429151363366914067465545 +8768505870421737992314357883218463757749966400771061631074878673057982116800200818694276568644501192730861238043718483873582182 +2517111306024948547742611078204983908881805478217286398022649611447119014425201196572509914583877236045184752832586045682283149 +1376425079937583047628837215348969478849396716756967260659465804416943561566523910593617918317955498182529845524570831173841723 +1739036166752846450279902298627287438597800774942709861167238037011909085160508767640035085875297386568223167138576660874567993 +2749444684052418908020211162253183694144723421354516311236418763218072849726536005435788876083510665834462678112083821685346156 +4979778261313034734593447279308534789267778320343662336147199781647935359075816167778411424508493052174895307203375091235720947 +7206632108909493796814807157234843620292819404196257674170712616419318834035027854762640349933755746419728237290445935893359476 +7972523340752476063451026561953128800722191374241698295085331249527419993680680244353440653002242129183827478637542127263140061 +6106361246560664390746249407209413785337414934577733325155578012914242942249188259728108681483189855141297703781608717770284437 +2459983080689617774602481236618573771655193699359105459132425259318658958293509205828433466503731104595644550263940782605577596 +7182234637562052669040625972588934688697821675314146092726738283785184154736418695256280074985786382718815935852352072100348316 +6798709996334119952629721938215622401513286500076236957073743607147857811179119661766817565585334251822559283874472847953361475 +8077368484549177973605286548826253514295310845993496549862916394171225254015547467894554807332772219846678815481086218479850253 +1557723569466563274008492657223419903746768944164337126986375831639044695750746490501676088871281694494821017358352031765251392 +3419994305802489285895038983304368890483549511884971605938842543322203428568381657097437964153561863023468226697072309642108698 +3155924019915150179568158859379848450337436841515789553391587979303261228659176045375192003213895824215877498665997186076059865 +2999137472267175701076913261675494050812219217526275916261893447733447516002165559492227300890776947227359067981526656072673696 +8601098495166116027734424221556221472614130194797807299983947972112789211510616421380969053402247170620773625487954585201101683 +3246912051329707460282887222043348831157339566467828142441386949856139091307353393247884498427787480035549002968948796358423112 +1150576199110861158548115562719716941558388002376600504843363323295796185489468606412792484061811432762347078760084900402466948 +4752997880608034066503743367113742314926769742881109939696550927489876774818149369014784149716022391683583701555271412269098049 +4122947315189371954201913791790379829165934721428116210038908501041491200253464990161405881080768468552171131345401309484164251 +2900730978305956981781132350165898118101889333020350831669690402919195278513062935410969054110767363793161548411520509850489790 +6317140142444713262780353377445709754525526526100220931420853524029521161445026020859823577131205174501439000115119816705181410 +4499991523210057790779337069784661258008182657881453125037535965670105347302421579889956188914279278573529309721593121892469205 +5508146256593369763940336135259800659809619929794178470062048041370071430831208645554427627646035391559292722312680898801625334 +1648967526716449029810215023304697906807519889979105852637730516484206305531886474058371893246706713362439898050285759339179316 +3759482577410685684593596493926547017312369150876917333521714609230709447558843397643743851645851153700140054595127571231949538 +3562024855350127743440468170916592187245116700044293261821848456699166330596624128415577062851344089770503995251522564366175797 +8477766635255068732724822778949890633417123111391309938080502220545229819981424955190249946666498348138015162432180329219887241 +8326147305998052694691664218432203744648063166741259109591553998325744681167769329433557902221338261010505326465825260282053606 +1089081066369978288230079958586716934294819087031912657105656191281125863299636207713663767265987214971817617524167760055475985 +5956659161052521449892118359418646820302358805073751690270525811449427458601574354728795417080178749838253936083489079736869589 +4756040554221482987126925729358987605986625930694194674956898248814661136244491613548633391179640626903451535330197156879634503 +5269657254348772763363513450959859105237330221730828709908803147787150002260299247932869706456162552333441429917744946610133738 +5165094570822740809419687589443353580562603424278039485215120717937567901150913213688361129548604982677760158365547321585786532 +5721986004191270987440236388972839714531938869537328769699467486823118683325371541953906389947764104960444262942308606574783420 +0825423274999976416479179163665065444342056794814403615470874263539258759123358841581177125724615901000777617590704389294753780 +6742419699812742131364238655136167729098672287631443854160975671169256136755724718145831652290376122821890697634805848496276496 +3900848450956356316829187800619138955746586281240305331127062820418337136267131988611645101253214320267600642044789378308027813 +7768142101816225544376840102775693516634889161775441232950103285109025549587962218777853283361116377890977393111956211092720773 +8166062372469258701826599477785259752674604446062893635372304334593302933414637458058589360745838888558751703330971096452276811 +2564850420537564339701047323195837094030546459418840734199428562634836997313660393990033302759129375346102025176996197579767609 +8872870135828449673289983810960029335233706075121561262257287764860415827304140209641297619500143080704997500176861691643727930 +3523914550868909381989245660265536145050317774721868915147125702074806836551953216452075481799528663908105214114354861340552001 +1593559589951778598427586959614078571783195940556392469576085221648989891950185568932203712798348499019366215767685195049047391 +3677450698912939130282894253043150590674429032380702463421683822229640128060377145549638924052619130706857319423374607432012118 +3201722283329551818782298921051193764095244804825793425784091160773607662095205922340014721050275267634431286245410339294369403 +2407761928636596422414810603411396624762992445889584536298033386878386163466235099237586521069756385945437909851885956052776101 +4551892967264629771755662186507262378960508623998075765778878560332855343021946501189553259398537769291592769103084773248272732 +9106997275950792264693136508466046590049216555344492329753333502148103978681698340982507624277442543719542272924397318970160593 +5003643337652703728951291818235842005801228735393820041591928726474748137284662169580644065969190131576241311478203049648965040 +8986752392715741495979561841249049222033021019539768600866686767034142327899022898431650915325022449300084738502689817549514938 +9360763437700859982761849014720209991738148978736994931574590369983794309597034958260372656270656787930070334337332515206838552 +4451175037964641122624713478708404279076794770827862704532161639277525181767823672699083856308324336587319988741595847500500923 +3042463864865181170367979354550819973787820266099597727415684485159423697903917044701020373206808416942704276522208882269559354 +3048080200899219985092787175219925954199186801590258882778436766885446862979938621696230463254471930200991285622531815432909506 +4494465194385245878424876179842789271630946260267264275467756829223006003558035998437034034758226419782837401641627699056717218 +4254906820448949384513753027180628971432660348348526219339979967375196656324459448972917993637121707012531419527163028051764969 +5795056709264059459045347065077313003930925374715251843125468051992798115117609874425243624614071216341941951333443769594712905 +9786422961702175763231201440570153261798473160575498751716552119467779736203318520988291431980397558340560735449110931384906041 +4425505790631864513694023511858116075339628625002969403915799344384754821079679161200533710912237340094421570796695940926114650 +7558163384552078162151051048460153091046870543482529259775071328325205703384361917997334121827387863984861026567298429845774499 +7024151222382954834390823993921385978213128615164507102313528814208567079688499912070171914927156859122712993045849060653099070 +9429307166946990362134229731057708239430020956417601592711428461279614801214510176796114831132813782448589275144006367262952407 +6718941975166588281116323921210724119843734085559061091993083658912394337924185488399990441930203011310984521684045028699686488 +1289068200800732284238439423123562064066343303384293220311845805205731603115472146721163687728630207002750751276592100892794448 +5346713601139175262412572458619231213504622828349869740030359059521925593845499598173510818770588358689016443776827808183107359 +0509643523301353915236067697449265692352443278187907371809463832094767055096167823904552345535023947977150405485951950799761287 +0296312146861071422545805752124301678869184550494728174693762162879350214577985621448093757895707899661121270041521839117379026 +8409726864010205799722565483699810469720898682668219634727965108320651761711145638969114309423068089263623838477654883333920986 +0701982653061769206880551891091640218809670563459796391962611157072335109465122237619770084725609181414534082646884976727487576 +3526106930335563839568672131933814040405238902124634964052890265269753914225841472646860370300002991439661062411618062909298184 +5489284594499017553239845981638823575689673477128445661568350850251951827238826810782941625211567718831490113495662231409070347 +0761858518186015339779421490664354210470217380607247145985104277374945800693689645130689632481933731656998223732805590077884069 +9539065740290512427091282228814664586680546652097563995149483899551962642839270109575078586841792310434801955703204357001468021 +8203312934682104375075307914019684558657139598128081066927393125488596174624606696359883862043330061635166484866461998328167370 +0125977272761404461319903866680344397363067561278546743044165247263103505646656505183679223334371532724158953424891372755170794 +6917119468055808943262561786048361427010843965462318965409483983665658166958859076133376429071237660115286920877219457193653148 +2210582961962854397595552344204221218872408837841098089244290975638461916790216426889841183579823514753626176299268639366257142 +6938938279494808076759108059141376006848614180404742864748925105751439953802762622388768762467629341123548099684486115445621424 +8751508911757378238494838354486964489105407120238937324611137021997266296685725619021335556120419627792798600024754767811910130 +8942964732151281745754352094851080075271176779020939272053185795856508342400932987956154784656391491859464794187568016580758549 +7093803490558017110343856896961686184727738060163871518042243290379067462803909424058805017311424240997471204900355661374314140 +1566736119124005628798032033275204642554171747963659618881718232992872787295516246973812659316873285663747257448232242034285446 +6562507237663679024109994426672469847713139656813945226527911495634070212834050520202668179078431355382392103665110926728808163 +7163337093376914471075674332970801236470473885515009415164064219689152465011362048740380213961234696943727976782304542628115287 +3743855222420028553131853842508021542825305338712873483864679766747424043976484573734764155416545908654414383080932700776375519 +7834242892295013548785979422302177591758705851513681511359120504187140172485698646885346881713211672798953029529425679446990175 +4633893183134377338289855331963450809211760110920084232081216223646405738494934518180959043407239414117561447413964700328518217 +5433662660787097930154056128793373805525457327147412372094936236754711564146018693671222546348461125048100620268398154557791577 +5116486674264823893675387799935762509658389400995246436075653037631158518006706329179420693907432583221559739906432889568752110 +1646689708083793966601961846963380825939768409816872372745312253381435289673316700861369097472435861788080748455563750632269991 +2394913678196583770021969391869849166343366172108313828891776494450171319520427070741615104344197304147604907461161478146821535 +6970220265997137270733854244424832298730380713169130932100655564124041223879414671509531965328454187192449944156019761861229509 +9043786409994994408835814038392730289274231805044666470305971162880140578631950679277309655392115372566040718447348800220519568 +0260472202194716089247619131312603168906540268723365507831041804698080034582459501938467511494620080710391206340388730610940838 +7326118518322238969330571001468796819573761190336351214992854388628577544497438052708919701742914513344327189122670316147267946 +6390506784888882547755768957113718192319906860888999507544967824165514074944084153555022322181782516106164395236293833936377054 +5714641021392941796626276072711028884736425391454865594302431453743809683815572479844749782164523410744380979378894640742103628 +6367297859579910614284261237130263461592389581599593438725404642412171244089574507753692124870352648825973497683300706972608091 +6528879643690843406788232137812536357500339309765626622435903074816275044864634059559694905228929605192781404349845940406623980 +6885914653705063359510874305420852766939519431522048421204491529430787666838710231728773489324489696476746687650429532899868255 +4233440617969212981294448839630057962261459110072028596849512767604956758310477460111261843845740348872559542671712436710774055 +1508618282454022497140169566835479909506224026572100667557407598641042136865104440157420255694196211078208396396117860451776365 +1507239834904105526832362883413042406464314583874137343256081467177503191240584039921334732134114272817444512404536172921303844 +1462051089737008115697275867129401567569051007638125298802754342170903145796642346081652330045536281219892265708368809837219879 +0453685864744973901237027525243434738854537803039815016737120599127474654581496567442710142699846446910049480896669404203340348 +9346689990227089014642019407223078337725269644715473933462904642091284492249290308972584444368741981346317066840314608857364424 +3692842182371137943388889815198380579665396475153546930512504814795832691928659417816231253704717088404598973194431677726441667 +2526815319204901006066476270701860537782781626529137506566276977867530476935375522834441559090476111355166836264357254224488482 +7009126058180938597916312502300245038383750490075362057823728327394777642420436778059817272275806207554818391187127829756177915 +5110816584935888048243109800031765306763187568267294316288420157299418121619714954580527036028226556975389796598626288986624596 +4019814997533165299992337612192568036796309043358811887855691200355299547444919142751489527995336846485123932685995757878474429 +6005599839728025790953582536623365308961400544193243119538823245141014368431208016452105852057876883877763572171550179791872562 +2500356597670124887859667877622171934055903916486360738831160634956448169247221568037938498171280531058898708454833291828495747 +3109569215216409575065435532987897643346725582420896049058159554867798859087403090846590114662541568279219050551516019161353868 +6185483127396494726368946515385004646852626975268477683334752638226219119476553266373574505398051454753329418244962919295715945 +5344724458607009080560091971792387153222793929070066348570583467465266923727598378490443838259560132593811322661974372369019403 +8458407692595863178782843772482279508788218196221474525584418608306184633515880367399229446502766746947115360332883675953446527 +3121980707768424272697486172479450164639268792099956499632892432254994295106673987928794494946432347077043928557939886000171261 +0577050269924549664323393933836052936324274196559262739026908246179444460993650264121746529145829029909925451248158087279201429 +1182711132849414035693613289891027291528838438771406387710865605582990490514444755922132927296236610194750925496351082081251878 +2109102647103389955876851996958955042517697886815050668367645651505615553003883543425993431580868653965842109551601532047774323 +0249085151080461985544466651926919056622702235431604488689367549402288274733008707563604434799848038664792491456040962095929365 +7278999875589390549123015596410656975040860435505974009899756364318118677742869501641184521884257988494124371969275324199080350 +2143525889485226056728303512644071737922673006177865168448238248795782597126416756968158704999395137061079007456174899879867776 +5408947704672811574519430929617541968994408425732774912564950806181733784295540418756875998713610000715279835936333399272549796 +0046618750251923685648324487668060245166359869535034556031423916249395766390348943983259976779662337330101459084587467420377611 +6066406518226332392745642377882173038406100184159399300600456331777383902825125059202230966872433187523955621780460854740743440 +8412271363530715396852912523334867927661153444730211881314493834485030033457029175684264387115709617680689679312093978999879040 +9657869183165570826073481937962858496613310115706782423102371799566041214554100144472536538085009196821977429400245149738069576 +0816241320499554734167424446072196247447636657341202004109886635882777290094614656786148313850718338218604613751038747577375251 +9117319836426478794488912868694112364734778843674655117850629082666869083673489067903986091210347641126774671760575626609401765 +1008118532396406153376026525242599592034858556342525818569312902010080131834952260460690347206525525021427489641699256115948329 +9403478796471636735520877378951136546880804020374976748551718084257630575582778549740221982719791953968237118454633169542490322 +1286802823239946142833722240859834876694232965252258418975725767245437772637447581851118844815831626565504328510567619469003430 +8292609187732000353612944093829468343729520826414837631688769850723905562968822078612443055462285102551209743540366646373935564 +6195307285792520347832027057354006551256656817339152300843201008404344101735346974655890347210090580920518304844457209111402489 +6224596631460367862650134161563558944936031773882670339156933695467888462978309028912902276648283451994600576418116774756356786 +5472438323418087607827579818345851863545677098783823512135936630219825008658803838212318120192887213931109375285743757983871070 +3462295993623789324823077363153428519235539388072977171154971504655245868325675529422838022778549930317329461099449011610221962 +5200838592621585186795894021330350557903923860725207306860907835414568816984390887997385942459848064357524983387010427408109850 +3373256185275744974070989354450321389766066372305525693275351316959271980719451196548752519719437550201627682282540860722912254 +5801613469773755028551251430496692677302091408721986707132740326731209806971448849117149983546378567141073671063743582525928527 +3697803453226110613863663691507092125649804560354539788548901908917360870034087584677937779281144785927759366742473292064354073 +9963717672138768910209602639579145502161357030053667109664830302862052329208650435316813897369377235565986275115675225384254363 +6761344873607397157287860030945733938977023836366529156220217899463330627464716487783276767734588772780285540219618726811099276 +2472361254288850026102274317845905290678407546967989656293686796378605109224367092391377532276351514638630925654015471910946545 +6893284960359512924679289275759756168128756913637757439682764283483027618435659261867805209746618113123871290501000298928987223 +4604230631532001258913408929361874112508575772563457725381109205712576762498230243782724863115387239822706836827854691589225919 +6353180226120522335706585585655463329450214403091956830465201717296254555917557791034667593584676865134148740185250129625269812 +7681527901091326113048786722462348325784769942754780702258059604458735745233803955056030921585788629283445553041509260093245352 +7309220586489676352977698261496440058970947266199089765873965038198042811288461602506269407874212537834374031596895817266480252 +5671380781713371554805278200673327258182545730441250958579106569242653773093761570049297046722445961242199585137608405012572007 +7828486850550976212754252064205756489990487555478160661135559387233240543781534504267534308047400112433806739577979094229507603 +1735652617764383585639984486995149658650155492118012228612717608699086389981358293860866276067465797190676060353147557719908880 +5809608466540093565672420660893504333126497209645992122475383330847359820073574627113129012380682929130905963299348891171922548 +5002869804918377770158242860178234038771989441187910348213084777623351555039769099812484337067510542127104020429900993982081671 +7709207109088318832932845793851072838104821066587277352066221192004565750065851231562111055731251503571280787745188052871173891 +1300675988020851714469995143129598445432183378365444739669948446307812357058373524561004834384315759687175675721550162755500065 +2986343436898030807360652672895860427883496016032784149587776588259863806298337541233639409877580883769111783951961729854269967 +1601823981755766290542611869833989750039385604668769648848238807861573337182702195579127745245081404264493592362053024115769418 +4107102716492675980158632893016973041072334043127798725365792088221597132425309628489541173280546793641323966773146909247868755 +3819683658930608253697914654878640066261793324512623727802178114862630064529284630973855427311675132424529284212748792990550668 +0736093271588205855246000014782826243591602482151161574269309829561758900770381929089531747951679154943426373942743857678907201 +2579391599623221851200754933465381040789651168701188912204464482208095449667962715138795642341183881460864419304811104815352329 +7570162647614550188694961496075537449438462429576197619766568228578156257291119319362788331980002153009584790725962518357588642 +2867301400919047583578998934993306274019943104502333712247027666654628326692485228040996686989047207277332133939435024962426111 +2800118335388070264161106773949228910777957160444174345990380796529948251126974186339060998840435346432168006747571320370538154 +4476893884627071583698402476874552862205711529686822374264860104226019597554331616506435529408791452032863624032022777509172662 +2387360521387922188586138212711962601419570169405135474539424296757638201501368935895796012267512214174579198583111876438042196 +3207944223864976640314026742675529342045001292563111065130367748865114709153385711357232421423093197866403543774881366812947410 +3024015733350974607130082705448223007580421749047845265584744661269361681112069541859763264352543531462633691003101294808859592 +1169692162436061367720119759930066692678060291627161481443668541634202666603165593913992477700731194838349007676531720171279849 +2751165267818731079924541076614810221037738242037928099580658966017244137214097278777683446450716576425458106456674343770827281 +0396856995901845858069688500739423611458585131275613789773338642449531843526049135797567793850609374983112677866381777610197584 +1531087534395262206112602286543697479767208631357778942636385138053327121554947922895001619615466561594121066079313165604153569 +2093692637556395792176696765614996657813671032355821221008345920651772816571893227375691474134293122057079026746734194976675858 +6656377492316720744820927448196522955924434793647944766092359157144753759050894426200065451501628415477521405487862719508590906 +8342026934212011048128231645702815266555959471653323962526192450377377432253217447538860983839942343996469379287531744331673799 +6119650763732836889688973448048471041648065382987440098529987344461076331072134069257926150018448979279801859696456939951283072 +3364756734455969829310248048507921203362218977909063710162344883654504743601191459570909322659994691353369156210663856657262211 +1878353636597017372001364702577584897453891661198546393569651046602221858825228640216854117340540870937305588763063713204215566 +7246416818039080901692045886943289879346943533530756863194707455582283075691686089441132581902594229725501704594144338085541945 +8508829844172609725721048474190455937918008659794137613216237230265268215971845365542440688734702896182248548131537748110574617 +9749837159931616345230389269791318525672049015765330922830030005687071010042562156668767526231766645728311230400611546831340883 +1042438692813477410787838927504883371438184111986343392632454033632895691282059130126453347410816312739668264804223203706756562 +6361009275683200524885847104957823290376975509915625274519396770442677593644815139040216252662268629183385844801360269251220612 +2229763646114181877287927118379174699532585850694835097303121614336315830870465640665166803477820913104310061050902102789844598 +0133060603914382154852269620976632642294770629603193082836985616373167078617561223317446714275063351779922957184196466970884767 +5219496051735919324804049698971068904312201333312370591154056704186542719979874616042547422134608890139798422893654424476506469 +5309445174646633737921041425428020908437429638540244462052129439971214228921839562115695079069371301404828066341916732725724068 +1929599109824380789046935363381838679112048207357698712063450869013602851026654755608800829647182811384855047709407638244169874 +8711156655539521674331213489806723157012020673218852893220562275184063123762927470825532953422135931696172529627131694897868313 +9146092529763359061139625016856546857269473202244951373230403084240273596615596144680054956193346943461881907308435567570589732 +4427806006379233157431109769793014446131865605460452718401487086503103098553754737499082181376863362004474070049162679495304786 +9908319411586467759323529251895892937674894302534759585448543523471481019079407250526711177578259561097423457479183321229451626 +1131371780154610377491766557592293428206914537775417828678921143427727968850975607814254333398412178045797967112746828184963972 +7463947060005178881443883529993444944563705484864960478232839522887114302507061077267547873033859704180182478601068253214443547 +6363654733262110562720880713693267188885990129857236073837004378034866347649760844198517835526780984480422568814264124396245788 +7422184144089713717140409869475766686474514248637954570906655127051840364408955207306296810118720773449322716315645914875117890 +0847277505961582786741295462154437342373825998662718119420520873688904858163025326891259732777158377960920534957757187301680750 +7127332835591365255818677928303920688285753363566564622227243735455708574607542891965353908456436608550001451776173990862992161 +3829054399301597187964270168808949938169540225900414534726047012413120885338161369990845308813286961059592718025627205221770817 +8092148297686870086401035932812080011392424084497972356658952923082172309746025699168011915919281009722984056602687718337362402 +3027398748877048907651150695381083835937725004446873842854430149618594741819924214123640276762016797602129360641044263480412772 +3710143070992584709931252199044234825397803955966283897640299886246679348943101389030142066287790993761176511866201467864299707 +8525777612726675246376342834401281272453399310074719106459898269090225065025922311415205451670630902470074176854745326883762644 +8262544278833799829033118364917993352082125138925496717970983928091600634758984244372250880458595883955890831596406628913521186 +6897853799297275638235876552327431216883341950745982036695977282177864189708565950522537254131161076036929940837531981536512451 +3213748658129585636531039879081929302798794268346651462737129452506002968774313717689240477017446831448952135158407313634350442 +2055874845167569647361541785365745319937172123917988776868257061731415878495755011208403654576322652419771761741735204486134128 +1873586980474608471307789365974467503495691071252149719435658541818854563735190343177059527503193288117651496365565656653307241 +4168177432498780914163093904219938645313613562804544457122557236850377399062589146156364240794704626265205228184552480787389528 +6002548119488363978641919486071068284491204688921592270835983882131004647117906646204653024128798598225163921424999116659512574 +2157407327085931030684434640191280950835140906873052295703366686157391470840100604628128222870118102675339834080247719766902753 +9516075696286688385617312294770633537786940805769023218236327039881513937432194699645278901401806327344576080861704678059497048 +6871189362548323056223946109945917378776965980493422695153032621053464957232674797346887230723234348508681395275248458966927875 +9336780663866116286377027773514857490216582712388958421219481975628619943979311778012113187699153459212550397452631591800634557 +1884225357321516913996913776484184458840279457439252830670922733976854064570789902403772321837879511792814698370553104269506613 +4608342183802630938543364752477985342180204673563980704638103829760160093636516325877632052705116775395863314477370432360214625 +3293602222611238360052445923968119215574068058661794694301159396049393023879006675439325347420522015724464971518661959121953285 +8043528468318108109054413534510021714964009352370382732039845180887830146914560592175998791412497342760924247174793888155615218 +9953136600515251517270114445026178472065320103271749951560132315486968402879259134348096557033081980214662985678737805295776016 +2082425869735558279582276249486763021868424618618158613101945224876744522204200404218768513612549469324132911065247448884834161 +2715300790048270780472694746992029478410340286154036701148546125752257415749783382221561686531788228065371707859970836437758369 +6586934570724918966767618089617174929582836792685611937969070528242452974277966964414407164887044841543253218406757856518905749 +6701113102105341541135759678684819193668388121785636742894877294681466156143067331027856246686661690573758062813040020779144644 +8573371736029851148801112116958518768083768805313811181481300256743484459392633904102235738743377368774016975057841718194084994 +0319902874585606893329741100525933056084182554392753427931707044407179198386674603799910970919558297811591565044725137884178871 +3211448717893811033719443147078575334620516368575812396883003880411447512133976398395409888756416803178312866623025053382610669 +4561353382089346913344316837392847914820218419836561651969690056195804908190155708078402986544580149409461279682788447362287221 +6541324108089851136771616890125941444979071358059045831564141967544312392874032082755961068514362364886944531733042486972382351 +1947537457345366294923388536623305727166280503481566200274732091551547741549840732121496504900782994752246247415294786133670139 +2790662270964346469038082957923935596601670431239525874522807293008957620170868585946546458722568195100218646703049798761457890 +7207291855021167963306163201383620528292099344156171536132565162121580753163039591601562538194503052311054883261440201088776719 +7081508994123346370534091387062163390235318525916429277771533969354167555829004274478142845946719915110847480617275074050516329 +3008933084371763781228021375479207498806120584984898188493731541257384657224732160257247009445015469067201744844463068899016846 +3400853085115213814503196501389761175483359326430453738990669485692964833263969136641748087708764716019323095274097377414065683 +3262060508094820191682139681063256834961825192399541866597766579614721818676700322392895466758698832928065782064894903472978312 +9760920512587884215163543988536526156574865982745337953096380337326849623081010538502752692990846114093305595114511445577198415 +4534258782640418619628264506406504403447005968106774436451122846867047076694261073542029684272737494317052196031968617018760037 +0005723042928179923030772189645554654702738582913437716383318676665447081003973531059059732707456286646505439805283337167567513 +6867755898727425848664558154624878031288945357600827739272457697556698844756274091214882872082839937589030308224565293706216309 +3727537228724168866963116807318580861851195574160478712150379828696545006021644250770529425526279645439265900826286791538135930 +3153997389799559939607422587882124977791743840140703583207584629133167628782910497529520542920062933572958127405711490497737856 +9067474435390875739488758031979945416769209293461704806750415570467527973320692921500611307072408238071671039355350126215323195 +3397087786775809367683818985726713074834263187298828458889483102002512283284602983872325517586754239568315454503146853407356331 +1156217453455804570907455838506537271360924924818686966590633031323668379608702859282734283827376245634390166587981939981633250 +3412106433453184147582934702569648764561354607002098617356742259624760387826342970038831595863063739528564750876654025034114351 +9541719290749487737141992118412804491779799111981476193385533300689670528815189491502637221085072487226634588032661043056759668 +2892064967147629869694614423619676513067951679311770744515022309616589783655846699918308471661872732172650395986258940991120106 +5144184865919978928920815222171229181997238748998964019938552954342118630154574987629428918689878418879411760377091395290416344 +6885105521575830610256425097707534533542028808834255387318107287078639558846084308180272966744285679093270081574095013341117481 +0825712842225983699546990461215024477130525988490880256409773994103713908818390693122587907689102651005737105310805023272218302 +5661584786775396459681434880691492087318430940497979741425101788956553140338175653002561188152737818909816437963790481976018401 +2333796064755969606150287094100220527676969430567702969130345130599373183629780646448254004373148786027781444947828389799036231 +2370159800594514645060430450080996614777495906387714719376787944968432214762676658329959973122495250133837956302119421564977403 +8812738504000258149359611948440268119800977088619535948947126735318099451090706379617772423943777604512120621890539370786597632 +1363253012488373680482054568974446439190725621429231276569324135313694462859720454323916583978487818838914751980359776090418942 +5309412448985837938866251311974802939648013868273488976753659392241870912692861488024484868367836148433592247524575436350374927 +9969514974899115014452925807661936280550953879875740514792224284344537632692977097082883985126798768022555743127429841568583436 +9127480468310694537875735497900079126170848463529624094796935948030072227221587835070990509651876620138981087391650544865276910 +0663274772751898865701481848764433338510687274568588217413099134231887382122648096865873338497066278774094796953376828006000126 +2223991886894820013487780739895880221686260123953688255289857284508367990146047269272253817887508457566736245360372546074724620 +9022913659536358863270572069247332195356175380482645548420742634480093147625717187780842180713151897503373416116801897541549813 +0040126272697263557284035143648280938089213881029883862911870694532327995219687847437429389232425351493591671711796587562791256 +4017430425461987380494086228031466897556783584467938250558585791601061306591861372175583299944343371927286490389555179774602318 +7872955211264709896492762009841470583148357109101357998512705454425930076065324016499469004837167664624659717653859578461687235 +9667310959423600740682018043806968632350609113790915003388710593533872466670325086871976600837835610439552558719613595783702236 +5280944927395295859052540423240381366808883628148042644568662154130067762884915257137995873057791352572012021048384019627522811 +6995106927652578556591681036413486142384921333996800868230237202393167717311762878194321699747706337864446498735182883503516810 +4894127087101054059360113153040095596503789486674685811347198640008673703554343464924312512011781208631600506972544379180978661 +3653914862578195810106428140030210938175294156340298752901536901851573154592330206774109827394766622168822827213223926407598523 +1752287994131637967328932739154812431240663561508299992489581255396838237199970478414918524473397520840434054432691053868383392 +4475254299662836002489198880413157763837585826054191105485073144949660180125415108179810953128183235091235263489809711965370068 +5618413924400018858280428271296438533254641173714166471746748160577329614446433367969692047250196643164558060988866789634419083 +1976673569829377798715474066924169354140929130742194843186922903877261219583485904128878654906618865153383966440192899927641018 +8533161119710252096705226091491715680664590252842043928311396400578644885931777054299394359376613317844464091496561469756448904 +8685184190096699452877940339075889148354699442923532123110999704074604303853725375307368452619373244845552599196580274471194639 +9829578543429362536437207266353476212156141077734490396691958840205815483098775874456262295189398128556239354143693110486242701 +9970409655619169147931181826146996933740442172396548865369872311637351974239103479278809874040155159335694067867509786798916993 +8934739229131395268686186934227620999682967502345755750450754377980375485746895673108969778424574385778481433357869228670933712 +6403853140387575318798294898252560753364466868740386419346298915926350400035543680556378607221479519818692502368546406996064171 +1053081251212324305626596587814428006013451031378599825941680949090154726066128761151070261252589982556578267160212779194125716 +2889755089191961889036525987804900491217472837363091046321293846058934856253393138783914577483067599584740608242898988136077107 +3871240659195668412257855781781704566864660291767024614816347476774763467463583115867939922590382680508695307915014840948963808 +2822134512303137695995007841922537885648949227095598324952993245073100048846948118672641286579492473549786447729014913418849577 +7115799710686817026544023974604506965289044028358314166807497671714007777434113787389629694840769613243261303424414900494011898 +7392257640112536311638754807439431161718219269700164766558394503991923942217206220355994391249150834799995352874004008637120732 +1738169836253384974782709569139552429502554327170357536474522232448622509720551967534903770682965229229310748674655279819812001 +1013488426299593714852536878758330763717657950972685023529624029812092380027913386862035092137102088651474613519196497838916967 +2365712331332348797178741500614741133803176085015200716152828860157706088237891249512432828832383523202673780925247731837609823 +8343543339820274706718254393402391405027365642281414672601521735599554959777377014041823630157212633295524276413560148004499161 +6041268421639300061742794731401962219148955309257726102832738599350862047986714418077392903665425072627420761149419742624568473 +5903163798089327161860705456299199357142261228683379549747158444438483324626130181126533716219751215937033507622299908730009529 +0388816251038799487192843352437014500990877413652618253793690269472286848421450299377527429839922951807455986611893857644960410 +6747097789593097432933016398117542211434597285816625911203020562919342940284869769635598001370921459163969583961849156409633978 +6058253598655377264266980145151402530578841715801191174134703632064724433621375404185347776838645447066321662228541280092856110 +8706460045506764337863851824656889505997963004840404222919355019179623835893581428684583324979366713626899343900308968866457307 +7217115588869155277775561876870949020701337732105962145058144137569073157203270186719489063643062743100706189008644101702506879 +2134117466607949382162669449831544904500037626615605917490932121868589822502449721652777432598078974018800021624932345792700743 +8772537472143251033025302537735317107162425743475413816038506301465925279856675607449722802889718409431120703912286958510209941 +4868004080038983558128526716248086993854257004538122139718459319434617070817759239833696965506062568522567601751636677225399970 +5772707421791629107161650375371738241195109244751411366251362152446624847548801413381446956815082676765934768156267696685640257 +3337783332137332131442737895629161974159375156476294489620941247396447219121871778030180022268049183913926389862808328015747496 +2718665162373177306261228085712460052227574664675638031209424855973371465310593845908878207798419801473033533102851536608498988 +8594650062259102571664509669089099570629940328720500583665382040942904077679708565306833479512196713193874213822763114277561821 +7763922398020531727099680865423760176807414153322182938644853758445162559723450312367220101572741122819976006288279862925391533 +0609418074163954317311535425632639460543917210847556877142776065667429208580080239400632276304974925905306867007809952150307911 +6324798067968629831501975773028726696101515750158909358262168518496428350181893495977033603403728917956907772455104980623840611 +3585603421384321409739580272281986370246357840985885096013164036482124665391931947880815558360671475167152552411643258730493458 +6674212278514790724079384829207583320586965949581600859999150201711417012504831581096750857003895316139028753798041233239967350 +0724838602272073110730847467620546382405164306488489590470927625949743749910925230346070277479842393578994351426637786933239473 +9758486324598916254826474547605389356962763994905059896268073096288839687527478615702716618454224618511400537520151578922366430 +0912492025822760544865884599694514920137845684369437041888556762598262707248611887100015044910247510922363893211117566916277207 +4970896832680655065722060515676245073680441413700428191825160119923053062012241474629435368642857468208602093702394160800952368 +0401643954061460061469538963715677841733067884978557675498904356226239814257513498478706711931106662782912959490144875688684392 +6427801429230014330280067244320390813250002494116054667882575684276220510978170909405937360209445943389405620264406898441269151 +4513961131947927648793953864754056606418638015682203461287515973198501950396575668694970608763112445843545620657603445132209084 +5756001157040952762796856519600056686459818285012859365932754153307811718432937457957972840218047257233193928872388202493970468 +5745171294650962698731398420701143198082563097452816181021021324846599968746630383827244381072653879098831871344995918422668994 +3440697357810684087609635992103602934958412129332726402190737073092334237927797275626663021730679412659760103153021222137031354 +2561873488408007135940986273685629006346887762950502897998328704241551747655409051903344700915847431066705228403387636950645457 +6677547618819900422307650673816956131536670246263056210725538571359966918216630406962626983531012334568659195035619701341684239 +2420112802529724475156900639848021386946999992189525298915178531829794178899375316321752347503745229384277622075513609101448077 +0978966580118766906719339709431650123507257526495690738543102142511117335171869720713914484644191091368983835287599510980274549 +7253035688826486175835885694607345249760996565501754434755643707038772708239856393717371875268895624588946691881982666455004955 +1229157575767360965832607989363181678043448764770403987112238108580504462888836301106969202711436540526584606392030520555629817 +4303800215068042550679859587110369621335305905151798020996167677493237088394878180209234115765088470219122388293468346691575131 +7160108019683761926270404198160152789263213718306724711634865022789627807005870451606075994736870542860235641292939358601690892 +1281304446363067443221484815128597038007892955460785907630142069752866701460026126386319238710932447939095864553352289508365136 +0314065667002957866858488765937097697311458242916562733459459218151551894102884025238852720369894804551004088501646041028776937 +6071040163745909418176019453877965601147459065352750105116872007917183143758403418504559900007858465882670700927317278238845262 +2057864431401330101261675645065867887669270980722933655585800822914522258771084932227437807465603988558837727049424395413086192 +0838403065062402417019069366493917705353727605973078124319419683057212743574597310780151384502291168595626908520023754345467963 +5237595147390251950945857498093768496867490339391087595954081288707206848402637964670896124834883491892023218043300933428536303 +9038575918527245239784697127935511565993493329345587370301152888215360283868124687096008034911588337723498130857892917083893566 +4326309213195337889918560482484162052931169753291241688638438611871045719430309380350737659700684663483713556511824284273648934 +4109675537859830665204217999322330504340383531423575355281348688917399955785319877102842092110480808916641344634371327017518067 +5976571379562477291805315122658077767583602698526304594638663614898936945525476463455648126285604724437435096863798048639029149 +6805684460338235182637901097297785029718931952225347991353757161498388530429639352729227333463916470311358261242265252454882149 +3713554069757475756235174202220726685159145970448234073615786797869189321230621988473892723703610057245772568034893831333824268 +6384329807418457095553971699330059290079181553384702995369891469896964859336812164228972258241580918221240870171611342340166996 +4238496467737330476039022845048701321182330582443048269767680094107459509184801830653831569366411726902088279531793681281083241 +1389317145760821195180078109186551396173257466092123382087137757380977821227017398905747777928583603397479220222412134551964141 +4172433396133877482307354312025003410197620920424049031355359918529883398507939057731022302176139038905017200234464598134643926 +3776455852500808911518410528880591214453698534859661007977301367112639128608957724121376845610991533136565527742397972171004531 +2169236758277228509207094221732914621136016542832442557119974462763405873279434363734315812872035133971702886820591857401769365 +0469858184126033613406108887661308724723319896168974862654713098974773391394034562612379275030863451007736429954454094411166680 +6170337864185847270755623945219598905895923685556701346236877669819713556924060508835912462997027984549870490684576221788467996 +2684567926478205893568909529013411584469062508988939792621227026970772272619563307265559984083650571137624804538833124458965412 +1535835911469765914801129303385752847166878267150358870063623206823946840223186885612380869419340578863726545596738437580294661 +1159345796279619020659424502037780228138063084481952522217422192474814460650570150288813492963026126513178935693001157815213888 +4115255308469968312956667592729481570942886979872098274544235236610800153069537830562688683755471055934166523033112817689423559 +9188936491751459321231439471707842349428821745374698747871010980962280880750506886802895843191246598782044554283344177438872823 +8347722755763795357915434510354216096329858917788023158970394599943300789945002208151468416849121549451743359325575523192943247 +0386458822265636778073946364464754663546374429692132747455551460488109624863176490317455770854516391720009905789081791014745242 +5441057113825577288143510443291415701276478572336349873718556812697918623782132025707841689574266907977935843620901112527314879 +0138974051576176017602134330816942995720273624834286549509110307443436271222852721817459586302582019304440451903755250893098006 +8714580715983710618302573323538613568787984646210126690769977327196476888780472811559022322326994280966501214045283109750894159 +1238724204438864658817267118920298143255297575901621515081809762078906592560699017605054545611366055292052387838303773046414151 +2117880548683675747162798543936995265803158300313026522266732130084089973850929862579530307924250791678839458713648131052866542 +8688866589649054666206289287368847639201089110726068276561807440633436849032550737263946103998593920752309241137169715090564726 +7992366978913349109203408811669845313806318145335482602504586472079577960741456598112230859758567892658655917499886892820156294 +9269289344377943028314943737975747440225464454099585770665233191895455121597963497469457088101021828673050970000696324467074275 +2620213720505096912466481021388401048520027588680889621938076715558063059090115474427731634806419367474059903683493740456083416 +0419258099585668206386510983923399175364308512576355916179825772896253917529784626455469847416203745543857585138629447495124687 +4926957394763419387802807256844093371043881188012601196274750577103452141865582262123425456983325248975618318937642166941469448 +7745531482212763608085822252151342022701776876422939982991937310338710298056659311711100694774221511805463875602371671771107991 +7622297042918195524581284988624821755976081656742322232878212092814401063399047757727446586120047765852193643525978220488403806 +7694553355731009082341963145016321622315287166497298675517840848325124707270827181601834343475566034855078916693975601380159131 +5691081048224248376219969516692299552551684485812747394911625377626308622713328149116932634160209525550974738125918251287523859 +0467965216958213222976523551707473542482226303543330237484038813545187688169900375790194437313184877958505335015185533760569003 +9313388170020500404223350587859029392425277844920011385363704844190916888907282100497715918895587428711392085764058004531043856 +4805593106127448601646584342013400666164278454947086863410933724954207481882298946080783397578430480735942749099815836074939980 +5829488935963766897295398693976065473334615080031996490066697080972996155867227149556928263586779745098746381741955988476480256 +5145688238158921108920160861540066499070337742408273263858244236306539277333029376512623358171655471201632148566471544471879593 +9921214229435926996318797357630113668084683534980450540625624914828693276400997437801995497337699991160179152196384869788686202 +5452970283364847951628816855350106161884215992345914720345452469417460755112437907196741570679787585797744951997013243245883760 +5291067302501955637725509930308800308821879902449540996764882569862895237545484938003965194362900224359317192536398128214213941 +6161982577027434530385173942460521391267875161999821213546453369930955209737128453903802184733420666743600784696394324963677611 +7259796660195828763104609417030954701404550411856256961919298349416990278446333820166279192694569999689157318159650815071786127 +1409252775006780923895660512761218733793242701073753044161926851887250329246949307610371367921483913476091276544943910212219648 +2024883815854805620725646102396569531338296559295702755514936293184048845778213281077499032309766928238274933799920207837132838 +8210747670883127249029109720500900846638073842072735692712299242482036224417598293305373767651397688387681839834164347629159924 +6849912340362293051521285112884702937904971535989594393302698489020568178389718625627324020502329354706893589619173731210032692 +7043789553739208010357759707009160597258827438739048435319844873364203025319025627611147486361041079291073784131340379802864271 +5630517567000455573262321447961724157507366377593234074637396775521413915860857523966422306759381837353927621547514858375348648 +3907374620037626404876010563369584978900791842626728009855287567645993864184962785602670934980414501849977057712840353440946698 +4146385141059123870591051497280881668008298341992956096236493101498703482256305064373982480667690632647788868461225656591267014 +1968850563894509480587740368883124473858275653505054105229249423142559298770511499954927552803754424222038587228379761857441343 +3593066522032907759239532950409720855609763959255411331452747381650146615224156577229227092417969990679672022642682488460230666 +4777284684581020086703554379926122352924845140673649610422757744893763657846499512591483929572896919893136740342514388775709356 +3278001195584405224574340518455727489287475412287946187833805383833415237459010850315390550944653852931708140442412147400013062 +4193626826135963841422031213601650476649922753686929704940497664287967482680127644394472418039840709944058706759350270065888771 +0496707764123239353916969348556102234417341754509205481354601177831190831135133718891421262586922485229955848858284322651713550 +2364211115463126546765721277139380044647678205497997698025656179147544409871931320460204274120159204240964415234469912669043171 +8252385686403049790333736390573303996823129984313927885511658858105668489196273816414521496403236288238282761677474764476093781 +1249545053006377773498004690990118213356171931577992909398589918937522320753583797192061779831406344206481621920250152295078939 +3709843278222111715104072968292537550135689257500851260250849491755576197121702408117922267107801447424437466479647604658838505 +9854374828488017805142065771759506637983062326803172669751646781389776006196509659848336558123328010610366919010236885546271093 +4449279473317813512456203547757437510023295240020059908269371826627596476653671730054696042131496333128833416321765418914320719 +3406255777740484368806154865989716577772954696365667763046622581695081667791363935783289021366520297269972594689838903064379670 +3971992965532938884360604600128828768832371480402617473079458005449743185395881312301575878592825423836990732749119210579499565 +4964414428994770524920627355735483099389790803639808833122768311697123316450497548024460387672711368021069867385676748771138907 +7272947208705814863061836555172890754692453812368615959884812383294177521377885043762530023437311935454983526848574910569784985 +9715236460972266941105456775641088939352379849288420663874812734503948765928906653066695278788734445614220395365589281481052985 +2219149571492569121653088856413977750911860290899252411873082297287513781665034941515671551141663335672214686872998009233611673 +3252445083210515790502427230581525204628936179674886617751355536540130374691771589975236704266655393863956951833668294842751553 +1066643170812591610511115847160870442604844517253754311969780356590045310298592533473063715395834561034249527692096332630663031 +6094678693574789972643719059293336351890222699519292906160704354339193005774875964485745420773135852897610846364640226020416376 +0078821325017540745977661794444919031229533952549930041702337654768594140134838827623154832919375367293846998623573154781662214 +7359805369711475680734455906157477769170543397898162106347663466476684547058977605857152935135397794083854436364067424905370520 +9053319467618246985715935346228519691376765059011804822695041370567668817595587435020117179808441224160004516561911764361051760 +5279785887324494559284533174001913906340668600486179364934683183133265522486775587610826495582037876134991475823478304604306409 +3941979376926208894324337577802341103192284179207690346815460983649049917703085665853453770292030524017689837132097566609313622 +0874696648882529341619751279943169854047021416531564941329234495433297775450795376776860235960754121606226960482122943806912723 +4403142549129181418798689888112583174439363438245360666469436690361242515845002333293117928704700704567851599171947638983428803 +3086178172887575667595570158882926151846562563832038255451988723965663322118638716096644911129577785347672668155756960077259260 +7016060211620447349613366392265953857687262706207804671997611538526738050334785125634177152240160503249217159499633364839660140 +5489512175142718110444335510020717260552562939322469369407097546446468340557730058147393474938996409762845800786386613781180803 +5316893398694618358236834304108186582233111640280225864142166804368748966281116139163914774116490280764125236118841244780654205 +7528194334785274348270807743401184076977359005808989097790847628594537909545137710117536079654129720175240043680499382931315617 +9670385576675265565821666921824539041489513174876467496206200652157451658908879498586135223489695875901066307973744711118179562 +9293554881752014067237144247792025493783429506129044867060712220683356640982811741139549073541449462843252209357539966735713385 +4766042719100373229390546845449462827735583320023428894346366740071025652090167656518273656932135482003817661060458673642777968 +6301526335239349282461856549816100522037177673330915447520746670914149246304276129215650553527297720437502141660843474787824161 +8981954968443818080133345527259601739419841565729583936192031325311804839597158161812415269217752774138988523259895019553556494 +1375817110090726843596408461202879175175450080051006863993971804570798947994555625585878024069026053117840656838335462247825434 +2951238989342425774096587516368372195370856662387116086488893532196578602234813604266303300411633704142608233959411992656817117 +1078366109359744038481314864313226318060249081213275603047710120330297564750748020015170091244280923403487005151336327967793644 +2590644710984063836471814396137903631095128763841800600303330827770204519245855428525596893191150792386804495821205935562805056 +7587927489845850917488079723623306056569190541982982580007185500509384196528055437763768159799054085157690272308144066104571631 +2716691447638059359782976991714931875589838509515317643963169052633209626064370311932621633892908726622398410656122098501462508 +2187341550744516068766043103052788709422923668066123030675357066234501008353745371362353282261896383609682551818558419836467474 +6386821883063127753430057004971408049021180273353275003607608462963509731380707112452702494852068512020333193164120541304971821 +6414619855917522699462290111082170337250489881403447602549155371621815281167692543542521085921503446688385053019234090686216914 +1514251753750908058132963706101498627247758343884090328099690893144318542740931341743091728225255850843917610568152776796003007 +0586747600603620622562637108763020672642036425806055677095355709416205188154703219135342081900691181922293308472380603914641966 +6943882703466448671671347942866066071729197097929636191832480851465967168936588557508460013143074231989501890259153468168582090 +5106944931177904690443492972535629421878279108027478056702348421231814228319595001969285380236108675594104178148223368821682505 +1013423077468134180344202453328649263556597230333389978398486618230751510288831048067335293062077986298410020248356624260186784 +9609742549854821633067526470397698241682366743287992123678759953855431672859515457048320102063982700799641338555460645670603233 +7227510683690963888209842085118318517866633639207568611885740247627869030070367221673493120062595703412511430540635725690733531 +4166904720216643652362019185541125336279973971095473810174484610634739482031555699998920445667293351815342116856019765913024721 +0204685598907595748165591866024787377707833163733227729761204199756529370097242687722444149797918578951904826714174399654373078 +0029695243252514591043935308120948291772608227171634548103690625246068887952914933102467669452782090604646361027594958463008648 +0606043910237689889945195721316745105694059044358961433889516172023390307325162841023925000976730946622777686367139699337140288 +6532770425227279669614506342121365572660042996371513972011473090716295756052824631404973053818570282987235859556502147507091296 +5784383703297394066201455976072965897350946662861440804630035111722516300582625065762906422553404492330383168345359661866546635 +7755352359361297136322168309187217709956128684689901207140385336402261861792474715981622821266358097679527860080999492143093132 +1766368206837633471921145548103904997241216165904015409300725066451342178028580494490424756975699571333186681161146526160888637 +6435676017243653308531673583521916536301571057843651379516536378840317706213255198563089909258098489728121633860286779010828363 +4431604721944445862183599288814310892377084499055111017374193726585680550914726436314439993180521094601524845475777232173548652 +8838913044786662722923315829866963645973025803566829523066407753524509752981118743428995908031509623037684132020735808440759714 +6831533201518779101708294838185079163952906812726056828622166710833228863087344104415454342957894082410432960146722394394312810 +0026750412265305432804678135281250107474394107101163961158631241381326874456825116424953228542574854725058161235809819422717951 +0530095666016377728309821374273950714947770494099333703734720428753672558560341529622248519497385032763976784920070775745998492 +9988103492323582802831123529566020729884479858348071107686123450154590877464099472518837056085048970237891379355375854402767816 +5730632914573233372640655739395224654373166501801926999740491530052469701082483441902155873112176686047852502749353152358488650 +2570177044053910045946498496953083595646140791798652856797075918041625426521730745013108556438270557756261535076617672897082024 +8070822196216776547306746987791806776779912117872036753101249436897151550298686891155685227497724326515850755173992122017730073 +8834834453446142095731369167693716173639921382555584546233080228430284169771661090869805523170856183199180807458939980812347666 +3374034628848110181967055896853894757362330956181597331342944383294532319770922905892283292682026888125967189041997170645906930 +2816796708896328193169753615809284330192400628212014852560752820595983573685822703155361815716715825780349446395688288981323009 +7068534005360767613192042797492589713014221879034298952445175191916602695770589539510033643533276163270684273301159972772079846 +0851847960381648176955810985408204753982844887706344041815181406997985867444440863020286602964269777581948956459433973575818007 +7244153065545137632239977736639101263396745129055793110973443976621943425823784182900120559207556057714294191011934647831514642 +6220112071917364308442903062123692544853904143920646475613405698807838684964691297033599042267911174717943845099876701146499041 +1997737579617455507368079950355247973464185824287192636611840482334933626092039322468638416062106707636638684831343949569888001 +9056473674062962705649948566947590595122246493016055906506275120659977645801864949778640366697230510631836906881678697914437413 +9851293934933351435860731679561845700615460829332071112719063019501427379839759911846192375852475742108532990355637732868083608 +1981670764831163220456625777162284911174669042984422665330847096514572430663915704539930674009046717718308791910081803605092076 +1436167228645743999726495063433059497403336540806399989879727825522625780324646087140141119024144101040594738802339934675234494 +4511331339256646611464785947970006064997210981736418145964184001849876673693526718038480153571548117761471182638795979792615062 +1795579829237064976093578272496505322533909223281608272636931019121795380635442871500788593701642237770643966129586714071750006 +2259558543726883196265093171148297545202455060457641437680621020678468079454735539167389894892674876922877875836968284903103240 +5190322659532635977759442475453937445078305277952610411039017125942798503797185204729242079034555967093953206391753749025286545 +3346586896166410527904055884469771043579999692960199777847808583456813215611676699557144873569764821207184084966272170201919310 +8492812746065013433267691323002027533207826063607337577540310499920611687417123621076705455447051576454279273880574231655581484 +7963980271743134112154087260650334248257065896933807620141668762775373128572470229135441642876072741306062133796013873900656717 +7155588355012012778870617504567123068873574015355543463865010611682540901252618661459039457810933495173848721472502301449759789 +6250342428228956319030041026630301654869718238658413832351916020529083012909954188736775565330222063260635143292678318707172682 +6306362489099506339243174369018743275045301435603437084640257826344578993743886263071878533536813315686531934206065891475930962 +7256078764576049009416184871748648772794832357694303718710674780587401830972226331756569564368514137449167545781329158975242668 +5137011302637382277771122596368810380574982751431852882121643844964841634725254405565914232138014950212916177841984286676442765 +0130766950573613943351861223297588274240988343477886895038747758639966388317215304568124673651393253970716618528199527580176531 +6359126988671940758748887099156058290162393017636744749307547677028026325587696225823070273928497980385751428178855356190336058 +1262747164602205208071416052898259778064629199619315166088744120423992672210748461774575909144208599043872184758764391922991513 +1637777143251220955861291729128344859755814246044289400356284088474702731048933137147907743901585132956196475715784684303014662 +2779656199507816022148661387875419531355613321583333187996164541556836852609250800638246651009398874732599973734012124894691052 +7354320128684821812105410562348993616339745664287363950112497961551242247169256027249678322546929448755022227225938009677843721 +8972855810687812606653074585753478301824708270260970161604946179349054120962204468271996990113102973342165999101606421899608935 +2427496334389700886512192278170872300796174251105207296701976843139085003204747190192754643836301630137058164840301068688355227 +3959418860712955101125551436177454788270598656333682307048221492143694319890230219738438292220497263359673419823698673057872078 +3013408623490314068776627587135426768753482653895410991590323095157887499728772798489129923069618646593123822639438579269538635 +1908216328408109211109195709992731650405613610224189255235529994289098765621895894667201782074632616819297670322225038809119550 +0817943537763128333901984566290468707429051150902489063403888015248830107015875323502758058360302496764604163625874755344905059 +7334562477121670573355518606105354751505482715112941146764597746549856497547302376364513456936736713783904762299121613461158516 +5283407076162674672773440443861453333470684691925760891504089347363035836279580135355221825821573398468104182485287843330909617 +7205284306122028970211792875328746564733992888131805747056055043004240391649557442465694153748887961289838987106491544764907052 +2500872325568857502281229098611844366313487869502518915819858430987626410127129066096878146872919622682973073559229804271266907 +8295782206872660046903141337058447164373782585028140797785111578071196136068281762757693333793450310851105370333208052458896958 +1725889330426145518489875923548346134621897254499345669060138933732620752098808224545684009099318550221718273960774029508377011 +6086360263760539829227685105382480409473035021789956560069988221518928563686053953426925951987832850158619577369793062706768043 +4352919017742582830329403068368693550555003865896903349651288061511525716234272382256062983241592888339539171693959034050993111 +7316775446918119981332285689855871890517757525471516688406003916733927585058269512316355228173253797483394637476145877140830653 +4449523419101039158408708904430347733804700552814623747680680614331040890499889250979677397459532347731846180044631683772954511 +8775927277284478472403620186389038850970249719140155845452372844321949705137084066166954690611328982193781479003940799798395822 +7036309001309751772232372080817838422988947200353430157955341599997125428100524596360021893214442902724490483771053395386266905 +2526553722458751882973801186174660169500936459737553375965211051557068305755252365586897544315025598670454207459745656417390706 +2637149880243542713228217652476736046313897745886197441813742086881967480415661410956495847064049122729695529600136287474175290 +1793795271658672504903869428016853432949018555098626397052181786382888682206907544219009649133780351002110388824693692103301828 +1084255129975610497399774428514444229695501619744314654316691842636967058501657387850037557822192693461604997872810120193484078 +1690635325513958119210187262311486075808664902577687177132226261921963946973671413085629761815694454894584772804358916692902290 +8584983060051303667121308176671074873807110361225378981139031261855126176018462516372607208578426174357538994152717337719252523 +4278036428830763724403099207458044021754910574858908048715320547117712294990028756794510860977637952837840922253254728458600984 +7883825345262132475931206135018623229949112586397174487649066363051114417355320278795734783658869193239014023523778761960029033 +8695026444117478979408146585926902316341504795145288358653821833800650855569985787340203101735816503203866669795526287437002033 +0245051742430260486282155254132179648064481192529358125651323989892545661828432074084630060930677773108561342125212783387281955 +1156534912244337917835348237158665382209292463641851049571853921423174805869049378131731211644957860891749912005458383867541676 +5778772564492822262562399502030821202632651082331648596186198421567114506013473886361019711381249854838415342169443696628858178 +4018937636066081000726527827443126701548588875106206226537909295887328559668190334839725133746234608966652449462868965060152769 +8173777546154775498367210355738304841393638757271541666703324260466614672029573295838066643359099243819511379078208545782984277 +3894531584135582441443997552974471433572355979512626259310899921004753808626563585592085956971397408889173848312206978387162122 +4249778224435541775834672279093907849532605623442929414275448734738794025759189569362602709330104719867391830325898742293638318 +5618458472230917207115381643121156657396683440299320861523898119230249426608095966989749077626813445096806473183743262392826169 +5141443017081978141752639960290174384446691905687453519079674615609648441987212131402698830771368709970102768419122745998384912 +2946562140012561122001343127839838295167741172616748608003211961781629115805578417306196780198449660660896672943032048541196007 +3488478058250138575080356458275864826075120035949332139371412714212223138848322951784969631050003335580101622399775487873990978 +4317860282593236100733831872685638869277432762005042347567404549772458118340256499688632865717895145858533810557576307974444603 +1928173630042126038543686101259940626688530747112695018229893249219604395612728537717051374474126617790845613836004135628746556 +6994181584676204860202674587876078621483635083475446039294576324194007589602377577833162510613729896861820149258590200450670657 +9591153201889299370958865743889668888080847094554165557463441635078117012681280739489741681606506751718013927402701136203548044 +1780167360508315641369667116901861812842526826720765686979668702075706357523197794146674984683061754983908320058757429657215020 +4621260027453785113729584439569855185620374311773708057945213037359358758689719104605870656508325190409944963614795797144191442 +6611412529823091494357371703456481103154272600004279607488151916951301067639113190114505272247774489140541745738078452548556834 +8106748866753253878302577929999224844368660368702977874939897082451513356653811013795048262545973632043664542271741351318143337 +0778069182951962287178555694626059563658908945272131830094793792647180018850962827338479100883010796179956428416377824203799082 +9473023880877077477396337686061036427727779399244945597262198872743579391652952681609513942805826872165303709494805916573728492 +6294092277154179951451094865452721844277717466377028709711650186357899779470166784063622634966195721705046717157464710442807466 +5804785796886616339545161571917895816310538794711393196665470438339980915686578699904466139950097093535760550147855823745768311 +4727439998296384114816286463820007469707513836075275400610242782096499770391027100262738743742494025582717951675320717526395925 +5770688225429387772370873623458820190254941682623110262393319797303540835367273621810830554446149103975368995431078077876936297 +2405529175616664747550451981407708416427503988134127731468009719622521436517602196339409678849267725633683568413854707742239684 +1747116986005191343502956460687122356601160408668274191052227378815062922961362392926820216609198684542476134407735883221896780 +2662260497987146574078784874203657867733135048405416920171958838688884627100473968981627006284094098910015991752992842055879818 +7034398681569535649106832499610645350629642323049347631261095876814181084980886779188444300173631359422796869708950194760718387 +6836733703618557086120703365229061728453720426186389529699692510071571231710123347703658534972106524417884249218380254553097325 +9819799847270574690752534052463450597318916271469886310284525865231260264874522733678980911253701800076986898602579299994595517 +9135488779985085378045629972341159746040904128068784460666062698063117787215014912137720162075250475265414449002077183911431414 +9809851736195297213683107966811862847637535801414773082875296684360754325074703853487298740359726050853338447561139214515571992 +1029599022890794387902179784268370369360935567144903247409435981421651841889958031240517172069175107596477581973881408689860362 +7400367259185579353010164869469689488102231791643828135531761198096403804156803391787688285965553218505121457658933398225058406 +5220283029470788325194233641742163486977117357701064964101876499830666046451739488161844284719262317454589282938926798508893597 +4856368375624850629404156042966360434746200563963426632219247842217835149470275034357811087591708435161482657165228727592100816 +6253772836778061529203496032750827006398455227451857109564867169437129073006140463432201664058302222335982176666409479667581160 +2936456922664809261586171438046311878472513284135606095783748274074803411461233988794425470611010249655925624921046206975885663 +7903340067868061097623144373319704434575514501755821812557204930865063397777392798291309341112700415633886191910088310537801960 +5573481392684970852911664492959855735295070104374454536083712417507044706607037494482191236686504544140731669546789850452790174 +0904993356742978486159542255719565213826027717277534463338027027906360296595539148861118855418724089346480767402189286985762676 +1005223076106599446960454896515318564456413560395537757983458843931854126284677517864393826744115625918435152244510791809692478 +2164405990650873094086602365842140190819719314893955030230888932507331906676191911961689961891255808931785984896260979975690681 +3253727014960636085246150995307788585817654532411113862158610700251523142672098671368947509829930523262641611496136611155754050 +5927873322526687970315921879739844277763573086829985560339421326548635360045717801382027508629836290928584431149151932663653210 +4760858192547745555268997678117732829237567270192822266668837972286266876280740550930386549524048812443132805094528566843643923 +0552200604353097511107833851418517885398565041104987050582967853637644070929822812409158588536557876274185029987107810161351297 +8488557995995493390642962339468082402922946581115285486581085810242830297673687274512734725802547502660334048415579446341045330 +5709356062023991965872528242196958896198910586431524813751700975464715731688842846588438403790663597080684309160359805580967928 +1809180118084899386382440090650505696513340627765123157216132637785703218992306349927445013022757227491085193637218705876572392 +0750092974015461540416097560544979024242793468547428554110331500932107320102022511854661891492261408877458706999860502548130498 +6089719190070430700600786193146021409101280174362984471307624705342744373049193769815102419864609660529369826990237068799451547 +9880823550824400406858764215681272127616403736552454898294728046365136835488231881807123019108941139138735003722461424915329488 +7529037321181887616470646776506017170053425836736636947752180676382271801340633168596956291990586642144447067879824547939050775 +0702265125795249993652978449111254341598591409462513412644706330823597010310419837016366205284525093508925291114478065520543206 +2486005489914656200352331842636430195667576175939710807407359823751196926836356542172998603833007594132689750457318477364742581 +7540614426291493245957654051180243018629278083258315716499682711339148507426602166172200768552170520220563426401305349960660512 +2404338617929580850851496148983728094122155851959629735499313721174625488662780908166750428714979229469660586277142190041914098 +2734970584167413642829413555542927092036718157943315563661250374551603827984602155759774285156429894221533968500275418633637182 +6560220052859607041833971976550951076186294969188109531953615241005064465315852535875503380591519469842863315620903120851882868 +4362109461838990069490474891539380133820330011017862518437725479693309451782697543824505868730424985500423739583859994419014638 +5084805381905175100929792755321577290419383142651436157340596535511027236705949268189435276570853108955493181266234603455114274 +8408563281709145363110504396918026400160693545535873614031123523353385515760396989543124701606521301192906747319853776998158894 +2312458699599599230972023213413420672303278131983854442962403875724144311681960596932014490316204359247402654596736629024793653 +9839221740989926513947745565345687824749236841443758276831531279631822480088615812410461505184335496692743570654981097859767887 +0148513962240941032376953093608164659349988907238073209903158402845914764726830224719377417036845150993399330628686012123441747 +5919614485617148484720530134538022911021657584302803306902527053421247675384565988567910916387370777305148589468626006026739483 +7159664152823325316769176118475149162428707398939203649766531576459678350978394525441052205823105345184421463902500121849981000 +8288659993085551546441461743163304405139009826361143974328223331967419739647241073571212327801960556484524118468883764120169524 +2246338306775029095975369446186770386599660011384020498379465046982275031718873753405808637548601707969197710675661519594764281 +9599700671171430778834023658375047244787074487357323294015535711745803913257607799601446000833932736200689998469969708376723008 +5081943791810860552689101511523648202214658112058332928346393590013555144813066914194030280115978731407804149764646197662984115 +8801177359464553557557965020150275317953374369713023241680758235462889780964894972416089799047047809594791811478533742030190073 +0665429844750818754679057503636454736605371676903157540119156106676570191787583727649819947468340786574584101743147082806534991 +3657977605543416424987117022654696293608374996078959363230667155054631271871981115488592293902083994811357408917297410729122498 +1862650934481731910956432936712254764601612734362801048040108459535230079759939514256093409449391542317896091270323008227781776 +6653398313274972684240408723778161592189390653041468423088421653090661249171357282254451145484842067919237970412388771640682294 +4059821256052393643115972017853583376641290368154419793622859038067932722073228138326697082834080833163668963795873824307443161 +4885945116785653293180979216652517189039102100870721593874386070396567484340764036310998065513336640289679790609291581823883333 +3428604720660372157112193405611757297738411485553283296939642822708738180393410023516446689646065368009275111140421244624003704 +5562960052258931350563961074454414104687746794788022658055770332756970270739126216170694306943534685894930524814764997593650416 +3008384543733504706776478261526614225467564069116098586935957253917385357687398588155803265872788670839186222042335779464643037 +3618994828725096012458832876031499829184389279845891008599618731986280732307102787004526300770299430819579608003697211024173693 +9217086377524454012913218100708405723232921242597498783314818967648121936298190307693812351239182876627690699979858904402641018 +6156985750875948360548164905855940506817600324843389599811157633619677210306664432514278405659673075559217581592839918628085400 +4764772987507938088705673246656764726283775492007659316410580014942504090614797117731679264296150069410102991752421283871178501 +9088021494611622294962362961658529846402973311983387998125910749644155422299932240783260368601058914444067830424352644504379196 +7142168355025888186137900382975205885091374237093915396552747486547739152007548268368634296637427030850473757422944218387854757 +7616183325502874293153395715965826837360328123096242771948215374071981140320994435687149135694129364024023152186261051421982087 +2832792230789485819847349814552874874835409931161612591863141976920380812857719621635320076324767599038119716738981834869773741 +2754959867605262800904073710237512445047870616662855713005574347858126481109562377143573312098011061313182520482126994274243272 +5944572495216446034342198294744331850728989442478427239279385418283344155306850003896073771784045248648249808863649089110085745 +4935039678346741118281854023639235666178336524231495974905785711915838245296715638301791203620715851604930385744033263165263809 +7601373980902035512868636064609722029939023536705525076519976556652492714730630698715244783359777077548960439099573621098774644 +6788103237769355632318040401274560065924878241545893694505228598734431875252964572555463844283713282670737691866931493869606535 +0416187164312031460323292595651046052380878338516759425813027437945468646970166296000101963391062426079829338723963248745406623 +0923089990664161924027191610177696764629811421643542437939862866986835556859764272302075103487218202328441244550298063164681875 +2019867260237068480077811032787819165579411546914115220954604696377168121100888419574969311251245311564544879246746582842436916 +6868613732260412990938531237301911324073055318005385120355972354500615925738636280898984348888758911483752216818128353784893125 +4877207978980281193491650735689287510067435635758992016711009033554062927257985428856499572775171602494384045603122508003854191 +5729531014485818371423575856151555802439384687195667542811436864469460636476071341511650757925855405897036006697261458329928553 +5460991959983883494783035364470651652782232918135295761659407906592669701830570881217510435070790362298581095301898060905173789 +7646351028988526172675924552809872541181626761125500157109077472362508995981523449708391893239705562284235588614429905027555758 +1619134181392874715654091932556140391724453901036282239744719175838602787922700433547863998561669814541630538374359332841359124 +0642132589359772875036014752032306706353508991088875094683831897858049315214902256977825841683325136398731544129046064024590934 +8005838235188541103906355513876550842936138467720245485620712847710339766152168792765762537490548824247757795430623230546063661 +1739498737086961685166983125262113774813358028871712079307291894391835363168962025939984050181882718770421839433587670806624476 +4755044069459609604971745446900378189205860081010611662569670882836262612421135453240273781870602310532514360568130085933991129 +7924290416111768289187834903766663006016528601174525638099383915633909474617753311810491476284105443999724682875652267118724896 +5850681695306617855936614324554272763867627188224507120915540200480120428647332751636414058742508052557803594703673781795569627 +5161939368398633733681924028671740682293310854421624725144685852406527972897380355105818239535806432392343196805358469063293155 +8819559114109771559602841216196553885494830284200973490048941610619985614867118986891106961906280843338752508763555675909702206 +6241566490588598271025864796744098875607062200785043619624462705052447512034382219094774621478633751781359662848610200184361678 +8704633729839925740538038689681271982506814499991016892558765319948519205733093865854380442216165127136281006009985719211033451 +5874590475603382790640233559612673482775953839089864470737210616394894296028059828241641608622138456309335058488482647911816483 +8528577480552717173959823507749229677977069134280953275629509898562844929982109573639706635205828305920402590508711781395835713 +0089930344552012906619817966789113931607542416094931145768127062728799915073397115668423507229674038044945322045301649766598530 +0236619236986319774758924493517372212562285619369190213778360087721105343106507409406716723533090029620699768530871602210981580 +4900295631035231554608058112293217835929574133906243363911069130644679986177379278388410792340109628431007635443781281483062060 +4988368133324357435318270787600078029431686121284484541827394881261897104701243599451161363480613963194743733844807088688115634 +5178871803932196297861340370803170767481272576611956494376594793113965439245117335478620700378039639755713637510132362926267306 +1973846871626730910248052978143950536530975380151232887509932339881071793647054810578672393058114957563604971882394961817835491 +8978209292630747251522017252062343300917165930874691119490642832557026684811610939173618238354559097444010525631775837097840951 +8326182506155413337881369137900095820346248710691175947937982923048432643166491650906416257503450243786283464120124419550826667 +6090914463028322098876359782808759824243499333411218847428154348072847658504046701534863522297418259623151406625685618209367317 +4253745664431692653592530190665547218310374995530293912340336697232683775175851639464892393461388136647485464789810252552522056 +9182156878387604674503124667923684369744370052287701241975705851689429509464670883347575783846313229293967680582071694204051095 +3321006971958449910630372247668837211034782309154572655303854890011708581736868651280877351904031869989741999700057554577154390 +1273067298313752548096207203603148138418750927315375729523637857281549085842436944573932481003443446922848528006413314412427332 +1441821872672191871420138772481289303458305298682170739045298765824678050356023853590500427850176428627107627730008396343350473 +1052326101498218606100898713263447573551827490516947668567656571319040829379674136592743558516074455124317265474909140902458087 +7834541971992916704455016525869622512851276565353916338983507230882585013003793323808346566693345857527479053033593116284574877 +6838252895614149905894777313466208709968773989472894570857815422084908815232548928189324969136065895039808122393702760781384523 +2929303794620328740393974372454877708805842222813897741603177439187185228877406834374147233014054610369299556729412885884560745 +4370856078387921623155622787671852014805409807299060561419288539719819906697663945124473843579383816264514077377269614977581685 +3394601383138607507974293429184042736065549935759370359893007688911547744095191909676349245026886870781533913476330551278360831 +5351259816322765307961191402595778339300322026355376338266384698585109900534462235764152505801492032987502080039176402011160649 +6015860540766825761905783450339596728965747988413885573654798343784315606643443103943694670901175274103478596479365688106195479 +9733596316022892971509096088392032363480840038385290255713310540683102401152243848292964926264212842555582110823197263253672828 +1062515708221923559386659794495097841198809034454364525085442636233669203446069926961726048176600711294973993391741061849180079 +9806127623797585819585111237146636415904332057123217989550966977692535517011996512651123910136604576120833852264076101548649068 +0894752723723477938861483316019471992063859736335516787664755140329957520633060255798252299793172160680450787189861312439796670 +3913142052442456614486902320774033281367626065329089488796925098543116615817296515061149179999137691128957532263158924327442783 +4918858536678070436088759702284980929577025191565900433342638171236644159755504144947154030057873677997786393398810399270491800 +8872734106335380929798217774610094691386997739307412068494969134661496618521118209935954632189343140189392135323738890270473225 +1245647673515237131109490917571703008777281273557596852732435015863930630266628924061119974383584899927931767742330245961719796 +6852379757973100072309834676038484260260899346508201154263890305770366822928200041404900931901074539015449894481478124870846606 +5288592940348602075775045536917167753748198797428454287343716300518081003013742004598309648467119358078992444950790157070897622 +6178224434793312241397484872165819485491096197408141733026367177440768594738187232231600889719889461459739097869779145699996070 +2192775687835295828387187709692569714086691619295450894879843196267448458847228413092057333128571855124524447189613776196209305 +6242914540023653075456224110675459930237745861318742551814072907467664681202170417286866153184100097311858010110265935844444511 +5209497239417620919469741474768380931492201661270471587341653498268177621359803201248700031595849957309397980724999380306103922 +0556894331856087053327675958304000445533946185075415894264871473185811965493131424842414390488954096468377128994425485984798739 +0196159676818515225003292466076872625799134390488404188280002687542570342770954127173812843410476595231005479765885421327808255 +2605242865388848411850848235973559236245406311003883744452617685164124063273300160172061005325886640047067612731170493761853684 +3723865217540793835273126631505051186346898929963194134815240955269409964826180214819749886514216356192979128714223656587784030 +1612652380049586827710856241890765244152817303616193799144244842589626119110544403111366867458976947676427002814778923808924618 +3631721504060950588384351554676808360758437911729833313655406741373502411393332262640744499827573245395519111703225362606878417 +1363409383247041202382518900846037480203513170344097014906470642027432914804792692599759592956926861572014354287205548305433650 +9938363167455625445680112095072694438021204263665499289036436205115409602435745285690360330318532747799997172095972559600945294 +3849224957526742255789376070484796163223388025899309472042686406132560940455845848040737742829859477224469824421878404438255777 +9153640594267832855398773722288983227094747426234571630063269545219291207342356377829705537624593717193629488670483397161337722 +4742683466675948276596206085950351177248268507712030644980189541847011659976997997291786802803131893537147312873666734632318013 +0962031832358588577470495716330583413563622072795533159442347186921867618325791765612531566938977664106322136330634216330706485 +3562792846227123177501866289549655646263423972314841727623087546398961182987448990222751356418413165373556965561892665216006475 +0506481362115739578595809306613922949438046074388531145186498774188751322860892046433870735773039483261252104554347599292426213 +6629076659887419139215806756640799939588104500582964705062365481405718348479866775939471858338157222844571173771380330117254323 +9427611277124624517129218643055712610406848999072749557192128496185603527876575966140152280783898310639896801498077008734557576 +4949349263979842274642964286301282439502057095606102606822710846752651420038445554041921101778203644249576817202379962365236644 +9000537778793090403522534841735088671708809700351632320108127651758914457761194719832533167517277944390073544087779452376879039 +2179620974987315833763764525622394287879156162214632089232065378247731638836108414730685627306496030775755582279275263164338314 +9850178620109230660864047257606734366763600031327072575073099542439779650618140023637464021494877439885488556217335419454669846 +7274436451730146692806124202594474275423757115268107978580315116832524063995327727093500514697813597360878942124695394787220781 +8077050800020141873083229149343719418506939350096709806686454834234546620350690053797423042331890117409969910833834635664137578 +4222569085314829230694422189066171115029353200770994620763730497186499678980036529016196644500138184587387164123266827728301061 +9140763168998745887384781477877234025340780171338591152461030018239430263211801503428946233869503372300942372235304400820946852 +8437399467538616927457301639250511729312614497619431195080809960619969552298634956256648869707957943168309430154835637684617202 +9927888920815679261894236713565064562156846670872463967287481642034739020192858148286147625951137420641797396365115772980951219 +3655403238951645285818609634851118279549531888817205406539748497770277166060720853420241694457643803188820531391415953932633187 +8096429698574955422724412934129163958768439154851606366794537099440778504483170647334793461803467077456150095540043989710581777 +2365201968161103470136827409580883843970089911974172408580391996075540014445987053836291636308746569551411995558157351658739831 +6950345059892537018933018077820506883809089865363951638996552650340293111131388149509355326175519823347067351884531383182623006 +9729692983578755541105852708900437725294440871971817004794137742625651321002377622365955498287088740364399525852759257372235252 +4593851734614428386795483124618557640086184414899615960895693770722697765927766996280493365204591488466719654529288305662919475 +0122479598904859751605583470001521191650223688479671370454989314043303051942287523649808609527628133476925823025842389422672084 +3056608819600468295745423499468437706122458751753728982506585826980159252966514250379765257256312249132321573973154317443215255 +1261003464887189939919598811711885508553376196973665253914113359871227784508364309872766800569364687529405313394507842277777341 +3335595614255964343357224505993110214988297629416503148121404268247459966936550735839779347369294370575468296662036041463516505 +7279136021331762991558929156754763672783232261635376259854674650491435829901582103560659803340084857348232939069746185793481086 +7001312755863777334623418958360382433258119385097977803059882470820602915102809159265450380632894446941317444541503087066649605 +8796169740654134032665098260784549928283316353597416172219489498769356834724310846396610790766934291348253075369200618334478026 +8802831163686839169667839421840117736653893219551959966388948111069365662007487678001136916755953605936920644573768537311349838 +9739545132806507773311726139940569054566559814706558048797461242055138311352937387308888484537481230253759123043684421386096698 +0953084370435253065135920609321712138864781756707944630525539032264477854921495176982483322151742604869890730064964430072465893 +8563889642871990807691809483723365775240608384031148556634799934500723149755352447233079304887498827996963939978814660785015524 +4422068594927829463105462477608520539620307156466022889708603842952278355035865136962605151617935839688034493508879959012432457 +1797597440197984699879058451944014171742351115567915351330880426124733117964473476818687526048324044778050824433512831279314278 +3544880014006914530017330247894174199038635755809673971423695196336512713364189925640866570283506889971990951009729565756092473 +7989020444450139152190142450813270776908916050703362005761467814560672206433992898775383186508883181569920382329721780736328572 +5963607248916565843748383716251646836534350100847190837736559193434644723784998135363197913246364668420334826697708573880522965 +3263484959293045146013061177283236124170085863131765193651484251463844732711431227375764646336930721004123585223994485289918901 +0583029668150873954157367992526151571563606918837234329358185868795166989340614129979830550547132857672143145949742222265113600 +7440572366306644814358767338023644569585694763643144805041668167574800647065193962831865616870056333413559981420227280146948103 +7295355536699420073390947523011459995035076225978883439577116325646324872851036992256964558891639573786018094493982547872559135 +7129457262838894246866292831053823350947068553550051302734533231301531811721807950765452389323781067242015285235015915373253500 +7003661686534989695171145431734885832337556123063280208162395256598378269110597151277334587048349487450140097618803853206266757 +9082217774055830430004211027285925285344275476747121049534781396260863522010542259349559050699781590339007899734612452403479287 +3191667292995301649109040619271406402727834137378627737752393244791408796163577067403946973388491815614922007647090321324522320 +2949712664738909367012483650928320008049914477514031653686312257788502415066119921871956279807885221949190218605724017500310920 +3169525625766817202209440542641629818384859195018848363928308881901815336912072399651202491194626625193357131514851165636605801 +7255316665553834762495164950088245569336300767957000036188227515262775800565384194139225891768802813443198621045801692291233140 +2574872647913296219947966169138393924496399035916640976364671914696565353222749621284958305509352891507065537978150518314618840 +1953749417768878991833661315137426428652077503145679920658786397955780176167247358992933014357557538701378753423037285649046608 +4668877807427263578702242652781568611601647436182495270690516771196062145719412914253392449657363828299691513193636571040444936 +6886803557429783411232920271165325523458164084362409904470754437138270784982746722601116409545759719688367256037780110379647023 +2300356518881462544550334413932731049376257074071686920169237332025867743966470152553906187385400688773328478685498436856277618 +3440412356699057087611095963196908710039931495043446625996426055839433344899946522222609944936452224313579344290272586318016202 +8993953982184479368753437594218699447584551919428710623167603425460525558734552005471227418138809891695708393142992004433396157 +1641364290874900167201473644758505077028954784718448987507776152112958786620272578580374033281627136838108776465262593788494546 +7335791534524477898748563122556395894974284747735359153516999217809354333772921832279669823385048414976444462237355621384907691 +2772845418405555209223672772048928810050444241654554863085813477997024445507615114437302354148041362541497074784257678291599777 +7178588655224708624898385615582303156928858845044700179599109039474166324676359828675085010955238472718587302252391630153958817 +7714350463980300266933000010667914572625520828229727411680133017980270989103849220980928606661613727680746076907445089175447948 +8959804988970943750240542329725780454835670277149205665534383835852603494258686925800889072070906952377561912949333090450610435 +9127584427822125708186685340454898072189100248623017561567872674357247419856515640720894591586135918230623264124957308938825903 +8565660274882658423962595807578322116479785809616786494557353542161430392650520437776730973491703217016387298185714704526578560 +9055899627055835280579204790473134990779700561942294557075688923514759353053304752653692669704340622777026507467115119994712711 +6401990429555126633982558874714554382951718476795179895512968275571839503381782370343773457772170386412634133151122279458473043 +0293309378136412704813380987793756861154395548470319616928895806731980671468030021029452883570696897386186564749939824907170573 +1417717547255789991707004411151265689787923836241613415526707758612724058279277154586299713815047231145490533591839466123670638 +8468716387126244026336620863493031193083664955579994896168486675960587844522828676215258682659462698160621004514607084416783102 +1449640886898263771104309544234611139764719954279503593908340282487665393419611852384397377672021450701130079538845214864162053 +8048887383017701836654298226594900075026630240187365178333016065699848058831083230977644940244574444820285738121188435273257549 +7734838976170306869973081121181660448148530318134255364556117593964696552501147168845028313551613495459270535042160362667658266 +1546639029308273692114824601121738207436726090786353640928151613146720401120659236951025944213817893334993036938357931276755092 +3794363877794105698247622335925064622343924657500773158672876932260234127197098205417204884907008074223982761704004122962430390 +2332227732674259579606138447783556533770663844706548255581644643545209413054398521429945056259218551425238380035190336407512204 +2377559578117061565563176922758264522313042429970112963159461355099505172026291216364090521544160711259708331105724128544891227 +6832428920554567726830569490691102432470549042227057932067103955101428247157515420358182404777567376342028775513115385366928419 +4241634693879583625259038169292585038330173637743744110426172931636984875802849325805759410994995655688205543970012859415471525 +4435409455307673255925091284402380740900794676435449835586381572908519616958395334196299914509777009013685912584824599000928046 +3987500940366892833785739644668889848240740024168650731790637517228616861342434915208852629849188021280517196274460956387434019 +9025408507685643206458885320228075047871187183125968258455419735129168110055303698970042275578099161589116560991845557669784646 +3366934559408606275260397226783303207383085083713447307195484607017198247596814686470103165356557218941037264416211493596219700 +9986983198296827970312097410216841561044999906047830934706495494099795173682393665158233220314878778675703926172446242051213291 +7327936388263057744394340969867975674336916032235104437222484250956772949755115175068343392125983148796915268153189466560134516 +4505129238068974615562203042384229709458199180125982048735704933556566236123385106774238252165050271569657572461570289000767894 +3255121682739504137653843468184029546420570010142268916658213975683507187608005044348538495082520951103601067607492368878206287 +4806852545996927533787451480123813559737763425204966730971472301392243492249231539770928832609537982396509758149828504617903683 +3852759316339548366895421554002413138488562983328873969801625973328318967409411308340716647240522981188392227872470754156065273 +9712297766053304902554018245083167928441148337025924749085910105658941917341095233228980927774840543348243226033787592995988515 +5229452832882947701986160360761878773430032426431930521890318903440689202842760168592576826646125537090631410703061420763990796 +9492775501530308620829606600311339640855706549659904872171618766544922435708097161505981598338090549562866490651446857290681656 +1678808597459616937537502211249586561665053229525645425138896011542471614595178170509581277828163045626690556829964393718215591 +7053324203154477437019031580603162546890080238913915157646559489429803976541625693989468806549155901365763128179885048681463067 +8903137465669354155616857417689085085205927330249024040964205952230818210265737328326102255377329508350857922236822007786665553 +2734904576394968648887628638128862963608976988414917192020797115239688653336018428937106985737003652208275177709699892675605393 +2387808386702882349280874532643852585858561738093225119093657885192947905905747402618285721157837466909047447734685099503267111 +4222155360968520550560237616569205752212233789555461181069785768415048417477169723562149137809030351670058489971512620085540222 +4298243051299268952240909211177826836030583513734437502197104884602882464340255753900393068054947046141922743129752936688868680 +1061375462364377985228292590232948700810225069864185759076897910654198361668411325978825417457852278989472721657536588031658991 +3940469338205826791620626804680412258933163753767218044848985297248760060861868407223928680948167632232230076213403901155993516 +4300489987649264460199719443525675282990880539122997731272161031062934060033770185574543151609533413639501738216215062848675971 +2177118188977585453528133388279985990921229278769939253605657684059131396416486258985713207176129279519783451725740599907936598 +5468202247459866994978249422059052436184697608003427416719037990135361500297493876720017894883453685240892163318994013086288251 +0775391987748089844439257445352687468561143051750745275017026626096588861151466861293340177217020485953364933966278607508804835 +1628141832107584076506487704333155316297016420320945282094351104594402285535278391969212402643994113779520387919087141691462578 +4568389047366302665905669107740203378971862242851210284195394536420153604497510004787197852478898999690840962496286253318550921 +5915870003274717803413556085627698745281531034825477926175968946204463905051355885851004345422507537344854894128229093755738123 +3106488353902440267975241920861283331388536648597182081274348906670754514090626662608509624737135193693712779860188833406087992 +7845555194352577902505815561879589158643398041086893321684441920278495085775044919835855368307051779503838399479822915036806084 +1432352323824143885623247262050454612927281487774810795510104248803405617391180050263987964438216016361064912733157137057008420 +8930414578064185485247400714821333921221435324501439994244285682760117301820424406392510602828280562393052749013520039103061655 +3173670884438194788166911165081056822054430147218290866655074231005149200684080014396687574505239932587314443974144267617007284 +1527450743968461168931869720925121234258369083166801029927251949358618661922498099466741377608784560458426370545741910023312926 +3234042028371733121222497619662259149254047701278276017559926645765011785573767542697475259020390524937117767929011666715323342 +4704564242794556338739787095791826301668604024597313082723259898622680965079684337190731792129157208207454560161440807617595099 +0586578102694447320030978034126807967366576180034644346378206991116172450484663474175770472979363179024738326934451423318607575 +6711140559239053730606360957739329179929007703740765446432207582934464258439308670322228705839599858462219965901802441668368441 +8135099406701400995240439771871466473527799329781080913941715241663698683730014850012873125772974693072458542177139084387995864 +6568179602365017697198231075678811557245910639538778108917663715366214082022315024791477373317009595760696873073681125441512291 +7907116539452577187506381898980888332568593365733396512784485641208492339371509014105839017197673863709466309379039960962534849 +4750829188061091962181509528044851805549096285740983094887975885868844208625379458016822827359147295255429793714493083753502809 +7458122759750053134279494636824383187336514214679539541415555708799139748870593845560799552363619897719534116667792275265497323 +5164505983084622666859401822913429103147582979952129213026773147211467590148787858747588041796556975884830210299871356341343674 +3017622046748766474496658478936400050960631692047289817971683454103138868538745678486924385845403439475522996310311537377432294 +4414433427221551082351019813726625491301604743637648323980649889708459225496111765515476857080238229389480546313915805440388154 +7128048414711368158025433805098117535633079017719775105234085215317610434688668866637527464933292899758107458405969861125706230 +3650863400245738680980130673926472854780296246906056457846071189797804208159343552552140320987399388241880735463419185645329289 +3725491818789741913366698889863185159632846810598378698135320769928269090296785595628838126516727088490704926873118733305049981 +1156989564178115063843077403158558596361694573121010738084003904783088187504126348616809589260761446819325657369704310082822306 +3039078554378698516122693013852039986103065748081512292985255853944982748397274910996825923066426171066912792454508578589756672 +1894849971820554276778681262205130604407309897326530536792905116831412406644096303796353324894146354102174288891383230945427806 +6519430441043631674771677332934701069341868125297788424647970576482950339536231483156528606063230103167532910086072700422834732 +5003294909621810744051481544451641763277370208048653102677095428677725528287642449117886584959778219864346164599848968697364087 +6751096317004399279893854072583230132666460132819591435600170059086840170914605351655306551416965997517899522960511150518237562 +5960953398400790636904166620422410033200069198453455057858306095484024517953196610901762809189095112424175853725058162186086849 +2378590573214575182954920757796313474094094261522041549359071498710995391605164191319908953433331128043052410209542459403183882 +6967512166626834760387377091334566417667045028443096775043889705526174743033818006933449908412813794430186357687977532151972724 +6459437209781961727829502811704779207375810741393525593812033931653583330049048561114949732748301604240502210577065589268424394 +5485945220569893838677587873427380906709988368671130490257845675526326075411511740051320570135507392241324306058959038656611327 +9867804575762976412063217986099462524791020332282964702449941371619543236717809914141667173375719858685004628965511347280443141 +2110798992334306839358051076527899985588107686694616660268331099137978416904137377280100977825535315137051402200610861241845328 +0363449860601942956031660094586702469467050513998615147930502263169980700379643465968570908871332692146680759419252634341789329 +4178175171299653546801324512595807182660716928201014996433033979566075147588628538125142107342090640143892070802843256418010103 +0456138779092213905930697158012877976499848878257289818896704580464918962127445564050766970246810568260512090191166877529968532 +5482828669997339487166576516726609979104834674845622909875424783106655102257236764736990686408975204793086071837965911886835119 +0267535380044835507754251971519271244598217325940244403179333727723642934577686643739339224195755654907218801997476469074206459 +0065138464529181013506268966674037435955704351830301533242369638335168171139259359713958529701206344946001165594755646272160394 +9247709545287060822576712084519151900147438436096859182537437687606226004692040127414633767616557761740535046385163645108196795 +3057714321815816843173779617852754091940306380163712243160807705160943819438940855547343479788687898931661575785126108832716960 +9533193667848378687611829528479562728893899352573352696025950207316753012731502558223567081292045437196069989946207141541601008 +2746003049129497733856902406986044430147897422219852413496236136281848127325893646312380454532608491203709112537348345922832907 +5956088319437370932773128721138395583820924421370173711672367829016504396575639303752597289231640106298414733655039838766441635 +6180360982212734271044173784836239476693843516863507994881855803639383910953492384867775507310386199169825014966152355545690080 +3474612962133776522839849256217894365224998226219581308883384099107152737580490415337430321380267679764659802341675603080689884 +8385214883553973286697705130178868327520836907858212929553123995458539418552809580075802670275820221517432874193060009661810485 +7635870550202301972338809565930908660230248958179738819097744222123891965883613360999839391218647211336772299291193850385713932 +0223196991899819000674224598203238140866873922543961390070945675722542943959098310960399662895718004978803705167927661114684329 +1353039351866220611077365279049149758286033094828313328435837478853295648033377121231092341833142961745552118705722131187887027 +0630837254454720024736805497864487627874608261900388501662821043624883721807437900259240409899471051510920617085099514167842832 +1988240186121860008928979530309334258534723646976027619874403865209331775508112215956780097468860667994721263707215073620406836 +6148969112241707992551262221756342003961909547272415999992255159101022681539848421608837594935060720460495066842999521680449846 +3457737254497423194650056810264127963010085423288394333781451846227050135758357730754076741230565869445804248143832527389323252 +9790762908682747953438433738621860423268309500095630988326150834401786630708761812852217486714179905608190957461442085387864531 +1522476382145618120242675473759696978079648715587754580423057134915914494338656294472760447616089310947302406703031809581038470 +0171535968526973835184507085581755238300981407341777890096801351412273999921818828858705625413157216884807494772810429053662454 +8619996360469711195123911341155232797961140155492488752604043466908848244534955186766472286479836154777737654804742176595782417 +0442287907416054215425872961111123258314536732740500399960690027024024360050777203631279476105577959878524421999307563248792796 +5348313098484066077132466864776966488466415145151104355092278471837179740560502130988667962911672913331290100002597980514807384 +6973148695771873711809347394667364106588642004523762177909283034839940053425839430616152779752491636974036365528618662576277264 +7738896628379276240988497456223829719984420682404133841162697625994660783140799400756439398245883415372300490612168611754778835 +2968155785628016494964773780830380372616577140688533547628672148913272007307142811840952334174750520152544790354169079912774158 +6160810488767205184519228782587106677087830138711937806611950216229418417002136640356986986424817853470899640679854298647927259 +7389645808720792658628388654675620007991152874617887911936239398213102595897949513877377839509375811277492184373982237380291082 +1922598786894089615133138929724905768857077536113336665514173213408958842847812824030198485744051056380839435865339196791176969 +2136038443877769241444454895223211664298720250012371885167260344691881420934862218782035849576910341176642258076960704001222187 +3619101830106431706477720342582912973276053324213930842322321478885278765764844239473581443510550046154371836075105045519367692 +6632620878175338782391713943229145276164648915620343082760668528508249819030769842115080099804055533615175586509178038716739002 +6375633941128896351976814553047937414266141967344986676911841486068926852092747499580940682140408121435982416523093436110154753 +3222139045759943847229548540511800241253979460730316619733573541964096618975921352811299052448712030457862855059140594007137487 +5542934473211875603591035637674955693033630753758706551752052733350020451305012112157194134893565133530552290159364523539238466 +9616750069420348886409611437053219761816707838644591613686536212537661191394262393089971240329521480448880783267163369576343597 +7663178436164222857814082876400941903973450706396969842174499622842764650335956819817450043712545807776915977745377365681672284 +2935635454175721831517097628434415466056776136240049950596242823574086540015840252558304179235526092054789562977490616481745276 +2672225222323108279006574534254983148801337991382313626787322270855974712894468368216489102586460739019744587910470428744488347 +4456074219727400675373619105257416175699861456100136151468602175897841754789156021282927447724373220233266891662271346817093855 +1945491471112632720796456775261728821202881302996527116048267476118823402662922873934255261349533307722895013671219044843545536 +3793064262773487395195026532854447589979023796935998929027789654646824047287035198327952786992674028615601422606183219879793442 +1630579923384492024946757118026605898057464509351571155868933384841116903091820066294965297665314366667361460313869589402652652 +5439801899827848778232404947415524928188533947410081530624338414230329436826968163987544645598294181482211511249572440955572589 +9020302797433585342712570197932059030880158484733343798115491391574065623327717092645488738835390428206014044345059915068656197 +6778633319862050630189473515487955936852231827578947650626369925629640725543661083890077620427798703103241551925751730707081567 +2865799458475868282589650599779702798567688820803000453419899694057649460056145012638522245118862822941880894081107383763766480 +9848357801406244621888791600769552120182014301160477310095737436308083268418063998275870777119923015945538580745005505737103338 +7945415897343252007659617183211493332054610541474762203740026194193724624943127621999433384183901217893204226254420212426063638 +7076158492133875552332779907935346207685056687613427451355599366014714317865416656614209291512293922523145859007589727797381122 +9946333250403836274494116934757202951674758307883028448572093526467181950556672615580772027423711017988838817957391185266051907 +2671612744740733351071730789204383122286756475831821553120581637533315869945628262846596800538216358815764391197830622296989405 +6992508792590965425974849365498446258624365117468171476061738700582645427331625361075430086525639161565604855405377972628830548 +1767000101633223839049666007882398066097691088797110243675124836228262533287561681535331091157763899063838214576189418367103407 +6457110670443987985620843280548984156731527065461755656723700576735027138342545097213504695717743645364185206786138748271882473 +1596890724976137980689991632152444686110535638170408866300049744279882252913535065665891060859035882361321345023887058337830307 +3373623078064203672049256820541947607511438489025503639693977760706696248117065715718091918113311545970967574977708709494235026 +3986923221612406956343410179388036231285096894778891156497956823154793926342464675477516471934158222112337889492119504249964378 +8383359661494890362436075780531143970408529612575647080037096675346333712031003814298042719987791032534165077846901212886629083 +1031714454907840175775311491859630739967098843327841882994334085317927475102526174618259645090436553276509976956833748682136561 +9064443423702201811261101290439334315411997283217810869669829733471939988609585312109817796812433049198581074521140984709783531 +1643172980645376942250431938106354165389537074459078140489244293256765030797030616074547245248075610258161012282504441637098927 +1963449744475973480042625489273145165780544076999525577661332442838969621507452441516783367746336168024894911875794707697541031 +7478540879501049129571343735300770536436533927260315165717247241849652331693508424305277324109605811185754598081962423629189840 +6683599827684293037220931133813347833193703737280727037200969748489983084498274135581120006138983726589125846483814619896382039 +1059986341689241962791022654106008362005402932907446471177268388894146241093638783546170660638544503679877547754823916693779241 +6066567318288101759879455792419222471083652734422111018558539534478864130071102091839803023165372816119391124984955696131849946 +6509143418752724874347225367749147359005862337831645120532349014908613153135844609402649987007336422813208143055617331460906483 +3354213199998376189921305928077126664201462525396049040175801721488556837349266500704234038852235700461088952452452884233498199 +4256488814582093977539133655050527294580364706253926894156976648375809383013472871520377935537449640402956782630405195205364082 +4477841383190587477839953144802218475017581377483593956131465077901385252056389226406917609350544701283827958845981175691165035 +9675317500791142255855936241556896374576355116352110763482014692288089584642987642868701466081788634493767773533747361856594926 +1789275548140948127853887574961649444778475578058016075475638253177323030087374164983465707068923303558558151943477905316462894 +6521653020185636533316557444077296865023590390333185648729621716717902230175881569862122737390334951189041533339830180227274093 +2241738740799072036341573074248811436322223477169424669409308388622966605065590062245566692647119962939155999944641570918974529 +1928105019305194521974392400885274260434232105024551451106931113484210411868649822559924165796213171984543031754090678126105970 +7223691850432923916423525460668413682740557862272946646859452084367199412001020057846809103738751744440123325432522452656213927 +3644451389904659220831784890291126628998096321970481352017638587386823902603865110497499248782547323024785254006989249949637036 +2368125490914853885015667449603549765164883918157991257417554348691174190395500642639268158971914968315411160008374937535704060 +2009275459811984599922767470947284560232970282990047819179940733002227635796160576632463265974978503386125557611307429312063703 +5284668210801338637743489600143584813073507148578726973558859500688227447435508050513188287747750741584385175252456997688110085 +3073679804770516059033436419747376347125944073396261678398209865762812539120718854779696622769273119321506920635555831381282691 +7090364017219499874566644997354338979508197792775841533060988865177458368657219876491035282278982441665265219245626731000939486 +0475155794230569303711742143247875808031690475413279208626652350784369479556492756708347931041817323940692567675398118277710202 +5987347394026084150071478790424131834854554229261815024383846088465079695373324503305612702444122591068763896541604539174335033 +4436269510455092220918525983424623113381645210225340601560482106077382634781965165780342556554085651205866195116920957396275838 +3995168889104032757269451494854524514321628575817645123299310077893411388042092201792884422861886303896708749182469953549119697 +4535001242830034769362884028681925364864771844433940737375296944979919237869503095436572944940055711115231301447509402025904715 +1800925216578304292056694261819970660613390371844516635237968892926502845404165792819051199273212724458326926182758338001776015 +5370775490948607047730587515100674887605918594533934598837391246334643161422435117919064366778561014958058716726561922586618810 +4628995498127197780734498354535034114170874670064085395510892816542000534421254922356138052258209965701576585535292854686090278 +3177671938008077658853731628156335766651218454783739782004804667103681002183460703859644141290605438413899770795832449230476552 +3025608055188815266778993615750176656164484757488579062995887277096944086284232725973634996318024169760316516668522315338538984 +6602875499695059755336245084768806035216324561236127708770615233130804510875618781547430556541215471796577953669434492812681721 +7882550875891690926234268000498082339514970368440072348061964280285302475222074394484586936518612822244399239424272459493452968 +5181563270479719492880854609815755784042683606603220498202601700410969538595041484423412424024708099469125521786037119075251369 +1248580037503075406711823500465872772309344548541150892813671393177549861436277865197182621221456420646870403987260426109411933 +8643731351640151069976036541617610531794845092323107755413601880551863223421905943669090586959614825141812500194544006658912658 +9742463384500880801343550699245637258131793581488832729126778269174701627529245091838528797596704702724365423027345604418154777 +1491976930735419986039574314558277854162840810698202905470921161004472351694186257157283632042119485754805929863758038770286999 +8776957710513013252382046879914405882388509080000643024892306629178763000752942096488188622280178608686752194263930907107345039 +7700223390484500013939160057722983340948636861897585123716556883603506625380447610584051517968188462287828774879778811063488262 +2795184513732482674790613434841545867779716565739406351364662664736206685645328526381352909903916053024580749353932896630696063 +4740510819037498201511535054658680017511924766740460699058893969064394902765714052038187304179100787141754044221407272788215435 +0593548885409678785621622325583756109305462458590848222639139506641697602853829884986982801690179238753218240401694408881886033 +7410128478642788707381845999570686789659413433178313527394929430353749262597920022866973734073910557598880611659693295671936482 +4050137980096982959463694663632299864309583041246448456720658864445243964035849113054975666938514766051492807225377478267946248 +9230953430893702870077668677812927912187053690817849571057339797969486866950088035191626753948056471063994773880460982770051756 +3412034824969056166032815703553100561028200933063775986905919463873509475321843375830242381162225467967586471609115452917543492 +2276619135566337663226716925555717587888015126203959605452287702197704113342129180746061351411545746680564903503675372305630554 +0286134594910134037086879568380122070111392862199290853114304309332761209262076794099989915249949904726721530928297666077629849 +4661032688517145326760870540723245614758880946406315180541283633369882854109153204199703802230512566561461752474068428161370873 +9985206963338598229560272298952476210547736996017303976951734650959079279082066159408637306110242916656383381032701493580216738 +5913923322753424774114001206375448829014685918194637698592895629614872898525330558761701771138709572823367430917528581806962699 +9424883011486160624035588850742276732199499002202807230209412116053419259700106808447590894864084149744926170960405759460241951 +0111713605391910548623870259046900302961074850779853822097709685279966809633918351283533571183917553534326378996150978977861347 +1050239621857569309671883851470643478606629045329299864518009651449122046063366124913574153087650803586812245327770871382994089 +7338464051366649209011968493386794592783056535912533194525947811637756396164788969037121048254365052840678648300844862166030403 +8244368111563457237764184604100489524090599993135317160344204589670504422777317128963386304474615794833181756194674508984426359 +6271679168658661069189900053139794326069244916803991726466041535821018909664293827288937825025439753702650278433431356713529954 +5346852690092871593686894605405997962260976572140748476453131018042391268683407218753909113662935434807662980531252367452071209 +0705634605741863631683627483540085543041836023190103343436641588904884705166081263630037435630150915106908441737355700766586129 +2154922713467232816471312647563953292047715226865590918846184574044421063130666383320555928376617359704955430293171097080231031 +6735370005980738361139146789749484206639522950848455336267649778629028940168072161013843975973729190308856690607301961044761977 +9897723534911105404785432577277586147477026587182509778621368216939584671433875920453602299697242616307976245807249371547062253 +3864808728722705732544842322660134226278752025136368637744919793216576700939583871247088670783610602247097814787263304145508390 +6389663947104499816842871432275844541634180937582381422116532840275365272420061136044394514594368541860114545782560162829849217 +6461508204689611076377900958964324912439197085219708917353896453831247651446957118893020342996847989755084869372230374673491989 +8433851389135866749479827490565035784654953912616746305746664585754447284795963325019971006496729330088735987747297399102902166 +1771497470275815103247219843409878702558703790756432271114114625912471360708464566558219993340028146304118900320024587821960404 +5976617030473732204037553918438169303047012033263099608437162902327180494837554619833899125897762143660304278719895133912883606 +7761343244919347585259429796353568917054332343654064758967248953925035366120514799510920191615767532119958355777106600079597516 +3095394058128591553990850297147880385815415033833458376807751479083131186229720635268920776120415936997418185983635572280182597 +6753076258298533536576760943150454522971260918131318703703785217592559654671733288977408306810194844570204346184368392179063403 +6769837756043399718259982003405678775173052005969429887545845745194588303371446514765027612890020128811539282517812340583382742 +1145447512453860943574015365970108349103036202936884373965378227317332374230149559079576915215507294569467043944303098203606050 +8331961563901986606916925958427127071602998579210529763507751756811409645017688889813292902535048662887095342609935463489041490 +9736668802244229960622339427799905563413901438749725163782080129777353814116461004513305940715905434522181472880934497720795747 +8089231465924779558459413345521275018800849869822143818873965081853887737597561843190118386215028657051186337416427295963183576 +5419828194392698210106536176184087551774670922454148544855468554475299499312061306599825835773673050247207685913286844665654322 +2029050617375681673334265164512497368276347055093268045529145666800867914194334235145959975127160210970603920222624846997715913 +3338289728310358326423356618856438897910927180875153967318427145237491006914876971227986916804404786999082347353908985698677388 +7003592673799641258705037230618030712653838166741113566125031462959628644295056269826492276245790120809269461415827782914919523 +7933808111352753578305022191116925704616734794703884497922451694274860316351759525772713769181944674741861315056661705956786856 +8984597543198443392303548683602615735750098545443867287028649374064105213301110278127297110019939173598045760404284726471910865 +7750264118815211285409646722851047280505065647360902845736489925465867330208651476223128001763893620074546763783343802324668030 +7948487261322735741590287466404743224701011374099821413313798593806289098955840277566381979282309078179393391191764372912784048 +2031963937573607333606298955675833689352959686316425308916957187087489102352532079680910868716368077096336327451835608150057007 +8744303746009770077282182559273953960324245388355803375849588763883535017206175431068707914836359928021715134879810455899706000 +5908433402194706392948327209167569206279605516036898586828659659699407482487006764800996251885333845684403771593097852947347762 +3484375963559500648211146100726353736952008101895174614070329067925003079219669004928157370633196400836866091467437872364498774 +2081484205640426298611707106538905461885148824043568864528394387116332857339440668358439402474410629912663441714724196706517933 +3644343088909312558175950443352757850056547896271926536743829402725583986356025290388997593791573353364649376090870755738159871 +3017618967565343354488657372318982952747583634309941675726567297969538505603943497976312767045820564276791044673865996984930840 +6755211147212352533715490737391598930938409102322563339784308800982635726297552669891177376792489264858203303571448544969284885 +7207413969560628720009738274631537646543552986672220301640499255496422838738773705028096119936179105477192211718065255929107570 +3952347918324244189239309810051132796520311457058379135751168560732193370931728585607246787585935133034431471433154073599431184 +5948122133173804973271249039138486246462418117000640467153032385374427588683813496957187641107568040808210363510174360369854945 +6331834561201934486343354906308493614367011264274134644391474781936706762560771237417922164625963715633624786284367782142544668 +7058988605827353891060874777352863740110376065711990117582066617633614262447752203022541270469488891576938820572417323582324874 +7650494373012942929901692837674976301695625545739424778268284160337186164992467580080040539595408395037645199431241659430435207 +8656919176134694488026966726643058582704520649434961668466021566505602068958240292321141423966866142697100141650705135865554164 +6074242737126300341900975693876422564450176547153341379638414445501488034596312318816964962988094532520846350286682968373824742 +4534503855120241805255426868718961329975641710164649957599487128347011734954984504486944932371682996864451326051197976215900771 +6630007989276431575068445405328391722701879448701430734576747641331918162377214794008905979133776103351215776077151862759310804 +6329412106193202180915032816114091690804553056071102324935636935651606678882718350090015499101515918794964067433252126478982521 +1507488089191785086302341745012119227252927110062837567351191646939013086128950794430586208439605756178768284242079693463131834 +5134585587442274519690540303774472196006421738851673713532054649167344075465437753885506375501473079004259777595528824429150142 +0167920777588082886922742091652852786107461336681294679615598036369318540905949328746327769828031291675250740935570893774610363 +0038129096989223899832722064059115068115632289006675498997130267266081648211647293457692956590546845542631469315045748487379716 +2520900382745986244596184110700770194581607346454171538723932383294827821673322365898338493511029865949821494463508742106545267 +0565722884026288308442559359348206336146522003909225136977382087745807490609842307769964489536237904153503877870475885643050177 +2879720936866676112852941496983745298612873646677215750883429056007867869583163799946103524454641087004812983861736126233920563 +2682472400392268500328620582825275316785330033129835929951831594780666869418979561874633816166907302039950903765570878767521214 +6000673278648346135978034734234778737168645117288730828023574501322757674622590092065551189918194137651428146720102471591456653 +5327338501961495043879408610895757013526179026139024561532644668711391863557108491473051168562266679950416482721536337117909482 +5138678303997976985493577927432562758408369467781462054517017307284536794110651849922852301352432225552398279721808418546811336 +5942090618633537593070786933293193872947689557210874049534803895058142797994756021217906395785182382696435144831868446840701054 +0805866230785777399683174946303367253262058687100298157860323746012789121984620953083069679370163347105957163924550067110548159 +4041789012854867563385153833627178486269852350526357948901776390770563029842109776450416321004402898078350462398525384367774030 +2665510818821145216742821558285899523213916502258403631925403804385199376750878427488901928524046342897043307449003184422505729 +2048478041171475190266068119919204384149581828382571880806118238802367751477551342088588974943077841775978105590373879036044712 +2582550167003327483543334770578776146465326561660371559249486818965320307377862762238777770099168908851119806663382875070424505 +5444680706893164832113072218288281376031594555136022935125966599054561592735752631698479935094668438169179747421550309847079353 +7876386229779167984882703960774028877296085268097204654180082218579776041301150186998163564389835397054336008319799363495236122 +3595623959964323543031266473552625582982574960453793645874949571464306599166850005238654688725079784506108930071723727021644099 +9904805562100105780931661247162991156577135587825115134266116050943890998432158388576995701892214120245792294717827415173723116 +3198902109416772023345787207073949684308452839612518670096056433197360112721221131290963458888501686457236376124484894557704239 +1439923839664744053843985184599889045003833780920311682293838839176055898856807867213644378338290067096258257324765963896543835 +5700918940980369141639579109635400284102047501766736959067637462699617150713985354772825179670276479720622682613542577452843978 +5832673606644924997743952540484980626339060224968813632527031664879549813855034184500405400404100621140495713672255224298990414 +7192889419886464319914820529555240869091567523179106161721958129392253172606561099507740238957725006168387598704764060094854671 +3309479510962185342022955705280901753250343720767822070179996628541199963151640899372621594086084748660799272108092321615608962 +4880909425016446720046143938905942262706584339160340379312192206890240470343647456683169242925461208146677458242299921836024115 +9112782389287307875231265914118831295892615090298799256818943475243201711378213965480178556028607894090008691361256922857338305 +7878997196220126636517126620736341433150726102479409458917297307599211520723041036260833938983460239182766740060701211352201191 +8683094774314359941970056128495804368612258860699432526379799515947600345795062224586289908023772808639146020665057226162935485 +9660235891943541046373003698737146580202165968722595524291168751596799140752064994627768069683240083731174671310320210501136516 +3952299801633659008575843631974719807086212606583728034228268635353770341290150958327244666611228164923899770204672751927925499 +1681865396625682674685451758504759903079794185779114689381923935911799262023419094458049787998569136087163049641052695527707434 +7051981372339577512946597699315700689964039969082898958542940125278978182609047860935808672479205043611404976983579300374284797 +9494276685604001130980581987978951663125638076896908522150813094023439651488500315910058707670482621722763754582892271598798239 +2297480620143752383199993430862475021936852109466853279971476608874530008450195147642064323768140191491557557914372058492369456 +3210874548598055876505562103390094444210433165591830328725710043504607452200463576289609058990598342445857989436858490154233543 +8132507672542987199374049904308113523835831443354290371614453927015873910762212637250354565826616538395197156337073925141693484 +6828640682510305462478950222772217556425645114511993512248969567524785308290443499084010706135805334321921588360262285590883409 +2594395457778771868965633838538089838472114546447151674569430202509411063873470113459762471547280714167783037132473173267526307 +1601839698883476534179701757004074668031433379390884724016658551011492507080285489891017007383477491584644025888213433125759305 +7523532561560703539946099285117454732843364985652611667389407913314960693543811095755855340427962780551780192610470189512825524 +0348247544916136977309530119407469259627024645454634138436438519986158560867875479423449700625816301245511994796229708771223160 +3917296084877555076938742844381232454610347414176454495292235478841999037736204668770470759408859717947445502249268193464913498 +9942815514474689741504492308012003642347161752400872994567873248383495234578303317585084447735662421098893789187005435875125952 +7400181919019490047282686705057224403673442901199627215721287263528829739748034312624985655044437343710362758307470148179539661 +8008829134593470730958751998114225068506458584106597781009501241738039124829892755451932663527484529522803117480503191650035801 +3220127286757997144992978308357689318213671314902934383221575508818519767526663815112777913873690474000419949051472231014820267 +1399894109901863236068753449286869832068943284626949416147238270002784106062189067937811371979896380361397172738949505933506481 +4254902873234171699063619785819122642732788292824537159169433666069361612397840595910726633660384383938078177403483650975568259 +2209368179438159444309719519784098123505334554409147220881995692904711982790135929098654648473758048308424437091906340638974131 +8784927854050511928452505266509077489186830632833630361379070849447072741785156478899585449175370978549268898176795868437582718 +3231975833056135924558298656515294400028803078329218944207863428380011023399283981167261222531969361122301223500410734344217228 +0958925958615134583645559459986109673563012970906230471133763988818583110841341249599399387466204289301186261815920165156578985 +1991110694748693828675086932988793084291818416422390423728768580762059411945640551530823790024178761601499474873560699371792162 +5361696020245404957765470178767621215479647553935020898244576640893571391546565492142211775010405579641149396961911772636411704 +5933641384834122776049822258585947581244135364908556513226296636775905866481756079367435635748095205281573764949264635941006411 +5041011491795017405240846374877784042033437774313006536528316953327476960247723830386371163515353865121506304361011426783525735 +5488997063517475914229301557438267886673740763412117613628897204796477231021684048588313466034859903879245472327441120566103502 +8530760385580859337137628786690355170400764087907594224462686544107056072485919968752489909254263413225447887894153248109175652 +6157903578937685041336967794345564722166007100607239958562466608483279980200607600350161886932405833906442195659498278360919430 +9828873150674702059652960737024831945705458855872859591267757719944168191278626072224244567244295799804168245158850647976515767 +6733614592966966657616990010769041211313964384427177615958783894640954404002730067938048093833284287697173874637222402450231077 +0815239310564450548270963278380227549898803833625140660969258768879529883774142739151346026790443492061196604908528916004363340 +4528891650268490704421594911777184311417838640306057034638291330148034732863406008674812263692977851302665645685305101437279625 +3233475352967519524983898638596372885741145463145393518228460271054338502613983364910024616107499752355701791207046590492672193 +1607766030654760272868975035371747266369258074306005268123601506826295532939956628851683606599198929232644016608548496393286833 +0164998823596400554560038697981327950512161089142533974519294883428101099674224630562022297926592367086936345609705271926077768 +7913025882622995728679690258052758137385077334095212932668418477051379462440145509778953697224292738907994604706710137743620276 +5951558921066718365814901585239666946472249768608440652585316569506390348051275814890810652375791805564478189249435436637972574 +9602716095164159166038408316037910403855288603710934148506742938423062837375851102966449162297207365844156933403348477083451353 +4627498098319599205600533412615626896664838788638148524473652142820739172777284703948483540298530565794965460580139030303425684 +1565284739336025065162908150482205331120001945515224141600855715524039864305366369748820024483450351626491054274468986093516231 +5340418952181086209554963565801571626818882174630725995663438985529129315572417340527118943747399687973820656682726051844769221 +4320926831481382140838060220194555123790396897025705923345941235597539508677805522059012930976097312217637965288298799051778479 +0908830534451543414307140854781014820685341080782566962124152921948070095677549807088847640541631088209567823713325266131951427 +6110373879023562027503964302970937405830359276112650695138428936697834271328145752116000081078410010751317037848623862138058557 +8822703971093401247422493922044598594234342733457558225718617586744418222085876701060696882821515543129744401944915912235798465 +6594559520819849307838207390295967732653907650649027886332594421647601798566341345946992510422578734064164433073111139225071621 +6354737391657731503659719583497860062575481051328877369003533126272128322177145084735337788672157007937391387995919117571678161 +6945608633872227095639320829071216249131553642307083427955811490415705093929385003029105106248578633207939138356220965510578956 +8875904572434922005652897358604531384916362396100014485333372047722658292461442588088931849935109173016445065149146280103138497 +6344250852997852406467930596512109372720069308708153172074632971208643949842907827125891877283714403480777088075917332714430078 +2141088112696908575603884902935424862191062652660730968192553423838776572288261313561512545353202622522229101786236940604077536 +5420581455921312988453042096323962394005097705232943223301112814607176607418563604729931333417412031473023307632699354685035657 +4904931609893963364651740622632458671635349731291306084872673976452365611310080979515796567705432384020374827307261624382593308 +4292701172515433578998040448234672894273087795215109333668233787741983605782088266764474905796649724673659270602807201042559641 +0405892711593975173584006410022813365106426354541842020523033260675160988528765885253376470412464249071597201808795790920249321 +2969506372363457771265917606965665281965775472879910780113511573805868748553670687571273946389007257215130433994049155446244186 +9422294818781445710760638755747391659377594672815933259737267891227254986222671631563563954141355946614955207418080860505944166 +0087286626782644309557643177400304492693387991339358406695023215774476540471075851900070969889709559625262772196723374356067617 +5897279697608188221125456894430238474410213921346239465846675042270793388259552491229704041982502727376305057500585797419096572 +9629126678093091084216993559586337141715599917690951379809444249525710962700663884823083705316885810747054287541385125785199583 +7595346119985543253389563272070967517963948640714499895302628870263490256644647992172402145011433583359949875045014139097383990 +7523156213061115787711878716816595972617134587220608008483487762219572353605120083032766155082751752855610567994430397014253895 +3701919959515768374982789437891830642750960645657994589236832995980797844253496076657711645006752860088626834982056251575040225 +0979950855271929320767873941275718754278787090305240807928650242101643623835476811933436063479233606315724201732634520101994785 +8113983065740845934933526321338302277547952641828910682131706421618856976337728051411744765715349602385454916896966865817502458 +3541043147303624444017398708391616664010004834339935228226187248791543655458437287589626370079460316930365464946671005586907844 +6467142906481062672351612846421358624595986748030105764102127849419935691001942740215832120384833855662335231988116688960608695 +2788530799703761722379561161257911074841314957231380008835616333860526518888348729384930186986891972631716493196919384541563044 +9960151488910951179842583784111062968107086150565586865326760613018375413860020781154310137687104793480739369654379775885629180 +5757125618091793590015555342485911625792045336449768530669665131967481618933921507464033442861020327590807013639300157233850794 +0108561117599044904729443235325450727345789731582292014742872599813512719247096059598721914318005872025916402947088829272312140 +0248866797650825595958204057508507830807476195084664283762933417519142347075582529326241506715718869000199121666869916905668032 +8723343677864688450245361288384636252992454056026441166941292835836423200070814147349431308408145782824860695919605646719597656 +4679974361161727145100641660878503281476844268161393833161261969965276371391589006952925603728082872969549646978882403423923010 +1471039994683623569789873331804012722815466719684852070254837711074357511238693361592620597284807521864906755820794891745584020 +0746324287724173135220206601559184495263174825463472438325932765982783597938536871848298666533763661034332204209283999095134727 +8847622479138729829083277482843135313544249094572861179333595590622772221061675396437819003622647477864448706978670376179646671 +2102601933270120920559135994545418775337484717153198432612404295706149728016561442479718981277683958181059597372600330224769132 +1259053282379608711992970881977405077810774056106800273103144031624467658496517236458989974966732962924668833215016485379965273 +8337096778081104941132624055977594869352911917306275484469902456879306916808789684303864335821138065913931420528939044385046271 +4299368276599609244497481316123376963837008957992736164808101728168540138407838646916364855310221850303575133697274394091619868 +2713682765289699129061431150379322274699877403663503040025037596762253721290401208216082436415144118858632156119324419976312546 +2881609371673910343032291044465003849815028985591391451188131927898325558643746240832264615980161723347980750483187867525894824 +1267916226042826987257806754004440337824069605413162886825139485042850988829975658205470396562194892003670768255291562747373670 +9234220033570807048609309912547070817083287541492891357818324994480536846132188820055694019487542207004276192292369053562016777 +0261127578143197454905032632100124793899615216318941692486325853609952184783830755458121912666549923939667928989436426770649961 +5323045993412285553048451071174802746049476412795340271320920389475261483664749025686760199917517732942380222819936015551403774 +9891352074356975839408670998973900071675970495889358565662068888386887973659152279951010618083877084169404988201568384833942255 +3238050166229051707803832286590198074734858215255713288528844554284335600396367554775218985201346513397387400209397068796825742 +5728107211476180762511967972657147473012924183544027968479506893336809289253531374220504641466496667214723927677602095165464315 +9011192930379184487659969643233786997863887173759888022579642343995986799605176120033884333316312893660726740709366636155465721 +8537171701432226276253255188614622965933151059377811653184802609155191205285178705789524447418786231655055288040171432169049130 +2707936970837574068569629371931407362409248401030806380356720552870356587923391333211272591561747393382939386843280495661244314 +1175509422804438381892148322240590701277543376729396595323849510597370466005945680426336918977914561211300792916203174309654171 +4042805192423544654158411198004453353597731647696727421911837745446359133507547657475484034048732290037678198364742205283336338 +9337695707833044354501853716373623356235483326284612569311911355595616206369186001725088755459930335959640295675396477063274478 +5056639209721879485672408789757891131387617511313947970994081160678454602320697412448491839531065552976208944273555314142041880 +4822378866853898824086257862623541730929040197220702007762078231078349736989651940081080724932828161588263934616036801731107678 +7042616549110273896023934480613769270956663187245804271172698934235574549161064775406631472230169390486643998381840493803484843 +4432695989367708020512810012276065008453086786966931783819054126557051679306430656639833606398733688366162720200084451796967553 +5350105131227057106985599834342048762794186401151883770272342638155104883645396726235498798575148882718582404327490926780130044 +5764192533929612390344710894456047465905143834205988681215996081613514814903908502795919703880401811242594826105306055481784823 +3081736917550671392293064759141270028494719167332097201455667940386826052104961288882027240309372922868375595753000121957263246 +4989894866974583276238356566258521642610551259726323252743986766553600472233437943119765905133915493843897768279428421555764861 +6801273161353076961494549671635819940275013580673219344227796480147357333556424794612960445706828140206119824948840749240399787 +1573785648039077385334262023034272384935398204989144292167011651539471971703585283557377107933951650497678725581455181207346698 +4874698339219730333194095651462069186623435369684882832433815952523247766685863473588031370751632910706819521148264821690235129 +1745856207579350369947762735836875634021204626000569901414064309006844235664625468595115296405455194740314530759379861609679354 +1263339035301388606388533163722497011371588322482095769750979879436887231718408664779720990691297703089437248834586600555339842 +6251850491772376275294393458209471528965471290147918965218220499155230361216164967212789860144972751525815583122397986208096706 +3484214175754929920259118693783510869997218078047468018854269713219002531566539459352397848678904622285083238008575450746485192 +5035485477939887603964624838580376893177633857009639663967781579876337080786152281286836780643711497334038142643551235377859312 +5770990490009748551131841465407612077195558195928231585911499648608018583508169531801015305515399706824738442071885239921251502 +5505962701249607762425096231287734763141665342862391398728642501706701840207749211087529147723821274132607543767155647569233366 +9531752996646776228330405381959650186056935054844849148234315573131250892996644098435195362019426286916985576819912113865830014 +1606555436659451432978242703284043412347220692073799660822918397493666695089541486316957331014765996271479614514099842437809535 +0289392478723841946498232349165923942701150815142311799644394965419544149980499852640646717021478951519649059011872310248139100 +5511032978947425845575588456445816057377640214105339473284188761415785249565799756251192934762389301520020016150949209737931857 +8405804800314504366659749363367231268121943183966514746661384830649579675620136873178912503421474419998605051121952540207826204 +7505457456876976664571641545260379929258562329503538400546169325488895767917670975295980264182906923671943997046060564087753548 +1274387949977098682446892314563863257816052408410571422114222854885606494860672338931736657657635104187063013087549529686451859 +5042755850832972022663213277248439000831269961224227718932272498745376713860758630276016079020855254056636945995345396441383249 +6344700532270890968181465829707309620564725384105716449055864407328703719779390283415257182087348958918979888739066384714906447 +2478502268884223599964876320836160710993832544375139942095572555604343446400261859797019349052899588226956407340248510969084412 +2858479499626608853379284122587510516464864248784114135053508629608620108109543831989223100900685873301894882329747241764687394 +3516073975240799285196283516667224035786957693305024409405170967493899593072209653378675045399225752043351688237909816464332488 +7045960591955279992811051225021192695063962021956994060992139199430008815112575669363055918094781304106258599283195179624447775 +8303548637737456436395221856298882133107004338700669993457647426042442520873552718504789607522324772936205923889075833787877983 +4528959091848677214793256254827374251508317477836642431769535470979890535339665561280184226951752824749258073794415152803073480 +1459998553825438645486213601892946519503789985188491129629213533576249295139024141113828276369687768834808863007813942900737363 +7827507025285847290454033855290419493832906414032145098230761761828468254431382777807611448800062745765313713732689219610038574 +3538625159483449646940178756166257692080479081424585343862543469052182432934925669284445293824171233112956439604122644220734705 +8884093066755275809492142335471176239144329953772128474695403479470145379846983260079920680227191033324315471442379828977213518 +8161874018859891048210600302227335518426279312266158906257386817733776024570411483770025016852705510503728654494429243801612182 +6746947553453295308964010256393213729923189009745905892535754926116629620973613924884980514744338995975237628963272677038960017 +2316979912101253225038374374358240155041996499533280966926264100171569427093110935120513047386276224147665309331079377719563345 +5252039218101191323619349210669314090591183299132043388783600602372822313158646436097466384432429894279310347011205483804258365 +8849078102378497326534646727789474466302897639756078400720546541976326397706257168948381071970398216623030217390426497155999023 +3271457271229858454657827770335612567349224212240695221407152662225083082143775459834013524692150107287815992741275927112028076 +6697268153473360409230969649314429281006253608637773637800129431557004336600687209561833649108523766841818697796052616713524406 +1264023927267188505910887476294511895228887727108862538027310305679469919872644028196540981088809214448552558389500274219570341 +2341820289505149961074855375132511787200836031120337305514546465652487797619606591322268017233923749079200060539286691098575276 +7095345917502068750136490067362248249806291619173145831611517423916491683475780750592775740226681145459237438975249038494918255 +5854207316691383141245954956380735287065353365324788816174156724268585661703068556010195412645633774843480772404581791184447848 +5983572806554767884384691619553163176913927916522767085440008009887977162986441335924089064521714048589756802500400756006764546 +8749074908864079527865640561432569820363365495585933467014689209329639785755338623694144042638210864919006966920756182400308068 +1488418928276481402385931315882264463640479342727038934158474806015143604448429971536086762337574137961555181782018130291223711 +7647631250366260770657650732694263292505761395977741171841175592881623563088242488954360090722844918014816525013438918815206919 +1155451667283610662459850201212152183408308503418856279359708202057227744768180593554527508439246428232801124311275672977637096 +3703563826897681685685127020250312031591544945988953353941140195645214477592978106315927871524898358121653801020581142274555566 +0502532594831295067422782714984502485832073390165137849670240378837997996971720348493993863606290066187249274244662460276318689 +0152755194697776938435286119441290637386200629628199446127743622173851669645911455169432495002024405765718643210153464143701889 +2562249212763374000591086405147643148598332714850654045728093130807693494090674026473297812238167578278207216049055667059931109 +1802942091998951751764726262310843905210614073350123667768202638105831131108058708767584753018377136585264570210097378501497188 +9619747156993809984322106521027559385156110152742577368251988283361064503255508785410345421538190771023025825991060075397692057 +7975279398046115700291300736456549463218759811012712333893946746580558137896237287869240420636891089058025806023431754219781359 +4988421426405962855719392598321200928497349040850013918965781094541442225330283536616202634976088463128589329235150833173556905 +8706200507172140856416642848813849966143817717503023224834106896623433469273328907429832511563972309658830234487588093651045739 +1804318877742597644565913208808319750885352430439083436497378247283858616368363127977363367800390625897013789449450637875537399 +6640589174553589654051256599633381120024749952650719727219130629373315456037578535279963334633963259335915488696463449646684320 +8032272830450718870582856393550555068150335019058461099968557096935808325051772579016403379075019846185741776197086257884683319 +9863845517847573376326098832507232200317675290516210661300649571154879306959674077908278504743343209243187243262639791806186829 +8260961262731301702271912238265956345814430445731867024044874570864287595912998480366912356847572447005441275362156399624494941 +9393142035376317879394642824057024278760474627041377093083443136837548371844421180440200277690935722486852916560800667873141543 +2707649253179074679795746660619684282278565191941032558500021642302377918384225264159606437430918854686420568233535916210590247 +3653446714468860199818770176822269463628134871277975181151133360475139855607198727163601307481114241837610243765527921963130342 +7740886414228885927181817969815506430494309276878003524396743847692333838266397442280441863182363662933711077420022986422070281 +1048387053761801939044385077721021658809540040018134673396554017098633248966461113346771441179046101247543088625644697892282167 +8354720955462557218106068480057094392968433715936618376234382444557134795213907950353329761280756136821666440990155365084581402 +3688937719186699342108738462217437423679733279917547383863629404136782590007047571508080094271518356483515978166149181632884691 +9910698753207335191315666777695731726998145344199523600464027456153460564479006998462380767579651598842317204792886070420998182 +9896983218119310999564732404427002821382477892511011450905458994448002735795600011464506066851494867367124445888153203962666112 +0874480478255387464724872537442210377291532518746867402289144019512477208223281547993390653821697806118478800205575150577662857 +4918888004811316598554705337363884151483714153627256672377291874950758530975380513235648385297027498654040181963476939202925535 +2944953053399838723025577285199841577289179795119166022276762443360574684698180143394617215098184693212410119796009072687729739 +9732016373361334253176297777291060712041712200359193577310902701783613655008446771602140682798342306314527926659629849329119883 +6451043591004466592393679872228794008130850294984708968342075888842891418002758594861642712850414025146690654543853793479725972 +3268929657246764345075224814644064768187234583627518324258659649632755014931180023989253455291331973610627056888949488127145955 +4261926089033087120812284628614836440477048559583788705295941729615760122728002946792084729956587080892613331345225489314812847 +1986796721026437522158295162217239471552249715846680613750709615232262912940447904521346670150883469709041492285328187488804299 +6628012528405245613161440847234508491425287943434287101928325272535635664307751412665147263964282878231211570769131706300759265 +7377088066364420743764402212273540338705138073396342307947494852950818207406971919647576428347904811456431944019834650692515506 +4705138654618320095006319217530505261916581737627651424491878629844356338877632449493703212698711863106207638016952512058495841 +3297864919576913522726817303334406976314952205292550373391722910736512885151802773872849953849211298237482028668331950499038336 +8974669281536290012748118151506739167431258730634523888446083432703773451432089051580445107667501300580071396777438663916505890 +6676388335245038478835892287014370527286751599266433016127479958260673956274510190770236842963946164980795747343439314929899367 +9777763357901635768971296015139054097237772494695148781717193551641896481882800982585569826367439263979611639005318235314749057 +2006174408272512048656262525906502452313110102817338015892638328650570498699472308145823254807565718645774486952361348150134146 +8820513608849516349458467726176340280776183307750894733331062785346417632047203675211923303269854905166452369376623717371465515 +0487090344893360730368974812186113379856699749061983876927406730979994152222167896194935782650684486995334925005887216364226178 +4428212757677682587368912604936275746129828292671231661463255473216528603314371364809318568990891647756504476074694167225494387 +6438298664776502895176225722692218937876815974395529173364685280466577427859435428997212680686584089016477667080112002827762955 +5372837358048305432426520203755641518748461432567909426155727245524828496289926253694681592910381964428484012886894327170000564 +0966786693488185832806410657062413915673905612790978639242251067404941406079608751238017548545457392976940768385484687626020964 +7081334569477079676309855956739406608641536536529136201573794386634730088085065014162237881535086131012737674806790302926845508 +3849506256608406770319555639552727023819412680964197246748248202159679944624779483651489821124252171233790787024388338169387126 +1220350189747959004534763654451835109235619957413590127042747161319131890755258681890420398880573552502186079819673130304226319 +7793578559294710797103985830517016163667215076322029333261233893714376644214340079810529332625249572639110330515277641165833848 +3375779071080037456833323971305312807921291040172365572121296035080033198190386403786992644365488916964829938879099337521148544 +2151885079681150752124200634430960212055491111062153428630936693865872096334814997531820067268121582290646921550422149642030109 +8613181778258533141158145972750085504588401393903967265445772834337987637418600396630528732934239316047591685154760079805548066 +6810794208813864500809678470319323448087878028803605043954670396669190693495990898866714597544374154361810960387728955757997027 +3780694542345726440651447280692129044592214402723296549397501233597057657590741164054767713089063618201384926245188297986421050 +8252358803202927045538322743598848427122577241918805031148056119515867274399187693181758263084888023518690338982779085780404769 +2424326538477152923084594002081423911440158206786241827087394911592939363042513317947630468078474296989628062484584400644343448 +6293560419015354447114147053377323271370974957505862615433195073799026219744354293045028357502721448147460831465672262899873109 +7387532795839583928982541818961799651808690944192536299119649406867938059084796080728700163640618737891537102681397327040310310 +8646371766907348023824656340748360066056892200614557523317897955392332090329097835097131487719957799594533173582638863081864212 +4986100049536290446290958993632891990045407747006838784207732735249166707023914891375926737812449203630774188546578985599909811 +1084402346707970131458723746814930784514195757804466199671002857316090262762516878154322011683754629425143857675752082486603247 +8761838577711280264766083372187992430863964477324546898292032760669740251929185543394862067810777754782525582117312630882437464 +0771509220114585291617227270613376384799093204219724418023916278133523270037263368542243169870229505561283586085433377009674011 +9820385787390786107677870809083528732710164474423943477332838364565199183957564959608617849089060421328797474250989472544417321 +7919326323563029775076417955118634250595776343877190502902725431786697484594693383405493496141691115991728752796848704617855725 +2558255581109821405634179872876958166347174474348761073684217931689004048682704410888017280103004988712033725528911209075578017 +9160863221896162516421424469589930231499714756713528296148018288586763465400454214603217797926981201646940396481239125062147650 +9840316514604720267572846195066141632646978531870664811024154364403354983561787177052598257504070314131100093324913554509718449 +9311775701841172266095504345180054522901312438857477399185382790126084433098439433269362739881218978298243657480689874068996355 +3022781945908762278768811002699224365141074026805076790548873962428543441756247033480754889441799694147133688175673557083134054 +5661203741867036406194178717036861247199166773801082533375613708479585773697108277538910907988050442661518792070910621436071059 +0690138478379736786396005725249615711997663990048585261495921831819669914145515073403200894318211855461357229650824823305525133 +1751585791160648049215935161043755494836647727651069321059714784747155745680573976254930604344088093264307898664770655705644210 +7624145261014687805152488774841289678583856425221410365164555890159519557132962343799732888357700797774204345288024762788982261 +7890710552868121263477187455194185399264846143513087916598365744278210315488246993448630198352186843484020444533061474327432896 +8206909625120146564482691781469049022430111838502858582644125909195387775957182358764356323550045553074686821737193044914677910 +8204337650424296695493149708468179951960014630538528111508141341188662936175939750414398867506644825258652647525390439239752201 +1219359010151342200692679606063626284788399861632782010310804100495631217242235430375489322644648855854345633079496117764048987 +6653697464408607985860924202927606340854610960203650555980002938739009437996650927711204711526682854187368829518663287620378635 +9615765538591556297112167827891749863300345638363300995749126057833117511099109253123549891887992882557489784541036449471882403 +5919028352049694001658038819829671235731815415745829048231249060308117257367536285634944658365311943856716917740139982945041953 +5779687741861700152585779009366027675558545586949850924189709461217199740749305528005166461978008407813915390937008493360553115 +5834977903061566139099870190655547716966444068581774636647968617674813198281268006404303675453751976888820043577600761967333881 +6972326585063978838443446520453713997506879344217941897803906052743968972254016142258731934881056702957790715642228115030783951 +1362425902796546798395679404782769752464205460307818179674687205006891103510168328363308032810297518997174255427908231022445396 +1509825900031470252867573280977984303757520410764668672441796612789584482854046983756780462029600563288455226754517497700564660 +7759844224301976256343546717305935453950775711909213261546888518009701479912670689049983757648821113680742909226880646315005569 +8295567234561669944338275143058695909726066802894545455268764288094686590468443329734647916141835949923568889966174907115365925 +8847804451408263609187211738763773284048172606546780028907522556324927271519705281526806470352840988271648499699486513703844773 +6679160425612818347335886820513491773906491628651444000288856405322740614920289642723221399498209432232637228104522990852837747 +4111012689294616788593607591102956684483592350971102538317426326909441395344097833084048144520705500574792401000355907792380226 +8347617047251388711302856164567942061715329050448779661635598992241168749527006373179435024773116055832951174916423963970112173 +9055087726497472517559694026453105136312139238988721622285684818285622456138820844308772825216777711338810456475617988242424162 +4197863232696784218000746661193362219301917851338032677667264033511098062281872897803087758355321546501532794013889570222549856 +6115749894285255554690322957000949336927917916709996337643587065466062558943015944180187981070089004992789679317730749848395116 +4787717685054333694149897473720745326568218218182852236278365043324649884567032268831086174748464582606382556519285049880278120 +3971535819215057024510844131306800637142690099398881617684362973207491682213220003762765264571054110883901103595485168757504333 +3733461701846483183011223494998710865469936261566419212369452729547931045469934427223536598514620405179798084202385746863975487 +6787515125117652836269017536232648894348703796120091034388135038967463209181552965632410604353037772756879239864709539921500366 +6197228209653401639582453147092770443022867307115583008238408014093437109999520308757528533277994723967340185717046004119026260 +0327403058251198538854684801262565688227840370906858688450070334229654311591524491257725374686504434311374381373826709761897189 +6320800720313217016667089641041343357180293991403820786439200978809209918586099663595021544518755767970175732723433500537775215 +8586605557740559261081287090127076957830333678084994220662406403052293892606977228772765289631860524156764537528844813070064403 +8457029064238615286786107279460798384585105993306593540395560420933072225512825677840166549704827482169925869109982598418898944 +0346460363516674840943211123653705144516811133167773396076974206411386351987012791478690845256647926914987854519697980693850902 +4071039292859550793054081697582930309815712485519665021072773643984757349601035844973218854642152867868823320985485836576692696 +3566944195916064023455915853671413519744116499257428302391059795726588226281448949481887857567458305547585518788070366283222643 +4475630384874509141748841479483575783195093285020751153893790442309506156006253308563119939383816229293457633418322795791651798 +2534236711768923060748330398823973796625511107263972895904532310389295669901143468746002992855700338033513519817927059960082258 +3266833442506157497412341209730995861476441931081314990884968759007025722755350805078872002993770048685210269397083340942969079 +3325013293836474893127556216824859727670678446693115589979109778870903809827936806876988266059842178470200459657153466761585276 +9339215768239697841992780261560272323641382940735693971437349481277136632058838727826902049882698280374121176916226254815769890 +0579266322319718076679029381516879866920214093645961826097287139995438556892638562807533592029177968784950961959651824138364712 +6905593271581094882293836027956049322801541013654133342099776388830151131142904625837046121178740403372359293970069573073460036 +1334657560229091661514767192486484034874814804158436588838115645203542626667263606421481459341052191296393864355597357106139612 +8983996484498008491962652081920903788269022697096829121085293173454864230335343930546161539206227982332409686050424799265682960 +5979943191308039044837616371196377606973368879788064283861010090419804092295381096855027541458106648574572073809903498794247525 +5748223869403535883463902896159174890497470441638233019825678773597770107171367867175754297335334571074534607008535043181405194 +8795353535341345876592035589428503364517129250540037247550521782811791666374510791517112671288737422099652323046937343991337828 +1845836116381331085732349956975527066319503790930145646064646988078457512408200058689118375881261700543344729871472397774510391 +9908840174753246368222566017712326787176851291551010131314349894185604484963390215007002796394968662213178018411586445058609354 +3648708782366479438386590662421575557958786432758515733410030956830742717789884410593667200352894830011183921069426063462365103 +0863636445701114598272155058937842933641739192649108654968863927721554489981493578009581287965498827240184001075889430327826948 +4493294744917241293353606762738125798775389304371272784848161045309965481110981617696456977856845295663146310544856961560395701 +5828350063875380289041787243953420472517640405923728557572182762701021146981223831971084235740009591348792854248246191617472390 +8142911427486019533916938360973756294199899650632305767565787069658787674361642673323286198646595884519076147156672045347769575 +6919034458315451231488534732525157456948587079445950850088410378838535977516348080551775162637211781455681436512037018188409356 +4896222628077283599302550538329307529265839416312577475209070966536398530653252477467412899117369638571925172776374288534229909 +2854126939661606237890723618332733028245947026454912620083338662672080884075389624244778233027185136679702658181696482687520771 +5486768970237811447646114465006645965128132304833174901323446834886512598955526763233636026886751282286763353050412483339880879 +9146149867375327619762240148102487012789152939324347118132416925423213349620444217738724989375651879154078406666532230695010261 +6432289704281535683765846177747137478857199113830933135896448323481180075359182281747819927805855289891296771338965010648932514 +3498778884149977137993362603088225708342761654666600988476000304267323094698652989046529561743798096047380169014973930605911712 +1555513950736376520645091335370846495268353191373549111643901821633347913274787967705297774512956543219272395543943905097818713 +9824515005207942672277682844175672734608049189360837751069564229080983682875340351162730595292836110710458156809584248318311829 +8644392801319979903321539224060850887494189887888933968978507838319574610389051812420770840579282228222004146980291262806411960 +7170669714045362254284955377287230002812263383500586848752066007279901210618595185664394253569437180949208007446172620486446640 +1197418824383226696240145317846075038217263309407432715400718235690780656588433075117078422196797078588171900080472556706089444 +6058081175983404316870378185108517992309376387807189465675421456144616633676191879370638648087281836756096354696164358804958793 +7175300097350528879964995120589418404250514397730435804307880003642423238815219743208925752113332762492815676234753855313749687 +2216821005143022212418811965038884804162533171952178272981773050431225972337066809470007006624527746167477143326209744916009321 +2874879795494394023670916234680048368860577149936343642623399857065876920729230526286990908222249218870734087284012928821855198 +4892669237695278094261059350844729450055740052868656916863083244548039117228735586420221099748057054754127348342899320517500476 +4913533064284953905878883623486045020791418669162484614426142342087782118817607434327885376004285685360966673400518425200903095 +5236853047264130432207807874987854720636307899392809936830966680235406253508899133299685304059306299982669510153104336419138281 +2635729217127660380760950574877790788724558832203965184259818062945607402185371541729168619518031385767332811305256686570812707 +2508693777459660048221434065348386546516873503781056818039736884680690592725066422073878336659156014383855199604525567079024098 +2527847075125720859307217125972603526269724367871881994978039745425984811500976939283305909268477371136914408464862183620857832 +0690848798245605847434249059051242459537031098176625887386313497201621005955291382938621297556760277294613833001841927601417783 +7028637829951952641999762601338035213911204478792655772152455046625599760300329777350980475951284319951538433620056110253564781 +6305383937871208842219397097772665248616270756493102017303010524320362272253687689906320569651514870191751716750048983549016387 +2503753802832702119325330648883246982515086513140942076923304514033686102637700160768687271319427097046984664384031053585769263 +8859227992404387206024381418823288245956367455432601449940099288001162654396819305512052575309088805570615034227083337938687529 +4650804026596773751781171691707686499798853858855531297662807643678150070418762900082986264876734988853277036828880591174772676 +9388499761915323706082278918760363181675840316081077581226482199990184463308418161663917171550066696858396843063924348798640785 +1339519437101345180144205804260788988338143185013195486237509665528555982435461459049630103016492555659996037806521222234571639 +4775406178328515482958996000719481194780201067828314313954411507309766697327602402398592936844052573242705274081653994583193860 +3528381321380368083594961454997422094241837847317957074543832643804670074418168234251527826193469648447514505161752970468032251 +5644398364843284455563221983222109730803753471463129763873997470655511240512635865533704491564823907699160432034740568090163512 +2019394829622861074339274739867648100223183661088275367604560854273519173101761322314118755022999112270121442893766921444175618 +1024961245677370458394203192048216519899540056189972652416007572725591801341729111029393594712226889998156435621046992279746041 +0271013842729391960551828591764938026380238772791709945105208575380084459912538928688891874547881085895537996185248760341022722 +6443644163751965239785936903668129878996022934136734635016147841655906985263150160687560568016130320940069489352717984945617017 +3980979401244807308375088961101007696165282732048547089807678715095196311609438432458589671418362252711116197050123998645160718 +5029882228039159089170628100687054725011048437854579104323727026278151658455314659996721845525056734089399419451901797673293081 +6285409273407838783004808714400055644398911907669583936293776767386307697838130877184976183405711337976463042849151509723365986 +3789116449777854153143327216807443418065067164460291516731014023725079965308205893342647719238044202553135045734641879985943779 +8298817649314977181973643591691453651364965447794449764965615478663842601481733390271221156996231579991946480360911957202482419 +9923477919534528913583979154285960230181626227828624997322864558028580882337999440750509388244247586570466385544276236851986673 +6266991152224272268084343794242067444247144028060688211987908624847699372423422643841707689624467016900013832905272766427426840 +1355067343839251713050029297274635544953158759007585898844352763675665452680269506694090597773786276467200134181329183129591894 +1203185079814519449730972616794089846681228932967991544425024262788862162483806670387321965112895932667954720701857303315356633 +3010223406691406448732071777784917670640450961064883921707223591238303897553076936064137492313930301276184114003498965768546700 +5055662053584872171686392213754554115803665951484341184481678793730256377197108916198950525496472627926989148952673813222574403 +9135301836957832126464142557708338619444651703425091454666943559384577035607172295502116858050756179608799326777436914985064677 +7849895476822416074994849352092796372963487124157171056152173017480389097568380804863683908585558640345480249639539155208814003 +9261731284405809837137712563712141454654064296465941789875748814378776568326711784539691121352690345339673695586510989770974288 +9504548640513288992288616894826847158596027709105403654435535528958145029279713253496007210603612311147571118952157286120765811 +3172019293671644099810154685400878900781863310226630973663196219151877477083273521217193649999031944551410361083160211498456657 +4584835039206895266033291139088493090671186991332152100943013968139485825629047583367832339953098893263224674847060194281239454 +0086703131238762939716791409510475698044261061184491246815071586146930458568320808697308895974335329174951890370629879266697586 +4531479959438670471013925537723663881523195629756820131576957595320119441034946472734403049979337927590688920672812290238838992 +4555784600301091463114748695085394421438230893318590726172334093673394223159678897396878630225087240466235943323892469006400425 +3181295825258212728965331361945064921751996326740368121061708410146864334211196041567878993280564094949434554682791503673171658 +0842856419453623298809261289857239402872945556557159985688775020115214582384781860334689651493902766220136835999359515682473907 +9925790212755349035787540271290598847673187935010889467948737594198142964082798741209190025028872359505393133888993203341623719 +6881035084279659821037463368291359288972809796102439700962259394869871767171237148065599497261891353342041591198086826562479936 +3071229176011336182119446125694433597177517266017496957596892485297788477134388949109492032269677674633006492819820954408939636 +2158505696670981583492100994722239785061126885782859457540392078035784726890160616966602169759722004423370966068470851220475055 +1608442432121043588281125489656099832629914426535320512741673998725277196703980523715247319219104260826637474643734228192860916 +4370675331345005605018147797477139808981239675873728794694700612511771169428567641131258023552337644203474230406734637422278712 +0072859216860622480346667080043679784914202926542586524758456470894276035984569755855670540260788837234221463158700441389231519 +5908942320852274627106540557811105888610228644494103402263750822407154871367868699882023311506835501899157241704189414346964284 +1936077419723541310264634055462791796583876957815065743467706393867078774982042343936087382789908364464848533080889495741496533 +7254584486083999860932338903788745031776193198150907253247533783144266741445178920158676792459480942978555519260352341102863868 +2324388659990555102109355727630603564463003730580206681960957998377155433799099008210231393030776738444219722412702108302468947 +9926613338642730749665937063588945780266089547872794933113744630542358661411190558266352099886620858549171525338223571617167711 +8986318093141580580876428982707764579287478469073583981696234648037808185140269756651566921560785874161230201920488349770275568 +1902800771331526181562098287158829944843731017880219524960324079005886150803168559590390474829249427791824941894802034999528722 +3088584384403360990907891602519470935005839028378253171884990772710302832296914348751708792698002306050160936562533914379396209 +1408310312115069530150233048818331378284637723150482392301602795737916335332815872089409379843010863320155338585218615794879180 +9677693345069319403701934383954872660888681515065440356051840106884171038792426226463129841417745680354406721408968771559961954 +4557539513449236642758101806291205473048350296831633561735117237579593752211878576489139742035316348287283326781007355249045447 +0883189604094854361797387824593448971971447378881204753543909216846733915206018358578418844775503490330084862478510316277108659 +4392550432502243314906975265564518025479113098902169136391369512817565837562370156090165943691065253900876426946219189578736203 +8435865573452195078786548417341611434423772217538030617156223901185493595765324312422048299670372764076523094427788078350076588 +8758192779916285126332006796232045283091922027709795938540004604312898200544114013208612412744965934919541932903174519980041254 +1905758872835083202758499042134848212716811095613617523820839042848090697442064092232729193928479222578050789364469898951584433 +2042272602260087723772848808702134767220173723358646135775389214656797340171301999623954978758165595263461874252751080813852370 +6274949154731112794816216220066048303875986872572001517499070940234617966182507743418989487331756345308613913050407521686762445 +6252730979488500452149052167583298496816203113490366754560247487872848255355376484138619549701240217905711665471993273764649239 +2281622687072260169420599936083131206454896277499378500769391609676460024680767553958496006032193910593446808391535507538458599 +2191010913898494379461759294973512837440625801801770581499807998348601134363013493664904191350527640096566620000395093356134597 +1660938804470213933500623938584649317535645503810385086838104030744441292813013981381095457570526776722828297499568794267530527 +0049591878181449823254506571309609592366697654558586847111232650386057621118329999460228856081046031697258812551653592183584022 +6258688415014551491572224840400910043380364244249549379338382107530054462860121947719561782286604468083436850199239844591653106 +2286199429292183729722721966817924988059655688863458946473056847154314312282407066615331874808857982581754086039286737461422747 +9602373046472185399409121350660074121130363291629972502459350277036971026440212505022927430609191673069989761094096992091670991 +7527879946650183351260678785085159093046180958175365176059942440628342275638429735328061936632746613485734744234236065653841137 +0853905941635849403951594887838110573652181835969202362039487221504242797299698627659839204874816529447544114385531980406666739 +8062623009337523311886378539061039298966733017052114071143987228104904834551295397466673191503750551657795489921304516776700376 +9007862096833941572335165844081198462047136819292917564132567522715598859947138927117299956642745110763969251319858190814717852 +8138958664951405431541453825461330550794221363483549976099721937150607149588852431715285804748382531904426919820630714135461983 +2082397885435139903190314072146037819079478576247984221600827812796545457214101194496220097049368103574832514769701472385466387 +6896832416290927778053721602166743175733725992261187904101998561355457754417056692268681305083190893002118259512241765998122185 +2636968504609327449982531058224266443456972154252698660084884189652857349507395676672582020179531698016039374573532855093674020 +1136026577034357822803043248989730467073666295507784903395493121839773911132783532427651934810756742564183961335402101819870313 +4917099632663820977131969667158301701831087873930909603855469814730542409624643679930249460380846245995843352815176947927777445 +7319944731827475004706218777611396753968431393783636860295774463198452128755272715443101250558014321901313052738593050746571261 +1707391395815003284391913583753339859531852184651610466904418403752429242924142601415582143805388089376849781779454300111958363 +9869108151029888337540428241180238714963181644033868424652184814249832670934245802862990793758097055499023243559198701686575666 +0187337650771748598070996214382876748717075026419386245187486192182726086987036097255846930650043060886509574171932845313409481 +6310275245723435456702127325230222604362757109683102954203602714184216564293906952316506569915992137104170152492029578927512300 +2305363922251604346990506838961419388192179030700159072409204210540634525740072656419492493606787180195424067534096459265589672 +9014680664163948601215499948895470475969838465633327315676059364457476028555130517706511356929094761498786703750352514595140558 +9277484889618105650227338993928615130344847104186489285414725102606461111639517477747291575877009412511989063198428666104065406 +5967555124952181729420151522869271680843682059283488086279627160865021360567069797183031645357984651583784813809428355166017913 +3607122306789653495447270140672966362274565501229973599229667563911606772081598572491953192767189192005766778789005815828610262 +2082222249317420567583150769393704141643717042001944511315146960110297197944325284159980684280693676403148609684883994123930097 +8731372124960991940185405379357412214932260280623846110357067671501251533776628907031655983387403452673941318358548346602760124 +1976021347697640428375581819297827333264016242688348297392388997119461967525771833238891591593742490277240125530759579876491077 +6072466888350935400785328899402008544459154165636826625026958037650740601926412225205263947146298128779293383020953022202454362 +8044034560774166401511114766914479073842481064943729623980836043134237110449322364783254518711575480004977127696613076710220521 +0564017571074921724777748862479718313010297525498781712387929033234996773668361156592993020275412793744070740135811199115493778 +8419964042103133610587792208097044601548949029168486670192222439144702607241592021192461521821947279760550828661220709620486303 +1088814982734131440185884327578226836944316287501836068430921447613687229952034388566585290882851495307655765844223170274340586 +9005799435401477659962899657617685769970912656575731463413483835844226203192655446200112583102919467583122082891247399200176783 +1851901588702515045318382295536798092875597244431065042731697266394552848998020859370638943554279437980607960337319274852691748 +4157648096188098267490705839256314766006302678665206847589272557754575914579199683296869793220703658904894112352768597742132369 +8723572441033421822131850984144065793127526826673880856427241209325971643349588168485181604003549286943794523645867631822180512 +9961788999122788832400015923298335447164102572685865228181427272076170219015236471640964326542574283167180040163937092128142732 +4937228274511747775697611764578391450794749466871545726021901964860582325608076362650424934625277209649989981489750831225970741 +5984541310271980053712817247539372152339967892775462123252335196274653874061592183413734875908267266920758029830479031847869228 +3871371830143074933411065987051697909188835376373962685096571028998970101211924718499411402849248202339306457377199877126871389 +0586547099952007576979285722825771295071780721616176263387445055392095793671230602073249233658371312728279604207471382798903479 +8923577166944872998618492077791653391106848257807366227129544881151900203904339062941439729248722161475876280041042987870134297 +6237224656556330445619379055992474217471454724969182529987949872722998015945413515067495314489889032179791415649686011073157477 +3003735334796840694489912665109880649358659073702150516968523096869480344821929684468744926264579118587187974938771984262183130 +9005753590334142936036827167253181517255166676962390793299722321456204920350620126047032582007457817410580787154332130609567063 +6768747081081207028623866855036005032042045118427663709885224327750939144410815093506371403873404919815687844934325487062884410 +9939375834444884922802271908890569123942112471025359287053666892829043053920922878243233005965596366467035614279935114942794959 +4228677728256796231027207112191813165638914358249269082781564785198933238543379292199026489661901488181239514397059686005148486 +9858539705692720243176916548886314393535919032096542151105543882501065636878211469917818253828853172821658378951042353768012326 +4422150194025015880156761058170304509425792379790006321176948407538912546088068547813798000277530779888405928477531694464231990 +9405650213736879289008838391184069127301195712710013925801226563743554996233482554161918019454022970208565224871459380574639835 +9130794172100568378934252595907690555743933712658951585006391044055921485334952756122241280828770097902109351842319381201019204 +5387985013440379266230518099534711152536460959269911655397255034626367231069408969413762813968064236154692333107664286960252886 +0837118680763800551465069959598175835527467398611671504215567143391659195926190188250898459493898805547983688580472819756799921 +3903220661160023222988830014776994424185300024466935537043911148899196047568212527843430862631973366330579525805431481011727438 +6908486037368642919233388174527626955887348839535505480142124695672332446751550909649591921180619995217291566458925431968029596 +5922782959375057616516378703156580986104482212435661177276107010237081047649806919032851751263569965116169589560831643100864715 +9530369870366296156375956282332841862206458152569867266578623568655520592049072587837930649946891321926334502571392866551701600 +2244120737959672078726164762991283327806243441107419508409939471693616693409630730506528517088302223393878360169050233927904848 +1173648771365582895309968552846929556758030253414605906336548466169612035971381029462786542081116525705113252983939162686385109 +3234155840835288921699357747660762996672264982174457829675917923339715647526557875639742064383339589189545825487931401530995501 +6887665436803180794927923177816956708553785264323074693008344374916559316617894764427215802134641031792988573701240438184249750 +3839539179042881405914564139952865952209303008409731497507386952470654995467985010268759732897359668659271914262252626200604099 +3901076984640950131960333781644223983532267536121313347358475229440208418673453842347657506344090138736051557656821297434982077 +1623302641135870181311409682200177224901019997404700215147219379739297672524540935209651730504196225871915072497095486880839456 +8525315354478812852093356397223120386907828133392270722811606375757983394361481913342296750609970915686272616757634461450159831 +1758237483830901977324883248068822612744747099358422855339260889327800667524049577373525949059825015390160043063670312221072626 +0817646239152869131112173902854770980862701701464607169211772950069653293230684510278012053087035337608494974396015461535406222 +8646346589715796030816122730522353992998487085588488148264344066530587174128544229529685414458831257128748560990578804587360715 +4892783957104711928531643465336739867007712821809759300577073458900066680389265377353301997705565938884080611069388312576416304 +2814927389151100129042633757615682635990656949553591616128856450443418496471442467646240189440950521511107482659253501599167684 +6135116334119022986012296077975055833220297155080260438551907096128989879928867449175546615606752828933549694825257606317586967 +7016532424149169529714586507287588315821039767281860867212525388739062009398857291177966375322314074508942295009826535646980545 +1130704846522761465358987904002837038112315864342341500267273512280490182439691698104185495677754466174462509539951393350430049 +6239545452060864474289817540605900770875649791603624701849860428246844860967392240963173839859434643202570338001444647069601535 +2892256768296868079082781775690843873409200886346998104928582896183217369070621962623861890767458984640863725872163427837396161 +1015987233236680540127175130575507867333440719260832000405122222181144252353002520085749044160442813319007733231316952017735844 +9207122991749764391450668652103974356514593013095625877412964336676292277200308352297929615022995501002585354045748416214004090 +5010570807508645991981283973843421015013551379801238469792061968346208698033924105198332080219826873915414042770997813678587105 +7079764317445013461353198084076807018884451655619023138435629015461147032511308684817994462582990789356614515379970417351047112 +9725881506883451817378068264565401988152381111185798791532824098438029254469346518084626414715068349480830207550031462251028658 +1504691090829009028694497048664643697386279163491642380984787314683252474807267403308118309227599112336474348292331546087385709 +8374045238326514850352601439525770150385106432876360117179641265901618172845807500617241643296725102357385086749024223414320695 +8751924711372854963768655608131773099674117966941670496696797649406607434691461307943445287152706412809962107879642575743136123 +0303974873561466480968743445273196900705427451656962370162651211903195479518829395352847623644931113876694882674530035776397562 +7062275706646629882491960277069009716984996924439240397590784333436313250958554553224460478301293892351431447612983420608299779 +4021998564670601304459878729468539224393130081394097452696802435298562203672631426231047502881058781296871522825905804208338252 +6603870763394362689777694406864771464497251023441752146535341971346977029094361489321326128197028616266687437237475295317363781 +9156020188117437220553660891624641158468448699614504995148457693527457205903178159270764484168386506020072977552224361333700137 +6800793585132251294965485406539737210356860431874461572263005112556102550605119443415354337340356018964066943951864924412299915 +9176918467253843792621229869758475550809947953676328982327607776920014653501974832672677357939313552991103607402118582223524673 +9686597280549256712036750220654564584483912204263497709158167656617008449221355316543489486938189978195357412962551710086697028 +0388429487175354035020158054103632924527177834523214082900849111108930893956355794346651605160826522047668675831813135358949190 +4345101694469873179969332042811556018423606297108948984557961826035019597070396285751114257989897953296299318364378614357933290 +4949793892609892089229851776026934561460317731511255552266868980253783700717106802153694611663582999276204920297363252094371934 +3674663375108601609896202396621130718019413744942494222052103933540452779973933501106619925311069127219850907767043719608829470 +2702622493254040904859153876396184987380395898007693621320399251663876437827374821903867019473977298529862021085159803043642262 +7628804430192406674682789691745692230306369379190920544277057351743344722619473702262372514469670378356778783948762245486971339 +0209360041654178829792127599208780855624409037374530019447406585398770659754872987726192651460619482209256452092276066423476738 +4709783672042319775995641852494077105915433063897114034300326610462577068371066683331006070063551265931785531029670403665686700 +4587667003896460717120912220270889981167563292172474600789871114756272885374551741764032376074283701965139574996577462686971467 +3848976224442081690472022609003207496720929495444016043645809920259850317579964113261377231461428538754038582648249855596427337 +6172335672098748490848298205075949543310509985651798361629655073050078700179132155025462638010290501496311312192593367695422649 +5726384573393389957861087954363684393648224844030047057212547523809090733733447320925926719918185564312722906061784825834541657 +4665490918503684461998203394016725914244105391374685986007968106627529172068516391306150797382538751326421691707344779240051404 +2641961236056444783029513171308212582932163735856854734301365261361011265730957017358847540597925397759304056715637265630175924 +3143119314616534179607705689766936638499515116711864605104806787742064610741185862931157231900890917045797092416341390041404007 +3447931172643093407899075956016941324761309449689246711224867235309324518097331543836073039264012951510199018054862544697302228 +1563566785164424421738528286186718541346261171578886688118979569809183215506908080307606078120020216388497864724764057557754295 +8570889786931244194585660557574434060147391448332058484468219132867607862446662006468299620134301595151046310252336800694251427 +0217713452224504960721091364387786965013258053103569599886166960333008816549340813042469646283329089138949995982273574974215868 +5241363057936964400802812697301682184281492778601196784957952838829973528993067785327299171845639921011068758599950934223980914 +3238619506166339720940794008445086813283016112070588367871958447351766426343723002443074101469688320695818179265745434758910694 +6296789680362763149161376071869217199662771928863547592423030630832336241218625794010606091635986342053359908043568336874619758 +5796450483018638379674081988788071939094611782491186354599577562913830254367966241420426194905385522339051453836242132016154075 +7739106134294761084702741107955615651072406250165155261201936784282832131480551569227393410480076418655073059966098939360374229 +2971567365547701845803783070635993312664824380381023912535236337973918393748125160984639017693662692774791712740599492208482304 +2270720904622266004044552929046382853488397030569658054130832432158967100326401536494483965188041727224259066348964879067583812 +3443273270836744114169351145812281297836105568675664537098362750380900035541986757124762712416133474277811013584828903165701857 +5742987445704693742780012691654778768416076035850059897986267208927114643299349752186944079949266040605774961616317552910045136 +1240573283641630694358812830029096031820509750547875205950719721469746524236951030334909216603563649107173637973757255912252238 +1182181309135378156725140117731218230316055150298076621940456444204177883491865113883523054776801063888819486505485385264913416 +5069068146388574137237425212952037225400371744975152454660711335647864580574001943714998326035983681508282903233341771279568745 +8467943310622656536228892538026964919357667642832630682651110048817452858559725652837991052187308635773654812551494387013316194 +6440134083862480163392226300450457593962112666903507568451147153007397865298280740371802995538252604177619078977124651051346524 +0537206772933648445348418297957633068071300723364120286017796847418742109018037795201017609886014503958468715621463005263787770 +3432430202920291906940764640954727327787760258258401409130943189103009317398784374181000619017547537895364515789076658241461815 +5914935373373404632064261928025631881068410722283620043865276873695737002786518062855969064577740316535280772509182462064465423 +4663807875203623784262624547123565729447932492414543360035449489544264792983057557527120665531457725601958452570287012086637115 +0847474551270439717567791117916716520890209772268515126243374020404961236068669964591032312120458856151122774707229179093256702 +2728583785414355071386563956562688209782901746373262288816864290881579324219562028197868974414398025024344306680144426091625030 +0910829767068265611067617086376443471268114912189289789288578161296371552096260414590275441364898692653660772972831143055685254 +6907552020126899986435407709381912657932246467804972165765350364308400598137400904853881086873855469475626923756560878726730206 +9692227748520363042215131444256441345425607520902904049752565589636006403442938604002826527897011684910364030121356648587505405 +4764025506989776287250936345001157192241256998927684892678268726606209680040843191546472014984020704327316352200558172668843000 +0564503985848436331698791790813182963520559967441699554268886929254925633860614440306175833628563304317763920599751129071094309 +0175566287557279025857970584297243285600759435104151831150200001725607992873593941524916608511725999483859291253760003734973775 +3039168535120967789573627753936889933310390638519872912077737282540474993630496322839461848810678646381314301370434612679202453 +5290955424609618789728513231303753525180260090737043547498273247450093929519045624882089783253628274100484171694237532423254859 +8186844862044986653314802892230697717326617551815053423012041608272398516591496440428962043914698069230013270892802838135287280 +2942112717174269473287862314996229628874313075502005461595688743732840097634991389852224318945641142059923309434297254976695326 +2444525231843962298275031069381314438305678331941237253189959661616931936428403908777740475422459580996785256969423138340880864 +3680179310627154010609137293613898326769365871171324366981855057912989303388559760676270131578093527806064422528032847530674921 +9355461118722504639190973677696806900807813249020915988601326357024391608006515894656601520223498473406143763133984605551760635 +2692839783034034225135191314183486878688437669767719161941937422862653760924569831725084821460846216076710658987697058689127356 +3364076178555288032798058397361297765867389733546080821420688209714329880465424718281983378741966307965647446727248970877582835 +5097154920011309551648620026419749934729546113147672841385317729841476832905993191729886098778334663461209723905553676387326787 +7544038203057755483948118938898802083672133837458011190981229303171157034238018580240354203030584798053880317332961525265980136 +1511152526450600575245224610801100379737911240153008627074626764499468695975779959724757934622234502314480579416277867481722879 +9208761275469748679977983637248710137550354113725122267268928582906191386758550109656015747293931779066403562806751200098816649 +2774371960428126935364571996703554555526862912399267217503390978028481995519553561409174881144281743002477593997449474113614320 +3283118387697337618913008345214573134792212133551845640272542182183565756097005950085429574121104212579366752791246087630740431 +2251626647794796903046417171601708089033102353726791435541243544024082843362831636356376144988414260766152555993385246920667885 +4120430579803988192186052185336786436260523534744839109920724061087523726726856003315563861271636470368332064799342319979701963 +1178460847308168095740461067148790390275190816124283621727277092177096823111400887004927669450338704196432823728793780107282726 +6147466566392578561700534815888328209861843963585876273190658740165656380640111544922854734365367040797352367883442096872576161 +0655254149992445465845820920429897376775083347655359252308973271695549200271712623988532809944592500421544522541040542744651771 +8895306524774577797925949127567789976094837338117657136712633080384090456221881872243825996086082147423517476054977573177854050 +4943011385246149882784947556915542478920754812582717541224558316858308474902436139789610336015128572291595765038163819815572793 +0059519490142292292642467486355644357871443222407272677363258010549846343690410511589487255437861629574221414317395258487976995 +6701428890977392343133134256358135045673673123158832116303929634062094950503008501398110339118434776130231986975400156621462276 +4809112204042804726404274446015570840535663272343219408139885535879522105193162064034829419212674983141334515042809663864776730 +3339872236842534294842100648778519220364381792736266281427277880995347117460315427394400908444602067720779504370559025897150525 +0666515892628240976196163023835072227181720931267205367481763695114784962335391370607825220441784558120081030826294430035145825 +5990171621923618467399400881105334235398291318764791324436841208478784739274728174080402657482105706012062680090302017358495564 +5800215155555227538662802534678916888507797284554941341847639680300462071370712985768701847640442454877861723356457893345549450 +0044372835991682943791367236860846326267985956993372513320157207230356385776345982286545530591945027191940491720657627496371424 +3896431871159669144163572836324937737933585239956934241358540063356296466656555165061326841756648545874963895216091891197215432 +1150436121846416639357239779525130732656046114770001886098939931426129354428928259723637803567803101198938817466591948073654097 +3680333838160206864509221215320873122528703603282455751545397796927467717336094169326962640299653768153008615924472102530867643 +6770050643269265320774249712789524649226429216132578192388680218997460246361157967129125548877818859824163987287642561738796200 +5254046150685030619784205850259746232647288005274167276588953501245671262327906633892459399349248665683674586756619925085832749 +8138659441098017401153449742392984232024716079021347321627336986000612605589176674195767657558188526805373912850372987580206973 +8191739475819996816485468724355025309335206345790673864079265551398839303546015301031614690697079405116794058344817857751170058 +5907733664187032783992586304169841870946162957846844623230375351751398150941213221188521406317499729525031329275753209552972135 +9176007624492506072188440485436086995355405038611315169617103420795642254487038087275622057368505174049950711687063028236514019 +2054382102455304130470152360854926810408620809647732343016107388521668970316223874173994646760209951211428665814705542349614124 +6010507266966547829863386841978029050310064720150129583679215294838434102788460967870976228374996605588773396271433676057289822 +7051841412308687295953020826737760628903413518500966433370038504670238126933304749517485860557144247313921158680957030845329616 +2360033770199629503223807832521648961131842719239878810835014788911969787608442436353630358920568228223028658040834409931217202 +8400997693934004696590555410764211165674028575041130093079293183383749718414250933932351869983505392135593326730456694681893830 +9601966479081797142729057700273038016579775812303462568369987589932810195102540708796028133906200420900692102185183676170434137 +3162497370959471401670744764602103343725945241209609871641803759642428128312023652491828868062451307296471228615569464816843941 +8764436487815154921343571706294950245330020417561336192910050884197950522678946771691143370350696790098631293864395203215663546 +2103973531056753606278933160819590888661208989653023859755262642075600795082475977389553769715231157147229187302967554388198370 +5511263147464601148510010944839861149087736327442223343582633059022202904665113422199247896714934045390179111585670544907398745 +6503691628140930095878935309503466424485388886339832328600458315631288162890181616057639363435222140281269282576385033650710680 +8454254370393439429040009560609022779619788056284224520991161927353411842168395626223590494290282053470410540843450407987926472 +1336825679355920509182270187363852809609898538144457557442453481966013998014732868105005901821373796606751500831791325616529203 +5075118330341336536977717509239821947081842843667207368538939749602952519028901304254002381236022047524790390418089280097834547 +7087002693940685994858725839374363930880942852293359924965925052590869503751762743229260822706873201650092697209849276502693145 +7306083000612674447155484200177291509744727003567573915026132438684216504412932709214471251820240604475149792423248249870477794 +1828750345492089414352178700274996717656637631789061874232476374372589026121784200003799632748786969419729627555600483655428983 +1419038605582900945308948756881180557106644399148478031842482768934058982366460243721966998715761966320233275607842960609086807 +8995674775072395937621113944922442969450956475559169579744736615353848691595206952225921509080923588771792361528472949801095259 +8071164393093218807836177998870397618178243287174730595843254576188688725044187468313573761652235487642008727514328298453902298 +0249472421551084408818726321837167660068433212182140863918498142778473568559604363644028430498972834010437189971676814372359835 +6326215711122626023900879284642848379045342532983675870120785391214889560676166670449442675798343547201651587376372310330260972 +7721128349367234491190961548434288775058240627555576639847795303346657927088312538298685663289068965153779510427872075661840307 +6404054826004851129562529146035965093659305313594131922007840749997852785915480413483418179403471734301385082608331943522033365 +9733723661694274986939415998932370600151791965983225598026282848806043753357041009447678948117256393500635321688798336983220809 +3023589135170563869561312040343087996767334919517638058142422305672311345678035221436322642357974773060658747505711133889241231 +1715152441111565992971519735514891973779991073609482568950260394204089469526267416519043590653468197703445241031998318436540928 +2571161687670398639649025206286059219618343667065497532146361041751088315806996390315937024865023472982359206847895714744923905 +5104856285757044366877940768642231044057490201353054349716001939164041109356173546969025877651803874584635636362767779008126317 +3656921864783698643075571406752016536758441120671704957195627803118074461526120679475816346733176098378409722349965248254591396 +1131443502066788861338718815939549028179403039813213172777877033453234020243883715396074872970863142546452822669185069959899184 +3828769910844736669658151259692028871696603091005326667495981150974845310134146991259645265882236097619326183020932698293809389 +4299190605742357746915386971343055529972124390299804481008342198376182446383261925929177219506772125075974364335839610948010146 +1147666764087177430198110110312435274731031579695008275499991697667495238558041459480531076474591001063866865437151222907417625 +3118583599222787818632900781019308738830040805599161996484071877144944130661026998282751748208682924005109173737574254051272267 +7248376535827628635806240316333585049198937287552863170331727642858039762809709721675292461634836370227092107000659180675306750 +5306506627623780068062337884640840561088552876565604494700675337745509144650468822636638698294453362040534422173031206225551013 +4252579308534696009946359817557643262143167696605231236359117810698816646524895198187607892477097108960784373761400944824549798 +6045449932335488814802338206074710052382801251742328755176048705595171441928353779134497634248965450262069632636038949744898591 +5048702418591439721957110373280102402968469372594873031440576890966279374555142674908990757259647232567240212811527932452059539 +6445116473158402926922663638150560761537491240522157144544441504700466911488897981059188305240921784775901100861995605851341634 +6426302952753103378949324692632848243080208624864503932730319385221633332864218296337728836033257029589381148577572779689079838 +8323351033213327371073881110283228611521471023685177945561596153462821296338506612181495253533007296541471850977144060198153791 +8612240193336900124220751825865213792796718254928148410457543169565765686381156512821242185857562689521331372459681870446271120 +2190046770619246800670427384048126037985607253433475697587520496744179478706647309818755439429545326720329201172200607765064227 +3736184856673482672738889087427014657874110745468152414492168023597661163943017362999706603380493625843592458433287608755926735 +4182865681401614720438201427164757077032257161772988732629306857669039790289782177374506368443513627759379215518499475202046737 +0697566863573366009839193988358046426820897141853585923998497091914817356341676241151406963597520055377063092845547594101697448 +9770510287164808256026738019695567751276805483623280478598925336381177794945569958800804342768914035606587017994256909693642474 +7259755154611086941695255874435534803016372667978524879504844920733928636400671846029963027849546724083520281554956394251995619 +1834558695670914755649864898844629047200426021450454765139131674876570357846943407935142270035129746721137853801869970041785717 +1163596604514706992103337640896730977362836499853948987124318776421720842943109071494096297915872890046802390417171851693498985 +6696873506860390067654571204036998680608912603658516851469732917336706912396900863519518863153116679969409193499475151912748965 +5772630153097284117632752487886616553301227224710840592032831301443809681968538716385350119500103381352429438906070006049324510 +7255320449590155185339594353774382542901912346562333365165993746566717810379624006716063017965110258762932735063223456498216112 +6248309487192090873881009634314790029246770378814629258114526635202683322798948006269443152846896870196478753064865917440440787 +1079237303256511529274839846470858035223497145513826461412560940325017754834231423361883666751693338383865225187216612119160015 +4861549012935871901840283860561328549067295489644918336638431557892707608901138017599623121810750153111482390969696067122547247 +2439131148441383234199376711842415663942174805857098114579871600691759196917341291603802573327405138562282722593414751206457843 +1973359935209045256671529911507316084304214657632392500349841226204006608244837820483129185333807659672251730206136609040933140 +8184943256874541309753395121370721662620513066937352652876771594490437810449353276322830929392380590748672786848276069239565457 +3348707026381104836690412750227080237859968000391150025633563394577801401905026748085615086352851413067595432050498561008571529 +1877075667972855586652155165911443897166143801340467259805712033287524340520335275436517702758812944056177253493418193171478211 +3774854369590560985587771159245595691913575127970445498501177157738494672527414527162836050852813684096791146417973149365483037 +6704552131555310027219368793280881514598163193897955852905718685913917573430057096937340959824715120372466589909944315684960960 +1012314375846207604646749185426484171598374093369935297204972798525329648052886568477644707532784462941904877487489747098535818 +7985674284714583813663198777955800874049562438949351977791559431711873042288148908962573859023348239888106006645129281470473018 +9254850687404139400561382825175974131012329412859199274634520950110017567085977669352012785318630525841745235957179742713982397 +5097303689162257167085390435448404263178199429357071875772715774544733708487138600770119134943129539543347602041901144161660698 +6953181183113203630678272715731094392489433607360104735402092320971396494926265205286840279679199637087859730680473846380532205 +7565033230336604389926974822292700950491849340699150720775179860790531037997637976970549940150909535407513425455328335706561853 +5335368312471755733760541805372968424193070274491865313428788002453279122401760512494622819480016105587606929438634932015824839 +5559016338160351661853688320039654018084969722536423822579994200012801736919709694310831609907486201946501673508356764142877308 +7309191546539260723637253628477957548575327772499530308454034050815650925443083418507106015799454347735223841084017225944595226 +4885590707691548323545253419048477650698836445425272829459034786263150039138385336170173040262385017882023012446601786508537725 +3159300795992899648874466508404624507417341674483549750138299511054547355595935184376005929821246479521411349307469101710751249 +2708412392547375974662585574062716099744667256888486220537125770970468132844596575936704205294434437646542619212125911160088350 +7671842861670510559275736664436986661209239228900436842961873275642777838933287515196746049787877411927981310119576400549956135 +8080578380146388148902757671260230173280743467984486835312292349158474503715607238743382799824745964056080082925917323860644616 +2120225379169780135148292573265658608927358543522142151484031366388491618786494744941370627406622031907792989518445311039563243 +6616422310981362438162790258221845503573731451992162868203601740290116853659138924079987226446597241351646843845278977024986205 +3662517640661488061073964114703410205367685498808240378692251201166863090113025726146269622096930731719426952075506480919216671 +4823643457908716999726557603223790753609939423361772712423018042932577115436095139459068730410826645568806300379584835433450946 +7837676208477360145796134097352072195957133041368812408314333472555459775670645841696822521516557015970698046160780950308863139 +5015658403765286649459660652152764801017680991103667912937430519322186712931073677796456215078436603241360959178629806771058487 +9139921128024002577938454673706047243620589115000240242563021016654786832806633509254081676514023612790990852595917560180876986 +9308821204883025320496874045455981233709125160954382611635568126874562312377547977111739040746530325193651004237373402504537024 +1600821697316131396065115975797494969376263815891380490449703227459763844691726208596151555303610430036532384862800860173068687 +1677749011530966588655330008218462556189908006964342216676682876215290543872066380630113889263010720992320394875173725292986311 +6989579261987669045792589013853182107944124182584456537044269091571439065320682853599165887592383283334586801282099984394392022 +9970108676029913047954776468886108937529661369633484559390003673582932372797656385704195055160295258401674267521236080828952316 +3273802761420937902459164150004835781371893878839475301617292925707508874975102558388990001315786419341804043737604037226331752 +6423865093791333509337877707981916567162340796885934470115945789596479789320653277617641131277451340371426117129794514423620761 +7417609321834214164793102707841568625675868448913528641242723253295495191476269874558175687931888576975179732585669568870748778 +5922089337143793314961512325922930026645734559888635155138481555659443901618808201784727019382947895019140665125084159195324704 +2832300412363246058905063308416630201890917641050411420603351067801736891124820850891397186056454796477918095532199343583413766 +3842538349880871712437577001398701888177344881522702272474861972439339253087204797211344636084653095826769076765966355059432859 +9877839498202302366738674681533509411775934456542381162185787945980551914209016996885507489310451917077029188324180322160260432 +2738878897545393488539641042595991720479279596803533846602159358145022104838297930283221202267258855473584700375502341592186375 +9062102173051107870783580788794675784912283028909776152369743147402812551267763802726764393893702046520930303504745751378996798 +1890158543698671287785156969862722836025364759849336645190863408137034806850238192071351320453303572785270717307117565718365448 +7734822410504169526033583218346170435610106103279145679136488844339942839493180608845290017442034674025577300763459944350044123 +6105040904995754702216898271414142776115817083443246834852579568391309141621714380298793479424942862844759389997283360409266478 +8207744090633188685115276563756960203356370566871533095111186389044186232441179082078908516925531610020829064403059153715127765 +4543411669625489070844963508370766554494552051662899693726257799474616282059274838670819153036396550543481066774790569529843561 +6679799651036598201198464287278166599830430479287605906552407498711145045497296895091710143423667516678729308766311555682391918 +9060618446032966067396024483927774799271013972963401931215359008050857228820472502367068320857075813753280582206475097498167537 +1091834892775689353192772704190529392359261014072163618697328287354942394830079331974933337512857161868992496906607708010073398 +5364177554337994653512816619520240821994073186844625407311661314696382001907220045017648487411581430791143507856735567122196867 +0716761726451434192525856251744493844256079661428358229774069510316066898590543756088198375973992063361152435222465923662351366 +0043075513893517349925411233398957648025096112448548886678072991181902374596438596256297098774636238148028169588473828501924259 +3228516645884143195864667466124866845099402543607000135666586926382976262509368071827459582454935921975303881150829096536404067 +4761157693052962556888050028734514069905365690844215214316263823173976897101122538440327270223709317772895230031391498145736860 +6385041084622772519780334652630409495229736116311103527030831268982936563366735822928512879378203876837625284271193343076105495 +8825229991999801614347782438381474867852878074358430022437396713162807802543161593009219312576950358519990247724194904234397934 +7703801976947027234960802861233790319790676821468496141323682139060568458559647230530529267618724263639883376817372167884440632 +1726382143017323548108870010820096752312836426102452450577004449101845698867822824844060001600494219691513701486376765683162295 +8567344046439933818792026021934172754782928648841755910876641284145941585066904498876784179002951113808421135227067381608911653 +2236981696764483162752804813037353468784452076739585261632267090804876266610444767882886160729217192498345880705115733282372008 +1313046830998564657547840785183730714291004250275503590948256987454019097344722968276149211207582987088443854985798581722550023 +9063006406664758572850022859344148370428972822517076773683529207252067208935050546823673576957790680951020623419459032466629849 +7938928170423206022080881849589066531884975323736816185913375521669990715327020849225286810140422793741838143465182856377816749 +1265756842388175742586708059491362377918488379984875832030848543287918941984050070389106581268802594158962582407355351035627049 +5900221712437059899622129468224343840497771816212975521659975058333318690798732737341876177817630804815720469182652538833483463 +5823081639915099173836211725045578069819187508359785075471465778986260818861591546963352470179514396250583548650281488440780418 +7451048170517299540822354367273854496577812259623792325601325690996083083112151683648900535783537568629739368276233311118065909 +9084734714613092289461178939653038690130944168935857129538400432419569476969252314501208058863985767404598559353985694589773895 +6277214607560240011967714168838143750302367595949536752213657372427124984199709048837186130780108450270594932950252051594320147 +3499572321088417920853211811454866152496225537457554175697449661675274675801653044218258736968600625898775131530277707042339508 +9914225219653958408220404955503421597988800462882509224240059485399789816322467527449242209991095322026817971575335077154317985 +5278081500892846628897442768350495212681297210154604830667333056587120720522354514329460517681155334733974540010110756237411297 +8210591237075811936724693019310304435150990235368637778807792760313222154371335709089813727563208995319466715986861635780866968 +7932415587435911762210037637958044637519791039364219987373646146500303943912999127265553435145017969397164413915816752545161718 +5633226031400350789184112011648210881013020831437183038308240362781644283374155015679893305443987813011631237184023671550613020 +5811978080228290171238069706605813947244025818423549831774560590994763911374360626408512454006214897383037052649605527910939842 +1578662832737601740265954708879993575835470810729170613732770615041114798158887175252200232752097837948350852301754510131346245 +6068409444406865116594371496646576550470605636603063539678037419994055698404550683594340710909264161990867009969259182486360989 +3207255402572128867597708386762297362519921843080894171603067736003753278183424031926979188196805787920726466251458690002582913 +5008569364534625206093594865389861974491057988729642501378201086357626134901180260883065306275795854872408871966970358994916566 +5260870280380960692056382943423819321600456629197713258529958092905135072192290151897612011642141070383635146934909632500071674 +5103943101344555719687493820066438065019249811470335254469779344109290667032018362593087359223818942995619770011635033602760781 +6090182191502983738438010896590866621707943859659299906860770756177151013807688207972939771298998138528654622092955429850905653 +1626336772125369872523005387925612023085181385255260458074393235131647700974860337649978590067937668268507156384372177447503498 +0983088220975640726485831380650348574527016263360984693517646111104451963030529155545125362875373684368744288268392996464031787 +1886577864886560438915496753180364296870396016073972814584707172757602216484353054194841318491639088652070784986581587056423963 +5791295955798452846006486957884980659241458306340044696019455166134558465794056766142717707049752847050948964499027310212291201 +3920118159051788842792018209898781792646763214510193250573669914454162275192491103856842226701566784572791940951044194717542411 +5568425247114245514540844570366702834465456557122445652286286614869534230920871827766118797945440453053831301603663599611151007 +9132285049335898106348530191639824752961756612096827245060794650887002292087021431444313048295402370831165699527783501924024187 +9094264576555332744996979181892440388769625070877328456545115444598432716233979462060670021904359658071557042696819752714761359 +2624335032983933790947468174067700780434730280286305837296068262033116687738482347338258135450536831314508366201724139587815922 +8450751384747855334597310320588125802084226440657572189418133991724784364158225409421748567123411489197359560319327166452150553 +0217315146965805878176534045386582646736519057669936823885725039838691917663928104666774688631639185290691596050519297245051431 +7940957013433199012061114563562278128253591876081307949974283942244584406336676182042124767131849195077146222822951756668914757 +6589005956437167623060683298552386150740712051896264556502825752701728084586373026820140301271827258402697782005508526864025995 +3287288894211530920495178421618385358928209601522908652144815977173646436635300032777312309529559530424309089857544514218168330 +6097483328468004536631471410495796391660358670051698715076410248502303660016161800128810231929174703618080502422800241933644130 +2956392224013095847029227680428083425620941588942686433369479814315411693725173853101111802188051631515982053459377355277916857 +3065681935968907368748287973480293783213808382487086015879418794067438804225330554229750223350574245985145312570124344685276455 +3489772935059313564818602631010215526615299221297140830316899208960430528821219213991899112889288129943720176337014173173174332 +4169981630658215328566362976636435099613640630157506404256612156966851420692144492745499377619802506248375541742742014068421649 +3723697831339666759125999969874386488250045997138226100381868783065151573396084764792101954730971063666073382268822664370648202 +7695739092394145422409597895972497681500338948172798211529024633083754418602669479252615594701113082363308526566375385313538692 +0362024243582700927996091721261779876357892824920462321831613846936511858949877169168914710103805138231321626700946199996026230 +0910370398336603134902288593437904852970471827636244578441003561376156126397047213862430430449454667576391124197185148345471598 +5028346423876572027639326365391106837855425740087158529449602646500698308039624506892347711322718143846562617911464289132024496 +9181010776426759091535139982513243227866023899159123168014651495554175929065144279128447458894513674919834462361991122972799428 +6472902683846165829490192503189706567753176937559527683694767361120216602239730653948099377610225798683453077405631209096231256 +0512986051426035486790488833906991553793296020140460296126591780195814787329857676072922129878562425136025882880600984983973989 +8577540121631192413042201337007929873227061283325668352988181356692287167551269236062168964313569524237889901244379824573640154 +5738145734013901847027973765976787682776812683421111101939034836663608749968365412229382487929512185391212964474327857768911300 +7562207836356806317921677823535902895646261255973726228824124734902468677105214682457780953879753523764199156291556619449942205 +6685144408630396033374384521831023348395708816803320958823787110609396335187767019703187308157675845831700883961350733812554544 +7576609828782901349340272529157644117188745883108516505970478068227119245704450471362770646680973620118556831550370068906691429 +7387998131808587529731801781598911046784196725282943998624870759033897379214694560999940160160935915403688360574784745832951209 +6248576464881189569842693329569180599977760906738941622134746107827192298531798906163755685774369390363347404463590563452485459 +0534570888244129725519284614466994594106901806409139995601464274045789187815608635654309801136434736637205014712781222746921224 +7852721958335248170761829133326246797700730414740667697834509581576475365734827900004460335634027879413672459976734498386195835 +5088370360545200433735093090688791665393907039059272593032100436084501418577206986549399579720624028466951070544585214365207165 +6435187129368614449937580538296933531792687165143536181239511296901417188887649499928950174527974591005214327286330726199831884 +5864261175041853165789209708620714822361777843032399821785843235985913827357647540975103877312914819841724359018056154229990484 +1936832320986190570216413377747783409617074645446275675504398782817281167330026961325947267465552277788937638823143961712338389 +6643014994016028760819796829700070956205113377466899907953033114796754643995517690305975245695734383895488045495784881027449542 +9277149772110266765516542199009389979979428729803481968242094114554310414335680714665255584623073310801300516821617747378223514 +5122582985363185134103472499467534154970594419472963412571152321765481331992341844237099548533622925469977982035402603376599366 +5702039170472823969912601643296899307773750835106819267275831078282467998094066465029472821615682366477091470622470049719032042 +0671010358945492991105729283517652015315441687350701252921272273957729883415005085140318681745681298665587818639399209287652478 +8504952151973256765543113775190946452315557877268395630007206112950274261280515123602637574614066439170518948486082023049534264 +8318785147359309244073883391968368385030412213804815219561275632927036049996919071912768810313118985299449822580618372563128123 +5447965794084022881336224721150430474622954063445132529997642171980649882452452874371660709010271333178089011623650142945593513 +7498570392070031176556263724838104407401975655807188803601068318852915004571680712468602581133705435004279673319305506280782318 +4235251770160613000830130983063100984485301111420207302571915861863284245935054969687285090784775829404258632032504825073887582 +6407248272722201582954889873840796724288126199508498948167917317499903480984445960848498060154084891463403285778107348687889003 +4953941330396268815099409977237615027521463257528034758197037642240947224961515275989640733601052280398818164960445717239008728 +4426630124553142687273205330644281511721380282097626819427510520776002629092574526215543555846304555626987349130475830717662512 +2679622415503744585776560549326024439456730222065789254179516934309405880579097989297964155354764447981652276792153719461894070 +6549452078295195952851972101892868752466073329081552637836554870868329745518970415001754631788228704301414087107528862687179382 +3095714010946058367115405111675588241660359378724218257897889232511664785827286570897205547229279697287663245465265895194075831 +9805465298621739062011152177896594580714703932025865262409510756369094551491315344002976208292437754134178483455044310999687172 +5635853083465443752399385809613532628000929554168679920157180193163062291041706935484519604033746044464716659982230583915172977 +1397085494342438160066950214161726216316797535747161972858218528442291370670389809452231490128596440810108729216233152926963393 +8718938957831579949398261083744948669476176529513240710990560722588644755296402086806852345746538236347718030640062631744819555 +9204989031916871283227517942830744003831314993547893753197955379403077174291901637842637655437310798500528596483721526174221178 +4510621681082500438705444746385938892179962383128718253236867948364044872547102979732643922151263657382171953178711857903512864 +3434625913079139632847162412927858083774910063803492872036606235419074389757800463882617655159555904544646003283714069095719127 +9422478480758793975004764634088848124200701219293815383644557399151474644429876593122326476162568364525117348282565684006439054 +6415412788931044743766779624337137193856769272687596658353208728053926044570228051289974744446738511898266628888688489033821326 +3857187055764486798882511807937125159502970249818799519647825055901143034107396715341111836431533491237288074873324479254847609 +1944275217447109672414805304798039892872048551668309961639053276921203694927946551682999672834794575568139407200344761188832584 +9228691574818073805866351085919567356330517627511140717776424135867621344167516969731782408297076690641510925488519425339786231 +2831239904815704920016412032913992948413502229392159186483640311971384616648328934218616172408437024641339341534952850417631408 +7800709599423404547224077175920136421077429331304204846667623312748910375272177983791546978316206296185604509872479031731294077 +6784795879386865023891006814459422181459242147354385223330262696056730516247499993593952360113613078289229711721789291466201911 +1558378948527819137236700414103122090944390583763494240148255786716027072401897565958932034145583789267171948650451004479200278 +5903365269957297061267790413012843976619330012086443873201405355132327717135407147467813950090532723390786355726062359803253439 +7629287528827946090011668159293077488731123501345393936384022425933097863996545653037874008416991412735073016080615679561676815 +3237960418959875052085168366152912866396485668534444686608623522780325138572281373258421046475238324527598957994900036953588632 +9883457218912143598750744649289916045155230666253998012127292069605955397882251577278616463161982956480851083203582599294132885 +8739605725967415654345383552272209454198363876790394143551885453633595477692847087747048445889713841117432214889427740276964122 +1808274594398149686259653460592417452536265711085053636183889817336547379067581749366999937190487929782077243956107305394452893 +2858306534503555758138192142342374976569912519114070071304001216131565171144485590883323767986065555672710513575942087665489357 +4757258115582473299797872077008431880804766429204591994348523186470015406253571125582684142251809933836340635261386229168816736 +9918555292750664784762306372865895993392873092492120139178044227677315162874458415577767229742354738329991177067335396671747156 +9915416169565774505455198608946138326580556663002878103141442158950284935423931276623631554768708518659417736110868144091121408 +7485024844800932215930566743200643612646843426043824587026297306446562930746643938917961270550045568670866510426684185886721448 +4920398501344484751530060228048138752162844188260697740262204586350188489261031255952257367650241279996726021255197979400713310 +0983903945365766675649828378701431579004857069167244696371192650313930100887222144395396003002837274898192175630515559492616368 +3277569441388072350944394279081011821982637576381780324461779633187405677617459795408379063310477462661505822241372624056397980 +2796195339856562934096874382820282192099707799004934067504550995736513197478848274092619553245513516215920528915358178045259427 +3947135872153511247151284668940911981933985156149720562664695031030349744372110088157223178406720212021960705751648342245477823 +8276198303392861443910819050105767637970397653898744484200995946877348235373325786715347188697829025445506211703973576651655150 +5882576032689008918318143363475006909501115410116941599853261283394744451901101910507633092078494500214632287815925595536310673 +7269241743888980922214944937348603959760552838607959592411748713582006971089673926445144319837129979120595791416170081356544964 +3311215693514569183444297282757452024106434743342415838798070572517431245405339373780763180400593713529558089381582197573125752 +4556878108283717136754864320383151773421100605854818531410424407658443836201366417280243912680383714161438889747985913112617159 +3825283938152818066103999819485641639048153915591788440799327104545750772861261496085802570014233362502408220258846139854490942 +3543037705426699624325696485612282411023745859388735341967646420985563945181345313960301854453402790876180716518598169356488497 +0055015399759018591540911328290609447855851641187622864276870240973176403939507548117764594583218172186919643238977138111158956 +2302146157347717928300258596580246250649908644352652697865369736710811431250945078622499892640698932203651495464626034751579299 +0465216581077322245370388400939469195489616593652097640401043603078806904801413336072374703228412879808024013691780444525050754 +9151727760868811886689437563229171390965138504950138407176506255752456724326383897323049258585289255126700265024238726662681731 +0098281899254404044904741984338800905867702052389334861534065540083641450439874866038625038945827791603161398328731872690722059 +6264530562489216043969280034408943083142765136686187837798682491792210809601751533837187815038112873969767271421029102693650921 +7670518311894906730059859298650685509520663627502700465860614043829130684092155123515969024766529965326820247830045239762876384 +1971827918374425559499103800305418434180845901819771222995898394549615763182780848478726307902754377493105414864115497763562067 +7293146824231836764324358936471102982745003549454252992884038807314576844595566006507289403671236553892547396334547842381936455 +3707012658412106034610065750021436552038057251146149724375405389299956024255098042568008242870114719254029045709055991603654930 +4543749521242879802728408392725236169206369176046813175364511678612424177154836269263324760122983569594445005273913013269664551 +5817700093174352547416009939112502133121085536020612539401905269863944981266637678387919942591042471679114800424206674615903746 +9928390417067700668674925186060124842260894166668581468768675095262594932684909013159566891337437296287921942274275342136808279 +1720526807320641693668140417766107453595978158154018754776221360462162393984770342801365549647871062173743896063916005240392253 +5161802386530577435326894842370519298812054283209695964545050386222673476579184628919106024296128426895608065795872894836272477 +9269809290510364760281346041981538977140734635535715877861564862122154561230471413453427434440292536928348809500100637413855174 +0657501300841813074935854851050163894991818276164751987873048063138250805404584707419292446697964047248725308217946641983844311 +8063478785147013188508380911686106501438761736788881545360437305874844044022285005421389985126661063912349587081288388003345466 +5080385912620164034274035663131591931589292314684519240800453976434478145811035864455399019210261903984106215732682395895799522 +0939020059406813121907073513079575778716665344964094359764782266431575550874973231091097656538411338304404129653958312868999842 +6732798933518323978985303323091966916070134400750942228988862433672654847071015762525448860254590537863809085645618465872667306 +0427846766351683194895724811056690695837239230026578986973494357639196637007765612283664140977141629336513743191474504552821790 +3228155682944358310924291228796943417467684350283874136741330013804595358442242749651649995689037646356343539795964269040555905 +6826145487746532059740843858150338835296508366342272235981293613561407683050389559856243442342524690245207486156018191382164551 +1327436225221742602074234993619036983345996345216008664910823535986496447271949044534929738194453356131648251656209560053354614 +0407290886807276555505472513777566509192309345071661761386510676177956004136233159240180697853305064135526857625954524710679122 +1431829687708285619275876929673787963838663779016128696006658789855078135858492072768308764926825429711165034052613724177837083 +7028553048202580189681827868563421125284289066186493489485470631057243002992982744971998313406439308961352814738730662842546474 +1661580089658576037549874036611720164076275368701553799369961428692585978010787204558502174932891500734632543309652429899761996 +9575856899639421670892327891209797029282817824375074240277705275435359082312316792956844954585652271368228646822716619012170165 +4690146795978834583378953218274128128954879679834962721495470431115747652300230454490265247933455376976025418712883919785265770 +4032584676449148273007032590154174755542691092611500872882432669889260868344191769763590664159630537010368972815582431630162563 +5692592732110530293190433213941741940871216528267789341138499704212959459631375151781937020467215223500973204249438057106028338 +1541214818072382006149334712979978351719934230083959861254175062060206728245934867359880240685020444293227864055618527079589472 +1439159705776204064690488345048299759772753492079024651663143703086754589449249599931908249754660757813918172727140178033198709 +7752381183500066741893133463291861429042158260704598873907314527361438231014952758222297919601552459311683576335125432073345960 +4158788096315630807867178963280856579732177587633674289101034755608242832707107748491269005626588055241208204418618388578035469 +1184416032139115028602210049000593619116279959516862765906011705138739437332038056873961010636018153261556031158803924660448426 +9834587809319862013631879808865489908412831755556199315250744409755028410760389172898604230817719091796600511778684843691064279 +7878372956127669698977302339079670592653146391325443980293253311254734925614678750486528888944734310378404782150993544089766813 +4482271911480681906845899905802627519765113355182171607609262740960268207890619067150293516596923655844247586174848972864721468 +9552003526630991149245651109495761934587784128507108647124483752936558263106644305431230364441437558819641682185001628863763328 +9406159315820904843204929487522677107941937076263988953382649335050929875295580457118791755887589396355358703932935557257606830 +2969493527901243167542109903168828628101917337300403325039813279857934939104326409957807201457446262732489661524241158979368142 +1833967145487476098973136162241392322964264292952490319901422154921305200229075272133864200487657774340560812039475075659326308 +1445298822625141627089594319278576573094647719202883678449230305616300998398218250958603519589651710614887336894157702428315107 +8911019753589361230572967964751571906635823516517521975275494263948066958905939807605210390275340148405377313480598158489558111 +3183749609096747980513864052412964607501407465024842349303716927046134047076464086834827581454812880653390898896534483223922702 +2047220404370553586495348686661844320582343653984119122227275984849504305399708851775077220602618170548438073982557547603633042 +5484875538490583169391720574953649919139069950121342298729189319935008363006908499817188567294646844902804708973781249800855552 +7421001424990409022663446235185073936721530809635245828159482916215742480785440259606637437758227633658889520747280770098045723 +6510075506635860479703887783619213078585799368504850589990946726994909109916359460180863720392604659818835051505356439580781411 +0959165291367850253561235537386721365762207442766902579631829064237987397582522884081947056330594794737026301952693630585102680 +6267281924773054128920399667616892395906931391070201721710734680164804934661829697133693962106573047827000567454860592220102084 +6351225785373403775734572811446819619019190839393144125015360556285521375890386016901082233720031340998488590003458019399286006 +8086983669350678861956334882898616321436714400384547300867927315159417274826609618496301870293049853960758679069277021904136960 +9865037759852510085988938641077600576537641357106366728530348981390205491195956901092388717027996104357442748303975082427786786 +7507248774842522159480897459767378325396763974897420116495369220388118224704985677597657612836164641593242643705906198310715923 +6020323897854469493762019753971728667497338537420217765895884555350482562903234559598367966289598307357713584270039680501758042 +2970682482901424731104641663111768757582340710358846705532647449734646971482108058532929343635001949578504462729212526976463810 +8406644630764947233594179128313043631264609165757699872474368034583055609364865453910940072958562666156078109884350362008700655 +9106501316131398908994689175684333822574784889714347654697727209736986308940910431037486363365667132654371414795819563310659169 +0137910472839714679161637852622472509459014895991142745425159229935069751019575455036034662579025492228994125955644298201357138 +2263663590472781654218931217059804728469684174302359039764853349702219977113201315914188584584955078222068657185984247622363558 +5695450824338155861977114741562006985207639935376048162114526556989656274727076392276059583216303897816482298413238883403403632 +0038975862485843345228895776545691226502347548540923009663654944132545600827380276828701039586345737400863528654887451057233197 +1285772332269416234665537910079978608222632602148452508934499816027147308767944385913555512454637610386856292966968437445496548 +1261330191618765210009782014085845073559775965093415001665110717532374517158996315004359428915981278340620344383170562473911188 +0755483943667647640579099438554263999127548734337936583257964438443463208543269429173892355146856191237124938489437234962714824 +9129303544439064208938552091032859116474732583566245269345387009587018850752964356086348467165770868170114265169858965115946288 +7279622065007376862875602615836848329672073134886504804366938182665036461895999032293214820132094992897811456738921945565996030 +4834236494451378286976564913106384851412866607459667235230175488097172277734513000370973157558689076209485032473704000163914074 +2906852713508769108670361019771730001144071549164484831831379004788411810506441972725735257048988599402331202363984166872400551 +4374843284474261559922201408952633150671870157517305411040726961843624607898601648184347808307024923212656070285299487288795732 +8117441033938778257340076295628604371272466046790715921004843853378457388333862596453687093388500288301007959254642621709441228 +8336419205482595246714478040728678515920231077940527413442313540990859460129785367310524821300597125039005330560255760446207185 +3407569957745240672748410048085824476146107644741791845367520753456514888607145322239479922742610070735465164422006326154369011 +8894279996606564122245246476739025836636961740221798143384506169709797876615015522415620193119156094455114077417058486277270353 +7131151035004395224877087554662361771017971918239145723682405673667071436258453271309611849685373102636336093172827546720740657 +8432145665345782054212385521456100690288045272048653500410117161030682916822394178179943934653293870095835416016828701651397729 +9807379993673315841883406823522043062628694861759093682354411179777001696451399181533177926653740164108712792568617753057780749 +7552198060883514609633356358462724322863739238474217533806217264897669042731651388657558396154686026363774639912794923252267503 +6291771443785740732590982981719905524567339994411151615939323485160260431350712239547320702908777027534814334898810771281406391 +8383725943909858701756050979518960102350055348726092344362770497395426060226252186716626586574811491995835598427931237638307288 +8507265667315900242180544608612147833573934012185340789193936346356941286917069481442515796977733286907179390046007293777378415 +8688298391307343027481331662068268845813946664921272848764804887244314914603468930763919183778517730411504413547554154855526591 +5310568546507610904043747230819248926074837842921381365456929822505503792963441504203788828448597610860322974517666735027993699 +8190512204618058514563016675739391233696406975567198996502543146118326981238682542313301537034074950744994938638623004424018628 +5962797353210542352910227068452267455627736914233254078275501019026029219977380762852165264848013890176220808626487330453830607 +1392715231741123546207682784414187261145744424815836598165304144212321073536110482416294352751979189695534837507189501132643925 +9547196873348778230662094351866444409683500705986806451682346762409749401895555972393170493498341109191510645940668680160665767 +9096955810744288162025152776265225979561511052982298941267330985571152782374720461772752985049704669084322641186449467889515212 +4360617959299125510229120098324080014685874269740158186892010379118371918256414709312045505595343772662619039003425826619680875 +8242651081630156918805572079983037638369031262141838260427869758050055377235638965872717517504788429694521145838583513263909672 +4433259455372438105974775408627724880736064277338137893108385178827104479149305047828800826485922049778207824411348676510055718 +8718825984885049258859271868511156347573558547616835487996237695354831720772501296121171500663173314753159506044722718279173640 +2709996430110563394535123371054969016136174141380219513450493806426805948941155396288218826477132071568170532718023856256400877 +4412955918580719390710872526700485018418160226844433869987075876676044792011973789419662798626013873183203506118837233134120481 +8772819562426148199715932002776622117594716096791960417797687467419822085071157480539482033047913473604918965910085487367720357 +4625085282706988812822930138218466374524023159847349699030998731244290316248877616127354994943514784160430249591960166158827428 +3012297335233487487686932791672504848939891571342804916267615897166810277253170819233520663757428230248104405100538854660927429 +9123919446207336590235297840044149569787830675448852079217813254125078565940555357151112153929694801251132171203795549941163920 +7476175369012980559938118310990110840174599144325883878065361105628152837782089093135806082805172562067617245231771061838879589 +4768469919871257284902195592516344833223851928691778696654759047583702848545509080899254152913598427216007302226235522358460750 +5223927757227044947866784478462919350895723643402842384138354740120256079056226884847266693352002580953694527348195695223607106 +4705073558122375415417200921965563405533347843275197814453910296613637292816403322858277400213988846375567124674868467361795016 +4745728385156852832029629080429064757351207656991841817680115495315482039644479548944488439788311653187692817074819840714497361 +5386314847182104599263810424039609228765878994123179215715094367283327303020988006589792974721368333841513658700314945433634456 +3685020221252259142980926069027074077129168059113331677771782227727933843623094566219277157186574916058687802285827552124321505 +0794558617931775588320039074320252238014802597836590634824227249703483529574186006183975509386000848443692647358501225142361167 +3942679708028160995866607265771564423203677879968679929985370044267348240320617033379072586756373490054605230219409903866795469 +7536599276764983130315949383875187945686502994166320326189211280377933687730984141460802381512939077916932152569652666724046409 +0292377825630966235158683241512971091944539929600065707699283877623772501033680549048220288927590692487589517712276010516389098 +4141777627623117777410741182897322646935774432337593489702909715834793849902052358894916963594941828827118210427424755222982593 +0263099400776234259640984881940007947968250580544744304077736076557953616095573458814306253535718606350692981623255928091096507 +0588327094996262431505059042095661828596107333578122924612340170790916991622052806671333685104366780235136494027277232872825498 +1523246308282008538061408160202517775966824053244715248653671495274650673682535042749033828795099010214859776685551489176975506 +5811209762314534687747281829159103097186141033482266881873367741901377849867980657673782571505747169485443159028925476795003998 +963436785863456565690613348237130676523955561372248056220702510606833697646989872355666869136182759934340379879840576201228LAST From 44c62d838e90af68e98cdf6f68bfe531bca15a7e Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 31 Jul 2023 07:43:50 -0400 Subject: [PATCH 0108/1222] Use memory mapped file for static file server (#1632) * Use memory mapped file for static file server * Fix build error --- httplib.h | 136 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 118 insertions(+), 18 deletions(-) diff --git a/httplib.h b/httplib.h index 22858ff177..256405f3e7 100644 --- a/httplib.h +++ b/httplib.h @@ -90,10 +90,6 @@ #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) #endif -#ifndef CPPHTTPLIB_SEND_BUFSIZ -#define CPPHTTPLIB_SEND_BUFSIZ size_t(4096u) -#endif - #ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ #define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) #endif @@ -197,6 +193,7 @@ using socket_t = SOCKET; #endif #include #include +#include #include #include #include @@ -2148,6 +2145,29 @@ class stream_line_reader { std::string glowable_buffer_; }; +class mmap { +public: + mmap(const char *path); + ~mmap(); + + bool open(const char *path); + void close(); + + bool is_open() const; + size_t size() const; + const char *data() const; + +private: +#if defined(_WIN32) + HANDLE hFile_; + HANDLE hMapping_; +#else + int fd_; +#endif + size_t size_; + void *addr_; +}; + } // namespace detail // ---------------------------------------------------------------------------- @@ -2528,6 +2548,95 @@ inline void stream_line_reader::append(char c) { } } +inline mmap::mmap(const char *path) +#if defined(_WIN32) + : hFile_(NULL), hMapping_(NULL) +#else + : fd_(-1) +#endif + , + size_(0), addr_(nullptr) { + if (!open(path)) { std::runtime_error(""); } +} + +inline mmap::~mmap() { close(); } + +inline bool mmap::open(const char *path) { + close(); + +#if defined(_WIN32) + hFile_ = ::CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if (hFile_ == INVALID_HANDLE_VALUE) { return false; } + + size_ = ::GetFileSize(hFile_, NULL); + + hMapping_ = ::CreateFileMapping(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); + + if (hMapping_ == NULL) { + close(); + return false; + } + + addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); +#else + fd_ = ::open(path, O_RDONLY); + if (fd_ == -1) { return false; } + + struct stat sb; + if (fstat(fd_, &sb) == -1) { + close(); + return false; + } + size_ = static_cast(sb.st_size); + + addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); +#endif + + if (addr_ == nullptr) { + close(); + return false; + } + + return true; +} + +inline bool mmap::is_open() const { return addr_ != nullptr; } + +inline size_t mmap::size() const { return size_; } + +inline const char *mmap::data() const { return (const char *)addr_; } + +inline void mmap::close() { +#if defined(_WIN32) + if (addr_) { + ::UnmapViewOfFile(addr_); + addr_ = nullptr; + } + + if (hMapping_) { + ::CloseHandle(hMapping_); + hMapping_ = NULL; + } + + if (hFile_ != INVALID_HANDLE_VALUE) { + ::CloseHandle(hFile_); + hFile_ = INVALID_HANDLE_VALUE; + } +#else + if (addr_ != nullptr) { + munmap(addr_, size_); + addr_ = nullptr; + } + + if (fd_ != -1) { + ::close(fd_); + fd_ = -1; + } +#endif + size_ = 0; +} inline int close_socket(socket_t sock) { #ifdef _WIN32 return closesocket(sock); @@ -5963,24 +6072,15 @@ inline bool Server::handle_file_request(const Request &req, Response &res, res.set_header(kv.first.c_str(), kv.second); } - auto fs = - std::make_shared(path, std::ios_base::binary); - - fs->seekg(0, std::ios_base::end); - auto size = static_cast(fs->tellg()); - fs->seekg(0); + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { return false; } res.set_content_provider( - size, + mm->size(), detail::find_content_type(path, file_extension_and_mimetype_map_, default_file_mimetype_), - [fs](size_t offset, size_t length, DataSink &sink) -> bool { - std::array buf{}; - length = std::min(length, CPPHTTPLIB_SEND_BUFSIZ); - - fs->seekg(static_cast(offset), std::ios_base::beg); - fs->read(buf.data(), static_cast(length)); - sink.write(buf.data(), length); + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); return true; }); From c7ed1796a778592ae5a122287a16b1dd4770858a Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 31 Jul 2023 21:28:33 -0400 Subject: [PATCH 0109/1222] Release v0.13.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 256405f3e7..6c074d2795 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.13.2" +#define CPPHTTPLIB_VERSION "0.13.3" /* * Configuration From 67f6ff7fa94cd9c3196bd264db29e65f25b0403d Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Thu, 3 Aug 2023 23:01:40 +0200 Subject: [PATCH 0110/1222] Fix CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR (#1634) --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 6c074d2795..96cb915c60 100644 --- a/httplib.h +++ b/httplib.h @@ -6723,9 +6723,9 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, if (!line_reader.getline()) { return false; } #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); -#else const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); +#else + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); #endif std::cmatch m; From afe627e7af13f78225076af1639c9ea40a807921 Mon Sep 17 00:00:00 2001 From: Sven Panne Date: Mon, 14 Aug 2023 16:26:54 +0200 Subject: [PATCH 0111/1222] Avoid a -Warray-bounds false positive in GCC 13. (#1639) The exact circumstances when this false positive is triggered are quite tricky to reproduce, but it happened reproducibly with g++ 13.1 and 13.2 in a close-source SW I'm working on. The fix even improves performance by a very tiny bit: There is no need to copy the std::smatch, having a const reference is enough. Just as a side note: -Warray-bounds seems to cause trouble in other projects, too, so e.g. the Linux kernel has disabled since June 2022. --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 96cb915c60..fd7f7b7d06 100644 --- a/httplib.h +++ b/httplib.h @@ -4986,7 +4986,7 @@ inline bool parse_www_authenticate(const Response &res, s = s.substr(pos + 1); auto beg = std::sregex_iterator(s.begin(), s.end(), re); for (auto i = beg; i != std::sregex_iterator(); ++i) { - auto m = *i; + const auto &m = *i; auto key = s.substr(static_cast(m.position(1)), static_cast(m.length(1))); auto val = m.length(2) > 0 From 6650632e7fd7ccffe2a6ac263445bf3ea0393cb3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 22 Aug 2023 19:36:10 -0400 Subject: [PATCH 0112/1222] Fix #1638 --- httplib.h | 43 +++++++++++++++++-------------------------- test/test.cc | 10 ++++------ 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index fd7f7b7d06..71b4c129a2 100644 --- a/httplib.h +++ b/httplib.h @@ -487,8 +487,7 @@ struct Request { bool has_header(const std::string &key) const; std::string get_header_value(const std::string &key, size_t id = 0) const; - template - T get_header_value(const std::string &key, size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); @@ -520,8 +519,7 @@ struct Response { bool has_header(const std::string &key) const; std::string get_header_value(const std::string &key, size_t id = 0) const; - template - T get_header_value(const std::string &key, size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); @@ -988,8 +986,8 @@ class Result { bool has_request_header(const std::string &key) const; std::string get_request_header_value(const std::string &key, size_t id = 0) const; - template - T get_request_header_value(const std::string &key, size_t id = 0) const; + uint64_t get_request_header_value_u64(const std::string &key, + size_t id = 0) const; size_t get_request_header_value_count(const std::string &key) const; private: @@ -1710,15 +1708,9 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { callback(static_cast(sec), static_cast(usec)); } -template -inline T get_header_value(const Headers & /*headers*/, - const std::string & /*key*/, size_t /*id*/ = 0, - uint64_t /*def*/ = 0) {} - -template <> -inline uint64_t get_header_value(const Headers &headers, - const std::string &key, size_t id, - uint64_t def) { +inline uint64_t get_header_value_u64(const Headers &headers, + const std::string &key, size_t id, + uint64_t def) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -1730,14 +1722,14 @@ inline uint64_t get_header_value(const Headers &headers, } // namespace detail -template -inline T Request::get_header_value(const std::string &key, size_t id) const { - return detail::get_header_value(headers, key, id, 0); +inline uint64_t Request::get_header_value_u64(const std::string &key, + size_t id) const { + return detail::get_header_value_u64(headers, key, id, 0); } -template -inline T Response::get_header_value(const std::string &key, size_t id) const { - return detail::get_header_value(headers, key, id, 0); +inline uint64_t Response::get_header_value_u64(const std::string &key, + size_t id) const { + return detail::get_header_value_u64(headers, key, id, 0); } template @@ -1906,10 +1898,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) { return os; } -template -inline T Result::get_request_header_value(const std::string &key, - size_t id) const { - return detail::get_header_value(request_headers_, key, id, 0); +inline uint64_t Result::get_request_header_value_u64(const std::string &key, + size_t id) const { + return detail::get_header_value_u64(request_headers_, key, id, 0); } template @@ -3894,7 +3885,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } else if (!has_header(x.headers, "Content-Length")) { ret = read_content_without_length(strm, out); } else { - auto len = get_header_value(x.headers, "Content-Length"); + auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0); if (len > payload_max_length) { exceed_payload_max_length = true; skip_content_with_length(strm, len); diff --git a/test/test.cc b/test/test.cc index 217991b2b3..e702e36c93 100644 --- a/test/test.cc +++ b/test/test.cc @@ -211,8 +211,7 @@ TEST(GetHeaderValueTest, DefaultValue) { TEST(GetHeaderValueTest, DefaultValueInt) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = - detail::get_header_value(headers, "Content-Length", 0, 100); + auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 100); EXPECT_EQ(100ull, val); } @@ -241,8 +240,7 @@ TEST(GetHeaderValueTest, SetContent) { TEST(GetHeaderValueTest, RegularValueInt) { Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; - auto val = - detail::get_header_value(headers, "Content-Length", 0, 0); + auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 0); EXPECT_EQ(100ull, val); } @@ -1016,7 +1014,7 @@ TEST(UrlWithSpace, Redirect_Online) { auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar"); ASSERT_TRUE(res); EXPECT_EQ(200, res->status); - EXPECT_EQ(18527U, res->get_header_value("Content-Length")); + EXPECT_EQ(18527U, res->get_header_value_u64("Content-Length")); } #endif @@ -3296,7 +3294,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) { ASSERT_TRUE(res); EXPECT_EQ(200, res->status); EXPECT_EQ(LARGE_DATA, res->body); - EXPECT_EQ(101942u, res.get_request_header_value("Content-Length")); + EXPECT_EQ(101942u, res.get_request_header_value_u64("Content-Length")); EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding")); } From 30b7732565c4630263b43c34267a605a1511f794 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 22 Aug 2023 20:19:07 -0400 Subject: [PATCH 0113/1222] Release v0.14.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 71b4c129a2..5a4b64a35d 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.13.3" +#define CPPHTTPLIB_VERSION "0.14.0" /* * Configuration From c029597a5a92e48a3f373a615b82962c2e35621c Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Wed, 13 Sep 2023 23:33:33 +0900 Subject: [PATCH 0114/1222] Update the remote address of www.httpwatch.com (#1664) --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index e702e36c93..c8cf9e02f4 100644 --- a/test/test.cc +++ b/test/test.cc @@ -442,7 +442,7 @@ TEST(HostnameToIPConversionTest, HTTPWatch_Online) { auto host = "www.httpwatch.com"; auto ip = hosted_at(host); - EXPECT_EQ("191.236.16.12", ip); + EXPECT_EQ("23.96.13.243", ip); std::vector addrs; hosted_at(host, addrs); From a609330e4c6374f741d3b369269f7848255e1954 Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Sun, 1 Oct 2023 04:13:14 +0200 Subject: [PATCH 0115/1222] Add optional user defined header writer (#1683) * Add optional user defined header writer * Fix errors and add test --- httplib.h | 36 ++++++++++++++++++++++++++++++++++-- test/test.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 5a4b64a35d..89449452aa 100644 --- a/httplib.h +++ b/httplib.h @@ -737,6 +737,8 @@ class RegexMatcher : public MatcherBase { std::regex regex_; }; +ssize_t write_headers(Stream &strm, const Headers &headers); + } // namespace detail class Server { @@ -800,6 +802,8 @@ class Server { Server &set_socket_options(SocketOptions socket_options); Server &set_default_headers(Headers headers); + Server & + set_header_writer(std::function const &writer); Server &set_keep_alive_max_count(size_t count); Server &set_keep_alive_timeout(time_t sec); @@ -934,6 +938,8 @@ class Server { SocketOptions socket_options_ = default_socket_options; Headers default_headers_; + std::function header_writer_ = + detail::write_headers; }; enum class Error { @@ -1164,6 +1170,9 @@ class ClientImpl { void set_default_headers(Headers headers); + void + set_header_writer(std::function const &writer); + void set_address_family(int family); void set_tcp_nodelay(bool on); void set_socket_options(SocketOptions socket_options); @@ -1273,6 +1282,10 @@ class ClientImpl { // Default headers Headers default_headers_; + // Header writer + std::function header_writer_ = + detail::write_headers; + // Settings std::string client_cert_path_; std::string client_key_path_; @@ -1539,6 +1552,9 @@ class Client { void set_default_headers(Headers headers); + void + set_header_writer(std::function const &writer); + void set_address_family(int family); void set_tcp_nodelay(bool on); void set_socket_options(SocketOptions socket_options); @@ -5672,6 +5688,12 @@ inline Server &Server::set_default_headers(Headers headers) { return *this; } +inline Server &Server::set_header_writer( + std::function const &writer) { + header_writer_ = writer; + return *this; +} + inline Server &Server::set_keep_alive_max_count(size_t count) { keep_alive_max_count_ = count; return *this; @@ -5866,7 +5888,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, return false; } - if (!detail::write_headers(bstrm, res.headers)) { return false; } + if (!header_writer_(bstrm, res.headers)) { return false; } // Flush buffer auto &data = bstrm.get_buffer(); @@ -7105,7 +7127,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); - detail::write_headers(bstrm, req.headers); + header_writer_(bstrm, req.headers); // Flush buffer auto &data = bstrm.get_buffer(); @@ -7916,6 +7938,11 @@ inline void ClientImpl::set_default_headers(Headers headers) { default_headers_ = std::move(headers); } +inline void ClientImpl::set_header_writer( + std::function const &writer) { + header_writer_ = writer; +} + inline void ClientImpl::set_address_family(int family) { address_family_ = family; } @@ -9110,6 +9137,11 @@ inline void Client::set_default_headers(Headers headers) { cli_->set_default_headers(std::move(headers)); } +inline void Client::set_header_writer( + std::function const &writer) { + cli_->set_header_writer(writer); +} + inline void Client::set_address_family(int family) { cli_->set_address_family(family); } diff --git a/test/test.cc b/test/test.cc index c8cf9e02f4..6d3f58627d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1592,6 +1592,46 @@ TEST(URLFragmentTest, WithFragment) { } } +TEST(HeaderWriter, SetHeaderWriter) { + Server svr; + + svr.set_header_writer([](Stream &strm, Headers &hdrs) { + hdrs.emplace("CustomServerHeader", "CustomServerValue"); + return detail::write_headers(strm, hdrs); + }); + svr.Get("/hi", [](const Request &req, Response &res) { + auto it = req.headers.find("CustomClientHeader"); + EXPECT_TRUE(it != req.headers.end()); + EXPECT_EQ(it->second, "CustomClientValue"); + res.set_content("Hello World!\n", "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + { + Client cli(HOST, PORT); + cli.set_header_writer([](Stream &strm, Headers &hdrs) { + hdrs.emplace("CustomClientHeader", "CustomClientValue"); + return detail::write_headers(strm, hdrs); + }); + + auto res = cli.Get("/hi"); + EXPECT_TRUE(res); + EXPECT_EQ(200, res->status); + + auto it = res->headers.find("CustomServerHeader"); + EXPECT_TRUE(it != res->headers.end()); + EXPECT_EQ(it->second, "CustomServerValue"); + } +} + class ServerTest : public ::testing::Test { protected: ServerTest() From 0a629d739127dcc5d828474a5aedae1f234687d3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 30 Sep 2023 22:14:02 -0400 Subject: [PATCH 0116/1222] Release v0.14.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 89449452aa..feeb130866 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.14.0" +#define CPPHTTPLIB_VERSION "0.14.1" /* * Configuration From f63ba7d013842bdeece3ed74cb3ab03552abf1ba Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Oct 2023 09:58:43 -0400 Subject: [PATCH 0117/1222] Fix #1685 --- test/test.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/test.cc b/test/test.cc index 6d3f58627d..0ad56b1bb7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5439,9 +5439,10 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { ASSERT_TRUE(content); Server svr; - svr.Get("/foo", [=](const httplib::Request &req, httplib::Response &resp) { - resp.set_content(content, content_size, "application/octet-stream"); - }); + svr.Get("/foo", + [=](const httplib::Request & /*req*/, httplib::Response &res) { + res.set_content(content, content_size, "application/octet-stream"); + }); auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); auto se = detail::scope_exit([&] { @@ -6242,7 +6243,7 @@ TEST(MultipartFormDataTest, AlternateFilename) { "\r\n" "text default\r\n" "----------\r\n" - "Content-Disposition: form-data; filename*=\"UTF-8''\%41.txt\"; " + "Content-Disposition: form-data; filename*=\"UTF-8''%41.txt\"; " "filename=\"a.txt\"; name=\"file1\"\r\n" "Content-Type: text/plain\r\n" "\r\n" From 20a7f088cea4df658ed66b17153148b75e64881e Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Fri, 20 Oct 2023 23:58:06 +0200 Subject: [PATCH 0118/1222] build(meson): copy 1MB.txt test file (#1695) Since tests are run in the build directory, the 1MB.txt file has to be copied there. --- test/www/dir/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/test/www/dir/meson.build b/test/www/dir/meson.build index 8339b63612..e88ec79fb8 100644 --- a/test/www/dir/meson.build +++ b/test/www/dir/meson.build @@ -5,3 +5,4 @@ configure_file(input: 'index.html', output: 'index.html', copy: true) configure_file(input: 'test.abcde', output: 'test.abcde', copy: true) configure_file(input: 'test.html', output: 'test.html', copy: true) +configure_file(input: '1MB.txt', output: '1MB.txt', copy: true) From e2813d9d4d4a3cb7ee5b1902cf44a0b9529c921e Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 23 Oct 2023 16:43:12 -0400 Subject: [PATCH 0119/1222] Code cleanup. (Removed unnecessary `.c_str()` calls) --- httplib.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index feeb130866..e400a2b602 100644 --- a/httplib.h +++ b/httplib.h @@ -3326,7 +3326,7 @@ find_content_type(const std::string &path, auto ext = file_extension(path); auto it = user_data.find(ext); - if (it != user_data.end()) { return it->second.c_str(); } + if (it != user_data.end()) { return it->second; } using udl::operator""_t; @@ -6082,7 +6082,7 @@ inline bool Server::handle_file_request(const Request &req, Response &res, if (detail::is_file(path)) { for (const auto &kv : entry.headers) { - res.set_header(kv.first.c_str(), kv.second); + res.set_header(kv.first, kv.second); } auto mm = std::make_shared(path.c_str()); @@ -6992,7 +6992,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { } else { if (next_scheme == "https") { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLClient cli(next_host.c_str(), next_port); + SSLClient cli(next_host, next_port); cli.copy_settings(*this); if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } return detail::redirect(cli, req, res, path, location, error); @@ -7000,7 +7000,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { return false; #endif } else { - ClientImpl cli(next_host.c_str(), next_port); + ClientImpl cli(next_host, next_port); cli.copy_settings(*this); return detail::redirect(cli, req, res, path, location, error); } @@ -7478,7 +7478,7 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, if (params.empty()) { return Get(path, headers); } std::string path_with_query = append_query_params(path, params); - return Get(path_with_query.c_str(), headers, progress); + return Get(path_with_query, headers, progress); } inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, @@ -7498,7 +7498,7 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, } std::string path_with_query = append_query_params(path, params); - return Get(path_with_query.c_str(), headers, response_handler, + return Get(path_with_query, headers, response_handler, content_receiver, progress); } @@ -7601,7 +7601,7 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type.c_str()); + return Post(path, headers, body, content_type); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, @@ -7614,7 +7614,7 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type.c_str()); + return Post(path, headers, body, content_type); } inline Result From d0e4cb3f079041949369cf115c8fb1b2a608bd21 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Mon, 30 Oct 2023 08:26:06 +0900 Subject: [PATCH 0120/1222] Include missing stdint.h on fuzz test (#1700) * Include missing stdint.h * Remove std:: from uint8_t --- test/fuzzing/server_fuzzer.cc | 3 ++- test/fuzzing/standalone_fuzz_target_runner.cpp | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/fuzzing/server_fuzzer.cc b/test/fuzzing/server_fuzzer.cc index 41f710f431..3c0e948026 100644 --- a/test/fuzzing/server_fuzzer.cc +++ b/test/fuzzing/server_fuzzer.cc @@ -1,5 +1,6 @@ +#include + #include -#include class FuzzedStream : public httplib::Stream { public: diff --git a/test/fuzzing/standalone_fuzz_target_runner.cpp b/test/fuzzing/standalone_fuzz_target_runner.cpp index 945ce175ef..e8bd5ed6ec 100644 --- a/test/fuzzing/standalone_fuzz_target_runner.cpp +++ b/test/fuzzing/standalone_fuzz_target_runner.cpp @@ -5,9 +5,9 @@ // on the test corpus or on a single file, // e.g. the one that comes from a bug report. -#include -#include +#include #include +#include #include // Forward declare the "fuzz target" interface. @@ -21,7 +21,7 @@ int main(int argc, char **argv) { std::ifstream in(argv[i]); in.seekg(0, in.end); size_t length = static_cast(in.tellg()); - in.seekg (0, in.beg); + in.seekg(0, in.beg); std::cout << "Reading " << length << " bytes from " << argv[i] << std::endl; // Allocate exactly length bytes so that we reliably catch buffer overflows. std::vector bytes(length); From 1a49076b5b26e6a5341e59e199cb2d33efd0ceda Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 29 Oct 2023 19:36:40 -0400 Subject: [PATCH 0121/1222] Removed unnecessary exception --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index e400a2b602..a4d44123dc 100644 --- a/httplib.h +++ b/httplib.h @@ -2563,7 +2563,7 @@ inline mmap::mmap(const char *path) #endif , size_(0), addr_(nullptr) { - if (!open(path)) { std::runtime_error(""); } + open(path); } inline mmap::~mmap() { close(); } From 1d6b22b5f07243cc3c0df97a7140e18bceafe9cb Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Mon, 30 Oct 2023 20:13:40 +0900 Subject: [PATCH 0122/1222] Fix C6001 (#1701) --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index a4d44123dc..1c78c35ecb 100644 --- a/httplib.h +++ b/httplib.h @@ -8659,8 +8659,8 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { auto type = GEN_DNS; - struct in6_addr addr6; - struct in_addr addr; + struct in6_addr addr6 {}; + struct in_addr addr {}; size_t addr_len = 0; #ifndef __MINGW32__ From 97ae6733ed1d39a074a063db21aae460ec78181d Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Fri, 10 Nov 2023 09:35:15 +0900 Subject: [PATCH 0123/1222] Run fuzz test in CTest (#1707) --- test/CMakeLists.txt | 2 ++ test/fuzzing/CMakeLists.txt | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 test/fuzzing/CMakeLists.txt diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 60e410c100..3cf2c24d89 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -102,3 +102,5 @@ if(HTTPLIB_IS_USING_OPENSSL) COMMAND_ERROR_IS_FATAL ANY ) endif() + +add_subdirectory(fuzzing) diff --git a/test/fuzzing/CMakeLists.txt b/test/fuzzing/CMakeLists.txt new file mode 100644 index 0000000000..7e416c7eb1 --- /dev/null +++ b/test/fuzzing/CMakeLists.txt @@ -0,0 +1,10 @@ +file(GLOB HTTPLIB_CORPUS corpus/*) +add_executable(httplib-test-fuzz + server_fuzzer.cc + standalone_fuzz_target_runner.cpp +) +target_link_libraries(httplib-test-fuzz PRIVATE httplib) +add_test( + NAME httplib-test-fuzz + COMMAND httplib-test-fuzz ${HTTPLIB_CORPUS} +) From 1d14e051a55132c8b4415f8f01f7b4124a4574f4 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sun, 12 Nov 2023 11:26:57 +0900 Subject: [PATCH 0124/1222] Remove cryptui on Windows (#1710) --- CMakeLists.txt | 1 - httplib.h | 1 - 2 files changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e3f50eef9..927cb63864 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,7 +219,6 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} # Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered. $<$:ws2_32> $<$:crypt32> - $<$:cryptui> # Needed for API from MacOS Security framework "$<$,$,$>:-framework CoreFoundation -framework Security>" # Can't put multiple targets in a single generator expression or it bugs out. diff --git a/httplib.h b/httplib.h index 1c78c35ecb..98e1ce778e 100644 --- a/httplib.h +++ b/httplib.h @@ -247,7 +247,6 @@ using socket_t = int; #ifdef _MSC_VER #pragma comment(lib, "crypt32.lib") -#pragma comment(lib, "cryptui.lib") #endif #elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) #include From f1431311a47b57212093e30ed38ba43f42b53b7c Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sun, 12 Nov 2023 11:28:50 +0900 Subject: [PATCH 0125/1222] Minor fixes on test cases (#1709) * Fix data race * Replace sleep_for() to wait_until_ready() --- test/test.cc | 119 +++++++++++++-------------------------------------- 1 file changed, 30 insertions(+), 89 deletions(-) diff --git a/test/test.cc b/test/test.cc index 0ad56b1bb7..b7e238a4de 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1088,12 +1088,8 @@ TEST(RedirectToDifferentPort, Redirect) { ASSERT_FALSE(svr1.is_running()); }); - while (!svr1.is_running() || !svr2.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)); + svr1.wait_until_ready(); + svr2.wait_until_ready(); Client cli("localhost", svr2_port); cli.set_follow_location(true); @@ -1125,9 +1121,6 @@ TEST(RedirectFromPageWithContent, Redirect) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - { Client cli("localhost", PORT); cli.set_follow_location(true); @@ -1193,9 +1186,6 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { ASSERT_LT(milliseconds, 5000U); } - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - { Client cli("http://[::1]:1234"); cli.set_follow_location(true); @@ -1246,8 +1236,7 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -1277,8 +1266,7 @@ TEST(BindServerTest, DISABLED_BindDualStack) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli("127.0.0.1", PORT); @@ -1349,8 +1337,7 @@ TEST(ErrorHandlerTest, ContentLength) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -1391,8 +1378,7 @@ TEST(ExceptionHandlerTest, ContentLength) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); for (size_t i = 0; i < 10; i++) { Client cli(HOST, PORT); @@ -1432,8 +1418,7 @@ TEST(NoContentTest, ContentLength) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -1483,8 +1468,7 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1552,8 +1536,7 @@ TEST(InvalidFormatTest, StatusCode) { ASSERT_FALSE(svr.is_running()); }); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -1577,7 +1560,7 @@ TEST(URLFragmentTest, WithFragment) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -1613,7 +1596,7 @@ TEST(HeaderWriter, SetHeaderWriter) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -2205,9 +2188,7 @@ class ServerTest : public ::testing::Test { t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); }); - while (!svr_.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr_.wait_until_ready(); } virtual void TearDown() { @@ -4222,9 +4203,6 @@ TEST(MountTest, Unmount) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - Client cli("localhost", PORT); svr.set_mount_point("/mount2", "./www2"); @@ -4275,9 +4253,6 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - Client cli("localhost", PORT); { @@ -4319,9 +4294,6 @@ TEST(KeepAliveTest, ReadTimeout) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - Client cli("localhost", PORT); cli.set_keep_alive(true); cli.set_read_timeout(std::chrono::seconds(1)); @@ -4351,7 +4323,7 @@ TEST(KeepAliveTest, Issue1041) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + svr.wait_until_ready(); Client cli(HOST, PORT); cli.set_keep_alive(true); @@ -4384,7 +4356,7 @@ TEST(KeepAliveTest, SSLClientReconnection) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT); cli.enable_server_certificate_verification(false); @@ -4447,9 +4419,6 @@ TEST(ClientProblemDetectionTest, ContentProvider) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - Client cli("localhost", PORT); { @@ -4487,9 +4456,6 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { svr.wait_until_ready(); - // 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("/"); @@ -4533,7 +4499,6 @@ TEST(GetWithParametersTest, GetWithParameters) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); { Client cli(HOST, PORT); @@ -4592,7 +4557,6 @@ TEST(GetWithParametersTest, GetWithParameters2) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); Client cli("localhost", PORT); @@ -4660,7 +4624,6 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); Client cli("localhost", PORT); @@ -4695,9 +4658,6 @@ TEST(KeepAliveTest, ReadTimeoutSSL) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - SSLClient cli("localhost", PORT); cli.enable_server_certificate_verification(false); cli.set_keep_alive(true); @@ -4736,9 +4696,7 @@ class ServerTestWithAI_PASSIVE : public ::testing::Test { t_ = thread( [&]() { ASSERT_TRUE(svr_.listen(std::string(), PORT, AI_PASSIVE)); }); - while (!svr_.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr_.wait_until_ready(); } virtual void TearDown() { @@ -4775,9 +4733,7 @@ class ServerUpDownTest : public ::testing::Test { ASSERT_TRUE(svr_.listen_after_bind()); }); - while (!svr_.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr_.wait_until_ready(); } virtual void TearDown() { @@ -4818,9 +4774,7 @@ class PayloadMaxLengthTest : public ::testing::Test { t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); }); - while (!svr_.is_running()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } + svr_.wait_until_ready(); } virtual void TearDown() { @@ -4937,7 +4891,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli("127.0.0.1", PORT); cli.set_ca_cert_path(SERVER_CERT2_FILE); @@ -5053,7 +5007,7 @@ TEST(SSLClientServerTest, ClientCertPresent) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); cli.enable_server_certificate_verification(false); @@ -5128,7 +5082,7 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT, client_cert, client_private_key); cli.enable_server_certificate_verification(false); @@ -5159,7 +5113,7 @@ TEST(SSLClientServerTest, ClientCertMissing) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT); auto res = cli.Get("/test"); @@ -5183,7 +5137,7 @@ TEST(SSLClientServerTest, TrustDirOptional) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); cli.enable_server_certificate_verification(false); @@ -5204,12 +5158,12 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { client_ca_cert_dir_path), stop_(false) {} - bool stop_; + std::atomic_bool stop_; private: bool process_and_close_socket(socket_t /*sock*/) override { // Don't create SSL context - while (!stop_) { + while (!stop_.load()) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); } return true; @@ -5231,7 +5185,7 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); cli.enable_server_certificate_verification(false); @@ -5303,7 +5257,7 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); cli.enable_server_certificate_verification(false); @@ -5454,9 +5408,6 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { svr.wait_until_ready(); - // Give GET time to get a few messages. - std::this_thread::sleep_for(std::chrono::seconds(1)); - Client cli(HOST, PORT); auto res = cli.Get("/foo"); ASSERT_TRUE(res); @@ -5591,7 +5542,8 @@ TEST(HttpToHttpsRedirectTest, CertFile) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + svr.wait_until_ready(); + ssl_svr.wait_until_ready(); Client cli("127.0.0.1", PORT); cli.set_ca_cert_path(SERVER_CERT2_FILE); @@ -5647,7 +5599,6 @@ TEST(MultipartFormDataTest, LargeData) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); { std::string data(1024 * 1024 * 2, '.'); @@ -5795,7 +5746,6 @@ TEST(MultipartFormDataTest, DataProviderItems) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); { Client cli("https://localhost:8080"); @@ -5997,7 +5947,6 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); { std::string data(1024 * 1024 * 2, '.'); @@ -6019,9 +5968,6 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { } TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { - - std::this_thread::sleep_for(std::chrono::seconds(1)); - std::string data(1024 * 1024 * 2, '&'); std::stringstream buffer; buffer << data; @@ -6084,7 +6030,6 @@ TEST(MultipartFormDataTest, PutFormData) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); { std::string data(1024 * 1024 * 2, '&'); @@ -6149,7 +6094,6 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { }); svr.wait_until_ready(); - std::this_thread::sleep_for(std::chrono::seconds(1)); { std::string data(1024 * 1024 * 2, '&'); @@ -6171,9 +6115,6 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { } TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { - - std::this_thread::sleep_for(std::chrono::seconds(1)); - std::string data(1024 * 1024 * 2, '&'); std::stringstream buffer; buffer << data; @@ -6502,7 +6443,7 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); @@ -6548,7 +6489,7 @@ TEST(VulnerabilityTest, CRLFInjection) { ASSERT_FALSE(svr.is_running()); }); - std::this_thread::sleep_for(std::chrono::seconds(1)); + svr.wait_until_ready(); { Client cli(HOST, PORT); From 7fc8682a0a3eb0530314495005067dce8148a1aa Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Mon, 20 Nov 2023 13:13:59 -0500 Subject: [PATCH 0126/1222] Fix performance-noexcept-move-constructor (#1715) --- httplib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 98e1ce778e..2db3e03ec8 100644 --- a/httplib.h +++ b/httplib.h @@ -330,7 +330,7 @@ struct scope_exit { explicit scope_exit(std::function &&f) : exit_function(std::move(f)), execute_on_destruction{true} {} - scope_exit(scope_exit &&rhs) + scope_exit(scope_exit &&rhs) noexcept : exit_function(std::move(rhs.exit_function)), execute_on_destruction{rhs.execute_on_destruction} { rhs.release(); @@ -7497,8 +7497,8 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, } std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, response_handler, - content_receiver, progress); + return Get(path_with_query, headers, response_handler, content_receiver, + progress); } inline Result ClientImpl::Head(const std::string &path) { From 03fecb2f784d05dbdd8d14930b9b237016e44770 Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Mon, 20 Nov 2023 22:10:04 -0500 Subject: [PATCH 0127/1222] Fix modernize warnings (#1720) --- httplib.h | 58 +++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index 2db3e03ec8..39c5fe5d26 100644 --- a/httplib.h +++ b/httplib.h @@ -392,7 +392,7 @@ class DataSink { explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} protected: - std::streamsize xsputn(const char *s, std::streamsize n) { + std::streamsize xsputn(const char *s, std::streamsize n) override { sink_.write(s, static_cast(n)); return n; } @@ -1679,7 +1679,7 @@ class SSLClient : public ClientImpl { private: bool create_and_connect_socket(Socket &socket, Error &error) override; void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; - void shutdown_ssl_impl(Socket &socket, bool shutdown_socket); + void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); bool process_socket(const Socket &socket, std::function callback) override; @@ -2065,7 +2065,7 @@ class decompressor { class nocompressor : public compressor { public: - virtual ~nocompressor() = default; + ~nocompressor() override = default; bool compress(const char *data, size_t data_length, bool /*last*/, Callback callback) override; @@ -2075,7 +2075,7 @@ class nocompressor : public compressor { class gzip_compressor : public compressor { public: gzip_compressor(); - ~gzip_compressor(); + ~gzip_compressor() override; bool compress(const char *data, size_t data_length, bool last, Callback callback) override; @@ -2088,7 +2088,7 @@ class gzip_compressor : public compressor { class gzip_decompressor : public decompressor { public: gzip_decompressor(); - ~gzip_decompressor(); + ~gzip_decompressor() override; bool is_valid() const override; @@ -5295,7 +5295,7 @@ inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, write_timeout_sec_(write_timeout_sec), write_timeout_usec_(write_timeout_usec), read_buff_(read_buff_size_, 0) {} -inline SocketStream::~SocketStream() {} +inline SocketStream::~SocketStream() = default; inline bool SocketStream::is_readable() const { return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; @@ -5509,7 +5509,7 @@ inline Server::Server() #endif } -inline Server::~Server() {} +inline Server::~Server() = default; inline std::unique_ptr Server::make_matcher(const std::string &pattern) { @@ -5521,66 +5521,60 @@ Server::make_matcher(const std::string &pattern) { } inline Server &Server::Get(const std::string &pattern, Handler handler) { - get_handlers_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); return *this; } inline Server &Server::Post(const std::string &pattern, Handler handler) { - post_handlers_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); return *this; } inline Server &Server::Post(const std::string &pattern, HandlerWithContentReader handler) { - post_handlers_for_content_reader_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); return *this; } inline Server &Server::Put(const std::string &pattern, Handler handler) { - put_handlers_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); return *this; } inline Server &Server::Put(const std::string &pattern, HandlerWithContentReader handler) { - put_handlers_for_content_reader_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); return *this; } inline Server &Server::Patch(const std::string &pattern, Handler handler) { - patch_handlers_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); return *this; } inline Server &Server::Patch(const std::string &pattern, HandlerWithContentReader handler) { - patch_handlers_for_content_reader_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); return *this; } inline Server &Server::Delete(const std::string &pattern, Handler handler) { - delete_handlers_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); return *this; } inline Server &Server::Delete(const std::string &pattern, HandlerWithContentReader handler) { - delete_handlers_for_content_reader_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); return *this; } inline Server &Server::Options(const std::string &pattern, Handler handler) { - options_handlers_.push_back( - std::make_pair(make_matcher(pattern), std::move(handler))); + options_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); return *this; } @@ -8147,7 +8141,7 @@ inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); } -inline SSLSocketStream::~SSLSocketStream() {} +inline SSLSocketStream::~SSLSocketStream() = default; inline bool SSLSocketStream::is_readable() const { return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; @@ -8363,7 +8357,7 @@ inline SSLClient::SSLClient(const std::string &host, int port, detail::split(&host_[0], &host_[host_.size()], '.', [&](const char *b, const char *e) { - host_components_.emplace_back(std::string(b, e)); + host_components_.emplace_back(b, e); }); if (!client_cert_path.empty() && !client_key_path.empty()) { @@ -8384,7 +8378,7 @@ inline SSLClient::SSLClient(const std::string &host, int port, detail::split(&host_[0], &host_[host_.size()], '.', [&](const char *b, const char *e) { - host_components_.emplace_back(std::string(b, e)); + host_components_.emplace_back(b, e); }); if (client_cert != nullptr && client_key != nullptr) { @@ -8734,7 +8728,7 @@ inline bool SSLClient::check_host_name(const char *pattern, std::vector pattern_components; detail::split(&pattern[0], &pattern[pattern_len], '.', [&](const char *b, const char *e) { - pattern_components.emplace_back(std::string(b, e)); + pattern_components.emplace_back(b, e); }); if (host_components_.size() != pattern_components.size()) { return false; } @@ -8813,7 +8807,7 @@ inline Client::Client(const std::string &host, int port, : cli_(detail::make_unique(host, port, client_cert_path, client_key_path)) {} -inline Client::~Client() {} +inline Client::~Client() = default; inline bool Client::is_valid() const { return cli_ != nullptr && cli_->is_valid(); From 5ef4cfd263309458afe27246d66f370e3b351faa Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Mon, 20 Nov 2023 22:14:00 -0500 Subject: [PATCH 0128/1222] Fix bugprone warnings (#1721) --- httplib.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 39c5fe5d26..30b4fbc882 100644 --- a/httplib.h +++ b/httplib.h @@ -2227,7 +2227,7 @@ inline std::string from_i_to_hex(size_t n) { inline size_t to_utf8(int code, char *buff) { if (code < 0x0080) { - buff[0] = (code & 0x7F); + buff[0] = static_cast(code & 0x7F); return 1; } else if (code < 0x0800) { buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); @@ -2835,7 +2835,7 @@ class SocketStream : public Stream { size_t read_buff_off_ = 0; size_t read_buff_content_size_ = 0; - static const size_t read_buff_size_ = 1024 * 4; + static const size_t read_buff_size_ = 1024l * 4; }; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -3804,7 +3804,7 @@ inline bool read_content_chunked(Stream &strm, T &x, if (!line_reader.getline()) { return false; } - if (strcmp(line_reader.ptr(), "\r\n")) { return false; } + if (strcmp(line_reader.ptr(), "\r\n") != 0) { return false; } if (!line_reader.getline()) { return false; } } @@ -3814,7 +3814,7 @@ inline bool read_content_chunked(Stream &strm, T &x, // Trailer if (!line_reader.getline()) { return false; } - while (strcmp(line_reader.ptr(), "\r\n")) { + while (strcmp(line_reader.ptr(), "\r\n") != 0) { if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } // Exclude line terminator From 115a786581df061757303173e4ff58d37d92000d Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Fri, 24 Nov 2023 09:55:04 -0500 Subject: [PATCH 0129/1222] Fix readability warnings (#1722) * Fix readability warnings Did not fix readbility-qualified-auto, will do a separate pull request * Revert changes where meaning is lost * Revert some style changes --- httplib.h | 83 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/httplib.h b/httplib.h index 30b4fbc882..b880de7f22 100644 --- a/httplib.h +++ b/httplib.h @@ -868,15 +868,15 @@ class Server { bool routing(Request &req, Response &res, Stream &strm); bool handle_file_request(const Request &req, Response &res, bool head = false); - bool dispatch_request(Request &req, Response &res, const Handlers &handlers); - bool - dispatch_request_for_content_reader(Request &req, Response &res, - ContentReader content_reader, - const HandlersForContentReader &handlers); + bool dispatch_request(Request &req, Response &res, + const Handlers &handlers) const; + bool dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const; - bool parse_request_line(const char *s, Request &req); + bool parse_request_line(const char *s, Request &req) const; void apply_ranges(const Request &req, Response &res, - std::string &content_type, std::string &boundary); + std::string &content_type, std::string &boundary) const; bool write_response(Stream &strm, bool close_connection, const Request &req, Response &res); bool write_response_with_content(Stream &strm, bool close_connection, @@ -896,7 +896,7 @@ class Server { bool read_content_core(Stream &strm, Request &req, Response &res, ContentReceiver receiver, MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver); + ContentReceiver multipart_receiver) const; virtual bool process_and_close_socket(socket_t sock); @@ -962,7 +962,7 @@ enum class Error { SSLPeerCouldBeClosed_, }; -std::string to_string(const Error error); +std::string to_string(Error error); std::ostream &operator<<(std::ostream &os, const Error &obj); @@ -1220,7 +1220,7 @@ class ClientImpl { void set_ca_cert_path(const std::string &ca_cert_file_path, const std::string &ca_cert_dir_path = std::string()); void set_ca_cert_store(X509_STORE *ca_cert_store); - X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size); + X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1249,14 +1249,14 @@ class ClientImpl { // Also, shutdown_ssl and close_socket should also NOT be called concurrently // with a DIFFERENT thread sending requests using that socket. virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); - void shutdown_socket(Socket &socket); + void shutdown_socket(Socket &socket) const; void close_socket(Socket &socket); bool process_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error); bool write_content_with_provider(Stream &strm, const Request &req, - Error &error); + Error &error) const; void copy_settings(const ClientImpl &rhs); @@ -1347,7 +1347,8 @@ class ClientImpl { Result send_(Request &&req); socket_t create_client_socket(Error &error) const; - bool read_response_line(Stream &strm, const Request &req, Response &res); + bool read_response_line(Stream &strm, const Request &req, + Response &res) const; bool write_request(Stream &strm, Request &req, bool close_connection, Error &error); bool redirect(Request &req, Response &res, Error &error); @@ -1366,7 +1367,7 @@ class ClientImpl { const std::string &content_type); ContentProviderWithoutLength get_multipart_content_provider( const std::string &boundary, const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); + const MultipartFormDataProviderItems &provider_items) const; std::string adjust_host_string(const std::string &host) const; @@ -2964,7 +2965,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #ifndef _WIN32 if (hints.ai_family == AF_UNIX) { const auto addrlen = host.length(); - if (addrlen > sizeof(sockaddr_un::sun_path)) return INVALID_SOCKET; + if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); if (sock != INVALID_SOCKET) { @@ -3168,7 +3169,7 @@ inline socket_t create_client_socket( #ifdef USE_IF2IP auto ip_from_if = if2ip(address_family, intf); if (ip_from_if.empty()) { ip_from_if = intf; } - if (!bind_ip_address(sock2, ip_from_if.c_str())) { + if (!bind_ip_address(sock2, ip_from_if)) { error = Error::BindIPAddress; return false; } @@ -3536,7 +3537,7 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length, } } - if (ret != Z_OK && ret != Z_STREAM_END) return false; + if (ret != Z_OK && ret != Z_STREAM_END) { return false; } } while (data_length > 0); @@ -4149,7 +4150,7 @@ inline bool redirect(T &cli, Request &req, Response &res, req = new_req; res = new_res; - if (res.location.empty()) res.location = location; + if (res.location.empty()) { res.location = location; } } return ret; } @@ -4236,7 +4237,7 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { auto len = static_cast(m.length(1)); auto all_valid_ranges = true; split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { - if (!all_valid_ranges) return; + if (!all_valid_ranges) { return; } static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))"); std::cmatch cm; if (std::regex_match(b, e, cm, re_another_range)) { @@ -4587,7 +4588,7 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, body += item.content + serialize_multipart_formdata_item_end(); } - if (finish) body += serialize_multipart_formdata_finish(boundary); + if (finish) { body += serialize_multipart_formdata_finish(boundary); } return body; } @@ -5722,8 +5723,7 @@ inline Server &Server::set_payload_max_length(size_t length) { inline bool Server::bind_to_port(const std::string &host, int port, int socket_flags) { - if (bind_internal(host, port, socket_flags) < 0) return false; - return true; + return bind_internal(host, port, socket_flags) >= 0; } inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { return bind_internal(host, 0, socket_flags); @@ -5757,7 +5757,7 @@ inline void Server::stop() { } } -inline bool Server::parse_request_line(const char *s, Request &req) { +inline bool Server::parse_request_line(const char *s, Request &req) const { auto len = strlen(s); if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } len -= 2; @@ -6008,10 +6008,11 @@ inline bool Server::read_content_with_content_receiver( std::move(multipart_receiver)); } -inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) { +inline bool +Server::read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) const { detail::MultipartFormDataParser multipart_form_data_parser; ContentReceiverWithProgress out; @@ -6302,7 +6303,7 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { } inline bool Server::dispatch_request(Request &req, Response &res, - const Handlers &handlers) { + const Handlers &handlers) const { for (const auto &x : handlers) { const auto &matcher = x.first; const auto &handler = x.second; @@ -6317,7 +6318,7 @@ inline bool Server::dispatch_request(Request &req, Response &res, inline void Server::apply_ranges(const Request &req, Response &res, std::string &content_type, - std::string &boundary) { + std::string &boundary) const { if (req.ranges.size() > 1) { boundary = detail::make_multipart_data_boundary(); @@ -6429,7 +6430,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, inline bool Server::dispatch_request_for_content_reader( Request &req, Response &res, ContentReader content_reader, - const HandlersForContentReader &handlers) { + const HandlersForContentReader &handlers) const { for (const auto &x : handlers) { const auto &matcher = x.first; const auto &handler = x.second; @@ -6671,7 +6672,7 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const { // Check is custom IP specified for host_ std::string ip; auto it = addr_map_.find(host_); - if (it != addr_map_.end()) ip = it->second; + if (it != addr_map_.end()) { ip = it->second; } return detail::create_client_socket( host_, ip, port_, address_family_, tcp_nodelay_, socket_options_, @@ -6696,7 +6697,7 @@ inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, socket_requests_are_from_thread_ == std::this_thread::get_id()); } -inline void ClientImpl::shutdown_socket(Socket &socket) { +inline void ClientImpl::shutdown_socket(Socket &socket) const { if (socket.sock == INVALID_SOCKET) { return; } detail::shutdown_socket(socket.sock); } @@ -6721,7 +6722,7 @@ inline void ClientImpl::close_socket(Socket &socket) { } inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, - Response &res) { + Response &res) const { std::array buf{}; detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); @@ -7002,7 +7003,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { inline bool ClientImpl::write_content_with_provider(Stream &strm, const Request &req, - Error &error) { + Error &error) const { auto is_shutting_down = []() { return false; }; if (req.is_chunked_content_provider_) { @@ -7328,13 +7329,14 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( const std::string &boundary, const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - size_t cur_item = 0, cur_start = 0; + const MultipartFormDataProviderItems &provider_items) const { + size_t cur_item = 0; + size_t cur_start = 0; // cur_item and cur_start are copied to within the std::function and maintain // state between successive calls return [&, cur_item, cur_start](size_t offset, DataSink &sink) mutable -> bool { - if (!offset && items.size()) { + if (!offset && !items.empty()) { sink.os << detail::serialize_multipart_formdata(items, boundary, false); return true; } else if (cur_item < provider_items.size()) { @@ -7351,8 +7353,9 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( cur_sink.write = sink.write; cur_sink.done = [&]() { has_data = false; }; - if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) + if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) { return false; + } if (!has_data) { sink.os << detail::serialize_multipart_formdata_item_end(); @@ -7989,9 +7992,9 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { } inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, - std::size_t size) { + std::size_t size) const { auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); - if (!mem) return nullptr; + if (!mem) { return nullptr; } auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); if (!inf) { From c5c704cb3b7f0e1ba5dbb5135e369847f6df08ac Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 4 Dec 2023 21:27:34 -0500 Subject: [PATCH 0130/1222] Fix #1724 --- httplib.h | 7 +++++++ test/test.cc | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/httplib.h b/httplib.h index b880de7f22..16940be21b 100644 --- a/httplib.h +++ b/httplib.h @@ -382,6 +382,7 @@ class DataSink { DataSink &operator=(DataSink &&) = delete; std::function write; + std::function is_writable; std::function done; std::function done_with_trailer; std::ostream os; @@ -3959,6 +3960,8 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, return ok; }; + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + while (offset < end_offset && !is_shutting_down()) { if (!strm.is_writable()) { error = Error::Write; @@ -4003,6 +4006,8 @@ write_content_without_length(Stream &strm, return ok; }; + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + data_sink.done = [&](void) { data_available = false; }; while (data_available && !is_shutting_down()) { @@ -4053,6 +4058,8 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, return ok; }; + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + auto done_with_trailer = [&](const Headers *trailer) { if (!ok) { return; } diff --git a/test/test.cc b/test/test.cc index b7e238a4de..4890a3aa29 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4464,6 +4464,43 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { EXPECT_EQ("helloworld", res->body); } +TEST(LongPollingTest, ClientCloseDetection) { + Server svr; + + svr.Get("/events", [&](const Request & /*req*/, Response &res) { + res.set_chunked_content_provider( + "text/plain", [](std::size_t const, DataSink &sink) -> bool { + EXPECT_TRUE(sink.is_writable()); // the socket is alive + sink.os << "hello"; + + auto count = 10; + while (count > 0 && sink.is_writable()) { + this_thread::sleep_for(chrono::milliseconds(10)); + } + EXPECT_FALSE(sink.is_writable()); // the socket is closed + return true; + }); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + auto res = cli.Get("/events", [&](const char *data, size_t data_length) { + EXPECT_EQ("hello", string(data, data_length)); + return false; // close the socket immediately. + }); + + ASSERT_FALSE(res); +} + TEST(GetWithParametersTest, GetWithParameters) { Server svr; From f14accb7b6ff4499321e14c61497bc7e4b28e49b Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 4 Dec 2023 22:31:12 -0500 Subject: [PATCH 0131/1222] Release v0.14.2 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 16940be21b..2540a7ef67 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.14.1" +#define CPPHTTPLIB_VERSION "0.14.2" /* * Configuration From e426a38c3e9b1ae8610fb023ba8104cb390d8520 Mon Sep 17 00:00:00 2001 From: davidalo Date: Thu, 7 Dec 2023 20:28:41 +0100 Subject: [PATCH 0132/1222] Fix: Query parameter including query delimiter ('?') not being parsed properly (#1713) * Fix: Query parameter including query delimiter ('?') not being parsed properly * Add details::split function with and without m argument to allow split parameters with/without counter * Revert changes in SplitTest.ParseQueryString --- httplib.h | 15 +++++++++++++-- test/test.cc | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 2540a7ef67..30e512d68a 100644 --- a/httplib.h +++ b/httplib.h @@ -1989,8 +1989,12 @@ void read_file(const std::string &path, std::string &out); std::string trim_copy(const std::string &s); void split(const char *b, const char *e, char d, + std::function fn); + +void split(const char *b, const char *e, char d, size_t m, std::function fn); + bool process_client_socket(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, @@ -2473,14 +2477,21 @@ inline std::string trim_double_quotes_copy(const std::string &s) { inline void split(const char *b, const char *e, char d, std::function fn) { + return split(b, e, d, std::numeric_limits::max(), fn); +} + +inline void split(const char *b, const char *e, char d, size_t m, + std::function fn) { size_t i = 0; size_t beg = 0; + size_t count = 1; while (e ? (b + i < e) : (b[i] != '\0')) { - if (b[i] == d) { + if (b[i] == d && count < m) { auto r = trim(b, e, beg, i); if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } beg = i + 1; + count++; } i++; } @@ -5804,7 +5815,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { size_t count = 0; - detail::split(req.target.data(), req.target.data() + req.target.size(), '?', + detail::split(req.target.data(), req.target.data() + req.target.size(), '?', 2, [&](const char *b, const char *e) { switch (count) { case 0: diff --git a/test/test.cc b/test/test.cc index 4890a3aa29..8af63554aa 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1847,6 +1847,11 @@ class ServerTest : public ::testing::Test { return true; }); }) + .Get("/regex-with-delimiter", + [&](const Request & req, Response &res) { + ASSERT_TRUE(req.has_param("key")); + EXPECT_EQ("^(?.*(value))", req.get_param_value("key")); + }) .Get("/with-range", [&](const Request & /*req*/, Response &res) { res.set_content("abcdefg", "text/plain"); @@ -3352,6 +3357,14 @@ TEST_F(ServerTest, GetStreamedChunkedWithGzip2) { EXPECT_EQ(std::string("123456789"), res->body); } + +TEST_F(ServerTest, SplitDelimiterInPathRegex) { + auto res = cli_.Get("/regex-with-delimiter?key=^(?.*(value))"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); +} + + TEST(GzipDecompressor, ChunkedDecompression) { std::string data; for (size_t i = 0; i < 32 * 1024; ++i) { From cefb5a88226bf832c9219fea7349e839094c1606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renato=20Foot=20Guimar=C3=A3es=20Costallat?= Date: Fri, 8 Dec 2023 01:21:59 -0300 Subject: [PATCH 0133/1222] Update README.md (#1731) Fix "With Progress Callback" code example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a0b183aa8c..4724892a89 100644 --- a/README.md +++ b/README.md @@ -662,7 +662,7 @@ auto res = cli.Post( ### With Progress Callback ```cpp -httplib::Client client(url, port); +httplib::Client cli(url, port); // prints: 0 / 000 bytes => 50% complete auto res = cli.Get("/", [](uint64_t len, uint64_t total) { From cddaedaff89ba79aba8288d1a2cebd369d0ff8ef Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 15 Dec 2023 19:29:54 -0500 Subject: [PATCH 0134/1222] Fix #1736 --- httplib.h | 15 ++++++++++++--- test/test.cc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 30e512d68a..e1bbe14079 100644 --- a/httplib.h +++ b/httplib.h @@ -4332,10 +4332,19 @@ class MultipartFormDataParser { break; } - static const std::string header_name = "content-type:"; + static const std::string header_content_type = "Content-Type:"; + static const std::string header_content_length = "Content-Length:"; + const auto header = buf_head(pos); - if (start_with_case_ignore(header, header_name)) { - file_.content_type = trim_copy(header.substr(header_name.size())); + if (start_with_case_ignore(header, header_content_type)) { + file_.content_type = + trim_copy(header.substr(header_content_type.size())); + } else if (start_with_case_ignore(header, header_content_length)) { + // NOTE: For now, we ignore the content length. In the future, the + // parser should check if the actual body length is same as this + // value. + // auto content_length = std::stoi( + // trim_copy(header.substr(header_content_length.size()))); } else { static const std::regex re_content_disposition( R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", diff --git a/test/test.cc b/test/test.cc index 8af63554aa..4862369aee 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6298,6 +6298,53 @@ TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) { ASSERT_EQ("200", resonse.substr(9, 3)); } +TEST(MultipartFormDataTest, ContentLength) { + auto handled = false; + + Server svr; + svr.Post("/test", [&](const Request &req, Response &) { + ASSERT_EQ(2u, req.files.size()); + + auto it = req.files.begin(); + ASSERT_EQ("text1", it->second.name); + ASSERT_EQ("text1", it->second.content); + + ++it; + ASSERT_EQ("text2", it->second.name); + ASSERT_EQ("text2", it->second.content); + + handled = true; + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + ASSERT_TRUE(handled); + }); + + svr.wait_until_ready(); + + auto req = "POST /test HTTP/1.1\r\n" + "Content-Type: multipart/form-data;boundary=--------\r\n" + "Content-Length: 167\r\n" + "\r\n----------\r\n" + "Content-Disposition: form-data; name=\"text1\"\r\n" + "Content-Length: 5\r\n" + "\r\n" + "text1" + "\r\n----------\r\n" + "Content-Disposition: form-data; name=\"text2\"\r\n" + "\r\n" + "text2" + "\r\n------------\r\n"; + + std::string resonse; + ASSERT_TRUE(send_request(1, req, &resonse)); + ASSERT_EQ("200", resonse.substr(9, 3)); +} + #endif #ifndef _WIN32 From f1dec77f46b5f671c7d06e08b35027774dc4dc09 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 17 Dec 2023 22:00:33 -0500 Subject: [PATCH 0135/1222] Code format --- httplib.h | 11 +++++------ test/test.cc | 8 +++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index e1bbe14079..aca84b3b65 100644 --- a/httplib.h +++ b/httplib.h @@ -141,11 +141,11 @@ using ssize_t = long; #endif // _MSC_VER #ifndef S_ISREG -#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) +#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) #endif // S_ISREG #ifndef S_ISDIR -#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) +#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) #endif // S_ISDIR #ifndef NOMINMAX @@ -1989,12 +1989,11 @@ void read_file(const std::string &path, std::string &out); std::string trim_copy(const std::string &s); void split(const char *b, const char *e, char d, - std::function fn); + std::function fn); void split(const char *b, const char *e, char d, size_t m, std::function fn); - bool process_client_socket(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, @@ -5824,8 +5823,8 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { size_t count = 0; - detail::split(req.target.data(), req.target.data() + req.target.size(), '?', 2, - [&](const char *b, const char *e) { + detail::split(req.target.data(), req.target.data() + req.target.size(), '?', + 2, [&](const char *b, const char *e) { switch (count) { case 0: req.path = detail::decode_url(std::string(b, e), false); diff --git a/test/test.cc b/test/test.cc index 4862369aee..91513cd936 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1848,9 +1848,9 @@ class ServerTest : public ::testing::Test { }); }) .Get("/regex-with-delimiter", - [&](const Request & req, Response &res) { - ASSERT_TRUE(req.has_param("key")); - EXPECT_EQ("^(?.*(value))", req.get_param_value("key")); + [&](const Request &req, Response & /*res*/) { + ASSERT_TRUE(req.has_param("key")); + EXPECT_EQ("^(?.*(value))", req.get_param_value("key")); }) .Get("/with-range", [&](const Request & /*req*/, Response &res) { @@ -3357,14 +3357,12 @@ TEST_F(ServerTest, GetStreamedChunkedWithGzip2) { EXPECT_EQ(std::string("123456789"), res->body); } - TEST_F(ServerTest, SplitDelimiterInPathRegex) { auto res = cli_.Get("/regex-with-delimiter?key=^(?.*(value))"); ASSERT_TRUE(res); EXPECT_EQ(200, res->status); } - TEST(GzipDecompressor, ChunkedDecompression) { std::string data; for (size_t i = 0; i < 32 * 1024; ++i) { From 8aa38aecaff3adbac2aa84771df946407d81f479 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 17 Dec 2023 22:01:27 -0500 Subject: [PATCH 0136/1222] Fix #1665 --- httplib.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index aca84b3b65..074a98cc68 100644 --- a/httplib.h +++ b/httplib.h @@ -3687,6 +3687,9 @@ inline bool parse_header(const char *beg, const char *end, T fn) { } if (p < end) { + auto key_len = key_end - beg; + if (!key_len) { return false; } + auto key = std::string(beg, key_end); auto val = compare_case_ignore(key, "Location") ? std::string(p, end) @@ -4331,19 +4334,19 @@ class MultipartFormDataParser { break; } + const auto header = buf_head(pos); + + if (!parse_header(header.data(), header.data() + header.size(), + [&](std::string &&, std::string &&) {})) { + is_valid_ = false; + return false; + } + static const std::string header_content_type = "Content-Type:"; - static const std::string header_content_length = "Content-Length:"; - const auto header = buf_head(pos); if (start_with_case_ignore(header, header_content_type)) { file_.content_type = trim_copy(header.substr(header_content_type.size())); - } else if (start_with_case_ignore(header, header_content_length)) { - // NOTE: For now, we ignore the content length. In the future, the - // parser should check if the actual body length is same as this - // value. - // auto content_length = std::stoi( - // trim_copy(header.substr(header_content_length.size()))); } else { static const std::regex re_content_disposition( R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", @@ -4379,9 +4382,6 @@ class MultipartFormDataParser { return false; } } - } else { - is_valid_ = false; - return false; } } buf_erase(pos + crlf_.size()); From 3a8adda38169af50f17b89895db582fd95f1dd2f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 17 Dec 2023 22:04:36 -0500 Subject: [PATCH 0137/1222] Fix #1737 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4724892a89..4340fa33a0 100644 --- a/README.md +++ b/README.md @@ -417,6 +417,8 @@ svr.set_idle_interval(0, 100000); // 100 milliseconds svr.set_payload_max_length(1024 * 1024 * 512); // 512MB ``` +NOTE: When the request body content type is 'www-form-urlencoded', the actual payload length shouldn't exceed `CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH`. + ### Server-Sent Events Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc) and [Client example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssecli.cc). From 37f8dc43829994e6cb2d1b78136cab320da8e546 Mon Sep 17 00:00:00 2001 From: Ilya Andreev Date: Tue, 19 Dec 2023 17:22:58 +0300 Subject: [PATCH 0138/1222] Change some of status messages based on RFC 9110 (#1740) --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 074a98cc68..5dfaeadec8 100644 --- a/httplib.h +++ b/httplib.h @@ -1807,7 +1807,7 @@ inline const char *status_message(int status) { case 207: return "Multi-Status"; case 208: return "Already Reported"; case 226: return "IM Used"; - case 300: return "Multiple Choice"; + case 300: return "Multiple Choices"; case 301: return "Moved Permanently"; case 302: return "Found"; case 303: return "See Other"; @@ -1836,7 +1836,7 @@ inline const char *status_message(int status) { case 417: return "Expectation Failed"; case 418: return "I'm a teapot"; case 421: return "Misdirected Request"; - case 422: return "Unprocessable Entity"; + case 422: return "Unprocessable Content"; case 423: return "Locked"; case 424: return "Failed Dependency"; case 425: return "Too Early"; From d39fda065701d5e55ce6d82ec517b3a5b903e749 Mon Sep 17 00:00:00 2001 From: Ilya Andreev Date: Wed, 20 Dec 2023 01:57:30 +0300 Subject: [PATCH 0139/1222] Add StatusCode enum (#1739) * Add StatusCode enum * Remove changes on RFC 9110 * Add number suffixes to StatusCode constants * Remove docs for StatusCode constants --- httplib.h | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/httplib.h b/httplib.h index 5dfaeadec8..a7f6836865 100644 --- a/httplib.h +++ b/httplib.h @@ -1791,6 +1791,81 @@ inline void default_socket_options(socket_t sock) { #endif } +enum StatusCode { + // Information responses + Continue_100 = 100, + SwitchingProtocol_101 = 101, + Processing_102 = 102, + EarlyHints_103 = 103, + + // Successful responses + OK_200 = 200, + Created_201 = 201, + Accepted_202 = 202, + NonAuthoritativeInformation_203 = 203, + NoContent_204 = 204, + ResetContent_205 = 205, + PartialContent_206 = 206, + MultiStatus_207 = 207, + AlreadyReported_208 = 208, + IMUsed_226 = 226, + + // Redirection messages + MultipleChoices_300 = 300, + MovedPermanently_301 = 301, + Found_302 = 302, + SeeOther_303 = 303, + NotModified_304 = 304, + UseProxy_305 = 305, + unused_306 = 306, + TemporaryRedirect_307 = 307, + PermanentRedirect_308 = 308, + + // Client error responses + BadRequest_400 = 400, + Unauthorized_401 = 401, + PaymentRequired_402 = 402, + Forbidden_403 = 403, + NotFound_404 = 404, + MethodNotAllowed_405 = 405, + NotAcceptable_406 = 406, + ProxyAuthenticationRequired_407 = 407, + RequestTimeout_408 = 408, + Conflict_409 = 409, + Gone_410 = 410, + LengthRequired_411 = 411, + PreconditionFailed_412 = 412, + PayloadTooLarge_413 = 413, + UriTooLong_414 = 414, + UnsupportedMediaType_415 = 415, + RangeNotSatisfiable_416 = 416, + ExpectationFailed_417 = 417, + ImATeapot_418 = 418, + MisdirectedRequest_421 = 421, + UnprocessableContent_422 = 422, + Locked_423 = 423, + FailedDependency_424 = 424, + TooEarly_425 = 425, + UpgradeRequired_426 = 426, + PreconditionRequired_428 = 428, + TooManyRequests_429 = 429, + RequestHeaderFieldsTooLarge_431 = 431, + UnavailableForLegalReasons_451 = 451, + + // Server error responses + InternalServerError_500 = 500, + NotImplemented_501 = 501, + BadGateway_502 = 502, + ServiceUnavailable_503 = 503, + GatewayTimeout_504 = 504, + HttpVersionNotSupported_505 = 505, + VariantAlsoNegotiates_506 = 506, + InsufficientStorage_507 = 507, + LoopDetected_508 = 508, + NotExtended_510 = 510, + NetworkAuthenticationRequired_511 = 511, +}; + inline const char *status_message(int status) { switch (status) { case 100: return "Continue"; From c86f69a1057bfd23dff635835bc16ae721536064 Mon Sep 17 00:00:00 2001 From: Ilya Andreev Date: Wed, 20 Dec 2023 06:17:24 +0300 Subject: [PATCH 0140/1222] Use StatusCode in httplib code (#1742) --- httplib.h | 351 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 183 insertions(+), 168 deletions(-) diff --git a/httplib.h b/httplib.h index a7f6836865..43c8a3bf54 100644 --- a/httplib.h +++ b/httplib.h @@ -353,6 +353,81 @@ struct scope_exit { } // namespace detail +enum StatusCode { + // Information responses + Continue_100 = 100, + SwitchingProtocol_101 = 101, + Processing_102 = 102, + EarlyHints_103 = 103, + + // Successful responses + OK_200 = 200, + Created_201 = 201, + Accepted_202 = 202, + NonAuthoritativeInformation_203 = 203, + NoContent_204 = 204, + ResetContent_205 = 205, + PartialContent_206 = 206, + MultiStatus_207 = 207, + AlreadyReported_208 = 208, + IMUsed_226 = 226, + + // Redirection messages + MultipleChoices_300 = 300, + MovedPermanently_301 = 301, + Found_302 = 302, + SeeOther_303 = 303, + NotModified_304 = 304, + UseProxy_305 = 305, + unused_306 = 306, + TemporaryRedirect_307 = 307, + PermanentRedirect_308 = 308, + + // Client error responses + BadRequest_400 = 400, + Unauthorized_401 = 401, + PaymentRequired_402 = 402, + Forbidden_403 = 403, + NotFound_404 = 404, + MethodNotAllowed_405 = 405, + NotAcceptable_406 = 406, + ProxyAuthenticationRequired_407 = 407, + RequestTimeout_408 = 408, + Conflict_409 = 409, + Gone_410 = 410, + LengthRequired_411 = 411, + PreconditionFailed_412 = 412, + PayloadTooLarge_413 = 413, + UriTooLong_414 = 414, + UnsupportedMediaType_415 = 415, + RangeNotSatisfiable_416 = 416, + ExpectationFailed_417 = 417, + ImATeapot_418 = 418, + MisdirectedRequest_421 = 421, + UnprocessableContent_422 = 422, + Locked_423 = 423, + FailedDependency_424 = 424, + TooEarly_425 = 425, + UpgradeRequired_426 = 426, + PreconditionRequired_428 = 428, + TooManyRequests_429 = 429, + RequestHeaderFieldsTooLarge_431 = 431, + UnavailableForLegalReasons_451 = 451, + + // Server error responses + InternalServerError_500 = 500, + NotImplemented_501 = 501, + BadGateway_502 = 502, + ServiceUnavailable_503 = 503, + GatewayTimeout_504 = 504, + HttpVersionNotSupported_505 = 505, + VariantAlsoNegotiates_506 = 506, + InsufficientStorage_507 = 507, + LoopDetected_508 = 508, + NotExtended_510 = 510, + NetworkAuthenticationRequired_511 = 511, +}; + using Headers = std::multimap; using Params = std::multimap; @@ -523,7 +598,7 @@ struct Response { size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); - void set_redirect(const std::string &url, int status = 302); + void set_redirect(const std::string &url, int status = StatusCode::Found_302); void set_content(const char *s, size_t n, const std::string &content_type); void set_content(const std::string &s, const std::string &content_type); @@ -1791,148 +1866,79 @@ inline void default_socket_options(socket_t sock) { #endif } -enum StatusCode { - // Information responses - Continue_100 = 100, - SwitchingProtocol_101 = 101, - Processing_102 = 102, - EarlyHints_103 = 103, - - // Successful responses - OK_200 = 200, - Created_201 = 201, - Accepted_202 = 202, - NonAuthoritativeInformation_203 = 203, - NoContent_204 = 204, - ResetContent_205 = 205, - PartialContent_206 = 206, - MultiStatus_207 = 207, - AlreadyReported_208 = 208, - IMUsed_226 = 226, - - // Redirection messages - MultipleChoices_300 = 300, - MovedPermanently_301 = 301, - Found_302 = 302, - SeeOther_303 = 303, - NotModified_304 = 304, - UseProxy_305 = 305, - unused_306 = 306, - TemporaryRedirect_307 = 307, - PermanentRedirect_308 = 308, - - // Client error responses - BadRequest_400 = 400, - Unauthorized_401 = 401, - PaymentRequired_402 = 402, - Forbidden_403 = 403, - NotFound_404 = 404, - MethodNotAllowed_405 = 405, - NotAcceptable_406 = 406, - ProxyAuthenticationRequired_407 = 407, - RequestTimeout_408 = 408, - Conflict_409 = 409, - Gone_410 = 410, - LengthRequired_411 = 411, - PreconditionFailed_412 = 412, - PayloadTooLarge_413 = 413, - UriTooLong_414 = 414, - UnsupportedMediaType_415 = 415, - RangeNotSatisfiable_416 = 416, - ExpectationFailed_417 = 417, - ImATeapot_418 = 418, - MisdirectedRequest_421 = 421, - UnprocessableContent_422 = 422, - Locked_423 = 423, - FailedDependency_424 = 424, - TooEarly_425 = 425, - UpgradeRequired_426 = 426, - PreconditionRequired_428 = 428, - TooManyRequests_429 = 429, - RequestHeaderFieldsTooLarge_431 = 431, - UnavailableForLegalReasons_451 = 451, - - // Server error responses - InternalServerError_500 = 500, - NotImplemented_501 = 501, - BadGateway_502 = 502, - ServiceUnavailable_503 = 503, - GatewayTimeout_504 = 504, - HttpVersionNotSupported_505 = 505, - VariantAlsoNegotiates_506 = 506, - InsufficientStorage_507 = 507, - LoopDetected_508 = 508, - NotExtended_510 = 510, - NetworkAuthenticationRequired_511 = 511, -}; - inline const char *status_message(int status) { switch (status) { - case 100: return "Continue"; - case 101: return "Switching Protocol"; - case 102: return "Processing"; - case 103: return "Early Hints"; - case 200: return "OK"; - case 201: return "Created"; - case 202: return "Accepted"; - case 203: return "Non-Authoritative Information"; - case 204: return "No Content"; - case 205: return "Reset Content"; - case 206: return "Partial Content"; - case 207: return "Multi-Status"; - case 208: return "Already Reported"; - case 226: return "IM Used"; - case 300: return "Multiple Choices"; - case 301: return "Moved Permanently"; - case 302: return "Found"; - case 303: return "See Other"; - case 304: return "Not Modified"; - case 305: return "Use Proxy"; - case 306: return "unused"; - case 307: return "Temporary Redirect"; - case 308: return "Permanent Redirect"; - case 400: return "Bad Request"; - case 401: return "Unauthorized"; - case 402: return "Payment Required"; - case 403: return "Forbidden"; - case 404: return "Not Found"; - case 405: return "Method Not Allowed"; - case 406: return "Not Acceptable"; - case 407: return "Proxy Authentication Required"; - case 408: return "Request Timeout"; - case 409: return "Conflict"; - case 410: return "Gone"; - case 411: return "Length Required"; - case 412: return "Precondition Failed"; - case 413: return "Payload Too Large"; - case 414: return "URI Too Long"; - case 415: return "Unsupported Media Type"; - case 416: return "Range Not Satisfiable"; - case 417: return "Expectation Failed"; - case 418: return "I'm a teapot"; - case 421: return "Misdirected Request"; - case 422: return "Unprocessable Content"; - case 423: return "Locked"; - case 424: return "Failed Dependency"; - case 425: return "Too Early"; - case 426: return "Upgrade Required"; - case 428: return "Precondition Required"; - case 429: return "Too Many Requests"; - case 431: return "Request Header Fields Too Large"; - case 451: return "Unavailable For Legal Reasons"; - case 501: return "Not Implemented"; - case 502: return "Bad Gateway"; - case 503: return "Service Unavailable"; - case 504: return "Gateway Timeout"; - case 505: return "HTTP Version Not Supported"; - case 506: return "Variant Also Negotiates"; - case 507: return "Insufficient Storage"; - case 508: return "Loop Detected"; - case 510: return "Not Extended"; - case 511: return "Network Authentication Required"; + case StatusCode::Continue_100: return "Continue"; + case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; + case StatusCode::Processing_102: return "Processing"; + case StatusCode::EarlyHints_103: return "Early Hints"; + case StatusCode::OK_200: return "OK"; + case StatusCode::Created_201: return "Created"; + case StatusCode::Accepted_202: return "Accepted"; + case StatusCode::NonAuthoritativeInformation_203: + return "Non-Authoritative Information"; + case StatusCode::NoContent_204: return "No Content"; + case StatusCode::ResetContent_205: return "Reset Content"; + case StatusCode::PartialContent_206: return "Partial Content"; + case StatusCode::MultiStatus_207: return "Multi-Status"; + case StatusCode::AlreadyReported_208: return "Already Reported"; + case StatusCode::IMUsed_226: return "IM Used"; + case StatusCode::MultipleChoices_300: return "Multiple Choices"; + case StatusCode::MovedPermanently_301: return "Moved Permanently"; + case StatusCode::Found_302: return "Found"; + case StatusCode::SeeOther_303: return "See Other"; + case StatusCode::NotModified_304: return "Not Modified"; + case StatusCode::UseProxy_305: return "Use Proxy"; + case StatusCode::unused_306: return "unused"; + case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; + case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; + case StatusCode::BadRequest_400: return "Bad Request"; + case StatusCode::Unauthorized_401: return "Unauthorized"; + case StatusCode::PaymentRequired_402: return "Payment Required"; + case StatusCode::Forbidden_403: return "Forbidden"; + case StatusCode::NotFound_404: return "Not Found"; + case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; + case StatusCode::NotAcceptable_406: return "Not Acceptable"; + case StatusCode::ProxyAuthenticationRequired_407: + return "Proxy Authentication Required"; + case StatusCode::RequestTimeout_408: return "Request Timeout"; + case StatusCode::Conflict_409: return "Conflict"; + case StatusCode::Gone_410: return "Gone"; + case StatusCode::LengthRequired_411: return "Length Required"; + case StatusCode::PreconditionFailed_412: return "Precondition Failed"; + case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; + case StatusCode::UriTooLong_414: return "URI Too Long"; + case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; + case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; + case StatusCode::ExpectationFailed_417: return "Expectation Failed"; + case StatusCode::ImATeapot_418: return "I'm a teapot"; + case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; + case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; + case StatusCode::Locked_423: return "Locked"; + case StatusCode::FailedDependency_424: return "Failed Dependency"; + case StatusCode::TooEarly_425: return "Too Early"; + case StatusCode::UpgradeRequired_426: return "Upgrade Required"; + case StatusCode::PreconditionRequired_428: return "Precondition Required"; + case StatusCode::TooManyRequests_429: return "Too Many Requests"; + case StatusCode::RequestHeaderFieldsTooLarge_431: + return "Request Header Fields Too Large"; + case StatusCode::UnavailableForLegalReasons_451: + return "Unavailable For Legal Reasons"; + case StatusCode::NotImplemented_501: return "Not Implemented"; + case StatusCode::BadGateway_502: return "Bad Gateway"; + case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; + case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; + case StatusCode::HttpVersionNotSupported_505: + return "HTTP Version Not Supported"; + case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; + case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; + case StatusCode::LoopDetected_508: return "Loop Detected"; + case StatusCode::NotExtended_510: return "Not Extended"; + case StatusCode::NetworkAuthenticationRequired_511: + return "Network Authentication Required"; default: - case 500: return "Internal Server Error"; + case StatusCode::InternalServerError_500: return "Internal Server Error"; } } @@ -3939,14 +3945,14 @@ bool prepare_content_receiver(T &x, int &status, #ifdef CPPHTTPLIB_ZLIB_SUPPORT decompressor = detail::make_unique(); #else - status = 415; + status = StatusCode::UnsupportedMediaType_415; return false; #endif } else if (encoding.find("br") != std::string::npos) { #ifdef CPPHTTPLIB_BROTLI_SUPPORT decompressor = detail::make_unique(); #else - status = 415; + status = StatusCode::UnsupportedMediaType_415; return false; #endif } @@ -3962,7 +3968,7 @@ bool prepare_content_receiver(T &x, int &status, }; return callback(std::move(out)); } else { - status = 500; + status = StatusCode::InternalServerError_500; return false; } } @@ -4000,7 +4006,10 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } } - if (!ret) { status = exceed_payload_max_length ? 413 : 400; } + if (!ret) { + status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413 + : StatusCode::BadRequest_400; + } return ret; }); } // namespace detail @@ -4232,7 +4241,8 @@ inline bool redirect(T &cli, Request &req, Response &res, new_req.path = path; new_req.redirect_count_ -= 1; - if (res.status == 303 && (req.method != "GET" && req.method != "HEAD")) { + if (res.status == StatusCode::SeeOther_303 && + (req.method != "GET" && req.method != "HEAD")) { new_req.method = "GET"; new_req.body.clear(); new_req.headers.clear(); @@ -5311,7 +5321,7 @@ inline void Response::set_redirect(const std::string &url, int stat) { if (300 <= stat && stat < 400) { this->status = stat; } else { - this->status = 302; + this->status = StatusCode::Found_302; } } } @@ -6090,7 +6100,7 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { const auto &content_type = req.get_header_value("Content-Type"); if (!content_type.find("application/x-www-form-urlencoded")) { if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { - res.status = 413; // NOTE: should be 414? + res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? return false; } detail::parse_query_text(req.body, req.params); @@ -6121,7 +6131,7 @@ Server::read_content_core(Stream &strm, Request &req, Response &res, const auto &content_type = req.get_header_value("Content-Type"); std::string boundary; if (!detail::parse_multipart_boundary(content_type, boundary)) { - res.status = 400; + res.status = StatusCode::BadRequest_400; return false; } @@ -6157,7 +6167,7 @@ Server::read_content_core(Stream &strm, Request &req, Response &res, if (req.is_multipart_form_data()) { if (!multipart_form_data_parser.is_valid()) { - res.status = 400; + res.status = StatusCode::BadRequest_400; return false; } } @@ -6399,7 +6409,7 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { return dispatch_request(req, res, patch_handlers_); } - res.status = 400; + res.status = StatusCode::BadRequest_400; return false; } @@ -6482,7 +6492,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, res.body = res.body.substr(offset, length); } else { res.body.clear(); - res.status = 416; + res.status = StatusCode::RangeNotSatisfiable_416; } } else { std::string data; @@ -6491,7 +6501,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, res.body.swap(data); } else { res.body.clear(); - res.status = 416; + res.status = StatusCode::RangeNotSatisfiable_416; } } @@ -6569,7 +6579,7 @@ Server::process_request(Stream &strm, bool close_connection, if (strm.socket() >= FD_SETSIZE) { Headers dummy; detail::read_headers(strm, dummy); - res.status = 500; + res.status = StatusCode::InternalServerError_500; return write_response(strm, close_connection, req, res); } #endif @@ -6579,14 +6589,14 @@ Server::process_request(Stream &strm, bool close_connection, if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { Headers dummy; detail::read_headers(strm, dummy); - res.status = 414; + res.status = StatusCode::UriTooLong_414; return write_response(strm, close_connection, req, res); } // Request line and headers if (!parse_request_line(line_reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { - res.status = 400; + res.status = StatusCode::BadRequest_400; return write_response(strm, close_connection, req, res); } @@ -6610,7 +6620,7 @@ Server::process_request(Stream &strm, bool close_connection, if (req.has_header("Range")) { const auto &range_header_value = req.get_header_value("Range"); if (!detail::parse_range_header(range_header_value, req.ranges)) { - res.status = 416; + res.status = StatusCode::RangeNotSatisfiable_416; return write_response(strm, close_connection, req, res); } } @@ -6618,13 +6628,13 @@ Server::process_request(Stream &strm, bool close_connection, if (setup_request) { setup_request(req); } if (req.get_header_value("Expect") == "100-continue") { - auto status = 100; + int status = StatusCode::Continue_100; if (expect_100_continue_handler_) { status = expect_100_continue_handler_(req, res); } switch (status) { - case 100: - case 417: + case StatusCode::Continue_100: + case StatusCode::ExpectationFailed_417: strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, status_message(status)); break; @@ -6645,7 +6655,7 @@ Server::process_request(Stream &strm, bool close_connection, exception_handler_(req, res, ep); routed = true; } else { - res.status = 500; + res.status = StatusCode::InternalServerError_500; std::string val; auto s = e.what(); for (size_t i = 0; s[i]; i++) { @@ -6663,17 +6673,20 @@ Server::process_request(Stream &strm, bool close_connection, exception_handler_(req, res, ep); routed = true; } else { - res.status = 500; + res.status = StatusCode::InternalServerError_500; res.set_header("EXCEPTION_WHAT", "UNKNOWN"); } } #endif if (routed) { - if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } + if (res.status == -1) { + res.status = req.ranges.empty() ? StatusCode::OK_200 + : StatusCode::PartialContent_206; + } return write_response_with_content(strm, close_connection, req, res); } else { - if (res.status == -1) { res.status = 404; } + if (res.status == -1) { res.status = StatusCode::NotFound_404; } return write_response(strm, close_connection, req, res); } } @@ -6845,7 +6858,7 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, res.reason = std::string(m[3]); // Ignore '100 Continue' - while (res.status == 100) { + while (res.status == StatusCode::Continue_100) { if (!line_reader.getline()) { return false; } // CRLF if (!line_reader.getline()) { return false; } // next response line @@ -7014,9 +7027,10 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if ((res.status == 401 || res.status == 407) && + if ((res.status == StatusCode::Unauthorized_401 || + res.status == StatusCode::ProxyAuthenticationRequired_407) && req.authorization_count_ < 5) { - auto is_proxy = res.status == 407; + auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407; const auto &username = is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; const auto &password = @@ -7377,7 +7391,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } // Body - if ((res.status != 204) && req.method != "HEAD" && req.method != "CONNECT") { + if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && + req.method != "CONNECT") { auto redirect = 300 < res.status && res.status < 400 && follow_location_; if (req.response_handler && !redirect) { @@ -8554,7 +8569,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, return false; } - if (proxy_res.status == 407) { + if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) { if (!proxy_digest_auth_username_.empty() && !proxy_digest_auth_password_.empty()) { std::map auth; @@ -8587,7 +8602,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, // If status code is not 200, proxy request is failed. // Set error to ProxyConnection and return proxy response // as the response of the request - if (proxy_res.status != 200) { + if (proxy_res.status != StatusCode::OK_200) { error = Error::ProxyConnection; res = std::move(proxy_res); // Thread-safe to close everything because we are assuming there are From 5b943d9bb8237e4026d8a48aba4b827c5117d372 Mon Sep 17 00:00:00 2001 From: Ilya Andreev Date: Thu, 21 Dec 2023 01:28:57 +0300 Subject: [PATCH 0141/1222] Use StatusCode in tests and examples (#1743) * Use StatusCode in tests and examples * Use StatusCode in README --- README.md | 10 +- example/benchmark.cc | 2 +- test/test.cc | 547 ++++++++++++++++++++++--------------------- test/test_proxy.cc | 30 +-- 4 files changed, 297 insertions(+), 292 deletions(-) diff --git a/README.md b/README.md index 4340fa33a0..6be4fdeeb4 100644 --- a/README.md +++ b/README.md @@ -235,7 +235,7 @@ svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep) snprintf(buf, sizeof(buf), fmt, "Unknown Exception"); } res.set_content(buf, "text/html"); - res.status = 500; + res.status = StatusCode::InternalServerError_500; }); ``` @@ -385,14 +385,14 @@ By default, the server sends a `100 Continue` response for an `Expect: 100-conti ```cpp // Send a '417 Expectation Failed' response. svr.set_expect_100_continue_handler([](const Request &req, Response &res) { - return 417; + return StatusCode::ExpectationFailed_417; }); ``` ```cpp // Send a final status without reading the message body. svr.set_expect_100_continue_handler([](const Request &req, Response &res) { - return res.status = 401; + return res.status = StatusCode::Unauthorized_401; }); ``` @@ -473,7 +473,7 @@ int main(void) httplib::Client cli("localhost", 1234); if (auto res = cli.Get("/hi")) { - if (res->status == 200) { + if (res->status == StatusCode::OK_200) { std::cout << res->body << std::endl; } } else { @@ -623,7 +623,7 @@ std::string body; auto res = cli.Get( "/stream", Headers(), [&](const Response &response) { - EXPECT_EQ(200, response.status); + EXPECT_EQ(StatusCode::OK_200, response.status); return true; // return 'false' if you want to cancel the request. }, [&](const char *data, size_t data_length) { diff --git a/example/benchmark.cc b/example/benchmark.cc index 8e300b9e63..433cc675c4 100644 --- a/example/benchmark.cc +++ b/example/benchmark.cc @@ -26,7 +26,7 @@ int main(void) { for (int i = 0; i < 3; i++) { StopWatch sw(to_string(i).c_str()); auto res = cli.Post("/post", body, "application/octet-stream"); - assert(res->status == 200); + assert(res->status == httplib::StatusCode::OK_200); } return 0; diff --git a/test/test.cc b/test/test.cc index 91513cd936..3de1fb9710 100644 --- a/test/test.cc +++ b/test/test.cc @@ -434,7 +434,7 @@ TEST(ChunkedEncodingTest, FromHTTPWatch_Online) { std::string out; detail::read_file("./image.jpg", out); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, res->body); } @@ -487,7 +487,7 @@ TEST(ChunkedEncodingTest, WithContentReceiver_Online) { std::string out; detail::read_file("./image.jpg", out); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, body); } @@ -507,7 +507,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { auto res = cli.Get( "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", [&](const Response &response) { - EXPECT_EQ(200, response.status); + EXPECT_EQ(StatusCode::OK_200, response.status); return true; }, [&](const char *data, size_t data_length) { @@ -519,7 +519,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { std::string out; detail::read_file("./image.jpg", out); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, body); } @@ -545,7 +545,7 @@ TEST(RangeTest, FromHTTPBin_Online) { auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -553,7 +553,7 @@ TEST(RangeTest, FromHTTPBin_Online) { auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); } { @@ -561,7 +561,7 @@ TEST(RangeTest, FromHTTPBin_Online) { auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); } { @@ -569,7 +569,7 @@ TEST(RangeTest, FromHTTPBin_Online) { auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -577,14 +577,14 @@ TEST(RangeTest, FromHTTPBin_Online) { auto res = cli.Get(path, headers); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { Headers headers = {make_range_header({{0, 32}})}; auto res = cli.Get(path, headers); ASSERT_TRUE(res); - EXPECT_EQ(416, res->status); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } } @@ -697,7 +697,7 @@ TEST(CancelTest, NoCancel_Online) { auto res = cli.Get(path, [](uint64_t, uint64_t) { return true; }); ASSERT_TRUE(res); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(CancelTest, WithCancelSmallPayload_Online) { @@ -768,7 +768,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { { auto res = cli.Get(path); ASSERT_TRUE(res); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } { @@ -777,7 +777,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -786,21 +786,21 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { cli.set_basic_auth("hello", "bad"); auto res = cli.Get(path); ASSERT_TRUE(res); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } { cli.set_basic_auth("bad", "world"); auto res = cli.Get(path); ASSERT_TRUE(res); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } } @@ -832,7 +832,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { { auto res = cli.Get(unauth_path); ASSERT_TRUE(res); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } { @@ -843,14 +843,14 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { ASSERT_TRUE(res); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } cli.set_digest_auth("hello", "bad"); for (const auto &path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } // NOTE: Until httpbin.org fixes issue #46, the following test is commented @@ -859,7 +859,7 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { // for (const auto& path : paths) { // auto res = cli.Get(path.c_str()); // ASSERT_TRUE(res); - // EXPECT_EQ(400, res->status); + // EXPECT_EQ(StatusCode::BadRequest_400, res->status); // } } } @@ -879,7 +879,7 @@ TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) { cli.set_hostname_addr_map({{another_host, wrong_ip}}); auto res = cli.Get("/"); ASSERT_TRUE(res); - ASSERT_EQ(301, res->status); + ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(SpecifyServerIPAddressTest, RealHostname_Online) { @@ -910,7 +910,7 @@ TEST(AbsoluteRedirectTest, Redirect_Online) { cli.set_follow_location(true); auto res = cli.Get("/httpbin/absolute-redirect/3"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(RedirectTest, Redirect_Online) { @@ -925,7 +925,7 @@ TEST(RedirectTest, Redirect_Online) { cli.set_follow_location(true); auto res = cli.Get("/httpbin/redirect/3"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(RelativeRedirectTest, Redirect_Online) { @@ -940,7 +940,7 @@ TEST(RelativeRedirectTest, Redirect_Online) { cli.set_follow_location(true); auto res = cli.Get("/httpbin/relative-redirect/3"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(TooManyRedirectTest, Redirect_Online) { @@ -964,12 +964,12 @@ TEST(YahooRedirectTest, Redirect_Online) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(301, res->status); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); cli.set_follow_location(true); res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("https://www.yahoo.com/", res->location); } @@ -979,7 +979,7 @@ TEST(HttpsToHttpRedirectTest, Redirect_Online) { auto res = cli.Get( "/httpbin/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest2, Redirect_Online) { @@ -992,7 +992,7 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) { auto res = cli.Get("/httpbin/redirect-to", params, Headers{}); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest3, Redirect_Online) { @@ -1004,7 +1004,7 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) { auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{}); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(UrlWithSpace, Redirect_Online) { @@ -1013,7 +1013,7 @@ TEST(UrlWithSpace, Redirect_Online) { auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(18527U, res->get_header_value_u64("Content-Length")); } @@ -1096,7 +1096,7 @@ TEST(RedirectToDifferentPort, Redirect) { auto res = cli.Get("/2"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", res->body); } @@ -1132,7 +1132,7 @@ TEST(RedirectFromPageWithContent, Redirect) { }); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", body); } @@ -1146,7 +1146,7 @@ TEST(RedirectFromPageWithContent, Redirect) { }); ASSERT_TRUE(res); - EXPECT_EQ(302, res->status); + EXPECT_EQ(StatusCode::Found_302, res->status); EXPECT_EQ("___", body); } } @@ -1197,7 +1197,7 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { }); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", body); } @@ -1211,7 +1211,7 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { }); ASSERT_TRUE(res); - EXPECT_EQ(302, res->status); + EXPECT_EQ(StatusCode::Found_302, res->status); EXPECT_EQ("___", body); } } @@ -1223,9 +1223,9 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { auto a = req.params.find("a"); if (a != req.params.end()) { res.set_content((*a).second, "text/plain"); - res.status = 200; + res.status = StatusCode::OK_200; } else { - res.status = 400; + res.status = StatusCode::BadRequest_400; } }); @@ -1244,7 +1244,7 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { auto res = cli.Get("/foo?a=explicitly+encoded"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); // This expects it back with a space, as the `+` won't have been // url-encoded, and server-side the params get decoded turning `+` // into spaces. @@ -1273,7 +1273,7 @@ TEST(BindServerTest, DISABLED_BindDualStack) { auto res = cli.Get("/1"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", res->body); } { @@ -1281,7 +1281,7 @@ TEST(BindServerTest, DISABLED_BindDualStack) { auto res = cli.Get("/1"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", res->body); } } @@ -1320,7 +1320,7 @@ TEST(ErrorHandlerTest, ContentLength) { Server svr; svr.set_error_handler([](const Request & /*req*/, Response &res) { - res.status = 200; + res.status = StatusCode::OK_200; res.set_content("abcdefghijklmnopqrstuvwxyz", "text/html"); // <= Content-Length still 13 }); @@ -1344,7 +1344,7 @@ TEST(ErrorHandlerTest, ContentLength) { auto res = cli.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("26", res->get_header_value("Content-Length")); EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); @@ -1361,7 +1361,7 @@ TEST(ExceptionHandlerTest, ContentLength) { try { std::rethrow_exception(ep); } catch (std::exception &e) { EXPECT_EQ("abc", std::string(e.what())); } - res.status = 500; + res.status = StatusCode::InternalServerError_500; res.set_content("abcdefghijklmnopqrstuvwxyz", "text/html"); // <= Content-Length still 13 at this point }); @@ -1386,7 +1386,7 @@ TEST(ExceptionHandlerTest, ContentLength) { for (size_t j = 0; j < 100; j++) { auto res = cli.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(500, res->status); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("26", res->get_header_value("Content-Length")); EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); @@ -1397,7 +1397,7 @@ TEST(ExceptionHandlerTest, ContentLength) { for (size_t j = 0; j < 100; j++) { auto res = cli.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(500, res->status); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("26", res->get_header_value("Content-Length")); EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body); @@ -1409,8 +1409,9 @@ TEST(ExceptionHandlerTest, ContentLength) { TEST(NoContentTest, ContentLength) { Server svr; - svr.Get("/hi", - [](const Request & /*req*/, Response &res) { res.status = 204; }); + svr.Get("/hi", [](const Request & /*req*/, Response &res) { + res.status = StatusCode::NoContent_204; + }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); auto se = detail::scope_exit([&] { svr.stop(); @@ -1425,7 +1426,7 @@ TEST(NoContentTest, ContentLength) { auto res = cli.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(204, res->status); + EXPECT_EQ(StatusCode::NoContent_204, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); } } @@ -1480,7 +1481,7 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { auto res = cli.Get("/routing_handler"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Routing Handler", res->body); EXPECT_EQ(1U, res->get_header_value_count("PRE_ROUTING")); EXPECT_EQ("on", res->get_header_value("PRE_ROUTING")); @@ -1498,7 +1499,7 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { auto res = cli.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!\n", res->body); EXPECT_EQ(0U, res->get_header_value_count("PRE_ROUTING")); EXPECT_EQ(0U, res->get_header_value_count("POST_ROUTING")); @@ -1514,7 +1515,7 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { auto res = cli.Get("/aaa"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); EXPECT_EQ("Error", res->body); EXPECT_EQ(0U, res->get_header_value_count("PRE_ROUTING")); EXPECT_EQ(0U, res->get_header_value_count("POST_ROUTING")); @@ -1567,11 +1568,11 @@ TEST(URLFragmentTest, WithFragment) { auto res = cli.Get("/hi#key1=val1=key2=val2"); EXPECT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); res = cli.Get("/hi%23key1=val1=key2=val2"); EXPECT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } } @@ -1607,7 +1608,7 @@ TEST(HeaderWriter, SetHeaderWriter) { auto res = cli.Get("/hi"); EXPECT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); auto it = res->headers.find("CustomServerHeader"); EXPECT_TRUE(it != res->headers.end()); @@ -1712,12 +1713,14 @@ class ServerTest : public ::testing::Test { }) .Get("/", [&](const Request & /*req*/, Response &res) { res.set_redirect("/hi"); }) - .Post("/1", [](const Request & /*req*/, - Response &res) { res.set_redirect("/2", 303); }) + .Post("/1", + [](const Request & /*req*/, Response &res) { + res.set_redirect("/2", StatusCode::SeeOther_303); + }) .Get("/2", [](const Request & /*req*/, Response &res) { res.set_content("redirected.", "text/plain"); - res.status = 200; + res.status = StatusCode::OK_200; }) .Post("/person", [&](const Request &req, Response &res) { @@ -1725,7 +1728,7 @@ class ServerTest : public ::testing::Test { persons_[req.get_param_value("name")] = req.get_param_value("note"); } else { - res.status = 400; + res.status = StatusCode::BadRequest_400; } }) .Put("/person", @@ -1734,7 +1737,7 @@ class ServerTest : public ::testing::Test { persons_[req.get_param_value("name")] = req.get_param_value("note"); } else { - res.status = 400; + res.status = StatusCode::BadRequest_400; } }) .Get("/person/(.*)", @@ -1744,7 +1747,7 @@ class ServerTest : public ::testing::Test { auto note = persons_[name]; res.set_content(note, "text/plain"); } else { - res.status = 404; + res.status = StatusCode::NotFound_404; } }) .Post("/x-www-form-urlencoded-json", @@ -1752,7 +1755,7 @@ class ServerTest : public ::testing::Test { auto json = req.get_param_value("json"); ASSERT_EQ(JSON_DATA, json); res.set_content(json, "appliation/json"); - res.status = 200; + res.status = StatusCode::OK_200; }) .Get("/streamed-chunked", [&](const Request & /*req*/, Response &res) { @@ -2223,7 +2226,7 @@ TEST_F(ServerTest, GetMethod200) { auto res = cli_.Get("/hi"); ASSERT_TRUE(res); EXPECT_EQ("HTTP/1.1", res->version); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("OK", res->reason); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ(1U, res->get_header_value_count("Content-Type")); @@ -2234,7 +2237,7 @@ TEST_F(ServerTest, GetMethod200withPercentEncoding) { auto res = cli_.Get("/%68%69"); // auto res = cli_.Get("/hi"); ASSERT_TRUE(res); EXPECT_EQ("HTTP/1.1", res->version); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ(1U, res->get_header_value_count("Content-Type")); EXPECT_EQ("Hello World!", res->body); @@ -2243,7 +2246,7 @@ TEST_F(ServerTest, GetMethod200withPercentEncoding) { TEST_F(ServerTest, GetMethod302) { auto res = cli_.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(302, res->status); + EXPECT_EQ(StatusCode::Found_302, res->status); EXPECT_EQ("/hi", res->get_header_value("Location")); } @@ -2251,7 +2254,7 @@ TEST_F(ServerTest, GetMethod302Redirect) { cli_.set_follow_location(true); auto res = cli_.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", res->body); EXPECT_EQ("/hi", res->location); } @@ -2259,13 +2262,13 @@ TEST_F(ServerTest, GetMethod302Redirect) { TEST_F(ServerTest, GetMethod404) { auto res = cli_.Get("/invalid"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, HeadMethod200) { auto res = cli_.Head("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_TRUE(res->body.empty()); } @@ -2273,7 +2276,7 @@ TEST_F(ServerTest, HeadMethod200) { TEST_F(ServerTest, HeadMethod200Static) { auto res = cli_.Head("/mount/dir/index.html"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ(104, std::stoi(res->get_header_value("Content-Length"))); EXPECT_TRUE(res->body.empty()); @@ -2282,14 +2285,14 @@ TEST_F(ServerTest, HeadMethod200Static) { TEST_F(ServerTest, HeadMethod404) { auto res = cli_.Head("/invalid"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); EXPECT_TRUE(res->body.empty()); } TEST_F(ServerTest, GetMethodPersonJohn) { auto res = cli_.Get("/person/john"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("programmer", res->body); } @@ -2297,16 +2300,16 @@ TEST_F(ServerTest, GetMethodPersonJohn) { TEST_F(ServerTest, PostMethod1) { auto res = cli_.Get("/person/john1"); ASSERT_TRUE(res); - ASSERT_EQ(404, res->status); + ASSERT_EQ(StatusCode::NotFound_404, res->status); res = cli_.Post("/person", "name=john1¬e=coder", "application/x-www-form-urlencoded"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); res = cli_.Get("/person/john1"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); ASSERT_EQ("coder", res->body); } @@ -2314,7 +2317,7 @@ TEST_F(ServerTest, PostMethod1) { TEST_F(ServerTest, PostMethod2) { auto res = cli_.Get("/person/john2"); ASSERT_TRUE(res); - ASSERT_EQ(404, res->status); + ASSERT_EQ(StatusCode::NotFound_404, res->status); Params params; params.emplace("name", "john2"); @@ -2322,11 +2325,11 @@ TEST_F(ServerTest, PostMethod2) { res = cli_.Post("/person", params); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); res = cli_.Get("/person/john2"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); ASSERT_EQ("coder", res->body); } @@ -2334,7 +2337,7 @@ TEST_F(ServerTest, PostMethod2) { TEST_F(ServerTest, PutMethod3) { auto res = cli_.Get("/person/john3"); ASSERT_TRUE(res); - ASSERT_EQ(404, res->status); + ASSERT_EQ(StatusCode::NotFound_404, res->status); Params params; params.emplace("name", "john3"); @@ -2342,11 +2345,11 @@ TEST_F(ServerTest, PutMethod3) { res = cli_.Put("/person", params); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); res = cli_.Get("/person/john3"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); ASSERT_EQ("coder", res->body); } @@ -2358,28 +2361,28 @@ TEST_F(ServerTest, PostWwwFormUrlEncodedJson) { auto res = cli_.Post("/x-www-form-urlencoded-json", params); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(JSON_DATA, res->body); } TEST_F(ServerTest, PostEmptyContent) { auto res = cli_.Post("/empty", "", "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("empty", res->body); } TEST_F(ServerTest, PostEmptyContentWithNoContentType) { auto res = cli_.Post("/empty-no-content-type"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("empty-no-content-type", res->body); } TEST_F(ServerTest, PostPathOnly) { auto res = cli_.Post("/path-only"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("path-only", res->body); } @@ -2387,28 +2390,28 @@ TEST_F(ServerTest, PostPathAndHeadersOnly) { auto res = cli_.Post("/path-headers-only", Headers({{"hello", "world"}, {"hello2", "world2"}})); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("path-headers-only", res->body); } TEST_F(ServerTest, PostLarge) { auto res = cli_.Post("/post-large", LARGE_DATA, "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(LARGE_DATA, res->body); } TEST_F(ServerTest, PutEmptyContentWithNoContentType) { auto res = cli_.Put("/empty-no-content-type"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("empty-no-content-type", res->body); } TEST_F(ServerTest, GetMethodDir) { auto res = cli_.Get("/dir/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); auto body = R"( @@ -2426,7 +2429,7 @@ TEST_F(ServerTest, GetMethodDir) { TEST_F(ServerTest, GetMethodDirTest) { auto res = cli_.Get("/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("test.html", res->body); } @@ -2434,7 +2437,7 @@ TEST_F(ServerTest, GetMethodDirTest) { TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) { auto res = cli_.Get("/dir/../dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("test.html", res->body); } @@ -2442,25 +2445,25 @@ TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) { TEST_F(ServerTest, GetMethodInvalidPath) { auto res = cli_.Get("/dir/../test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, GetMethodOutOfBaseDir) { auto res = cli_.Get("/../www/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, GetMethodOutOfBaseDir2) { auto res = cli_.Get("/dir/../../www/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, GetMethodDirMountTest) { auto res = cli_.Get("/mount/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("test.html", res->body); } @@ -2468,7 +2471,7 @@ TEST_F(ServerTest, GetMethodDirMountTest) { TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) { auto res = cli_.Get("/mount/dir/../dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); EXPECT_EQ("test.html", res->body); } @@ -2476,25 +2479,25 @@ TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) { TEST_F(ServerTest, GetMethodInvalidMountPath) { auto res = cli_.Get("/mount/dir/../test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, GetMethodOutOfBaseDirMount) { auto res = cli_.Get("/mount/../www2/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) { auto res = cli_.Get("/mount/dir/../../www2/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, PostMethod303) { auto res = cli_.Post("/1", "body", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(303, res->status); + EXPECT_EQ(StatusCode::SeeOther_303, res->status); EXPECT_EQ("/2", res->get_header_value("Location")); } @@ -2502,7 +2505,7 @@ TEST_F(ServerTest, PostMethod303Redirect) { cli_.set_follow_location(true); auto res = cli_.Post("/1", "body", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("redirected.", res->body); EXPECT_EQ("/2", res->location); } @@ -2510,7 +2513,7 @@ TEST_F(ServerTest, PostMethod303Redirect) { TEST_F(ServerTest, UserDefinedMIMETypeMapping) { auto res = cli_.Get("/dir/test.abcde"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("abcde", res->body); } @@ -2518,7 +2521,7 @@ TEST_F(ServerTest, UserDefinedMIMETypeMapping) { TEST_F(ServerTest, StaticFileRange) { auto res = cli_.Get("/dir/test.abcde", {{make_range_header({{2, 3}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); @@ -2529,7 +2532,7 @@ TEST_F(ServerTest, StaticFileRanges) { auto res = cli_.Get("/dir/test.abcde", {{make_range_header({{1, 2}, {4, -1}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_TRUE( res->get_header_value("Content-Type") .find( @@ -2541,7 +2544,7 @@ TEST_F(ServerTest, StaticFileRanges) { TEST_F(ServerTest, StaticFileRangeHead) { auto res = cli_.Head("/dir/test.abcde", {{make_range_header({{2, 3}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); @@ -2550,7 +2553,7 @@ TEST_F(ServerTest, StaticFileRangeHead) { TEST_F(ServerTest, StaticFileRangeBigFile) { auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{-1, 5}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("5", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); @@ -2560,7 +2563,7 @@ TEST_F(ServerTest, StaticFileRangeBigFile) { TEST_F(ServerTest, StaticFileRangeBigFile2) { auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{1, 4097}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("4097", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); @@ -2569,7 +2572,7 @@ TEST_F(ServerTest, StaticFileRangeBigFile2) { TEST_F(ServerTest, StaticFileBigFile) { auto res = cli_.Get("/dir/1MB.txt"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("1048576", res->get_header_value("Content-Length")); } @@ -2584,25 +2587,25 @@ TEST_F(ServerTest, Binary) { auto res = cli_.Post("/binary", binary.data(), binary.size(), "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); res = cli_.Put("/binary", binary.data(), binary.size(), "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); res = cli_.Patch("/binary", binary.data(), binary.size(), "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); res = cli_.Delete("/binary", binary.data(), binary.size(), "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); } @@ -2611,22 +2614,22 @@ TEST_F(ServerTest, BinaryString) { auto res = cli_.Post("/binary", binary, "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); res = cli_.Put("/binary", binary, "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); res = cli_.Patch("/binary", binary, "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); res = cli_.Delete("/binary", binary, "application/octet-stream"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(4U, res->body.size()); } @@ -2646,7 +2649,7 @@ TEST_F(ServerTest, LongRequest) { auto res = cli_.Get(request.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, TooLongRequest) { @@ -2659,7 +2662,7 @@ TEST_F(ServerTest, TooLongRequest) { auto res = cli_.Get(request.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(414, res->status); + EXPECT_EQ(StatusCode::UriTooLong_414, res->status); } TEST_F(ServerTest, LongHeader) { @@ -2713,14 +2716,14 @@ TEST_F(ServerTest, LongHeader) { auto ret = cli_.send(req, *res, error); ASSERT_TRUE(ret); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, LongQueryValue) { auto res = cli_.Get(LONG_QUERY_URL.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(414, res->status); + EXPECT_EQ(StatusCode::UriTooLong_414, res->status); } TEST_F(ServerTest, TooLongHeader) { @@ -2774,43 +2777,43 @@ TEST_F(ServerTest, TooLongHeader) { auto ret = cli_.send(req, *res, error); ASSERT_TRUE(ret); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, PercentEncoding) { auto res = cli_.Get("/e%6edwith%"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, PercentEncodingUnicode) { auto res = cli_.Get("/e%u006edwith%"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, InvalidPercentEncoding) { auto res = cli_.Get("/%endwith%"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, InvalidPercentEncodingUnicode) { auto res = cli_.Get("/%uendwith%"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, EndWithPercentCharacterInQuery) { auto res = cli_.Get("/hello?aaa=bbb%"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } TEST_F(ServerTest, PlusSignEncoding) { auto res = cli_.Get("/a+%2Bb?a %2bb=a %2Bb"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("a +b", res->body); } @@ -2826,7 +2829,7 @@ TEST_F(ServerTest, MultipartFormData) { auto res = cli_.Post("/multipart", items); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, MultipartFormDataMultiFileValues) { @@ -2844,13 +2847,13 @@ TEST_F(ServerTest, MultipartFormDataMultiFileValues) { auto res = cli_.Post("/multipart/multi_file_values", items); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, CaseInsensitiveHeaderName) { auto res = cli_.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("content-type")); EXPECT_EQ("Hello World!", res->body); } @@ -2882,13 +2885,13 @@ TEST_F(ServerTest, CaseInsensitiveTransferEncoding) { auto ret = cli_.send(req, *res, error); ASSERT_TRUE(ret); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, GetStreamed2) { auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(std::string("ab"), res->body); } @@ -2896,7 +2899,7 @@ TEST_F(ServerTest, GetStreamed2) { TEST_F(ServerTest, GetStreamed) { auto res = cli_.Get("/streamed"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); EXPECT_EQ(std::string("aaabbb"), res->body); } @@ -2904,7 +2907,7 @@ TEST_F(ServerTest, GetStreamed) { TEST_F(ServerTest, GetStreamedWithRange1) { auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("def"), res->body); @@ -2913,7 +2916,7 @@ TEST_F(ServerTest, GetStreamedWithRange1) { TEST_F(ServerTest, GetStreamedWithRange2) { auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("bcdefg"), res->body); @@ -2922,7 +2925,7 @@ TEST_F(ServerTest, GetStreamedWithRange2) { TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-3"}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("efg"), res->body); @@ -2931,7 +2934,7 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-9999"}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("7", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("abcdefg"), res->body); @@ -2942,13 +2945,13 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { {{"Range", "bytes=92233720368547758079223372036854775806-" "92233720368547758079223372036854775807"}}); ASSERT_TRUE(res); - EXPECT_EQ(416, res->status); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } TEST_F(ServerTest, GetRangeWithMaxLongLength) { auto res = cli_.Get("/with-range", {{"Range", "bytes=0-9223372036854775807"}}); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("7", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("abcdefg"), res->body); @@ -2958,7 +2961,7 @@ TEST_F(ServerTest, GetStreamedWithRangeMultipart) { auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); EXPECT_EQ(267U, res->body.size()); @@ -3008,7 +3011,7 @@ TEST_F(ServerTest, ClientStop) { TEST_F(ServerTest, GetWithRange1) { auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("def"), res->body); @@ -3017,7 +3020,7 @@ TEST_F(ServerTest, GetWithRange1) { TEST_F(ServerTest, GetWithRange2) { auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("bcdefg"), res->body); @@ -3026,7 +3029,7 @@ TEST_F(ServerTest, GetWithRange2) { TEST_F(ServerTest, GetWithRange3) { auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("1", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("a"), res->body); @@ -3035,7 +3038,7 @@ TEST_F(ServerTest, GetWithRange3) { TEST_F(ServerTest, GetWithRange4) { auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("fg"), res->body); @@ -3044,13 +3047,13 @@ TEST_F(ServerTest, GetWithRange4) { TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) { auto res = cli_.Get("/with-range", {{make_range_header({{10000, 20000}})}}); ASSERT_TRUE(res); - EXPECT_EQ(416, res->status); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } TEST_F(ServerTest, GetWithRangeMultipart) { auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}}); ASSERT_TRUE(res); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); EXPECT_EQ(267U, res->body.size()); @@ -3060,27 +3063,27 @@ TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}, {10000, 30000}})}}); ASSERT_TRUE(res); - EXPECT_EQ(416, res->status); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } TEST_F(ServerTest, GetStreamedChunked) { auto res = cli_.Get("/streamed-chunked"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); } TEST_F(ServerTest, GetStreamedChunked2) { auto res = cli_.Get("/streamed-chunked2"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); } TEST_F(ServerTest, GetStreamedChunkedWithTrailer) { auto res = cli_.Get("/streamed-chunked-with-trailer"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); EXPECT_EQ(std::string("DummyVal1"), res->get_header_value("Dummy1")); EXPECT_EQ(std::string("DummyVal2"), res->get_header_value("Dummy2")); @@ -3115,13 +3118,13 @@ TEST_F(ServerTest, LargeChunkedPost) { auto ret = cli_.send(req, *res, error); ASSERT_TRUE(ret); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, GetMethodRemoteAddr) { auto res = cli_.Get("/remote_addr"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1"); } @@ -3129,7 +3132,7 @@ TEST_F(ServerTest, GetMethodRemoteAddr) { TEST_F(ServerTest, GetMethodLocalAddr) { auto res = cli_.Get("/local_addr"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_TRUE(res->body == std::string("::1:").append(to_string(PORT)) || res->body == std::string("127.0.0.1:").append(to_string(PORT))); @@ -3138,7 +3141,7 @@ TEST_F(ServerTest, GetMethodLocalAddr) { TEST_F(ServerTest, HTTPResponseSplitting) { auto res = cli_.Get("/http_response_splitting"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, SlowRequest) { @@ -3162,7 +3165,7 @@ TEST_F(ServerTest, SlowPost) { "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, SlowPostFail) { @@ -3186,7 +3189,7 @@ TEST_F(ServerTest, SlowPostFail) { TEST_F(ServerTest, Put) { auto res = cli_.Put("/put", "PUT", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PUT", res->body); } @@ -3200,7 +3203,7 @@ TEST_F(ServerTest, PutWithContentProvider) { "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PUT", res->body); } @@ -3227,7 +3230,7 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLength) { "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PUT", res->body); } @@ -3252,7 +3255,7 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) { "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PUT", res->body); } @@ -3281,7 +3284,7 @@ TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) { "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PUT", res->body); } @@ -3300,7 +3303,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip) { auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(LARGE_DATA, res->body); } @@ -3318,7 +3321,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) { auto res = cli.Put("/put-large", LARGE_DATA, "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(LARGE_DATA, res->body); EXPECT_EQ(101942u, res.get_request_header_value_u64("Content-Length")); EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding")); @@ -3333,7 +3336,7 @@ TEST_F(ServerTest, PutContentWithDeflate) { "\170\234\013\010\015\001\0\001\361\0\372", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PUT", res->body); } @@ -3343,7 +3346,7 @@ TEST_F(ServerTest, GetStreamedChunkedWithGzip) { auto res = cli_.Get("/streamed-chunked", headers); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); } @@ -3353,14 +3356,14 @@ TEST_F(ServerTest, GetStreamedChunkedWithGzip2) { auto res = cli_.Get("/streamed-chunked2", headers); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); } TEST_F(ServerTest, SplitDelimiterInPathRegex) { auto res = cli_.Get("/regex-with-delimiter?key=^(?.*(value))"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(GzipDecompressor, ChunkedDecompression) { @@ -3518,7 +3521,7 @@ TEST_F(ServerTest, GetStreamedChunkedWithBrotli) { auto res = cli_.Get("/streamed-chunked", headers); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); } @@ -3528,7 +3531,7 @@ TEST_F(ServerTest, GetStreamedChunkedWithBrotli2) { auto res = cli_.Get("/streamed-chunked2", headers); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); } #endif @@ -3536,28 +3539,28 @@ TEST_F(ServerTest, GetStreamedChunkedWithBrotli2) { TEST_F(ServerTest, Patch) { auto res = cli_.Patch("/patch", "PATCH", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("PATCH", res->body); } TEST_F(ServerTest, Delete) { auto res = cli_.Delete("/delete"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("DELETE", res->body); } TEST_F(ServerTest, DeleteContentReceiver) { auto res = cli_.Delete("/delete-body", "content", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("content", res->body); } TEST_F(ServerTest, Options) { auto res = cli_.Options("*"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow")); EXPECT_TRUE(res->body.empty()); } @@ -3565,13 +3568,13 @@ TEST_F(ServerTest, Options) { TEST_F(ServerTest, URL) { auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, ArrayParam) { auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, NoMultipleHeaders) { @@ -3579,13 +3582,13 @@ TEST_F(ServerTest, NoMultipleHeaders) { auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, PostContentReceiver) { auto res = cli_.Post("/content_receiver", "content", "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("content", res->body); } @@ -3601,7 +3604,7 @@ TEST_F(ServerTest, PostMultipartFileContentReceiver) { auto res = cli_.Post("/content_receiver", items); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, PostMultipartPlusBoundary) { @@ -3636,28 +3639,28 @@ TEST_F(ServerTest, PostMultipartPlusBoundary) { auto res = cli_.Post("/content_receiver", body, content_type.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, PostContentReceiverGzip) { cli_.set_compress(true); auto res = cli_.Post("/content_receiver", "content", "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("content", res->body); } TEST_F(ServerTest, PutContentReceiver) { auto res = cli_.Put("/content_receiver", "content", "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("content", res->body); } TEST_F(ServerTest, PatchContentReceiver) { auto res = cli_.Patch("/content_receiver", "content", "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ("content", res->body); } @@ -3665,7 +3668,7 @@ TEST_F(ServerTest, PostQueryStringAndBody) { auto res = cli_.Post("/query-string-and-body?key=value", "content", "text/plain"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, HTTP2Magic) { @@ -3679,35 +3682,35 @@ TEST_F(ServerTest, HTTP2Magic) { auto ret = cli_.send(req, *res, error); ASSERT_TRUE(ret); - EXPECT_EQ(400, res->status); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); } TEST_F(ServerTest, KeepAlive) { auto res = cli_.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("Hello World!", res->body); res = cli_.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("Hello World!", res->body); res = cli_.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("Hello World!", res->body); res = cli_.Get("/not-exist"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); res = cli_.Post("/empty", "", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("empty", res->body); EXPECT_EQ("close", res->get_header_value("Connection")); @@ -3716,14 +3719,14 @@ TEST_F(ServerTest, KeepAlive) { "/empty", 0, [&](size_t, size_t, DataSink &) { return true; }, "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("empty", res->body); cli_.set_keep_alive(false); res = cli_.Get("/last-request"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("close", res->get_header_value("Connection")); } @@ -3747,7 +3750,7 @@ TEST_F(ServerTest, Gzip) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, GzipWithoutAcceptEncoding) { @@ -3760,7 +3763,7 @@ TEST_F(ServerTest, GzipWithoutAcceptEncoding) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, GzipWithContentReceiver) { @@ -3781,7 +3784,7 @@ TEST_F(ServerTest, GzipWithContentReceiver) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, GzipWithoutDecompressing) { @@ -3796,7 +3799,7 @@ TEST_F(ServerTest, GzipWithoutDecompressing) { EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("33", res->get_header_value("Content-Length")); EXPECT_EQ(33U, res->body.size()); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) { @@ -3814,7 +3817,7 @@ TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, NoGzip) { @@ -3829,7 +3832,7 @@ TEST_F(ServerTest, NoGzip) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, NoGzipWithContentReceiver) { @@ -3850,7 +3853,7 @@ TEST_F(ServerTest, NoGzipWithContentReceiver) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST_F(ServerTest, MultipartFormDataGzip) { @@ -3863,7 +3866,7 @@ TEST_F(ServerTest, MultipartFormDataGzip) { auto res = cli_.Post("/compress-multipart", items); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } #endif @@ -3880,7 +3883,7 @@ TEST_F(ServerTest, Brotli) { EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" "7890123456789012345678901234567890", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } #endif @@ -4130,8 +4133,10 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) { TEST(ServerStopTest, ClientAccessAfterServerDown) { httplib::Server svr; - svr.Post("/hi", [&](const httplib::Request & /*req*/, - httplib::Response &res) { res.status = 200; }); + svr.Post("/hi", + [&](const httplib::Request & /*req*/, httplib::Response &res) { + res.status = StatusCode::OK_200; + }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); @@ -4141,7 +4146,7 @@ TEST(ServerStopTest, ClientAccessAfterServerDown) { auto res = cli.Post("/hi", "data", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); svr.stop(); thread.join(); @@ -4220,27 +4225,27 @@ TEST(MountTest, Unmount) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); res = cli.Get("/mount2/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); svr.set_mount_point("/", "./www"); res = cli.Get("/dir/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); svr.remove_mount_point("/"); res = cli.Get("/dir/"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); svr.remove_mount_point("/mount2"); res = cli.Get("/mount2/dir/test.html"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); } #ifndef CPPHTTPLIB_NO_EXCEPTIONS @@ -4269,7 +4274,7 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { { auto res = cli.Get("/exception"); ASSERT_TRUE(res); - EXPECT_EQ(500, res->status); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT")); } @@ -4277,7 +4282,7 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { { auto res = cli.Get("/unknown"); ASSERT_TRUE(res); - EXPECT_EQ(500, res->status); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT")); } @@ -4315,7 +4320,7 @@ TEST(KeepAliveTest, ReadTimeout) { auto resb = cli.Get("/b"); ASSERT_TRUE(resb); - EXPECT_EQ(200, resb->status); + EXPECT_EQ(StatusCode::OK_200, resb->status); EXPECT_EQ("b", resb->body); } @@ -4341,13 +4346,13 @@ TEST(KeepAliveTest, Issue1041) { auto result = cli.Get("/hi"); ASSERT_TRUE(result); - EXPECT_EQ(200, result->status); + EXPECT_EQ(StatusCode::OK_200, result->status); std::this_thread::sleep_for(std::chrono::seconds(5)); result = cli.Get("/hi"); ASSERT_TRUE(result); - EXPECT_EQ(200, result->status); + EXPECT_EQ(StatusCode::OK_200, result->status); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -4375,22 +4380,22 @@ TEST(KeepAliveTest, SSLClientReconnection) { auto result = cli.Get("/hi"); ASSERT_TRUE(result); - EXPECT_EQ(200, result->status); + EXPECT_EQ(StatusCode::OK_200, result->status); result = cli.Get("/hi"); ASSERT_TRUE(result); - EXPECT_EQ(200, result->status); + EXPECT_EQ(StatusCode::OK_200, result->status); std::this_thread::sleep_for(std::chrono::seconds(2)); // Recoonect result = cli.Get("/hi"); ASSERT_TRUE(result); - EXPECT_EQ(200, result->status); + EXPECT_EQ(StatusCode::OK_200, result->status); result = cli.Get("/hi"); ASSERT_TRUE(result); - EXPECT_EQ(200, result->status); + EXPECT_EQ(StatusCode::OK_200, result->status); } #endif @@ -4471,7 +4476,7 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(404, res->status); + EXPECT_EQ(StatusCode::NotFound_404, res->status); EXPECT_EQ("helloworld", res->body); } @@ -4558,7 +4563,7 @@ TEST(GetWithParametersTest, GetWithParameters) { auto res = cli.Get("/", params, Headers{}); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -4567,7 +4572,7 @@ TEST(GetWithParametersTest, GetWithParameters) { auto res = cli.Get("/params?hello=world&hello2=world2&hello3=world3"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -4576,7 +4581,7 @@ TEST(GetWithParametersTest, GetWithParameters) { auto res = cli.Get("/resources/resource-id?param1=foo¶m2=bar"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -4585,7 +4590,7 @@ TEST(GetWithParametersTest, GetWithParameters) { auto res = cli.Get("/users/user-id?param1=foo¶m2=bar"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } } @@ -4619,7 +4624,7 @@ TEST(GetWithParametersTest, GetWithParameters2) { }); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("world", body); } @@ -4645,14 +4650,14 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); } { auto res = cli.Get(path); ASSERT_TRUE(res); EXPECT_EQ("bcdefghijk", res->body); - EXPECT_EQ(206, res->status); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); } } @@ -4678,7 +4683,7 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("ok", res->body); EXPECT_EQ("World", res->get_header_value("Hello")); } @@ -4717,7 +4722,7 @@ TEST(KeepAliveTest, ReadTimeoutSSL) { auto resb = cli.Get("/b"); ASSERT_TRUE(resb); - EXPECT_EQ(200, resb->status); + EXPECT_EQ(StatusCode::OK_200, resb->status); EXPECT_EQ("b", resb->body); } #endif @@ -4765,7 +4770,7 @@ class ServerTestWithAI_PASSIVE : public ::testing::Test { TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) { auto res = cli_.Get("/hi"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("Hello World!", res->body); } @@ -4843,11 +4848,11 @@ class PayloadMaxLengthTest : public ::testing::Test { TEST_F(PayloadMaxLengthTest, ExceedLimit) { auto res = cli_.Post("/test", "123456789", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(413, res->status); + EXPECT_EQ(StatusCode::PayloadTooLarge_413, res->status); res = cli_.Post("/test", "12345678", "text/plain"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HostAndPortPropertiesTest, NoSSL) { @@ -4896,14 +4901,14 @@ TEST(SSLClientTest, ServerNameIndication_Online) { SSLClient cli(host, 443); auto res = cli.Get(path); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } TEST(SSLClientTest, ServerCertificateVerification1_Online) { Client cli("https://google.com"); auto res = cli.Get("/"); ASSERT_TRUE(res); - ASSERT_EQ(301, res->status); + ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(SSLClientTest, ServerCertificateVerification2_Online) { @@ -4920,7 +4925,7 @@ TEST(SSLClientTest, ServerCertificateVerification3_Online) { cli.set_ca_cert_path(CA_CERT_FILE); auto res = cli.Get("/"); ASSERT_TRUE(res); - ASSERT_EQ(301, res->status); + ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(SSLClientTest, ServerCertificateVerification4) { @@ -4948,7 +4953,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) { auto res = cli.Get("/test"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } TEST(SSLClientTest, ServerCertificateVerification5_Online) { @@ -4959,7 +4964,7 @@ TEST(SSLClientTest, ServerCertificateVerification5_Online) { cli.load_ca_cert_store(cert.data(), cert.size()); const auto res = cli.Get("/"); ASSERT_TRUE(res); - ASSERT_EQ(301, res->status); + ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(SSLClientTest, ServerCertificateVerification6_Online) { @@ -4991,7 +4996,7 @@ TEST(SSLClientTest, ServerCertificateVerification6_Online) { cli.load_ca_cert_store(cert, sizeof(cert)); const auto res = cli.Get("/"); ASSERT_TRUE(res); - ASSERT_EQ(301, res->status); + ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(SSLClientTest, WildcardHostNameMatch_Online) { @@ -5003,7 +5008,7 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) { auto res = cli.Get("/"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } #if 0 @@ -5016,7 +5021,7 @@ TEST(SSLClientTest, SetInterfaceWithINET6) { auto res = cli->Get("/get"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } #endif @@ -5063,7 +5068,7 @@ TEST(SSLClientServerTest, ClientCertPresent) { auto res = cli.Get("/test"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } #if !defined(_WIN32) || defined(OPENSSL_USE_APPLINK) @@ -5138,7 +5143,7 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { auto res = cli.Get("/test"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); X509_free(server_cert); EVP_PKEY_free(server_private_key); @@ -5193,7 +5198,7 @@ TEST(SSLClientServerTest, TrustDirOptional) { auto res = cli.Get("/test"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } TEST(SSLClientServerTest, SSLConnectTimeout) { @@ -5313,7 +5318,7 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { auto res = cli.Get("/test"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } // Disabled due to the out-of-memory problem on GitHub Actions Workflows @@ -5354,7 +5359,7 @@ TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { large_size_byte, "application/octet-stream"); // compare - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(large_size_byte, res->body.size()); EXPECT_EQ(0, std::memcmp(binary.data(), res->body.data(), large_size_byte)); } @@ -5393,13 +5398,13 @@ TEST(SendAPI, SimpleInterface_Online) { auto res = cli.send(req); ASSERT_TRUE(res); - EXPECT_EQ(301, res->status); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(ClientImplMethods, GetSocketTest) { httplib::Server svr; svr.Get("/", [&](const httplib::Request & /*req*/, httplib::Response &res) { - res.status = 200; + res.status = StatusCode::OK_200; }); auto thread = std::thread([&]() { svr.listen("127.0.0.1", 3333); }); @@ -5427,7 +5432,7 @@ TEST(ClientImplMethods, GetSocketTest) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); ASSERT_TRUE(cli.socket() != INVALID_SOCKET); } } @@ -5459,7 +5464,7 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { Client cli(HOST, PORT); auto res = cli.Get("/foo"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(content_size, res->body.length()); } #endif @@ -5470,12 +5475,12 @@ TEST(YahooRedirectTest2, SimpleInterface_Online) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(301, res->status); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); cli.set_follow_location(true); res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("https://www.yahoo.com/", res->location); } @@ -5484,12 +5489,12 @@ TEST(YahooRedirectTest3, SimpleInterface_Online) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(301, res->status); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); cli.set_follow_location(true); res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("https://www.yahoo.com/", res->location); } @@ -5503,17 +5508,17 @@ TEST(YahooRedirectTest3, NewResultInterface_Online) { ASSERT_FALSE(res == nullptr); ASSERT_TRUE(res != nullptr); EXPECT_EQ(Error::Success, res.error()); - EXPECT_EQ(301, res.value().status); - EXPECT_EQ(301, (*res).status); - EXPECT_EQ(301, res->status); + EXPECT_EQ(StatusCode::MovedPermanently_301, res.value().status); + EXPECT_EQ(StatusCode::MovedPermanently_301, (*res).status); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); cli.set_follow_location(true); res = cli.Get("/"); ASSERT_TRUE(res); EXPECT_EQ(Error::Success, res.error()); - EXPECT_EQ(200, res.value().status); - EXPECT_EQ(200, (*res).status); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res.value().status); + EXPECT_EQ(StatusCode::OK_200, (*res).status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("https://www.yahoo.com/", res->location); } @@ -5524,7 +5529,7 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) { cli.Get("/ajax/libs/jquery/3.5.1/jquery.js", {{"Accept-Encoding", "br"}}); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(287630U, res->body.size()); EXPECT_EQ("application/javascript; charset=utf-8", res->get_header_value("Content-Type")); @@ -5539,7 +5544,7 @@ TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) { "redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) { @@ -5552,7 +5557,7 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) { auto res = cli.Get("/httpbin/redirect-to", params, Headers{}); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) { @@ -5564,7 +5569,7 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) { auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{}); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpToHttpsRedirectTest, CertFile) { @@ -5601,7 +5606,7 @@ TEST(HttpToHttpsRedirectTest, CertFile) { auto res = cli.Get("/index"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } TEST(MultipartFormDataTest, LargeData) { @@ -5663,7 +5668,7 @@ TEST(MultipartFormDataTest, LargeData) { auto res = cli.Post("/post", items); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } } @@ -5807,7 +5812,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { { auto res = cli.Post("/post-none", {}, {}, {}); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } MultipartFormDataProviderItems providers; @@ -5816,7 +5821,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { auto res = cli.Post("/post-items", {}, items, providers); // empty providers ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } providers.push_back({"name3", @@ -5854,13 +5859,13 @@ TEST(MultipartFormDataTest, DataProviderItems) { { auto res = cli.Post("/post-providers", {}, {}, providers); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } { auto res = cli.Post("/post-both", {}, items, providers); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } } } @@ -5906,7 +5911,7 @@ TEST(MultipartFormDataTest, BadHeader) { auto res = cli.Post("/post", body, content_type.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(400, res->status); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); } TEST(MultipartFormDataTest, WithPreamble) { @@ -5949,7 +5954,7 @@ TEST(MultipartFormDataTest, WithPreamble) { auto res = cli.Post("/post", body, content_type.c_str()); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(MultipartFormDataTest, PostCustomBoundary) { @@ -6011,7 +6016,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { auto res = cli.Post("/post_customboundary", {}, items, "abc-abc"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } } @@ -6094,7 +6099,7 @@ TEST(MultipartFormDataTest, PutFormData) { auto res = cli.Put("/put", items); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } } @@ -6158,7 +6163,7 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { auto res = cli.Put("/put_customboundary", {}, items, "abc-abc_"); ASSERT_TRUE(res); - ASSERT_EQ(200, res->status); + ASSERT_EQ(StatusCode::OK_200, res->status); } } @@ -6359,7 +6364,7 @@ class UnixSocketTest : public ::testing::Test { ASSERT_TRUE(result) << "error: " << result.error(); const auto &resp = result.value(); - EXPECT_EQ(resp.status, 200); + EXPECT_EQ(resp.status, StatusCode::OK_200); EXPECT_EQ(resp.body, content_); } @@ -6546,7 +6551,7 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { auto res = cli.Get("/"); ASSERT_TRUE(res); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("val&key2=val2", res->body); } } diff --git a/test/test_proxy.cc b/test/test_proxy.cc index f44ec63c10..88a8ba963d 100644 --- a/test/test_proxy.cc +++ b/test/test_proxy.cc @@ -10,7 +10,7 @@ void ProxyTest(T& cli, bool basic) { cli.set_proxy("localhost", basic ? 3128 : 3129); auto res = cli.Get("/httpbin/get"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(407, res->status); + EXPECT_EQ(StatusCode::ProxyAuthenticationRequired_407, res->status); } TEST(ProxyTest, NoSSLBasic) { @@ -51,7 +51,7 @@ void RedirectProxyText(T& cli, const char *path, bool basic) { auto res = cli.Get(path); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(RedirectTest, HTTPBinNoSSLBasic) { @@ -108,7 +108,7 @@ void BaseAuthTestFromHTTPWatch(T& cli) { { auto res = cli.Get("/basic-auth/hello/world"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } { @@ -117,7 +117,7 @@ void BaseAuthTestFromHTTPWatch(T& cli) { {make_basic_authentication_header("hello", "world")}); ASSERT_TRUE(res != nullptr); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -125,21 +125,21 @@ void BaseAuthTestFromHTTPWatch(T& cli) { auto res = cli.Get("/basic-auth/hello/world"); ASSERT_TRUE(res != nullptr); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { cli.set_basic_auth("hello", "bad"); auto res = cli.Get("/basic-auth/hello/world"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } { cli.set_basic_auth("bad", "world"); auto res = cli.Get("/basic-auth/hello/world"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } } @@ -166,7 +166,7 @@ void DigestAuthTestFromHTTPWatch(T& cli) { { auto res = cli.Get("/digest-auth/auth/hello/world"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } { @@ -182,14 +182,14 @@ void DigestAuthTestFromHTTPWatch(T& cli) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res != nullptr); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } cli.set_digest_auth("hello", "bad"); for (auto path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(401, res->status); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } // NOTE: Until httpbin.org fixes issue #46, the following test is commented @@ -198,7 +198,7 @@ void DigestAuthTestFromHTTPWatch(T& cli) { // for (auto path : paths) { // auto res = cli.Get(path.c_str()); // ASSERT_TRUE(res != nullptr); - // EXPECT_EQ(401, res->status); + // EXPECT_EQ(StatusCode::Unauthorized_401, res->status); // } } } @@ -234,11 +234,11 @@ void KeepAliveTest(T& cli, bool basic) { { auto res = cli.Get("/httpbin/get"); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { auto res = cli.Get("/httpbin/redirect/2"); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } { @@ -252,7 +252,7 @@ void KeepAliveTest(T& cli, bool basic) { for (auto path: paths) { auto res = cli.Get(path.c_str()); EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } } @@ -260,7 +260,7 @@ void KeepAliveTest(T& cli, bool basic) { int count = 10; while (count--) { auto res = cli.Get("/httpbin/get"); - EXPECT_EQ(200, res->status); + EXPECT_EQ(StatusCode::OK_200, res->status); } } } From b4748a226cf8a2d8846d7c2c2319d1bb5c643623 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 21 Dec 2023 13:33:52 -0500 Subject: [PATCH 0142/1222] Fix #1738 --- httplib.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 43c8a3bf54..2f6df1c40c 100644 --- a/httplib.h +++ b/httplib.h @@ -3860,11 +3860,7 @@ inline bool read_content_without_length(Stream &strm, uint64_t r = 0; for (;;) { auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); - if (n < 0) { - return false; - } else if (n == 0) { - return true; - } + if (n <= 0) { return true; } if (!out(buf, static_cast(n), r, 0)) { return false; } r += static_cast(n); From cbca63f091ef1147ff57e90eb1ee5e558aa05d2c Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 21 Dec 2023 19:55:25 -0500 Subject: [PATCH 0143/1222] Release v0.14.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 2f6df1c40c..d29ff85291 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.14.2" +#define CPPHTTPLIB_VERSION "0.14.3" /* * Configuration From ad9f6423e2ff0a7795f64bbf91828b7e32be87cb Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 23 Dec 2023 11:45:08 -0500 Subject: [PATCH 0144/1222] Fix #1744 --- httplib.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index d29ff85291..528bbf3c0d 100644 --- a/httplib.h +++ b/httplib.h @@ -160,10 +160,6 @@ using ssize_t = long; #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 #endif -#ifndef strcasecmp -#define strcasecmp _stricmp -#endif // strcasecmp - using socket_t = SOCKET; #ifdef CPPHTTPLIB_USE_POLL #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) @@ -3925,8 +3921,8 @@ inline bool read_content_chunked(Stream &strm, T &x, } inline bool is_chunked_transfer_encoding(const Headers &headers) { - return !strcasecmp(get_header_value(headers, "Transfer-Encoding", 0, ""), - "chunked"); + return compare_case_ignore( + get_header_value(headers, "Transfer-Encoding", 0, ""), "chunked"); } template From 31cdcc3c3aa1f2d645ff36531bb9b499ce3b4dc9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 23 Dec 2023 21:37:34 -0500 Subject: [PATCH 0145/1222] Update README about MSYS2 and MinGW --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6be4fdeeb4..ec2546f79b 100644 --- a/README.md +++ b/README.md @@ -848,7 +848,7 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32 NOTE: cpp-httplib officially supports only the latest Visual Studio. It might work with former versions of Visual Studio, but I can no longer verify it. Pull requests are always welcome for the older versions of Visual Studio unless they break the C++11 conformance. -NOTE: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin on Windows are not supported. +NOTE: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin and MSYS2 including MinGW are neither supported nor tested. License ------- From 374d058de70ec1195e1fbc39713d0e00a5390d95 Mon Sep 17 00:00:00 2001 From: vmaffione <4920001+vmaffione@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:20:22 +0100 Subject: [PATCH 0146/1222] ThreadPool: optional limit for jobs queue (#1741) For very busy servers, the internal jobs queue where accepted sockets are enqueued can grow without limit. This is a problem for two reasons: - queueing too much work causes the server to respond with huge latency, resulting in repetead timeouts on the clients; it is definitely better to reject the connection early, so that the client receives the backpressure signal as soon as the queue is becoming too large - the jobs list can eventually cause an out of memory condition --- README.md | 17 ++++++++-- httplib.h | 18 +++++++--- test/test.cc | 93 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 118 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ec2546f79b..ac3763fbb5 100644 --- a/README.md +++ b/README.md @@ -433,6 +433,17 @@ If you want to set the thread count at runtime, there is no convenient way... Bu svr.new_task_queue = [] { return new ThreadPool(12); }; ``` +You can also provide an optional parameter to limit the maximum number +of pending requests, i.e. requests `accept()`ed by the listener but +still waiting to be serviced by worker threads. + +```cpp +svr.new_task_queue = [] { return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); }; +``` + +Default limit is 0 (unlimited). Once the limit is reached, the listener +will shutdown the client connection. + ### Override the default thread pool with yours You can supply your own thread pool implementation according to your need. @@ -444,8 +455,10 @@ public: pool_.start_with_thread_count(n); } - virtual void enqueue(std::function fn) override { - pool_.enqueue(fn); + virtual bool enqueue(std::function fn) override { + /* Return true if the task was actually enqueued, or false + * if the caller must drop the corresponding connection. */ + return pool_.enqueue(fn); } virtual void shutdown() override { diff --git a/httplib.h b/httplib.h index 528bbf3c0d..2bca8e1fb9 100644 --- a/httplib.h +++ b/httplib.h @@ -653,7 +653,7 @@ class TaskQueue { TaskQueue() = default; virtual ~TaskQueue() = default; - virtual void enqueue(std::function fn) = 0; + virtual bool enqueue(std::function fn) = 0; virtual void shutdown() = 0; virtual void on_idle() {} @@ -661,7 +661,8 @@ class TaskQueue { class ThreadPool : public TaskQueue { public: - explicit ThreadPool(size_t n) : shutdown_(false) { + explicit ThreadPool(size_t n, size_t mqr = 0) + : shutdown_(false), max_queued_requests_(mqr) { while (n) { threads_.emplace_back(worker(*this)); n--; @@ -671,13 +672,17 @@ class ThreadPool : public TaskQueue { ThreadPool(const ThreadPool &) = delete; ~ThreadPool() override = default; - void enqueue(std::function fn) override { + bool enqueue(std::function fn) override { { std::unique_lock lock(mutex_); + if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { + return false; + } jobs_.push_back(std::move(fn)); } cond_.notify_one(); + return true; } void shutdown() override { @@ -727,6 +732,7 @@ class ThreadPool : public TaskQueue { std::list> jobs_; bool shutdown_; + size_t max_queued_requests_ = 0; std::condition_variable cond_; std::mutex mutex_; @@ -6319,7 +6325,11 @@ inline bool Server::listen_internal() { #endif } - task_queue->enqueue([this, sock]() { process_and_close_socket(sock); }); + if (!task_queue->enqueue( + [this, sock]() { process_and_close_socket(sock); })) { + detail::shutdown_socket(sock); + detail::close_socket(sock); + } } task_queue->shutdown(); diff --git a/test/test.cc b/test/test.cc index 3de1fb9710..31deb23827 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6511,18 +6511,103 @@ TEST(SocketStream, is_writable_INET) { #endif // #ifndef _WIN32 TEST(TaskQueueTest, IncreaseAtomicInteger) { - static constexpr unsigned int number_of_task{1000000}; + static constexpr unsigned int number_of_tasks{1000000}; std::atomic_uint count{0}; std::unique_ptr task_queue{ new ThreadPool{CPPHTTPLIB_THREAD_POOL_COUNT}}; - for (unsigned int i = 0; i < number_of_task; ++i) { - task_queue->enqueue( + for (unsigned int i = 0; i < number_of_tasks; ++i) { + auto queued = task_queue->enqueue( [&count] { count.fetch_add(1, std::memory_order_relaxed); }); + EXPECT_TRUE(queued); + } + + EXPECT_NO_THROW(task_queue->shutdown()); + EXPECT_EQ(number_of_tasks, count.load()); +} + +TEST(TaskQueueTest, IncreaseAtomicIntegerWithQueueLimit) { + static constexpr unsigned int number_of_tasks{1000000}; + static constexpr unsigned int qlimit{2}; + unsigned int queued_count{0}; + std::atomic_uint count{0}; + std::unique_ptr task_queue{ + new ThreadPool{/*num_threads=*/1, qlimit}}; + + for (unsigned int i = 0; i < number_of_tasks; ++i) { + if (task_queue->enqueue( + [&count] { count.fetch_add(1, std::memory_order_relaxed); })) { + queued_count++; + } + } + + EXPECT_NO_THROW(task_queue->shutdown()); + EXPECT_EQ(queued_count, count.load()); + EXPECT_TRUE(queued_count <= number_of_tasks); + EXPECT_TRUE(queued_count >= qlimit); +} + +TEST(TaskQueueTest, MaxQueuedRequests) { + static constexpr unsigned int qlimit{3}; + std::unique_ptr task_queue{new ThreadPool{1, qlimit}}; + std::condition_variable sem_cv; + std::mutex sem_mtx; + int credits = 0; + bool queued; + + /* Fill up the queue with tasks that will block until we give them credits to + * complete. */ + for (unsigned int n = 0; n <= qlimit;) { + queued = task_queue->enqueue([&sem_mtx, &sem_cv, &credits] { + std::unique_lock lock(sem_mtx); + while (credits <= 0) { + sem_cv.wait(lock); + } + /* Consume the credit and signal the test code if they are all gone. */ + if (--credits == 0) { sem_cv.notify_one(); } + }); + + if (n < qlimit) { + /* The first qlimit enqueues must succeed. */ + EXPECT_TRUE(queued); + } else { + /* The last one will succeed only when the worker thread + * starts and dequeues the first blocking task. Although + * not necessary for the correctness of this test, we sleep for + * a short while to avoid busy waiting. */ + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + if (queued) { n++; } + } + + /* Further enqueues must fail since the queue is full. */ + for (auto i = 0; i < 4; i++) { + queued = task_queue->enqueue([] {}); + EXPECT_FALSE(queued); + } + + /* Give the credits to allow the previous tasks to complete. */ + { + std::unique_lock lock(sem_mtx); + credits += qlimit + 1; + } + sem_cv.notify_all(); + + /* Wait for all the credits to be consumed. */ + { + std::unique_lock lock(sem_mtx); + while (credits > 0) { + sem_cv.wait(lock); + } + } + + /* Check that we are able again to enqueue at least qlimit tasks. */ + for (unsigned int i = 0; i < qlimit; i++) { + queued = task_queue->enqueue([] {}); + EXPECT_TRUE(queued); } EXPECT_NO_THROW(task_queue->shutdown()); - EXPECT_EQ(number_of_task, count.load()); } TEST(RedirectTest, RedirectToUrlWithQueryParameters) { From eba980846be1711796106b3c0bb7100bce2394c7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 24 Dec 2023 08:20:58 -0500 Subject: [PATCH 0147/1222] Fix #1628 (OpenSSL 1.1.1 End of Life on September 11, 2023) (#1745) --- CMakeLists.txt | 2 +- README.md | 2 +- example/Makefile | 3 +-- httplib.h | 6 ++---- meson.build | 2 +- test/Makefile | 3 +-- 6 files changed, 7 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 927cb63864..20b85c2c80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF) # Change as needed to set an OpenSSL minimum version. # This is used in the installed Cmake config file. -set(_HTTPLIB_OPENSSL_MIN_VER "1.1.1") +set(_HTTPLIB_OPENSSL_MIN_VER "3.0.0") # Allow for a build to require OpenSSL to pass, instead of just being optional option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF) diff --git a/README.md b/README.md index ac3763fbb5..fdb0e3b50a 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ SSL Support SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. -NOTE: cpp-httplib currently supports only version 1.1.1 and 3.0. +NOTE: cpp-httplib currently supports only version 3.0 or later. Please see [this page](https://www.openssl.org/policies/releasestrat.html) to get more information. NOTE for macOS: cpp-httplib now can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`. diff --git a/example/Makefile b/example/Makefile index 76366db235..64a35885f2 100644 --- a/example/Makefile +++ b/example/Makefile @@ -4,8 +4,7 @@ CXXFLAGS = -O2 -std=c++11 -I.. -Wall -Wextra -pthread PREFIX = /usr/local #PREFIX = $(shell brew --prefix) -OPENSSL_DIR = $(PREFIX)/opt/openssl@1.1 -#OPENSSL_DIR = $(PREFIX)/opt/openssl@3 +OPENSSL_DIR = $(PREFIX)/opt/openssl@3 OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto ifneq ($(OS), Windows_NT) diff --git a/httplib.h b/httplib.h index 2bca8e1fb9..241e5260f9 100644 --- a/httplib.h +++ b/httplib.h @@ -264,10 +264,8 @@ using socket_t = int; #include #include -#if OPENSSL_VERSION_NUMBER < 0x1010100fL -#error Sorry, OpenSSL versions prior to 1.1.1 are not supported -#elif OPENSSL_VERSION_NUMBER < 0x30000000L -#define SSL_get1_peer_certificate SSL_get_peer_certificate +#if OPENSSL_VERSION_NUMBER < 0x30000000L +#error Sorry, OpenSSL versions prior to 3.0.0 are not supported #endif #endif diff --git a/meson.build b/meson.build index 16362ad29a..e82ae84514 100644 --- a/meson.build +++ b/meson.build @@ -30,7 +30,7 @@ endif deps = [dependency('threads')] args = [] -openssl_dep = dependency('openssl', version: '>=1.1.1', required: get_option('cpp-httplib_openssl')) +openssl_dep = dependency('openssl', version: '>=3.0.0', required: get_option('cpp-httplib_openssl')) if openssl_dep.found() deps += openssl_dep args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' diff --git a/test/Makefile b/test/Makefile index 9feae74c65..3b72d209fa 100644 --- a/test/Makefile +++ b/test/Makefile @@ -4,8 +4,7 @@ CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow # PREFIX = /usr/local #PREFIX = $(shell brew --prefix) -OPENSSL_DIR = $(PREFIX)/opt/openssl@1.1 -#OPENSSL_DIR = $(PREFIX)/opt/openssl@3 +OPENSSL_DIR = $(PREFIX)/opt/openssl@3 OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto ifneq ($(OS), Windows_NT) From b63d50671d82179e8dc67c432c8b16e1d2290224 Mon Sep 17 00:00:00 2001 From: TheOnlyJoey Date: Sat, 30 Dec 2023 08:37:58 -0800 Subject: [PATCH 0148/1222] Fixes Windows std::max macro problems (#1750) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 241e5260f9..4cbe402aec 100644 --- a/httplib.h +++ b/httplib.h @@ -2557,7 +2557,7 @@ inline std::string trim_double_quotes_copy(const std::string &s) { inline void split(const char *b, const char *e, char d, std::function fn) { - return split(b, e, d, std::numeric_limits::max(), fn); + return split(b, e, d, (std::numeric_limits::max)(), fn); } inline void split(const char *b, const char *e, char d, size_t m, From 55e99c40300757fcf0a9f5fdc1ffebaba18db9ca Mon Sep 17 00:00:00 2001 From: Adam Gajda <80600850+adgajda@users.noreply.github.com> Date: Mon, 1 Jan 2024 00:43:31 +0100 Subject: [PATCH 0149/1222] Fix -Wold-style-cast warning (#1751) --- httplib.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index 4cbe402aec..7f96f6b58a 100644 --- a/httplib.h +++ b/httplib.h @@ -141,11 +141,11 @@ using ssize_t = long; #endif // _MSC_VER #ifndef S_ISREG -#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) +#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) #endif // S_ISREG #ifndef S_ISDIR -#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) +#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) #endif // S_ISDIR #ifndef NOMINMAX @@ -2705,7 +2705,9 @@ inline bool mmap::is_open() const { return addr_ != nullptr; } inline size_t mmap::size() const { return size_; } -inline const char *mmap::data() const { return (const char *)addr_; } +inline const char *mmap::data() const { + return static_cast(addr_); +} inline void mmap::close() { #if defined(_WIN32) @@ -8693,11 +8695,11 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return true; }, [&](SSL *ssl2) { - // NOTE: With -Wold-style-cast, this can produce a warning, since - // SSL_set_tlsext_host_name is a macro (in OpenSSL), which contains - // an old style cast. Short of doing compiler specific pragma's - // here, we can't get rid of this warning. :'( - SSL_set_tlsext_host_name(ssl2, host_.c_str()); + // NOTE: Direct call instead of using the OpenSSL macro to suppress + // -Wold-style-cast warning + // SSL_set_tlsext_host_name(ssl2, host_.c_str()); + SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, + static_cast(const_cast(host_.c_str()))); return true; }); From 65218ce222ecb052cb249f0b22c67f73e8817031 Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Mon, 1 Jan 2024 00:59:43 +0100 Subject: [PATCH 0150/1222] added missing include of exception (#1752) --- httplib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/httplib.h b/httplib.h index 7f96f6b58a..e6f6fa8340 100644 --- a/httplib.h +++ b/httplib.h @@ -210,6 +210,7 @@ using socket_t = int; #include #include #include +#include #include #include #include From d948e38820ddc60650655e8cc65797c37d1984ac Mon Sep 17 00:00:00 2001 From: KTGH Date: Thu, 4 Jan 2024 18:20:37 -0500 Subject: [PATCH 0151/1222] Minor cmake fix & cleanup (#1754) * Reorder cmake docs a bit Just wanted to group the more related build options together. Also removed a pointless reference to the old reasoning for the required min ver since it's 3.14 now anyways. * Fix outdated cmake comment We don't use Git to find the version string anymore. Just updated to match what it's actually used for now. * Group options and build-tree vars in Cmake Doesn't really change anything, I just wanted to clean these up a bit. * Fix how we set HTTPLIB_IS_USING_XXX vars in Cmake Prevents us acidentally using libs when the user didn't want them actually used. This could happen if they set the option to OFF but their own project itself is using the lib, thus we'd find and use it anyways. Ref #1602 to see an example of this already happening before. This is merely apply that kind of fix to all 3 of our deps, instead of just OpenSSL. * Minor formatting/comment change to Cmake Pointless, but these things were bothering me.. --- CMakeLists.txt | 67 ++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 20b85c2c80..73de511899 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,11 +3,11 @@ * BUILD_SHARED_LIBS (default off) builds as a shared library (if HTTPLIB_COMPILE is ON) * HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on) * HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on) + * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) * HTTPLIB_REQUIRE_OPENSSL (default off) * HTTPLIB_REQUIRE_ZLIB (default off) - * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) - * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) * HTTPLIB_REQUIRE_BROTLI (default off) + * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) * HTTPLIB_COMPILE (default off) * HTTPLIB_INSTALL (default on) * HTTPLIB_TEST (default off) @@ -59,7 +59,6 @@ ------------------------------------------------------------------------------- - FindPython3 requires Cmake v3.12 ARCH_INDEPENDENT option of write_basic_package_version_file() requires Cmake v3.14 ]] cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR) @@ -69,20 +68,19 @@ cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR) # This is so the maintainer doesn't actually need to update this manually. file(STRINGS httplib.h _raw_version_string REGEX "CPPHTTPLIB_VERSION \"([0-9]+\\.[0-9]+\\.[0-9]+)\"") -# Needed since git tags have "v" prefixing them. -# Also used if the fallback to user agent string is being used. +# Extracts just the version string itself from the whole string contained in _raw_version_string +# since _raw_version_string would contain the entire line of code where it found the version string string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}") project(httplib VERSION ${_httplib_version} LANGUAGES CXX) -# Lets you disable C++ exception during CMake configure time. -# The value is used in the install CMake config file. -option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF) - # Change as needed to set an OpenSSL minimum version. # This is used in the installed Cmake config file. set(_HTTPLIB_OPENSSL_MIN_VER "3.0.0") +# Lets you disable C++ exception during CMake configure time. +# The value is used in the install CMake config file. +option(HTTPLIB_NO_EXCEPTIONS "Disable the use of C++ exceptions" OFF) # Allow for a build to require OpenSSL to pass, instead of just being optional option(HTTPLIB_REQUIRE_OPENSSL "Requires OpenSSL to be found & linked, or fails build." OFF) option(HTTPLIB_REQUIRE_ZLIB "Requires ZLIB to be found & linked, or fails build." OFF) @@ -94,10 +92,6 @@ option(HTTPLIB_USE_ZLIB_IF_AVAILABLE "Uses ZLIB (if available) to enable Zlib co option(HTTPLIB_COMPILE "If ON, uses a Python script to split the header into a compilable header & source file (requires Python v3)." OFF) # Lets you disable the installation (useful when fetched from another CMake project) option(HTTPLIB_INSTALL "Enables the installation target" ON) -# Just setting this variable here for people building in-tree -if(HTTPLIB_COMPILE) - set(HTTPLIB_IS_COMPILED TRUE) -endif() option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) @@ -110,45 +104,48 @@ if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() +# Set some variables that are used in-tree and while building based on our options +set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) +set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}) + # Threads needed for on some systems, and for on Linux -set(THREADS_PREFER_PTHREAD_FLAG true) +set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) # Since Cmake v3.11, Crypto & SSL became optional when not specified as COMPONENTS. if(HTTPLIB_REQUIRE_OPENSSL) find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL REQUIRED) + set(HTTPLIB_IS_USING_OPENSSL TRUE) elseif(HTTPLIB_USE_OPENSSL_IF_AVAILABLE) find_package(OpenSSL ${_HTTPLIB_OPENSSL_MIN_VER} COMPONENTS Crypto SSL QUIET) -endif() -# Just setting this variable here for people building in-tree -if(OPENSSL_FOUND AND NOT DEFINED HTTPLIB_IS_USING_OPENSSL) - set(HTTPLIB_IS_USING_OPENSSL TRUE) + # Avoid a rare circumstance of not finding all components but the end-user did their + # own call for OpenSSL, which might trick us into thinking we'd otherwise have what we wanted + if (TARGET OpenSSL::SSL AND TARGET OpenSSL::Crypto) + set(HTTPLIB_IS_USING_OPENSSL ${OPENSSL_FOUND}) + else() + set(HTTPLIB_IS_USING_OPENSSL FALSE) + endif() endif() if(HTTPLIB_REQUIRE_ZLIB) find_package(ZLIB REQUIRED) + set(HTTPLIB_IS_USING_ZLIB TRUE) elseif(HTTPLIB_USE_ZLIB_IF_AVAILABLE) find_package(ZLIB QUIET) -endif() -# Just setting this variable here for people building in-tree -# FindZLIB doesn't have a ZLIB_FOUND variable, so check the target. -if(TARGET ZLIB::ZLIB) - set(HTTPLIB_IS_USING_ZLIB TRUE) + # FindZLIB doesn't have a ZLIB_FOUND variable, so check the target. + if(TARGET ZLIB::ZLIB) + set(HTTPLIB_IS_USING_ZLIB TRUE) + endif() endif() # Adds our cmake folder to the search path for find_package +# This is so we can use our custom FindBrotli.cmake list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") if(HTTPLIB_REQUIRE_BROTLI) find_package(Brotli COMPONENTS encoder decoder common REQUIRED) + set(HTTPLIB_IS_USING_BROTLI TRUE) elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE) find_package(Brotli COMPONENTS encoder decoder common QUIET) -endif() -# Just setting this variable here for people building in-tree -if(Brotli_FOUND) - set(HTTPLIB_IS_USING_BROTLI TRUE) -endif() - -if(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) - set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN TRUE) + set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND}) endif() # Used for default, common dirs that the end-user can change (if needed) @@ -204,9 +201,7 @@ endif() add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) # Require C++11 -target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} - cxx_std_11 -) +target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11) target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC} $ @@ -273,9 +268,7 @@ if(HTTPLIB_INSTALL) # Creates the export httplibTargets.cmake # This is strictly what holds compilation requirements # and linkage information (doesn't find deps though). - install(TARGETS ${PROJECT_NAME} - EXPORT httplibTargets - ) + install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets) install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE) From af2928d3162485337cb0372f40d6c82cdd25f982 Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Mon, 15 Jan 2024 08:27:31 -0500 Subject: [PATCH 0152/1222] Fix select() return code for fd >= 1024 (#1757) --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index e6f6fa8340..1bb4677bba 100644 --- a/httplib.h +++ b/httplib.h @@ -2793,7 +2793,7 @@ inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else #ifndef _WIN32 - if (sock >= FD_SETSIZE) { return 1; } + if (sock >= FD_SETSIZE) { return -1; } #endif fd_set fds; @@ -2821,7 +2821,7 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else #ifndef _WIN32 - if (sock >= FD_SETSIZE) { return 1; } + if (sock >= FD_SETSIZE) { return -1; } #endif fd_set fds; From 449801990fd6629ba2a51f7a82383306d707d53e Mon Sep 17 00:00:00 2001 From: Ilya Andreev Date: Mon, 15 Jan 2024 16:57:22 +0300 Subject: [PATCH 0153/1222] Add a getter for a bearer token from a request (#1755) * Add a getter for a bearer token from a request * Replace a method for bearer token getter with a free function --- httplib.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/httplib.h b/httplib.h index 1bb4677bba..d04ac77740 100644 --- a/httplib.h +++ b/httplib.h @@ -745,6 +745,8 @@ void default_socket_options(socket_t sock); const char *status_message(int status); +std::string get_bearer_token_auth(const Request &req); + namespace detail { class MatcherBase { @@ -1943,6 +1945,15 @@ inline const char *status_message(int status) { } } +inline std::string get_bearer_token_auth(const Request &req) { + if (req.has_header("Authorization")) { + static std::string BearerHeaderPrefix = "Bearer "; + return req.get_header_value("Authorization") + .substr(BearerHeaderPrefix.length()); + } + return ""; +} + template inline Server & Server::set_read_timeout(const std::chrono::duration &duration) { From 44b3fe6277398f424f1844295b7ae46ba5a1a35f Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 27 Jan 2024 21:53:19 +0900 Subject: [PATCH 0154/1222] Support move semantics for Response::set_content() (#1764) --- httplib.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/httplib.h b/httplib.h index d04ac77740..0197b2fa88 100644 --- a/httplib.h +++ b/httplib.h @@ -596,6 +596,7 @@ struct Response { void set_redirect(const std::string &url, int status = StatusCode::Found_302); void set_content(const char *s, size_t n, const std::string &content_type); void set_content(const std::string &s, const std::string &content_type); + void set_content(std::string &&s, const std::string &content_type); void set_content_provider( size_t length, const std::string &content_type, ContentProvider provider, @@ -5350,6 +5351,15 @@ inline void Response::set_content(const std::string &s, set_content(s.data(), s.size(), content_type); } +inline void Response::set_content(std::string &&s, + const std::string &content_type) { + body = std::move(s); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + inline void Response::set_content_provider( size_t in_length, const std::string &content_type, ContentProvider provider, ContentProviderResourceReleaser resource_releaser) { From 4ef9ed80cde4074b74219a8284dd86ee01f5a687 Mon Sep 17 00:00:00 2001 From: Wander Nauta Date: Sat, 27 Jan 2024 14:22:00 +0100 Subject: [PATCH 0155/1222] Treat paths with embedded NUL bytes as invalid (#1765) Fixes #1763. --- httplib.h | 1 + test/test.cc | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/httplib.h b/httplib.h index 0197b2fa88..884db99432 100644 --- a/httplib.h +++ b/httplib.h @@ -2413,6 +2413,7 @@ inline bool is_valid_path(const std::string &path) { // Read component auto beg = i; while (i < path.size() && path[i] != '/') { + if (path[i] == '\0') { return false; } i++; } diff --git a/test/test.cc b/test/test.cc index 31deb23827..1d236eee8f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -71,6 +71,15 @@ TEST(DecodeURLTest, PercentCharacter) { R"(descrip=Gastos áéíóúñÑ 6)"); } +TEST(DecodeURLTest, PercentCharacterNUL) { + string expected; + expected.push_back('x'); + expected.push_back('\0'); + expected.push_back('x'); + + EXPECT_EQ(detail::decode_url("x%00x", false), expected); +} + TEST(EncodeQueryParamTest, ParseUnescapedChararactersTest) { string unescapedCharacters = "-_.!~*'()"; @@ -2482,6 +2491,12 @@ TEST_F(ServerTest, GetMethodInvalidMountPath) { EXPECT_EQ(StatusCode::NotFound_404, res->status); } +TEST_F(ServerTest, GetMethodEmbeddedNUL) { + auto res = cli_.Get("/mount/dir/test.html%00.js"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::NotFound_404, res->status); +} + TEST_F(ServerTest, GetMethodOutOfBaseDirMount) { auto res = cli_.Get("/mount/../www2/dir/test.html"); ASSERT_TRUE(res); From 2ce7c22218348d9a265319b675b0ab44be596a95 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 27 Jan 2024 12:55:59 -0500 Subject: [PATCH 0156/1222] Fix #1747 --- httplib.h | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/httplib.h b/httplib.h index 884db99432..3297c51252 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.14.3" +#define CPPHTTPLIB_VERSION "0.15.0" /* * Configuration @@ -4637,28 +4637,31 @@ inline std::string to_lower(const char *beg, const char *end) { return out; } -inline std::string make_multipart_data_boundary() { +inline std::string random_string(size_t length) { static const char data[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // std::random_device might actually be deterministic on some // platforms, but due to lack of support in the c++ standard library, // doing better requires either some ugly hacks or breaking portability. - std::random_device seed_gen; + static std::random_device seed_gen; // Request 128 bits of entropy for initialization - std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; - std::mt19937 engine(seed_sequence); + static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; - std::string result = "--cpp-httplib-multipart-data-"; + static std::mt19937 engine(seed_sequence); - for (auto i = 0; i < 16; i++) { + std::string result; + for (size_t i = 0; i < length; i++) { result += data[engine() % (sizeof(data) - 1)]; } - return result; } +inline std::string make_multipart_data_boundary() { + return "--cpp-httplib-multipart-data-" + detail::random_string(16); +} + inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { auto valid = true; for (size_t i = 0; i < boundary.size(); i++) { @@ -5133,20 +5136,6 @@ inline bool parse_www_authenticate(const Response &res, return false; } -// https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c/440240#answer-440240 -inline std::string random_string(size_t length) { - auto randchar = []() -> char { - const char charset[] = "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - const size_t max_index = (sizeof(charset) - 1); - return charset[static_cast(std::rand()) % max_index]; - }; - std::string str(length, 0); - std::generate_n(str.begin(), length, randchar); - return str; -} - class ContentProviderAdapter { public: explicit ContentProviderAdapter( From 420c9759c62addec424502e7ec9a41e493d079b1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 27 Jan 2024 16:13:54 -0500 Subject: [PATCH 0157/1222] Fix #1694 --- httplib.h | 77 +++++++++++++++++++++++++--------------------------- test/test.cc | 18 +++++++++++- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/httplib.h b/httplib.h index 3297c51252..ec9809cc38 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.15.0" +#define CPPHTTPLIB_VERSION "0.14.3" /* * Configuration @@ -141,11 +141,11 @@ using ssize_t = long; #endif // _MSC_VER #ifndef S_ISREG -#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) +#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) #endif // S_ISREG #ifndef S_ISDIR -#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) +#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) #endif // S_ISDIR #ifndef NOMINMAX @@ -4647,7 +4647,8 @@ inline std::string random_string(size_t length) { static std::random_device seed_gen; // Request 128 bits of entropy for initialization - static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; + static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), + seed_gen()}; static std::mt19937 engine(seed_sequence); @@ -4720,32 +4721,41 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, } inline std::pair -get_range_offset_and_length(const Request &req, size_t content_length, - size_t index) { - auto r = req.ranges[index]; - - if (r.first == -1 && r.second == -1) { +get_range_offset_and_length(Range range, size_t content_length) { + if (range.first == -1 && range.second == -1) { return std::make_pair(0, content_length); } auto slen = static_cast(content_length); - if (r.first == -1) { - r.first = (std::max)(static_cast(0), slen - r.second); - r.second = slen - 1; + if (range.first == -1) { + range.first = (std::max)(static_cast(0), slen - range.second); + range.second = slen - 1; } - if (r.second == -1) { r.second = slen - 1; } - return std::make_pair(r.first, static_cast(r.second - r.first) + 1); + if (range.second == -1) { range.second = slen - 1; } + return std::make_pair(range.first, + static_cast(range.second - range.first) + 1); +} + +inline std::pair +get_range_offset_and_length(const Request &req, size_t content_length, + size_t index) { + return get_range_offset_and_length(req.ranges[index], content_length); } inline std::string make_content_range_header_field(const std::pair &range, size_t content_length) { + + auto ret = get_range_offset_and_length(range, content_length); + auto st = ret.first; + auto ed = (std::min)(st + ret.second - 1, content_length - 1); + std::string field = "bytes "; - if (range.first != -1) { field += std::to_string(range.first); } + field += std::to_string(st); field += "-"; - if (range.second != -1) { field += std::to_string(range.second); } + field += std::to_string(ed); field += "/"; field += std::to_string(content_length); return field; @@ -4773,9 +4783,9 @@ bool process_multipart_ranges_data(const Request &req, Response &res, ctoken("\r\n"); ctoken("\r\n"); - auto offsets = get_range_offset_and_length(req, res.content_length_, i); - auto offset = offsets.first; - auto length = offsets.second; + auto ret = get_range_offset_and_length(req, res.content_length_, i); + auto offset = ret.first; + auto length = ret.second; if (!content(offset, length)) { return false; } ctoken("\r\n"); } @@ -4838,18 +4848,6 @@ inline bool write_multipart_ranges_data(Stream &strm, const Request &req, }); } -inline std::pair -get_range_offset_and_length(const Request &req, const Response &res, - size_t index) { - auto r = req.ranges[index]; - - if (r.second == -1) { - r.second = static_cast(res.content_length_) - 1; - } - - return std::make_pair(r.first, r.second - r.first + 1); -} - inline bool expect_content(const Request &req) { if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || req.method == "PRI" || req.method == "DELETE") { @@ -6045,10 +6043,10 @@ Server::write_content_with_provider(Stream &strm, const Request &req, return detail::write_content(strm, res.content_provider_, 0, res.content_length_, is_shutting_down); } else if (req.ranges.size() == 1) { - auto offsets = + auto ret = detail::get_range_offset_and_length(req, res.content_length_, 0); - auto offset = offsets.first; - auto length = offsets.second; + auto offset = ret.first; + auto length = ret.second; return detail::write_content(strm, res.content_provider_, offset, length, is_shutting_down); } else { @@ -6465,9 +6463,9 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (req.ranges.empty()) { length = res.content_length_; } else if (req.ranges.size() == 1) { - auto offsets = + auto ret = detail::get_range_offset_and_length(req, res.content_length_, 0); - length = offsets.second; + length = ret.second; auto content_range = detail::make_content_range_header_field( req.ranges[0], res.content_length_); @@ -6497,10 +6495,9 @@ inline void Server::apply_ranges(const Request &req, Response &res, req.ranges[0], res.body.size()); res.set_header("Content-Range", content_range); - auto offsets = - detail::get_range_offset_and_length(req, res.body.size(), 0); - auto offset = offsets.first; - auto length = offsets.second; + auto ret = detail::get_range_offset_and_length(req, res.body.size(), 0); + auto offset = ret.first; + auto length = ret.second; if (offset < res.body.size()) { res.body = res.body.substr(offset, length); diff --git a/test/test.cc b/test/test.cc index 1d236eee8f..e33ab416dc 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2540,6 +2540,7 @@ TEST_F(ServerTest, StaticFileRange) { EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 2-3/5", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("cd"), res->body); } @@ -2553,7 +2554,7 @@ TEST_F(ServerTest, StaticFileRanges) { .find( "multipart/byteranges; boundary=--cpp-httplib-multipart-data-") == 0); - EXPECT_EQ("265", res->get_header_value("Content-Length")); + EXPECT_EQ("266", res->get_header_value("Content-Length")); } TEST_F(ServerTest, StaticFileRangeHead) { @@ -2563,6 +2564,7 @@ TEST_F(ServerTest, StaticFileRangeHead) { EXPECT_EQ("text/abcde", res->get_header_value("Content-Type")); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 2-3/5", res->get_header_value("Content-Range")); } TEST_F(ServerTest, StaticFileRangeBigFile) { @@ -2572,6 +2574,8 @@ TEST_F(ServerTest, StaticFileRangeBigFile) { EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("5", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 1048571-1048575/1048576", + res->get_header_value("Content-Range")); EXPECT_EQ("LAST\n", res->body); } @@ -2582,6 +2586,7 @@ TEST_F(ServerTest, StaticFileRangeBigFile2) { EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("4097", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 1-4097/1048576", res->get_header_value("Content-Range")); } TEST_F(ServerTest, StaticFileBigFile) { @@ -2908,6 +2913,8 @@ TEST_F(ServerTest, GetStreamed2) { ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("2", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 2-3/6", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("ab"), res->body); } @@ -2925,6 +2932,7 @@ TEST_F(ServerTest, GetStreamedWithRange1) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 3-5/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("def"), res->body); } @@ -2934,6 +2942,7 @@ TEST_F(ServerTest, GetStreamedWithRange2) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 1-6/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("bcdefg"), res->body); } @@ -2943,6 +2952,7 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 4-6/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("efg"), res->body); } @@ -2952,6 +2962,7 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("7", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 0-6/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("abcdefg"), res->body); } @@ -2968,6 +2979,7 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) { cli_.Get("/with-range", {{"Range", "bytes=0-9223372036854775807"}}); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("7", res->get_header_value("Content-Length")); + EXPECT_EQ("bytes 0-6/7", res->get_header_value("Content-Range")); EXPECT_EQ(true, res->has_header("Content-Range")); EXPECT_EQ(std::string("abcdefg"), res->body); } @@ -3029,6 +3041,7 @@ TEST_F(ServerTest, GetWithRange1) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 3-5/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("def"), res->body); } @@ -3038,6 +3051,7 @@ TEST_F(ServerTest, GetWithRange2) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 1-6/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("bcdefg"), res->body); } @@ -3047,6 +3061,7 @@ TEST_F(ServerTest, GetWithRange3) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("1", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 0-0/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("a"), res->body); } @@ -3056,6 +3071,7 @@ TEST_F(ServerTest, GetWithRange4) { EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("2", res->get_header_value("Content-Length")); EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 5-6/7", res->get_header_value("Content-Range")); EXPECT_EQ(std::string("fg"), res->body); } From 530d6ee09816d7d37032b03e122efca8bf9448e3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 27 Jan 2024 17:39:58 -0500 Subject: [PATCH 0158/1222] Release v0.15.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ec9809cc38..05c423878f 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.14.3" +#define CPPHTTPLIB_VERSION "0.15.0" /* * Configuration From 5f0f73fad99b32eabd1e3d60deb8b6be5bc04098 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 27 Jan 2024 19:07:52 -0500 Subject: [PATCH 0159/1222] Reduce duplicate computation for ranges --- httplib.h | 62 ++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/httplib.h b/httplib.h index 05c423878f..94f6e9534f 100644 --- a/httplib.h +++ b/httplib.h @@ -4738,19 +4738,11 @@ get_range_offset_and_length(Range range, size_t content_length) { static_cast(range.second - range.first) + 1); } -inline std::pair -get_range_offset_and_length(const Request &req, size_t content_length, - size_t index) { - return get_range_offset_and_length(req.ranges[index], content_length); -} - -inline std::string -make_content_range_header_field(const std::pair &range, - size_t content_length) { +inline std::string make_content_range_header_field( + const std::pair &offset_and_length, size_t content_length) { - auto ret = get_range_offset_and_length(range, content_length); - auto st = ret.first; - auto ed = (std::min)(st + ret.second - 1, content_length - 1); + auto st = offset_and_length.first; + auto ed = (std::min)(st + offset_and_length.second - 1, content_length - 1); std::string field = "bytes "; field += std::to_string(st); @@ -4777,16 +4769,18 @@ bool process_multipart_ranges_data(const Request &req, Response &res, ctoken("\r\n"); } + auto offset_and_length = + get_range_offset_and_length(req.ranges[i], res.content_length_); + ctoken("Content-Range: "); - const auto &range = req.ranges[i]; - stoken(make_content_range_header_field(range, res.content_length_)); + stoken(make_content_range_header_field(offset_and_length, + res.content_length_)); ctoken("\r\n"); ctoken("\r\n"); - auto ret = get_range_offset_and_length(req, res.content_length_, i); - auto offset = ret.first; - auto length = ret.second; - if (!content(offset, length)) { return false; } + if (!content(offset_and_length.first, offset_and_length.second)) { + return false; + } ctoken("\r\n"); } @@ -6043,12 +6037,12 @@ Server::write_content_with_provider(Stream &strm, const Request &req, return detail::write_content(strm, res.content_provider_, 0, res.content_length_, is_shutting_down); } else if (req.ranges.size() == 1) { - auto ret = - detail::get_range_offset_and_length(req, res.content_length_, 0); - auto offset = ret.first; - auto length = ret.second; - return detail::write_content(strm, res.content_provider_, offset, length, - is_shutting_down); + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + return detail::write_content(strm, res.content_provider_, + offset_and_length.first, + offset_and_length.second, is_shutting_down); } else { return detail::write_multipart_ranges_data( strm, req, res, boundary, content_type, is_shutting_down); @@ -6463,12 +6457,13 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (req.ranges.empty()) { length = res.content_length_; } else if (req.ranges.size() == 1) { - auto ret = - detail::get_range_offset_and_length(req, res.content_length_, 0); - length = ret.second; + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + length = offset_and_length.second; auto content_range = detail::make_content_range_header_field( - req.ranges[0], res.content_length_); + offset_and_length, res.content_length_); res.set_header("Content-Range", content_range); } else { length = detail::get_multipart_ranges_data_length(req, res, boundary, @@ -6491,14 +6486,15 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (req.ranges.empty()) { ; } else if (req.ranges.size() == 1) { + auto offset_and_length = + detail::get_range_offset_and_length(req.ranges[0], res.body.size()); + auto offset = offset_and_length.first; + auto length = offset_and_length.second; + auto content_range = detail::make_content_range_header_field( - req.ranges[0], res.body.size()); + offset_and_length, res.body.size()); res.set_header("Content-Range", content_range); - auto ret = detail::get_range_offset_and_length(req, res.body.size(), 0); - auto offset = ret.first; - auto length = ret.second; - if (offset < res.body.size()) { res.body = res.body.substr(offset, length); } else { From fceada9ef42ce1af64a1d5941d8009dd0520901a Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 28 Jan 2024 08:13:19 -0500 Subject: [PATCH 0160/1222] Changed to return 416 for a request with an invalid range --- httplib.h | 141 ++++++++++++++++++++++++++++----------------------- test/test.cc | 29 ++++++----- 2 files changed, 93 insertions(+), 77 deletions(-) diff --git a/httplib.h b/httplib.h index 94f6e9534f..d659744930 100644 --- a/httplib.h +++ b/httplib.h @@ -4720,29 +4720,47 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, return body; } -inline std::pair -get_range_offset_and_length(Range range, size_t content_length) { - if (range.first == -1 && range.second == -1) { - return std::make_pair(0, content_length); - } +inline bool normalize_ranges(Request &req, Response &res) { + ssize_t len = static_cast(res.content_length_ ? res.content_length_ + : res.body.size()); + + if (!req.ranges.empty()) { + for (auto &r : req.ranges) { + auto &st = r.first; + auto &ed = r.second; + + if (st == -1 && ed == -1) { + st = 0; + ed = len; + } + + if (st == -1) { + st = len - ed; + ed = len - 1; + } - auto slen = static_cast(content_length); + if (ed == -1) { ed = len - 1; } - if (range.first == -1) { - range.first = (std::max)(static_cast(0), slen - range.second); - range.second = slen - 1; + if (!(0 <= st && st <= ed && ed <= len - 1)) { return false; } + } } + return true; +} - if (range.second == -1) { range.second = slen - 1; } - return std::make_pair(range.first, - static_cast(range.second - range.first) + 1); +inline std::pair +get_range_offset_and_length(Range r, size_t content_length) { + assert(r.first != -1 && r.second != -1); + assert(0 <= r.first && r.first < static_cast(content_length)); + assert(r.first <= r.second && + r.second < static_cast(content_length)); + + return std::make_pair(r.first, static_cast(r.second - r.first) + 1); } inline std::string make_content_range_header_field( const std::pair &offset_and_length, size_t content_length) { - auto st = offset_and_length.first; - auto ed = (std::min)(st + offset_and_length.second - 1, content_length - 1); + auto ed = st + offset_and_length.second - 1; std::string field = "bytes "; field += std::to_string(st); @@ -4754,11 +4772,11 @@ inline std::string make_content_range_header_field( } template -bool process_multipart_ranges_data(const Request &req, Response &res, +bool process_multipart_ranges_data(const Request &req, const std::string &boundary, const std::string &content_type, - SToken stoken, CToken ctoken, - Content content) { + size_t content_length, SToken stoken, + CToken ctoken, Content content) { for (size_t i = 0; i < req.ranges.size(); i++) { ctoken("--"); stoken(boundary); @@ -4770,11 +4788,10 @@ bool process_multipart_ranges_data(const Request &req, Response &res, } auto offset_and_length = - get_range_offset_and_length(req.ranges[i], res.content_length_); + get_range_offset_and_length(req.ranges[i], content_length); ctoken("Content-Range: "); - stoken(make_content_range_header_field(offset_and_length, - res.content_length_)); + stoken(make_content_range_header_field(offset_and_length, content_length)); ctoken("\r\n"); ctoken("\r\n"); @@ -4791,31 +4808,30 @@ bool process_multipart_ranges_data(const Request &req, Response &res, return true; } -inline bool make_multipart_ranges_data(const Request &req, Response &res, +inline void make_multipart_ranges_data(const Request &req, Response &res, const std::string &boundary, const std::string &content_type, + size_t content_length, std::string &data) { - return process_multipart_ranges_data( - req, res, boundary, content_type, + process_multipart_ranges_data( + req, boundary, content_type, content_length, [&](const std::string &token) { data += token; }, [&](const std::string &token) { data += token; }, [&](size_t offset, size_t length) { - if (offset < res.body.size()) { - data += res.body.substr(offset, length); - return true; - } - return false; + assert(offset + length <= content_length); + data += res.body.substr(offset, length); + return true; }); } -inline size_t -get_multipart_ranges_data_length(const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type) { +inline size_t get_multipart_ranges_data_length(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length) { size_t data_length = 0; process_multipart_ranges_data( - req, res, boundary, content_type, + req, boundary, content_type, content_length, [&](const std::string &token) { data_length += token.size(); }, [&](const std::string &token) { data_length += token.size(); }, [&](size_t /*offset*/, size_t length) { @@ -4827,13 +4843,13 @@ get_multipart_ranges_data_length(const Request &req, Response &res, } template -inline bool write_multipart_ranges_data(Stream &strm, const Request &req, - Response &res, - const std::string &boundary, - const std::string &content_type, - const T &is_shutting_down) { +inline bool +write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, const T &is_shutting_down) { return process_multipart_ranges_data( - req, res, boundary, content_type, + req, boundary, content_type, content_length, [&](const std::string &token) { strm.write(token); }, [&](const std::string &token) { strm.write(token); }, [&](size_t offset, size_t length) { @@ -6012,7 +6028,6 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, if (write_content_with_provider(strm, req, res, boundary, content_type)) { res.content_provider_success_ = true; } else { - res.content_provider_success_ = false; ret = false; } } @@ -6045,7 +6060,8 @@ Server::write_content_with_provider(Stream &strm, const Request &req, offset_and_length.second, is_shutting_down); } else { return detail::write_multipart_ranges_data( - strm, req, res, boundary, content_type, is_shutting_down); + strm, req, res, boundary, content_type, res.content_length_, + is_shutting_down); } } else { if (res.is_chunked_content_provider_) { @@ -6437,14 +6453,14 @@ inline void Server::apply_ranges(const Request &req, Response &res, std::string &content_type, std::string &boundary) const { if (req.ranges.size() > 1) { - boundary = detail::make_multipart_data_boundary(); - auto it = res.headers.find("Content-Type"); if (it != res.headers.end()) { content_type = it->second; res.headers.erase(it); } + boundary = detail::make_multipart_data_boundary(); + res.set_header("Content-Type", "multipart/byteranges; boundary=" + boundary); } @@ -6466,8 +6482,8 @@ inline void Server::apply_ranges(const Request &req, Response &res, offset_and_length, res.content_length_); res.set_header("Content-Range", content_range); } else { - length = detail::get_multipart_ranges_data_length(req, res, boundary, - content_type); + length = detail::get_multipart_ranges_data_length( + req, boundary, content_type, res.content_length_); } res.set_header("Content-Length", std::to_string(length)); } else { @@ -6495,21 +6511,13 @@ inline void Server::apply_ranges(const Request &req, Response &res, offset_and_length, res.body.size()); res.set_header("Content-Range", content_range); - if (offset < res.body.size()) { - res.body = res.body.substr(offset, length); - } else { - res.body.clear(); - res.status = StatusCode::RangeNotSatisfiable_416; - } + assert(offset + length <= res.body.size()); + res.body = res.body.substr(offset, length); } else { std::string data; - if (detail::make_multipart_ranges_data(req, res, boundary, content_type, - data)) { - res.body.swap(data); - } else { - res.body.clear(); - res.status = StatusCode::RangeNotSatisfiable_416; - } + detail::make_multipart_ranges_data(req, res, boundary, content_type, + res.body.size(), data); + res.body.swap(data); } if (type != detail::EncodingType::None) { @@ -6685,13 +6693,20 @@ Server::process_request(Stream &strm, bool close_connection, } } #endif - if (routed) { - if (res.status == -1) { - res.status = req.ranges.empty() ? StatusCode::OK_200 - : StatusCode::PartialContent_206; + if (detail::normalize_ranges(req, res)) { + if (res.status == -1) { + res.status = req.ranges.empty() ? StatusCode::OK_200 + : StatusCode::PartialContent_206; + } + return write_response_with_content(strm, close_connection, req, res); + } else { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); } - return write_response_with_content(strm, close_connection, req, res); } else { if (res.status == -1) { res.status = StatusCode::NotFound_404; } return write_response(strm, close_connection, req, res); diff --git a/test/test.cc b/test/test.cc index e33ab416dc..9ca168160a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1831,7 +1831,7 @@ class ServerTest : public ::testing::Test { }); }) .Get("/streamed-with-range", - [&](const Request & /*req*/, Response &res) { + [&](const Request &req, Response &res) { auto data = new std::string("abcdefg"); res.set_content_provider( data->size(), "text/plain", @@ -1845,8 +1845,8 @@ class ServerTest : public ::testing::Test { EXPECT_TRUE(ret); return true; }, - [data](bool success) { - EXPECT_TRUE(success); + [data, &req](bool success) { + EXPECT_EQ(success, !req.has_param("error")); delete data; }); }) @@ -2957,13 +2957,12 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix1) { } TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { - auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-9999"}}); + auto res = cli_.Get("/streamed-with-range?error", {{"Range", "bytes=-9999"}}); ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::PartialContent_206, res->status); - EXPECT_EQ("7", res->get_header_value("Content-Length")); - EXPECT_EQ(true, res->has_header("Content-Range")); - EXPECT_EQ("bytes 0-6/7", res->get_header_value("Content-Range")); - EXPECT_EQ(std::string("abcdefg"), res->body); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); + EXPECT_EQ("0", res->get_header_value("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(0, res->body.size()); } TEST_F(ServerTest, GetStreamedWithRangeError) { @@ -2972,16 +2971,18 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { "92233720368547758079223372036854775807"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); + EXPECT_EQ("0", res->get_header_value("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(0, res->body.size()); } TEST_F(ServerTest, GetRangeWithMaxLongLength) { auto res = cli_.Get("/with-range", {{"Range", "bytes=0-9223372036854775807"}}); - EXPECT_EQ(StatusCode::PartialContent_206, res->status); - EXPECT_EQ("7", res->get_header_value("Content-Length")); - EXPECT_EQ("bytes 0-6/7", res->get_header_value("Content-Range")); - EXPECT_EQ(true, res->has_header("Content-Range")); - EXPECT_EQ(std::string("abcdefg"), res->body); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); + EXPECT_EQ("0", res->get_header_value("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(0, res->body.size()); } TEST_F(ServerTest, GetStreamedWithRangeMultipart) { From ffc294d37e7a0fdb1b63af6409c798bd0d43f237 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sun, 28 Jan 2024 22:18:29 +0900 Subject: [PATCH 0161/1222] Reduce object copy (#1767) --- httplib.h | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index d659744930..464a421283 100644 --- a/httplib.h +++ b/httplib.h @@ -2065,7 +2065,7 @@ void hosted_at(const std::string &hostname, std::vector &addrs); std::string append_query_params(const std::string &path, const Params ¶ms); -std::pair make_range_header(Ranges ranges); +std::pair make_range_header(const Ranges &ranges); std::pair make_basic_authentication_header(const std::string &username, @@ -2571,7 +2571,7 @@ inline std::string trim_double_quotes_copy(const std::string &s) { inline void split(const char *b, const char *e, char d, std::function fn) { - return split(b, e, d, (std::numeric_limits::max)(), fn); + return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); } inline void split(const char *b, const char *e, char d, size_t m, @@ -5208,10 +5208,11 @@ inline std::string append_query_params(const std::string &path, } // Header utilities -inline std::pair make_range_header(Ranges ranges) { +inline std::pair +make_range_header(const Ranges &ranges) { std::string field = "bytes="; auto i = 0; - for (auto r : ranges) { + for (const auto &r : ranges) { if (i != 0) { field += ", "; } if (r.first != -1) { field += std::to_string(r.first); } field += '-'; @@ -5364,7 +5365,7 @@ inline void Response::set_content_provider( set_header("Content-Type", content_type); content_length_ = in_length; if (in_length > 0) { content_provider_ = std::move(provider); } - content_provider_resource_releaser_ = resource_releaser; + content_provider_resource_releaser_ = std::move(resource_releaser); is_chunked_content_provider_ = false; } @@ -5374,7 +5375,7 @@ inline void Response::set_content_provider( set_header("Content-Type", content_type); content_length_ = 0; content_provider_ = detail::ContentProviderAdapter(std::move(provider)); - content_provider_resource_releaser_ = resource_releaser; + content_provider_resource_releaser_ = std::move(resource_releaser); is_chunked_content_provider_ = false; } @@ -5384,7 +5385,7 @@ inline void Response::set_chunked_content_provider( set_header("Content-Type", content_type); content_length_ = 0; content_provider_ = detail::ContentProviderAdapter(std::move(provider)); - content_provider_resource_releaser_ = resource_releaser; + content_provider_resource_releaser_ = std::move(resource_releaser); is_chunked_content_provider_ = true; } @@ -7612,14 +7613,15 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, if (params.empty()) { return Get(path, headers); } std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, progress); + return Get(path_with_query, headers, std::move(progress)); } inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, Progress progress) { - return Get(path, params, headers, nullptr, content_receiver, progress); + return Get(path, params, headers, nullptr, std::move(content_receiver), + std::move(progress)); } inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, @@ -7628,12 +7630,13 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, ContentReceiver content_receiver, Progress progress) { if (params.empty()) { - return Get(path, headers, response_handler, content_receiver, progress); + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); } std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, response_handler, content_receiver, - progress); + return Get(path_with_query, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); } inline Result ClientImpl::Head(const std::string &path) { @@ -9008,19 +9011,20 @@ inline Result Client::Get(const std::string &path, const Headers &headers, } inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, Progress progress) { - return cli_->Get(path, params, headers, progress); + return cli_->Get(path, params, headers, std::move(progress)); } inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, params, headers, content_receiver, progress); + return cli_->Get(path, params, headers, std::move(content_receiver), + std::move(progress)); } inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, params, headers, response_handler, content_receiver, - progress); + return cli_->Get(path, params, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); } inline Result Client::Head(const std::string &path) { return cli_->Head(path); } From e323374d2abbcfd548c236e767aa2dd7499dcb33 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 28 Jan 2024 17:43:51 -0500 Subject: [PATCH 0162/1222] Fix #1766 --- httplib.h | 56 +++++++++++++++++++++++++++++++++++++++++----------- test/test.cc | 41 +++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 15 deletions(-) diff --git a/httplib.h b/httplib.h index 464a421283..036e819d9e 100644 --- a/httplib.h +++ b/httplib.h @@ -82,6 +82,10 @@ #define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 #endif +#ifndef CPPHTTPLIB_RANGE_MAX_COUNT +#define CPPHTTPLIB_RANGE_MAX_COUNT 1024 +#endif + #ifndef CPPHTTPLIB_TCP_NODELAY #define CPPHTTPLIB_TCP_NODELAY false #endif @@ -4721,29 +4725,57 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, } inline bool normalize_ranges(Request &req, Response &res) { - ssize_t len = static_cast(res.content_length_ ? res.content_length_ - : res.body.size()); + ssize_t contant_len = static_cast( + res.content_length_ ? res.content_length_ : res.body.size()); + + ssize_t prev_first_pos = -1; + ssize_t prev_last_pos = -1; + size_t overwrapping_count = 0; if (!req.ranges.empty()) { + // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 + // 'HTTP Semantics' to avoid potential denial-of-service attacks. + // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 + + // Too many ranges + if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return false; } + for (auto &r : req.ranges) { - auto &st = r.first; - auto &ed = r.second; + auto &first_pos = r.first; + auto &last_pos = r.second; + + if (first_pos == -1 && last_pos == -1) { + first_pos = 0; + last_pos = contant_len; + } - if (st == -1 && ed == -1) { - st = 0; - ed = len; + if (first_pos == -1) { + first_pos = contant_len - last_pos; + last_pos = contant_len - 1; } - if (st == -1) { - st = len - ed; - ed = len - 1; + if (last_pos == -1) { last_pos = contant_len - 1; } + + // Range must be within content length + if (!(0 <= first_pos && first_pos <= last_pos && + last_pos <= contant_len - 1)) { + return false; } - if (ed == -1) { ed = len - 1; } + // Ranges must be in ascending order + if (first_pos <= prev_first_pos) { return false; } - if (!(0 <= st && st <= ed && ed <= len - 1)) { return false; } + // Request must not have more than two overlapping ranges + if (first_pos <= prev_last_pos) { + overwrapping_count++; + if (overwrapping_count > 2) { return false; } + } + + prev_first_pos = (std::max)(prev_first_pos, first_pos); + prev_last_pos = (std::max)(prev_last_pos, last_pos); } } + return true; } diff --git a/test/test.cc b/test/test.cc index 9ca168160a..7b6e8d961a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2962,7 +2962,7 @@ TEST_F(ServerTest, GetStreamedWithRangeSuffix2) { EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(0, res->body.size()); + EXPECT_EQ(0U, res->body.size()); } TEST_F(ServerTest, GetStreamedWithRangeError) { @@ -2973,7 +2973,7 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(0, res->body.size()); + EXPECT_EQ(0U, res->body.size()); } TEST_F(ServerTest, GetRangeWithMaxLongLength) { @@ -2982,7 +2982,7 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) { EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(0, res->body.size()); + EXPECT_EQ(0U, res->body.size()); } TEST_F(ServerTest, GetStreamedWithRangeMultipart) { @@ -2995,6 +2995,41 @@ TEST_F(ServerTest, GetStreamedWithRangeMultipart) { EXPECT_EQ(267U, res->body.size()); } +TEST_F(ServerTest, GetStreamedWithTooManyRanges) { + Ranges ranges; + for (size_t i = 0; i < CPPHTTPLIB_RANGE_MAX_COUNT + 1; i++) { + ranges.emplace_back(0, -1); + } + + auto res = + cli_.Get("/streamed-with-range?error", {{make_range_header(ranges)}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); + EXPECT_EQ("0", res->get_header_value("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(0U, res->body.size()); +} + +TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) { + auto res = cli_.Get("/streamed-with-range?error", + {{make_range_header({{0, -1}, {0, -1}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); + EXPECT_EQ("0", res->get_header_value("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(0U, res->body.size()); +} + +TEST_F(ServerTest, GetStreamedWithRangesMoreThanTwoOverwrapping) { + auto res = cli_.Get("/streamed-with-range?error", + {{make_range_header({{0, 1}, {1, 2}, {2, 3}, {3, 4}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); + EXPECT_EQ("0", res->get_header_value("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(0U, res->body.size()); +} + TEST_F(ServerTest, GetStreamedEndless) { uint64_t offset = 0; auto res = cli_.Get("/streamed-cancel", From b7cac4f4b80d74ba31be3865d637824067574f08 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 29 Jan 2024 07:40:56 -0500 Subject: [PATCH 0163/1222] Release v0.15.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 036e819d9e..5ab47b13ef 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.15.0" +#define CPPHTTPLIB_VERSION "0.15.1" /* * Configuration From 82a90a2325343faaf1c0604561a5e2859fccc3fb Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 29 Jan 2024 08:53:01 -0500 Subject: [PATCH 0164/1222] Update year --- README.md | 2 +- httplib.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fdb0e3b50a..ebde68aafc 100644 --- a/README.md +++ b/README.md @@ -866,7 +866,7 @@ NOTE: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin and MSYS2 incl License ------- -MIT license (© 2023 Yuji Hirose) +MIT license (© 2024 Yuji Hirose) Special Thanks To ----------------- diff --git a/httplib.h b/httplib.h index 5ab47b13ef..bfb74d557b 100644 --- a/httplib.h +++ b/httplib.h @@ -1,7 +1,7 @@ // // httplib.h // -// Copyright (c) 2023 Yuji Hirose. All rights reserved. +// Copyright (c) 2024 Yuji Hirose. All rights reserved. // MIT License // From 762024b890c5747ce1e5655a87300e53013b3832 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 2 Feb 2024 23:17:32 -0500 Subject: [PATCH 0165/1222] Fix #1768 --- httplib.h | 1 + test/test.cc | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/httplib.h b/httplib.h index bfb74d557b..4f61165f91 100644 --- a/httplib.h +++ b/httplib.h @@ -2418,6 +2418,7 @@ inline bool is_valid_path(const std::string &path) { auto beg = i; while (i < path.size() && path[i] != '/') { if (path[i] == '\0') { return false; } + else if (path[i] == '\\') { return false; } i++; } diff --git a/test/test.cc b/test/test.cc index 7b6e8d961a..cfcc5f9d57 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2509,6 +2509,12 @@ TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) { EXPECT_EQ(StatusCode::NotFound_404, res->status); } +TEST_F(ServerTest, GetMethodOutOfBaseDirMountWithBackslash) { + auto res = cli_.Get("/mount/%2e%2e%5c/www2/dir/test.html"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::NotFound_404, res->status); +} + TEST_F(ServerTest, PostMethod303) { auto res = cli_.Post("/1", "body", "text/plain"); ASSERT_TRUE(res); From 80c0cc445e4c555e93758423c34e2b1df9d33bd2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 2 Feb 2024 23:29:30 -0500 Subject: [PATCH 0166/1222] Release v0.15.2 --- httplib.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 4f61165f91..57b8898f61 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.15.1" +#define CPPHTTPLIB_VERSION "0.15.2" /* * Configuration @@ -2417,8 +2417,11 @@ inline bool is_valid_path(const std::string &path) { // Read component auto beg = i; while (i < path.size() && path[i] != '/') { - if (path[i] == '\0') { return false; } - else if (path[i] == '\\') { return false; } + if (path[i] == '\0') { + return false; + } else if (path[i] == '\\') { + return false; + } i++; } From f06fd934f64926aa5b67d853aaaff2ff08e2e448 Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Tue, 6 Feb 2024 05:35:33 +0900 Subject: [PATCH 0167/1222] Fix typo in gtest-all.cc (#1770) synthetic -> synthetic --- test/gtest/gtest-all.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/gtest/gtest-all.cc b/test/gtest/gtest-all.cc index eb8fa17c8f..24e669e1e3 100644 --- a/test/gtest/gtest-all.cc +++ b/test/gtest/gtest-all.cc @@ -1912,7 +1912,7 @@ void AssertHelper::operator=(const Message& message) const { namespace { // When TEST_P is found without a matching INSTANTIATE_TEST_SUITE_P -// to creates test cases for it, a syntetic test case is +// to creates test cases for it, a synthetic test case is // inserted to report ether an error or a log message. // // This configuration bit will likely be removed at some point. From 9d6f5372a3e42468ecafe93ee408bb23f3cc3c78 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 5 Feb 2024 22:10:22 -0500 Subject: [PATCH 0168/1222] Fix #1772 --- httplib.h | 49 +++++++++++++++++++++++++++---------------------- test/test.cc | 11 +++++++++++ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/httplib.h b/httplib.h index 57b8898f61..6064565f8e 100644 --- a/httplib.h +++ b/httplib.h @@ -961,7 +961,7 @@ class Server { bool parse_request_line(const char *s, Request &req) const; void apply_ranges(const Request &req, Response &res, std::string &content_type, std::string &boundary) const; - bool write_response(Stream &strm, bool close_connection, const Request &req, + bool write_response(Stream &strm, bool close_connection, Request &req, Response &res); bool write_response_with_content(Stream &strm, bool close_connection, const Request &req, Response &res); @@ -4728,21 +4728,21 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, return body; } -inline bool normalize_ranges(Request &req, Response &res) { - ssize_t contant_len = static_cast( - res.content_length_ ? res.content_length_ : res.body.size()); +inline bool range_error(Request &req, Response &res) { + if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { + ssize_t contant_len = static_cast( + res.content_length_ ? res.content_length_ : res.body.size()); - ssize_t prev_first_pos = -1; - ssize_t prev_last_pos = -1; - size_t overwrapping_count = 0; + ssize_t prev_first_pos = -1; + ssize_t prev_last_pos = -1; + size_t overwrapping_count = 0; - if (!req.ranges.empty()) { // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 // 'HTTP Semantics' to avoid potential denial-of-service attacks. // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 // Too many ranges - if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return false; } + if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; } for (auto &r : req.ranges) { auto &first_pos = r.first; @@ -4763,16 +4763,16 @@ inline bool normalize_ranges(Request &req, Response &res) { // Range must be within content length if (!(0 <= first_pos && first_pos <= last_pos && last_pos <= contant_len - 1)) { - return false; + return true; } // Ranges must be in ascending order - if (first_pos <= prev_first_pos) { return false; } + if (first_pos <= prev_first_pos) { return true; } // Request must not have more than two overlapping ranges if (first_pos <= prev_last_pos) { overwrapping_count++; - if (overwrapping_count > 2) { return false; } + if (overwrapping_count > 2) { return true; } } prev_first_pos = (std::max)(prev_first_pos, first_pos); @@ -4780,7 +4780,7 @@ inline bool normalize_ranges(Request &req, Response &res) { } } - return true; + return false; } inline std::pair @@ -5987,7 +5987,10 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { } inline bool Server::write_response(Stream &strm, bool close_connection, - const Request &req, Response &res) { + Request &req, Response &res) { + // NOTE: `req.ranges` should be empty, otherwise it will be applied + // incorrectly to the error content. + req.ranges.clear(); return write_response_core(strm, close_connection, req, res, false); } @@ -6694,7 +6697,7 @@ Server::process_request(Stream &strm, bool close_connection, } } - // Rounting + // Routing auto routed = false; #ifdef CPPHTTPLIB_NO_EXCEPTIONS routed = routing(req, res, strm); @@ -6731,21 +6734,23 @@ Server::process_request(Stream &strm, bool close_connection, } #endif if (routed) { - if (detail::normalize_ranges(req, res)) { - if (res.status == -1) { - res.status = req.ranges.empty() ? StatusCode::OK_200 - : StatusCode::PartialContent_206; - } - return write_response_with_content(strm, close_connection, req, res); - } else { + if (res.status == -1) { + res.status = req.ranges.empty() ? StatusCode::OK_200 + : StatusCode::PartialContent_206; + } + + if (detail::range_error(req, res)) { res.body.clear(); res.content_length_ = 0; res.content_provider_ = nullptr; res.status = StatusCode::RangeNotSatisfiable_416; return write_response(strm, close_connection, req, res); } + + return write_response_with_content(strm, close_connection, req, res); } else { if (res.status == -1) { res.status = StatusCode::NotFound_404; } + return write_response(strm, close_connection, req, res); } } diff --git a/test/test.cc b/test/test.cc index cfcc5f9d57..64fc8f2891 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2166,6 +2166,11 @@ class ServerTest : public ::testing::Test { EXPECT_EQ("4", req.get_header_value("Content-Length")); res.set_content(req.body, "application/octet-stream"); }) + .Get("/issue1772", + [&](const Request & /*req*/, Response &res) { + res.status = 401; + res.set_header("WWW-Authenticate", "Basic realm=123456"); + }) #if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT) .Get("/compress", [&](const Request & /*req*/, Response &res) { @@ -3139,6 +3144,12 @@ TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } +TEST_F(ServerTest, Issue1772) { + auto res = cli_.Get("/issue1772", {{make_range_header({{1000, -1}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); +} + TEST_F(ServerTest, GetStreamedChunked) { auto res = cli_.Get("/streamed-chunked"); ASSERT_TRUE(res); From 5c00bbf36ba8ff47b4fb97712fc38cb2884e5b98 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 5 Feb 2024 22:12:43 -0500 Subject: [PATCH 0169/1222] Release v0.15.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 6064565f8e..dfdd260ab3 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.15.2" +#define CPPHTTPLIB_VERSION "0.15.3" /* * Configuration From ad40bd6a009c5fd237f2972987a528d59a8526ba Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 8 Feb 2024 20:59:34 -0700 Subject: [PATCH 0170/1222] Implement file mapping for UWP apps (#1775) Fixes #1773. --- httplib.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index dfdd260ab3..f5436e2eb8 100644 --- a/httplib.h +++ b/httplib.h @@ -2686,21 +2686,29 @@ inline bool mmap::open(const char *path) { close(); #if defined(_WIN32) - hFile_ = ::CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + std::wstring wpath; + for (size_t i = 0; i < strlen(path); i++) { + wpath += path[i]; + } + + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); if (hFile_ == INVALID_HANDLE_VALUE) { return false; } - size_ = ::GetFileSize(hFile_, NULL); + LARGE_INTEGER size{}; + if (!::GetFileSizeEx(hFile_, &size)) { return false; } + size_ = static_cast(size.QuadPart); - hMapping_ = ::CreateFileMapping(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); + hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, + NULL); if (hMapping_ == NULL) { close(); return false; } - addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); + addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); #else fd_ = ::open(path, O_RDONLY); if (fd_ == -1) { return false; } From c5a0673c934f409d0114dc5d32593e3efdbb2212 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 17 Feb 2024 23:17:15 +0900 Subject: [PATCH 0171/1222] Use final keyword for devirtualization (#1779) --- httplib.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index f5436e2eb8..cabdb48b83 100644 --- a/httplib.h +++ b/httplib.h @@ -462,7 +462,7 @@ class DataSink { std::ostream os; private: - class data_sink_streambuf : public std::streambuf { + class data_sink_streambuf final : public std::streambuf { public: explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} @@ -663,7 +663,7 @@ class TaskQueue { virtual void on_idle() {} }; -class ThreadPool : public TaskQueue { +class ThreadPool final : public TaskQueue { public: explicit ThreadPool(size_t n, size_t mqr = 0) : shutdown_(false), max_queued_requests_(mqr) { @@ -780,7 +780,7 @@ class MatcherBase { * the resulting capture will be * {{"capture", "1"}, {"second_capture", "2"}} */ -class PathParamsMatcher : public MatcherBase { +class PathParamsMatcher final : public MatcherBase { public: PathParamsMatcher(const std::string &pattern); @@ -810,7 +810,7 @@ class PathParamsMatcher : public MatcherBase { * This means that wildcard patterns may match multiple path segments with /: * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". */ -class RegexMatcher : public MatcherBase { +class RegexMatcher final : public MatcherBase { public: RegexMatcher(const std::string &pattern) : regex_(pattern) {} @@ -1737,7 +1737,7 @@ class SSLServer : public Server { std::mutex ctx_mutex_; }; -class SSLClient : public ClientImpl { +class SSLClient final : public ClientImpl { public: explicit SSLClient(const std::string &host); @@ -2126,7 +2126,7 @@ enum class EncodingType { None = 0, Gzip, Brotli }; EncodingType encoding_type(const Request &req, const Response &res); -class BufferStream : public Stream { +class BufferStream final : public Stream { public: BufferStream() = default; ~BufferStream() override = default; @@ -2166,7 +2166,7 @@ class decompressor { Callback callback) = 0; }; -class nocompressor : public compressor { +class nocompressor final : public compressor { public: ~nocompressor() override = default; @@ -2175,7 +2175,7 @@ class nocompressor : public compressor { }; #ifdef CPPHTTPLIB_ZLIB_SUPPORT -class gzip_compressor : public compressor { +class gzip_compressor final : public compressor { public: gzip_compressor(); ~gzip_compressor() override; @@ -2188,7 +2188,7 @@ class gzip_compressor : public compressor { z_stream strm_; }; -class gzip_decompressor : public decompressor { +class gzip_decompressor final : public decompressor { public: gzip_decompressor(); ~gzip_decompressor() override; @@ -2205,7 +2205,7 @@ class gzip_decompressor : public decompressor { #endif #ifdef CPPHTTPLIB_BROTLI_SUPPORT -class brotli_compressor : public compressor { +class brotli_compressor final : public compressor { public: brotli_compressor(); ~brotli_compressor(); @@ -2217,7 +2217,7 @@ class brotli_compressor : public compressor { BrotliEncoderState *state_ = nullptr; }; -class brotli_decompressor : public decompressor { +class brotli_decompressor final : public decompressor { public: brotli_decompressor(); ~brotli_decompressor(); @@ -2935,7 +2935,7 @@ inline bool is_socket_alive(socket_t sock) { return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; } -class SocketStream : public Stream { +class SocketStream final : public Stream { public: SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec); @@ -2964,7 +2964,7 @@ class SocketStream : public Stream { }; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLSocketStream : public Stream { +class SSLSocketStream final : public Stream { public: SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, From b4d26badf2544d6f3b749a55762eff96aefb2b19 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 17 Feb 2024 09:17:51 -0500 Subject: [PATCH 0172/1222] Update github actions --- .github/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 173daa8c89..22fd72ef71 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -17,7 +17,7 @@ jobs: git config --global core.autocrlf false git config --global core.eol lf - name: checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: install brotli library on ubuntu if: matrix.os == 'ubuntu-latest' run: sudo apt update && sudo apt-get install -y libbrotli-dev @@ -32,7 +32,7 @@ jobs: run: cd test && make fuzz_test - name: setup msbuild on windows if: matrix.os == 'windows-latest' - uses: microsoft/setup-msbuild@v1.1 + uses: microsoft/setup-msbuild@v2 - name: make-windows if: matrix.os == 'windows-latest' run: | From d1a1c8a1589f3cdb8673c4a38d390191f2f5e79a Mon Sep 17 00:00:00 2001 From: KTGH Date: Tue, 27 Feb 2024 19:22:44 -0500 Subject: [PATCH 0173/1222] Add description & URL to Cmake (#1785) Doesn't do much, but some packages/builders might find a use for these vars it provides... --- CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 73de511899..94dcae90d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,12 @@ file(STRINGS httplib.h _raw_version_string REGEX "CPPHTTPLIB_VERSION \"([0-9]+\\ # since _raw_version_string would contain the entire line of code where it found the version string string(REGEX MATCH "([0-9]+\\.?)+" _httplib_version "${_raw_version_string}") -project(httplib VERSION ${_httplib_version} LANGUAGES CXX) +project(httplib + VERSION ${_httplib_version} + LANGUAGES CXX + DESCRIPTION "A C++ header-only HTTP/HTTPS server and client library." + HOMEPAGE_URL "https://github.com/yhirose/cpp-httplib" +) # Change as needed to set an OpenSSL minimum version. # This is used in the installed Cmake config file. From 6791a8364d1644b44e0fb13fca472c398f78eb67 Mon Sep 17 00:00:00 2001 From: KTGH Date: Tue, 27 Feb 2024 19:25:02 -0500 Subject: [PATCH 0174/1222] FindBrotli cleanup & fixes (#1786) * Unimportant cleanup of FindBrotli Some whitespace, removal of an unused var, and add a comment about the missing version support * Fix incorrect var in FindBrotli Not much to say, it was just the wrong one. Not sure how that happened, maybe a holdover from when I did an overhaul at some point.. * Tiny useless edit to FindBrotli Just a little tweak to how I was postfixing those -static strings to the lib names. Mostly pointless.. * Simplify some FindBrotli code Nothing much, just reducing redundant stuff & a cleanup of a comment. * Ignore PkgConf in FindBrotli if looking for static As per the issue mentioned in this comment, static PkgConf support is broken upstream. After testing, I found it'll just accept shared even if you try to find static, so I'm merely disabling that feature for this FindBrotli module. That said, you can still get the static libs if you have them installed, but PkgConf will be ignored even if found in favor of a regular find_library call. Otherwise you'd end up with shared libs even if you ask for static (with PkgConf installed). TLDR: actually fail correctly when static libs aren't found when PkgConf thought they were. --- cmake/FindBrotli.cmake | 46 +++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/cmake/FindBrotli.cmake b/cmake/FindBrotli.cmake index de1079cde4..2048e55499 100644 --- a/cmake/FindBrotli.cmake +++ b/cmake/FindBrotli.cmake @@ -1,6 +1,6 @@ # A simple FindBrotli package for Cmake's find_package function. # Note: This find package doesn't have version support, as the version file doesn't seem to be installed on most systems. -# +# # If you want to find the static packages instead of shared (the default), define BROTLI_USE_STATIC_LIBS as TRUE. # The targets will have the same names, but it will use the static libs. # @@ -14,7 +14,7 @@ # If they asked for a specific version, warn/fail since we don't support it. # TODO: if they start distributing the version somewhere, implement finding it. -# But currently there's a version header that doesn't seem to get installed. +# See https://github.com/google/brotli/issues/773#issuecomment-579133187 if(Brotli_FIND_VERSION) set(_brotli_version_error_msg "FindBrotli.cmake doesn't have version support!") # If the package is required, throw a fatal error @@ -68,29 +68,24 @@ if(BROTLI_USE_STATIC_LIBS) set(_brotli_stat_str "_STATIC") endif() -# Lets us know we are using the PkgConfig libraries -# Will be set false if any non-pkgconf vars are used -set(_brotli_using_pkgconf TRUE) - # Each string here is "ComponentName;LiteralName" (the semi-colon is a delimiter) foreach(_listvar "common;common" "decoder;dec" "encoder;enc") # Split the component name and literal library name from the listvar list(GET _listvar 0 _component_name) list(GET _listvar 1 _libname) - if(PKG_CONFIG_FOUND) + # NOTE: We can't rely on PkgConf for static libs since the upstream static lib support is broken + # See https://github.com/google/brotli/issues/795 + # TODO: whenever their issue is fixed upstream, remove this "AND NOT BROTLI_USE_STATIC_LIBS" check + if(PKG_CONFIG_FOUND AND NOT BROTLI_USE_STATIC_LIBS) # These need to be GLOBAL for MinGW when making ALIAS libraries against them. - if(BROTLI_USE_STATIC_LIBS) - # Have to use _STATIC to tell PkgConfig to find the static libs. - pkg_check_modules(Brotli_${_component_name}_STATIC QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname}) - else() - pkg_check_modules(Brotli_${_component_name} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname}) - endif() + # Have to postfix _STATIC on the name to tell PkgConfig to find the static libs. + pkg_check_modules(Brotli_${_component_name}${_brotli_stat_str} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname}) endif() - # Check if the target was already found by Pkgconf - if(TARGET PkgConfig::Brotli_${_component_name} OR TARGET PkgConfig::Brotli_${_component_name}_STATIC) - # Can't use generators for ALIAS targets, so you get this jank + # Check if the target was already found by Pkgconf + if(TARGET PkgConfig::Brotli_${_component_name}${_brotli_stat_str}) + # ALIAS since we don't want the PkgConfig namespace on the Cmake library (for end-users) add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str}) # Tells HANDLE_COMPONENTS we found the component @@ -109,23 +104,18 @@ foreach(_listvar "common;common" "decoder;dec" "encoder;enc") continue() endif() - # Lets us know we aren't using the PkgConfig libraries - set(_brotli_using_pkgconf FALSE) if(Brotli_FIND_REQUIRED_${_component_name}) # If it's required, we can set the name used in find_library as a required var for FindPackageHandleStandardArgs list(APPEND _brotli_req_vars "Brotli_${_component_name}") endif() + list(APPEND _brotli_lib_names + "brotli${_libname}" + "libbrotli${_libname}" + ) if(BROTLI_USE_STATIC_LIBS) - list(APPEND _brotli_lib_names - "brotli${_libname}-static" - "libbrotli${_libname}-static" - ) - else() - list(APPEND _brotli_lib_names - "brotli${_libname}" - "libbrotli${_libname}" - ) + # Postfix "-static" to the libnames since we're looking for static libs + list(TRANSFORM _brotli_lib_names APPEND "-static") endif() find_library(Brotli_${_component_name} @@ -168,7 +158,7 @@ find_package_handle_standard_args(Brotli Brotli_FOUND REQUIRED_VARS Brotli_INCLUDE_DIR - ${_brotli_required_targets} + ${_brotli_req_vars} HANDLE_COMPONENTS ) From 4dd2f3d03dcad28c267ecee85560a9ff151bcb66 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 7 Mar 2024 19:07:39 +0800 Subject: [PATCH 0175/1222] fix ambiguous for HandlerWithResponse and Handler in set_error_handler using lambda expression as Handler in set_error_handler will cause ambiguous. Template forwarding can forward HandlerWithResponse to the correct overloading function --- httplib.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index cabdb48b83..74c034e93b 100644 --- a/httplib.h +++ b/httplib.h @@ -231,6 +231,7 @@ using socket_t = int; #include #include #include +#include #include #include #include @@ -871,8 +872,13 @@ class Server { Server &set_default_file_mimetype(const std::string &mime); Server &set_file_request_handler(Handler handler); - Server &set_error_handler(HandlerWithResponse handler); - Server &set_error_handler(Handler handler); + template + Server &set_error_handler(ErrorHandlerFunc &&handler) { + return set_error_handler_impl( + std::forward(handler), + std::is_convertible{}); + } + Server &set_exception_handler(ExceptionHandler handler); Server &set_pre_routing_handler(HandlerWithResponse handler); Server &set_post_routing_handler(Handler handler); @@ -934,6 +940,9 @@ class Server { size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; private: + Server &set_error_handler_impl(HandlerWithResponse handler, std::true_type); + Server &set_error_handler_impl(Handler handler, std::false_type); + using Handlers = std::vector, Handler>>; using HandlersForContentReader = @@ -5797,12 +5806,14 @@ inline Server &Server::set_file_request_handler(Handler handler) { return *this; } -inline Server &Server::set_error_handler(HandlerWithResponse handler) { +inline Server &Server::set_error_handler_impl(HandlerWithResponse handler, + std::true_type) { error_handler_ = std::move(handler); return *this; } -inline Server &Server::set_error_handler(Handler handler) { +inline Server &Server::set_error_handler_impl(Handler handler, + std::false_type) { error_handler_ = [handler](const Request &req, Response &res) { handler(req, res); return HandlerResponse::Handled; From 548dfff0aef25e36e971af96b49ce7fbb72d840e Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 9 Mar 2024 22:26:17 -0500 Subject: [PATCH 0176/1222] Fix #1793 --- httplib.h | 33 ++++++++++++++++------- test/Makefile | 4 ++- test/test.cc | 74 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 76 insertions(+), 35 deletions(-) diff --git a/httplib.h b/httplib.h index cabdb48b83..a77d6d9cc4 100644 --- a/httplib.h +++ b/httplib.h @@ -145,11 +145,11 @@ using ssize_t = long; #endif // _MSC_VER #ifndef S_ISREG -#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) +#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) #endif // S_ISREG #ifndef S_ISDIR -#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) +#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) #endif // S_ISDIR #ifndef NOMINMAX @@ -1745,10 +1745,12 @@ class SSLClient final : public ClientImpl { explicit SSLClient(const std::string &host, int port, const std::string &client_cert_path, - const std::string &client_key_path); + const std::string &client_key_path, + const std::string &private_key_password = std::string()); explicit SSLClient(const std::string &host, int port, X509 *client_cert, - EVP_PKEY *client_key); + EVP_PKEY *client_key, + const std::string &private_key_password = std::string()); ~SSLClient() override; @@ -2700,8 +2702,8 @@ inline bool mmap::open(const char *path) { if (!::GetFileSizeEx(hFile_, &size)) { return false; } size_ = static_cast(size.QuadPart); - hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, - NULL); + hMapping_ = + ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); if (hMapping_ == NULL) { close(); @@ -8438,7 +8440,6 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); - // add default password callback before opening encrypted private key if (private_key_password != nullptr && (private_key_password[0] != '\0')) { SSL_CTX_set_default_passwd_cb_userdata( ctx_, @@ -8544,7 +8545,8 @@ inline SSLClient::SSLClient(const std::string &host, int port) inline SSLClient::SSLClient(const std::string &host, int port, const std::string &client_cert_path, - const std::string &client_key_path) + const std::string &client_key_path, + const std::string &private_key_password) : ClientImpl(host, port, client_cert_path, client_key_path) { ctx_ = SSL_CTX_new(TLS_client_method()); @@ -8554,6 +8556,12 @@ inline SSLClient::SSLClient(const std::string &host, int port, }); if (!client_cert_path.empty() && !client_key_path.empty()) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), SSL_FILETYPE_PEM) != 1 || SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), @@ -8565,7 +8573,8 @@ inline SSLClient::SSLClient(const std::string &host, int port, } inline SSLClient::SSLClient(const std::string &host, int port, - X509 *client_cert, EVP_PKEY *client_key) + X509 *client_cert, EVP_PKEY *client_key, + const std::string &private_key_password) : ClientImpl(host, port) { ctx_ = SSL_CTX_new(TLS_client_method()); @@ -8575,6 +8584,12 @@ inline SSLClient::SSLClient(const std::string &host, int port, }); if (client_cert != nullptr && client_key != nullptr) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { SSL_CTX_free(ctx_); diff --git a/test/Makefile b/test/Makefile index 3b72d209fa..6594af1c07 100644 --- a/test/Makefile +++ b/test/Makefile @@ -70,9 +70,11 @@ cert.pem: openssl genrsa 2048 > rootCA.key.pem openssl req -x509 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem openssl genrsa 2048 > client.key.pem - openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem + openssl req -new -batch -config test.conf -key client.key.pem| openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem openssl req -new -batch -config test.conf -key key_encrypted.pem | openssl x509 -days 3650 -req -signkey key_encrypted.pem > cert_encrypted.pem + openssl genrsa -aes256 -passout pass:test012! 2048 > client_encrypted.key.pem + openssl req -new -batch -config test.conf -key client_encrypted.key.pem -passin pass:test012! | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client_encrypted.cert.pem #c_rehash . clean: diff --git a/test/test.cc b/test/test.cc index 64fc8f2891..801673ad08 100644 --- a/test/test.cc +++ b/test/test.cc @@ -20,6 +20,9 @@ #define CLIENT_CA_CERT_DIR "." #define CLIENT_CERT_FILE "./client.cert.pem" #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem" +#define CLIENT_ENCRYPTED_CERT_FILE "./client_encrypted.cert.pem" +#define CLIENT_ENCRYPTED_PRIVATE_KEY_FILE "./client_encrypted.key.pem" +#define CLIENT_ENCRYPTED_PRIVATE_KEY_PASS "test012!" #define SERVER_ENCRYPTED_CERT_FILE "./cert_encrypted.pem" #define SERVER_ENCRYPTED_PRIVATE_KEY_FILE "./key_encrypted.pem" #define SERVER_ENCRYPTED_PRIVATE_KEY_PASS "test123!" @@ -5109,15 +5112,16 @@ TEST(SSLClientTest, SetInterfaceWithINET6) { } #endif -TEST(SSLClientServerTest, ClientCertPresent) { +void ClientCertPresent( + const std::string &client_cert_file, + const std::string &client_private_key_file, + const std::string &client_encrypted_private_key_pass = std::string()) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE, CLIENT_CA_CERT_DIR); ASSERT_TRUE(svr.is_valid()); svr.Get("/test", [&](const Request &req, Response &res) { res.set_content("test", "text/plain"); - svr.stop(); - ASSERT_TRUE(true); auto peer_cert = SSL_get_peer_certificate(req.ssl); ASSERT_TRUE(peer_cert != nullptr); @@ -5140,13 +5144,15 @@ TEST(SSLClientServerTest, ClientCertPresent) { thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); auto se = detail::scope_exit([&] { + svr.stop(); t.join(); ASSERT_FALSE(svr.is_running()); }); svr.wait_until_ready(); - SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + SSLClient cli(HOST, PORT, client_cert_file, client_private_key_file, + client_encrypted_private_key_pass); cli.enable_server_certificate_verification(false); cli.set_connection_timeout(30); @@ -5155,35 +5161,43 @@ TEST(SSLClientServerTest, ClientCertPresent) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(SSLClientServerTest, ClientCertPresent) { + ClientCertPresent(CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); +} + +TEST(SSLClientServerTest, ClientEncryptedCertPresent) { + ClientCertPresent(CLIENT_ENCRYPTED_CERT_FILE, + CLIENT_ENCRYPTED_PRIVATE_KEY_FILE, + CLIENT_ENCRYPTED_PRIVATE_KEY_PASS); +} + #if !defined(_WIN32) || defined(OPENSSL_USE_APPLINK) -TEST(SSLClientServerTest, MemoryClientCertPresent) { - X509 *server_cert; - EVP_PKEY *server_private_key; - X509_STORE *client_ca_cert_store; - X509 *client_cert; - EVP_PKEY *client_private_key; - - FILE *f = fopen(SERVER_CERT_FILE, "r+"); - server_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); +void MemoryClientCertPresent( + const std::string &client_cert_file, + const std::string &client_private_key_file, + const std::string &client_encrypted_private_key_pass = std::string()) { + auto f = fopen(SERVER_CERT_FILE, "r+"); + auto server_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); fclose(f); f = fopen(SERVER_PRIVATE_KEY_FILE, "r+"); - server_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr); + auto server_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr); fclose(f); f = fopen(CLIENT_CA_CERT_FILE, "r+"); - client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); - client_ca_cert_store = X509_STORE_new(); + auto client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); + auto client_ca_cert_store = X509_STORE_new(); X509_STORE_add_cert(client_ca_cert_store, client_cert); X509_free(client_cert); fclose(f); - f = fopen(CLIENT_CERT_FILE, "r+"); + f = fopen(client_cert_file.c_str(), "r+"); client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); fclose(f); - f = fopen(CLIENT_PRIVATE_KEY_FILE, "r+"); - client_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr); + f = fopen(client_private_key_file.c_str(), "r+"); + auto client_private_key = PEM_read_PrivateKey( + f, nullptr, nullptr, (void *)client_encrypted_private_key_pass.c_str()); fclose(f); SSLServer svr(server_cert, server_private_key, client_ca_cert_store); @@ -5191,8 +5205,6 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { svr.Get("/test", [&](const Request &req, Response &res) { res.set_content("test", "text/plain"); - svr.stop(); - ASSERT_TRUE(true); auto peer_cert = SSL_get_peer_certificate(req.ssl); ASSERT_TRUE(peer_cert != nullptr); @@ -5215,13 +5227,15 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); auto se = detail::scope_exit([&] { + svr.stop(); t.join(); ASSERT_FALSE(svr.is_running()); }); svr.wait_until_ready(); - SSLClient cli(HOST, PORT, client_cert, client_private_key); + SSLClient cli(HOST, PORT, client_cert, client_private_key, + client_encrypted_private_key_pass); cli.enable_server_certificate_verification(false); cli.set_connection_timeout(30); @@ -5234,6 +5248,16 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) { X509_free(client_cert); EVP_PKEY_free(client_private_key); } + +TEST(SSLClientServerTest, MemoryClientCertPresent) { + MemoryClientCertPresent(CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); +} + +TEST(SSLClientServerTest, MemoryClientEncryptedCertPresent) { + MemoryClientCertPresent(CLIENT_ENCRYPTED_CERT_FILE, + CLIENT_ENCRYPTED_PRIVATE_KEY_FILE, + CLIENT_ENCRYPTED_PRIVATE_KEY_PASS); +} #endif TEST(SSLClientServerTest, ClientCertMissing) { @@ -5265,11 +5289,11 @@ TEST(SSLClientServerTest, TrustDirOptional) { svr.Get("/test", [&](const Request &, Response &res) { res.set_content("test", "text/plain"); - svr.stop(); }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); auto se = detail::scope_exit([&] { + svr.stop(); t.join(); ASSERT_FALSE(svr.is_running()); }); @@ -5361,13 +5385,12 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { nullptr); return true; }; + SSLServer svr(setup_ssl_ctx_callback); ASSERT_TRUE(svr.is_valid()); svr.Get("/test", [&](const Request &req, Response &res) { res.set_content("test", "text/plain"); - svr.stop(); - ASSERT_TRUE(true); auto peer_cert = SSL_get_peer_certificate(req.ssl); ASSERT_TRUE(peer_cert != nullptr); @@ -5390,6 +5413,7 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); auto se = detail::scope_exit([&] { + svr.stop(); t.join(); ASSERT_FALSE(svr.is_running()); }); From b8bafbc29129a9f12e58032e608b51996219d6f5 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 6 Apr 2024 02:50:21 +0900 Subject: [PATCH 0177/1222] Generate missing PEMs for CTest (#1811) * Generate missing PEMs * Fix typo * Copy files using simpler command --- test/CMakeLists.txt | 26 +++++++++++++++++--------- test/Makefile | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3cf2c24d89..d982253317 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,7 +2,7 @@ find_package(GTest) if(GTest_FOUND) if(NOT TARGET GTest::gtest_main AND TARGET GTest::Main) - # CMake <3.20 + # CMake <3.20 add_library(GTest::gtest_main INTERFACE IMPORTED) target_link_libraries(GTest::gtest_main INTERFACE GTest::Main) endif() @@ -29,14 +29,9 @@ target_compile_options(httplib-test PRIVATE "$<$:/utf-8;/b target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main) gtest_discover_tests(httplib-test) -execute_process( - COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www www - COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www2 www2 - COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_LIST_DIR}/www3 www3 - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_LIST_DIR}/ca-bundle.crt ca-bundle.crt - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_LIST_DIR}/image.jpg image.jpg - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMAND_ERROR_IS_FATAL ANY +file( + COPY www www2 www3 ca-bundle.crt image.jpg + DESTINATION ${CMAKE_CURRENT_BINARY_DIR} ) if(HTTPLIB_IS_USING_OPENSSL) @@ -101,6 +96,19 @@ if(HTTPLIB_IS_USING_OPENSSL) WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND_ERROR_IS_FATAL ANY ) + execute_process( + COMMAND ${OPENSSL_COMMAND} genrsa -aes256 -passout pass:test012! 2048 + OUTPUT_FILE client_encrypted.key.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${OPENSSL_COMMAND} req -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key client_encrypted.key.pem -passin pass:test012! + COMMAND ${OPENSSL_COMMAND} x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial + OUTPUT_FILE client_encrypted.cert.pem + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMAND_ERROR_IS_FATAL ANY + ) endif() add_subdirectory(fuzzing) diff --git a/test/Makefile b/test/Makefile index 6594af1c07..c4dc4f1b36 100644 --- a/test/Makefile +++ b/test/Makefile @@ -70,7 +70,7 @@ cert.pem: openssl genrsa 2048 > rootCA.key.pem openssl req -x509 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem openssl genrsa 2048 > client.key.pem - openssl req -new -batch -config test.conf -key client.key.pem| openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem + openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem openssl req -new -batch -config test.conf -key key_encrypted.pem | openssl x509 -days 3650 -req -signkey key_encrypted.pem > cert_encrypted.pem openssl genrsa -aes256 -passout pass:test012! 2048 > client_encrypted.key.pem From a61f2b89becf406679e882384157f8ad4bacf7c1 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Sun, 7 Apr 2024 16:05:07 +0200 Subject: [PATCH 0178/1222] build(meson): generate new test PEMs (#1813) Follow-up to commits 548dfff0aef25e36e971af96b49ce7fbb72d840e and b8bafbc29129a9f12e58032e608b51996219d6f5 --- test/meson.build | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/meson.build b/test/meson.build index a83360d5ca..52fb8251db 100644 --- a/test/meson.build +++ b/test/meson.build @@ -79,6 +79,26 @@ client_cert_pem = custom_target( command: [openssl, 'x509', '-in', '@INPUT0@', '-days', '370', '-req', '-CA', '@INPUT1@', '-CAkey', '@INPUT2@', '-CAcreateserial', '-out', '@OUTPUT@'] ) +client_encrypted_key_pem = custom_target( + 'client_encrypted_key_pem', + output: 'client_encrypted.key.pem', + command: [openssl, 'genrsa', '-aes256', '-passout', 'pass:test012!', '-out', '@OUTPUT@', '2048'] +) + +client_encrypted_temp_req = custom_target( + 'client_encrypted_temp_req', + input: client_encrypted_key_pem, + output: 'client_encrypted_temp_req', + command: [openssl, 'req', '-new', '-batch', '-config', test_conf, '-key', '@INPUT@', '-passin', 'pass:test012!', '-out', '@OUTPUT@'] +) + +client_encrypted_cert_pem = custom_target( + 'client_encrypted_cert_pem', + input: [client_encrypted_temp_req, rootca_cert_pem, rootca_key_pem], + output: 'client_encrypted.cert.pem', + command: [openssl, 'x509', '-in', '@INPUT0@', '-days', '370', '-req', '-CA', '@INPUT1@', '-CAkey', '@INPUT2@', '-CAcreateserial', '-out', '@OUTPUT@'] +) + # Copy test files to the build directory configure_file(input: 'ca-bundle.crt', output: 'ca-bundle.crt', copy: true) configure_file(input: 'image.jpg', output: 'image.jpg', copy: true) @@ -112,7 +132,9 @@ test( rootca_key_pem, rootca_cert_pem, client_key_pem, - client_cert_pem + client_cert_pem, + client_encrypted_key_pem, + client_encrypted_cert_pem ], workdir: meson.current_build_dir(), timeout: 300 From f44ab9b3da7bfc830c230d3fe2f50db7b818b66b Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sun, 7 Apr 2024 23:06:16 +0900 Subject: [PATCH 0179/1222] Fix range parser when parsing too many ranges (#1812) * Implement range parser without std::regex * Add test cases for invalid ranges --- httplib.h | 53 ++++++++++++++++++++++++++++++---------------------- test/test.cc | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/httplib.h b/httplib.h index a77d6d9cc4..b4363f6cad 100644 --- a/httplib.h +++ b/httplib.h @@ -4365,35 +4365,44 @@ 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)) { - auto pos = static_cast(m.position(1)); - auto len = static_cast(m.length(1)); + auto is_valid = [](const std::string &str) { + return std::all_of(str.cbegin(), str.cend(), + [](unsigned char c) { return std::isdigit(c); }); + }; + + if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) { + const auto pos = static_cast(6); + const auto len = static_cast(s.size() - 6); auto all_valid_ranges = true; split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { if (!all_valid_ranges) { return; } - static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))"); - std::cmatch cm; - if (std::regex_match(b, e, cm, re_another_range)) { - ssize_t first = -1; - if (!cm.str(1).empty()) { - first = static_cast(std::stoll(cm.str(1))); - } - ssize_t last = -1; - if (!cm.str(2).empty()) { - last = static_cast(std::stoll(cm.str(2))); - } + const auto it = std::find(b, e, '-'); + if (it == e) { + all_valid_ranges = false; + return; + } - if (first != -1 && last != -1 && first > last) { - all_valid_ranges = false; - return; - } - ranges.emplace_back(std::make_pair(first, last)); + const auto lhs = std::string(b, it); + const auto rhs = std::string(it + 1, e); + if (!is_valid(lhs) || !is_valid(rhs)) { + all_valid_ranges = false; + return; + } + + const auto first = + static_cast(lhs.empty() ? -1 : std::stoll(lhs)); + const auto last = + static_cast(rhs.empty() ? -1 : std::stoll(rhs)); + if ((first == -1 && last == -1) || + (first != -1 && last != -1 && first > last)) { + all_valid_ranges = false; + return; } + + ranges.emplace_back(first, last); }); - return all_valid_ranges; + return all_valid_ranges && !ranges.empty(); } return false; #ifdef CPPHTTPLIB_NO_EXCEPTIONS diff --git a/test/test.cc b/test/test.cc index 801673ad08..71023f3f2c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -352,6 +352,25 @@ TEST(ParseHeaderValueTest, Range) { EXPECT_EQ(300u, ranges[2].first); EXPECT_EQ(400u, ranges[2].second); } + + { + Ranges ranges; + + EXPECT_FALSE(detail::parse_range_header("bytes", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=0", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=-", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes= ", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=,", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=,,", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=,,,", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=a-b", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=1-0", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=0--1", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=0- 1", ranges)); + EXPECT_FALSE(detail::parse_range_header("bytes=0 -1", ranges)); + EXPECT_TRUE(ranges.empty()); + } } TEST(ParseAcceptEncoding1, AcceptEncoding) { From 00bdf73ec63c824b6cfc46050b94ef8fbff952d6 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Tue, 9 Apr 2024 16:59:07 -0400 Subject: [PATCH 0180/1222] fix: increase default receive buffer to 16kb (#1814) Since TLS packets have a maximum size of 16kb it makes sense to fully accommodate them on reads. Co-authored-by: Rusty Conover --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index b4363f6cad..8ef9a7052d 100644 --- a/httplib.h +++ b/httplib.h @@ -91,7 +91,7 @@ #endif #ifndef CPPHTTPLIB_RECV_BUFSIZ -#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) +#define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) #endif #ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ From 825c3fbbb1bac024c2745f5f0fa9209f2711d265 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 11 Apr 2024 21:15:19 -0400 Subject: [PATCH 0181/1222] Removed excess usage of std::move --- httplib.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index 8ef9a7052d..0509d929a3 100644 --- a/httplib.h +++ b/httplib.h @@ -145,11 +145,11 @@ using ssize_t = long; #endif // _MSC_VER #ifndef S_ISREG -#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) +#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) #endif // S_ISREG #ifndef S_ISDIR -#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) +#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) #endif // S_ISDIR #ifndef NOMINMAX @@ -719,7 +719,7 @@ class ThreadPool final : public TaskQueue { if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } - fn = std::move(pool_.jobs_.front()); + fn = pool_.jobs_.front(); pool_.jobs_.pop_front(); } @@ -3809,7 +3809,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) { auto val = compare_case_ignore(key, "Location") ? std::string(p, end) : decode_url(std::string(p, end), false); - fn(std::move(key), std::move(val)); + fn(key, val); return true; } @@ -3847,8 +3847,8 @@ inline bool read_headers(Stream &strm, Headers &headers) { auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; parse_header(line_reader.ptr(), end, - [&](std::string &&key, std::string &&val) { - headers.emplace(std::move(key), std::move(val)); + [&](const std::string &key, const std::string &val) { + headers.emplace(key, val); }); } @@ -3948,8 +3948,8 @@ inline bool read_content_chunked(Stream &strm, T &x, auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; parse_header(line_reader.ptr(), end, - [&](std::string &&key, std::string &&val) { - x.headers.emplace(std::move(key), std::move(val)); + [&](const std::string &key, const std::string &val) { + x.headers.emplace(key, val); }); if (!line_reader.getline()) { return false; } @@ -4461,7 +4461,7 @@ class MultipartFormDataParser { const auto header = buf_head(pos); if (!parse_header(header.data(), header.data() + header.size(), - [&](std::string &&, std::string &&) {})) { + [&](const std::string &, const std::string &) {})) { is_valid_ = false; return false; } From 07288888ad39004ddcdfefadfdd666c3a8737a5c Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 11 Apr 2024 22:26:30 -0400 Subject: [PATCH 0182/1222] Code cleanup --- httplib.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 698849c772..4c446d91b6 100644 --- a/httplib.h +++ b/httplib.h @@ -231,7 +231,6 @@ using socket_t = int; #include #include #include -#include #include #include #include @@ -874,7 +873,7 @@ class Server { template Server &set_error_handler(ErrorHandlerFunc &&handler) { - return set_error_handler_impl( + return set_error_handler_core( std::forward(handler), std::is_convertible{}); } @@ -940,9 +939,6 @@ class Server { size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; private: - Server &set_error_handler_impl(HandlerWithResponse handler, std::true_type); - Server &set_error_handler_impl(Handler handler, std::false_type); - using Handlers = std::vector, Handler>>; using HandlersForContentReader = @@ -952,6 +948,9 @@ class Server { static std::unique_ptr make_matcher(const std::string &pattern); + Server &set_error_handler_core(HandlerWithResponse handler, std::true_type); + Server &set_error_handler_core(Handler handler, std::false_type); + socket_t create_server_socket(const std::string &host, int port, int socket_flags, SocketOptions socket_options) const; @@ -5817,13 +5816,13 @@ inline Server &Server::set_file_request_handler(Handler handler) { return *this; } -inline Server &Server::set_error_handler_impl(HandlerWithResponse handler, +inline Server &Server::set_error_handler_core(HandlerWithResponse handler, std::true_type) { error_handler_ = std::move(handler); return *this; } -inline Server &Server::set_error_handler_impl(Handler handler, +inline Server &Server::set_error_handler_core(Handler handler, std::false_type) { error_handler_ = [handler](const Request &req, Response &res) { handler(req, res); From 560854a9611ebe2361bebd7cc60b11a7e76ef2f9 Mon Sep 17 00:00:00 2001 From: Kent Date: Fri, 12 Apr 2024 11:28:21 +0800 Subject: [PATCH 0183/1222] Apply range header base on response status code (#1806) * Enable ignoring range header to generate customized response * Apply range header base on response status code --- httplib.h | 6 +++--- test/test.cc | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 4c446d91b6..4e79f11f2d 100644 --- a/httplib.h +++ b/httplib.h @@ -6521,7 +6521,7 @@ inline bool Server::dispatch_request(Request &req, Response &res, inline void Server::apply_ranges(const Request &req, Response &res, std::string &content_type, std::string &boundary) const { - if (req.ranges.size() > 1) { + if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) { auto it = res.headers.find("Content-Type"); if (it != res.headers.end()) { content_type = it->second; @@ -6539,7 +6539,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (res.body.empty()) { if (res.content_length_ > 0) { size_t length = 0; - if (req.ranges.empty()) { + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { length = res.content_length_; } else if (req.ranges.size() == 1) { auto offset_and_length = detail::get_range_offset_and_length( @@ -6568,7 +6568,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, } } } else { - if (req.ranges.empty()) { + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { ; } else if (req.ranges.size() == 1) { auto offset_and_length = diff --git a/test/test.cc b/test/test.cc index 71023f3f2c..eae7dae3eb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1890,6 +1890,11 @@ class ServerTest : public ::testing::Test { [&](const Request & /*req*/, Response &res) { res.set_content("abcdefg", "text/plain"); }) + .Get("/with-range-customized-response", + [&](const Request & /*req*/, Response &res) { + res.status = StatusCode::BadRequest_400; + res.set_content(JSON_DATA, "application/json"); + }) .Post("/chunked", [&](const Request &req, Response & /*res*/) { EXPECT_EQ(req.body, "dechunked post body"); @@ -3166,6 +3171,24 @@ TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); } +TEST_F(ServerTest, GetWithRangeCustomizedResponse) { + auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); + EXPECT_EQ(true, res->has_header("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(JSON_DATA, res->body); +} + +TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) { + auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}, {4, 5}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); + EXPECT_EQ(true, res->has_header("Content-Length")); + EXPECT_EQ(false, res->has_header("Content-Range")); + EXPECT_EQ(JSON_DATA, res->body); +} + TEST_F(ServerTest, Issue1772) { auto res = cli_.Get("/issue1772", {{make_range_header({{1000, -1}})}}); ASSERT_TRUE(res); From ce36b8a6e5ddd049a1a00cac61d9b8a37be3e75f Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Thu, 18 Apr 2024 23:19:54 +0900 Subject: [PATCH 0184/1222] Highlight notes using markdown features (#1820) --- README.md | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ebde68aafc..b2b4b9e776 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ A C++11 single-file header-only cross platform HTTP/HTTPS library. It's extremely easy to setup. Just include the **httplib.h** file in your code! -NOTE: This library uses 'blocking' socket I/O. If you are looking for a library with 'non-blocking' socket I/O, this is not the one that you want. +> [!IMPORTANT] +> This library uses 'blocking' socket I/O. If you are looking for a library with 'non-blocking' socket I/O, this is not the one that you want. Simple examples --------------- @@ -53,9 +54,11 @@ SSL Support SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. -NOTE: cpp-httplib currently supports only version 3.0 or later. Please see [this page](https://www.openssl.org/policies/releasestrat.html) to get more information. +> [!NOTE] +> cpp-httplib currently supports only version 3.0 or later. Please see [this page](https://www.openssl.org/policies/releasestrat.html) to get more information. -NOTE for macOS: cpp-httplib now can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`. +> [!TIP] +> For macOS: cpp-httplib now can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`. ```c++ #define CPPHTTPLIB_OPENSSL_SUPPORT @@ -76,7 +79,8 @@ cli.set_ca_cert_path("./ca-bundle.crt"); cli.enable_server_certificate_verification(false); ``` -NOTE: When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. +> [!NOTE] +> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. Server ------ @@ -190,7 +194,8 @@ The followings are built-in mappings: | webm | video/webm | zip | application/zip | | mp3 | audio/mp3 | wasm | application/wasm | -NOTE: These static file server methods are not thread-safe. +> [!WARNING] +> These static file server methods are not thread-safe. ### File request handler @@ -239,7 +244,8 @@ svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep) }); ``` -NOTE: if you don't provide the `catch (...)` block for a rethrown exception pointer, an uncaught exception will end up causing the server crash. Be careful! +> [!CAUTION] +> if you don't provide the `catch (...)` block for a rethrown exception pointer, an uncaught exception will end up causing the server crash. Be careful! ### Pre routing handler @@ -417,7 +423,8 @@ svr.set_idle_interval(0, 100000); // 100 milliseconds svr.set_payload_max_length(1024 * 1024 * 512); // 512MB ``` -NOTE: When the request body content type is 'www-form-urlencoded', the actual payload length shouldn't exceed `CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH`. +> [!NOTE] +> When the request body content type is 'www-form-urlencoded', the actual payload length shouldn't exceed `CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH`. ### Server-Sent Events @@ -496,7 +503,8 @@ int main(void) } ``` -NOTE: Constructor with scheme-host-port string is now supported! +> [!TIP] +> Constructor with scheme-host-port string is now supported! ```c++ httplib::Client cli("localhost"); @@ -704,7 +712,8 @@ cli.set_digest_auth("user", "pass"); cli.set_bearer_token_auth("token"); ``` -NOTE: OpenSSL is required for Digest Authentication. +> [!NOTE] +> OpenSSL is required for Digest Authentication. ### Proxy server support @@ -721,7 +730,8 @@ cli.set_proxy_digest_auth("user", "pass"); cli.set_proxy_bearer_token_auth("pass"); ``` -NOTE: OpenSSL is required for Digest Authentication. +> [!NOTE] +> OpenSSL is required for Digest Authentication. ### Range @@ -770,7 +780,8 @@ res->status; // 200 ### Use a specific network interface -NOTE: This feature is not available on Windows, yet. +> [!NOTE] +> This feature is not available on Windows, yet. ```cpp cli.set_interface("eth0"); // Interface name, IP address or host name @@ -859,9 +870,11 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32 #include ``` -NOTE: cpp-httplib officially supports only the latest Visual Studio. It might work with former versions of Visual Studio, but I can no longer verify it. Pull requests are always welcome for the older versions of Visual Studio unless they break the C++11 conformance. +> [!NOTE] +> cpp-httplib officially supports only the latest Visual Studio. It might work with former versions of Visual Studio, but I can no longer verify it. Pull requests are always welcome for the older versions of Visual Studio unless they break the C++11 conformance. -NOTE: Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin and MSYS2 including MinGW are neither supported nor tested. +> [!NOTE] +> Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin and MSYS2 including MinGW are neither supported nor tested. License ------- From 2bc550b2f0d33d7a774f6e9423de795c4c1ee152 Mon Sep 17 00:00:00 2001 From: rndm13 <93862532+rndm13@users.noreply.github.com> Date: Sun, 21 Apr 2024 23:13:41 +0000 Subject: [PATCH 0185/1222] Added progress to POST, PUT, PATCH and DELETE requests (#1821) * Added progress to POST, PUT, PATCH, DELETE requests * Added tests for post, put, patch, delete progress/cancellation * fix accidental infinite recursion in delete --------- Co-authored-by: rndm --- httplib.h | 321 +++++++++++++++++++++++++++++++++++++++++++++++---- test/test.cc | 316 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 617 insertions(+), 20 deletions(-) diff --git a/httplib.h b/httplib.h index 4e79f11f2d..a1acb48d0f 100644 --- a/httplib.h +++ b/httplib.h @@ -1148,10 +1148,18 @@ class ClientImpl { const std::string &content_type); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); Result Post(const std::string &path, const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1167,6 +1175,8 @@ class ClientImpl { Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const Headers &headers, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); Result Post(const std::string &path, const MultipartFormDataItems &items); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1181,10 +1191,18 @@ class ClientImpl { const std::string &content_type); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); Result Put(const std::string &path, const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); Result Put(const std::string &path, @@ -1199,6 +1217,8 @@ class ClientImpl { Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const Headers &headers, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); Result Put(const std::string &path, const MultipartFormDataItems &items); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1211,13 +1231,23 @@ class ClientImpl { Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type, Progress progress); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); Result Patch(const std::string &path, const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1235,13 +1265,24 @@ class ClientImpl { Result Delete(const std::string &path, const Headers &headers); Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); Result Delete(const std::string &path, const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); @@ -1456,7 +1497,7 @@ class ClientImpl { const Headers &headers, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type); + const std::string &content_type, Progress progress); ContentProviderWithoutLength get_multipart_content_provider( const std::string &boundary, const MultipartFormDataItems &items, const MultipartFormDataProviderItems &provider_items) const; @@ -1531,10 +1572,18 @@ class Client { const std::string &content_type); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); Result Post(const std::string &path, const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1550,6 +1599,8 @@ class Client { Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const Headers &headers, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); Result Post(const std::string &path, const MultipartFormDataItems &items); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1564,10 +1615,18 @@ class Client { const std::string &content_type); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); Result Put(const std::string &path, const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); Result Put(const std::string &path, @@ -1582,6 +1641,8 @@ class Client { Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const Headers &headers, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); Result Put(const std::string &path, const MultipartFormDataItems &items); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItems &items); @@ -1594,13 +1655,23 @@ class Client { Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type, Progress progress); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); Result Patch(const std::string &path, const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type); @@ -1618,13 +1689,24 @@ class Client { Result Delete(const std::string &path, const Headers &headers); Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); Result Delete(const std::string &path, const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); @@ -7435,11 +7517,12 @@ inline Result ClientImpl::send_with_content_provider( const std::string &method, const std::string &path, const Headers &headers, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type) { + const std::string &content_type, Progress progress) { Request req; req.method = method; req.headers = headers; req.path = path; + req.progress = progress; auto error = Error::Success; @@ -7735,14 +7818,22 @@ inline Result ClientImpl::Post(const std::string &path, inline Result ClientImpl::Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type) { - return Post(path, Headers(), body, content_length, content_type); + return Post(path, Headers(), body, content_length, content_type, nullptr); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type) { return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type); + nullptr, nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); } inline Result ClientImpl::Post(const std::string &path, const std::string &body, @@ -7750,12 +7841,27 @@ inline Result ClientImpl::Post(const std::string &path, const std::string &body, return Post(path, Headers(), body, content_type); } +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return Post(path, Headers(), body, content_type, progress); +} + inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, - content_type); + body.size(), nullptr, nullptr, content_type, + nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); } inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { @@ -7781,14 +7887,15 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const std::string &content_type) { return send_with_content_provider("POST", path, headers, nullptr, content_length, std::move(content_provider), - nullptr, content_type); + nullptr, content_type, nullptr); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type) { return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type); + std::move(content_provider), content_type, + nullptr); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, @@ -7797,6 +7904,13 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, return Post(path, headers, query, "application/x-www-form-urlencoded"); } +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + inline Result ClientImpl::Post(const std::string &path, const MultipartFormDataItems &items) { return Post(path, Headers(), items); @@ -7834,7 +7948,7 @@ ClientImpl::Post(const std::string &path, const Headers &headers, return send_with_content_provider( "POST", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type); + content_type, nullptr); } inline Result ClientImpl::Put(const std::string &path) { @@ -7851,7 +7965,15 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type) { return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type); + nullptr, nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); } inline Result ClientImpl::Put(const std::string &path, const std::string &body, @@ -7859,12 +7981,27 @@ inline Result ClientImpl::Put(const std::string &path, const std::string &body, return Put(path, Headers(), body, content_type); } +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return Put(path, Headers(), body, content_type, progress); +} + inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, - content_type); + body.size(), nullptr, nullptr, content_type, + nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); } inline Result ClientImpl::Put(const std::string &path, size_t content_length, @@ -7886,14 +8023,15 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const std::string &content_type) { return send_with_content_provider("PUT", path, headers, nullptr, content_length, std::move(content_provider), - nullptr, content_type); + nullptr, content_type, nullptr); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type) { return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type); + std::move(content_provider), content_type, + nullptr); } inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { @@ -7906,6 +8044,13 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, return Put(path, headers, query, "application/x-www-form-urlencoded"); } +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + inline Result ClientImpl::Put(const std::string &path, const MultipartFormDataItems &items) { return Put(path, Headers(), items); @@ -7943,7 +8088,7 @@ ClientImpl::Put(const std::string &path, const Headers &headers, return send_with_content_provider( "PUT", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type); + content_type, nullptr); } inline Result ClientImpl::Patch(const std::string &path) { return Patch(path, std::string(), std::string()); @@ -7955,12 +8100,26 @@ inline Result ClientImpl::Patch(const std::string &path, const char *body, return Patch(path, Headers(), body, content_length, content_type); } +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return Patch(path, Headers(), body, content_length, content_type, progress); +} + inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type) { + return Patch(path, headers, body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { return send_with_content_provider("PATCH", path, headers, body, content_length, nullptr, nullptr, - content_type); + content_type, progress); } inline Result ClientImpl::Patch(const std::string &path, @@ -7969,12 +8128,25 @@ inline Result ClientImpl::Patch(const std::string &path, return Patch(path, Headers(), body, content_type); } +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type, Progress progress) { + return Patch(path, Headers(), body, content_type, progress); +} + inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { + return Patch(path, headers, body, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { return send_with_content_provider("PATCH", path, headers, body.data(), - body.size(), nullptr, nullptr, - content_type); + body.size(), nullptr, nullptr, content_type, + progress); } inline Result ClientImpl::Patch(const std::string &path, size_t content_length, @@ -7996,14 +8168,15 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const std::string &content_type) { return send_with_content_provider("PATCH", path, headers, nullptr, content_length, std::move(content_provider), - nullptr, content_type); + nullptr, content_type, nullptr); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type) { return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type); + std::move(content_provider), content_type, + nullptr); } inline Result ClientImpl::Delete(const std::string &path) { @@ -8021,14 +8194,30 @@ inline Result ClientImpl::Delete(const std::string &path, const char *body, return Delete(path, Headers(), body, content_length, content_type); } +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return Delete(path, Headers(), body, content_length, content_type, progress); +} + inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type) { + return Delete(path, headers, body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { Request req; req.method = "DELETE"; req.headers = headers; req.path = path; + req.progress = progress; if (!content_type.empty()) { req.set_header("Content-Type", content_type); } req.body.assign(body, content_length); @@ -8042,6 +8231,14 @@ inline Result ClientImpl::Delete(const std::string &path, return Delete(path, Headers(), body.data(), body.size(), content_type); } +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Delete(path, Headers(), body.data(), body.size(), content_type, + progress); +} + inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, const std::string &body, @@ -8049,6 +8246,15 @@ inline Result ClientImpl::Delete(const std::string &path, return Delete(path, headers, body.data(), body.size(), content_type); } +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Delete(path, headers, body.data(), body.size(), content_type, + progress); +} + inline Result ClientImpl::Options(const std::string &path) { return Options(path, Headers()); } @@ -9129,15 +9335,30 @@ inline Result Client::Post(const std::string &path, const Headers &headers, const std::string &content_type) { return cli_->Post(path, headers, body, content_length, content_type); } +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Post(path, headers, body, content_length, content_type, + progress); +} inline Result Client::Post(const std::string &path, const std::string &body, const std::string &content_type) { return cli_->Post(path, body, content_type); } +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Post(path, body, content_type, progress); +} inline Result Client::Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { return cli_->Post(path, headers, body, content_type); } +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Post(path, headers, body, content_type, progress); +} inline Result Client::Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type) { @@ -9168,6 +9389,10 @@ inline Result Client::Post(const std::string &path, const Headers &headers, const Params ¶ms) { return cli_->Post(path, headers, params); } +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Post(path, headers, params, progress); +} inline Result Client::Post(const std::string &path, const MultipartFormDataItems &items) { return cli_->Post(path, items); @@ -9198,15 +9423,29 @@ inline Result Client::Put(const std::string &path, const Headers &headers, const std::string &content_type) { return cli_->Put(path, headers, body, content_length, content_type); } +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Put(path, headers, body, content_length, content_type, progress); +} inline Result Client::Put(const std::string &path, const std::string &body, const std::string &content_type) { return cli_->Put(path, body, content_type); } +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Put(path, body, content_type, progress); +} inline Result Client::Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { return cli_->Put(path, headers, body, content_type); } +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Put(path, headers, body, content_type, progress); +} inline Result Client::Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type) { @@ -9237,6 +9476,10 @@ inline Result Client::Put(const std::string &path, const Headers &headers, const Params ¶ms) { return cli_->Put(path, headers, params); } +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Put(path, headers, params, progress); +} inline Result Client::Put(const std::string &path, const MultipartFormDataItems &items) { return cli_->Put(path, items); @@ -9264,20 +9507,39 @@ inline Result Client::Patch(const std::string &path, const char *body, const std::string &content_type) { return cli_->Patch(path, body, content_length, content_type); } +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Patch(path, body, content_length, content_type, progress); +} inline Result Client::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type) { return cli_->Patch(path, headers, body, content_length, content_type); } +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Patch(path, headers, body, content_length, content_type, progress); +} inline Result Client::Patch(const std::string &path, const std::string &body, const std::string &content_type) { return cli_->Patch(path, body, content_type); } +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Patch(path, body, content_type, progress); +} inline Result Client::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { return cli_->Patch(path, headers, body, content_type); } +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Patch(path, headers, body, content_type, progress); +} inline Result Client::Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type) { @@ -9312,20 +9574,39 @@ inline Result Client::Delete(const std::string &path, const char *body, const std::string &content_type) { return cli_->Delete(path, body, content_length, content_type); } +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Delete(path, body, content_length, content_type, progress); +} inline Result Client::Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type) { return cli_->Delete(path, headers, body, content_length, content_type); } +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, progress); +} inline Result Client::Delete(const std::string &path, const std::string &body, const std::string &content_type) { return cli_->Delete(path, body, content_type); } +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Delete(path, body, content_type, progress); +} inline Result Client::Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type) { return cli_->Delete(path, headers, body, content_type); } +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Delete(path, headers, body, content_type, progress); +} inline Result Client::Options(const std::string &path) { return cli_->Options(path); } diff --git a/test/test.cc b/test/test.cc index eae7dae3eb..8fadce57aa 100644 --- a/test/test.cc +++ b/test/test.cc @@ -779,6 +779,322 @@ TEST(CancelTest, WithCancelLargePayload_Online) { EXPECT_EQ(Error::Canceled, res.error()); } +TEST(CancelTest, NoCancelPost) { + Server svr; + + svr.Post("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return true; }); + ASSERT_TRUE(res); + EXPECT_EQ("Hello World!", res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST(CancelTest, WithCancelSmallPayloadPost) { + Server svr; + + svr.Post("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, WithCancelLargePayloadPost) { + Server svr; + + svr.Post("/", [&](const Request & /*req*/, Response &res) { + res.set_content(LARGE_DATA, "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Post("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, NoCancelPut) { + Server svr; + + svr.Put("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return true; }); + ASSERT_TRUE(res); + EXPECT_EQ("Hello World!", res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST(CancelTest, WithCancelSmallPayloadPut) { + Server svr; + + svr.Put("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, WithCancelLargePayloadPut) { + Server svr; + + svr.Put("/", [&](const Request & /*req*/, Response &res) { + res.set_content(LARGE_DATA, "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Put("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, NoCancelPatch) { + Server svr; + + svr.Patch("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return true; }); + ASSERT_TRUE(res); + EXPECT_EQ("Hello World!", res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST(CancelTest, WithCancelSmallPayloadPatch) { + Server svr; + + svr.Patch("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, WithCancelLargePayloadPatch) { + Server svr; + + svr.Patch("/", [&](const Request & /*req*/, Response &res) { + res.set_content(LARGE_DATA, "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Patch("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, NoCancelDelete) { + Server svr; + + svr.Delete("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return true; }); + ASSERT_TRUE(res); + EXPECT_EQ("Hello World!", res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST(CancelTest, WithCancelSmallPayloadDelete) { + Server svr; + + svr.Delete("/", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + +TEST(CancelTest, WithCancelLargePayloadDelete) { + Server svr; + + svr.Delete("/", [&](const Request & /*req*/, Response &res) { + res.set_content(LARGE_DATA, "text/plain"); + }); + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_connection_timeout(std::chrono::seconds(5)); + + auto res = + cli.Delete("/", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return false; }); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::Canceled, res.error()); +} + TEST(BaseAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; From f10720ed6974cd26675cfaad359007a991c32600 Mon Sep 17 00:00:00 2001 From: KTGH Date: Sun, 21 Apr 2024 19:14:12 -0400 Subject: [PATCH 0186/1222] Move httplibConf.cmake.in & install readme/license (#1826) * Move httplibConfig.cmake.in to cmake dir Just makes more sense to put it there I suppose. * Cmake install README & License Seems to make sense since you might already do this as a package manager, or an end user might want them anyways. The locations are just based on standard Linux locations using GNUInstallDirs, so it should be sane on other machines too. --- CMakeLists.txt | 7 ++++++- httplibConfig.cmake.in => cmake/httplibConfig.cmake.in | 0 2 files changed, 6 insertions(+), 1 deletion(-) rename httplibConfig.cmake.in => cmake/httplibConfig.cmake.in (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94dcae90d0..96d755a261 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -245,7 +245,7 @@ set(_TARGET_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") include(CMakePackageConfigHelpers) # Configures the meta-file httplibConfig.cmake.in to replace variables with paths/values/etc. -configure_package_config_file("${PROJECT_NAME}Config.cmake.in" +configure_package_config_file("cmake/${PROJECT_NAME}Config.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION "${_TARGET_INSTALL_CMAKEDIR}" # Passes the includedir install path @@ -293,6 +293,11 @@ if(HTTPLIB_INSTALL) NAMESPACE ${PROJECT_NAME}:: DESTINATION ${_TARGET_INSTALL_CMAKEDIR} ) + + # Install documentation & license + # ex: /usr/share/doc/httplib/README.md and /usr/share/licenses/httplib/LICENSE + install(FILES "README.md" DESTINATION "${CMAKE_INSTALL_DOCDIR}") + install(FILES "LICENSE" DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME}") endif() if(HTTPLIB_TEST) diff --git a/httplibConfig.cmake.in b/cmake/httplibConfig.cmake.in similarity index 100% rename from httplibConfig.cmake.in rename to cmake/httplibConfig.cmake.in From 3b6597bba913d51161383657829b7e644e59c006 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Mon, 22 Apr 2024 08:17:14 +0900 Subject: [PATCH 0187/1222] Fix query parsing when value has `=` characters (#1822) * Implement string divider to replace splitter * Divide query string in half * Add a test case for query values containing the '=' character * Add test cases for string divider * Fix warnings --- httplib.h | 84 +++++++++++++++++++++++++++++---------------- test/test.cc | 96 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 144 insertions(+), 36 deletions(-) diff --git a/httplib.h b/httplib.h index a1acb48d0f..c7449cd009 100644 --- a/httplib.h +++ b/httplib.h @@ -2178,6 +2178,16 @@ void read_file(const std::string &path, std::string &out); std::string trim_copy(const std::string &s); +void divide( + const char *data, std::size_t size, char d, + std::function + fn); + +void divide( + const std::string &str, char d, + std::function + fn); + void split(const char *b, const char *e, char d, std::function fn); @@ -2201,6 +2211,8 @@ const char *get_header_value(const Headers &headers, const std::string &key, std::string params_to_query_str(const Params ¶ms); +void parse_query_text(const char *data, std::size_t size, Params ¶ms); + void parse_query_text(const std::string &s, Params ¶ms); bool parse_multipart_boundary(const std::string &content_type, @@ -2669,6 +2681,27 @@ inline std::string trim_double_quotes_copy(const std::string &s) { return s; } +inline void +divide(const char *data, std::size_t size, char d, + std::function + fn) { + const auto it = std::find(data, data + size, d); + const auto found = static_cast(it != data + size); + const auto lhs_data = data; + const auto lhs_size = static_cast(it - data); + const auto rhs_data = it + found; + const auto rhs_size = size - lhs_size - found; + + fn(lhs_data, lhs_size, rhs_data, rhs_size); +} + +inline void +divide(const std::string &str, char d, + std::function + fn) { + divide(str.data(), str.size(), d, std::move(fn)); +} + inline void split(const char *b, const char *e, char d, std::function fn) { return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); @@ -4392,22 +4425,22 @@ inline std::string params_to_query_str(const Params ¶ms) { return query; } -inline void parse_query_text(const std::string &s, Params ¶ms) { +inline void parse_query_text(const char *data, std::size_t size, + Params ¶ms) { std::set cache; - split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) { + split(data, data + size, '&', [&](const char *b, const char *e) { std::string kv(b, e); if (cache.find(kv) != cache.end()) { return; } - cache.insert(kv); + cache.insert(std::move(kv)); std::string key; std::string val; - split(b, e, '=', [&](const char *b2, const char *e2) { - if (key.empty()) { - key.assign(b2, e2); - } else { - val.assign(b2, e2); - } - }); + divide(b, static_cast(e - b), '=', + [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, + std::size_t rhs_size) { + key.assign(lhs_data, lhs_size); + val.assign(rhs_data, rhs_size); + }); if (!key.empty()) { params.emplace(decode_url(key, true), decode_url(val, true)); @@ -4415,6 +4448,10 @@ inline void parse_query_text(const std::string &s, Params ¶ms) { }); } +inline void parse_query_text(const std::string &s, Params ¶ms) { + parse_query_text(s.data(), s.size(), params); +} + inline bool parse_multipart_boundary(const std::string &content_type, std::string &boundary) { auto boundary_keyword = "boundary="; @@ -6072,26 +6109,13 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { } } - size_t count = 0; - - detail::split(req.target.data(), req.target.data() + req.target.size(), '?', - 2, [&](const char *b, const char *e) { - switch (count) { - case 0: - req.path = detail::decode_url(std::string(b, e), false); - break; - case 1: { - if (e - b > 0) { - detail::parse_query_text(std::string(b, e), req.params); - } - break; - } - default: break; - } - count++; - }); - - if (count > 2) { return false; } + detail::divide(req.target, '?', + [&](const char *lhs_data, std::size_t lhs_size, + const char *rhs_data, std::size_t rhs_size) { + req.path = detail::decode_url( + std::string(lhs_data, lhs_size), false); + detail::parse_query_text(rhs_data, rhs_size, req.params); + }); } return true; diff --git a/test/test.cc b/test/test.cc index 8fadce57aa..dce82abe49 100644 --- a/test/test.cc +++ b/test/test.cc @@ -116,6 +116,76 @@ TEST(TrimTests, TrimStringTests) { EXPECT_TRUE(detail::trim_copy("").empty()); } +TEST(DivideTest, DivideStringTests) { + auto divide = [](const std::string &str, char d) { + std::string lhs; + std::string rhs; + + detail::divide(str, d, + [&](const char *lhs_data, std::size_t lhs_size, + const char *rhs_data, std::size_t rhs_size) { + lhs.assign(lhs_data, lhs_size); + rhs.assign(rhs_data, rhs_size); + }); + + return std::make_pair(std::move(lhs), std::move(rhs)); + }; + + { + const auto res = divide("", '='); + EXPECT_EQ(res.first, ""); + EXPECT_EQ(res.second, ""); + } + + { + const auto res = divide("=", '='); + EXPECT_EQ(res.first, ""); + EXPECT_EQ(res.second, ""); + } + + { + const auto res = divide(" ", '='); + EXPECT_EQ(res.first, " "); + EXPECT_EQ(res.second, ""); + } + + { + const auto res = divide("a", '='); + EXPECT_EQ(res.first, "a"); + EXPECT_EQ(res.second, ""); + } + + { + const auto res = divide("a=", '='); + EXPECT_EQ(res.first, "a"); + EXPECT_EQ(res.second, ""); + } + + { + const auto res = divide("=b", '='); + EXPECT_EQ(res.first, ""); + EXPECT_EQ(res.second, "b"); + } + + { + const auto res = divide("a=b", '='); + EXPECT_EQ(res.first, "a"); + EXPECT_EQ(res.second, "b"); + } + + { + const auto res = divide("a=b=", '='); + EXPECT_EQ(res.first, "a"); + EXPECT_EQ(res.second, "b="); + } + + { + const auto res = divide("a=b=c", '='); + EXPECT_EQ(res.first, "a"); + EXPECT_EQ(res.second, "b=c"); + } +} + TEST(SplitTest, ParseQueryString) { string s = "key1=val1&key2=val2&key3=val3"; Params dic; @@ -156,14 +226,28 @@ TEST(SplitTest, ParseInvalidQueryTests) { } TEST(ParseQueryTest, ParseQueryString) { - string s = "key1=val1&key2=val2&key3=val3"; - Params dic; + { + std::string s = "key1=val1&key2=val2&key3=val3"; + Params dic; - detail::parse_query_text(s, dic); + detail::parse_query_text(s, dic); - EXPECT_EQ("val1", dic.find("key1")->second); - EXPECT_EQ("val2", dic.find("key2")->second); - EXPECT_EQ("val3", dic.find("key3")->second); + EXPECT_EQ("val1", dic.find("key1")->second); + EXPECT_EQ("val2", dic.find("key2")->second); + EXPECT_EQ("val3", dic.find("key3")->second); + } + + { + std::string s = "key1&key2=val1&key3=val1=val2&key4=val1=val2=val3"; + Params dic; + + detail::parse_query_text(s, dic); + + EXPECT_EQ("", dic.find("key1")->second); + EXPECT_EQ("val1", dic.find("key2")->second); + EXPECT_EQ("val1=val2", dic.find("key3")->second); + EXPECT_EQ("val1=val2=val3", dic.find("key4")->second); + } } TEST(ParamsToQueryTest, ConvertParamsToQuery) { From 50fce538c685e476039f1da473443a7bbc3f50ca Mon Sep 17 00:00:00 2001 From: Karen Dombroski Date: Wed, 1 May 2024 21:41:44 +1200 Subject: [PATCH 0188/1222] threadsafe CLOEXEC on platforms that support it SOCK_CLOEXEC is a flag available on some platforms to enable creation of sockets with CLOEXEC already set --- httplib.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index c7449cd009..5e2eff2adc 100644 --- a/httplib.h +++ b/httplib.h @@ -3216,7 +3216,12 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, const auto addrlen = host.length(); if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } +#ifdef SOCK_CLOEXEC + auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, hints.ai_protocol); +#else auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); +#endif + if (sock != INVALID_SOCKET) { sockaddr_un addr{}; addr.sun_family = AF_UNIX; @@ -3226,7 +3231,10 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_addrlen = static_cast( sizeof(addr) - sizeof(addr.sun_path) + addrlen); +#ifndef SOCK_CLOEXEC fcntl(sock, F_SETFD, FD_CLOEXEC); +#endif + if (socket_options) { socket_options(sock); } if (!bind_or_connect(sock, hints)) { @@ -3271,11 +3279,17 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); } #else + +#ifdef SOCK_CLOEXEC + auto sock = socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); +#else auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); +#endif + #endif if (sock == INVALID_SOCKET) { continue; } -#ifndef _WIN32 +#if !defined _WIN32 && !defined SOCK_CLOEXEC if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { close_socket(sock); continue; From fb739dbaecf38c6b5e4b794d10f4c4edb83ea52c Mon Sep 17 00:00:00 2001 From: Karen Dombroski Date: Wed, 1 May 2024 21:43:38 +1200 Subject: [PATCH 0189/1222] threadsafe accept on windows, linux * Windows has WSAAccept() which will create sockets inheriting flags from the server socket * Linux has accept4() which has a flags argument supporting SOCK_CLOEXEC --- httplib.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/httplib.h b/httplib.h index 5e2eff2adc..f1b78910d2 100644 --- a/httplib.h +++ b/httplib.h @@ -6484,7 +6484,15 @@ inline bool Server::listen_internal() { #ifndef _WIN32 } #endif + +#if defined _WIN32 + // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, OVERLAPPED + socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); +#elif defined __linux__ + socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); +#else socket_t sock = accept(svr_sock_, nullptr, nullptr); +#endif if (sock == INVALID_SOCKET) { if (errno == EMFILE) { From 05f9f83240a388239c088f067a1e3e6af2a7bf51 Mon Sep 17 00:00:00 2001 From: Pavel P Date: Fri, 17 May 2024 05:56:06 +0500 Subject: [PATCH 0190/1222] Avoid unreferenced formal parameter warning in get_range_offset_and_length (#1838) Release builds result in the following warning because `content_length` param was used only inside asserts: 1> cpp-httplib\httplib.h(4933,45): warning C4100: 'content_length': unreferenced formal parameter --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index c7449cd009..ba6e666957 100644 --- a/httplib.h +++ b/httplib.h @@ -4935,7 +4935,7 @@ get_range_offset_and_length(Range r, size_t content_length) { assert(0 <= r.first && r.first < static_cast(content_length)); assert(r.first <= r.second && r.second < static_cast(content_length)); - + (void)(content_length); return std::make_pair(r.first, static_cast(r.second - r.first) + 1); } From 25b1e0d906e33ae328c5bdf89e586b1508ee4c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Chlup?= <30288319+jajik@users.noreply.github.com> Date: Sun, 26 May 2024 14:24:29 +0200 Subject: [PATCH 0191/1222] Tweak CI & fix macOS prefix (#1843) * Use brew prefix or given one * Polish CI workflow file --- .github/workflows/test.yaml | 42 +++++++++++++++++++------------------ test/Makefile | 3 +-- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 22fd72ef71..531fd4dccd 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -3,39 +3,41 @@ name: test on: [push, pull_request] jobs: - build: - runs-on: ${{ matrix.os }} + ubuntu: + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: install brotli + run: sudo apt-get update && sudo apt-get install -y libbrotli-dev + - name: build and run tests + run: cd test && make -j4 + - name: run fuzz test target + run: cd test && make fuzz_test - strategy: - matrix: - os: [macOS-latest, ubuntu-latest, windows-latest] + macos: + runs-on: macos-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: build and run tests + run: | + cd test && make -j2 + windows: + runs-on: windows-latest steps: - name: prepare git for checkout on windows - if: matrix.os == 'windows-latest' run: | git config --global core.autocrlf false git config --global core.eol lf - name: checkout uses: actions/checkout@v4 - - name: install brotli library on ubuntu - if: matrix.os == 'ubuntu-latest' - run: sudo apt update && sudo apt-get install -y libbrotli-dev - - name: install brotli library on macOS - if: matrix.os == 'macOS-latest' - run: brew install brotli - - name: make - if: matrix.os != 'windows-latest' - run: cd test && make -j2 - - name: check fuzz test target - if: matrix.os == 'ubuntu-latest' - run: cd test && make fuzz_test - name: setup msbuild on windows - if: matrix.os == 'windows-latest' uses: microsoft/setup-msbuild@v2 - name: make-windows - if: matrix.os == 'windows-latest' run: | cd test msbuild.exe test.sln /verbosity:minimal /t:Build "/p:Configuration=Release;Platform=x64" x64\Release\test.exe + diff --git a/test/Makefile b/test/Makefile index c4dc4f1b36..5468488e4f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,8 +1,7 @@ CXX = clang++ CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address -PREFIX = /usr/local -#PREFIX = $(shell brew --prefix) +PREFIX ?= $(shell brew --prefix) OPENSSL_DIR = $(PREFIX)/opt/openssl@3 OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto From 98cc1ec344da3250e7dc8135853d0a5bef52d788 Mon Sep 17 00:00:00 2001 From: Sean Quinn Date: Sun, 26 May 2024 05:57:07 -0700 Subject: [PATCH 0192/1222] Allow hex for ip6 literal addr, fix #1800 (#1830) * Allow hex for ip6 literal addr, fix #1800 * Add UT for ipv6 + Universal client implementation * add /n at EOF --- httplib.h | 4 +++- test/test.cc | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ba6e666957..a7a561333e 100644 --- a/httplib.h +++ b/httplib.h @@ -9213,7 +9213,7 @@ inline Client::Client(const std::string &scheme_host_port, const std::string &client_cert_path, const std::string &client_key_path) { const static std::regex re( - R"((?:([a-z]+):\/\/)?(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); + R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); std::smatch m; if (std::regex_match(scheme_host_port, m, re)) { @@ -9250,6 +9250,8 @@ inline Client::Client(const std::string &scheme_host_port, client_key_path); } } else { + // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) + // if port param below changes. cli_ = detail::make_unique(scheme_host_port, 80, client_cert_path, client_key_path); } diff --git a/test/test.cc b/test/test.cc index dce82abe49..aab9db057e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7373,3 +7373,18 @@ TEST(PathParamsTest, SequenceOfParams) { EXPECT_EQ(request.path_params, expected_params); } + +TEST(UniversalClientImplTest, Ipv6LiteralAddress) { + // If ipv6 regex working, regex match codepath is taken. + // else port will default to 80 in Client impl + int clientImplMagicPort = 80; + int port = 4321; + // above ports must be different to avoid false negative + EXPECT_NE(clientImplMagicPort, port); + + std::string ipV6TestURL = "http://[ff06::c3]"; + + Client cli(ipV6TestURL + ":" + std::to_string(port), CLIENT_CERT_FILE, + CLIENT_PRIVATE_KEY_FILE); + EXPECT_EQ(cli.port(), port); +} From d44031615d7ef548c3d78a820035848cb5990288 Mon Sep 17 00:00:00 2001 From: Rainer Schielke <56628927+RainerSchielke@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:37:40 +0200 Subject: [PATCH 0193/1222] New function SSLServer::update_certs. Allows to update certificates while server is running (#1827) * New function SSLServer::update_certs. Allows to update certificates while server is running * New function SSLServer::update_certs. Added unit test --------- Co-authored-by: CEU\schielke --- httplib.h | 16 +++++++++++++++ test/test.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/httplib.h b/httplib.h index a7a561333e..69a34d6046 100644 --- a/httplib.h +++ b/httplib.h @@ -1819,6 +1819,9 @@ class SSLServer : public Server { bool is_valid() const override; SSL_CTX *ssl_context() const; + + void update_certs (X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); private: bool process_and_close_socket(socket_t sock) override; @@ -8753,6 +8756,19 @@ inline bool SSLServer::is_valid() const { return ctx_; } inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } +inline void SSLServer::update_certs (X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + + std::lock_guard guard(ctx_mutex_); + + SSL_CTX_use_certificate (ctx_, cert); + SSL_CTX_use_PrivateKey (ctx_, private_key); + + if (client_ca_cert_store != nullptr) { + SSL_CTX_set_cert_store (ctx_, client_ca_cert_store); + } +} + inline bool SSLServer::process_and_close_socket(socket_t sock) { auto ssl = detail::ssl_new( sock, ctx_, ctx_mutex_, diff --git a/test/test.cc b/test/test.cc index aab9db057e..7fe39adc33 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1747,6 +1747,64 @@ TEST(BindServerTest, BindAndListenSeparatelySSLEncryptedKey) { } #endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +X509* readCertificate (const std::string& strFileName) { + std::ifstream inStream (strFileName); + std::string strCertPEM ((std::istreambuf_iterator(inStream)), std::istreambuf_iterator()); + + if (strCertPEM.empty ()) return (nullptr); + + BIO* pbCert = BIO_new (BIO_s_mem ()); + BIO_write (pbCert, strCertPEM.c_str (), (int)strCertPEM.size ()); + X509* pCert = PEM_read_bio_X509 (pbCert, NULL, 0, NULL); + BIO_free (pbCert); + + return (pCert); +} + +EVP_PKEY* readPrivateKey (const std::string& strFileName) { + std::ifstream inStream (strFileName); + std::string strPrivateKeyPEM ((std::istreambuf_iterator(inStream)), std::istreambuf_iterator()); + + if (strPrivateKeyPEM.empty ()) return (nullptr); + + BIO* pbPrivKey = BIO_new (BIO_s_mem ()); + BIO_write (pbPrivKey, strPrivateKeyPEM.c_str (), (int) strPrivateKeyPEM.size ()); + EVP_PKEY* pPrivateKey = PEM_read_bio_PrivateKey (pbPrivKey, NULL, NULL, NULL); + BIO_free (pbPrivKey); + + return (pPrivateKey); +} + +TEST(BindServerTest, UpdateCerts) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); + int port = svr.bind_to_any_port("0.0.0.0"); + ASSERT_TRUE(svr.is_valid()); + ASSERT_TRUE(port > 0); + + X509* cert = readCertificate (SERVER_CERT_FILE); + X509* ca_cert = readCertificate (CLIENT_CA_CERT_FILE); + EVP_PKEY* key = readPrivateKey (SERVER_PRIVATE_KEY_FILE); + + ASSERT_TRUE(cert != nullptr); + ASSERT_TRUE(ca_cert != nullptr); + ASSERT_TRUE(key != nullptr); + + X509_STORE* cert_store = X509_STORE_new (); + + X509_STORE_add_cert (cert_store, ca_cert); + + svr.update_certs (cert, key, cert_store); + + ASSERT_TRUE(svr.is_valid()); + svr.stop(); + + X509_free (cert); + X509_free (ca_cert); + EVP_PKEY_free (key); +} +#endif + TEST(ErrorHandlerTest, ContentLength) { Server svr; From 67fd7e3d0950eaee85ddbcfe51a236a80b38fb04 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Tue, 11 Jun 2024 02:18:19 +0900 Subject: [PATCH 0194/1222] Change library name to cpp-httplib (#1854) --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 96d755a261..e27481bc5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -194,6 +194,7 @@ if(HTTPLIB_COMPILE) PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} SOVERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}" + OUTPUT_NAME cpp-httplib ) else() # This is for header-only. From 8438df4a953da398fc794387618a3c9676b2ba7e Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 11 Jun 2024 18:20:22 -0400 Subject: [PATCH 0195/1222] Release v0.16.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 69a34d6046..062f66b860 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.15.3" +#define CPPHTTPLIB_VERSION "0.16.0" /* * Configuration From c1a09daf153a4a5f7e6824a50524b483dd508c03 Mon Sep 17 00:00:00 2001 From: Rainer Schielke <56628927+RainerSchielke@users.noreply.github.com> Date: Fri, 14 Jun 2024 21:40:03 +0200 Subject: [PATCH 0196/1222] avoid memory leaks if linked with static openssl libs (#1857) * New function SSLServer::update_certs. Allows to update certificates while server is running * New function SSLServer::update_certs. Added unit test * avoid memory leaks if linked with static openssl libs --------- Co-authored-by: CEU\schielke --- httplib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/httplib.h b/httplib.h index 062f66b860..b1b3f7d538 100644 --- a/httplib.h +++ b/httplib.h @@ -726,6 +726,10 @@ class ThreadPool final : public TaskQueue { assert(true == static_cast(fn)); fn(); } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + OPENSSL_thread_stop (); +#endif } ThreadPool &pool_; From 0b657d28cfeaf2b799d983094d04c0db95532c08 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 14 Jun 2024 18:29:34 -0400 Subject: [PATCH 0197/1222] Added example/one_time_request.cc. --- .gitignore | 1 + example/Makefile | 6 ++-- example/one_time_request.cc | 56 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 example/one_time_request.cc diff --git a/.gitignore b/.gitignore index 0e11163821..59be00ebf4 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ test/*.srl *.swp +build/ Debug Release *.vcxproj.user diff --git a/example/Makefile b/example/Makefile index 64a35885f2..7a049ced87 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,8 +1,7 @@ #CXX = clang++ CXXFLAGS = -O2 -std=c++11 -I.. -Wall -Wextra -pthread -PREFIX = /usr/local -#PREFIX = $(shell brew --prefix) +PREFIX ?= $(shell brew --prefix) OPENSSL_DIR = $(PREFIX)/opt/openssl@3 OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto @@ -51,6 +50,9 @@ ssecli : ssecli.cc ../httplib.h Makefile benchmark : benchmark.cc ../httplib.h Makefile $(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) +one_time_request : one_time_request.cc ../httplib.h Makefile + $(CXX) -o one_time_request $(CXXFLAGS) one_time_request.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) + pem: openssl genrsa 2048 > key.pem openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem diff --git a/example/one_time_request.cc b/example/one_time_request.cc new file mode 100644 index 0000000000..9a8ac3450a --- /dev/null +++ b/example/one_time_request.cc @@ -0,0 +1,56 @@ +#include +#include + +using namespace httplib; + +const char *HOST = "localhost"; +const int PORT = 1234; + +void one_time_request_server(const char *label) { + std::thread th; + Server svr; + + svr.Get("/hi", [&](const Request & /*req*/, Response &res) { + res.set_content(std::string("Hello from ") + label, "text/plain"); + + // Stop server + th = std::thread([&]() { svr.stop(); }); + }); + + svr.listen(HOST, PORT); + th.join(); + + std::cout << label << " ended..." << std::endl; +} + +void send_request(const char *label) { + Client cli(HOST, PORT); + + std::cout << "Send " << label << " request" << std::endl; + auto res = cli.Get("/hi"); + + if (res) { + std::cout << res->body << std::endl; + } else { + std::cout << "Request error: " + to_string(res.error()) << std::endl; + } +} + +int main(void) { + auto th1 = std::thread([&]() { one_time_request_server("Server #1"); }); + auto th2 = std::thread([&]() { one_time_request_server("Server #2"); }); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + send_request("1st"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + send_request("2nd"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + send_request("3rd"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + th1.join(); + th2.join(); +} From 9e4f93d87e744c9038554d19f3837980959093e2 Mon Sep 17 00:00:00 2001 From: Zhenlin Huang Date: Mon, 17 Jun 2024 23:44:51 +0800 Subject: [PATCH 0198/1222] Allow hex for ipv6 literal addr in redirect (#1859) Co-authored-by: jaredhuang --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index b1b3f7d538..0085a39aba 100644 --- a/httplib.h +++ b/httplib.h @@ -7275,7 +7275,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { if (location.empty()) { return false; } const static std::regex re( - R"((?:(https?):)?(?://(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); + R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); std::smatch m; if (!std::regex_match(location, m, re)) { return false; } From bdefdce1aecc8dcf6f16e6023a90bb7752c08c70 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Sun, 23 Jun 2024 23:49:00 +0200 Subject: [PATCH 0199/1222] test: fix GetRangeWithMaxLongLength on 32 bit machines (#1867) The test used the hardcoded long value for 64 bit machines even on 32 bit ones, leading to test failures. With this patch the max long length is obtained using std::numeric_limits::max(). Thanks to q2a3z for the hint! Fixes: https://github.com/yhirose/cpp-httplib/issues/1795 --- test/test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 7fe39adc33..855988da61 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -3474,7 +3475,7 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { TEST_F(ServerTest, GetRangeWithMaxLongLength) { auto res = - cli_.Get("/with-range", {{"Range", "bytes=0-9223372036854775807"}}); + cli_.Get("/with-range", {{"Range", "bytes=0-" + std::to_string(std::numeric_limits::max())}}); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); From 388a8c007c96857b36901f3801f6572362ad6b17 Mon Sep 17 00:00:00 2001 From: Daniel Ludwig Date: Mon, 24 Jun 2024 21:13:37 +0200 Subject: [PATCH 0200/1222] Fix build on Windows with no WINAPI_PARTITION_APP support (#1865) --- httplib.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/httplib.h b/httplib.h index 0085a39aba..616b9141ad 100644 --- a/httplib.h +++ b/httplib.h @@ -2832,15 +2832,25 @@ inline bool mmap::open(const char *path) { if (!::GetFileSizeEx(hFile_, &size)) { return false; } size_ = static_cast(size.QuadPart); +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); +#else + hMapping_ = + ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart, + size.LowPart, NULL); +#endif if (hMapping_ == NULL) { close(); return false; } +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); +#else + addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); +#endif #else fd_ = ::open(path, O_RDONLY); if (fd_ == -1) { return false; } From 177d8420a1467554d0d207bc1a19c66d41bc3f38 Mon Sep 17 00:00:00 2001 From: Hlado <47942366+Hlado@users.noreply.github.com> Date: Sun, 30 Jun 2024 18:16:48 +0300 Subject: [PATCH 0201/1222] Added .gitattributes file to prevent git from changing line endings (#1872) of text files using as data for tests. --- .gitattributes | 2 ++ .gitignore | 1 + 2 files changed, 3 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..f8184834e9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +/test/www*/dir/*.html text eol=lf +/test/www*/dir/*.txt text eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore index 59be00ebf4..065648162c 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ Release ipch *.dSYM .* +!/.gitattributes !/.travis.yml From 8cd0ed05096e5b446ad1d8818ae68295b49541ea Mon Sep 17 00:00:00 2001 From: Hlado <47942366+Hlado@users.noreply.github.com> Date: Sun, 30 Jun 2024 18:17:00 +0300 Subject: [PATCH 0202/1222] Added move assignment operator to Client class. (#1873) --- httplib.h | 1 + test/test.cc | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 616b9141ad..ceb5cdf7ae 100644 --- a/httplib.h +++ b/httplib.h @@ -1530,6 +1530,7 @@ class Client { const std::string &client_key_path); Client(Client &&) = default; + Client &operator=(Client &&) = default; ~Client(); diff --git a/test/test.cc b/test/test.cc index 855988da61..446e7f9cc4 100644 --- a/test/test.cc +++ b/test/test.cc @@ -54,11 +54,17 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files, #endif } -TEST(ConstructorTest, MoveConstructible) { +TEST(ClientTest, MoveConstructible) { EXPECT_FALSE(std::is_copy_constructible::value); EXPECT_TRUE(std::is_nothrow_move_constructible::value); } +TEST(ClientTest, MoveAssignable) +{ + EXPECT_FALSE(std::is_copy_assignable::value); + EXPECT_TRUE(std::is_nothrow_move_assignable::value); +} + #ifdef _WIN32 TEST(StartupTest, WSAStartup) { WSADATA wsaData; From c8bcaf8a913f1ac087b076488e0cbbc272cccd19 Mon Sep 17 00:00:00 2001 From: mol123 <60134413+mol123@users.noreply.github.com> Date: Tue, 2 Jul 2024 16:58:40 +0200 Subject: [PATCH 0203/1222] Fix build when targeting Windows 7 as platform. (#1869) * Fix build when targeting Windows 7 as platform. This change makes more of the code introduced in https://github.com/yhirose/cpp-httplib/pull/1775 conditional on feature macros. `CreateFile2`, `CreateFileMappingFromApp` and `MapViewOfFileFromApp` are available only starting from Windows 8. * https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfile2 * https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-createfilemappingfromapp * https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffilefromapp * Update feature macros used and use `GetFileSizeEx` conditionally. --- httplib.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index ceb5cdf7ae..d170f3226e 100644 --- a/httplib.h +++ b/httplib.h @@ -2824,16 +2824,29 @@ inline bool mmap::open(const char *path) { wpath += path[i]; } +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8) hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL); +#else + hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); +#endif if (hFile_ == INVALID_HANDLE_VALUE) { return false; } +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES) LARGE_INTEGER size{}; if (!::GetFileSizeEx(hFile_, &size)) { return false; } size_ = static_cast(size.QuadPart); +#else + DWORD sizeHigh; + DWORD sizeLow; + sizeLow = ::GetFileSize(hFile_, &sizeHigh); + if (sizeLow == INVALID_FILE_SIZE) { return false; } + size_ = (static_cast(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow; +#endif -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8) hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); #else @@ -2847,7 +2860,7 @@ inline bool mmap::open(const char *path) { return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8) addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); #else addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); From 6a848b1a16437fe216cb26a14ee9fcb5c5785464 Mon Sep 17 00:00:00 2001 From: hanslivingstone <20542150+hanslivingstone@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:18:33 -0700 Subject: [PATCH 0204/1222] Require a minimum of TLS 1.2 (#1889) TLS 1. is deprecated: https://www.ietf.org/rfc/rfc8996.html --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index d170f3226e..c7c395fa0e 100644 --- a/httplib.h +++ b/httplib.h @@ -8718,7 +8718,7 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); if (private_key_password != nullptr && (private_key_password[0] != '\0')) { SSL_CTX_set_default_passwd_cb_userdata( @@ -8750,7 +8750,7 @@ inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); if (SSL_CTX_use_certificate(ctx_, cert) != 1 || SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { From ed0719f2bcb5fd7a3f7a52f304316a1c20851fbb Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 6 Aug 2024 07:20:05 -0400 Subject: [PATCH 0205/1222] Code format --- httplib.h | 75 +++++++++++++++++++++++++++------------------- test/test.cc | 85 +++++++++++++++++++++++++++------------------------- 2 files changed, 90 insertions(+), 70 deletions(-) diff --git a/httplib.h b/httplib.h index c7c395fa0e..2aefc4bb76 100644 --- a/httplib.h +++ b/httplib.h @@ -728,7 +728,7 @@ class ThreadPool final : public TaskQueue { } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - OPENSSL_thread_stop (); + OPENSSL_thread_stop(); #endif } @@ -1824,9 +1824,9 @@ class SSLServer : public Server { bool is_valid() const override; SSL_CTX *ssl_context() const; - - void update_certs (X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); + + void update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); private: bool process_and_close_socket(socket_t sock) override; @@ -2824,7 +2824,9 @@ inline bool mmap::open(const char *path) { wpath += path[i]; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ + WINAPI_PARTITION_GAMES) && \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN8) hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL); #else @@ -2834,7 +2836,8 @@ inline bool mmap::open(const char *path) { if (hFile_ == INVALID_HANDLE_VALUE) { return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | WINAPI_PARTITION_GAMES) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ + WINAPI_PARTITION_GAMES) LARGE_INTEGER size{}; if (!::GetFileSizeEx(hFile_, &size)) { return false; } size_ = static_cast(size.QuadPart); @@ -2846,13 +2849,13 @@ inline bool mmap::open(const char *path) { size_ = (static_cast(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow; #endif -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN8) hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); #else - hMapping_ = - ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart, - size.LowPart, NULL); + hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart, + size.LowPart, NULL); #endif if (hMapping_ == NULL) { @@ -2860,7 +2863,8 @@ inline bool mmap::open(const char *path) { return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN8) addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); #else addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); @@ -8185,7 +8189,8 @@ inline Result ClientImpl::Patch(const std::string &path, inline Result ClientImpl::Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return Patch(path, Headers(), body, content_type, progress); } @@ -8784,17 +8789,17 @@ inline bool SSLServer::is_valid() const { return ctx_; } inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } -inline void SSLServer::update_certs (X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { +inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { - std::lock_guard guard(ctx_mutex_); + std::lock_guard guard(ctx_mutex_); - SSL_CTX_use_certificate (ctx_, cert); - SSL_CTX_use_PrivateKey (ctx_, private_key); + SSL_CTX_use_certificate(ctx_, cert); + SSL_CTX_use_PrivateKey(ctx_, private_key); - if (client_ca_cert_store != nullptr) { - SSL_CTX_set_cert_store (ctx_, client_ca_cert_store); - } + if (client_ca_cert_store != nullptr) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + } } inline bool SSLServer::process_and_close_socket(socket_t sock) { @@ -9579,7 +9584,8 @@ inline Result Client::Patch(const std::string &path, const char *body, } inline Result Client::Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return cli_->Patch(path, body, content_length, content_type, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, @@ -9589,15 +9595,18 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, } inline Result Client::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress) { - return cli_->Patch(path, headers, body, content_length, content_type, progress); + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, headers, body, content_length, content_type, + progress); } inline Result Client::Patch(const std::string &path, const std::string &body, const std::string &content_type) { return cli_->Patch(path, body, content_type); } inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return cli_->Patch(path, body, content_type, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, @@ -9607,7 +9616,8 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, } inline Result Client::Patch(const std::string &path, const Headers &headers, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return cli_->Patch(path, headers, body, content_type, progress); } inline Result Client::Patch(const std::string &path, size_t content_length, @@ -9646,7 +9656,8 @@ inline Result Client::Delete(const std::string &path, const char *body, } inline Result Client::Delete(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return cli_->Delete(path, body, content_length, content_type, progress); } inline Result Client::Delete(const std::string &path, const Headers &headers, @@ -9656,15 +9667,18 @@ inline Result Client::Delete(const std::string &path, const Headers &headers, } inline Result Client::Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress) { - return cli_->Delete(path, headers, body, content_length, content_type, progress); + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, + progress); } inline Result Client::Delete(const std::string &path, const std::string &body, const std::string &content_type) { return cli_->Delete(path, body, content_type); } inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return cli_->Delete(path, body, content_type, progress); } inline Result Client::Delete(const std::string &path, const Headers &headers, @@ -9674,7 +9688,8 @@ inline Result Client::Delete(const std::string &path, const Headers &headers, } inline Result Client::Delete(const std::string &path, const Headers &headers, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + Progress progress) { return cli_->Delete(path, headers, body, content_type, progress); } inline Result Client::Options(const std::string &path) { diff --git a/test/test.cc b/test/test.cc index 446e7f9cc4..df69d4a2b6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -59,10 +59,9 @@ TEST(ClientTest, MoveConstructible) { EXPECT_TRUE(std::is_nothrow_move_constructible::value); } -TEST(ClientTest, MoveAssignable) -{ - EXPECT_FALSE(std::is_copy_assignable::value); - EXPECT_TRUE(std::is_nothrow_move_assignable::value); +TEST(ClientTest, MoveAssignable) { + EXPECT_FALSE(std::is_copy_assignable::value); + EXPECT_TRUE(std::is_nothrow_move_assignable::value); } #ifdef _WIN32 @@ -1755,32 +1754,34 @@ TEST(BindServerTest, BindAndListenSeparatelySSLEncryptedKey) { #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -X509* readCertificate (const std::string& strFileName) { - std::ifstream inStream (strFileName); - std::string strCertPEM ((std::istreambuf_iterator(inStream)), std::istreambuf_iterator()); +X509 *readCertificate(const std::string &strFileName) { + std::ifstream inStream(strFileName); + std::string strCertPEM((std::istreambuf_iterator(inStream)), + std::istreambuf_iterator()); - if (strCertPEM.empty ()) return (nullptr); + if (strCertPEM.empty()) return (nullptr); - BIO* pbCert = BIO_new (BIO_s_mem ()); - BIO_write (pbCert, strCertPEM.c_str (), (int)strCertPEM.size ()); - X509* pCert = PEM_read_bio_X509 (pbCert, NULL, 0, NULL); - BIO_free (pbCert); + BIO *pbCert = BIO_new(BIO_s_mem()); + BIO_write(pbCert, strCertPEM.c_str(), (int)strCertPEM.size()); + X509 *pCert = PEM_read_bio_X509(pbCert, NULL, 0, NULL); + BIO_free(pbCert); - return (pCert); + return (pCert); } -EVP_PKEY* readPrivateKey (const std::string& strFileName) { - std::ifstream inStream (strFileName); - std::string strPrivateKeyPEM ((std::istreambuf_iterator(inStream)), std::istreambuf_iterator()); +EVP_PKEY *readPrivateKey(const std::string &strFileName) { + std::ifstream inStream(strFileName); + std::string strPrivateKeyPEM((std::istreambuf_iterator(inStream)), + std::istreambuf_iterator()); - if (strPrivateKeyPEM.empty ()) return (nullptr); + if (strPrivateKeyPEM.empty()) return (nullptr); - BIO* pbPrivKey = BIO_new (BIO_s_mem ()); - BIO_write (pbPrivKey, strPrivateKeyPEM.c_str (), (int) strPrivateKeyPEM.size ()); - EVP_PKEY* pPrivateKey = PEM_read_bio_PrivateKey (pbPrivKey, NULL, NULL, NULL); - BIO_free (pbPrivKey); + BIO *pbPrivKey = BIO_new(BIO_s_mem()); + BIO_write(pbPrivKey, strPrivateKeyPEM.c_str(), (int)strPrivateKeyPEM.size()); + EVP_PKEY *pPrivateKey = PEM_read_bio_PrivateKey(pbPrivKey, NULL, NULL, NULL); + BIO_free(pbPrivKey); - return (pPrivateKey); + return (pPrivateKey); } TEST(BindServerTest, UpdateCerts) { @@ -1789,26 +1790,26 @@ TEST(BindServerTest, UpdateCerts) { ASSERT_TRUE(svr.is_valid()); ASSERT_TRUE(port > 0); - X509* cert = readCertificate (SERVER_CERT_FILE); - X509* ca_cert = readCertificate (CLIENT_CA_CERT_FILE); - EVP_PKEY* key = readPrivateKey (SERVER_PRIVATE_KEY_FILE); + X509 *cert = readCertificate(SERVER_CERT_FILE); + X509 *ca_cert = readCertificate(CLIENT_CA_CERT_FILE); + EVP_PKEY *key = readPrivateKey(SERVER_PRIVATE_KEY_FILE); - ASSERT_TRUE(cert != nullptr); + ASSERT_TRUE(cert != nullptr); ASSERT_TRUE(ca_cert != nullptr); - ASSERT_TRUE(key != nullptr); + ASSERT_TRUE(key != nullptr); - X509_STORE* cert_store = X509_STORE_new (); + X509_STORE *cert_store = X509_STORE_new(); - X509_STORE_add_cert (cert_store, ca_cert); + X509_STORE_add_cert(cert_store, ca_cert); - svr.update_certs (cert, key, cert_store); + svr.update_certs(cert, key, cert_store); ASSERT_TRUE(svr.is_valid()); svr.stop(); - X509_free (cert); - X509_free (ca_cert); - EVP_PKEY_free (key); + X509_free(cert); + X509_free(ca_cert); + EVP_PKEY_free(key); } #endif @@ -2357,8 +2358,8 @@ class ServerTest : public ::testing::Test { }) .Get("/with-range-customized-response", [&](const Request & /*req*/, Response &res) { - res.status = StatusCode::BadRequest_400; - res.set_content(JSON_DATA, "application/json"); + res.status = StatusCode::BadRequest_400; + res.set_content(JSON_DATA, "application/json"); }) .Post("/chunked", [&](const Request &req, Response & /*res*/) { @@ -3480,8 +3481,10 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { } TEST_F(ServerTest, GetRangeWithMaxLongLength) { - auto res = - cli_.Get("/with-range", {{"Range", "bytes=0-" + std::to_string(std::numeric_limits::max())}}); + auto res = cli_.Get( + "/with-range", + {{"Range", + "bytes=0-" + std::to_string(std::numeric_limits::max())}}); EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); EXPECT_EQ("0", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); @@ -3637,7 +3640,8 @@ TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) { } TEST_F(ServerTest, GetWithRangeCustomizedResponse) { - auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}})}}); + auto res = cli_.Get("/with-range-customized-response", + {{make_range_header({{1, 2}})}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::BadRequest_400, res->status); EXPECT_EQ(true, res->has_header("Content-Length")); @@ -3646,7 +3650,8 @@ TEST_F(ServerTest, GetWithRangeCustomizedResponse) { } TEST_F(ServerTest, GetWithRangeMultipartCustomizedResponseMultipleRange) { - auto res = cli_.Get("/with-range-customized-response", {{make_range_header({{1, 2}, {4, 5}})}}); + auto res = cli_.Get("/with-range-customized-response", + {{make_range_header({{1, 2}, {4, 5}})}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::BadRequest_400, res->status); EXPECT_EQ(true, res->has_header("Content-Length")); @@ -7450,6 +7455,6 @@ TEST(UniversalClientImplTest, Ipv6LiteralAddress) { std::string ipV6TestURL = "http://[ff06::c3]"; Client cli(ipV6TestURL + ":" + std::to_string(port), CLIENT_CERT_FILE, - CLIENT_PRIVATE_KEY_FILE); + CLIENT_PRIVATE_KEY_FILE); EXPECT_EQ(cli.port(), port); } From 521529d24d3815777fb62ed34328daf1c7ca75ff Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 6 Aug 2024 13:43:00 -0400 Subject: [PATCH 0206/1222] Fix #1481 (with content provider) (#1527) * Fix #1481 (with content provider) * Improve shutdown performance * Make shutdown action more stable * Move some tests up * Simplified * Simplified --- httplib.h | 25 +++- test/test.cc | 374 +++++++++++++++++++++++++++++---------------------- 2 files changed, 235 insertions(+), 164 deletions(-) diff --git a/httplib.h b/httplib.h index 2aefc4bb76..d6d65414be 100644 --- a/httplib.h +++ b/httplib.h @@ -8541,13 +8541,29 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, return ssl; } -inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, +inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, bool shutdown_gracefully) { // sometimes we may want to skip this to try to avoid SIGPIPE if we know // the remote has closed the network connection // Note that it is not always possible to avoid SIGPIPE, this is merely a // best-efforts. - if (shutdown_gracefully) { SSL_shutdown(ssl); } + if (shutdown_gracefully) { +#ifdef _WIN32 + SSL_shutdown(ssl); +#else + timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); + + auto ret = SSL_shutdown(ssl); + while (ret == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + ret = SSL_shutdown(ssl); + } +#endif + } std::lock_guard guard(ctx_mutex); SSL_free(ssl); @@ -8826,7 +8842,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { // Shutdown gracefully if the result seemed successful, non-gracefully if // the connection appeared to be closed. const bool shutdown_gracefully = ret; - detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully); + detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); } detail::shutdown_socket(sock); @@ -9109,7 +9125,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket, return; } if (socket.ssl) { - detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully); + detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, + shutdown_gracefully); socket.ssl = nullptr; } assert(socket.ssl == nullptr); diff --git a/test/test.cc b/test/test.cc index df69d4a2b6..d6610a758a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -54,6 +54,166 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files, #endif } +#ifndef _WIN32 +class UnixSocketTest : public ::testing::Test { +protected: + void TearDown() override { std::remove(pathname_.c_str()); } + + void client_GET(const std::string &addr) { + httplib::Client cli{addr}; + cli.set_address_family(AF_UNIX); + ASSERT_TRUE(cli.is_valid()); + + const auto &result = cli.Get(pattern_); + ASSERT_TRUE(result) << "error: " << result.error(); + + const auto &resp = result.value(); + EXPECT_EQ(resp.status, StatusCode::OK_200); + EXPECT_EQ(resp.body, content_); + } + + const std::string pathname_{"./httplib-server.sock"}; + const std::string pattern_{"/hi"}; + const std::string content_{"Hello World!"}; +}; + +TEST_F(UnixSocketTest, pathname) { + httplib::Server svr; + svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) { + res.set_content(content_, "text/plain"); + }); + + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); + }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + ASSERT_TRUE(svr.is_running()); + + client_GET(pathname_); +} + +#if defined(__linux__) || \ + /* __APPLE__ */ (defined(SOL_LOCAL) && defined(SO_PEERPID)) +TEST_F(UnixSocketTest, PeerPid) { + httplib::Server svr; + std::string remote_port_val; + svr.Get(pattern_, [&](const httplib::Request &req, httplib::Response &res) { + res.set_content(content_, "text/plain"); + remote_port_val = req.get_header_value("REMOTE_PORT"); + }); + + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); + }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + ASSERT_TRUE(svr.is_running()); + + client_GET(pathname_); + EXPECT_EQ(std::to_string(getpid()), remote_port_val); +} +#endif + +#ifdef __linux__ +TEST_F(UnixSocketTest, abstract) { + constexpr char svr_path[]{"\x00httplib-server.sock"}; + const std::string abstract_addr{svr_path, sizeof(svr_path) - 1}; + + httplib::Server svr; + svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) { + res.set_content(content_, "text/plain"); + }); + + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80)); + }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + ASSERT_TRUE(svr.is_running()); + + client_GET(abstract_addr); +} +#endif + +TEST(SocketStream, is_writable_UNIX) { + int fds[2]; + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); + + const auto asSocketStream = [&](socket_t fd, + std::function func) { + return detail::process_client_socket(fd, 0, 0, 0, 0, func); + }; + asSocketStream(fds[0], [&](Stream &s0) { + EXPECT_EQ(s0.socket(), fds[0]); + EXPECT_TRUE(s0.is_writable()); + + EXPECT_EQ(0, close(fds[1])); + EXPECT_FALSE(s0.is_writable()); + + return true; + }); + EXPECT_EQ(0, close(fds[0])); +} + +TEST(SocketStream, is_writable_INET) { + sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 1); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + int disconnected_svr_sock = -1; + std::thread svr{[&] { + const int s = socket(AF_INET, SOCK_STREAM, 0); + ASSERT_LE(0, s); + ASSERT_EQ(0, ::bind(s, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, listen(s, 1)); + ASSERT_LE(0, disconnected_svr_sock = accept(s, nullptr, nullptr)); + ASSERT_EQ(0, close(s)); + }}; + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + std::thread cli{[&] { + const int s = socket(AF_INET, SOCK_STREAM, 0); + ASSERT_LE(0, s); + ASSERT_EQ(0, connect(s, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, close(s)); + }}; + cli.join(); + svr.join(); + ASSERT_NE(disconnected_svr_sock, -1); + + const auto asSocketStream = [&](socket_t fd, + std::function func) { + return detail::process_client_socket(fd, 0, 0, 0, 0, func); + }; + asSocketStream(disconnected_svr_sock, [&](Stream &ss) { + EXPECT_EQ(ss.socket(), disconnected_svr_sock); + EXPECT_FALSE(ss.is_writable()); + + return true; + }); + + ASSERT_EQ(0, close(disconnected_svr_sock)); +} +#endif // #ifndef _WIN32 + TEST(ClientTest, MoveConstructible) { EXPECT_FALSE(std::is_copy_constructible::value); EXPECT_TRUE(std::is_nothrow_move_constructible::value); @@ -4996,6 +5156,60 @@ TEST(KeepAliveTest, SSLClientReconnection) { ASSERT_TRUE(result); EXPECT_EQ(StatusCode::OK_200, result->status); } + +TEST(KeepAliveTest, SSLClientReconnectionPost) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + svr.set_keep_alive_timeout(1); + std::string content = "reconnect"; + + svr.Post("/hi", [](const httplib::Request &, httplib::Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_keep_alive(true); + + auto result = cli.Post( + "/hi", content.size(), + [&content](size_t offset, size_t length, DataSink &sink) { + sink.write(content.c_str(), content.size()); + return true; + }, + "text/plain"); + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); + + std::this_thread::sleep_for(std::chrono::seconds(2)); + + // Recoonect + result = cli.Post( + "/hi", content.size(), + [&content](size_t offset, size_t length, DataSink &sink) { + sink.write(content.c_str(), content.size()); + return true; + }, + "text/plain"); + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); + + result = cli.Post( + "/hi", content.size(), + [&content](size_t offset, size_t length, DataSink &sink) { + sink.write(content.c_str(), content.size()); + return true; + }, + "text/plain"); + ASSERT_TRUE(result); + EXPECT_EQ(200, result->status); + + svr.stop(); + f.wait(); +} #endif TEST(ClientProblemDetectionTest, ContentProvider) { @@ -6970,166 +7184,6 @@ TEST(MultipartFormDataTest, ContentLength) { #endif -#ifndef _WIN32 -class UnixSocketTest : public ::testing::Test { -protected: - void TearDown() override { std::remove(pathname_.c_str()); } - - void client_GET(const std::string &addr) { - httplib::Client cli{addr}; - cli.set_address_family(AF_UNIX); - ASSERT_TRUE(cli.is_valid()); - - const auto &result = cli.Get(pattern_); - ASSERT_TRUE(result) << "error: " << result.error(); - - const auto &resp = result.value(); - EXPECT_EQ(resp.status, StatusCode::OK_200); - EXPECT_EQ(resp.body, content_); - } - - const std::string pathname_{"./httplib-server.sock"}; - const std::string pattern_{"/hi"}; - const std::string content_{"Hello World!"}; -}; - -TEST_F(UnixSocketTest, pathname) { - httplib::Server svr; - svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) { - res.set_content(content_, "text/plain"); - }); - - std::thread t{[&] { - ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); - }}; - auto se = detail::scope_exit([&] { - svr.stop(); - t.join(); - ASSERT_FALSE(svr.is_running()); - }); - - svr.wait_until_ready(); - ASSERT_TRUE(svr.is_running()); - - client_GET(pathname_); -} - -#if defined(__linux__) || \ - /* __APPLE__ */ (defined(SOL_LOCAL) && defined(SO_PEERPID)) -TEST_F(UnixSocketTest, PeerPid) { - httplib::Server svr; - std::string remote_port_val; - svr.Get(pattern_, [&](const httplib::Request &req, httplib::Response &res) { - res.set_content(content_, "text/plain"); - remote_port_val = req.get_header_value("REMOTE_PORT"); - }); - - std::thread t{[&] { - ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); - }}; - auto se = detail::scope_exit([&] { - svr.stop(); - t.join(); - ASSERT_FALSE(svr.is_running()); - }); - - svr.wait_until_ready(); - ASSERT_TRUE(svr.is_running()); - - client_GET(pathname_); - EXPECT_EQ(std::to_string(getpid()), remote_port_val); -} -#endif - -#ifdef __linux__ -TEST_F(UnixSocketTest, abstract) { - constexpr char svr_path[]{"\x00httplib-server.sock"}; - const std::string abstract_addr{svr_path, sizeof(svr_path) - 1}; - - httplib::Server svr; - svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) { - res.set_content(content_, "text/plain"); - }); - - std::thread t{[&] { - ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80)); - }}; - auto se = detail::scope_exit([&] { - svr.stop(); - t.join(); - ASSERT_FALSE(svr.is_running()); - }); - - svr.wait_until_ready(); - ASSERT_TRUE(svr.is_running()); - - client_GET(abstract_addr); -} -#endif - -TEST(SocketStream, is_writable_UNIX) { - int fds[2]; - ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); - - const auto asSocketStream = [&](socket_t fd, - std::function func) { - return detail::process_client_socket(fd, 0, 0, 0, 0, func); - }; - asSocketStream(fds[0], [&](Stream &s0) { - EXPECT_EQ(s0.socket(), fds[0]); - EXPECT_TRUE(s0.is_writable()); - - EXPECT_EQ(0, close(fds[1])); - EXPECT_FALSE(s0.is_writable()); - - return true; - }); - EXPECT_EQ(0, close(fds[0])); -} - -TEST(SocketStream, is_writable_INET) { - sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(PORT + 1); - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - - int disconnected_svr_sock = -1; - std::thread svr{[&] { - const int s = socket(AF_INET, SOCK_STREAM, 0); - ASSERT_LE(0, s); - ASSERT_EQ(0, ::bind(s, reinterpret_cast(&addr), sizeof(addr))); - ASSERT_EQ(0, listen(s, 1)); - ASSERT_LE(0, disconnected_svr_sock = accept(s, nullptr, nullptr)); - ASSERT_EQ(0, close(s)); - }}; - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - - std::thread cli{[&] { - const int s = socket(AF_INET, SOCK_STREAM, 0); - ASSERT_LE(0, s); - ASSERT_EQ(0, connect(s, reinterpret_cast(&addr), sizeof(addr))); - ASSERT_EQ(0, close(s)); - }}; - cli.join(); - svr.join(); - ASSERT_NE(disconnected_svr_sock, -1); - - const auto asSocketStream = [&](socket_t fd, - std::function func) { - return detail::process_client_socket(fd, 0, 0, 0, 0, func); - }; - asSocketStream(disconnected_svr_sock, [&](Stream &ss) { - EXPECT_EQ(ss.socket(), disconnected_svr_sock); - EXPECT_FALSE(ss.is_writable()); - - return true; - }); - - ASSERT_EQ(0, close(disconnected_svr_sock)); -} -#endif // #ifndef _WIN32 - TEST(TaskQueueTest, IncreaseAtomicInteger) { static constexpr unsigned int number_of_tasks{1000000}; std::atomic_uint count{0}; From e00fd06355e8717032950a0a6e07cfa3d3df594e Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 6 Aug 2024 17:04:22 -0400 Subject: [PATCH 0207/1222] Release v0.16.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index d6d65414be..e0d0cd4496 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.16.0" +#define CPPHTTPLIB_VERSION "0.16.1" /* * Configuration From ae63b89cbf70481ae60515dfd95467e91eecd992 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 6 Aug 2024 17:31:55 -0400 Subject: [PATCH 0208/1222] Use SOCK_CLOEXEC instead of __linux__ --- httplib.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index a5e5da249a..fded7e1293 100644 --- a/httplib.h +++ b/httplib.h @@ -3252,7 +3252,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } #ifdef SOCK_CLOEXEC - auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, hints.ai_protocol); + auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, + hints.ai_protocol); #else auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); #endif @@ -3316,7 +3317,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #else #ifdef SOCK_CLOEXEC - auto sock = socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); + auto sock = + socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); #else auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); #endif @@ -6521,9 +6523,10 @@ inline bool Server::listen_internal() { #endif #if defined _WIN32 - // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, OVERLAPPED + // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, + // OVERLAPPED socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); -#elif defined __linux__ +#elif defined SOCK_CLOEXEC socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); #else socket_t sock = accept(svr_sock_, nullptr, nullptr); @@ -9343,7 +9346,7 @@ inline Client::Client(const std::string &scheme_host_port, cli_ = detail::make_unique(scheme_host_port, 80, client_cert_path, client_key_path); } -} +} // namespace detail inline Client::Client(const std::string &host, int port) : cli_(detail::make_unique(host, port)) {} From 69c84c9597c3bb901503279b248a5710349fa4c7 Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Thu, 8 Aug 2024 11:47:56 -0400 Subject: [PATCH 0209/1222] BoringSSL compatibility fixes (#1892) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch is necessary to build cpp-httplib in Crashpad, itself in Chromium, using BoringSSL. Details at [1]. The fixes include: - Library version check: tolerate BoringSSL as an alternative to OpenSSL 3. - Don’t call `OPENSSL_thread_stop`, which is not in BoringSSL. - Use `SSL_get_peer_certificate` (deprecated in OpenSSL 3), the old name for `SSL_get1_peer_certificate`, because the new name is not in BoringSSL. - Call `SSL_set_tlsext_host_name` directly instead of making an `SSL_ctrl` call that BoringSSL does not support. The feared -Wold-style-cast warning that occurs when buidling with OpenSSL is not triggered in BoringSSL. [1] https://chromium.googlesource.com/crashpad/crashpad/+/1a62a0182557c89494676c06611f1ca731bcb2db --- httplib.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index fded7e1293..5c7cba2e64 100644 --- a/httplib.h +++ b/httplib.h @@ -269,7 +269,12 @@ using socket_t = int; #include #include -#if OPENSSL_VERSION_NUMBER < 0x30000000L +#if defined(OPENSSL_IS_BORINGSSL) +#if OPENSSL_VERSION_NUMBER < 0x1010107f +#error Please use OpenSSL or a current version of BoringSSL +#endif +#define SSL_get1_peer_certificate SSL_get_peer_certificate +#elif OPENSSL_VERSION_NUMBER < 0x30000000L #error Sorry, OpenSSL versions prior to 3.0.0 are not supported #endif @@ -727,7 +732,7 @@ class ThreadPool final : public TaskQueue { fn(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) OPENSSL_thread_stop(); #endif } @@ -9121,11 +9126,14 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return true; }, [&](SSL *ssl2) { +#if defined(OPENSSL_IS_BORINGSSL) + SSL_set_tlsext_host_name(ssl2, host_.c_str()); +#else // NOTE: Direct call instead of using the OpenSSL macro to suppress // -Wold-style-cast warning - // SSL_set_tlsext_host_name(ssl2, host_.c_str()); SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, static_cast(const_cast(host_.c_str()))); +#endif return true; }); From c5c54b31e2bf8bd37ceba2715de756d1132e3685 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 8 Aug 2024 11:48:50 -0400 Subject: [PATCH 0210/1222] Release v0.16.2 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5c7cba2e64..117e498faf 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.16.1" +#define CPPHTTPLIB_VERSION "0.16.2" /* * Configuration From 45f3694f820dd325cb7451240479afc2606f0b74 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 8 Aug 2024 19:30:46 -0400 Subject: [PATCH 0211/1222] Fix problem with clean command in Makefile --- example/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/Makefile b/example/Makefile index 7a049ced87..c0739883f8 100644 --- a/example/Makefile +++ b/example/Makefile @@ -58,4 +58,4 @@ pem: openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request *.pem From aa04feebb4ede6e80d188d31d96ea407d1a86088 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 8 Aug 2024 20:54:33 -0400 Subject: [PATCH 0212/1222] Fix warnings --- test/test.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test.cc b/test/test.cc index d6610a758a..6cb05bc7d2 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5176,7 +5176,7 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { auto result = cli.Post( "/hi", content.size(), - [&content](size_t offset, size_t length, DataSink &sink) { + [&content](size_t /*offset*/, size_t /*length*/, DataSink &sink) { sink.write(content.c_str(), content.size()); return true; }, @@ -5189,7 +5189,7 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { // Recoonect result = cli.Post( "/hi", content.size(), - [&content](size_t offset, size_t length, DataSink &sink) { + [&content](size_t /*offset*/, size_t /*length*/, DataSink &sink) { sink.write(content.c_str(), content.size()); return true; }, @@ -5199,7 +5199,7 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { result = cli.Post( "/hi", content.size(), - [&content](size_t offset, size_t length, DataSink &sink) { + [&content](size_t /*offset*/, size_t /*length*/, DataSink &sink) { sink.write(content.c_str(), content.size()); return true; }, From 390f2c41f62bf10f41deff1722374826a1b011d8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 8 Aug 2024 22:07:46 -0400 Subject: [PATCH 0213/1222] Fix #1878 (#1893) * Fix #1878 --- httplib.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 117e498faf..4af592aa9b 100644 --- a/httplib.h +++ b/httplib.h @@ -3278,7 +3278,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (socket_options) { socket_options(sock); } - if (!bind_or_connect(sock, hints)) { + bool dummy; + if (!bind_or_connect(sock, hints, dummy)) { close_socket(sock); sock = INVALID_SOCKET; } @@ -3363,12 +3364,15 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, } // bind or connect - if (bind_or_connect(sock, *rp)) { + auto quit = false; + if (bind_or_connect(sock, *rp, quit)) { freeaddrinfo(result); return sock; } close_socket(sock); + + if (quit) { break; } } freeaddrinfo(result); @@ -3469,7 +3473,7 @@ inline socket_t create_client_socket( time_t write_timeout_usec, const std::string &intf, Error &error) { auto sock = create_socket( host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options), - [&](socket_t sock2, struct addrinfo &ai) -> bool { + [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { if (!intf.empty()) { #ifdef USE_IF2IP auto ip_from_if = if2ip(address_family, intf); @@ -3493,7 +3497,10 @@ inline socket_t create_client_socket( } error = wait_until_socket_is_ready(sock2, connection_timeout_sec, connection_timeout_usec); - if (error != Error::Success) { return false; } + if (error != Error::Success) { + if (error == Error::ConnectionTimeout) { quit = true; } + return false; + } } set_nonblocking(sock2, false); @@ -6470,7 +6477,7 @@ Server::create_server_socket(const std::string &host, int port, return detail::create_socket( host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, std::move(socket_options), - [](socket_t sock, struct addrinfo &ai) -> bool { + [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { return false; } From 6c3e8482f7b4e3b307bb42afbb85fd8771da86b8 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Sat, 10 Aug 2024 20:19:59 +0900 Subject: [PATCH 0214/1222] Fix KeepAliveTest.SSLClientReconnectionPost (#1895) --- test/test.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/test.cc b/test/test.cc index 6cb05bc7d2..33ef30b496 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5167,8 +5167,14 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { res.set_content("Hello World!", "text/plain"); }); - auto f = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); }); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); SSLClient cli(HOST, PORT); cli.enable_server_certificate_verification(false); @@ -5206,9 +5212,6 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { "text/plain"); ASSERT_TRUE(result); EXPECT_EQ(200, result->status); - - svr.stop(); - f.wait(); } #endif From af56b7ec0ba9d18b788ed064f76d936eb52c5db5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 17 Aug 2024 09:53:26 -0400 Subject: [PATCH 0215/1222] Release v0.16.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 4af592aa9b..919306c179 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.16.2" +#define CPPHTTPLIB_VERSION "0.16.3" /* * Configuration From 048edec9edf61664a73c6c630769a47b6613893a Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 26 Aug 2024 21:10:38 -0400 Subject: [PATCH 0216/1222] Changed CPPHTTPLIB_KEEPALIVE_MAX_COUNT to 100 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 919306c179..7ef58264c8 100644 --- a/httplib.h +++ b/httplib.h @@ -19,7 +19,7 @@ #endif #ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT -#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5 +#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 #endif #ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND From 52a18c78a52b1bcffc10506fe5fdc76568b3c646 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 27 Aug 2024 00:23:31 -0400 Subject: [PATCH 0217/1222] Add docker related files --- Dockerfile | 11 +++++++++++ docker/index.html | 21 +++++++++++++++++++++ docker/main.cc | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 Dockerfile create mode 100644 docker/index.html create mode 100644 docker/main.cc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..ec8259687d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu AS builder +WORKDIR /app +COPY httplib.h . +COPY docker/main.cc . +RUN apt update && apt install g++ -y +RUN g++ -std=c++14 -static -o server -O3 -I. -DCPPHTTPLIB_USE_POLL main.cc + +FROM scratch +COPY --from=builder /app/server /server +COPY docker/index.html /html/index.html +CMD ["/server"] diff --git a/docker/index.html b/docker/index.html new file mode 100644 index 0000000000..1abe9780d9 --- /dev/null +++ b/docker/index.html @@ -0,0 +1,21 @@ + + + +Welcome to cpp-httplib! + + + +

Welcome to cpp-httplib!

+

If you see this page, the cpp-httplib web server is successfully installed and +working. Further configuration is required.

+ +

For online documentation and support please refer to +github.com/yhirose/cpp-httplib.
+ +

Thank you for using cpp-httplib.

+ + diff --git a/docker/main.cc b/docker/main.cc new file mode 100644 index 0000000000..1f65cb6e7e --- /dev/null +++ b/docker/main.cc @@ -0,0 +1,39 @@ +// +// main.cc +// +// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// MIT License +// + +#include +#include +#include + +using namespace httplib; +using namespace std; + +auto error_html = R"( +%d %s + +

404 Not Found

+
cpp-httplib/%s
+ + +)"; + +int main(int argc, const char **argv) { + Server svr; + + svr.set_error_handler([](const Request & /*req*/, Response &res) { + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), error_html, res.status, + status_message(res.status), CPPHTTPLIB_VERSION); + res.set_content(buf, "text/html"); + }); + + svr.set_mount_point("/", "./html"); + + svr.listen("0.0.0.0", 80); + + return 0; +} From da0c6579fa467a7f768e9bbce26289a742743186 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 31 Aug 2024 17:07:48 -0400 Subject: [PATCH 0218/1222] Breaking Change! get_header_ methods on Request and Response now take a default value. --- httplib.h | 48 ++++++++++++++++++++++++++++-------------------- test/test.cc | 8 ++++---- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/httplib.h b/httplib.h index 7ef58264c8..c777991c5a 100644 --- a/httplib.h +++ b/httplib.h @@ -565,8 +565,10 @@ struct Request { #endif bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); @@ -597,8 +599,10 @@ struct Response { std::string location; // Redirect location bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); @@ -1091,9 +1095,10 @@ class Result { // Request Headers bool has_request_header(const std::string &key) const; std::string get_request_header_value(const std::string &key, + const char *def = "", size_t id = 0) const; uint64_t get_request_header_value_u64(const std::string &key, - size_t id = 0) const; + uint64_t def = 0, size_t id = 0) const; size_t get_request_header_value_count(const std::string &key) const; private: @@ -1914,8 +1919,8 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { } inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, size_t id, - uint64_t def) { + const std::string &key, uint64_t def, + size_t id) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -1928,13 +1933,13 @@ inline uint64_t get_header_value_u64(const Headers &headers, } // namespace detail inline uint64_t Request::get_header_value_u64(const std::string &key, - size_t id) const { - return detail::get_header_value_u64(headers, key, id, 0); + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); } inline uint64_t Response::get_header_value_u64(const std::string &key, - size_t id) const { - return detail::get_header_value_u64(headers, key, id, 0); + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); } template @@ -2119,8 +2124,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) { } inline uint64_t Result::get_request_header_value_u64(const std::string &key, + uint64_t def, size_t id) const { - return detail::get_header_value_u64(request_headers_, key, id, 0); + return detail::get_header_value_u64(request_headers_, key, def, id); } template @@ -2220,7 +2226,7 @@ socket_t create_client_socket( time_t write_timeout_usec, const std::string &intf, Error &error); const char *get_header_value(const Headers &headers, const std::string &key, - size_t id = 0, const char *def = nullptr); + const char *def, size_t id); std::string params_to_query_str(const Params ¶ms); @@ -3948,8 +3954,8 @@ inline bool has_header(const Headers &headers, const std::string &key) { } inline const char *get_header_value(const Headers &headers, - const std::string &key, size_t id, - const char *def) { + const std::string &key, const char *def, + size_t id) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -4146,7 +4152,7 @@ inline bool read_content_chunked(Stream &strm, T &x, inline bool is_chunked_transfer_encoding(const Headers &headers) { return compare_case_ignore( - get_header_value(headers, "Transfer-Encoding", 0, ""), "chunked"); + get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); } template @@ -5489,8 +5495,8 @@ inline bool Request::has_header(const std::string &key) const { } inline std::string Request::get_header_value(const std::string &key, - size_t id) const { - return detail::get_header_value(headers, key, id, ""); + const char *def, size_t id) const { + return detail::get_header_value(headers, key, def, id); } inline size_t Request::get_header_value_count(const std::string &key) const { @@ -5554,8 +5560,9 @@ inline bool Response::has_header(const std::string &key) const { } inline std::string Response::get_header_value(const std::string &key, + const char *def, size_t id) const { - return detail::get_header_value(headers, key, id, ""); + return detail::get_header_value(headers, key, def, id); } inline size_t Response::get_header_value_count(const std::string &key) const { @@ -5640,8 +5647,9 @@ inline bool Result::has_request_header(const std::string &key) const { } inline std::string Result::get_request_header_value(const std::string &key, + const char *def, size_t id) const { - return detail::get_header_value(request_headers_, key, id, ""); + return detail::get_header_value(request_headers_, key, def, id); } inline size_t diff --git a/test/test.cc b/test/test.cc index 33ef30b496..e13d3b9321 100644 --- a/test/test.cc +++ b/test/test.cc @@ -467,25 +467,25 @@ TEST(ParseMultipartBoundaryTest, ValueWithQuotesAndCharset) { TEST(GetHeaderValueTest, DefaultValue) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); + auto val = detail::get_header_value(headers, "Content-Type", "text/plain", 0); EXPECT_STREQ("text/plain", val); } TEST(GetHeaderValueTest, DefaultValueInt) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 100); + auto val = detail::get_header_value_u64(headers, "Content-Length", 100, 0); EXPECT_EQ(100ull, val); } TEST(GetHeaderValueTest, RegularValue) { Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain"); + auto val = detail::get_header_value(headers, "Content-Type", "text/plain", 0); EXPECT_STREQ("text/html", val); } TEST(GetHeaderValueTest, RegularValueWithDifferentCase) { Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value(headers, "content-type", 0, "text/plain"); + auto val = detail::get_header_value(headers, "content-type", "text/plain", 0); EXPECT_STREQ("text/html", val); } From ba638ff38e4a3374a92e4921d09da1d2d9f8c0c5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 31 Aug 2024 17:09:20 -0400 Subject: [PATCH 0219/1222] Update Docker support --- Dockerfile | 9 ++--- docker-compose.yml | 7 ++++ docker/{ => html}/index.html | 0 docker/main.cc | 70 +++++++++++++++++++++++++++--------- 4 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 docker-compose.yml rename docker/{ => html}/index.html (100%) diff --git a/Dockerfile b/Dockerfile index ec8259687d..1c63941dd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,12 @@ FROM ubuntu AS builder -WORKDIR /app +WORKDIR /build COPY httplib.h . COPY docker/main.cc . RUN apt update && apt install g++ -y -RUN g++ -std=c++14 -static -o server -O3 -I. -DCPPHTTPLIB_USE_POLL main.cc +RUN g++ -std=c++23 -static -o server -O2 -I. -DCPPHTTPLIB_USE_POLL main.cc && strip server FROM scratch -COPY --from=builder /app/server /server -COPY docker/index.html /html/index.html +COPY --from=builder /build/server /server +COPY docker/html/index.html /html/index.html +EXPOSE 80 CMD ["/server"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000..4a55b0dc4e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +services: + http: + build: . + ports: + - "8080:80" + volumes: + - ./docker/html:/html diff --git a/docker/index.html b/docker/html/index.html similarity index 100% rename from docker/index.html rename to docker/html/index.html diff --git a/docker/main.cc b/docker/main.cc index 1f65cb6e7e..8c4968125d 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -5,35 +5,73 @@ // MIT License // -#include -#include +#include +#include +#include +#include #include +#include -using namespace httplib; -using namespace std; +#include -auto error_html = R"( -%d %s +constexpr auto error_html = R"( +{} {}

404 Not Found

-
cpp-httplib/%s
+
cpp-httplib/{}
)"; +std::string time_local() { + auto p = std::chrono::system_clock::now(); + auto t = std::chrono::system_clock::to_time_t(p); + + std::stringstream ss; + ss << std::put_time(std::localtime(&t), "%d/%b/%Y:%H:%M:%S %z"); + return ss.str(); +} + +std::string log(auto &req, auto &res) { + auto remote_user = "-"; // TODO: + auto request = std::format("{} {} {}", req.method, req.path, req.version); + auto body_bytes_sent = res.get_header_value("Content-Length"); + auto http_referer = "-"; // TODO: + auto http_user_agent = req.get_header_value("User-Agent", "-"); + + // NOTE: From NGINX defualt access log format + // log_format combined '$remote_addr - $remote_user [$time_local] ' + // '"$request" $status $body_bytes_sent ' + // '"$http_referer" "$http_user_agent"'; + return std::format(R"({} - {} [{}] "{}" {} {} "{}" "{}")", req.remote_addr, + remote_user, time_local(), request, res.status, + body_bytes_sent, http_referer, http_user_agent); +} + int main(int argc, const char **argv) { - Server svr; + auto base_dir = "./html"; + auto host = "0.0.0.0"; + auto port = 80; + + httplib::Server svr; - svr.set_error_handler([](const Request & /*req*/, Response &res) { - char buf[BUFSIZ]; - snprintf(buf, sizeof(buf), error_html, res.status, - status_message(res.status), CPPHTTPLIB_VERSION); - res.set_content(buf, "text/html"); + svr.set_error_handler([](auto & /*req*/, auto &res) { + auto body = + std::format(error_html, res.status, httplib::status_message(res.status), + CPPHTTPLIB_VERSION); + + res.set_content(body, "text/html"); }); - svr.set_mount_point("/", "./html"); + svr.set_logger( + [](auto &req, auto &res) { std::cout << log(req, res) << std::endl; }); + + svr.set_mount_point("/", base_dir); + + std::cout << std::format("Serving HTTP on {0} port {1} ...", host, port) + << std::endl; - svr.listen("0.0.0.0", 80); + auto ret = svr.listen(host, port); - return 0; + return ret ? 0 : 1; } From d82c82db2cd07fa4e5f7fa669d72dd618c89ed16 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 31 Aug 2024 17:19:52 -0400 Subject: [PATCH 0220/1222] Add sleep in handle_EINTR --- httplib.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index c777991c5a..2bdae5d73c 100644 --- a/httplib.h +++ b/httplib.h @@ -2951,7 +2951,10 @@ template inline ssize_t handle_EINTR(T fn) { ssize_t res = 0; while (true) { res = fn(); - if (res < 0 && errno == EINTR) { continue; } + if (res < 0 && errno == EINTR) { + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + continue; + } break; } return res; From cee838e335ba514df65fd7344f55fe8e525848c5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 31 Aug 2024 17:42:43 -0400 Subject: [PATCH 0221/1222] Documentation --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index b2b4b9e776..7312c44d1f 100644 --- a/README.md +++ b/README.md @@ -848,6 +848,22 @@ $ ./split.py Wrote out/httplib.h and out/httplib.cc ``` +Dockerfile for Static HTTP Server +--------------------------------- + +Dockerfile for static HTTP server is available. Port number of this HTTP server is 80, and it serves static files from `/html` directory in the container. + +```bash +> docker build -t cpp-httplib-server . +... + +> docker run --init --rm -it -p 8080:80 -v ./docker/html:/html cpp-httplib-server +Serving HTTP on 0.0.0.0 port 80 ... +192.168.65.1 - - [31/Aug/2024:21:33:56 +0000] "GET / HTTP/1.1" 200 599 "-" "curl/8.7.1" +192.168.65.1 - - [31/Aug/2024:21:34:26 +0000] "GET / HTTP/1.1" 200 599 "-" "Mozilla/5.0 ..." +192.168.65.1 - - [31/Aug/2024:21:34:26 +0000] "GET /favicon.ico HTTP/1.1" 404 152 "-" "Mozilla/5.0 ..." +``` + NOTE ---- From 9c91b6f4a62f7ad108c6ecdd33ac5f791232e3f2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 1 Sep 2024 00:11:07 -0400 Subject: [PATCH 0222/1222] Fix #1645 --- httplib.h | 27 ++++++++++++++++++--------- test/test.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index 2bdae5d73c..7b7b3f297d 100644 --- a/httplib.h +++ b/httplib.h @@ -932,6 +932,7 @@ class Server { bool is_running() const; void wait_until_ready() const; void stop(); + void decommission(); std::function new_task_queue; @@ -1006,7 +1007,7 @@ class Server { virtual bool process_and_close_socket(socket_t sock); std::atomic is_running_{false}; - std::atomic done_{false}; + std::atomic is_decommisioned{false}; struct MountPointEntry { std::string mount_point; @@ -6111,27 +6112,27 @@ inline Server &Server::set_payload_max_length(size_t length) { inline bool Server::bind_to_port(const std::string &host, int port, int socket_flags) { - return bind_internal(host, port, socket_flags) >= 0; + auto ret = bind_internal(host, port, socket_flags); + if (ret == -1) { is_decommisioned = true; } + return ret >= 0; } inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { - return bind_internal(host, 0, socket_flags); + auto ret = bind_internal(host, 0, socket_flags); + if (ret == -1) { is_decommisioned = true; } + return ret; } -inline bool Server::listen_after_bind() { - auto se = detail::scope_exit([&]() { done_ = true; }); - return listen_internal(); -} +inline bool Server::listen_after_bind() { return listen_internal(); } inline bool Server::listen(const std::string &host, int port, int socket_flags) { - auto se = detail::scope_exit([&]() { done_ = true; }); return bind_to_port(host, port, socket_flags) && listen_internal(); } inline bool Server::is_running() const { return is_running_; } inline void Server::wait_until_ready() const { - while (!is_running() && !done_) { + while (!is_running_ && !is_decommisioned) { std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } @@ -6143,8 +6144,11 @@ inline void Server::stop() { detail::shutdown_socket(sock); detail::close_socket(sock); } + is_decommisioned = false; } +inline void Server::decommission() { is_decommisioned = true; } + inline bool Server::parse_request_line(const char *s, Request &req) const { auto len = strlen(s); if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } @@ -6499,6 +6503,8 @@ Server::create_server_socket(const std::string &host, int port, inline int Server::bind_internal(const std::string &host, int port, int socket_flags) { + if (is_decommisioned) { return -1; } + if (!is_valid()) { return -1; } svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); @@ -6524,6 +6530,8 @@ inline int Server::bind_internal(const std::string &host, int port, } inline bool Server::listen_internal() { + if (is_decommisioned) { return false; } + auto ret = true; is_running_ = true; auto se = detail::scope_exit([&]() { is_running_ = false; }); @@ -6613,6 +6621,7 @@ inline bool Server::listen_internal() { task_queue->shutdown(); } + is_decommisioned = !ret; return ret; } diff --git a/test/test.cc b/test/test.cc index e13d3b9321..8c25ec0cd9 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4926,6 +4926,52 @@ TEST(ServerStopTest, ListenFailure) { t.join(); } +TEST(ServerStopTest, Decommision) { + Server svr; + + svr.Get("/hi", [&](const Request &, Response &res) { res.body = "hi..."; }); + + for (int i = 0; i < 4; i++) { + auto is_even = !(i % 2); + + std::thread t{[&] { + try { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + if (is_even) { + throw std::runtime_error("Some thing that happens to go wrong."); + } + + svr.listen(HOST, PORT); + } catch (...) { svr.decommission(); } + }}; + + svr.wait_until_ready(); + + // Server is up + { + Client cli(HOST, PORT); + auto res = cli.Get("/hi"); + if (is_even) { + EXPECT_FALSE(res); + } else { + EXPECT_TRUE(res); + EXPECT_EQ("hi...", res->body); + } + } + + svr.stop(); + t.join(); + + // Server is down... + { + Client cli(HOST, PORT); + auto res = cli.Get("/hi"); + EXPECT_FALSE(res); + } + } +} + TEST(StreamingTest, NoContentLengthStreaming) { Server svr; From 6cdd3493a147605290df5cc66dcc5b6f2a21db8f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 1 Sep 2024 01:55:27 -0400 Subject: [PATCH 0223/1222] Fix #1788 --- httplib.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/httplib.h b/httplib.h index 7b7b3f297d..97c71a97c9 100644 --- a/httplib.h +++ b/httplib.h @@ -7720,6 +7720,14 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, return ret; }; + if (res.has_header("Content-Length")) { + if (!req.content_receiver) { + auto len = std::min(res.get_header_value_u64("Content-Length"), + res.body.max_size()); + if (len > 0) { res.body.reserve(len); } + } + } + int dummy_status; if (!detail::read_content(strm, res, (std::numeric_limits::max)(), dummy_status, std::move(progress), std::move(out), From 88277139e721b14677d5fb5afa96451f3ddc7730 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 1 Sep 2024 07:52:24 -0400 Subject: [PATCH 0224/1222] Added `set_ipv6_v6only` method (#1905) * Added `set_ipv6_v6only` method * Adjust the place where socket_options is called --- httplib.h | 85 ++++++++++++++++++++++++++++++++-------------------- test/test.cc | 2 +- 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/httplib.h b/httplib.h index 97c71a97c9..d5f9e6c6de 100644 --- a/httplib.h +++ b/httplib.h @@ -90,6 +90,10 @@ #define CPPHTTPLIB_TCP_NODELAY false #endif +#ifndef CPPHTTPLIB_IPV6_V6ONLY +#define CPPHTTPLIB_IPV6_V6ONLY false +#endif + #ifndef CPPHTTPLIB_RECV_BUFSIZ #define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) #endif @@ -900,6 +904,7 @@ class Server { Server &set_address_family(int family); Server &set_tcp_nodelay(bool on); + Server &set_ipv6_v6only(bool on); Server &set_socket_options(SocketOptions socket_options); Server &set_default_headers(Headers headers); @@ -1040,6 +1045,7 @@ class Server { int address_family_ = AF_UNSPEC; bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; SocketOptions socket_options_ = default_socket_options; Headers default_headers_; @@ -1322,6 +1328,7 @@ class ClientImpl { void set_address_family(int family); void set_tcp_nodelay(bool on); + void set_ipv6_v6only(bool on); void set_socket_options(SocketOptions socket_options); void set_connection_timeout(time_t sec, time_t usec = 0); @@ -1459,6 +1466,7 @@ class ClientImpl { int address_family_ = AF_UNSPEC; bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; SocketOptions socket_options_ = nullptr; bool compress_ = false; @@ -1968,19 +1976,19 @@ inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { } inline void default_socket_options(socket_t sock) { - int yes = 1; + int opt = 1; #ifdef _WIN32 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&opt), sizeof(opt)); setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&opt), sizeof(opt)); #else #ifdef SO_REUSEPORT setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&opt), sizeof(opt)); #else setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&opt), sizeof(opt)); #endif #endif } @@ -2219,12 +2227,15 @@ bool process_client_socket(socket_t sock, time_t read_timeout_sec, time_t write_timeout_usec, std::function callback); -socket_t create_client_socket( - const std::string &host, const std::string &ip, int port, - int address_family, bool tcp_nodelay, SocketOptions socket_options, - time_t connection_timeout_sec, time_t connection_timeout_usec, - time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, const std::string &intf, Error &error); +socket_t create_client_socket(const std::string &host, const std::string &ip, + int port, int address_family, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + time_t connection_timeout_sec, + time_t connection_timeout_usec, + time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, + const std::string &intf, Error &error); const char *get_header_value(const Headers &headers, const std::string &key, const char *def, size_t id); @@ -3239,7 +3250,7 @@ inline int shutdown_socket(socket_t sock) { template socket_t create_socket(const std::string &host, const std::string &ip, int port, int address_family, int socket_flags, bool tcp_nodelay, - SocketOptions socket_options, + bool ipv6_v6only, SocketOptions socket_options, BindOrConnect bind_or_connect) { // Get address info const char *node = nullptr; @@ -3350,29 +3361,29 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #endif if (tcp_nodelay) { - auto yes = 1; + auto opt = 1; #ifdef _WIN32 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&opt), sizeof(opt)); #else setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&opt), sizeof(opt)); #endif } - if (socket_options) { socket_options(sock); } - if (rp->ai_family == AF_INET6) { - auto no = 0; + auto opt = ipv6_v6only ? 1 : 0; #ifdef _WIN32 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&no), sizeof(no)); + reinterpret_cast(&opt), sizeof(opt)); #else setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&no), sizeof(no)); + reinterpret_cast(&opt), sizeof(opt)); #endif } + if (socket_options) { socket_options(sock); } + // bind or connect auto quit = false; if (bind_or_connect(sock, *rp, quit)) { @@ -3477,12 +3488,14 @@ inline std::string if2ip(int address_family, const std::string &ifn) { inline socket_t create_client_socket( const std::string &host, const std::string &ip, int port, - int address_family, bool tcp_nodelay, SocketOptions socket_options, - time_t connection_timeout_sec, time_t connection_timeout_usec, - time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + int address_family, bool tcp_nodelay, bool ipv6_v6only, + SocketOptions socket_options, time_t connection_timeout_sec, + time_t connection_timeout_usec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, const std::string &intf, Error &error) { auto sock = create_socket( - host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options), + host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only, + std::move(socket_options), [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { if (!intf.empty()) { #ifdef USE_IF2IP @@ -6061,6 +6074,11 @@ inline Server &Server::set_tcp_nodelay(bool on) { return *this; } +inline Server &Server::set_ipv6_v6only(bool on) { + ipv6_v6only_ = on; + return *this; +} + inline Server &Server::set_socket_options(SocketOptions socket_options) { socket_options_ = std::move(socket_options); return *this; @@ -6491,7 +6509,7 @@ Server::create_server_socket(const std::string &host, int port, SocketOptions socket_options) const { return detail::create_socket( host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, - std::move(socket_options), + ipv6_v6only_, std::move(socket_options), [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { return false; @@ -7041,6 +7059,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { url_encode_ = rhs.url_encode_; address_family_ = rhs.address_family_; tcp_nodelay_ = rhs.tcp_nodelay_; + ipv6_v6only_ = rhs.ipv6_v6only_; socket_options_ = rhs.socket_options_; compress_ = rhs.compress_; decompress_ = rhs.decompress_; @@ -7069,9 +7088,9 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const { if (!proxy_host_.empty() && proxy_port_ != -1) { return detail::create_client_socket( proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, - socket_options_, connection_timeout_sec_, connection_timeout_usec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, interface_, error); + ipv6_v6only_, socket_options_, connection_timeout_sec_, + connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, interface_, error); } // Check is custom IP specified for host_ @@ -7080,10 +7099,10 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const { if (it != addr_map_.end()) { ip = it->second; } return detail::create_client_socket( - host_, ip, port_, address_family_, tcp_nodelay_, socket_options_, - connection_timeout_sec_, connection_timeout_usec_, read_timeout_sec_, - read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, interface_, - error); + host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_, + socket_options_, connection_timeout_sec_, connection_timeout_usec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, interface_, error); } inline bool ClientImpl::create_and_connect_socket(Socket &socket, @@ -8487,6 +8506,8 @@ inline void ClientImpl::set_address_family(int family) { inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } +inline void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; } + inline void ClientImpl::set_socket_options(SocketOptions socket_options) { socket_options_ = std::move(socket_options); } diff --git a/test/test.cc b/test/test.cc index 8c25ec0cd9..b2d565ac20 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4652,7 +4652,7 @@ static bool send_request(time_t read_timeout_sec, const std::string &req, auto error = Error::Success; auto client_sock = detail::create_client_socket( - HOST, "", PORT, AF_UNSPEC, false, nullptr, + HOST, "", PORT, AF_UNSPEC, false, false, nullptr, /*connection_timeout_sec=*/5, 0, /*read_timeout_sec=*/5, 0, /*write_timeout_sec=*/5, 0, std::string(), error); From 7f6d413ddd8304c8ff79c4cfaf89c7bcea8298a2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 1 Sep 2024 07:53:56 -0400 Subject: [PATCH 0225/1222] Release v0.17.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index d5f9e6c6de..f038a1f2d4 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.16.3" +#define CPPHTTPLIB_VERSION "0.17.0" /* * Configuration From 21c9a6a1fffd5b32b7956fd059dc6e5d91c7251e Mon Sep 17 00:00:00 2001 From: mol123 <60134413+mol123@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:01:05 +0200 Subject: [PATCH 0226/1222] Windows: simplify conditional compilation and fix call to CreateFileMappingW. (#1909) --- httplib.h | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/httplib.h b/httplib.h index f038a1f2d4..7782c380f8 100644 --- a/httplib.h +++ b/httplib.h @@ -2847,9 +2847,7 @@ inline bool mmap::open(const char *path) { wpath += path[i]; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ - WINAPI_PARTITION_GAMES) && \ - (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL); #else @@ -2859,26 +2857,22 @@ inline bool mmap::open(const char *path) { if (hFile_ == INVALID_HANDLE_VALUE) { return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ - WINAPI_PARTITION_GAMES) LARGE_INTEGER size{}; if (!::GetFileSizeEx(hFile_, &size)) { return false; } + // If the following line doesn't compile due to QuadPart, update Windows SDK. + // See: + // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 + if (size.QuadPart > std::numeric_limits::max()) { + // `size_t` might be 32-bits, on 32-bits Windows. + return false; + } size_ = static_cast(size.QuadPart); -#else - DWORD sizeHigh; - DWORD sizeLow; - sizeLow = ::GetFileSize(hFile_, &sizeHigh); - if (sizeLow == INVALID_FILE_SIZE) { return false; } - size_ = (static_cast(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow; -#endif -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ - (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); #else - hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart, - size.LowPart, NULL); + hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); #endif if (hMapping_ == NULL) { @@ -2886,8 +2880,7 @@ inline bool mmap::open(const char *path) { return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ - (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); #else addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); From 4f9c6540b2491da4903fedd876b461d0a485b0c3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 2 Sep 2024 20:33:33 -0400 Subject: [PATCH 0227/1222] Fixed warning --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 7782c380f8..0b482f44c9 100644 --- a/httplib.h +++ b/httplib.h @@ -2862,7 +2862,7 @@ inline bool mmap::open(const char *path) { // If the following line doesn't compile due to QuadPart, update Windows SDK. // See: // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 - if (size.QuadPart > std::numeric_limits::max()) { + if (static_cast(size.QuadPart) > std::numeric_limits::max()) { // `size_t` might be 32-bits, on 32-bits Windows. return false; } From 2514ebc20fbf0d584926a3ff74080ca5881bf54b Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 2 Sep 2024 20:38:01 -0400 Subject: [PATCH 0228/1222] Fix #1848 --- httplib.h | 4 ---- test/test.cc | 11 +++++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 0b482f44c9..29d09a73a0 100644 --- a/httplib.h +++ b/httplib.h @@ -2526,12 +2526,8 @@ inline std::string base64_encode(const std::string &in) { } inline bool is_file(const std::string &path) { -#ifdef _WIN32 - return _access_s(path.c_str(), 0) == 0; -#else struct stat st; return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); -#endif } inline bool is_dir(const std::string &path) { diff --git a/test/test.cc b/test/test.cc index b2d565ac20..d960ab5823 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7561,3 +7561,14 @@ TEST(UniversalClientImplTest, Ipv6LiteralAddress) { CLIENT_PRIVATE_KEY_FILE); EXPECT_EQ(cli.port(), port); } + +TEST(FileSystemTest, FileAndDirExistenceCheck) { + auto file_path = "./www/dir/index.html"; + auto dir_path = "./www/dir"; + + EXPECT_TRUE(detail::is_file(file_path)); + EXPECT_FALSE(detail::is_dir(file_path)); + + EXPECT_FALSE(detail::is_file(dir_path)); + EXPECT_TRUE(detail::is_dir(dir_path)); +} From ddfdacfa499e96ce106b21849550686dc6f90eb3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 2 Sep 2024 22:49:31 -0400 Subject: [PATCH 0229/1222] Fix build error --- test/test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.cc b/test/test.cc index d960ab5823..e15bf74e25 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7563,8 +7563,8 @@ TEST(UniversalClientImplTest, Ipv6LiteralAddress) { } TEST(FileSystemTest, FileAndDirExistenceCheck) { - auto file_path = "./www/dir/index.html"; - auto dir_path = "./www/dir"; + std::string file_path = "./www/dir/index.html"; + std::string dir_path = "./www/dir"; EXPECT_TRUE(detail::is_file(file_path)); EXPECT_FALSE(detail::is_dir(file_path)); From c5ee20877597e4b9a7082d111036aac636598ee0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 2 Sep 2024 23:04:38 -0400 Subject: [PATCH 0230/1222] Fix build error on Mac and Linux --- httplib.h | 24 ++++++++++++++---------- test/test.cc | 4 ++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index 29d09a73a0..542f62cc20 100644 --- a/httplib.h +++ b/httplib.h @@ -2198,6 +2198,10 @@ make_basic_authentication_header(const std::string &username, namespace detail { +bool is_file(const std::string &path); + +bool is_dir(const std::string &path); + std::string encode_query_param(const std::string &value); std::string decode_url(const std::string &s, bool convert_plus_to_space); @@ -2525,16 +2529,6 @@ inline std::string base64_encode(const std::string &in) { return out; } -inline bool is_file(const std::string &path) { - struct stat st; - return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); -} - -inline bool is_dir(const std::string &path) { - struct stat st; - return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); -} - inline bool is_valid_path(const std::string &path) { size_t level = 0; size_t i = 0; @@ -2577,6 +2571,16 @@ inline bool is_valid_path(const std::string &path) { return true; } +inline bool is_file(const std::string &path) { + struct stat st; + return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); +} + +inline bool is_dir(const std::string &path) { + struct stat st; + return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); +} + inline std::string encode_query_param(const std::string &value) { std::ostringstream escaped; escaped.fill('0'); diff --git a/test/test.cc b/test/test.cc index e15bf74e25..d960ab5823 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7563,8 +7563,8 @@ TEST(UniversalClientImplTest, Ipv6LiteralAddress) { } TEST(FileSystemTest, FileAndDirExistenceCheck) { - std::string file_path = "./www/dir/index.html"; - std::string dir_path = "./www/dir"; + auto file_path = "./www/dir/index.html"; + auto dir_path = "./www/dir"; EXPECT_TRUE(detail::is_file(file_path)); EXPECT_FALSE(detail::is_dir(file_path)); From b1f8e986bfe896a68bc3db464f399f3bd533430e Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Sep 2024 00:47:39 -0400 Subject: [PATCH 0231/1222] Fix #1908 (#1910) * Fix #1908 * Code format --- httplib.h | 29 +++++++++++++++++++++++++++-- test/test.cc | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 542f62cc20..b7be298e07 100644 --- a/httplib.h +++ b/httplib.h @@ -2790,6 +2790,10 @@ inline bool stream_line_reader::getline() { fixed_buffer_used_size_ = 0; glowable_buffer_.clear(); +#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + char prev_byte = 0; +#endif + for (size_t i = 0;; i++) { char byte; auto n = strm_.read(&byte, 1); @@ -2806,7 +2810,12 @@ inline bool stream_line_reader::getline() { append(byte); +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR if (byte == '\n') { break; } +#else + if (prev_byte == '\r' && byte == '\n') { break; } + prev_byte = byte; +#endif } return true; @@ -2862,7 +2871,8 @@ inline bool mmap::open(const char *path) { // If the following line doesn't compile due to QuadPart, update Windows SDK. // See: // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 - if (static_cast(size.QuadPart) > std::numeric_limits::max()) { + if (static_cast(size.QuadPart) > + std::numeric_limits::max()) { // `size_t` might be 32-bits, on 32-bits Windows. return false; } @@ -4049,7 +4059,22 @@ inline bool read_headers(Stream &strm, Headers &headers) { auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; parse_header(line_reader.ptr(), end, - [&](const std::string &key, const std::string &val) { + [&](const std::string &key, std::string &val) { + // NOTE: From RFC 9110: + // Field values containing CR, LF, or NUL characters are + // invalid and dangerous, due to the varying ways that + // implementations might parse and interpret those + // characters; a recipient of CR, LF, or NUL within a field + // value MUST either reject the message or replace each of + // those characters with SP before further processing or + // forwarding of that message. + for (auto &c : val) { + switch (c) { + case '\0': + case '\n': + case '\r': c = ' '; break; + } + } headers.emplace(key, val); }); } diff --git a/test/test.cc b/test/test.cc index d960ab5823..09a2eba084 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4718,6 +4718,11 @@ static void test_raw_request(const std::string &req, svr.Put("/put_hi", [&](const Request & /*req*/, Response &res) { res.set_content("ok", "text/plain"); }); + svr.Get("/header_field_value_check", [&](const Request &req, Response &res) { + auto val = req.get_header_value("Test"); + EXPECT_EQ("[ ]", val); + res.set_content("ok", "text/plain"); + }); // Server read timeout must be longer than the client read timeout for the // bug to reproduce, probably to force the server to process a request @@ -4851,6 +4856,12 @@ TEST(ServerRequestParsingTest, InvalidSpaceInURL) { EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); } +TEST(ServerRequestParsingTest, InvalidFieldValueContains_CR_LF_NUL) { + std::string request( + "GET /header_field_value_check HTTP/1.1\r\nTest: [\r\x00\n]\r\n\r\n", 55); + test_raw_request(request); +} + TEST(ServerStopTest, StopServerWithChunkedTransmission) { Server svr; @@ -7572,3 +7583,26 @@ TEST(FileSystemTest, FileAndDirExistenceCheck) { EXPECT_FALSE(detail::is_file(dir_path)); EXPECT_TRUE(detail::is_dir(dir_path)); } + +TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { + Server svr; + + svr.Get("/test", [&](const Request &req, Response &) { + auto val = req.get_header_value("Test"); + EXPECT_EQ(val.size(), 7u); + EXPECT_EQ(val, "_ _ _"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.Get("/test", {{"Test", "_\n\r_\n\r_"}}); +} From 4854a694cd8cb1b49ad9d7e5c803ec5c9179be42 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Sep 2024 17:29:28 -0400 Subject: [PATCH 0232/1222] Use IPPROTO_IP --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index b7be298e07..3185dd7405 100644 --- a/httplib.h +++ b/httplib.h @@ -3262,7 +3262,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; + hints.ai_protocol = IPPROTO_IP; if (!ip.empty()) { node = ip.c_str(); From 975cf0dae55142109c74bf6b721cc4f0c2042eda Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Sep 2024 18:00:12 -0400 Subject: [PATCH 0233/1222] Fix #1908 --- httplib.h | 37 ++++++++++++++++++------------------- test/test.cc | 14 ++++++-------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/httplib.h b/httplib.h index 3185dd7405..f0295bd5b3 100644 --- a/httplib.h +++ b/httplib.h @@ -4021,6 +4021,18 @@ inline bool parse_header(const char *beg, const char *end, T fn) { auto val = compare_case_ignore(key, "Location") ? std::string(p, end) : decode_url(std::string(p, end), false); + + // NOTE: From RFC 9110: + // Field values containing CR, LF, or NUL characters are + // invalid and dangerous, due to the varying ways that + // implementations might parse and interpret those + // characters; a recipient of CR, LF, or NUL within a field + // value MUST either reject the message or replace each of + // those characters with SP before further processing or + // forwarding of that message. + static const std::string CR_LF_NUL("\r\n\0", 3); + if (val.find_first_of(CR_LF_NUL) != std::string::npos) { return false; } + fn(key, val); return true; } @@ -4058,25 +4070,12 @@ inline bool read_headers(Stream &strm, Headers &headers) { // Exclude line terminator auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - parse_header(line_reader.ptr(), end, - [&](const std::string &key, std::string &val) { - // NOTE: From RFC 9110: - // Field values containing CR, LF, or NUL characters are - // invalid and dangerous, due to the varying ways that - // implementations might parse and interpret those - // characters; a recipient of CR, LF, or NUL within a field - // value MUST either reject the message or replace each of - // those characters with SP before further processing or - // forwarding of that message. - for (auto &c : val) { - switch (c) { - case '\0': - case '\n': - case '\r': c = ' '; break; - } - } - headers.emplace(key, val); - }); + if (!parse_header(line_reader.ptr(), end, + [&](const std::string &key, std::string &val) { + headers.emplace(key, val); + })) { + return false; + } } return true; diff --git a/test/test.cc b/test/test.cc index 09a2eba084..10e6080454 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4718,9 +4718,7 @@ static void test_raw_request(const std::string &req, svr.Put("/put_hi", [&](const Request & /*req*/, Response &res) { res.set_content("ok", "text/plain"); }); - svr.Get("/header_field_value_check", [&](const Request &req, Response &res) { - auto val = req.get_header_value("Test"); - EXPECT_EQ("[ ]", val); + svr.Get("/header_field_value_check", [&](const Request &/*req*/, Response &res) { res.set_content("ok", "text/plain"); }); @@ -4857,9 +4855,11 @@ TEST(ServerRequestParsingTest, InvalidSpaceInURL) { } TEST(ServerRequestParsingTest, InvalidFieldValueContains_CR_LF_NUL) { + std::string out; std::string request( "GET /header_field_value_check HTTP/1.1\r\nTest: [\r\x00\n]\r\n\r\n", 55); - test_raw_request(request); + test_raw_request(request, &out); + EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); } TEST(ServerStopTest, StopServerWithChunkedTransmission) { @@ -7587,10 +7587,8 @@ TEST(FileSystemTest, FileAndDirExistenceCheck) { TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { Server svr; - svr.Get("/test", [&](const Request &req, Response &) { - auto val = req.get_header_value("Test"); - EXPECT_EQ(val.size(), 7u); - EXPECT_EQ(val, "_ _ _"); + svr.Get("/test", [&](const Request &/*req*/, Response &res) { + EXPECT_EQ(res.status, 400); }); auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); From 4e6055f0840c61c74f4a0eb6dd1cf69d5aef7cbd Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Sep 2024 20:56:16 -0400 Subject: [PATCH 0234/1222] Fix problem with Abstract Namespace Unix Domain --- httplib.h | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index f0295bd5b3..5e21b3de68 100644 --- a/httplib.h +++ b/httplib.h @@ -3250,6 +3250,24 @@ inline int shutdown_socket(socket_t sock) { #endif } +inline std::string escape_abstract_namespace_unix_domain(const std::string& s) { + if (s.size() > 1 && s[0] == '\0') { + auto ret = s; + ret[0] = '@'; + return ret; + } + return s; +} + +inline std::string unescape_abstract_namespace_unix_domain(const std::string& s) { + if (s.size() > 1 && s[0] == '@') { + auto ret = s; + ret[0] = '\0'; + return ret; + } + return s; +} + template socket_t create_socket(const std::string &host, const std::string &ip, int port, int address_family, int socket_flags, bool tcp_nodelay, @@ -3290,7 +3308,9 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (sock != INVALID_SOCKET) { sockaddr_un addr{}; addr.sun_family = AF_UNIX; - std::copy(host.begin(), host.end(), addr.sun_path); + + auto unescaped_host = unescape_abstract_namespace_unix_domain(host); + std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); hints.ai_addr = reinterpret_cast(&addr); hints.ai_addrlen = static_cast( @@ -7044,9 +7064,10 @@ inline ClientImpl::ClientImpl(const std::string &host, int port) inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) - : host_(host), port_(port), - host_and_port_(adjust_host_string(host) + ":" + std::to_string(port)), - client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} + : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), + host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), + client_cert_path_(client_cert_path), client_key_path_(client_key_path) { + } inline ClientImpl::~ClientImpl() { std::lock_guard guard(socket_mutex_); From 87fab847b851024783d133f767b618b5001aa014 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Sep 2024 18:04:58 -0400 Subject: [PATCH 0235/1222] Fix SIGINT problem in Docker image --- Dockerfile | 3 +-- docker/main.cc | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c63941dd7..654845b2ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,7 @@ -FROM ubuntu AS builder +FROM yhirose4dockerhub/ubuntu-builder AS builder WORKDIR /build COPY httplib.h . COPY docker/main.cc . -RUN apt update && apt install g++ -y RUN g++ -std=c++23 -static -o server -O2 -I. -DCPPHTTPLIB_USE_POLL main.cc && strip server FROM scratch diff --git a/docker/main.cc b/docker/main.cc index 8c4968125d..512b2664a0 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -23,6 +23,8 @@ constexpr auto error_html = R"( )"; +void sigint_handler(int s) { exit(1); } + std::string time_local() { auto p = std::chrono::system_clock::now(); auto t = std::chrono::system_clock::to_time_t(p); @@ -49,6 +51,8 @@ std::string log(auto &req, auto &res) { } int main(int argc, const char **argv) { + signal(SIGINT, sigint_handler); + auto base_dir = "./html"; auto host = "0.0.0.0"; auto port = 80; From c88b09bc6be4300d07140c777719ba24fc6288a2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Sep 2024 21:20:57 -0400 Subject: [PATCH 0236/1222] Release v0.17.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5e21b3de68..567284f43c 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.17.0" +#define CPPHTTPLIB_VERSION "0.17.1" /* * Configuration From 7196ac8a07100da06cfe278719a5b96a1ddfee1b Mon Sep 17 00:00:00 2001 From: "Sung, Po Han" Date: Wed, 4 Sep 2024 17:38:05 +0800 Subject: [PATCH 0237/1222] Fix incorrect handling of Expect: 100-continue Fix #1808 --- httplib.h | 4 +- test/CMakeLists.txt | 4 +- test/test.cc | 100 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index b7be298e07..7c7bf04236 100644 --- a/httplib.h +++ b/httplib.h @@ -6956,7 +6956,9 @@ Server::process_request(Stream &strm, bool close_connection, strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, status_message(status)); break; - default: return write_response(strm, close_connection, req, res); + default: + connection_closed = true; + return write_response(strm, true, req, res); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d982253317..75dd978cd2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,9 +24,11 @@ else() FetchContent_MakeAvailable(gtest) endif() +find_package(curl REQUIRED) + add_executable(httplib-test test.cc) target_compile_options(httplib-test PRIVATE "$<$:/utf-8;/bigobj>") -target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main) +target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main CURL::libcurl) gtest_discover_tests(httplib-test) file( diff --git a/test/test.cc b/test/test.cc index 09a2eba084..c75cdd9f63 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -12,6 +13,7 @@ #include #include #include +#include #define SERVER_CERT_FILE "./cert.pem" #define SERVER_CERT2_FILE "./cert2.pem" @@ -7606,3 +7608,101 @@ TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { Client cli(HOST, PORT); cli.Get("/test", {{"Test", "_\n\r_\n\r_"}}); } + +TEST(Expect100ContinueTest, ServerClosesConnection) { + static constexpr char reject[] = "Unauthorized"; + static constexpr char accept[] = "Upload accepted"; + constexpr size_t total_size = 10 * 1024 * 1024 * 1024ULL; + + Server svr; + + svr.set_expect_100_continue_handler([](const Request &req, Response &res) { + res.status = StatusCode::Unauthorized_401; + res.set_content(reject, "text/plain"); + return res.status; + }); + svr.Post("/", [&](const Request & /*req*/, Response &res) { + res.set_content(accept, "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + { + const auto curl = std::unique_ptr{ + curl_easy_init(), &curl_easy_cleanup}; + ASSERT_NE(curl, nullptr); + + curl_easy_setopt(curl.get(), CURLOPT_URL, HOST); + curl_easy_setopt(curl.get(), CURLOPT_PORT, PORT); + curl_easy_setopt(curl.get(), CURLOPT_POST, 1L); + auto list = std::unique_ptr{ + curl_slist_append(nullptr, "Content-Type: application/octet-stream"), + &curl_slist_free_all}; + ASSERT_NE(list, nullptr); + curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, list.get()); + + struct read_data { + size_t read_size; + size_t total_size; + } data = {0, total_size}; + using read_callback_t = + size_t (*)(char *ptr, size_t size, size_t nmemb, void *userdata); + read_callback_t read_callback = [](char *ptr, size_t size, size_t nmemb, + void *userdata) -> size_t { + read_data *data = (read_data *)userdata; + + if (!userdata || data->read_size >= data->total_size) { return 0; } + + std::fill_n(ptr, size * nmemb, 'A'); + data->read_size += size * nmemb; + return size * nmemb; + }; + curl_easy_setopt(curl.get(), CURLOPT_READDATA, data); + curl_easy_setopt(curl.get(), CURLOPT_READFUNCTION, read_callback); + + std::vector buffer; + curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &buffer); + using write_callback_t = + size_t (*)(char *ptr, size_t size, size_t nmemb, void *userdata); + write_callback_t write_callback = [](char *ptr, size_t size, size_t nmemb, + void *userdata) -> size_t { + std::vector *buffer = (std::vector *)userdata; + buffer->reserve(buffer->size() + size * nmemb + 1); + buffer->insert(buffer->end(), (char *)ptr, (char *)ptr + size * nmemb); + return size * nmemb; + }; + curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback); + + { + const auto res = curl_easy_perform(curl.get()); + ASSERT_EQ(res, CURLE_OK); + } + + { + auto response_code = long{}; + const auto res = + curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &response_code); + ASSERT_EQ(res, CURLE_OK); + ASSERT_EQ(response_code, StatusCode::Unauthorized_401); + } + + { + auto dl = curl_off_t{}; + const auto res = curl_easy_getinfo(curl.get(), CURLINFO_SIZE_DOWNLOAD_T, &dl); + ASSERT_EQ(res, CURLE_OK); + ASSERT_EQ(dl, sizeof reject - 1); + } + + { + buffer.push_back('\0'); + ASSERT_STRCASEEQ(buffer.data(), reject); + } + } +} From 4c2a608a0c85c9a5f92417255e4ce641b3cfb51a Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 4 Sep 2024 09:06:27 -0400 Subject: [PATCH 0238/1222] Fix GitHub Actions errors --- .github/workflows/test.yaml | 4 ++-- test/Makefile | 2 +- test/test.cc | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 531fd4dccd..cf2104feee 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,8 +8,8 @@ jobs: steps: - name: checkout uses: actions/checkout@v4 - - name: install brotli - run: sudo apt-get update && sudo apt-get install -y libbrotli-dev + - name: install libraries + run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev - name: build and run tests run: cd test && make -j4 - name: run fuzz test target diff --git a/test/Makefile b/test/Makefile index 5468488e4f..96ebec9b56 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -TEST_ARGS = gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread +TEST_ARGS = gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl # By default, use standalone_fuzz_target_runner. # This runner does no fuzzing, but simply executes the inputs diff --git a/test/test.cc b/test/test.cc index 95b993eef1..231b290a48 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7614,7 +7614,7 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { Server svr; - svr.set_expect_100_continue_handler([](const Request &req, Response &res) { + svr.set_expect_100_continue_handler([](const Request &/*req*/, Response &res) { res.status = StatusCode::Unauthorized_401; res.set_content(reject, "text/plain"); return res.status; From bd1da4346abd74673a48ac6942bf8ab5de8c4603 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 4 Sep 2024 09:30:14 -0400 Subject: [PATCH 0239/1222] Disable Expect100ContinueTest test on Windows --- test/test.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test.cc b/test/test.cc index 231b290a48..e2ae142eb6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,7 +1,9 @@ #include #include +#ifndef _WIN32 #include +#endif #include #include @@ -7607,6 +7609,7 @@ TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { cli.Get("/test", {{"Test", "_\n\r_\n\r_"}}); } +#ifndef _WIN32 TEST(Expect100ContinueTest, ServerClosesConnection) { static constexpr char reject[] = "Unauthorized"; static constexpr char accept[] = "Upload accepted"; @@ -7704,3 +7707,4 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { } } } +#endif From d5fc340c3060712a00573630a2c88b401c0e9bcc Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 4 Sep 2024 12:23:48 -0400 Subject: [PATCH 0240/1222] Update README --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 7312c44d1f..640aa2ce4a 100644 --- a/README.md +++ b/README.md @@ -857,6 +857,19 @@ Dockerfile for static HTTP server is available. Port number of this HTTP server > docker build -t cpp-httplib-server . ... +> docker run --rm -it -p 8080:80 -v ./docker/html:/html cpp-httplib-server +Serving HTTP on 0.0.0.0 port 80 ... +192.168.65.1 - - [31/Aug/2024:21:33:56 +0000] "GET / HTTP/1.1" 200 599 "-" "curl/8.7.1" +192.168.65.1 - - [31/Aug/2024:21:34:26 +0000] "GET / HTTP/1.1" 200 599 "-" "Mozilla/5.0 ..." +192.168.65.1 - - [31/Aug/2024:21:34:26 +0000] "GET /favicon.ico HTTP/1.1" 404 152 "-" "Mozilla/5.0 ..." +``` + +From Docker Hub + +```bash +> docker run --rm -it -p 8080:80 -v ./docker/html:/html yhirose4dockerhub/cpp-httplib-server +... + > docker run --init --rm -it -p 8080:80 -v ./docker/html:/html cpp-httplib-server Serving HTTP on 0.0.0.0 port 80 ... 192.168.65.1 - - [31/Aug/2024:21:33:56 +0000] "GET / HTTP/1.1" 200 599 "-" "curl/8.7.1" From f69587656f647e53394f8c047f3c118e008046bc Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Thu, 5 Sep 2024 00:05:03 +0200 Subject: [PATCH 0241/1222] build(meson): add libcurl test dependency (#1914) Prompted by PR #1911 --- test/meson.build | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/meson.build b/test/meson.build index 52fb8251db..be9ca2b0f5 100644 --- a/test/meson.build +++ b/test/meson.build @@ -3,6 +3,7 @@ # SPDX-License-Identifier: MIT gtest_dep = dependency('gtest', main: true) +libcurl_dep = dependency('libcurl') openssl = find_program('openssl') test_conf = files('test.conf') @@ -119,7 +120,8 @@ test( 'test.cc', dependencies: [ cpp_httplib_dep, - gtest_dep + gtest_dep, + libcurl_dep ], override_options: test_options ), From 3d9cc51851e335602783ed65d0fb40d631682acf Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:01:32 -0400 Subject: [PATCH 0242/1222] Fixed build error on Windows due to `max` macro in windows.h --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5d9efb976f..d8d9b172b8 100644 --- a/httplib.h +++ b/httplib.h @@ -2872,7 +2872,7 @@ inline bool mmap::open(const char *path) { // See: // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 if (static_cast(size.QuadPart) > - std::numeric_limits::max()) { + (std::numeric_limits::max)()) { // `size_t` might be 32-bits, on 32-bits Windows. return false; } From 4fc0303bdafd5464261f9278390de9a6875fd6b5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:05:56 -0400 Subject: [PATCH 0243/1222] clangformat --- httplib.h | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index d8d9b172b8..f87365a557 100644 --- a/httplib.h +++ b/httplib.h @@ -3250,7 +3250,7 @@ inline int shutdown_socket(socket_t sock) { #endif } -inline std::string escape_abstract_namespace_unix_domain(const std::string& s) { +inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { if (s.size() > 1 && s[0] == '\0') { auto ret = s; ret[0] = '@'; @@ -3259,7 +3259,8 @@ inline std::string escape_abstract_namespace_unix_domain(const std::string& s) { return s; } -inline std::string unescape_abstract_namespace_unix_domain(const std::string& s) { +inline std::string +unescape_abstract_namespace_unix_domain(const std::string &s) { if (s.size() > 1 && s[0] == '@') { auto ret = s; ret[0] = '\0'; @@ -4904,16 +4905,6 @@ class MultipartFormDataParser { size_t buf_epos_ = 0; }; -inline std::string to_lower(const char *beg, const char *end) { - std::string out; - auto it = beg; - while (it != end) { - out += static_cast(::tolower(*it)); - it++; - } - return out; -} - inline std::string random_string(size_t length) { static const char data[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -7068,8 +7059,7 @@ inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_key_path) : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), - client_cert_path_(client_cert_path), client_key_path_(client_key_path) { - } + client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { std::lock_guard guard(socket_mutex_); From b4989130da0d0eef80b16d18bac6d96e99ad369d Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:09:38 -0400 Subject: [PATCH 0244/1222] Peformance improvement by removing `tolower` function call --- httplib.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index f87365a557..db5861a1e9 100644 --- a/httplib.h +++ b/httplib.h @@ -321,13 +321,50 @@ make_unique(std::size_t n) { return std::unique_ptr(new RT[n]); } -struct ci { +inline unsigned char to_lower(int c) { + const static unsigned char table[256] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, + 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, + 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, + 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, + 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, + 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226, + 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, + 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224, + 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, + 255, + }; + assert(0 <= c && c < 256); + return table[c]; +} + +struct case_ignore_equal { bool operator()(const std::string &s1, const std::string &s2) const { - return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), - s2.end(), - [](unsigned char c1, unsigned char c2) { - return ::tolower(c1) < ::tolower(c2); - }); + return s1.size() == s2.size() && + std::equal(s1.begin(), s1.end(), s2.begin(), [](char a, char b) { + return to_lower(a) == to_lower(b); + }); + } +}; + +struct case_ignore_hash { + constexpr size_t operator()(std::string_view key) const { + return hash_core(key.data(), key.size(), 0); + } + + constexpr size_t hash_core(const char *s, size_t l, size_t h) const { + return (l == 0) + ? h + : hash_core(s + 1, l - 1, + (h * 33) ^ static_cast(to_lower(*s))); } }; @@ -436,7 +473,9 @@ enum StatusCode { NetworkAuthenticationRequired_511 = 511, }; -using Headers = std::multimap; +using Headers = + std::unordered_multimap; using Params = std::multimap; using Match = std::smatch; @@ -4007,7 +4046,7 @@ inline const char *get_header_value(const Headers &headers, inline bool compare_case_ignore(const std::string &a, const std::string &b) { if (a.size() != b.size()) { return false; } for (size_t i = 0; i < b.size(); i++) { - if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + if (to_lower(a[i]) != to_lower(b[i])) { return false; } } return true; } @@ -4822,7 +4861,7 @@ class MultipartFormDataParser { const std::string &b) const { if (a.size() < b.size()) { return false; } for (size_t i = 0; i < b.size(); i++) { - if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + if (to_lower(a[i]) != to_lower(b[i])) { return false; } } return true; } From c75d07161501b63d225e53b42d7303ec91c41d93 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:22:46 -0400 Subject: [PATCH 0245/1222] Add benchmark tool --- .gitignore | 3 + benchmark/Makefile | 31 + benchmark/cpp-httplib/main.cpp | 12 + benchmark/crow/crow_all.h | 14316 +++++++++++++++++++++++++++++++ benchmark/crow/main.cpp | 17 + benchmark/flask/main.py | 9 + 6 files changed, 14388 insertions(+) create mode 100644 benchmark/Makefile create mode 100644 benchmark/cpp-httplib/main.cpp create mode 100644 benchmark/crow/crow_all.h create mode 100644 benchmark/crow/main.cpp create mode 100644 benchmark/flask/main.py diff --git a/.gitignore b/.gitignore index 065648162c..1939510319 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,8 @@ test/test.xcodeproj/*/xcuser* test/*.o test/*.pem test/*.srl +benchmark/server +benchmark/server-crow *.swp @@ -34,6 +36,7 @@ Release *.db ipch *.dSYM +*.pyc .* !/.gitattributes !/.travis.yml diff --git a/benchmark/Makefile b/benchmark/Makefile new file mode 100644 index 0000000000..f44b62298a --- /dev/null +++ b/benchmark/Makefile @@ -0,0 +1,31 @@ +CXXFLAGS = -std=c++14 -O2 -I.. + +THEAD_POOL_COUNT = 16 +BENCH_FLAGS = -c 8 -d 5s + +# cpp-httplib +bench: server + @./server & export PID=$$!; bombardier $(BENCH_FLAGS) localhost:8080; kill $${PID} + +server : cpp-httplib/main.cpp ../httplib.h + g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib/main.cpp + +run : server + @./server + +# crow +server-crow : crow/main.cpp + g++ -o $@ $(CXXFLAGS) crow/main.cpp + +bench-crow: server-crow + @./server-crow & export PID=$$!; bombardier $(BENCH_FLAGS) localhost:8080; kill $${PID} + +# flask +bench-flask: + @FLASK_APP=flask/main.py flask run --port=8080 & export PID=$$!; bombardier $(BENCH_FLAGS) localhost:8080; kill $${PID} + +# misc +bench-all: bench bench-crow bench-flask + +clean: + rm -rf server* diff --git a/benchmark/cpp-httplib/main.cpp b/benchmark/cpp-httplib/main.cpp new file mode 100644 index 0000000000..ab2e757b1b --- /dev/null +++ b/benchmark/cpp-httplib/main.cpp @@ -0,0 +1,12 @@ +#include "httplib.h" +using namespace httplib; + +int main() { + Server svr; + + svr.Get("/", [](const Request &, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + svr.listen("0.0.0.0", 8080); +} diff --git a/benchmark/crow/crow_all.h b/benchmark/crow/crow_all.h new file mode 100644 index 0000000000..0465f96dc8 --- /dev/null +++ b/benchmark/crow/crow_all.h @@ -0,0 +1,14316 @@ +// SPDX-License-Identifier: BSD-3-Clause AND ISC AND MIT +/*BSD 3-Clause License + +Copyright (c) 2014-2017, ipkn + 2020-2022, CrowCpp +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The Crow logo and other graphic material (excluding third party logos) used are under exclusive Copyright (c) 2021-2022, Farook Al-Sammarraie (The-EDev), All rights reserved. +*/ +#pragma once +// This file is generated from nginx/conf/mime.types using nginx_mime2cpp.py on 2021-12-03. +#include +#include + +namespace crow +{ + const std::unordered_map mime_types{ + {"shtml", "text/html"}, + {"htm", "text/html"}, + {"html", "text/html"}, + {"css", "text/css"}, + {"xml", "text/xml"}, + {"gif", "image/gif"}, + {"jpg", "image/jpeg"}, + {"jpeg", "image/jpeg"}, + {"js", "application/javascript"}, + {"atom", "application/atom+xml"}, + {"rss", "application/rss+xml"}, + {"mml", "text/mathml"}, + {"txt", "text/plain"}, + {"jad", "text/vnd.sun.j2me.app-descriptor"}, + {"wml", "text/vnd.wap.wml"}, + {"htc", "text/x-component"}, + {"avif", "image/avif"}, + {"png", "image/png"}, + {"svgz", "image/svg+xml"}, + {"svg", "image/svg+xml"}, + {"tiff", "image/tiff"}, + {"tif", "image/tiff"}, + {"wbmp", "image/vnd.wap.wbmp"}, + {"webp", "image/webp"}, + {"ico", "image/x-icon"}, + {"jng", "image/x-jng"}, + {"bmp", "image/x-ms-bmp"}, + {"woff", "font/woff"}, + {"woff2", "font/woff2"}, + {"ear", "application/java-archive"}, + {"war", "application/java-archive"}, + {"jar", "application/java-archive"}, + {"json", "application/json"}, + {"hqx", "application/mac-binhex40"}, + {"doc", "application/msword"}, + {"pdf", "application/pdf"}, + {"ai", "application/postscript"}, + {"eps", "application/postscript"}, + {"ps", "application/postscript"}, + {"rtf", "application/rtf"}, + {"m3u8", "application/vnd.apple.mpegurl"}, + {"kml", "application/vnd.google-earth.kml+xml"}, + {"kmz", "application/vnd.google-earth.kmz"}, + {"xls", "application/vnd.ms-excel"}, + {"eot", "application/vnd.ms-fontobject"}, + {"ppt", "application/vnd.ms-powerpoint"}, + {"odg", "application/vnd.oasis.opendocument.graphics"}, + {"odp", "application/vnd.oasis.opendocument.presentation"}, + {"ods", "application/vnd.oasis.opendocument.spreadsheet"}, + {"odt", "application/vnd.oasis.opendocument.text"}, + {"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {"wmlc", "application/vnd.wap.wmlc"}, + {"wasm", "application/wasm"}, + {"7z", "application/x-7z-compressed"}, + {"cco", "application/x-cocoa"}, + {"jardiff", "application/x-java-archive-diff"}, + {"jnlp", "application/x-java-jnlp-file"}, + {"run", "application/x-makeself"}, + {"pm", "application/x-perl"}, + {"pl", "application/x-perl"}, + {"pdb", "application/x-pilot"}, + {"prc", "application/x-pilot"}, + {"rar", "application/x-rar-compressed"}, + {"rpm", "application/x-redhat-package-manager"}, + {"sea", "application/x-sea"}, + {"swf", "application/x-shockwave-flash"}, + {"sit", "application/x-stuffit"}, + {"tk", "application/x-tcl"}, + {"tcl", "application/x-tcl"}, + {"crt", "application/x-x509-ca-cert"}, + {"pem", "application/x-x509-ca-cert"}, + {"der", "application/x-x509-ca-cert"}, + {"xpi", "application/x-xpinstall"}, + {"xhtml", "application/xhtml+xml"}, + {"xspf", "application/xspf+xml"}, + {"zip", "application/zip"}, + {"dll", "application/octet-stream"}, + {"exe", "application/octet-stream"}, + {"bin", "application/octet-stream"}, + {"deb", "application/octet-stream"}, + {"dmg", "application/octet-stream"}, + {"img", "application/octet-stream"}, + {"iso", "application/octet-stream"}, + {"msm", "application/octet-stream"}, + {"msp", "application/octet-stream"}, + {"msi", "application/octet-stream"}, + {"kar", "audio/midi"}, + {"midi", "audio/midi"}, + {"mid", "audio/midi"}, + {"mp3", "audio/mpeg"}, + {"ogg", "audio/ogg"}, + {"m4a", "audio/x-m4a"}, + {"ra", "audio/x-realaudio"}, + {"3gp", "video/3gpp"}, + {"3gpp", "video/3gpp"}, + {"ts", "video/mp2t"}, + {"mp4", "video/mp4"}, + {"mpg", "video/mpeg"}, + {"mpeg", "video/mpeg"}, + {"mov", "video/quicktime"}, + {"webm", "video/webm"}, + {"flv", "video/x-flv"}, + {"m4v", "video/x-m4v"}, + {"mng", "video/x-mng"}, + {"asf", "video/x-ms-asf"}, + {"asx", "video/x-ms-asf"}, + {"wmv", "video/x-ms-wmv"}, + {"avi", "video/x-msvideo"}}; +} + + +#include + +namespace crow +{ + /// An abstract class that allows any other class to be returned by a handler. + struct returnable + { + std::string content_type; + virtual std::string dump() const = 0; + + returnable(std::string ctype): + content_type{ctype} + {} + + virtual ~returnable(){}; + }; +} // namespace crow + + +#include +#include +#include +#include +#include +#include +#include + +namespace crow +{ + +// ---------------------------------------------------------------------------- +// qs_parse (modified) +// https://github.com/bartgrantham/qs_parse +// ---------------------------------------------------------------------------- +/* Similar to strncmp, but handles URL-encoding for either string */ +int qs_strncmp(const char* s, const char* qs, size_t n); + + +/* Finds the beginning of each key/value pair and stores a pointer in qs_kv. + * Also decodes the value portion of the k/v pair *in-place*. In a future + * enhancement it will also have a compile-time option of sorting qs_kv + * alphabetically by key. */ +size_t qs_parse(char* qs, char* qs_kv[], size_t qs_kv_size, bool parse_url); + + +/* Used by qs_parse to decode the value portion of a k/v pair */ +int qs_decode(char * qs); + + +/* Looks up the value according to the key on a pre-processed query string + * A future enhancement will be a compile-time option to look up the key + * in a pre-sorted qs_kv array via a binary search. */ +//char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size); + char * qs_k2v(const char * key, char * const * qs_kv, size_t qs_kv_size, int nth); + + +/* Non-destructive lookup of value, based on key. User provides the + * destinaton string and length. */ +char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len); + +// TODO: implement sorting of the qs_kv array; for now ensure it's not compiled +#undef _qsSORTING + +// isxdigit _is_ available in , but let's avoid another header instead +#define CROW_QS_ISHEX(x) ((((x)>='0'&&(x)<='9') || ((x)>='A'&&(x)<='F') || ((x)>='a'&&(x)<='f')) ? 1 : 0) +#define CROW_QS_HEX2DEC(x) (((x)>='0'&&(x)<='9') ? (x)-48 : ((x)>='A'&&(x)<='F') ? (x)-55 : ((x)>='a'&&(x)<='f') ? (x)-87 : 0) +#define CROW_QS_ISQSCHR(x) ((((x)=='=')||((x)=='#')||((x)=='&')||((x)=='\0')) ? 0 : 1) + +inline int qs_strncmp(const char * s, const char * qs, size_t n) +{ + unsigned char u1, u2, unyb, lnyb; + + while(n-- > 0) + { + u1 = static_cast(*s++); + u2 = static_cast(*qs++); + + if ( ! CROW_QS_ISQSCHR(u1) ) { u1 = '\0'; } + if ( ! CROW_QS_ISQSCHR(u2) ) { u2 = '\0'; } + + if ( u1 == '+' ) { u1 = ' '; } + if ( u1 == '%' ) // easier/safer than scanf + { + unyb = static_cast(*s++); + lnyb = static_cast(*s++); + if ( CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb) ) + u1 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb); + else + u1 = '\0'; + } + + if ( u2 == '+' ) { u2 = ' '; } + if ( u2 == '%' ) // easier/safer than scanf + { + unyb = static_cast(*qs++); + lnyb = static_cast(*qs++); + if ( CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb) ) + u2 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb); + else + u2 = '\0'; + } + + if ( u1 != u2 ) + return u1 - u2; + if ( u1 == '\0' ) + return 0; + } + if ( CROW_QS_ISQSCHR(*qs) ) + return -1; + else + return 0; +} + + +inline size_t qs_parse(char* qs, char* qs_kv[], size_t qs_kv_size, bool parse_url = true) +{ + size_t i, j; + char * substr_ptr; + + for(i=0; i means x iterations of this loop -> means *x+1* k/v pairs + substr_ptr += j + 1; + i++; + } + + // we only decode the values in place, the keys could have '='s in them + // which will hose our ability to distinguish keys from values later + for(j=0; j> qs_dict_name2kv(const char * dict_name, char * const * qs_kv, size_t qs_kv_size, int nth = 0) +{ + size_t i; + size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close; + + name_len = strlen(dict_name); + +#ifdef _qsSORTING +// TODO: binary search for key in the sorted qs_kv +#else // _qsSORTING + for(i=0; i 0 && + skip_to_brace_close > 0 && + nth == 0 ) + { + auto key = std::string(qs_kv[i] + skip_to_brace_open, skip_to_brace_close - skip_to_brace_open); + auto value = std::string(qs_kv[i] + skip_to_eq); + return std::unique_ptr>(new std::pair(key, value)); + } + else + { + --nth; + } + } + } +#endif // _qsSORTING + + return nullptr; +} + + +inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len) +{ + size_t i, key_len; + const char * tmp; + + // find the beginning of the k/v substrings + if ( (tmp = strchr(qs, '?')) != NULL ) + qs = tmp + 1; + + key_len = strlen(key); + while(qs[0] != '#' && qs[0] != '\0') + { + if ( qs_strncmp(key, qs, key_len) == 0 ) + break; + qs += strcspn(qs, "&") + 1; + } + + if ( qs[0] == '\0' ) return NULL; + + qs += strcspn(qs, "=&#"); + if ( qs[0] == '=' ) + { + qs++; + i = strcspn(qs, "&=#"); +#ifdef _MSC_VER + strncpy_s(val, val_len, qs, (val_len - 1)<(i + 1) ? (val_len - 1) : (i + 1)); +#else + strncpy(val, qs, (val_len - 1)<(i + 1) ? (val_len - 1) : (i + 1)); +#endif + qs_decode(val); + } + else + { + if ( val_len > 0 ) + val[0] = '\0'; + } + + return val; +} +} +// ---------------------------------------------------------------------------- + + +namespace crow +{ + struct request; + /// A class to represent any data coming after the `?` in the request URL into key-value pairs. + class query_string + { + public: + static const int MAX_KEY_VALUE_PAIRS_COUNT = 256; + + query_string() = default; + + query_string(const query_string& qs): + url_(qs.url_) + { + for (auto p : qs.key_value_pairs_) + { + key_value_pairs_.push_back((char*)(p - qs.url_.c_str() + url_.c_str())); + } + } + + query_string& operator=(const query_string& qs) + { + url_ = qs.url_; + key_value_pairs_.clear(); + for (auto p : qs.key_value_pairs_) + { + key_value_pairs_.push_back((char*)(p - qs.url_.c_str() + url_.c_str())); + } + return *this; + } + + query_string& operator=(query_string&& qs) noexcept + { + key_value_pairs_ = std::move(qs.key_value_pairs_); + char* old_data = (char*)qs.url_.c_str(); + url_ = std::move(qs.url_); + for (auto& p : key_value_pairs_) + { + p += (char*)url_.c_str() - old_data; + } + return *this; + } + + + query_string(std::string params, bool url = true): + url_(std::move(params)) + { + if (url_.empty()) + return; + + key_value_pairs_.resize(MAX_KEY_VALUE_PAIRS_COUNT); + size_t count = qs_parse(&url_[0], &key_value_pairs_[0], MAX_KEY_VALUE_PAIRS_COUNT, url); + + key_value_pairs_.resize(count); + key_value_pairs_.shrink_to_fit(); + } + + void clear() + { + key_value_pairs_.clear(); + url_.clear(); + } + + friend std::ostream& operator<<(std::ostream& os, const query_string& qs) + { + os << "[ "; + for (size_t i = 0; i < qs.key_value_pairs_.size(); ++i) + { + if (i) + os << ", "; + os << qs.key_value_pairs_[i]; + } + os << " ]"; + return os; + } + + /// Get a value from a name, used for `?name=value`. + + /// + /// Note: this method returns the value of the first occurrence of the key only, to return all occurrences, see \ref get_list(). + char* get(const std::string& name) const + { + char* ret = qs_k2v(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size()); + return ret; + } + + /// Works similar to \ref get() except it removes the item from the query string. + char* pop(const std::string& name) + { + char* ret = get(name); + if (ret != nullptr) + { + for (unsigned int i = 0; i < key_value_pairs_.size(); i++) + { + std::string str_item(key_value_pairs_[i]); + if (str_item.substr(0, name.size() + 1) == name + '=') + { + key_value_pairs_.erase(key_value_pairs_.begin() + i); + break; + } + } + } + return ret; + } + + /// Returns a list of values, passed as `?name[]=value1&name[]=value2&...name[]=valuen` with n being the size of the list. + + /// + /// Note: Square brackets in the above example are controlled by `use_brackets` boolean (true by default). If set to false, the example becomes `?name=value1,name=value2...name=valuen` + std::vector get_list(const std::string& name, bool use_brackets = true) const + { + std::vector ret; + std::string plus = name + (use_brackets ? "[]" : ""); + char* element = nullptr; + + int count = 0; + while (1) + { + element = qs_k2v(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++); + if (!element) + break; + ret.push_back(element); + } + return ret; + } + + /// Similar to \ref get_list() but it removes the + std::vector pop_list(const std::string& name, bool use_brackets = true) + { + std::vector ret = get_list(name, use_brackets); + if (!ret.empty()) + { + for (unsigned int i = 0; i < key_value_pairs_.size(); i++) + { + std::string str_item(key_value_pairs_[i]); + if ((use_brackets ? (str_item.substr(0, name.size() + 3) == name + "[]=") : (str_item.substr(0, name.size() + 1) == name + '='))) + { + key_value_pairs_.erase(key_value_pairs_.begin() + i--); + } + } + } + return ret; + } + + /// Works similar to \ref get_list() except the brackets are mandatory must not be empty. + + /// + /// For example calling `get_dict(yourname)` on `?yourname[sub1]=42&yourname[sub2]=84` would give a map containing `{sub1 : 42, sub2 : 84}`. + /// + /// if your query string has both empty brackets and ones with a key inside, use pop_list() to get all the values without a key before running this method. + std::unordered_map get_dict(const std::string& name) const + { + std::unordered_map ret; + + int count = 0; + while (1) + { + if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++)) + ret.insert(*element); + else + break; + } + return ret; + } + + /// Works the same as \ref get_dict() but removes the values from the query string. + std::unordered_map pop_dict(const std::string& name) + { + std::unordered_map ret = get_dict(name); + if (!ret.empty()) + { + for (unsigned int i = 0; i < key_value_pairs_.size(); i++) + { + std::string str_item(key_value_pairs_[i]); + if (str_item.substr(0, name.size() + 1) == name + '[') + { + key_value_pairs_.erase(key_value_pairs_.begin() + i--); + } + } + } + return ret; + } + + std::vector keys() const + { + std::vector keys; + keys.reserve(key_value_pairs_.size()); + + for (const char* const element : key_value_pairs_) + { + const char* delimiter = strchr(element, '='); + if (delimiter) + keys.emplace_back(element, delimiter); + else + keys.emplace_back(element); + } + + return keys; + } + + private: + std::string url_; + std::vector key_value_pairs_; + }; + +} // namespace crow + +#ifdef CROW_ENABLE_COMPRESSION + +#include +#include + +// http://zlib.net/manual.html +namespace crow // NOTE: Already documented in "crow/app.h" +{ + namespace compression + { + // Values used in the 'windowBits' parameter for deflateInit2. + enum algorithm + { + // 15 is the default value for deflate + DEFLATE = 15, + // windowBits can also be greater than 15 for optional gzip encoding. + // Add 16 to windowBits to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper. + GZIP = 15 | 16, + }; + + inline std::string compress_string(std::string const& str, algorithm algo) + { + std::string compressed_str; + z_stream stream{}; + // Initialize with the default values + if (::deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, algo, 8, Z_DEFAULT_STRATEGY) == Z_OK) + { + char buffer[8192]; + + stream.avail_in = str.size(); + // zlib does not take a const pointer. The data is not altered. + stream.next_in = const_cast(reinterpret_cast(str.c_str())); + + int code = Z_OK; + do + { + stream.avail_out = sizeof(buffer); + stream.next_out = reinterpret_cast(&buffer[0]); + + code = ::deflate(&stream, Z_FINISH); + // Successful and non-fatal error code returned by deflate when used with Z_FINISH flush + if (code == Z_OK || code == Z_STREAM_END) + { + std::copy(&buffer[0], &buffer[sizeof(buffer) - stream.avail_out], std::back_inserter(compressed_str)); + } + + } while (code == Z_OK); + + if (code != Z_STREAM_END) + compressed_str.clear(); + + ::deflateEnd(&stream); + } + + return compressed_str; + } + + inline std::string decompress_string(std::string const& deflated_string) + { + std::string inflated_string; + Bytef tmp[8192]; + + z_stream zstream{}; + zstream.avail_in = deflated_string.size(); + // Nasty const_cast but zlib won't alter its contents + zstream.next_in = const_cast(reinterpret_cast(deflated_string.c_str())); + // Initialize with automatic header detection, for gzip support + if (::inflateInit2(&zstream, MAX_WBITS | 32) == Z_OK) + { + do + { + zstream.avail_out = sizeof(tmp); + zstream.next_out = &tmp[0]; + + auto ret = ::inflate(&zstream, Z_NO_FLUSH); + if (ret == Z_OK || ret == Z_STREAM_END) + { + std::copy(&tmp[0], &tmp[sizeof(tmp) - zstream.avail_out], std::back_inserter(inflated_string)); + } + else + { + // Something went wrong with inflate; make sure we return an empty string + inflated_string.clear(); + break; + } + + } while (zstream.avail_out == 0); + + // Free zlib's internal memory + ::inflateEnd(&zstream); + } + + return inflated_string; + } + } // namespace compression +} // namespace crow + +#endif + +/* + * SHA1 Wikipedia Page: http://en.wikipedia.org/wiki/SHA-1 + * + * Copyright (c) 2012-22 SAURAV MOHAPATRA + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/** + * \file TinySHA1.hpp + * \author SAURAV MOHAPATRA + * \date 2012-22 + * \brief TinySHA1 - a header only implementation of the SHA1 algorithm in C++. Based + * on the implementation in boost::uuid::details. + * + * In this file are defined: + * - sha1::SHA1 + */ +#ifndef _TINY_SHA1_HPP_ +#define _TINY_SHA1_HPP_ +#include +#include +#include +#include + +/** + * \namespace sha1 + * \brief Here is defined the SHA1 class + */ +namespace sha1 +{ + /** + * \class SHA1 + * \brief A tiny SHA1 algorithm implementation used internally in the + * Crow server (specifically in crow/websocket.h). + */ + class SHA1 + { + public: + typedef uint32_t digest32_t[5]; + typedef uint8_t digest8_t[20]; + inline static uint32_t LeftRotate(uint32_t value, size_t count) { + return (value << count) ^ (value >> (32-count)); + } + SHA1(){ reset(); } + virtual ~SHA1() {} + SHA1(const SHA1& s) { *this = s; } + const SHA1& operator = (const SHA1& s) { + memcpy(m_digest, s.m_digest, 5 * sizeof(uint32_t)); + memcpy(m_block, s.m_block, 64); + m_blockByteIndex = s.m_blockByteIndex; + m_byteCount = s.m_byteCount; + return *this; + } + SHA1& reset() { + m_digest[0] = 0x67452301; + m_digest[1] = 0xEFCDAB89; + m_digest[2] = 0x98BADCFE; + m_digest[3] = 0x10325476; + m_digest[4] = 0xC3D2E1F0; + m_blockByteIndex = 0; + m_byteCount = 0; + return *this; + } + SHA1& processByte(uint8_t octet) { + this->m_block[this->m_blockByteIndex++] = octet; + ++this->m_byteCount; + if(m_blockByteIndex == 64) { + this->m_blockByteIndex = 0; + processBlock(); + } + return *this; + } + SHA1& processBlock(const void* const start, const void* const end) { + const uint8_t* begin = static_cast(start); + const uint8_t* finish = static_cast(end); + while(begin != finish) { + processByte(*begin); + begin++; + } + return *this; + } + SHA1& processBytes(const void* const data, size_t len) { + const uint8_t* block = static_cast(data); + processBlock(block, block + len); + return *this; + } + const uint32_t* getDigest(digest32_t digest) { + size_t bitCount = this->m_byteCount * 8; + processByte(0x80); + if (this->m_blockByteIndex > 56) { + while (m_blockByteIndex != 0) { + processByte(0); + } + while (m_blockByteIndex < 56) { + processByte(0); + } + } else { + while (m_blockByteIndex < 56) { + processByte(0); + } + } + processByte(0); + processByte(0); + processByte(0); + processByte(0); + processByte( static_cast((bitCount>>24) & 0xFF)); + processByte( static_cast((bitCount>>16) & 0xFF)); + processByte( static_cast((bitCount>>8 ) & 0xFF)); + processByte( static_cast((bitCount) & 0xFF)); + + memcpy(digest, m_digest, 5 * sizeof(uint32_t)); + return digest; + } + const uint8_t* getDigestBytes(digest8_t digest) { + digest32_t d32; + getDigest(d32); + size_t di = 0; + digest[di++] = ((d32[0] >> 24) & 0xFF); + digest[di++] = ((d32[0] >> 16) & 0xFF); + digest[di++] = ((d32[0] >> 8) & 0xFF); + digest[di++] = ((d32[0]) & 0xFF); + + digest[di++] = ((d32[1] >> 24) & 0xFF); + digest[di++] = ((d32[1] >> 16) & 0xFF); + digest[di++] = ((d32[1] >> 8) & 0xFF); + digest[di++] = ((d32[1]) & 0xFF); + + digest[di++] = ((d32[2] >> 24) & 0xFF); + digest[di++] = ((d32[2] >> 16) & 0xFF); + digest[di++] = ((d32[2] >> 8) & 0xFF); + digest[di++] = ((d32[2]) & 0xFF); + + digest[di++] = ((d32[3] >> 24) & 0xFF); + digest[di++] = ((d32[3] >> 16) & 0xFF); + digest[di++] = ((d32[3] >> 8) & 0xFF); + digest[di++] = ((d32[3]) & 0xFF); + + digest[di++] = ((d32[4] >> 24) & 0xFF); + digest[di++] = ((d32[4] >> 16) & 0xFF); + digest[di++] = ((d32[4] >> 8) & 0xFF); + digest[di++] = ((d32[4]) & 0xFF); + return digest; + } + + protected: + void processBlock() { + uint32_t w[80]; + for (size_t i = 0; i < 16; i++) { + w[i] = (m_block[i*4 + 0] << 24); + w[i] |= (m_block[i*4 + 1] << 16); + w[i] |= (m_block[i*4 + 2] << 8); + w[i] |= (m_block[i*4 + 3]); + } + for (size_t i = 16; i < 80; i++) { + w[i] = LeftRotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1); + } + + uint32_t a = m_digest[0]; + uint32_t b = m_digest[1]; + uint32_t c = m_digest[2]; + uint32_t d = m_digest[3]; + uint32_t e = m_digest[4]; + + for (std::size_t i=0; i<80; ++i) { + uint32_t f = 0; + uint32_t k = 0; + + if (i<20) { + f = (b & c) | (~b & d); + k = 0x5A827999; + } else if (i<40) { + f = b ^ c ^ d; + k = 0x6ED9EBA1; + } else if (i<60) { + f = (b & c) | (b & d) | (c & d); + k = 0x8F1BBCDC; + } else { + f = b ^ c ^ d; + k = 0xCA62C1D6; + } + uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i]; + e = d; + d = c; + c = LeftRotate(b, 30); + b = a; + a = temp; + } + + m_digest[0] += a; + m_digest[1] += b; + m_digest[2] += c; + m_digest[3] += d; + m_digest[4] += e; + } + private: + digest32_t m_digest; + uint8_t m_block[64]; + size_t m_blockByteIndex; + size_t m_byteCount; + }; +} +#endif + +// settings for crow +// TODO(ipkn) replace with runtime config. libucl? + +/* #ifdef - enables debug mode */ +//#define CROW_ENABLE_DEBUG + +/* #ifdef - enables logging */ +#define CROW_ENABLE_LOGGING + +/* #ifdef - enforces section 5.2 and 6.1 of RFC6455 (only accepting masked messages from clients) */ +//#define CROW_ENFORCE_WS_SPEC + +/* #define - specifies log level */ +/* + Debug = 0 + Info = 1 + Warning = 2 + Error = 3 + Critical = 4 + + default to INFO +*/ +#ifndef CROW_LOG_LEVEL +#define CROW_LOG_LEVEL 1 +#endif + +#ifndef CROW_STATIC_DIRECTORY +#define CROW_STATIC_DIRECTORY "static/" +#endif +#ifndef CROW_STATIC_ENDPOINT +#define CROW_STATIC_ENDPOINT "/static/" +#endif + +// compiler flags +#if defined(_MSVC_LANG) && _MSVC_LANG >= 201402L +#define CROW_CAN_USE_CPP14 +#endif +#if __cplusplus >= 201402L +#define CROW_CAN_USE_CPP14 +#endif + +#if defined(_MSVC_LANG) && _MSVC_LANG >= 201703L +#define CROW_CAN_USE_CPP17 +#endif +#if __cplusplus >= 201703L +#define CROW_CAN_USE_CPP17 +#if defined(__GNUC__) && __GNUC__ < 8 +#define CROW_FILESYSTEM_IS_EXPERIMENTAL +#endif +#endif + +#if defined(_MSC_VER) +#if _MSC_VER < 1900 +#define CROW_MSVC_WORKAROUND +#define constexpr const +#define noexcept throw() +#endif +#endif + +#if defined(__GNUC__) && __GNUC__ == 8 && __GNUC_MINOR__ < 4 +#if __cplusplus > 201103L +#define CROW_GCC83_WORKAROUND +#else +#error "GCC 8.1 - 8.3 has a bug that prevents Crow from compiling with C++11. Please update GCC to > 8.3 or use C++ > 11." +#endif +#endif + + +#ifdef CROW_USE_BOOST +#include +#include +#ifdef CROW_ENABLE_SSL +#include +#endif +#else +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include +#include +#ifdef CROW_ENABLE_SSL +#include +#endif +#endif + +#if (CROW_USE_BOOST && BOOST_VERSION >= 107000) || (ASIO_VERSION >= 101300) +#define GET_IO_SERVICE(s) ((asio::io_context&)(s).get_executor().context()) +#else +#define GET_IO_SERVICE(s) ((s).get_io_service()) +#endif + +namespace crow +{ +#ifdef CROW_USE_BOOST + namespace asio = boost::asio; + using error_code = boost::system::error_code; +#else + using error_code = asio::error_code; +#endif + using tcp = asio::ip::tcp; + + /// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream + struct SocketAdaptor + { + using context = void; + SocketAdaptor(asio::io_service& io_service, context*): + socket_(io_service) + {} + + asio::io_service& get_io_service() + { + return GET_IO_SERVICE(socket_); + } + + /// Get the TCP socket handling data trasfers, regardless of what layer is handling transfers on top of the socket. + tcp::socket& raw_socket() + { + return socket_; + } + + /// Get the object handling data transfers, this can be either a TCP socket or an SSL stream (if SSL is enabled). + tcp::socket& socket() + { + return socket_; + } + + tcp::endpoint remote_endpoint() + { + return socket_.remote_endpoint(); + } + + bool is_open() + { + return socket_.is_open(); + } + + void close() + { + error_code ec; + socket_.close(ec); + } + + void shutdown_readwrite() + { + error_code ec; + socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec); + } + + void shutdown_write() + { + error_code ec; + socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec); + } + + void shutdown_read() + { + error_code ec; + socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec); + } + + template + void start(F f) + { + f(error_code()); + } + + tcp::socket socket_; + }; + +#ifdef CROW_ENABLE_SSL + struct SSLAdaptor + { + using context = asio::ssl::context; + using ssl_socket_t = asio::ssl::stream; + SSLAdaptor(asio::io_service& io_service, context* ctx): + ssl_socket_(new ssl_socket_t(io_service, *ctx)) + {} + + asio::ssl::stream& socket() + { + return *ssl_socket_; + } + + tcp::socket::lowest_layer_type& + raw_socket() + { + return ssl_socket_->lowest_layer(); + } + + tcp::endpoint remote_endpoint() + { + return raw_socket().remote_endpoint(); + } + + bool is_open() + { + return ssl_socket_ ? raw_socket().is_open() : false; + } + + void close() + { + if (is_open()) + { + error_code ec; + raw_socket().close(ec); + } + } + + void shutdown_readwrite() + { + if (is_open()) + { + error_code ec; + raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec); + } + } + + void shutdown_write() + { + if (is_open()) + { + error_code ec; + raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec); + } + } + + void shutdown_read() + { + if (is_open()) + { + error_code ec; + raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec); + } + } + + asio::io_service& get_io_service() + { + return GET_IO_SERVICE(raw_socket()); + } + + template + void start(F f) + { + ssl_socket_->async_handshake(asio::ssl::stream_base::server, + [f](const error_code& ec) { + f(ec); + }); + } + + std::unique_ptr> ssl_socket_; + }; +#endif +} // namespace crow + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#if defined(CROW_CAN_USE_CPP17) && !defined(CROW_FILESYSTEM_IS_EXPERIMENTAL) +#include +#endif + +// TODO(EDev): Adding C++20's [[likely]] and [[unlikely]] attributes might be useful +#if defined(__GNUG__) || defined(__clang__) +#define CROW_LIKELY(X) __builtin_expect(!!(X), 1) +#define CROW_UNLIKELY(X) __builtin_expect(!!(X), 0) +#else +#define CROW_LIKELY(X) (X) +#define CROW_UNLIKELY(X) (X) +#endif + +namespace crow +{ + /// @cond SKIP + namespace black_magic + { +#ifndef CROW_MSVC_WORKAROUND + /// Out of Range Exception for const_str + struct OutOfRange + { + OutOfRange(unsigned /*pos*/, unsigned /*length*/) {} + }; + /// Helper function to throw an exception if i is larger than len + constexpr unsigned requires_in_range(unsigned i, unsigned len) + { + return i >= len ? throw OutOfRange(i, len) : i; + } + + /// A constant string implementation. + class const_str + { + const char* const begin_; + unsigned size_; + + public: + template + constexpr const_str(const char (&arr)[N]): + begin_(arr), size_(N - 1) + { + static_assert(N >= 1, "not a string literal"); + } + constexpr char operator[](unsigned i) const + { + return requires_in_range(i, size_), begin_[i]; + } + + constexpr operator const char*() const + { + return begin_; + } + + constexpr const char* begin() const { return begin_; } + constexpr const char* end() const { return begin_ + size_; } + + constexpr unsigned size() const + { + return size_; + } + }; + + constexpr unsigned find_closing_tag(const_str s, unsigned p) + { + return s[p] == '>' ? p : find_closing_tag(s, p + 1); + } + + /// Check that the CROW_ROUTE string is valid + constexpr bool is_valid(const_str s, unsigned i = 0, int f = 0) + { + return i == s.size() ? f == 0 : + f < 0 || f >= 2 ? false : + s[i] == '<' ? is_valid(s, i + 1, f + 1) : + s[i] == '>' ? is_valid(s, i + 1, f - 1) : + is_valid(s, i + 1, f); + } + + constexpr bool is_equ_p(const char* a, const char* b, unsigned n) + { + return *a == 0 && *b == 0 && n == 0 ? true : + (*a == 0 || *b == 0) ? false : + n == 0 ? true : + *a != *b ? false : + is_equ_p(a + 1, b + 1, n - 1); + } + + constexpr bool is_equ_n(const_str a, unsigned ai, const_str b, unsigned bi, unsigned n) + { + return ai + n > a.size() || bi + n > b.size() ? false : + n == 0 ? true : + a[ai] != b[bi] ? false : + is_equ_n(a, ai + 1, b, bi + 1, n - 1); + } + + constexpr bool is_int(const_str s, unsigned i) + { + return is_equ_n(s, i, "", 0, 5); + } + + constexpr bool is_uint(const_str s, unsigned i) + { + return is_equ_n(s, i, "", 0, 6); + } + + constexpr bool is_float(const_str s, unsigned i) + { + return is_equ_n(s, i, "", 0, 7) || + is_equ_n(s, i, "", 0, 8); + } + + constexpr bool is_str(const_str s, unsigned i) + { + return is_equ_n(s, i, "", 0, 5) || + is_equ_n(s, i, "", 0, 8); + } + + constexpr bool is_path(const_str s, unsigned i) + { + return is_equ_n(s, i, "", 0, 6); + } +#endif + template + struct parameter_tag + { + static const int value = 0; + }; +#define CROW_INTERNAL_PARAMETER_TAG(t, i) \ + template<> \ + struct parameter_tag \ + { \ + static const int value = i; \ + } + CROW_INTERNAL_PARAMETER_TAG(int, 1); + CROW_INTERNAL_PARAMETER_TAG(char, 1); + CROW_INTERNAL_PARAMETER_TAG(short, 1); + CROW_INTERNAL_PARAMETER_TAG(long, 1); + CROW_INTERNAL_PARAMETER_TAG(long long, 1); + CROW_INTERNAL_PARAMETER_TAG(unsigned int, 2); + CROW_INTERNAL_PARAMETER_TAG(unsigned char, 2); + CROW_INTERNAL_PARAMETER_TAG(unsigned short, 2); + CROW_INTERNAL_PARAMETER_TAG(unsigned long, 2); + CROW_INTERNAL_PARAMETER_TAG(unsigned long long, 2); + CROW_INTERNAL_PARAMETER_TAG(double, 3); + CROW_INTERNAL_PARAMETER_TAG(std::string, 4); +#undef CROW_INTERNAL_PARAMETER_TAG + template + struct compute_parameter_tag_from_args_list; + + template<> + struct compute_parameter_tag_from_args_list<> + { + static const int value = 0; + }; + + template + struct compute_parameter_tag_from_args_list + { + static const int sub_value = + compute_parameter_tag_from_args_list::value; + static const int value = + parameter_tag::type>::value ? sub_value * 6 + parameter_tag::type>::value : sub_value; + }; + + static inline bool is_parameter_tag_compatible(uint64_t a, uint64_t b) + { + if (a == 0) + return b == 0; + if (b == 0) + return a == 0; + int sa = a % 6; + int sb = a % 6; + if (sa == 5) sa = 4; + if (sb == 5) sb = 4; + if (sa != sb) + return false; + return is_parameter_tag_compatible(a / 6, b / 6); + } + + static inline unsigned find_closing_tag_runtime(const char* s, unsigned p) + { + return s[p] == 0 ? throw std::runtime_error("unmatched tag <") : + s[p] == '>' ? p : + find_closing_tag_runtime(s, p + 1); + } + + static inline uint64_t get_parameter_tag_runtime(const char* s, unsigned p = 0) + { + return s[p] == 0 ? 0 : + s[p] == '<' ? ( + std::strncmp(s + p, "", 5) == 0 ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 1 : + std::strncmp(s + p, "", 6) == 0 ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 2 : + (std::strncmp(s + p, "", 7) == 0 || + std::strncmp(s + p, "", 8) == 0) ? + get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 3 : + (std::strncmp(s + p, "", 5) == 0 || + std::strncmp(s + p, "", 8) == 0) ? + get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 4 : + std::strncmp(s + p, "", 6) == 0 ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 5 : + throw std::runtime_error("invalid parameter type")) : + get_parameter_tag_runtime(s, p + 1); + } +#ifndef CROW_MSVC_WORKAROUND + constexpr uint64_t get_parameter_tag(const_str s, unsigned p = 0) + { + return p == s.size() ? 0 : + s[p] == '<' ? ( + is_int(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1 : + is_uint(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 2 : + is_float(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 3 : + is_str(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 4 : + is_path(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 5 : + throw std::runtime_error("invalid parameter type")) : + get_parameter_tag(s, p + 1); + } +#endif + + template + struct S + { + template + using push = S; + template + using push_back = S; + template class U> + using rebind = U; + }; + + // Check whether the template function can be called with specific arguments + template + struct CallHelper; + template + struct CallHelper> + { + template()(std::declval()...))> + static char __test(int); + + template + static int __test(...); + + static constexpr bool value = sizeof(__test(0)) == sizeof(char); + }; + + // Check Tuple contains type T + template + struct has_type; + + template + struct has_type> : std::false_type + {}; + + template + struct has_type> : has_type> + {}; + + template + struct has_type> : std::true_type + {}; + + // Find index of type in tuple + template + struct tuple_index; + + template + struct tuple_index> + { + static const int value = 0; + }; + + template + struct tuple_index> + { + static const int value = 1 + tuple_index>::value; + }; + + // Extract element from forward tuple or get default +#ifdef CROW_CAN_USE_CPP14 + template + typename std::enable_if::value, typename std::decay::type&&>::type + tuple_extract(Tup& tup) + { + return std::move(std::get(tup)); + } +#else + template + typename std::enable_if::value, T&&>::type + tuple_extract(Tup& tup) + { + return std::move(std::get::value>(tup)); + } +#endif + + template + typename std::enable_if::value, T>::type + tuple_extract(Tup&) + { + return T{}; + } + + // Kind of fold expressions in C++11 + template + struct bool_pack; + template + using all_true = std::is_same, bool_pack>; + + template + struct single_tag_to_type + {}; + + template<> + struct single_tag_to_type<1> + { + using type = int64_t; + }; + + template<> + struct single_tag_to_type<2> + { + using type = uint64_t; + }; + + template<> + struct single_tag_to_type<3> + { + using type = double; + }; + + template<> + struct single_tag_to_type<4> + { + using type = std::string; + }; + + template<> + struct single_tag_to_type<5> + { + using type = std::string; + }; + + + template + struct arguments + { + using subarguments = typename arguments::type; + using type = + typename subarguments::template push::type>; + }; + + template<> + struct arguments<0> + { + using type = S<>; + }; + + template + struct last_element_type + { + using type = typename std::tuple_element>::type; + }; + + + template<> + struct last_element_type<> + {}; + + + // from http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth + template + using Invoke = typename T::type; + + template + struct seq + { + using type = seq; + }; + + template + struct concat; + + template + struct concat, seq> : seq + {}; + + template + using Concat = Invoke>; + + template + struct gen_seq; + template + using GenSeq = Invoke>; + + template + struct gen_seq : Concat, GenSeq> + {}; + + template<> + struct gen_seq<0> : seq<> + {}; + template<> + struct gen_seq<1> : seq<0> + {}; + + template + struct pop_back_helper; + + template + struct pop_back_helper, Tuple> + { + template class U> + using rebind = U::type...>; + }; + + template + struct pop_back //: public pop_back_helper::type, std::tuple> + { + template class U> + using rebind = typename pop_back_helper::type, std::tuple>::template rebind; + }; + + template<> + struct pop_back<> + { + template class U> + using rebind = U<>; + }; + + // from http://stackoverflow.com/questions/2118541/check-if-c0x-parameter-pack-contains-a-type + template + struct contains : std::true_type + {}; + + template + struct contains : std::conditional::value, std::true_type, contains>::type + {}; + + template + struct contains : std::false_type + {}; + + template + struct empty_context + {}; + + template + struct promote + { + using type = T; + }; + +#define CROW_INTERNAL_PROMOTE_TYPE(t1, t2) \ + template<> \ + struct promote \ + { \ + using type = t2; \ + } + + CROW_INTERNAL_PROMOTE_TYPE(char, int64_t); + CROW_INTERNAL_PROMOTE_TYPE(short, int64_t); + CROW_INTERNAL_PROMOTE_TYPE(int, int64_t); + CROW_INTERNAL_PROMOTE_TYPE(long, int64_t); + CROW_INTERNAL_PROMOTE_TYPE(long long, int64_t); + CROW_INTERNAL_PROMOTE_TYPE(unsigned char, uint64_t); + CROW_INTERNAL_PROMOTE_TYPE(unsigned short, uint64_t); + CROW_INTERNAL_PROMOTE_TYPE(unsigned int, uint64_t); + CROW_INTERNAL_PROMOTE_TYPE(unsigned long, uint64_t); + CROW_INTERNAL_PROMOTE_TYPE(unsigned long long, uint64_t); + CROW_INTERNAL_PROMOTE_TYPE(float, double); +#undef CROW_INTERNAL_PROMOTE_TYPE + + template + using promote_t = typename promote::type; + + } // namespace black_magic + + namespace detail + { + + template + struct get_index_of_element_from_tuple_by_type_impl + { + static constexpr auto value = N; + }; + + template + struct get_index_of_element_from_tuple_by_type_impl + { + static constexpr auto value = N; + }; + + template + struct get_index_of_element_from_tuple_by_type_impl + { + static constexpr auto value = get_index_of_element_from_tuple_by_type_impl::value; + }; + } // namespace detail + + namespace utility + { + template + T& get_element_by_type(std::tuple& t) + { + return std::get::value>(t); + } + + template + struct function_traits; + +#ifndef CROW_MSVC_WORKAROUND + template + struct function_traits : public function_traits + { + using parent_t = function_traits; + static const size_t arity = parent_t::arity; + using result_type = typename parent_t::result_type; + template + using arg = typename parent_t::template arg; + }; +#endif + + template + struct function_traits + { + static const size_t arity = sizeof...(Args); + + typedef R result_type; + + template + using arg = typename std::tuple_element>::type; + }; + + template + struct function_traits + { + static const size_t arity = sizeof...(Args); + + typedef R result_type; + + template + using arg = typename std::tuple_element>::type; + }; + + template + struct function_traits> + { + static const size_t arity = sizeof...(Args); + + typedef R result_type; + + template + using arg = typename std::tuple_element>::type; + }; + /// @endcond + + inline static std::string base64encode(const unsigned char* data, size_t size, const char* key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") + { + std::string ret; + ret.resize((size + 2) / 3 * 4); + auto it = ret.begin(); + while (size >= 3) + { + *it++ = key[(static_cast(*data) & 0xFC) >> 2]; + unsigned char h = (static_cast(*data++) & 0x03) << 4; + *it++ = key[h | ((static_cast(*data) & 0xF0) >> 4)]; + h = (static_cast(*data++) & 0x0F) << 2; + *it++ = key[h | ((static_cast(*data) & 0xC0) >> 6)]; + *it++ = key[static_cast(*data++) & 0x3F]; + + size -= 3; + } + if (size == 1) + { + *it++ = key[(static_cast(*data) & 0xFC) >> 2]; + unsigned char h = (static_cast(*data++) & 0x03) << 4; + *it++ = key[h]; + *it++ = '='; + *it++ = '='; + } + else if (size == 2) + { + *it++ = key[(static_cast(*data) & 0xFC) >> 2]; + unsigned char h = (static_cast(*data++) & 0x03) << 4; + *it++ = key[h | ((static_cast(*data) & 0xF0) >> 4)]; + h = (static_cast(*data++) & 0x0F) << 2; + *it++ = key[h]; + *it++ = '='; + } + return ret; + } + + inline static std::string base64encode(std::string data, size_t size, const char* key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") + { + return base64encode((const unsigned char*)data.c_str(), size, key); + } + + inline static std::string base64encode_urlsafe(const unsigned char* data, size_t size) + { + return base64encode(data, size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); + } + + inline static std::string base64encode_urlsafe(std::string data, size_t size) + { + return base64encode((const unsigned char*)data.c_str(), size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); + } + + inline static std::string base64decode(const char* data, size_t size) + { + // We accept both regular and url encoding here, as there does not seem to be any downside to that. + // If we want to distinguish that we should use +/ for non-url and -_ for url. + + // Mapping logic from characters to [0-63] + auto key = [](char c) -> unsigned char { + if ((c >= 'A') && (c <= 'Z')) return c - 'A'; + if ((c >= 'a') && (c <= 'z')) return c - 'a' + 26; + if ((c >= '0') && (c <= '9')) return c - '0' + 52; + if ((c == '+') || (c == '-')) return 62; + if ((c == '/') || (c == '_')) return 63; + return 0; + }; + + // Not padded + if (size % 4 == 2) // missing last 2 characters + size = (size / 4 * 3) + 1; // Not subtracting extra characters because they're truncated in int division + else if (size % 4 == 3) // missing last character + size = (size / 4 * 3) + 2; // Not subtracting extra characters because they're truncated in int division + + // Padded + else if (data[size - 2] == '=') // padded with '==' + size = (size / 4 * 3) - 2; // == padding means the last block only has 1 character instead of 3, hence the '-2' + else if (data[size - 1] == '=') // padded with '=' + size = (size / 4 * 3) - 1; // = padding means the last block only has 2 character instead of 3, hence the '-1' + + // Padding not needed + else + size = size / 4 * 3; + + std::string ret; + ret.resize(size); + auto it = ret.begin(); + + // These will be used to decode 1 character at a time + unsigned char odd; // char1 and char3 + unsigned char even; // char2 and char4 + + // Take 4 character blocks to turn into 3 + while (size >= 3) + { + // dec_char1 = (char1 shifted 2 bits to the left) OR ((char2 AND 00110000) shifted 4 bits to the right)) + odd = key(*data++); + even = key(*data++); + *it++ = (odd << 2) | ((even & 0x30) >> 4); + // dec_char2 = ((char2 AND 00001111) shifted 4 bits left) OR ((char3 AND 00111100) shifted 2 bits right)) + odd = key(*data++); + *it++ = ((even & 0x0F) << 4) | ((odd & 0x3C) >> 2); + // dec_char3 = ((char3 AND 00000011) shifted 6 bits left) OR (char4) + even = key(*data++); + *it++ = ((odd & 0x03) << 6) | (even); + + size -= 3; + } + if (size == 2) + { + // d_char1 = (char1 shifted 2 bits to the left) OR ((char2 AND 00110000) shifted 4 bits to the right)) + odd = key(*data++); + even = key(*data++); + *it++ = (odd << 2) | ((even & 0x30) >> 4); + // d_char2 = ((char2 AND 00001111) shifted 4 bits left) OR ((char3 AND 00111100) shifted 2 bits right)) + odd = key(*data++); + *it++ = ((even & 0x0F) << 4) | ((odd & 0x3C) >> 2); + } + else if (size == 1) + { + // d_char1 = (char1 shifted 2 bits to the left) OR ((char2 AND 00110000) shifted 4 bits to the right)) + odd = key(*data++); + even = key(*data++); + *it++ = (odd << 2) | ((even & 0x30) >> 4); + } + return ret; + } + + inline static std::string base64decode(const std::string& data, size_t size) + { + return base64decode(data.data(), size); + } + + inline static std::string base64decode(const std::string& data) + { + return base64decode(data.data(), data.length()); + } + + inline static std::string normalize_path(const std::string& directoryPath) + { + std::string normalizedPath = directoryPath; + std::replace(normalizedPath.begin(), normalizedPath.end(), '\\', '/'); + if (!normalizedPath.empty() && normalizedPath.back() != '/') + normalizedPath += '/'; + return normalizedPath; + } + + inline static void sanitize_filename(std::string& data, char replacement = '_') + { + if (data.length() > 255) + data.resize(255); + + static const auto toUpper = [](char c) { + return ((c >= 'a') && (c <= 'z')) ? (c - ('a' - 'A')) : c; + }; + // Check for special device names. The Windows behavior is really odd here, it will consider both AUX and AUX.txt + // a special device. Thus we search for the string (case-insensitive), and then check if the string ends or if + // is has a dangerous follow up character (.:\/) + auto sanitizeSpecialFile = [](std::string& source, unsigned ofs, const char* pattern, bool includeNumber, char replacement) { + unsigned i = ofs; + size_t len = source.length(); + const char* p = pattern; + while (*p) + { + if (i >= len) return; + if (toUpper(source[i]) != *p) return; + ++i; + ++p; + } + if (includeNumber) + { + if ((i >= len) || (source[i] < '1') || (source[i] > '9')) return; + ++i; + } + if ((i >= len) || (source[i] == '.') || (source[i] == ':') || (source[i] == '/') || (source[i] == '\\')) + { + source.erase(ofs + 1, (i - ofs) - 1); + source[ofs] = replacement; + } + }; + bool checkForSpecialEntries = true; + for (unsigned i = 0; i < data.length(); ++i) + { + // Recognize directory traversals and the special devices CON/PRN/AUX/NULL/COM[1-]/LPT[1-9] + if (checkForSpecialEntries) + { + checkForSpecialEntries = false; + switch (toUpper(data[i])) + { + case 'A': + sanitizeSpecialFile(data, i, "AUX", false, replacement); + break; + case 'C': + sanitizeSpecialFile(data, i, "CON", false, replacement); + sanitizeSpecialFile(data, i, "COM", true, replacement); + break; + case 'L': + sanitizeSpecialFile(data, i, "LPT", true, replacement); + break; + case 'N': + sanitizeSpecialFile(data, i, "NUL", false, replacement); + break; + case 'P': + sanitizeSpecialFile(data, i, "PRN", false, replacement); + break; + case '.': + sanitizeSpecialFile(data, i, "..", false, replacement); + break; + } + } + + // Sanitize individual characters + unsigned char c = data[i]; + if ((c < ' ') || ((c >= 0x80) && (c <= 0x9F)) || (c == '?') || (c == '<') || (c == '>') || (c == ':') || (c == '*') || (c == '|') || (c == '\"')) + { + data[i] = replacement; + } + else if ((c == '/') || (c == '\\')) + { + if (CROW_UNLIKELY(i == 0)) //Prevent Unix Absolute Paths (Windows Absolute Paths are prevented with `(c == ':')`) + { + data[i] = replacement; + } + else + { + checkForSpecialEntries = true; + } + } + } + } + + inline static std::string random_alphanum(std::size_t size) + { + static const char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist(0, sizeof(alphabet) - 2); + std::string out; + out.reserve(size); + for (std::size_t i = 0; i < size; i++) + out.push_back(alphabet[dist(rng)]); + return out; + } + + inline static std::string join_path(std::string path, const std::string& fname) + { +#if defined(CROW_CAN_USE_CPP17) && !defined(CROW_FILESYSTEM_IS_EXPERIMENTAL) + return (std::filesystem::path(path) / fname).string(); +#else + if (!(path.back() == '/' || path.back() == '\\')) + path += '/'; + path += fname; + return path; +#endif + } + + /** + * @brief Checks two string for equality. + * Always returns false if strings differ in size. + * Defaults to case-insensitive comparison. + */ + inline static bool string_equals(const std::string& l, const std::string& r, bool case_sensitive = false) + { + if (l.length() != r.length()) + return false; + + for (size_t i = 0; i < l.length(); i++) + { + if (case_sensitive) + { + if (l[i] != r[i]) + return false; + } + else + { + if (std::toupper(l[i]) != std::toupper(r[i])) + return false; + } + } + + return true; + } + + template + inline static T lexical_cast(const U& v) + { + std::stringstream stream; + T res; + + stream << v; + stream >> res; + + return res; + } + + template + inline static T lexical_cast(const char* v, size_t count) + { + std::stringstream stream; + T res; + + stream.write(v, count); + stream >> res; + + return res; + } + + + /// Return a copy of the given string with its + /// leading and trailing whitespaces removed. + inline static std::string trim(const std::string& v) + { + if (v.empty()) + return ""; + + size_t begin = 0, end = v.length(); + + size_t i; + for (i = 0; i < v.length(); i++) + { + if (!std::isspace(v[i])) + { + begin = i; + break; + } + } + + if (i == v.length()) + return ""; + + for (i = v.length(); i > 0; i--) + { + if (!std::isspace(v[i - 1])) + { + end = i; + break; + } + } + + return v.substr(begin, end - begin); + } + } // namespace utility +} // namespace crow + + +#include +#include + +namespace crow +{ + /// Hashing function for ci_map (unordered_multimap). + struct ci_hash + { + size_t operator()(const std::string& key) const + { + std::size_t seed = 0; + std::locale locale; + + for (auto c : key) + hash_combine(seed, std::toupper(c, locale)); + + return seed; + } + + private: + static inline void hash_combine(std::size_t& seed, char v) + { + std::hash hasher; + seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } + }; + + /// Equals function for ci_map (unordered_multimap). + struct ci_key_eq + { + bool operator()(const std::string& l, const std::string& r) const + { + return utility::string_equals(l, r); + } + }; + + using ci_map = std::unordered_multimap; +} // namespace crow + + +#include +#include +#include +#include + +namespace crow +{ + const char cr = '\r'; + const char lf = '\n'; + const std::string crlf("\r\n"); + + enum class HTTPMethod : char + { +#ifndef DELETE + DELETE = 0, + GET, + HEAD, + POST, + PUT, + + CONNECT, + OPTIONS, + TRACE, + + PATCH, + PURGE, + + COPY, + LOCK, + MKCOL, + MOVE, + PROPFIND, + PROPPATCH, + SEARCH, + UNLOCK, + BIND, + REBIND, + UNBIND, + ACL, + + REPORT, + MKACTIVITY, + CHECKOUT, + MERGE, + + MSEARCH, + NOTIFY, + SUBSCRIBE, + UNSUBSCRIBE, + + MKCALENDAR, + + LINK, + UNLINK, + + SOURCE, +#endif + + Delete = 0, + Get, + Head, + Post, + Put, + + Connect, + Options, + Trace, + + Patch, + Purge, + + Copy, + Lock, + MkCol, + Move, + Propfind, + Proppatch, + Search, + Unlock, + Bind, + Rebind, + Unbind, + Acl, + + Report, + MkActivity, + Checkout, + Merge, + + MSearch, + Notify, + Subscribe, + Unsubscribe, + + MkCalendar, + + Link, + Unlink, + + Source, + + + InternalMethodCount, + // should not add an item below this line: used for array count + }; + + constexpr const char* method_strings[] = + { + "DELETE", + "GET", + "HEAD", + "POST", + "PUT", + + "CONNECT", + "OPTIONS", + "TRACE", + + "PATCH", + "PURGE", + + "COPY", + "LOCK", + "MKCOL", + "MOVE", + "PROPFIND", + "PROPPATCH", + "SEARCH", + "UNLOCK", + "BIND", + "REBIND", + "UNBIND", + "ACL", + + "REPORT", + "MKACTIVITY", + "CHECKOUT", + "MERGE", + + "M-SEARCH", + "NOTIFY", + "SUBSCRIBE", + "UNSUBSCRIBE", + + "MKCALENDAR", + + "LINK", + "UNLINK", + + "SOURCE"}; + + + inline std::string method_name(HTTPMethod method) + { + if (CROW_LIKELY(method < HTTPMethod::InternalMethodCount)) + { + return method_strings[(unsigned char)method]; + } + return "invalid"; + } + + // clang-format off + + enum status + { + CONTINUE = 100, + SWITCHING_PROTOCOLS = 101, + + OK = 200, + CREATED = 201, + ACCEPTED = 202, + NON_AUTHORITATIVE_INFORMATION = 203, + NO_CONTENT = 204, + RESET_CONTENT = 205, + PARTIAL_CONTENT = 206, + + MULTIPLE_CHOICES = 300, + MOVED_PERMANENTLY = 301, + FOUND = 302, + SEE_OTHER = 303, + NOT_MODIFIED = 304, + TEMPORARY_REDIRECT = 307, + PERMANENT_REDIRECT = 308, + + BAD_REQUEST = 400, + UNAUTHORIZED = 401, + FORBIDDEN = 403, + NOT_FOUND = 404, + METHOD_NOT_ALLOWED = 405, + NOT_ACCEPTABLE = 406, + PROXY_AUTHENTICATION_REQUIRED = 407, + CONFLICT = 409, + GONE = 410, + PAYLOAD_TOO_LARGE = 413, + UNSUPPORTED_MEDIA_TYPE = 415, + RANGE_NOT_SATISFIABLE = 416, + EXPECTATION_FAILED = 417, + PRECONDITION_REQUIRED = 428, + TOO_MANY_REQUESTS = 429, + UNAVAILABLE_FOR_LEGAL_REASONS = 451, + + INTERNAL_SERVER_ERROR = 500, + NOT_IMPLEMENTED = 501, + BAD_GATEWAY = 502, + SERVICE_UNAVAILABLE = 503, + GATEWAY_TIMEOUT = 504, + VARIANT_ALSO_NEGOTIATES = 506 + }; + + // clang-format on + + enum class ParamType : char + { + INT, + UINT, + DOUBLE, + STRING, + PATH, + + MAX + }; + + /// @cond SKIP + struct routing_params + { + std::vector int_params; + std::vector uint_params; + std::vector double_params; + std::vector string_params; + + void debug_print() const + { + std::cerr << "routing_params" << std::endl; + for (auto i : int_params) + std::cerr << i << ", "; + std::cerr << std::endl; + for (auto i : uint_params) + std::cerr << i << ", "; + std::cerr << std::endl; + for (auto i : double_params) + std::cerr << i << ", "; + std::cerr << std::endl; + for (auto& i : string_params) + std::cerr << i << ", "; + std::cerr << std::endl; + } + + template + T get(unsigned) const; + }; + + template<> + inline int64_t routing_params::get(unsigned index) const + { + return int_params[index]; + } + + template<> + inline uint64_t routing_params::get(unsigned index) const + { + return uint_params[index]; + } + + template<> + inline double routing_params::get(unsigned index) const + { + return double_params[index]; + } + + template<> + inline std::string routing_params::get(unsigned index) const + { + return string_params[index]; + } + /// @endcond + + struct routing_handle_result + { + uint16_t rule_index; + std::vector blueprint_indices; + routing_params r_params; + HTTPMethod method; + + routing_handle_result() {} + + routing_handle_result(uint16_t rule_index_, std::vector blueprint_indices_, routing_params r_params_): + rule_index(rule_index_), + blueprint_indices(blueprint_indices_), + r_params(r_params_) {} + + routing_handle_result(uint16_t rule_index_, std::vector blueprint_indices_, routing_params r_params_, HTTPMethod method_): + rule_index(rule_index_), + blueprint_indices(blueprint_indices_), + r_params(r_params_), + method(method_) {} + }; +} // namespace crow + +// clang-format off +#ifndef CROW_MSVC_WORKAROUND +constexpr crow::HTTPMethod method_from_string(const char* str) +{ + return crow::black_magic::is_equ_p(str, "GET", 3) ? crow::HTTPMethod::Get : + crow::black_magic::is_equ_p(str, "DELETE", 6) ? crow::HTTPMethod::Delete : + crow::black_magic::is_equ_p(str, "HEAD", 4) ? crow::HTTPMethod::Head : + crow::black_magic::is_equ_p(str, "POST", 4) ? crow::HTTPMethod::Post : + crow::black_magic::is_equ_p(str, "PUT", 3) ? crow::HTTPMethod::Put : + + crow::black_magic::is_equ_p(str, "OPTIONS", 7) ? crow::HTTPMethod::Options : + crow::black_magic::is_equ_p(str, "CONNECT", 7) ? crow::HTTPMethod::Connect : + crow::black_magic::is_equ_p(str, "TRACE", 5) ? crow::HTTPMethod::Trace : + + crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch : + crow::black_magic::is_equ_p(str, "PURGE", 5) ? crow::HTTPMethod::Purge : + crow::black_magic::is_equ_p(str, "COPY", 4) ? crow::HTTPMethod::Copy : + crow::black_magic::is_equ_p(str, "LOCK", 4) ? crow::HTTPMethod::Lock : + crow::black_magic::is_equ_p(str, "MKCOL", 5) ? crow::HTTPMethod::MkCol : + crow::black_magic::is_equ_p(str, "MOVE", 4) ? crow::HTTPMethod::Move : + crow::black_magic::is_equ_p(str, "PROPFIND", 8) ? crow::HTTPMethod::Propfind : + crow::black_magic::is_equ_p(str, "PROPPATCH", 9) ? crow::HTTPMethod::Proppatch : + crow::black_magic::is_equ_p(str, "SEARCH", 6) ? crow::HTTPMethod::Search : + crow::black_magic::is_equ_p(str, "UNLOCK", 6) ? crow::HTTPMethod::Unlock : + crow::black_magic::is_equ_p(str, "BIND", 4) ? crow::HTTPMethod::Bind : + crow::black_magic::is_equ_p(str, "REBIND", 6) ? crow::HTTPMethod::Rebind : + crow::black_magic::is_equ_p(str, "UNBIND", 6) ? crow::HTTPMethod::Unbind : + crow::black_magic::is_equ_p(str, "ACL", 3) ? crow::HTTPMethod::Acl : + + crow::black_magic::is_equ_p(str, "REPORT", 6) ? crow::HTTPMethod::Report : + crow::black_magic::is_equ_p(str, "MKACTIVITY", 10) ? crow::HTTPMethod::MkActivity : + crow::black_magic::is_equ_p(str, "CHECKOUT", 8) ? crow::HTTPMethod::Checkout : + crow::black_magic::is_equ_p(str, "MERGE", 5) ? crow::HTTPMethod::Merge : + + crow::black_magic::is_equ_p(str, "MSEARCH", 7) ? crow::HTTPMethod::MSearch : + crow::black_magic::is_equ_p(str, "NOTIFY", 6) ? crow::HTTPMethod::Notify : + crow::black_magic::is_equ_p(str, "SUBSCRIBE", 9) ? crow::HTTPMethod::Subscribe : + crow::black_magic::is_equ_p(str, "UNSUBSCRIBE", 11) ? crow::HTTPMethod::Unsubscribe : + + crow::black_magic::is_equ_p(str, "MKCALENDAR", 10) ? crow::HTTPMethod::MkCalendar : + + crow::black_magic::is_equ_p(str, "LINK", 4) ? crow::HTTPMethod::Link : + crow::black_magic::is_equ_p(str, "UNLINK", 6) ? crow::HTTPMethod::Unlink : + + crow::black_magic::is_equ_p(str, "SOURCE", 6) ? crow::HTTPMethod::Source : + throw std::runtime_error("invalid http method"); +} + +constexpr crow::HTTPMethod operator"" _method(const char* str, size_t /*len*/) +{ + return method_from_string( str ); +} +#endif +// clang-format on + + +#ifdef CROW_USE_BOOST +#include +#else +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include +#endif + + +namespace crow // NOTE: Already documented in "crow/app.h" +{ +#ifdef CROW_USE_BOOST + namespace asio = boost::asio; +#endif + + /// Find and return the value associated with the key. (returns an empty string if nothing is found) + template + inline const std::string& get_header_value(const T& headers, const std::string& key) + { + if (headers.count(key)) + { + return headers.find(key)->second; + } + static std::string empty; + return empty; + } + + /// An HTTP request. + struct request + { + HTTPMethod method; + std::string raw_url; ///< The full URL containing the `?` and URL parameters. + std::string url; ///< The endpoint without any parameters. + query_string url_params; ///< The parameters associated with the request. (everything after the `?` in the URL) + ci_map headers; + std::string body; + std::string remote_ip_address; ///< The IP address from which the request was sent. + unsigned char http_ver_major, http_ver_minor; + bool keep_alive, ///< Whether or not the server should send a `connection: Keep-Alive` header to the client. + close_connection, ///< Whether or not the server should shut down the TCP connection once a response is sent. + upgrade; ///< Whether or noth the server should change the HTTP connection to a different connection. + + void* middleware_context{}; + void* middleware_container{}; + asio::io_service* io_service{}; + + /// Construct an empty request. (sets the method to `GET`) + request(): + method(HTTPMethod::Get) + {} + + /// Construct a request with all values assigned. + request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body, unsigned char http_major, unsigned char http_minor, bool has_keep_alive, bool has_close_connection, bool is_upgrade): + method(method), raw_url(std::move(raw_url)), url(std::move(url)), url_params(std::move(url_params)), headers(std::move(headers)), body(std::move(body)), http_ver_major(http_major), http_ver_minor(http_minor), keep_alive(has_keep_alive), close_connection(has_close_connection), upgrade(is_upgrade) + {} + + void add_header(std::string key, std::string value) + { + headers.emplace(std::move(key), std::move(value)); + } + + const std::string& get_header_value(const std::string& key) const + { + return crow::get_header_value(headers, key); + } + + bool check_version(unsigned char major, unsigned char minor) const + { + return http_ver_major == major && http_ver_minor == minor; + } + + /// Get the body as parameters in QS format. + + /// + /// This is meant to be used with requests of type "application/x-www-form-urlencoded" + const query_string get_body_params() const + { + return query_string(body, false); + } + + /// Send data to whoever made this request with a completion handler and return immediately. + template + void post(CompletionHandler handler) + { + io_service->post(handler); + } + + /// Send data to whoever made this request with a completion handler. + template + void dispatch(CompletionHandler handler) + { + io_service->dispatch(handler); + } + }; +} // namespace crow + + +#include +#include +#include + + +namespace crow +{ + + /// Encapsulates anything related to processing and organizing `multipart/xyz` messages + namespace multipart + { + + const std::string dd = "--"; + + /// The first part in a section, contains metadata about the part + struct header + { + std::string value; ///< The first part of the header, usually `Content-Type` or `Content-Disposition` + std::unordered_map params; ///< The parameters of the header, come after the `value` + + operator int() const { return std::stoi(value); } ///< Returns \ref value as integer + operator double() const { return std::stod(value); } ///< Returns \ref value as double + }; + + /// Multipart header map (key is header key). + using mph_map = std::unordered_multimap; + + /// Find and return the value object associated with the key. (returns an empty class if nothing is found) + template + inline const O& get_header_value_object(const T& headers, const std::string& key) + { + if (headers.count(key)) + { + return headers.find(key)->second; + } + static O empty; + return empty; + } + + /// Same as \ref get_header_value_object() but for \ref multipart.header + template + inline const header& get_header_object(const T& headers, const std::string& key) + { + return get_header_value_object
(headers, key); + } + + ///One part of the multipart message + + /// + /// It is usually separated from other sections by a `boundary` + struct part + { + mph_map headers; ///< (optional) The first part before the data, Contains information regarding the type of data and encoding + std::string body; ///< The actual data in the part + + operator int() const { return std::stoi(body); } ///< Returns \ref body as integer + operator double() const { return std::stod(body); } ///< Returns \ref body as double + + const header& get_header_object(const std::string& key) const + { + return multipart::get_header_object(headers, key); + } + }; + + /// Multipart map (key is the name parameter). + using mp_map = std::unordered_multimap; + + /// The parsed multipart request/response + struct message : public returnable + { + ci_map headers; ///< The request/response headers + std::string boundary; ///< The text boundary that separates different `parts` + std::vector parts; ///< The individual parts of the message + mp_map part_map; ///< The individual parts of the message, organized in a map with the `name` header parameter being the key + + const std::string& get_header_value(const std::string& key) const + { + return crow::get_header_value(headers, key); + } + + part get_part_by_name(const std::string& name) + { + mp_map::iterator result = part_map.find(name); + if (result != part_map.end()) + return result->second; + else + return {}; + } + + /// Represent all parts as a string (**does not include message headers**) + std::string dump() const override + { + std::stringstream str; + std::string delimiter = dd + boundary; + + for (unsigned i = 0; i < parts.size(); i++) + { + str << delimiter << crlf; + str << dump(i); + } + str << delimiter << dd << crlf; + return str.str(); + } + + /// Represent an individual part as a string + std::string dump(int part_) const + { + std::stringstream str; + part item = parts[part_]; + for (auto& item_h : item.headers) + { + str << item_h.first << ": " << item_h.second.value; + for (auto& it : item_h.second.params) + { + str << "; " << it.first << '=' << pad(it.second); + } + str << crlf; + } + str << crlf; + str << item.body << crlf; + return str.str(); + } + + /// Default constructor using default values + message(const ci_map& headers, const std::string& boundary, const std::vector& sections): + returnable("multipart/form-data; boundary=CROW-BOUNDARY"), headers(headers), boundary(boundary), parts(sections) + { + if (!boundary.empty()) + content_type = "multipart/form-data; boundary=" + boundary; + for (auto& item : parts) + { + part_map.emplace( + (get_header_object(item.headers, "Content-Disposition").params.find("name")->second), + item); + } + } + + /// Create a multipart message from a request data + message(const request& req): + returnable("multipart/form-data; boundary=CROW-BOUNDARY"), + headers(req.headers), + boundary(get_boundary(get_header_value("Content-Type"))) + { + if (!boundary.empty()) + content_type = "multipart/form-data; boundary=" + boundary; + parse_body(req.body, parts, part_map); + } + + private: + std::string get_boundary(const std::string& header) const + { + constexpr char boundary_text[] = "boundary="; + size_t found = header.find(boundary_text); + if (found != std::string::npos) + { + std::string to_return(header.substr(found + strlen(boundary_text))); + if (to_return[0] == '\"') + { + to_return = to_return.substr(1, to_return.length() - 2); + } + return to_return; + } + return std::string(); + } + + void parse_body(std::string body, std::vector& sections, mp_map& part_map) + { + + std::string delimiter = dd + boundary; + + // TODO(EDev): Exit on error + while (body != (crlf)) + { + size_t found = body.find(delimiter); + if (found == std::string::npos) + { + // did not find delimiter; probably an ill-formed body; ignore the rest + break; + } + std::string section = body.substr(0, found); + + // +2 is the CRLF. + // We don't check it and delete it so that the same delimiter can be used for The last delimiter (--delimiter--CRLF). + body.erase(0, found + delimiter.length() + 2); + if (!section.empty()) + { + part parsed_section(parse_section(section)); + part_map.emplace( + (get_header_object(parsed_section.headers, "Content-Disposition").params.find("name")->second), + parsed_section); + sections.push_back(std::move(parsed_section)); + } + } + } + + part parse_section(std::string& section) + { + struct part to_return; + + size_t found = section.find(crlf + crlf); + std::string head_line = section.substr(0, found + 2); + section.erase(0, found + 4); + + parse_section_head(head_line, to_return); + to_return.body = section.substr(0, section.length() - 2); + return to_return; + } + + void parse_section_head(std::string& lines, part& part) + { + while (!lines.empty()) + { + header to_add; + + size_t found = lines.find(crlf); + std::string line = lines.substr(0, found); + std::string key; + lines.erase(0, found + 2); + // Add the header if available + if (!line.empty()) + { + size_t found = line.find("; "); + std::string header = line.substr(0, found); + if (found != std::string::npos) + line.erase(0, found + 2); + else + line = std::string(); + + size_t header_split = header.find(": "); + key = header.substr(0, header_split); + + to_add.value = header.substr(header_split + 2); + } + + // Add the parameters + while (!line.empty()) + { + size_t found = line.find("; "); + std::string param = line.substr(0, found); + if (found != std::string::npos) + line.erase(0, found + 2); + else + line = std::string(); + + size_t param_split = param.find('='); + + std::string value = param.substr(param_split + 1); + + to_add.params.emplace(param.substr(0, param_split), trim(value)); + } + part.headers.emplace(key, to_add); + } + } + + inline std::string trim(std::string& string, const char& excess = '"') const + { + if (string.length() > 1 && string[0] == excess && string[string.length() - 1] == excess) + return string.substr(1, string.length() - 2); + return string; + } + + inline std::string pad(std::string& string, const char& padding = '"') const + { + return (padding + string + padding); + } + }; + } // namespace multipart +} // namespace crow + +/* merged revision: 5b951d74bd66ec9d38448e0a85b1cf8b85d97db3 */ +/* updated to : e13b274770da9b82a1085dec29182acfea72e7a7 (beyond v2.9.5) */ +/* commits not included: + * 091ebb87783a58b249062540bbea07de2a11e9cf + * 6132d1fefa03f769a3979355d1f5da0b8889cad2 + * 7ba312397c2a6c851a4b5efe6c1603b1e1bda6ff + * d7675453a6c03180572f084e95eea0d02df39164 + * dff604db203986e532e5a679bafd0e7382c6bdd9 (Might be useful to actually add [upgrade requests with a body]) + * e01811e7f4894d7f0f7f4bd8492cccec6f6b4038 (related to above) + * 05525c5fde1fc562481f6ae08fa7056185325daf (also related to above) + * 350258965909f249f9c59823aac240313e0d0120 (cannot be implemented due to upgrade) + */ + +// clang-format off +extern "C" { +#include +#if defined(_WIN32) && !defined(__MINGW32__) && \ + (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__) +#include +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#elif (defined(__sun) || defined(__sun__)) && defined(__SunOS_5_9) +#include +#else +#include +#endif +#include +#include +#include +#include +} + +namespace crow +{ +/* Maximium header size allowed. If the macro is not defined + * before including this header then the default is used. To + * change the maximum header size, define the macro in the build + * environment (e.g. -DHTTP_MAX_HEADER_SIZE=). To remove + * the effective limit on the size of the header, define the macro + * to a very large number (e.g. -DCROW_HTTP_MAX_HEADER_SIZE=0x7fffffff) + */ +#ifndef CROW_HTTP_MAX_HEADER_SIZE +# define CROW_HTTP_MAX_HEADER_SIZE (80*1024) +#endif + +typedef struct http_parser http_parser; +typedef struct http_parser_settings http_parser_settings; + +/* Callbacks should return non-zero to indicate an error. The parser will + * then halt execution. + * + * The one exception is on_headers_complete. In a HTTP_RESPONSE parser + * returning '1' from on_headers_complete will tell the parser that it + * should not expect a body. This is used when receiving a response to a + * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: + * chunked' headers that indicate the presence of a body. + * + * Returning `2` from on_headers_complete will tell parser that it should not + * expect neither a body nor any futher responses on this connection. This is + * useful for handling responses to a CONNECT request which may not contain + * `Upgrade` or `Connection: upgrade` headers. + * + * http_data_cb does not return data chunks. It will be called arbitrarally + * many times for each string. E.G. you might get 10 callbacks for "on_url" + * each providing just a few characters more data. + */ +typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); +typedef int (*http_cb) (http_parser*); + + +/* Flag values for http_parser.flags field */ +enum http_connection_flags // This is basically 7 booleans placed into 1 integer. Uses 4 bytes instead of n bytes (7 currently). + { F_CHUNKED = 1 << 0 // 00000000 00000000 00000000 00000001 + , F_CONNECTION_KEEP_ALIVE = 1 << 1 // 00000000 00000000 00000000 00000010 + , F_CONNECTION_CLOSE = 1 << 2 // 00000000 00000000 00000000 00000100 + , F_TRAILING = 1 << 3 // 00000000 00000000 00000000 00001000 + , F_UPGRADE = 1 << 4 // 00000000 00000000 00000000 00010000 + , F_SKIPBODY = 1 << 5 // 00000000 00000000 00000000 00100000 + , F_CONTENTLENGTH = 1 << 6 // 00000000 00000000 00000000 01000000 + }; + + +/* Map for errno-related constants + * + * The provided argument should be a macro that takes 2 arguments. + */ +#define CROW_HTTP_ERRNO_MAP(CROW_XX) \ + /* No error */ \ + CROW_XX(OK, "success") \ + \ + /* Callback-related errors */ \ + CROW_XX(CB_message_begin, "the on_message_begin callback failed") \ + CROW_XX(CB_method, "the on_method callback failed") \ + CROW_XX(CB_url, "the \"on_url\" callback failed") \ + CROW_XX(CB_header_field, "the \"on_header_field\" callback failed") \ + CROW_XX(CB_header_value, "the \"on_header_value\" callback failed") \ + CROW_XX(CB_headers_complete, "the \"on_headers_complete\" callback failed") \ + CROW_XX(CB_body, "the \"on_body\" callback failed") \ + CROW_XX(CB_message_complete, "the \"on_message_complete\" callback failed") \ + CROW_XX(CB_status, "the \"on_status\" callback failed") \ + \ + /* Parsing-related errors */ \ + CROW_XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ + CROW_XX(HEADER_OVERFLOW, "too many header bytes seen; overflow detected") \ + CROW_XX(CLOSED_CONNECTION, "data received after completed connection: close message") \ + CROW_XX(INVALID_VERSION, "invalid HTTP version") \ + CROW_XX(INVALID_STATUS, "invalid HTTP status code") \ + CROW_XX(INVALID_METHOD, "invalid HTTP method") \ + CROW_XX(INVALID_URL, "invalid URL") \ + CROW_XX(INVALID_HOST, "invalid host") \ + CROW_XX(INVALID_PORT, "invalid port") \ + CROW_XX(INVALID_PATH, "invalid path") \ + CROW_XX(INVALID_QUERY_STRING, "invalid query string") \ + CROW_XX(INVALID_FRAGMENT, "invalid fragment") \ + CROW_XX(LF_EXPECTED, "LF character expected") \ + CROW_XX(INVALID_HEADER_TOKEN, "invalid character in header") \ + CROW_XX(INVALID_CONTENT_LENGTH, "invalid character in content-length header") \ + CROW_XX(UNEXPECTED_CONTENT_LENGTH, "unexpected content-length header") \ + CROW_XX(INVALID_CHUNK_SIZE, "invalid character in chunk size header") \ + CROW_XX(INVALID_CONSTANT, "invalid constant string") \ + CROW_XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state") \ + CROW_XX(STRICT, "strict mode assertion failed") \ + CROW_XX(UNKNOWN, "an unknown error occurred") \ + CROW_XX(INVALID_TRANSFER_ENCODING, "request has invalid transfer-encoding") \ + + +/* Define CHPE_* values for each errno value above */ +#define CROW_HTTP_ERRNO_GEN(n, s) CHPE_##n, +enum http_errno { + CROW_HTTP_ERRNO_MAP(CROW_HTTP_ERRNO_GEN) +}; +#undef CROW_HTTP_ERRNO_GEN + + +/* Get an http_errno value from an http_parser */ +#define CROW_HTTP_PARSER_ERRNO(p) ((enum http_errno)(p)->http_errno) + + + struct http_parser + { + /** PRIVATE **/ + unsigned int flags : 7; /* F_* values from 'flags' enum; semi-public */ + unsigned int state : 8; /* enum state from http_parser.c */ + unsigned int header_state : 7; /* enum header_state from http_parser.c */ + unsigned int index : 5; /* index into current matcher */ + unsigned int uses_transfer_encoding : 1; /* Transfer-Encoding header is present */ + unsigned int allow_chunked_length : 1; /* Allow headers with both `Content-Length` and `Transfer-Encoding: chunked` set */ + unsigned int lenient_http_headers : 1; + + uint32_t nread; /* # bytes read in various scenarios */ + uint64_t content_length; /* # bytes in body. `(uint64_t) -1` (all bits one) if no Content-Length header. */ + unsigned long qs_point; + + /** READ-ONLY **/ + unsigned char http_major; + unsigned char http_minor; + unsigned int method : 8; /* requests only */ + unsigned int http_errno : 7; + + /* 1 = Upgrade header was present and the parser has exited because of that. + * 0 = No upgrade header present. + * Should be checked when http_parser_execute() returns in addition to + * error checking. + */ + unsigned int upgrade : 1; + + /** PUBLIC **/ + void* data; /* A pointer to get hook to the "connection" or "socket" object */ + }; + + + struct http_parser_settings + { + http_cb on_message_begin; + http_cb on_method; + http_data_cb on_url; + http_data_cb on_header_field; + http_data_cb on_header_value; + http_cb on_headers_complete; + http_data_cb on_body; + http_cb on_message_complete; + }; + + + +// SOURCE (.c) CODE +static uint32_t max_header_size = CROW_HTTP_MAX_HEADER_SIZE; + +#ifndef CROW_ULLONG_MAX +# define CROW_ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */ +#endif + +#ifndef CROW_MIN +# define CROW_MIN(a,b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef CROW_ARRAY_SIZE +# define CROW_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#endif + +#ifndef CROW_BIT_AT +# define CROW_BIT_AT(a, i) \ + (!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \ + (1 << ((unsigned int) (i) & 7)))) +#endif + +#define CROW_SET_ERRNO(e) \ +do { \ + parser->nread = nread; \ + parser->http_errno = (e); \ +} while(0) + +/* Run the notify callback FOR, returning ER if it fails */ +#define CROW_CALLBACK_NOTIFY_(FOR, ER) \ +do { \ + assert(CROW_HTTP_PARSER_ERRNO(parser) == CHPE_OK); \ + \ + if (CROW_LIKELY(settings->on_##FOR)) { \ + if (CROW_UNLIKELY(0 != settings->on_##FOR(parser))) { \ + CROW_SET_ERRNO(CHPE_CB_##FOR); \ + } \ + \ + /* We either errored above or got paused; get out */ \ + if (CROW_UNLIKELY(CROW_HTTP_PARSER_ERRNO(parser) != CHPE_OK)) { \ + return (ER); \ + } \ + } \ +} while (0) + +/* Run the notify callback FOR and consume the current byte */ +#define CROW_CALLBACK_NOTIFY(FOR) CROW_CALLBACK_NOTIFY_(FOR, p - data + 1) + +/* Run the notify callback FOR and don't consume the current byte */ +#define CROW_CALLBACK_NOTIFY_NOADVANCE(FOR) CROW_CALLBACK_NOTIFY_(FOR, p - data) + +/* Run data callback FOR with LEN bytes, returning ER if it fails */ +#define CROW_CALLBACK_DATA_(FOR, LEN, ER) \ +do { \ + assert(CROW_HTTP_PARSER_ERRNO(parser) == CHPE_OK); \ + \ + if (FOR##_mark) { \ + if (CROW_LIKELY(settings->on_##FOR)) { \ + if (CROW_UNLIKELY(0 != \ + settings->on_##FOR(parser, FOR##_mark, (LEN)))) { \ + CROW_SET_ERRNO(CHPE_CB_##FOR); \ + } \ + \ + /* We either errored above or got paused; get out */ \ + if (CROW_UNLIKELY(CROW_HTTP_PARSER_ERRNO(parser) != CHPE_OK)) {\ + return (ER); \ + } \ + } \ + FOR##_mark = NULL; \ + } \ +} while (0) + +/* Run the data callback FOR and consume the current byte */ +#define CROW_CALLBACK_DATA(FOR) \ + CROW_CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1) + +/* Run the data callback FOR and don't consume the current byte */ +#define CROW_CALLBACK_DATA_NOADVANCE(FOR) \ + CROW_CALLBACK_DATA_(FOR, p - FOR##_mark, p - data) + +/* Set the mark FOR; non-destructive if mark is already set */ +#define CROW_MARK(FOR) \ +do { \ + if (!FOR##_mark) { \ + FOR##_mark = p; \ + } \ +} while (0) + +/* Don't allow the total size of the HTTP headers (including the status + * line) to exceed max_header_size. This check is here to protect + * embedders against denial-of-service attacks where the attacker feeds + * us a never-ending header that the embedder keeps buffering. + * + * This check is arguably the responsibility of embedders but we're doing + * it on the embedder's behalf because most won't bother and this way we + * make the web a little safer. max_header_size is still far bigger + * than any reasonable request or response so this should never affect + * day-to-day operation. + */ +#define CROW_COUNT_HEADER_SIZE(V) \ +do { \ + nread += (uint32_t)(V); \ + if (CROW_UNLIKELY(nread > max_header_size)) { \ + CROW_SET_ERRNO(CHPE_HEADER_OVERFLOW); \ + goto error; \ + } \ +} while (0) +#define CROW_REEXECUTE() \ + goto reexecute; \ + +#define CROW_PROXY_CONNECTION "proxy-connection" +#define CROW_CONNECTION "connection" +#define CROW_CONTENT_LENGTH "content-length" +#define CROW_TRANSFER_ENCODING "transfer-encoding" +#define CROW_UPGRADE "upgrade" +#define CROW_CHUNKED "chunked" +#define CROW_KEEP_ALIVE "keep-alive" +#define CROW_CLOSE "close" + + + + enum state + { + s_dead = 1 /* important that this is > 0 */ + + , + s_start_req + + , + s_req_method, + s_req_spaces_before_url, + s_req_schema, + s_req_schema_slash, + s_req_schema_slash_slash, + s_req_server_start, + s_req_server, // } + s_req_server_with_at, // | + s_req_path, // | The parser recognizes how to switch between these states, + s_req_query_string_start, // | however it doesn't process them any differently. + s_req_query_string, // } + s_req_http_start, + s_req_http_H, + s_req_http_HT, + s_req_http_HTT, + s_req_http_HTTP, + s_req_http_I, + s_req_http_IC, + s_req_http_major, + s_req_http_dot, + s_req_http_minor, + s_req_http_end, + s_req_line_almost_done + + , + s_header_field_start, + s_header_field, + s_header_value_discard_ws, + s_header_value_discard_ws_almost_done, + s_header_value_discard_lws, + s_header_value_start, + s_header_value, + s_header_value_lws + + , + s_header_almost_done + + , + s_chunk_size_start, + s_chunk_size, + s_chunk_parameters, + s_chunk_size_almost_done + + , + s_headers_almost_done, + s_headers_done + + /* Important: 's_headers_done' must be the last 'header' state. All + * states beyond this must be 'body' states. It is used for overflow + * checking. See the CROW_PARSING_HEADER() macro. + */ + + , + s_chunk_data, + s_chunk_data_almost_done, + s_chunk_data_done + + , + s_body_identity, + s_body_identity_eof + + , + s_message_done + }; + + +#define CROW_PARSING_HEADER(state) (state <= s_headers_done) + + +enum header_states + { h_general = 0 + , h_C + , h_CO + , h_CON + + , h_matching_connection + , h_matching_proxy_connection + , h_matching_content_length + , h_matching_transfer_encoding + , h_matching_upgrade + + , h_connection + , h_content_length + , h_content_length_num + , h_content_length_ws + , h_transfer_encoding + , h_upgrade + + , h_matching_transfer_encoding_token_start + , h_matching_transfer_encoding_chunked + , h_matching_transfer_encoding_token + + , h_matching_connection_keep_alive + , h_matching_connection_close + + , h_transfer_encoding_chunked + , h_connection_keep_alive + , h_connection_close + }; + +enum http_host_state + { + s_http_host_dead = 1 + , s_http_userinfo_start + , s_http_userinfo + , s_http_host_start + , s_http_host_v6_start + , s_http_host + , s_http_host_v6 + , s_http_host_v6_end + , s_http_host_v6_zone_start + , s_http_host_v6_zone + , s_http_host_port_start + , s_http_host_port +}; + +/* Macros for character classes; depends on strict-mode */ +#define CROW_LOWER(c) (unsigned char)(c | 0x20) +#define CROW_IS_ALPHA(c) (CROW_LOWER(c) >= 'a' && CROW_LOWER(c) <= 'z') +#define CROW_IS_NUM(c) ((c) >= '0' && (c) <= '9') +#define CROW_IS_ALPHANUM(c) (CROW_IS_ALPHA(c) || CROW_IS_NUM(c)) +//#define CROW_IS_HEX(c) (CROW_IS_NUM(c) || (CROW_LOWER(c) >= 'a' && CROW_LOWER(c) <= 'f')) +#define CROW_IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \ + (c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \ + (c) == ')') +#define CROW_IS_USERINFO_CHAR(c) (CROW_IS_ALPHANUM(c) || CROW_IS_MARK(c) || (c) == '%' || \ + (c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \ + (c) == '$' || (c) == ',') + +#define CROW_TOKEN(c) (tokens[(unsigned char)c]) +#define CROW_IS_URL_CHAR(c) (CROW_BIT_AT(normal_url_char, (unsigned char)c)) +//#define CROW_IS_HOST_CHAR(c) (CROW_IS_ALPHANUM(c) || (c) == '.' || (c) == '-') + + /** + * Verify that a char is a valid visible (printable) US-ASCII + * character or %x80-FF + **/ +#define CROW_IS_HEADER_CHAR(ch) \ + (ch == cr || ch == lf || ch == 9 || ((unsigned char)ch > 31 && ch != 127)) + +#define CROW_start_state s_start_req + +# define CROW_STRICT_CHECK(cond) \ +do { \ + if (cond) { \ + CROW_SET_ERRNO(CHPE_STRICT); \ + goto error; \ + } \ +} while (0) +#define CROW_NEW_MESSAGE() (CROW_start_state) + +/* Our URL parser. + * + * This is designed to be shared by http_parser_execute() for URL validation, + * hence it has a state transition + byte-for-byte interface. In addition, it + * is meant to be embedded in http_parser_parse_url(), which does the dirty + * work of turning state transitions URL components for its API. + * + * This function should only be invoked with non-space characters. It is + * assumed that the caller cares about (and can detect) the transition between + * URL and non-URL states by looking for these. + */ +inline enum state +parse_url_char(enum state s, const char ch, http_parser *parser, const char* url_mark, const char* p) +{ +# define CROW_T(v) 0 + + +static const uint8_t normal_url_char[32] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0 |CROW_T(2)| 0 | 0 |CROW_T(16)| 0 | 0 | 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0 | 2 | 4 | 0 | 16 | 32 | 64 | 128, +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 80 P 81 Q 82 R 83 S 84 CROW_T 85 U 86 V 87 W */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128, +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, }; + +#undef CROW_T + + if (ch == ' ' || ch == '\r' || ch == '\n') { + return s_dead; + } + if (ch == '\t' || ch == '\f') { + return s_dead; + } + + switch (s) { + case s_req_spaces_before_url: + /* Proxied requests are followed by scheme of an absolute URI (alpha). + * All methods except CONNECT are followed by '/' or '*'. + */ + + if (ch == '/' || ch == '*') { + return s_req_path; + } + + if (CROW_IS_ALPHA(ch)) { + return s_req_schema; + } + + break; + + case s_req_schema: + if (CROW_IS_ALPHA(ch)) { + return s; + } + + if (ch == ':') { + return s_req_schema_slash; + } + + break; + + case s_req_schema_slash: + if (ch == '/') { + return s_req_schema_slash_slash; + } + + break; + + case s_req_schema_slash_slash: + if (ch == '/') { + return s_req_server_start; + } + + break; + + case s_req_server_with_at: + if (ch == '@') { + return s_dead; + } + + /* fall through */ + case s_req_server_start: + case s_req_server: + if (ch == '/') { + return s_req_path; + } + + if (ch == '?') { + parser->qs_point = p - url_mark; + return s_req_query_string_start; + } + + if (ch == '@') { + return s_req_server_with_at; + } + + if (CROW_IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') { + return s_req_server; + } + + break; + + case s_req_path: + if (CROW_IS_URL_CHAR(ch)) { + return s; + } + else if (ch == '?') + { + parser->qs_point = p - url_mark; + return s_req_query_string_start; + } + + break; + + case s_req_query_string_start: + case s_req_query_string: + if (CROW_IS_URL_CHAR(ch)) { + return s_req_query_string; + } + else if (ch == '?') + { + return s_req_query_string; + } + + break; + + default: + break; + } + + /* We should never fall out of the switch above unless there's an error */ + return s_dead; +} + +inline size_t http_parser_execute (http_parser *parser, + const http_parser_settings *settings, + const char *data, + size_t len) +{ + +/* Tokens as defined by rfc 2616. Also lowercases them. + * token = 1* + * separators = "(" | ")" | "<" | ">" | "@" + * | "," | ";" | ":" | "\" | <"> + * | "/" | "[" | "]" | "?" | "=" + * | "{" | "}" | SP | HT + */ +static const char tokens[256] = { +/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */ + 0, 0, 0, 0, 0, 0, 0, 0, +/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */ + 0, '!', 0, '#', '$', '%', '&', '\'', +/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */ + 0, 0, '*', '+', 0, '-', '.', 0, +/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */ + '0', '1', '2', '3', '4', '5', '6', '7', +/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */ + '8', '9', 0, 0, 0, 0, 0, 0, +/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */ + 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */ + 'x', 'y', 'z', 0, 0, 0, '^', '_', +/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */ + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', +/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */ + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */ + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */ + 'x', 'y', 'z', 0, '|', 0, '~', 0 }; + + +static const int8_t unhex[256] = + {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + }; + + + + char c, ch; + int8_t unhex_val; + const char *p = data; + const char *header_field_mark = 0; + const char *header_value_mark = 0; + const char *url_mark = 0; + const char *url_start_mark = 0; + const char *body_mark = 0; + const unsigned int lenient = parser->lenient_http_headers; + const unsigned int allow_chunked_length = parser->allow_chunked_length; + + uint32_t nread = parser->nread; + + /* We're in an error state. Don't bother doing anything. */ + if (CROW_HTTP_PARSER_ERRNO(parser) != CHPE_OK) { + return 0; + } + + if (len == 0) { + switch (parser->state) { + case s_body_identity_eof: + /* Use of CROW_CALLBACK_NOTIFY() here would erroneously return 1 byte read if we got paused. */ + CROW_CALLBACK_NOTIFY_NOADVANCE(message_complete); + return 0; + + case s_dead: + case s_start_req: + return 0; + + default: + CROW_SET_ERRNO(CHPE_INVALID_EOF_STATE); + return 1; + } + } + + + if (parser->state == s_header_field) + header_field_mark = data; + if (parser->state == s_header_value) + header_value_mark = data; + switch (parser->state) { + case s_req_path: + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + case s_req_server: + case s_req_server_with_at: + case s_req_query_string_start: + case s_req_query_string: + url_mark = data; + break; + default: + break; + } + + for (p=data; p != data + len; p++) { + ch = *p; + + if (CROW_PARSING_HEADER(parser->state)) + CROW_COUNT_HEADER_SIZE(1); + +reexecute: + switch (parser->state) { + + case s_dead: + /* this state is used after a 'Connection: close' message + * the parser will error out if it reads another message + */ + if (CROW_LIKELY(ch == cr || ch == lf)) + break; + + CROW_SET_ERRNO(CHPE_CLOSED_CONNECTION); + goto error; + + case s_start_req: + { + if (ch == cr || ch == lf) + break; + parser->flags = 0; + parser->uses_transfer_encoding = 0; + parser->content_length = CROW_ULLONG_MAX; + + if (CROW_UNLIKELY(!CROW_IS_ALPHA(ch))) { + CROW_SET_ERRNO(CHPE_INVALID_METHOD); + goto error; + } + + parser->method = 0; + parser->index = 1; + switch (ch) { + case 'A': parser->method = (unsigned)HTTPMethod::Acl; break; + case 'B': parser->method = (unsigned)HTTPMethod::Bind; break; + case 'C': parser->method = (unsigned)HTTPMethod::Connect; /* or COPY, CHECKOUT */ break; + case 'D': parser->method = (unsigned)HTTPMethod::Delete; break; + case 'G': parser->method = (unsigned)HTTPMethod::Get; break; + case 'H': parser->method = (unsigned)HTTPMethod::Head; break; + case 'L': parser->method = (unsigned)HTTPMethod::Lock; /* or LINK */ break; + case 'M': parser->method = (unsigned)HTTPMethod::MkCol; /* or MOVE, MKACTIVITY, MERGE, M-SEARCH, MKCALENDAR */ break; + case 'N': parser->method = (unsigned)HTTPMethod::Notify; break; + case 'O': parser->method = (unsigned)HTTPMethod::Options; break; + case 'P': parser->method = (unsigned)HTTPMethod::Post; /* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */ break; + case 'R': parser->method = (unsigned)HTTPMethod::Report; /* or REBIND */ break; + case 'S': parser->method = (unsigned)HTTPMethod::Subscribe; /* or SEARCH, SOURCE */ break; + case 'T': parser->method = (unsigned)HTTPMethod::Trace; break; + case 'U': parser->method = (unsigned)HTTPMethod::Unlock; /* or UNSUBSCRIBE, UNBIND, UNLINK */ break; + default: + CROW_SET_ERRNO(CHPE_INVALID_METHOD); + goto error; + } + parser->state = s_req_method; + + CROW_CALLBACK_NOTIFY(message_begin); + + break; + } + + case s_req_method: + { + const char *matcher; + if (CROW_UNLIKELY(ch == '\0')) { + CROW_SET_ERRNO(CHPE_INVALID_METHOD); + goto error; + } + + matcher = method_strings[parser->method]; + if (ch == ' ' && matcher[parser->index] == '\0') { + parser->state = s_req_spaces_before_url; + } else if (ch == matcher[parser->index]) { + ; /* nada */ + } else if ((ch >= 'A' && ch <= 'Z') || ch == '-') { + + switch (parser->method << 16 | parser->index << 8 | ch) { +#define CROW_XX(meth, pos, ch, new_meth) \ + case ((unsigned)HTTPMethod::meth << 16 | pos << 8 | ch): \ + parser->method = (unsigned)HTTPMethod::new_meth; break; + + CROW_XX(Post, 1, 'U', Put) + CROW_XX(Post, 1, 'A', Patch) + CROW_XX(Post, 1, 'R', Propfind) + CROW_XX(Put, 2, 'R', Purge) + CROW_XX(Connect, 1, 'H', Checkout) + CROW_XX(Connect, 2, 'P', Copy) + CROW_XX(MkCol, 1, 'O', Move) + CROW_XX(MkCol, 1, 'E', Merge) + CROW_XX(MkCol, 1, '-', MSearch) + CROW_XX(MkCol, 2, 'A', MkActivity) + CROW_XX(MkCol, 3, 'A', MkCalendar) + CROW_XX(Subscribe, 1, 'E', Search) + CROW_XX(Subscribe, 1, 'O', Source) + CROW_XX(Report, 2, 'B', Rebind) + CROW_XX(Propfind, 4, 'P', Proppatch) + CROW_XX(Lock, 1, 'I', Link) + CROW_XX(Unlock, 2, 'S', Unsubscribe) + CROW_XX(Unlock, 2, 'B', Unbind) + CROW_XX(Unlock, 3, 'I', Unlink) +#undef CROW_XX + default: + CROW_SET_ERRNO(CHPE_INVALID_METHOD); + goto error; + } + } else { + CROW_SET_ERRNO(CHPE_INVALID_METHOD); + goto error; + } + + CROW_CALLBACK_NOTIFY_NOADVANCE(method); + + ++parser->index; + break; + } + + case s_req_spaces_before_url: + { + if (ch == ' ') break; + + CROW_MARK(url); + CROW_MARK(url_start); + if (parser->method == (unsigned)HTTPMethod::Connect) { + parser->state = s_req_server_start; + } + + parser->state = parse_url_char(static_cast(parser->state), ch, parser, url_start_mark, p); + if (CROW_UNLIKELY(parser->state == s_dead)) { + CROW_SET_ERRNO(CHPE_INVALID_URL); + goto error; + } + + break; + } + + case s_req_schema: + case s_req_schema_slash: + case s_req_schema_slash_slash: + case s_req_server_start: + { + switch (ch) { + /* No whitespace allowed here */ + case ' ': + case cr: + case lf: + CROW_SET_ERRNO(CHPE_INVALID_URL); + goto error; + default: + parser->state = parse_url_char(static_cast(parser->state), ch, parser, url_start_mark, p); + if (CROW_UNLIKELY(parser->state == s_dead)) { + CROW_SET_ERRNO(CHPE_INVALID_URL); + goto error; + } + } + + break; + } + + case s_req_server: + case s_req_server_with_at: + case s_req_path: + case s_req_query_string_start: + case s_req_query_string: + { + switch (ch) { + case ' ': + parser->state = s_req_http_start; + CROW_CALLBACK_DATA(url); + break; + case cr: // No space after URL means no HTTP version. Which means the request is using HTTP/0.9 + case lf: + if (CROW_UNLIKELY(parser->method != (unsigned)HTTPMethod::Get)) // HTTP/0.9 doesn't define any method other than GET + { + parser->state = s_dead; + CROW_SET_ERRNO(CHPE_INVALID_VERSION); + goto error; + } + parser->http_major = 0; + parser->http_minor = 9; + parser->state = (ch == cr) ? + s_req_line_almost_done : + s_header_field_start; + CROW_CALLBACK_DATA(url); + break; + default: + parser->state = parse_url_char(static_cast(parser->state), ch, parser, url_start_mark, p); + if (CROW_UNLIKELY(parser->state == s_dead)) { + CROW_SET_ERRNO(CHPE_INVALID_URL); + goto error; + } + } + break; + } + + case s_req_http_start: + switch (ch) { + case ' ': + break; + case 'H': + parser->state = s_req_http_H; + break; + case 'I': + if (parser->method == (unsigned)HTTPMethod::Source) { + parser->state = s_req_http_I; + break; + } + /* fall through */ + default: + CROW_SET_ERRNO(CHPE_INVALID_CONSTANT); + goto error; + } + break; + + case s_req_http_H: + CROW_STRICT_CHECK(ch != 'T'); + parser->state = s_req_http_HT; + break; + + case s_req_http_HT: + CROW_STRICT_CHECK(ch != 'T'); + parser->state = s_req_http_HTT; + break; + + case s_req_http_HTT: + CROW_STRICT_CHECK(ch != 'P'); + parser->state = s_req_http_HTTP; + break; + + case s_req_http_I: + CROW_STRICT_CHECK(ch != 'C'); + parser->state = s_req_http_IC; + break; + + case s_req_http_IC: + CROW_STRICT_CHECK(ch != 'E'); + parser->state = s_req_http_HTTP; /* Treat "ICE" as "HTTP". */ + break; + + case s_req_http_HTTP: + CROW_STRICT_CHECK(ch != '/'); + parser->state = s_req_http_major; + break; + + /* dot */ + case s_req_http_major: + if (CROW_UNLIKELY(!CROW_IS_NUM(ch))) { + CROW_SET_ERRNO(CHPE_INVALID_VERSION); + goto error; + } + + parser->http_major = ch - '0'; + parser->state = s_req_http_dot; + break; + + case s_req_http_dot: + { + if (CROW_UNLIKELY(ch != '.')) { + CROW_SET_ERRNO(CHPE_INVALID_VERSION); + goto error; + } + + parser->state = s_req_http_minor; + break; + } + + /* minor HTTP version */ + case s_req_http_minor: + if (CROW_UNLIKELY(!CROW_IS_NUM(ch))) { + CROW_SET_ERRNO(CHPE_INVALID_VERSION); + goto error; + } + + parser->http_minor = ch - '0'; + parser->state = s_req_http_end; + break; + + /* end of request line */ + case s_req_http_end: + { + if (ch == cr) { + parser->state = s_req_line_almost_done; + break; + } + + if (ch == lf) { + parser->state = s_header_field_start; + break; + } + + CROW_SET_ERRNO(CHPE_INVALID_VERSION); + goto error; + break; + } + + /* end of request line */ + case s_req_line_almost_done: + { + if (CROW_UNLIKELY(ch != lf)) { + CROW_SET_ERRNO(CHPE_LF_EXPECTED); + goto error; + } + + parser->state = s_header_field_start; + break; + } + + case s_header_field_start: + { + if (ch == cr) { + parser->state = s_headers_almost_done; + break; + } + + if (ch == lf) { + /* they might be just sending \n instead of \r\n so this would be + * the second \n to denote the end of headers*/ + parser->state = s_headers_almost_done; + CROW_REEXECUTE(); + } + + c = CROW_TOKEN(ch); + + if (CROW_UNLIKELY(!c)) { + CROW_SET_ERRNO(CHPE_INVALID_HEADER_TOKEN); + goto error; + } + + CROW_MARK(header_field); + + parser->index = 0; + parser->state = s_header_field; + + switch (c) { + case 'c': + parser->header_state = h_C; + break; + + case 'p': + parser->header_state = h_matching_proxy_connection; + break; + + case 't': + parser->header_state = h_matching_transfer_encoding; + break; + + case 'u': + parser->header_state = h_matching_upgrade; + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_field: + { + const char* start = p; + for (; p != data + len; p++) { + ch = *p; + c = CROW_TOKEN(ch); + + if (!c) + break; + + switch (parser->header_state) { + case h_general: { + size_t left = data + len - p; + const char* pe = p + CROW_MIN(left, max_header_size); + while (p+1 < pe && CROW_TOKEN(p[1])) { + p++; + } + break; + } + + case h_C: + parser->index++; + parser->header_state = (c == 'o' ? h_CO : h_general); + break; + + case h_CO: + parser->index++; + parser->header_state = (c == 'n' ? h_CON : h_general); + break; + + case h_CON: + parser->index++; + switch (c) { + case 'n': + parser->header_state = h_matching_connection; + break; + case 't': + parser->header_state = h_matching_content_length; + break; + default: + parser->header_state = h_general; + break; + } + break; + + /* connection */ + + case h_matching_connection: + parser->index++; + if (parser->index > sizeof(CROW_CONNECTION)-1 || c != CROW_CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CROW_CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* proxy-connection */ + + case h_matching_proxy_connection: + parser->index++; + if (parser->index > sizeof(CROW_PROXY_CONNECTION)-1 || c != CROW_PROXY_CONNECTION[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CROW_PROXY_CONNECTION)-2) { + parser->header_state = h_connection; + } + break; + + /* content-length */ + + case h_matching_content_length: + parser->index++; + if (parser->index > sizeof(CROW_CONTENT_LENGTH)-1 || c != CROW_CONTENT_LENGTH[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CROW_CONTENT_LENGTH)-2) { + parser->header_state = h_content_length; + } + break; + + /* transfer-encoding */ + + case h_matching_transfer_encoding: + parser->index++; + if (parser->index > sizeof(CROW_TRANSFER_ENCODING)-1 || c != CROW_TRANSFER_ENCODING[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CROW_TRANSFER_ENCODING)-2) { + parser->header_state = h_transfer_encoding; + parser->uses_transfer_encoding = 1; + } + break; + + /* upgrade */ + + case h_matching_upgrade: + parser->index++; + if (parser->index > sizeof(CROW_UPGRADE)-1 || c != CROW_UPGRADE[parser->index]) { + parser->header_state = h_general; + } else if (parser->index == sizeof(CROW_UPGRADE)-2) { + parser->header_state = h_upgrade; + } + break; + + case h_connection: + case h_content_length: + case h_transfer_encoding: + case h_upgrade: + if (ch != ' ') parser->header_state = h_general; + break; + + default: + assert(0 && "Unknown header_state"); + break; + } + } + + if (p == data + len) { + --p; + CROW_COUNT_HEADER_SIZE(p - start); + break; + } + + CROW_COUNT_HEADER_SIZE(p - start); + + if (ch == ':') { + parser->state = s_header_value_discard_ws; + CROW_CALLBACK_DATA(header_field); + break; + } +/* RFC-7230 Sec 3.2.4 expressly forbids line-folding in header field-names. + if (ch == cr) { + parser->state = s_header_almost_done; + CROW_CALLBACK_DATA(header_field); + break; + } + + if (ch == lf) { + parser->state = s_header_field_start; + CROW_CALLBACK_DATA(header_field); + break; + } +*/ + CROW_SET_ERRNO(CHPE_INVALID_HEADER_TOKEN); + goto error; + } + + case s_header_value_discard_ws: + if (ch == ' ' || ch == '\t') break; + + if (ch == cr) { + parser->state = s_header_value_discard_ws_almost_done; + break; + } + + if (ch == lf) { + parser->state = s_header_value_discard_lws; + break; + } + + /* fall through */ + + case s_header_value_start: + { + CROW_MARK(header_value); + + parser->state = s_header_value; + parser->index = 0; + + c = CROW_LOWER(ch); + + switch (parser->header_state) { + case h_upgrade: + // Crow does not support HTTP/2 at the moment. + // According to the RFC https://datatracker.ietf.org/doc/html/rfc7540#section-3.2 + // "A server that does not support HTTP/2 can respond to the request as though the Upgrade header field were absent" + // => `F_UPGRADE` is not set if the header starts by "h2". + // This prevents the parser from skipping the request body. + if (ch != 'h' || p+1 == (data + len) || *(p+1) != '2') { + parser->flags |= F_UPGRADE; + } + parser->header_state = h_general; + break; + + case h_transfer_encoding: + /* looking for 'Transfer-Encoding: chunked' */ + if ('c' == c) { + parser->header_state = h_matching_transfer_encoding_chunked; + } else { + parser->header_state = h_matching_transfer_encoding_token; + } + break; + + /* Multi-value `Transfer-Encoding` header */ + case h_matching_transfer_encoding_token_start: + break; + + case h_content_length: + if (CROW_UNLIKELY(!CROW_IS_NUM(ch))) { + CROW_SET_ERRNO(CHPE_INVALID_CONTENT_LENGTH); + goto error; + } + + if (parser->flags & F_CONTENTLENGTH) { + CROW_SET_ERRNO(CHPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + parser->flags |= F_CONTENTLENGTH; + parser->content_length = ch - '0'; + parser->header_state = h_content_length_num; + break; + + /* when obsolete line folding is encountered for content length + * continue to the s_header_value state */ + case h_content_length_ws: + break; + + case h_connection: + /* looking for 'Connection: keep-alive' */ + if (c == 'k') { + parser->header_state = h_matching_connection_keep_alive; + /* looking for 'Connection: close' */ + } else if (c == 'c') { + parser->header_state = h_matching_connection_close; + } else if (c == ' ' || c == '\t') { + /* Skip lws */ + } else { + parser->header_state = h_general; + } + break; + + default: + parser->header_state = h_general; + break; + } + break; + } + + case s_header_value: + { + const char* start = p; + enum header_states h_state = static_cast(parser->header_state); + for (; p != data + len; p++) { + ch = *p; + + if (ch == cr) { + parser->state = s_header_almost_done; + parser->header_state = h_state; + CROW_CALLBACK_DATA(header_value); + break; + } + + if (ch == lf) { + parser->state = s_header_almost_done; + CROW_COUNT_HEADER_SIZE(p - start); + parser->header_state = h_state; + CROW_CALLBACK_DATA_NOADVANCE(header_value); + CROW_REEXECUTE(); + } + + if (!lenient && !CROW_IS_HEADER_CHAR(ch)) { + CROW_SET_ERRNO(CHPE_INVALID_HEADER_TOKEN); + goto error; + } + + c = CROW_LOWER(ch); + + switch (h_state) { + case h_general: + { + size_t left = data + len - p; + const char* pe = p + CROW_MIN(left, max_header_size); + + for (; p != pe; p++) { + ch = *p; + if (ch == cr || ch == lf) { + --p; + break; + } + if (!lenient && !CROW_IS_HEADER_CHAR(ch)) { + CROW_SET_ERRNO(CHPE_INVALID_HEADER_TOKEN); + goto error; + } + } + if (p == data + len) + --p; + break; + } + + case h_connection: + case h_transfer_encoding: + assert(0 && "Shouldn't get here."); + break; + + case h_content_length: + if (ch == ' ') break; + h_state = h_content_length_num; + /* fall through */ + + case h_content_length_num: + { + uint64_t t; + + if (ch == ' ') { + h_state = h_content_length_ws; + break; + } + + if (CROW_UNLIKELY(!CROW_IS_NUM(ch))) { + CROW_SET_ERRNO(CHPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + } + + t = parser->content_length; + t *= 10; + t += ch - '0'; + + /* Overflow? Test against a conservative limit for simplicity. */ + if (CROW_UNLIKELY((CROW_ULLONG_MAX - 10) / 10 < parser->content_length)) { + CROW_SET_ERRNO(CHPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + } + + parser->content_length = t; + break; + } + + case h_content_length_ws: + if (ch == ' ') break; + CROW_SET_ERRNO(CHPE_INVALID_CONTENT_LENGTH); + parser->header_state = h_state; + goto error; + + /* Transfer-Encoding: chunked */ + case h_matching_transfer_encoding_token_start: + /* looking for 'Transfer-Encoding: chunked' */ + if ('c' == c) { + h_state = h_matching_transfer_encoding_chunked; + } else if (CROW_TOKEN(c)) { + /* TODO(indutny): similar code below does this, but why? + * At the very least it seems to be inconsistent given that + * h_matching_transfer_encoding_token does not check for + * `STRICT_TOKEN` + */ + h_state = h_matching_transfer_encoding_token; + } else if (c == ' ' || c == '\t') { + /* Skip lws */ + } else { + h_state = h_general; + } + break; + + case h_matching_transfer_encoding_chunked: + parser->index++; + if (parser->index > sizeof(CROW_CHUNKED)-1 || c != CROW_CHUNKED[parser->index]) { + h_state = h_matching_transfer_encoding_token; + } else if (parser->index == sizeof(CROW_CHUNKED)-2) { + h_state = h_transfer_encoding_chunked; + } + break; + + case h_matching_transfer_encoding_token: + if (ch == ',') { + h_state = h_matching_transfer_encoding_token_start; + parser->index = 0; + } + break; + + /* looking for 'Connection: keep-alive' */ + case h_matching_connection_keep_alive: + parser->index++; + if (parser->index > sizeof(CROW_KEEP_ALIVE)-1 || c != CROW_KEEP_ALIVE[parser->index]) { + h_state = h_general; + } else if (parser->index == sizeof(CROW_KEEP_ALIVE)-2) { + h_state = h_connection_keep_alive; + } + break; + + /* looking for 'Connection: close' */ + case h_matching_connection_close: + parser->index++; + if (parser->index > sizeof(CROW_CLOSE)-1 || c != CROW_CLOSE[parser->index]) { + h_state = h_general; + } else if (parser->index == sizeof(CROW_CLOSE)-2) { + h_state = h_connection_close; + } + break; + + // Edited from original (because of commits that werent included) + case h_transfer_encoding_chunked: + if (ch != ' ') h_state = h_matching_transfer_encoding_token; + break; + case h_connection_keep_alive: + case h_connection_close: + if (ch != ' ') h_state = h_general; + break; + + default: + parser->state = s_header_value; + h_state = h_general; + break; + } + } + parser->header_state = h_state; + + + if (p == data + len) + --p; + + CROW_COUNT_HEADER_SIZE(p - start); + break; + } + + case s_header_almost_done: + { + if (CROW_UNLIKELY(ch != lf)) { + CROW_SET_ERRNO(CHPE_LF_EXPECTED); + goto error; + } + + parser->state = s_header_value_lws; + break; + } + + case s_header_value_lws: + { + if (ch == ' ' || ch == '\t') { + if (parser->header_state == h_content_length_num) { + /* treat obsolete line folding as space */ + parser->header_state = h_content_length_ws; + } + parser->state = s_header_value_start; + CROW_REEXECUTE(); + } + + /* finished the header */ + switch (parser->header_state) { + case h_connection_keep_alive: + parser->flags |= F_CONNECTION_KEEP_ALIVE; + break; + case h_connection_close: + parser->flags |= F_CONNECTION_CLOSE; + break; + case h_transfer_encoding_chunked: + parser->flags |= F_CHUNKED; + break; + default: + break; + } + + parser->state = s_header_field_start; + CROW_REEXECUTE(); + } + + case s_header_value_discard_ws_almost_done: + { + CROW_STRICT_CHECK(ch != lf); + parser->state = s_header_value_discard_lws; + break; + } + + case s_header_value_discard_lws: + { + if (ch == ' ' || ch == '\t') { + parser->state = s_header_value_discard_ws; + break; + } else { + /* header value was empty */ + CROW_MARK(header_value); + parser->state = s_header_field_start; + CROW_CALLBACK_DATA_NOADVANCE(header_value); + CROW_REEXECUTE(); + } + } + + case s_headers_almost_done: + { + CROW_STRICT_CHECK(ch != lf); + + if (parser->flags & F_TRAILING) { + /* End of a chunked request */ + CROW_CALLBACK_NOTIFY(message_complete); + break; + } + + /* Cannot use transfer-encoding and a content-length header together + per the HTTP specification. (RFC 7230 Section 3.3.3) */ + if ((parser->uses_transfer_encoding == 1) && + (parser->flags & F_CONTENTLENGTH)) { + /* Allow it for lenient parsing as long as `Transfer-Encoding` is + * not `chunked` or allow_length_with_encoding is set + */ + if (parser->flags & F_CHUNKED) { + if (!allow_chunked_length) { + CROW_SET_ERRNO(CHPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + } else if (!lenient) { + CROW_SET_ERRNO(CHPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + } + + parser->state = s_headers_done; + + /* Set this here so that on_headers_complete() callbacks can see it */ + parser->upgrade = + (parser->flags & F_UPGRADE || parser->method == (unsigned)HTTPMethod::Connect); + + /* Here we call the headers_complete callback. This is somewhat + * different than other callbacks because if the user returns 1, we + * will interpret that as saying that this message has no body. This + * is needed for the annoying case of recieving a response to a HEAD + * request. + * + * We'd like to use CROW_CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so + * we have to simulate it by handling a change in errno below. + */ + if (settings->on_headers_complete) { + switch (settings->on_headers_complete(parser)) { + case 0: + break; + + case 2: + parser->upgrade = 1; + //break; + + /* fall through */ + case 1: + parser->flags |= F_SKIPBODY; + break; + + default: + CROW_SET_ERRNO(CHPE_CB_headers_complete); + parser->nread = nread; + return p - data; /* Error */ + } + } + + if (CROW_HTTP_PARSER_ERRNO(parser) != CHPE_OK) { + parser->nread = nread; + return p - data; + } + + CROW_REEXECUTE(); + } + + case s_headers_done: + { + CROW_STRICT_CHECK(ch != lf); + + parser->nread = 0; + nread = 0; + + /* Exit, the rest of the connect is in a different protocol. */ + if (parser->upgrade) { + CROW_CALLBACK_NOTIFY(message_complete); + parser->nread = nread; + return (p - data) + 1; + } + + if (parser->flags & F_SKIPBODY) { + CROW_CALLBACK_NOTIFY(message_complete); + } else if (parser->flags & F_CHUNKED) { + /* chunked encoding - ignore Content-Length header, + * prepare for a chunk */ + parser->state = s_chunk_size_start; + } + else if (parser->uses_transfer_encoding == 1) + { + if (!lenient) + { + /* RFC 7230 3.3.3 */ + + /* If a Transfer-Encoding header field + * is present in a request and the chunked transfer coding is not + * the final encoding, the message body length cannot be determined + * reliably; the server MUST respond with the 400 (Bad Request) + * status code and then close the connection. + */ + CROW_SET_ERRNO(CHPE_INVALID_TRANSFER_ENCODING); + parser->nread = nread; + return (p - data); /* Error */ + } + else + { + /* RFC 7230 3.3.3 */ + + /* If a Transfer-Encoding header field is present in a response and + * the chunked transfer coding is not the final encoding, the + * message body length is determined by reading the connection until + * it is closed by the server. + */ + parser->state = s_body_identity_eof; + } + } + else + { + if (parser->content_length == 0) + { + /* Content-Length header given but zero: Content-Length: 0\r\n */ + CROW_CALLBACK_NOTIFY(message_complete); + } + else if (parser->content_length != CROW_ULLONG_MAX) + { + /* Content-Length header given and non-zero */ + parser->state = s_body_identity; + } + else + { + /* Assume content-length 0 - read the next */ + CROW_CALLBACK_NOTIFY(message_complete); + } + } + + break; + } + + case s_body_identity: + { + uint64_t to_read = CROW_MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->content_length != 0 + && parser->content_length != CROW_ULLONG_MAX); + + /* The difference between advancing content_length and p is because + * the latter will automaticaly advance on the next loop iteration. + * Further, if content_length ends up at 0, we want to see the last + * byte again for our message complete callback. + */ + CROW_MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + parser->state = s_message_done; + + /* Mimic CROW_CALLBACK_DATA_NOADVANCE() but with one extra byte. + * + * The alternative to doing this is to wait for the next byte to + * trigger the data callback, just as in every other case. The + * problem with this is that this makes it difficult for the test + * harness to distinguish between complete-on-EOF and + * complete-on-length. It's not clear that this distinction is + * important for applications, but let's keep it for now. + */ + CROW_CALLBACK_DATA_(body, p - body_mark + 1, p - data); + CROW_REEXECUTE(); + } + + break; + } + + /* read until EOF */ + case s_body_identity_eof: + CROW_MARK(body); + p = data + len - 1; + + break; + + case s_message_done: + CROW_CALLBACK_NOTIFY(message_complete); + break; + + case s_chunk_size_start: + { + assert(nread == 1); + assert(parser->flags & F_CHUNKED); + + unhex_val = unhex[static_cast(ch)]; + if (CROW_UNLIKELY(unhex_val == -1)) { + CROW_SET_ERRNO(CHPE_INVALID_CHUNK_SIZE); + goto error; + } + + parser->content_length = unhex_val; + parser->state = s_chunk_size; + break; + } + + case s_chunk_size: + { + uint64_t t; + + assert(parser->flags & F_CHUNKED); + + if (ch == cr) { + parser->state = s_chunk_size_almost_done; + break; + } + + unhex_val = unhex[static_cast(ch)]; + + if (unhex_val == -1) { + if (ch == ';' || ch == ' ') { + parser->state = s_chunk_parameters; + break; + } + + CROW_SET_ERRNO(CHPE_INVALID_CHUNK_SIZE); + goto error; + } + + t = parser->content_length; + t *= 16; + t += unhex_val; + + /* Overflow? Test against a conservative limit for simplicity. */ + if (CROW_UNLIKELY((CROW_ULLONG_MAX - 16) / 16 < parser->content_length)) { + CROW_SET_ERRNO(CHPE_INVALID_CONTENT_LENGTH); + goto error; + } + + parser->content_length = t; + break; + } + + case s_chunk_parameters: + { + assert(parser->flags & F_CHUNKED); + /* just ignore this shit. TODO check for overflow */ + if (ch == cr) { + parser->state = s_chunk_size_almost_done; + break; + } + break; + } + + case s_chunk_size_almost_done: + { + assert(parser->flags & F_CHUNKED); + CROW_STRICT_CHECK(ch != lf); + + parser->nread = 0; + nread = 0; + + if (parser->content_length == 0) { + parser->flags |= F_TRAILING; + parser->state = s_header_field_start; + } else { + parser->state = s_chunk_data; + } + break; + } + + case s_chunk_data: + { + uint64_t to_read = CROW_MIN(parser->content_length, + (uint64_t) ((data + len) - p)); + + assert(parser->flags & F_CHUNKED); + assert(parser->content_length != 0 + && parser->content_length != CROW_ULLONG_MAX); + + /* See the explanation in s_body_identity for why the content + * length and data pointers are managed this way. + */ + CROW_MARK(body); + parser->content_length -= to_read; + p += to_read - 1; + + if (parser->content_length == 0) { + parser->state = s_chunk_data_almost_done; + } + + break; + } + + case s_chunk_data_almost_done: + assert(parser->flags & F_CHUNKED); + assert(parser->content_length == 0); + CROW_STRICT_CHECK(ch != cr); + parser->state = s_chunk_data_done; + CROW_CALLBACK_DATA(body); + break; + + case s_chunk_data_done: + assert(parser->flags & F_CHUNKED); + CROW_STRICT_CHECK(ch != lf); + parser->nread = 0; + nread = 0; + parser->state = s_chunk_size_start; + break; + + default: + assert(0 && "unhandled state"); + CROW_SET_ERRNO(CHPE_INVALID_INTERNAL_STATE); + goto error; + } + } + + /* Run callbacks for any marks that we have leftover after we ran out of + * bytes. There should be at most one of these set, so it's OK to invoke + * them in series (unset marks will not result in callbacks). + * + * We use the NOADVANCE() variety of callbacks here because 'p' has already + * overflowed 'data' and this allows us to correct for the off-by-one that + * we'd otherwise have (since CROW_CALLBACK_DATA() is meant to be run with a 'p' + * value that's in-bounds). + */ + + assert(((header_field_mark ? 1 : 0) + + (header_value_mark ? 1 : 0) + + (url_mark ? 1 : 0) + + (body_mark ? 1 : 0)) <= 1); + + CROW_CALLBACK_DATA_NOADVANCE(header_field); + CROW_CALLBACK_DATA_NOADVANCE(header_value); + CROW_CALLBACK_DATA_NOADVANCE(url); + CROW_CALLBACK_DATA_NOADVANCE(body); + + parser->nread = nread; + return len; + +error: + if (CROW_HTTP_PARSER_ERRNO(parser) == CHPE_OK) { + CROW_SET_ERRNO(CHPE_UNKNOWN); + } + + parser->nread = nread; + return (p - data); +} + +inline void + http_parser_init(http_parser* parser) +{ + void *data = parser->data; /* preserve application data */ + memset(parser, 0, sizeof(*parser)); + parser->data = data; + parser->state = s_start_req; + parser->http_errno = CHPE_OK; +} + +/* Return a string name of the given error */ +inline const char * +http_errno_name(enum http_errno err) { +/* Map errno values to strings for human-readable output */ +#define CROW_HTTP_STRERROR_GEN(n, s) { "CHPE_" #n, s }, +static struct { + const char *name; + const char *description; +} http_strerror_tab[] = { + CROW_HTTP_ERRNO_MAP(CROW_HTTP_STRERROR_GEN) +}; +#undef CROW_HTTP_STRERROR_GEN + assert(((size_t) err) < CROW_ARRAY_SIZE(http_strerror_tab)); + return http_strerror_tab[err].name; +} + +/* Return a string description of the given error */ +inline const char * +http_errno_description(enum http_errno err) { +/* Map errno values to strings for human-readable output */ +#define CROW_HTTP_STRERROR_GEN(n, s) { "CHPE_" #n, s }, +static struct { + const char *name; + const char *description; +} http_strerror_tab[] = { + CROW_HTTP_ERRNO_MAP(CROW_HTTP_STRERROR_GEN) +}; +#undef CROW_HTTP_STRERROR_GEN + assert(((size_t) err) < CROW_ARRAY_SIZE(http_strerror_tab)); + return http_strerror_tab[err].description; +} + +/* Checks if this is the final chunk of the body. */ +inline int +http_body_is_final(const struct http_parser *parser) { + return parser->state == s_message_done; +} + +/* Change the maximum header size provided at compile time. */ +inline void +http_parser_set_max_header_size(uint32_t size) { + max_header_size = size; +} + +#undef CROW_HTTP_ERRNO_MAP +#undef CROW_SET_ERRNO +#undef CROW_CALLBACK_NOTIFY_ +#undef CROW_CALLBACK_NOTIFY +#undef CROW_CALLBACK_NOTIFY_NOADVANCE +#undef CROW_CALLBACK_DATA_ +#undef CROW_CALLBACK_DATA +#undef CROW_CALLBACK_DATA_NOADVANCE +#undef CROW_MARK +#undef CROW_PROXY_CONNECTION +#undef CROW_CONNECTION +#undef CROW_CONTENT_LENGTH +#undef CROW_TRANSFER_ENCODING +#undef CROW_UPGRADE +#undef CROW_CHUNKED +#undef CROW_KEEP_ALIVE +#undef CROW_CLOSE +#undef CROW_PARSING_HEADER +#undef CROW_LOWER +#undef CROW_IS_ALPHA +#undef CROW_IS_NUM +#undef CROW_IS_ALPHANUM +//#undef CROW_IS_HEX +#undef CROW_IS_MARK +#undef CROW_IS_USERINFO_CHAR +#undef CROW_TOKEN +#undef CROW_IS_URL_CHAR +//#undef CROW_IS_HOST_CHAR +#undef CROW_STRICT_CHECK + +} + +// clang-format on + + +#include +#include +#include + + +namespace crow +{ + /// A wrapper for `nodejs/http-parser`. + + /// + /// Used to generate a \ref crow.request from the TCP socket buffer. + template + struct HTTPParser : public http_parser + { + static int on_message_begin(http_parser*) + { + return 0; + } + static int on_method(http_parser* self_) + { + HTTPParser* self = static_cast(self_); + self->req.method = static_cast(self->method); + + return 0; + } + static int on_url(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + self->req.raw_url.insert(self->req.raw_url.end(), at, at + length); + self->req.url_params = query_string(self->req.raw_url); + self->req.url = self->req.raw_url.substr(0, self->qs_point != 0 ? self->qs_point : std::string::npos); + + self->process_url(); + + return 0; + } + static int on_header_field(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + switch (self->header_building_state) + { + case 0: + if (!self->header_value.empty()) + { + self->req.headers.emplace(std::move(self->header_field), std::move(self->header_value)); + } + self->header_field.assign(at, at + length); + self->header_building_state = 1; + break; + case 1: + self->header_field.insert(self->header_field.end(), at, at + length); + break; + } + return 0; + } + static int on_header_value(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + switch (self->header_building_state) + { + case 0: + self->header_value.insert(self->header_value.end(), at, at + length); + break; + case 1: + self->header_building_state = 0; + self->header_value.assign(at, at + length); + break; + } + return 0; + } + static int on_headers_complete(http_parser* self_) + { + HTTPParser* self = static_cast(self_); + if (!self->header_field.empty()) + { + self->req.headers.emplace(std::move(self->header_field), std::move(self->header_value)); + } + + self->set_connection_parameters(); + + self->process_header(); + return 0; + } + static int on_body(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + self->req.body.insert(self->req.body.end(), at, at + length); + return 0; + } + static int on_message_complete(http_parser* self_) + { + HTTPParser* self = static_cast(self_); + + self->message_complete = true; + self->process_message(); + return 0; + } + HTTPParser(Handler* handler): + handler_(handler) + { + http_parser_init(this); + } + + // return false on error + /// Parse a buffer into the different sections of an HTTP request. + bool feed(const char* buffer, int length) + { + if (message_complete) + return true; + + const static http_parser_settings settings_{ + on_message_begin, + on_method, + on_url, + on_header_field, + on_header_value, + on_headers_complete, + on_body, + on_message_complete, + }; + + int nparsed = http_parser_execute(this, &settings_, buffer, length); + if (http_errno != CHPE_OK) + { + return false; + } + return nparsed == length; + } + + bool done() + { + return feed(nullptr, 0); + } + + void clear() + { + req = crow::request(); + header_field.clear(); + header_value.clear(); + header_building_state = 0; + qs_point = 0; + message_complete = false; + state = CROW_NEW_MESSAGE(); + } + + inline void process_url() + { + handler_->handle_url(); + } + + inline void process_header() + { + handler_->handle_header(); + } + + inline void process_message() + { + handler_->handle(); + } + + inline void set_connection_parameters() + { + req.http_ver_major = http_major; + req.http_ver_minor = http_minor; + + //NOTE(EDev): it seems that the problem is with crow's policy on closing the connection for HTTP_VERSION < 1.0, the behaviour for that in crow is "don't close the connection, but don't send a keep-alive either" + + // HTTP1.1 = always send keep_alive, HTTP1.0 = only send if header exists, HTTP?.? = never send + req.keep_alive = (http_major == 1 && http_minor == 0) ? + ((flags & F_CONNECTION_KEEP_ALIVE) ? true : false) : + ((http_major == 1 && http_minor == 1) ? true : false); + + // HTTP1.1 = only close if close header exists, HTTP1.0 = always close unless keep_alive header exists, HTTP?.?= never close + req.close_connection = (http_major == 1 && http_minor == 0) ? + ((flags & F_CONNECTION_KEEP_ALIVE) ? false : true) : + ((http_major == 1 && http_minor == 1) ? ((flags & F_CONNECTION_CLOSE) ? true : false) : false); + req.upgrade = static_cast(upgrade); + } + + /// The final request that this parser outputs. + /// + /// Data parsed is put directly into this object as soon as the related callback returns. (e.g. the request will have the cooorect method as soon as on_method() returns) + request req; + + private: + int header_building_state = 0; + bool message_complete = false; + std::string header_field; + std::string header_value; + + Handler* handler_; ///< This is currently an HTTP connection object (\ref crow.Connection). + }; +} // namespace crow + +#undef CROW_NEW_MESSAGE +#undef CROW_start_state + + + +#include +#include +#include +#include +#include +#include + +namespace crow +{ + enum class LogLevel + { +#ifndef ERROR +#ifndef DEBUG + DEBUG = 0, + INFO, + WARNING, + ERROR, + CRITICAL, +#endif +#endif + + Debug = 0, + Info, + Warning, + Error, + Critical, + }; + + class ILogHandler + { + public: + virtual ~ILogHandler() = default; + + virtual void log(std::string message, LogLevel level) = 0; + }; + + class CerrLogHandler : public ILogHandler + { + public: + void log(std::string message, LogLevel level) override + { + std::string prefix; + switch (level) + { + case LogLevel::Debug: + prefix = "DEBUG "; + break; + case LogLevel::Info: + prefix = "INFO "; + break; + case LogLevel::Warning: + prefix = "WARNING "; + break; + case LogLevel::Error: + prefix = "ERROR "; + break; + case LogLevel::Critical: + prefix = "CRITICAL"; + break; + } + std::cerr << std::string("(") + timestamp() + std::string(") [") + prefix + std::string("] ") + message << std::endl; + } + + private: + static std::string timestamp() + { + char date[32]; + time_t t = time(0); + + tm my_tm; + +#if defined(_MSC_VER) || defined(__MINGW32__) +#ifdef CROW_USE_LOCALTIMEZONE + localtime_s(&my_tm, &t); +#else + gmtime_s(&my_tm, &t); +#endif +#else +#ifdef CROW_USE_LOCALTIMEZONE + localtime_r(&t, &my_tm); +#else + gmtime_r(&t, &my_tm); +#endif +#endif + + size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm); + return std::string(date, date + sz); + } + }; + + class logger + { + public: + logger(LogLevel level): + level_(level) + {} + ~logger() + { +#ifdef CROW_ENABLE_LOGGING + if (level_ >= get_current_log_level()) + { + get_handler_ref()->log(stringstream_.str(), level_); + } +#endif + } + + // + template + logger& operator<<(T const& value) + { +#ifdef CROW_ENABLE_LOGGING + if (level_ >= get_current_log_level()) + { + stringstream_ << value; + } +#endif + return *this; + } + + // + static void setLogLevel(LogLevel level) { get_log_level_ref() = level; } + + static void setHandler(ILogHandler* handler) { get_handler_ref() = handler; } + + static LogLevel get_current_log_level() { return get_log_level_ref(); } + + private: + // + static LogLevel& get_log_level_ref() + { + static LogLevel current_level = static_cast(CROW_LOG_LEVEL); + return current_level; + } + static ILogHandler*& get_handler_ref() + { + static CerrLogHandler default_handler; + static ILogHandler* current_handler = &default_handler; + return current_handler; + } + + // + std::ostringstream stringstream_; + LogLevel level_; + }; +} // namespace crow + +#define CROW_LOG_CRITICAL \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::Critical) \ + crow::logger(crow::LogLevel::Critical) +#define CROW_LOG_ERROR \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::Error) \ + crow::logger(crow::LogLevel::Error) +#define CROW_LOG_WARNING \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning) \ + crow::logger(crow::LogLevel::Warning) +#define CROW_LOG_INFO \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::Info) \ + crow::logger(crow::LogLevel::Info) +#define CROW_LOG_DEBUG \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug) \ + crow::logger(crow::LogLevel::Debug) + + +//#define CROW_JSON_NO_ERROR_CHECK +//#define CROW_JSON_USE_MAP + +#include +#ifdef CROW_JSON_USE_MAP +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include + + +using std::isinf; +using std::isnan; + + +namespace crow // NOTE: Already documented in "crow/app.h" +{ + namespace mustache + { + class template_t; + } + + namespace json + { + inline void escape(const std::string& str, std::string& ret) + { + ret.reserve(ret.size() + str.size() + str.size() / 4); + for (auto c : str) + { + switch (c) + { + case '"': ret += "\\\""; break; + case '\\': ret += "\\\\"; break; + case '\n': ret += "\\n"; break; + case '\b': ret += "\\b"; break; + case '\f': ret += "\\f"; break; + case '\r': ret += "\\r"; break; + case '\t': ret += "\\t"; break; + default: + if (c >= 0 && c < 0x20) + { + ret += "\\u00"; + auto to_hex = [](char c) { + c = c & 0xf; + if (c < 10) + return '0' + c; + return 'a' + c - 10; + }; + ret += to_hex(c / 16); + ret += to_hex(c % 16); + } + else + ret += c; + break; + } + } + } + inline std::string escape(const std::string& str) + { + std::string ret; + escape(str, ret); + return ret; + } + + enum class type : char + { + Null, + False, + True, + Number, + String, + List, + Object, + Function + }; + + inline const char* get_type_str(type t) + { + switch (t) + { + case type::Number: return "Number"; + case type::False: return "False"; + case type::True: return "True"; + case type::List: return "List"; + case type::String: return "String"; + case type::Object: return "Object"; + case type::Function: return "Function"; + default: return "Unknown"; + } + } + + enum class num_type : char + { + Signed_integer, + Unsigned_integer, + Floating_point, + Null, + Double_precision_floating_point + }; + + class rvalue; + rvalue load(const char* data, size_t size); + + namespace detail + { + /// A read string implementation with comparison functionality. + struct r_string + { + r_string(){}; + r_string(char* s, char* e): + s_(s), e_(e){}; + ~r_string() + { + if (owned_) + delete[] s_; + } + + r_string(const r_string& r) + { + *this = r; + } + + r_string(r_string&& r) + { + *this = r; + } + + r_string& operator=(r_string&& r) + { + s_ = r.s_; + e_ = r.e_; + owned_ = r.owned_; + if (r.owned_) + r.owned_ = 0; + return *this; + } + + r_string& operator=(const r_string& r) + { + s_ = r.s_; + e_ = r.e_; + owned_ = 0; + return *this; + } + + operator std::string() const + { + return std::string(s_, e_); + } + + + const char* begin() const { return s_; } + const char* end() const { return e_; } + size_t size() const { return end() - begin(); } + + using iterator = const char*; + using const_iterator = const char*; + + char* s_; ///< Start. + mutable char* e_; ///< End. + uint8_t owned_{0}; + friend std::ostream& operator<<(std::ostream& os, const r_string& s) + { + os << static_cast(s); + return os; + } + + private: + void force(char* s, uint32_t length) + { + s_ = s; + e_ = s_ + length; + owned_ = 1; + } + friend rvalue crow::json::load(const char* data, size_t size); + + friend bool operator==(const r_string& l, const r_string& r); + friend bool operator==(const std::string& l, const r_string& r); + friend bool operator==(const r_string& l, const std::string& r); + + template + inline static bool equals(const T& l, const U& r) + { + if (l.size() != r.size()) + return false; + + for (size_t i = 0; i < l.size(); i++) + { + if (*(l.begin() + i) != *(r.begin() + i)) + return false; + } + + return true; + } + }; + + inline bool operator<(const r_string& l, const r_string& r) + { + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); + } + + inline bool operator<(const r_string& l, const std::string& r) + { + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); + } + + inline bool operator<(const std::string& l, const r_string& r) + { + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); + } + + inline bool operator>(const r_string& l, const r_string& r) + { + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); + } + + inline bool operator>(const r_string& l, const std::string& r) + { + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); + } + + inline bool operator>(const std::string& l, const r_string& r) + { + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); + } + + inline bool operator==(const r_string& l, const r_string& r) + { + return r_string::equals(l, r); + } + + inline bool operator==(const r_string& l, const std::string& r) + { + return r_string::equals(l, r); + } + + inline bool operator==(const std::string& l, const r_string& r) + { + return r_string::equals(l, r); + } + + inline bool operator!=(const r_string& l, const r_string& r) + { + return !(l == r); + } + + inline bool operator!=(const r_string& l, const std::string& r) + { + return !(l == r); + } + + inline bool operator!=(const std::string& l, const r_string& r) + { + return !(l == r); + } + } // namespace detail + + /// JSON read value. + + /// + /// Value can mean any json value, including a JSON object. + /// Read means this class is used to primarily read strings into a JSON value. + class rvalue + { + static const int cached_bit = 2; + static const int error_bit = 4; + + public: + rvalue() noexcept: + option_{error_bit} + { + } + rvalue(type t) noexcept: + lsize_{}, lremain_{}, t_{t} + { + } + rvalue(type t, char* s, char* e) noexcept: + start_{s}, end_{e}, t_{t} + { + determine_num_type(); + } + + rvalue(const rvalue& r): + start_(r.start_), end_(r.end_), key_(r.key_), t_(r.t_), nt_(r.nt_), option_(r.option_) + { + copy_l(r); + } + + rvalue(rvalue&& r) noexcept + { + *this = std::move(r); + } + + rvalue& operator=(const rvalue& r) + { + start_ = r.start_; + end_ = r.end_; + key_ = r.key_; + t_ = r.t_; + nt_ = r.nt_; + option_ = r.option_; + copy_l(r); + return *this; + } + rvalue& operator=(rvalue&& r) noexcept + { + start_ = r.start_; + end_ = r.end_; + key_ = std::move(r.key_); + l_ = std::move(r.l_); + lsize_ = r.lsize_; + lremain_ = r.lremain_; + t_ = r.t_; + nt_ = r.nt_; + option_ = r.option_; + return *this; + } + + explicit operator bool() const noexcept + { + return (option_ & error_bit) == 0; + } + + explicit operator int64_t() const + { + return i(); + } + + explicit operator uint64_t() const + { + return u(); + } + + explicit operator int() const + { + return static_cast(i()); + } + + /// Return any json value (not object or list) as a string. + explicit operator std::string() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() == type::Object || t() == type::List) + throw std::runtime_error("json type container"); +#endif + switch (t()) + { + case type::String: + return std::string(s()); + case type::Null: + return std::string("null"); + case type::True: + return std::string("true"); + case type::False: + return std::string("false"); + default: + return std::string(start_, end_ - start_); + } + } + + /// The type of the JSON value. + type t() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (option_ & error_bit) + { + throw std::runtime_error("invalid json object"); + } +#endif + return t_; + } + + /// The number type of the JSON value. + num_type nt() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (option_ & error_bit) + { + throw std::runtime_error("invalid json object"); + } +#endif + return nt_; + } + + /// The integer value. + int64_t i() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + switch (t()) + { + case type::Number: + case type::String: + return utility::lexical_cast(start_, end_ - start_); + default: + const std::string msg = "expected number, got: " + std::string(get_type_str(t())); + throw std::runtime_error(msg); + } +#endif + return utility::lexical_cast(start_, end_ - start_); + } + + /// The unsigned integer value. + uint64_t u() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + switch (t()) + { + case type::Number: + case type::String: + return utility::lexical_cast(start_, end_ - start_); + default: + throw std::runtime_error(std::string("expected number, got: ") + get_type_str(t())); + } +#endif + return utility::lexical_cast(start_, end_ - start_); + } + + /// The double precision floating-point number value. + double d() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Number) + throw std::runtime_error("value is not number"); +#endif + return utility::lexical_cast(start_, end_ - start_); + } + + /// The boolean value. + bool b() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::True && t() != type::False) + throw std::runtime_error("value is not boolean"); +#endif + return t() == type::True; + } + + /// The string value. + detail::r_string s() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::String) + throw std::runtime_error("value is not string"); +#endif + unescape(); + return detail::r_string{start_, end_}; + } + + /// The list or object value + std::vector lo() + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Object && t() != type::List) + throw std::runtime_error("value is not a container"); +#endif + std::vector ret; + ret.reserve(lsize_); + for (uint32_t i = 0; i < lsize_; i++) + { + ret.emplace_back(l_[i]); + } + return ret; + } + + /// Convert escaped string character to their original form ("\\n" -> '\n'). + void unescape() const + { + if (*(start_ - 1)) + { + char* head = start_; + char* tail = start_; + while (head != end_) + { + if (*head == '\\') + { + switch (*++head) + { + case '"': *tail++ = '"'; break; + case '\\': *tail++ = '\\'; break; + case '/': *tail++ = '/'; break; + case 'b': *tail++ = '\b'; break; + case 'f': *tail++ = '\f'; break; + case 'n': *tail++ = '\n'; break; + case 'r': *tail++ = '\r'; break; + case 't': *tail++ = '\t'; break; + case 'u': + { + auto from_hex = [](char c) { + if (c >= 'a') + return c - 'a' + 10; + if (c >= 'A') + return c - 'A' + 10; + return c - '0'; + }; + unsigned int code = + (from_hex(head[1]) << 12) + + (from_hex(head[2]) << 8) + + (from_hex(head[3]) << 4) + + from_hex(head[4]); + if (code >= 0x800) + { + *tail++ = 0xE0 | (code >> 12); + *tail++ = 0x80 | ((code >> 6) & 0x3F); + *tail++ = 0x80 | (code & 0x3F); + } + else if (code >= 0x80) + { + *tail++ = 0xC0 | (code >> 6); + *tail++ = 0x80 | (code & 0x3F); + } + else + { + *tail++ = code; + } + head += 4; + } + break; + } + } + else + *tail++ = *head; + head++; + } + end_ = tail; + *end_ = 0; + *(start_ - 1) = 0; + } + } + + /// Check if the json object has the passed string as a key. + bool has(const char* str) const + { + return has(std::string(str)); + } + + bool has(const std::string& str) const + { + struct Pred + { + bool operator()(const rvalue& l, const rvalue& r) const + { + return l.key_ < r.key_; + }; + bool operator()(const rvalue& l, const std::string& r) const + { + return l.key_ < r; + }; + bool operator()(const std::string& l, const rvalue& r) const + { + return l < r.key_; + }; + }; + if (!is_cached()) + { + std::sort(begin(), end(), Pred()); + set_cached(); + } + auto it = lower_bound(begin(), end(), str, Pred()); + return it != end() && it->key_ == str; + } + + int count(const std::string& str) + { + return has(str) ? 1 : 0; + } + + rvalue* begin() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Object && t() != type::List) + throw std::runtime_error("value is not a container"); +#endif + return l_.get(); + } + rvalue* end() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Object && t() != type::List) + throw std::runtime_error("value is not a container"); +#endif + return l_.get() + lsize_; + } + + const detail::r_string& key() const + { + return key_; + } + + size_t size() const + { + if (t() == type::String) + return s().size(); +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Object && t() != type::List) + throw std::runtime_error("value is not a container"); +#endif + return lsize_; + } + + const rvalue& operator[](int index) const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::List) + throw std::runtime_error("value is not a list"); + if (index >= static_cast(lsize_) || index < 0) + throw std::runtime_error("list out of bound"); +#endif + return l_[index]; + } + + const rvalue& operator[](size_t index) const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::List) + throw std::runtime_error("value is not a list"); + if (index >= lsize_) + throw std::runtime_error("list out of bound"); +#endif + return l_[index]; + } + + const rvalue& operator[](const char* str) const + { + return this->operator[](std::string(str)); + } + + const rvalue& operator[](const std::string& str) const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Object) + throw std::runtime_error("value is not an object"); +#endif + struct Pred + { + bool operator()(const rvalue& l, const rvalue& r) const + { + return l.key_ < r.key_; + }; + bool operator()(const rvalue& l, const std::string& r) const + { + return l.key_ < r; + }; + bool operator()(const std::string& l, const rvalue& r) const + { + return l < r.key_; + }; + }; + if (!is_cached()) + { + std::sort(begin(), end(), Pred()); + set_cached(); + } + auto it = lower_bound(begin(), end(), str, Pred()); + if (it != end() && it->key_ == str) + return *it; +#ifndef CROW_JSON_NO_ERROR_CHECK + throw std::runtime_error("cannot find key"); +#else + static rvalue nullValue; + return nullValue; +#endif + } + + void set_error() + { + option_ |= error_bit; + } + + bool error() const + { + return (option_ & error_bit) != 0; + } + + std::vector keys() const + { +#ifndef CROW_JSON_NO_ERROR_CHECK + if (t() != type::Object) + throw std::runtime_error("value is not an object"); +#endif + std::vector ret; + ret.reserve(lsize_); + for (uint32_t i = 0; i < lsize_; i++) + { + ret.emplace_back(std::string(l_[i].key())); + } + return ret; + } + + private: + bool is_cached() const + { + return (option_ & cached_bit) != 0; + } + void set_cached() const + { + option_ |= cached_bit; + } + void copy_l(const rvalue& r) + { + if (r.t() != type::Object && r.t() != type::List) + return; + lsize_ = r.lsize_; + lremain_ = 0; + l_.reset(new rvalue[lsize_]); + std::copy(r.begin(), r.end(), begin()); + } + + void emplace_back(rvalue&& v) + { + if (!lremain_) + { + int new_size = lsize_ + lsize_; + if (new_size - lsize_ > 60000) + new_size = lsize_ + 60000; + if (new_size < 4) + new_size = 4; + rvalue* p = new rvalue[new_size]; + rvalue* p2 = p; + for (auto& x : *this) + *p2++ = std::move(x); + l_.reset(p); + lremain_ = new_size - lsize_; + } + l_[lsize_++] = std::move(v); + lremain_--; + } + + /// Determines num_type from the string. + void determine_num_type() + { + if (t_ != type::Number) + { + nt_ = num_type::Null; + return; + } + + const std::size_t len = end_ - start_; + const bool has_minus = std::memchr(start_, '-', len) != nullptr; + const bool has_e = std::memchr(start_, 'e', len) != nullptr || std::memchr(start_, 'E', len) != nullptr; + const bool has_dec_sep = std::memchr(start_, '.', len) != nullptr; + if (has_dec_sep || has_e) + nt_ = num_type::Floating_point; + else if (has_minus) + nt_ = num_type::Signed_integer; + else + nt_ = num_type::Unsigned_integer; + } + + mutable char* start_; + mutable char* end_; + detail::r_string key_; + std::unique_ptr l_; + uint32_t lsize_; + uint16_t lremain_; + type t_; + num_type nt_{num_type::Null}; + mutable uint8_t option_{0}; + + friend rvalue load_nocopy_internal(char* data, size_t size); + friend rvalue load(const char* data, size_t size); + friend std::ostream& operator<<(std::ostream& os, const rvalue& r) + { + switch (r.t_) + { + + case type::Null: os << "null"; break; + case type::False: os << "false"; break; + case type::True: os << "true"; break; + case type::Number: + { + switch (r.nt()) + { + case num_type::Floating_point: os << r.d(); break; + case num_type::Double_precision_floating_point: os << r.d(); break; + case num_type::Signed_integer: os << r.i(); break; + case num_type::Unsigned_integer: os << r.u(); break; + case num_type::Null: throw std::runtime_error("Number with num_type Null"); + } + } + break; + case type::String: os << '"' << r.s() << '"'; break; + case type::List: + { + os << '['; + bool first = true; + for (auto& x : r) + { + if (!first) + os << ','; + first = false; + os << x; + } + os << ']'; + } + break; + case type::Object: + { + os << '{'; + bool first = true; + for (auto& x : r) + { + if (!first) + os << ','; + os << '"' << escape(x.key_) << "\":"; + first = false; + os << x; + } + os << '}'; + } + break; + case type::Function: os << "custom function"; break; + } + return os; + } + }; + namespace detail + { + } + + inline bool operator==(const rvalue& l, const std::string& r) + { + return l.s() == r; + } + + inline bool operator==(const std::string& l, const rvalue& r) + { + return l == r.s(); + } + + inline bool operator!=(const rvalue& l, const std::string& r) + { + return l.s() != r; + } + + inline bool operator!=(const std::string& l, const rvalue& r) + { + return l != r.s(); + } + + inline bool operator==(const rvalue& l, double r) + { + return l.d() == r; + } + + inline bool operator==(double l, const rvalue& r) + { + return l == r.d(); + } + + inline bool operator!=(const rvalue& l, double r) + { + return l.d() != r; + } + + inline bool operator!=(double l, const rvalue& r) + { + return l != r.d(); + } + + + inline rvalue load_nocopy_internal(char* data, size_t size) + { + // Defend against excessive recursion + static constexpr unsigned max_depth = 10000; + + //static const char* escaped = "\"\\/\b\f\n\r\t"; + struct Parser + { + Parser(char* data, size_t /*size*/): + data(data) + { + } + + bool consume(char c) + { + if (CROW_UNLIKELY(*data != c)) + return false; + data++; + return true; + } + + void ws_skip() + { + while (*data == ' ' || *data == '\t' || *data == '\r' || *data == '\n') + ++data; + }; + + rvalue decode_string() + { + if (CROW_UNLIKELY(!consume('"'))) + return {}; + char* start = data; + uint8_t has_escaping = 0; + while (1) + { + if (CROW_LIKELY(*data != '"' && *data != '\\' && *data != '\0')) + { + data++; + } + else if (*data == '"') + { + *data = 0; + *(start - 1) = has_escaping; + data++; + return {type::String, start, data - 1}; + } + else if (*data == '\\') + { + has_escaping = 1; + data++; + switch (*data) + { + case 'u': + { + auto check = [](char c) { + return ('0' <= c && c <= '9') || + ('a' <= c && c <= 'f') || + ('A' <= c && c <= 'F'); + }; + if (!(check(*(data + 1)) && + check(*(data + 2)) && + check(*(data + 3)) && + check(*(data + 4)))) + return {}; + } + data += 5; + break; + case '"': + case '\\': + case '/': + case 'b': + case 'f': + case 'n': + case 'r': + case 't': + data++; + break; + default: + return {}; + } + } + else + return {}; + } + return {}; + } + + rvalue decode_list(unsigned depth) + { + rvalue ret(type::List); + if (CROW_UNLIKELY(!consume('[')) || CROW_UNLIKELY(depth > max_depth)) + { + ret.set_error(); + return ret; + } + ws_skip(); + if (CROW_UNLIKELY(*data == ']')) + { + data++; + return ret; + } + + while (1) + { + auto v = decode_value(depth + 1); + if (CROW_UNLIKELY(!v)) + { + ret.set_error(); + break; + } + ws_skip(); + ret.emplace_back(std::move(v)); + if (*data == ']') + { + data++; + break; + } + if (CROW_UNLIKELY(!consume(','))) + { + ret.set_error(); + break; + } + ws_skip(); + } + return ret; + } + + rvalue decode_number() + { + char* start = data; + + enum NumberParsingState + { + Minus, + AfterMinus, + ZeroFirst, + Digits, + DigitsAfterPoints, + E, + DigitsAfterE, + Invalid, + } state{Minus}; + while (CROW_LIKELY(state != Invalid)) + { + switch (*data) + { + case '0': + state = static_cast("\2\2\7\3\4\6\6"[state]); + /*if (state == NumberParsingState::Minus || state == NumberParsingState::AfterMinus) + { + state = NumberParsingState::ZeroFirst; + } + else if (state == NumberParsingState::Digits || + state == NumberParsingState::DigitsAfterE || + state == NumberParsingState::DigitsAfterPoints) + { + // ok; pass + } + else if (state == NumberParsingState::E) + { + state = NumberParsingState::DigitsAfterE; + } + else + return {};*/ + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + state = static_cast("\3\3\7\3\4\6\6"[state]); + while (*(data + 1) >= '0' && *(data + 1) <= '9') + data++; + /*if (state == NumberParsingState::Minus || state == NumberParsingState::AfterMinus) + { + state = NumberParsingState::Digits; + } + else if (state == NumberParsingState::Digits || + state == NumberParsingState::DigitsAfterE || + state == NumberParsingState::DigitsAfterPoints) + { + // ok; pass + } + else if (state == NumberParsingState::E) + { + state = NumberParsingState::DigitsAfterE; + } + else + return {};*/ + break; + case '.': + state = static_cast("\7\7\4\4\7\7\7"[state]); + /* + if (state == NumberParsingState::Digits || state == NumberParsingState::ZeroFirst) + { + state = NumberParsingState::DigitsAfterPoints; + } + else + return {}; + */ + break; + case '-': + state = static_cast("\1\7\7\7\7\6\7"[state]); + /*if (state == NumberParsingState::Minus) + { + state = NumberParsingState::AfterMinus; + } + else if (state == NumberParsingState::E) + { + state = NumberParsingState::DigitsAfterE; + } + else + return {};*/ + break; + case '+': + state = static_cast("\7\7\7\7\7\6\7"[state]); + /*if (state == NumberParsingState::E) + { + state = NumberParsingState::DigitsAfterE; + } + else + return {};*/ + break; + case 'e': + case 'E': + state = static_cast("\7\7\7\5\5\7\7"[state]); + /*if (state == NumberParsingState::Digits || + state == NumberParsingState::DigitsAfterPoints) + { + state = NumberParsingState::E; + } + else + return {};*/ + break; + default: + if (CROW_LIKELY(state == NumberParsingState::ZeroFirst || + state == NumberParsingState::Digits || + state == NumberParsingState::DigitsAfterPoints || + state == NumberParsingState::DigitsAfterE)) + return {type::Number, start, data}; + else + return {}; + } + data++; + } + + return {}; + } + + + rvalue decode_value(unsigned depth) + { + switch (*data) + { + case '[': + return decode_list(depth + 1); + case '{': + return decode_object(depth + 1); + case '"': + return decode_string(); + case 't': + if ( //e-data >= 4 && + data[1] == 'r' && + data[2] == 'u' && + data[3] == 'e') + { + data += 4; + return {type::True}; + } + else + return {}; + case 'f': + if ( //e-data >= 5 && + data[1] == 'a' && + data[2] == 'l' && + data[3] == 's' && + data[4] == 'e') + { + data += 5; + return {type::False}; + } + else + return {}; + case 'n': + if ( //e-data >= 4 && + data[1] == 'u' && + data[2] == 'l' && + data[3] == 'l') + { + data += 4; + return {type::Null}; + } + else + return {}; + //case '1': case '2': case '3': + //case '4': case '5': case '6': + //case '7': case '8': case '9': + //case '0': case '-': + default: + return decode_number(); + } + return {}; + } + + rvalue decode_object(unsigned depth) + { + rvalue ret(type::Object); + if (CROW_UNLIKELY(!consume('{')) || CROW_UNLIKELY(depth > max_depth)) + { + ret.set_error(); + return ret; + } + + ws_skip(); + + if (CROW_UNLIKELY(*data == '}')) + { + data++; + return ret; + } + + while (1) + { + auto t = decode_string(); + if (CROW_UNLIKELY(!t)) + { + ret.set_error(); + break; + } + + ws_skip(); + if (CROW_UNLIKELY(!consume(':'))) + { + ret.set_error(); + break; + } + + // TODO(ipkn) caching key to speed up (flyweight?) + // I have no idea how flyweight could apply here, but maybe some speedup can happen if we stopped checking type since decode_string returns a string anyway + auto key = t.s(); + + ws_skip(); + auto v = decode_value(depth + 1); + if (CROW_UNLIKELY(!v)) + { + ret.set_error(); + break; + } + ws_skip(); + + v.key_ = std::move(key); + ret.emplace_back(std::move(v)); + if (CROW_UNLIKELY(*data == '}')) + { + data++; + break; + } + if (CROW_UNLIKELY(!consume(','))) + { + ret.set_error(); + break; + } + ws_skip(); + } + return ret; + } + + rvalue parse() + { + ws_skip(); + auto ret = decode_value(0); // or decode object? + ws_skip(); + if (ret && *data != '\0') + ret.set_error(); + return ret; + } + + char* data; + }; + return Parser(data, size).parse(); + } + inline rvalue load(const char* data, size_t size) + { + char* s = new char[size + 1]; + memcpy(s, data, size); + s[size] = 0; + auto ret = load_nocopy_internal(s, size); + if (ret) + ret.key_.force(s, size); + else + delete[] s; + return ret; + } + + inline rvalue load(const char* data) + { + return load(data, strlen(data)); + } + + inline rvalue load(const std::string& str) + { + return load(str.data(), str.size()); + } + + struct wvalue_reader; + + /// JSON write value. + + /// + /// Value can mean any json value, including a JSON object.
+ /// Write means this class is used to primarily assemble JSON objects using keys and values and export those into a string. + class wvalue : public returnable + { + friend class crow::mustache::template_t; + friend struct wvalue_reader; + + public: + using object = +#ifdef CROW_JSON_USE_MAP + std::map; +#else + std::unordered_map; +#endif + + using list = std::vector; + + type t() const { return t_; } + + /// Create an empty json value (outputs "{}" instead of a "null" string) + static crow::json::wvalue empty_object() { return crow::json::wvalue::object(); } + + private: + type t_{type::Null}; ///< The type of the value. + num_type nt{num_type::Null}; ///< The specific type of the number if \ref t_ is a number. + union number + { + double d; + int64_t si; + uint64_t ui; + + public: + constexpr number() noexcept: + ui() {} /* default constructor initializes unsigned integer. */ + constexpr number(std::uint64_t value) noexcept: + ui(value) {} + constexpr number(std::int64_t value) noexcept: + si(value) {} + explicit constexpr number(double value) noexcept: + d(value) {} + explicit constexpr number(float value) noexcept: + d(value) {} + } num; ///< Value if type is a number. + std::string s; ///< Value if type is a string. + std::unique_ptr l; ///< Value if type is a list. + std::unique_ptr o; ///< Value if type is a JSON object. + std::function f; ///< Value if type is a function (C++ lambda) + + public: + wvalue(): + returnable("application/json") {} + + wvalue(std::nullptr_t): + returnable("application/json"), t_(type::Null) {} + + wvalue(bool value): + returnable("application/json"), t_(value ? type::True : type::False) {} + + wvalue(std::uint8_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast(value)) {} + wvalue(std::uint16_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast(value)) {} + wvalue(std::uint32_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast(value)) {} + wvalue(std::uint64_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Unsigned_integer), num(static_cast(value)) {} + + wvalue(std::int8_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast(value)) {} + wvalue(std::int16_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast(value)) {} + wvalue(std::int32_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast(value)) {} + wvalue(std::int64_t value): + returnable("application/json"), t_(type::Number), nt(num_type::Signed_integer), num(static_cast(value)) {} + + wvalue(float value): + returnable("application/json"), t_(type::Number), nt(num_type::Floating_point), num(static_cast(value)) {} + wvalue(double value): + returnable("application/json"), t_(type::Number), nt(num_type::Double_precision_floating_point), num(static_cast(value)) {} + + wvalue(char const* value): + returnable("application/json"), t_(type::String), s(value) {} + + wvalue(std::string const& value): + returnable("application/json"), t_(type::String), s(value) {} + wvalue(std::string&& value): + returnable("application/json"), t_(type::String), s(std::move(value)) {} + + wvalue(std::initializer_list> initializer_list): + returnable("application/json"), t_(type::Object), o(new object(initializer_list)) {} + + wvalue(object const& value): + returnable("application/json"), t_(type::Object), o(new object(value)) {} + wvalue(object&& value): + returnable("application/json"), t_(type::Object), o(new object(std::move(value))) {} + + wvalue(const list& r): + returnable("application/json") + { + t_ = type::List; + l = std::unique_ptr(new list{}); + l->reserve(r.size()); + for (auto it = r.begin(); it != r.end(); ++it) + l->emplace_back(*it); + } + wvalue(list& r): + returnable("application/json") + { + t_ = type::List; + l = std::unique_ptr(new list{}); + l->reserve(r.size()); + for (auto it = r.begin(); it != r.end(); ++it) + l->emplace_back(*it); + } + + /// Create a write value from a read value (useful for editing JSON strings). + wvalue(const rvalue& r): + returnable("application/json") + { + t_ = r.t(); + switch (r.t()) + { + case type::Null: + case type::False: + case type::True: + case type::Function: + return; + case type::Number: + nt = r.nt(); + if (nt == num_type::Floating_point || nt == num_type::Double_precision_floating_point) + num.d = r.d(); + else if (nt == num_type::Signed_integer) + num.si = r.i(); + else + num.ui = r.u(); + return; + case type::String: + s = r.s(); + return; + case type::List: + l = std::unique_ptr(new list{}); + l->reserve(r.size()); + for (auto it = r.begin(); it != r.end(); ++it) + l->emplace_back(*it); + return; + case type::Object: + o = std::unique_ptr(new object{}); + for (auto it = r.begin(); it != r.end(); ++it) + o->emplace(it->key(), *it); + return; + } + } + + wvalue(const wvalue& r): + returnable("application/json") + { + t_ = r.t(); + switch (r.t()) + { + case type::Null: + case type::False: + case type::True: + return; + case type::Number: + nt = r.nt; + if (nt == num_type::Floating_point || nt == num_type::Double_precision_floating_point) + num.d = r.num.d; + else if (nt == num_type::Signed_integer) + num.si = r.num.si; + else + num.ui = r.num.ui; + return; + case type::String: + s = r.s; + return; + case type::List: + l = std::unique_ptr(new list{}); + l->reserve(r.size()); + for (auto it = r.l->begin(); it != r.l->end(); ++it) + l->emplace_back(*it); + return; + case type::Object: + o = std::unique_ptr(new object{}); + o->insert(r.o->begin(), r.o->end()); + return; + case type::Function: + f = r.f; + } + } + + wvalue(wvalue&& r): + returnable("application/json") + { + *this = std::move(r); + } + + wvalue& operator=(wvalue&& r) + { + t_ = r.t_; + nt = r.nt; + num = r.num; + s = std::move(r.s); + l = std::move(r.l); + o = std::move(r.o); + return *this; + } + + /// Used for compatibility, same as \ref reset() + void clear() + { + reset(); + } + + void reset() + { + t_ = type::Null; + l.reset(); + o.reset(); + } + + wvalue& operator=(std::nullptr_t) + { + reset(); + return *this; + } + wvalue& operator=(bool value) + { + reset(); + if (value) + t_ = type::True; + else + t_ = type::False; + return *this; + } + + wvalue& operator=(float value) + { + reset(); + t_ = type::Number; + num.d = value; + nt = num_type::Floating_point; + return *this; + } + + wvalue& operator=(double value) + { + reset(); + t_ = type::Number; + num.d = value; + nt = num_type::Double_precision_floating_point; + return *this; + } + + wvalue& operator=(unsigned short value) + { + reset(); + t_ = type::Number; + num.ui = value; + nt = num_type::Unsigned_integer; + return *this; + } + + wvalue& operator=(short value) + { + reset(); + t_ = type::Number; + num.si = value; + nt = num_type::Signed_integer; + return *this; + } + + wvalue& operator=(long long value) + { + reset(); + t_ = type::Number; + num.si = value; + nt = num_type::Signed_integer; + return *this; + } + + wvalue& operator=(long value) + { + reset(); + t_ = type::Number; + num.si = value; + nt = num_type::Signed_integer; + return *this; + } + + wvalue& operator=(int value) + { + reset(); + t_ = type::Number; + num.si = value; + nt = num_type::Signed_integer; + return *this; + } + + wvalue& operator=(unsigned long long value) + { + reset(); + t_ = type::Number; + num.ui = value; + nt = num_type::Unsigned_integer; + return *this; + } + + wvalue& operator=(unsigned long value) + { + reset(); + t_ = type::Number; + num.ui = value; + nt = num_type::Unsigned_integer; + return *this; + } + + wvalue& operator=(unsigned int value) + { + reset(); + t_ = type::Number; + num.ui = value; + nt = num_type::Unsigned_integer; + return *this; + } + + wvalue& operator=(const char* str) + { + reset(); + t_ = type::String; + s = str; + return *this; + } + + wvalue& operator=(const std::string& str) + { + reset(); + t_ = type::String; + s = str; + return *this; + } + + wvalue& operator=(list&& v) + { + if (t_ != type::List) + reset(); + t_ = type::List; + if (!l) + l = std::unique_ptr(new list{}); + l->clear(); + l->resize(v.size()); + size_t idx = 0; + for (auto& x : v) + { + (*l)[idx++] = std::move(x); + } + return *this; + } + + template + wvalue& operator=(const std::vector& v) + { + if (t_ != type::List) + reset(); + t_ = type::List; + if (!l) + l = std::unique_ptr(new list{}); + l->clear(); + l->resize(v.size()); + size_t idx = 0; + for (auto& x : v) + { + (*l)[idx++] = x; + } + return *this; + } + + wvalue& operator=(std::initializer_list> initializer_list) + { + if (t_ != type::Object) + { + reset(); + t_ = type::Object; + o = std::unique_ptr(new object(initializer_list)); + } + else + { +#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION) + o = std::unique_ptr(new object(initializer_list)); +#else + (*o) = initializer_list; +#endif + } + return *this; + } + + wvalue& operator=(object const& value) + { + if (t_ != type::Object) + { + reset(); + t_ = type::Object; + o = std::unique_ptr(new object(value)); + } + else + { +#if defined(__APPLE__) || defined(__MACH__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(_LIBCPP_VERSION) + o = std::unique_ptr(new object(value)); +#else + (*o) = value; +#endif + } + return *this; + } + + wvalue& operator=(object&& value) + { + if (t_ != type::Object) + { + reset(); + t_ = type::Object; + o = std::unique_ptr(new object(std::move(value))); + } + else + { + (*o) = std::move(value); + } + return *this; + } + + wvalue& operator=(std::function&& func) + { + reset(); + t_ = type::Function; + f = std::move(func); + return *this; + } + + wvalue& operator[](unsigned index) + { + if (t_ != type::List) + reset(); + t_ = type::List; + if (!l) + l = std::unique_ptr(new list{}); + if (l->size() < index + 1) + l->resize(index + 1); + return (*l)[index]; + } + + const wvalue& operator[](unsigned index) const + { + return const_cast(this)->operator[](index); + } + + int count(const std::string& str) const + { + if (t_ != type::Object) + return 0; + if (!o) + return 0; + return o->count(str); + } + + wvalue& operator[](const std::string& str) + { + if (t_ != type::Object) + reset(); + t_ = type::Object; + if (!o) + o = std::unique_ptr(new object{}); + return (*o)[str]; + } + + const wvalue& operator[](const std::string& str) const + { + return const_cast(this)->operator[](str); + } + + std::vector keys() const + { + if (t_ != type::Object) + return {}; + std::vector result; + for (auto& kv : *o) + { + result.push_back(kv.first); + } + return result; + } + + std::string execute(std::string txt = "") const //Not using reference because it cannot be used with a default rvalue + { + if (t_ != type::Function) + return ""; + return f(txt); + } + + /// If the wvalue is a list, it returns the length of the list, otherwise it returns 1. + std::size_t size() const + { + if (t_ != type::List) + return 1; + return l->size(); + } + + /// Returns an estimated size of the value in bytes. + size_t estimate_length() const + { + switch (t_) + { + case type::Null: return 4; + case type::False: return 5; + case type::True: return 4; + case type::Number: return 30; + case type::String: return 2 + s.size() + s.size() / 2; + case type::List: + { + size_t sum{}; + if (l) + { + for (auto& x : *l) + { + sum += 1; + sum += x.estimate_length(); + } + } + return sum + 2; + } + case type::Object: + { + size_t sum{}; + if (o) + { + for (auto& kv : *o) + { + sum += 2; + sum += 2 + kv.first.size() + kv.first.size() / 2; + sum += kv.second.estimate_length(); + } + } + return sum + 2; + } + case type::Function: + return 0; + } + return 1; + } + + private: + inline void dump_string(const std::string& str, std::string& out) const + { + out.push_back('"'); + escape(str, out); + out.push_back('"'); + } + + inline void dump_indentation_part(std::string& out, const int indent, const char separator, const int indent_level) const + { + out.push_back('\n'); + out.append(indent_level * indent, separator); + } + + + inline void dump_internal(const wvalue& v, std::string& out, const int indent, const char separator, const int indent_level = 0) const + { + switch (v.t_) + { + case type::Null: out += "null"; break; + case type::False: out += "false"; break; + case type::True: out += "true"; break; + case type::Number: + { + if (v.nt == num_type::Floating_point || v.nt == num_type::Double_precision_floating_point) + { + if (isnan(v.num.d) || isinf(v.num.d)) + { + out += "null"; + CROW_LOG_WARNING << "Invalid JSON value detected (" << v.num.d << "), value set to null"; + break; + } + enum + { + start, + decp, // Decimal point + zero + } f_state; + char outbuf[128]; + if (v.nt == num_type::Double_precision_floating_point) + { +#ifdef _MSC_VER + sprintf_s(outbuf, sizeof(outbuf), "%.*g", DECIMAL_DIG, v.num.d); +#else + snprintf(outbuf, sizeof(outbuf), "%.*g", DECIMAL_DIG, v.num.d); +#endif + } + else + { +#ifdef _MSC_VER + sprintf_s(outbuf, sizeof(outbuf), "%f", v.num.d); +#else + snprintf(outbuf, sizeof(outbuf), "%f", v.num.d); +#endif + } + char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0 + f_state = start; + while (*p != '\0') + { + //std::cout << *p << std::endl; + char ch = *p; + switch (f_state) + { + case start: // Loop and lookahead until a decimal point is found + if (ch == '.') + { + char fch = *(p + 1); + // if the first character is 0, leave it be (this is so that "1.00000" becomes "1.0" and not "1.") + if (fch != '\0' && fch == '0') p++; + f_state = decp; + } + p++; + break; + case decp: // Loop until a 0 is found, if found, record its position + if (ch == '0') + { + f_state = zero; + o = p; + } + p++; + break; + case zero: // if a non 0 is found (e.g. 1.00004) remove the earlier recorded 0 position and look for more trailing 0s + if (ch != '0') + { + o = nullptr; + f_state = decp; + } + p++; + break; + } + } + if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin + *o = '\0'; + out += outbuf; + } + else if (v.nt == num_type::Signed_integer) + { + out += std::to_string(v.num.si); + } + else + { + out += std::to_string(v.num.ui); + } + } + break; + case type::String: dump_string(v.s, out); break; + case type::List: + { + out.push_back('['); + + if (indent >= 0) + { + dump_indentation_part(out, indent, separator, indent_level + 1); + } + + if (v.l) + { + bool first = true; + for (auto& x : *v.l) + { + if (!first) + { + out.push_back(','); + + if (indent >= 0) + { + dump_indentation_part(out, indent, separator, indent_level + 1); + } + } + first = false; + dump_internal(x, out, indent, separator, indent_level + 1); + } + } + + if (indent >= 0) + { + dump_indentation_part(out, indent, separator, indent_level); + } + + out.push_back(']'); + } + break; + case type::Object: + { + out.push_back('{'); + + if (indent >= 0) + { + dump_indentation_part(out, indent, separator, indent_level + 1); + } + + if (v.o) + { + bool first = true; + for (auto& kv : *v.o) + { + if (!first) + { + out.push_back(','); + if (indent >= 0) + { + dump_indentation_part(out, indent, separator, indent_level + 1); + } + } + first = false; + dump_string(kv.first, out); + out.push_back(':'); + + if (indent >= 0) + { + out.push_back(' '); + } + + dump_internal(kv.second, out, indent, separator, indent_level + 1); + } + } + + if (indent >= 0) + { + dump_indentation_part(out, indent, separator, indent_level); + } + + out.push_back('}'); + } + break; + + case type::Function: + out += "custom function"; + break; + } + } + + public: + std::string dump(const int indent, const char separator = ' ') const + { + std::string ret; + ret.reserve(estimate_length()); + dump_internal(*this, ret, indent, separator); + return ret; + } + + std::string dump() const + { + static constexpr int DontIndent = -1; + + return dump(DontIndent); + } + }; + + // Used for accessing the internals of a wvalue + struct wvalue_reader + { + int64_t get(int64_t fallback) + { + if (ref.t() != type::Number || ref.nt == num_type::Floating_point || + ref.nt == num_type::Double_precision_floating_point) + return fallback; + return ref.num.si; + } + + double get(double fallback) + { + if (ref.t() != type::Number || ref.nt != num_type::Floating_point || + ref.nt == num_type::Double_precision_floating_point) + return fallback; + return ref.num.d; + } + + bool get(bool fallback) + { + if (ref.t() == type::True) return true; + if (ref.t() == type::False) return false; + return fallback; + } + + std::string get(const std::string& fallback) + { + if (ref.t() != type::String) return fallback; + return ref.s; + } + + const wvalue& ref; + }; + + //std::vector dump_ref(wvalue& v) + //{ + //} + } // namespace json +} // namespace crow + +#include +#include +#include +#include +#include +// S_ISREG is not defined for windows +// This defines it like suggested in https://stackoverflow.com/a/62371749 +#if defined(_MSC_VER) +#define _CRT_INTERNAL_NONSTDC_NAMES 1 +#endif +#include +#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG) +#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG) +#endif + + + +namespace crow +{ + template + class Connection; + + class Router; + + /// HTTP response + struct response + { + template + friend class crow::Connection; + + friend class Router; + + int code{200}; ///< The Status code for the response. + std::string body; ///< The actual payload containing the response data. + ci_map headers; ///< HTTP headers. + +#ifdef CROW_ENABLE_COMPRESSION + bool compressed = true; ///< If compression is enabled and this is false, the individual response will not be compressed. +#endif + bool skip_body = false; ///< Whether this is a response to a HEAD request. + bool manual_length_header = false; ///< Whether Crow should automatically add a "Content-Length" header. + + /// Set the value of an existing header in the response. + void set_header(std::string key, std::string value) + { + headers.erase(key); + headers.emplace(std::move(key), std::move(value)); + } + + /// Add a new header to the response. + void add_header(std::string key, std::string value) + { + headers.emplace(std::move(key), std::move(value)); + } + + const std::string& get_header_value(const std::string& key) + { + return crow::get_header_value(headers, key); + } + + // naive validation of a mime-type string + static bool validate_mime_type(const std::string& candidate) noexcept + { + // Here we simply check that the candidate type starts with + // a valid parent type, and has at least one character afterwards. + std::array valid_parent_types = { + "application/", "audio/", "font/", "example/", + "image/", "message/", "model/", "multipart/", + "text/", "video/"}; + for (const std::string& parent : valid_parent_types) + { + // ensure the candidate is *longer* than the parent, + // to avoid unnecessary string comparison and to + // reject zero-length subtypes. + if (candidate.size() <= parent.size()) + { + continue; + } + // strncmp is used rather than substr to avoid allocation, + // but a string_view approach would be better if Crow + // migrates to C++17. + if (strncmp(parent.c_str(), candidate.c_str(), parent.size()) == 0) + { + return true; + } + } + return false; + } + + // Find the mime type from the content type either by lookup, + // or by the content type itself, if it is a valid a mime type. + // Defaults to text/plain. + static std::string get_mime_type(const std::string& contentType) + { + const auto mimeTypeIterator = mime_types.find(contentType); + if (mimeTypeIterator != mime_types.end()) + { + return mimeTypeIterator->second; + } + else if (validate_mime_type(contentType)) + { + return contentType; + } + else + { + CROW_LOG_WARNING << "Unable to interpret mime type for content type '" << contentType << "'. Defaulting to text/plain."; + return "text/plain"; + } + } + + + // clang-format off + response() {} + explicit response(int code) : code(code) {} + response(std::string body) : body(std::move(body)) {} + response(int code, std::string body) : code(code), body(std::move(body)) {} + // clang-format on + response(returnable&& value) + { + body = value.dump(); + set_header("Content-Type", value.content_type); + } + response(returnable& value) + { + body = value.dump(); + set_header("Content-Type", value.content_type); + } + response(int code, returnable& value): + code(code) + { + body = value.dump(); + set_header("Content-Type", value.content_type); + } + response(int code, returnable&& value): + code(code), body(value.dump()) + { + set_header("Content-Type", std::move(value.content_type)); + } + + response(response&& r) + { + *this = std::move(r); + } + + response(std::string contentType, std::string body): + body(std::move(body)) + { + set_header("Content-Type", get_mime_type(contentType)); + } + + response(int code, std::string contentType, std::string body): + code(code), body(std::move(body)) + { + set_header("Content-Type", get_mime_type(contentType)); + } + + response& operator=(const response& r) = delete; + + response& operator=(response&& r) noexcept + { + body = std::move(r.body); + code = r.code; + headers = std::move(r.headers); + completed_ = r.completed_; + file_info = std::move(r.file_info); + return *this; + } + + /// Check if the response has completed (whether response.end() has been called) + bool is_completed() const noexcept + { + return completed_; + } + + void clear() + { + body.clear(); + code = 200; + headers.clear(); + completed_ = false; + file_info = static_file_info{}; + } + + /// Return a "Temporary Redirect" response. + + /// + /// Location can either be a route or a full URL. + void redirect(const std::string& location) + { + code = 307; + set_header("Location", location); + } + + /// Return a "Permanent Redirect" response. + + /// + /// Location can either be a route or a full URL. + void redirect_perm(const std::string& location) + { + code = 308; + set_header("Location", location); + } + + /// Return a "Found (Moved Temporarily)" response. + + /// + /// Location can either be a route or a full URL. + void moved(const std::string& location) + { + code = 302; + set_header("Location", location); + } + + /// Return a "Moved Permanently" response. + + /// + /// Location can either be a route or a full URL. + void moved_perm(const std::string& location) + { + code = 301; + set_header("Location", location); + } + + void write(const std::string& body_part) + { + body += body_part; + } + + /// Set the response completion flag and call the handler (to send the response). + void end() + { + if (!completed_) + { + completed_ = true; + if (skip_body) + { + set_header("Content-Length", std::to_string(body.size())); + body = ""; + manual_length_header = true; + } + if (complete_request_handler_) + { + complete_request_handler_(); + manual_length_header = false; + skip_body = false; + } + } + } + + /// Same as end() except it adds a body part right before ending. + void end(const std::string& body_part) + { + body += body_part; + end(); + } + + /// Check if the connection is still alive (usually by checking the socket status). + bool is_alive() + { + return is_alive_helper_ && is_alive_helper_(); + } + + /// Check whether the response has a static file defined. + bool is_static_type() + { + return file_info.path.size(); + } + + /// This constains metadata (coming from the `stat` command) related to any static files associated with this response. + + /// + /// Either a static file or a string body can be returned as 1 response. + struct static_file_info + { + std::string path = ""; + struct stat statbuf; + int statResult; + }; + + /// Return a static file as the response body + void set_static_file_info(std::string path) + { + utility::sanitize_filename(path); + set_static_file_info_unsafe(path); + } + + /// Return a static file as the response body without sanitizing the path (use set_static_file_info instead) + void set_static_file_info_unsafe(std::string path) + { + file_info.path = path; + file_info.statResult = stat(file_info.path.c_str(), &file_info.statbuf); +#ifdef CROW_ENABLE_COMPRESSION + compressed = false; +#endif + if (file_info.statResult == 0 && S_ISREG(file_info.statbuf.st_mode)) + { + std::size_t last_dot = path.find_last_of("."); + std::string extension = path.substr(last_dot + 1); + code = 200; + this->add_header("Content-Length", std::to_string(file_info.statbuf.st_size)); + + if (!extension.empty()) + { + this->add_header("Content-Type", get_mime_type(extension)); + } + } + else + { + code = 404; + file_info.path.clear(); + } + } + + private: + bool completed_{}; + std::function complete_request_handler_; + std::function is_alive_helper_; + static_file_info file_info; + }; +} // namespace crow + + +namespace crow +{ + + struct UTF8 + { + struct context + {}; + + void before_handle(request& /*req*/, response& /*res*/, context& /*ctx*/) + {} + + void after_handle(request& /*req*/, response& res, context& /*ctx*/) + { + if (get_header_value(res.headers, "Content-Type").empty()) + { + res.set_header("Content-Type", "text/plain; charset=utf-8"); + } + } + }; + +} // namespace crow + +#include +#include + +namespace crow +{ + // Any middleware requires following 3 members: + + // struct context; + // storing data for the middleware; can be read from another middleware or handlers + + // before_handle + // called before handling the request. + // if res.end() is called, the operation is halted. + // (still call after_handle of this middleware) + // 2 signatures: + // void before_handle(request& req, response& res, context& ctx) + // if you only need to access this middlewares context. + // template + // void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + // you can access another middlewares' context by calling `all_ctx.template get()' + // ctx == all_ctx.template get() + + // after_handle + // called after handling the request. + // void after_handle(request& req, response& res, context& ctx) + // template + // void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + + struct CookieParser + { + // Cookie stores key, value and attributes + struct Cookie + { + enum class SameSitePolicy + { + Strict, + Lax, + None + }; + + template + Cookie(const std::string& key, U&& value): + Cookie() + { + key_ = key; + value_ = std::forward(value); + } + + Cookie(const std::string& key): + Cookie(key, "") {} + + // format cookie to HTTP header format + std::string dump() const + { + const static char* HTTP_DATE_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"; + + std::stringstream ss; + ss << key_ << '='; + ss << (value_.empty() ? "\"\"" : value_); + dumpString(ss, !domain_.empty(), "Domain=", domain_); + dumpString(ss, !path_.empty(), "Path=", path_); + dumpString(ss, secure_, "Secure"); + dumpString(ss, httponly_, "HttpOnly"); + if (expires_at_) + { + ss << DIVIDER << "Expires=" + << std::put_time(expires_at_.get(), HTTP_DATE_FORMAT); + } + if (max_age_) + { + ss << DIVIDER << "Max-Age=" << *max_age_; + } + if (same_site_) + { + ss << DIVIDER << "SameSite="; + switch (*same_site_) + { + case SameSitePolicy::Strict: + ss << "Strict"; + break; + case SameSitePolicy::Lax: + ss << "Lax"; + break; + case SameSitePolicy::None: + ss << "None"; + break; + } + } + return ss.str(); + } + + const std::string& name() + { + return key_; + } + + template + Cookie& value(U&& value) + { + value_ = std::forward(value); + return *this; + } + + // Expires attribute + Cookie& expires(const std::tm& time) + { + expires_at_ = std::unique_ptr(new std::tm(time)); + return *this; + } + + // Max-Age attribute + Cookie& max_age(long long seconds) + { + max_age_ = std::unique_ptr(new long long(seconds)); + return *this; + } + + // Domain attribute + Cookie& domain(const std::string& name) + { + domain_ = name; + return *this; + } + + // Path attribute + Cookie& path(const std::string& path) + { + path_ = path; + return *this; + } + + // Secured attribute + Cookie& secure() + { + secure_ = true; + return *this; + } + + // HttpOnly attribute + Cookie& httponly() + { + httponly_ = true; + return *this; + } + + // SameSite attribute + Cookie& same_site(SameSitePolicy ssp) + { + same_site_ = std::unique_ptr(new SameSitePolicy(ssp)); + return *this; + } + + Cookie(const Cookie& c): + key_(c.key_), + value_(c.value_), + domain_(c.domain_), + path_(c.path_), + secure_(c.secure_), + httponly_(c.httponly_) + { + if (c.max_age_) + max_age_ = std::unique_ptr(new long long(*c.max_age_)); + + if (c.expires_at_) + expires_at_ = std::unique_ptr(new std::tm(*c.expires_at_)); + + if (c.same_site_) + same_site_ = std::unique_ptr(new SameSitePolicy(*c.same_site_)); + } + + private: + Cookie() = default; + + static void dumpString(std::stringstream& ss, bool cond, const char* prefix, + const std::string& value = "") + { + if (cond) + { + ss << DIVIDER << prefix << value; + } + } + + private: + std::string key_; + std::string value_; + std::unique_ptr max_age_{}; + std::string domain_ = ""; + std::string path_ = ""; + bool secure_ = false; + bool httponly_ = false; + std::unique_ptr expires_at_{}; + std::unique_ptr same_site_{}; + + static constexpr const char* DIVIDER = "; "; + }; + + + struct context + { + std::unordered_map jar; + + std::string get_cookie(const std::string& key) const + { + auto cookie = jar.find(key); + if (cookie != jar.end()) + return cookie->second; + return {}; + } + + template + Cookie& set_cookie(const std::string& key, U&& value) + { + cookies_to_add.emplace_back(key, std::forward(value)); + return cookies_to_add.back(); + } + + Cookie& set_cookie(Cookie cookie) + { + cookies_to_add.push_back(std::move(cookie)); + return cookies_to_add.back(); + } + + private: + friend struct CookieParser; + std::vector cookies_to_add; + }; + + void before_handle(request& req, response& res, context& ctx) + { + // TODO(dranikpg): remove copies, use string_view with c++17 + int count = req.headers.count("Cookie"); + if (!count) + return; + if (count > 1) + { + res.code = 400; + res.end(); + return; + } + std::string cookies = req.get_header_value("Cookie"); + size_t pos = 0; + while (pos < cookies.size()) + { + size_t pos_equal = cookies.find('=', pos); + if (pos_equal == cookies.npos) + break; + std::string name = cookies.substr(pos, pos_equal - pos); + name = utility::trim(name); + pos = pos_equal + 1; + if (pos == cookies.size()) + break; + + size_t pos_semicolon = cookies.find(';', pos); + std::string value = cookies.substr(pos, pos_semicolon - pos); + + value = utility::trim(value); + if (value[0] == '"' && value[value.size() - 1] == '"') + { + value = value.substr(1, value.size() - 2); + } + + ctx.jar.emplace(std::move(name), std::move(value)); + + pos = pos_semicolon; + if (pos == cookies.npos) + break; + pos++; + } + } + + void after_handle(request& /*req*/, response& res, context& ctx) + { + for (const auto& cookie : ctx.cookies_to_add) + { + res.add_header("Set-Cookie", cookie.dump()); + } + } + }; + + /* + App app; + A B C + A::context + int aa; + + ctx1 : public A::context + ctx2 : public ctx1, public B::context + ctx3 : public ctx2, public C::context + + C depends on A + + C::handle + context.aaa + + App::context : private CookieParser::context, ... + { + jar + + } + + SimpleApp + */ +} // namespace crow + + + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#ifdef CROW_CAN_USE_CPP17 +#include +#endif + +namespace +{ + // convert all integer values to int64_t + template + using wrap_integral_t = typename std::conditional< + std::is_integral::value && !std::is_same::value + // except for uint64_t because that could lead to overflow on conversion + && !std::is_same::value, + int64_t, T>::type; + + // convert char[]/char* to std::string + template + using wrap_char_t = typename std::conditional< + std::is_same::type, char*>::value, + std::string, T>::type; + + // Upgrade to correct type for multi_variant use + template + using wrap_mv_t = wrap_char_t>; +} // namespace + +namespace crow +{ + namespace session + { + +#ifdef CROW_CAN_USE_CPP17 + using multi_value_types = black_magic::S; + + /// A multi_value is a safe variant wrapper with json conversion support + struct multi_value + { + json::wvalue json() const + { + // clang-format off + return std::visit([](auto arg) { + return json::wvalue(arg); + }, v_); + // clang-format on + } + + static multi_value from_json(const json::rvalue&); + + std::string string() const + { + // clang-format off + return std::visit([](auto arg) { + if constexpr (std::is_same_v) + return arg; + else + return std::to_string(arg); + }, v_); + // clang-format on + } + + template> + RT get(const T& fallback) + { + if (const RT* val = std::get_if(&v_)) return *val; + return fallback; + } + + template> + void set(T val) + { + v_ = RT(std::move(val)); + } + + typename multi_value_types::rebind v_; + }; + + inline multi_value multi_value::from_json(const json::rvalue& rv) + { + using namespace json; + switch (rv.t()) + { + case type::Number: + { + if (rv.nt() == num_type::Floating_point || rv.nt() == num_type::Double_precision_floating_point) + return multi_value{rv.d()}; + else if (rv.nt() == num_type::Unsigned_integer) + return multi_value{int64_t(rv.u())}; + else + return multi_value{rv.i()}; + } + case type::False: return multi_value{false}; + case type::True: return multi_value{true}; + case type::String: return multi_value{std::string(rv)}; + default: return multi_value{false}; + } + } +#else + // Fallback for C++11/14 that uses a raw json::wvalue internally. + // This implementation consumes significantly more memory + // than the variant-based version + struct multi_value + { + json::wvalue json() const { return v_; } + + static multi_value from_json(const json::rvalue&); + + std::string string() const { return v_.dump(); } + + template> + RT get(const T& fallback) + { + return json::wvalue_reader{v_}.get((const RT&)(fallback)); + } + + template> + void set(T val) + { + v_ = RT(std::move(val)); + } + + json::wvalue v_; + }; + + inline multi_value multi_value::from_json(const json::rvalue& rv) + { + return {rv}; + } +#endif + + /// Expiration tracker keeps track of soonest-to-expire keys + struct ExpirationTracker + { + using DataPair = std::pair; + + /// Add key with time to tracker. + /// If the key is already present, it will be updated + void add(std::string key, uint64_t time) + { + auto it = times_.find(key); + if (it != times_.end()) remove(key); + times_[key] = time; + queue_.insert({time, std::move(key)}); + } + + void remove(const std::string& key) + { + auto it = times_.find(key); + if (it != times_.end()) + { + queue_.erase({it->second, key}); + times_.erase(it); + } + } + + /// Get expiration time of soonest-to-expire entry + uint64_t peek_first() const + { + if (queue_.empty()) return std::numeric_limits::max(); + return queue_.begin()->first; + } + + std::string pop_first() + { + auto it = times_.find(queue_.begin()->second); + auto key = it->first; + times_.erase(it); + queue_.erase(queue_.begin()); + return key; + } + + using iterator = typename std::set::const_iterator; + + iterator begin() const { return queue_.cbegin(); } + + iterator end() const { return queue_.cend(); } + + private: + std::set queue_; + std::unordered_map times_; + }; + + /// CachedSessions are shared across requests + struct CachedSession + { + std::string session_id; + std::string requested_session_id; // session hasn't been created yet, but a key was requested + + std::unordered_map entries; + std::unordered_set dirty; // values that were changed after last load + + void* store_data; + bool requested_refresh; + + // number of references held - used for correctly destroying the cache. + // No need to be atomic, all SessionMiddleware accesses are synchronized + int referrers; + std::recursive_mutex mutex; + }; + } // namespace session + + // SessionMiddleware allows storing securely and easily small snippets of user information + template + struct SessionMiddleware + { +#ifdef CROW_CAN_USE_CPP17 + using lock = std::scoped_lock; + using rc_lock = std::scoped_lock; +#else + using lock = std::lock_guard; + using rc_lock = std::lock_guard; +#endif + + struct context + { + // Get a mutex for locking this session + std::recursive_mutex& mutex() + { + check_node(); + return node->mutex; + } + + // Check whether this session is already present + bool exists() { return bool(node); } + + // Get a value by key or fallback if it doesn't exist or is of another type + template + auto get(const std::string& key, const F& fallback = F()) + // This trick lets the multi_value deduce the return type from the fallback + // which allows both: + // context.get("key") + // context.get("key", "") -> char[] is transformed into string by multivalue + // to return a string + -> decltype(std::declval().get(std::declval())) + { + if (!node) return fallback; + rc_lock l(node->mutex); + + auto it = node->entries.find(key); + if (it != node->entries.end()) return it->second.get(fallback); + return fallback; + } + + // Set a value by key + template + void set(const std::string& key, T value) + { + check_node(); + rc_lock l(node->mutex); + + node->dirty.insert(key); + node->entries[key].set(std::move(value)); + } + + bool contains(const std::string& key) + { + if (!node) return false; + return node->entries.find(key) != node->entries.end(); + } + + // Atomically mutate a value with a function + template + void apply(const std::string& key, const Func& f) + { + using traits = utility::function_traits; + using arg = typename std::decay>::type; + using retv = typename std::decay::type; + check_node(); + rc_lock l(node->mutex); + node->dirty.insert(key); + node->entries[key].set(f(node->entries[key].get(arg{}))); + } + + // Remove a value from the session + void remove(const std::string& key) + { + if (!node) return; + rc_lock l(node->mutex); + node->dirty.insert(key); + node->entries.erase(key); + } + + // Format value by key as a string + std::string string(const std::string& key) + { + if (!node) return ""; + rc_lock l(node->mutex); + + auto it = node->entries.find(key); + if (it != node->entries.end()) return it->second.string(); + return ""; + } + + // Get a list of keys present in session + std::vector keys() + { + if (!node) return {}; + rc_lock l(node->mutex); + + std::vector out; + for (const auto& p : node->entries) + out.push_back(p.first); + return out; + } + + // Delay expiration by issuing another cookie with an updated expiration time + // and notifying the store + void refresh_expiration() + { + if (!node) return; + node->requested_refresh = true; + } + + private: + friend struct SessionMiddleware; + + void check_node() + { + if (!node) node = std::make_shared(); + } + + std::shared_ptr node; + }; + + template + SessionMiddleware( + CookieParser::Cookie cookie, + int id_length, + Ts... ts): + id_length_(id_length), + cookie_(cookie), + store_(std::forward(ts)...), mutex_(new std::mutex{}) + {} + + template + SessionMiddleware(Ts... ts): + SessionMiddleware( + CookieParser::Cookie("session").path("/").max_age(/*month*/ 30 * 24 * 60 * 60), + /*id_length */ 20, // around 10^34 possible combinations, but small enough to fit into SSO + std::forward(ts)...) + {} + + template + void before_handle(request& /*req*/, response& /*res*/, context& ctx, AllContext& all_ctx) + { + lock l(*mutex_); + + auto& cookies = all_ctx.template get(); + auto session_id = load_id(cookies); + if (session_id == "") return; + + // search entry in cache + auto it = cache_.find(session_id); + if (it != cache_.end()) + { + it->second->referrers++; + ctx.node = it->second; + return; + } + + // check this is a valid entry before loading + if (!store_.contains(session_id)) return; + + auto node = std::make_shared(); + node->session_id = session_id; + node->referrers = 1; + + try + { + store_.load(*node); + } + catch (...) + { + CROW_LOG_ERROR << "Exception occurred during session load"; + return; + } + + ctx.node = node; + cache_[session_id] = node; + } + + template + void after_handle(request& /*req*/, response& /*res*/, context& ctx, AllContext& all_ctx) + { + lock l(*mutex_); + if (!ctx.node || --ctx.node->referrers > 0) return; + ctx.node->requested_refresh |= ctx.node->session_id == ""; + + // generate new id + if (ctx.node->session_id == "") + { + // check for requested id + ctx.node->session_id = std::move(ctx.node->requested_session_id); + if (ctx.node->session_id == "") + { + ctx.node->session_id = utility::random_alphanum(id_length_); + } + } + else + { + cache_.erase(ctx.node->session_id); + } + + if (ctx.node->requested_refresh) + { + auto& cookies = all_ctx.template get(); + store_id(cookies, ctx.node->session_id); + } + + try + { + store_.save(*ctx.node); + } + catch (...) + { + CROW_LOG_ERROR << "Exception occurred during session save"; + return; + } + } + + private: + std::string next_id() + { + std::string id; + do + { + id = utility::random_alphanum(id_length_); + } while (store_.contains(id)); + return id; + } + + std::string load_id(const CookieParser::context& cookies) + { + return cookies.get_cookie(cookie_.name()); + } + + void store_id(CookieParser::context& cookies, const std::string& session_id) + { + cookie_.value(session_id); + cookies.set_cookie(cookie_); + } + + private: + int id_length_; + + // prototype for cookie + CookieParser::Cookie cookie_; + + Store store_; + + // mutexes are immovable + std::unique_ptr mutex_; + std::unordered_map> cache_; + }; + + /// InMemoryStore stores all entries in memory + struct InMemoryStore + { + // Load a value into the session cache. + // A load is always followed by a save, no loads happen consecutively + void load(session::CachedSession& cn) + { + // load & stores happen sequentially, so moving is safe + cn.entries = std::move(entries[cn.session_id]); + } + + // Persist session data + void save(session::CachedSession& cn) + { + entries[cn.session_id] = std::move(cn.entries); + // cn.dirty is a list of changed keys since the last load + } + + bool contains(const std::string& key) + { + return entries.count(key) > 0; + } + + std::unordered_map> entries; + }; + + // FileStore stores all data as json files in a folder. + // Files are deleted after expiration. Expiration refreshes are automatically picked up. + struct FileStore + { + FileStore(const std::string& folder, uint64_t expiration_seconds = /*month*/ 30 * 24 * 60 * 60): + path_(folder), expiration_seconds_(expiration_seconds) + { + std::ifstream ifs(get_filename(".expirations", false)); + + auto current_ts = chrono_time(); + std::string key; + uint64_t time; + while (ifs >> key >> time) + { + if (current_ts > time) + { + evict(key); + } + else if (contains(key)) + { + expirations_.add(key, time); + } + } + } + + ~FileStore() + { + std::ofstream ofs(get_filename(".expirations", false), std::ios::trunc); + for (const auto& p : expirations_) + ofs << p.second << " " << p.first << "\n"; + } + + // Delete expired entries + // At most 3 to prevent freezes + void handle_expired() + { + int deleted = 0; + auto current_ts = chrono_time(); + while (current_ts > expirations_.peek_first() && deleted < 3) + { + evict(expirations_.pop_first()); + deleted++; + } + } + + void load(session::CachedSession& cn) + { + handle_expired(); + + std::ifstream file(get_filename(cn.session_id)); + + std::stringstream buffer; + buffer << file.rdbuf() << std::endl; + + for (const auto& p : json::load(buffer.str())) + cn.entries[p.key()] = session::multi_value::from_json(p); + } + + void save(session::CachedSession& cn) + { + if (cn.requested_refresh) + expirations_.add(cn.session_id, chrono_time() + expiration_seconds_); + if (cn.dirty.empty()) return; + + std::ofstream file(get_filename(cn.session_id)); + json::wvalue jw; + for (const auto& p : cn.entries) + jw[p.first] = p.second.json(); + file << jw.dump() << std::flush; + } + + std::string get_filename(const std::string& key, bool suffix = true) + { + return utility::join_path(path_, key + (suffix ? ".json" : "")); + } + + bool contains(const std::string& key) + { + std::ifstream file(get_filename(key)); + return file.good(); + } + + void evict(const std::string& key) + { + std::remove(get_filename(key).c_str()); + } + + uint64_t chrono_time() const + { + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + } + + std::string path_; + uint64_t expiration_seconds_; + session::ExpirationTracker expirations_; + }; + +} // namespace crow + + + +#include +#include +#include +#include + +namespace crow // NOTE: Already documented in "crow/app.h" +{ + + /// Local middleware should extend ILocalMiddleware + struct ILocalMiddleware + { + using call_global = std::false_type; + }; + + namespace detail + { + template + struct check_before_handle_arity_3_const + { + template + struct get + {}; + }; + + template + struct check_before_handle_arity_3 + { + template + struct get + {}; + }; + + template + struct check_after_handle_arity_3_const + { + template + struct get + {}; + }; + + template + struct check_after_handle_arity_3 + { + template + struct get + {}; + }; + + template + struct check_global_call_false + { + template::type = true> + struct get + {}; + }; + + template + struct is_before_handle_arity_3_impl + { + template + static std::true_type f(typename check_before_handle_arity_3_const::template get*); + + template + static std::true_type f(typename check_before_handle_arity_3::template get*); + + template + static std::false_type f(...); + + public: + static const bool value = decltype(f(nullptr))::value; + }; + + template + struct is_after_handle_arity_3_impl + { + template + static std::true_type f(typename check_after_handle_arity_3_const::template get*); + + template + static std::true_type f(typename check_after_handle_arity_3::template get*); + + template + static std::false_type f(...); + + public: + static constexpr bool value = decltype(f(nullptr))::value; + }; + + template + struct is_middleware_global + { + template + static std::false_type f(typename check_global_call_false::template get*); + + template + static std::true_type f(...); + + static const bool value = decltype(f(nullptr))::value; + }; + + template + typename std::enable_if::value>::type + before_handler_call(MW& mw, request& req, response& res, Context& ctx, ParentContext& /*parent_ctx*/) + { + mw.before_handle(req, res, ctx.template get(), ctx); + } + + template + typename std::enable_if::value>::type + before_handler_call(MW& mw, request& req, response& res, Context& ctx, ParentContext& /*parent_ctx*/) + { + mw.before_handle(req, res, ctx.template get()); + } + + template + typename std::enable_if::value>::type + after_handler_call(MW& mw, request& req, response& res, Context& ctx, ParentContext& /*parent_ctx*/) + { + mw.after_handle(req, res, ctx.template get(), ctx); + } + + template + typename std::enable_if::value>::type + after_handler_call(MW& mw, request& req, response& res, Context& ctx, ParentContext& /*parent_ctx*/) + { + mw.after_handle(req, res, ctx.template get()); + } + + + template + typename std::enable_if<(N < std::tuple_size::type>::value), bool>::type + middleware_call_helper(const CallCriteria& cc, Container& middlewares, request& req, response& res, Context& ctx) + { + + using CurrentMW = typename std::tuple_element::type>::type; + + if (!cc.template enabled(N)) + { + return middleware_call_helper(cc, middlewares, req, res, ctx); + } + + using parent_context_t = typename Context::template partial; + before_handler_call(std::get(middlewares), req, res, ctx, static_cast(ctx)); + if (res.is_completed()) + { + after_handler_call(std::get(middlewares), req, res, ctx, static_cast(ctx)); + return true; + } + + if (middleware_call_helper(cc, middlewares, req, res, ctx)) + { + after_handler_call(std::get(middlewares), req, res, ctx, static_cast(ctx)); + return true; + } + + return false; + } + + template + typename std::enable_if<(N >= std::tuple_size::type>::value), bool>::type + middleware_call_helper(const CallCriteria& /*cc*/, Container& /*middlewares*/, request& /*req*/, response& /*res*/, Context& /*ctx*/) + { + return false; + } + + template + typename std::enable_if<(N < 0)>::type + after_handlers_call_helper(const CallCriteria& /*cc*/, Container& /*middlewares*/, Context& /*context*/, request& /*req*/, response& /*res*/) + { + } + + template + typename std::enable_if<(N == 0)>::type after_handlers_call_helper(const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res) + { + using parent_context_t = typename Context::template partial; + using CurrentMW = typename std::tuple_element::type>::type; + if (cc.template enabled(N)) + { + after_handler_call(std::get(middlewares), req, res, ctx, static_cast(ctx)); + } + } + + template + typename std::enable_if<(N > 0)>::type after_handlers_call_helper(const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res) + { + using parent_context_t = typename Context::template partial; + using CurrentMW = typename std::tuple_element::type>::type; + if (cc.template enabled(N)) + { + after_handler_call(std::get(middlewares), req, res, ctx, static_cast(ctx)); + } + after_handlers_call_helper(cc, middlewares, ctx, req, res); + } + + // A CallCriteria that accepts only global middleware + struct middleware_call_criteria_only_global + { + template + constexpr bool enabled(int) const + { + return is_middleware_global::value; + } + }; + + template + typename std::enable_if>::value, void>::type + wrapped_handler_call(crow::request& /*req*/, crow::response& res, const F& f, Args&&... args) + { + static_assert(!std::is_same()...))>::value, + "Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable"); + + res = crow::response(f(std::forward(args)...)); + res.end(); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && + black_magic::CallHelper>::value, + void>::type + wrapped_handler_call(crow::request& req, crow::response& res, const F& f, Args&&... args) + { + static_assert(!std::is_same(), std::declval()...))>::value, + "Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable"); + + res = crow::response(f(req, std::forward(args)...)); + res.end(); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + black_magic::CallHelper>::value, + void>::type + wrapped_handler_call(crow::request& /*req*/, crow::response& res, const F& f, Args&&... args) + { + static_assert(std::is_same(), std::declval()...))>::value, + "Handler function with response argument should have void return type"); + + f(res, std::forward(args)...); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + black_magic::CallHelper>::value, + void>::type + wrapped_handler_call(crow::request& req, crow::response& res, const F& f, Args&&... args) + { + static_assert(std::is_same(), std::declval(), std::declval()...))>::value, + "Handler function with response argument should have void return type"); + + f(req, res, std::forward(args)...); + } + + // wrapped_handler_call transparently wraps a handler call behind (req, res, args...) + template + typename std::enable_if< + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value, + void>::type + wrapped_handler_call(crow::request& req, crow::response& res, const F& f, Args&&... args) + { + static_assert(std::is_same(), std::declval(), std::declval()...))>::value, + "Handler function with response argument should have void return type"); + + f(req, res, std::forward(args)...); + } + + template + struct middleware_call_criteria_dynamic + {}; + + template<> + struct middleware_call_criteria_dynamic + { + middleware_call_criteria_dynamic(const std::vector& indices): + indices(indices), slider(0) {} + + template + bool enabled(int mw_index) const + { + if (slider < int(indices.size()) && indices[slider] == mw_index) + { + slider++; + return true; + } + return false; + } + + private: + const std::vector& indices; + mutable int slider; + }; + + template<> + struct middleware_call_criteria_dynamic + { + middleware_call_criteria_dynamic(const std::vector& indices): + indices(indices), slider(int(indices.size()) - 1) {} + + template + bool enabled(int mw_index) const + { + if (slider >= 0 && indices[slider] == mw_index) + { + slider--; + return true; + } + return false; + } + + private: + const std::vector& indices; + mutable int slider; + }; + + } // namespace detail +} // namespace crow + + + +namespace crow +{ + namespace detail + { + + + template + struct partial_context : public black_magic::pop_back::template rebind, public black_magic::last_element_type::type::context + { + using parent_context = typename black_magic::pop_back::template rebind<::crow::detail::partial_context>; + template + using partial = typename std::conditional>::type; + + template + typename T::context& get() + { + return static_cast(*this); + } + }; + + + + template<> + struct partial_context<> + { + template + using partial = partial_context; + }; + + + template + struct context : private partial_context + //struct context : private Middlewares::context... // simple but less type-safe + { + template + friend typename std::enable_if<(N == 0)>::type after_handlers_call_helper(const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res); + template + friend typename std::enable_if<(N > 0)>::type after_handlers_call_helper(const CallCriteria& cc, Container& middlewares, Context& ctx, request& req, response& res); + + template + friend typename std::enable_if<(N < std::tuple_size::type>::value), bool>::type + middleware_call_helper(const CallCriteria& cc, Container& middlewares, request& req, response& res, Context& ctx); + + template + typename T::context& get() + { + return static_cast(*this); + } + + template + using partial = typename partial_context::template partial; + }; + } // namespace detail +} // namespace crow + + +#ifdef CROW_USE_BOOST +#include +#include +#else +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include +#include +#endif + +#include +#include +#include +#include + + +namespace crow +{ +#ifdef CROW_USE_BOOST + namespace asio = boost::asio; + using error_code = boost::system::error_code; +#else + using error_code = asio::error_code; +#endif + namespace detail + { + + /// A class for scheduling functions to be called after a specific amount of ticks. A tick is equal to 1 second. + class task_timer + { + public: + using task_type = std::function; + using identifier_type = size_t; + + private: + using clock_type = std::chrono::steady_clock; + using time_type = clock_type::time_point; + + public: + task_timer(asio::io_service& io_service): + io_service_(io_service), timer_(io_service_) + { + timer_.expires_after(std::chrono::seconds(1)); + timer_.async_wait( + std::bind(&task_timer::tick_handler, this, std::placeholders::_1)); + } + + ~task_timer() { timer_.cancel(); } + + void cancel(identifier_type id) + { + tasks_.erase(id); + CROW_LOG_DEBUG << "task_timer cancelled: " << this << ' ' << id; + } + + /// Schedule the given task to be executed after the default amount of ticks. + + /// + /// \return identifier_type Used to cancel the thread. + /// It is not bound to this task_timer instance and in some cases could lead to + /// undefined behavior if used with other task_timer objects or after the task + /// has been successfully executed. + identifier_type schedule(const task_type& task) + { + tasks_.insert( + {++highest_id_, + {clock_type::now() + std::chrono::seconds(get_default_timeout()), + task}}); + CROW_LOG_DEBUG << "task_timer scheduled: " << this << ' ' << highest_id_; + return highest_id_; + } + + /// Schedule the given task to be executed after the given time. + + /// + /// \param timeout The amount of ticks (seconds) to wait before execution. + /// + /// \return identifier_type Used to cancel the thread. + /// It is not bound to this task_timer instance and in some cases could lead to + /// undefined behavior if used with other task_timer objects or after the task + /// has been successfully executed. + identifier_type schedule(const task_type& task, std::uint8_t timeout) + { + tasks_.insert({++highest_id_, + {clock_type::now() + std::chrono::seconds(timeout), task}}); + CROW_LOG_DEBUG << "task_timer scheduled: " << this << ' ' << highest_id_; + return highest_id_; + } + + /// Set the default timeout for this task_timer instance. (Default: 5) + + /// + /// \param timeout The amount of ticks (seconds) to wait before execution. + void set_default_timeout(std::uint8_t timeout) { default_timeout_ = timeout; } + + /// Get the default timeout. (Default: 5) + std::uint8_t get_default_timeout() const { return default_timeout_; } + + private: + void process_tasks() + { + time_type current_time = clock_type::now(); + std::vector finished_tasks; + + for (const auto& task : tasks_) + { + if (task.second.first < current_time) + { + (task.second.second)(); + finished_tasks.push_back(task.first); + CROW_LOG_DEBUG << "task_timer called: " << this << ' ' << task.first; + } + } + + for (const auto& task : finished_tasks) + tasks_.erase(task); + + // If no task is currently scheduled, reset the issued ids back to 0. + if (tasks_.empty()) highest_id_ = 0; + } + + void tick_handler(const error_code& ec) + { + if (ec) return; + + process_tasks(); + + timer_.expires_after(std::chrono::seconds(1)); + timer_.async_wait( + std::bind(&task_timer::tick_handler, this, std::placeholders::_1)); + } + + private: + std::uint8_t default_timeout_{5}; + asio::io_service& io_service_; + asio::basic_waitable_timer timer_; + std::map> tasks_; + + // A continuosly increasing number to be issued to threads to identify them. + // If no tasks are scheduled, it will be reset to 0. + identifier_type highest_id_{0}; + }; + } // namespace detail +} // namespace crow + + +#ifdef CROW_USE_BOOST +#include +#else +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include +#endif + +#include +#include +#include +#include +#include + + +namespace crow +{ +#ifdef CROW_USE_BOOST + namespace asio = boost::asio; + using error_code = boost::system::error_code; +#else + using error_code = asio::error_code; +#endif + using tcp = asio::ip::tcp; + +#ifdef CROW_ENABLE_DEBUG + static std::atomic connectionCount; +#endif + + /// An HTTP connection. + template + class Connection: public std::enable_shared_from_this> + { + friend struct crow::response; + + public: + Connection( + asio::io_service& io_service, + Handler* handler, + const std::string& server_name, + std::tuple* middlewares, + std::function& get_cached_date_str_f, + detail::task_timer& task_timer, + typename Adaptor::context* adaptor_ctx_, + std::atomic& queue_length): + adaptor_(io_service, adaptor_ctx_), + handler_(handler), + parser_(this), + req_(parser_.req), + server_name_(server_name), + middlewares_(middlewares), + get_cached_date_str(get_cached_date_str_f), + task_timer_(task_timer), + res_stream_threshold_(handler->stream_threshold()), + queue_length_(queue_length) + { +#ifdef CROW_ENABLE_DEBUG + connectionCount++; + CROW_LOG_DEBUG << "Connection (" << this << ") allocated, total: " << connectionCount; +#endif + } + + ~Connection() + { +#ifdef CROW_ENABLE_DEBUG + connectionCount--; + CROW_LOG_DEBUG << "Connection (" << this << ") freed, total: " << connectionCount; +#endif + } + + /// The TCP socket on top of which the connection is established. + decltype(std::declval().raw_socket())& socket() + { + return adaptor_.raw_socket(); + } + + void start() + { + auto self = this->shared_from_this(); + adaptor_.start([self](const error_code& ec) { + if (!ec) + { + self->start_deadline(); + self->parser_.clear(); + + self->do_read(); + } + else + { + CROW_LOG_ERROR << "Could not start adaptor: " << ec.message(); + } + }); + } + + void handle_url() + { + routing_handle_result_ = handler_->handle_initial(req_, res); + // if no route is found for the request method, return the response without parsing or processing anything further. + if (!routing_handle_result_->rule_index) + { + parser_.done(); + need_to_call_after_handlers_ = true; + complete_request(); + } + } + + void handle_header() + { + // HTTP 1.1 Expect: 100-continue + if (req_.http_ver_major == 1 && req_.http_ver_minor == 1 && get_header_value(req_.headers, "expect") == "100-continue") + { + continue_requested = true; + buffers_.clear(); + static std::string expect_100_continue = "HTTP/1.1 100 Continue\r\n\r\n"; + buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size()); + do_write(); + } + } + + void handle() + { + // TODO(EDev): cancel_deadline_timer should be looked into, it might be a good idea to add it to handle_url() and then restart the timer once everything passes + cancel_deadline_timer(); + bool is_invalid_request = false; + add_keep_alive_ = false; + + // Create context + ctx_ = detail::context(); + req_.middleware_context = static_cast(&ctx_); + req_.middleware_container = static_cast(middlewares_); + req_.io_service = &adaptor_.get_io_service(); + + req_.remote_ip_address = adaptor_.remote_endpoint().address().to_string(); + + add_keep_alive_ = req_.keep_alive; + close_connection_ = req_.close_connection; + + if (req_.check_version(1, 1)) // HTTP/1.1 + { + if (!req_.headers.count("host")) + { + is_invalid_request = true; + res = response(400); + } + else if (req_.upgrade) + { + // h2 or h2c headers + if (req_.get_header_value("upgrade").substr(0, 2) == "h2") + { + // TODO(ipkn): HTTP/2 + // currently, ignore upgrade header + } + else + { + + detail::middleware_call_helper({}, *middlewares_, req_, res, ctx_); + close_connection_ = true; + handler_->handle_upgrade(req_, res, std::move(adaptor_)); + return; + } + } + } + + CROW_LOG_INFO << "Request: " << utility::lexical_cast(adaptor_.remote_endpoint()) << " " << this << " HTTP/" << (char)(req_.http_ver_major + '0') << "." << (char)(req_.http_ver_minor + '0') << ' ' << method_name(req_.method) << " " << req_.url; + + + need_to_call_after_handlers_ = false; + if (!is_invalid_request) + { + res.complete_request_handler_ = nullptr; + auto self = this->shared_from_this(); + res.is_alive_helper_ = [self]() -> bool { + return self->adaptor_.is_open(); + }; + + detail::middleware_call_helper({}, *middlewares_, req_, res, ctx_); + + if (!res.completed_) + { + auto self = this->shared_from_this(); + res.complete_request_handler_ = [self] { + self->complete_request(); + }; + need_to_call_after_handlers_ = true; + handler_->handle(req_, res, routing_handle_result_); + if (add_keep_alive_) + res.set_header("connection", "Keep-Alive"); + } + else + { + complete_request(); + } + } + else + { + complete_request(); + } + } + + /// Call the after handle middleware and send the write the response to the connection. + void complete_request() + { + CROW_LOG_INFO << "Response: " << this << ' ' << req_.raw_url << ' ' << res.code << ' ' << close_connection_; + res.is_alive_helper_ = nullptr; + + if (need_to_call_after_handlers_) + { + need_to_call_after_handlers_ = false; + + // call all after_handler of middlewares + detail::after_handlers_call_helper< + detail::middleware_call_criteria_only_global, + (static_cast(sizeof...(Middlewares)) - 1), + decltype(ctx_), + decltype(*middlewares_)>({}, *middlewares_, ctx_, req_, res); + } +#ifdef CROW_ENABLE_COMPRESSION + if (handler_->compression_used()) + { + std::string accept_encoding = req_.get_header_value("Accept-Encoding"); + if (!accept_encoding.empty() && res.compressed) + { + switch (handler_->compression_algorithm()) + { + case compression::DEFLATE: + if (accept_encoding.find("deflate") != std::string::npos) + { + res.body = compression::compress_string(res.body, compression::algorithm::DEFLATE); + res.set_header("Content-Encoding", "deflate"); + } + break; + case compression::GZIP: + if (accept_encoding.find("gzip") != std::string::npos) + { + res.body = compression::compress_string(res.body, compression::algorithm::GZIP); + res.set_header("Content-Encoding", "gzip"); + } + break; + default: + break; + } + } + } +#endif + //if there is a redirection with a partial URL, treat the URL as a route. + std::string location = res.get_header_value("Location"); + if (!location.empty() && location.find("://", 0) == std::string::npos) + { +#ifdef CROW_ENABLE_SSL + if (handler_->ssl_used()) + location.insert(0, "https://" + req_.get_header_value("Host")); + else +#endif + location.insert(0, "http://" + req_.get_header_value("Host")); + res.set_header("location", location); + } + + prepare_buffers(); + + if (res.is_static_type()) + { + do_write_static(); + } + else + { + do_write_general(); + } + } + + private: + void prepare_buffers() + { + res.complete_request_handler_ = nullptr; + res.is_alive_helper_ = nullptr; + + if (!adaptor_.is_open()) + { + //CROW_LOG_DEBUG << this << " delete (socket is closed) " << is_reading << ' ' << is_writing; + //delete this; + return; + } + // TODO(EDev): HTTP version in status codes should be dynamic + // Keep in sync with common.h/status + static std::unordered_map statusCodes = { + {status::CONTINUE, "HTTP/1.1 100 Continue\r\n"}, + {status::SWITCHING_PROTOCOLS, "HTTP/1.1 101 Switching Protocols\r\n"}, + + {status::OK, "HTTP/1.1 200 OK\r\n"}, + {status::CREATED, "HTTP/1.1 201 Created\r\n"}, + {status::ACCEPTED, "HTTP/1.1 202 Accepted\r\n"}, + {status::NON_AUTHORITATIVE_INFORMATION, "HTTP/1.1 203 Non-Authoritative Information\r\n"}, + {status::NO_CONTENT, "HTTP/1.1 204 No Content\r\n"}, + {status::RESET_CONTENT, "HTTP/1.1 205 Reset Content\r\n"}, + {status::PARTIAL_CONTENT, "HTTP/1.1 206 Partial Content\r\n"}, + + {status::MULTIPLE_CHOICES, "HTTP/1.1 300 Multiple Choices\r\n"}, + {status::MOVED_PERMANENTLY, "HTTP/1.1 301 Moved Permanently\r\n"}, + {status::FOUND, "HTTP/1.1 302 Found\r\n"}, + {status::SEE_OTHER, "HTTP/1.1 303 See Other\r\n"}, + {status::NOT_MODIFIED, "HTTP/1.1 304 Not Modified\r\n"}, + {status::TEMPORARY_REDIRECT, "HTTP/1.1 307 Temporary Redirect\r\n"}, + {status::PERMANENT_REDIRECT, "HTTP/1.1 308 Permanent Redirect\r\n"}, + + {status::BAD_REQUEST, "HTTP/1.1 400 Bad Request\r\n"}, + {status::UNAUTHORIZED, "HTTP/1.1 401 Unauthorized\r\n"}, + {status::FORBIDDEN, "HTTP/1.1 403 Forbidden\r\n"}, + {status::NOT_FOUND, "HTTP/1.1 404 Not Found\r\n"}, + {status::METHOD_NOT_ALLOWED, "HTTP/1.1 405 Method Not Allowed\r\n"}, + {status::NOT_ACCEPTABLE, "HTTP/1.1 406 Not Acceptable\r\n"}, + {status::PROXY_AUTHENTICATION_REQUIRED, "HTTP/1.1 407 Proxy Authentication Required\r\n"}, + {status::CONFLICT, "HTTP/1.1 409 Conflict\r\n"}, + {status::GONE, "HTTP/1.1 410 Gone\r\n"}, + {status::PAYLOAD_TOO_LARGE, "HTTP/1.1 413 Payload Too Large\r\n"}, + {status::UNSUPPORTED_MEDIA_TYPE, "HTTP/1.1 415 Unsupported Media Type\r\n"}, + {status::RANGE_NOT_SATISFIABLE, "HTTP/1.1 416 Range Not Satisfiable\r\n"}, + {status::EXPECTATION_FAILED, "HTTP/1.1 417 Expectation Failed\r\n"}, + {status::PRECONDITION_REQUIRED, "HTTP/1.1 428 Precondition Required\r\n"}, + {status::TOO_MANY_REQUESTS, "HTTP/1.1 429 Too Many Requests\r\n"}, + {status::UNAVAILABLE_FOR_LEGAL_REASONS, "HTTP/1.1 451 Unavailable For Legal Reasons\r\n"}, + + {status::INTERNAL_SERVER_ERROR, "HTTP/1.1 500 Internal Server Error\r\n"}, + {status::NOT_IMPLEMENTED, "HTTP/1.1 501 Not Implemented\r\n"}, + {status::BAD_GATEWAY, "HTTP/1.1 502 Bad Gateway\r\n"}, + {status::SERVICE_UNAVAILABLE, "HTTP/1.1 503 Service Unavailable\r\n"}, + {status::GATEWAY_TIMEOUT, "HTTP/1.1 504 Gateway Timeout\r\n"}, + {status::VARIANT_ALSO_NEGOTIATES, "HTTP/1.1 506 Variant Also Negotiates\r\n"}, + }; + + static const std::string seperator = ": "; + + buffers_.clear(); + buffers_.reserve(4 * (res.headers.size() + 5) + 3); + + if (!statusCodes.count(res.code)) + { + CROW_LOG_WARNING << this << " status code " + << "(" << res.code << ")" + << " not defined, returning 500 instead"; + res.code = 500; + } + + auto& status = statusCodes.find(res.code)->second; + buffers_.emplace_back(status.data(), status.size()); + + if (res.code >= 400 && res.body.empty()) + res.body = statusCodes[res.code].substr(9); + + for (auto& kv : res.headers) + { + buffers_.emplace_back(kv.first.data(), kv.first.size()); + buffers_.emplace_back(seperator.data(), seperator.size()); + buffers_.emplace_back(kv.second.data(), kv.second.size()); + buffers_.emplace_back(crlf.data(), crlf.size()); + } + + if (!res.manual_length_header && !res.headers.count("content-length")) + { + content_length_ = std::to_string(res.body.size()); + static std::string content_length_tag = "Content-Length: "; + buffers_.emplace_back(content_length_tag.data(), content_length_tag.size()); + buffers_.emplace_back(content_length_.data(), content_length_.size()); + buffers_.emplace_back(crlf.data(), crlf.size()); + } + if (!res.headers.count("server")) + { + static std::string server_tag = "Server: "; + buffers_.emplace_back(server_tag.data(), server_tag.size()); + buffers_.emplace_back(server_name_.data(), server_name_.size()); + buffers_.emplace_back(crlf.data(), crlf.size()); + } + if (!res.headers.count("date")) + { + static std::string date_tag = "Date: "; + date_str_ = get_cached_date_str(); + buffers_.emplace_back(date_tag.data(), date_tag.size()); + buffers_.emplace_back(date_str_.data(), date_str_.size()); + buffers_.emplace_back(crlf.data(), crlf.size()); + } + if (add_keep_alive_) + { + static std::string keep_alive_tag = "Connection: Keep-Alive"; + buffers_.emplace_back(keep_alive_tag.data(), keep_alive_tag.size()); + buffers_.emplace_back(crlf.data(), crlf.size()); + } + + buffers_.emplace_back(crlf.data(), crlf.size()); + } + + void do_write_static() + { + asio::write(adaptor_.socket(), buffers_); + + if (res.file_info.statResult == 0) + { + std::ifstream is(res.file_info.path.c_str(), std::ios::in | std::ios::binary); + std::vector buffers{1}; + char buf[16384]; + is.read(buf, sizeof(buf)); + while (is.gcount() > 0) + { + buffers[0] = asio::buffer(buf, is.gcount()); + do_write_sync(buffers); + is.read(buf, sizeof(buf)); + } + } + if (close_connection_) + { + adaptor_.shutdown_readwrite(); + adaptor_.close(); + CROW_LOG_DEBUG << this << " from write (static)"; + } + + res.end(); + res.clear(); + buffers_.clear(); + parser_.clear(); + } + + void do_write_general() + { + if (res.body.length() < res_stream_threshold_) + { + res_body_copy_.swap(res.body); + buffers_.emplace_back(res_body_copy_.data(), res_body_copy_.size()); + + do_write(); + + if (need_to_start_read_after_complete_) + { + need_to_start_read_after_complete_ = false; + start_deadline(); + do_read(); + } + } + else + { + asio::write(adaptor_.socket(), buffers_); // Write the response start / headers + cancel_deadline_timer(); + if (res.body.length() > 0) + { + std::vector buffers{1}; + const uint8_t *data = reinterpret_cast(res.body.data()); + size_t length = res.body.length(); + for(size_t transferred = 0; transferred < length;) + { + size_t to_transfer = CROW_MIN(16384UL, length-transferred); + buffers[0] = asio::const_buffer(data+transferred, to_transfer); + do_write_sync(buffers); + transferred += to_transfer; + } + } + if (close_connection_) + { + adaptor_.shutdown_readwrite(); + adaptor_.close(); + CROW_LOG_DEBUG << this << " from write (res_stream)"; + } + + res.end(); + res.clear(); + buffers_.clear(); + parser_.clear(); + } + } + + void do_read() + { + auto self = this->shared_from_this(); + adaptor_.socket().async_read_some( + asio::buffer(buffer_), + [self](const error_code& ec, std::size_t bytes_transferred) { + bool error_while_reading = true; + if (!ec) + { + bool ret = self->parser_.feed(self->buffer_.data(), bytes_transferred); + if (ret && self->adaptor_.is_open()) + { + error_while_reading = false; + } + } + + if (error_while_reading) + { + self->cancel_deadline_timer(); + self->parser_.done(); + self->adaptor_.shutdown_read(); + self->adaptor_.close(); + CROW_LOG_DEBUG << self << " from read(1) with description: \"" << http_errno_description(static_cast(self->parser_.http_errno)) << '\"'; + } + else if (self->close_connection_) + { + self->cancel_deadline_timer(); + self->parser_.done(); + // adaptor will close after write + } + else if (!self->need_to_call_after_handlers_) + { + self->start_deadline(); + self->do_read(); + } + else + { + // res will be completed later by user + self->need_to_start_read_after_complete_ = true; + } + }); + } + + void do_write() + { + auto self = this->shared_from_this(); + asio::async_write( + adaptor_.socket(), buffers_, + [self](const error_code& ec, std::size_t /*bytes_transferred*/) { + self->res.clear(); + self->res_body_copy_.clear(); + if (!self->continue_requested) + { + self->parser_.clear(); + } + else + { + self->continue_requested = false; + } + + if (!ec) + { + if (self->close_connection_) + { + self->adaptor_.shutdown_write(); + self->adaptor_.close(); + CROW_LOG_DEBUG << self << " from write(1)"; + } + } + else + { + CROW_LOG_DEBUG << self << " from write(2)"; + } + }); + } + + inline void do_write_sync(std::vector& buffers) + { + + asio::write(adaptor_.socket(), buffers, [&](error_code ec, std::size_t) { + if (!ec) + { + return false; + } + else + { + CROW_LOG_ERROR << ec << " - happened while sending buffers"; + CROW_LOG_DEBUG << this << " from write (sync)(2)"; + return true; + } + }); + } + + void cancel_deadline_timer() + { + CROW_LOG_DEBUG << this << " timer cancelled: " << &task_timer_ << ' ' << task_id_; + task_timer_.cancel(task_id_); + } + + void start_deadline(/*int timeout = 5*/) + { + cancel_deadline_timer(); + + auto self = this->shared_from_this(); + task_id_ = task_timer_.schedule([self] { + if (!self->adaptor_.is_open()) + { + return; + } + self->adaptor_.shutdown_readwrite(); + self->adaptor_.close(); + }); + CROW_LOG_DEBUG << this << " timer added: " << &task_timer_ << ' ' << task_id_; + } + + private: + Adaptor adaptor_; + Handler* handler_; + + std::array buffer_; + + HTTPParser parser_; + std::unique_ptr routing_handle_result_; + request& req_; + response res; + + bool close_connection_ = false; + + const std::string& server_name_; + std::vector buffers_; + + std::string content_length_; + std::string date_str_; + std::string res_body_copy_; + + detail::task_timer::identifier_type task_id_{}; + + bool continue_requested{}; + bool need_to_call_after_handlers_{}; + bool need_to_start_read_after_complete_{}; + bool add_keep_alive_{}; + + std::tuple* middlewares_; + detail::context ctx_; + + std::function& get_cached_date_str; + detail::task_timer& task_timer_; + + size_t res_stream_threshold_; + + std::atomic& queue_length_; + }; + +} // namespace crow + +#include + +namespace crow // NOTE: Already documented in "crow/app.h" +{ +#ifdef CROW_USE_BOOST + namespace asio = boost::asio; + using error_code = boost::system::error_code; +#else + using error_code = asio::error_code; +#endif + + /** + * \namespace crow::websocket + * \brief Namespace that includes the \ref Connection class + * and \ref connection struct. Useful for WebSockets connection. + * + * Used specially in crow/websocket.h, crow/app.h and crow/routing.h + */ + namespace websocket + { + enum class WebSocketReadState + { + MiniHeader, + Len16, + Len64, + Mask, + Payload, + }; + + /// A base class for websocket connection. + struct connection + { + virtual void send_binary(std::string msg) = 0; + virtual void send_text(std::string msg) = 0; + virtual void send_ping(std::string msg) = 0; + virtual void send_pong(std::string msg) = 0; + virtual void close(std::string const& msg = "quit") = 0; + virtual std::string get_remote_ip() = 0; + virtual ~connection() = default; + + void userdata(void* u) { userdata_ = u; } + void* userdata() { return userdata_; } + + private: + void* userdata_; + }; + + // Modified version of the illustration in RFC6455 Section-5.2 + // + // + // 0 1 2 3 -byte + // 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 -bit + // +-+-+-+-+-------+-+-------------+-------------------------------+ + // |F|R|R|R| opcode|M| Payload len | Extended payload length | + // |I|S|S|S| (4) |A| (7) | (16/64) | + // |N|V|V|V| |S| | (if payload len==126/127) | + // | |1|2|3| |K| | | + // +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + + // | Extended payload length continued, if payload len == 127 | + // + - - - - - - - - - - - - - - - +-------------------------------+ + // | |Masking-key, if MASK set to 1 | + // +-------------------------------+-------------------------------+ + // | Masking-key (continued) | Payload Data | + // +-------------------------------- - - - - - - - - - - - - - - - + + // : Payload Data continued ... : + // + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + // | Payload Data continued ... | + // +---------------------------------------------------------------+ + // + + /// A websocket connection. + + template + class Connection : public connection + { + public: + /// Constructor for a connection. + + /// + /// Requires a request with an "Upgrade: websocket" header.
+ /// Automatically handles the handshake. + Connection(const crow::request& req, Adaptor&& adaptor, Handler* handler, uint64_t max_payload, + std::function open_handler, + std::function message_handler, + std::function close_handler, + std::function error_handler, + std::function accept_handler): + adaptor_(std::move(adaptor)), + handler_(handler), + max_payload_bytes_(max_payload), + open_handler_(std::move(open_handler)), + message_handler_(std::move(message_handler)), + close_handler_(std::move(close_handler)), + error_handler_(std::move(error_handler)), + accept_handler_(std::move(accept_handler)) + { + if (!utility::string_equals(req.get_header_value("upgrade"), "websocket")) + { + adaptor_.close(); + handler_->remove_websocket(this); + delete this; + return; + } + + if (accept_handler_) + { + void* ud = nullptr; + if (!accept_handler_(req, &ud)) + { + adaptor_.close(); + handler_->remove_websocket(this); + delete this; + return; + } + userdata(ud); + } + + // Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== + // Sec-WebSocket-Version: 13 + std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + sha1::SHA1 s; + s.processBytes(magic.data(), magic.size()); + uint8_t digest[20]; + s.getDigestBytes(digest); + + start(crow::utility::base64encode((unsigned char*)digest, 20)); + } + + ~Connection() noexcept override + { + // Do not modify anchor_ here since writing shared_ptr is not atomic. + auto watch = std::weak_ptr{anchor_}; + + // Wait until all unhandled asynchronous operations to join. + // As the deletion occurs inside 'check_destroy()', which already locks + // anchor, use count can be 1 on valid deletion context. + while (watch.use_count() > 2) // 1 for 'check_destroy() routine', 1 for 'this->anchor_' + { + std::this_thread::yield(); + } + } + + template + struct WeakWrappedMessage + { + Callable callable; + std::weak_ptr watch; + + void operator()() + { + if (auto anchor = watch.lock()) + { + std::move(callable)(); + } + } + }; + + /// Send data through the socket. + template + void dispatch(CompletionHandler&& handler) + { + asio::dispatch(adaptor_.get_io_service(), + WeakWrappedMessage::type>{ + std::forward(handler), anchor_}); + } + + /// Send data through the socket and return immediately. + template + void post(CompletionHandler&& handler) + { + asio::post(adaptor_.get_io_service(), + WeakWrappedMessage::type>{ + std::forward(handler), anchor_}); + } + + /// Send a "Ping" message. + + /// + /// Usually invoked to check if the other point is still online. + void send_ping(std::string msg) override + { + send_data(0x9, std::move(msg)); + } + + /// Send a "Pong" message. + + /// + /// Usually automatically invoked as a response to a "Ping" message. + void send_pong(std::string msg) override + { + send_data(0xA, std::move(msg)); + } + + /// Send a binary encoded message. + void send_binary(std::string msg) override + { + send_data(0x2, std::move(msg)); + } + + /// Send a plaintext message. + void send_text(std::string msg) override + { + send_data(0x1, std::move(msg)); + } + + /// Send a close signal. + + /// + /// Sets a flag to destroy the object once the message is sent. + void close(std::string const& msg) override + { + dispatch([this, msg]() mutable { + has_sent_close_ = true; + if (has_recv_close_ && !is_close_handler_called_) + { + is_close_handler_called_ = true; + if (close_handler_) + close_handler_(*this, msg); + } + auto header = build_header(0x8, msg.size()); + write_buffers_.emplace_back(std::move(header)); + write_buffers_.emplace_back(msg); + do_write(); + }); + } + + std::string get_remote_ip() override + { + return adaptor_.remote_endpoint().address().to_string(); + } + + void set_max_payload_size(uint64_t payload) + { + max_payload_bytes_ = payload; + } + + protected: + /// Generate the websocket headers using an opcode and the message size (in bytes). + std::string build_header(int opcode, size_t size) + { + char buf[2 + 8] = "\x80\x00"; + buf[0] += opcode; + if (size < 126) + { + buf[1] += static_cast(size); + return {buf, buf + 2}; + } + else if (size < 0x10000) + { + buf[1] += 126; + *(uint16_t*)(buf + 2) = htons(static_cast(size)); + return {buf, buf + 4}; + } + else + { + buf[1] += 127; + *reinterpret_cast(buf + 2) = ((1 == htonl(1)) ? static_cast(size) : (static_cast(htonl((size)&0xFFFFFFFF)) << 32) | htonl(static_cast(size) >> 32)); + return {buf, buf + 10}; + } + } + + /// Send the HTTP upgrade response. + + /// + /// Finishes the handshake process, then starts reading messages from the socket. + void start(std::string&& hello) + { + static const std::string header = + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: "; + write_buffers_.emplace_back(header); + write_buffers_.emplace_back(std::move(hello)); + write_buffers_.emplace_back(crlf); + write_buffers_.emplace_back(crlf); + do_write(); + if (open_handler_) + open_handler_(*this); + do_read(); + } + + /// Read a websocket message. + + /// + /// Involves:
+ /// Handling headers (opcodes, size).
+ /// Unmasking the payload.
+ /// Reading the actual payload.
+ void do_read() + { + if (has_sent_close_ && has_recv_close_) + { + close_connection_ = true; + adaptor_.shutdown_readwrite(); + adaptor_.close(); + check_destroy(); + return; + } + + is_reading = true; + switch (state_) + { + case WebSocketReadState::MiniHeader: + { + mini_header_ = 0; + //asio::async_read(adaptor_.socket(), asio::buffer(&mini_header_, 1), + adaptor_.socket().async_read_some( + asio::buffer(&mini_header_, 2), + [this](const error_code& ec, std::size_t +#ifdef CROW_ENABLE_DEBUG + bytes_transferred +#endif + ) + + { + is_reading = false; + mini_header_ = ntohs(mini_header_); +#ifdef CROW_ENABLE_DEBUG + + if (!ec && bytes_transferred != 2) + { + throw std::runtime_error("WebSocket:MiniHeader:async_read fail:asio bug?"); + } +#endif + + if (!ec) + { + if ((mini_header_ & 0x80) == 0x80) + has_mask_ = true; + else //if the websocket specification is enforced and the message isn't masked, terminate the connection + { +#ifndef CROW_ENFORCE_WS_SPEC + has_mask_ = false; +#else + close_connection_ = true; + adaptor_.shutdown_readwrite(); + adaptor_.close(); + if (error_handler_) + error_handler_(*this, "Client connection not masked."); + check_destroy(); +#endif + } + + if ((mini_header_ & 0x7f) == 127) + { + state_ = WebSocketReadState::Len64; + } + else if ((mini_header_ & 0x7f) == 126) + { + state_ = WebSocketReadState::Len16; + } + else + { + remaining_length_ = mini_header_ & 0x7f; + state_ = WebSocketReadState::Mask; + } + do_read(); + } + else + { + close_connection_ = true; + adaptor_.shutdown_readwrite(); + adaptor_.close(); + if (error_handler_) + error_handler_(*this, ec.message()); + check_destroy(); + } + }); + } + break; + case WebSocketReadState::Len16: + { + remaining_length_ = 0; + remaining_length16_ = 0; + asio::async_read( + adaptor_.socket(), asio::buffer(&remaining_length16_, 2), + [this](const error_code& ec, std::size_t +#ifdef CROW_ENABLE_DEBUG + bytes_transferred +#endif + ) { + is_reading = false; + remaining_length16_ = ntohs(remaining_length16_); + remaining_length_ = remaining_length16_; +#ifdef CROW_ENABLE_DEBUG + if (!ec && bytes_transferred != 2) + { + throw std::runtime_error("WebSocket:Len16:async_read fail:asio bug?"); + } +#endif + + if (!ec) + { + state_ = WebSocketReadState::Mask; + do_read(); + } + else + { + close_connection_ = true; + adaptor_.shutdown_readwrite(); + adaptor_.close(); + if (error_handler_) + error_handler_(*this, ec.message()); + check_destroy(); + } + }); + } + break; + case WebSocketReadState::Len64: + { + asio::async_read( + adaptor_.socket(), asio::buffer(&remaining_length_, 8), + [this](const error_code& ec, std::size_t +#ifdef CROW_ENABLE_DEBUG + bytes_transferred +#endif + ) { + is_reading = false; + remaining_length_ = ((1 == ntohl(1)) ? (remaining_length_) : (static_cast(ntohl((remaining_length_)&0xFFFFFFFF)) << 32) | ntohl((remaining_length_) >> 32)); +#ifdef CROW_ENABLE_DEBUG + if (!ec && bytes_transferred != 8) + { + throw std::runtime_error("WebSocket:Len16:async_read fail:asio bug?"); + } +#endif + + if (!ec) + { + state_ = WebSocketReadState::Mask; + do_read(); + } + else + { + close_connection_ = true; + adaptor_.shutdown_readwrite(); + adaptor_.close(); + if (error_handler_) + error_handler_(*this, ec.message()); + check_destroy(); + } + }); + } + break; + case WebSocketReadState::Mask: + if (remaining_length_ > max_payload_bytes_) + { + close_connection_ = true; + adaptor_.close(); + if (error_handler_) + error_handler_(*this, "Message length exceeds maximum payload."); + check_destroy(); + } + else if (has_mask_) + { + asio::async_read( + adaptor_.socket(), asio::buffer((char*)&mask_, 4), + [this](const error_code& ec, std::size_t +#ifdef CROW_ENABLE_DEBUG + bytes_transferred +#endif + ) { + is_reading = false; +#ifdef CROW_ENABLE_DEBUG + if (!ec && bytes_transferred != 4) + { + throw std::runtime_error("WebSocket:Mask:async_read fail:asio bug?"); + } +#endif + + if (!ec) + { + state_ = WebSocketReadState::Payload; + do_read(); + } + else + { + close_connection_ = true; + if (error_handler_) + error_handler_(*this, ec.message()); + adaptor_.shutdown_readwrite(); + adaptor_.close(); + check_destroy(); + } + }); + } + else + { + state_ = WebSocketReadState::Payload; + do_read(); + } + break; + case WebSocketReadState::Payload: + { + auto to_read = static_cast(buffer_.size()); + if (remaining_length_ < to_read) + to_read = remaining_length_; + adaptor_.socket().async_read_some( + asio::buffer(buffer_, static_cast(to_read)), + [this](const error_code& ec, std::size_t bytes_transferred) { + is_reading = false; + + if (!ec) + { + fragment_.insert(fragment_.end(), buffer_.begin(), buffer_.begin() + bytes_transferred); + remaining_length_ -= bytes_transferred; + if (remaining_length_ == 0) + { + if (handle_fragment()) + { + state_ = WebSocketReadState::MiniHeader; + do_read(); + } + } + else + do_read(); + } + else + { + close_connection_ = true; + if (error_handler_) + error_handler_(*this, ec.message()); + adaptor_.shutdown_readwrite(); + adaptor_.close(); + check_destroy(); + } + }); + } + break; + } + } + + /// Check if the FIN bit is set. + bool is_FIN() + { + return mini_header_ & 0x8000; + } + + /// Extract the opcode from the header. + int opcode() + { + return (mini_header_ & 0x0f00) >> 8; + } + + /// Process the payload fragment. + + /// + /// Unmasks the fragment, checks the opcode, merges fragments into 1 message body, and calls the appropriate handler. + bool handle_fragment() + { + if (has_mask_) + { + for (decltype(fragment_.length()) i = 0; i < fragment_.length(); i++) + { + fragment_[i] ^= ((char*)&mask_)[i % 4]; + } + } + switch (opcode()) + { + case 0: // Continuation + { + message_ += fragment_; + if (is_FIN()) + { + if (message_handler_) + message_handler_(*this, message_, is_binary_); + message_.clear(); + } + } + break; + case 1: // Text + { + is_binary_ = false; + message_ += fragment_; + if (is_FIN()) + { + if (message_handler_) + message_handler_(*this, message_, is_binary_); + message_.clear(); + } + } + break; + case 2: // Binary + { + is_binary_ = true; + message_ += fragment_; + if (is_FIN()) + { + if (message_handler_) + message_handler_(*this, message_, is_binary_); + message_.clear(); + } + } + break; + case 0x8: // Close + { + has_recv_close_ = true; + if (!has_sent_close_) + { + close(fragment_); + } + else + { + adaptor_.shutdown_readwrite(); + adaptor_.close(); + close_connection_ = true; + if (!is_close_handler_called_) + { + if (close_handler_) + close_handler_(*this, fragment_); + is_close_handler_called_ = true; + } + check_destroy(); + return false; + } + } + break; + case 0x9: // Ping + { + send_pong(fragment_); + } + break; + case 0xA: // Pong + { + pong_received_ = true; + } + break; + } + + fragment_.clear(); + return true; + } + + /// Send the buffers' data through the socket. + + /// + /// Also destroys the object if the Close flag is set. + void do_write() + { + if (sending_buffers_.empty()) + { + sending_buffers_.swap(write_buffers_); + std::vector buffers; + buffers.reserve(sending_buffers_.size()); + for (auto& s : sending_buffers_) + { + buffers.emplace_back(asio::buffer(s)); + } + auto watch = std::weak_ptr{anchor_}; + asio::async_write( + adaptor_.socket(), buffers, + [&, watch](const error_code& ec, std::size_t /*bytes_transferred*/) { + if (!ec && !close_connection_) + { + sending_buffers_.clear(); + if (!write_buffers_.empty()) + do_write(); + if (has_sent_close_) + close_connection_ = true; + } + else + { + auto anchor = watch.lock(); + if (anchor == nullptr) { return; } + + sending_buffers_.clear(); + close_connection_ = true; + check_destroy(); + } + }); + } + } + + /// Destroy the Connection. + void check_destroy() + { + //if (has_sent_close_ && has_recv_close_) + if (!is_close_handler_called_) + if (close_handler_) + close_handler_(*this, "uncleanly"); + handler_->remove_websocket(this); + if (sending_buffers_.empty() && !is_reading) + delete this; + } + + + struct SendMessageType + { + std::string payload; + Connection* self; + int opcode; + + void operator()() + { + self->send_data_impl(this); + } + }; + + void send_data_impl(SendMessageType* s) + { + auto header = build_header(s->opcode, s->payload.size()); + write_buffers_.emplace_back(std::move(header)); + write_buffers_.emplace_back(std::move(s->payload)); + do_write(); + } + + void send_data(int opcode, std::string&& msg) + { + SendMessageType event_arg{ + std::move(msg), + this, + opcode}; + + post(std::move(event_arg)); + } + + private: + Adaptor adaptor_; + Handler* handler_; + + std::vector sending_buffers_; + std::vector write_buffers_; + + std::array buffer_; + bool is_binary_; + std::string message_; + std::string fragment_; + WebSocketReadState state_{WebSocketReadState::MiniHeader}; + uint16_t remaining_length16_{0}; + uint64_t remaining_length_{0}; + uint64_t max_payload_bytes_{UINT64_MAX}; + bool close_connection_{false}; + bool is_reading{false}; + bool has_mask_{false}; + uint32_t mask_; + uint16_t mini_header_; + bool has_sent_close_{false}; + bool has_recv_close_{false}; + bool error_occurred_{false}; + bool pong_received_{false}; + bool is_close_handler_called_{false}; + + std::shared_ptr anchor_ = std::make_shared(); // Value is just for placeholding + + std::function open_handler_; + std::function message_handler_; + std::function close_handler_; + std::function error_handler_; + std::function accept_handler_; + }; + } // namespace websocket +} // namespace crow + + +namespace crow +{ + constexpr const char VERSION[] = "master"; +} + + +#ifdef CROW_USE_BOOST +#include +#ifdef CROW_ENABLE_SSL +#include +#endif +#else +#ifndef ASIO_STANDALONE +#define ASIO_STANDALONE +#endif +#include +#ifdef CROW_ENABLE_SSL +#include +#endif +#endif + +#include +#include +#include +#include +#include +#include + + + +namespace crow // NOTE: Already documented in "crow/app.h" +{ +#ifdef CROW_USE_BOOST + namespace asio = boost::asio; + using error_code = boost::system::error_code; +#else + using error_code = asio::error_code; +#endif + using tcp = asio::ip::tcp; + + template + class Server + { + public: + Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr): + acceptor_(io_service_, tcp::endpoint(asio::ip::address::from_string(bindaddr), port)), + signals_(io_service_), + tick_timer_(io_service_), + handler_(handler), + concurrency_(concurrency), + timeout_(timeout), + server_name_(server_name), + port_(port), + bindaddr_(bindaddr), + task_queue_length_pool_(concurrency_ - 1), + middlewares_(middlewares), + adaptor_ctx_(adaptor_ctx) + {} + + void set_tick_function(std::chrono::milliseconds d, std::function f) + { + tick_interval_ = d; + tick_function_ = f; + } + + void on_tick() + { + tick_function_(); + tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count())); + tick_timer_.async_wait([this](const error_code& ec) { + if (ec) + return; + on_tick(); + }); + } + + void run() + { + uint16_t worker_thread_count = concurrency_ - 1; + for (int i = 0; i < worker_thread_count; i++) + io_service_pool_.emplace_back(new asio::io_service()); + get_cached_date_str_pool_.resize(worker_thread_count); + task_timer_pool_.resize(worker_thread_count); + + std::vector> v; + std::atomic init_count(0); + for (uint16_t i = 0; i < worker_thread_count; i++) + v.push_back( + std::async( + std::launch::async, [this, i, &init_count] { + // thread local date string get function + auto last = std::chrono::steady_clock::now(); + + std::string date_str; + auto update_date_str = [&] { + auto last_time_t = time(0); + tm my_tm; + +#if defined(_MSC_VER) || defined(__MINGW32__) + gmtime_s(&my_tm, &last_time_t); +#else + gmtime_r(&last_time_t, &my_tm); +#endif + date_str.resize(100); + size_t date_str_sz = strftime(&date_str[0], 99, "%a, %d %b %Y %H:%M:%S GMT", &my_tm); + date_str.resize(date_str_sz); + }; + update_date_str(); + get_cached_date_str_pool_[i] = [&]() -> std::string { + if (std::chrono::steady_clock::now() - last >= std::chrono::seconds(1)) + { + last = std::chrono::steady_clock::now(); + update_date_str(); + } + return date_str; + }; + + // initializing task timers + detail::task_timer task_timer(*io_service_pool_[i]); + task_timer.set_default_timeout(timeout_); + task_timer_pool_[i] = &task_timer; + task_queue_length_pool_[i] = 0; + + init_count++; + while (1) + { + try + { + if (io_service_pool_[i]->run() == 0) + { + // when io_service.run returns 0, there are no more works to do. + break; + } + } + catch (std::exception& e) + { + CROW_LOG_ERROR << "Worker Crash: An uncaught exception occurred: " << e.what(); + } + } + })); + + if (tick_function_ && tick_interval_.count() > 0) + { + tick_timer_.expires_after(std::chrono::milliseconds(tick_interval_.count())); + tick_timer_.async_wait( + [this](const error_code& ec) { + if (ec) + return; + on_tick(); + }); + } + + port_ = acceptor_.local_endpoint().port(); + handler_->port(port_); + + + CROW_LOG_INFO << server_name_ << " server is running at " << (handler_->ssl_used() ? "https://" : "http://") << bindaddr_ << ":" << acceptor_.local_endpoint().port() << " using " << concurrency_ << " threads"; + CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs."; + + signals_.async_wait( + [&](const error_code& /*error*/, int /*signal_number*/) { + stop(); + }); + + while (worker_thread_count != init_count) + std::this_thread::yield(); + + do_accept(); + + std::thread( + [this] { + notify_start(); + io_service_.run(); + CROW_LOG_INFO << "Exiting."; + }) + .join(); + } + + void stop() + { + shutting_down_ = true; // Prevent the acceptor from taking new connections + for (auto& io_service : io_service_pool_) + { + if (io_service != nullptr) + { + CROW_LOG_INFO << "Closing IO service " << &io_service; + io_service->stop(); // Close all io_services (and HTTP connections) + } + } + + CROW_LOG_INFO << "Closing main IO service (" << &io_service_ << ')'; + io_service_.stop(); // Close main io_service + } + + /// Wait until the server has properly started + void wait_for_start() + { + std::unique_lock lock(start_mutex_); + if (!server_started_) + cv_started_.wait(lock); + } + + void signal_clear() + { + signals_.clear(); + } + + void signal_add(int signal_number) + { + signals_.add(signal_number); + } + + private: + uint16_t pick_io_service_idx() + { + uint16_t min_queue_idx = 0; + + // TODO improve load balancing + // size_t is used here to avoid the security issue https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type/ + // even though the max value of this can be only uint16_t as concurrency is uint16_t. + for (size_t i = 1; i < task_queue_length_pool_.size() && task_queue_length_pool_[min_queue_idx] > 0; i++) + // No need to check other io_services if the current one has no tasks + { + if (task_queue_length_pool_[i] < task_queue_length_pool_[min_queue_idx]) + min_queue_idx = i; + } + return min_queue_idx; + } + + void do_accept() + { + if (!shutting_down_) + { + uint16_t service_idx = pick_io_service_idx(); + asio::io_service& is = *io_service_pool_[service_idx]; + task_queue_length_pool_[service_idx]++; + CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx]; + + auto p = std::make_shared>( + is, handler_, server_name_, middlewares_, + get_cached_date_str_pool_[service_idx], *task_timer_pool_[service_idx], adaptor_ctx_, task_queue_length_pool_[service_idx]); + + acceptor_.async_accept( + p->socket(), + [this, p, &is, service_idx](error_code ec) { + if (!ec) + { + is.post( + [p] { + p->start(); + }); + } + else + { + task_queue_length_pool_[service_idx]--; + CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx]; + } + do_accept(); + }); + } + } + + /// Notify anything using `wait_for_start()` to proceed + void notify_start() + { + std::unique_lock lock(start_mutex_); + server_started_ = true; + cv_started_.notify_all(); + } + + private: + std::vector> io_service_pool_; + asio::io_service io_service_; + std::vector task_timer_pool_; + std::vector> get_cached_date_str_pool_; + tcp::acceptor acceptor_; + bool shutting_down_ = false; + bool server_started_{false}; + std::condition_variable cv_started_; + std::mutex start_mutex_; + asio::signal_set signals_; + + asio::basic_waitable_timer tick_timer_; + + Handler* handler_; + uint16_t concurrency_{2}; + std::uint8_t timeout_; + std::string server_name_; + uint16_t port_; + std::string bindaddr_; + std::vector> task_queue_length_pool_; + + std::chrono::milliseconds tick_interval_; + std::function tick_function_; + + std::tuple* middlewares_; + + typename Adaptor::context* adaptor_ctx_; + }; +} // namespace crow + +#include +#include +#include +#include +#include + +namespace crow +{ + namespace mustache + { + using context = json::wvalue; + + template_t load(const std::string& filename); + + class invalid_template_exception : public std::exception + { + public: + invalid_template_exception(const std::string& msg): + msg("crow::mustache error: " + msg) + {} + virtual const char* what() const throw() + { + return msg.c_str(); + } + std::string msg; + }; + + struct rendered_template : returnable + { + rendered_template(): + returnable("text/html") {} + + rendered_template(std::string& body): + returnable("text/html"), body_(std::move(body)) {} + + std::string body_; + + std::string dump() const override + { + return body_; + } + }; + + enum class ActionType + { + Ignore, + Tag, + UnescapeTag, + OpenBlock, + CloseBlock, + ElseBlock, + Partial, + }; + + struct Action + { + int start; + int end; + int pos; + ActionType t; + Action(ActionType t, size_t start, size_t end, size_t pos = 0): + start(static_cast(start)), end(static_cast(end)), pos(static_cast(pos)), t(t) + { + } + }; + + /// A mustache template object. + class template_t + { + public: + template_t(std::string body): + body_(std::move(body)) + { + // {{ {{# {{/ {{^ {{! {{> {{= + parse(); + } + + private: + std::string tag_name(const Action& action) const + { + return body_.substr(action.start, action.end - action.start); + } + auto find_context(const std::string& name, const std::vector& stack, bool shouldUseOnlyFirstStackValue = false) const -> std::pair + { + if (name == ".") + { + return {true, *stack.back()}; + } + static json::wvalue empty_str; + empty_str = ""; + + int dotPosition = name.find("."); + if (dotPosition == static_cast(name.npos)) + { + for (auto it = stack.rbegin(); it != stack.rend(); ++it) + { + if ((*it)->t() == json::type::Object) + { + if ((*it)->count(name)) + return {true, (**it)[name]}; + } + } + } + else + { + std::vector dotPositions; + dotPositions.push_back(-1); + while (dotPosition != static_cast(name.npos)) + { + dotPositions.push_back(dotPosition); + dotPosition = name.find(".", dotPosition + 1); + } + dotPositions.push_back(name.size()); + std::vector names; + names.reserve(dotPositions.size() - 1); + for (int i = 1; i < static_cast(dotPositions.size()); i++) + names.emplace_back(name.substr(dotPositions[i - 1] + 1, dotPositions[i] - dotPositions[i - 1] - 1)); + + for (auto it = stack.rbegin(); it != stack.rend(); ++it) + { + const context* view = *it; + bool found = true; + for (auto jt = names.begin(); jt != names.end(); ++jt) + { + if (view->t() == json::type::Object && + view->count(*jt)) + { + view = &(*view)[*jt]; + } + else + { + if (shouldUseOnlyFirstStackValue) + { + return {false, empty_str}; + } + found = false; + break; + } + } + if (found) + return {true, *view}; + } + } + + return {false, empty_str}; + } + + void escape(const std::string& in, std::string& out) const + { + out.reserve(out.size() + in.size()); + for (auto it = in.begin(); it != in.end(); ++it) + { + switch (*it) + { + case '&': out += "&"; break; + case '<': out += "<"; break; + case '>': out += ">"; break; + case '"': out += """; break; + case '\'': out += "'"; break; + case '/': out += "/"; break; + case '`': out += "`"; break; + case '=': out += "="; break; + default: out += *it; break; + } + } + } + + bool isTagInsideObjectBlock(const int& current, const std::vector& stack) const + { + int openedBlock = 0; + for (int i = current; i > 0; --i) + { + auto& action = actions_[i - 1]; + + if (action.t == ActionType::OpenBlock) + { + if (openedBlock == 0 && (*stack.rbegin())->t() == json::type::Object) + { + return true; + } + --openedBlock; + } + else if (action.t == ActionType::CloseBlock) + { + ++openedBlock; + } + } + + return false; + } + + void render_internal(int actionBegin, int actionEnd, std::vector& stack, std::string& out, int indent) const + { + int current = actionBegin; + + if (indent) + out.insert(out.size(), indent, ' '); + + while (current < actionEnd) + { + auto& fragment = fragments_[current]; + auto& action = actions_[current]; + render_fragment(fragment, indent, out); + switch (action.t) + { + case ActionType::Ignore: + // do nothing + break; + case ActionType::Partial: + { + std::string partial_name = tag_name(action); + auto partial_templ = load(partial_name); + int partial_indent = action.pos; + partial_templ.render_internal(0, partial_templ.fragments_.size() - 1, stack, out, partial_indent ? indent + partial_indent : 0); + } + break; + case ActionType::UnescapeTag: + case ActionType::Tag: + { + bool shouldUseOnlyFirstStackValue = false; + if (isTagInsideObjectBlock(current, stack)) + { + shouldUseOnlyFirstStackValue = true; + } + auto optional_ctx = find_context(tag_name(action), stack, shouldUseOnlyFirstStackValue); + auto& ctx = optional_ctx.second; + switch (ctx.t()) + { + case json::type::False: + case json::type::True: + case json::type::Number: + out += ctx.dump(); + break; + case json::type::String: + if (action.t == ActionType::Tag) + escape(ctx.s, out); + else + out += ctx.s; + break; + case json::type::Function: + { + std::string execute_result = ctx.execute(); + while (execute_result.find("{{") != std::string::npos) + { + template_t result_plug(execute_result); + execute_result = result_plug.render_string(*(stack[0])); + } + + if (action.t == ActionType::Tag) + escape(execute_result, out); + else + out += execute_result; + } + break; + default: + throw std::runtime_error("not implemented tag type" + utility::lexical_cast(static_cast(ctx.t()))); + } + } + break; + case ActionType::ElseBlock: + { + static context nullContext; + auto optional_ctx = find_context(tag_name(action), stack); + if (!optional_ctx.first) + { + stack.emplace_back(&nullContext); + break; + } + + auto& ctx = optional_ctx.second; + switch (ctx.t()) + { + case json::type::List: + if (ctx.l && !ctx.l->empty()) + current = action.pos; + else + stack.emplace_back(&nullContext); + break; + case json::type::False: + case json::type::Null: + stack.emplace_back(&nullContext); + break; + default: + current = action.pos; + break; + } + break; + } + case ActionType::OpenBlock: + { + auto optional_ctx = find_context(tag_name(action), stack); + if (!optional_ctx.first) + { + current = action.pos; + break; + } + + auto& ctx = optional_ctx.second; + switch (ctx.t()) + { + case json::type::List: + if (ctx.l) + for (auto it = ctx.l->begin(); it != ctx.l->end(); ++it) + { + stack.push_back(&*it); + render_internal(current + 1, action.pos, stack, out, indent); + stack.pop_back(); + } + current = action.pos; + break; + case json::type::Number: + case json::type::String: + case json::type::Object: + case json::type::True: + stack.push_back(&ctx); + break; + case json::type::False: + case json::type::Null: + current = action.pos; + break; + default: + throw std::runtime_error("{{#: not implemented context type: " + utility::lexical_cast(static_cast(ctx.t()))); + break; + } + break; + } + case ActionType::CloseBlock: + stack.pop_back(); + break; + default: + throw std::runtime_error("not implemented " + utility::lexical_cast(static_cast(action.t))); + } + current++; + } + auto& fragment = fragments_[actionEnd]; + render_fragment(fragment, indent, out); + } + void render_fragment(const std::pair fragment, int indent, std::string& out) const + { + if (indent) + { + for (int i = fragment.first; i < fragment.second; i++) + { + out += body_[i]; + if (body_[i] == '\n' && i + 1 != static_cast(body_.size())) + out.insert(out.size(), indent, ' '); + } + } + else + out.insert(out.size(), body_, fragment.first, fragment.second - fragment.first); + } + + public: + /// Output a returnable template from this mustache template + rendered_template render() const + { + context empty_ctx; + std::vector stack; + stack.emplace_back(&empty_ctx); + + std::string ret; + render_internal(0, fragments_.size() - 1, stack, ret, 0); + return rendered_template(ret); + } + + /// Apply the values from the context provided and output a returnable template from this mustache template + rendered_template render(const context& ctx) const + { + std::vector stack; + stack.emplace_back(&ctx); + + std::string ret; + render_internal(0, fragments_.size() - 1, stack, ret, 0); + return rendered_template(ret); + } + + /// Apply the values from the context provided and output a returnable template from this mustache template + rendered_template render(const context&& ctx) const + { + return render(ctx); + } + + /// Output a returnable template from this mustache template + std::string render_string() const + { + context empty_ctx; + std::vector stack; + stack.emplace_back(&empty_ctx); + + std::string ret; + render_internal(0, fragments_.size() - 1, stack, ret, 0); + return ret; + } + + /// Apply the values from the context provided and output a returnable template from this mustache template + std::string render_string(const context& ctx) const + { + std::vector stack; + stack.emplace_back(&ctx); + + std::string ret; + render_internal(0, fragments_.size() - 1, stack, ret, 0); + return ret; + } + + private: + void parse() + { + std::string tag_open = "{{"; + std::string tag_close = "}}"; + + std::vector blockPositions; + + size_t current = 0; + while (1) + { + size_t idx = body_.find(tag_open, current); + if (idx == body_.npos) + { + fragments_.emplace_back(static_cast(current), static_cast(body_.size())); + actions_.emplace_back(ActionType::Ignore, 0, 0); + break; + } + fragments_.emplace_back(static_cast(current), static_cast(idx)); + + idx += tag_open.size(); + size_t endIdx = body_.find(tag_close, idx); + if (endIdx == idx) + { + throw invalid_template_exception("empty tag is not allowed"); + } + if (endIdx == body_.npos) + { + // error, no matching tag + throw invalid_template_exception("not matched opening tag"); + } + current = endIdx + tag_close.size(); + switch (body_[idx]) + { + case '#': + idx++; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + blockPositions.emplace_back(static_cast(actions_.size())); + actions_.emplace_back(ActionType::OpenBlock, idx, endIdx); + break; + case '/': + idx++; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + { + auto& matched = actions_[blockPositions.back()]; + if (body_.compare(idx, endIdx - idx, + body_, matched.start, matched.end - matched.start) != 0) + { + throw invalid_template_exception("not matched {{# {{/ pair: " + + body_.substr(matched.start, matched.end - matched.start) + ", " + + body_.substr(idx, endIdx - idx)); + } + matched.pos = actions_.size(); + } + actions_.emplace_back(ActionType::CloseBlock, idx, endIdx, blockPositions.back()); + blockPositions.pop_back(); + break; + case '^': + idx++; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + blockPositions.emplace_back(static_cast(actions_.size())); + actions_.emplace_back(ActionType::ElseBlock, idx, endIdx); + break; + case '!': + // do nothing action + actions_.emplace_back(ActionType::Ignore, idx + 1, endIdx); + break; + case '>': // partial + idx++; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + actions_.emplace_back(ActionType::Partial, idx, endIdx); + break; + case '{': + if (tag_open != "{{" || tag_close != "}}") + throw invalid_template_exception("cannot use triple mustache when delimiter changed"); + + idx++; + if (body_[endIdx + 2] != '}') + { + throw invalid_template_exception("{{{: }}} not matched"); + } + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + actions_.emplace_back(ActionType::UnescapeTag, idx, endIdx); + current++; + break; + case '&': + idx++; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + actions_.emplace_back(ActionType::UnescapeTag, idx, endIdx); + break; + case '=': + // tag itself is no-op + idx++; + actions_.emplace_back(ActionType::Ignore, idx, endIdx); + endIdx--; + if (body_[endIdx] != '=') + throw invalid_template_exception("{{=: not matching = tag: " + body_.substr(idx, endIdx - idx)); + endIdx--; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx] == ' ') + endIdx--; + endIdx++; + { + bool succeeded = false; + for (size_t i = idx; i < endIdx; i++) + { + if (body_[i] == ' ') + { + tag_open = body_.substr(idx, i - idx); + while (body_[i] == ' ') + i++; + tag_close = body_.substr(i, endIdx - i); + if (tag_open.empty()) + throw invalid_template_exception("{{=: empty open tag"); + if (tag_close.empty()) + throw invalid_template_exception("{{=: empty close tag"); + + if (tag_close.find(" ") != tag_close.npos) + throw invalid_template_exception("{{=: invalid open/close tag: " + tag_open + " " + tag_close); + succeeded = true; + break; + } + } + if (!succeeded) + throw invalid_template_exception("{{=: cannot find space between new open/close tags"); + } + break; + default: + // normal tag case; + while (body_[idx] == ' ') + idx++; + while (body_[endIdx - 1] == ' ') + endIdx--; + actions_.emplace_back(ActionType::Tag, idx, endIdx); + break; + } + } + + // removing standalones + for (int i = actions_.size() - 2; i >= 0; i--) + { + if (actions_[i].t == ActionType::Tag || actions_[i].t == ActionType::UnescapeTag) + continue; + auto& fragment_before = fragments_[i]; + auto& fragment_after = fragments_[i + 1]; + bool is_last_action = i == static_cast(actions_.size()) - 2; + bool all_space_before = true; + int j, k; + for (j = fragment_before.second - 1; j >= fragment_before.first; j--) + { + if (body_[j] != ' ') + { + all_space_before = false; + break; + } + } + if (all_space_before && i > 0) + continue; + if (!all_space_before && body_[j] != '\n') + continue; + bool all_space_after = true; + for (k = fragment_after.first; k < static_cast(body_.size()) && k < fragment_after.second; k++) + { + if (body_[k] != ' ') + { + all_space_after = false; + break; + } + } + if (all_space_after && !is_last_action) + continue; + if (!all_space_after && + !( + body_[k] == '\n' || + (body_[k] == '\r' && + k + 1 < static_cast(body_.size()) && + body_[k + 1] == '\n'))) + continue; + if (actions_[i].t == ActionType::Partial) + { + actions_[i].pos = fragment_before.second - j - 1; + } + fragment_before.second = j + 1; + if (!all_space_after) + { + if (body_[k] == '\n') + k++; + else + k += 2; + fragment_after.first = k; + } + } + } + + std::vector> fragments_; + std::vector actions_; + std::string body_; + }; + + inline template_t compile(const std::string& body) + { + return template_t(body); + } + namespace detail + { + inline std::string& get_template_base_directory_ref() + { + static std::string template_base_directory = "templates"; + return template_base_directory; + } + + /// A base directory not related to any blueprint + inline std::string& get_global_template_base_directory_ref() + { + static std::string template_base_directory = "templates"; + return template_base_directory; + } + } // namespace detail + + inline std::string default_loader(const std::string& filename) + { + std::string path = detail::get_template_base_directory_ref(); + std::ifstream inf(utility::join_path(path, filename)); + if (!inf) + { + CROW_LOG_WARNING << "Template \"" << filename << "\" not found."; + return {}; + } + return {std::istreambuf_iterator(inf), std::istreambuf_iterator()}; + } + + namespace detail + { + inline std::function& get_loader_ref() + { + static std::function loader = default_loader; + return loader; + } + } // namespace detail + + inline void set_base(const std::string& path) + { + auto& base = detail::get_template_base_directory_ref(); + base = path; + if (base.back() != '\\' && + base.back() != '/') + { + base += '/'; + } + } + + inline void set_global_base(const std::string& path) + { + auto& base = detail::get_global_template_base_directory_ref(); + base = path; + if (base.back() != '\\' && + base.back() != '/') + { + base += '/'; + } + } + + inline void set_loader(std::function loader) + { + detail::get_loader_ref() = std::move(loader); + } + + inline std::string load_text(const std::string& filename) + { + std::string filename_sanitized(filename); + utility::sanitize_filename(filename_sanitized); + return detail::get_loader_ref()(filename_sanitized); + } + + inline std::string load_text_unsafe(const std::string& filename) + { + return detail::get_loader_ref()(filename); + } + + inline template_t load(const std::string& filename) + { + std::string filename_sanitized(filename); + utility::sanitize_filename(filename_sanitized); + return compile(detail::get_loader_ref()(filename_sanitized)); + } + + inline template_t load_unsafe(const std::string& filename) + { + return compile(detail::get_loader_ref()(filename)); + } + } // namespace mustache +} // namespace crow + + +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace crow // NOTE: Already documented in "crow/app.h" +{ + + constexpr const uint16_t INVALID_BP_ID{((uint16_t)-1)}; + + namespace detail + { + /// Typesafe wrapper for storing lists of middleware as their indices in the App + struct middleware_indices + { + template + void push() + {} + + template + void push() + { + using MwContainer = typename App::mw_container_t; + static_assert(black_magic::has_type::value, "Middleware must be present in app"); + static_assert(std::is_base_of::value, "Middleware must extend ILocalMiddleware"); + int idx = black_magic::tuple_index::value; + indices_.push_back(idx); + push(); + } + + void merge_front(const detail::middleware_indices& other) + { + indices_.insert(indices_.begin(), other.indices_.cbegin(), other.indices_.cend()); + } + + void merge_back(const detail::middleware_indices& other) + { + indices_.insert(indices_.end(), other.indices_.cbegin(), other.indices_.cend()); + } + + void pop_back(const detail::middleware_indices& other) + { + indices_.resize(indices_.size() - other.indices_.size()); + } + + bool empty() const + { + return indices_.empty(); + } + + // Sorts indices and filters out duplicates to allow fast lookups with traversal + void pack() + { + std::sort(indices_.begin(), indices_.end()); + indices_.erase(std::unique(indices_.begin(), indices_.end()), indices_.end()); + } + + const std::vector& indices() + { + return indices_; + } + + private: + std::vector indices_; + }; + } // namespace detail + + /// A base class for all rules. + + /// + /// Used to provide a common interface for code dealing with different types of rules.
+ /// A Rule provides a URL, allowed HTTP methods, and handlers. + class BaseRule + { + public: + BaseRule(std::string rule): + rule_(std::move(rule)) + {} + + virtual ~BaseRule() + {} + + virtual void validate() = 0; + + void set_added() { + added_ = true; + } + + bool is_added() { + return added_; + } + + std::unique_ptr upgrade() + { + if (rule_to_upgrade_) + return std::move(rule_to_upgrade_); + return {}; + } + + virtual void handle(request&, response&, const routing_params&) = 0; + virtual void handle_upgrade(const request&, response& res, SocketAdaptor&&) + { + res = response(404); + res.end(); + } +#ifdef CROW_ENABLE_SSL + virtual void handle_upgrade(const request&, response& res, SSLAdaptor&&) + { + res = response(404); + res.end(); + } +#endif + + uint32_t get_methods() + { + return methods_; + } + + template + void foreach_method(F f) + { + for (uint32_t method = 0, method_bit = 1; method < static_cast(HTTPMethod::InternalMethodCount); method++, method_bit <<= 1) + { + if (methods_ & method_bit) + f(method); + } + } + + std::string custom_templates_base; + + const std::string& rule() { return rule_; } + + protected: + uint32_t methods_{1 << static_cast(HTTPMethod::Get)}; + + std::string rule_; + std::string name_; + bool added_{false}; + + std::unique_ptr rule_to_upgrade_; + + detail::middleware_indices mw_indices_; + + friend class Router; + friend class Blueprint; + template + friend struct RuleParameterTraits; + }; + + + namespace detail + { + namespace routing_handler_call_helper + { + template + struct call_pair + { + using type = T; + static const int pos = Pos; + }; + + template + struct call_params + { + H1& handler; + const routing_params& params; + request& req; + response& res; + }; + + template + struct call + {}; + + template + struct call, black_magic::S> + { + void operator()(F cparams) + { + using pushed = typename black_magic::S::template push_back>; + call, pushed>()(cparams); + } + }; + + template + struct call, black_magic::S> + { + void operator()(F cparams) + { + using pushed = typename black_magic::S::template push_back>; + call, pushed>()(cparams); + } + }; + + template + struct call, black_magic::S> + { + void operator()(F cparams) + { + using pushed = typename black_magic::S::template push_back>; + call, pushed>()(cparams); + } + }; + + template + struct call, black_magic::S> + { + void operator()(F cparams) + { + using pushed = typename black_magic::S::template push_back>; + call, pushed>()(cparams); + } + }; + + template + struct call, black_magic::S> + { + void operator()(F cparams) + { + cparams.handler( + cparams.req, + cparams.res, + cparams.params.template get(Args1::pos)...); + } + }; + + template + struct Wrapped + { + template + void set_(Func f, typename std::enable_if>::type, const request&>::value, int>::type = 0) + { + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (const request&, response& res, Args... args) { + res = response(f(args...)); + res.end(); + }); + } + + template + struct req_handler_wrapper + { + req_handler_wrapper(Func f): + f(std::move(f)) + { + } + + void operator()(const request& req, response& res, Args... args) + { + res = response(f(req, args...)); + res.end(); + } + + Func f; + }; + + template + void set_(Func f, typename std::enable_if< + std::is_same>::type, const request&>::value && + !std::is_same>::type, response&>::value, + int>::type = 0) + { + handler_ = req_handler_wrapper(std::move(f)); + /*handler_ = ( + [f = std::move(f)] + (const request& req, response& res, Args... args){ + res = response(f(req, args...)); + res.end(); + });*/ + } + + template + void set_(Func f, typename std::enable_if< + std::is_same>::type, const request&>::value && + std::is_same>::type, response&>::value, + int>::type = 0) + { + handler_ = std::move(f); + } + + template + struct handler_type_helper + { + using type = std::function; + using args_type = black_magic::S...>; + }; + + template + struct handler_type_helper + { + using type = std::function; + using args_type = black_magic::S...>; + }; + + template + struct handler_type_helper + { + using type = std::function; + using args_type = black_magic::S...>; + }; + + typename handler_type_helper::type handler_; + + void operator()(request& req, response& res, const routing_params& params) + { + detail::routing_handler_call_helper::call< + detail::routing_handler_call_helper::call_params< + decltype(handler_)>, + 0, 0, 0, 0, + typename handler_type_helper::args_type, + black_magic::S<>>()( + detail::routing_handler_call_helper::call_params< + decltype(handler_)>{handler_, params, req, res}); + } + }; + + } // namespace routing_handler_call_helper + } // namespace detail + + + class CatchallRule + { + public: + /// @cond SKIP + CatchallRule() {} + + template + typename std::enable_if>::value, void>::type + operator()(Func&& f) + { + static_assert(!std::is_same::value, + "Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable"); + + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (const request&, response& res) { + res = response(f()); + res.end(); + }); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && + black_magic::CallHelper>::value, + void>::type + operator()(Func&& f) + { + static_assert(!std::is_same()))>::value, + "Handler function cannot have void return type; valid return types: string, int, crow::response, crow::returnable"); + + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (const crow::request& req, crow::response& res) { + res = response(f(req)); + res.end(); + }); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + black_magic::CallHelper>::value, + void>::type + operator()(Func&& f) + { + static_assert(std::is_same()))>::value, + "Handler function with response argument should have void return type"); + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (const crow::request&, crow::response& res) { + f(res); + }); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value, + void>::type + operator()(Func&& f) + { + static_assert(std::is_same(), std::declval()))>::value, + "Handler function with response argument should have void return type"); + + handler_ = std::move(f); + } + /// @endcond + bool has_handler() + { + return (handler_ != nullptr); + } + + protected: + friend class Router; + + private: + std::function handler_; + }; + + + /// A rule dealing with websockets. + + /// + /// Provides the interface for the user to put in the necessary handlers for a websocket to work. + template + class WebSocketRule : public BaseRule + { + using self_t = WebSocketRule; + + public: + WebSocketRule(std::string rule, App* app): + BaseRule(std::move(rule)), + app_(app), + max_payload_(UINT64_MAX) + {} + + void validate() override + {} + + void handle(request&, response& res, const routing_params&) override + { + res = response(404); + res.end(); + } + + void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override + { + max_payload_ = max_payload_override_ ? max_payload_ : app_->websocket_max_payload(); + new crow::websocket::Connection(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); + } +#ifdef CROW_ENABLE_SSL + void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override + { + new crow::websocket::Connection(req, std::move(adaptor), app_, max_payload_, open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); + } +#endif + + /// Override the global payload limit for this single WebSocket rule + self_t& max_payload(uint64_t max_payload) + { + max_payload_ = max_payload; + max_payload_override_ = true; + return *this; + } + + template + self_t& onopen(Func f) + { + open_handler_ = f; + return *this; + } + + template + self_t& onmessage(Func f) + { + message_handler_ = f; + return *this; + } + + template + self_t& onclose(Func f) + { + close_handler_ = f; + return *this; + } + + template + self_t& onerror(Func f) + { + error_handler_ = f; + return *this; + } + + template + self_t& onaccept(Func f) + { + accept_handler_ = f; + return *this; + } + + protected: + App* app_; + std::function open_handler_; + std::function message_handler_; + std::function close_handler_; + std::function error_handler_; + std::function accept_handler_; + uint64_t max_payload_; + bool max_payload_override_ = false; + }; + + /// Allows the user to assign parameters using functions. + + /// + /// `rule.name("name").methods(HTTPMethod::POST)` + template + struct RuleParameterTraits + { + using self_t = T; + + template + WebSocketRule& websocket(App* app) + { + auto p = new WebSocketRule(static_cast(this)->rule_, app); + static_cast(this)->rule_to_upgrade_.reset(p); + return *p; + } + + self_t& name(std::string name) noexcept + { + static_cast(this)->name_ = std::move(name); + return static_cast(*this); + } + + self_t& methods(HTTPMethod method) + { + static_cast(this)->methods_ = 1 << static_cast(method); + return static_cast(*this); + } + + template + self_t& methods(HTTPMethod method, MethodArgs... args_method) + { + methods(args_method...); + static_cast(this)->methods_ |= 1 << static_cast(method); + return static_cast(*this); + } + + /// Enable local middleware for this handler + template + self_t& middlewares() + { + static_cast(this)->mw_indices_.template push(); + return static_cast(*this); + } + }; + + /// A rule that can change its parameters during runtime. + class DynamicRule : public BaseRule, public RuleParameterTraits + { + public: + DynamicRule(std::string rule): + BaseRule(std::move(rule)) + {} + + void validate() override + { + if (!erased_handler_) + { + throw std::runtime_error(name_ + (!name_.empty() ? ": " : "") + "no handler for url " + rule_); + } + } + + void handle(request& req, response& res, const routing_params& params) override + { + if (!custom_templates_base.empty()) + mustache::set_base(custom_templates_base); + else if (mustache::detail::get_template_base_directory_ref() != "templates") + mustache::set_base("templates"); + erased_handler_(req, res, params); + } + + template + void operator()(Func f) + { +#ifdef CROW_MSVC_WORKAROUND + using function_t = utility::function_traits; +#else + using function_t = utility::function_traits; +#endif + erased_handler_ = wrap(std::move(f), black_magic::gen_seq()); + } + + // enable_if Arg1 == request && Arg2 == response + // enable_if Arg1 == request && Arg2 != resposne + // enable_if Arg1 != request +#ifdef CROW_MSVC_WORKAROUND + template +#else + template +#endif + std::function + wrap(Func f, black_magic::seq) + { +#ifdef CROW_MSVC_WORKAROUND + using function_t = utility::function_traits; +#else + using function_t = utility::function_traits; +#endif + if (!black_magic::is_parameter_tag_compatible( + black_magic::get_parameter_tag_runtime(rule_.c_str()), + black_magic::compute_parameter_tag_from_args_list< + typename function_t::template arg...>::value)) + { + throw std::runtime_error("route_dynamic: Handler type is mismatched with URL parameters: " + rule_); + } + auto ret = detail::routing_handler_call_helper::Wrapped...>(); + ret.template set_< + typename function_t::template arg...>(std::move(f)); + return ret; + } + + template + void operator()(std::string name, Func&& f) + { + name_ = std::move(name); + (*this).template operator()(std::forward(f)); + } + + private: + std::function erased_handler_; + }; + + /// Default rule created when CROW_ROUTE is called. + template + class TaggedRule : public BaseRule, public RuleParameterTraits> + { + public: + using self_t = TaggedRule; + + TaggedRule(std::string rule): + BaseRule(std::move(rule)) + {} + + void validate() override + { + if (rule_.at(0) != '/') + throw std::runtime_error("Internal error: Routes must start with a '/'"); + + if (!handler_) + { + throw std::runtime_error(name_ + (!name_.empty() ? ": " : "") + "no handler for url " + rule_); + } + } + + template + void operator()(Func&& f) + { + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (crow::request& req, crow::response& res, Args... args) { + detail::wrapped_handler_call(req, res, f, std::forward(args)...); + }); + } + + template + void operator()(std::string name, Func&& f) + { + name_ = std::move(name); + (*this).template operator()(std::forward(f)); + } + + void handle(request& req, response& res, const routing_params& params) override + { + if (!custom_templates_base.empty()) + mustache::set_base(custom_templates_base); + else if (mustache::detail::get_template_base_directory_ref() != mustache::detail::get_global_template_base_directory_ref()) + mustache::set_base(mustache::detail::get_global_template_base_directory_ref()); + + detail::routing_handler_call_helper::call< + detail::routing_handler_call_helper::call_params, + 0, 0, 0, 0, + black_magic::S, + black_magic::S<>>()( + detail::routing_handler_call_helper::call_params{handler_, params, req, res}); + } + + private: + std::function handler_; + }; + + const int RULE_SPECIAL_REDIRECT_SLASH = 1; + + + /// A search tree. + class Trie + { + public: + struct Node + { + uint16_t rule_index{}; + // Assign the index to the maximum 32 unsigned integer value by default so that any other number (specifically 0) is a valid BP id. + uint16_t blueprint_index{INVALID_BP_ID}; + std::string key; + ParamType param = ParamType::MAX; // MAX = No param. + std::vector children; + + bool IsSimpleNode() const + { + return !rule_index && + blueprint_index == INVALID_BP_ID && + children.size() < 2 && + param == ParamType::MAX && + std::all_of(std::begin(children), std::end(children), [](const Node& x) { + return x.param == ParamType::MAX; + }); + } + + Node& add_child_node() + { + children.emplace_back(); + return children.back(); + } + }; + + + Trie() + {} + + /// Check whether or not the trie is empty. + bool is_empty() + { + return head_.children.empty(); + } + + void optimize() + { + for (auto& child : head_.children) + { + optimizeNode(child); + } + } + + + private: + void optimizeNode(Node& node) + { + if (node.children.empty()) + return; + if (node.IsSimpleNode()) + { + auto children_temp = std::move(node.children); + auto& child_temp = children_temp[0]; + node.key += child_temp.key; + node.rule_index = child_temp.rule_index; + node.blueprint_index = child_temp.blueprint_index; + node.children = std::move(child_temp.children); + optimizeNode(node); + } + else + { + for (auto& child : node.children) + { + optimizeNode(child); + } + } + } + + void debug_node_print(const Node& node, int level) + { + if (node.param != ParamType::MAX) + { + switch (node.param) + { + case ParamType::INT: + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " + << ""; + break; + case ParamType::UINT: + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " + << ""; + break; + case ParamType::DOUBLE: + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " + << ""; + break; + case ParamType::STRING: + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " + << ""; + break; + case ParamType::PATH: + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " + << ""; + break; + default: + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " + << ""; + break; + } + } + else + CROW_LOG_DEBUG << std::string(3 * level, ' ') << "└➝ " << node.key; + + for (const auto& child : node.children) + { + debug_node_print(child, level + 1); + } + } + + public: + void debug_print() + { + CROW_LOG_DEBUG << "└➙ ROOT"; + for (const auto& child : head_.children) + debug_node_print(child, 1); + } + + void validate() + { + if (!head_.IsSimpleNode()) + throw std::runtime_error("Internal error: Trie header should be simple!"); + optimize(); + } + + //Rule_index, Blueprint_index, routing_params + routing_handle_result find(const std::string& req_url, const Node& node, unsigned pos = 0, routing_params* params = nullptr, std::vector* blueprints = nullptr) const + { + //start params as an empty struct + routing_params empty; + if (params == nullptr) + params = ∅ + //same for blueprint vector + std::vector MT; + if (blueprints == nullptr) + blueprints = &MT; + + uint16_t found{}; //The rule index to be found + std::vector found_BP; //The Blueprint indices to be found + routing_params match_params; //supposedly the final matched parameters + + auto update_found = [&found, &found_BP, &match_params](routing_handle_result& ret) { + found_BP = std::move(ret.blueprint_indices); + if (ret.rule_index && (!found || found > ret.rule_index)) + { + found = ret.rule_index; + match_params = std::move(ret.r_params); + } + }; + + //if the function was called on a node at the end of the string (the last recursion), return the nodes rule index, and whatever params were passed to the function + if (pos == req_url.size()) + { + found_BP = std::move(*blueprints); + return routing_handle_result{node.rule_index, *blueprints, *params}; + } + + bool found_fragment = false; + + for (const auto& child : node.children) + { + if (child.param != ParamType::MAX) + { + if (child.param == ParamType::INT) + { + char c = req_url[pos]; + if ((c >= '0' && c <= '9') || c == '+' || c == '-') + { + char* eptr; + errno = 0; + long long int value = strtoll(req_url.data() + pos, &eptr, 10); + if (errno != ERANGE && eptr != req_url.data() + pos) + { + found_fragment = true; + params->int_params.push_back(value); + if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index); + auto ret = find(req_url, child, eptr - req_url.data(), params, blueprints); + update_found(ret); + params->int_params.pop_back(); + if (!blueprints->empty()) blueprints->pop_back(); + } + } + } + + else if (child.param == ParamType::UINT) + { + char c = req_url[pos]; + if ((c >= '0' && c <= '9') || c == '+') + { + char* eptr; + errno = 0; + unsigned long long int value = strtoull(req_url.data() + pos, &eptr, 10); + if (errno != ERANGE && eptr != req_url.data() + pos) + { + found_fragment = true; + params->uint_params.push_back(value); + if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index); + auto ret = find(req_url, child, eptr - req_url.data(), params, blueprints); + update_found(ret); + params->uint_params.pop_back(); + if (!blueprints->empty()) blueprints->pop_back(); + } + } + } + + else if (child.param == ParamType::DOUBLE) + { + char c = req_url[pos]; + if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.') + { + char* eptr; + errno = 0; + double value = strtod(req_url.data() + pos, &eptr); + if (errno != ERANGE && eptr != req_url.data() + pos) + { + found_fragment = true; + params->double_params.push_back(value); + if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index); + auto ret = find(req_url, child, eptr - req_url.data(), params, blueprints); + update_found(ret); + params->double_params.pop_back(); + if (!blueprints->empty()) blueprints->pop_back(); + } + } + } + + else if (child.param == ParamType::STRING) + { + size_t epos = pos; + for (; epos < req_url.size(); epos++) + { + if (req_url[epos] == '/') + break; + } + + if (epos != pos) + { + found_fragment = true; + params->string_params.push_back(req_url.substr(pos, epos - pos)); + if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index); + auto ret = find(req_url, child, epos, params, blueprints); + update_found(ret); + params->string_params.pop_back(); + if (!blueprints->empty()) blueprints->pop_back(); + } + } + + else if (child.param == ParamType::PATH) + { + size_t epos = req_url.size(); + + if (epos != pos) + { + found_fragment = true; + params->string_params.push_back(req_url.substr(pos, epos - pos)); + if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index); + auto ret = find(req_url, child, epos, params, blueprints); + update_found(ret); + params->string_params.pop_back(); + if (!blueprints->empty()) blueprints->pop_back(); + } + } + } + + else + { + const std::string& fragment = child.key; + if (req_url.compare(pos, fragment.size(), fragment) == 0) + { + found_fragment = true; + if (child.blueprint_index != INVALID_BP_ID) blueprints->push_back(child.blueprint_index); + auto ret = find(req_url, child, pos + fragment.size(), params, blueprints); + update_found(ret); + if (!blueprints->empty()) blueprints->pop_back(); + } + } + } + + if (!found_fragment) + found_BP = std::move(*blueprints); + + return routing_handle_result{found, found_BP, match_params}; //Called after all the recursions have been done + } + + routing_handle_result find(const std::string& req_url) const + { + return find(req_url, head_); + } + + //This functions assumes any blueprint info passed is valid + void add(const std::string& url, uint16_t rule_index, unsigned bp_prefix_length = 0, uint16_t blueprint_index = INVALID_BP_ID) + { + auto idx = &head_; + + bool has_blueprint = bp_prefix_length != 0 && blueprint_index != INVALID_BP_ID; + + for (unsigned i = 0; i < url.size(); i++) + { + char c = url[i]; + if (c == '<') + { + static struct ParamTraits + { + ParamType type; + std::string name; + } paramTraits[] = + { + {ParamType::INT, ""}, + {ParamType::UINT, ""}, + {ParamType::DOUBLE, ""}, + {ParamType::DOUBLE, ""}, + {ParamType::STRING, ""}, + {ParamType::STRING, ""}, + {ParamType::PATH, ""}, + }; + + for (const auto& x : paramTraits) + { + if (url.compare(i, x.name.size(), x.name) == 0) + { + bool found = false; + for (auto& child : idx->children) + { + if (child.param == x.type) + { + idx = &child; + i += x.name.size(); + found = true; + break; + } + } + if (found) + break; + + auto new_node_idx = &idx->add_child_node(); + new_node_idx->param = x.type; + idx = new_node_idx; + i += x.name.size(); + break; + } + } + + i--; + } + else + { + //This part assumes the tree is unoptimized (every node has a max 1 character key) + bool piece_found = false; + for (auto& child : idx->children) + { + if (child.key[0] == c) + { + idx = &child; + piece_found = true; + break; + } + } + if (!piece_found) + { + auto new_node_idx = &idx->add_child_node(); + new_node_idx->key = c; + //The assumption here is that you'd only need to add a blueprint index if the tree didn't have the BP prefix. + if (has_blueprint && i == bp_prefix_length) + new_node_idx->blueprint_index = blueprint_index; + idx = new_node_idx; + } + } + } + + //check if the last node already has a value (exact url already in Trie) + if (idx->rule_index) + throw std::runtime_error("handler already exists for " + url); + idx->rule_index = rule_index; + } + + private: + Node head_; + }; + + /// A blueprint can be considered a smaller section of a Crow app, specifically where the router is conecerned. + + /// + /// You can use blueprints to assign a common prefix to rules' prefix, set custom static and template folders, and set a custom catchall route. + /// You can also assign nest blueprints for maximum Compartmentalization. + class Blueprint + { + public: + Blueprint(const std::string& prefix): + prefix_(prefix){}; + + Blueprint(const std::string& prefix, const std::string& static_dir): + prefix_(prefix), static_dir_(static_dir){}; + + Blueprint(const std::string& prefix, const std::string& static_dir, const std::string& templates_dir): + prefix_(prefix), static_dir_(static_dir), templates_dir_(templates_dir){}; + + /* + Blueprint(Blueprint& other) + { + prefix_ = std::move(other.prefix_); + all_rules_ = std::move(other.all_rules_); + } + + Blueprint(const Blueprint& other) + { + prefix_ = other.prefix_; + all_rules_ = other.all_rules_; + } +*/ + Blueprint(Blueprint&& value) + { + *this = std::move(value); + } + + Blueprint& operator=(const Blueprint& value) = delete; + + Blueprint& operator=(Blueprint&& value) noexcept + { + prefix_ = std::move(value.prefix_); + static_dir_ = std::move(value.static_dir_); + templates_dir_ = std::move(value.templates_dir_); + all_rules_ = std::move(value.all_rules_); + catchall_rule_ = std::move(value.catchall_rule_); + blueprints_ = std::move(value.blueprints_); + mw_indices_ = std::move(value.mw_indices_); + return *this; + } + + bool operator==(const Blueprint& value) + { + return value.prefix() == prefix_; + } + + bool operator!=(const Blueprint& value) + { + return value.prefix() != prefix_; + } + + std::string prefix() const + { + return prefix_; + } + + std::string static_dir() const + { + return static_dir_; + } + + void set_added() { + added_ = true; + } + + bool is_added() { + return added_; + } + + DynamicRule& new_rule_dynamic(const std::string& rule) + { + std::string new_rule = '/' + prefix_ + rule; + auto ruleObject = new DynamicRule(std::move(new_rule)); + ruleObject->custom_templates_base = templates_dir_; + all_rules_.emplace_back(ruleObject); + + return *ruleObject; + } + + template + typename black_magic::arguments::type::template rebind& new_rule_tagged(const std::string& rule) + { + std::string new_rule = '/' + prefix_ + rule; + using RuleT = typename black_magic::arguments::type::template rebind; + + auto ruleObject = new RuleT(std::move(new_rule)); + ruleObject->custom_templates_base = templates_dir_; + all_rules_.emplace_back(ruleObject); + + return *ruleObject; + } + + void register_blueprint(Blueprint& blueprint) + { + if (blueprints_.empty() || std::find(blueprints_.begin(), blueprints_.end(), &blueprint) == blueprints_.end()) + { + apply_blueprint(blueprint); + blueprints_.emplace_back(&blueprint); + } + else + throw std::runtime_error("blueprint \"" + blueprint.prefix_ + "\" already exists in blueprint \"" + prefix_ + '\"'); + } + + + CatchallRule& catchall_rule() + { + return catchall_rule_; + } + + template + void middlewares() + { + mw_indices_.push(); + } + + private: + void apply_blueprint(Blueprint& blueprint) + { + + blueprint.prefix_ = prefix_ + '/' + blueprint.prefix_; + blueprint.static_dir_ = static_dir_ + '/' + blueprint.static_dir_; + blueprint.templates_dir_ = templates_dir_ + '/' + blueprint.templates_dir_; + for (auto& rule : blueprint.all_rules_) + { + std::string new_rule = '/' + prefix_ + rule->rule_; + rule->rule_ = new_rule; + } + for (Blueprint* bp_child : blueprint.blueprints_) + { + Blueprint& bp_ref = *bp_child; + apply_blueprint(bp_ref); + } + } + + std::string prefix_; + std::string static_dir_; + std::string templates_dir_; + std::vector> all_rules_; + CatchallRule catchall_rule_; + std::vector blueprints_; + detail::middleware_indices mw_indices_; + bool added_{false}; + + friend class Router; + }; + + /// Handles matching requests to existing rules and upgrade requests. + class Router + { + public: + Router() + {} + + DynamicRule& new_rule_dynamic(const std::string& rule) + { + auto ruleObject = new DynamicRule(rule); + all_rules_.emplace_back(ruleObject); + + return *ruleObject; + } + + template + typename black_magic::arguments::type::template rebind& new_rule_tagged(const std::string& rule) + { + using RuleT = typename black_magic::arguments::type::template rebind; + + auto ruleObject = new RuleT(rule); + all_rules_.emplace_back(ruleObject); + + return *ruleObject; + } + + CatchallRule& catchall_rule() + { + return catchall_rule_; + } + + void internal_add_rule_object(const std::string& rule, BaseRule* ruleObject) + { + internal_add_rule_object(rule, ruleObject, INVALID_BP_ID, blueprints_); + } + + void internal_add_rule_object(const std::string& rule, BaseRule* ruleObject, const uint16_t& BP_index, std::vector& blueprints) + { + bool has_trailing_slash = false; + std::string rule_without_trailing_slash; + if (rule.size() > 1 && rule.back() == '/') + { + has_trailing_slash = true; + rule_without_trailing_slash = rule; + rule_without_trailing_slash.pop_back(); + } + + ruleObject->mw_indices_.pack(); + + ruleObject->foreach_method([&](int method) { + per_methods_[method].rules.emplace_back(ruleObject); + per_methods_[method].trie.add(rule, per_methods_[method].rules.size() - 1, BP_index != INVALID_BP_ID ? blueprints[BP_index]->prefix().length() : 0, BP_index); + + // directory case: + // request to '/about' url matches '/about/' rule + if (has_trailing_slash) + { + per_methods_[method].trie.add(rule_without_trailing_slash, RULE_SPECIAL_REDIRECT_SLASH, BP_index != INVALID_BP_ID ? blueprints[BP_index]->prefix().length() : 0, BP_index); + } + }); + + ruleObject->set_added(); + } + + void register_blueprint(Blueprint& blueprint) + { + if (std::find(blueprints_.begin(), blueprints_.end(), &blueprint) == blueprints_.end()) + { + blueprints_.emplace_back(&blueprint); + } + else + throw std::runtime_error("blueprint \"" + blueprint.prefix_ + "\" already exists in router"); + } + + void get_recursive_child_methods(Blueprint* blueprint, std::vector& methods) + { + //we only need to deal with children if the blueprint has absolutely no methods (meaning its index won't be added to the trie) + if (blueprint->static_dir_.empty() && blueprint->all_rules_.empty()) + { + for (Blueprint* bp : blueprint->blueprints_) + { + get_recursive_child_methods(bp, methods); + } + } + else if (!blueprint->static_dir_.empty()) + methods.emplace_back(HTTPMethod::Get); + for (auto& rule : blueprint->all_rules_) + { + rule->foreach_method([&methods](unsigned method) { + HTTPMethod method_final = static_cast(method); + if (std::find(methods.begin(), methods.end(), method_final) == methods.end()) + methods.emplace_back(method_final); + }); + } + } + + void validate_bp() { + //Take all the routes from the registered blueprints and add them to `all_rules_` to be processed. + detail::middleware_indices blueprint_mw; + validate_bp(blueprints_, blueprint_mw); + } + + void validate_bp(std::vector blueprints, detail::middleware_indices& current_mw) + { + for (unsigned i = 0; i < blueprints.size(); i++) + { + Blueprint* blueprint = blueprints[i]; + + if (blueprint->is_added()) continue; + + if (blueprint->static_dir_ == "" && blueprint->all_rules_.empty()) + { + std::vector methods; + get_recursive_child_methods(blueprint, methods); + for (HTTPMethod x : methods) + { + int i = static_cast(x); + per_methods_[i].trie.add(blueprint->prefix(), 0, blueprint->prefix().length(), i); + } + } + + current_mw.merge_back(blueprint->mw_indices_); + for (auto& rule : blueprint->all_rules_) + { + if (rule && !rule->is_added()) + { + auto upgraded = rule->upgrade(); + if (upgraded) + rule = std::move(upgraded); + rule->validate(); + rule->mw_indices_.merge_front(current_mw); + internal_add_rule_object(rule->rule(), rule.get(), i, blueprints); + } + } + validate_bp(blueprint->blueprints_, current_mw); + current_mw.pop_back(blueprint->mw_indices_); + blueprint->set_added(); + } + } + + void validate() + { + for (auto& rule : all_rules_) + { + if (rule && !rule->is_added()) + { + auto upgraded = rule->upgrade(); + if (upgraded) + rule = std::move(upgraded); + rule->validate(); + internal_add_rule_object(rule->rule(), rule.get()); + } + } + for (auto& per_method : per_methods_) + { + per_method.trie.validate(); + } + } + + // TODO maybe add actual_method + template + void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) + { + if (req.method >= HTTPMethod::InternalMethodCount) + return; + + auto& per_method = per_methods_[static_cast(req.method)]; + auto& rules = per_method.rules; + unsigned rule_index = per_method.trie.find(req.url).rule_index; + + if (!rule_index) + { + for (auto& per_method : per_methods_) + { + if (per_method.trie.find(req.url).rule_index) + { + CROW_LOG_DEBUG << "Cannot match method " << req.url << " " << method_name(req.method); + res = response(405); + res.end(); + return; + } + } + + CROW_LOG_INFO << "Cannot match rules " << req.url; + res = response(404); + res.end(); + return; + } + + if (rule_index >= rules.size()) + throw std::runtime_error("Trie internal structure corrupted!"); + + if (rule_index == RULE_SPECIAL_REDIRECT_SLASH) + { + CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; + res = response(301); + + // TODO(ipkn) absolute url building + if (req.get_header_value("Host").empty()) + { + res.add_header("Location", req.url + "/"); + } + else + { + res.add_header("Location", "http://" + req.get_header_value("Host") + req.url + "/"); + } + res.end(); + return; + } + + CROW_LOG_DEBUG << "Matched rule (upgrade) '" << rules[rule_index]->rule_ << "' " << static_cast(req.method) << " / " << rules[rule_index]->get_methods(); + + try + { + rules[rule_index]->handle_upgrade(req, res, std::move(adaptor)); + } + catch (...) + { + exception_handler_(res); + res.end(); + return; + } + } + + void get_found_bp(std::vector& bp_i, std::vector& blueprints, std::vector& found_bps, uint16_t index = 0) + { + // This statement makes 3 assertions: + // 1. The index is above 0. + // 2. The index does not lie outside the given blueprint list. + // 3. The next blueprint we're adding has a prefix that starts the same as the already added blueprint + a slash (the rest is irrelevant). + // + // This is done to prevent a blueprint that has a prefix of "bp_prefix2" to be assumed as a child of one that has "bp_prefix". + // + // If any of the assertions is untrue, we delete the last item added, and continue using the blueprint list of the blueprint found before, the topmost being the router's list + auto verify_prefix = [&bp_i, &index, &blueprints, &found_bps]() { + return index > 0 && + bp_i[index] < blueprints.size() && + blueprints[bp_i[index]]->prefix().substr(0, found_bps[index - 1]->prefix().length() + 1).compare(std::string(found_bps[index - 1]->prefix() + '/')) == 0; + }; + if (index < bp_i.size()) + { + + if (verify_prefix()) + { + found_bps.push_back(blueprints[bp_i[index]]); + get_found_bp(bp_i, found_bps.back()->blueprints_, found_bps, ++index); + } + else + { + if (found_bps.size() < 2) + { + found_bps.clear(); + found_bps.push_back(blueprints_[bp_i[index]]); + } + else + { + found_bps.pop_back(); + Blueprint* last_element = found_bps.back(); + found_bps.push_back(last_element->blueprints_[bp_i[index]]); + } + get_found_bp(bp_i, found_bps.back()->blueprints_, found_bps, ++index); + } + } + } + + /// Is used to handle errors, you insert the error code, found route, request, and response. and it'll either call the appropriate catchall route (considering the blueprint system) and send you a status string (which is mainly used for debug messages), or just set the response code to the proper error code. + std::string get_error(unsigned short code, routing_handle_result& found, const request& req, response& res) + { + res.code = code; + std::vector bps_found; + get_found_bp(found.blueprint_indices, blueprints_, bps_found); + for (int i = bps_found.size() - 1; i > 0; i--) + { + std::vector bpi = found.blueprint_indices; + if (bps_found[i]->catchall_rule().has_handler()) + { + try + { + bps_found[i]->catchall_rule().handler_(req, res); + } + catch (...) + { + exception_handler_(res); + } +#ifdef CROW_ENABLE_DEBUG + return std::string("Redirected to Blueprint \"" + bps_found[i]->prefix() + "\" Catchall rule"); +#else + return std::string(); +#endif + } + } + if (catchall_rule_.has_handler()) + { + try + { + catchall_rule_.handler_(req, res); + } + catch (...) + { + exception_handler_(res); + } +#ifdef CROW_ENABLE_DEBUG + return std::string("Redirected to global Catchall rule"); +#else + return std::string(); +#endif + } + return std::string(); + } + + std::unique_ptr handle_initial(request& req, response& res) + { + HTTPMethod method_actual = req.method; + + std::unique_ptr found{ + new routing_handle_result( + 0, + std::vector(), + routing_params(), + HTTPMethod::InternalMethodCount)}; // This is always returned to avoid a null pointer dereference. + + // NOTE(EDev): This most likely will never run since the parser should handle this situation and close the connection before it gets here. + if (CROW_UNLIKELY(req.method >= HTTPMethod::InternalMethodCount)) + return found; + else if (req.method == HTTPMethod::Head) + { + *found = per_methods_[static_cast(method_actual)].trie.find(req.url); + // support HEAD requests using GET if not defined as method for the requested URL + if (!found->rule_index) + { + method_actual = HTTPMethod::Get; + *found = per_methods_[static_cast(method_actual)].trie.find(req.url); + if (!found->rule_index) // If a route is still not found, return a 404 without executing the rest of the HEAD specific code. + { + CROW_LOG_DEBUG << "Cannot match rules " << req.url; + res = response(404); //TODO(EDev): Should this redirect to catchall? + res.end(); + return found; + } + } + + res.skip_body = true; + found->method = method_actual; + return found; + } + else if (req.method == HTTPMethod::Options) + { + std::string allow = "OPTIONS, HEAD, "; + + if (req.url == "/*") + { + for (int i = 0; i < static_cast(HTTPMethod::InternalMethodCount); i++) + { + if (static_cast(HTTPMethod::Head) == i) + continue; // HEAD is always allowed + + if (!per_methods_[i].trie.is_empty()) + { + allow += method_name(static_cast(i)) + ", "; + } + } + allow = allow.substr(0, allow.size() - 2); + res = response(204); + res.set_header("Allow", allow); + res.end(); + found->method = method_actual; + return found; + } + else + { + bool rules_matched = false; + for (int i = 0; i < static_cast(HTTPMethod::InternalMethodCount); i++) + { + if (per_methods_[i].trie.find(req.url).rule_index) + { + rules_matched = true; + + if (static_cast(HTTPMethod::Head) == i) + continue; // HEAD is always allowed + + allow += method_name(static_cast(i)) + ", "; + } + } + if (rules_matched) + { + allow = allow.substr(0, allow.size() - 2); + res = response(204); + res.set_header("Allow", allow); + res.end(); + found->method = method_actual; + return found; + } + else + { + CROW_LOG_DEBUG << "Cannot match rules " << req.url; + res = response(404); //TODO(EDev): Should this redirect to catchall? + res.end(); + return found; + } + } + } + else // Every request that isn't a HEAD or OPTIONS request + { + *found = per_methods_[static_cast(method_actual)].trie.find(req.url); + // TODO(EDev): maybe ending the else here would allow the requests coming from above (after removing the return statement) to be checked on whether they actually point to a route + if (!found->rule_index) + { + for (auto& per_method : per_methods_) + { + if (per_method.trie.find(req.url).rule_index) //Route found, but in another method + { + const std::string error_message(get_error(405, *found, req, res)); + CROW_LOG_DEBUG << "Cannot match method " << req.url << " " << method_name(method_actual) << ". " << error_message; + res.end(); + return found; + } + } + //Route does not exist anywhere + + const std::string error_message(get_error(404, *found, req, res)); + CROW_LOG_DEBUG << "Cannot match rules " << req.url << ". " << error_message; + res.end(); + return found; + } + + found->method = method_actual; + return found; + } + } + + template + void handle(request& req, response& res, routing_handle_result found) + { + HTTPMethod method_actual = found.method; + auto& rules = per_methods_[static_cast(method_actual)].rules; + unsigned rule_index = found.rule_index; + + if (rule_index >= rules.size()) + throw std::runtime_error("Trie internal structure corrupted!"); + + if (rule_index == RULE_SPECIAL_REDIRECT_SLASH) + { + CROW_LOG_INFO << "Redirecting to a url with trailing slash: " << req.url; + res = response(301); + + // TODO(ipkn) absolute url building + if (req.get_header_value("Host").empty()) + { + res.add_header("Location", req.url + "/"); + } + else + { + res.add_header("Location", "http://" + req.get_header_value("Host") + req.url + "/"); + } + res.end(); + return; + } + + CROW_LOG_DEBUG << "Matched rule '" << rules[rule_index]->rule_ << "' " << static_cast(req.method) << " / " << rules[rule_index]->get_methods(); + + try + { + auto& rule = rules[rule_index]; + handle_rule(rule, req, res, found.r_params); + } + catch (...) + { + exception_handler_(res); + res.end(); + return; + } + } + + template + typename std::enable_if::value != 0, void>::type + handle_rule(BaseRule* rule, crow::request& req, crow::response& res, const crow::routing_params& rp) + { + if (!rule->mw_indices_.empty()) + { + auto& ctx = *reinterpret_cast(req.middleware_context); + auto& container = *reinterpret_cast(req.middleware_container); + detail::middleware_call_criteria_dynamic crit_fwd(rule->mw_indices_.indices()); + + auto glob_completion_handler = std::move(res.complete_request_handler_); + res.complete_request_handler_ = [] {}; + + detail::middleware_call_helper(crit_fwd, container, req, res, ctx); + + if (res.completed_) + { + glob_completion_handler(); + return; + } + + res.complete_request_handler_ = [&rule, &ctx, &container, &req, &res, glob_completion_handler] { + detail::middleware_call_criteria_dynamic crit_bwd(rule->mw_indices_.indices()); + + detail::after_handlers_call_helper< + decltype(crit_bwd), + std::tuple_size::value - 1, + typename App::context_t, + typename App::mw_container_t>(crit_bwd, container, ctx, req, res); + glob_completion_handler(); + }; + } + rule->handle(req, res, rp); + } + + template + typename std::enable_if::value == 0, void>::type + handle_rule(BaseRule* rule, crow::request& req, crow::response& res, const crow::routing_params& rp) + { + rule->handle(req, res, rp); + } + + void debug_print() + { + for (int i = 0; i < static_cast(HTTPMethod::InternalMethodCount); i++) + { + Trie& trie_ = per_methods_[i].trie; + if (!trie_.is_empty()) + { + CROW_LOG_DEBUG << method_name(static_cast(i)); + trie_.debug_print(); + } + } + } + + std::vector& blueprints() + { + return blueprints_; + } + + std::function& exception_handler() + { + return exception_handler_; + } + + static void default_exception_handler(response& res) + { + // any uncaught exceptions become 500s + res = response(500); + + try + { + throw; + } + catch (const std::exception& e) + { + CROW_LOG_ERROR << "An uncaught exception occurred: " << e.what(); + } + catch (...) + { + CROW_LOG_ERROR << "An uncaught exception occurred. The type was unknown so no information was available."; + } + } + + private: + CatchallRule catchall_rule_; + + struct PerMethod + { + std::vector rules; + Trie trie; + + // rule index 0, 1 has special meaning; preallocate it to avoid duplication. + PerMethod(): + rules(2) {} + }; + std::array(HTTPMethod::InternalMethodCount)> per_methods_; + std::vector> all_rules_; + std::vector blueprints_; + std::function exception_handler_ = &default_exception_handler; + }; +} // namespace crow + + +namespace crow +{ + struct CORSHandler; + + /// Used for tuning CORS policies + struct CORSRules + { + friend struct crow::CORSHandler; + + /// Set Access-Control-Allow-Origin. Default is "*" + CORSRules& origin(const std::string& origin) + { + origin_ = origin; + return *this; + } + + /// Set Access-Control-Allow-Methods. Default is "*" + CORSRules& methods(crow::HTTPMethod method) + { + add_list_item(methods_, crow::method_name(method)); + return *this; + } + + /// Set Access-Control-Allow-Methods. Default is "*" + template + CORSRules& methods(crow::HTTPMethod method, Methods... method_list) + { + add_list_item(methods_, crow::method_name(method)); + methods(method_list...); + return *this; + } + + /// Set Access-Control-Allow-Headers. Default is "*" + CORSRules& headers(const std::string& header) + { + add_list_item(headers_, header); + return *this; + } + + /// Set Access-Control-Allow-Headers. Default is "*" + template + CORSRules& headers(const std::string& header, Headers... header_list) + { + add_list_item(headers_, header); + headers(header_list...); + return *this; + } + + /// Set Access-Control-Max-Age. Default is none + CORSRules& max_age(int max_age) + { + max_age_ = std::to_string(max_age); + return *this; + } + + /// Enable Access-Control-Allow-Credentials + CORSRules& allow_credentials() + { + allow_credentials_ = true; + return *this; + } + + /// Ignore CORS and don't send any headers + void ignore() + { + ignore_ = true; + } + + /// Handle CORS on specific prefix path + CORSRules& prefix(const std::string& prefix); + + /// Handle CORS for specific blueprint + CORSRules& blueprint(const Blueprint& bp); + + /// Global CORS policy + CORSRules& global(); + + private: + CORSRules() = delete; + CORSRules(CORSHandler* handler): + handler_(handler) {} + + /// build comma separated list + void add_list_item(std::string& list, const std::string& val) + { + if (list == "*") list = ""; + if (list.size() > 0) list += ", "; + list += val; + } + + /// Set header `key` to `value` if it is not set + void set_header_no_override(const std::string& key, const std::string& value, crow::response& res) + { + if (value.size() == 0) return; + if (!get_header_value(res.headers, key).empty()) return; + res.add_header(key, value); + } + + /// Set response headers + void apply(crow::response& res) + { + if (ignore_) return; + set_header_no_override("Access-Control-Allow-Origin", origin_, res); + set_header_no_override("Access-Control-Allow-Methods", methods_, res); + set_header_no_override("Access-Control-Allow-Headers", headers_, res); + set_header_no_override("Access-Control-Max-Age", max_age_, res); + if (allow_credentials_) set_header_no_override("Access-Control-Allow-Credentials", "true", res); + } + + bool ignore_ = false; + // TODO: support multiple origins that are dynamically selected + std::string origin_ = "*"; + std::string methods_ = "*"; + std::string headers_ = "*"; + std::string max_age_; + bool allow_credentials_ = false; + + CORSHandler* handler_; + }; + + /// CORSHandler is a global middleware for setting CORS headers. + + /// + /// By default, it sets Access-Control-Allow-Origin/Methods/Headers to "*". + /// The default behaviour can be changed with the `global()` cors rule. + /// Additional rules for prexies can be added with `prefix()`. + struct CORSHandler + { + struct context + {}; + + void before_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/) + {} + + void after_handle(crow::request& req, crow::response& res, context& /*ctx*/) + { + auto& rule = find_rule(req.url); + rule.apply(res); + } + + /// Handle CORS on a specific prefix path + CORSRules& prefix(const std::string& prefix) + { + rules.emplace_back(prefix, CORSRules(this)); + return rules.back().second; + } + + /// Handle CORS for a specific blueprint + CORSRules& blueprint(const Blueprint& bp) + { + rules.emplace_back(bp.prefix(), CORSRules(this)); + return rules.back().second; + } + + /// Get the global CORS policy + CORSRules& global() + { + return default_; + } + + private: + CORSRules& find_rule(const std::string& path) + { + // TODO: use a trie in case of many rules + for (auto& rule : rules) + { + // Check if path starts with a rules prefix + if (path.rfind(rule.first, 0) == 0) + { + return rule.second; + } + } + return default_; + } + + std::vector> rules; + CORSRules default_ = CORSRules(this); + }; + + inline CORSRules& CORSRules::prefix(const std::string& prefix) + { + return handler_->prefix(prefix); + } + + inline CORSRules& CORSRules::blueprint(const Blueprint& bp) + { + return handler_->blueprint(bp); + } + + inline CORSRules& CORSRules::global() + { + return handler_->global(); + } + +} // namespace crow + +/** + * \file crow/app.h + * \brief This file includes the definition of the crow::Crow class, + * the crow::App and crow::SimpleApp aliases, and some macros. + * + * In this file are defined: + * - crow::Crow + * - crow::App + * - crow::SimpleApp + * - \ref CROW_ROUTE + * - \ref CROW_BP_ROUTE + * - \ref CROW_WEBSOCKET_ROUTE + * - \ref CROW_MIDDLEWARES + * - \ref CROW_CATCHALL_ROUTE + * - \ref CROW_BP_CATCHALL_ROUTE + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CROW_ENABLE_COMPRESSION +#endif // #ifdef CROW_ENABLE_COMPRESSION + + +#ifdef CROW_MSVC_WORKAROUND + +#define CROW_ROUTE(app, url) app.route_dynamic(url) // See the documentation in the comment below. +#define CROW_BP_ROUTE(blueprint, url) blueprint.new_rule_dynamic(url) // See the documentation in the comment below. + +#else // #ifdef CROW_MSVC_WORKAROUND + +/** + * \def CROW_ROUTE(app, url) + * \brief Creates a route for app using a rule. + * + * It use crow::Crow::route_dynamic or crow::Crow::route to define + * a rule for your application. It's usage is like this: + * + * ```cpp + * auto app = crow::SimpleApp(); // or crow::App() + * CROW_ROUTE(app, "/") + * ([](){ + * return "

Hello, world!

"; + * }); + * ``` + * + * This is the recommended way to define routes in a crow application. + * \see [Page of guide "Routes"](https://crowcpp.org/master/guides/routes/). + */ +#define CROW_ROUTE(app, url) app.template route(url) + +/** + * \def CROW_BP_ROUTE(blueprint, url) + * \brief Creates a route for a blueprint using a rule. + * + * It may use crow::Blueprint::new_rule_dynamic or + * crow::Blueprint::new_rule_tagged to define a new rule for + * an given blueprint. It's usage is similar + * to CROW_ROUTE macro: + * + * ```cpp + * crow::Blueprint my_bp(); + * CROW_BP_ROUTE(my_bp, "/") + * ([](){ + * return "

Hello, world!

"; + * }); + * ``` + * + * This is the recommended way to define routes in a crow blueprint + * because of its compile-time capabilities. + * + * \see [Page of the guide "Blueprints"](https://crowcpp.org/master/guides/blueprints/). + */ +#define CROW_BP_ROUTE(blueprint, url) blueprint.new_rule_tagged(url) + +/** + * \def CROW_WEBSOCKET_ROUTE(app, url) + * \brief Defines WebSocket route for app. + * + * It binds a WebSocket route to app. Easy solution to implement + * WebSockets in your app. The usage syntax of this macro is + * like this: + * + * ```cpp + * auto app = crow::SimpleApp(); // or crow::App() + * CROW_WEBSOCKET_ROUTE(app, "/ws") + * .onopen([&](crow::websocket::connection& conn){ + * do_something(); + * }) + * .onclose([&](crow::websocket::connection& conn, const std::string& reason){ + * do_something(); + * }) + * .onmessage([&](crow::websocket::connection&, const std::string& data, bool is_binary){ + * if (is_binary) + * do_something(data); + * else + * do_something_else(data); + * }); + * ``` + * + * \see [Page of the guide "WebSockets"](https://crowcpp.org/master/guides/websockets/). + */ +#define CROW_WEBSOCKET_ROUTE(app, url) app.route(url).websocket::type>(&app) + +/** + * \def CROW_MIDDLEWARES(app, ...) + * \brief Enable a Middleware for an specific route in app + * or blueprint. + * + * It defines the usage of a Middleware in one route. And it + * can be used in both crow::SimpleApp (and crow::App) instances and + * crow::Blueprint. Its usage syntax is like this: + * + * ```cpp + * auto app = crow::SimpleApp(); // or crow::App() + * CROW_ROUTE(app, "/with_middleware") + * .CROW_MIDDLEWARES(app, LocalMiddleware) // Can be used more than one + * ([]() { // middleware. + * return "Hello world!"; + * }); + * ``` + * + * \see [Page of the guide "Middlewares"](https://crowcpp.org/master/guides/middleware/). + */ +#define CROW_MIDDLEWARES(app, ...) template middlewares::type, __VA_ARGS__>() + +#endif // #ifdef CROW_MSVC_WORKAROUND + +/** + * \def CROW_CATCHALL_ROUTE(app) + * \brief Defines a custom catchall route for app using a + * custom rule. + * + * It defines a handler when the client make a request for an + * undefined route. Instead of just reply with a `404` status + * code (default behavior), you can define a custom handler + * using this macro. + * + * \see [Page of the guide "Routes" (Catchall routes)](https://crowcpp.org/master/guides/routes/#catchall-routes). + */ +#define CROW_CATCHALL_ROUTE(app) app.catchall_route() + +/** + * \def CROW_BP_CATCHALL_ROUTE(blueprint) + * \brief Defines a custom catchall route for blueprint + * using a custom rule. + * + * It defines a handler when the client make a request for an + * undefined route in the blueprint. + * + * \see [Page of the guide "Blueprint" (Define a custom Catchall route)](https://crowcpp.org/master/guides/blueprints/#define-a-custom-catchall-route). + */ +#define CROW_BP_CATCHALL_ROUTE(blueprint) blueprint.catchall_rule() + + +/** + * \namespace crow + * \brief The main namespace of the library. In this namespace + * is defined the most important classes and functions of the + * library. + * + * Within this namespace, the Crow class, Router class, Connection + * class, and other are defined. + */ +namespace crow +{ +#ifdef CROW_ENABLE_SSL + using ssl_context_t = asio::ssl::context; +#endif + /** + * \class Crow + * \brief The main server application class. + * + * Use crow::SimpleApp or crow::App instead of + * directly instantiate this class. + */ + template + class Crow + { + public: + /// \brief This is the crow application + using self_t = Crow; + + /// \brief The HTTP server + using server_t = Server; + +#ifdef CROW_ENABLE_SSL + /// \brief An HTTP server that runs on SSL with an SSLAdaptor + using ssl_server_t = Server; +#endif + Crow() + {} + + /// \brief Construct Crow with a subset of middleware + template + Crow(Ts&&... ts): + middlewares_(make_middleware_tuple(std::forward(ts)...)) + {} + + /// \brief Process an Upgrade request + /// + /// Currently used to upgrade an HTTP connection to a WebSocket connection + template + void handle_upgrade(const request& req, response& res, Adaptor&& adaptor) + { + router_.handle_upgrade(req, res, adaptor); + } + + /// \brief Process only the method and URL of a request and provide a route (or an error response) + std::unique_ptr handle_initial(request& req, response& res) + { + return router_.handle_initial(req, res); + } + + /// \brief Process the fully parsed request and generate a response for it + void handle(request& req, response& res, std::unique_ptr& found) + { + router_.handle(req, res, *found); + } + + /// \brief Process a fully parsed request from start to finish (primarily used for debugging) + void handle_full(request& req, response& res) + { + auto found = handle_initial(req, res); + if (found->rule_index) + handle(req, res, found); + } + + /// \brief Create a dynamic route using a rule (**Use CROW_ROUTE instead**) + DynamicRule& route_dynamic(const std::string& rule) + { + return router_.new_rule_dynamic(rule); + } + + /// \brief Create a route using a rule (**Use CROW_ROUTE instead**) + template +#ifdef CROW_GCC83_WORKAROUND + auto& route(const std::string& rule) +#else + auto route(const std::string& rule) +#endif +#if defined CROW_CAN_USE_CPP17 && !defined CROW_GCC83_WORKAROUND + -> typename std::invoke_result), Router, const std::string&>::type +#elif !defined CROW_GCC83_WORKAROUND + -> typename std::result_of)(Router, const std::string&)>::type +#endif + { + return router_.new_rule_tagged(rule); + } + + /// \brief Create a route for any requests without a proper route (**Use CROW_CATCHALL_ROUTE instead**) + CatchallRule& catchall_route() + { + return router_.catchall_rule(); + } + + /// \brief Set the default max payload size for websockets + self_t& websocket_max_payload(uint64_t max_payload) + { + max_payload_ = max_payload; + return *this; + } + + /// \brief Get the default max payload size for websockets + uint64_t websocket_max_payload() + { + return max_payload_; + } + + self_t& signal_clear() + { + signals_.clear(); + return *this; + } + + self_t& signal_add(int signal_number) + { + signals_.push_back(signal_number); + return *this; + } + + std::vector signals() + { + return signals_; + } + + /// \brief Set the port that Crow will handle requests on + self_t& port(std::uint16_t port) + { + port_ = port; + return *this; + } + + /// \brief Get the port that Crow will handle requests on + std::uint16_t port() + { + return port_; + } + + /// \brief Set the connection timeout in seconds (default is 5) + self_t& timeout(std::uint8_t timeout) + { + timeout_ = timeout; + return *this; + } + + /// \brief Set the server name + self_t& server_name(std::string server_name) + { + server_name_ = server_name; + return *this; + } + + /// \brief The IP address that Crow will handle requests on (default is 0.0.0.0) + self_t& bindaddr(std::string bindaddr) + { + bindaddr_ = bindaddr; + return *this; + } + + /// \brief Get the address that Crow will handle requests on + std::string bindaddr() + { + return bindaddr_; + } + + /// \brief Run the server on multiple threads using all available threads + self_t& multithreaded() + { + return concurrency(std::thread::hardware_concurrency()); + } + + /// \brief Run the server on multiple threads using a specific number + self_t& concurrency(std::uint16_t concurrency) + { + if (concurrency < 2) // Crow can have a minimum of 2 threads running + concurrency = 2; + concurrency_ = concurrency; + return *this; + } + + /// \brief Get the number of threads that server is using + std::uint16_t concurrency() + { + return concurrency_; + } + + /// \brief Set the server's log level + /// + /// Possible values are: + /// - crow::LogLevel::Debug (0) + /// - crow::LogLevel::Info (1) + /// - crow::LogLevel::Warning (2) + /// - crow::LogLevel::Error (3) + /// - crow::LogLevel::Critical (4) + self_t& loglevel(LogLevel level) + { + crow::logger::setLogLevel(level); + return *this; + } + + /// \brief Set the response body size (in bytes) beyond which Crow automatically streams responses (Default is 1MiB) + /// + /// Any streamed response is unaffected by Crow's timer, and therefore won't timeout before a response is fully sent. + self_t& stream_threshold(size_t threshold) + { + res_stream_threshold_ = threshold; + return *this; + } + + /// \brief Get the response body size (in bytes) beyond which Crow automatically streams responses + size_t& stream_threshold() + { + return res_stream_threshold_; + } + + + self_t& register_blueprint(Blueprint& blueprint) + { + router_.register_blueprint(blueprint); + return *this; + } + + /// \brief Set the function to call to handle uncaught exceptions generated in routes (Default generates error 500). + /// + /// The function must have the following signature: void(crow::response&). + /// It must set the response passed in argument to the function, which will be sent back to the client. + /// See Router::default_exception_handler() for the default implementation. + template + self_t& exception_handler(Func&& f) + { + router_.exception_handler() = std::forward(f); + return *this; + } + + std::function& exception_handler() + { + return router_.exception_handler(); + } + + /// \brief Set a custom duration and function to run on every tick + template + self_t& tick(Duration d, Func f) + { + tick_interval_ = std::chrono::duration_cast(d); + tick_function_ = f; + return *this; + } + +#ifdef CROW_ENABLE_COMPRESSION + + self_t& use_compression(compression::algorithm algorithm) + { + comp_algorithm_ = algorithm; + compression_used_ = true; + return *this; + } + + compression::algorithm compression_algorithm() + { + return comp_algorithm_; + } + + bool compression_used() const + { + return compression_used_; + } +#endif + + /// \brief Apply blueprints + void add_blueprint() + { +#if defined(__APPLE__) || defined(__MACH__) + if (router_.blueprints().empty()) return; +#endif + + for (Blueprint* bp : router_.blueprints()) + { + if (bp->static_dir().empty()) continue; + + auto static_dir_ = crow::utility::normalize_path(bp->static_dir()); + + bp->new_rule_tagged(CROW_STATIC_ENDPOINT)([static_dir_](crow::response& res, std::string file_path_partial) { + utility::sanitize_filename(file_path_partial); + res.set_static_file_info_unsafe(static_dir_ + file_path_partial); + res.end(); + }); + } + + router_.validate_bp(); + } + + /// \brief Go through the rules, upgrade them if possible, and add them to the list of rules + void add_static_dir() + { + if (are_static_routes_added()) return; + auto static_dir_ = crow::utility::normalize_path(CROW_STATIC_DIRECTORY); + + route(CROW_STATIC_ENDPOINT)([static_dir_](crow::response& res, std::string file_path_partial) { + utility::sanitize_filename(file_path_partial); + res.set_static_file_info_unsafe(static_dir_ + file_path_partial); + res.end(); + }); + set_static_routes_added(); + } + + /// \brief A wrapper for `validate()` in the router + void validate() + { + router_.validate(); + } + + /// \brief Run the server + void run() + { +#ifndef CROW_DISABLE_STATIC_DIR + add_blueprint(); + add_static_dir(); +#endif + validate(); + +#ifdef CROW_ENABLE_SSL + if (ssl_used_) + { + ssl_server_ = std::move(std::unique_ptr(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_))); + ssl_server_->set_tick_function(tick_interval_, tick_function_); + ssl_server_->signal_clear(); + for (auto snum : signals_) + { + ssl_server_->signal_add(snum); + } + notify_server_start(); + ssl_server_->run(); + } + else +#endif + { + server_ = std::move(std::unique_ptr(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, nullptr))); + server_->set_tick_function(tick_interval_, tick_function_); + for (auto snum : signals_) + { + server_->signal_add(snum); + } + notify_server_start(); + server_->run(); + } + } + + /// \brief Non-blocking version of \ref run() + /// + /// The output from this method needs to be saved into a variable! + /// Otherwise the call will be made on the same thread. + std::future run_async() + { + return std::async(std::launch::async, [&] { + this->run(); + }); + } + + /// \brief Stop the server + void stop() + { +#ifdef CROW_ENABLE_SSL + if (ssl_used_) + { + if (ssl_server_) { ssl_server_->stop(); } + } + else +#endif + { + // TODO(EDev): Move these 6 lines to a method in http_server. + std::vector websockets_to_close = websockets_; + for (auto websocket : websockets_to_close) + { + CROW_LOG_INFO << "Quitting Websocket: " << websocket; + websocket->close("Server Application Terminated"); + } + if (server_) { server_->stop(); } + } + } + + void add_websocket(crow::websocket::connection* conn) + { + websockets_.push_back(conn); + } + + void remove_websocket(crow::websocket::connection* conn) + { + websockets_.erase(std::remove(websockets_.begin(), websockets_.end(), conn), websockets_.end()); + } + + /// \brief Print the routing paths defined for each HTTP method + void debug_print() + { + CROW_LOG_DEBUG << "Routing:"; + router_.debug_print(); + } + + +#ifdef CROW_ENABLE_SSL + + /// \brief Use certificate and key files for SSL + self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename) + { + ssl_used_ = true; + ssl_context_.set_verify_mode(asio::ssl::verify_peer); + ssl_context_.set_verify_mode(asio::ssl::verify_client_once); + ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem); + ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem); + ssl_context_.set_options( + asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3); + return *this; + } + + /// \brief Use `.pem` file for SSL + self_t& ssl_file(const std::string& pem_filename) + { + ssl_used_ = true; + ssl_context_.set_verify_mode(asio::ssl::verify_peer); + ssl_context_.set_verify_mode(asio::ssl::verify_client_once); + ssl_context_.load_verify_file(pem_filename); + ssl_context_.set_options( + asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3); + return *this; + } + + /// \brief Use certificate chain and key files for SSL + self_t& ssl_chainfile(const std::string& crt_filename, const std::string& key_filename) + { + ssl_used_ = true; + ssl_context_.set_verify_mode(asio::ssl::verify_peer); + ssl_context_.set_verify_mode(asio::ssl::verify_client_once); + ssl_context_.use_certificate_chain_file(crt_filename); + ssl_context_.use_private_key_file(key_filename, ssl_context_t::pem); + ssl_context_.set_options( + asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3); + return *this; + } + + self_t& ssl(asio::ssl::context&& ctx) + { + ssl_used_ = true; + ssl_context_ = std::move(ctx); + return *this; + } + + bool ssl_used() const + { + return ssl_used_; + } +#else + + template + self_t& ssl_file(T&&, Remain&&...) + { + // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. + static_assert( + // make static_assert dependent to T; always false + std::is_base_of::value, + "Define CROW_ENABLE_SSL to enable ssl support."); + return *this; + } + + template + self_t& ssl_chainfile(T&&, Remain&&...) + { + // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. + static_assert( + // make static_assert dependent to T; always false + std::is_base_of::value, + "Define CROW_ENABLE_SSL to enable ssl support."); + return *this; + } + + template + self_t& ssl(T&&) + { + // We can't call .ssl() member function unless CROW_ENABLE_SSL is defined. + static_assert( + // make static_assert dependent to T; always false + std::is_base_of::value, + "Define CROW_ENABLE_SSL to enable ssl support."); + return *this; + } + + bool ssl_used() const + { + return false; + } +#endif + + // middleware + using context_t = detail::context; + using mw_container_t = std::tuple; + template + typename T::context& get_context(const request& req) + { + static_assert(black_magic::contains::value, "App doesn't have the specified middleware type."); + auto& ctx = *reinterpret_cast(req.middleware_context); + return ctx.template get(); + } + + template + T& get_middleware() + { + return utility::get_element_by_type(middlewares_); + } + + /// \brief Wait until the server has properly started + void wait_for_server_start() + { + { + std::unique_lock lock(start_mutex_); + if (!server_started_) + cv_started_.wait(lock); + } + if (server_) + server_->wait_for_start(); +#ifdef CROW_ENABLE_SSL + else if (ssl_server_) + ssl_server_->wait_for_start(); +#endif + } + + private: + template + std::tuple make_middleware_tuple(Ts&&... ts) + { + auto fwd = std::forward_as_tuple((ts)...); + return std::make_tuple( + std::forward( + black_magic::tuple_extract(fwd))...); + } + + /// \brief Notify anything using \ref wait_for_server_start() to proceed + void notify_server_start() + { + std::unique_lock lock(start_mutex_); + server_started_ = true; + cv_started_.notify_all(); + } + + void set_static_routes_added() { + static_routes_added_ = true; + } + + bool are_static_routes_added() { + return static_routes_added_; + } + + private: + std::uint8_t timeout_{5}; + uint16_t port_ = 80; + uint16_t concurrency_ = 2; + uint64_t max_payload_{UINT64_MAX}; + std::string server_name_ = std::string("Crow/") + VERSION; + std::string bindaddr_ = "0.0.0.0"; + size_t res_stream_threshold_ = 1048576; + Router router_; + bool static_routes_added_{false}; + +#ifdef CROW_ENABLE_COMPRESSION + compression::algorithm comp_algorithm_; + bool compression_used_{false}; +#endif + + std::chrono::milliseconds tick_interval_; + std::function tick_function_; + + std::tuple middlewares_; + +#ifdef CROW_ENABLE_SSL + std::unique_ptr ssl_server_; + bool ssl_used_{false}; + ssl_context_t ssl_context_{asio::ssl::context::sslv23}; +#endif + + std::unique_ptr server_; + + std::vector signals_{SIGINT, SIGTERM}; + + bool server_started_{false}; + std::condition_variable cv_started_; + std::mutex start_mutex_; + std::vector websockets_; + }; + + /// \brief Alias of Crow. Useful if you want + /// a instance of an Crow application that require Middlewares + template + using App = Crow; + + /// \brief Alias of Crow<>. Useful if you want a instance of + /// an Crow application that doesn't require of Middlewares + using SimpleApp = Crow<>; +} // namespace crow + diff --git a/benchmark/crow/main.cpp b/benchmark/crow/main.cpp new file mode 100644 index 0000000000..034cf78b8a --- /dev/null +++ b/benchmark/crow/main.cpp @@ -0,0 +1,17 @@ +#include "crow_all.h" + +class CustomLogger : public crow::ILogHandler { +public: + void log(std::string, crow::LogLevel) {} +}; + +int main() { + CustomLogger logger; + crow::logger::setHandler(&logger); + + crow::SimpleApp app; + + CROW_ROUTE(app, "/")([]() { return "Hello world!"; }); + + app.port(8080).multithreaded().run(); +} diff --git a/benchmark/flask/main.py b/benchmark/flask/main.py new file mode 100644 index 0000000000..f6707fff79 --- /dev/null +++ b/benchmark/flask/main.py @@ -0,0 +1,9 @@ +from flask import Flask +app = Flask(__name__) + +import logging +logging.getLogger('werkzeug').disabled = True + +@app.route('/') +def hello_world(): + return 'Hello, World!' From 9ff3ff944663dcf52ad1ccc0de4bfa1ddd6c1f19 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:27:50 -0400 Subject: [PATCH 0246/1222] Fixed build error --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index db5861a1e9..f339475b34 100644 --- a/httplib.h +++ b/httplib.h @@ -356,7 +356,7 @@ struct case_ignore_equal { }; struct case_ignore_hash { - constexpr size_t operator()(std::string_view key) const { + constexpr size_t operator()(const std::string &key) const { return hash_core(key.data(), key.size(), 0); } From bda74db01d6a1b338cc40af111a10a693d14b66f Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:50:05 -0400 Subject: [PATCH 0247/1222] Fix fuzzing test error --- httplib.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index f339475b34..6c2a6f6483 100644 --- a/httplib.h +++ b/httplib.h @@ -342,8 +342,7 @@ inline unsigned char to_lower(int c) { 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, }; - assert(0 <= c && c < 256); - return table[c]; + return table[(unsigned char)(char)c]; } struct case_ignore_equal { From ef63f97afe2080b5f9515c1933aa4942f456662a Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 12:57:02 -0400 Subject: [PATCH 0248/1222] Release v0.17.2 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 6c2a6f6483..78029b71ba 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.17.1" +#define CPPHTTPLIB_VERSION "0.17.2" /* * Configuration From 913314f1b1756c899e369845f2bc7402b22e67cf Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 17:43:51 -0400 Subject: [PATCH 0249/1222] Fix warning --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index e2ae142eb6..f6b4b11be8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7698,7 +7698,7 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { auto dl = curl_off_t{}; const auto res = curl_easy_getinfo(curl.get(), CURLINFO_SIZE_DOWNLOAD_T, &dl); ASSERT_EQ(res, CURLE_OK); - ASSERT_EQ(dl, sizeof reject - 1); + ASSERT_EQ(dl, (curl_off_t)sizeof reject - 1); } { From 12c829f6d38bc82ba343873f24f6566e0043e1fa Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 17:44:32 -0400 Subject: [PATCH 0250/1222] Fix #1389 and #1907 --- httplib.h | 5 +++++ test/test.cc | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/httplib.h b/httplib.h index 78029b71ba..43286c4104 100644 --- a/httplib.h +++ b/httplib.h @@ -6540,6 +6540,11 @@ inline bool Server::handle_file_request(const Request &req, Response &res, auto path = entry.base_dir + sub_path; if (path.back() == '/') { path += "index.html"; } + if (detail::is_dir(path)) { + res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); + return true; + } + if (detail::is_file(path)) { for (const auto &kv : entry.headers) { res.set_header(kv.first, kv.second); diff --git a/test/test.cc b/test/test.cc index f6b4b11be8..0c3e720218 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5068,6 +5068,38 @@ TEST(MountTest, Unmount) { EXPECT_EQ(StatusCode::NotFound_404, res->status); } +TEST(MountTest, Redicect) { + Server svr; + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.set_mount_point("/", "./www"); + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + auto res = cli.Get("/dir/"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + res = cli.Get("/dir"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); + + res = cli.Get("/file"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + res = cli.Get("/file/"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::NotFound_404, res->status); +} + #ifndef CPPHTTPLIB_NO_EXCEPTIONS TEST(ExceptionTest, ThrowExceptionInHandler) { Server svr; From adf65cfe61b2b036ec8d4f8e593f5029295ad9a2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 17:44:51 -0400 Subject: [PATCH 0251/1222] Target C++11 for benchmark --- benchmark/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/Makefile b/benchmark/Makefile index f44b62298a..f9eda68ad3 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -1,4 +1,4 @@ -CXXFLAGS = -std=c++14 -O2 -I.. +CXXFLAGS = -std=c++11 -O2 -I.. THEAD_POOL_COUNT = 16 BENCH_FLAGS = -c 8 -d 5s From 953e4f3841cc46e3dc9b0608342c648ae2e0c5dc Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 17:45:09 -0400 Subject: [PATCH 0252/1222] Adjust sleep duration --- httplib.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index 43286c4104..d038325650 100644 --- a/httplib.h +++ b/httplib.h @@ -3005,7 +3005,7 @@ template inline ssize_t handle_EINTR(T fn) { while (true) { res = fn(); if (res < 0 && errno == EINTR) { - std::this_thread::sleep_for(std::chrono::milliseconds{1}); + std::this_thread::sleep_for(std::chrono::microseconds{1}); continue; } break; @@ -3225,10 +3225,10 @@ inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { return false; } else if (val == 0) { auto current = steady_clock::now(); - auto duration = duration_cast(current - start); - auto timeout = keep_alive_timeout_sec * 1000; + auto duration = duration_cast(current - start); + auto timeout = keep_alive_timeout_sec * 1000 * 1000; if (duration.count() > timeout) { return false; } - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{1}); } else { return true; } @@ -6217,7 +6217,7 @@ inline bool Server::is_running() const { return is_running_; } inline void Server::wait_until_ready() const { while (!is_running_ && !is_decommisioned) { - std::this_thread::sleep_for(std::chrono::milliseconds{1}); + std::this_thread::sleep_for(std::chrono::microseconds{1}); } } @@ -6656,7 +6656,7 @@ inline bool Server::listen_internal() { if (errno == EMFILE) { // The per-process limit of open file descriptors has been reached. // Try to accept new connections after a short sleep. - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{1}); continue; } else if (errno == EINTR || errno == EAGAIN) { continue; @@ -8718,7 +8718,7 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, auto ret = SSL_shutdown(ssl); while (ret == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::microseconds{1}); ret = SSL_shutdown(ssl); } #endif @@ -8825,7 +8825,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (SSL_pending(ssl_) > 0) { return SSL_read(ssl_, ptr, static_cast(size)); } else if (is_readable()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{1}); ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); @@ -8856,7 +8856,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { #endif if (is_writable()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{1}); ret = SSL_write(ssl_, ptr, static_cast(handle_size)); if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); From 485f8f24119c88923d691be5c5bd874451e94b51 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 17:49:12 -0400 Subject: [PATCH 0253/1222] Added one more case to MountTest.Redicect unit test. --- test/test.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test.cc b/test/test.cc index 0c3e720218..b42463408d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5098,6 +5098,11 @@ TEST(MountTest, Redicect) { res = cli.Get("/file/"); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::NotFound_404, res->status); + + cli.set_follow_location(true); + res = cli.Get("/dir"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); } #ifndef CPPHTTPLIB_NO_EXCEPTIONS From b8315278cb4c313e97f9b980d29b09d35f742a97 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 19:35:43 -0400 Subject: [PATCH 0254/1222] Add a missing file --- test/www/file | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/www/file diff --git a/test/www/file b/test/www/file new file mode 100644 index 0000000000..f73f3093ff --- /dev/null +++ b/test/www/file @@ -0,0 +1 @@ +file From c099b42ba3361bd0615db91c8afe4f8a23d22959 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 22:17:56 -0400 Subject: [PATCH 0255/1222] Removed write_format --- httplib.h | 96 +++++++++++++++++++++++++------------------------------ 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/httplib.h b/httplib.h index d038325650..7564299536 100644 --- a/httplib.h +++ b/httplib.h @@ -697,8 +697,6 @@ class Stream { virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; virtual socket_t socket() const = 0; - template - ssize_t write_format(const char *fmt, const Args &...args); ssize_t write(const char *ptr); ssize_t write(const std::string &s); }; @@ -1989,30 +1987,6 @@ inline uint64_t Response::get_header_value_u64(const std::string &key, return detail::get_header_value_u64(headers, key, def, id); } -template -inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { - const auto bufsiz = 2048; - std::array buf{}; - - auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); - if (sn <= 0) { return sn; } - - auto n = static_cast(sn); - - if (n >= buf.size() - 1) { - std::vector glowable_buf(buf.size()); - - while (n >= glowable_buf.size() - 1) { - glowable_buf.resize(glowable_buf.size() * 2); - n = static_cast( - snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...)); - } - return write(&glowable_buf[0], n); - } else { - return write(buf.data(), n); - } -} - inline void default_socket_options(socket_t sock) { int opt = 1; #ifdef _WIN32 @@ -4117,12 +4091,11 @@ inline bool read_headers(Stream &strm, Headers &headers) { // Blank line indicates end of headers. if (line_reader.size() == 1) { break; } line_terminator_len = 1; - } #else } else { continue; // Skip invalid line. - } #endif + } if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } @@ -4327,13 +4300,36 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } return ret; }); -} // namespace detail +} + +inline ssize_t write_request_line(Stream &strm, const std::string &method, + const std::string &path) { + std::string s = method; + s += " "; + s += path; + s += " HTTP/1.1\r\n"; + return strm.write(s.data(), s.size()); +} + +inline ssize_t write_response_line(Stream &strm, int status) { + std::string s = "HTTP/1.1 "; + s += std::to_string(status); + s += " "; + s += httplib::status_message(status); + s += "\r\n"; + return strm.write(s.data(), s.size()); +} inline ssize_t write_headers(Stream &strm, const Headers &headers) { ssize_t write_len = 0; for (const auto &x : headers) { - auto len = - strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); + std::string s; + s = x.first; + s += ": "; + s += x.second; + s += "\r\n"; + + auto len = strm.write(s.data(), s.size()); if (len < 0) { return len; } write_len += len; } @@ -6316,23 +6312,24 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, if (close_connection || req.get_header_value("Connection") == "close") { res.set_header("Connection", "close"); } else { - std::stringstream ss; - ss << "timeout=" << keep_alive_timeout_sec_ - << ", max=" << keep_alive_max_count_; - res.set_header("Keep-Alive", ss.str()); + std::string s = "timeout="; + s += std::to_string(keep_alive_timeout_sec_); + s += ", max="; + s += std::to_string(keep_alive_max_count_); + res.set_header("Keep-Alive", s); } - if (!res.has_header("Content-Type") && - (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { + if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) && + !res.has_header("Content-Type")) { res.set_header("Content-Type", "text/plain"); } - if (!res.has_header("Content-Length") && res.body.empty() && - !res.content_length_ && !res.content_provider_) { + if (res.body.empty() && !res.content_length_ && !res.content_provider_ && + !res.has_header("Content-Length")) { res.set_header("Content-Length", "0"); } - if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { + if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) { res.set_header("Accept-Ranges", "bytes"); } @@ -6341,12 +6338,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, // Response line and headers { detail::BufferStream bstrm; - - if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, - status_message(res.status))) { - return false; - } - + if (!detail::write_response_line(bstrm, res.status)) { return false; } if (!header_writer_(bstrm, res.headers)) { return false; } // Flush buffer @@ -7006,8 +6998,8 @@ Server::process_request(Stream &strm, bool close_connection, switch (status) { case StatusCode::Continue_100: case StatusCode::ExpectationFailed_417: - strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, - status_message(status)); + detail::write_response_line(strm, status); + strm.write("\r\n"); break; default: connection_closed = true; @@ -7617,7 +7609,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, detail::BufferStream bstrm; const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; - bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); + detail::write_request_line(bstrm, req.method, path); header_writer_(bstrm, req.headers); @@ -8718,7 +8710,7 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, auto ret = SSL_shutdown(ssl); while (ret == 0) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); + std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_shutdown(ssl); } #endif @@ -8825,7 +8817,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (SSL_pending(ssl_) > 0) { return SSL_read(ssl_, ptr, static_cast(size)); } else if (is_readable()) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); + std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); @@ -8856,7 +8848,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { #endif if (is_writable()) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); + std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_write(ssl_, ptr, static_cast(handle_size)); if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); From 969a9f99d51ea03c38bcbc8b0120b926219ccf43 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 22:54:28 -0400 Subject: [PATCH 0256/1222] Adjust sleep --- httplib.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 7564299536..48f9f8b100 100644 --- a/httplib.h +++ b/httplib.h @@ -3192,6 +3192,7 @@ class SSLSocketStream final : public Stream { inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { using namespace std::chrono; + auto timeout = keep_alive_timeout_sec * 1000; auto start = steady_clock::now(); while (true) { auto val = select_read(sock, 0, 10000); @@ -3199,10 +3200,9 @@ inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { return false; } else if (val == 0) { auto current = steady_clock::now(); - auto duration = duration_cast(current - start); - auto timeout = keep_alive_timeout_sec * 1000 * 1000; + auto duration = duration_cast(current - start); if (duration.count() > timeout) { return false; } - std::this_thread::sleep_for(std::chrono::microseconds{1}); + std::this_thread::sleep_for(std::chrono::milliseconds{10}); } else { return true; } @@ -4303,7 +4303,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } inline ssize_t write_request_line(Stream &strm, const std::string &method, - const std::string &path) { + const std::string &path) { std::string s = method; s += " "; s += path; @@ -6213,7 +6213,7 @@ inline bool Server::is_running() const { return is_running_; } inline void Server::wait_until_ready() const { while (!is_running_ && !is_decommisioned) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); + std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } From dfa641ca41fa73644c699057510626dc98e9ae34 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 22:54:48 -0400 Subject: [PATCH 0257/1222] Misc --- .gitignore | 1 + benchmark/Makefile | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 1939510319..a3a52d925a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ test/test.xcodeproj/*/xcuser* test/*.o test/*.pem test/*.srl +work/ benchmark/server benchmark/server-crow diff --git a/benchmark/Makefile b/benchmark/Makefile index f9eda68ad3..30f7175215 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -1,28 +1,36 @@ CXXFLAGS = -std=c++11 -O2 -I.. THEAD_POOL_COUNT = 16 -BENCH_FLAGS = -c 8 -d 5s + +BENCH_CMD = bombardier -c 8 -d 5s localhost:8080 +# BENCH_CMD = wrk -d 5s http://localhost:8080 # cpp-httplib bench: server - @./server & export PID=$$!; bombardier $(BENCH_FLAGS) localhost:8080; kill $${PID} - -server : cpp-httplib/main.cpp ../httplib.h - g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib/main.cpp + @./server & export PID=$$!; $(BENCH_CMD); kill $${PID} run : server @./server +server : cpp-httplib/main.cpp ../httplib.h + g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib/main.cpp + # crow +bench-crow: server-crow + @./server-crow & export PID=$$!; $(BENCH_CMD); kill $${PID} + +run-crow : server-crow + @./server-crow + server-crow : crow/main.cpp g++ -o $@ $(CXXFLAGS) crow/main.cpp -bench-crow: server-crow - @./server-crow & export PID=$$!; bombardier $(BENCH_FLAGS) localhost:8080; kill $${PID} - # flask bench-flask: - @FLASK_APP=flask/main.py flask run --port=8080 & export PID=$$!; bombardier $(BENCH_FLAGS) localhost:8080; kill $${PID} + @FLASK_APP=flask/main.py flask run --port=8080 & export PID=$$!; $(BENCH_CMD); kill $${PID} + +run-flask: + @FLASK_APP=flask/main.py flask run --port=8080 # misc bench-all: bench bench-crow bench-flask From cb74e4191bb2700ae0fb0d2a6e46cd7a6fce962e Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 5 Sep 2024 23:58:34 -0400 Subject: [PATCH 0258/1222] Performance imporovement for Keep-Alive --- httplib.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 48f9f8b100..a8bc856938 100644 --- a/httplib.h +++ b/httplib.h @@ -3191,18 +3191,36 @@ class SSLSocketStream final : public Stream { #endif inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { - using namespace std::chrono; - auto timeout = keep_alive_timeout_sec * 1000; - auto start = steady_clock::now(); + const auto timeout = keep_alive_timeout_sec * 1000; + +#ifdef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE + const auto start = std::chrono::steady_clock::now(); +#else + time_t elapse = 0; +#endif while (true) { auto val = select_read(sock, 0, 10000); + +#ifndef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE + elapse += 12; // heuristic... +#endif + if (val < 0) { return false; } else if (val == 0) { - auto current = steady_clock::now(); +#ifdef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE + auto current = std::chrono::steady_clock::now(); auto duration = duration_cast(current - start); if (duration.count() > timeout) { return false; } +#else + if (elapse > timeout) { return false; } +#endif + std::this_thread::sleep_for(std::chrono::milliseconds{10}); + +#ifndef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE + elapse += 12; // heuristic... +#endif } else { return true; } From eb6f610a45ab12aaa2e5398c8af102615179824e Mon Sep 17 00:00:00 2001 From: bgs99 Date: Fri, 6 Sep 2024 14:22:03 +0300 Subject: [PATCH 0259/1222] Fix find_package for curl (#1920) --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 75dd978cd2..f52604777d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,7 +24,7 @@ else() FetchContent_MakeAvailable(gtest) endif() -find_package(curl REQUIRED) +find_package(CURL REQUIRED) add_executable(httplib-test test.cc) target_compile_options(httplib-test PRIVATE "$<$:/utf-8;/bigobj>") From 2480c0342ca405a6fee930a7bd05e5d2cae7eeb6 Mon Sep 17 00:00:00 2001 From: laowai9189 Date: Fri, 6 Sep 2024 19:23:29 +0800 Subject: [PATCH 0260/1222] =?UTF-8?q?=E2=80=98constexpr=E2=80=99=20error?= =?UTF-8?q?=20(#1918)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit httplib.h: In member function ‘constexpr size_t httplib::detail::case_ignore_hash::operator()(const string&) const’: httplib.h:359:30: error: call to non-‘constexpr’ function ‘const _CharT* std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::data() const [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]’ 359 | return hash_core(key.data(), key.size(), 0); --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index a8bc856938..2196593ee5 100644 --- a/httplib.h +++ b/httplib.h @@ -355,7 +355,7 @@ struct case_ignore_equal { }; struct case_ignore_hash { - constexpr size_t operator()(const std::string &key) const { + size_t operator()(const std::string &key) const { return hash_core(key.data(), key.size(), 0); } From 80fb03628bb57ca9d3ab855a7feec2876249bb61 Mon Sep 17 00:00:00 2001 From: bgs99 Date: Fri, 6 Sep 2024 15:48:51 +0300 Subject: [PATCH 0261/1222] Only match path params that span full path segment (#1919) * Only match path params that span full path segment * Fix C++11 build --- httplib.h | 12 +++++++----- test/test.cc | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 2196593ee5..d65fff866c 100644 --- a/httplib.h +++ b/httplib.h @@ -840,7 +840,6 @@ class PathParamsMatcher final : public MatcherBase { bool match(Request &request) const override; private: - static constexpr char marker = ':'; // Treat segment separators as the end of path parameter capture // Does not need to handle query parameters as they are parsed before path // matching @@ -5887,6 +5886,8 @@ inline socket_t BufferStream::socket() const { return 0; } inline const std::string &BufferStream::get_buffer() const { return buffer; } inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { + static constexpr char marker[] = "/:"; + // One past the last ending position of a path param substring std::size_t last_param_end = 0; @@ -5899,13 +5900,14 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { #endif while (true) { - const auto marker_pos = pattern.find(marker, last_param_end); + const auto marker_pos = pattern.find( + marker, last_param_end == 0 ? last_param_end : last_param_end - 1); if (marker_pos == std::string::npos) { break; } static_fragments_.push_back( - pattern.substr(last_param_end, marker_pos - last_param_end)); + pattern.substr(last_param_end, marker_pos - last_param_end + 1)); - const auto param_name_start = marker_pos + 1; + const auto param_name_start = marker_pos + 2; auto sep_pos = pattern.find(separator, param_name_start); if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } @@ -5967,7 +5969,7 @@ inline bool PathParamsMatcher::match(Request &request) const { request.path_params.emplace( param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); - // Mark everythin up to '/' as matched + // Mark everything up to '/' as matched starting_pos = sep_pos + 1; } // Returns false if the path is longer than the pattern diff --git a/test/test.cc b/test/test.cc index b42463408d..5c6ceefe06 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7599,6 +7599,18 @@ TEST(PathParamsTest, SequenceOfParams) { EXPECT_EQ(request.path_params, expected_params); } +TEST(PathParamsTest, SemicolonInTheMiddleIsNotAParam) { + const auto pattern = "/prefix:suffix"; + detail::PathParamsMatcher matcher(pattern); + + Request request; + request.path = "/prefix:suffix"; + ASSERT_TRUE(matcher.match(request)); + + const std::unordered_map expected_params = {}; + EXPECT_EQ(request.path_params, expected_params); +} + TEST(UniversalClientImplTest, Ipv6LiteralAddress) { // If ipv6 regex working, regex match codepath is taken. // else port will default to 80 in Client impl From 978a4f63454d08a78944a03b2c4c07762afd297b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Sep 2024 13:58:24 -0400 Subject: [PATCH 0262/1222] Fix KeepAliveTest.SSLClientReconnectionPost problem (#1921) --- httplib.h | 44 +++----------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/httplib.h b/httplib.h index d65fff866c..cad7a56b49 100644 --- a/httplib.h +++ b/httplib.h @@ -3189,43 +3189,6 @@ class SSLSocketStream final : public Stream { }; #endif -inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { - const auto timeout = keep_alive_timeout_sec * 1000; - -#ifdef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE - const auto start = std::chrono::steady_clock::now(); -#else - time_t elapse = 0; -#endif - while (true) { - auto val = select_read(sock, 0, 10000); - -#ifndef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE - elapse += 12; // heuristic... -#endif - - if (val < 0) { - return false; - } else if (val == 0) { -#ifdef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE - auto current = std::chrono::steady_clock::now(); - auto duration = duration_cast(current - start); - if (duration.count() > timeout) { return false; } -#else - if (elapse > timeout) { return false; } -#endif - - std::this_thread::sleep_for(std::chrono::milliseconds{10}); - -#ifndef CPPHTTPLIB_USE_STEADY_TIMER_FOR_KEEP_ALIVE - elapse += 12; // heuristic... -#endif - } else { - return true; - } - } -} - template inline bool process_server_socket_core(const std::atomic &svr_sock, socket_t sock, @@ -3235,7 +3198,7 @@ process_server_socket_core(const std::atomic &svr_sock, socket_t sock, auto ret = false; auto count = keep_alive_max_count; while (svr_sock != INVALID_SOCKET && count > 0 && - keep_alive(sock, keep_alive_timeout_sec)) { + select_read(sock, keep_alive_timeout_sec, 0) > 0) { auto close_connection = count == 1; auto connection_closed = false; ret = callback(close_connection, connection_closed); @@ -4103,13 +4066,12 @@ inline bool read_headers(Stream &strm, Headers &headers) { if (line_reader.end_with_crlf()) { // Blank line indicates end of headers. if (line_reader.size() == 2) { break; } -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR } else { +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR // Blank line indicates end of headers. if (line_reader.size() == 1) { break; } line_terminator_len = 1; #else - } else { continue; // Skip invalid line. #endif } @@ -8730,7 +8692,7 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, auto ret = SSL_shutdown(ssl); while (ret == 0) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); + std::this_thread::sleep_for(std::chrono::milliseconds{100}); ret = SSL_shutdown(ssl); } #endif From 9720ef8c34cb94b77b97357f8029b2e8453bda91 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Sep 2024 19:48:25 -0400 Subject: [PATCH 0263/1222] Code cleanup --- httplib.h | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/httplib.h b/httplib.h index cad7a56b49..4e9fc20d6f 100644 --- a/httplib.h +++ b/httplib.h @@ -321,6 +321,8 @@ make_unique(std::size_t n) { return std::unique_ptr(new RT[n]); } +namespace case_ignore { + inline unsigned char to_lower(int c) { const static unsigned char table[256] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, @@ -345,28 +347,36 @@ inline unsigned char to_lower(int c) { return table[(unsigned char)(char)c]; } -struct case_ignore_equal { - bool operator()(const std::string &s1, const std::string &s2) const { - return s1.size() == s2.size() && - std::equal(s1.begin(), s1.end(), s2.begin(), [](char a, char b) { - return to_lower(a) == to_lower(b); - }); +inline bool equal(const std::string &a, const std::string &b) { + return a.size() == b.size() && + std::equal(a.begin(), a.end(), b.begin(), + [](char a, char b) { return to_lower(a) == to_lower(b); }); +} + +struct equal_to { + bool operator()(const std::string &a, const std::string &b) const { + return equal(a, b); } }; -struct case_ignore_hash { +struct hash { size_t operator()(const std::string &key) const { return hash_core(key.data(), key.size(), 0); } - constexpr size_t hash_core(const char *s, size_t l, size_t h) const { - return (l == 0) - ? h - : hash_core(s + 1, l - 1, - (h * 33) ^ static_cast(to_lower(*s))); + size_t hash_core(const char *s, size_t l, size_t h) const { + return (l == 0) ? h + : hash_core(s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no + // overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(to_lower(*s))); } }; +}; // namespace case_ignore + // This is based on // "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". @@ -473,8 +483,8 @@ enum StatusCode { }; using Headers = - std::unordered_multimap; + std::unordered_multimap; using Params = std::multimap; using Match = std::smatch; @@ -3996,14 +4006,6 @@ inline const char *get_header_value(const Headers &headers, return def; } -inline bool compare_case_ignore(const std::string &a, const std::string &b) { - if (a.size() != b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { - if (to_lower(a[i]) != to_lower(b[i])) { return false; } - } - return true; -} - template inline bool parse_header(const char *beg, const char *end, T fn) { // Skip trailing spaces and tabs. @@ -4031,7 +4033,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) { if (!key_len) { return false; } auto key = std::string(beg, key_end); - auto val = compare_case_ignore(key, "Location") + auto val = case_ignore::equal(key, "Location") ? std::string(p, end) : decode_url(std::string(p, end), false); @@ -4196,7 +4198,7 @@ inline bool read_content_chunked(Stream &strm, T &x, } inline bool is_chunked_transfer_encoding(const Headers &headers) { - return compare_case_ignore( + return case_ignore::equal( get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); } @@ -4835,7 +4837,9 @@ class MultipartFormDataParser { const std::string &b) const { if (a.size() < b.size()) { return false; } for (size_t i = 0; i < b.size(); i++) { - if (to_lower(a[i]) != to_lower(b[i])) { return false; } + if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { + return false; + } } return true; } From 61c418048d8b0e42cc55e908c1c2da1e2f617f86 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Sep 2024 19:58:02 -0400 Subject: [PATCH 0264/1222] Release v0.17.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 4e9fc20d6f..d3d1bb6363 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.17.2" +#define CPPHTTPLIB_VERSION "0.17.3" /* * Configuration From 327ff263f523533b752972968540e50c6435740f Mon Sep 17 00:00:00 2001 From: orbea Date: Fri, 6 Sep 2024 19:19:53 -0700 Subject: [PATCH 0265/1222] httplib.h: support LibreSSL (#1922) --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index d3d1bb6363..d0d0603c4c 100644 --- a/httplib.h +++ b/httplib.h @@ -273,7 +273,7 @@ using socket_t = int; #include #include -#if defined(OPENSSL_IS_BORINGSSL) +#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) #if OPENSSL_VERSION_NUMBER < 0x1010107f #error Please use OpenSSL or a current version of BoringSSL #endif @@ -786,7 +786,7 @@ class ThreadPool final : public TaskQueue { fn(); } -#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && !defined(LIBRESSL_VERSION_NUMBER) OPENSSL_thread_stop(); #endif } From 8415bf082306c9e89e14b5eeca171011cfa182d9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Sep 2024 23:51:39 -0400 Subject: [PATCH 0266/1222] Resolve #1906 --- README.md | 12 +++++++++++ httplib.h | 42 +++++++++++++++++++++++++++++++++++++ test/test.cc | 59 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 103 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 640aa2ce4a..a25f4b3fa2 100644 --- a/README.md +++ b/README.md @@ -384,6 +384,18 @@ svr.Get("/chunked", [&](const Request& req, Response& res) { }); ``` +### Send file content + +```cpp +svr.Get("/content", [&](const Request &req, Response &res) { + res.set_file_content("./path/to/conent.html"); +}); + +svr.Get("/content", [&](const Request &req, Response &res) { + res.set_file_content("./path/to/conent", "text/html"); +}); +``` + ### 'Expect: 100-continue' handler By default, the server sends a `100 Continue` response for an `Expect: 100-continue` header. diff --git a/httplib.h b/httplib.h index d0d0603c4c..c9d8552a53 100644 --- a/httplib.h +++ b/httplib.h @@ -675,6 +675,10 @@ struct Response { const std::string &content_type, ContentProviderWithoutLength provider, ContentProviderResourceReleaser resource_releaser = nullptr); + void set_file_content(const std::string &path, + const std::string &content_type); + void set_file_content(const std::string &path); + Response() = default; Response(const Response &) = default; Response &operator=(const Response &) = default; @@ -692,6 +696,8 @@ struct Response { ContentProviderResourceReleaser content_provider_resource_releaser_; bool is_chunked_content_provider_ = false; bool content_provider_success_ = false; + std::string file_content_path_; + std::string file_content_content_type_; }; class Stream { @@ -5703,6 +5709,16 @@ inline void Response::set_chunked_content_provider( is_chunked_content_provider_ = true; } +inline void Response::set_file_content(const std::string &path, + const std::string &content_type) { + file_content_path_ = path; + file_content_content_type_ = content_type; +} + +inline void Response::set_file_content(const std::string &path) { + file_content_path_ = path; +} + // Result implementation inline bool Result::has_request_header(const std::string &key) const { return request_headers_.find(key) != request_headers_.end(); @@ -7043,6 +7059,32 @@ Server::process_request(Stream &strm, bool close_connection, return write_response(strm, close_connection, req, res); } + // Serve file content by using a content provider + if (!res.file_content_path_.empty()) { + const auto &path = res.file_content_path_; + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::NotFound_404; + return write_response(strm, close_connection, req, res); + } + + auto content_type = res.file_content_content_type_; + if (content_type.empty()) { + content_type = detail::find_content_type( + path, file_extension_and_mimetype_map_, default_file_mimetype_); + } + + res.set_content_provider( + mm->size(), content_type, + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + } + return write_response_with_content(strm, close_connection, req, res); } else { if (res.status == -1) { res.status = StatusCode::NotFound_404; } diff --git a/test/test.cc b/test/test.cc index 5c6ceefe06..e3e202a749 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2300,6 +2300,18 @@ class ServerTest : public ::testing::Test { [&](const Request & /*req*/, Response &res) { res.set_content("Hello World!", "text/plain"); }) + .Get("/file_content", + [&](const Request & /*req*/, Response &res) { + res.set_file_content("./www/dir/test.html"); + }) + .Get("/file_content_with_content_type", + [&](const Request & /*req*/, Response &res) { + res.set_file_content("./www/file", "text/plain"); + }) + .Get("/invalid_file_content", + [&](const Request & /*req*/, Response &res) { + res.set_file_content("./www/dir/invalid_file_path"); + }) .Get("/http_response_splitting", [&](const Request & /*req*/, Response &res) { res.set_header("a", "1\r\nSet-Cookie: a=1"); @@ -2904,6 +2916,30 @@ TEST_F(ServerTest, GetMethod200) { EXPECT_EQ("Hello World!", res->body); } +TEST_F(ServerTest, GetFileContent) { + auto res = cli_.Get("/file_content"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("text/html", res->get_header_value("Content-Type")); + EXPECT_EQ(9, std::stoi(res->get_header_value("Content-Length"))); + EXPECT_EQ("test.html", res->body); +} + +TEST_F(ServerTest, GetFileContentWithContentType) { + auto res = cli_.Get("/file_content_with_content_type"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ(5, std::stoi(res->get_header_value("Content-Length"))); + EXPECT_EQ("file\n", res->body); +} + +TEST_F(ServerTest, GetInvalidFileContent) { + auto res = cli_.Get("/invalid_file_content"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::NotFound_404, res->status); +} + TEST_F(ServerTest, GetMethod200withPercentEncoding) { auto res = cli_.Get("/%68%69"); // auto res = cli_.Get("/hi"); ASSERT_TRUE(res); @@ -4722,9 +4758,10 @@ static void test_raw_request(const std::string &req, svr.Put("/put_hi", [&](const Request & /*req*/, Response &res) { res.set_content("ok", "text/plain"); }); - svr.Get("/header_field_value_check", [&](const Request &/*req*/, Response &res) { - res.set_content("ok", "text/plain"); - }); + svr.Get("/header_field_value_check", + [&](const Request & /*req*/, Response &res) { + res.set_content("ok", "text/plain"); + }); // Server read timeout must be longer than the client read timeout for the // bug to reproduce, probably to force the server to process a request @@ -7640,7 +7677,7 @@ TEST(FileSystemTest, FileAndDirExistenceCheck) { TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { Server svr; - svr.Get("/test", [&](const Request &/*req*/, Response &res) { + svr.Get("/test", [&](const Request & /*req*/, Response &res) { EXPECT_EQ(res.status, 400); }); @@ -7666,11 +7703,12 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { Server svr; - svr.set_expect_100_continue_handler([](const Request &/*req*/, Response &res) { - res.status = StatusCode::Unauthorized_401; - res.set_content(reject, "text/plain"); - return res.status; - }); + svr.set_expect_100_continue_handler( + [](const Request & /*req*/, Response &res) { + res.status = StatusCode::Unauthorized_401; + res.set_content(reject, "text/plain"); + return res.status; + }); svr.Post("/", [&](const Request & /*req*/, Response &res) { res.set_content(accept, "text/plain"); }); @@ -7745,7 +7783,8 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { { auto dl = curl_off_t{}; - const auto res = curl_easy_getinfo(curl.get(), CURLINFO_SIZE_DOWNLOAD_T, &dl); + const auto res = + curl_easy_getinfo(curl.get(), CURLINFO_SIZE_DOWNLOAD_T, &dl); ASSERT_EQ(res, CURLE_OK); ASSERT_EQ(dl, (curl_off_t)sizeof reject - 1); } From 01a52aa8bd5d8c3d68b6cf0110be91359502fbc1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 10:05:53 -0400 Subject: [PATCH 0267/1222] Add example/server_and_client.cc --- .gitignore | 2 ++ example/Makefile | 5 +++- example/server_and_client.cc | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 example/server_and_client.cc diff --git a/.gitignore b/.gitignore index a3a52d925a..e94e36cc29 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ example/benchmark example/redirect example/sse* example/upload +example/one_time_request +example/server_and_client example/*.pem test/httplib.cc test/httplib.h diff --git a/example/Makefile b/example/Makefile index c0739883f8..7682e10029 100644 --- a/example/Makefile +++ b/example/Makefile @@ -53,9 +53,12 @@ benchmark : benchmark.cc ../httplib.h Makefile one_time_request : one_time_request.cc ../httplib.h Makefile $(CXX) -o one_time_request $(CXXFLAGS) one_time_request.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) +server_and_client : server_and_client.cc ../httplib.h Makefile + $(CXX) -o server_and_client $(CXXFLAGS) server_and_client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) + pem: openssl genrsa 2048 > key.pem openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client *.pem diff --git a/example/server_and_client.cc b/example/server_and_client.cc new file mode 100644 index 0000000000..cc6ec0a0ba --- /dev/null +++ b/example/server_and_client.cc @@ -0,0 +1,45 @@ +// +// server_and_client.cc +// +// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// MIT License +// + +#include +#include +#include +#include + +using namespace httplib; + +const char *HOST = "localhost"; +const int PORT = 1234; + +const std::string JSON_DATA = R"({"hello": "world"})"; + +int main(void) { + Server svr; + + svr.Post("/api", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + + auto res = + cli.Post("/api", Headers(), JSON_DATA.data(), JSON_DATA.size(), + "application/json", [](uint64_t, uint64_t) { return true; }); + + if (res) { + std::cout << res->body << std::endl; + } +} \ No newline at end of file From f6e4e2d0f332fcf9e8896e4241e6e4e323643cef Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 10:15:22 -0400 Subject: [PATCH 0268/1222] Code cleanup --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index c9d8552a53..0d079611f5 100644 --- a/httplib.h +++ b/httplib.h @@ -3765,8 +3765,9 @@ inline bool can_compress_content_type(const std::string &content_type) { case "application/protobuf"_t: case "application/xhtml+xml"_t: return true; - default: - return !content_type.rfind("text/", 0) && tag != "text/event-stream"_t; + case "text/event-stream"_t: return false; + + default: return !content_type.rfind("text/", 0); } } From 3e86d93d132740e9a8c254c9ff1caacfdf6f7617 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 10:16:03 -0400 Subject: [PATCH 0269/1222] clangformat --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 0d079611f5..966595b90c 100644 --- a/httplib.h +++ b/httplib.h @@ -792,7 +792,8 @@ class ThreadPool final : public TaskQueue { fn(); } -#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && !defined(LIBRESSL_VERSION_NUMBER) +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ + !defined(LIBRESSL_VERSION_NUMBER) OPENSSL_thread_stop(); #endif } From c43c51362a888af2ec75fde7ca5ad3abf81cb18e Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 10:16:15 -0400 Subject: [PATCH 0270/1222] Add monitor tool --- benchmark/Makefile | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/benchmark/Makefile b/benchmark/Makefile index 30f7175215..b202443873 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -2,12 +2,15 @@ CXXFLAGS = -std=c++11 -O2 -I.. THEAD_POOL_COUNT = 16 -BENCH_CMD = bombardier -c 8 -d 5s localhost:8080 -# BENCH_CMD = wrk -d 5s http://localhost:8080 +BENCH = bombardier -l -c 10 -d 5s localhost:8080 +MONITOR = ali http://localhost:8080 # cpp-httplib bench: server - @./server & export PID=$$!; $(BENCH_CMD); kill $${PID} + @./server & export PID=$$!; $(BENCH); kill $${PID} + +monitor: server + @./server & export PID=$$!; $(MONITOR); kill $${PID} run : server @./server @@ -17,7 +20,10 @@ server : cpp-httplib/main.cpp ../httplib.h # crow bench-crow: server-crow - @./server-crow & export PID=$$!; $(BENCH_CMD); kill $${PID} + @./server-crow & export PID=$$!; $(BENCH); kill $${PID} + +monitor-crow: server-crow + @./server-crow & export PID=$$!; $(MONITOR); kill $${PID} run-crow : server-crow @./server-crow @@ -25,15 +31,9 @@ run-crow : server-crow server-crow : crow/main.cpp g++ -o $@ $(CXXFLAGS) crow/main.cpp -# flask -bench-flask: - @FLASK_APP=flask/main.py flask run --port=8080 & export PID=$$!; $(BENCH_CMD); kill $${PID} - -run-flask: - @FLASK_APP=flask/main.py flask run --port=8080 - # misc -bench-all: bench bench-crow bench-flask +issue: + $(BENCH) clean: rm -rf server* From c673d502b983f617e0ab0c503f4f93242891a766 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 11:11:57 -0400 Subject: [PATCH 0271/1222] Update server_and_client.cc --- example/server_and_client.cc | 79 ++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/example/server_and_client.cc b/example/server_and_client.cc index cc6ec0a0ba..66cb8b6ccc 100644 --- a/example/server_and_client.cc +++ b/example/server_and_client.cc @@ -5,41 +5,86 @@ // MIT License // +#include #include #include -#include -#include using namespace httplib; -const char *HOST = "localhost"; -const int PORT = 1234; +std::string dump_headers(const Headers &headers) { + std::string s; + char buf[BUFSIZ]; + + for (auto it = headers.begin(); it != headers.end(); ++it) { + const auto &x = *it; + snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str()); + s += buf; + } -const std::string JSON_DATA = R"({"hello": "world"})"; + return s; +} + +void logger(const Request &req, const Response &res) { + std::string s; + char buf[BUFSIZ]; + + s += "================================\n"; + + snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(), + req.version.c_str(), req.path.c_str()); + s += buf; + + std::string query; + for (auto it = req.params.begin(); it != req.params.end(); ++it) { + const auto &x = *it; + snprintf(buf, sizeof(buf), "%c%s=%s", + (it == req.params.begin()) ? '?' : '&', x.first.c_str(), + x.second.c_str()); + query += buf; + } + snprintf(buf, sizeof(buf), "%s\n", query.c_str()); + s += buf; + + s += dump_headers(req.headers); + + s += "--------------------------------\n"; + + snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str()); + s += buf; + s += dump_headers(res.headers); + s += "\n"; + + if (!res.body.empty()) { s += res.body; } + + s += "\n"; + + std::cout << s; +} int main(void) { + // Server Server svr; + svr.set_logger(logger); - svr.Post("/api", [&](const Request & /*req*/, Response &res) { - res.set_content("Hello World!", "text/plain"); + svr.Post("/post", [&](const Request & /*req*/, Response &res) { + res.set_content("POST", "text/plain"); }); - auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto th = std::thread([&]() { svr.listen("localhost", 8080); }); auto se = detail::scope_exit([&] { svr.stop(); - thread.join(); + th.join(); }); svr.wait_until_ready(); - Client cli(HOST, PORT); + // Client + Client cli{"localhost", 8080}; - auto res = - cli.Post("/api", Headers(), JSON_DATA.data(), JSON_DATA.size(), - "application/json", [](uint64_t, uint64_t) { return true; }); + std::string body = R"({"hello": "world"})"; - if (res) { - std::cout << res->body << std::endl; - } -} \ No newline at end of file + auto res = cli.Post("/post", body, "application/json"); + std::cout << "--------------------------------" << std::endl; + std::cout << to_string(res.error()) << std::endl; +} From 7fd346a2ca9e5500111d4b3884b1f5f3f423660b Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 16:07:45 -0400 Subject: [PATCH 0272/1222] Fix #1379 --- httplib.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 966595b90c..9dd4445c20 100644 --- a/httplib.h +++ b/httplib.h @@ -4184,8 +4184,12 @@ inline bool read_content_chunked(Stream &strm, T &x, assert(chunk_len == 0); - // Trailer - if (!line_reader.getline()) { return false; } + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked transfer coding is complete when a chunk with a chunk-size of zero is received, possibly followed by a trailer section, and finally terminated by an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section does't care for the existence of the final CRLF. In other words, it seems to be ok whether the final CRLF exists or not in the chunked data. https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // + // According to the reference code in RFC 9112, cpp-htpplib now allows chuncked transfer coding data without the final CRLF. + if (!line_reader.getline()) { return true; } while (strcmp(line_reader.ptr(), "\r\n") != 0) { if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } From d173a37d17b289834b08cff894404c13f50c8b66 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 16:10:26 -0400 Subject: [PATCH 0273/1222] Increased CPPHTTPLIB_READ_TIMEOUT_SECOND to 300 from 5 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 9dd4445c20..cca4a69de4 100644 --- a/httplib.h +++ b/httplib.h @@ -31,7 +31,7 @@ #endif #ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5 +#define CPPHTTPLIB_READ_TIMEOUT_SECOND 300 #endif #ifndef CPPHTTPLIB_READ_TIMEOUT_USECOND From 4c27f9c6ef5eba920b2c69e4ef7229e1c630e140 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Sep 2024 21:06:23 -0400 Subject: [PATCH 0274/1222] Made default server and client read/write timeout settings separately --- httplib.h | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index cca4a69de4..8eb2a08c80 100644 --- a/httplib.h +++ b/httplib.h @@ -30,20 +30,36 @@ #define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 #endif -#ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_READ_TIMEOUT_SECOND 300 +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND 5 #endif -#ifndef CPPHTTPLIB_READ_TIMEOUT_USECOND -#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0 +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND 0 #endif -#ifndef CPPHTTPLIB_WRITE_TIMEOUT_SECOND -#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND 5 +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND 5 #endif -#ifndef CPPHTTPLIB_WRITE_TIMEOUT_USECOND -#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0 +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 #endif #ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND @@ -1001,10 +1017,10 @@ class Server { std::atomic svr_sock_{INVALID_SOCKET}; size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND; time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; @@ -1498,10 +1514,10 @@ class ClientImpl { time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; std::string basic_auth_username_; std::string basic_auth_password_; From 3d6e315a4cffee46d3a1c8ae7dc73ee2984a1f68 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 08:38:36 -0400 Subject: [PATCH 0275/1222] Fix #1923 --- benchmark/Makefile | 2 ++ benchmark/flask/main.py | 9 --------- 2 files changed, 2 insertions(+), 9 deletions(-) delete mode 100644 benchmark/flask/main.py diff --git a/benchmark/Makefile b/benchmark/Makefile index b202443873..f517042a53 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -32,6 +32,8 @@ server-crow : crow/main.cpp g++ -o $@ $(CXXFLAGS) crow/main.cpp # misc +bench-all: bench-crow bench + issue: $(BENCH) diff --git a/benchmark/flask/main.py b/benchmark/flask/main.py deleted file mode 100644 index f6707fff79..0000000000 --- a/benchmark/flask/main.py +++ /dev/null @@ -1,9 +0,0 @@ -from flask import Flask -app = Flask(__name__) - -import logging -logging.getLogger('werkzeug').disabled = True - -@app.route('/') -def hello_world(): - return 'Hello, World!' From a79c56d06b6e269c3be2d4c82a9347b6ca04d91b Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 09:26:19 -0400 Subject: [PATCH 0276/1222] Fix #1796 --- README.md | 3 +++ httplib.h | 78 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a25f4b3fa2..0a525122ad 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,9 @@ cli.set_ca_cert_path("./ca-bundle.crt"); // Disable cert verification cli.enable_server_certificate_verification(false); + +// Disable host verification +cli.enable_server_host_verification(false); ``` > [!NOTE] diff --git a/httplib.h b/httplib.h index 8eb2a08c80..d56475acfc 100644 --- a/httplib.h +++ b/httplib.h @@ -1010,7 +1010,9 @@ class Server { std::function new_task_queue; protected: - bool process_request(Stream &strm, bool close_connection, + bool process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, bool &connection_closed, const std::function &setup_request); @@ -1448,6 +1450,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); + void enable_server_host_verification(bool enabled); #endif void set_logger(Logger logger); @@ -1562,6 +1565,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT bool server_certificate_verification_ = true; + bool server_host_verification_ = true; #endif Logger logger_; @@ -1867,6 +1871,7 @@ class Client { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); + void enable_server_host_verification(bool enabled); #endif void set_logger(Logger logger); @@ -4200,11 +4205,18 @@ inline bool read_content_chunked(Stream &strm, T &x, assert(chunk_len == 0); - // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked transfer coding is complete when a chunk with a chunk-size of zero is received, possibly followed by a trailer section, and finally terminated by an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked + // transfer coding is complete when a chunk with a chunk-size of zero is + // received, possibly followed by a trailer section, and finally terminated by + // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 // - // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section does't care for the existence of the final CRLF. In other words, it seems to be ok whether the final CRLF exists or not in the chunked data. https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section + // does't care for the existence of the final CRLF. In other words, it seems + // to be ok whether the final CRLF exists or not in the chunked data. + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 // - // According to the reference code in RFC 9112, cpp-htpplib now allows chuncked transfer coding data without the final CRLF. + // According to the reference code in RFC 9112, cpp-htpplib now allows + // chuncked transfer coding data without the final CRLF. if (!line_reader.getline()) { return true; } while (strcmp(line_reader.ptr(), "\r\n") != 0) { @@ -6942,7 +6954,9 @@ inline bool Server::dispatch_request_for_content_reader( } inline bool -Server::process_request(Stream &strm, bool close_connection, +Server::process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, bool &connection_closed, const std::function &setup_request) { std::array buf{}; @@ -6996,11 +7010,13 @@ Server::process_request(Stream &strm, bool close_connection, connection_closed = true; } - strm.get_remote_ip_and_port(req.remote_addr, req.remote_port); + req.remote_addr = remote_addr; + req.remote_port = remote_port; req.set_header("REMOTE_ADDR", req.remote_addr); req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); - strm.get_local_ip_and_port(req.local_addr, req.local_port); + req.local_addr = local_addr; + req.local_port = local_port; req.set_header("LOCAL_ADDR", req.local_addr); req.set_header("LOCAL_PORT", std::to_string(req.local_port)); @@ -7118,12 +7134,21 @@ Server::process_request(Stream &strm, bool close_connection, inline bool Server::is_valid() const { return true; } inline bool Server::process_and_close_socket(socket_t sock) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + auto ret = detail::process_server_socket( svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, - [this](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, close_connection, connection_closed, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, connection_closed, nullptr); }); @@ -7195,6 +7220,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT server_certificate_verification_ = rhs.server_certificate_verification_; + server_host_verification_ = rhs.server_host_verification_; #endif logger_ = rhs.logger_; } @@ -8699,6 +8725,10 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, inline void ClientImpl::enable_server_certificate_verification(bool enabled) { server_certificate_verification_ = enabled; } + +inline void ClientImpl::enable_server_host_verification(bool enabled) { + server_host_verification_ = enabled; +} #endif inline void ClientImpl::set_logger(Logger logger) { @@ -9030,13 +9060,22 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { auto ret = false; if (ssl) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + ret = detail::process_server_socket_ssl( svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, - [this, ssl](Stream &strm, bool close_connection, - bool &connection_closed) { - return process_request(strm, close_connection, connection_closed, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, + connection_closed, [&](Request &req) { req.ssl = ssl; }); }); @@ -9286,11 +9325,14 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return false; } - if (!verify_host(server_cert)) { - X509_free(server_cert); - error = Error::SSLServerVerification; - return false; + if (server_host_verification_) { + if (!verify_host(server_cert)) { + X509_free(server_cert); + error = Error::SSLServerVerification; + return false; + } } + X509_free(server_cert); } @@ -10022,6 +10064,10 @@ inline void Client::set_proxy_digest_auth(const std::string &username, inline void Client::enable_server_certificate_verification(bool enabled) { cli_->enable_server_certificate_verification(enabled); } + +inline void Client::enable_server_host_verification(bool enabled) { + cli_->enable_server_host_verification(enabled); +} #endif inline void Client::set_logger(Logger logger) { From 7b18ae6f16f53e8ea425ec6935464a2300b6d22b Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 10:56:13 -0400 Subject: [PATCH 0277/1222] Update benchmark --- .gitignore | 3 +- benchmark/Makefile | 23 +- benchmark/cpp-httplib-base/httplib.h | 9955 ++++++++++++++++++++++++++ benchmark/cpp-httplib-base/main.cpp | 12 + benchmark/download.sh | 2 + 5 files changed, 9991 insertions(+), 4 deletions(-) create mode 100644 benchmark/cpp-httplib-base/httplib.h create mode 100644 benchmark/cpp-httplib-base/main.cpp create mode 100755 benchmark/download.sh diff --git a/.gitignore b/.gitignore index e94e36cc29..95c588784d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,8 +24,7 @@ test/*.o test/*.pem test/*.srl work/ -benchmark/server -benchmark/server-crow +benchmark/server* *.swp diff --git a/benchmark/Makefile b/benchmark/Makefile index f517042a53..556c811c36 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -2,12 +2,14 @@ CXXFLAGS = -std=c++11 -O2 -I.. THEAD_POOL_COUNT = 16 -BENCH = bombardier -l -c 10 -d 5s localhost:8080 +BENCH = bombardier -c 10 -d 5s localhost:8080 MONITOR = ali http://localhost:8080 # cpp-httplib bench: server + @echo "--------------------\n cpp-httplib latest\n--------------------\n" @./server & export PID=$$!; $(BENCH); kill $${PID} + @echo "" monitor: server @./server & export PID=$$!; $(MONITOR); kill $${PID} @@ -18,9 +20,26 @@ run : server server : cpp-httplib/main.cpp ../httplib.h g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib/main.cpp +# cpp-httplib +bench-base: server-base + @echo "---------------------\n cpp-httplib v0.17.0\n---------------------\n" + @./server-base & export PID=$$!; $(BENCH); kill $${PID} + @echo "" + +monitor-base: server-base + @./server-base & export PID=$$!; $(MONITOR); kill $${PID} + +run-base : server-base + @./server-base + +server-base : cpp-httplib-base/main.cpp cpp-httplib-base/httplib.h + g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib-base/main.cpp + # crow bench-crow: server-crow + @echo "-------------\n Crow v1.2.0\n-------------\n" @./server-crow & export PID=$$!; $(BENCH); kill $${PID} + @echo "" monitor-crow: server-crow @./server-crow & export PID=$$!; $(MONITOR); kill $${PID} @@ -32,7 +51,7 @@ server-crow : crow/main.cpp g++ -o $@ $(CXXFLAGS) crow/main.cpp # misc -bench-all: bench-crow bench +bench-all: bench-crow bench bench-base issue: $(BENCH) diff --git a/benchmark/cpp-httplib-base/httplib.h b/benchmark/cpp-httplib-base/httplib.h new file mode 100644 index 0000000000..f038a1f2d4 --- /dev/null +++ b/benchmark/cpp-httplib-base/httplib.h @@ -0,0 +1,9955 @@ +// +// httplib.h +// +// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// MIT License +// + +#ifndef CPPHTTPLIB_HTTPLIB_H +#define CPPHTTPLIB_HTTPLIB_H + +#define CPPHTTPLIB_VERSION "0.17.0" + +/* + * Configuration + */ + +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT +#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND +#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND +#ifdef _WIN32 +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 +#else +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 +#endif +#endif + +#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH +#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_HEADER_MAX_LENGTH +#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT +#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 +#endif + +#ifndef CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT +#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT 1024 +#endif + +#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) +#endif + +#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_RANGE_MAX_COUNT +#define CPPHTTPLIB_RANGE_MAX_COUNT 1024 +#endif + +#ifndef CPPHTTPLIB_TCP_NODELAY +#define CPPHTTPLIB_TCP_NODELAY false +#endif + +#ifndef CPPHTTPLIB_IPV6_V6ONLY +#define CPPHTTPLIB_IPV6_V6ONLY false +#endif + +#ifndef CPPHTTPLIB_RECV_BUFSIZ +#define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ +#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_THREAD_POOL_COUNT +#define CPPHTTPLIB_THREAD_POOL_COUNT \ + ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ + ? std::thread::hardware_concurrency() - 1 \ + : 0)) +#endif + +#ifndef CPPHTTPLIB_RECV_FLAGS +#define CPPHTTPLIB_RECV_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_SEND_FLAGS +#define CPPHTTPLIB_SEND_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_LISTEN_BACKLOG +#define CPPHTTPLIB_LISTEN_BACKLOG 5 +#endif + +/* + * Headers + */ + +#ifdef _WIN32 +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif //_CRT_SECURE_NO_WARNINGS + +#ifndef _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_NONSTDC_NO_DEPRECATE +#endif //_CRT_NONSTDC_NO_DEPRECATE + +#if defined(_MSC_VER) +#if _MSC_VER < 1900 +#error Sorry, Visual Studio versions prior to 2015 are not supported +#endif + +#pragma comment(lib, "ws2_32.lib") + +#ifdef _WIN64 +using ssize_t = __int64; +#else +using ssize_t = long; +#endif +#endif // _MSC_VER + +#ifndef S_ISREG +#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) +#endif // S_ISREG + +#ifndef S_ISDIR +#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) +#endif // S_ISDIR + +#ifndef NOMINMAX +#define NOMINMAX +#endif // NOMINMAX + +#include +#include +#include + +#ifndef WSA_FLAG_NO_HANDLE_INHERIT +#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 +#endif + +using socket_t = SOCKET; +#ifdef CPPHTTPLIB_USE_POLL +#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) +#endif + +#else // not _WIN32 + +#include +#if !defined(_AIX) && !defined(__MVS__) +#include +#endif +#ifdef __MVS__ +#include +#ifndef NI_MAXHOST +#define NI_MAXHOST 1025 +#endif +#endif +#include +#include +#include +#ifdef __linux__ +#include +#endif +#include +#ifdef CPPHTTPLIB_USE_POLL +#include +#endif +#include +#include +#include +#include +#include +#include +#include + +using socket_t = int; +#ifndef INVALID_SOCKET +#define INVALID_SOCKET (-1) +#endif +#endif //_WIN32 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef _WIN32 +#include + +// these are defined in wincrypt.h and it breaks compilation if BoringSSL is +// used +#undef X509_NAME +#undef X509_CERT_PAIR +#undef X509_EXTENSIONS +#undef PKCS7_SIGNER_INFO + +#ifdef _MSC_VER +#pragma comment(lib, "crypt32.lib") +#endif +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#include +#if TARGET_OS_OSX +#include +#include +#endif // TARGET_OS_OSX +#endif // _WIN32 + +#include +#include +#include +#include + +#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) +#include +#endif + +#include +#include + +#if defined(OPENSSL_IS_BORINGSSL) +#if OPENSSL_VERSION_NUMBER < 0x1010107f +#error Please use OpenSSL or a current version of BoringSSL +#endif +#define SSL_get1_peer_certificate SSL_get_peer_certificate +#elif OPENSSL_VERSION_NUMBER < 0x30000000L +#error Sorry, OpenSSL versions prior to 3.0.0 are not supported +#endif + +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +#include +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +#include +#include +#endif + +/* + * Declaration + */ +namespace httplib { + +namespace detail { + +/* + * Backport std::make_unique from C++14. + * + * NOTE: This code came up with the following stackoverflow post: + * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique + * + */ + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(Args &&...args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(std::size_t n) { + typedef typename std::remove_extent::type RT; + return std::unique_ptr(new RT[n]); +} + +struct ci { + bool operator()(const std::string &s1, const std::string &s2) const { + return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), + s2.end(), + [](unsigned char c1, unsigned char c2) { + return ::tolower(c1) < ::tolower(c2); + }); + } +}; + +// This is based on +// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". + +struct scope_exit { + explicit scope_exit(std::function &&f) + : exit_function(std::move(f)), execute_on_destruction{true} {} + + scope_exit(scope_exit &&rhs) noexcept + : exit_function(std::move(rhs.exit_function)), + execute_on_destruction{rhs.execute_on_destruction} { + rhs.release(); + } + + ~scope_exit() { + if (execute_on_destruction) { this->exit_function(); } + } + + void release() { this->execute_on_destruction = false; } + +private: + scope_exit(const scope_exit &) = delete; + void operator=(const scope_exit &) = delete; + scope_exit &operator=(scope_exit &&) = delete; + + std::function exit_function; + bool execute_on_destruction; +}; + +} // namespace detail + +enum StatusCode { + // Information responses + Continue_100 = 100, + SwitchingProtocol_101 = 101, + Processing_102 = 102, + EarlyHints_103 = 103, + + // Successful responses + OK_200 = 200, + Created_201 = 201, + Accepted_202 = 202, + NonAuthoritativeInformation_203 = 203, + NoContent_204 = 204, + ResetContent_205 = 205, + PartialContent_206 = 206, + MultiStatus_207 = 207, + AlreadyReported_208 = 208, + IMUsed_226 = 226, + + // Redirection messages + MultipleChoices_300 = 300, + MovedPermanently_301 = 301, + Found_302 = 302, + SeeOther_303 = 303, + NotModified_304 = 304, + UseProxy_305 = 305, + unused_306 = 306, + TemporaryRedirect_307 = 307, + PermanentRedirect_308 = 308, + + // Client error responses + BadRequest_400 = 400, + Unauthorized_401 = 401, + PaymentRequired_402 = 402, + Forbidden_403 = 403, + NotFound_404 = 404, + MethodNotAllowed_405 = 405, + NotAcceptable_406 = 406, + ProxyAuthenticationRequired_407 = 407, + RequestTimeout_408 = 408, + Conflict_409 = 409, + Gone_410 = 410, + LengthRequired_411 = 411, + PreconditionFailed_412 = 412, + PayloadTooLarge_413 = 413, + UriTooLong_414 = 414, + UnsupportedMediaType_415 = 415, + RangeNotSatisfiable_416 = 416, + ExpectationFailed_417 = 417, + ImATeapot_418 = 418, + MisdirectedRequest_421 = 421, + UnprocessableContent_422 = 422, + Locked_423 = 423, + FailedDependency_424 = 424, + TooEarly_425 = 425, + UpgradeRequired_426 = 426, + PreconditionRequired_428 = 428, + TooManyRequests_429 = 429, + RequestHeaderFieldsTooLarge_431 = 431, + UnavailableForLegalReasons_451 = 451, + + // Server error responses + InternalServerError_500 = 500, + NotImplemented_501 = 501, + BadGateway_502 = 502, + ServiceUnavailable_503 = 503, + GatewayTimeout_504 = 504, + HttpVersionNotSupported_505 = 505, + VariantAlsoNegotiates_506 = 506, + InsufficientStorage_507 = 507, + LoopDetected_508 = 508, + NotExtended_510 = 510, + NetworkAuthenticationRequired_511 = 511, +}; + +using Headers = std::multimap; + +using Params = std::multimap; +using Match = std::smatch; + +using Progress = std::function; + +struct Response; +using ResponseHandler = std::function; + +struct MultipartFormData { + std::string name; + std::string content; + std::string filename; + std::string content_type; +}; +using MultipartFormDataItems = std::vector; +using MultipartFormDataMap = std::multimap; + +class DataSink { +public: + DataSink() : os(&sb_), sb_(*this) {} + + DataSink(const DataSink &) = delete; + DataSink &operator=(const DataSink &) = delete; + DataSink(DataSink &&) = delete; + DataSink &operator=(DataSink &&) = delete; + + std::function write; + std::function is_writable; + std::function done; + std::function done_with_trailer; + std::ostream os; + +private: + class data_sink_streambuf final : public std::streambuf { + public: + explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} + + protected: + std::streamsize xsputn(const char *s, std::streamsize n) override { + sink_.write(s, static_cast(n)); + return n; + } + + private: + DataSink &sink_; + }; + + data_sink_streambuf sb_; +}; + +using ContentProvider = + std::function; + +using ContentProviderWithoutLength = + std::function; + +using ContentProviderResourceReleaser = std::function; + +struct MultipartFormDataProvider { + std::string name; + ContentProviderWithoutLength provider; + std::string filename; + std::string content_type; +}; +using MultipartFormDataProviderItems = std::vector; + +using ContentReceiverWithProgress = + std::function; + +using ContentReceiver = + std::function; + +using MultipartContentHeader = + std::function; + +class ContentReader { +public: + using Reader = std::function; + using MultipartReader = std::function; + + ContentReader(Reader reader, MultipartReader multipart_reader) + : reader_(std::move(reader)), + multipart_reader_(std::move(multipart_reader)) {} + + bool operator()(MultipartContentHeader header, + ContentReceiver receiver) const { + return multipart_reader_(std::move(header), std::move(receiver)); + } + + bool operator()(ContentReceiver receiver) const { + return reader_(std::move(receiver)); + } + + Reader reader_; + MultipartReader multipart_reader_; +}; + +using Range = std::pair; +using Ranges = std::vector; + +struct Request { + std::string method; + std::string path; + Headers headers; + std::string body; + + std::string remote_addr; + int remote_port = -1; + std::string local_addr; + int local_port = -1; + + // for server + std::string version; + std::string target; + Params params; + MultipartFormDataMap files; + Ranges ranges; + Match matches; + std::unordered_map path_params; + + // for client + ResponseHandler response_handler; + ContentReceiverWithProgress content_receiver; + Progress progress; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + const SSL *ssl = nullptr; +#endif + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + bool has_param(const std::string &key) const; + std::string get_param_value(const std::string &key, size_t id = 0) const; + size_t get_param_value_count(const std::string &key) const; + + bool is_multipart_form_data() const; + + bool has_file(const std::string &key) const; + MultipartFormData get_file_value(const std::string &key) const; + std::vector get_file_values(const std::string &key) const; + + // private members... + size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; + size_t content_length_ = 0; + ContentProvider content_provider_; + bool is_chunked_content_provider_ = false; + size_t authorization_count_ = 0; +}; + +struct Response { + std::string version; + int status = -1; + std::string reason; + Headers headers; + std::string body; + std::string location; // Redirect location + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + void set_redirect(const std::string &url, int status = StatusCode::Found_302); + void set_content(const char *s, size_t n, const std::string &content_type); + void set_content(const std::string &s, const std::string &content_type); + void set_content(std::string &&s, const std::string &content_type); + + void set_content_provider( + size_t length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + Response() = default; + Response(const Response &) = default; + Response &operator=(const Response &) = default; + Response(Response &&) = default; + Response &operator=(Response &&) = default; + ~Response() { + if (content_provider_resource_releaser_) { + content_provider_resource_releaser_(content_provider_success_); + } + } + + // private members... + size_t content_length_ = 0; + ContentProvider content_provider_; + ContentProviderResourceReleaser content_provider_resource_releaser_; + bool is_chunked_content_provider_ = false; + bool content_provider_success_ = false; +}; + +class Stream { +public: + virtual ~Stream() = default; + + virtual bool is_readable() const = 0; + virtual bool is_writable() const = 0; + + virtual ssize_t read(char *ptr, size_t size) = 0; + virtual ssize_t write(const char *ptr, size_t size) = 0; + virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; + virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; + virtual socket_t socket() const = 0; + + template + ssize_t write_format(const char *fmt, const Args &...args); + ssize_t write(const char *ptr); + ssize_t write(const std::string &s); +}; + +class TaskQueue { +public: + TaskQueue() = default; + virtual ~TaskQueue() = default; + + virtual bool enqueue(std::function fn) = 0; + virtual void shutdown() = 0; + + virtual void on_idle() {} +}; + +class ThreadPool final : public TaskQueue { +public: + explicit ThreadPool(size_t n, size_t mqr = 0) + : shutdown_(false), max_queued_requests_(mqr) { + while (n) { + threads_.emplace_back(worker(*this)); + n--; + } + } + + ThreadPool(const ThreadPool &) = delete; + ~ThreadPool() override = default; + + bool enqueue(std::function fn) override { + { + std::unique_lock lock(mutex_); + if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { + return false; + } + jobs_.push_back(std::move(fn)); + } + + cond_.notify_one(); + return true; + } + + void shutdown() override { + // Stop all worker threads... + { + std::unique_lock lock(mutex_); + shutdown_ = true; + } + + cond_.notify_all(); + + // Join... + for (auto &t : threads_) { + t.join(); + } + } + +private: + struct worker { + explicit worker(ThreadPool &pool) : pool_(pool) {} + + void operator()() { + for (;;) { + std::function fn; + { + std::unique_lock lock(pool_.mutex_); + + pool_.cond_.wait( + lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); + + if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } + + fn = pool_.jobs_.front(); + pool_.jobs_.pop_front(); + } + + assert(true == static_cast(fn)); + fn(); + } + +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) + OPENSSL_thread_stop(); +#endif + } + + ThreadPool &pool_; + }; + friend struct worker; + + std::vector threads_; + std::list> jobs_; + + bool shutdown_; + size_t max_queued_requests_ = 0; + + std::condition_variable cond_; + std::mutex mutex_; +}; + +using Logger = std::function; + +using SocketOptions = std::function; + +void default_socket_options(socket_t sock); + +const char *status_message(int status); + +std::string get_bearer_token_auth(const Request &req); + +namespace detail { + +class MatcherBase { +public: + virtual ~MatcherBase() = default; + + // Match request path and populate its matches and + virtual bool match(Request &request) const = 0; +}; + +/** + * Captures parameters in request path and stores them in Request::path_params + * + * Capture name is a substring of a pattern from : to /. + * The rest of the pattern is matched agains the request path directly + * Parameters are captured starting from the next character after + * the end of the last matched static pattern fragment until the next /. + * + * Example pattern: + * "/path/fragments/:capture/more/fragments/:second_capture" + * Static fragments: + * "/path/fragments/", "more/fragments/" + * + * Given the following request path: + * "/path/fragments/:1/more/fragments/:2" + * the resulting capture will be + * {{"capture", "1"}, {"second_capture", "2"}} + */ +class PathParamsMatcher final : public MatcherBase { +public: + PathParamsMatcher(const std::string &pattern); + + bool match(Request &request) const override; + +private: + static constexpr char marker = ':'; + // Treat segment separators as the end of path parameter capture + // Does not need to handle query parameters as they are parsed before path + // matching + static constexpr char separator = '/'; + + // Contains static path fragments to match against, excluding the '/' after + // path params + // Fragments are separated by path params + std::vector static_fragments_; + // Stores the names of the path parameters to be used as keys in the + // Request::path_params map + std::vector param_names_; +}; + +/** + * Performs std::regex_match on request path + * and stores the result in Request::matches + * + * Note that regex match is performed directly on the whole request. + * This means that wildcard patterns may match multiple path segments with /: + * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". + */ +class RegexMatcher final : public MatcherBase { +public: + RegexMatcher(const std::string &pattern) : regex_(pattern) {} + + bool match(Request &request) const override; + +private: + std::regex regex_; +}; + +ssize_t write_headers(Stream &strm, const Headers &headers); + +} // namespace detail + +class Server { +public: + using Handler = std::function; + + using ExceptionHandler = + std::function; + + enum class HandlerResponse { + Handled, + Unhandled, + }; + using HandlerWithResponse = + std::function; + + using HandlerWithContentReader = std::function; + + using Expect100ContinueHandler = + std::function; + + Server(); + + virtual ~Server(); + + virtual bool is_valid() const; + + Server &Get(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, HandlerWithContentReader handler); + Server &Put(const std::string &pattern, Handler handler); + Server &Put(const std::string &pattern, HandlerWithContentReader handler); + Server &Patch(const std::string &pattern, Handler handler); + Server &Patch(const std::string &pattern, HandlerWithContentReader handler); + Server &Delete(const std::string &pattern, Handler handler); + Server &Delete(const std::string &pattern, HandlerWithContentReader handler); + Server &Options(const std::string &pattern, Handler handler); + + bool set_base_dir(const std::string &dir, + const std::string &mount_point = std::string()); + bool set_mount_point(const std::string &mount_point, const std::string &dir, + Headers headers = Headers()); + bool remove_mount_point(const std::string &mount_point); + Server &set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime); + Server &set_default_file_mimetype(const std::string &mime); + Server &set_file_request_handler(Handler handler); + + template + Server &set_error_handler(ErrorHandlerFunc &&handler) { + return set_error_handler_core( + std::forward(handler), + std::is_convertible{}); + } + + Server &set_exception_handler(ExceptionHandler handler); + Server &set_pre_routing_handler(HandlerWithResponse handler); + Server &set_post_routing_handler(Handler handler); + + Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); + Server &set_logger(Logger logger); + + Server &set_address_family(int family); + Server &set_tcp_nodelay(bool on); + Server &set_ipv6_v6only(bool on); + Server &set_socket_options(SocketOptions socket_options); + + Server &set_default_headers(Headers headers); + Server & + set_header_writer(std::function const &writer); + + Server &set_keep_alive_max_count(size_t count); + Server &set_keep_alive_timeout(time_t sec); + + Server &set_read_timeout(time_t sec, time_t usec = 0); + template + Server &set_read_timeout(const std::chrono::duration &duration); + + Server &set_write_timeout(time_t sec, time_t usec = 0); + template + Server &set_write_timeout(const std::chrono::duration &duration); + + Server &set_idle_interval(time_t sec, time_t usec = 0); + template + Server &set_idle_interval(const std::chrono::duration &duration); + + Server &set_payload_max_length(size_t length); + + bool bind_to_port(const std::string &host, int port, int socket_flags = 0); + int bind_to_any_port(const std::string &host, int socket_flags = 0); + bool listen_after_bind(); + + bool listen(const std::string &host, int port, int socket_flags = 0); + + bool is_running() const; + void wait_until_ready() const; + void stop(); + void decommission(); + + std::function new_task_queue; + +protected: + bool process_request(Stream &strm, bool close_connection, + bool &connection_closed, + const std::function &setup_request); + + std::atomic svr_sock_{INVALID_SOCKET}; + size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; + time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; + size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; + +private: + using Handlers = + std::vector, Handler>>; + using HandlersForContentReader = + std::vector, + HandlerWithContentReader>>; + + static std::unique_ptr + make_matcher(const std::string &pattern); + + Server &set_error_handler_core(HandlerWithResponse handler, std::true_type); + Server &set_error_handler_core(Handler handler, std::false_type); + + socket_t create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const; + int bind_internal(const std::string &host, int port, int socket_flags); + bool listen_internal(); + + bool routing(Request &req, Response &res, Stream &strm); + bool handle_file_request(const Request &req, Response &res, + bool head = false); + bool dispatch_request(Request &req, Response &res, + const Handlers &handlers) const; + bool dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const; + + bool parse_request_line(const char *s, Request &req) const; + void apply_ranges(const Request &req, Response &res, + std::string &content_type, std::string &boundary) const; + bool write_response(Stream &strm, bool close_connection, Request &req, + Response &res); + bool write_response_with_content(Stream &strm, bool close_connection, + const Request &req, Response &res); + bool write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges); + bool write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type); + bool read_content(Stream &strm, Request &req, Response &res); + bool + read_content_with_content_receiver(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver); + bool read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) const; + + virtual bool process_and_close_socket(socket_t sock); + + std::atomic is_running_{false}; + std::atomic is_decommisioned{false}; + + struct MountPointEntry { + std::string mount_point; + std::string base_dir; + Headers headers; + }; + std::vector base_dirs_; + std::map file_extension_and_mimetype_map_; + std::string default_file_mimetype_ = "application/octet-stream"; + Handler file_request_handler_; + + Handlers get_handlers_; + Handlers post_handlers_; + HandlersForContentReader post_handlers_for_content_reader_; + Handlers put_handlers_; + HandlersForContentReader put_handlers_for_content_reader_; + Handlers patch_handlers_; + HandlersForContentReader patch_handlers_for_content_reader_; + Handlers delete_handlers_; + HandlersForContentReader delete_handlers_for_content_reader_; + Handlers options_handlers_; + + HandlerWithResponse error_handler_; + ExceptionHandler exception_handler_; + HandlerWithResponse pre_routing_handler_; + Handler post_routing_handler_; + Expect100ContinueHandler expect_100_continue_handler_; + + Logger logger_; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; + SocketOptions socket_options_ = default_socket_options; + + Headers default_headers_; + std::function header_writer_ = + detail::write_headers; +}; + +enum class Error { + Success = 0, + Unknown, + Connection, + BindIPAddress, + Read, + Write, + ExceedRedirectCount, + Canceled, + SSLConnection, + SSLLoadingCerts, + SSLServerVerification, + UnsupportedMultipartBoundaryChars, + Compression, + ConnectionTimeout, + ProxyConnection, + + // For internal use only + SSLPeerCouldBeClosed_, +}; + +std::string to_string(Error error); + +std::ostream &operator<<(std::ostream &os, const Error &obj); + +class Result { +public: + Result() = default; + Result(std::unique_ptr &&res, Error err, + Headers &&request_headers = Headers{}) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)) {} + // Response + operator bool() const { return res_ != nullptr; } + bool operator==(std::nullptr_t) const { return res_ == nullptr; } + bool operator!=(std::nullptr_t) const { return res_ != nullptr; } + const Response &value() const { return *res_; } + Response &value() { return *res_; } + const Response &operator*() const { return *res_; } + Response &operator*() { return *res_; } + const Response *operator->() const { return res_.get(); } + Response *operator->() { return res_.get(); } + + // Error + Error error() const { return err_; } + + // Request Headers + bool has_request_header(const std::string &key) const; + std::string get_request_header_value(const std::string &key, + const char *def = "", + size_t id = 0) const; + uint64_t get_request_header_value_u64(const std::string &key, + uint64_t def = 0, size_t id = 0) const; + size_t get_request_header_value_count(const std::string &key) const; + +private: + std::unique_ptr res_; + Error err_ = Error::Unknown; + Headers request_headers_; +}; + +class ClientImpl { +public: + explicit ClientImpl(const std::string &host); + + explicit ClientImpl(const std::string &host, int port); + + explicit ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + virtual ~ClientImpl(); + + virtual bool is_valid() const; + + Result Get(const std::string &path); + Result Get(const std::string &path, const Headers &headers); + Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, const Headers &headers, + Progress progress); + Result Get(const std::string &path, ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const std::string &path, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ContentReceiver content_receiver, + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const Headers &headers); + Result Post(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, size_t content_length, + ContentProvider content_provider, const std::string &content_type); + Result Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Put(const std::string &path, const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + + Result Delete(const std::string &path); + Result Delete(const std::string &path, const Headers &headers); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + void stop(); + + std::string host() const; + int port() const; + + size_t is_socket_open() const; + socket_t socket() const; + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void + set_header_writer(std::function const &writer); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_ipv6_v6only(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + void set_ca_cert_store(X509_STORE *ca_cert_store); + X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); +#endif + + void set_logger(Logger logger); + +protected: + struct Socket { + socket_t sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSL *ssl = nullptr; +#endif + + bool is_open() const { return sock != INVALID_SOCKET; } + }; + + virtual bool create_and_connect_socket(Socket &socket, Error &error); + + // All of: + // shutdown_ssl + // shutdown_socket + // close_socket + // should ONLY be called when socket_mutex_ is locked. + // Also, shutdown_ssl and close_socket should also NOT be called concurrently + // with a DIFFERENT thread sending requests using that socket. + virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); + void shutdown_socket(Socket &socket) const; + void close_socket(Socket &socket); + + bool process_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + + bool write_content_with_provider(Stream &strm, const Request &req, + Error &error) const; + + void copy_settings(const ClientImpl &rhs); + + // Socket endpoint information + const std::string host_; + const int port_; + const std::string host_and_port_; + + // Current open socket + Socket socket_; + mutable std::mutex socket_mutex_; + std::recursive_mutex request_mutex_; + + // These are all protected under socket_mutex + size_t socket_requests_in_flight_ = 0; + std::thread::id socket_requests_are_from_thread_ = std::thread::id(); + bool socket_should_be_closed_when_request_is_done_ = false; + + // Hostname-IP map + std::map addr_map_; + + // Default headers + Headers default_headers_; + + // Header writer + std::function header_writer_ = + detail::write_headers; + + // Settings + std::string client_cert_path_; + std::string client_key_path_; + + time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; + time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + + std::string basic_auth_username_; + std::string basic_auth_password_; + std::string bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string digest_auth_username_; + std::string digest_auth_password_; +#endif + + bool keep_alive_ = false; + bool follow_location_ = false; + + bool url_encode_ = true; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; + SocketOptions socket_options_ = nullptr; + + bool compress_ = false; + bool decompress_ = true; + + std::string interface_; + + std::string proxy_host_; + int proxy_port_ = -1; + + std::string proxy_basic_auth_username_; + std::string proxy_basic_auth_password_; + std::string proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string proxy_digest_auth_username_; + std::string proxy_digest_auth_password_; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string ca_cert_file_path_; + std::string ca_cert_dir_path_; + + X509_STORE *ca_cert_store_ = nullptr; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool server_certificate_verification_ = true; +#endif + + Logger logger_; + +private: + bool send_(Request &req, Response &res, Error &error); + Result send_(Request &&req); + + socket_t create_client_socket(Error &error) const; + bool read_response_line(Stream &strm, const Request &req, + Response &res) const; + bool write_request(Stream &strm, Request &req, bool close_connection, + Error &error); + bool redirect(Request &req, Response &res, Error &error); + bool handle_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + std::unique_ptr send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error); + Result send_with_content_provider( + const std::string &method, const std::string &path, + const Headers &headers, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Progress progress); + ContentProviderWithoutLength get_multipart_content_provider( + const std::string &boundary, const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) const; + + std::string adjust_host_string(const std::string &host) const; + + virtual bool process_socket(const Socket &socket, + std::function callback); + virtual bool is_ssl() const; +}; + +class Client { +public: + // Universal interface + explicit Client(const std::string &scheme_host_port); + + explicit Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path); + + // HTTP only interface + explicit Client(const std::string &host, int port); + + explicit Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + Client(Client &&) = default; + Client &operator=(Client &&) = default; + + ~Client(); + + bool is_valid() const; + + Result Get(const std::string &path); + Result Get(const std::string &path, const Headers &headers); + Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, const Headers &headers, + Progress progress); + Result Get(const std::string &path, ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const std::string &path, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ContentReceiver content_receiver, + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const Headers &headers); + Result Post(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, size_t content_length, + ContentProvider content_provider, const std::string &content_type); + Result Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Put(const std::string &path, const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + + Result Delete(const std::string &path); + Result Delete(const std::string &path, const Headers &headers); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + void stop(); + + std::string host() const; + int port() const; + + size_t is_socket_open() const; + socket_t socket() const; + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void + set_header_writer(std::function const &writer); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); +#endif + + void set_logger(Logger logger); + + // SSL +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + + void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; +#endif + +private: + std::unique_ptr cli_; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool is_ssl_ = false; +#endif +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLServer : public Server { +public: + SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path = nullptr, + const char *client_ca_cert_dir_path = nullptr, + const char *private_key_password = nullptr); + + SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + + SSLServer( + const std::function &setup_ssl_ctx_callback); + + ~SSLServer() override; + + bool is_valid() const override; + + SSL_CTX *ssl_context() const; + + void update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + +private: + bool process_and_close_socket(socket_t sock) override; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; +}; + +class SSLClient final : public ClientImpl { +public: + explicit SSLClient(const std::string &host); + + explicit SSLClient(const std::string &host, int port); + + explicit SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password = std::string()); + + explicit SSLClient(const std::string &host, int port, X509 *client_cert, + EVP_PKEY *client_key, + const std::string &private_key_password = std::string()); + + ~SSLClient() override; + + bool is_valid() const override; + + void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; + +private: + bool create_and_connect_socket(Socket &socket, Error &error) override; + void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; + void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); + + bool process_socket(const Socket &socket, + std::function callback) override; + bool is_ssl() const override; + + bool connect_with_proxy(Socket &sock, Response &res, bool &success, + Error &error); + bool initialize_ssl(Socket &socket, Error &error); + + bool load_certs(); + + bool verify_host(X509 *server_cert) const; + bool verify_host_with_subject_alt_name(X509 *server_cert) const; + bool verify_host_with_common_name(X509 *server_cert) const; + bool check_host_name(const char *pattern, size_t pattern_len) const; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; + std::once_flag initialize_cert_; + + std::vector host_components_; + + long verify_result_ = 0; + + friend class ClientImpl; +}; +#endif + +/* + * Implementation of template methods. + */ + +namespace detail { + +template +inline void duration_to_sec_and_usec(const T &duration, U callback) { + auto sec = std::chrono::duration_cast(duration).count(); + auto usec = std::chrono::duration_cast( + duration - std::chrono::seconds(sec)) + .count(); + callback(static_cast(sec), static_cast(usec)); +} + +inline uint64_t get_header_value_u64(const Headers &headers, + const std::string &key, uint64_t def, + size_t id) { + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { + return std::strtoull(it->second.data(), nullptr, 10); + } + return def; +} + +} // namespace detail + +inline uint64_t Request::get_header_value_u64(const std::string &key, + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + +inline uint64_t Response::get_header_value_u64(const std::string &key, + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + +template +inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { + const auto bufsiz = 2048; + std::array buf{}; + + auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); + if (sn <= 0) { return sn; } + + auto n = static_cast(sn); + + if (n >= buf.size() - 1) { + std::vector glowable_buf(buf.size()); + + while (n >= glowable_buf.size() - 1) { + glowable_buf.resize(glowable_buf.size() * 2); + n = static_cast( + snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...)); + } + return write(&glowable_buf[0], n); + } else { + return write(buf.data(), n); + } +} + +inline void default_socket_options(socket_t sock) { + int opt = 1; +#ifdef _WIN32 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&opt), sizeof(opt)); + setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, + reinterpret_cast(&opt), sizeof(opt)); +#else +#ifdef SO_REUSEPORT + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, + reinterpret_cast(&opt), sizeof(opt)); +#else + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&opt), sizeof(opt)); +#endif +#endif +} + +inline const char *status_message(int status) { + switch (status) { + case StatusCode::Continue_100: return "Continue"; + case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; + case StatusCode::Processing_102: return "Processing"; + case StatusCode::EarlyHints_103: return "Early Hints"; + case StatusCode::OK_200: return "OK"; + case StatusCode::Created_201: return "Created"; + case StatusCode::Accepted_202: return "Accepted"; + case StatusCode::NonAuthoritativeInformation_203: + return "Non-Authoritative Information"; + case StatusCode::NoContent_204: return "No Content"; + case StatusCode::ResetContent_205: return "Reset Content"; + case StatusCode::PartialContent_206: return "Partial Content"; + case StatusCode::MultiStatus_207: return "Multi-Status"; + case StatusCode::AlreadyReported_208: return "Already Reported"; + case StatusCode::IMUsed_226: return "IM Used"; + case StatusCode::MultipleChoices_300: return "Multiple Choices"; + case StatusCode::MovedPermanently_301: return "Moved Permanently"; + case StatusCode::Found_302: return "Found"; + case StatusCode::SeeOther_303: return "See Other"; + case StatusCode::NotModified_304: return "Not Modified"; + case StatusCode::UseProxy_305: return "Use Proxy"; + case StatusCode::unused_306: return "unused"; + case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; + case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; + case StatusCode::BadRequest_400: return "Bad Request"; + case StatusCode::Unauthorized_401: return "Unauthorized"; + case StatusCode::PaymentRequired_402: return "Payment Required"; + case StatusCode::Forbidden_403: return "Forbidden"; + case StatusCode::NotFound_404: return "Not Found"; + case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; + case StatusCode::NotAcceptable_406: return "Not Acceptable"; + case StatusCode::ProxyAuthenticationRequired_407: + return "Proxy Authentication Required"; + case StatusCode::RequestTimeout_408: return "Request Timeout"; + case StatusCode::Conflict_409: return "Conflict"; + case StatusCode::Gone_410: return "Gone"; + case StatusCode::LengthRequired_411: return "Length Required"; + case StatusCode::PreconditionFailed_412: return "Precondition Failed"; + case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; + case StatusCode::UriTooLong_414: return "URI Too Long"; + case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; + case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; + case StatusCode::ExpectationFailed_417: return "Expectation Failed"; + case StatusCode::ImATeapot_418: return "I'm a teapot"; + case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; + case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; + case StatusCode::Locked_423: return "Locked"; + case StatusCode::FailedDependency_424: return "Failed Dependency"; + case StatusCode::TooEarly_425: return "Too Early"; + case StatusCode::UpgradeRequired_426: return "Upgrade Required"; + case StatusCode::PreconditionRequired_428: return "Precondition Required"; + case StatusCode::TooManyRequests_429: return "Too Many Requests"; + case StatusCode::RequestHeaderFieldsTooLarge_431: + return "Request Header Fields Too Large"; + case StatusCode::UnavailableForLegalReasons_451: + return "Unavailable For Legal Reasons"; + case StatusCode::NotImplemented_501: return "Not Implemented"; + case StatusCode::BadGateway_502: return "Bad Gateway"; + case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; + case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; + case StatusCode::HttpVersionNotSupported_505: + return "HTTP Version Not Supported"; + case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; + case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; + case StatusCode::LoopDetected_508: return "Loop Detected"; + case StatusCode::NotExtended_510: return "Not Extended"; + case StatusCode::NetworkAuthenticationRequired_511: + return "Network Authentication Required"; + + default: + case StatusCode::InternalServerError_500: return "Internal Server Error"; + } +} + +inline std::string get_bearer_token_auth(const Request &req) { + if (req.has_header("Authorization")) { + static std::string BearerHeaderPrefix = "Bearer "; + return req.get_header_value("Authorization") + .substr(BearerHeaderPrefix.length()); + } + return ""; +} + +template +inline Server & +Server::set_read_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_write_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_idle_interval(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); + return *this; +} + +inline std::string to_string(const Error error) { + switch (error) { + case Error::Success: return "Success (no error)"; + case Error::Connection: return "Could not establish connection"; + case Error::BindIPAddress: return "Failed to bind IP address"; + case Error::Read: return "Failed to read connection"; + case Error::Write: return "Failed to write connection"; + case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; + case Error::Canceled: return "Connection handling canceled"; + case Error::SSLConnection: return "SSL connection failed"; + case Error::SSLLoadingCerts: return "SSL certificate loading failed"; + case Error::SSLServerVerification: return "SSL server verification failed"; + case Error::UnsupportedMultipartBoundaryChars: + return "Unsupported HTTP multipart boundary characters"; + case Error::Compression: return "Compression failed"; + case Error::ConnectionTimeout: return "Connection timed out"; + case Error::ProxyConnection: return "Proxy connection failed"; + case Error::Unknown: return "Unknown"; + default: break; + } + + return "Invalid"; +} + +inline std::ostream &operator<<(std::ostream &os, const Error &obj) { + os << to_string(obj); + os << " (" << static_cast::type>(obj) << ')'; + return os; +} + +inline uint64_t Result::get_request_header_value_u64(const std::string &key, + uint64_t def, + size_t id) const { + return detail::get_header_value_u64(request_headers_, key, def, id); +} + +template +inline void ClientImpl::set_connection_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { + set_connection_timeout(sec, usec); + }); +} + +template +inline void ClientImpl::set_read_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); +} + +template +inline void ClientImpl::set_write_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); +} + +template +inline void Client::set_connection_timeout( + const std::chrono::duration &duration) { + cli_->set_connection_timeout(duration); +} + +template +inline void +Client::set_read_timeout(const std::chrono::duration &duration) { + cli_->set_read_timeout(duration); +} + +template +inline void +Client::set_write_timeout(const std::chrono::duration &duration) { + cli_->set_write_timeout(duration); +} + +/* + * Forward declarations and types that will be part of the .h file if split into + * .h + .cc. + */ + +std::string hosted_at(const std::string &hostname); + +void hosted_at(const std::string &hostname, std::vector &addrs); + +std::string append_query_params(const std::string &path, const Params ¶ms); + +std::pair make_range_header(const Ranges &ranges); + +std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, + bool is_proxy = false); + +namespace detail { + +std::string encode_query_param(const std::string &value); + +std::string decode_url(const std::string &s, bool convert_plus_to_space); + +void read_file(const std::string &path, std::string &out); + +std::string trim_copy(const std::string &s); + +void divide( + const char *data, std::size_t size, char d, + std::function + fn); + +void divide( + const std::string &str, char d, + std::function + fn); + +void split(const char *b, const char *e, char d, + std::function fn); + +void split(const char *b, const char *e, char d, size_t m, + std::function fn); + +bool process_client_socket(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, + std::function callback); + +socket_t create_client_socket(const std::string &host, const std::string &ip, + int port, int address_family, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + time_t connection_timeout_sec, + time_t connection_timeout_usec, + time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, + const std::string &intf, Error &error); + +const char *get_header_value(const Headers &headers, const std::string &key, + const char *def, size_t id); + +std::string params_to_query_str(const Params ¶ms); + +void parse_query_text(const char *data, std::size_t size, Params ¶ms); + +void parse_query_text(const std::string &s, Params ¶ms); + +bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary); + +bool parse_range_header(const std::string &s, Ranges &ranges); + +int close_socket(socket_t sock); + +ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); + +ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); + +enum class EncodingType { None = 0, Gzip, Brotli }; + +EncodingType encoding_type(const Request &req, const Response &res); + +class BufferStream final : public Stream { +public: + BufferStream() = default; + ~BufferStream() override = default; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + + const std::string &get_buffer() const; + +private: + std::string buffer; + size_t position = 0; +}; + +class compressor { +public: + virtual ~compressor() = default; + + typedef std::function Callback; + virtual bool compress(const char *data, size_t data_length, bool last, + Callback callback) = 0; +}; + +class decompressor { +public: + virtual ~decompressor() = default; + + virtual bool is_valid() const = 0; + + typedef std::function Callback; + virtual bool decompress(const char *data, size_t data_length, + Callback callback) = 0; +}; + +class nocompressor final : public compressor { +public: + ~nocompressor() override = default; + + bool compress(const char *data, size_t data_length, bool /*last*/, + Callback callback) override; +}; + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +class gzip_compressor final : public compressor { +public: + gzip_compressor(); + ~gzip_compressor() override; + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; + +class gzip_decompressor final : public decompressor { +public: + gzip_decompressor(); + ~gzip_decompressor() override; + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +class brotli_compressor final : public compressor { +public: + brotli_compressor(); + ~brotli_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + BrotliEncoderState *state_ = nullptr; +}; + +class brotli_decompressor final : public decompressor { +public: + brotli_decompressor(); + ~brotli_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + BrotliDecoderResult decoder_r; + BrotliDecoderState *decoder_s = nullptr; +}; +#endif + +// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` +// to store data. The call can set memory on stack for performance. +class stream_line_reader { +public: + stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size); + const char *ptr() const; + size_t size() const; + bool end_with_crlf() const; + bool getline(); + +private: + void append(char c); + + Stream &strm_; + char *fixed_buffer_; + const size_t fixed_buffer_size_; + size_t fixed_buffer_used_size_ = 0; + std::string glowable_buffer_; +}; + +class mmap { +public: + mmap(const char *path); + ~mmap(); + + bool open(const char *path); + void close(); + + bool is_open() const; + size_t size() const; + const char *data() const; + +private: +#if defined(_WIN32) + HANDLE hFile_; + HANDLE hMapping_; +#else + int fd_; +#endif + size_t size_; + void *addr_; +}; + +} // namespace detail + +// ---------------------------------------------------------------------------- + +/* + * Implementation that will be part of the .cc file if split into .h + .cc. + */ + +namespace detail { + +inline bool is_hex(char c, int &v) { + if (0x20 <= c && isdigit(c)) { + v = c - '0'; + return true; + } else if ('A' <= c && c <= 'F') { + v = c - 'A' + 10; + return true; + } else if ('a' <= c && c <= 'f') { + v = c - 'a' + 10; + return true; + } + return false; +} + +inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, + int &val) { + if (i >= s.size()) { return false; } + + val = 0; + for (; cnt; i++, cnt--) { + if (!s[i]) { return false; } + auto v = 0; + if (is_hex(s[i], v)) { + val = val * 16 + v; + } else { + return false; + } + } + return true; +} + +inline std::string from_i_to_hex(size_t n) { + static const auto charset = "0123456789abcdef"; + std::string ret; + do { + ret = charset[n & 15] + ret; + n >>= 4; + } while (n > 0); + return ret; +} + +inline size_t to_utf8(int code, char *buff) { + if (code < 0x0080) { + buff[0] = static_cast(code & 0x7F); + return 1; + } else if (code < 0x0800) { + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); + return 2; + } else if (code < 0xD800) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0xE000) { // D800 - DFFF is invalid... + return 0; + } else if (code < 0x10000) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0x110000) { + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); + return 4; + } + + // NOTREACHED + return 0; +} + +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c +inline std::string base64_encode(const std::string &in) { + static const auto lookup = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + std::string out; + out.reserve(in.size()); + + auto val = 0; + auto valb = -6; + + for (auto c : in) { + val = (val << 8) + static_cast(c); + valb += 8; + while (valb >= 0) { + out.push_back(lookup[(val >> valb) & 0x3F]); + valb -= 6; + } + } + + if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } + + while (out.size() % 4) { + out.push_back('='); + } + + return out; +} + +inline bool is_file(const std::string &path) { +#ifdef _WIN32 + return _access_s(path.c_str(), 0) == 0; +#else + struct stat st; + return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); +#endif +} + +inline bool is_dir(const std::string &path) { + struct stat st; + return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); +} + +inline bool is_valid_path(const std::string &path) { + size_t level = 0; + size_t i = 0; + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + + while (i < path.size()) { + // Read component + auto beg = i; + while (i < path.size() && path[i] != '/') { + if (path[i] == '\0') { + return false; + } else if (path[i] == '\\') { + return false; + } + i++; + } + + auto len = i - beg; + assert(len > 0); + + if (!path.compare(beg, len, ".")) { + ; + } else if (!path.compare(beg, len, "..")) { + if (level == 0) { return false; } + level--; + } else { + level++; + } + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + } + + return true; +} + +inline std::string encode_query_param(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string encode_url(const std::string &s) { + std::string result; + result.reserve(s.size()); + + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; + case '\r': result += "%0D"; break; + case '\n': result += "%0A"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + // case ':': result += "%3A"; break; // ok? probably... + case ';': result += "%3B"; break; + default: + auto c = static_cast(s[i]); + if (c >= 0x80) { + result += '%'; + char hex[4]; + auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); + assert(len == 2); + result.append(hex, static_cast(len)); + } else { + result += s[i]; + } + break; + } + } + + return result; +} + +inline std::string decode_url(const std::string &s, + bool convert_plus_to_space) { + std::string result; + + for (size_t i = 0; i < s.size(); i++) { + if (s[i] == '%' && i + 1 < s.size()) { + if (s[i + 1] == 'u') { + auto val = 0; + if (from_hex_to_i(s, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = to_utf8(val, buff); + if (len > 0) { result.append(buff, len); } + i += 5; // 'u0000' + } else { + result += s[i]; + } + } else { + auto val = 0; + if (from_hex_to_i(s, i + 1, 2, val)) { + // 2 digits hex codes + result += static_cast(val); + i += 2; // '00' + } else { + result += s[i]; + } + } + } else if (convert_plus_to_space && s[i] == '+') { + result += ' '; + } else { + result += s[i]; + } + } + + return result; +} + +inline void read_file(const std::string &path, std::string &out) { + std::ifstream fs(path, std::ios_base::binary); + fs.seekg(0, std::ios_base::end); + auto size = fs.tellg(); + fs.seekg(0); + out.resize(static_cast(size)); + fs.read(&out[0], static_cast(size)); +} + +inline std::string file_extension(const std::string &path) { + std::smatch m; + static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); + if (std::regex_search(path, m, re)) { return m[1].str(); } + return std::string(); +} + +inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } + +inline std::pair trim(const char *b, const char *e, size_t left, + size_t right) { + while (b + left < e && is_space_or_tab(b[left])) { + left++; + } + while (right > 0 && is_space_or_tab(b[right - 1])) { + right--; + } + return std::make_pair(left, right); +} + +inline std::string trim_copy(const std::string &s) { + auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); + return s.substr(r.first, r.second - r.first); +} + +inline std::string trim_double_quotes_copy(const std::string &s) { + if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { + return s.substr(1, s.size() - 2); + } + return s; +} + +inline void +divide(const char *data, std::size_t size, char d, + std::function + fn) { + const auto it = std::find(data, data + size, d); + const auto found = static_cast(it != data + size); + const auto lhs_data = data; + const auto lhs_size = static_cast(it - data); + const auto rhs_data = it + found; + const auto rhs_size = size - lhs_size - found; + + fn(lhs_data, lhs_size, rhs_data, rhs_size); +} + +inline void +divide(const std::string &str, char d, + std::function + fn) { + divide(str.data(), str.size(), d, std::move(fn)); +} + +inline void split(const char *b, const char *e, char d, + std::function fn) { + return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); +} + +inline void split(const char *b, const char *e, char d, size_t m, + std::function fn) { + size_t i = 0; + size_t beg = 0; + size_t count = 1; + + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d && count < m) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + beg = i + 1; + count++; + } + i++; + } + + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + } +} + +inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size) + : strm_(strm), fixed_buffer_(fixed_buffer), + fixed_buffer_size_(fixed_buffer_size) {} + +inline const char *stream_line_reader::ptr() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_; + } else { + return glowable_buffer_.data(); + } +} + +inline size_t stream_line_reader::size() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_used_size_; + } else { + return glowable_buffer_.size(); + } +} + +inline bool stream_line_reader::end_with_crlf() const { + auto end = ptr() + size(); + return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; +} + +inline bool stream_line_reader::getline() { + fixed_buffer_used_size_ = 0; + glowable_buffer_.clear(); + + for (size_t i = 0;; i++) { + char byte; + auto n = strm_.read(&byte, 1); + + if (n < 0) { + return false; + } else if (n == 0) { + if (i == 0) { + return false; + } else { + break; + } + } + + append(byte); + + if (byte == '\n') { break; } + } + + return true; +} + +inline void stream_line_reader::append(char c) { + if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { + fixed_buffer_[fixed_buffer_used_size_++] = c; + fixed_buffer_[fixed_buffer_used_size_] = '\0'; + } else { + if (glowable_buffer_.empty()) { + assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); + glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); + } + glowable_buffer_ += c; + } +} + +inline mmap::mmap(const char *path) +#if defined(_WIN32) + : hFile_(NULL), hMapping_(NULL) +#else + : fd_(-1) +#endif + , + size_(0), addr_(nullptr) { + open(path); +} + +inline mmap::~mmap() { close(); } + +inline bool mmap::open(const char *path) { + close(); + +#if defined(_WIN32) + std::wstring wpath; + for (size_t i = 0; i < strlen(path); i++) { + wpath += path[i]; + } + +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ + WINAPI_PARTITION_GAMES) && \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN8) + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); +#else + hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); +#endif + + if (hFile_ == INVALID_HANDLE_VALUE) { return false; } + +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ + WINAPI_PARTITION_GAMES) + LARGE_INTEGER size{}; + if (!::GetFileSizeEx(hFile_, &size)) { return false; } + size_ = static_cast(size.QuadPart); +#else + DWORD sizeHigh; + DWORD sizeLow; + sizeLow = ::GetFileSize(hFile_, &sizeHigh); + if (sizeLow == INVALID_FILE_SIZE) { return false; } + size_ = (static_cast(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow; +#endif + +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN8) + hMapping_ = + ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); +#else + hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart, + size.LowPart, NULL); +#endif + + if (hMapping_ == NULL) { + close(); + return false; + } + +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ + (_WIN32_WINNT >= _WIN32_WINNT_WIN8) + addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); +#else + addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); +#endif +#else + fd_ = ::open(path, O_RDONLY); + if (fd_ == -1) { return false; } + + struct stat sb; + if (fstat(fd_, &sb) == -1) { + close(); + return false; + } + size_ = static_cast(sb.st_size); + + addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); +#endif + + if (addr_ == nullptr) { + close(); + return false; + } + + return true; +} + +inline bool mmap::is_open() const { return addr_ != nullptr; } + +inline size_t mmap::size() const { return size_; } + +inline const char *mmap::data() const { + return static_cast(addr_); +} + +inline void mmap::close() { +#if defined(_WIN32) + if (addr_) { + ::UnmapViewOfFile(addr_); + addr_ = nullptr; + } + + if (hMapping_) { + ::CloseHandle(hMapping_); + hMapping_ = NULL; + } + + if (hFile_ != INVALID_HANDLE_VALUE) { + ::CloseHandle(hFile_); + hFile_ = INVALID_HANDLE_VALUE; + } +#else + if (addr_ != nullptr) { + munmap(addr_, size_); + addr_ = nullptr; + } + + if (fd_ != -1) { + ::close(fd_); + fd_ = -1; + } +#endif + size_ = 0; +} +inline int close_socket(socket_t sock) { +#ifdef _WIN32 + return closesocket(sock); +#else + return close(sock); +#endif +} + +template inline ssize_t handle_EINTR(T fn) { + ssize_t res = 0; + while (true) { + res = fn(); + if (res < 0 && errno == EINTR) { + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + continue; + } + break; + } + return res; +} + +inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { + return handle_EINTR([&]() { + return recv(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, + int flags) { + return handle_EINTR([&]() { + return send(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return -1; } +#endif + + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); + }); +#endif +} + +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLOUT; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return -1; } +#endif + + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), nullptr, &fds, nullptr, &tv); + }); +#endif +} + +inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, + time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN | POLLOUT; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); + + if (poll_res == 0) { return Error::ConnectionTimeout; } + + if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + + return Error::Connection; +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return Error::Connection; } +#endif + + fd_set fdsr; + FD_ZERO(&fdsr); + FD_SET(sock, &fdsr); + + auto fdsw = fdsr; + auto fdse = fdsr; + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); + }); + + if (ret == 0) { return Error::ConnectionTimeout; } + + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + return Error::Connection; +#endif +} + +inline bool is_socket_alive(socket_t sock) { + const auto val = detail::select_read(sock, 0, 0); + if (val == 0) { + return true; + } else if (val < 0 && errno == EBADF) { + return false; + } + char buf[1]; + return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; +} + +class SocketStream final : public Stream { +public: + SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec); + ~SocketStream() override; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + +private: + socket_t sock_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + + std::vector read_buff_; + size_t read_buff_off_ = 0; + size_t read_buff_content_size_ = 0; + + static const size_t read_buff_size_ = 1024l * 4; +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLSocketStream final : public Stream { +public: + SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec); + ~SSLSocketStream() override; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + +private: + socket_t sock_; + SSL *ssl_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; +}; +#endif + +inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { + using namespace std::chrono; + auto start = steady_clock::now(); + while (true) { + auto val = select_read(sock, 0, 10000); + if (val < 0) { + return false; + } else if (val == 0) { + auto current = steady_clock::now(); + auto duration = duration_cast(current - start); + auto timeout = keep_alive_timeout_sec * 1000; + if (duration.count() > timeout) { return false; } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } else { + return true; + } + } +} + +template +inline bool +process_server_socket_core(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, T callback) { + assert(keep_alive_max_count > 0); + auto ret = false; + auto count = keep_alive_max_count; + while (svr_sock != INVALID_SOCKET && count > 0 && + keep_alive(sock, keep_alive_timeout_sec)) { + auto close_connection = count == 1; + auto connection_closed = false; + ret = callback(close_connection, connection_closed); + if (!ret || connection_closed) { break; } + count--; + } + return ret; +} + +template +inline bool +process_server_socket(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +inline bool process_client_socket(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, + std::function callback) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm); +} + +inline int shutdown_socket(socket_t sock) { +#ifdef _WIN32 + return shutdown(sock, SD_BOTH); +#else + return shutdown(sock, SHUT_RDWR); +#endif +} + +template +socket_t create_socket(const std::string &host, const std::string &ip, int port, + int address_family, int socket_flags, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + BindOrConnect bind_or_connect) { + // Get address info + const char *node = nullptr; + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (!ip.empty()) { + node = ip.c_str(); + // Ask getaddrinfo to convert IP in c-string to address + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + } else { + if (!host.empty()) { node = host.c_str(); } + hints.ai_family = address_family; + hints.ai_flags = socket_flags; + } + +#ifndef _WIN32 + if (hints.ai_family == AF_UNIX) { + const auto addrlen = host.length(); + if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } + +#ifdef SOCK_CLOEXEC + auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, + hints.ai_protocol); +#else + auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); +#endif + + if (sock != INVALID_SOCKET) { + sockaddr_un addr{}; + addr.sun_family = AF_UNIX; + std::copy(host.begin(), host.end(), addr.sun_path); + + hints.ai_addr = reinterpret_cast(&addr); + hints.ai_addrlen = static_cast( + sizeof(addr) - sizeof(addr.sun_path) + addrlen); + +#ifndef SOCK_CLOEXEC + fcntl(sock, F_SETFD, FD_CLOEXEC); +#endif + + if (socket_options) { socket_options(sock); } + + bool dummy; + if (!bind_or_connect(sock, hints, dummy)) { + close_socket(sock); + sock = INVALID_SOCKET; + } + } + return sock; + } +#endif + + auto service = std::to_string(port); + + if (getaddrinfo(node, service.c_str(), &hints, &result)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return INVALID_SOCKET; + } + + for (auto rp = result; rp; rp = rp->ai_next) { + // Create a socket +#ifdef _WIN32 + auto sock = + WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, + WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); + /** + * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 + * and above the socket creation fails on older Windows Systems. + * + * Let's try to create a socket the old way in this case. + * + * Reference: + * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa + * + * WSA_FLAG_NO_HANDLE_INHERIT: + * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with + * SP1, and later + * + */ + if (sock == INVALID_SOCKET) { + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + } +#else + +#ifdef SOCK_CLOEXEC + auto sock = + socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); +#else + auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); +#endif + +#endif + if (sock == INVALID_SOCKET) { continue; } + +#if !defined _WIN32 && !defined SOCK_CLOEXEC + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { + close_socket(sock); + continue; + } +#endif + + if (tcp_nodelay) { + auto opt = 1; +#ifdef _WIN32 + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, + reinterpret_cast(&opt), sizeof(opt)); +#else + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, + reinterpret_cast(&opt), sizeof(opt)); +#endif + } + + if (rp->ai_family == AF_INET6) { + auto opt = ipv6_v6only ? 1 : 0; +#ifdef _WIN32 + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&opt), sizeof(opt)); +#else + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&opt), sizeof(opt)); +#endif + } + + if (socket_options) { socket_options(sock); } + + // bind or connect + auto quit = false; + if (bind_or_connect(sock, *rp, quit)) { + freeaddrinfo(result); + return sock; + } + + close_socket(sock); + + if (quit) { break; } + } + + freeaddrinfo(result); + return INVALID_SOCKET; +} + +inline void set_nonblocking(socket_t sock, bool nonblocking) { +#ifdef _WIN32 + auto flags = nonblocking ? 1UL : 0UL; + ioctlsocket(sock, FIONBIO, &flags); +#else + auto flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, + nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); +#endif +} + +inline bool is_connection_error() { +#ifdef _WIN32 + return WSAGetLastError() != WSAEWOULDBLOCK; +#else + return errno != EINPROGRESS; +#endif +} + +inline bool bind_ip_address(socket_t sock, const std::string &host) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + + auto ret = false; + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &ai = *rp; + if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + ret = true; + break; + } + } + + freeaddrinfo(result); + return ret; +} + +#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ +#define USE_IF2IP +#endif + +#ifdef USE_IF2IP +inline std::string if2ip(int address_family, const std::string &ifn) { + struct ifaddrs *ifap; + getifaddrs(&ifap); + std::string addr_candidate; + for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && ifn == ifa->ifa_name && + (AF_UNSPEC == address_family || + ifa->ifa_addr->sa_family == address_family)) { + if (ifa->ifa_addr->sa_family == AF_INET) { + auto sa = reinterpret_cast(ifa->ifa_addr); + char buf[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { + freeifaddrs(ifap); + return std::string(buf, INET_ADDRSTRLEN); + } + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto sa = reinterpret_cast(ifa->ifa_addr); + if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { + char buf[INET6_ADDRSTRLEN] = {}; + if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { + // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL + auto s6_addr_head = sa->sin6_addr.s6_addr[0]; + if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { + addr_candidate = std::string(buf, INET6_ADDRSTRLEN); + } else { + freeifaddrs(ifap); + return std::string(buf, INET6_ADDRSTRLEN); + } + } + } + } + } + } + freeifaddrs(ifap); + return addr_candidate; +} +#endif + +inline socket_t create_client_socket( + const std::string &host, const std::string &ip, int port, + int address_family, bool tcp_nodelay, bool ipv6_v6only, + SocketOptions socket_options, time_t connection_timeout_sec, + time_t connection_timeout_usec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, const std::string &intf, Error &error) { + auto sock = create_socket( + host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only, + std::move(socket_options), + [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { + if (!intf.empty()) { +#ifdef USE_IF2IP + auto ip_from_if = if2ip(address_family, intf); + if (ip_from_if.empty()) { ip_from_if = intf; } + if (!bind_ip_address(sock2, ip_from_if)) { + error = Error::BindIPAddress; + return false; + } +#endif + } + + set_nonblocking(sock2, true); + + auto ret = + ::connect(sock2, ai.ai_addr, static_cast(ai.ai_addrlen)); + + if (ret < 0) { + if (is_connection_error()) { + error = Error::Connection; + return false; + } + error = wait_until_socket_is_ready(sock2, connection_timeout_sec, + connection_timeout_usec); + if (error != Error::Success) { + if (error == Error::ConnectionTimeout) { quit = true; } + return false; + } + } + + set_nonblocking(sock2, false); + + { +#ifdef _WIN32 + auto timeout = static_cast(read_timeout_sec * 1000 + + read_timeout_usec / 1000); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec); + tv.tv_usec = static_cast(read_timeout_usec); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + { + +#ifdef _WIN32 + auto timeout = static_cast(write_timeout_sec * 1000 + + write_timeout_usec / 1000); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec); + tv.tv_usec = static_cast(write_timeout_usec); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + + error = Error::Success; + return true; + }); + + if (sock != INVALID_SOCKET) { + error = Error::Success; + } else { + if (error == Error::Success) { error = Error::Connection; } + } + + return sock; +} + +inline bool get_ip_and_port(const struct sockaddr_storage &addr, + socklen_t addr_len, std::string &ip, int &port) { + if (addr.ss_family == AF_INET) { + port = ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + port = + ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return false; + } + + std::array ipstr{}; + if (getnameinfo(reinterpret_cast(&addr), addr_len, + ipstr.data(), static_cast(ipstr.size()), nullptr, + 0, NI_NUMERICHOST)) { + return false; + } + + ip = ipstr.data(); + return true; +} + +inline void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (!getsockname(sock, reinterpret_cast(&addr), + &addr_len)) { + get_ip_and_port(addr, addr_len, ip, port); + } +} + +inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + + if (!getpeername(sock, reinterpret_cast(&addr), + &addr_len)) { +#ifndef _WIN32 + if (addr.ss_family == AF_UNIX) { +#if defined(__linux__) + struct ucred ucred; + socklen_t len = sizeof(ucred); + if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { + port = ucred.pid; + } +#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ + pid_t pid; + socklen_t len = sizeof(pid); + if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { + port = pid; + } +#endif + return; + } +#endif + get_ip_and_port(addr, addr_len, ip, port); + } +} + +inline constexpr unsigned int str2tag_core(const char *s, size_t l, + unsigned int h) { + return (l == 0) + ? h + : str2tag_core( + s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(*s)); +} + +inline unsigned int str2tag(const std::string &s) { + return str2tag_core(s.data(), s.size(), 0); +} + +namespace udl { + +inline constexpr unsigned int operator"" _t(const char *s, size_t l) { + return str2tag_core(s, l, 0); +} + +} // namespace udl + +inline std::string +find_content_type(const std::string &path, + const std::map &user_data, + const std::string &default_content_type) { + auto ext = file_extension(path); + + auto it = user_data.find(ext); + if (it != user_data.end()) { return it->second; } + + using udl::operator""_t; + + switch (str2tag(ext)) { + default: return default_content_type; + + case "css"_t: return "text/css"; + case "csv"_t: return "text/csv"; + case "htm"_t: + case "html"_t: return "text/html"; + case "js"_t: + case "mjs"_t: return "text/javascript"; + case "txt"_t: return "text/plain"; + case "vtt"_t: return "text/vtt"; + + case "apng"_t: return "image/apng"; + case "avif"_t: return "image/avif"; + case "bmp"_t: return "image/bmp"; + case "gif"_t: return "image/gif"; + case "png"_t: return "image/png"; + case "svg"_t: return "image/svg+xml"; + case "webp"_t: return "image/webp"; + case "ico"_t: return "image/x-icon"; + case "tif"_t: return "image/tiff"; + case "tiff"_t: return "image/tiff"; + case "jpg"_t: + case "jpeg"_t: return "image/jpeg"; + + case "mp4"_t: return "video/mp4"; + case "mpeg"_t: return "video/mpeg"; + case "webm"_t: return "video/webm"; + + case "mp3"_t: return "audio/mp3"; + case "mpga"_t: return "audio/mpeg"; + case "weba"_t: return "audio/webm"; + case "wav"_t: return "audio/wave"; + + case "otf"_t: return "font/otf"; + case "ttf"_t: return "font/ttf"; + case "woff"_t: return "font/woff"; + case "woff2"_t: return "font/woff2"; + + case "7z"_t: return "application/x-7z-compressed"; + case "atom"_t: return "application/atom+xml"; + case "pdf"_t: return "application/pdf"; + case "json"_t: return "application/json"; + case "rss"_t: return "application/rss+xml"; + case "tar"_t: return "application/x-tar"; + case "xht"_t: + case "xhtml"_t: return "application/xhtml+xml"; + case "xslt"_t: return "application/xslt+xml"; + case "xml"_t: return "application/xml"; + case "gz"_t: return "application/gzip"; + case "zip"_t: return "application/zip"; + case "wasm"_t: return "application/wasm"; + } +} + +inline bool can_compress_content_type(const std::string &content_type) { + using udl::operator""_t; + + auto tag = str2tag(content_type); + + switch (tag) { + case "image/svg+xml"_t: + case "application/javascript"_t: + case "application/json"_t: + case "application/xml"_t: + case "application/protobuf"_t: + case "application/xhtml+xml"_t: return true; + + default: + return !content_type.rfind("text/", 0) && tag != "text/event-stream"_t; + } +} + +inline EncodingType encoding_type(const Request &req, const Response &res) { + auto ret = + detail::can_compress_content_type(res.get_header_value("Content-Type")); + if (!ret) { return EncodingType::None; } + + const auto &s = req.get_header_value("Accept-Encoding"); + (void)(s); + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + // TODO: 'Accept-Encoding' has br, not br;q=0 + ret = s.find("br") != std::string::npos; + if (ret) { return EncodingType::Brotli; } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 + ret = s.find("gzip") != std::string::npos; + if (ret) { return EncodingType::Gzip; } +#endif + + return EncodingType::None; +} + +inline bool nocompressor::compress(const char *data, size_t data_length, + bool /*last*/, Callback callback) { + if (!data_length) { return true; } + return callback(data, data_length); +} + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +inline gzip_compressor::gzip_compressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, + Z_DEFAULT_STRATEGY) == Z_OK; +} + +inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } + +inline bool gzip_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + assert(is_valid_); + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; + auto ret = Z_OK; + + std::array buff{}; + do { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = deflate(&strm_, flush); + if (ret == Z_STREAM_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } while (strm_.avail_out == 0); + + assert((flush == Z_FINISH && ret == Z_STREAM_END) || + (flush == Z_NO_FLUSH && ret == Z_OK)); + assert(strm_.avail_in == 0); + } while (data_length > 0); + + return true; +} + +inline gzip_decompressor::gzip_decompressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + // 15 is the value of wbits, which should be at the maximum possible value + // to ensure that any gzip stream can be decoded. The offset of 32 specifies + // that the stream type should be automatically detected either gzip or + // deflate. + is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; +} + +inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } + +inline bool gzip_decompressor::is_valid() const { return is_valid_; } + +inline bool gzip_decompressor::decompress(const char *data, size_t data_length, + Callback callback) { + assert(is_valid_); + + auto ret = Z_OK; + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + std::array buff{}; + while (strm_.avail_in > 0 && ret == Z_OK) { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = inflate(&strm_, Z_NO_FLUSH); + + assert(ret != Z_STREAM_ERROR); + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: inflateEnd(&strm_); return false; + } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } + + if (ret != Z_OK && ret != Z_STREAM_END) { return false; } + + } while (data_length > 0); + + return true; +} +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +inline brotli_compressor::brotli_compressor() { + state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); +} + +inline brotli_compressor::~brotli_compressor() { + BrotliEncoderDestroyInstance(state_); +} + +inline bool brotli_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + std::array buff{}; + + auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; + auto available_in = data_length; + auto next_in = reinterpret_cast(data); + + for (;;) { + if (last) { + if (BrotliEncoderIsFinished(state_)) { break; } + } else { + if (!available_in) { break; } + } + + auto available_out = buff.size(); + auto next_out = buff.data(); + + if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in, + &available_out, &next_out, nullptr)) { + return false; + } + + auto output_bytes = buff.size() - available_out; + if (output_bytes) { + callback(reinterpret_cast(buff.data()), output_bytes); + } + } + + return true; +} + +inline brotli_decompressor::brotli_decompressor() { + decoder_s = BrotliDecoderCreateInstance(0, 0, 0); + decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT + : BROTLI_DECODER_RESULT_ERROR; +} + +inline brotli_decompressor::~brotli_decompressor() { + if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } +} + +inline bool brotli_decompressor::is_valid() const { return decoder_s; } + +inline bool brotli_decompressor::decompress(const char *data, + size_t data_length, + Callback callback) { + if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_ERROR) { + return 0; + } + + auto next_in = reinterpret_cast(data); + size_t avail_in = data_length; + size_t total_out; + + decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; + + std::array buff{}; + while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { + char *next_out = buff.data(); + size_t avail_out = buff.size(); + + decoder_r = BrotliDecoderDecompressStream( + decoder_s, &avail_in, &next_in, &avail_out, + reinterpret_cast(&next_out), &total_out); + + if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - avail_out)) { return false; } + } + + return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; +} +#endif + +inline bool has_header(const Headers &headers, const std::string &key) { + return headers.find(key) != headers.end(); +} + +inline const char *get_header_value(const Headers &headers, + const std::string &key, const char *def, + size_t id) { + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.c_str(); } + return def; +} + +inline bool compare_case_ignore(const std::string &a, const std::string &b) { + if (a.size() != b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + } + return true; +} + +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; + } + + auto p = beg; + while (p < end && *p != ':') { + p++; + } + + if (p == end) { return false; } + + auto key_end = p; + + if (*p++ != ':') { return false; } + + while (p < end && is_space_or_tab(*p)) { + p++; + } + + if (p < end) { + auto key_len = key_end - beg; + if (!key_len) { return false; } + + auto key = std::string(beg, key_end); + auto val = compare_case_ignore(key, "Location") + ? std::string(p, end) + : decode_url(std::string(p, end), false); + fn(key, val); + return true; + } + + return false; +} + +inline bool read_headers(Stream &strm, Headers &headers) { + const auto bufsiz = 2048; + char buf[bufsiz]; + stream_line_reader line_reader(strm, buf, bufsiz); + + for (;;) { + if (!line_reader.getline()) { return false; } + + // Check if the line ends with CRLF. + auto line_terminator_len = 2; + if (line_reader.end_with_crlf()) { + // Blank line indicates end of headers. + if (line_reader.size() == 2) { break; } +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + } else { + // Blank line indicates end of headers. + if (line_reader.size() == 1) { break; } + line_terminator_len = 1; + } +#else + } else { + continue; // Skip invalid line. + } +#endif + + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Exclude line terminator + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + parse_header(line_reader.ptr(), end, + [&](const std::string &key, const std::string &val) { + headers.emplace(key, val); + }); + } + + return true; +} + +inline bool read_content_with_length(Stream &strm, uint64_t len, + Progress progress, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + + uint64_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return false; } + + if (!out(buf, static_cast(n), r, len)) { return false; } + r += static_cast(n); + + if (progress) { + if (!progress(r, len)) { return false; } + } + } + + return true; +} + +inline void skip_content_with_length(Stream &strm, uint64_t len) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + uint64_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return; } + r += static_cast(n); + } +} + +inline bool read_content_without_length(Stream &strm, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + uint64_t r = 0; + for (;;) { + auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); + if (n <= 0) { return true; } + + if (!out(buf, static_cast(n), r, 0)) { return false; } + r += static_cast(n); + } + + return true; +} + +template +inline bool read_content_chunked(Stream &strm, T &x, + ContentReceiverWithProgress out) { + const auto bufsiz = 16; + char buf[bufsiz]; + + stream_line_reader line_reader(strm, buf, bufsiz); + + if (!line_reader.getline()) { return false; } + + unsigned long chunk_len; + while (true) { + char *end_ptr; + + chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); + + if (end_ptr == line_reader.ptr()) { return false; } + if (chunk_len == ULONG_MAX) { return false; } + + if (chunk_len == 0) { break; } + + if (!read_content_with_length(strm, chunk_len, nullptr, out)) { + return false; + } + + if (!line_reader.getline()) { return false; } + + if (strcmp(line_reader.ptr(), "\r\n") != 0) { return false; } + + if (!line_reader.getline()) { return false; } + } + + assert(chunk_len == 0); + + // Trailer + if (!line_reader.getline()) { return false; } + + while (strcmp(line_reader.ptr(), "\r\n") != 0) { + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Exclude line terminator + constexpr auto line_terminator_len = 2; + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + parse_header(line_reader.ptr(), end, + [&](const std::string &key, const std::string &val) { + x.headers.emplace(key, val); + }); + + if (!line_reader.getline()) { return false; } + } + + return true; +} + +inline bool is_chunked_transfer_encoding(const Headers &headers) { + return compare_case_ignore( + get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); +} + +template +bool prepare_content_receiver(T &x, int &status, + ContentReceiverWithProgress receiver, + bool decompress, U callback) { + if (decompress) { + std::string encoding = x.get_header_value("Content-Encoding"); + std::unique_ptr decompressor; + + if (encoding == "gzip" || encoding == "deflate") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } else if (encoding.find("br") != std::string::npos) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } + + if (decompressor) { + if (decompressor->is_valid()) { + ContentReceiverWithProgress out = [&](const char *buf, size_t n, + uint64_t off, uint64_t len) { + return decompressor->decompress(buf, n, + [&](const char *buf2, size_t n2) { + return receiver(buf2, n2, off, len); + }); + }; + return callback(std::move(out)); + } else { + status = StatusCode::InternalServerError_500; + return false; + } + } + } + + ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, + uint64_t len) { + return receiver(buf, n, off, len); + }; + return callback(std::move(out)); +} + +template +bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, + Progress progress, ContentReceiverWithProgress receiver, + bool decompress) { + return prepare_content_receiver( + x, status, std::move(receiver), decompress, + [&](const ContentReceiverWithProgress &out) { + auto ret = true; + auto exceed_payload_max_length = false; + + if (is_chunked_transfer_encoding(x.headers)) { + ret = read_content_chunked(strm, x, out); + } else if (!has_header(x.headers, "Content-Length")) { + ret = read_content_without_length(strm, out); + } else { + auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0); + if (len > payload_max_length) { + exceed_payload_max_length = true; + skip_content_with_length(strm, len); + ret = false; + } else if (len > 0) { + ret = read_content_with_length(strm, len, std::move(progress), out); + } + } + + if (!ret) { + status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413 + : StatusCode::BadRequest_400; + } + return ret; + }); +} // namespace detail + +inline ssize_t write_headers(Stream &strm, const Headers &headers) { + ssize_t write_len = 0; + for (const auto &x : headers) { + auto len = + strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); + if (len < 0) { return len; } + write_len += len; + } + auto len = strm.write("\r\n"); + if (len < 0) { return len; } + write_len += len; + return write_len; +} + +inline bool write_data(Stream &strm, const char *d, size_t l) { + size_t offset = 0; + while (offset < l) { + auto length = strm.write(d + offset, l - offset); + if (length < 0) { return false; } + offset += static_cast(length); + } + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, T is_shutting_down, + Error &error) { + size_t end_offset = offset + length; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + if (strm.is_writable() && write_data(strm, d, l)) { + offset += l; + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + + while (offset < end_offset && !is_shutting_down()) { + if (!strm.is_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, end_offset - offset, data_sink)) { + error = Error::Canceled; + return false; + } else if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, + const T &is_shutting_down) { + auto error = Error::Success; + return write_content(strm, content_provider, offset, length, is_shutting_down, + error); +} + +template +inline bool +write_content_without_length(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + offset += l; + if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + + data_sink.done = [&](void) { data_available = false; }; + + while (data_available && !is_shutting_down()) { + if (!strm.is_writable()) { + return false; + } else if (!content_provider(offset, 0, data_sink)) { + return false; + } else if (!ok) { + return false; + } + } + return true; +} + +template +inline bool +write_content_chunked(Stream &strm, const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor, Error &error) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + data_available = l > 0; + offset += l; + + std::string payload; + if (compressor.compress(d, l, false, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = + from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!strm.is_writable() || + !write_data(strm, chunk.data(), chunk.size())) { + ok = false; + } + } + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + + auto done_with_trailer = [&](const Headers *trailer) { + if (!ok) { return; } + + data_available = false; + + std::string payload; + if (!compressor.compress(nullptr, 0, true, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + ok = false; + return; + } + + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!strm.is_writable() || + !write_data(strm, chunk.data(), chunk.size())) { + ok = false; + return; + } + } + + static const std::string done_marker("0\r\n"); + if (!write_data(strm, done_marker.data(), done_marker.size())) { + ok = false; + } + + // Trailer + if (trailer) { + for (const auto &kv : *trailer) { + std::string field_line = kv.first + ": " + kv.second + "\r\n"; + if (!write_data(strm, field_line.data(), field_line.size())) { + ok = false; + } + } + } + + static const std::string crlf("\r\n"); + if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; } + }; + + data_sink.done = [&](void) { done_with_trailer(nullptr); }; + + data_sink.done_with_trailer = [&](const Headers &trailer) { + done_with_trailer(&trailer); + }; + + while (data_available && !is_shutting_down()) { + if (!strm.is_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, 0, data_sink)) { + error = Error::Canceled; + return false; + } else if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content_chunked(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor) { + auto error = Error::Success; + return write_content_chunked(strm, content_provider, is_shutting_down, + compressor, error); +} + +template +inline bool redirect(T &cli, Request &req, Response &res, + const std::string &path, const std::string &location, + Error &error) { + Request new_req = req; + new_req.path = path; + new_req.redirect_count_ -= 1; + + if (res.status == StatusCode::SeeOther_303 && + (req.method != "GET" && req.method != "HEAD")) { + new_req.method = "GET"; + new_req.body.clear(); + new_req.headers.clear(); + } + + Response new_res; + + auto ret = cli.send(new_req, new_res, error); + if (ret) { + req = new_req; + res = new_res; + + if (res.location.empty()) { res.location = location; } + } + return ret; +} + +inline std::string params_to_query_str(const Params ¶ms) { + std::string query; + + for (auto it = params.begin(); it != params.end(); ++it) { + if (it != params.begin()) { query += "&"; } + query += it->first; + query += "="; + query += encode_query_param(it->second); + } + return query; +} + +inline void parse_query_text(const char *data, std::size_t size, + Params ¶ms) { + std::set cache; + split(data, data + size, '&', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(std::move(kv)); + + std::string key; + std::string val; + divide(b, static_cast(e - b), '=', + [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, + std::size_t rhs_size) { + key.assign(lhs_data, lhs_size); + val.assign(rhs_data, rhs_size); + }); + + if (!key.empty()) { + params.emplace(decode_url(key, true), decode_url(val, true)); + } + }); +} + +inline void parse_query_text(const std::string &s, Params ¶ms) { + parse_query_text(s.data(), s.size(), params); +} + +inline bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary) { + auto boundary_keyword = "boundary="; + auto pos = content_type.find(boundary_keyword); + if (pos == std::string::npos) { return false; } + auto end = content_type.find(';', pos); + auto beg = pos + strlen(boundary_keyword); + boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg)); + return !boundary.empty(); +} + +inline void parse_disposition_params(const std::string &s, Params ¶ms) { + std::set cache; + split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(kv); + + std::string key; + std::string val; + split(b, e, '=', [&](const char *b2, const char *e2) { + if (key.empty()) { + key.assign(b2, e2); + } else { + val.assign(b2, e2); + } + }); + + if (!key.empty()) { + params.emplace(trim_double_quotes_copy((key)), + trim_double_quotes_copy((val))); + } + }); +} + +#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 + auto is_valid = [](const std::string &str) { + return std::all_of(str.cbegin(), str.cend(), + [](unsigned char c) { return std::isdigit(c); }); + }; + + if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) { + const auto pos = static_cast(6); + const auto len = static_cast(s.size() - 6); + auto all_valid_ranges = true; + split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { + if (!all_valid_ranges) { return; } + + const auto it = std::find(b, e, '-'); + if (it == e) { + all_valid_ranges = false; + return; + } + + const auto lhs = std::string(b, it); + const auto rhs = std::string(it + 1, e); + if (!is_valid(lhs) || !is_valid(rhs)) { + all_valid_ranges = false; + return; + } + + const auto first = + static_cast(lhs.empty() ? -1 : std::stoll(lhs)); + const auto last = + static_cast(rhs.empty() ? -1 : std::stoll(rhs)); + if ((first == -1 && last == -1) || + (first != -1 && last != -1 && first > last)) { + all_valid_ranges = false; + return; + } + + ranges.emplace_back(first, last); + }); + return all_valid_ranges && !ranges.empty(); + } + return false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +} +#else +} catch (...) { return false; } +#endif + +class MultipartFormDataParser { +public: + MultipartFormDataParser() = default; + + void set_boundary(std::string &&boundary) { + boundary_ = boundary; + dash_boundary_crlf_ = dash_ + boundary_ + crlf_; + crlf_dash_boundary_ = crlf_ + dash_ + boundary_; + } + + bool is_valid() const { return is_valid_; } + + bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, + const MultipartContentHeader &header_callback) { + + buf_append(buf, n); + + while (buf_size() > 0) { + switch (state_) { + case 0: { // Initial boundary + buf_erase(buf_find(dash_boundary_crlf_)); + if (dash_boundary_crlf_.size() > buf_size()) { return true; } + if (!buf_start_with(dash_boundary_crlf_)) { return false; } + buf_erase(dash_boundary_crlf_.size()); + state_ = 1; + break; + } + case 1: { // New entry + clear_file_info(); + state_ = 2; + break; + } + case 2: { // Headers + auto pos = buf_find(crlf_); + if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + while (pos < buf_size()) { + // Empty line + if (pos == 0) { + if (!header_callback(file_)) { + is_valid_ = false; + return false; + } + buf_erase(crlf_.size()); + state_ = 3; + break; + } + + const auto header = buf_head(pos); + + if (!parse_header(header.data(), header.data() + header.size(), + [&](const std::string &, const std::string &) {})) { + is_valid_ = false; + return false; + } + + static const std::string header_content_type = "Content-Type:"; + + if (start_with_case_ignore(header, header_content_type)) { + file_.content_type = + trim_copy(header.substr(header_content_type.size())); + } else { + static const std::regex re_content_disposition( + R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", + std::regex_constants::icase); + + std::smatch m; + if (std::regex_match(header, m, re_content_disposition)) { + Params params; + parse_disposition_params(m[1], params); + + auto it = params.find("name"); + if (it != params.end()) { + file_.name = it->second; + } else { + is_valid_ = false; + return false; + } + + it = params.find("filename"); + if (it != params.end()) { file_.filename = it->second; } + + it = params.find("filename*"); + if (it != params.end()) { + // Only allow UTF-8 enconnding... + static const std::regex re_rfc5987_encoding( + R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); + + std::smatch m2; + if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { + file_.filename = decode_url(m2[1], false); // override... + } else { + is_valid_ = false; + return false; + } + } + } + } + buf_erase(pos + crlf_.size()); + pos = buf_find(crlf_); + } + if (state_ != 3) { return true; } + break; + } + case 3: { // Body + if (crlf_dash_boundary_.size() > buf_size()) { return true; } + auto pos = buf_find(crlf_dash_boundary_); + if (pos < buf_size()) { + if (!content_callback(buf_data(), pos)) { + is_valid_ = false; + return false; + } + buf_erase(pos + crlf_dash_boundary_.size()); + state_ = 4; + } else { + auto len = buf_size() - crlf_dash_boundary_.size(); + if (len > 0) { + if (!content_callback(buf_data(), len)) { + is_valid_ = false; + return false; + } + buf_erase(len); + } + return true; + } + break; + } + case 4: { // Boundary + if (crlf_.size() > buf_size()) { return true; } + if (buf_start_with(crlf_)) { + buf_erase(crlf_.size()); + state_ = 1; + } else { + if (dash_.size() > buf_size()) { return true; } + if (buf_start_with(dash_)) { + buf_erase(dash_.size()); + is_valid_ = true; + buf_erase(buf_size()); // Remove epilogue + } else { + return true; + } + } + break; + } + } + } + + return true; + } + +private: + void clear_file_info() { + file_.name.clear(); + file_.filename.clear(); + file_.content_type.clear(); + } + + bool start_with_case_ignore(const std::string &a, + const std::string &b) const { + if (a.size() < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + } + return true; + } + + const std::string dash_ = "--"; + const std::string crlf_ = "\r\n"; + std::string boundary_; + std::string dash_boundary_crlf_; + std::string crlf_dash_boundary_; + + size_t state_ = 0; + bool is_valid_ = false; + MultipartFormData file_; + + // Buffer + bool start_with(const std::string &a, size_t spos, size_t epos, + const std::string &b) const { + if (epos - spos < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (a[i + spos] != b[i]) { return false; } + } + return true; + } + + size_t buf_size() const { return buf_epos_ - buf_spos_; } + + const char *buf_data() const { return &buf_[buf_spos_]; } + + std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } + + bool buf_start_with(const std::string &s) const { + return start_with(buf_, buf_spos_, buf_epos_, s); + } + + size_t buf_find(const std::string &s) const { + auto c = s.front(); + + size_t off = buf_spos_; + while (off < buf_epos_) { + auto pos = off; + while (true) { + if (pos == buf_epos_) { return buf_size(); } + if (buf_[pos] == c) { break; } + pos++; + } + + auto remaining_size = buf_epos_ - pos; + if (s.size() > remaining_size) { return buf_size(); } + + if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } + + off = pos + 1; + } + + return buf_size(); + } + + void buf_append(const char *data, size_t n) { + auto remaining_size = buf_size(); + if (remaining_size > 0 && buf_spos_ > 0) { + for (size_t i = 0; i < remaining_size; i++) { + buf_[i] = buf_[buf_spos_ + i]; + } + } + buf_spos_ = 0; + buf_epos_ = remaining_size; + + if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } + + for (size_t i = 0; i < n; i++) { + buf_[buf_epos_ + i] = data[i]; + } + buf_epos_ += n; + } + + void buf_erase(size_t size) { buf_spos_ += size; } + + std::string buf_; + size_t buf_spos_ = 0; + size_t buf_epos_ = 0; +}; + +inline std::string to_lower(const char *beg, const char *end) { + std::string out; + auto it = beg; + while (it != end) { + out += static_cast(::tolower(*it)); + it++; + } + return out; +} + +inline std::string random_string(size_t length) { + static const char data[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + // std::random_device might actually be deterministic on some + // platforms, but due to lack of support in the c++ standard library, + // doing better requires either some ugly hacks or breaking portability. + static std::random_device seed_gen; + + // Request 128 bits of entropy for initialization + static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), + seed_gen()}; + + static std::mt19937 engine(seed_sequence); + + std::string result; + for (size_t i = 0; i < length; i++) { + result += data[engine() % (sizeof(data) - 1)]; + } + return result; +} + +inline std::string make_multipart_data_boundary() { + return "--cpp-httplib-multipart-data-" + detail::random_string(16); +} + +inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { + auto valid = true; + for (size_t i = 0; i < boundary.size(); i++) { + auto c = boundary[i]; + if (!std::isalnum(c) && c != '-' && c != '_') { + valid = false; + break; + } + } + return valid; +} + +template +inline std::string +serialize_multipart_formdata_item_begin(const T &item, + const std::string &boundary) { + std::string body = "--" + boundary + "\r\n"; + body += "Content-Disposition: form-data; name=\"" + item.name + "\""; + if (!item.filename.empty()) { + body += "; filename=\"" + item.filename + "\""; + } + body += "\r\n"; + if (!item.content_type.empty()) { + body += "Content-Type: " + item.content_type + "\r\n"; + } + body += "\r\n"; + + return body; +} + +inline std::string serialize_multipart_formdata_item_end() { return "\r\n"; } + +inline std::string +serialize_multipart_formdata_finish(const std::string &boundary) { + return "--" + boundary + "--\r\n"; +} + +inline std::string +serialize_multipart_formdata_get_content_type(const std::string &boundary) { + return "multipart/form-data; boundary=" + boundary; +} + +inline std::string +serialize_multipart_formdata(const MultipartFormDataItems &items, + const std::string &boundary, bool finish = true) { + std::string body; + + for (const auto &item : items) { + body += serialize_multipart_formdata_item_begin(item, boundary); + body += item.content + serialize_multipart_formdata_item_end(); + } + + if (finish) { body += serialize_multipart_formdata_finish(boundary); } + + return body; +} + +inline bool range_error(Request &req, Response &res) { + if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { + ssize_t contant_len = static_cast( + res.content_length_ ? res.content_length_ : res.body.size()); + + ssize_t prev_first_pos = -1; + ssize_t prev_last_pos = -1; + size_t overwrapping_count = 0; + + // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 + // 'HTTP Semantics' to avoid potential denial-of-service attacks. + // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 + + // Too many ranges + if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; } + + for (auto &r : req.ranges) { + auto &first_pos = r.first; + auto &last_pos = r.second; + + if (first_pos == -1 && last_pos == -1) { + first_pos = 0; + last_pos = contant_len; + } + + if (first_pos == -1) { + first_pos = contant_len - last_pos; + last_pos = contant_len - 1; + } + + if (last_pos == -1) { last_pos = contant_len - 1; } + + // Range must be within content length + if (!(0 <= first_pos && first_pos <= last_pos && + last_pos <= contant_len - 1)) { + return true; + } + + // Ranges must be in ascending order + if (first_pos <= prev_first_pos) { return true; } + + // Request must not have more than two overlapping ranges + if (first_pos <= prev_last_pos) { + overwrapping_count++; + if (overwrapping_count > 2) { return true; } + } + + prev_first_pos = (std::max)(prev_first_pos, first_pos); + prev_last_pos = (std::max)(prev_last_pos, last_pos); + } + } + + return false; +} + +inline std::pair +get_range_offset_and_length(Range r, size_t content_length) { + assert(r.first != -1 && r.second != -1); + assert(0 <= r.first && r.first < static_cast(content_length)); + assert(r.first <= r.second && + r.second < static_cast(content_length)); + (void)(content_length); + return std::make_pair(r.first, static_cast(r.second - r.first) + 1); +} + +inline std::string make_content_range_header_field( + const std::pair &offset_and_length, size_t content_length) { + auto st = offset_and_length.first; + auto ed = st + offset_and_length.second - 1; + + std::string field = "bytes "; + field += std::to_string(st); + field += "-"; + field += std::to_string(ed); + field += "/"; + field += std::to_string(content_length); + return field; +} + +template +bool process_multipart_ranges_data(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length, SToken stoken, + CToken ctoken, Content content) { + for (size_t i = 0; i < req.ranges.size(); i++) { + ctoken("--"); + stoken(boundary); + ctoken("\r\n"); + if (!content_type.empty()) { + ctoken("Content-Type: "); + stoken(content_type); + ctoken("\r\n"); + } + + auto offset_and_length = + get_range_offset_and_length(req.ranges[i], content_length); + + ctoken("Content-Range: "); + stoken(make_content_range_header_field(offset_and_length, content_length)); + ctoken("\r\n"); + ctoken("\r\n"); + + if (!content(offset_and_length.first, offset_and_length.second)) { + return false; + } + ctoken("\r\n"); + } + + ctoken("--"); + stoken(boundary); + ctoken("--"); + + return true; +} + +inline void make_multipart_ranges_data(const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, + std::string &data) { + process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { data += token; }, + [&](const std::string &token) { data += token; }, + [&](size_t offset, size_t length) { + assert(offset + length <= content_length); + data += res.body.substr(offset, length); + return true; + }); +} + +inline size_t get_multipart_ranges_data_length(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length) { + size_t data_length = 0; + + process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { data_length += token.size(); }, + [&](const std::string &token) { data_length += token.size(); }, + [&](size_t /*offset*/, size_t length) { + data_length += length; + return true; + }); + + return data_length; +} + +template +inline bool +write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, const T &is_shutting_down) { + return process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { strm.write(token); }, + [&](const std::string &token) { strm.write(token); }, + [&](size_t offset, size_t length) { + return write_content(strm, res.content_provider_, offset, length, + is_shutting_down); + }); +} + +inline bool expect_content(const Request &req) { + if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || + req.method == "PRI" || req.method == "DELETE") { + return true; + } + // TODO: check if Content-Length is set + return false; +} + +inline bool has_crlf(const std::string &s) { + auto p = s.c_str(); + while (*p) { + if (*p == '\r' || *p == '\n') { return true; } + p++; + } + return false; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::string message_digest(const std::string &s, const EVP_MD *algo) { + auto context = std::unique_ptr( + EVP_MD_CTX_new(), EVP_MD_CTX_free); + + unsigned int hash_length = 0; + unsigned char hash[EVP_MAX_MD_SIZE]; + + EVP_DigestInit_ex(context.get(), algo, nullptr); + EVP_DigestUpdate(context.get(), s.c_str(), s.size()); + EVP_DigestFinal_ex(context.get(), hash, &hash_length); + + std::stringstream ss; + for (auto i = 0u; i < hash_length; ++i) { + ss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(hash[i]); + } + + return ss.str(); +} + +inline std::string MD5(const std::string &s) { + return message_digest(s, EVP_md5()); +} + +inline std::string SHA_256(const std::string &s) { + return message_digest(s, EVP_sha256()); +} + +inline std::string SHA_512(const std::string &s) { + return message_digest(s, EVP_sha512()); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef _WIN32 +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store +inline bool load_system_certs_on_windows(X509_STORE *store) { + auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); + if (!hStore) { return false; } + + auto result = false; + PCCERT_CONTEXT pContext = NULL; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + auto encoded_cert = + static_cast(pContext->pbCertEncoded); + + auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + CertFreeCertificateContext(pContext); + CertCloseStore(hStore, 0); + + return result; +} +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#if TARGET_OS_OSX +template +using CFObjectPtr = + std::unique_ptr::type, void (*)(CFTypeRef)>; + +inline void cf_object_ptr_deleter(CFTypeRef obj) { + if (obj) { CFRelease(obj); } +} + +inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { + CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; + CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, + kCFBooleanTrue}; + + CFObjectPtr query( + CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, + sizeof(keys) / sizeof(keys[0]), + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks), + cf_object_ptr_deleter); + + if (!query) { return false; } + + CFTypeRef security_items = nullptr; + if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || + CFArrayGetTypeID() != CFGetTypeID(security_items)) { + return false; + } + + certs.reset(reinterpret_cast(security_items)); + return true; +} + +inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { + CFArrayRef root_security_items = nullptr; + if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { + return false; + } + + certs.reset(root_security_items); + return true; +} + +inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { + auto result = false; + for (auto i = 0; i < CFArrayGetCount(certs); ++i) { + const auto cert = reinterpret_cast( + CFArrayGetValueAtIndex(certs, i)); + + if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } + + CFDataRef cert_data = nullptr; + if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != + errSecSuccess) { + continue; + } + + CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); + + auto encoded_cert = static_cast( + CFDataGetBytePtr(cert_data_ptr.get())); + + auto x509 = + d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); + + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + return result; +} + +inline bool load_system_certs_on_macos(X509_STORE *store) { + auto result = false; + CFObjectPtr certs(nullptr, cf_object_ptr_deleter); + if (retrieve_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store); + } + + if (retrieve_root_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store) || result; + } + + return result; +} +#endif // TARGET_OS_OSX +#endif // _WIN32 +#endif // CPPHTTPLIB_OPENSSL_SUPPORT + +#ifdef _WIN32 +class WSInit { +public: + WSInit() { + WSADATA wsaData; + if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; + } + + ~WSInit() { + if (is_valid_) WSACleanup(); + } + + bool is_valid_ = false; +}; + +static WSInit wsinit_; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::pair make_digest_authentication_header( + const Request &req, const std::map &auth, + size_t cnonce_count, const std::string &cnonce, const std::string &username, + const std::string &password, bool is_proxy = false) { + std::string nc; + { + std::stringstream ss; + ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; + nc = ss.str(); + } + + std::string qop; + if (auth.find("qop") != auth.end()) { + qop = auth.at("qop"); + if (qop.find("auth-int") != std::string::npos) { + qop = "auth-int"; + } else if (qop.find("auth") != std::string::npos) { + qop = "auth"; + } else { + qop.clear(); + } + } + + std::string algo = "MD5"; + if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } + + std::string response; + { + auto H = algo == "SHA-256" ? detail::SHA_256 + : algo == "SHA-512" ? detail::SHA_512 + : detail::MD5; + + auto A1 = username + ":" + auth.at("realm") + ":" + password; + + auto A2 = req.method + ":" + req.path; + if (qop == "auth-int") { A2 += ":" + H(req.body); } + + if (qop.empty()) { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); + } else { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + + ":" + qop + ":" + H(A2)); + } + } + + auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; + + auto field = "Digest username=\"" + username + "\", realm=\"" + + auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + + "\", uri=\"" + req.path + "\", algorithm=" + algo + + (qop.empty() ? ", response=\"" + : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + + cnonce + "\", response=\"") + + response + "\"" + + (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); + + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} +#endif + +inline bool parse_www_authenticate(const Response &res, + std::map &auth, + bool is_proxy) { + auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; + if (res.has_header(auth_key)) { + static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + auto s = res.get_header_value(auth_key); + auto pos = s.find(' '); + if (pos != std::string::npos) { + auto type = s.substr(0, pos); + if (type == "Basic") { + return false; + } else if (type == "Digest") { + s = s.substr(pos + 1); + auto beg = std::sregex_iterator(s.begin(), s.end(), re); + for (auto i = beg; i != std::sregex_iterator(); ++i) { + const auto &m = *i; + auto key = s.substr(static_cast(m.position(1)), + static_cast(m.length(1))); + auto val = m.length(2) > 0 + ? s.substr(static_cast(m.position(2)), + static_cast(m.length(2))) + : s.substr(static_cast(m.position(3)), + static_cast(m.length(3))); + auth[key] = val; + } + return true; + } + } + } + return false; +} + +class ContentProviderAdapter { +public: + explicit ContentProviderAdapter( + ContentProviderWithoutLength &&content_provider) + : content_provider_(content_provider) {} + + bool operator()(size_t offset, size_t, DataSink &sink) { + return content_provider_(offset, sink); + } + +private: + ContentProviderWithoutLength content_provider_; +}; + +} // namespace detail + +inline std::string hosted_at(const std::string &hostname) { + std::vector addrs; + hosted_at(hostname, addrs); + if (addrs.empty()) { return std::string(); } + return addrs[0]; +} + +inline void hosted_at(const std::string &hostname, + std::vector &addrs) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return; + } + + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &addr = + *reinterpret_cast(rp->ai_addr); + std::string ip; + auto dummy = -1; + if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, + dummy)) { + addrs.push_back(ip); + } + } + + freeaddrinfo(result); +} + +inline std::string append_query_params(const std::string &path, + const Params ¶ms) { + std::string path_with_query = path; + const static std::regex re("[^?]+\\?.*"); + auto delm = std::regex_match(path, re) ? '&' : '?'; + path_with_query += delm + detail::params_to_query_str(params); + return path_with_query; +} + +// Header utilities +inline std::pair +make_range_header(const Ranges &ranges) { + std::string field = "bytes="; + auto i = 0; + for (const auto &r : ranges) { + if (i != 0) { field += ", "; } + if (r.first != -1) { field += std::to_string(r.first); } + field += '-'; + if (r.second != -1) { field += std::to_string(r.second); } + i++; + } + return std::make_pair("Range", std::move(field)); +} + +inline std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, bool is_proxy) { + auto field = "Basic " + detail::base64_encode(username + ":" + password); + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +inline std::pair +make_bearer_token_authentication_header(const std::string &token, + bool is_proxy = false) { + auto field = "Bearer " + token; + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +// Request implementation +inline bool Request::has_header(const std::string &key) const { + return detail::has_header(headers, key); +} + +inline std::string Request::get_header_value(const std::string &key, + const char *def, size_t id) const { + return detail::get_header_value(headers, key, def, id); +} + +inline size_t Request::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Request::set_header(const std::string &key, + const std::string &val) { + if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + headers.emplace(key, val); + } +} + +inline bool Request::has_param(const std::string &key) const { + return params.find(key) != params.end(); +} + +inline std::string Request::get_param_value(const std::string &key, + size_t id) const { + auto rng = params.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Request::get_param_value_count(const std::string &key) const { + auto r = params.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline bool Request::is_multipart_form_data() const { + const auto &content_type = get_header_value("Content-Type"); + return !content_type.rfind("multipart/form-data", 0); +} + +inline bool Request::has_file(const std::string &key) const { + return files.find(key) != files.end(); +} + +inline MultipartFormData Request::get_file_value(const std::string &key) const { + auto it = files.find(key); + if (it != files.end()) { return it->second; } + return MultipartFormData(); +} + +inline std::vector +Request::get_file_values(const std::string &key) const { + std::vector values; + auto rng = files.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second); + } + return values; +} + +// Response implementation +inline bool Response::has_header(const std::string &key) const { + return headers.find(key) != headers.end(); +} + +inline std::string Response::get_header_value(const std::string &key, + const char *def, + size_t id) const { + return detail::get_header_value(headers, key, def, id); +} + +inline size_t Response::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Response::set_header(const std::string &key, + const std::string &val) { + if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + headers.emplace(key, val); + } +} + +inline void Response::set_redirect(const std::string &url, int stat) { + if (!detail::has_crlf(url)) { + set_header("Location", url); + if (300 <= stat && stat < 400) { + this->status = stat; + } else { + this->status = StatusCode::Found_302; + } + } +} + +inline void Response::set_content(const char *s, size_t n, + const std::string &content_type) { + body.assign(s, n); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content(const std::string &s, + const std::string &content_type) { + set_content(s.data(), s.size(), content_type); +} + +inline void Response::set_content(std::string &&s, + const std::string &content_type) { + body = std::move(s); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content_provider( + size_t in_length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = in_length; + if (in_length > 0) { content_provider_ = std::move(provider); } + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = false; +} + +inline void Response::set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = false; +} + +inline void Response::set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = true; +} + +// Result implementation +inline bool Result::has_request_header(const std::string &key) const { + return request_headers_.find(key) != request_headers_.end(); +} + +inline std::string Result::get_request_header_value(const std::string &key, + const char *def, + size_t id) const { + return detail::get_header_value(request_headers_, key, def, id); +} + +inline size_t +Result::get_request_header_value_count(const std::string &key) const { + auto r = request_headers_.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +// Stream implementation +inline ssize_t Stream::write(const char *ptr) { + return write(ptr, strlen(ptr)); +} + +inline ssize_t Stream::write(const std::string &s) { + return write(s.data(), s.size()); +} + +namespace detail { + +// Socket stream implementation +inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec) + : sock_(sock), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), read_buff_(read_buff_size_, 0) {} + +inline SocketStream::~SocketStream() = default; + +inline bool SocketStream::is_readable() const { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; +} + +inline bool SocketStream::is_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_); +} + +inline ssize_t SocketStream::read(char *ptr, size_t size) { +#ifdef _WIN32 + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#else + size = (std::min)(size, + static_cast((std::numeric_limits::max)())); +#endif + + if (read_buff_off_ < read_buff_content_size_) { + auto remaining_size = read_buff_content_size_ - read_buff_off_; + if (size <= remaining_size) { + memcpy(ptr, read_buff_.data() + read_buff_off_, size); + read_buff_off_ += size; + return static_cast(size); + } else { + memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); + read_buff_off_ += remaining_size; + return static_cast(remaining_size); + } + } + + if (!is_readable()) { return -1; } + + read_buff_off_ = 0; + read_buff_content_size_ = 0; + + if (size < read_buff_size_) { + auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, + CPPHTTPLIB_RECV_FLAGS); + if (n <= 0) { + return n; + } else if (n <= static_cast(size)) { + memcpy(ptr, read_buff_.data(), static_cast(n)); + return n; + } else { + memcpy(ptr, read_buff_.data(), size); + read_buff_off_ = size; + read_buff_content_size_ = static_cast(n); + return static_cast(size); + } + } else { + return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); + } +} + +inline ssize_t SocketStream::write(const char *ptr, size_t size) { + if (!is_writable()) { return -1; } + +#if defined(_WIN32) && !defined(_WIN64) + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#endif + + return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); +} + +inline void SocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + return detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + return detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SocketStream::socket() const { return sock_; } + +// Buffer stream implementation +inline bool BufferStream::is_readable() const { return true; } + +inline bool BufferStream::is_writable() const { return true; } + +inline ssize_t BufferStream::read(char *ptr, size_t size) { +#if defined(_MSC_VER) && _MSC_VER < 1910 + auto len_read = buffer._Copy_s(ptr, size, size, position); +#else + auto len_read = buffer.copy(ptr, size, position); +#endif + position += static_cast(len_read); + return static_cast(len_read); +} + +inline ssize_t BufferStream::write(const char *ptr, size_t size) { + buffer.append(ptr, size); + return static_cast(size); +} + +inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline socket_t BufferStream::socket() const { return 0; } + +inline const std::string &BufferStream::get_buffer() const { return buffer; } + +inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { + // One past the last ending position of a path param substring + std::size_t last_param_end = 0; + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + // Needed to ensure that parameter names are unique during matcher + // construction + // If exceptions are disabled, only last duplicate path + // parameter will be set + std::unordered_set param_name_set; +#endif + + while (true) { + const auto marker_pos = pattern.find(marker, last_param_end); + if (marker_pos == std::string::npos) { break; } + + static_fragments_.push_back( + pattern.substr(last_param_end, marker_pos - last_param_end)); + + const auto param_name_start = marker_pos + 1; + + auto sep_pos = pattern.find(separator, param_name_start); + if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } + + auto param_name = + pattern.substr(param_name_start, sep_pos - param_name_start); + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + if (param_name_set.find(param_name) != param_name_set.cend()) { + std::string msg = "Encountered path parameter '" + param_name + + "' multiple times in route pattern '" + pattern + "'."; + throw std::invalid_argument(msg); + } +#endif + + param_names_.push_back(std::move(param_name)); + + last_param_end = sep_pos + 1; + } + + if (last_param_end < pattern.length()) { + static_fragments_.push_back(pattern.substr(last_param_end)); + } +} + +inline bool PathParamsMatcher::match(Request &request) const { + request.matches = std::smatch(); + request.path_params.clear(); + request.path_params.reserve(param_names_.size()); + + // One past the position at which the path matched the pattern last time + std::size_t starting_pos = 0; + for (size_t i = 0; i < static_fragments_.size(); ++i) { + const auto &fragment = static_fragments_[i]; + + if (starting_pos + fragment.length() > request.path.length()) { + return false; + } + + // Avoid unnecessary allocation by using strncmp instead of substr + + // comparison + if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(), + fragment.length()) != 0) { + return false; + } + + starting_pos += fragment.length(); + + // Should only happen when we have a static fragment after a param + // Example: '/users/:id/subscriptions' + // The 'subscriptions' fragment here does not have a corresponding param + if (i >= param_names_.size()) { continue; } + + auto sep_pos = request.path.find(separator, starting_pos); + if (sep_pos == std::string::npos) { sep_pos = request.path.length(); } + + const auto ¶m_name = param_names_[i]; + + request.path_params.emplace( + param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); + + // Mark everythin up to '/' as matched + starting_pos = sep_pos + 1; + } + // Returns false if the path is longer than the pattern + return starting_pos >= request.path.length(); +} + +inline bool RegexMatcher::match(Request &request) const { + request.path_params.clear(); + return std::regex_match(request.path, request.matches, regex_); +} + +} // namespace detail + +// HTTP server implementation +inline Server::Server() + : new_task_queue( + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif +} + +inline Server::~Server() = default; + +inline std::unique_ptr +Server::make_matcher(const std::string &pattern) { + if (pattern.find("/:") != std::string::npos) { + return detail::make_unique(pattern); + } else { + return detail::make_unique(pattern); + } +} + +inline Server &Server::Get(const std::string &pattern, Handler handler) { + get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, Handler handler) { + post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, + HandlerWithContentReader handler) { + post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, Handler handler) { + put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, + HandlerWithContentReader handler) { + put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, Handler handler) { + patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, + HandlerWithContentReader handler) { + patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, Handler handler) { + delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, + HandlerWithContentReader handler) { + delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Options(const std::string &pattern, Handler handler) { + options_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline bool Server::set_base_dir(const std::string &dir, + const std::string &mount_point) { + return set_mount_point(mount_point, dir); +} + +inline bool Server::set_mount_point(const std::string &mount_point, + const std::string &dir, Headers headers) { + if (detail::is_dir(dir)) { + std::string mnt = !mount_point.empty() ? mount_point : "/"; + if (!mnt.empty() && mnt[0] == '/') { + base_dirs_.push_back({mnt, dir, std::move(headers)}); + return true; + } + } + return false; +} + +inline bool Server::remove_mount_point(const std::string &mount_point) { + for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { + if (it->mount_point == mount_point) { + base_dirs_.erase(it); + return true; + } + } + return false; +} + +inline Server & +Server::set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime) { + file_extension_and_mimetype_map_[ext] = mime; + return *this; +} + +inline Server &Server::set_default_file_mimetype(const std::string &mime) { + default_file_mimetype_ = mime; + return *this; +} + +inline Server &Server::set_file_request_handler(Handler handler) { + file_request_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler_core(HandlerWithResponse handler, + std::true_type) { + error_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler_core(Handler handler, + std::false_type) { + error_handler_ = [handler](const Request &req, Response &res) { + handler(req, res); + return HandlerResponse::Handled; + }; + return *this; +} + +inline Server &Server::set_exception_handler(ExceptionHandler handler) { + exception_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { + pre_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_post_routing_handler(Handler handler) { + post_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_logger(Logger logger) { + logger_ = std::move(logger); + return *this; +} + +inline Server & +Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { + expect_100_continue_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_address_family(int family) { + address_family_ = family; + return *this; +} + +inline Server &Server::set_tcp_nodelay(bool on) { + tcp_nodelay_ = on; + return *this; +} + +inline Server &Server::set_ipv6_v6only(bool on) { + ipv6_v6only_ = on; + return *this; +} + +inline Server &Server::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); + return *this; +} + +inline Server &Server::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); + return *this; +} + +inline Server &Server::set_header_writer( + std::function const &writer) { + header_writer_ = writer; + return *this; +} + +inline Server &Server::set_keep_alive_max_count(size_t count) { + keep_alive_max_count_ = count; + return *this; +} + +inline Server &Server::set_keep_alive_timeout(time_t sec) { + keep_alive_timeout_sec_ = sec; + return *this; +} + +inline Server &Server::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_idle_interval(time_t sec, time_t usec) { + idle_interval_sec_ = sec; + idle_interval_usec_ = usec; + return *this; +} + +inline Server &Server::set_payload_max_length(size_t length) { + payload_max_length_ = length; + return *this; +} + +inline bool Server::bind_to_port(const std::string &host, int port, + int socket_flags) { + auto ret = bind_internal(host, port, socket_flags); + if (ret == -1) { is_decommisioned = true; } + return ret >= 0; +} +inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { + auto ret = bind_internal(host, 0, socket_flags); + if (ret == -1) { is_decommisioned = true; } + return ret; +} + +inline bool Server::listen_after_bind() { return listen_internal(); } + +inline bool Server::listen(const std::string &host, int port, + int socket_flags) { + return bind_to_port(host, port, socket_flags) && listen_internal(); +} + +inline bool Server::is_running() const { return is_running_; } + +inline void Server::wait_until_ready() const { + while (!is_running_ && !is_decommisioned) { + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } +} + +inline void Server::stop() { + if (is_running_) { + assert(svr_sock_ != INVALID_SOCKET); + std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); + detail::shutdown_socket(sock); + detail::close_socket(sock); + } + is_decommisioned = false; +} + +inline void Server::decommission() { is_decommisioned = true; } + +inline bool Server::parse_request_line(const char *s, Request &req) const { + auto len = strlen(s); + if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } + len -= 2; + + { + size_t count = 0; + + detail::split(s, s + len, ' ', [&](const char *b, const char *e) { + switch (count) { + case 0: req.method = std::string(b, e); break; + case 1: req.target = std::string(b, e); break; + case 2: req.version = std::string(b, e); break; + default: break; + } + count++; + }); + + if (count != 3) { return false; } + } + + static const std::set methods{ + "GET", "HEAD", "POST", "PUT", "DELETE", + "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; + + if (methods.find(req.method) == methods.end()) { return false; } + + if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } + + { + // Skip URL fragment + for (size_t i = 0; i < req.target.size(); i++) { + if (req.target[i] == '#') { + req.target.erase(i); + break; + } + } + + detail::divide(req.target, '?', + [&](const char *lhs_data, std::size_t lhs_size, + const char *rhs_data, std::size_t rhs_size) { + req.path = detail::decode_url( + std::string(lhs_data, lhs_size), false); + detail::parse_query_text(rhs_data, rhs_size, req.params); + }); + } + + return true; +} + +inline bool Server::write_response(Stream &strm, bool close_connection, + Request &req, Response &res) { + // NOTE: `req.ranges` should be empty, otherwise it will be applied + // incorrectly to the error content. + req.ranges.clear(); + return write_response_core(strm, close_connection, req, res, false); +} + +inline bool Server::write_response_with_content(Stream &strm, + bool close_connection, + const Request &req, + Response &res) { + return write_response_core(strm, close_connection, req, res, true); +} + +inline bool Server::write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges) { + assert(res.status != -1); + + if (400 <= res.status && error_handler_ && + error_handler_(req, res) == HandlerResponse::Handled) { + need_apply_ranges = true; + } + + std::string content_type; + std::string boundary; + if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } + + // Prepare additional headers + if (close_connection || req.get_header_value("Connection") == "close") { + res.set_header("Connection", "close"); + } else { + std::stringstream ss; + ss << "timeout=" << keep_alive_timeout_sec_ + << ", max=" << keep_alive_max_count_; + res.set_header("Keep-Alive", ss.str()); + } + + if (!res.has_header("Content-Type") && + (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { + res.set_header("Content-Type", "text/plain"); + } + + if (!res.has_header("Content-Length") && res.body.empty() && + !res.content_length_ && !res.content_provider_) { + res.set_header("Content-Length", "0"); + } + + if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { + res.set_header("Accept-Ranges", "bytes"); + } + + if (post_routing_handler_) { post_routing_handler_(req, res); } + + // Response line and headers + { + detail::BufferStream bstrm; + + if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, + status_message(res.status))) { + return false; + } + + if (!header_writer_(bstrm, res.headers)) { return false; } + + // Flush buffer + auto &data = bstrm.get_buffer(); + detail::write_data(strm, data.data(), data.size()); + } + + // Body + auto ret = true; + if (req.method != "HEAD") { + if (!res.body.empty()) { + if (!detail::write_data(strm, res.body.data(), res.body.size())) { + ret = false; + } + } else if (res.content_provider_) { + if (write_content_with_provider(strm, req, res, boundary, content_type)) { + res.content_provider_success_ = true; + } else { + ret = false; + } + } + } + + // Log + if (logger_) { logger_(req, res); } + + return ret; +} + +inline bool +Server::write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type) { + auto is_shutting_down = [this]() { + return this->svr_sock_ == INVALID_SOCKET; + }; + + if (res.content_length_ > 0) { + if (req.ranges.empty()) { + return detail::write_content(strm, res.content_provider_, 0, + res.content_length_, is_shutting_down); + } else if (req.ranges.size() == 1) { + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + return detail::write_content(strm, res.content_provider_, + offset_and_length.first, + offset_and_length.second, is_shutting_down); + } else { + return detail::write_multipart_ranges_data( + strm, req, res, boundary, content_type, res.content_length_, + is_shutting_down); + } + } else { + if (res.is_chunked_content_provider_) { + auto type = detail::encoding_type(req, res); + + std::unique_ptr compressor; + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); +#endif + } else { + compressor = detail::make_unique(); + } + assert(compressor != nullptr); + + return detail::write_content_chunked(strm, res.content_provider_, + is_shutting_down, *compressor); + } else { + return detail::write_content_without_length(strm, res.content_provider_, + is_shutting_down); + } + } +} + +inline bool Server::read_content(Stream &strm, Request &req, Response &res) { + MultipartFormDataMap::iterator cur; + auto file_count = 0; + if (read_content_core( + strm, req, res, + // Regular + [&](const char *buf, size_t n) { + if (req.body.size() + n > req.body.max_size()) { return false; } + req.body.append(buf, n); + return true; + }, + // Multipart + [&](const MultipartFormData &file) { + if (file_count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { + return false; + } + cur = req.files.emplace(file.name, file); + return true; + }, + [&](const char *buf, size_t n) { + auto &content = cur->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + return true; + })) { + const auto &content_type = req.get_header_value("Content-Type"); + if (!content_type.find("application/x-www-form-urlencoded")) { + if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { + res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? + return false; + } + detail::parse_query_text(req.body, req.params); + } + return true; + } + return false; +} + +inline bool Server::read_content_with_content_receiver( + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) { + return read_content_core(strm, req, res, std::move(receiver), + std::move(multipart_header), + std::move(multipart_receiver)); +} + +inline bool +Server::read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) const { + detail::MultipartFormDataParser multipart_form_data_parser; + ContentReceiverWithProgress out; + + if (req.is_multipart_form_data()) { + const auto &content_type = req.get_header_value("Content-Type"); + std::string boundary; + if (!detail::parse_multipart_boundary(content_type, boundary)) { + res.status = StatusCode::BadRequest_400; + return false; + } + + multipart_form_data_parser.set_boundary(std::move(boundary)); + out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { + /* For debug + size_t pos = 0; + while (pos < n) { + auto read_size = (std::min)(1, n - pos); + auto ret = multipart_form_data_parser.parse( + buf + pos, read_size, multipart_receiver, multipart_header); + if (!ret) { return false; } + pos += read_size; + } + return true; + */ + return multipart_form_data_parser.parse(buf, n, multipart_receiver, + multipart_header); + }; + } else { + out = [receiver](const char *buf, size_t n, uint64_t /*off*/, + uint64_t /*len*/) { return receiver(buf, n); }; + } + + if (req.method == "DELETE" && !req.has_header("Content-Length")) { + return true; + } + + if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, + out, true)) { + return false; + } + + if (req.is_multipart_form_data()) { + if (!multipart_form_data_parser.is_valid()) { + res.status = StatusCode::BadRequest_400; + return false; + } + } + + return true; +} + +inline bool Server::handle_file_request(const Request &req, Response &res, + bool head) { + for (const auto &entry : base_dirs_) { + // Prefix match + if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { + std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); + if (detail::is_valid_path(sub_path)) { + auto path = entry.base_dir + sub_path; + if (path.back() == '/') { path += "index.html"; } + + if (detail::is_file(path)) { + for (const auto &kv : entry.headers) { + res.set_header(kv.first, kv.second); + } + + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { return false; } + + res.set_content_provider( + mm->size(), + detail::find_content_type(path, file_extension_and_mimetype_map_, + default_file_mimetype_), + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + + if (!head && file_request_handler_) { + file_request_handler_(req, res); + } + + return true; + } + } + } + } + return false; +} + +inline socket_t +Server::create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const { + return detail::create_socket( + host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, + ipv6_v6only_, std::move(socket_options), + [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { + if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + return false; + } + if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } + return true; + }); +} + +inline int Server::bind_internal(const std::string &host, int port, + int socket_flags) { + if (is_decommisioned) { return -1; } + + if (!is_valid()) { return -1; } + + svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); + if (svr_sock_ == INVALID_SOCKET) { return -1; } + + if (port == 0) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (getsockname(svr_sock_, reinterpret_cast(&addr), + &addr_len) == -1) { + return -1; + } + if (addr.ss_family == AF_INET) { + return ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + return ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return -1; + } + } else { + return port; + } +} + +inline bool Server::listen_internal() { + if (is_decommisioned) { return false; } + + auto ret = true; + is_running_ = true; + auto se = detail::scope_exit([&]() { is_running_ = false; }); + + { + std::unique_ptr task_queue(new_task_queue()); + + while (svr_sock_ != INVALID_SOCKET) { +#ifndef _WIN32 + if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { +#endif + auto val = detail::select_read(svr_sock_, idle_interval_sec_, + idle_interval_usec_); + if (val == 0) { // Timeout + task_queue->on_idle(); + continue; + } +#ifndef _WIN32 + } +#endif + +#if defined _WIN32 + // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, + // OVERLAPPED + socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); +#elif defined SOCK_CLOEXEC + socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); +#else + socket_t sock = accept(svr_sock_, nullptr, nullptr); +#endif + + if (sock == INVALID_SOCKET) { + if (errno == EMFILE) { + // The per-process limit of open file descriptors has been reached. + // Try to accept new connections after a short sleep. + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + continue; + } else if (errno == EINTR || errno == EAGAIN) { + continue; + } + if (svr_sock_ != INVALID_SOCKET) { + detail::close_socket(svr_sock_); + ret = false; + } else { + ; // The server socket was closed by user. + } + break; + } + + { +#ifdef _WIN32 + auto timeout = static_cast(read_timeout_sec_ * 1000 + + read_timeout_usec_ / 1000); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec_); + tv.tv_usec = static_cast(read_timeout_usec_); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + { + +#ifdef _WIN32 + auto timeout = static_cast(write_timeout_sec_ * 1000 + + write_timeout_usec_ / 1000); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec_); + tv.tv_usec = static_cast(write_timeout_usec_); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + + if (!task_queue->enqueue( + [this, sock]() { process_and_close_socket(sock); })) { + detail::shutdown_socket(sock); + detail::close_socket(sock); + } + } + + task_queue->shutdown(); + } + + is_decommisioned = !ret; + return ret; +} + +inline bool Server::routing(Request &req, Response &res, Stream &strm) { + if (pre_routing_handler_ && + pre_routing_handler_(req, res) == HandlerResponse::Handled) { + return true; + } + + // File handler + auto is_head_request = req.method == "HEAD"; + if ((req.method == "GET" || is_head_request) && + handle_file_request(req, res, is_head_request)) { + return true; + } + + if (detail::expect_content(req)) { + // Content reader handler + { + ContentReader reader( + [&](ContentReceiver receiver) { + return read_content_with_content_receiver( + strm, req, res, std::move(receiver), nullptr, nullptr); + }, + [&](MultipartContentHeader header, ContentReceiver receiver) { + return read_content_with_content_receiver(strm, req, res, nullptr, + std::move(header), + std::move(receiver)); + }); + + if (req.method == "POST") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + post_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PUT") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + put_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PATCH") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + patch_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "DELETE") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + delete_handlers_for_content_reader_)) { + return true; + } + } + } + + // Read content into `req.body` + if (!read_content(strm, req, res)) { return false; } + } + + // Regular handler + if (req.method == "GET" || req.method == "HEAD") { + return dispatch_request(req, res, get_handlers_); + } else if (req.method == "POST") { + return dispatch_request(req, res, post_handlers_); + } else if (req.method == "PUT") { + return dispatch_request(req, res, put_handlers_); + } else if (req.method == "DELETE") { + return dispatch_request(req, res, delete_handlers_); + } else if (req.method == "OPTIONS") { + return dispatch_request(req, res, options_handlers_); + } else if (req.method == "PATCH") { + return dispatch_request(req, res, patch_handlers_); + } + + res.status = StatusCode::BadRequest_400; + return false; +} + +inline bool Server::dispatch_request(Request &req, Response &res, + const Handlers &handlers) const { + for (const auto &x : handlers) { + const auto &matcher = x.first; + const auto &handler = x.second; + + if (matcher->match(req)) { + handler(req, res); + return true; + } + } + return false; +} + +inline void Server::apply_ranges(const Request &req, Response &res, + std::string &content_type, + std::string &boundary) const { + if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) { + auto it = res.headers.find("Content-Type"); + if (it != res.headers.end()) { + content_type = it->second; + res.headers.erase(it); + } + + boundary = detail::make_multipart_data_boundary(); + + res.set_header("Content-Type", + "multipart/byteranges; boundary=" + boundary); + } + + auto type = detail::encoding_type(req, res); + + if (res.body.empty()) { + if (res.content_length_ > 0) { + size_t length = 0; + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { + length = res.content_length_; + } else if (req.ranges.size() == 1) { + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + length = offset_and_length.second; + + auto content_range = detail::make_content_range_header_field( + offset_and_length, res.content_length_); + res.set_header("Content-Range", content_range); + } else { + length = detail::get_multipart_ranges_data_length( + req, boundary, content_type, res.content_length_); + } + res.set_header("Content-Length", std::to_string(length)); + } else { + if (res.content_provider_) { + if (res.is_chunked_content_provider_) { + res.set_header("Transfer-Encoding", "chunked"); + if (type == detail::EncodingType::Gzip) { + res.set_header("Content-Encoding", "gzip"); + } else if (type == detail::EncodingType::Brotli) { + res.set_header("Content-Encoding", "br"); + } + } + } + } + } else { + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { + ; + } else if (req.ranges.size() == 1) { + auto offset_and_length = + detail::get_range_offset_and_length(req.ranges[0], res.body.size()); + auto offset = offset_and_length.first; + auto length = offset_and_length.second; + + auto content_range = detail::make_content_range_header_field( + offset_and_length, res.body.size()); + res.set_header("Content-Range", content_range); + + assert(offset + length <= res.body.size()); + res.body = res.body.substr(offset, length); + } else { + std::string data; + detail::make_multipart_ranges_data(req, res, boundary, content_type, + res.body.size(), data); + res.body.swap(data); + } + + if (type != detail::EncodingType::None) { + std::unique_ptr compressor; + std::string content_encoding; + + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); + content_encoding = "gzip"; +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); + content_encoding = "br"; +#endif + } + + if (compressor) { + std::string compressed; + if (compressor->compress(res.body.data(), res.body.size(), true, + [&](const char *data, size_t data_len) { + compressed.append(data, data_len); + return true; + })) { + res.body.swap(compressed); + res.set_header("Content-Encoding", content_encoding); + } + } + } + + auto length = std::to_string(res.body.size()); + res.set_header("Content-Length", length); + } +} + +inline bool Server::dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const { + for (const auto &x : handlers) { + const auto &matcher = x.first; + const auto &handler = x.second; + + if (matcher->match(req)) { + handler(req, res, content_reader); + return true; + } + } + return false; +} + +inline bool +Server::process_request(Stream &strm, bool close_connection, + bool &connection_closed, + const std::function &setup_request) { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + // Connection has been closed on client + if (!line_reader.getline()) { return false; } + + Request req; + + Response res; + res.version = "HTTP/1.1"; + res.headers = default_headers_; + +#ifdef _WIN32 + // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). +#else +#ifndef CPPHTTPLIB_USE_POLL + // Socket file descriptor exceeded FD_SETSIZE... + if (strm.socket() >= FD_SETSIZE) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::InternalServerError_500; + return write_response(strm, close_connection, req, res); + } +#endif +#endif + + // Check if the request URI doesn't exceed the limit + if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::UriTooLong_414; + return write_response(strm, close_connection, req, res); + } + + // Request line and headers + if (!parse_request_line(line_reader.ptr(), req) || + !detail::read_headers(strm, req.headers)) { + res.status = StatusCode::BadRequest_400; + return write_response(strm, close_connection, req, res); + } + + if (req.get_header_value("Connection") == "close") { + connection_closed = true; + } + + if (req.version == "HTTP/1.0" && + req.get_header_value("Connection") != "Keep-Alive") { + connection_closed = true; + } + + strm.get_remote_ip_and_port(req.remote_addr, req.remote_port); + req.set_header("REMOTE_ADDR", req.remote_addr); + req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); + + strm.get_local_ip_and_port(req.local_addr, req.local_port); + req.set_header("LOCAL_ADDR", req.local_addr); + req.set_header("LOCAL_PORT", std::to_string(req.local_port)); + + if (req.has_header("Range")) { + const auto &range_header_value = req.get_header_value("Range"); + if (!detail::parse_range_header(range_header_value, req.ranges)) { + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); + } + } + + if (setup_request) { setup_request(req); } + + if (req.get_header_value("Expect") == "100-continue") { + int status = StatusCode::Continue_100; + if (expect_100_continue_handler_) { + status = expect_100_continue_handler_(req, res); + } + switch (status) { + case StatusCode::Continue_100: + case StatusCode::ExpectationFailed_417: + strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, + status_message(status)); + break; + default: return write_response(strm, close_connection, req, res); + } + } + + // Routing + auto routed = false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + routed = routing(req, res, strm); +#else + try { + routed = routing(req, res, strm); + } catch (std::exception &e) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = StatusCode::InternalServerError_500; + std::string val; + auto s = e.what(); + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case '\r': val += "\\r"; break; + case '\n': val += "\\n"; break; + default: val += s[i]; break; + } + } + res.set_header("EXCEPTION_WHAT", val); + } + } catch (...) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = StatusCode::InternalServerError_500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); + } + } +#endif + if (routed) { + if (res.status == -1) { + res.status = req.ranges.empty() ? StatusCode::OK_200 + : StatusCode::PartialContent_206; + } + + if (detail::range_error(req, res)) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); + } + + return write_response_with_content(strm, close_connection, req, res); + } else { + if (res.status == -1) { res.status = StatusCode::NotFound_404; } + + return write_response(strm, close_connection, req, res); + } +} + +inline bool Server::is_valid() const { return true; } + +inline bool Server::process_and_close_socket(socket_t sock) { + auto ret = detail::process_server_socket( + svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [this](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, close_connection, connection_closed, + nullptr); + }); + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +// HTTP client implementation +inline ClientImpl::ClientImpl(const std::string &host) + : ClientImpl(host, 80, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port) + : ClientImpl(host, port, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : host_(host), port_(port), + host_and_port_(adjust_host_string(host) + ":" + std::to_string(port)), + client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} + +inline ClientImpl::~ClientImpl() { + std::lock_guard guard(socket_mutex_); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline bool ClientImpl::is_valid() const { return true; } + +inline void ClientImpl::copy_settings(const ClientImpl &rhs) { + client_cert_path_ = rhs.client_cert_path_; + client_key_path_ = rhs.client_key_path_; + connection_timeout_sec_ = rhs.connection_timeout_sec_; + read_timeout_sec_ = rhs.read_timeout_sec_; + read_timeout_usec_ = rhs.read_timeout_usec_; + write_timeout_sec_ = rhs.write_timeout_sec_; + write_timeout_usec_ = rhs.write_timeout_usec_; + basic_auth_username_ = rhs.basic_auth_username_; + basic_auth_password_ = rhs.basic_auth_password_; + bearer_token_auth_token_ = rhs.bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + digest_auth_username_ = rhs.digest_auth_username_; + digest_auth_password_ = rhs.digest_auth_password_; +#endif + keep_alive_ = rhs.keep_alive_; + follow_location_ = rhs.follow_location_; + url_encode_ = rhs.url_encode_; + address_family_ = rhs.address_family_; + tcp_nodelay_ = rhs.tcp_nodelay_; + ipv6_v6only_ = rhs.ipv6_v6only_; + socket_options_ = rhs.socket_options_; + compress_ = rhs.compress_; + decompress_ = rhs.decompress_; + interface_ = rhs.interface_; + proxy_host_ = rhs.proxy_host_; + proxy_port_ = rhs.proxy_port_; + proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; + proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; + proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; + proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + ca_cert_file_path_ = rhs.ca_cert_file_path_; + ca_cert_dir_path_ = rhs.ca_cert_dir_path_; + ca_cert_store_ = rhs.ca_cert_store_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + server_certificate_verification_ = rhs.server_certificate_verification_; +#endif + logger_ = rhs.logger_; +} + +inline socket_t ClientImpl::create_client_socket(Error &error) const { + if (!proxy_host_.empty() && proxy_port_ != -1) { + return detail::create_client_socket( + proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, + ipv6_v6only_, socket_options_, connection_timeout_sec_, + connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, interface_, error); + } + + // Check is custom IP specified for host_ + std::string ip; + auto it = addr_map_.find(host_); + if (it != addr_map_.end()) { ip = it->second; } + + return detail::create_client_socket( + host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_, + socket_options_, connection_timeout_sec_, connection_timeout_usec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, interface_, error); +} + +inline bool ClientImpl::create_and_connect_socket(Socket &socket, + Error &error) { + auto sock = create_client_socket(error); + if (sock == INVALID_SOCKET) { return false; } + socket.sock = sock; + return true; +} + +inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, + bool /*shutdown_gracefully*/) { + // If there are any requests in flight from threads other than us, then it's + // a thread-unsafe race because individual ssl* objects are not thread-safe. + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); +} + +inline void ClientImpl::shutdown_socket(Socket &socket) const { + if (socket.sock == INVALID_SOCKET) { return; } + detail::shutdown_socket(socket.sock); +} + +inline void ClientImpl::close_socket(Socket &socket) { + // If there are requests in flight in another thread, usually closing + // the socket will be fine and they will simply receive an error when + // using the closed socket, but it is still a bug since rarely the OS + // may reassign the socket id to be used for a new socket, and then + // suddenly they will be operating on a live socket that is different + // than the one they intended! + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); + + // It is also a bug if this happens while SSL is still active +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + assert(socket.ssl == nullptr); +#endif + if (socket.sock == INVALID_SOCKET) { return; } + detail::close_socket(socket.sock); + socket.sock = INVALID_SOCKET; +} + +inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, + Response &res) const { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + if (!line_reader.getline()) { return false; } + +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); +#else + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); +#endif + + std::cmatch m; + if (!std::regex_match(line_reader.ptr(), m, re)) { + return req.method == "CONNECT"; + } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + + // Ignore '100 Continue' + while (res.status == StatusCode::Continue_100) { + if (!line_reader.getline()) { return false; } // CRLF + if (!line_reader.getline()) { return false; } // next response line + + if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + } + + return true; +} + +inline bool ClientImpl::send(Request &req, Response &res, Error &error) { + std::lock_guard request_mutex_guard(request_mutex_); + auto ret = send_(req, res, error); + if (error == Error::SSLPeerCouldBeClosed_) { + assert(!ret); + ret = send_(req, res, error); + } + return ret; +} + +inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { + { + std::lock_guard guard(socket_mutex_); + + // Set this to false immediately - if it ever gets set to true by the end of + // the request, we know another thread instructed us to close the socket. + socket_should_be_closed_when_request_is_done_ = false; + + auto is_alive = false; + if (socket_.is_open()) { + is_alive = detail::is_socket_alive(socket_.sock); + if (!is_alive) { + // Attempt to avoid sigpipe by shutting down nongracefully if it seems + // like the other side has already closed the connection Also, there + // cannot be any requests in flight from other threads since we locked + // request_mutex_, so safe to close everything immediately + const bool shutdown_gracefully = false; + shutdown_ssl(socket_, shutdown_gracefully); + shutdown_socket(socket_); + close_socket(socket_); + } + } + + if (!is_alive) { + if (!create_and_connect_socket(socket_, error)) { return false; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // TODO: refactoring + if (is_ssl()) { + auto &scli = static_cast(*this); + if (!proxy_host_.empty() && proxy_port_ != -1) { + auto success = false; + if (!scli.connect_with_proxy(socket_, res, success, error)) { + return success; + } + } + + if (!scli.initialize_ssl(socket_, error)) { return false; } + } +#endif + } + + // Mark the current socket as being in use so that it cannot be closed by + // anyone else while this request is ongoing, even though we will be + // releasing the mutex. + if (socket_requests_in_flight_ > 1) { + assert(socket_requests_are_from_thread_ == std::this_thread::get_id()); + } + socket_requests_in_flight_ += 1; + socket_requests_are_from_thread_ = std::this_thread::get_id(); + } + + for (const auto &header : default_headers_) { + if (req.headers.find(header.first) == req.headers.end()) { + req.headers.insert(header); + } + } + + auto ret = false; + auto close_connection = !keep_alive_; + + auto se = detail::scope_exit([&]() { + // Briefly lock mutex in order to mark that a request is no longer ongoing + std::lock_guard guard(socket_mutex_); + socket_requests_in_flight_ -= 1; + if (socket_requests_in_flight_ <= 0) { + assert(socket_requests_in_flight_ == 0); + socket_requests_are_from_thread_ = std::thread::id(); + } + + if (socket_should_be_closed_when_request_is_done_ || close_connection || + !ret) { + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + }); + + ret = process_socket(socket_, [&](Stream &strm) { + return handle_request(strm, req, res, close_connection, error); + }); + + if (!ret) { + if (error == Error::Success) { error = Error::Unknown; } + } + + return ret; +} + +inline Result ClientImpl::send(const Request &req) { + auto req2 = req; + return send_(std::move(req2)); +} + +inline Result ClientImpl::send_(Request &&req) { + auto res = detail::make_unique(); + auto error = Error::Success; + auto ret = send(req, *res, error); + return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; +} + +inline bool ClientImpl::handle_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + if (req.path.empty()) { + error = Error::Connection; + return false; + } + + auto req_save = req; + + bool ret; + + if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { + auto req2 = req; + req2.path = "http://" + host_and_port_ + req.path; + ret = process_request(strm, req2, res, close_connection, error); + req = req2; + req.path = req_save.path; + } else { + ret = process_request(strm, req, res, close_connection, error); + } + + if (!ret) { return false; } + + if (res.get_header_value("Connection") == "close" || + (res.version == "HTTP/1.0" && res.reason != "Connection established")) { + // TODO this requires a not-entirely-obvious chain of calls to be correct + // for this to be safe. + + // This is safe to call because handle_request is only called by send_ + // which locks the request mutex during the process. It would be a bug + // to call it from a different thread since it's a thread-safety issue + // to do these things to the socket if another thread is using the socket. + std::lock_guard guard(socket_mutex_); + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + + if (300 < res.status && res.status < 400 && follow_location_) { + req = req_save; + ret = redirect(req, res, error); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if ((res.status == StatusCode::Unauthorized_401 || + res.status == StatusCode::ProxyAuthenticationRequired_407) && + req.authorization_count_ < 5) { + auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407; + const auto &username = + is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; + const auto &password = + is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; + + if (!username.empty() && !password.empty()) { + std::map auth; + if (detail::parse_www_authenticate(res, auth, is_proxy)) { + Request new_req = req; + new_req.authorization_count_ += 1; + new_req.headers.erase(is_proxy ? "Proxy-Authorization" + : "Authorization"); + new_req.headers.insert(detail::make_digest_authentication_header( + req, auth, new_req.authorization_count_, detail::random_string(10), + username, password, is_proxy)); + + Response new_res; + + ret = send(new_req, new_res, error); + if (ret) { res = new_res; } + } + } + } +#endif + + return ret; +} + +inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { + if (req.redirect_count_ == 0) { + error = Error::ExceedRedirectCount; + return false; + } + + auto location = res.get_header_value("location"); + if (location.empty()) { return false; } + + const static std::regex re( + R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); + + std::smatch m; + if (!std::regex_match(location, m, re)) { return false; } + + auto scheme = is_ssl() ? "https" : "http"; + + auto next_scheme = m[1].str(); + auto next_host = m[2].str(); + if (next_host.empty()) { next_host = m[3].str(); } + auto port_str = m[4].str(); + auto next_path = m[5].str(); + auto next_query = m[6].str(); + + auto next_port = port_; + if (!port_str.empty()) { + next_port = std::stoi(port_str); + } else if (!next_scheme.empty()) { + next_port = next_scheme == "https" ? 443 : 80; + } + + if (next_scheme.empty()) { next_scheme = scheme; } + if (next_host.empty()) { next_host = host_; } + if (next_path.empty()) { next_path = "/"; } + + auto path = detail::decode_url(next_path, true) + next_query; + + if (next_scheme == scheme && next_host == host_ && next_port == port_) { + return detail::redirect(*this, req, res, path, location, error); + } else { + if (next_scheme == "https") { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(next_host, next_port); + cli.copy_settings(*this); + if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } + return detail::redirect(cli, req, res, path, location, error); +#else + return false; +#endif + } else { + ClientImpl cli(next_host, next_port); + cli.copy_settings(*this); + return detail::redirect(cli, req, res, path, location, error); + } + } +} + +inline bool ClientImpl::write_content_with_provider(Stream &strm, + const Request &req, + Error &error) const { + auto is_shutting_down = []() { return false; }; + + if (req.is_chunked_content_provider_) { + // TODO: Brotli support + std::unique_ptr compressor; +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { + compressor = detail::make_unique(); + } else +#endif + { + compressor = detail::make_unique(); + } + + return detail::write_content_chunked(strm, req.content_provider_, + is_shutting_down, *compressor, error); + } else { + return detail::write_content(strm, req.content_provider_, 0, + req.content_length_, is_shutting_down, error); + } +} + +inline bool ClientImpl::write_request(Stream &strm, Request &req, + bool close_connection, Error &error) { + // Prepare additional headers + if (close_connection) { + if (!req.has_header("Connection")) { + req.set_header("Connection", "close"); + } + } + + if (!req.has_header("Host")) { + if (is_ssl()) { + if (port_ == 443) { + req.set_header("Host", host_); + } else { + req.set_header("Host", host_and_port_); + } + } else { + if (port_ == 80) { + req.set_header("Host", host_); + } else { + req.set_header("Host", host_and_port_); + } + } + } + + if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } + +#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT + if (!req.has_header("User-Agent")) { + auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; + req.set_header("User-Agent", agent); + } +#endif + + if (req.body.empty()) { + if (req.content_provider_) { + if (!req.is_chunked_content_provider_) { + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.content_length_); + req.set_header("Content-Length", length); + } + } + } else { + if (req.method == "POST" || req.method == "PUT" || + req.method == "PATCH") { + req.set_header("Content-Length", "0"); + } + } + } else { + if (!req.has_header("Content-Type")) { + req.set_header("Content-Type", "text/plain"); + } + + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.body.size()); + req.set_header("Content-Length", length); + } + } + + if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_basic_authentication_header( + basic_auth_username_, basic_auth_password_, false)); + } + } + + if (!proxy_basic_auth_username_.empty() && + !proxy_basic_auth_password_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_basic_authentication_header( + proxy_basic_auth_username_, proxy_basic_auth_password_, true)); + } + } + + if (!bearer_token_auth_token_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + bearer_token_auth_token_, false)); + } + } + + if (!proxy_bearer_token_auth_token_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + proxy_bearer_token_auth_token_, true)); + } + } + + // Request line and headers + { + detail::BufferStream bstrm; + + const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; + bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); + + header_writer_(bstrm, req.headers); + + // Flush buffer + auto &data = bstrm.get_buffer(); + if (!detail::write_data(strm, data.data(), data.size())) { + error = Error::Write; + return false; + } + } + + // Body + if (req.body.empty()) { + return write_content_with_provider(strm, req, error); + } + + if (!detail::write_data(strm, req.body.data(), req.body.size())) { + error = Error::Write; + return false; + } + + return true; +} + +inline std::unique_ptr ClientImpl::send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error) { + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { req.set_header("Content-Encoding", "gzip"); } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_ && !content_provider_without_length) { + // TODO: Brotli support + detail::gzip_compressor compressor; + + if (content_provider) { + auto ok = true; + size_t offset = 0; + DataSink data_sink; + + data_sink.write = [&](const char *data, size_t data_len) -> bool { + if (ok) { + auto last = offset + data_len == content_length; + + auto ret = compressor.compress( + data, data_len, last, + [&](const char *compressed_data, size_t compressed_data_len) { + req.body.append(compressed_data, compressed_data_len); + return true; + }); + + if (ret) { + offset += data_len; + } else { + ok = false; + } + } + return ok; + }; + + while (ok && offset < content_length) { + if (!content_provider(offset, content_length - offset, data_sink)) { + error = Error::Canceled; + return nullptr; + } + } + } else { + if (!compressor.compress(body, content_length, true, + [&](const char *data, size_t data_len) { + req.body.append(data, data_len); + return true; + })) { + error = Error::Compression; + return nullptr; + } + } + } else +#endif + { + if (content_provider) { + req.content_length_ = content_length; + req.content_provider_ = std::move(content_provider); + req.is_chunked_content_provider_ = false; + } else if (content_provider_without_length) { + req.content_length_ = 0; + req.content_provider_ = detail::ContentProviderAdapter( + std::move(content_provider_without_length)); + req.is_chunked_content_provider_ = true; + req.set_header("Transfer-Encoding", "chunked"); + } else { + req.body.assign(body, content_length); + } + } + + auto res = detail::make_unique(); + return send(req, *res, error) ? std::move(res) : nullptr; +} + +inline Result ClientImpl::send_with_content_provider( + const std::string &method, const std::string &path, const Headers &headers, + const char *body, size_t content_length, ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Progress progress) { + Request req; + req.method = method; + req.headers = headers; + req.path = path; + req.progress = progress; + + auto error = Error::Success; + + auto res = send_with_content_provider( + req, body, content_length, std::move(content_provider), + std::move(content_provider_without_length), content_type, error); + + return Result{std::move(res), error, std::move(req.headers)}; +} + +inline std::string +ClientImpl::adjust_host_string(const std::string &host) const { + if (host.find(':') != std::string::npos) { return "[" + host + "]"; } + return host; +} + +inline bool ClientImpl::process_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + // Send request + if (!write_request(strm, req, close_connection, error)) { return false; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl()) { + auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; + if (!is_proxy_enabled) { + char buf[1]; + if (SSL_peek(socket_.ssl, buf, 1) == 0 && + SSL_get_error(socket_.ssl, 0) == SSL_ERROR_ZERO_RETURN) { + error = Error::SSLPeerCouldBeClosed_; + return false; + } + } + } +#endif + + // Receive response and headers + if (!read_response_line(strm, req, res) || + !detail::read_headers(strm, res.headers)) { + error = Error::Read; + return false; + } + + // Body + if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && + req.method != "CONNECT") { + auto redirect = 300 < res.status && res.status < 400 && follow_location_; + + if (req.response_handler && !redirect) { + if (!req.response_handler(res)) { + error = Error::Canceled; + return false; + } + } + + auto out = + req.content_receiver + ? static_cast( + [&](const char *buf, size_t n, uint64_t off, uint64_t len) { + if (redirect) { return true; } + auto ret = req.content_receiver(buf, n, off, len); + if (!ret) { error = Error::Canceled; } + return ret; + }) + : static_cast( + [&](const char *buf, size_t n, uint64_t /*off*/, + uint64_t /*len*/) { + if (res.body.size() + n > res.body.max_size()) { + return false; + } + res.body.append(buf, n); + return true; + }); + + auto progress = [&](uint64_t current, uint64_t total) { + if (!req.progress || redirect) { return true; } + auto ret = req.progress(current, total); + if (!ret) { error = Error::Canceled; } + return ret; + }; + + if (res.has_header("Content-Length")) { + if (!req.content_receiver) { + auto len = std::min(res.get_header_value_u64("Content-Length"), + res.body.max_size()); + if (len > 0) { res.body.reserve(len); } + } + } + + int dummy_status; + if (!detail::read_content(strm, res, (std::numeric_limits::max)(), + dummy_status, std::move(progress), std::move(out), + decompress_)) { + if (error != Error::Canceled) { error = Error::Read; } + return false; + } + } + + // Log + if (logger_) { logger_(req, res); } + + return true; +} + +inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( + const std::string &boundary, const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) const { + size_t cur_item = 0; + size_t cur_start = 0; + // cur_item and cur_start are copied to within the std::function and maintain + // state between successive calls + return [&, cur_item, cur_start](size_t offset, + DataSink &sink) mutable -> bool { + if (!offset && !items.empty()) { + sink.os << detail::serialize_multipart_formdata(items, boundary, false); + return true; + } else if (cur_item < provider_items.size()) { + if (!cur_start) { + const auto &begin = detail::serialize_multipart_formdata_item_begin( + provider_items[cur_item], boundary); + offset += begin.size(); + cur_start = offset; + sink.os << begin; + } + + DataSink cur_sink; + auto has_data = true; + cur_sink.write = sink.write; + cur_sink.done = [&]() { has_data = false; }; + + if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) { + return false; + } + + if (!has_data) { + sink.os << detail::serialize_multipart_formdata_item_end(); + cur_item++; + cur_start = 0; + } + return true; + } else { + sink.os << detail::serialize_multipart_formdata_finish(boundary); + sink.done(); + return true; + } + }; +} + +inline bool +ClientImpl::process_socket(const Socket &socket, + std::function callback) { + return detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, std::move(callback)); +} + +inline bool ClientImpl::is_ssl() const { return false; } + +inline Result ClientImpl::Get(const std::string &path) { + return Get(path, Headers(), Progress()); +} + +inline Result ClientImpl::Get(const std::string &path, Progress progress) { + return Get(path, Headers(), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { + return Get(path, headers, Progress()); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + Progress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.progress = std::move(progress); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver) { + return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver) { + return Get(path, headers, nullptr, std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.response_handler = std::move(response_handler); + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.progress = std::move(progress); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress) { + if (params.empty()) { return Get(path, headers); } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, params, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + if (params.empty()) { + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); + } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Head(const std::string &path) { + return Head(path, Headers()); +} + +inline Result ClientImpl::Head(const std::string &path, + const Headers &headers) { + Request req; + req.method = "HEAD"; + req.headers = headers; + req.path = path; + + return send_(std::move(req)); +} + +inline Result ClientImpl::Post(const std::string &path) { + return Post(path, std::string(), std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, + const Headers &headers) { + return Post(path, headers, nullptr, 0, std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Post(path, Headers(), body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type) { + return Post(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return Post(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { + return Post(path, Headers(), params); +} + +inline Result ClientImpl::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Post(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Post(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + +inline Result ClientImpl::Post(const std::string &path, + const MultipartFormDataItems &items) { + return Post(path, Headers(), items); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Post(path, headers, body, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Post(path, headers, body, content_type); +} + +inline Result +ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "POST", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path) { + return Put(path, std::string(), std::string()); +} + +inline Result ClientImpl::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Put(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type) { + return Put(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return Put(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Put(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Put(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { + return Put(path, Headers(), params); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + +inline Result ClientImpl::Put(const std::string &path, + const MultipartFormDataItems &items) { + return Put(path, Headers(), items); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Put(path, headers, body, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Put(path, headers, body, content_type); +} + +inline Result +ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "PUT", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, nullptr); +} +inline Result ClientImpl::Patch(const std::string &path) { + return Patch(path, std::string(), std::string()); +} + +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Patch(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return Patch(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return Patch(path, headers, body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PATCH", path, headers, body, + content_length, nullptr, nullptr, + content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type) { + return Patch(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Patch(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return Patch(path, headers, body, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PATCH", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Patch(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Patch(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + nullptr); +} + +inline Result ClientImpl::Delete(const std::string &path) { + return Delete(path, Headers(), std::string(), std::string()); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers) { + return Delete(path, headers, std::string(), std::string()); +} + +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Delete(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return Delete(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type) { + return Delete(path, headers, body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + Request req; + req.method = "DELETE"; + req.headers = headers; + req.path = path; + req.progress = progress; + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + req.body.assign(body, content_length); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type) { + return Delete(path, Headers(), body.data(), body.size(), content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Delete(path, Headers(), body.data(), body.size(), content_type, + progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type) { + return Delete(path, headers, body.data(), body.size(), content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Delete(path, headers, body.data(), body.size(), content_type, + progress); +} + +inline Result ClientImpl::Options(const std::string &path) { + return Options(path, Headers()); +} + +inline Result ClientImpl::Options(const std::string &path, + const Headers &headers) { + Request req; + req.method = "OPTIONS"; + req.headers = headers; + req.path = path; + + return send_(std::move(req)); +} + +inline void ClientImpl::stop() { + std::lock_guard guard(socket_mutex_); + + // If there is anything ongoing right now, the ONLY thread-safe thing we can + // do is to shutdown_socket, so that threads using this socket suddenly + // discover they can't read/write any more and error out. Everything else + // (closing the socket, shutting ssl down) is unsafe because these actions are + // not thread-safe. + if (socket_requests_in_flight_ > 0) { + shutdown_socket(socket_); + + // Aside from that, we set a flag for the socket to be closed when we're + // done. + socket_should_be_closed_when_request_is_done_ = true; + return; + } + + // Otherwise, still holding the mutex, we can shut everything down ourselves + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline std::string ClientImpl::host() const { return host_; } + +inline int ClientImpl::port() const { return port_; } + +inline size_t ClientImpl::is_socket_open() const { + std::lock_guard guard(socket_mutex_); + return socket_.is_open(); +} + +inline socket_t ClientImpl::socket() const { return socket_.sock; } + +inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { + connection_timeout_sec_ = sec; + connection_timeout_usec_ = usec; +} + +inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; +} + +inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; +} + +inline void ClientImpl::set_basic_auth(const std::string &username, + const std::string &password) { + basic_auth_username_ = username; + basic_auth_password_ = password; +} + +inline void ClientImpl::set_bearer_token_auth(const std::string &token) { + bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_digest_auth(const std::string &username, + const std::string &password) { + digest_auth_username_ = username; + digest_auth_password_ = password; +} +#endif + +inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } + +inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } + +inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } + +inline void +ClientImpl::set_hostname_addr_map(std::map addr_map) { + addr_map_ = std::move(addr_map); +} + +inline void ClientImpl::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); +} + +inline void ClientImpl::set_header_writer( + std::function const &writer) { + header_writer_ = writer; +} + +inline void ClientImpl::set_address_family(int family) { + address_family_ = family; +} + +inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } + +inline void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; } + +inline void ClientImpl::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); +} + +inline void ClientImpl::set_compress(bool on) { compress_ = on; } + +inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } + +inline void ClientImpl::set_interface(const std::string &intf) { + interface_ = intf; +} + +inline void ClientImpl::set_proxy(const std::string &host, int port) { + proxy_host_ = host; + proxy_port_ = port; +} + +inline void ClientImpl::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + proxy_basic_auth_username_ = username; + proxy_basic_auth_password_ = password; +} + +inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { + proxy_bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + proxy_digest_auth_username_ = username; + proxy_digest_auth_password_ = password; +} + +inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + ca_cert_file_path_ = ca_cert_file_path; + ca_cert_dir_path_ = ca_cert_dir_path; +} + +inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store && ca_cert_store != ca_cert_store_) { + ca_cert_store_ = ca_cert_store; + } +} + +inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, + std::size_t size) const { + auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); + if (!mem) { return nullptr; } + + auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); + if (!inf) { + BIO_free_all(mem); + return nullptr; + } + + auto cts = X509_STORE_new(); + if (cts) { + for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { + auto itmp = sk_X509_INFO_value(inf, i); + if (!itmp) { continue; } + + if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } + if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); } + } + } + + sk_X509_INFO_pop_free(inf, X509_INFO_free); + BIO_free_all(mem); + return cts; +} + +inline void ClientImpl::enable_server_certificate_verification(bool enabled) { + server_certificate_verification_ = enabled; +} +#endif + +inline void ClientImpl::set_logger(Logger logger) { + logger_ = std::move(logger); +} + +/* + * SSL Implementation + */ +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +namespace detail { + +template +inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, + U SSL_connect_or_accept, V setup) { + SSL *ssl = nullptr; + { + std::lock_guard guard(ctx_mutex); + ssl = SSL_new(ctx); + } + + if (ssl) { + set_nonblocking(sock, true); + auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); + BIO_set_nbio(bio, 1); + SSL_set_bio(ssl, bio, bio); + + if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { + SSL_shutdown(ssl); + { + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); + } + set_nonblocking(sock, false); + return nullptr; + } + BIO_set_nbio(bio, 0); + set_nonblocking(sock, false); + } + + return ssl; +} + +inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, + bool shutdown_gracefully) { + // sometimes we may want to skip this to try to avoid SIGPIPE if we know + // the remote has closed the network connection + // Note that it is not always possible to avoid SIGPIPE, this is merely a + // best-efforts. + if (shutdown_gracefully) { +#ifdef _WIN32 + SSL_shutdown(ssl); +#else + timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); + + auto ret = SSL_shutdown(ssl); + while (ret == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + ret = SSL_shutdown(ssl); + } +#endif + } + + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); +} + +template +bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, + U ssl_connect_or_accept, + time_t timeout_sec, + time_t timeout_usec) { + auto res = 0; + while ((res = ssl_connect_or_accept(ssl)) != 1) { + auto err = SSL_get_error(ssl, res); + switch (err) { + case SSL_ERROR_WANT_READ: + if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + case SSL_ERROR_WANT_WRITE: + if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + default: break; + } + return false; + } + return true; +} + +template +inline bool process_server_socket_ssl( + const std::atomic &svr_sock, SSL *ssl, socket_t sock, + size_t keep_alive_max_count, time_t keep_alive_timeout_sec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +template +inline bool +process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm); +} + +class SSLInit { +public: + SSLInit() { + OPENSSL_init_ssl( + OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); + } +}; + +// SSL socket stream implementation +inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, + time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec) + : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec) { + SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); +} + +inline SSLSocketStream::~SSLSocketStream() = default; + +inline bool SSLSocketStream::is_readable() const { + return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; +} + +inline bool SSLSocketStream::is_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_); +} + +inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (is_readable()) { + auto ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_READ || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_READ) { +#endif + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (is_readable()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + return -1; + } + } + } + return ret; + } + return -1; +} + +inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { + if (is_writable()) { + auto handle_size = static_cast( + std::min(size, (std::numeric_limits::max)())); + + auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { +#endif + if (is_writable()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + return -1; + } + } + } + return ret; + } + return -1; +} + +inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SSLSocketStream::socket() const { return sock_; } + +static SSLInit sslinit_; + +} // namespace detail + +// SSL HTTP server implementation +inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path, + const char *client_ca_cert_dir_path, + const char *private_key_password) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + if (private_key_password != nullptr && (private_key_password[0] != '\0')) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, + reinterpret_cast(const_cast(private_key_password))); + } + + if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != + 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { + SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, + client_ca_cert_dir_path); + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + if (SSL_CTX_use_certificate(ctx_, cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_store) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer( + const std::function &setup_ssl_ctx_callback) { + ctx_ = SSL_CTX_new(TLS_method()); + if (ctx_) { + if (!setup_ssl_ctx_callback(*ctx_)) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLServer::~SSLServer() { + if (ctx_) { SSL_CTX_free(ctx_); } +} + +inline bool SSLServer::is_valid() const { return ctx_; } + +inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } + +inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + + std::lock_guard guard(ctx_mutex_); + + SSL_CTX_use_certificate(ctx_, cert); + SSL_CTX_use_PrivateKey(ctx_, private_key); + + if (client_ca_cert_store != nullptr) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + } +} + +inline bool SSLServer::process_and_close_socket(socket_t sock) { + auto ssl = detail::ssl_new( + sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + return detail::ssl_connect_or_accept_nonblocking( + sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); + }, + [](SSL * /*ssl2*/) { return true; }); + + auto ret = false; + if (ssl) { + ret = detail::process_server_socket_ssl( + svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [this, ssl](Stream &strm, bool close_connection, + bool &connection_closed) { + return process_request(strm, close_connection, connection_closed, + [&](Request &req) { req.ssl = ssl; }); + }); + + // Shutdown gracefully if the result seemed successful, non-gracefully if + // the connection appeared to be closed. + const bool shutdown_gracefully = ret; + detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); + } + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +// SSL HTTP client implementation +inline SSLClient::SSLClient(const std::string &host) + : SSLClient(host, 443, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port) + : SSLClient(host, port, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password) + : ClientImpl(host, port, client_cert_path, client_key_path) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(b, e); + }); + + if (!client_cert_path.empty() && !client_key_path.empty()) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + + if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), + SSL_FILETYPE_PEM) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), + SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::SSLClient(const std::string &host, int port, + X509 *client_cert, EVP_PKEY *client_key, + const std::string &private_key_password) + : ClientImpl(host, port) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(b, e); + }); + + if (client_cert != nullptr && client_key != nullptr) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + + if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::~SSLClient() { + if (ctx_) { SSL_CTX_free(ctx_); } + // Make sure to shut down SSL since shutdown_ssl will resolve to the + // base function rather than the derived function once we get to the + // base class destructor, and won't free the SSL (causing a leak). + shutdown_ssl_impl(socket_, true); +} + +inline bool SSLClient::is_valid() const { return ctx_; } + +inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store) { + if (ctx_) { + if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { + // Free memory allocated for old cert and use new store `ca_cert_store` + SSL_CTX_set_cert_store(ctx_, ca_cert_store); + } + } else { + X509_STORE_free(ca_cert_store); + } + } +} + +inline void SSLClient::load_ca_cert_store(const char *ca_cert, + std::size_t size) { + set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); +} + +inline long SSLClient::get_openssl_verify_result() const { + return verify_result_; +} + +inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } + +inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { + return is_valid() && ClientImpl::create_and_connect_socket(socket, error); +} + +// Assumes that socket_mutex_ is locked and that there are no requests in flight +inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, + bool &success, Error &error) { + success = true; + Response proxy_res; + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + Request req2; + req2.method = "CONNECT"; + req2.path = host_and_port_; + return process_request(strm, req2, proxy_res, false, error); + })) { + // Thread-safe to close everything because we are assuming there are no + // requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + + if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) { + if (!proxy_digest_auth_username_.empty() && + !proxy_digest_auth_password_.empty()) { + std::map auth; + if (detail::parse_www_authenticate(proxy_res, auth, true)) { + proxy_res = Response(); + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + Request req3; + req3.method = "CONNECT"; + req3.path = host_and_port_; + req3.headers.insert(detail::make_digest_authentication_header( + req3, auth, 1, detail::random_string(10), + proxy_digest_auth_username_, proxy_digest_auth_password_, + true)); + return process_request(strm, req3, proxy_res, false, error); + })) { + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + } + } + } + + // If status code is not 200, proxy request is failed. + // Set error to ProxyConnection and return proxy response + // as the response of the request + if (proxy_res.status != StatusCode::OK_200) { + error = Error::ProxyConnection; + res = std::move(proxy_res); + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + return false; + } + + return true; +} + +inline bool SSLClient::load_certs() { + auto ret = true; + + std::call_once(initialize_cert_, [&]() { + std::lock_guard guard(ctx_mutex_); + if (!ca_cert_file_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), + nullptr)) { + ret = false; + } + } else if (!ca_cert_dir_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, nullptr, + ca_cert_dir_path_.c_str())) { + ret = false; + } + } else { + auto loaded = false; +#ifdef _WIN32 + loaded = + detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#if TARGET_OS_OSX + loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); +#endif // TARGET_OS_OSX +#endif // _WIN32 + if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } + } + }); + + return ret; +} + +inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { + auto ssl = detail::ssl_new( + socket.sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + if (server_certificate_verification_) { + if (!load_certs()) { + error = Error::SSLLoadingCerts; + return false; + } + SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); + } + + if (!detail::ssl_connect_or_accept_nonblocking( + socket.sock, ssl2, SSL_connect, connection_timeout_sec_, + connection_timeout_usec_)) { + error = Error::SSLConnection; + return false; + } + + if (server_certificate_verification_) { + verify_result_ = SSL_get_verify_result(ssl2); + + if (verify_result_ != X509_V_OK) { + error = Error::SSLServerVerification; + return false; + } + + auto server_cert = SSL_get1_peer_certificate(ssl2); + + if (server_cert == nullptr) { + error = Error::SSLServerVerification; + return false; + } + + if (!verify_host(server_cert)) { + X509_free(server_cert); + error = Error::SSLServerVerification; + return false; + } + X509_free(server_cert); + } + + return true; + }, + [&](SSL *ssl2) { +#if defined(OPENSSL_IS_BORINGSSL) + SSL_set_tlsext_host_name(ssl2, host_.c_str()); +#else + // NOTE: Direct call instead of using the OpenSSL macro to suppress + // -Wold-style-cast warning + SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, + static_cast(const_cast(host_.c_str()))); +#endif + return true; + }); + + if (ssl) { + socket.ssl = ssl; + return true; + } + + shutdown_socket(socket); + close_socket(socket); + return false; +} + +inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { + shutdown_ssl_impl(socket, shutdown_gracefully); +} + +inline void SSLClient::shutdown_ssl_impl(Socket &socket, + bool shutdown_gracefully) { + if (socket.sock == INVALID_SOCKET) { + assert(socket.ssl == nullptr); + return; + } + if (socket.ssl) { + detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, + shutdown_gracefully); + socket.ssl = nullptr; + } + assert(socket.ssl == nullptr); +} + +inline bool +SSLClient::process_socket(const Socket &socket, + std::function callback) { + assert(socket.ssl); + return detail::process_client_socket_ssl( + socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, std::move(callback)); +} + +inline bool SSLClient::is_ssl() const { return true; } + +inline bool SSLClient::verify_host(X509 *server_cert) const { + /* Quote from RFC2818 section 3.1 "Server Identity" + + If a subjectAltName extension of type dNSName is present, that MUST + be used as the identity. Otherwise, the (most specific) Common Name + field in the Subject field of the certificate MUST be used. Although + the use of the Common Name is existing practice, it is deprecated and + Certification Authorities are encouraged to use the dNSName instead. + + Matching is performed using the matching rules specified by + [RFC2459]. If more than one identity of a given type is present in + the certificate (e.g., more than one dNSName name, a match in any one + of the set is considered acceptable.) Names may contain the wildcard + character * which is considered to match any single domain name + component or component fragment. E.g., *.a.com matches foo.a.com but + not bar.foo.a.com. f*.com matches foo.com but not bar.com. + + In some cases, the URI is specified as an IP address rather than a + hostname. In this case, the iPAddress subjectAltName must be present + in the certificate and must exactly match the IP in the URI. + + */ + return verify_host_with_subject_alt_name(server_cert) || + verify_host_with_common_name(server_cert); +} + +inline bool +SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { + auto ret = false; + + auto type = GEN_DNS; + + struct in6_addr addr6 {}; + struct in_addr addr {}; + size_t addr_len = 0; + +#ifndef __MINGW32__ + if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { + type = GEN_IPADD; + addr_len = sizeof(struct in6_addr); + } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { + type = GEN_IPADD; + addr_len = sizeof(struct in_addr); + } +#endif + + auto alt_names = static_cast( + X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); + + if (alt_names) { + auto dsn_matched = false; + auto ip_matched = false; + + auto count = sk_GENERAL_NAME_num(alt_names); + + for (decltype(count) i = 0; i < count && !dsn_matched; i++) { + auto val = sk_GENERAL_NAME_value(alt_names, i); + if (val->type == type) { + auto name = + reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); + auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); + + switch (type) { + case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + + case GEN_IPADD: + if (!memcmp(&addr6, name, addr_len) || + !memcmp(&addr, name, addr_len)) { + ip_matched = true; + } + break; + } + } + } + + if (dsn_matched || ip_matched) { ret = true; } + } + + GENERAL_NAMES_free(const_cast( + reinterpret_cast(alt_names))); + return ret; +} + +inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { + const auto subject_name = X509_get_subject_name(server_cert); + + if (subject_name != nullptr) { + char name[BUFSIZ]; + auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, + name, sizeof(name)); + + if (name_len != -1) { + return check_host_name(name, static_cast(name_len)); + } + } + + return false; +} + +inline bool SSLClient::check_host_name(const char *pattern, + size_t pattern_len) const { + if (host_.size() == pattern_len && host_ == pattern) { return true; } + + // Wildcard match + // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 + std::vector pattern_components; + detail::split(&pattern[0], &pattern[pattern_len], '.', + [&](const char *b, const char *e) { + pattern_components.emplace_back(b, e); + }); + + if (host_components_.size() != pattern_components.size()) { return false; } + + auto itr = pattern_components.begin(); + for (const auto &h : host_components_) { + auto &p = *itr; + if (p != h && p != "*") { + auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && + !p.compare(0, p.size() - 1, h)); + if (!partial_match) { return false; } + } + ++itr; + } + + return true; +} +#endif + +// Universal client implementation +inline Client::Client(const std::string &scheme_host_port) + : Client(scheme_host_port, std::string(), std::string()) {} + +inline Client::Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path) { + const static std::regex re( + R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); + + std::smatch m; + if (std::regex_match(scheme_host_port, m, re)) { + auto scheme = m[1].str(); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!scheme.empty() && (scheme != "http" && scheme != "https")) { +#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; + } + + auto is_ssl = scheme == "https"; + + auto host = m[2].str(); + if (host.empty()) { host = m[3].str(); } + + auto port_str = m[4].str(); + auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); + + if (is_ssl) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + is_ssl_ = is_ssl; +#endif + } else { + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + } + } else { + // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) + // if port param below changes. + cli_ = detail::make_unique(scheme_host_port, 80, + client_cert_path, client_key_path); + } +} // namespace detail + +inline Client::Client(const std::string &host, int port) + : cli_(detail::make_unique(host, port)) {} + +inline Client::Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : cli_(detail::make_unique(host, port, client_cert_path, + client_key_path)) {} + +inline Client::~Client() = default; + +inline bool Client::is_valid() const { + return cli_ != nullptr && cli_->is_valid(); +} + +inline Result Client::Get(const std::string &path) { return cli_->Get(path); } +inline Result Client::Get(const std::string &path, const Headers &headers) { + return cli_->Get(path, headers); +} +inline Result Client::Get(const std::string &path, Progress progress) { + return cli_->Get(path, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + Progress progress) { + return cli_->Get(path, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress) { + return cli_->Get(path, params, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result Client::Head(const std::string &path) { return cli_->Head(path); } +inline Result Client::Head(const std::string &path, const Headers &headers) { + return cli_->Head(path, headers); +} + +inline Result Client::Post(const std::string &path) { return cli_->Post(path); } +inline Result Client::Post(const std::string &path, const Headers &headers) { + return cli_->Post(path, headers); +} +inline Result Client::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Post(path, body, content_length, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Post(path, headers, body, content_length, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Post(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Post(path, body, content_type); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Post(path, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Post(path, headers, body, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Post(path, headers, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Post(path, std::move(content_provider), content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Post(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Post(const std::string &path, const Params ¶ms) { + return cli_->Post(path, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Post(path, headers, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Post(path, headers, params, progress); +} +inline Result Client::Post(const std::string &path, + const MultipartFormDataItems &items) { + return cli_->Post(path, items); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + return cli_->Post(path, headers, items); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + return cli_->Post(path, headers, items, boundary); +} +inline Result +Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + return cli_->Post(path, headers, items, provider_items); +} +inline Result Client::Put(const std::string &path) { return cli_->Put(path); } +inline Result Client::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Put(path, body, content_length, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Put(path, headers, body, content_length, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Put(path, headers, body, content_length, content_type, progress); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Put(path, body, content_type); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Put(path, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Put(path, headers, body, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Put(path, headers, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Put(path, std::move(content_provider), content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Put(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Put(const std::string &path, const Params ¶ms) { + return cli_->Put(path, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Put(path, headers, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Put(path, headers, params, progress); +} +inline Result Client::Put(const std::string &path, + const MultipartFormDataItems &items) { + return cli_->Put(path, items); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + return cli_->Put(path, headers, items); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + return cli_->Put(path, headers, items, boundary); +} +inline Result +Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + return cli_->Put(path, headers, items, provider_items); +} +inline Result Client::Patch(const std::string &path) { + return cli_->Patch(path); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Patch(path, body, content_length, content_type); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, body, content_length, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Patch(path, headers, body, content_length, content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Patch(path, body, content_type); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Patch(path, headers, body, content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, headers, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Patch(path, std::move(content_provider), content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Patch(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Delete(const std::string &path) { + return cli_->Delete(path); +} +inline Result Client::Delete(const std::string &path, const Headers &headers) { + return cli_->Delete(path, headers); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Delete(path, body, content_length, content_type); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, body, content_length, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Delete(path, headers, body, content_length, content_type); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Delete(path, body, content_type); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, body, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Delete(path, headers, body, content_type); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, headers, body, content_type, progress); +} +inline Result Client::Options(const std::string &path) { + return cli_->Options(path); +} +inline Result Client::Options(const std::string &path, const Headers &headers) { + return cli_->Options(path, headers); +} + +inline bool Client::send(Request &req, Response &res, Error &error) { + return cli_->send(req, res, error); +} + +inline Result Client::send(const Request &req) { return cli_->send(req); } + +inline void Client::stop() { cli_->stop(); } + +inline std::string Client::host() const { return cli_->host(); } + +inline int Client::port() const { return cli_->port(); } + +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + +inline socket_t Client::socket() const { return cli_->socket(); } + +inline void +Client::set_hostname_addr_map(std::map addr_map) { + cli_->set_hostname_addr_map(std::move(addr_map)); +} + +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); +} + +inline void Client::set_header_writer( + std::function const &writer) { + cli_->set_header_writer(writer); +} + +inline void Client::set_address_family(int family) { + cli_->set_address_family(family); +} + +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } + +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(std::move(socket_options)); +} + +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); +} + +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); +} + +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); +} + +inline void Client::set_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_basic_auth(username, password); +} +inline void Client::set_bearer_token_auth(const std::string &token) { + cli_->set_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_digest_auth(username, password); +} +#endif + +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); +} + +inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); } + +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const std::string &intf) { + cli_->set_interface(intf); +} + +inline void Client::set_proxy(const std::string &host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const std::string &token) { + cli_->set_proxy_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_digest_auth(username, password); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); +} +#endif + +inline void Client::set_logger(Logger logger) { + cli_->set_logger(std::move(logger)); +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +} + +inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } else { + cli_->set_ca_cert_store(ca_cert_store); + } +} + +inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { + set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); +} + +inline long Client::get_openssl_verify_result() const { + if (is_ssl_) { + return static_cast(*cli_).get_openssl_verify_result(); + } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +} + +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; +} +#endif + +// ---------------------------------------------------------------------------- + +} // namespace httplib + +#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL) +#undef poll +#endif + +#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/benchmark/cpp-httplib-base/main.cpp b/benchmark/cpp-httplib-base/main.cpp new file mode 100644 index 0000000000..86070a100c --- /dev/null +++ b/benchmark/cpp-httplib-base/main.cpp @@ -0,0 +1,12 @@ +#include "./httplib.h" +using namespace httplib; + +int main() { + Server svr; + + svr.Get("/", [](const Request &, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + svr.listen("0.0.0.0", 8080); +} diff --git a/benchmark/download.sh b/benchmark/download.sh new file mode 100755 index 0000000000..90c959ed85 --- /dev/null +++ b/benchmark/download.sh @@ -0,0 +1,2 @@ +rm -f httplib.h +wget https://raw.githubusercontent.com/yhirose/cpp-httplib/v$1/httplib.h From f35aff84c2ae62e3caffd72b5ba67e3c636be0cc Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 19:00:23 -0400 Subject: [PATCH 0278/1222] Fixed FuzzableServer build error --- test/fuzzing/server_fuzzer.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/fuzzing/server_fuzzer.cc b/test/fuzzing/server_fuzzer.cc index 3c0e948026..3cffbae244 100644 --- a/test/fuzzing/server_fuzzer.cc +++ b/test/fuzzing/server_fuzzer.cc @@ -50,8 +50,12 @@ class FuzzableServer : public httplib::Server { public: void ProcessFuzzedRequest(FuzzedStream &stream) { bool connection_close = false; - process_request(stream, /*last_connection=*/false, connection_close, - nullptr); + process_request(stream, + /*remote_addr=*/"", + /*remote_port =*/0, + /*local_addr=*/"", + /*local_port =*/0, + /*last_connection=*/false, connection_close, nullptr); } }; From ea79494b29480354fb957aff1e8b9a46eafb696a Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 20:08:52 -0400 Subject: [PATCH 0279/1222] Renamed enable_server_host_verification to enable_server_hostname_verification and added Error::SSLServerHostnameVerification --- httplib.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index d56475acfc..6ec8112a7d 100644 --- a/httplib.h +++ b/httplib.h @@ -1135,6 +1135,7 @@ enum class Error { SSLConnection, SSLLoadingCerts, SSLServerVerification, + SSLServerHostnameVerification, UnsupportedMultipartBoundaryChars, Compression, ConnectionTimeout, @@ -1450,7 +1451,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); - void enable_server_host_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); #endif void set_logger(Logger logger); @@ -1565,7 +1566,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT bool server_certificate_verification_ = true; - bool server_host_verification_ = true; + bool server_hostname_verification_ = true; #endif Logger logger_; @@ -1871,7 +1872,7 @@ class Client { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); - void enable_server_host_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); #endif void set_logger(Logger logger); @@ -2163,6 +2164,8 @@ inline std::string to_string(const Error error) { case Error::SSLConnection: return "SSL connection failed"; case Error::SSLLoadingCerts: return "SSL certificate loading failed"; case Error::SSLServerVerification: return "SSL server verification failed"; + case Error::SSLServerHostnameVerification: + return "SSL server hostname verification failed"; case Error::UnsupportedMultipartBoundaryChars: return "Unsupported HTTP multipart boundary characters"; case Error::Compression: return "Compression failed"; @@ -8726,8 +8729,8 @@ inline void ClientImpl::enable_server_certificate_verification(bool enabled) { server_certificate_verification_ = enabled; } -inline void ClientImpl::enable_server_host_verification(bool enabled) { - server_host_verification_ = enabled; +inline void ClientImpl::enable_server_hostname_verification(bool enabled) { + server_hostname_verification_ = enabled; } #endif @@ -9319,21 +9322,19 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { } auto server_cert = SSL_get1_peer_certificate(ssl2); + auto se = detail::scope_exit([&] { X509_free(server_cert); }); if (server_cert == nullptr) { error = Error::SSLServerVerification; return false; } - if (server_host_verification_) { + if (server_hostname_verification_) { if (!verify_host(server_cert)) { - X509_free(server_cert); - error = Error::SSLServerVerification; + error = Error::SSLServerHostnameVerification; return false; } } - - X509_free(server_cert); } return true; @@ -10065,8 +10066,8 @@ inline void Client::enable_server_certificate_verification(bool enabled) { cli_->enable_server_certificate_verification(enabled); } -inline void Client::enable_server_host_verification(bool enabled) { - cli_->enable_server_host_verification(enabled); +inline void Client::enable_server_hostname_verification(bool enabled) { + cli_->enable_server_hostname_verification(enabled); } #endif From dbd2465b56f55d33664db6127919c54a9cf30c25 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 20:10:17 -0400 Subject: [PATCH 0280/1222] Add some Open SSL function calls --- httplib.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 6ec8112a7d..089f339fb6 100644 --- a/httplib.h +++ b/httplib.h @@ -8983,7 +8983,8 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != - 1) { + 1 || + SSL_CTX_check_private_key(ctx_) != 1) { SSL_CTX_free(ctx_); ctx_ = nullptr; } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { @@ -9107,6 +9108,8 @@ inline SSLClient::SSLClient(const std::string &host, int port, : ClientImpl(host, port, client_cert_path, client_key_path) { ctx_ = SSL_CTX_new(TLS_client_method()); + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + detail::split(&host_[0], &host_[host_.size()], '.', [&](const char *b, const char *e) { host_components_.emplace_back(b, e); From 82fcbe39012a2d5767ac224db97b3d9fac811c73 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 20:10:35 -0400 Subject: [PATCH 0281/1222] Code cleanup --- httplib.h | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index 089f339fb6..7e5a95f88d 100644 --- a/httplib.h +++ b/httplib.h @@ -3374,6 +3374,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #endif return INVALID_SOCKET; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); for (auto rp = result; rp; rp = rp->ai_next) { // Create a socket @@ -3443,17 +3444,13 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, // bind or connect auto quit = false; - if (bind_or_connect(sock, *rp, quit)) { - freeaddrinfo(result); - return sock; - } + if (bind_or_connect(sock, *rp, quit)) { return sock; } close_socket(sock); if (quit) { break; } } - freeaddrinfo(result); return INVALID_SOCKET; } @@ -3486,6 +3483,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { hints.ai_protocol = 0; if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); auto ret = false; for (auto rp = result; rp; rp = rp->ai_next) { @@ -3496,7 +3494,6 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { } } - freeaddrinfo(result); return ret; } @@ -3508,6 +3505,8 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { inline std::string if2ip(int address_family, const std::string &ifn) { struct ifaddrs *ifap; getifaddrs(&ifap); + auto se = detail::scope_exit([&] { freeifaddrs(ifap); }); + std::string addr_candidate; for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr && ifn == ifa->ifa_name && @@ -3517,7 +3516,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) { auto sa = reinterpret_cast(ifa->ifa_addr); char buf[INET_ADDRSTRLEN]; if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { - freeifaddrs(ifap); return std::string(buf, INET_ADDRSTRLEN); } } else if (ifa->ifa_addr->sa_family == AF_INET6) { @@ -3530,7 +3528,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) { if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { addr_candidate = std::string(buf, INET6_ADDRSTRLEN); } else { - freeifaddrs(ifap); return std::string(buf, INET6_ADDRSTRLEN); } } @@ -3538,7 +3535,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) { } } } - freeifaddrs(ifap); return addr_candidate; } #endif @@ -5539,6 +5535,7 @@ inline void hosted_at(const std::string &hostname, #endif return; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); for (auto rp = result; rp; rp = rp->ai_next) { const auto &addr = @@ -5550,8 +5547,6 @@ inline void hosted_at(const std::string &hostname, addrs.push_back(ip); } } - - freeaddrinfo(result); } inline std::string append_query_params(const std::string &path, @@ -7223,7 +7218,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT server_certificate_verification_ = rhs.server_certificate_verification_; - server_host_verification_ = rhs.server_host_verification_; + server_hostname_verification_ = rhs.server_hostname_verification_; #endif logger_ = rhs.logger_; } @@ -8701,11 +8696,11 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, std::size_t size) const { auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); + auto se = detail::scope_exit([&] { BIO_free_all(mem); }); if (!mem) { return nullptr; } auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); if (!inf) { - BIO_free_all(mem); return nullptr; } @@ -8721,7 +8716,6 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, } sk_X509_INFO_pop_free(inf, X509_INFO_free); - BIO_free_all(mem); return cts; } From e612154694e5f5fa05fc38366b17a36fe5693d71 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Sep 2024 22:45:47 -0400 Subject: [PATCH 0282/1222] Issue1431 (#1926) * Renamed enable_server_host_verification to enable_server_hostname_verification and added Error::SSLServerHostnameVerification * Add some Open SSL function calls * Code cleanup * Fix #1431 --- httplib.h | 55 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/httplib.h b/httplib.h index 7e5a95f88d..56af371dcc 100644 --- a/httplib.h +++ b/httplib.h @@ -1452,6 +1452,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier(std::function verifier); #endif void set_logger(Logger logger); @@ -1567,6 +1568,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT bool server_certificate_verification_ = true; bool server_hostname_verification_ = true; + std::function server_certificate_verifier_; #endif Logger logger_; @@ -1873,6 +1875,7 @@ class Client { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier(std::function verifier); #endif void set_logger(Logger logger); @@ -7219,6 +7222,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT server_certificate_verification_ = rhs.server_certificate_verification_; server_hostname_verification_ = rhs.server_hostname_verification_; + server_certificate_verifier_ = rhs.server_certificate_verifier_; #endif logger_ = rhs.logger_; } @@ -8700,9 +8704,7 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, if (!mem) { return nullptr; } auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); - if (!inf) { - return nullptr; - } + if (!inf) { return nullptr; } auto cts = X509_STORE_new(); if (cts) { @@ -8726,6 +8728,11 @@ inline void ClientImpl::enable_server_certificate_verification(bool enabled) { inline void ClientImpl::enable_server_hostname_verification(bool enabled) { server_hostname_verification_ = enabled; } + +inline void ClientImpl::set_server_certificate_verifier( + std::function verifier) { + server_certificate_verifier_ = verifier; +} #endif inline void ClientImpl::set_logger(Logger logger) { @@ -9311,26 +9318,33 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { } if (server_certificate_verification_) { - verify_result_ = SSL_get_verify_result(ssl2); - - if (verify_result_ != X509_V_OK) { - error = Error::SSLServerVerification; - return false; - } + if (server_certificate_verifier_) { + if (!server_certificate_verifier_(ssl2)) { + error = Error::SSLServerVerification; + return false; + } + } else { + verify_result_ = SSL_get_verify_result(ssl2); - auto server_cert = SSL_get1_peer_certificate(ssl2); - auto se = detail::scope_exit([&] { X509_free(server_cert); }); + if (verify_result_ != X509_V_OK) { + error = Error::SSLServerVerification; + return false; + } - if (server_cert == nullptr) { - error = Error::SSLServerVerification; - return false; - } + auto server_cert = SSL_get1_peer_certificate(ssl2); + auto se = detail::scope_exit([&] { X509_free(server_cert); }); - if (server_hostname_verification_) { - if (!verify_host(server_cert)) { - error = Error::SSLServerHostnameVerification; + if (server_cert == nullptr) { + error = Error::SSLServerVerification; return false; } + + if (server_hostname_verification_) { + if (!verify_host(server_cert)) { + error = Error::SSLServerHostnameVerification; + return false; + } + } } } @@ -10066,6 +10080,11 @@ inline void Client::enable_server_certificate_verification(bool enabled) { inline void Client::enable_server_hostname_verification(bool enabled) { cli_->enable_server_hostname_verification(enabled); } + +inline void Client::set_server_certificate_verifier( + std::function verifier) { + cli_->set_server_certificate_verifier(verifier); +} #endif inline void Client::set_logger(Logger logger) { From 2d01e712866d3ed17d33569b8fa5345f5cade146 Mon Sep 17 00:00:00 2001 From: Paul Harris Date: Tue, 10 Sep 2024 05:54:11 +0800 Subject: [PATCH 0283/1222] Test reading empty zero-length file (#1931) --- test/test.cc | 9 +++++++++ test/www/empty_file | 0 2 files changed, 9 insertions(+) create mode 100644 test/www/empty_file diff --git a/test/test.cc b/test/test.cc index e3e202a749..a0fd061529 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2916,6 +2916,15 @@ TEST_F(ServerTest, GetMethod200) { EXPECT_EQ("Hello World!", res->body); } +TEST_F(ServerTest, GetEmptyFile) { + auto res = cli_.Get("/empty_file"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ(0, std::stoi(res->get_header_value("Content-Length"))); + EXPECT_EQ("", res->body); +} + TEST_F(ServerTest, GetFileContent) { auto res = cli_.Get("/file_content"); ASSERT_TRUE(res); diff --git a/test/www/empty_file b/test/www/empty_file new file mode 100644 index 0000000000..e69de29bb2 From 509f583dca0ae55c22e5a59ae12a2d3f160e8379 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 19:19:52 -0400 Subject: [PATCH 0284/1222] Fix problem caused by #1931. --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index a0fd061529..05a9744973 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2920,7 +2920,7 @@ TEST_F(ServerTest, GetEmptyFile) { auto res = cli_.Get("/empty_file"); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); - EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type")); EXPECT_EQ(0, std::stoi(res->get_header_value("Content-Length"))); EXPECT_EQ("", res->body); } From 3f2922b3faab7d77507ad23c63a33cceeaa7c054 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 19:25:52 -0400 Subject: [PATCH 0285/1222] Fix #1929 --- httplib.h | 33 +++++++++++++++++++++------------ test/test.cc | 10 ++++++---- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 56af371dcc..40bcb8b212 100644 --- a/httplib.h +++ b/httplib.h @@ -2253,9 +2253,15 @@ make_basic_authentication_header(const std::string &username, namespace detail { -bool is_file(const std::string &path); +struct FileStat { + FileStat(const std::string &path); + bool is_file() const; + bool is_dir() const; -bool is_dir(const std::string &path); +private: + struct stat st_; + int ret_ = -1; +}; std::string encode_query_param(const std::string &value); @@ -2626,14 +2632,14 @@ inline bool is_valid_path(const std::string &path) { return true; } -inline bool is_file(const std::string &path) { - struct stat st; - return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); +inline FileStat::FileStat(const std::string &path) { + ret_ = stat(path.c_str(), &st_); } - -inline bool is_dir(const std::string &path) { - struct stat st; - return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); +inline bool FileStat::is_file() const { + return ret_ >= 0 && S_ISREG(st_.st_mode); +} +inline bool FileStat::is_dir() const { + return ret_ >= 0 && S_ISDIR(st_.st_mode); } inline std::string encode_query_param(const std::string &value) { @@ -6085,7 +6091,8 @@ inline bool Server::set_base_dir(const std::string &dir, inline bool Server::set_mount_point(const std::string &mount_point, const std::string &dir, Headers headers) { - if (detail::is_dir(dir)) { + detail::FileStat stat(dir); + if (stat.is_dir()) { std::string mnt = !mount_point.empty() ? mount_point : "/"; if (!mnt.empty() && mnt[0] == '/') { base_dirs_.push_back({mnt, dir, std::move(headers)}); @@ -6569,12 +6576,14 @@ inline bool Server::handle_file_request(const Request &req, Response &res, auto path = entry.base_dir + sub_path; if (path.back() == '/') { path += "index.html"; } - if (detail::is_dir(path)) { + detail::FileStat stat(path); + + if (stat.is_dir()) { res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); return true; } - if (detail::is_file(path)) { + if (stat.is_file()) { for (const auto &kv : entry.headers) { res.set_header(kv.first, kv.second); } diff --git a/test/test.cc b/test/test.cc index 05a9744973..b9bc977bce 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7676,11 +7676,13 @@ TEST(FileSystemTest, FileAndDirExistenceCheck) { auto file_path = "./www/dir/index.html"; auto dir_path = "./www/dir"; - EXPECT_TRUE(detail::is_file(file_path)); - EXPECT_FALSE(detail::is_dir(file_path)); + detail::FileStat stat_file(file_path); + EXPECT_TRUE(stat_file.is_file()); + EXPECT_FALSE(stat_file.is_dir()); - EXPECT_FALSE(detail::is_file(dir_path)); - EXPECT_TRUE(detail::is_dir(dir_path)); + detail::FileStat stat_dir(dir_path); + EXPECT_FALSE(stat_dir.is_file()); + EXPECT_TRUE(stat_dir.is_dir()); } TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { From 7ab9c119efb0d1a2f139e6d53da105b9e214b596 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 19:59:18 -0400 Subject: [PATCH 0286/1222] Changed set_file_content to accept only a regular file path. --- README.md | 164 ++++++++++++++++++++++++--------------------------- httplib.h | 15 ++++- test/test.cc | 2 + 3 files changed, 90 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 0a525122ad..10465fb893 100644 --- a/README.md +++ b/README.md @@ -97,37 +97,33 @@ int main(void) Server svr; - svr.Get("/hi", [](const Request& req, Response& res) { + svr.Get("/hi", [](const Request &req, Response &res) { res.set_content("Hello World!", "text/plain"); }); // Match the request path against a regular expression // and extract its captures - svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) { + svr.Get(R"(/numbers/(\d+))", [&](const Request &req, Response &res) { auto numbers = req.matches[1]; res.set_content(numbers, "text/plain"); }); // Capture the second segment of the request path as "id" path param - svr.Get("/users/:id", [&](const Request& req, Response& res) { + svr.Get("/users/:id", [&](const Request &req, Response &res) { auto user_id = req.path_params.at("id"); res.set_content(user_id, "text/plain"); }); // Extract values from HTTP headers and URL query params - svr.Get("/body-header-param", [](const Request& req, Response& res) { + svr.Get("/body-header-param", [](const Request &req, Response &res) { if (req.has_header("Content-Length")) { auto val = req.get_header_value("Content-Length"); } - if (req.has_param("key")) { - auto val = req.get_param_value("key"); - } + if (req.has_param("key")) { auto val = req.get_param_value("key"); } res.set_content(req.body, "text/plain"); }); - svr.Get("/stop", [&](const Request& req, Response& res) { - svr.stop(); - }); + svr.Get("/stop", [&](const Request &req, Response &res) { svr.stop(); }); svr.listen("localhost", 1234); } @@ -276,7 +272,7 @@ svr.set_post_routing_handler([](const auto& req, auto& res) { svr.Post("/multipart", [&](const auto& req, auto& res) { auto size = req.files.size(); auto ret = req.has_file("name1"); - const auto& file = req.get_file_value("name1"); + const auto &file = req.get_file_value("name1"); // file.filename; // file.content_type; // file.content; @@ -288,10 +284,10 @@ svr.Post("/multipart", [&](const auto& req, auto& res) { ```cpp svr.Post("/content_receiver", [&](const Request &req, Response &res, const ContentReader &content_reader) { - if (req.is_multipart_form_data()) { - // NOTE: `content_reader` is blocking until every form data field is read - MultipartFormDataItems files; - content_reader( + if (req.is_multipart_form_data()) { + // NOTE: `content_reader` is blocking until every form data field is read + MultipartFormDataItems files; + content_reader( [&](const MultipartFormData &file) { files.push_back(file); return true; @@ -300,13 +296,13 @@ svr.Post("/content_receiver", files.back().content.append(data, data_length); return true; }); - } else { - std::string body; - content_reader([&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; - }); - } + } else { + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + } }); ``` @@ -319,14 +315,14 @@ svr.Get("/stream", [&](const Request &req, Response &res) { auto data = new std::string("abcdefg"); res.set_content_provider( - data->size(), // Content length - "text/plain", // Content type - [&, data](size_t offset, size_t length, DataSink &sink) { - const auto &d = *data; - sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); - return true; // return 'false' if you want to cancel the process. - }, - [data](bool success) { delete data; }); + data->size(), // Content length + "text/plain", // Content type + [&, data](size_t offset, size_t length, DataSink &sink) { + const auto &d = *data; + sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); + return true; // return 'false' if you want to cancel the process. + }, + [data](bool success) { delete data; }); }); ``` @@ -335,17 +331,17 @@ Without content length: ```cpp svr.Get("/stream", [&](const Request &req, Response &res) { res.set_content_provider( - "text/plain", // Content type - [&](size_t offset, DataSink &sink) { - if (/* there is still data */) { - std::vector data; - // prepare data... - sink.write(data.data(), data.size()); - } else { - sink.done(); // No more data - } - return true; // return 'false' if you want to cancel the process. - }); + "text/plain", // Content type + [&](size_t offset, DataSink &sink) { + if (/* there is still data */) { + std::vector data; + // prepare data... + sink.write(data.data(), data.size()); + } else { + sink.done(); // No more data + } + return true; // return 'false' if you want to cancel the process. + }); }); ``` @@ -354,15 +350,13 @@ svr.Get("/stream", [&](const Request &req, Response &res) { ```cpp svr.Get("/chunked", [&](const Request& req, Response& res) { res.set_chunked_content_provider( - "text/plain", - [](size_t offset, DataSink &sink) { - sink.write("123", 3); - sink.write("345", 3); - sink.write("789", 3); - sink.done(); // No more data - return true; // return 'false' if you want to cancel the process. - } - ); + "text/plain", [](size_t offset, DataSink &sink) { + sink.write("123", 3); + sink.write("345", 3); + sink.write("789", 3); + sink.done(); // No more data + return true; // return 'false' if you want to cancel the process. + }); }); ``` @@ -371,24 +365,21 @@ With trailer: ```cpp svr.Get("/chunked", [&](const Request& req, Response& res) { res.set_header("Trailer", "Dummy1, Dummy2"); - res.set_chunked_content_provider( - "text/plain", - [](size_t offset, DataSink &sink) { - sink.write("123", 3); - sink.write("345", 3); - sink.write("789", 3); - sink.done_with_trailer({ - {"Dummy1", "DummyVal1"}, - {"Dummy2", "DummyVal2"} - }); - return true; - } - ); + res.set_chunked_content_provider("text/plain", [](size_t offset, + DataSink &sink) { + sink.write("123", 3); + sink.write("345", 3); + sink.write("789", 3); + sink.done_with_trailer({{"Dummy1", "DummyVal1"}, {"Dummy2", "DummyVal2"}}); + return true; + }); }); ``` ### Send file content +We can set a file path for the response body. It's a user's responsibility to pass a valid regular file path. If the path doesn't exist, or a directory path, cpp-httplib throws an exception. + ```cpp svr.Get("/content", [&](const Request &req, Response &res) { res.set_file_content("./path/to/conent.html"); @@ -452,7 +443,8 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e If you want to set the thread count at runtime, there is no convenient way... But here is how. ```cpp -svr.new_task_queue = [] { return new ThreadPool(12); }; +svr.new_task_queue = [] { + return new ThreadPool(12); }; ``` You can also provide an optional parameter to limit the maximum number @@ -460,7 +452,8 @@ of pending requests, i.e. requests `accept()`ed by the listener but still waiting to be serviced by worker threads. ```cpp -svr.new_task_queue = [] { return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); }; +svr.new_task_queue = [] { + return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); }; ``` Default limit is 0 (unlimited). Once the limit is reached, the listener @@ -473,9 +466,7 @@ You can supply your own thread pool implementation according to your need. ```cpp class YourThreadPoolTaskQueue : public TaskQueue { public: - YourThreadPoolTaskQueue(size_t n) { - pool_.start_with_thread_count(n); - } + YourThreadPoolTaskQueue(size_t n) { pool_.start_with_thread_count(n); } virtual bool enqueue(std::function fn) override { /* Return true if the task was actually enqueued, or false @@ -483,9 +474,7 @@ public: return pool_.enqueue(fn); } - virtual void shutdown() override { - pool_.shutdown_gracefully(); - } + virtual void shutdown() override { pool_.shutdown_gracefully(); } private: YourThreadPool pool_; @@ -648,8 +637,8 @@ std::string body; auto res = cli.Get("/large-data", [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; + body.append(data, data_length); + return true; }); ``` @@ -659,12 +648,12 @@ std::string body; auto res = cli.Get( "/stream", Headers(), [&](const Response &response) { - EXPECT_EQ(StatusCode::OK_200, response.status); - return true; // return 'false' if you want to cancel the request. + EXPECT_EQ(StatusCode::OK_200, response.status); + return true; // return 'false' if you want to cancel the request. }, [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; // return 'false' if you want to cancel the request. + body.append(data, data_length); + return true; // return 'false' if you want to cancel the request. }); ``` @@ -676,8 +665,8 @@ std::string body = ...; auto res = cli.Post( "/stream", body.size(), [](size_t offset, size_t length, DataSink &sink) { - sink.write(body.data() + offset, length); - return true; // return 'false' if you want to cancel the request. + sink.write(body.data() + offset, length); + return true; // return 'false' if you want to cancel the request. }, "text/plain"); ``` @@ -688,11 +677,11 @@ auto res = cli.Post( auto res = cli.Post( "/stream", [](size_t offset, DataSink &sink) { - sink.os << "chunked data 1"; - sink.os << "chunked data 2"; - sink.os << "chunked data 3"; - sink.done(); - return true; // return 'false' if you want to cancel the request. + sink.os << "chunked data 1"; + sink.os << "chunked data 2"; + sink.os << "chunked data 3"; + sink.done(); + return true; // return 'false' if you want to cancel the request. }, "text/plain"); ``` @@ -704,9 +693,8 @@ httplib::Client cli(url, port); // prints: 0 / 000 bytes => 50% complete auto res = cli.Get("/", [](uint64_t len, uint64_t total) { - printf("%lld / %lld bytes => %d%% complete\n", - len, total, - (int)(len*100/total)); + printf("%lld / %lld bytes => %d%% complete\n", len, total, + (int)(len * 100 / total)); return true; // return 'false' if you want to cancel the request. } ); @@ -904,8 +892,8 @@ g++ 4.8 and below cannot build this library since `` in the versions are Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32_LEAN_AND_MEAN` beforehand. ```cpp -#include #include +#include ``` ```cpp diff --git a/httplib.h b/httplib.h index 40bcb8b212..28c5100ae2 100644 --- a/httplib.h +++ b/httplib.h @@ -5752,12 +5752,21 @@ inline void Response::set_chunked_content_provider( inline void Response::set_file_content(const std::string &path, const std::string &content_type) { - file_content_path_ = path; - file_content_content_type_ = content_type; + detail::FileStat stat(dir); + if (stat.is_file(path)) { + file_content_path_ = path; + file_content_content_type_ = content_type; + return; + } + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + std::string msg = "'" + path + "' is not a regular file."; + throw std::invalid_argument(msg); +#endif } inline void Response::set_file_content(const std::string &path) { - file_content_path_ = path; + return set_file_content(path, std::string()); } // Result implementation diff --git a/test/test.cc b/test/test.cc index b9bc977bce..96765e42e0 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2288,6 +2288,8 @@ class ServerTest : public ::testing::Test { { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT cli_.enable_server_certificate_verification(false); +#else +#error no ssl #endif } From 3f00e1b321c3165ad00462fde2aaeaacbb27e5a1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 20:03:47 -0400 Subject: [PATCH 0287/1222] Revert "Changed set_file_content to accept only a regular file path." This reverts commit 7ab9c119efb0d1a2f139e6d53da105b9e214b596. --- README.md | 164 +++++++++++++++++++++++++++------------------------ httplib.h | 15 +---- test/test.cc | 2 - 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index 10465fb893..0a525122ad 100644 --- a/README.md +++ b/README.md @@ -97,33 +97,37 @@ int main(void) Server svr; - svr.Get("/hi", [](const Request &req, Response &res) { + svr.Get("/hi", [](const Request& req, Response& res) { res.set_content("Hello World!", "text/plain"); }); // Match the request path against a regular expression // and extract its captures - svr.Get(R"(/numbers/(\d+))", [&](const Request &req, Response &res) { + svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) { auto numbers = req.matches[1]; res.set_content(numbers, "text/plain"); }); // Capture the second segment of the request path as "id" path param - svr.Get("/users/:id", [&](const Request &req, Response &res) { + svr.Get("/users/:id", [&](const Request& req, Response& res) { auto user_id = req.path_params.at("id"); res.set_content(user_id, "text/plain"); }); // Extract values from HTTP headers and URL query params - svr.Get("/body-header-param", [](const Request &req, Response &res) { + svr.Get("/body-header-param", [](const Request& req, Response& res) { if (req.has_header("Content-Length")) { auto val = req.get_header_value("Content-Length"); } - if (req.has_param("key")) { auto val = req.get_param_value("key"); } + if (req.has_param("key")) { + auto val = req.get_param_value("key"); + } res.set_content(req.body, "text/plain"); }); - svr.Get("/stop", [&](const Request &req, Response &res) { svr.stop(); }); + svr.Get("/stop", [&](const Request& req, Response& res) { + svr.stop(); + }); svr.listen("localhost", 1234); } @@ -272,7 +276,7 @@ svr.set_post_routing_handler([](const auto& req, auto& res) { svr.Post("/multipart", [&](const auto& req, auto& res) { auto size = req.files.size(); auto ret = req.has_file("name1"); - const auto &file = req.get_file_value("name1"); + const auto& file = req.get_file_value("name1"); // file.filename; // file.content_type; // file.content; @@ -284,10 +288,10 @@ svr.Post("/multipart", [&](const auto& req, auto& res) { ```cpp svr.Post("/content_receiver", [&](const Request &req, Response &res, const ContentReader &content_reader) { - if (req.is_multipart_form_data()) { - // NOTE: `content_reader` is blocking until every form data field is read - MultipartFormDataItems files; - content_reader( + if (req.is_multipart_form_data()) { + // NOTE: `content_reader` is blocking until every form data field is read + MultipartFormDataItems files; + content_reader( [&](const MultipartFormData &file) { files.push_back(file); return true; @@ -296,13 +300,13 @@ svr.Post("/content_receiver", files.back().content.append(data, data_length); return true; }); - } else { - std::string body; - content_reader([&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; - }); - } + } else { + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + } }); ``` @@ -315,14 +319,14 @@ svr.Get("/stream", [&](const Request &req, Response &res) { auto data = new std::string("abcdefg"); res.set_content_provider( - data->size(), // Content length - "text/plain", // Content type - [&, data](size_t offset, size_t length, DataSink &sink) { - const auto &d = *data; - sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); - return true; // return 'false' if you want to cancel the process. - }, - [data](bool success) { delete data; }); + data->size(), // Content length + "text/plain", // Content type + [&, data](size_t offset, size_t length, DataSink &sink) { + const auto &d = *data; + sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); + return true; // return 'false' if you want to cancel the process. + }, + [data](bool success) { delete data; }); }); ``` @@ -331,17 +335,17 @@ Without content length: ```cpp svr.Get("/stream", [&](const Request &req, Response &res) { res.set_content_provider( - "text/plain", // Content type - [&](size_t offset, DataSink &sink) { - if (/* there is still data */) { - std::vector data; - // prepare data... - sink.write(data.data(), data.size()); - } else { - sink.done(); // No more data - } - return true; // return 'false' if you want to cancel the process. - }); + "text/plain", // Content type + [&](size_t offset, DataSink &sink) { + if (/* there is still data */) { + std::vector data; + // prepare data... + sink.write(data.data(), data.size()); + } else { + sink.done(); // No more data + } + return true; // return 'false' if you want to cancel the process. + }); }); ``` @@ -350,13 +354,15 @@ svr.Get("/stream", [&](const Request &req, Response &res) { ```cpp svr.Get("/chunked", [&](const Request& req, Response& res) { res.set_chunked_content_provider( - "text/plain", [](size_t offset, DataSink &sink) { - sink.write("123", 3); - sink.write("345", 3); - sink.write("789", 3); - sink.done(); // No more data - return true; // return 'false' if you want to cancel the process. - }); + "text/plain", + [](size_t offset, DataSink &sink) { + sink.write("123", 3); + sink.write("345", 3); + sink.write("789", 3); + sink.done(); // No more data + return true; // return 'false' if you want to cancel the process. + } + ); }); ``` @@ -365,21 +371,24 @@ With trailer: ```cpp svr.Get("/chunked", [&](const Request& req, Response& res) { res.set_header("Trailer", "Dummy1, Dummy2"); - res.set_chunked_content_provider("text/plain", [](size_t offset, - DataSink &sink) { - sink.write("123", 3); - sink.write("345", 3); - sink.write("789", 3); - sink.done_with_trailer({{"Dummy1", "DummyVal1"}, {"Dummy2", "DummyVal2"}}); - return true; - }); + res.set_chunked_content_provider( + "text/plain", + [](size_t offset, DataSink &sink) { + sink.write("123", 3); + sink.write("345", 3); + sink.write("789", 3); + sink.done_with_trailer({ + {"Dummy1", "DummyVal1"}, + {"Dummy2", "DummyVal2"} + }); + return true; + } + ); }); ``` ### Send file content -We can set a file path for the response body. It's a user's responsibility to pass a valid regular file path. If the path doesn't exist, or a directory path, cpp-httplib throws an exception. - ```cpp svr.Get("/content", [&](const Request &req, Response &res) { res.set_file_content("./path/to/conent.html"); @@ -443,8 +452,7 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e If you want to set the thread count at runtime, there is no convenient way... But here is how. ```cpp -svr.new_task_queue = [] { - return new ThreadPool(12); }; +svr.new_task_queue = [] { return new ThreadPool(12); }; ``` You can also provide an optional parameter to limit the maximum number @@ -452,8 +460,7 @@ of pending requests, i.e. requests `accept()`ed by the listener but still waiting to be serviced by worker threads. ```cpp -svr.new_task_queue = [] { - return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); }; +svr.new_task_queue = [] { return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); }; ``` Default limit is 0 (unlimited). Once the limit is reached, the listener @@ -466,7 +473,9 @@ You can supply your own thread pool implementation according to your need. ```cpp class YourThreadPoolTaskQueue : public TaskQueue { public: - YourThreadPoolTaskQueue(size_t n) { pool_.start_with_thread_count(n); } + YourThreadPoolTaskQueue(size_t n) { + pool_.start_with_thread_count(n); + } virtual bool enqueue(std::function fn) override { /* Return true if the task was actually enqueued, or false @@ -474,7 +483,9 @@ public: return pool_.enqueue(fn); } - virtual void shutdown() override { pool_.shutdown_gracefully(); } + virtual void shutdown() override { + pool_.shutdown_gracefully(); + } private: YourThreadPool pool_; @@ -637,8 +648,8 @@ std::string body; auto res = cli.Get("/large-data", [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; + body.append(data, data_length); + return true; }); ``` @@ -648,12 +659,12 @@ std::string body; auto res = cli.Get( "/stream", Headers(), [&](const Response &response) { - EXPECT_EQ(StatusCode::OK_200, response.status); - return true; // return 'false' if you want to cancel the request. + EXPECT_EQ(StatusCode::OK_200, response.status); + return true; // return 'false' if you want to cancel the request. }, [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; // return 'false' if you want to cancel the request. + body.append(data, data_length); + return true; // return 'false' if you want to cancel the request. }); ``` @@ -665,8 +676,8 @@ std::string body = ...; auto res = cli.Post( "/stream", body.size(), [](size_t offset, size_t length, DataSink &sink) { - sink.write(body.data() + offset, length); - return true; // return 'false' if you want to cancel the request. + sink.write(body.data() + offset, length); + return true; // return 'false' if you want to cancel the request. }, "text/plain"); ``` @@ -677,11 +688,11 @@ auto res = cli.Post( auto res = cli.Post( "/stream", [](size_t offset, DataSink &sink) { - sink.os << "chunked data 1"; - sink.os << "chunked data 2"; - sink.os << "chunked data 3"; - sink.done(); - return true; // return 'false' if you want to cancel the request. + sink.os << "chunked data 1"; + sink.os << "chunked data 2"; + sink.os << "chunked data 3"; + sink.done(); + return true; // return 'false' if you want to cancel the request. }, "text/plain"); ``` @@ -693,8 +704,9 @@ httplib::Client cli(url, port); // prints: 0 / 000 bytes => 50% complete auto res = cli.Get("/", [](uint64_t len, uint64_t total) { - printf("%lld / %lld bytes => %d%% complete\n", len, total, - (int)(len * 100 / total)); + printf("%lld / %lld bytes => %d%% complete\n", + len, total, + (int)(len*100/total)); return true; // return 'false' if you want to cancel the request. } ); @@ -892,8 +904,8 @@ g++ 4.8 and below cannot build this library since `` in the versions are Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32_LEAN_AND_MEAN` beforehand. ```cpp -#include #include +#include ``` ```cpp diff --git a/httplib.h b/httplib.h index 28c5100ae2..40bcb8b212 100644 --- a/httplib.h +++ b/httplib.h @@ -5752,21 +5752,12 @@ inline void Response::set_chunked_content_provider( inline void Response::set_file_content(const std::string &path, const std::string &content_type) { - detail::FileStat stat(dir); - if (stat.is_file(path)) { - file_content_path_ = path; - file_content_content_type_ = content_type; - return; - } - -#ifndef CPPHTTPLIB_NO_EXCEPTIONS - std::string msg = "'" + path + "' is not a regular file."; - throw std::invalid_argument(msg); -#endif + file_content_path_ = path; + file_content_content_type_ = content_type; } inline void Response::set_file_content(const std::string &path) { - return set_file_content(path, std::string()); + file_content_path_ = path; } // Result implementation diff --git a/test/test.cc b/test/test.cc index 96765e42e0..b9bc977bce 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2288,8 +2288,6 @@ class ServerTest : public ::testing::Test { { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT cli_.enable_server_certificate_verification(false); -#else -#error no ssl #endif } From 9f8db2c230299fee8b9f0aff693262a8671c213a Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 22:22:56 -0400 Subject: [PATCH 0288/1222] Fix #1933 --- httplib.h | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 40bcb8b212..3c14105b5a 100644 --- a/httplib.h +++ b/httplib.h @@ -2468,13 +2468,14 @@ class mmap { private: #if defined(_WIN32) - HANDLE hFile_; - HANDLE hMapping_; + HANDLE hFile_ = NULL; + HANDLE hMapping_ = NULL; + bool is_open_empty_file_on_windows_ = false; #else - int fd_; + int fd_ = -1; #endif - size_t size_; - void *addr_; + size_t size_ = 0; + void *addr_ = nullptr; }; } // namespace detail @@ -2895,14 +2896,7 @@ inline void stream_line_reader::append(char c) { } } -inline mmap::mmap(const char *path) -#if defined(_WIN32) - : hFile_(NULL), hMapping_(NULL) -#else - : fd_(-1) -#endif - , - size_(0), addr_(nullptr) { +inline mmap::mmap(const char *path) { open(path); } @@ -2946,6 +2940,13 @@ inline bool mmap::open(const char *path) { hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); #endif + // TODO: Special treatment for an empty file on Windows... (#1933) + if (hMapping_ == NULL && size_ == 0) { + close(); + is_open_empty_file_on_windows_ = true; + return true; + } + if (hMapping_ == NULL) { close(); return false; @@ -2956,6 +2957,11 @@ inline bool mmap::open(const char *path) { #else addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); #endif + + if (addr_ == nullptr) { + close(); + return false; + } #else fd_ = ::open(path, O_RDONLY); if (fd_ == -1) { return false; } @@ -2968,21 +2974,33 @@ inline bool mmap::open(const char *path) { size_ = static_cast(sb.st_size); addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); -#endif - if (addr_ == nullptr) { + if (addr_ == MAP_FAILED) { close(); return false; } +#endif return true; } -inline bool mmap::is_open() const { return addr_ != nullptr; } +inline bool mmap::is_open() const { +#if defined(_WIN32) + if (is_open_empty_file_on_windows_) { + return true; + } +#endif + return addr_ != nullptr; +} inline size_t mmap::size() const { return size_; } inline const char *mmap::data() const { +#if defined(_WIN32) + if (is_open_empty_file_on_windows_) { + return ""; + } +#endif return static_cast(addr_); } @@ -3002,6 +3020,8 @@ inline void mmap::close() { ::CloseHandle(hFile_); hFile_ = INVALID_HANDLE_VALUE; } + + is_open_empty_file_on_windows_ = false; #else if (addr_ != nullptr) { munmap(addr_, size_); From de36ea7755735534649f3d2b6b0e4eaf33027b8d Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 23:07:11 -0400 Subject: [PATCH 0289/1222] Fix #1933 on Linux and macOS --- httplib.h | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/httplib.h b/httplib.h index 3c14105b5a..bc8cae2f13 100644 --- a/httplib.h +++ b/httplib.h @@ -2470,12 +2470,12 @@ class mmap { #if defined(_WIN32) HANDLE hFile_ = NULL; HANDLE hMapping_ = NULL; - bool is_open_empty_file_on_windows_ = false; #else int fd_ = -1; #endif size_t size_ = 0; void *addr_ = nullptr; + bool is_open_empty_file = false; }; } // namespace detail @@ -2940,10 +2940,10 @@ inline bool mmap::open(const char *path) { hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); #endif - // TODO: Special treatment for an empty file on Windows... (#1933) + // Special treatment for an empty file... if (hMapping_ == NULL && size_ == 0) { close(); - is_open_empty_file_on_windows_ = true; + is_open_empty_file = true; return true; } @@ -2975,8 +2975,10 @@ inline bool mmap::open(const char *path) { addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); - if (addr_ == MAP_FAILED) { + // Special treatment for an empty file... + if (addr_ == MAP_FAILED && size_ == 0) { close(); + is_open_empty_file = true; return false; } #endif @@ -2985,23 +2987,13 @@ inline bool mmap::open(const char *path) { } inline bool mmap::is_open() const { -#if defined(_WIN32) - if (is_open_empty_file_on_windows_) { - return true; - } -#endif - return addr_ != nullptr; + return is_open_empty_file ? true : addr_ != nullptr; } inline size_t mmap::size() const { return size_; } inline const char *mmap::data() const { -#if defined(_WIN32) - if (is_open_empty_file_on_windows_) { - return ""; - } -#endif - return static_cast(addr_); + return is_open_empty_file ? "" : static_cast(addr_); } inline void mmap::close() { @@ -3021,7 +3013,7 @@ inline void mmap::close() { hFile_ = INVALID_HANDLE_VALUE; } - is_open_empty_file_on_windows_ = false; + is_open_empty_file = false; #else if (addr_ != nullptr) { munmap(addr_, size_); From 932b1cbc3231da4fa749e5264d4b8ac1f251e6c2 Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Thu, 12 Sep 2024 12:02:25 -0400 Subject: [PATCH 0290/1222] Fix shadow parameter warning (#1936) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index bc8cae2f13..675609f844 100644 --- a/httplib.h +++ b/httplib.h @@ -366,7 +366,7 @@ inline unsigned char to_lower(int c) { inline bool equal(const std::string &a, const std::string &b) { return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), - [](char a, char b) { return to_lower(a) == to_lower(b); }); + [](char ca, char cb) { return to_lower(ca) == to_lower(cb); }); } struct equal_to { From 5053912534e2c05fad93bd95da2078860ee1b133 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Sep 2024 12:04:03 -0400 Subject: [PATCH 0291/1222] Updated actions/upload-artifact from v1 to v4 --- .github/workflows/cifuzz.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cifuzz.yaml b/.github/workflows/cifuzz.yaml index 5325fd4c6e..43e1de3a39 100644 --- a/.github/workflows/cifuzz.yaml +++ b/.github/workflows/cifuzz.yaml @@ -19,7 +19,7 @@ jobs: dry-run: false language: c++ - name: Upload Crash - uses: actions/upload-artifact@v1 + uses: actions/upload-artifact@v4 if: failure() && steps.build.outcome == 'success' with: name: artifacts From e64379c3d71ccf3f62e4e4853bfd1316901564b3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Sep 2024 12:28:35 -0400 Subject: [PATCH 0292/1222] Release v0.18.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 675609f844..121ce34955 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.17.3" +#define CPPHTTPLIB_VERSION "0.18.0" /* * Configuration From af4ece3d5fc43938e84e445b2884bd866d163a34 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Sep 2024 23:39:06 -0400 Subject: [PATCH 0293/1222] Update benchmark/Makefile --- benchmark/Makefile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/benchmark/Makefile b/benchmark/Makefile index 556c811c36..12d3120f0e 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -1,6 +1,6 @@ CXXFLAGS = -std=c++11 -O2 -I.. -THEAD_POOL_COUNT = 16 +CPPHTTPLIB_FLAGS = -DCPPHTTPLIB_THREAD_POOL_COUNT=16 BENCH = bombardier -c 10 -d 5s localhost:8080 MONITOR = ali http://localhost:8080 @@ -18,7 +18,7 @@ run : server @./server server : cpp-httplib/main.cpp ../httplib.h - g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib/main.cpp + g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp # cpp-httplib bench-base: server-base @@ -33,7 +33,7 @@ run-base : server-base @./server-base server-base : cpp-httplib-base/main.cpp cpp-httplib-base/httplib.h - g++ -o $@ $(CXXFLAGS) -DCPPHTTPLIB_THREAD_POOL_COUNT=$(THEAD_POOL_COUNT) cpp-httplib-base/main.cpp + g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib-base/main.cpp # crow bench-crow: server-crow @@ -51,10 +51,12 @@ server-crow : crow/main.cpp g++ -o $@ $(CXXFLAGS) crow/main.cpp # misc +build: server server-base server-crow + bench-all: bench-crow bench bench-base issue: - $(BENCH) + bombardier -c 10 -d 30s localhost:8080 clean: rm -rf server* From a61b2427b0b3267eabdeb647b963b4bb915d0301 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 13 Sep 2024 20:34:30 -0400 Subject: [PATCH 0294/1222] Update benchmark base vertion to 0.18.0 --- benchmark/Makefile | 2 +- benchmark/cpp-httplib-base/httplib.h | 667 +++++++++++++++++---------- 2 files changed, 434 insertions(+), 235 deletions(-) diff --git a/benchmark/Makefile b/benchmark/Makefile index 12d3120f0e..5107664c03 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -22,7 +22,7 @@ server : cpp-httplib/main.cpp ../httplib.h # cpp-httplib bench-base: server-base - @echo "---------------------\n cpp-httplib v0.17.0\n---------------------\n" + @echo "---------------------\n cpp-httplib v0.18.0\n---------------------\n" @./server-base & export PID=$$!; $(BENCH); kill $${PID} @echo "" diff --git a/benchmark/cpp-httplib-base/httplib.h b/benchmark/cpp-httplib-base/httplib.h index f038a1f2d4..121ce34955 100644 --- a/benchmark/cpp-httplib-base/httplib.h +++ b/benchmark/cpp-httplib-base/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.17.0" +#define CPPHTTPLIB_VERSION "0.18.0" /* * Configuration @@ -30,20 +30,36 @@ #define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 #endif -#ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5 +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND 5 #endif -#ifndef CPPHTTPLIB_READ_TIMEOUT_USECOND -#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0 +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND 0 #endif -#ifndef CPPHTTPLIB_WRITE_TIMEOUT_SECOND -#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND 5 +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND 5 #endif -#ifndef CPPHTTPLIB_WRITE_TIMEOUT_USECOND -#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0 +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 #endif #ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND @@ -273,7 +289,7 @@ using socket_t = int; #include #include -#if defined(OPENSSL_IS_BORINGSSL) +#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) #if OPENSSL_VERSION_NUMBER < 0x1010107f #error Please use OpenSSL or a current version of BoringSSL #endif @@ -321,16 +337,62 @@ make_unique(std::size_t n) { return std::unique_ptr(new RT[n]); } -struct ci { - bool operator()(const std::string &s1, const std::string &s2) const { - return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), - s2.end(), - [](unsigned char c1, unsigned char c2) { - return ::tolower(c1) < ::tolower(c2); - }); +namespace case_ignore { + +inline unsigned char to_lower(int c) { + const static unsigned char table[256] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, + 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, + 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, + 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, + 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, + 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226, + 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, + 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224, + 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, + 255, + }; + return table[(unsigned char)(char)c]; +} + +inline bool equal(const std::string &a, const std::string &b) { + return a.size() == b.size() && + std::equal(a.begin(), a.end(), b.begin(), + [](char ca, char cb) { return to_lower(ca) == to_lower(cb); }); +} + +struct equal_to { + bool operator()(const std::string &a, const std::string &b) const { + return equal(a, b); } }; +struct hash { + size_t operator()(const std::string &key) const { + return hash_core(key.data(), key.size(), 0); + } + + size_t hash_core(const char *s, size_t l, size_t h) const { + return (l == 0) ? h + : hash_core(s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no + // overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(to_lower(*s))); + } +}; + +}; // namespace case_ignore + // This is based on // "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". @@ -436,7 +498,9 @@ enum StatusCode { NetworkAuthenticationRequired_511 = 511, }; -using Headers = std::multimap; +using Headers = + std::unordered_multimap; using Params = std::multimap; using Match = std::smatch; @@ -627,6 +691,10 @@ struct Response { const std::string &content_type, ContentProviderWithoutLength provider, ContentProviderResourceReleaser resource_releaser = nullptr); + void set_file_content(const std::string &path, + const std::string &content_type); + void set_file_content(const std::string &path); + Response() = default; Response(const Response &) = default; Response &operator=(const Response &) = default; @@ -644,6 +712,8 @@ struct Response { ContentProviderResourceReleaser content_provider_resource_releaser_; bool is_chunked_content_provider_ = false; bool content_provider_success_ = false; + std::string file_content_path_; + std::string file_content_content_type_; }; class Stream { @@ -659,8 +729,6 @@ class Stream { virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; virtual socket_t socket() const = 0; - template - ssize_t write_format(const char *fmt, const Args &...args); ssize_t write(const char *ptr); ssize_t write(const std::string &s); }; @@ -740,7 +808,8 @@ class ThreadPool final : public TaskQueue { fn(); } -#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ + !defined(LIBRESSL_VERSION_NUMBER) OPENSSL_thread_stop(); #endif } @@ -804,7 +873,6 @@ class PathParamsMatcher final : public MatcherBase { bool match(Request &request) const override; private: - static constexpr char marker = ':'; // Treat segment separators as the end of path parameter capture // Does not need to handle query parameters as they are parsed before path // matching @@ -942,17 +1010,19 @@ class Server { std::function new_task_queue; protected: - bool process_request(Stream &strm, bool close_connection, + bool process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, bool &connection_closed, const std::function &setup_request); std::atomic svr_sock_{INVALID_SOCKET}; size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND; time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; @@ -1065,6 +1135,7 @@ enum class Error { SSLConnection, SSLLoadingCerts, SSLServerVerification, + SSLServerHostnameVerification, UnsupportedMultipartBoundaryChars, Compression, ConnectionTimeout, @@ -1380,6 +1451,8 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier(std::function verifier); #endif void set_logger(Logger logger); @@ -1446,10 +1519,10 @@ class ClientImpl { time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; std::string basic_auth_username_; std::string basic_auth_password_; @@ -1494,6 +1567,8 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT bool server_certificate_verification_ = true; + bool server_hostname_verification_ = true; + std::function server_certificate_verifier_; #endif Logger logger_; @@ -1799,6 +1874,8 @@ class Client { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier(std::function verifier); #endif void set_logger(Logger logger); @@ -1951,30 +2028,6 @@ inline uint64_t Response::get_header_value_u64(const std::string &key, return detail::get_header_value_u64(headers, key, def, id); } -template -inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { - const auto bufsiz = 2048; - std::array buf{}; - - auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); - if (sn <= 0) { return sn; } - - auto n = static_cast(sn); - - if (n >= buf.size() - 1) { - std::vector glowable_buf(buf.size()); - - while (n >= glowable_buf.size() - 1) { - glowable_buf.resize(glowable_buf.size() * 2); - n = static_cast( - snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...)); - } - return write(&glowable_buf[0], n); - } else { - return write(buf.data(), n); - } -} - inline void default_socket_options(socket_t sock) { int opt = 1; #ifdef _WIN32 @@ -2114,6 +2167,8 @@ inline std::string to_string(const Error error) { case Error::SSLConnection: return "SSL connection failed"; case Error::SSLLoadingCerts: return "SSL certificate loading failed"; case Error::SSLServerVerification: return "SSL server verification failed"; + case Error::SSLServerHostnameVerification: + return "SSL server hostname verification failed"; case Error::UnsupportedMultipartBoundaryChars: return "Unsupported HTTP multipart boundary characters"; case Error::Compression: return "Compression failed"; @@ -2198,6 +2253,16 @@ make_basic_authentication_header(const std::string &username, namespace detail { +struct FileStat { + FileStat(const std::string &path); + bool is_file() const; + bool is_dir() const; + +private: + struct stat st_; + int ret_ = -1; +}; + std::string encode_query_param(const std::string &value); std::string decode_url(const std::string &s, bool convert_plus_to_space); @@ -2403,13 +2468,14 @@ class mmap { private: #if defined(_WIN32) - HANDLE hFile_; - HANDLE hMapping_; + HANDLE hFile_ = NULL; + HANDLE hMapping_ = NULL; #else - int fd_; + int fd_ = -1; #endif - size_t size_; - void *addr_; + size_t size_ = 0; + void *addr_ = nullptr; + bool is_open_empty_file = false; }; } // namespace detail @@ -2525,20 +2591,6 @@ inline std::string base64_encode(const std::string &in) { return out; } -inline bool is_file(const std::string &path) { -#ifdef _WIN32 - return _access_s(path.c_str(), 0) == 0; -#else - struct stat st; - return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode); -#endif -} - -inline bool is_dir(const std::string &path) { - struct stat st; - return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); -} - inline bool is_valid_path(const std::string &path) { size_t level = 0; size_t i = 0; @@ -2581,6 +2633,16 @@ inline bool is_valid_path(const std::string &path) { return true; } +inline FileStat::FileStat(const std::string &path) { + ret_ = stat(path.c_str(), &st_); +} +inline bool FileStat::is_file() const { + return ret_ >= 0 && S_ISREG(st_.st_mode); +} +inline bool FileStat::is_dir() const { + return ret_ >= 0 && S_ISDIR(st_.st_mode); +} + inline std::string encode_query_param(const std::string &value) { std::ostringstream escaped; escaped.fill('0'); @@ -2790,6 +2852,10 @@ inline bool stream_line_reader::getline() { fixed_buffer_used_size_ = 0; glowable_buffer_.clear(); +#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + char prev_byte = 0; +#endif + for (size_t i = 0;; i++) { char byte; auto n = strm_.read(&byte, 1); @@ -2806,7 +2872,12 @@ inline bool stream_line_reader::getline() { append(byte); +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR if (byte == '\n') { break; } +#else + if (prev_byte == '\r' && byte == '\n') { break; } + prev_byte = byte; +#endif } return true; @@ -2825,14 +2896,7 @@ inline void stream_line_reader::append(char c) { } } -inline mmap::mmap(const char *path) -#if defined(_WIN32) - : hFile_(NULL), hMapping_(NULL) -#else - : fd_(-1) -#endif - , - size_(0), addr_(nullptr) { +inline mmap::mmap(const char *path) { open(path); } @@ -2847,9 +2911,7 @@ inline bool mmap::open(const char *path) { wpath += path[i]; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ - WINAPI_PARTITION_GAMES) && \ - (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL); #else @@ -2859,39 +2921,47 @@ inline bool mmap::open(const char *path) { if (hFile_ == INVALID_HANDLE_VALUE) { return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM | \ - WINAPI_PARTITION_GAMES) LARGE_INTEGER size{}; if (!::GetFileSizeEx(hFile_, &size)) { return false; } + // If the following line doesn't compile due to QuadPart, update Windows SDK. + // See: + // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 + if (static_cast(size.QuadPart) > + (std::numeric_limits::max)()) { + // `size_t` might be 32-bits, on 32-bits Windows. + return false; + } size_ = static_cast(size.QuadPart); -#else - DWORD sizeHigh; - DWORD sizeLow; - sizeLow = ::GetFileSize(hFile_, &sizeHigh); - if (sizeLow == INVALID_FILE_SIZE) { return false; } - size_ = (static_cast(sizeHigh) << (sizeof(DWORD) * 8)) | sizeLow; -#endif -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ - (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); #else - hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, size.HighPart, - size.LowPart, NULL); + hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); #endif + // Special treatment for an empty file... + if (hMapping_ == NULL && size_ == 0) { + close(); + is_open_empty_file = true; + return true; + } + if (hMapping_ == NULL) { close(); return false; } -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM) && \ - (_WIN32_WINNT >= _WIN32_WINNT_WIN8) +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); #else addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); #endif + + if (addr_ == nullptr) { + close(); + return false; + } #else fd_ = ::open(path, O_RDONLY); if (fd_ == -1) { return false; } @@ -2904,22 +2974,26 @@ inline bool mmap::open(const char *path) { size_ = static_cast(sb.st_size); addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); -#endif - if (addr_ == nullptr) { + // Special treatment for an empty file... + if (addr_ == MAP_FAILED && size_ == 0) { close(); + is_open_empty_file = true; return false; } +#endif return true; } -inline bool mmap::is_open() const { return addr_ != nullptr; } +inline bool mmap::is_open() const { + return is_open_empty_file ? true : addr_ != nullptr; +} inline size_t mmap::size() const { return size_; } inline const char *mmap::data() const { - return static_cast(addr_); + return is_open_empty_file ? "" : static_cast(addr_); } inline void mmap::close() { @@ -2938,6 +3012,8 @@ inline void mmap::close() { ::CloseHandle(hFile_); hFile_ = INVALID_HANDLE_VALUE; } + + is_open_empty_file = false; #else if (addr_ != nullptr) { munmap(addr_, size_); @@ -2964,7 +3040,7 @@ template inline ssize_t handle_EINTR(T fn) { while (true) { res = fn(); if (res < 0 && errno == EINTR) { - std::this_thread::sleep_for(std::chrono::milliseconds{1}); + std::this_thread::sleep_for(std::chrono::microseconds{1}); continue; } break; @@ -3175,25 +3251,6 @@ class SSLSocketStream final : public Stream { }; #endif -inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { - using namespace std::chrono; - auto start = steady_clock::now(); - while (true) { - auto val = select_read(sock, 0, 10000); - if (val < 0) { - return false; - } else if (val == 0) { - auto current = steady_clock::now(); - auto duration = duration_cast(current - start); - auto timeout = keep_alive_timeout_sec * 1000; - if (duration.count() > timeout) { return false; } - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } else { - return true; - } - } -} - template inline bool process_server_socket_core(const std::atomic &svr_sock, socket_t sock, @@ -3203,7 +3260,7 @@ process_server_socket_core(const std::atomic &svr_sock, socket_t sock, auto ret = false; auto count = keep_alive_max_count; while (svr_sock != INVALID_SOCKET && count > 0 && - keep_alive(sock, keep_alive_timeout_sec)) { + select_read(sock, keep_alive_timeout_sec, 0) > 0) { auto close_connection = count == 1; auto connection_closed = false; ret = callback(close_connection, connection_closed); @@ -3247,6 +3304,25 @@ inline int shutdown_socket(socket_t sock) { #endif } +inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '\0') { + auto ret = s; + ret[0] = '@'; + return ret; + } + return s; +} + +inline std::string +unescape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '@') { + auto ret = s; + ret[0] = '\0'; + return ret; + } + return s; +} + template socket_t create_socket(const std::string &host, const std::string &ip, int port, int address_family, int socket_flags, bool tcp_nodelay, @@ -3259,7 +3335,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; + hints.ai_protocol = IPPROTO_IP; if (!ip.empty()) { node = ip.c_str(); @@ -3287,7 +3363,9 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (sock != INVALID_SOCKET) { sockaddr_un addr{}; addr.sun_family = AF_UNIX; - std::copy(host.begin(), host.end(), addr.sun_path); + + auto unescaped_host = unescape_abstract_namespace_unix_domain(host); + std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); hints.ai_addr = reinterpret_cast(&addr); hints.ai_addrlen = static_cast( @@ -3317,6 +3395,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #endif return INVALID_SOCKET; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); for (auto rp = result; rp; rp = rp->ai_next) { // Create a socket @@ -3386,17 +3465,13 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, // bind or connect auto quit = false; - if (bind_or_connect(sock, *rp, quit)) { - freeaddrinfo(result); - return sock; - } + if (bind_or_connect(sock, *rp, quit)) { return sock; } close_socket(sock); if (quit) { break; } } - freeaddrinfo(result); return INVALID_SOCKET; } @@ -3429,6 +3504,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { hints.ai_protocol = 0; if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); auto ret = false; for (auto rp = result; rp; rp = rp->ai_next) { @@ -3439,7 +3515,6 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { } } - freeaddrinfo(result); return ret; } @@ -3451,6 +3526,8 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { inline std::string if2ip(int address_family, const std::string &ifn) { struct ifaddrs *ifap; getifaddrs(&ifap); + auto se = detail::scope_exit([&] { freeifaddrs(ifap); }); + std::string addr_candidate; for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr && ifn == ifa->ifa_name && @@ -3460,7 +3537,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) { auto sa = reinterpret_cast(ifa->ifa_addr); char buf[INET_ADDRSTRLEN]; if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { - freeifaddrs(ifap); return std::string(buf, INET_ADDRSTRLEN); } } else if (ifa->ifa_addr->sa_family == AF_INET6) { @@ -3473,7 +3549,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) { if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { addr_candidate = std::string(buf, INET6_ADDRSTRLEN); } else { - freeifaddrs(ifap); return std::string(buf, INET6_ADDRSTRLEN); } } @@ -3481,7 +3556,6 @@ inline std::string if2ip(int address_family, const std::string &ifn) { } } } - freeifaddrs(ifap); return addr_candidate; } #endif @@ -3733,8 +3807,9 @@ inline bool can_compress_content_type(const std::string &content_type) { case "application/protobuf"_t: case "application/xhtml+xml"_t: return true; - default: - return !content_type.rfind("text/", 0) && tag != "text/event-stream"_t; + case "text/event-stream"_t: return false; + + default: return !content_type.rfind("text/", 0); } } @@ -3980,14 +4055,6 @@ inline const char *get_header_value(const Headers &headers, return def; } -inline bool compare_case_ignore(const std::string &a, const std::string &b) { - if (a.size() != b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { - if (::tolower(a[i]) != ::tolower(b[i])) { return false; } - } - return true; -} - template inline bool parse_header(const char *beg, const char *end, T fn) { // Skip trailing spaces and tabs. @@ -4015,9 +4082,21 @@ inline bool parse_header(const char *beg, const char *end, T fn) { if (!key_len) { return false; } auto key = std::string(beg, key_end); - auto val = compare_case_ignore(key, "Location") + auto val = case_ignore::equal(key, "Location") ? std::string(p, end) : decode_url(std::string(p, end), false); + + // NOTE: From RFC 9110: + // Field values containing CR, LF, or NUL characters are + // invalid and dangerous, due to the varying ways that + // implementations might parse and interpret those + // characters; a recipient of CR, LF, or NUL within a field + // value MUST either reject the message or replace each of + // those characters with SP before further processing or + // forwarding of that message. + static const std::string CR_LF_NUL("\r\n\0", 3); + if (val.find_first_of(CR_LF_NUL) != std::string::npos) { return false; } + fn(key, val); return true; } @@ -4038,27 +4117,27 @@ inline bool read_headers(Stream &strm, Headers &headers) { if (line_reader.end_with_crlf()) { // Blank line indicates end of headers. if (line_reader.size() == 2) { break; } -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR } else { +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR // Blank line indicates end of headers. if (line_reader.size() == 1) { break; } line_terminator_len = 1; - } #else - } else { continue; // Skip invalid line. - } #endif + } if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } // Exclude line terminator auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - parse_header(line_reader.ptr(), end, - [&](const std::string &key, const std::string &val) { - headers.emplace(key, val); - }); + if (!parse_header(line_reader.ptr(), end, + [&](const std::string &key, std::string &val) { + headers.emplace(key, val); + })) { + return false; + } } return true; @@ -4146,8 +4225,19 @@ inline bool read_content_chunked(Stream &strm, T &x, assert(chunk_len == 0); - // Trailer - if (!line_reader.getline()) { return false; } + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked + // transfer coding is complete when a chunk with a chunk-size of zero is + // received, possibly followed by a trailer section, and finally terminated by + // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section + // does't care for the existence of the final CRLF. In other words, it seems + // to be ok whether the final CRLF exists or not in the chunked data. + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // + // According to the reference code in RFC 9112, cpp-htpplib now allows + // chuncked transfer coding data without the final CRLF. + if (!line_reader.getline()) { return true; } while (strcmp(line_reader.ptr(), "\r\n") != 0) { if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } @@ -4168,7 +4258,7 @@ inline bool read_content_chunked(Stream &strm, T &x, } inline bool is_chunked_transfer_encoding(const Headers &headers) { - return compare_case_ignore( + return case_ignore::equal( get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); } @@ -4251,13 +4341,36 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } return ret; }); -} // namespace detail +} + +inline ssize_t write_request_line(Stream &strm, const std::string &method, + const std::string &path) { + std::string s = method; + s += " "; + s += path; + s += " HTTP/1.1\r\n"; + return strm.write(s.data(), s.size()); +} + +inline ssize_t write_response_line(Stream &strm, int status) { + std::string s = "HTTP/1.1 "; + s += std::to_string(status); + s += " "; + s += httplib::status_message(status); + s += "\r\n"; + return strm.write(s.data(), s.size()); +} inline ssize_t write_headers(Stream &strm, const Headers &headers) { ssize_t write_len = 0; for (const auto &x : headers) { - auto len = - strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); + std::string s; + s = x.first; + s += ": "; + s += x.second; + s += "\r\n"; + + auto len = strm.write(s.data(), s.size()); if (len < 0) { return len; } write_len += len; } @@ -4784,7 +4897,9 @@ class MultipartFormDataParser { const std::string &b) const { if (a.size() < b.size()) { return false; } for (size_t i = 0; i < b.size(); i++) { - if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { + return false; + } } return true; } @@ -4867,16 +4982,6 @@ class MultipartFormDataParser { size_t buf_epos_ = 0; }; -inline std::string to_lower(const char *beg, const char *end) { - std::string out; - auto it = beg; - while (it != end) { - out += static_cast(::tolower(*it)); - it++; - } - return out; -} - inline std::string random_string(size_t length) { static const char data[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -5451,6 +5556,7 @@ inline void hosted_at(const std::string &hostname, #endif return; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); for (auto rp = result; rp; rp = rp->ai_next) { const auto &addr = @@ -5462,8 +5568,6 @@ inline void hosted_at(const std::string &hostname, addrs.push_back(ip); } } - - freeaddrinfo(result); } inline std::string append_query_params(const std::string &path, @@ -5658,6 +5762,16 @@ inline void Response::set_chunked_content_provider( is_chunked_content_provider_ = true; } +inline void Response::set_file_content(const std::string &path, + const std::string &content_type) { + file_content_path_ = path; + file_content_content_type_ = content_type; +} + +inline void Response::set_file_content(const std::string &path) { + file_content_path_ = path; +} + // Result implementation inline bool Result::has_request_header(const std::string &key) const { return request_headers_.find(key) != request_headers_.end(); @@ -5807,6 +5921,8 @@ inline socket_t BufferStream::socket() const { return 0; } inline const std::string &BufferStream::get_buffer() const { return buffer; } inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { + static constexpr char marker[] = "/:"; + // One past the last ending position of a path param substring std::size_t last_param_end = 0; @@ -5819,13 +5935,14 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { #endif while (true) { - const auto marker_pos = pattern.find(marker, last_param_end); + const auto marker_pos = pattern.find( + marker, last_param_end == 0 ? last_param_end : last_param_end - 1); if (marker_pos == std::string::npos) { break; } static_fragments_.push_back( - pattern.substr(last_param_end, marker_pos - last_param_end)); + pattern.substr(last_param_end, marker_pos - last_param_end + 1)); - const auto param_name_start = marker_pos + 1; + const auto param_name_start = marker_pos + 2; auto sep_pos = pattern.find(separator, param_name_start); if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } @@ -5887,7 +6004,7 @@ inline bool PathParamsMatcher::match(Request &request) const { request.path_params.emplace( param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); - // Mark everythin up to '/' as matched + // Mark everything up to '/' as matched starting_pos = sep_pos + 1; } // Returns false if the path is longer than the pattern @@ -5986,7 +6103,8 @@ inline bool Server::set_base_dir(const std::string &dir, inline bool Server::set_mount_point(const std::string &mount_point, const std::string &dir, Headers headers) { - if (detail::is_dir(dir)) { + detail::FileStat stat(dir); + if (stat.is_dir()) { std::string mnt = !mount_point.empty() ? mount_point : "/"; if (!mnt.empty() && mnt[0] == '/') { base_dirs_.push_back({mnt, dir, std::move(headers)}); @@ -6250,23 +6368,24 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, if (close_connection || req.get_header_value("Connection") == "close") { res.set_header("Connection", "close"); } else { - std::stringstream ss; - ss << "timeout=" << keep_alive_timeout_sec_ - << ", max=" << keep_alive_max_count_; - res.set_header("Keep-Alive", ss.str()); + std::string s = "timeout="; + s += std::to_string(keep_alive_timeout_sec_); + s += ", max="; + s += std::to_string(keep_alive_max_count_); + res.set_header("Keep-Alive", s); } - if (!res.has_header("Content-Type") && - (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { + if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) && + !res.has_header("Content-Type")) { res.set_header("Content-Type", "text/plain"); } - if (!res.has_header("Content-Length") && res.body.empty() && - !res.content_length_ && !res.content_provider_) { + if (res.body.empty() && !res.content_length_ && !res.content_provider_ && + !res.has_header("Content-Length")) { res.set_header("Content-Length", "0"); } - if (!res.has_header("Accept-Ranges") && req.method == "HEAD") { + if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) { res.set_header("Accept-Ranges", "bytes"); } @@ -6275,12 +6394,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, // Response line and headers { detail::BufferStream bstrm; - - if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status, - status_message(res.status))) { - return false; - } - + if (!detail::write_response_line(bstrm, res.status)) { return false; } if (!header_writer_(bstrm, res.headers)) { return false; } // Flush buffer @@ -6474,7 +6588,14 @@ inline bool Server::handle_file_request(const Request &req, Response &res, auto path = entry.base_dir + sub_path; if (path.back() == '/') { path += "index.html"; } - if (detail::is_file(path)) { + detail::FileStat stat(path); + + if (stat.is_dir()) { + res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); + return true; + } + + if (stat.is_file()) { for (const auto &kv : entry.headers) { res.set_header(kv.first, kv.second); } @@ -6585,7 +6706,7 @@ inline bool Server::listen_internal() { if (errno == EMFILE) { // The per-process limit of open file descriptors has been reached. // Try to accept new connections after a short sleep. - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{1}); continue; } else if (errno == EINTR || errno == EAGAIN) { continue; @@ -6855,7 +6976,9 @@ inline bool Server::dispatch_request_for_content_reader( } inline bool -Server::process_request(Stream &strm, bool close_connection, +Server::process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, bool &connection_closed, const std::function &setup_request) { std::array buf{}; @@ -6909,11 +7032,13 @@ Server::process_request(Stream &strm, bool close_connection, connection_closed = true; } - strm.get_remote_ip_and_port(req.remote_addr, req.remote_port); + req.remote_addr = remote_addr; + req.remote_port = remote_port; req.set_header("REMOTE_ADDR", req.remote_addr); req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); - strm.get_local_ip_and_port(req.local_addr, req.local_port); + req.local_addr = local_addr; + req.local_port = local_port; req.set_header("LOCAL_ADDR", req.local_addr); req.set_header("LOCAL_PORT", std::to_string(req.local_port)); @@ -6935,10 +7060,12 @@ Server::process_request(Stream &strm, bool close_connection, switch (status) { case StatusCode::Continue_100: case StatusCode::ExpectationFailed_417: - strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, - status_message(status)); + detail::write_response_line(strm, status); + strm.write("\r\n"); break; - default: return write_response(strm, close_connection, req, res); + default: + connection_closed = true; + return write_response(strm, true, req, res); } } @@ -6992,6 +7119,32 @@ Server::process_request(Stream &strm, bool close_connection, return write_response(strm, close_connection, req, res); } + // Serve file content by using a content provider + if (!res.file_content_path_.empty()) { + const auto &path = res.file_content_path_; + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::NotFound_404; + return write_response(strm, close_connection, req, res); + } + + auto content_type = res.file_content_content_type_; + if (content_type.empty()) { + content_type = detail::find_content_type( + path, file_extension_and_mimetype_map_, default_file_mimetype_); + } + + res.set_content_provider( + mm->size(), content_type, + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + } + return write_response_with_content(strm, close_connection, req, res); } else { if (res.status == -1) { res.status = StatusCode::NotFound_404; } @@ -7003,12 +7156,21 @@ Server::process_request(Stream &strm, bool close_connection, inline bool Server::is_valid() const { return true; } inline bool Server::process_and_close_socket(socket_t sock) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + auto ret = detail::process_server_socket( svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, - [this](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, close_connection, connection_closed, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, connection_closed, nullptr); }); @@ -7027,8 +7189,8 @@ inline ClientImpl::ClientImpl(const std::string &host, int port) inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) - : host_(host), port_(port), - host_and_port_(adjust_host_string(host) + ":" + std::to_string(port)), + : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), + host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { @@ -7080,6 +7242,8 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT server_certificate_verification_ = rhs.server_certificate_verification_; + server_hostname_verification_ = rhs.server_hostname_verification_; + server_certificate_verifier_ = rhs.server_certificate_verifier_; #endif logger_ = rhs.logger_; } @@ -7544,7 +7708,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, detail::BufferStream bstrm; const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; - bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); + detail::write_request_line(bstrm, req.method, path); header_writer_(bstrm, req.headers); @@ -8557,13 +8721,11 @@ inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, std::size_t size) const { auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); + auto se = detail::scope_exit([&] { BIO_free_all(mem); }); if (!mem) { return nullptr; } auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); - if (!inf) { - BIO_free_all(mem); - return nullptr; - } + if (!inf) { return nullptr; } auto cts = X509_STORE_new(); if (cts) { @@ -8577,13 +8739,21 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, } sk_X509_INFO_pop_free(inf, X509_INFO_free); - BIO_free_all(mem); return cts; } inline void ClientImpl::enable_server_certificate_verification(bool enabled) { server_certificate_verification_ = enabled; } + +inline void ClientImpl::enable_server_hostname_verification(bool enabled) { + server_hostname_verification_ = enabled; +} + +inline void ClientImpl::set_server_certificate_verifier( + std::function verifier) { + server_certificate_verifier_ = verifier; +} #endif inline void ClientImpl::set_logger(Logger logger) { @@ -8645,7 +8815,7 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, auto ret = SSL_shutdown(ssl); while (ret == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds{100}); ret = SSL_shutdown(ssl); } #endif @@ -8752,7 +8922,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (SSL_pending(ssl_) > 0) { return SSL_read(ssl_, ptr, static_cast(size)); } else if (is_readable()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); @@ -8783,7 +8953,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { #endif if (is_writable()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_write(ssl_, ptr, static_cast(handle_size)); if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); @@ -8835,7 +9005,8 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != - 1) { + 1 || + SSL_CTX_check_private_key(ctx_) != 1) { SSL_CTX_free(ctx_); ctx_ = nullptr; } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { @@ -8915,13 +9086,22 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { auto ret = false; if (ssl) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + ret = detail::process_server_socket_ssl( svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, - [this, ssl](Stream &strm, bool close_connection, - bool &connection_closed) { - return process_request(strm, close_connection, connection_closed, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, + connection_closed, [&](Request &req) { req.ssl = ssl; }); }); @@ -8950,6 +9130,8 @@ inline SSLClient::SSLClient(const std::string &host, int port, : ClientImpl(host, port, client_cert_path, client_key_path) { ctx_ = SSL_CTX_new(TLS_client_method()); + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + detail::split(&host_[0], &host_[host_.size()], '.', [&](const char *b, const char *e) { host_components_.emplace_back(b, e); @@ -9157,26 +9339,34 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { } if (server_certificate_verification_) { - verify_result_ = SSL_get_verify_result(ssl2); + if (server_certificate_verifier_) { + if (!server_certificate_verifier_(ssl2)) { + error = Error::SSLServerVerification; + return false; + } + } else { + verify_result_ = SSL_get_verify_result(ssl2); - if (verify_result_ != X509_V_OK) { - error = Error::SSLServerVerification; - return false; - } + if (verify_result_ != X509_V_OK) { + error = Error::SSLServerVerification; + return false; + } - auto server_cert = SSL_get1_peer_certificate(ssl2); + auto server_cert = SSL_get1_peer_certificate(ssl2); + auto se = detail::scope_exit([&] { X509_free(server_cert); }); - if (server_cert == nullptr) { - error = Error::SSLServerVerification; - return false; - } + if (server_cert == nullptr) { + error = Error::SSLServerVerification; + return false; + } - if (!verify_host(server_cert)) { - X509_free(server_cert); - error = Error::SSLServerVerification; - return false; + if (server_hostname_verification_) { + if (!verify_host(server_cert)) { + error = Error::SSLServerHostnameVerification; + return false; + } + } } - X509_free(server_cert); } return true; @@ -9907,6 +10097,15 @@ inline void Client::set_proxy_digest_auth(const std::string &username, inline void Client::enable_server_certificate_verification(bool enabled) { cli_->enable_server_certificate_verification(enabled); } + +inline void Client::enable_server_hostname_verification(bool enabled) { + cli_->enable_server_hostname_verification(enabled); +} + +inline void Client::set_server_certificate_verifier( + std::function verifier) { + cli_->set_server_certificate_verifier(verifier); +} #endif inline void Client::set_logger(Logger logger) { From 6553cdedab9bcd87cc87d32f1550d57c237c1a13 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 13 Sep 2024 20:45:59 -0400 Subject: [PATCH 0295/1222] Enabled HostnameToIPConversionTest.YouTube_Online partially --- test/test.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/test.cc b/test/test.cc index b9bc977bce..235a05a534 100644 --- a/test/test.cc +++ b/test/test.cc @@ -734,19 +734,17 @@ TEST(HostnameToIPConversionTest, HTTPWatch_Online) { EXPECT_EQ(1u, addrs.size()); } -#if 0 // It depends on each test environment... TEST(HostnameToIPConversionTest, YouTube_Online) { auto host = "www.youtube.com"; std::vector addrs; hosted_at(host, addrs); - EXPECT_EQ(20u, addrs.size()); - auto it = std::find(addrs.begin(), addrs.end(), "2607:f8b0:4006:809::200e"); - EXPECT_TRUE(it != addrs.end()); + // It depends on each test environment... + // auto it = std::find(addrs.begin(), addrs.end(), "2607:f8b0:4006:809::200e"); + // EXPECT_TRUE(it != addrs.end()); } -#endif TEST(ChunkedEncodingTest, WithContentReceiver_Online) { auto host = "www.httpwatch.com"; From 6c93aea59a55eaee128a7abd15ebc88116410254 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 15 Sep 2024 01:18:20 -0400 Subject: [PATCH 0296/1222] Revert "Enabled HostnameToIPConversionTest.YouTube_Online partially" This reverts commit 6553cdedab9bcd87cc87d32f1550d57c237c1a13. --- test/test.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/test.cc b/test/test.cc index 235a05a534..b9bc977bce 100644 --- a/test/test.cc +++ b/test/test.cc @@ -734,17 +734,19 @@ TEST(HostnameToIPConversionTest, HTTPWatch_Online) { EXPECT_EQ(1u, addrs.size()); } +#if 0 // It depends on each test environment... TEST(HostnameToIPConversionTest, YouTube_Online) { auto host = "www.youtube.com"; std::vector addrs; hosted_at(host, addrs); + EXPECT_EQ(20u, addrs.size()); - // It depends on each test environment... - // auto it = std::find(addrs.begin(), addrs.end(), "2607:f8b0:4006:809::200e"); - // EXPECT_TRUE(it != addrs.end()); + auto it = std::find(addrs.begin(), addrs.end(), "2607:f8b0:4006:809::200e"); + EXPECT_TRUE(it != addrs.end()); } +#endif TEST(ChunkedEncodingTest, WithContentReceiver_Online) { auto host = "www.httpwatch.com"; From 5064373c23833ba4b07be51e209c27485d0ac0c3 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Tue, 17 Sep 2024 22:58:09 +0200 Subject: [PATCH 0297/1222] test(meson): fix `SSLClientServerTest.*` tests with OpenSSL 3.2.0 (#1940) * build(meson): bump minimum version to 0.62.0 This allows making some minor cleanups * test(meson): fix SSLClientServerTest.* tests with OpenSSL 3.2.0 Since OpenSSL commit , the default X.509 certificate format generated with the `openssl req` command has been changed to X.509 v3 from X.509 v1. For some reason, this change breaks cpp-httplib's SSLClientServerTest.* tests. To fix the test failures, this patch passes the '-x509v1' flag instead of '-x509' when OpenSSL 3.2.0 or newer is detected. To detect the version of a command line utility, Meson 0.62.0 or later is required. Fixes , but only for the Meson build system. --- meson.build | 10 ++++------ test/meson.build | 13 +++++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/meson.build b/meson.build index e82ae84514..9fa1919c8b 100644 --- a/meson.build +++ b/meson.build @@ -13,7 +13,7 @@ project( 'b_lto=true', 'warning_level=3' ], - meson_version: '>=0.47.0' + meson_version: '>=0.62.0' ) # Check just in case downstream decides to edit the source @@ -98,20 +98,18 @@ if get_option('cpp-httplib_compile') ) else install_headers('httplib.h') - cpp_httplib_dep = declare_dependency(compile_args: args, dependencies: deps, include_directories: include_directories('.')) + cpp_httplib_dep = declare_dependency(compile_args: args, dependencies: deps, include_directories: '.') import('pkgconfig').generate( name: 'cpp-httplib', description: 'A C++ HTTP/HTTPS server and client library', - install_dir: join_paths(get_option('datadir'), 'pkgconfig'), + install_dir: get_option('datadir')/'pkgconfig', url: 'https://github.com/yhirose/cpp-httplib', version: version ) endif -if meson.version().version_compare('>=0.54.0') - meson.override_dependency('cpp-httplib', cpp_httplib_dep) -endif +meson.override_dependency('cpp-httplib', cpp_httplib_dep) if get_option('cpp-httplib_test') subdir('test') diff --git a/test/meson.build b/test/meson.build index be9ca2b0f5..e7819dbfb6 100644 --- a/test/meson.build +++ b/test/meson.build @@ -6,6 +6,7 @@ gtest_dep = dependency('gtest', main: true) libcurl_dep = dependency('libcurl') openssl = find_program('openssl') test_conf = files('test.conf') +req_x509_flag = openssl.version().version_compare('>=3.2.0') ? '-x509v1' : '-x509' key_pem = custom_target( 'key_pem', @@ -31,7 +32,7 @@ cert2_pem = custom_target( 'cert2_pem', input: key_pem, output: 'cert2.pem', - command: [openssl, 'req', '-x509', '-config', test_conf, '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-out', '@OUTPUT@', '-extensions', 'SAN'] + command: [openssl, 'req', req_x509_flag, '-config', test_conf, '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-out', '@OUTPUT@', '-extensions', 'SAN'] ) key_encrypted_pem = custom_target( @@ -44,7 +45,7 @@ cert_encrypted_pem = custom_target( 'cert_encrypted_pem', input: key_encrypted_pem, output: 'cert_encrypted.pem', - command: [openssl, 'req', '-x509', '-config', test_conf, '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-out', '@OUTPUT@', '-extensions', 'SAN'] + command: [openssl, 'req', req_x509_flag, '-config', test_conf, '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-out', '@OUTPUT@', '-extensions', 'SAN'] ) rootca_key_pem = custom_target( @@ -57,7 +58,7 @@ rootca_cert_pem = custom_target( 'rootca_cert_pem', input: rootca_key_pem, output: 'rootCA.cert.pem', - command: [openssl, 'req', '-x509', '-new', '-batch', '-config', files('test.rootCA.conf'), '-key', '@INPUT@', '-days', '1024', '-out', '@OUTPUT@'] + command: [openssl, 'req', req_x509_flag, '-new', '-batch', '-config', files('test.rootCA.conf'), '-key', '@INPUT@', '-days', '1024', '-out', '@OUTPUT@'] ) client_key_pem = custom_target( @@ -103,9 +104,9 @@ client_encrypted_cert_pem = custom_target( # Copy test files to the build directory configure_file(input: 'ca-bundle.crt', output: 'ca-bundle.crt', copy: true) configure_file(input: 'image.jpg', output: 'image.jpg', copy: true) -subdir(join_paths('www', 'dir')) -subdir(join_paths('www2', 'dir')) -subdir(join_paths('www3', 'dir')) +subdir('www' /'dir') +subdir('www2'/'dir') +subdir('www3'/'dir') # GoogleTest 1.13.0 requires C++14 test_options = [] From 4990b4b4b7c4d2c927bb3fc376e0a8083682dccb Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Sep 2024 17:00:17 -0400 Subject: [PATCH 0298/1222] Fix problems with `SSLSlientServerTest.*` tests --- test/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index 96ebec9b56..6691641a25 100644 --- a/test/Makefile +++ b/test/Makefile @@ -67,7 +67,7 @@ cert.pem: openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN openssl genrsa 2048 > rootCA.key.pem - openssl req -x509 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem + openssl req -x509v1 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem openssl genrsa 2048 > client.key.pem openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem From 7018e9263d08697fad008a5e20a99c6ed3e5880e Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Wed, 18 Sep 2024 00:06:01 +0200 Subject: [PATCH 0299/1222] test(meson): copy files in www directory (#1941) These files were added in commits 2d01e712866d3ed17d33569b8fa5345f5cade146 and b8315278cb4c313e97f9b980d29b09d35f742a97 --- test/meson.build | 2 +- test/www/meson.build | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 test/www/meson.build diff --git a/test/meson.build b/test/meson.build index e7819dbfb6..80621d30aa 100644 --- a/test/meson.build +++ b/test/meson.build @@ -104,7 +104,7 @@ client_encrypted_cert_pem = custom_target( # Copy test files to the build directory configure_file(input: 'ca-bundle.crt', output: 'ca-bundle.crt', copy: true) configure_file(input: 'image.jpg', output: 'image.jpg', copy: true) -subdir('www' /'dir') +subdir('www') subdir('www2'/'dir') subdir('www3'/'dir') diff --git a/test/www/meson.build b/test/www/meson.build new file mode 100644 index 0000000000..ed3d357716 --- /dev/null +++ b/test/www/meson.build @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2024 Andrea Pappacoda +# +# SPDX-License-Identifier: MIT + +configure_file(input: 'empty_file', output: 'empty_file', copy: true) +configure_file(input: 'file', output: 'file', copy: true) +subdir('dir') From c239087332c7de20a013a0311e5c25e0c7f85542 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Sep 2024 18:37:16 -0400 Subject: [PATCH 0300/1222] Fix Mafile errors --- test/Makefile | 14 ++------------ test/gen-certs.sh | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) create mode 100755 test/gen-certs.sh diff --git a/test/Makefile b/test/Makefile index 6691641a25..587a6753ff 100644 --- a/test/Makefile +++ b/test/Makefile @@ -63,18 +63,8 @@ httplib.cc : ../httplib.h python3 ../split.py -o . cert.pem: - openssl genrsa 2048 > key.pem - openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem - openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN - openssl genrsa 2048 > rootCA.key.pem - openssl req -x509v1 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem - openssl genrsa 2048 > client.key.pem - openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem - openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem - openssl req -new -batch -config test.conf -key key_encrypted.pem | openssl x509 -days 3650 -req -signkey key_encrypted.pem > cert_encrypted.pem - openssl genrsa -aes256 -passout pass:test012! 2048 > client_encrypted.key.pem - openssl req -new -batch -config test.conf -key client_encrypted.key.pem -passin pass:test012! | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client_encrypted.cert.pem - #c_rehash . + ./gen-certs.sh clean: rm -f test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc + diff --git a/test/gen-certs.sh b/test/gen-certs.sh new file mode 100755 index 0000000000..ee2a2cf310 --- /dev/null +++ b/test/gen-certs.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +if [[ $(openssl version) =~ 3\.[2-9]\.[0-9]+ ]]; then + OPENSSL_X509_FLAG='-x509v1' +else + OPENSSL_X509_FLAG='-x509' +fi + +openssl genrsa 2048 > key.pem +openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem +openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN +openssl genrsa 2048 > rootCA.key.pem +openssl req $OPENSSL_X509_FLAG -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem +openssl genrsa 2048 > client.key.pem +openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem +openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem +openssl req -new -batch -config test.conf -key key_encrypted.pem | openssl x509 -days 3650 -req -signkey key_encrypted.pem > cert_encrypted.pem +openssl genrsa -aes256 -passout pass:test012! 2048 > client_encrypted.key.pem +openssl req -new -batch -config test.conf -key client_encrypted.key.pem -passin pass:test012! | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client_encrypted.cert.pem From 7c4799d0cf6aa8b2053f763343dd6dc2bb0a22a4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 19 Sep 2024 18:33:32 -0400 Subject: [PATCH 0301/1222] Fix #1798 for CMake (#1944) --- test/CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f52604777d..a9982b922a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,6 +37,11 @@ file( ) if(HTTPLIB_IS_USING_OPENSSL) + if (OPENSSL_VERSION VERSION_LESS "3.2.0") + set(OPENSSL_X509_FLAG "-x509") + else() + set(OPENSSL_X509_FLAG "-x509v1") + endif() find_program(OPENSSL_COMMAND NAMES openssl PATHS ${OPENSSL_INCLUDE_DIR}/../bin @@ -56,7 +61,7 @@ if(HTTPLIB_IS_USING_OPENSSL) COMMAND_ERROR_IS_FATAL ANY ) execute_process( - COMMAND ${OPENSSL_COMMAND} req -x509 -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN + COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND_ERROR_IS_FATAL ANY ) @@ -67,7 +72,7 @@ if(HTTPLIB_IS_USING_OPENSSL) COMMAND_ERROR_IS_FATAL ANY ) execute_process( - COMMAND ${OPENSSL_COMMAND} req -x509 -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024 + COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024 OUTPUT_FILE rootCA.cert.pem WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMAND_ERROR_IS_FATAL ANY From 996acc52537a62435769bf1f873ad0504d6cf620 Mon Sep 17 00:00:00 2001 From: zjyhjqs <141055431+zjyhjqs@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:19:41 +0800 Subject: [PATCH 0302/1222] Feat: add CPack support (#1950) --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e27481bc5d..61419c63a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -299,6 +299,8 @@ if(HTTPLIB_INSTALL) # ex: /usr/share/doc/httplib/README.md and /usr/share/licenses/httplib/LICENSE install(FILES "README.md" DESTINATION "${CMAKE_INSTALL_DOCDIR}") install(FILES "LICENSE" DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/${PROJECT_NAME}") + + include(CPack) endif() if(HTTPLIB_TEST) From 10d68cff5081d8c7b7cae74000e0c423107dc7b8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 26 Sep 2024 22:24:44 -0400 Subject: [PATCH 0303/1222] Added a unit test for #1946 --- test/test.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test.cc b/test/test.cc index b9bc977bce..76c6f60d42 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3700,6 +3700,16 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) { EXPECT_EQ(0U, res->body.size()); } +TEST_F(ServerTest, GetRangeWithZeroToInfinite) { + auto res = cli_.Get("/with-range", {{"Range", "bytes=0-"}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ("7", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 0-6/7", res->get_header_value("Content-Range")); + EXPECT_EQ(std::string("abcdefg"), res->body); +} + TEST_F(ServerTest, GetStreamedWithRangeMultipart) { auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}}); From 131bc6c6743691af673f67371dae812258942aea Mon Sep 17 00:00:00 2001 From: Andrew McDaniel Date: Thu, 3 Oct 2024 10:53:25 -0400 Subject: [PATCH 0304/1222] Add documentation for using Unix domain sockets. (#1954) * Add documentation for using unix domain sockets. * Formatting --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 0a525122ad..7148e5502a 100644 --- a/README.md +++ b/README.md @@ -843,6 +843,23 @@ Use `poll` instead of `select` `select` system call is used as default since it's more widely supported. If you want to let cpp-httplib use `poll` instead, you can do so with `CPPHTTPLIB_USE_POLL`. +Unix Domain Socket Support +-------------------------- + +Unix Domain Socket support is available on Linux and macOS. + +```c++ +// Server +httplib::Server svr("./my-socket.sock"); +svr.set_address_family(AF_UNIX).listen("./my-socket.sock", 80); + +// Client +httplib::Client cli("./my-socket.sock"); +cli.set_address_family(AF_UNIX); +``` + +"my-socket.sock" can be a relative path or an absolute path. You application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. + Split httplib.h into .h and .cc ------------------------------- From e0ebc431dc78e3eee04400c0619162f94f6476ab Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 11 Oct 2024 13:29:55 -0400 Subject: [PATCH 0305/1222] Fix #1959 --- httplib.h | 42 ++++++++++++++++++++++++++++++++++++++++-- test/test.cc | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 121ce34955..99112b9063 100644 --- a/httplib.h +++ b/httplib.h @@ -18,6 +18,10 @@ #define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 #endif +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND 10000 +#endif + #ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT #define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 #endif @@ -3251,6 +3255,41 @@ class SSLSocketStream final : public Stream { }; #endif +inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, + time_t keep_alive_timeout_sec) { + using namespace std::chrono; + + const auto interval_usec = + CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND; + + // Avoid expensive `steady_clock::now()` call for the first time + if (select_read(sock, 0, interval_usec) > 0) { return true; } + + const auto start = steady_clock::now() - microseconds{interval_usec}; + const auto timeout = seconds{keep_alive_timeout_sec}; + + while (true) { + if (svr_sock == INVALID_SOCKET) { + break; // Server socket is closed + } + + auto val = select_read(sock, 0, interval_usec); + if (val < 0) { + break; // Ssocket error + } else if (val == 0) { + if (steady_clock::now() - start > timeout) { + break; // Timeout + } + } else { + return true; // Ready for read + } + + std::this_thread::sleep_for(microseconds{interval_usec}); + } + + return false; +} + template inline bool process_server_socket_core(const std::atomic &svr_sock, socket_t sock, @@ -3259,8 +3298,7 @@ process_server_socket_core(const std::atomic &svr_sock, socket_t sock, assert(keep_alive_max_count > 0); auto ret = false; auto count = keep_alive_max_count; - while (svr_sock != INVALID_SOCKET && count > 0 && - select_read(sock, keep_alive_timeout_sec, 0) > 0) { + while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { auto close_connection = count == 1; auto connection_closed = false; ret = callback(close_connection, connection_closed); diff --git a/test/test.cc b/test/test.cc index 76c6f60d42..0cd450e763 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5268,6 +5268,41 @@ TEST(KeepAliveTest, Issue1041) { EXPECT_EQ(StatusCode::OK_200, result->status); } +TEST(KeepAliveTest, Issue1959) { + Server svr; + svr.set_keep_alive_timeout(5); + + svr.Get("/a", [&](const Request & /*req*/, Response &res) { + res.set_content("a", "text/plain"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + if (!svr.is_running()) return; + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli("localhost", PORT); + cli.set_keep_alive(true); + + using namespace std::chrono; + auto start = steady_clock::now(); + + cli.Get("/a"); + + svr.stop(); + listen_thread.join(); + + auto end = steady_clock::now(); + auto elapsed = duration_cast(end - start).count(); + + EXPECT_LT(elapsed, 5000); +} + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(KeepAliveTest, SSLClientReconnection) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); From d79633ff52c2dc07becf20a589b706dcc6f87f95 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 11 Oct 2024 14:49:46 -0400 Subject: [PATCH 0306/1222] clangformat --- httplib.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 99112b9063..11faac889f 100644 --- a/httplib.h +++ b/httplib.h @@ -369,8 +369,9 @@ inline unsigned char to_lower(int c) { inline bool equal(const std::string &a, const std::string &b) { return a.size() == b.size() && - std::equal(a.begin(), a.end(), b.begin(), - [](char ca, char cb) { return to_lower(ca) == to_lower(cb); }); + std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) { + return to_lower(ca) == to_lower(cb); + }); } struct equal_to { @@ -2900,9 +2901,7 @@ inline void stream_line_reader::append(char c) { } } -inline mmap::mmap(const char *path) { - open(path); -} +inline mmap::mmap(const char *path) { open(path); } inline mmap::~mmap() { close(); } @@ -9492,8 +9491,8 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { auto type = GEN_DNS; - struct in6_addr addr6 {}; - struct in_addr addr {}; + struct in6_addr addr6{}; + struct in_addr addr{}; size_t addr_len = 0; #ifndef __MINGW32__ From f884a56258475df626d91e06398f856891a76a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= Date: Tue, 15 Oct 2024 12:32:13 +1100 Subject: [PATCH 0307/1222] Remove space between operator"" and _t (#1962) This should fix a -Wdeprecated-literal-operator instance since this is deprecated as a result of CWG2521 (iiuc C++23). --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 99112b9063..e81e7ef0ed 100644 --- a/httplib.h +++ b/httplib.h @@ -3760,7 +3760,7 @@ inline unsigned int str2tag(const std::string &s) { namespace udl { -inline constexpr unsigned int operator"" _t(const char *s, size_t l) { +inline constexpr unsigned int operator""_t(const char *s, size_t l) { return str2tag_core(s, l, 0); } From 0cc1ca9a8dcff402891582dda4da34a5d1687adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= Date: Tue, 15 Oct 2024 15:09:04 +1100 Subject: [PATCH 0308/1222] Remove extra semicolon (#1963) This fixes a -Wc++98-compat-extra-semi instance. --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index e81e7ef0ed..72bd2b6307 100644 --- a/httplib.h +++ b/httplib.h @@ -395,7 +395,7 @@ struct hash { } }; -}; // namespace case_ignore +} // namespace case_ignore // This is based on // "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". From d869054318d2289d92b2f306cfa2335d2be22091 Mon Sep 17 00:00:00 2001 From: Jiwoo Park Date: Fri, 18 Oct 2024 23:16:48 +0900 Subject: [PATCH 0309/1222] Allow empty header values (#1965) --- httplib.h | 2 +- test/test.cc | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 72bd2b6307..56af8f673c 100644 --- a/httplib.h +++ b/httplib.h @@ -4115,7 +4115,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) { p++; } - if (p < end) { + if (p <= end) { auto key_len = key_end - beg; if (!key_len) { return false; } diff --git a/test/test.cc b/test/test.cc index 0cd450e763..612304b371 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4922,6 +4922,15 @@ TEST(ServerRequestParsingTest, InvalidFieldValueContains_CR_LF_NUL) { EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); } +TEST(ServerRequestParsingTest, EmptyFieldValue) { + std::string out; + + test_raw_request("GET /header_field_value_check HTTP/1.1\r\n" + "Test: \r\n\r\n", + &out); + EXPECT_EQ("HTTP/1.1 200 OK", out.substr(0, 15)); +} + TEST(ServerStopTest, StopServerWithChunkedTransmission) { Server svr; From 5c1a34e766817cf0865ca8bc5af5edeb0f9b2df4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 18 Oct 2024 17:16:54 -0400 Subject: [PATCH 0310/1222] Release v0.18.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 44f1da4c40..c621f5b53d 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.0" +#define CPPHTTPLIB_VERSION "0.18.1" /* * Configuration From 924f214303b860b78350e1e2dfb0521a8724464f Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 2 Nov 2024 07:23:44 -0400 Subject: [PATCH 0311/1222] Added unit test for exception handler --- test/test.cc | 146 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 103 insertions(+), 43 deletions(-) diff --git a/test/test.cc b/test/test.cc index 612304b371..154b60c5e2 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2013,7 +2013,46 @@ TEST(ErrorHandlerTest, ContentLength) { } #ifndef CPPHTTPLIB_NO_EXCEPTIONS -TEST(ExceptionHandlerTest, ContentLength) { +TEST(ExceptionTest, WithoutExceptionHandler) { + Server svr; + + svr.Get("/exception", [&](const Request & /*req*/, Response & /*res*/) { + throw std::runtime_error("exception..."); + }); + + svr.Get("/unknown", [&](const Request & /*req*/, Response & /*res*/) { + throw std::runtime_error("exception\r\n..."); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + { + auto res = cli.Get("/exception"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); + ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); + EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT")); + } + + { + auto res = cli.Get("/unknown"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); + ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); + EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT")); + } +} + +TEST(ExceptionTest, WithExceptionHandler) { Server svr; svr.set_exception_handler([](const Request & /*req*/, Response &res, @@ -2021,7 +2060,9 @@ TEST(ExceptionHandlerTest, ContentLength) { EXPECT_FALSE(ep == nullptr); try { std::rethrow_exception(ep); - } catch (std::exception &e) { EXPECT_EQ("abc", std::string(e.what())); } + } catch (std::exception &e) { + EXPECT_EQ("abc", std::string(e.what())); + } catch (...) {} res.status = StatusCode::InternalServerError_500; res.set_content("abcdefghijklmnopqrstuvwxyz", "text/html"); // <= Content-Length still 13 at this point @@ -2065,6 +2106,66 @@ TEST(ExceptionHandlerTest, ContentLength) { } } } + +TEST(ExceptionTest, AndErrorHandler) { + Server svr; + + svr.set_error_handler([](const Request & /*req*/, Response &res) { + if (res.body.empty()) { res.set_content("NOT_FOUND", "text/html"); } + }); + + svr.set_exception_handler( + [](const Request & /*req*/, Response &res, std::exception_ptr ep) { + EXPECT_FALSE(ep == nullptr); + try { + std::rethrow_exception(ep); + } catch (std::exception &e) { + res.set_content(e.what(), "text/html"); + } catch (...) {} + res.status = StatusCode::InternalServerError_500; + }); + + svr.Get("/exception", [](const Request & /*req*/, Response &res) { + throw std::runtime_error("EXCEPTION"); + }); + + svr.Get("/error", [](const Request & /*req*/, Response &res) { + res.set_content("ERROR", "text/html"); + res.status = StatusCode::InternalServerError_500; + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + + { + auto res = cli.Get("/exception"); + ASSERT_TRUE(res); + EXPECT_EQ("text/html", res->get_header_value("Content-Type")); + EXPECT_EQ("EXCEPTION", res->body); + } + + { + auto res = cli.Get("/error"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::InternalServerError_500, res->status); + EXPECT_EQ("ERROR", res->body); + } + + { + auto res = cli.Get("/invalid"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::NotFound_404, res->status); + EXPECT_EQ("NOT_FOUND", res->body); + } +} #endif TEST(NoContentTest, ContentLength) { @@ -5170,47 +5271,6 @@ TEST(MountTest, Redicect) { EXPECT_EQ(StatusCode::OK_200, res->status); } -#ifndef CPPHTTPLIB_NO_EXCEPTIONS -TEST(ExceptionTest, ThrowExceptionInHandler) { - Server svr; - - svr.Get("/exception", [&](const Request & /*req*/, Response & /*res*/) { - throw std::runtime_error("exception..."); - }); - - svr.Get("/unknown", [&](const Request & /*req*/, Response & /*res*/) { - throw std::runtime_error("exception\r\n..."); - }); - - auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); - auto se = detail::scope_exit([&] { - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); - }); - - svr.wait_until_ready(); - - Client cli("localhost", PORT); - - { - auto res = cli.Get("/exception"); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::InternalServerError_500, res->status); - ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); - EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT")); - } - - { - auto res = cli.Get("/unknown"); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::InternalServerError_500, res->status); - ASSERT_TRUE(res->has_header("EXCEPTION_WHAT")); - EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT")); - } -} -#endif - TEST(KeepAliveTest, ReadTimeout) { Server svr; From 9dd565b6e37c843129ade4e02761e18657297c33 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 13 Nov 2024 22:47:09 -0500 Subject: [PATCH 0312/1222] Resolve #1973 (#1976) * Fix #1973 * Fixed problems with 'Language for non-Unicode programs' setting on Windows * Fix problems on English locale --- httplib.h | 31 ++++++++++++++++--- test/test.cc | 30 +++++++++++++++--- ...6\227\245\346\234\254\350\252\236File.txt" | 1 + 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 "test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" diff --git a/httplib.h b/httplib.h index c621f5b53d..d75dcf0df5 100644 --- a/httplib.h +++ b/httplib.h @@ -2258,13 +2258,33 @@ make_basic_authentication_header(const std::string &username, namespace detail { +#if defined(_WIN32) +std::wstring u8string_to_wstring(const char *s) { + std::wstring ws; + auto len = static_cast(strlen(s)); + auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); + if (wlen > 0) { + ws.resize(wlen); + wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, const_cast(reinterpret_cast(ws.data())), wlen); + if (wlen != ws.size()) { + ws.clear(); + } + } + return ws; +} +#endif + struct FileStat { FileStat(const std::string &path); bool is_file() const; bool is_dir() const; private: +#if defined(_WIN32) + struct _stat st_; +#else struct stat st_; +#endif int ret_ = -1; }; @@ -2639,7 +2659,12 @@ inline bool is_valid_path(const std::string &path) { } inline FileStat::FileStat(const std::string &path) { +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path.c_str()); + ret_ = _wstat(wpath.c_str(), &st_); +#else ret_ = stat(path.c_str(), &st_); +#endif } inline bool FileStat::is_file() const { return ret_ >= 0 && S_ISREG(st_.st_mode); @@ -2909,10 +2934,8 @@ inline bool mmap::open(const char *path) { close(); #if defined(_WIN32) - std::wstring wpath; - for (size_t i = 0; i < strlen(path); i++) { - wpath += path[i]; - } + auto wpath = u8string_to_wstring(path); + if (wpath.empty()) { return false; } #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, diff --git a/test/test.cc b/test/test.cc index 154b60c5e2..556bf2497d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1,3 +1,4 @@ +// NOTE: This file should be saved as UTF-8 w/ BOM #include #include @@ -241,7 +242,7 @@ TEST(DecodeURLTest, PercentCharacter) { detail::decode_url( R"(descrip=Gastos%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1%C3%91%206)", false), - R"(descrip=Gastos áéíóúñÑ 6)"); + u8"descrip=Gastos áéíóúñÑ 6"); } TEST(DecodeURLTest, PercentCharacterNUL) { @@ -267,9 +268,9 @@ TEST(EncodeQueryParamTest, ParseReservedCharactersTest) { } TEST(EncodeQueryParamTest, TestUTF8Characters) { - string chineseCharacters = "中国語"; - string russianCharacters = "дом"; - string brazilianCharacters = "óculos"; + string chineseCharacters = u8"中国語"; + string russianCharacters = u8"дом"; + string brazilianCharacters = u8"óculos"; EXPECT_EQ(detail::encode_query_param(chineseCharacters), "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); @@ -5271,6 +5272,27 @@ TEST(MountTest, Redicect) { EXPECT_EQ(StatusCode::OK_200, res->status); } +TEST(MountTest, MultibytesPathName) { + Server svr; + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.set_mount_point("/", "./www"); + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + auto res = cli.Get(u8"/日本語Dir/日本語File.txt"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(u8"日本語コンテンツ", res->body); +} + TEST(KeepAliveTest, ReadTimeout) { Server svr; diff --git "a/test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" "b/test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" new file mode 100644 index 0000000000..3cc1ce7c92 --- /dev/null +++ "b/test/www/\346\227\245\346\234\254\350\252\236Dir/\346\227\245\346\234\254\350\252\236File.txt" @@ -0,0 +1 @@ +日本語コンテンツ \ No newline at end of file From b1b4bb8850392b050a6634434ab1e38df6c65f3c Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 13 Nov 2024 22:50:03 -0500 Subject: [PATCH 0313/1222] clangformat --- httplib.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index d75dcf0df5..1773014d24 100644 --- a/httplib.h +++ b/httplib.h @@ -2265,10 +2265,10 @@ std::wstring u8string_to_wstring(const char *s) { auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); if (wlen > 0) { ws.resize(wlen); - wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, const_cast(reinterpret_cast(ws.data())), wlen); - if (wlen != ws.size()) { - ws.clear(); - } + wlen = ::MultiByteToWideChar( + CP_UTF8, 0, s, len, + const_cast(reinterpret_cast(ws.data())), wlen); + if (wlen != ws.size()) { ws.clear(); } } return ws; } From 26208363eee84d6911fdb2175ce2e050cb110427 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Nov 2024 16:46:09 -0500 Subject: [PATCH 0314/1222] Fix warning --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 556bf2497d..007842a63d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2126,7 +2126,7 @@ TEST(ExceptionTest, AndErrorHandler) { res.status = StatusCode::InternalServerError_500; }); - svr.Get("/exception", [](const Request & /*req*/, Response &res) { + svr.Get("/exception", [](const Request & /*req*/, Response & /*res*/) { throw std::runtime_error("EXCEPTION"); }); From 7bd316f3d0a6bee6bbab79ec3896f9fd9a2a9ad1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Nov 2024 16:46:27 -0500 Subject: [PATCH 0315/1222] Fix #1977 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 1773014d24..2c3423fc2c 100644 --- a/httplib.h +++ b/httplib.h @@ -2259,7 +2259,7 @@ make_basic_authentication_header(const std::string &username, namespace detail { #if defined(_WIN32) -std::wstring u8string_to_wstring(const char *s) { +inline std::wstring u8string_to_wstring(const char *s) { std::wstring ws; auto len = static_cast(strlen(s)); auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); From bfef4b3e9b481fbadaa8c47f3141bc8cb45104dc Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Nov 2024 17:25:51 -0500 Subject: [PATCH 0316/1222] Fix #1975 --- httplib.h | 12 ++++++++++++ test/test.cc | 49 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index 2c3423fc2c..0917617e93 100644 --- a/httplib.h +++ b/httplib.h @@ -7702,6 +7702,18 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } + if (!req.has_header("Accept-Encoding")) { + std::string accept_encoding; +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + accept_encoding = "br"; +#endif +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "gzip, deflate"; +#endif + req.set_header("Accept-Encoding", accept_encoding); + } + #ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT if (!req.has_header("User-Agent")) { auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; diff --git a/test/test.cc b/test/test.cc index 007842a63d..822c8bb137 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2004,7 +2004,7 @@ TEST(ErrorHandlerTest, ContentLength) { { Client cli(HOST, PORT); - auto res = cli.Get("/hi"); + auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -2087,7 +2087,7 @@ TEST(ExceptionTest, WithExceptionHandler) { Client cli(HOST, PORT); for (size_t j = 0; j < 100; j++) { - auto res = cli.Get("/hi"); + auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -2098,7 +2098,7 @@ TEST(ExceptionTest, WithExceptionHandler) { cli.set_keep_alive(true); for (size_t j = 0; j < 100; j++) { - auto res = cli.Get("/hi"); + auto res = cli.Get("/hi", {{"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::InternalServerError_500, res->status); EXPECT_EQ("text/html", res->get_header_value("Content-Type")); @@ -3803,7 +3803,10 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) { } TEST_F(ServerTest, GetRangeWithZeroToInfinite) { - auto res = cli_.Get("/with-range", {{"Range", "bytes=0-"}}); + auto res = cli_.Get("/with-range", { + {"Range", "bytes=0-"}, + {"Accept-Encoding", ""}, + }); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("7", res->get_header_value("Content-Length")); @@ -3899,7 +3902,10 @@ TEST_F(ServerTest, ClientStop) { } TEST_F(ServerTest, GetWithRange1) { - auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}}); + auto res = cli_.Get("/with-range", { + make_range_header({{3, 5}}), + {"Accept-Encoding", ""}, + }); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("3", res->get_header_value("Content-Length")); @@ -3909,7 +3915,10 @@ TEST_F(ServerTest, GetWithRange1) { } TEST_F(ServerTest, GetWithRange2) { - auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}}); + auto res = cli_.Get("/with-range", { + make_range_header({{1, -1}}), + {"Accept-Encoding", ""}, + }); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("6", res->get_header_value("Content-Length")); @@ -3919,7 +3928,10 @@ TEST_F(ServerTest, GetWithRange2) { } TEST_F(ServerTest, GetWithRange3) { - auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}}); + auto res = cli_.Get("/with-range", { + make_range_header({{0, 0}}), + {"Accept-Encoding", ""}, + }); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("1", res->get_header_value("Content-Length")); @@ -3929,7 +3941,10 @@ TEST_F(ServerTest, GetWithRange3) { } TEST_F(ServerTest, GetWithRange4) { - auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}}); + auto res = cli_.Get("/with-range", { + make_range_header({{-1, 2}}), + {"Accept-Encoding", ""}, + }); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ("2", res->get_header_value("Content-Length")); @@ -4674,7 +4689,9 @@ TEST_F(ServerTest, Gzip) { } TEST_F(ServerTest, GzipWithoutAcceptEncoding) { - auto res = cli_.Get("/compress"); + Headers headers; + headers.emplace("Accept-Encoding", ""); + auto res = cli_.Get("/compress", headers); ASSERT_TRUE(res); EXPECT_TRUE(res->get_header_value("Content-Encoding").empty()); @@ -4723,12 +4740,16 @@ TEST_F(ServerTest, GzipWithoutDecompressing) { } TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) { + Headers headers; + headers.emplace("Accept-Encoding", ""); + std::string body; - auto res = cli_.Get("/compress", [&](const char *data, uint64_t data_length) { - EXPECT_EQ(100U, data_length); - body.append(data, data_length); - return true; - }); + auto res = cli_.Get("/compress", headers, + [&](const char *data, uint64_t data_length) { + EXPECT_EQ(100U, data_length); + body.append(data, data_length); + return true; + }); ASSERT_TRUE(res); EXPECT_TRUE(res->get_header_value("Content-Encoding").empty()); From 412ba04d1933cb13ff37a5b0184dc8691dde563c Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 14 Nov 2024 20:33:08 -0500 Subject: [PATCH 0317/1222] Fix problem caused by #1975 --- httplib.h | 24 +++++++++++++----------- test/test.cc | 1 + 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index 0917617e93..e50ad45613 100644 --- a/httplib.h +++ b/httplib.h @@ -7702,24 +7702,26 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } - if (!req.has_header("Accept-Encoding")) { - std::string accept_encoding; + if (!req.content_receiver) { + if (!req.has_header("Accept-Encoding")) { + std::string accept_encoding; #ifdef CPPHTTPLIB_BROTLI_SUPPORT - accept_encoding = "br"; + accept_encoding = "br"; #endif #ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (!accept_encoding.empty()) { accept_encoding += ", "; } - accept_encoding += "gzip, deflate"; + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "gzip, deflate"; #endif - req.set_header("Accept-Encoding", accept_encoding); - } + req.set_header("Accept-Encoding", accept_encoding); + } #ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT - if (!req.has_header("User-Agent")) { - auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; - req.set_header("User-Agent", agent); - } + if (!req.has_header("User-Agent")) { + auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; + req.set_header("User-Agent", agent); + } #endif + }; if (req.body.empty()) { if (req.content_provider_) { diff --git a/test/test.cc b/test/test.cc index 822c8bb137..cc9ffeb526 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5609,6 +5609,7 @@ TEST(LongPollingTest, ClientCloseDetection) { auto count = 10; while (count > 0 && sink.is_writable()) { this_thread::sleep_for(chrono::milliseconds(10)); + count--; } EXPECT_FALSE(sink.is_writable()); // the socket is closed return true; From 970b52897c8f7b358a14940b3c33de3267c7b7fa Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 16 Nov 2024 02:09:52 -0500 Subject: [PATCH 0318/1222] Fix #1980 Fix #1980 --- .github/workflows/test.yaml | 42 ++++++++++++++++++++++++++++--------- httplib.h | 24 ++++++++++++++++++--- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index cf2104feee..3d84808c1a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -11,7 +11,7 @@ jobs: - name: install libraries run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev - name: build and run tests - run: cd test && make -j4 + run: cd test && make - name: run fuzz test target run: cd test && make fuzz_test @@ -21,23 +21,45 @@ jobs: - name: checkout uses: actions/checkout@v4 - name: build and run tests - run: | - cd test && make -j2 + run: cd test && make + - name: run fuzz test target + run: cd test && make fuzz_test windows: runs-on: windows-latest steps: - - name: prepare git for checkout on windows + - name: Prepare Git for Checkout on Windows run: | git config --global core.autocrlf false git config --global core.eol lf - - name: checkout + - name: Checkout uses: actions/checkout@v4 - - name: setup msbuild on windows + - name: Export GitHub Actions cache environment variables + uses: actions/github-script@v7 + with: + script: | + core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || ''); + core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); + - name: Setup msbuild on windows uses: microsoft/setup-msbuild@v2 - - name: make-windows + - name: Install libraries run: | - cd test - msbuild.exe test.sln /verbosity:minimal /t:Build "/p:Configuration=Release;Platform=x64" - x64\Release\test.exe + vcpkg install gtest curl zlib brotli + choco install openssl + + - name: Configure CMake with SSL + run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON + - name: Build with with SSL + run: cmake --build build --config Release + - name: Run tests with SSL + run: ctest --output-on-failure --test-dir build -C Release + - name: Configure CMake without SSL + run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON + - name: Build without SSL + run: cmake --build build-no-ssl --config Release + - name: Run tests without SSL + run: ctest --output-on-failure --test-dir build-no-ssl -C Release + env: + VCPKG_ROOT: "C:/vcpkg" + VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite" diff --git a/httplib.h b/httplib.h index e50ad45613..f3835ee330 100644 --- a/httplib.h +++ b/httplib.h @@ -1582,6 +1582,9 @@ class ClientImpl { bool send_(Request &req, Response &res, Error &error); Result send_(Request &&req); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool is_ssl_peer_could_be_closed(SSL *ssl) const; +#endif socket_t create_client_socket(Error &error) const; bool read_response_line(Stream &strm, const Request &req, Response &res) const; @@ -7415,6 +7418,14 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { return ret; } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline bool ClientImpl::is_ssl_peer_could_be_closed(SSL *ssl) const { + char buf[1]; + return !SSL_peek(ssl, buf, 1) && + SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; +} +#endif + inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { { std::lock_guard guard(socket_mutex_); @@ -7426,6 +7437,15 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { auto is_alive = false; if (socket_.is_open()) { is_alive = detail::is_socket_alive(socket_.sock); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_alive && is_ssl()) { + if (is_ssl_peer_could_be_closed(socket_.ssl)) { + is_alive = false; + } + } +#endif + if (!is_alive) { // Attempt to avoid sigpipe by shutting down nongracefully if it seems // like the other side has already closed the connection Also, there @@ -7922,9 +7942,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (is_ssl()) { auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; if (!is_proxy_enabled) { - char buf[1]; - if (SSL_peek(socket_.ssl, buf, 1) == 0 && - SSL_get_error(socket_.ssl, 0) == SSL_ERROR_ZERO_RETURN) { + if (is_ssl_peer_could_be_closed(socket_.ssl)) { error = Error::SSLPeerCouldBeClosed_; return false; } From 8e378779c247e5a7fa8edea7bea86c2c19abf94e Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 16 Nov 2024 09:45:04 -0500 Subject: [PATCH 0319/1222] Update README --- README.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7148e5502a..eb8b075c4e 100644 --- a/README.md +++ b/README.md @@ -557,18 +557,18 @@ enum Error { ```c++ httplib::Headers headers = { - { "Accept-Encoding", "gzip, deflate" } + { "Hello", "World!" } }; auto res = cli.Get("/hi", headers); ``` or ```c++ -auto res = cli.Get("/hi", {{"Accept-Encoding", "gzip, deflate"}}); +auto res = cli.Get("/hi", {{"Hello", "World!"}}); ``` or ```c++ cli.set_default_headers({ - { "Accept-Encoding", "gzip, deflate" } + { "Hello", "World!" } }); auto res = cli.Get("/hi"); ``` @@ -823,6 +823,21 @@ The server can apply compression to the following MIME type contents: Brotli compression is available with `CPPHTTPLIB_BROTLI_SUPPORT`. Necessary libraries should be linked. Please see https://github.com/google/brotli for more detail. +### Default `Accept-Encoding` value + +The default `Acdcept-Encoding` value contains all possible compression types. So, the following two examples are same. + +```c++ +res = cli.Get("/resource/foo"); +res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate, br"}}); +``` + +If we don't want a response without compression, we have to set `Accept-Encoding` to an empty string. This behavior is similar to curl. + +```c++ +res = cli.Get("/resource/foo", {{"Accept-Encoding", ""}}); +``` + ### Compress request body on client ```c++ @@ -834,8 +849,9 @@ res = cli.Post("/resource/foo", "...", "text/plain"); ```c++ cli.set_decompress(false); -res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate, br"}}); +res = cli.Get("/resource/foo"); res->body; // Compressed data + ``` Use `poll` instead of `select` From 01dcf1d0adb05f0ce1a18e9494326ae9746953ce Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 16 Nov 2024 10:56:57 -0500 Subject: [PATCH 0320/1222] Fix #1969 (without unnecessary sleep_for) (#1982) --- httplib.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/httplib.h b/httplib.h index f3835ee330..bfad4fffeb 100644 --- a/httplib.h +++ b/httplib.h @@ -3308,8 +3308,6 @@ inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, } else { return true; // Ready for read } - - std::this_thread::sleep_for(microseconds{interval_usec}); } return false; From 413994912d19b76262078fa54c4beb9de700b633 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 16 Nov 2024 11:14:13 -0500 Subject: [PATCH 0321/1222] Update vcxproj files --- example/client.vcxproj | 10 +++++----- example/server.vcxproj | 16 ++++++++-------- test/test.vcxproj | 10 +++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/example/client.vcxproj b/example/client.vcxproj index 1abb773910..bc90190a56 100644 --- a/example/client.vcxproj +++ b/example/client.vcxproj @@ -22,34 +22,34 @@ {6DB1FC63-B153-4279-92B7-D8A11AF285D6} Win32Proj client - 10.0.15063.0 + 10.0 Application true Unicode - v141 + v143 Application true Unicode - v141 + v143 Application false true Unicode - v141 + v143 Application false true Unicode - v141 + v143 diff --git a/example/server.vcxproj b/example/server.vcxproj index 31ff203747..4dfb9e0a2a 100644 --- a/example/server.vcxproj +++ b/example/server.vcxproj @@ -18,38 +18,41 @@ x64 + + + {864CD288-050A-4C8B-9BEF-3048BD876C5B} Win32Proj sample - 10.0.15063.0 + 10.0 Application true Unicode - v141 + v143 Application true Unicode - v141 + v143 Application false true Unicode - v141 + v143 Application false true Unicode - v141 + v143 @@ -151,9 +154,6 @@ Ws2_32.lib;%(AdditionalDependencies) - - - diff --git a/test/test.vcxproj b/test/test.vcxproj index b169311b1b..148adf01aa 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -28,26 +28,26 @@ Application true - v142 + v143 Unicode Application true - v142 + v143 Unicode Application false - v142 + v143 true Unicode Application false - v142 + v143 true Unicode @@ -177,4 +177,4 @@ - + \ No newline at end of file From 1a7a7ed1c301f4ef08ced9c398d320a80f1468f1 Mon Sep 17 00:00:00 2001 From: sebastianas Date: Mon, 25 Nov 2024 21:46:41 +0100 Subject: [PATCH 0322/1222] test: Don't check for the exact size of compressed content. (#1984) The testsuite checks for the exact size of the compressed content. The exact size can change if the zlib library is using a different strategy. In thise case using zlib-ng results in a slightly larger content leading to a failure in the test. Check that the compressed content is less than 10MiB which is a tenth of the orignal content and proves that compression works. Signed-off-by: Sebastian Andrzej Siewior --- test/test.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index cc9ffeb526..d2aa07afa5 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4258,7 +4258,9 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) { ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(LARGE_DATA, res->body); - EXPECT_EQ(101942u, res.get_request_header_value_u64("Content-Length")); + // The compressed size should be less than a 10th of the original. May vary + // depending on the zlib library. + EXPECT_LT(res.get_request_header_value_u64("Content-Length"), 10 * 1024 * 1024); EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding")); } From da2f9e476ee3a70624c861ba397676314520b2ea Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 27 Nov 2024 12:18:23 -0500 Subject: [PATCH 0323/1222] Fix #1985 (#1989) --- httplib.h | 16 ++++++++++------ test/test.cc | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index bfad4fffeb..c671b8dbe0 100644 --- a/httplib.h +++ b/httplib.h @@ -612,6 +612,7 @@ using Ranges = std::vector; struct Request { std::string method; std::string path; + Params params; Headers headers; std::string body; @@ -623,7 +624,6 @@ struct Request { // for server std::string version; std::string target; - Params params; MultipartFormDataMap files; Ranges ranges; Match matches; @@ -7420,7 +7420,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { inline bool ClientImpl::is_ssl_peer_could_be_closed(SSL *ssl) const { char buf[1]; return !SSL_peek(ssl, buf, 1) && - SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; + SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; } #endif @@ -7438,9 +7438,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_alive && is_ssl()) { - if (is_ssl_peer_could_be_closed(socket_.ssl)) { - is_alive = false; - } + if (is_ssl_peer_could_be_closed(socket_.ssl)) { is_alive = false; } } #endif @@ -7799,7 +7797,13 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, { detail::BufferStream bstrm; - const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; + const auto &path_with_query = + req.params.empty() ? req.path + : append_query_params(req.path, req.params); + + const auto &path = + url_encode_ ? detail::encode_url(path_with_query) : path_with_query; + detail::write_request_line(bstrm, req.method, path); header_writer_(bstrm, req.headers); diff --git a/test/test.cc b/test/test.cc index d2aa07afa5..6ce6c8cb48 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6542,6 +6542,40 @@ TEST(SendAPI, SimpleInterface_Online) { EXPECT_EQ(StatusCode::MovedPermanently_301, res->status); } +TEST(SendAPI, WithParamsInRequest) { + Server svr; + + svr.Get("/", [&](const Request &req, Response & /*res*/) { + EXPECT_TRUE(req.has_param("test")); + EXPECT_EQ("test_value", req.get_param_value("test")); + }); + + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + + { + Request req; + req.method = "GET"; + req.path = "/"; + req.params.emplace("test", "test_value"); + auto res = cli.send(req); + ASSERT_TRUE(res); + } + { + auto res = cli.Get("/", {{"test", "test_value"}}, Headers{}); + ASSERT_TRUE(res); + } +} + TEST(ClientImplMethods, GetSocketTest) { httplib::Server svr; svr.Get("/", [&](const httplib::Request & /*req*/, httplib::Response &res) { From fe07660f40c3f29bdda84f0c637e6b7dc4734ee4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 27 Nov 2024 12:18:35 -0500 Subject: [PATCH 0324/1222] Fix #1986 (#1988) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index c671b8dbe0..b4f89d6183 100644 --- a/httplib.h +++ b/httplib.h @@ -2271,7 +2271,7 @@ inline std::wstring u8string_to_wstring(const char *s) { wlen = ::MultiByteToWideChar( CP_UTF8, 0, s, len, const_cast(reinterpret_cast(ws.data())), wlen); - if (wlen != ws.size()) { ws.clear(); } + if (wlen != static_cast(ws.size())) { ws.clear(); } } return ws; } From 5421e2710608055ebbd02a42115e892cbe898748 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 28 Nov 2024 20:37:39 -0500 Subject: [PATCH 0325/1222] Fix a compiler warning --- test/test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 6ce6c8cb48..d2a30783e7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4260,7 +4260,8 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) { EXPECT_EQ(LARGE_DATA, res->body); // The compressed size should be less than a 10th of the original. May vary // depending on the zlib library. - EXPECT_LT(res.get_request_header_value_u64("Content-Length"), 10 * 1024 * 1024); + EXPECT_LT(res.get_request_header_value_u64("Content-Length"), + static_cast(10 * 1024 * 1024)); EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding")); } From 4f5b003e760b1fd62c5c22eff09f4c5934bfdfc5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 28 Nov 2024 20:40:38 -0500 Subject: [PATCH 0326/1222] Fix #1992 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb8b075c4e..6165702cb2 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ cli.set_ca_cert_path("./ca-bundle.crt"); cli.enable_server_certificate_verification(false); // Disable host verification -cli.enable_server_host_verification(false); +cli.enable_server_hostname_verification(false); ``` > [!NOTE] From 457fc4306e70d85f5858314a0282d4a921e27d0c Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Nov 2024 20:46:48 -0500 Subject: [PATCH 0327/1222] Fix #1993 --- httplib.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index b4f89d6183..6ec2d768cf 100644 --- a/httplib.h +++ b/httplib.h @@ -7983,9 +7983,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, : static_cast( [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { - if (res.body.size() + n > res.body.max_size()) { - return false; - } + assert(res.body.size() + n <= res.body.max_size()); res.body.append(buf, n); return true; }); @@ -7999,9 +7997,12 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (res.has_header("Content-Length")) { if (!req.content_receiver) { - auto len = std::min(res.get_header_value_u64("Content-Length"), - res.body.max_size()); - if (len > 0) { res.body.reserve(len); } + auto len = res.get_header_value_u64("Content-Length"); + if (len > res.body.max_size()) { + error = Error::Read; + return false; + } + res.body.reserve(len); } } From 51dee793fec2fa70239f5cf190e165b54803880f Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Nov 2024 20:49:50 -0500 Subject: [PATCH 0328/1222] Release v0.18.2 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 6ec2d768cf..727d2d7546 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.1" +#define CPPHTTPLIB_VERSION "0.18.2" /* * Configuration From c817d656950134106f5d994f29d0cf3dab95dac5 Mon Sep 17 00:00:00 2001 From: Pavel P Date: Mon, 2 Dec 2024 21:09:52 +0500 Subject: [PATCH 0329/1222] Fix casting uint64_t to size_t for 32-bit builds (#1999) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 727d2d7546..e25fa2a860 100644 --- a/httplib.h +++ b/httplib.h @@ -8002,7 +8002,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, error = Error::Read; return false; } - res.body.reserve(len); + res.body.reserve(static_cast(len)); } } From 3e86bdb4d81c6457665beb6469f4d907019268b2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Dec 2024 00:11:29 -0500 Subject: [PATCH 0330/1222] Fix #1997 (#2001) --- httplib.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/httplib.h b/httplib.h index e25fa2a860..f7e54d2a44 100644 --- a/httplib.h +++ b/httplib.h @@ -7438,7 +7438,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_alive && is_ssl()) { + detail::set_nonblocking(socket_.sock, true); if (is_ssl_peer_could_be_closed(socket_.ssl)) { is_alive = false; } + detail::set_nonblocking(socket_.sock, false); } #endif From 11a40584e992bcf6a66bffce96bf75a78bf528c5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Dec 2024 00:38:20 -0500 Subject: [PATCH 0331/1222] Fix #1998 --- httplib.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index f7e54d2a44..9612f2c586 100644 --- a/httplib.h +++ b/httplib.h @@ -7964,7 +7964,9 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, // Body if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && req.method != "CONNECT") { - auto redirect = 300 < res.status && res.status < 400 && follow_location_; + auto redirect = 300 < res.status && res.status < 400 && + res.status != StatusCode::NotModified_304 && + follow_location_; if (req.response_handler && !redirect) { if (!req.response_handler(res)) { @@ -8008,12 +8010,14 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } } - int dummy_status; - if (!detail::read_content(strm, res, (std::numeric_limits::max)(), - dummy_status, std::move(progress), std::move(out), - decompress_)) { - if (error != Error::Canceled) { error = Error::Read; } - return false; + if (res.status != StatusCode::NotModified_304) { + int dummy_status; + if (!detail::read_content(strm, res, (std::numeric_limits::max)(), + dummy_status, std::move(progress), + std::move(out), decompress_)) { + if (error != Error::Canceled) { error = Error::Read; } + return false; + } } } From a7bc00e3307fecdb4d67545e93be7b88cfb1e186 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Dec 2024 06:33:00 -0500 Subject: [PATCH 0332/1222] Release v0.18.3 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 9612f2c586..704d0156d1 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.2" +#define CPPHTTPLIB_VERSION "0.18.3" /* * Configuration From 258992a160e306b6907c74abed448e487b60f74c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Dec 2024 19:26:08 -0500 Subject: [PATCH 0333/1222] Changed to use non-blocking socket in is_ssl_peer_could_be_closed --- httplib.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 704d0156d1..a582fadf01 100644 --- a/httplib.h +++ b/httplib.h @@ -7418,6 +7418,10 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline bool ClientImpl::is_ssl_peer_could_be_closed(SSL *ssl) const { + detail::set_nonblocking(socket_.sock, true); + auto se = detail::scope_exit( + [&]() { detail::set_nonblocking(socket_.sock, false); }); + char buf[1]; return !SSL_peek(ssl, buf, 1) && SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; @@ -7438,9 +7442,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_alive && is_ssl()) { - detail::set_nonblocking(socket_.sock, true); if (is_ssl_peer_could_be_closed(socket_.ssl)) { is_alive = false; } - detail::set_nonblocking(socket_.sock, false); } #endif From e6d71bd702d4ec224940c2c34e3d0b55c06eeb8b Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Dec 2024 18:15:22 -0500 Subject: [PATCH 0334/1222] Add a unit test for Issue #2004 --- test/test.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test.cc b/test/test.cc index d2a30783e7..da34ea9cb7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6132,6 +6132,18 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(SSLClientTest, Issue2004) { + Client client("https://google.com"); + client.set_follow_location(true); + + auto res = client.Get("/"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + + auto body = res->body; + EXPECT_EQ(body.substr(0, 15), ""); +} + #if 0 TEST(SSLClientTest, SetInterfaceWithINET6) { auto cli = std::make_shared("https://httpbin.org"); From b85768c1f34f8b7ff60dfe0e2938b35b50e2610b Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 16 Dec 2024 17:03:45 -0500 Subject: [PATCH 0335/1222] Fix #2005 --- httplib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/httplib.h b/httplib.h index a582fadf01..1a722d5203 100644 --- a/httplib.h +++ b/httplib.h @@ -8908,6 +8908,7 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, // best-efforts. if (shutdown_gracefully) { #ifdef _WIN32 + (void)(sock); SSL_shutdown(ssl); #else timeval tv; From 8794792baa1b2a14eceedd590f6d2f2b837dce39 Mon Sep 17 00:00:00 2001 From: Sergey Bobrenok Date: Tue, 24 Dec 2024 01:14:36 +0700 Subject: [PATCH 0336/1222] Treat out-of-range last_pos as the end of the content (#2009) RFC-9110 '14.1.2. Byte Ranges': A client can limit the number of bytes requested without knowing the size of the selected representation. If the last-pos value is absent, or if the value is greater than or equal to the current length of the representation data, the byte range is interpreted as the remainder of the representation (i.e., the server replaces the value of last-pos with a value that is one less than the current length of the selected representation). https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 --- httplib.h | 13 ++++++++++++- test/test.cc | 13 ++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 1a722d5203..60d6b4c014 100644 --- a/httplib.h +++ b/httplib.h @@ -5156,7 +5156,18 @@ inline bool range_error(Request &req, Response &res) { last_pos = contant_len - 1; } - if (last_pos == -1) { last_pos = contant_len - 1; } + // NOTE: RFC-9110 '14.1.2. Byte Ranges': + // A client can limit the number of bytes requested without knowing the + // size of the selected representation. If the last-pos value is absent, + // or if the value is greater than or equal to the current length of the + // representation data, the byte range is interpreted as the remainder of + // the representation (i.e., the server replaces the value of last-pos + // with a value that is one less than the current length of the selected + // representation). + // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 + if (last_pos == -1 || last_pos >= contant_len) { + last_pos = contant_len - 1; + } // Range must be within content length if (!(0 <= first_pos && first_pos <= last_pos && diff --git a/test/test.cc b/test/test.cc index da34ea9cb7..a82912adfe 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3795,11 +3795,14 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) { auto res = cli_.Get( "/with-range", {{"Range", - "bytes=0-" + std::to_string(std::numeric_limits::max())}}); - EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); - EXPECT_EQ("0", res->get_header_value("Content-Length")); - EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(0U, res->body.size()); + "bytes=0-" + std::to_string(std::numeric_limits::max())}, + {"Accept-Encoding", ""}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ("7", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 0-6/7", res->get_header_value("Content-Range")); + EXPECT_EQ(std::string("abcdefg"), res->body); } TEST_F(ServerTest, GetRangeWithZeroToInfinite) { From d647f484a48ae59b429e7e3a86e2b8b3fc493167 Mon Sep 17 00:00:00 2001 From: sinnren <111736305+sinnren@users.noreply.github.com> Date: Tue, 24 Dec 2024 22:38:59 +0800 Subject: [PATCH 0337/1222] fix:set_file_content with range request return 416. (#2010) Co-authored-by: fenlog --- httplib.h | 16 ++++++++-------- test/test.cc | 10 ++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index 60d6b4c014..2a85da6b96 100644 --- a/httplib.h +++ b/httplib.h @@ -7183,14 +7183,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr, : StatusCode::PartialContent_206; } - if (detail::range_error(req, res)) { - res.body.clear(); - res.content_length_ = 0; - res.content_provider_ = nullptr; - res.status = StatusCode::RangeNotSatisfiable_416; - return write_response(strm, close_connection, req, res); - } - // Serve file content by using a content provider if (!res.file_content_path_.empty()) { const auto &path = res.file_content_path_; @@ -7217,6 +7209,14 @@ Server::process_request(Stream &strm, const std::string &remote_addr, }); } + if (detail::range_error(req, res)) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); + } + return write_response_with_content(strm, close_connection, req, res); } else { if (res.status == -1) { res.status = StatusCode::NotFound_404; } diff --git a/test/test.cc b/test/test.cc index a82912adfe..d9f2036173 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3036,6 +3036,16 @@ TEST_F(ServerTest, GetFileContent) { EXPECT_EQ("test.html", res->body); } +TEST_F(ServerTest, GetFileContentWithRange) { + auto res = cli_.Get("/file_content", {{make_range_header({{1, 3}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ("text/html", res->get_header_value("Content-Type")); + EXPECT_EQ("bytes 1-3/9", res->get_header_value("Content-Range")); + EXPECT_EQ(3, std::stoi(res->get_header_value("Content-Length"))); + EXPECT_EQ("est", res->body); +} + TEST_F(ServerTest, GetFileContentWithContentType) { auto res = cli_.Get("/file_content_with_content_type"); ASSERT_TRUE(res); From 9b5f76f8337d54c8f6a0920df2232ac74eff613b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 27 Dec 2024 17:19:23 -0500 Subject: [PATCH 0338/1222] Fix #2012 --- example/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/Makefile b/example/Makefile index 7682e10029..ba68359313 100644 --- a/example/Makefile +++ b/example/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark issue +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) From b766025a8382ac3543eb9188579ed6299933b8f3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 16 Jan 2025 00:03:10 -0500 Subject: [PATCH 0339/1222] clangformat --- httplib.h | 2 +- test/test.cc | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 2a85da6b96..7743f9fd6f 100644 --- a/httplib.h +++ b/httplib.h @@ -5166,7 +5166,7 @@ inline bool range_error(Request &req, Response &res) { // representation). // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 if (last_pos == -1 || last_pos >= contant_len) { - last_pos = contant_len - 1; + last_pos = contant_len - 1; } // Range must be within content length diff --git a/test/test.cc b/test/test.cc index d9f2036173..6ec4b6fc63 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3804,8 +3804,7 @@ TEST_F(ServerTest, GetStreamedWithRangeError) { TEST_F(ServerTest, GetRangeWithMaxLongLength) { auto res = cli_.Get( "/with-range", - {{"Range", - "bytes=0-" + std::to_string(std::numeric_limits::max())}, + {{"Range", "bytes=0-" + std::to_string(std::numeric_limits::max())}, {"Accept-Encoding", ""}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::PartialContent_206, res->status); From 9c36aae4b73e2b6e493f4133e4173103c9266289 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 16 Jan 2025 00:04:17 -0500 Subject: [PATCH 0340/1222] Fix HTTP Response Splitting Vulnerability --- httplib.h | 62 +++++++++++++++++++++++++++++++++++++-- test/test.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 7743f9fd6f..27141f0bf4 100644 --- a/httplib.h +++ b/httplib.h @@ -2506,6 +2506,60 @@ class mmap { bool is_open_empty_file = false; }; +// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 +namespace fields { + +inline bool is_token_char(char c) { + return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || + c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || + c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; +} + +inline bool is_token(const std::string &s) { + if (s.empty()) { return false; } + for (auto c : s) { + if (!is_token_char(c)) { return false; } + } + return true; +} + +inline bool is_field_name(const std::string &s) { return is_token(s); } + +inline bool is_vchar(char c) { return c >= 33 && c <= 126; } + +inline bool is_obs_text(char c) { return 128 <= static_cast(c); } + +inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } + +inline bool is_field_content(const std::string &s) { + if (s.empty()) { return false; } + + if (s.size() == 1) { + return is_field_vchar(s[0]); + } else if (s.size() == 2) { + return is_field_vchar(s[0]) && is_field_vchar(s[1]); + } else { + size_t i = 0; + + if (!is_field_vchar(s[i])) { return false; } + i++; + + while (i < s.size() - 1) { + auto c = s[i++]; + if (c == ' ' || c == '\t' || is_field_vchar(c)) { + } else { + return false; + } + } + + return is_field_vchar(s[i]); + } +} + +inline bool is_field_value(const std::string &s) { return is_field_content(s); } + +}; // namespace fields + } // namespace detail // ---------------------------------------------------------------------------- @@ -5699,7 +5753,8 @@ inline size_t Request::get_header_value_count(const std::string &key) const { inline void Request::set_header(const std::string &key, const std::string &val) { - if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + if (detail::fields::is_field_name(key) && + detail::fields::is_field_value(val)) { headers.emplace(key, val); } } @@ -5765,13 +5820,14 @@ inline size_t Response::get_header_value_count(const std::string &key) const { inline void Response::set_header(const std::string &key, const std::string &val) { - if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + if (detail::fields::is_field_name(key) && + detail::fields::is_field_value(val)) { headers.emplace(key, val); } } inline void Response::set_redirect(const std::string &url, int stat) { - if (!detail::has_crlf(url)) { + if (detail::fields::is_field_value(url)) { set_header("Location", url); if (300 <= stat && stat < 400) { this->status = stat; diff --git a/test/test.cc b/test/test.cc index 6ec4b6fc63..ebc50f6f01 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7925,6 +7925,88 @@ TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { cli.Get("/test", {{"Test", "_\n\r_\n\r_"}}); } +TEST(InvalidHeaderCharsTest, is_field_name) { + EXPECT_TRUE(detail::fields::is_field_name("exampleToken")); + EXPECT_TRUE(detail::fields::is_field_name("token123")); + EXPECT_TRUE(detail::fields::is_field_name("!#$%&'*+-.^_`|~")); + + EXPECT_FALSE(detail::fields::is_field_name("example token")); + EXPECT_FALSE(detail::fields::is_field_name(" example_token")); + EXPECT_FALSE(detail::fields::is_field_name("example_token ")); + EXPECT_FALSE(detail::fields::is_field_name("token@123")); + EXPECT_FALSE(detail::fields::is_field_name("")); + EXPECT_FALSE(detail::fields::is_field_name("example\rtoken")); + EXPECT_FALSE(detail::fields::is_field_name("example\ntoken")); + EXPECT_FALSE(detail::fields::is_field_name(std::string("\0", 1))); + EXPECT_FALSE(detail::fields::is_field_name("example\ttoken")); +} + +TEST(InvalidHeaderCharsTest, is_field_value) { + EXPECT_TRUE(detail::fields::is_field_value("exampleToken")); + EXPECT_TRUE(detail::fields::is_field_value("token123")); + EXPECT_TRUE(detail::fields::is_field_value("!#$%&'*+-.^_`|~")); + + EXPECT_TRUE(detail::fields::is_field_value("example token")); + EXPECT_FALSE(detail::fields::is_field_value(" example_token")); + EXPECT_FALSE(detail::fields::is_field_value("example_token ")); + EXPECT_TRUE(detail::fields::is_field_value("token@123")); + EXPECT_FALSE(detail::fields::is_field_value("")); + EXPECT_FALSE(detail::fields::is_field_value("example\rtoken")); + EXPECT_FALSE(detail::fields::is_field_value("example\ntoken")); + EXPECT_FALSE(detail::fields::is_field_value(std::string("\0", 1))); + EXPECT_TRUE(detail::fields::is_field_value("example\ttoken")); + + EXPECT_TRUE(detail::fields::is_field_value("0")); +} + +TEST(InvalidHeaderCharsTest, OnServer) { + Server svr; + + svr.Get("/test_name", [&](const Request &req, Response &res) { + std::string header = "Not Set"; + if (req.has_param("header")) { header = req.get_param_value("header"); } + + res.set_header(header, "value"); + res.set_content("Page Content Page Content", "text/plain"); + }); + + svr.Get("/test_value", [&](const Request &req, Response &res) { + std::string header = "Not Set"; + if (req.has_param("header")) { header = req.get_param_value("header"); } + + res.set_header("X-Test", header); + res.set_content("Page Content Page Content", "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + { + auto res = cli.Get( + R"(/test_name?header=Value%00%0d%0aHEADER_KEY%3aHEADER_VALUE%0d%0a%0d%0aBODY_BODY_BODY)"); + + ASSERT_TRUE(res); + EXPECT_EQ("Page Content Page Content", res->body); + EXPECT_FALSE(res->has_header("HEADER_KEY")); + } + { + auto res = cli.Get( + R"(/test_value?header=Value%00%0d%0aHEADER_KEY%3aHEADER_VALUE%0d%0a%0d%0aBODY_BODY_BODY)"); + + ASSERT_TRUE(res); + EXPECT_EQ("Page Content Page Content", res->body); + EXPECT_FALSE(res->has_header("HEADER_KEY")); + } +} + #ifndef _WIN32 TEST(Expect100ContinueTest, ServerClosesConnection) { static constexpr char reject[] = "Unauthorized"; From 54f8a4d0f34acb3cc508e337d9881296b70201ad Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 16 Jan 2025 01:00:25 -0500 Subject: [PATCH 0341/1222] Release v0.18.4 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 27141f0bf4..47cceebbf4 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.3" +#define CPPHTTPLIB_VERSION "0.18.4" /* * Configuration From 343a0fc073309d0ff2d8a2ce9f2cc5afbfd63ac8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 16 Jan 2025 21:38:45 -0500 Subject: [PATCH 0342/1222] Fix #2011 --- httplib.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/httplib.h b/httplib.h index 47cceebbf4..096e944903 100644 --- a/httplib.h +++ b/httplib.h @@ -2038,12 +2038,6 @@ inline uint64_t Response::get_header_value_u64(const std::string &key, inline void default_socket_options(socket_t sock) { int opt = 1; -#ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); - setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - reinterpret_cast(&opt), sizeof(opt)); -#else #ifdef SO_REUSEPORT setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&opt), sizeof(opt)); @@ -2051,7 +2045,6 @@ inline void default_socket_options(socket_t sock) { setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&opt), sizeof(opt)); #endif -#endif } inline const char *status_message(int status) { From ba6845925d4465653a4ad1ad54d645bc4b8c8463 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 16 Jan 2025 22:36:07 -0500 Subject: [PATCH 0343/1222] Fix #2014 --- httplib.h | 30 ++++++++++++++++++++++++++---- test/test.cc | 48 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index 096e944903..7ef7369782 100644 --- a/httplib.h +++ b/httplib.h @@ -2012,18 +2012,34 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { callback(static_cast(sec), static_cast(usec)); } +inline bool is_numeric(const std::string &str) { + return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); +} + inline uint64_t get_header_value_u64(const Headers &headers, const std::string &key, uint64_t def, - size_t id) { + size_t id, bool &is_invalid_value) { + is_invalid_value = false; auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); if (it != rng.second) { - return std::strtoull(it->second.data(), nullptr, 10); + if (is_numeric(it->second)) { + return std::strtoull(it->second.data(), nullptr, 10); + } else { + is_invalid_value = true; + } } return def; } +inline uint64_t get_header_value_u64(const Headers &headers, + const std::string &key, uint64_t def, + size_t id) { + bool dummy = false; + return get_header_value_u64(headers, key, def, id, dummy); +} + } // namespace detail inline uint64_t Request::get_header_value_u64(const std::string &key, @@ -4433,8 +4449,14 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } else if (!has_header(x.headers, "Content-Length")) { ret = read_content_without_length(strm, out); } else { - auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0); - if (len > payload_max_length) { + auto is_invalid_value = false; + auto len = get_header_value_u64(x.headers, "Content-Length", + std::numeric_limits::max(), + 0, is_invalid_value); + + if (is_invalid_value) { + ret = false; + } else if (len > payload_max_length) { exceed_payload_max_length = true; skip_content_with_length(strm, len); ret = false; diff --git a/test/test.cc b/test/test.cc index ebc50f6f01..5372832754 100644 --- a/test/test.cc +++ b/test/test.cc @@ -511,6 +511,15 @@ TEST(GetHeaderValueTest, RegularValueInt) { EXPECT_EQ(100ull, val); } +TEST(GetHeaderValueTest, RegularInvalidValueInt) { + Headers headers = {{"Content-Length", "x"}}; + auto is_invalid_value = false; + auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 0, + is_invalid_value); + EXPECT_EQ(0ull, val); + EXPECT_TRUE(is_invalid_value); +} + TEST(GetHeaderValueTest, Range) { { Headers headers = {make_range_header({{1, -1}})}; @@ -7496,9 +7505,9 @@ TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) { "text2" "\r\n------------"; - std::string resonse; - ASSERT_TRUE(send_request(1, req, &resonse)); - ASSERT_EQ("200", resonse.substr(9, 3)); + std::string response; + ASSERT_TRUE(send_request(1, req, &response)); + ASSERT_EQ("200", response.substr(9, 3)); } TEST(MultipartFormDataTest, ContentLength) { @@ -7543,11 +7552,10 @@ TEST(MultipartFormDataTest, ContentLength) { "text2" "\r\n------------\r\n"; - std::string resonse; - ASSERT_TRUE(send_request(1, req, &resonse)); - ASSERT_EQ("200", resonse.substr(9, 3)); + std::string response; + ASSERT_TRUE(send_request(1, req, &response)); + ASSERT_EQ("200", response.substr(9, 3)); } - #endif TEST(TaskQueueTest, IncreaseAtomicInteger) { @@ -8007,6 +8015,32 @@ TEST(InvalidHeaderCharsTest, OnServer) { } } +TEST(InvalidHeaderValueTest, InvalidContentLength) { + auto handled = false; + + Server svr; + svr.Post("/test", [&](const Request &, Response &) { handled = true; }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + ASSERT_FALSE(handled); + }); + + svr.wait_until_ready(); + + auto req = "POST /test HTTP/1.1\r\n" + "Content-Length: x\r\n" + "\r\n"; + + std::string response; + ASSERT_TRUE(send_request(1, req, &response)); + ASSERT_EQ("HTTP/1.1 400 Bad Request", + response.substr(0, response.find("\r\n"))); +} + #ifndef _WIN32 TEST(Expect100ContinueTest, ServerClosesConnection) { static constexpr char reject[] = "Unauthorized"; From 8311e1105fde2e1318c5e5acb567179ea70b3d1f Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 16 Jan 2025 23:26:04 -0500 Subject: [PATCH 0344/1222] Fix Windows build problem --- httplib.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/httplib.h b/httplib.h index 7ef7369782..c5c8f3f278 100644 --- a/httplib.h +++ b/httplib.h @@ -2054,6 +2054,10 @@ inline uint64_t Response::get_header_value_u64(const std::string &key, inline void default_socket_options(socket_t sock) { int opt = 1; +#ifdef _WIN32 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&opt), sizeof(opt)); +#else #ifdef SO_REUSEPORT setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&opt), sizeof(opt)); @@ -2061,6 +2065,7 @@ inline void default_socket_options(socket_t sock) { setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&opt), sizeof(opt)); #endif +#endif } inline const char *status_message(int status) { From 986a20fb7d368115324c6c456961dbdafdf5c0bf Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 17 Jan 2025 17:37:07 -0500 Subject: [PATCH 0345/1222] Resolve #2017 (#2022) * Resolve #2017 * Fix warning * Update README --- README.md | 15 +++++++++++++++ httplib.h | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6165702cb2..2c569c77ca 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,21 @@ int main(void) res.set_content(req.body, "text/plain"); }); + // If the handler takes time to finish, you can also poll the connection state + svr.Get("/task", [&](const Request& req, Response& res) { + const char * result = nullptr; + process.run(); // for example, starting an external process + while (result == nullptr) { + sleep(1); + if (req.is_connection_closed()) { + process.kill(); // kill the process + return; + } + result = process.stdout(); // != nullptr if the process finishes + } + res.set_content(result, "text/plain"); + }); + svr.Get("/stop", [&](const Request& req, Response& res) { svr.stop(); }); diff --git a/httplib.h b/httplib.h index c5c8f3f278..2ec2c9e958 100644 --- a/httplib.h +++ b/httplib.h @@ -628,6 +628,7 @@ struct Request { Ranges ranges; Match matches; std::unordered_map path_params; + std::function is_connection_closed = []() { return true; }; // for client ResponseHandler response_handler; @@ -2572,7 +2573,7 @@ inline bool is_field_content(const std::string &s) { inline bool is_field_value(const std::string &s) { return is_field_content(s); } -}; // namespace fields +} // namespace fields } // namespace detail @@ -7217,6 +7218,11 @@ Server::process_request(Stream &strm, const std::string &remote_addr, } } + // Setup `is_connection_closed` method + req.is_connection_closed = [&]() { + return !detail::is_socket_alive(strm.socket()); + }; + // Routing auto routed = false; #ifdef CPPHTTPLIB_NO_EXCEPTIONS From 37798003220c997c4a6dc30d35e76d478b53a6e2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 17 Jan 2025 17:38:03 -0500 Subject: [PATCH 0346/1222] Release v0.18.5 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 2ec2c9e958..c2f12dd2ac 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.4" +#define CPPHTTPLIB_VERSION "0.18.5" /* * Configuration From ef5e4044f13b21f8048290b7f3740ff462c72f82 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 19 Jan 2025 23:46:12 -0500 Subject: [PATCH 0347/1222] Update README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c569c77ca..9b4ab832b9 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ svr.listen("0.0.0.0", 8080); #include "path/to/httplib.h" // HTTP -httplib::Client cli("http://cpp-httplib-server.yhirose.repl.co"); +httplib::Client cli("http://yhirose.github.io/hi"); // HTTPS -httplib::Client cli("https://cpp-httplib-server.yhirose.repl.co"); +httplib::Client cli("https://yhirose.github.io/hi"); auto res = cli.Get("/hi"); res->status; From 3047183fd97d4111c21bb5ab5c7057caa53a081b Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 20 Jan 2025 00:02:02 -0500 Subject: [PATCH 0348/1222] Update README --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 9b4ab832b9..6bd584e85b 100644 --- a/README.md +++ b/README.md @@ -931,9 +931,6 @@ From Docker Hub ```bash > docker run --rm -it -p 8080:80 -v ./docker/html:/html yhirose4dockerhub/cpp-httplib-server -... - -> docker run --init --rm -it -p 8080:80 -v ./docker/html:/html cpp-httplib-server Serving HTTP on 0.0.0.0 port 80 ... 192.168.65.1 - - [31/Aug/2024:21:33:56 +0000] "GET / HTTP/1.1" 200 599 "-" "curl/8.7.1" 192.168.65.1 - - [31/Aug/2024:21:34:26 +0000] "GET / HTTP/1.1" 200 599 "-" "Mozilla/5.0 ..." From 929dfbd34850b56921156b9b93529435dd1ffc88 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 20 Jan 2025 00:32:10 -0500 Subject: [PATCH 0349/1222] Update copyright year --- README.md | 2 +- benchmark/cpp-httplib-base/httplib.h | 2 +- docker/main.cc | 2 +- example/server_and_client.cc | 2 +- httplib.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6bd584e85b..eea79d992f 100644 --- a/README.md +++ b/README.md @@ -968,7 +968,7 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32 License ------- -MIT license (© 2024 Yuji Hirose) +MIT license (© 2025 Yuji Hirose) Special Thanks To ----------------- diff --git a/benchmark/cpp-httplib-base/httplib.h b/benchmark/cpp-httplib-base/httplib.h index 121ce34955..3e28fcff91 100644 --- a/benchmark/cpp-httplib-base/httplib.h +++ b/benchmark/cpp-httplib-base/httplib.h @@ -1,7 +1,7 @@ // // httplib.h // -// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// Copyright (c) 2025 Yuji Hirose. All rights reserved. // MIT License // diff --git a/docker/main.cc b/docker/main.cc index 512b2664a0..913275da6e 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -1,7 +1,7 @@ // // main.cc // -// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// Copyright (c) 2025 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/server_and_client.cc b/example/server_and_client.cc index 66cb8b6ccc..34bf852714 100644 --- a/example/server_and_client.cc +++ b/example/server_and_client.cc @@ -1,7 +1,7 @@ // // server_and_client.cc // -// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// Copyright (c) 2025 Yuji Hirose. All rights reserved. // MIT License // diff --git a/httplib.h b/httplib.h index c2f12dd2ac..f8c7439a10 100644 --- a/httplib.h +++ b/httplib.h @@ -1,7 +1,7 @@ // // httplib.h // -// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// Copyright (c) 2025 Yuji Hirose. All rights reserved. // MIT License // From d69f144a99910260e5f2ba2be020adb385b5af95 Mon Sep 17 00:00:00 2001 From: Baiyies <87919016+baiyies@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:50:10 +0800 Subject: [PATCH 0350/1222] Update httplib.h (#2030) fix 'max' --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index f8c7439a10..7813cd4716 100644 --- a/httplib.h +++ b/httplib.h @@ -4457,7 +4457,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } else { auto is_invalid_value = false; auto len = get_header_value_u64(x.headers, "Content-Length", - std::numeric_limits::max(), + (std::numeric_limits::max)(), 0, is_invalid_value); if (is_invalid_value) { From 9104054ca56a712377ceb238d63fcab49e389e68 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Jan 2025 13:37:16 -0500 Subject: [PATCH 0351/1222] Fix README example --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eea79d992f..dc2d9c5341 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ svr.listen("0.0.0.0", 8080); #include "path/to/httplib.h" // HTTP -httplib::Client cli("http://yhirose.github.io/hi"); +httplib::Client cli("http://yhirose.github.io"); // HTTPS -httplib::Client cli("https://yhirose.github.io/hi"); +httplib::Client cli("https://yhirose.github.io"); auto res = cli.Get("/hi"); res->status; From 60a1f00618a4fa135969f8b6da36f6849e2fd086 Mon Sep 17 00:00:00 2001 From: alex-cornford Date: Wed, 29 Jan 2025 12:44:22 +1300 Subject: [PATCH 0352/1222] Support building httplib.h on OpenVMS x86 systems (#2031) Modify for OpenVMS x86 C++. Make tests on OpenVMS currently not supported due to no cmake support. Changes tested on OpenVMS clang C++ and Fedora & GCC --- httplib.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 7813cd4716..3a72d55716 100644 --- a/httplib.h +++ b/httplib.h @@ -218,7 +218,9 @@ using socket_t = SOCKET; #include #include #include -#include +#ifndef __VMS + #include +#endif #include #include #include From 282f2feb77088ab905e2da5ae2e22a6d4d99a992 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 1 Feb 2025 22:11:15 -0500 Subject: [PATCH 0353/1222] Add a unit test --- test/test.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/test.cc b/test/test.cc index 5372832754..bfbf992a61 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1863,6 +1863,32 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { } } +TEST(PathUrlEncodeTest, IncludePercentEncodingLF) { + Server svr; + + svr.Get("/", [](const Request &req, Response &res) { + EXPECT_EQ("\x0A", req.get_param_value("something")); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + { + Client cli(HOST, PORT); + cli.set_url_encode(false); + + auto res = cli.Get("/?something=%0A"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + } +} + TEST(BindServerTest, DISABLED_BindDualStack) { Server svr; From 9bbb4741b4f7c8fc5083c8a56d8d301a8abc25a3 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 3 Feb 2025 04:32:33 +0100 Subject: [PATCH 0354/1222] Run clang-format (#2037) --- httplib.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 3a72d55716..cc9cbf3a35 100644 --- a/httplib.h +++ b/httplib.h @@ -219,7 +219,7 @@ using socket_t = SOCKET; #include #include #ifndef __VMS - #include +#include #endif #include #include @@ -4458,9 +4458,9 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, ret = read_content_without_length(strm, out); } else { auto is_invalid_value = false; - auto len = get_header_value_u64(x.headers, "Content-Length", - (std::numeric_limits::max)(), - 0, is_invalid_value); + auto len = get_header_value_u64( + x.headers, "Content-Length", + (std::numeric_limits::max)(), 0, is_invalid_value); if (is_invalid_value) { ret = false; From 4941d5b56b7e8340a94ac9b89ea1fa92f5fc8cc7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 5 Feb 2025 12:46:33 -0500 Subject: [PATCH 0355/1222] Fix #2033 (#2039) --- httplib.h | 35 +++++++++++++++++------------------ test/test.cc | 18 ++++++++++++++---- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/httplib.h b/httplib.h index cc9cbf3a35..a16dd260e6 100644 --- a/httplib.h +++ b/httplib.h @@ -2549,7 +2549,7 @@ inline bool is_obs_text(char c) { return 128 <= static_cast(c); } inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } inline bool is_field_content(const std::string &s) { - if (s.empty()) { return false; } + if (s.empty()) { return true; } if (s.size() == 1) { return is_field_vchar(s[0]); @@ -4214,22 +4214,21 @@ inline bool parse_header(const char *beg, const char *end, T fn) { if (!key_len) { return false; } auto key = std::string(beg, key_end); - auto val = case_ignore::equal(key, "Location") - ? std::string(p, end) - : decode_url(std::string(p, end), false); - - // NOTE: From RFC 9110: - // Field values containing CR, LF, or NUL characters are - // invalid and dangerous, due to the varying ways that - // implementations might parse and interpret those - // characters; a recipient of CR, LF, or NUL within a field - // value MUST either reject the message or replace each of - // those characters with SP before further processing or - // forwarding of that message. - static const std::string CR_LF_NUL("\r\n\0", 3); - if (val.find_first_of(CR_LF_NUL) != std::string::npos) { return false; } - - fn(key, val); + // auto val = (case_ignore::equal(key, "Location") || + // case_ignore::equal(key, "Referer")) + // ? std::string(p, end) + // : decode_url(std::string(p, end), false); + auto val = std::string(p, end); + + if (!detail::fields::is_field_value(val)) { return false; } + + if (case_ignore::equal(key, "Location") || + case_ignore::equal(key, "Referer")) { + fn(key, val); + } else { + fn(key, decode_url(val, false)); + } + return true; } @@ -4265,7 +4264,7 @@ inline bool read_headers(Stream &strm, Headers &headers) { auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; if (!parse_header(line_reader.ptr(), end, - [&](const std::string &key, std::string &val) { + [&](const std::string &key, const std::string &val) { headers.emplace(key, val); })) { return false; diff --git a/test/test.cc b/test/test.cc index bfbf992a61..76c322377e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1866,7 +1866,7 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { TEST(PathUrlEncodeTest, IncludePercentEncodingLF) { Server svr; - svr.Get("/", [](const Request &req, Response &res) { + svr.Get("/", [](const Request &req, Response &) { EXPECT_EQ("\x0A", req.get_param_value("something")); }); @@ -4936,8 +4936,10 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) { "Connection: close\r\n" "\r\n"; - ASSERT_TRUE(send_request(5, req)); - EXPECT_EQ(header_value, "\v bar \x1B"); + std::string res; + ASSERT_TRUE(send_request(5, req, &res)); + EXPECT_EQ(header_value, ""); + EXPECT_EQ("HTTP/1.1 400 Bad Request", res.substr(0, 24)); } // Sends a raw request and verifies that there isn't a crash or exception. @@ -5095,6 +5097,14 @@ TEST(ServerRequestParsingTest, InvalidFieldValueContains_CR_LF_NUL) { EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); } +TEST(ServerRequestParsingTest, InvalidFieldValueContains_LF) { + std::string out; + std::string request( + "GET /header_field_value_check HTTP/1.1\r\nTest: [\n\n\n]\r\n\r\n", 55); + test_raw_request(request, &out); + EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); +} + TEST(ServerRequestParsingTest, EmptyFieldValue) { std::string out; @@ -7984,7 +7994,7 @@ TEST(InvalidHeaderCharsTest, is_field_value) { EXPECT_FALSE(detail::fields::is_field_value(" example_token")); EXPECT_FALSE(detail::fields::is_field_value("example_token ")); EXPECT_TRUE(detail::fields::is_field_value("token@123")); - EXPECT_FALSE(detail::fields::is_field_value("")); + EXPECT_TRUE(detail::fields::is_field_value("")); EXPECT_FALSE(detail::fields::is_field_value("example\rtoken")); EXPECT_FALSE(detail::fields::is_field_value("example\ntoken")); EXPECT_FALSE(detail::fields::is_field_value(std::string("\0", 1))); From eb30f15363fd418103d8cf1dd64e206f51e5cbc8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 5 Feb 2025 19:14:20 -0500 Subject: [PATCH 0356/1222] Release v0.18.6 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index a16dd260e6..5c649c4e89 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.5" +#define CPPHTTPLIB_VERSION "0.18.6" /* * Configuration From 708f860e3ab611144eeb55cd76d89b27be134a47 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 6 Feb 2025 05:56:31 -0500 Subject: [PATCH 0357/1222] Fix #2042 --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 76c322377e..d06c9d36fb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6189,7 +6189,7 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) { ASSERT_EQ(StatusCode::OK_200, res->status); } -TEST(SSLClientTest, Issue2004) { +TEST(SSLClientTest, Issue2004_Online) { Client client("https://google.com"); client.set_follow_location(true); From eb10c22db10995c4a30f92e01968df1319f8783e Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Feb 2025 10:17:09 -0500 Subject: [PATCH 0358/1222] Add unit test for #609 --- test/test.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test.cc b/test/test.cc index d06c9d36fb..b69be5c35c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2977,6 +2977,11 @@ class ServerTest : public ::testing::Test { res.status = 401; res.set_header("WWW-Authenticate", "Basic realm=123456"); }) + .Delete("/issue609", + [](const httplib::Request &, httplib::Response &res, + const httplib::ContentReader &) { + res.set_content("ok", "text/plain"); + }) #if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT) .Get("/compress", [&](const Request & /*req*/, Response &res) { @@ -4048,6 +4053,13 @@ TEST_F(ServerTest, Issue1772) { EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } +TEST_F(ServerTest, Issue609) { + auto res = cli_.Delete("/issue609"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(std::string("ok"), res->body); +} + TEST_F(ServerTest, GetStreamedChunked) { auto res = cli_.Get("/streamed-chunked"); ASSERT_TRUE(res); From 7adbccbaf71c28195949a67350f721da44a32896 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 8 Feb 2025 21:51:52 +0100 Subject: [PATCH 0359/1222] Refine when content is expected (#2044) Consider Content-Length and Transfer-Encoding headers when determining whether to expect content. Don't handle the HTTP/2 connection preface pseudo-method PRI. Fixes #2028. --- httplib.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 5c649c4e89..257d9ebc14 100644 --- a/httplib.h +++ b/httplib.h @@ -5381,10 +5381,14 @@ write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, inline bool expect_content(const Request &req) { if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || - req.method == "PRI" || req.method == "DELETE") { + req.method == "DELETE") { return true; } - // TODO: check if Content-Length is set + if (req.has_header("Content-Length") && + req.get_header_value_u64("Content-Length") > 0) { + return true; + } + if (is_chunked_transfer_encoding(req.headers)) { return true; } return false; } From 5814e121dfb5049f72a5c3956c3c8961b40da78b Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Feb 2025 15:53:35 -0500 Subject: [PATCH 0360/1222] Release v0.18.7 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 257d9ebc14..eb592d2e7a 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.6" +#define CPPHTTPLIB_VERSION "0.18.7" /* * Configuration From 8aad481c69938eefc1e9f7100aefa79967697851 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 8 Feb 2025 23:37:41 -0500 Subject: [PATCH 0361/1222] Fix test.yaml problem --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3d84808c1a..f7dc0c866d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -55,7 +55,7 @@ jobs: run: ctest --output-on-failure --test-dir build -C Release - name: Configure CMake without SSL - run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON + run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=OFF -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON - name: Build without SSL run: cmake --build build-no-ssl --config Release - name: Run tests without SSL From 8a7c536ad5b2d304bd467209a13627a8db510bc9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 10 Feb 2025 06:51:07 -0500 Subject: [PATCH 0362/1222] Fix #2034 (#2048) * Fix #2034 * Fix build error * Adjust threshold * Add temporary debug prints * Adjust threshhold * Another threshold adjustment for macOS on GitHub Actions CI... * Performance improvement by avoiding unnecessary chrono access * More performance improvement to avoid unnecessary chrono access --- httplib.h | 408 ++++++++++++++++++++++------------ test/fuzzing/server_fuzzer.cc | 2 + test/test.cc | 261 +++++++++++++++++++++- 3 files changed, 532 insertions(+), 139 deletions(-) diff --git a/httplib.h b/httplib.h index eb592d2e7a..ed49a54d57 100644 --- a/httplib.h +++ b/httplib.h @@ -66,6 +66,10 @@ #define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 #endif +#ifndef CPPHTTPLIB_CLIENT_GLOBAL_TIMEOUT_MSECOND +#define CPPHTTPLIB_CLIENT_GLOBAL_TIMEOUT_MSECOND 0 +#endif + #ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND #define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 #endif @@ -664,6 +668,8 @@ struct Request { ContentProvider content_provider_; bool is_chunked_content_provider_ = false; size_t authorization_count_ = 0; + std::chrono::time_point start_time_ = + std::chrono::steady_clock::time_point::min(); }; struct Response { @@ -737,6 +743,8 @@ class Stream { virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; virtual socket_t socket() const = 0; + virtual time_t duration() const = 0; + ssize_t write(const char *ptr); ssize_t write(const std::string &s); }; @@ -1423,6 +1431,10 @@ class ClientImpl { template void set_write_timeout(const std::chrono::duration &duration); + void set_global_timeout(time_t msec); + template + void set_global_timeout(const std::chrono::duration &duration); + void set_basic_auth(const std::string &username, const std::string &password); void set_bearer_token_auth(const std::string &token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1531,6 +1543,7 @@ class ClientImpl { time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; + time_t global_timeout_msec_ = CPPHTTPLIB_CLIENT_GLOBAL_TIMEOUT_MSECOND; std::string basic_auth_username_; std::string basic_auth_password_; @@ -1585,9 +1598,6 @@ class ClientImpl { bool send_(Request &req, Response &res, Error &error); Result send_(Request &&req); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool is_ssl_peer_could_be_closed(SSL *ssl) const; -#endif socket_t create_client_socket(Error &error) const; bool read_response_line(Stream &strm, const Request &req, Response &res) const; @@ -1613,8 +1623,10 @@ class ClientImpl { std::string adjust_host_string(const std::string &host) const; - virtual bool process_socket(const Socket &socket, - std::function callback); + virtual bool + process_socket(const Socket &socket, + std::chrono::time_point start_time, + std::function callback); virtual bool is_ssl() const; }; @@ -1856,6 +1868,10 @@ class Client { template void set_write_timeout(const std::chrono::duration &duration); + void set_global_timeout(time_t msec); + template + void set_global_timeout(const std::chrono::duration &duration); + void set_basic_auth(const std::string &username, const std::string &password); void set_bearer_token_auth(const std::string &token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1973,12 +1989,16 @@ class SSLClient final : public ClientImpl { void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); - bool process_socket(const Socket &socket, - std::function callback) override; + bool + process_socket(const Socket &socket, + std::chrono::time_point start_time, + std::function callback) override; bool is_ssl() const override; - bool connect_with_proxy(Socket &sock, Response &res, bool &success, - Error &error); + bool connect_with_proxy( + Socket &sock, + std::chrono::time_point start_time, + Response &res, bool &success, Error &error); bool initialize_ssl(Socket &socket, Error &error); bool load_certs(); @@ -2240,6 +2260,14 @@ inline void ClientImpl::set_write_timeout( duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); } +template +inline void ClientImpl::set_global_timeout( + const std::chrono::duration &duration) { + auto msec = + std::chrono::duration_cast(duration).count(); + set_global_timeout(msec); +} + template inline void Client::set_connection_timeout( const std::chrono::duration &duration) { @@ -2258,6 +2286,12 @@ Client::set_write_timeout(const std::chrono::duration &duration) { cli_->set_write_timeout(duration); } +template +inline void +Client::set_global_timeout(const std::chrono::duration &duration) { + cli_->set_global_timeout(duration); +} + /* * Forward declarations and types that will be part of the .h file if split into * .h + .cc. @@ -2332,10 +2366,12 @@ void split(const char *b, const char *e, char d, void split(const char *b, const char *e, char d, size_t m, std::function fn); -bool process_client_socket(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, - std::function callback); +bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t global_timeout_msec, + std::chrono::time_point start_time, + std::function callback); socket_t create_client_socket(const std::string &host, const std::string &ip, int port, int address_family, bool tcp_nodelay, @@ -2383,6 +2419,7 @@ class BufferStream final : public Stream { void get_remote_ip_and_port(std::string &ip, int &port) const override; void get_local_ip_and_port(std::string &ip, int &port) const override; socket_t socket() const override; + time_t duration() const override; const std::string &get_buffer() const; @@ -3300,7 +3337,10 @@ inline bool is_socket_alive(socket_t sock) { class SocketStream final : public Stream { public: SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec); + time_t write_timeout_sec, time_t write_timeout_usec, + time_t global_timeout_msec = 0, + std::chrono::time_point start_time = + std::chrono::steady_clock::time_point::min()); ~SocketStream() override; bool is_readable() const override; @@ -3310,6 +3350,7 @@ class SocketStream final : public Stream { void get_remote_ip_and_port(std::string &ip, int &port) const override; void get_local_ip_and_port(std::string &ip, int &port) const override; socket_t socket() const override; + time_t duration() const override; private: socket_t sock_; @@ -3317,6 +3358,8 @@ class SocketStream final : public Stream { time_t read_timeout_usec_; time_t write_timeout_sec_; time_t write_timeout_usec_; + time_t global_timeout_msec_; + const std::chrono::time_point start_time; std::vector read_buff_; size_t read_buff_off_ = 0; @@ -3328,9 +3371,12 @@ class SocketStream final : public Stream { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT class SSLSocketStream final : public Stream { public: - SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec); + SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t global_timeout_msec = 0, + std::chrono::time_point start_time = + std::chrono::steady_clock::time_point::min()); ~SSLSocketStream() override; bool is_readable() const override; @@ -3340,6 +3386,7 @@ class SSLSocketStream final : public Stream { void get_remote_ip_and_port(std::string &ip, int &port) const override; void get_local_ip_and_port(std::string &ip, int &port) const override; socket_t socket() const override; + time_t duration() const override; private: socket_t sock_; @@ -3348,6 +3395,8 @@ class SSLSocketStream final : public Stream { time_t read_timeout_usec_; time_t write_timeout_sec_; time_t write_timeout_usec_; + time_t global_timeout_msec_; + const std::chrono::time_point start_time; }; #endif @@ -3418,13 +3467,15 @@ process_server_socket(const std::atomic &svr_sock, socket_t sock, }); } -inline bool process_client_socket(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec, - std::function callback) { +inline bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t global_timeout_msec, + std::chrono::time_point start_time, + std::function callback) { SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); + write_timeout_sec, write_timeout_usec, global_timeout_msec, + start_time); return callback(strm); } @@ -4313,7 +4364,7 @@ inline bool read_content_without_length(Stream &strm, uint64_t r = 0; for (;;) { auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); - if (n <= 0) { return true; } + if (n <= 0) { return false; } if (!out(buf, static_cast(n), r, 0)) { return false; } r += static_cast(n); @@ -5433,9 +5484,76 @@ inline std::string SHA_256(const std::string &s) { inline std::string SHA_512(const std::string &s) { return message_digest(s, EVP_sha512()); } -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::pair make_digest_authentication_header( + const Request &req, const std::map &auth, + size_t cnonce_count, const std::string &cnonce, const std::string &username, + const std::string &password, bool is_proxy = false) { + std::string nc; + { + std::stringstream ss; + ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; + nc = ss.str(); + } + + std::string qop; + if (auth.find("qop") != auth.end()) { + qop = auth.at("qop"); + if (qop.find("auth-int") != std::string::npos) { + qop = "auth-int"; + } else if (qop.find("auth") != std::string::npos) { + qop = "auth"; + } else { + qop.clear(); + } + } + + std::string algo = "MD5"; + if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } + + std::string response; + { + auto H = algo == "SHA-256" ? detail::SHA_256 + : algo == "SHA-512" ? detail::SHA_512 + : detail::MD5; + + auto A1 = username + ":" + auth.at("realm") + ":" + password; + + auto A2 = req.method + ":" + req.path; + if (qop == "auth-int") { A2 += ":" + H(req.body); } + + if (qop.empty()) { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); + } else { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + + ":" + qop + ":" + H(A2)); + } + } + + auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; + + auto field = "Digest username=\"" + username + "\", realm=\"" + + auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + + "\", uri=\"" + req.path + "\", algorithm=" + algo + + (qop.empty() ? ", response=\"" + : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + + cnonce + "\", response=\"") + + response + "\"" + + (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); + + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} + +inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { + detail::set_nonblocking(sock, true); + auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); + + char buf[1]; + return !SSL_peek(ssl, buf, 1) && + SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; +} + #ifdef _WIN32 // NOTE: This code came up with the following stackoverflow post: // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store @@ -5574,68 +5692,6 @@ class WSInit { static WSInit wsinit_; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline std::pair make_digest_authentication_header( - const Request &req, const std::map &auth, - size_t cnonce_count, const std::string &cnonce, const std::string &username, - const std::string &password, bool is_proxy = false) { - std::string nc; - { - std::stringstream ss; - ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; - nc = ss.str(); - } - - std::string qop; - if (auth.find("qop") != auth.end()) { - qop = auth.at("qop"); - if (qop.find("auth-int") != std::string::npos) { - qop = "auth-int"; - } else if (qop.find("auth") != std::string::npos) { - qop = "auth"; - } else { - qop.clear(); - } - } - - std::string algo = "MD5"; - if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } - - std::string response; - { - auto H = algo == "SHA-256" ? detail::SHA_256 - : algo == "SHA-512" ? detail::SHA_512 - : detail::MD5; - - auto A1 = username + ":" + auth.at("realm") + ":" + password; - - auto A2 = req.method + ":" + req.path; - if (qop == "auth-int") { A2 += ":" + H(req.body); } - - if (qop.empty()) { - response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); - } else { - response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + - ":" + qop + ":" + H(A2)); - } - } - - auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; - - auto field = "Digest username=\"" + username + "\", realm=\"" + - auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + - "\", uri=\"" + req.path + "\", algorithm=" + algo + - (qop.empty() ? ", response=\"" - : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + - cnonce + "\", response=\"") + - response + "\"" + - (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); - - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, field); -} -#endif - inline bool parse_www_authenticate(const Response &res, std::map &auth, bool is_proxy) { @@ -5954,20 +6010,45 @@ inline ssize_t Stream::write(const std::string &s) { namespace detail { +inline void calc_actual_timeout(time_t global_timeout_msec, + time_t duration_msec, time_t timeout_sec, + time_t timeout_usec, time_t &actual_timeout_sec, + time_t &actual_timeout_usec) { + auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); + + auto actual_timeout_msec = + std::min(global_timeout_msec - duration_msec, timeout_msec); + + actual_timeout_sec = actual_timeout_msec / 1000; + actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; +} + // Socket stream implementation -inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec) +inline SocketStream::SocketStream( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t global_timeout_msec, + std::chrono::time_point start_time) : sock_(sock), read_timeout_sec_(read_timeout_sec), read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec), read_buff_(read_buff_size_, 0) {} + write_timeout_usec_(write_timeout_usec), + global_timeout_msec_(global_timeout_msec), start_time(start_time), + read_buff_(read_buff_size_, 0) {} inline SocketStream::~SocketStream() = default; inline bool SocketStream::is_readable() const { - return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + if (global_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } + + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(global_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); + + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; } inline bool SocketStream::is_writable() const { @@ -6044,6 +6125,12 @@ inline void SocketStream::get_local_ip_and_port(std::string &ip, inline socket_t SocketStream::socket() const { return sock_; } +inline time_t SocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time) + .count(); +} + // Buffer stream implementation inline bool BufferStream::is_readable() const { return true; } @@ -6072,6 +6159,8 @@ inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, inline socket_t BufferStream::socket() const { return 0; } +inline time_t BufferStream::duration() const { return 0; } + inline const std::string &BufferStream::get_buffer() const { return buffer; } inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { @@ -7368,6 +7457,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { read_timeout_usec_ = rhs.read_timeout_usec_; write_timeout_sec_ = rhs.write_timeout_sec_; write_timeout_usec_ = rhs.write_timeout_usec_; + global_timeout_msec_ = rhs.global_timeout_msec_; basic_auth_username_ = rhs.basic_auth_username_; basic_auth_password_ = rhs.basic_auth_password_; bearer_token_auth_token_ = rhs.bearer_token_auth_token_; @@ -7514,18 +7604,6 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { return ret; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline bool ClientImpl::is_ssl_peer_could_be_closed(SSL *ssl) const { - detail::set_nonblocking(socket_.sock, true); - auto se = detail::scope_exit( - [&]() { detail::set_nonblocking(socket_.sock, false); }); - - char buf[1]; - return !SSL_peek(ssl, buf, 1) && - SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; -} -#endif - inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { { std::lock_guard guard(socket_mutex_); @@ -7540,7 +7618,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_alive && is_ssl()) { - if (is_ssl_peer_could_be_closed(socket_.ssl)) { is_alive = false; } + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + is_alive = false; + } } #endif @@ -7565,7 +7645,8 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { auto &scli = static_cast(*this); if (!proxy_host_.empty() && proxy_port_ != -1) { auto success = false; - if (!scli.connect_with_proxy(socket_, res, success, error)) { + if (!scli.connect_with_proxy(socket_, req.start_time_, res, success, + error)) { return success; } } @@ -7611,7 +7692,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { } }); - ret = process_socket(socket_, [&](Stream &strm) { + ret = process_socket(socket_, req.start_time_, [&](Stream &strm) { return handle_request(strm, req, res, close_connection, error); }); @@ -8020,6 +8101,9 @@ inline Result ClientImpl::send_with_content_provider( req.headers = headers; req.path = path; req.progress = progress; + if (global_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } auto error = Error::Success; @@ -8046,7 +8130,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (is_ssl()) { auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; if (!is_proxy_enabled) { - if (is_ssl_peer_could_be_closed(socket_.ssl)) { + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { error = Error::SSLPeerCouldBeClosed_; return false; } @@ -8171,12 +8255,14 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( }; } -inline bool -ClientImpl::process_socket(const Socket &socket, - std::function callback) { +inline bool ClientImpl::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { return detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, std::move(callback)); + write_timeout_usec_, global_timeout_msec_, start_time, + std::move(callback)); } inline bool ClientImpl::is_ssl() const { return false; } @@ -8200,6 +8286,9 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, req.path = path; req.headers = headers; req.progress = std::move(progress); + if (global_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } return send_(std::move(req)); } @@ -8265,6 +8354,9 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return content_receiver(data, data_length); }; req.progress = std::move(progress); + if (global_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } return send_(std::move(req)); } @@ -8310,6 +8402,9 @@ inline Result ClientImpl::Head(const std::string &path, req.method = "HEAD"; req.headers = headers; req.path = path; + if (global_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } return send_(std::move(req)); } @@ -8727,6 +8822,9 @@ inline Result ClientImpl::Delete(const std::string &path, req.headers = headers; req.path = path; req.progress = progress; + if (global_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } if (!content_type.empty()) { req.set_header("Content-Type", content_type); } req.body.assign(body, content_length); @@ -8774,6 +8872,9 @@ inline Result ClientImpl::Options(const std::string &path, req.method = "OPTIONS"; req.headers = headers; req.path = path; + if (global_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } return send_(std::move(req)); } @@ -8827,6 +8928,10 @@ inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { write_timeout_usec_ = usec; } +inline void ClientImpl::set_global_timeout(time_t msec) { + global_timeout_msec_ = msec; +} + inline void ClientImpl::set_basic_auth(const std::string &username, const std::string &password) { basic_auth_username_ = username; @@ -9065,12 +9170,14 @@ inline bool process_server_socket_ssl( } template -inline bool -process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { +inline bool process_client_socket_ssl( + SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t global_timeout_msec, + std::chrono::time_point start_time, T callback) { SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); + write_timeout_sec, write_timeout_usec, + global_timeout_msec, start_time); return callback(strm); } @@ -9083,27 +9190,37 @@ class SSLInit { }; // SSL socket stream implementation -inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, - time_t read_timeout_sec, - time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec) +inline SSLSocketStream::SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t global_timeout_msec, + std::chrono::time_point start_time) : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec) { + write_timeout_usec_(write_timeout_usec), + global_timeout_msec_(global_timeout_msec), start_time(start_time) { SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); } inline SSLSocketStream::~SSLSocketStream() = default; inline bool SSLSocketStream::is_readable() const { - return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + if (global_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } + + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(global_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); + + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; } inline bool SSLSocketStream::is_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); + is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_); } inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { @@ -9134,8 +9251,9 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { } } return ret; + } else { + return -1; } - return -1; } inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { @@ -9181,6 +9299,12 @@ inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, inline socket_t SSLSocketStream::socket() const { return sock_; } +inline time_t SSLSocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time) + .count(); +} + static SSLInit sslinit_; } // namespace detail @@ -9421,16 +9545,22 @@ inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { } // Assumes that socket_mutex_ is locked and that there are no requests in flight -inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, - bool &success, Error &error) { +inline bool SSLClient::connect_with_proxy( + Socket &socket, + std::chrono::time_point start_time, + Response &res, bool &success, Error &error) { success = true; Response proxy_res; if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + write_timeout_sec_, write_timeout_usec_, global_timeout_msec_, + start_time, [&](Stream &strm) { Request req2; req2.method = "CONNECT"; req2.path = host_and_port_; + if (global_timeout_msec_ > 0) { + req2.start_time_ = std::chrono::steady_clock::now(); + } return process_request(strm, req2, proxy_res, false, error); })) { // Thread-safe to close everything because we are assuming there are no @@ -9450,7 +9580,8 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, proxy_res = Response(); if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + write_timeout_sec_, write_timeout_usec_, global_timeout_msec_, + start_time, [&](Stream &strm) { Request req3; req3.method = "CONNECT"; req3.path = host_and_port_; @@ -9458,6 +9589,9 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, req3, auth, 1, detail::random_string(10), proxy_digest_auth_username_, proxy_digest_auth_password_, true)); + if (global_timeout_msec_ > 0) { + req3.start_time_ = std::chrono::steady_clock::now(); + } return process_request(strm, req3, proxy_res, false, error); })) { // Thread-safe to close everything because we are assuming there are @@ -9613,13 +9747,15 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket, assert(socket.ssl == nullptr); } -inline bool -SSLClient::process_socket(const Socket &socket, - std::function callback) { +inline bool SSLClient::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { assert(socket.ssl); return detail::process_client_socket_ssl( socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, std::move(callback)); + write_timeout_sec_, write_timeout_usec_, global_timeout_msec_, start_time, + std::move(callback)); } inline bool SSLClient::is_ssl() const { return true; } diff --git a/test/fuzzing/server_fuzzer.cc b/test/fuzzing/server_fuzzer.cc index 3cffbae244..b1ba3dbf85 100644 --- a/test/fuzzing/server_fuzzer.cc +++ b/test/fuzzing/server_fuzzer.cc @@ -39,6 +39,8 @@ class FuzzedStream : public httplib::Stream { socket_t socket() const override { return 0; } + time_t duration() const override { return 0; }; + private: const uint8_t *data_; size_t size_; diff --git a/test/test.cc b/test/test.cc index b69be5c35c..f2b108924f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -162,7 +162,8 @@ TEST(SocketStream, is_writable_UNIX) { const auto asSocketStream = [&](socket_t fd, std::function func) { - return detail::process_client_socket(fd, 0, 0, 0, 0, func); + return detail::process_client_socket( + fd, 0, 0, 0, 0, 0, std::chrono::steady_clock::time_point::min(), func); }; asSocketStream(fds[0], [&](Stream &s0) { EXPECT_EQ(s0.socket(), fds[0]); @@ -206,7 +207,8 @@ TEST(SocketStream, is_writable_INET) { const auto asSocketStream = [&](socket_t fd, std::function func) { - return detail::process_client_socket(fd, 0, 0, 0, 0, func); + return detail::process_client_socket( + fd, 0, 0, 0, 0, 0, std::chrono::steady_clock::time_point::min(), func); }; asSocketStream(disconnected_svr_sock, [&](Stream &ss) { EXPECT_EQ(ss.socket(), disconnected_svr_sock); @@ -4904,7 +4906,8 @@ static bool send_request(time_t read_timeout_sec, const std::string &req, if (client_sock == INVALID_SOCKET) { return false; } auto ret = detail::process_client_socket( - client_sock, read_timeout_sec, 0, 0, 0, [&](Stream &strm) { + client_sock, read_timeout_sec, 0, 0, 0, 0, + std::chrono::steady_clock::time_point::min(), [&](Stream &strm) { if (req.size() != static_cast(strm.write(req.data(), req.size()))) { return false; @@ -8190,3 +8193,255 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { } } #endif + +TEST(GlobalTimeoutTest, ContentStream) { + Server svr; + + svr.Get("/stream", [&](const Request &, Response &res) { + auto data = new std::string("01234567890123456789"); + + res.set_content_provider( + data->size(), "text/plain", + [&, data](size_t offset, size_t length, DataSink &sink) { + const size_t DATA_CHUNK_SIZE = 4; + const auto &d = *data; + std::this_thread::sleep_for(std::chrono::seconds(1)); + sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); + return true; + }, + [data](bool success) { + EXPECT_FALSE(success); + delete data; + }); + }); + + svr.Get("/stream_without_length", [&](const Request &, Response &res) { + auto i = new size_t(0); + + res.set_content_provider( + "text/plain", + [i](size_t, DataSink &sink) { + if (*i < 5) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + sink.write("abcd", 4); + (*i)++; + } else { + sink.done(); + } + return true; + }, + [i](bool success) { + EXPECT_FALSE(success); + delete i; + }); + }); + + svr.Get("/chunked", [&](const Request &, Response &res) { + auto i = new size_t(0); + + res.set_chunked_content_provider( + "text/plain", + [i](size_t, DataSink &sink) { + if (*i < 5) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + sink.os << "abcd"; + (*i)++; + } else { + sink.done(); + } + return true; + }, + [i](bool success) { + EXPECT_FALSE(success); + delete i; + }); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + const time_t timeout = 2000; + const time_t threshold = 200; + + Client cli("localhost", PORT); + cli.set_global_timeout(std::chrono::milliseconds(timeout)); + + + { + auto start = std::chrono::steady_clock::now(); + + auto res = cli.Get("/stream"); + + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + } + + { + auto start = std::chrono::steady_clock::now(); + + auto res = cli.Get("/stream_without_length"); + + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + } + + { + auto start = std::chrono::steady_clock::now(); + + auto res = cli.Get("/chunked", [&](const char *data, size_t data_length) { + EXPECT_EQ("abcd", string(data, data_length)); + return true; + }); + + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + } +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(GlobalTimeoutTest, ContentStreamSSL) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + + svr.Get("/stream", [&](const Request &, Response &res) { + auto data = new std::string("01234567890123456789"); + + res.set_content_provider( + data->size(), "text/plain", + [&, data](size_t offset, size_t length, DataSink &sink) { + const size_t DATA_CHUNK_SIZE = 4; + const auto &d = *data; + std::this_thread::sleep_for(std::chrono::seconds(1)); + sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); + return true; + }, + [data](bool success) { + EXPECT_FALSE(success); + delete data; + }); + }); + + svr.Get("/stream_without_length", [&](const Request &, Response &res) { + auto i = new size_t(0); + + res.set_content_provider( + "text/plain", + [i](size_t, DataSink &sink) { + if (*i < 5) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + sink.write("abcd", 4); + (*i)++; + } else { + sink.done(); + } + return true; + }, + [i](bool success) { + EXPECT_FALSE(success); + delete i; + }); + }); + + svr.Get("/chunked", [&](const Request &, Response &res) { + auto i = new size_t(0); + + res.set_chunked_content_provider( + "text/plain", + [i](size_t, DataSink &sink) { + if (*i < 5) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + sink.os << "abcd"; + (*i)++; + } else { + sink.done(); + } + return true; + }, + [i](bool success) { + EXPECT_FALSE(success); + delete i; + }); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + const time_t timeout = 2000; + const time_t threshold = 1000; // SSL_shutdown is slow... + + SSLClient cli("localhost", PORT); + cli.enable_server_certificate_verification(false); + cli.set_global_timeout(std::chrono::milliseconds(timeout)); + + { + auto start = std::chrono::steady_clock::now(); + + auto res = cli.Get("/stream"); + + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + } + + { + auto start = std::chrono::steady_clock::now(); + + auto res = cli.Get("/stream_without_length"); + + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + } + + { + auto start = std::chrono::steady_clock::now(); + + auto res = cli.Get("/chunked", [&](const char *data, size_t data_length) { + EXPECT_EQ("abcd", string(data, data_length)); + return true; + }); + + auto elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start) + .count(); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + } +} +#endif From 8e22a7676a7a3a484a08b7ae4bb9c8ac9733bb6f Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 10 Feb 2025 18:07:30 -0500 Subject: [PATCH 0363/1222] Remome 'global timeout' to 'max timeout' --- README.md | 3 ++ httplib.h | 90 ++++++++++++++++++++++++++-------------------------- test/test.cc | 8 ++--- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index dc2d9c5341..78a0ed4f4b 100644 --- a/README.md +++ b/README.md @@ -654,6 +654,9 @@ res = cli.Options("/resource/foo"); cli.set_connection_timeout(0, 300000); // 300 milliseconds cli.set_read_timeout(5, 0); // 5 seconds cli.set_write_timeout(5, 0); // 5 seconds + +// This method works the same as curl's `--max-timeout` option +svr.set_max_timeout(5000); // 5 seconds ``` ### Receive content with a content receiver diff --git a/httplib.h b/httplib.h index ed49a54d57..4ab5148bf2 100644 --- a/httplib.h +++ b/httplib.h @@ -66,8 +66,8 @@ #define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 #endif -#ifndef CPPHTTPLIB_CLIENT_GLOBAL_TIMEOUT_MSECOND -#define CPPHTTPLIB_CLIENT_GLOBAL_TIMEOUT_MSECOND 0 +#ifndef CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND +#define CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND 0 #endif #ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND @@ -1431,9 +1431,9 @@ class ClientImpl { template void set_write_timeout(const std::chrono::duration &duration); - void set_global_timeout(time_t msec); + void set_max_timeout(time_t msec); template - void set_global_timeout(const std::chrono::duration &duration); + void set_max_timeout(const std::chrono::duration &duration); void set_basic_auth(const std::string &username, const std::string &password); void set_bearer_token_auth(const std::string &token); @@ -1543,7 +1543,7 @@ class ClientImpl { time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; - time_t global_timeout_msec_ = CPPHTTPLIB_CLIENT_GLOBAL_TIMEOUT_MSECOND; + time_t max_timeout_msec_ = CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND; std::string basic_auth_username_; std::string basic_auth_password_; @@ -1868,9 +1868,9 @@ class Client { template void set_write_timeout(const std::chrono::duration &duration); - void set_global_timeout(time_t msec); + void set_max_timeout(time_t msec); template - void set_global_timeout(const std::chrono::duration &duration); + void set_max_timeout(const std::chrono::duration &duration); void set_basic_auth(const std::string &username, const std::string &password); void set_bearer_token_auth(const std::string &token); @@ -2261,11 +2261,11 @@ inline void ClientImpl::set_write_timeout( } template -inline void ClientImpl::set_global_timeout( +inline void ClientImpl::set_max_timeout( const std::chrono::duration &duration) { auto msec = std::chrono::duration_cast(duration).count(); - set_global_timeout(msec); + set_max_timeout(msec); } template @@ -2288,8 +2288,8 @@ Client::set_write_timeout(const std::chrono::duration &duration) { template inline void -Client::set_global_timeout(const std::chrono::duration &duration) { - cli_->set_global_timeout(duration); +Client::set_max_timeout(const std::chrono::duration &duration) { + cli_->set_max_timeout(duration); } /* @@ -2369,7 +2369,7 @@ void split(const char *b, const char *e, char d, size_t m, bool process_client_socket( socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - time_t global_timeout_msec, + time_t max_timeout_msec, std::chrono::time_point start_time, std::function callback); @@ -3338,7 +3338,7 @@ class SocketStream final : public Stream { public: SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - time_t global_timeout_msec = 0, + time_t max_timeout_msec = 0, std::chrono::time_point start_time = std::chrono::steady_clock::time_point::min()); ~SocketStream() override; @@ -3358,7 +3358,7 @@ class SocketStream final : public Stream { time_t read_timeout_usec_; time_t write_timeout_sec_; time_t write_timeout_usec_; - time_t global_timeout_msec_; + time_t max_timeout_msec_; const std::chrono::time_point start_time; std::vector read_buff_; @@ -3374,7 +3374,7 @@ class SSLSocketStream final : public Stream { SSLSocketStream( socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, time_t global_timeout_msec = 0, + time_t write_timeout_usec, time_t max_timeout_msec = 0, std::chrono::time_point start_time = std::chrono::steady_clock::time_point::min()); ~SSLSocketStream() override; @@ -3395,7 +3395,7 @@ class SSLSocketStream final : public Stream { time_t read_timeout_usec_; time_t write_timeout_sec_; time_t write_timeout_usec_; - time_t global_timeout_msec_; + time_t max_timeout_msec_; const std::chrono::time_point start_time; }; #endif @@ -3470,11 +3470,11 @@ process_server_socket(const std::atomic &svr_sock, socket_t sock, inline bool process_client_socket( socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - time_t global_timeout_msec, + time_t max_timeout_msec, std::chrono::time_point start_time, std::function callback) { SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec, global_timeout_msec, + write_timeout_sec, write_timeout_usec, max_timeout_msec, start_time); return callback(strm); } @@ -6010,14 +6010,14 @@ inline ssize_t Stream::write(const std::string &s) { namespace detail { -inline void calc_actual_timeout(time_t global_timeout_msec, +inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, time_t timeout_sec, time_t timeout_usec, time_t &actual_timeout_sec, time_t &actual_timeout_usec) { auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); auto actual_timeout_msec = - std::min(global_timeout_msec - duration_msec, timeout_msec); + std::min(max_timeout_msec - duration_msec, timeout_msec); actual_timeout_sec = actual_timeout_msec / 1000; actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; @@ -6027,25 +6027,25 @@ inline void calc_actual_timeout(time_t global_timeout_msec, inline SocketStream::SocketStream( socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - time_t global_timeout_msec, + time_t max_timeout_msec, std::chrono::time_point start_time) : sock_(sock), read_timeout_sec_(read_timeout_sec), read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), write_timeout_usec_(write_timeout_usec), - global_timeout_msec_(global_timeout_msec), start_time(start_time), + max_timeout_msec_(max_timeout_msec), start_time(start_time), read_buff_(read_buff_size_, 0) {} inline SocketStream::~SocketStream() = default; inline bool SocketStream::is_readable() const { - if (global_timeout_msec_ <= 0) { + if (max_timeout_msec_ <= 0) { return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; } time_t read_timeout_sec; time_t read_timeout_usec; - calc_actual_timeout(global_timeout_msec_, duration(), read_timeout_sec_, + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, read_timeout_usec_, read_timeout_sec, read_timeout_usec); return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; @@ -7457,7 +7457,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { read_timeout_usec_ = rhs.read_timeout_usec_; write_timeout_sec_ = rhs.write_timeout_sec_; write_timeout_usec_ = rhs.write_timeout_usec_; - global_timeout_msec_ = rhs.global_timeout_msec_; + max_timeout_msec_ = rhs.max_timeout_msec_; basic_auth_username_ = rhs.basic_auth_username_; basic_auth_password_ = rhs.basic_auth_password_; bearer_token_auth_token_ = rhs.bearer_token_auth_token_; @@ -8101,7 +8101,7 @@ inline Result ClientImpl::send_with_content_provider( req.headers = headers; req.path = path; req.progress = progress; - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8261,7 +8261,7 @@ inline bool ClientImpl::process_socket( std::function callback) { return detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, global_timeout_msec_, start_time, + write_timeout_usec_, max_timeout_msec_, start_time, std::move(callback)); } @@ -8286,7 +8286,7 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, req.path = path; req.headers = headers; req.progress = std::move(progress); - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8354,7 +8354,7 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return content_receiver(data, data_length); }; req.progress = std::move(progress); - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8402,7 +8402,7 @@ inline Result ClientImpl::Head(const std::string &path, req.method = "HEAD"; req.headers = headers; req.path = path; - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8822,7 +8822,7 @@ inline Result ClientImpl::Delete(const std::string &path, req.headers = headers; req.path = path; req.progress = progress; - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8872,7 +8872,7 @@ inline Result ClientImpl::Options(const std::string &path, req.method = "OPTIONS"; req.headers = headers; req.path = path; - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8928,8 +8928,8 @@ inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { write_timeout_usec_ = usec; } -inline void ClientImpl::set_global_timeout(time_t msec) { - global_timeout_msec_ = msec; +inline void ClientImpl::set_max_timeout(time_t msec) { + max_timeout_msec_ = msec; } inline void ClientImpl::set_basic_auth(const std::string &username, @@ -9173,11 +9173,11 @@ template inline bool process_client_socket_ssl( SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - time_t global_timeout_msec, + time_t max_timeout_msec, std::chrono::time_point start_time, T callback) { SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, write_timeout_sec, write_timeout_usec, - global_timeout_msec, start_time); + max_timeout_msec, start_time); return callback(strm); } @@ -9193,26 +9193,26 @@ class SSLInit { inline SSLSocketStream::SSLSocketStream( socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - time_t global_timeout_msec, + time_t max_timeout_msec, std::chrono::time_point start_time) : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), write_timeout_usec_(write_timeout_usec), - global_timeout_msec_(global_timeout_msec), start_time(start_time) { + max_timeout_msec_(max_timeout_msec), start_time(start_time) { SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); } inline SSLSocketStream::~SSLSocketStream() = default; inline bool SSLSocketStream::is_readable() const { - if (global_timeout_msec_ <= 0) { + if (max_timeout_msec_ <= 0) { return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; } time_t read_timeout_sec; time_t read_timeout_usec; - calc_actual_timeout(global_timeout_msec_, duration(), read_timeout_sec_, + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, read_timeout_usec_, read_timeout_sec, read_timeout_usec); return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; @@ -9553,12 +9553,12 @@ inline bool SSLClient::connect_with_proxy( Response proxy_res; if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, global_timeout_msec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, [&](Stream &strm) { Request req2; req2.method = "CONNECT"; req2.path = host_and_port_; - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req2.start_time_ = std::chrono::steady_clock::now(); } return process_request(strm, req2, proxy_res, false, error); @@ -9580,7 +9580,7 @@ inline bool SSLClient::connect_with_proxy( proxy_res = Response(); if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, global_timeout_msec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, [&](Stream &strm) { Request req3; req3.method = "CONNECT"; @@ -9589,7 +9589,7 @@ inline bool SSLClient::connect_with_proxy( req3, auth, 1, detail::random_string(10), proxy_digest_auth_username_, proxy_digest_auth_password_, true)); - if (global_timeout_msec_ > 0) { + if (max_timeout_msec_ > 0) { req3.start_time_ = std::chrono::steady_clock::now(); } return process_request(strm, req3, proxy_res, false, error); @@ -9754,7 +9754,7 @@ inline bool SSLClient::process_socket( assert(socket.ssl); return detail::process_client_socket_ssl( socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, global_timeout_msec_, start_time, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, std::move(callback)); } diff --git a/test/test.cc b/test/test.cc index f2b108924f..45a1d7ae7c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8194,7 +8194,7 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { } #endif -TEST(GlobalTimeoutTest, ContentStream) { +TEST(MaxTimeoutTest, ContentStream) { Server svr; svr.Get("/stream", [&](const Request &, Response &res) { @@ -8270,7 +8270,7 @@ TEST(GlobalTimeoutTest, ContentStream) { const time_t threshold = 200; Client cli("localhost", PORT); - cli.set_global_timeout(std::chrono::milliseconds(timeout)); + cli.set_max_timeout(std::chrono::milliseconds(timeout)); { @@ -8320,7 +8320,7 @@ TEST(GlobalTimeoutTest, ContentStream) { } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -TEST(GlobalTimeoutTest, ContentStreamSSL) { +TEST(MaxTimeoutTest, ContentStreamSSL) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); svr.Get("/stream", [&](const Request &, Response &res) { @@ -8397,7 +8397,7 @@ TEST(GlobalTimeoutTest, ContentStreamSSL) { SSLClient cli("localhost", PORT); cli.enable_server_certificate_verification(false); - cli.set_global_timeout(std::chrono::milliseconds(timeout)); + cli.set_max_timeout(std::chrono::milliseconds(timeout)); { auto start = std::chrono::steady_clock::now(); From b397c768e414b20da3e452d5adb6d5ed4fb73791 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 11 Feb 2025 00:15:19 +0100 Subject: [PATCH 0364/1222] Unify select_read() and select_write() (#2047) --- httplib.h | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/httplib.h b/httplib.h index 4ab5148bf2..f7d131d5ae 100644 --- a/httplib.h +++ b/httplib.h @@ -3210,60 +3210,43 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, }); } -inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { +template +inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { #ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN; + struct pollfd pfd; + pfd.fd = sock; + pfd.events = (Read ? POLLIN : POLLOUT); auto timeout = static_cast(sec * 1000 + usec / 1000); - return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); + return handle_EINTR([&]() { return poll(&pfd, 1, timeout); }); #else #ifndef _WIN32 if (sock >= FD_SETSIZE) { return -1; } #endif - fd_set fds; + fd_set fds, *rfds, *wfds; FD_ZERO(&fds); FD_SET(sock, &fds); + rfds = (Read ? &fds : nullptr); + wfds = (Read ? nullptr : &fds); timeval tv; tv.tv_sec = static_cast(sec); tv.tv_usec = static_cast(usec); return handle_EINTR([&]() { - return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); + return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); }); #endif } -inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLOUT; - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return -1; } -#endif - - fd_set fds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} - return handle_EINTR([&]() { - return select(static_cast(sock + 1), nullptr, &fds, nullptr, &tv); - }); -#endif +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); } inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, From a268d65c4f84c9f60063ee7c5d3c583c176255fb Mon Sep 17 00:00:00 2001 From: Brett Profitt Date: Mon, 10 Feb 2025 21:46:38 -0500 Subject: [PATCH 0365/1222] Fix check for URI length to prevent incorrect HTTP 414 errors (#2046) --- httplib.h | 16 ++++++++-------- test/test.cc | 13 ++++++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/httplib.h b/httplib.h index f7d131d5ae..1c0775907e 100644 --- a/httplib.h +++ b/httplib.h @@ -7234,14 +7234,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr, #endif #endif - // Check if the request URI doesn't exceed the limit - if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::UriTooLong_414; - return write_response(strm, close_connection, req, res); - } - // Request line and headers if (!parse_request_line(line_reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { @@ -7249,6 +7241,14 @@ Server::process_request(Stream &strm, const std::string &remote_addr, return write_response(strm, close_connection, req, res); } + // Check if the request URI doesn't exceed the limit + if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::UriTooLong_414; + return write_response(strm, close_connection, req, res); + } + if (req.get_header_value("Connection") == "close") { connection_closed = true; } diff --git a/test/test.cc b/test/test.cc index 45a1d7ae7c..82648b9531 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3541,7 +3541,7 @@ TEST_F(ServerTest, LongRequest) { TEST_F(ServerTest, TooLongRequest) { std::string request; - for (size_t i = 0; i < 545; i++) { + for (size_t i = 0; i < 546; i++) { request += "/TooLongRequest"; } request += "_NG"; @@ -3552,6 +3552,17 @@ TEST_F(ServerTest, TooLongRequest) { EXPECT_EQ(StatusCode::UriTooLong_414, res->status); } +TEST_F(ServerTest, AlmostTooLongRequest) { + // test for #2046 - URI length check shouldn't include other content on req line + // URI is max URI length, minus 14 other chars in req line (GET, space, leading /, space, HTTP/1.1) + std::string request = "/" + string(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH - 14, 'A'); + + auto res = cli_.Get(request.c_str()); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::NotFound_404, res->status); +} + TEST_F(ServerTest, LongHeader) { Request req; req.method = "GET"; From dd2034282571d01e6891ccfaf301185507d71ac2 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 11 Feb 2025 12:55:13 +0100 Subject: [PATCH 0366/1222] Don't run CI twice (on push AND pull request) (#2049) --- .github/workflows/test.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f7dc0c866d..5a4e180cfa 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,6 +5,7 @@ on: [push, pull_request] jobs: ubuntu: runs-on: ubuntu-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name steps: - name: checkout uses: actions/checkout@v4 @@ -17,6 +18,7 @@ jobs: macos: runs-on: macos-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name steps: - name: checkout uses: actions/checkout@v4 @@ -27,6 +29,7 @@ jobs: windows: runs-on: windows-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name steps: - name: Prepare Git for Checkout on Windows run: | From 1880693aefa49c6b3897fc6821afc5d518774cf4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 11 Feb 2025 11:22:14 -0500 Subject: [PATCH 0367/1222] Dropped Visual Studio 2015 support --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 78a0ed4f4b..fc4b537f94 100644 --- a/README.md +++ b/README.md @@ -966,7 +966,7 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32 > cpp-httplib officially supports only the latest Visual Studio. It might work with former versions of Visual Studio, but I can no longer verify it. Pull requests are always welcome for the older versions of Visual Studio unless they break the C++11 conformance. > [!NOTE] -> Windows 8 or lower, Visual Studio 2013 or lower, and Cygwin and MSYS2 including MinGW are neither supported nor tested. +> Windows 8 or lower, Visual Studio 2015 or lower, and Cygwin and MSYS2 including MinGW are neither supported nor tested. License ------- From d7c14b6f3a58661fd609d703dca1ba70596d4483 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 11 Feb 2025 17:48:31 -0500 Subject: [PATCH 0368/1222] Add API compatibility check tool --- test/Makefile | 3 + test/check-abi-compatibility.sh | 59 +++++++++++++++++++ .../check-shared-library-abi-compatibility.sh | 58 ++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100755 test/check-abi-compatibility.sh create mode 100755 test/check-shared-library-abi-compatibility.sh diff --git a/test/Makefile b/test/Makefile index 587a6753ff..081feb97a2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -42,6 +42,9 @@ test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem test_split : test.cc ../httplib.h httplib.cc Makefile cert.pem $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS) +check_abi: + @./check-shared-library-abi-compatibility.sh + test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem $(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS) diff --git a/test/check-abi-compatibility.sh b/test/check-abi-compatibility.sh new file mode 100755 index 0000000000..a7d9f8761a --- /dev/null +++ b/test/check-abi-compatibility.sh @@ -0,0 +1,59 @@ +#!/bin/bash +if [ "$#" -ne 2 ]; then + echo "Usage: $0 old_library.so new_library.so" + exit 1 +fi + +OLD_LIB=$1 +NEW_LIB=$2 + +OLD_FUNCS=_old_funcs.txt +NEW_FUNCS=_new_funcs.txt +OLD_VARS=_old_vars.txt +NEW_VARS=_new_vars.txt + +# Extract function symbols from the old and new libraries +nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[TW]/ {print substr($0, index($0,$3))}' | sort > $OLD_FUNCS +nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[TW]/ {print substr($0, index($0,$3))}' | sort > $NEW_FUNCS + +# Extract variable symbols from the old and new libraries +nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[BDG]/ {print substr($0, index($0,$3))}' | sort > $OLD_VARS +nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[BDG]/ {print substr($0, index($0,$3))}' | sort > $NEW_VARS + +# Initialize error flag and message +error_flag=0 +error_message="" + +# Check for removed function symbols +removed_funcs=$(comm -23 $OLD_FUNCS $NEW_FUNCS) +if [ -n "$removed_funcs" ]; then + error_flag=1 + error_message+="[Removed Functions]\n$removed_funcs\n" +fi + +# Check for removed variable symbols +removed_vars=$(comm -23 $OLD_VARS $NEW_VARS) +if [ -n "$removed_vars" ]; then + error_flag=1 + error_message+="[Removed Variables]\n$removed_vars\n" +fi + +# Check for added variable symbols +added_vars=$(comm -13 $OLD_VARS $NEW_VARS) +if [ -n "$added_vars" ]; then + error_flag=1 + error_message+="[Added Variables]\n$added_vars\n" +fi + +# Remove temporary files +rm -f $NEW_FUNCS $OLD_FUNCS $OLD_VARS $NEW_VARS + +# Display error messages if any +if [ "$error_flag" -eq 1 ]; then + echo -e "$error_message" + echo "ABI compatibility check failed." + exit 1 +fi + +echo "ABI compatibility check passed: No variable symbols were removed or added, and no function symbols were removed." +exit 0 diff --git a/test/check-shared-library-abi-compatibility.sh b/test/check-shared-library-abi-compatibility.sh new file mode 100755 index 0000000000..08c403cb4d --- /dev/null +++ b/test/check-shared-library-abi-compatibility.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +CURRENT_COMMIT=$(git rev-parse HEAD) +PREVIOUS_VERSION=$(git describe --tags --abbrev=0 $CURRENT_COMMIT) + +BUILD_DIR=_build_for_is_abi_compatible + +# Make the build directory +rm -rf $BUILD_DIR +mkdir -p $BUILD_DIR/new +mkdir -p $BUILD_DIR/old + +cd $BUILD_DIR + +# Build the current commit +cd new + +cmake \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_CXX_FLAGS="-g -Og" \ + -DBUILD_SHARED_LIBS=ON \ + -DHTTPLIB_COMPILE=ON \ + -DCMAKE_INSTALL_PREFIX=./out \ + ../../.. > /dev/null + +cmake --build . --target install > /dev/null +cmake --build . --target clean > /dev/null + +cd .. + +# Build the nearest vesion +cd old +cmake \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_CXX_FLAGS="-g -Og" \ + -DBUILD_SHARED_LIBS=ON \ + -DHTTPLIB_COMPILE=ON \ + -DCMAKE_INSTALL_PREFIX=./out \ + ../../.. > /dev/null + +git checkout -q "${PREVIOUS_VERSION}" +cmake --build . --target install > /dev/null +cmake --build . --target clean > /dev/null + +cd .. + +# Checkout the original commit +if [ "$CURRENT_COMMIT" = "$(git rev-parse master)" ]; then + git checkout -q master +else + git checkout -q "${CURRENT_COMMIT}" +fi + +# ABI compatibility check +../check-abi-compatibility.sh ./old/out/lib/libcpp-httplib.so ./new/out/lib/libcpp-httplib.so + +# Clean the build directory +cd .. +rm -rf $BUILD_DIR From 39a64fb4e7e42216f14f0ec51ccc5fa85e651432 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 11 Feb 2025 18:10:27 -0500 Subject: [PATCH 0369/1222] Fix ABI compatibility tool on macOS --- .gitignore | 1 + test/Makefile | 2 +- .../check-shared-library-abi-compatibility.sh | 28 +++++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 95c588784d..1ae8acf708 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ test/test.xcodeproj/*/xcuser* test/*.o test/*.pem test/*.srl +test/_build_* work/ benchmark/server* diff --git a/test/Makefile b/test/Makefile index 081feb97a2..152f9098ef 100644 --- a/test/Makefile +++ b/test/Makefile @@ -69,5 +69,5 @@ cert.pem: ./gen-certs.sh clean: - rm -f test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc + rm -rf test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* diff --git a/test/check-shared-library-abi-compatibility.sh b/test/check-shared-library-abi-compatibility.sh index 08c403cb4d..18eb43f027 100755 --- a/test/check-shared-library-abi-compatibility.sh +++ b/test/check-shared-library-abi-compatibility.sh @@ -1,8 +1,6 @@ #!/usr/bin/env bash -CURRENT_COMMIT=$(git rev-parse HEAD) -PREVIOUS_VERSION=$(git describe --tags --abbrev=0 $CURRENT_COMMIT) - -BUILD_DIR=_build_for_is_abi_compatible +PREVIOUS_VERSION=$(git describe --tags --abbrev=0 master) +BUILD_DIR=_build_for_abi_compatibility_check # Make the build directory rm -rf $BUILD_DIR @@ -29,6 +27,7 @@ cd .. # Build the nearest vesion cd old + cmake \ -DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_CXX_FLAGS="-g -Og" \ @@ -44,15 +43,16 @@ cmake --build . --target clean > /dev/null cd .. # Checkout the original commit -if [ "$CURRENT_COMMIT" = "$(git rev-parse master)" ]; then - git checkout -q master -else - git checkout -q "${CURRENT_COMMIT}" -fi +git checkout -q master # ABI compatibility check -../check-abi-compatibility.sh ./old/out/lib/libcpp-httplib.so ./new/out/lib/libcpp-httplib.so - -# Clean the build directory -cd .. -rm -rf $BUILD_DIR +if [[ "$OSTYPE" == "linux-gnu"* ]]; then + ../check-abi-compatibility.sh ./old/out/lib/libcpp-httplib.so ./new/out/lib/libcpp-httplib.so + exit $? +elif [[ "$OSTYPE" == "darwin"* ]]; then + ../check-abi-compatibility.sh ./old/out/lib/libcpp-httplib.dylib ./new/out/lib/libcpp-httplib.dylib + exit $? +else + echo "Unknown OS..." + exit 1 +fi From b6ab8435d792e35e56955b17fd53ae73185746ab Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 12 Feb 2025 12:49:20 -0500 Subject: [PATCH 0370/1222] Improve ABI check tool on macOS --- test/check-abi-compatibility.sh | 16 ++++++++-------- test/make-shared-library.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) create mode 100755 test/make-shared-library.sh diff --git a/test/check-abi-compatibility.sh b/test/check-abi-compatibility.sh index a7d9f8761a..ec3b211ad4 100755 --- a/test/check-abi-compatibility.sh +++ b/test/check-abi-compatibility.sh @@ -13,12 +13,12 @@ OLD_VARS=_old_vars.txt NEW_VARS=_new_vars.txt # Extract function symbols from the old and new libraries -nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[TW]/ {print substr($0, index($0,$3))}' | sort > $OLD_FUNCS -nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[TW]/ {print substr($0, index($0,$3))}' | sort > $NEW_FUNCS +nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[TWt]/ {print substr($0, index($0,$3))}' | sort > $OLD_FUNCS +nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[TWt]/ {print substr($0, index($0,$3))}' | sort > $NEW_FUNCS # Extract variable symbols from the old and new libraries -nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[BDG]/ {print substr($0, index($0,$3))}' | sort > $OLD_VARS -nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[BDG]/ {print substr($0, index($0,$3))}' | sort > $NEW_VARS +nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[BDGVs]/ {print substr($0, index($0,$3))}' | sort > $OLD_VARS +nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[BDGVs]/ {print substr($0, index($0,$3))}' | sort > $NEW_VARS # Initialize error flag and message error_flag=0 @@ -28,21 +28,21 @@ error_message="" removed_funcs=$(comm -23 $OLD_FUNCS $NEW_FUNCS) if [ -n "$removed_funcs" ]; then error_flag=1 - error_message+="[Removed Functions]\n$removed_funcs\n" + error_message+="[Removed Functions]\n$removed_funcs\n\n" fi # Check for removed variable symbols removed_vars=$(comm -23 $OLD_VARS $NEW_VARS) if [ -n "$removed_vars" ]; then error_flag=1 - error_message+="[Removed Variables]\n$removed_vars\n" + error_message+="[Removed Variables]\n$removed_vars\n\n" fi # Check for added variable symbols added_vars=$(comm -13 $OLD_VARS $NEW_VARS) if [ -n "$added_vars" ]; then error_flag=1 - error_message+="[Added Variables]\n$added_vars\n" + error_message+="[Added Variables]\n$added_vars\n\n" fi # Remove temporary files @@ -50,7 +50,7 @@ rm -f $NEW_FUNCS $OLD_FUNCS $OLD_VARS $NEW_VARS # Display error messages if any if [ "$error_flag" -eq 1 ]; then - echo -e "$error_message" + echo -en "$error_message" echo "ABI compatibility check failed." exit 1 fi diff --git a/test/make-shared-library.sh b/test/make-shared-library.sh new file mode 100755 index 0000000000..04f2fc0891 --- /dev/null +++ b/test/make-shared-library.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +if [ "$#" -ne 1 ]; then + echo "Usage: $0 build_dir" + exit 1 +fi + +BUILD_DIR=$1 + +# Make the build directory +rm -rf $BUILD_DIR +mkdir -p $BUILD_DIR/out + +cd $BUILD_DIR + +# Build the version +git checkout $BUILD_DIR -q + +cmake \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_CXX_FLAGS="-g -Og" \ + -DBUILD_SHARED_LIBS=ON \ + -DHTTPLIB_COMPILE=ON \ + -DCMAKE_INSTALL_PREFIX=./out \ + ../.. + +cmake --build . --target install +cmake --build . --target clean + From bfa2f735f290fe30b3fddd6b9b7d8d96f557ca29 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Fri, 14 Feb 2025 20:06:35 +0100 Subject: [PATCH 0371/1222] ci: add abidiff workflow (#2054) This CI workflow checks ABI compatibility between the pushed commit and the latest tagged release, helping preventing accidental ABI breaks. Helps with https://github.com/yhirose/cpp-httplib/issues/2043 --- .github/workflows/abidiff.yaml | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/abidiff.yaml diff --git a/.github/workflows/abidiff.yaml b/.github/workflows/abidiff.yaml new file mode 100644 index 0000000000..e914a0663b --- /dev/null +++ b/.github/workflows/abidiff.yaml @@ -0,0 +1,64 @@ +# SPDX-FileCopyrightText: 2025 Andrea Pappacoda +# SPDX-License-Identifier: MIT + +name: abidiff + +on: [push, pull_request] + +defaults: + run: + shell: sh + +jobs: + abi: + runs-on: ubuntu-latest + container: + image: debian:testing + + steps: + - name: Install dependencies + run: apt -y --update install --no-install-recommends + abigail-tools + ca-certificates + g++ + git + libbrotli-dev + libssl-dev + meson + pkg-config + python3 + zlib1g-dev + + - uses: actions/checkout@v4 + with: + path: current + + - uses: actions/checkout@v4 + with: + path: previous + fetch-depth: 0 + + - name: Checkout previous + working-directory: previous + run: | + git switch master + git describe --tags --abbrev=0 master | xargs git checkout + + - name: Build current + working-directory: current + run: | + meson setup --buildtype=debug -Dcpp-httplib_compile=true build + ninja -C build + + - name: Build previous + working-directory: previous + run: | + meson setup --buildtype=debug -Dcpp-httplib_compile=true build + ninja -C build + + - name: Run abidiff + run: abidiff + --headers-dir1 previous/build + --headers-dir2 current/build + previous/build/libcpp-httplib.so + current/build/libcpp-httplib.so From d74e4a7c9c6241fc4b35128a7c5d615bfc364956 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 14 Feb 2025 14:10:06 -0500 Subject: [PATCH 0372/1222] Removed incomplete API compatibility check scripts. --- test/check-abi-compatibility.sh | 59 ------------------- .../check-shared-library-abi-compatibility.sh | 58 ------------------ 2 files changed, 117 deletions(-) delete mode 100755 test/check-abi-compatibility.sh delete mode 100755 test/check-shared-library-abi-compatibility.sh diff --git a/test/check-abi-compatibility.sh b/test/check-abi-compatibility.sh deleted file mode 100755 index ec3b211ad4..0000000000 --- a/test/check-abi-compatibility.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash -if [ "$#" -ne 2 ]; then - echo "Usage: $0 old_library.so new_library.so" - exit 1 -fi - -OLD_LIB=$1 -NEW_LIB=$2 - -OLD_FUNCS=_old_funcs.txt -NEW_FUNCS=_new_funcs.txt -OLD_VARS=_old_vars.txt -NEW_VARS=_new_vars.txt - -# Extract function symbols from the old and new libraries -nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[TWt]/ {print substr($0, index($0,$3))}' | sort > $OLD_FUNCS -nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[TWt]/ {print substr($0, index($0,$3))}' | sort > $NEW_FUNCS - -# Extract variable symbols from the old and new libraries -nm -C --defined-only $OLD_LIB | c++filt | awk '$2 ~ /[BDGVs]/ {print substr($0, index($0,$3))}' | sort > $OLD_VARS -nm -C --defined-only $NEW_LIB | c++filt | awk '$2 ~ /[BDGVs]/ {print substr($0, index($0,$3))}' | sort > $NEW_VARS - -# Initialize error flag and message -error_flag=0 -error_message="" - -# Check for removed function symbols -removed_funcs=$(comm -23 $OLD_FUNCS $NEW_FUNCS) -if [ -n "$removed_funcs" ]; then - error_flag=1 - error_message+="[Removed Functions]\n$removed_funcs\n\n" -fi - -# Check for removed variable symbols -removed_vars=$(comm -23 $OLD_VARS $NEW_VARS) -if [ -n "$removed_vars" ]; then - error_flag=1 - error_message+="[Removed Variables]\n$removed_vars\n\n" -fi - -# Check for added variable symbols -added_vars=$(comm -13 $OLD_VARS $NEW_VARS) -if [ -n "$added_vars" ]; then - error_flag=1 - error_message+="[Added Variables]\n$added_vars\n\n" -fi - -# Remove temporary files -rm -f $NEW_FUNCS $OLD_FUNCS $OLD_VARS $NEW_VARS - -# Display error messages if any -if [ "$error_flag" -eq 1 ]; then - echo -en "$error_message" - echo "ABI compatibility check failed." - exit 1 -fi - -echo "ABI compatibility check passed: No variable symbols were removed or added, and no function symbols were removed." -exit 0 diff --git a/test/check-shared-library-abi-compatibility.sh b/test/check-shared-library-abi-compatibility.sh deleted file mode 100755 index 18eb43f027..0000000000 --- a/test/check-shared-library-abi-compatibility.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash -PREVIOUS_VERSION=$(git describe --tags --abbrev=0 master) -BUILD_DIR=_build_for_abi_compatibility_check - -# Make the build directory -rm -rf $BUILD_DIR -mkdir -p $BUILD_DIR/new -mkdir -p $BUILD_DIR/old - -cd $BUILD_DIR - -# Build the current commit -cd new - -cmake \ - -DCMAKE_BUILD_TYPE=Debug \ - -DCMAKE_CXX_FLAGS="-g -Og" \ - -DBUILD_SHARED_LIBS=ON \ - -DHTTPLIB_COMPILE=ON \ - -DCMAKE_INSTALL_PREFIX=./out \ - ../../.. > /dev/null - -cmake --build . --target install > /dev/null -cmake --build . --target clean > /dev/null - -cd .. - -# Build the nearest vesion -cd old - -cmake \ - -DCMAKE_BUILD_TYPE=Debug \ - -DCMAKE_CXX_FLAGS="-g -Og" \ - -DBUILD_SHARED_LIBS=ON \ - -DHTTPLIB_COMPILE=ON \ - -DCMAKE_INSTALL_PREFIX=./out \ - ../../.. > /dev/null - -git checkout -q "${PREVIOUS_VERSION}" -cmake --build . --target install > /dev/null -cmake --build . --target clean > /dev/null - -cd .. - -# Checkout the original commit -git checkout -q master - -# ABI compatibility check -if [[ "$OSTYPE" == "linux-gnu"* ]]; then - ../check-abi-compatibility.sh ./old/out/lib/libcpp-httplib.so ./new/out/lib/libcpp-httplib.so - exit $? -elif [[ "$OSTYPE" == "darwin"* ]]; then - ../check-abi-compatibility.sh ./old/out/lib/libcpp-httplib.dylib ./new/out/lib/libcpp-httplib.dylib - exit $? -else - echo "Unknown OS..." - exit 1 -fi From 3c4b96024f6dd36fd67fef412103909914dd1086 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 14 Feb 2025 14:19:54 -0500 Subject: [PATCH 0373/1222] Don't run CI twice (on push AND pull request) --- .github/workflows/abidiff.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/abidiff.yaml b/.github/workflows/abidiff.yaml index e914a0663b..215d63a4f3 100644 --- a/.github/workflows/abidiff.yaml +++ b/.github/workflows/abidiff.yaml @@ -12,6 +12,7 @@ defaults: jobs: abi: runs-on: ubuntu-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name container: image: debian:testing From 03cf43ebaa55f27a2778bed870ea3549f7e84e2c Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 14 Feb 2025 14:42:29 -0500 Subject: [PATCH 0374/1222] Release v0.19.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 1c0775907e..e4799dab79 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.18.7" +#define CPPHTTPLIB_VERSION "0.19.0" /* * Configuration From 233f0fb1b8f516eb7a9e54d9fc8cc97141b77c9d Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 15 Feb 2025 04:40:24 +0100 Subject: [PATCH 0375/1222] Refactor setting socket options (#2053) Add detail::set_socket_opt() and detail::set_socket_opt_time() to avoid repetition of platform-specific code. --- httplib.h | 147 +++++++++++++++++++++--------------------------------- 1 file changed, 56 insertions(+), 91 deletions(-) diff --git a/httplib.h b/httplib.h index e4799dab79..593beb5015 100644 --- a/httplib.h +++ b/httplib.h @@ -193,6 +193,7 @@ using ssize_t = long; #endif using socket_t = SOCKET; +using socklen_t = int; #ifdef CPPHTTPLIB_USE_POLL #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) #endif @@ -848,6 +849,16 @@ using Logger = std::function; using SocketOptions = std::function; +namespace detail { + +bool set_socket_opt_impl(socket_t sock, int level, int optname, + const void *optval, socklen_t optlen); +bool set_socket_opt(socket_t sock, int level, int optname, int opt); +bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, + time_t usec); + +} // namespace detail + void default_socket_options(socket_t sock); const char *status_message(int status); @@ -2075,20 +2086,45 @@ inline uint64_t Response::get_header_value_u64(const std::string &key, return detail::get_header_value_u64(headers, key, def, id); } -inline void default_socket_options(socket_t sock) { - int opt = 1; +namespace detail { + +inline bool set_socket_opt_impl(socket_t sock, int level, int optname, + const void *optval, socklen_t optlen) { + return setsockopt(sock, level, optname, #ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); + reinterpret_cast(optval), #else -#ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, - reinterpret_cast(&opt), sizeof(opt)); + optval, +#endif + optlen) == 0; +} + +inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) { + return set_socket_opt_impl(sock, level, optname, &optval, sizeof(optval)); +} + +inline bool set_socket_opt_time(socket_t sock, int level, int optname, + time_t sec, time_t usec) { +#ifdef _WIN32 + auto timeout = static_cast(sec * 1000 + usec / 1000); #else - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); + timeval timeout; + timeout.tv_sec = static_cast(sec); + timeout.tv_usec = static_cast(usec); #endif + return set_socket_opt_impl(sock, level, optname, &timeout, sizeof(timeout)); +} + +} // namespace detail + +inline void default_socket_options(socket_t sock) { + detail::set_socket_opt(sock, SOL_SOCKET, +#ifdef SO_REUSEPORT + SO_REUSEPORT, +#else + SO_REUSEADDR, #endif + 1); } inline const char *status_message(int status) { @@ -3605,26 +3641,10 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, } #endif - if (tcp_nodelay) { - auto opt = 1; -#ifdef _WIN32 - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&opt), sizeof(opt)); -#endif - } + if (tcp_nodelay) { set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); } if (rp->ai_family == AF_INET6) { - auto opt = ipv6_v6only ? 1 : 0; -#ifdef _WIN32 - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&opt), sizeof(opt)); -#endif + set_socket_opt(sock, IPPROTO_IPV6, IPV6_V6ONLY, ipv6_v6only ? 1 : 0); } if (socket_options) { socket_options(sock); } @@ -3767,36 +3787,10 @@ inline socket_t create_client_socket( } set_nonblocking(sock2, false); - - { -#ifdef _WIN32 - auto timeout = static_cast(read_timeout_sec * 1000 + - read_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(read_timeout_sec); - tv.tv_usec = static_cast(read_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - { - -#ifdef _WIN32 - auto timeout = static_cast(write_timeout_sec * 1000 + - write_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(write_timeout_sec); - tv.tv_usec = static_cast(write_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } + set_socket_opt_time(sock2, SOL_SOCKET, SO_RCVTIMEO, read_timeout_sec, + read_timeout_usec); + set_socket_opt_time(sock2, SOL_SOCKET, SO_SNDTIMEO, write_timeout_sec, + write_timeout_usec); error = Error::Success; return true; @@ -6946,35 +6940,10 @@ inline bool Server::listen_internal() { break; } - { -#ifdef _WIN32 - auto timeout = static_cast(read_timeout_sec_ * 1000 + - read_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(read_timeout_sec_); - tv.tv_usec = static_cast(read_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - { - -#ifdef _WIN32 - auto timeout = static_cast(write_timeout_sec_ * 1000 + - write_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(write_timeout_sec_); - tv.tv_usec = static_cast(write_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } + detail::set_socket_opt_time(sock, SOL_SOCKET, SO_RCVTIMEO, + read_timeout_sec_, read_timeout_usec_); + detail::set_socket_opt_time(sock, SOL_SOCKET, SO_SNDTIMEO, + write_timeout_sec_, write_timeout_usec_); if (!task_queue->enqueue( [this, sock]() { process_and_close_socket(sock); })) { @@ -9097,11 +9066,7 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, (void)(sock); SSL_shutdown(ssl); #else - timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); + detail::set_socket_opt_time(sock, SOL_SOCKET, SO_RCVTIMEO, 1, 0); auto ret = SSL_shutdown(ssl); while (ret == 0) { From 985cd9f6a288fa0bbdd9c77697746ef02be98e0a Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 16 Feb 2025 14:39:29 +0100 Subject: [PATCH 0376/1222] Fix compilation failures with include (#2057) --- httplib.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 593beb5015..cebc1c089c 100644 --- a/httplib.h +++ b/httplib.h @@ -670,7 +670,7 @@ struct Request { bool is_chunked_content_provider_ = false; size_t authorization_count_ = 0; std::chrono::time_point start_time_ = - std::chrono::steady_clock::time_point::min(); + (std::chrono::steady_clock::time_point::min)(); }; struct Response { @@ -3359,7 +3359,7 @@ class SocketStream final : public Stream { time_t write_timeout_sec, time_t write_timeout_usec, time_t max_timeout_msec = 0, std::chrono::time_point start_time = - std::chrono::steady_clock::time_point::min()); + (std::chrono::steady_clock::time_point::min)()); ~SocketStream() override; bool is_readable() const override; @@ -3395,7 +3395,7 @@ class SSLSocketStream final : public Stream { time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, time_t max_timeout_msec = 0, std::chrono::time_point start_time = - std::chrono::steady_clock::time_point::min()); + (std::chrono::steady_clock::time_point::min)()); ~SSLSocketStream() override; bool is_readable() const override; @@ -5994,7 +5994,7 @@ inline void calc_actual_timeout(time_t max_timeout_msec, auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); auto actual_timeout_msec = - std::min(max_timeout_msec - duration_msec, timeout_msec); + (std::min)(max_timeout_msec - duration_msec, timeout_msec); actual_timeout_sec = actual_timeout_msec / 1000; actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; From 4cb8ff9f905312707d5d91551ed9ea02eab444d7 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 16 Feb 2025 14:43:54 +0100 Subject: [PATCH 0377/1222] Print timeout exceedance in MaxTimeoutTest (#2060) --- test/test.cc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/test/test.cc b/test/test.cc index 82648b9531..87f991749e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8295,7 +8295,8 @@ TEST(MaxTimeoutTest, ContentStream) { ASSERT_FALSE(res); EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) + << "Timeout exceeded by " << (elapsed - timeout) << "ms"; } { @@ -8309,7 +8310,8 @@ TEST(MaxTimeoutTest, ContentStream) { ASSERT_FALSE(res); EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) + << "Timeout exceeded by " << (elapsed - timeout) << "ms"; } { @@ -8326,7 +8328,8 @@ TEST(MaxTimeoutTest, ContentStream) { ASSERT_FALSE(res); EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) + << "Timeout exceeded by " << (elapsed - timeout) << "ms"; } } @@ -8421,7 +8424,8 @@ TEST(MaxTimeoutTest, ContentStreamSSL) { ASSERT_FALSE(res); EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) + << "Timeout exceeded by " << (elapsed - timeout) << "ms"; } { @@ -8435,7 +8439,8 @@ TEST(MaxTimeoutTest, ContentStreamSSL) { ASSERT_FALSE(res); EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) + << "Timeout exceeded by " << (elapsed - timeout) << "ms"; } { @@ -8452,7 +8457,8 @@ TEST(MaxTimeoutTest, ContentStreamSSL) { ASSERT_FALSE(res); EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold); + EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) + << "Timeout exceeded by " << (elapsed - timeout) << "ms"; } } #endif From 748f47b3772d39b9c8705fc636b2530d13c7f13c Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 16 Feb 2025 18:34:28 +0100 Subject: [PATCH 0378/1222] Add workflow_dispatch with Google Test filter and OS selection (#2056) * Add workflow_dispatch with Google Test filter Add the workflow_dispatch trigger to the test.yaml workflow. Includes an input for an optional Google Test filter pattern. * Add OS selection to workflow_dispatch * Fix wording --- .github/workflows/test.yaml | 41 +++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5a4e180cfa..370ad16346 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,11 +1,36 @@ name: test -on: [push, pull_request] +on: + push: + pull_request: + workflow_dispatch: + inputs: + gtest_filter: + description: 'Google Test filter' + test_linux: + description: 'Test on Linux' + type: boolean + default: true + test_macos: + description: 'Test on MacOS' + type: boolean + default: true + test_windows: + description: 'Test on Windows' + type: boolean + default: true + +env: + GTEST_FILTER: ${{ github.event.inputs.gtest_filter || '*' }} jobs: ubuntu: runs-on: ubuntu-latest - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name + if: > + (github.event_name == 'push') || + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || + (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') steps: - name: checkout uses: actions/checkout@v4 @@ -18,7 +43,11 @@ jobs: macos: runs-on: macos-latest - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name + if: > + (github.event_name == 'push') || + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || + (github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true') steps: - name: checkout uses: actions/checkout@v4 @@ -29,7 +58,11 @@ jobs: windows: runs-on: windows-latest - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name + if: > + (github.event_name == 'push') || + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || + (github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true') steps: - name: Prepare Git for Checkout on Windows run: | From 735e5930eb36db417547825e4771b6ad10fe043c Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 16 Feb 2025 21:45:28 +0100 Subject: [PATCH 0379/1222] Detect additional CMake build failures (#2058) Add include_httplib.cc to the main test executable (already done in Makefile), and add include_windows_h.cc to the main test executable on Windows to test if including windows.h conflicts with httplib.h. --- test/CMakeLists.txt | 2 +- test/include_windows_h.cc | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test/include_windows_h.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a9982b922a..d4e684c9b2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,7 +26,7 @@ endif() find_package(CURL REQUIRED) -add_executable(httplib-test test.cc) +add_executable(httplib-test test.cc include_httplib.cc $<$:include_windows_h.cc>) target_compile_options(httplib-test PRIVATE "$<$:/utf-8;/bigobj>") target_link_libraries(httplib-test PRIVATE httplib GTest::gtest_main CURL::libcurl) gtest_discover_tests(httplib-test) diff --git a/test/include_windows_h.cc b/test/include_windows_h.cc new file mode 100644 index 0000000000..44f541f6e4 --- /dev/null +++ b/test/include_windows_h.cc @@ -0,0 +1,6 @@ +// Test if including windows.h conflicts with httplib.h + +#define WIN32_LEAN_AND_MEAN +#include + +#include From 32bf5c9c09eaefb03974a1d7a39418ab803807ba Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sun, 16 Feb 2025 23:38:41 +0100 Subject: [PATCH 0380/1222] Simplify SSL shutdown (#2059) --- httplib.h | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index cebc1c089c..edea018af9 100644 --- a/httplib.h +++ b/httplib.h @@ -9062,18 +9062,13 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, // Note that it is not always possible to avoid SIGPIPE, this is merely a // best-efforts. if (shutdown_gracefully) { -#ifdef _WIN32 (void)(sock); - SSL_shutdown(ssl); -#else - detail::set_socket_opt_time(sock, SOL_SOCKET, SO_RCVTIMEO, 1, 0); - - auto ret = SSL_shutdown(ssl); - while (ret == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds{100}); - ret = SSL_shutdown(ssl); + // SSL_shutdown() returns 0 on first call (indicating close_notify alert + // sent) and 1 on subsequent call (indicating close_notify alert received) + if (SSL_shutdown(ssl) == 0) { + // Expected to return 1, but even if it doesn't, we free ssl + SSL_shutdown(ssl); } -#endif } std::lock_guard guard(ctx_mutex); From 2996cecee0a947f681571cf0bd205a674d07b220 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 17 Feb 2025 18:14:02 +0100 Subject: [PATCH 0381/1222] Fix code inconsistently formatted and re-format (#2063) * Fix code inconsistently formatted by clang-format * Run clang-format --- example/ssesvr.cc | 3 +-- httplib.h | 17 ++++++++--------- test/test.cc | 11 ++++++----- test/test_proxy.cc | 33 ++++++++++++++++----------------- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/example/ssesvr.cc b/example/ssesvr.cc index 5b0e0b93bf..547b864f3d 100644 --- a/example/ssesvr.cc +++ b/example/ssesvr.cc @@ -12,8 +12,7 @@ using namespace std; class EventDispatcher { public: - EventDispatcher() { - } + EventDispatcher() {} void wait_event(DataSink *sink) { unique_lock lk(m_); diff --git a/httplib.h b/httplib.h index edea018af9..66a2c6f9d2 100644 --- a/httplib.h +++ b/httplib.h @@ -5987,9 +5987,9 @@ inline ssize_t Stream::write(const std::string &s) { namespace detail { -inline void calc_actual_timeout(time_t max_timeout_msec, - time_t duration_msec, time_t timeout_sec, - time_t timeout_usec, time_t &actual_timeout_sec, +inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, + time_t timeout_sec, time_t timeout_usec, + time_t &actual_timeout_sec, time_t &actual_timeout_usec) { auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); @@ -8213,8 +8213,7 @@ inline bool ClientImpl::process_socket( std::function callback) { return detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, max_timeout_msec_, start_time, - std::move(callback)); + write_timeout_usec_, max_timeout_msec_, start_time, std::move(callback)); } inline bool ClientImpl::is_ssl() const { return false; } @@ -9119,8 +9118,8 @@ inline bool process_client_socket_ssl( time_t max_timeout_msec, std::chrono::time_point start_time, T callback) { SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec, - max_timeout_msec, start_time); + write_timeout_sec, write_timeout_usec, max_timeout_msec, + start_time); return callback(strm); } @@ -9735,8 +9734,8 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { auto type = GEN_DNS; - struct in6_addr addr6{}; - struct in_addr addr{}; + struct in6_addr addr6 = {}; + struct in_addr addr = {}; size_t addr_len = 0; #ifndef __MINGW32__ diff --git a/test/test.cc b/test/test.cc index 87f991749e..423762b7a6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3553,9 +3553,11 @@ TEST_F(ServerTest, TooLongRequest) { } TEST_F(ServerTest, AlmostTooLongRequest) { - // test for #2046 - URI length check shouldn't include other content on req line - // URI is max URI length, minus 14 other chars in req line (GET, space, leading /, space, HTTP/1.1) - std::string request = "/" + string(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH - 14, 'A'); + // test for #2046 - URI length check shouldn't include other content on req + // line URI is max URI length, minus 14 other chars in req line (GET, space, + // leading /, space, HTTP/1.1) + std::string request = + "/" + string(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH - 14, 'A'); auto res = cli_.Get(request.c_str()); @@ -8283,7 +8285,6 @@ TEST(MaxTimeoutTest, ContentStream) { Client cli("localhost", PORT); cli.set_max_timeout(std::chrono::milliseconds(timeout)); - { auto start = std::chrono::steady_clock::now(); @@ -8345,7 +8346,7 @@ TEST(MaxTimeoutTest, ContentStreamSSL) { [&, data](size_t offset, size_t length, DataSink &sink) { const size_t DATA_CHUNK_SIZE = 4; const auto &d = *data; - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(std::chrono::seconds(1)); sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); return true; }, diff --git a/test/test_proxy.cc b/test/test_proxy.cc index 88a8ba963d..672bcce247 100644 --- a/test/test_proxy.cc +++ b/test/test_proxy.cc @@ -5,8 +5,7 @@ using namespace std; using namespace httplib; -template -void ProxyTest(T& cli, bool basic) { +template void ProxyTest(T &cli, bool basic) { cli.set_proxy("localhost", basic ? 3128 : 3129); auto res = cli.Get("/httpbin/get"); ASSERT_TRUE(res != nullptr); @@ -38,7 +37,7 @@ TEST(ProxyTest, SSLDigest) { // ---------------------------------------------------------------------------- template -void RedirectProxyText(T& cli, const char *path, bool basic) { +void RedirectProxyText(T &cli, const char *path, bool basic) { cli.set_proxy("localhost", basic ? 3128 : 3129); if (basic) { cli.set_proxy_basic_auth("hello", "world"); @@ -100,8 +99,7 @@ TEST(RedirectTest, YouTubeSSLDigest) { // ---------------------------------------------------------------------------- -template -void BaseAuthTestFromHTTPWatch(T& cli) { +template void BaseAuthTestFromHTTPWatch(T &cli) { cli.set_proxy("localhost", 3128); cli.set_proxy_basic_auth("hello", "world"); @@ -112,11 +110,11 @@ void BaseAuthTestFromHTTPWatch(T& cli) { } { - auto res = - cli.Get("/basic-auth/hello/world", - {make_basic_authentication_header("hello", "world")}); + auto res = cli.Get("/basic-auth/hello/world", + {make_basic_authentication_header("hello", "world")}); ASSERT_TRUE(res != nullptr); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); + EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", + res->body); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -124,7 +122,8 @@ void BaseAuthTestFromHTTPWatch(T& cli) { cli.set_basic_auth("hello", "world"); auto res = cli.Get("/basic-auth/hello/world"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); + EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", + res->body); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -158,8 +157,7 @@ TEST(BaseAuthTest, SSL) { // ---------------------------------------------------------------------------- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -template -void DigestAuthTestFromHTTPWatch(T& cli) { +template void DigestAuthTestFromHTTPWatch(T &cli) { cli.set_proxy("localhost", 3129); cli.set_proxy_digest_auth("hello", "world"); @@ -181,7 +179,8 @@ void DigestAuthTestFromHTTPWatch(T& cli) { for (auto path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res != nullptr); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); + EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", + res->body); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -216,8 +215,7 @@ TEST(DigestAuthTest, NoSSL) { // ---------------------------------------------------------------------------- -template -void KeepAliveTest(T& cli, bool basic) { +template void KeepAliveTest(T &cli, bool basic) { cli.set_proxy("localhost", basic ? 3128 : 3129); if (basic) { cli.set_proxy_basic_auth("hello", "world"); @@ -249,9 +247,10 @@ void KeepAliveTest(T& cli, bool basic) { "/httpbin/digest-auth/auth-int/hello/world/MD5", }; - for (auto path: paths) { + for (auto path : paths) { auto res = cli.Get(path.c_str()); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", res->body); + EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", + res->body); EXPECT_EQ(StatusCode::OK_200, res->status); } } From 574f5ce93e02462feae5c0675aa4ce0fe491ba93 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Mon, 17 Feb 2025 18:14:53 +0100 Subject: [PATCH 0382/1222] Add style check to workflow (#2062) * Add style check to workflow * Add example files to style check --- .github/workflows/test.yaml | 12 ++++++++++++ test/Makefile | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 370ad16346..4fc6339b78 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -24,6 +24,18 @@ env: GTEST_FILTER: ${{ github.event.inputs.gtest_filter || '*' }} jobs: + style-check: + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name + continue-on-error: true + steps: + - name: checkout + uses: actions/checkout@v4 + - name: run style check + run: | + clang-format --version + cd test && make style_check + ubuntu: runs-on: ubuntu-latest if: > diff --git a/test/Makefile b/test/Makefile index 152f9098ef..348bfa2bb8 100644 --- a/test/Makefile +++ b/test/Makefile @@ -28,6 +28,11 @@ TEST_ARGS = gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUP # OSS-Fuzz will define its own value for LIB_FUZZING_ENGINE. LIB_FUZZING_ENGINE ?= standalone_fuzz_target_runner.o +CLANG_FORMAT = clang-format +REALPATH = $(shell which grealpath 2>/dev/null || which realpath 2>/dev/null) +STYLE_CHECK_FILES = $(filter-out httplib.h httplib.cc, \ + $(wildcard example/*.h example/*.cc fuzzing/*.h fuzzing/*.cc *.h *.cc ../httplib.h)) + all : test test_split ./test @@ -45,6 +50,28 @@ test_split : test.cc ../httplib.h httplib.cc Makefile cert.pem check_abi: @./check-shared-library-abi-compatibility.sh +.PHONY: style_check +style_check: $(STYLE_CHECK_FILES) + @for file in $(STYLE_CHECK_FILES); do \ + $(CLANG_FORMAT) $$file > $$file.formatted; \ + if ! diff -u $$file $$file.formatted; then \ + file2=$$($(REALPATH) --relative-to=.. $$file); \ + printf "\n%*s\n" 80 | tr ' ' '#'; \ + printf "##%*s##\n" 76; \ + printf "## %-70s ##\n" "$$file2 not properly formatted. Please run clang-format."; \ + printf "##%*s##\n" 76; \ + printf "%*s\n\n" 80 | tr ' ' '#'; \ + failed=1; \ + fi; \ + rm -f $$file.formatted; \ + done; \ + if [ -n "$$failed" ]; then \ + echo "Style check failed for one or more files. See above for details."; \ + false; \ + else \ + echo "All files are properly formatted."; \ + fi + test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem $(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS) From cdc223019acf93e1c2f15fd7794aea81de85e6ce Mon Sep 17 00:00:00 2001 From: Uros Gaber Date: Mon, 17 Feb 2025 23:24:41 +0100 Subject: [PATCH 0383/1222] server_certificate_verifier extended to reuse built-in verifier (#2064) * server_certificate_verifier extended to reuse built-in verifier * code cleanup and SSLVerifierResponse enum clarification as per @falbrechtskirchinger comment * cleanup * clang-format * change local var verification_status_ declaration to auto * change local var verification_status_ to verification_status * clang-format * clang-format --------- Co-authored-by: UrosG --- httplib.h | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index 66a2c6f9d2..742455a8b3 100644 --- a/httplib.h +++ b/httplib.h @@ -435,6 +435,15 @@ struct scope_exit { } // namespace detail +enum SSLVerifierResponse { + // no decision has been made, use the built-in certificate verifier + NoDecisionMade, + // connection certificate is verified and accepted + CertificateAccepted, + // connection certificate was processed but is rejected + CertificateRejected +}; + enum StatusCode { // Information responses Continue_100 = 100, @@ -1483,7 +1492,8 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier(std::function verifier); + void set_server_certificate_verifier( + std::function verifier); #endif void set_logger(Logger logger); @@ -1600,7 +1610,7 @@ class ClientImpl { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT bool server_certificate_verification_ = true; bool server_hostname_verification_ = true; - std::function server_certificate_verifier_; + std::function server_certificate_verifier_; #endif Logger logger_; @@ -1913,7 +1923,8 @@ class Client { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier(std::function verifier); + void set_server_certificate_verifier( + std::function verifier); #endif void set_logger(Logger logger); @@ -9008,7 +9019,7 @@ inline void ClientImpl::enable_server_hostname_verification(bool enabled) { } inline void ClientImpl::set_server_certificate_verifier( - std::function verifier) { + std::function verifier) { server_certificate_verifier_ = verifier; } #endif @@ -9617,12 +9628,18 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { } if (server_certificate_verification_) { + auto verification_status = SSLVerifierResponse::NoDecisionMade; + if (server_certificate_verifier_) { - if (!server_certificate_verifier_(ssl2)) { - error = Error::SSLServerVerification; - return false; - } - } else { + verification_status = server_certificate_verifier_(ssl2); + } + + if (verification_status == SSLVerifierResponse::CertificateRejected) { + error = Error::SSLServerVerification; + return false; + } + + if (verification_status == SSLVerifierResponse::NoDecisionMade) { verify_result_ = SSL_get_verify_result(ssl2); if (verify_result_ != X509_V_OK) { @@ -10383,7 +10400,7 @@ inline void Client::enable_server_hostname_verification(bool enabled) { } inline void Client::set_server_certificate_verifier( - std::function verifier) { + std::function verifier) { cli_->set_server_certificate_verifier(verifier); } #endif From 6e73a63153e936e753211a5608acf336fb848a37 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 18 Feb 2025 11:23:23 +0100 Subject: [PATCH 0384/1222] Make poll() the default (#2065) * Make poll() the default select() can still be enabled by defining CPPHTTPLIB_USE_SELECT. * Run tests with select() and poll() --- .github/workflows/test.yaml | 42 +++++++++++++++++++++++++++++++++---- CMakeLists.txt | 5 +++++ Dockerfile | 2 +- README.md | 6 +++--- httplib.h | 6 ++++++ test/Makefile | 4 ++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4fc6339b78..463dbf0a1e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,14 +43,21 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') + strategy: + matrix: + select_impl: ['select', 'poll'] steps: - name: checkout uses: actions/checkout@v4 - name: install libraries run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev - name: build and run tests + env: + SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make - name: run fuzz test target + env: + SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make fuzz_test macos: @@ -60,12 +67,19 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true') + strategy: + matrix: + select_impl: ['select', 'poll'] steps: - name: checkout uses: actions/checkout@v4 - name: build and run tests + env: + SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make - name: run fuzz test target + env: + SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make fuzz_test windows: @@ -75,6 +89,9 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true') + strategy: + matrix: + select_impl: ['select', 'poll'] steps: - name: Prepare Git for Checkout on Windows run: | @@ -96,16 +113,33 @@ jobs: choco install openssl - name: Configure CMake with SSL - run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON + run: > + cmake -B build -S . + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake + -DHTTPLIB_TEST=ON + -DHTTPLIB_REQUIRE_OPENSSL=ON + -DHTTPLIB_REQUIRE_ZLIB=ON + -DHTTPLIB_REQUIRE_BROTLI=ON + -DHTTPLIB_USE_SELECT=${{ matrix.select_impl == 'select' && 'ON' || 'OFF' }} - name: Build with with SSL - run: cmake --build build --config Release + run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine /nologo - name: Run tests with SSL run: ctest --output-on-failure --test-dir build -C Release - name: Configure CMake without SSL - run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=OFF -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON + run: > + cmake -B build-no-ssl -S . + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + -DHTTPLIB_TEST=ON + -DHTTPLIB_REQUIRE_OPENSSL=OFF + -DHTTPLIB_REQUIRE_ZLIB=ON + -DHTTPLIB_REQUIRE_BROTLI=ON + -DHTTPLIB_USE_SELECT=${{ matrix.select_impl == 'select' && 'ON' || 'OFF' }} - name: Build without SSL - run: cmake --build build-no-ssl --config Release + run: cmake --build build-no-ssl --config Release -- /v:m /clp:ShowCommandLine /nologo - name: Run tests without SSL run: ctest --output-on-failure --test-dir build-no-ssl -C Release env: diff --git a/CMakeLists.txt b/CMakeLists.txt index 61419c63a8..4ffa31f2c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ * HTTPLIB_REQUIRE_ZLIB (default off) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) + * HTTPLIB_USE_SELECT (default off) choose between select() and poll() * HTTPLIB_COMPILE (default off) * HTTPLIB_INSTALL (default on) * HTTPLIB_TEST (default off) @@ -46,6 +47,7 @@ * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled. * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. * HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled. + * HTTPLIB_IS_USING_SELECT - a bool for if select() is used instead of poll(). * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only. * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include). * HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so). @@ -101,6 +103,7 @@ option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON) +option(HTTPLIB_USE_SELECT "Uses select() instead of poll()." OFF) # Defaults to static library option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) @@ -112,6 +115,7 @@ endif() # Set some variables that are used in-tree and while building based on our options set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}) +set(HTTPLIB_IS_USING_SELECT ${HTTPLIB_USE_SELECT}) # Threads needed for on some systems, and for on Linux set(THREADS_PREFER_PTHREAD_FLAG TRUE) @@ -238,6 +242,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_ZLIB_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> $<$,$,$>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> + $<$:CPPHTTPLIB_USE_SELECT> ) # CMake configuration files installation directory diff --git a/Dockerfile b/Dockerfile index 654845b2ac..4abae1793e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM yhirose4dockerhub/ubuntu-builder AS builder WORKDIR /build COPY httplib.h . COPY docker/main.cc . -RUN g++ -std=c++23 -static -o server -O2 -I. -DCPPHTTPLIB_USE_POLL main.cc && strip server +RUN g++ -std=c++23 -static -o server -O2 -I. main.cc && strip server FROM scratch COPY --from=builder /build/server /server diff --git a/README.md b/README.md index fc4b537f94..2a6cbfab6d 100644 --- a/README.md +++ b/README.md @@ -872,10 +872,10 @@ res->body; // Compressed data ``` -Use `poll` instead of `select` ------------------------------- +Use `select()` instead of `poll()` +---------------------------------- -`select` system call is used as default since it's more widely supported. If you want to let cpp-httplib use `poll` instead, you can do so with `CPPHTTPLIB_USE_POLL`. +cpp-httplib defaults to the widely supported `poll()` system call. If your OS lacks support for `poll()`, define `CPPHTTPLIB_USE_SELECT` to use `select()` instead. Unix Domain Socket Support -------------------------- diff --git a/httplib.h b/httplib.h index 742455a8b3..632dc35851 100644 --- a/httplib.h +++ b/httplib.h @@ -145,6 +145,12 @@ #define CPPHTTPLIB_LISTEN_BACKLOG 5 #endif +#if !defined(CPPHTTPLIB_USE_POLL) && !defined(CPPHTTPLIB_USE_SELECT) +#define CPPHTTPLIB_USE_POLL +#elif defined(CPPHTTPLIB_USE_POLL) && defined(CPPHTTPLIB_USE_SELECT) +#error "CPPHTTPLIB_USE_POLL and CPPHTTPLIB_USE_SELECT are mutually exclusive" +#endif + /* * Headers */ diff --git a/test/Makefile b/test/Makefile index 348bfa2bb8..1089dc6e7e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,10 @@ CXX = clang++ CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address +ifeq ($(SELECT_IMPL),select) + CXXFLAGS += -DCPPHTTPLIB_USE_SELECT +endif + PREFIX ?= $(shell brew --prefix) OPENSSL_DIR = $(PREFIX)/opt/openssl@3 From ada97046a23d525c9de3d461ede5f2803680e620 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 18 Feb 2025 05:51:24 -0500 Subject: [PATCH 0385/1222] Fix misspelled words --- README.md | 6 ++--- httplib.h | 68 ++++++++++++++++++++++++++----------------------------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 2a6cbfab6d..df245dbbd5 100644 --- a/README.md +++ b/README.md @@ -406,11 +406,11 @@ svr.Get("/chunked", [&](const Request& req, Response& res) { ```cpp svr.Get("/content", [&](const Request &req, Response &res) { - res.set_file_content("./path/to/conent.html"); + res.set_file_content("./path/to/content.html"); }); svr.Get("/content", [&](const Request &req, Response &res) { - res.set_file_content("./path/to/conent", "text/html"); + res.set_file_content("./path/to/content", "text/html"); }); ``` @@ -843,7 +843,7 @@ Please see https://github.com/google/brotli for more detail. ### Default `Accept-Encoding` value -The default `Acdcept-Encoding` value contains all possible compression types. So, the following two examples are same. +The default `Accept-Encoding` value contains all possible compression types. So, the following two examples are same. ```c++ res = cli.Get("/resource/foo"); diff --git a/httplib.h b/httplib.h index 632dc35851..b833e18b97 100644 --- a/httplib.h +++ b/httplib.h @@ -894,7 +894,7 @@ class MatcherBase { * Captures parameters in request path and stores them in Request::path_params * * Capture name is a substring of a pattern from : to /. - * The rest of the pattern is matched agains the request path directly + * The rest of the pattern is matched against the request path directly * Parameters are captured starting from the next character after * the end of the last matched static pattern fragment until the next /. * @@ -1124,7 +1124,7 @@ class Server { virtual bool process_and_close_socket(socket_t sock); std::atomic is_running_{false}; - std::atomic is_decommisioned{false}; + std::atomic is_decommissioned{false}; struct MountPointEntry { std::string mount_point; @@ -2586,7 +2586,7 @@ class stream_line_reader { char *fixed_buffer_; const size_t fixed_buffer_size_; size_t fixed_buffer_used_size_ = 0; - std::string glowable_buffer_; + std::string growable_buffer_; }; class mmap { @@ -3022,18 +3022,18 @@ inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, fixed_buffer_size_(fixed_buffer_size) {} inline const char *stream_line_reader::ptr() const { - if (glowable_buffer_.empty()) { + if (growable_buffer_.empty()) { return fixed_buffer_; } else { - return glowable_buffer_.data(); + return growable_buffer_.data(); } } inline size_t stream_line_reader::size() const { - if (glowable_buffer_.empty()) { + if (growable_buffer_.empty()) { return fixed_buffer_used_size_; } else { - return glowable_buffer_.size(); + return growable_buffer_.size(); } } @@ -3044,7 +3044,7 @@ inline bool stream_line_reader::end_with_crlf() const { inline bool stream_line_reader::getline() { fixed_buffer_used_size_ = 0; - glowable_buffer_.clear(); + growable_buffer_.clear(); #ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR char prev_byte = 0; @@ -3082,11 +3082,11 @@ inline void stream_line_reader::append(char c) { fixed_buffer_[fixed_buffer_used_size_++] = c; fixed_buffer_[fixed_buffer_used_size_] = '\0'; } else { - if (glowable_buffer_.empty()) { + if (growable_buffer_.empty()) { assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); - glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); + growable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); } - glowable_buffer_ += c; + growable_buffer_ += c; } } @@ -4259,10 +4259,6 @@ inline bool parse_header(const char *beg, const char *end, T fn) { if (!key_len) { return false; } auto key = std::string(beg, key_end); - // auto val = (case_ignore::equal(key, "Location") || - // case_ignore::equal(key, "Referer")) - // ? std::string(p, end) - // : decode_url(std::string(p, end), false); auto val = std::string(p, end); if (!detail::fields::is_field_value(val)) { return false; } @@ -4401,7 +4397,7 @@ inline bool read_content_chunked(Stream &strm, T &x, assert(chunk_len == 0); - // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked // transfer coding is complete when a chunk with a chunk-size of zero is // received, possibly followed by a trailer section, and finally terminated by // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 @@ -4411,8 +4407,8 @@ inline bool read_content_chunked(Stream &strm, T &x, // to be ok whether the final CRLF exists or not in the chunked data. // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 // - // According to the reference code in RFC 9112, cpp-htpplib now allows - // chuncked transfer coding data without the final CRLF. + // According to the reference code in RFC 9112, cpp-httplib now allows + // chunked transfer coding data without the final CRLF. if (!line_reader.getline()) { return true; } while (strcmp(line_reader.ptr(), "\r\n") != 0) { @@ -5002,7 +4998,7 @@ class MultipartFormDataParser { it = params.find("filename*"); if (it != params.end()) { - // Only allow UTF-8 enconnding... + // Only allow UTF-8 encoding... static const std::regex re_rfc5987_encoding( R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); @@ -5249,7 +5245,7 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, inline bool range_error(Request &req, Response &res) { if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { - ssize_t contant_len = static_cast( + ssize_t content_len = static_cast( res.content_length_ ? res.content_length_ : res.body.size()); ssize_t prev_first_pos = -1; @@ -5269,12 +5265,12 @@ inline bool range_error(Request &req, Response &res) { if (first_pos == -1 && last_pos == -1) { first_pos = 0; - last_pos = contant_len; + last_pos = content_len; } if (first_pos == -1) { - first_pos = contant_len - last_pos; - last_pos = contant_len - 1; + first_pos = content_len - last_pos; + last_pos = content_len - 1; } // NOTE: RFC-9110 '14.1.2. Byte Ranges': @@ -5286,13 +5282,13 @@ inline bool range_error(Request &req, Response &res) { // with a value that is one less than the current length of the selected // representation). // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 - if (last_pos == -1 || last_pos >= contant_len) { - last_pos = contant_len - 1; + if (last_pos == -1 || last_pos >= content_len) { + last_pos = content_len - 1; } // Range must be within content length if (!(0 <= first_pos && first_pos <= last_pos && - last_pos <= contant_len - 1)) { + last_pos <= content_len - 1)) { return true; } @@ -6486,12 +6482,12 @@ inline Server &Server::set_payload_max_length(size_t length) { inline bool Server::bind_to_port(const std::string &host, int port, int socket_flags) { auto ret = bind_internal(host, port, socket_flags); - if (ret == -1) { is_decommisioned = true; } + if (ret == -1) { is_decommissioned = true; } return ret >= 0; } inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { auto ret = bind_internal(host, 0, socket_flags); - if (ret == -1) { is_decommisioned = true; } + if (ret == -1) { is_decommissioned = true; } return ret; } @@ -6505,7 +6501,7 @@ inline bool Server::listen(const std::string &host, int port, inline bool Server::is_running() const { return is_running_; } inline void Server::wait_until_ready() const { - while (!is_running_ && !is_decommisioned) { + while (!is_running_ && !is_decommissioned) { std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } @@ -6517,10 +6513,10 @@ inline void Server::stop() { detail::shutdown_socket(sock); detail::close_socket(sock); } - is_decommisioned = false; + is_decommissioned = false; } -inline void Server::decommission() { is_decommisioned = true; } +inline void Server::decommission() { is_decommissioned = true; } inline bool Server::parse_request_line(const char *s, Request &req) const { auto len = strlen(s); @@ -6879,7 +6875,7 @@ Server::create_server_socket(const std::string &host, int port, inline int Server::bind_internal(const std::string &host, int port, int socket_flags) { - if (is_decommisioned) { return -1; } + if (is_decommissioned) { return -1; } if (!is_valid()) { return -1; } @@ -6906,7 +6902,7 @@ inline int Server::bind_internal(const std::string &host, int port, } inline bool Server::listen_internal() { - if (is_decommisioned) { return false; } + if (is_decommissioned) { return false; } auto ret = true; is_running_ = true; @@ -6930,7 +6926,7 @@ inline bool Server::listen_internal() { #endif #if defined _WIN32 - // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, + // sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT, // OVERLAPPED socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); #elif defined SOCK_CLOEXEC @@ -6972,7 +6968,7 @@ inline bool Server::listen_internal() { task_queue->shutdown(); } - is_decommisioned = !ret; + is_decommissioned = !ret; return ret; } @@ -7594,7 +7590,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #endif if (!is_alive) { - // Attempt to avoid sigpipe by shutting down nongracefully if it seems + // Attempt to avoid sigpipe by shutting down non-gracefully if it seems // like the other side has already closed the connection Also, there // cannot be any requests in flight from other threads since we locked // request_mutex_, so safe to close everything immediately From 321a86d9f2b1b790486105ec948fd4c1e7487b2f Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 18 Feb 2025 05:56:22 -0500 Subject: [PATCH 0386/1222] Add *.dSYM to Makefile clean --- test/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index 1089dc6e7e..d4f10d12f2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -100,5 +100,5 @@ cert.pem: ./gen-certs.sh clean: - rm -rf test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* + rm -rf test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM From dda2e007a02e2aafb10dee05b20cd0d2d4f58fd0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 18 Feb 2025 11:40:50 -0500 Subject: [PATCH 0387/1222] Fixed documentation about Unix Domain Sockt (#2066) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index df245dbbd5..a1416ccac1 100644 --- a/README.md +++ b/README.md @@ -884,7 +884,7 @@ Unix Domain Socket support is available on Linux and macOS. ```c++ // Server -httplib::Server svr("./my-socket.sock"); +httplib::Server svr; svr.set_address_family(AF_UNIX).listen("./my-socket.sock", 80); // Client From d274c0abe577ec75a6f31430e2b9456250dfdc19 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 18 Feb 2025 21:33:32 -0500 Subject: [PATCH 0388/1222] Fix typo --- docker/main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/main.cc b/docker/main.cc index 913275da6e..62d0c74f5f 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -41,7 +41,7 @@ std::string log(auto &req, auto &res) { auto http_referer = "-"; // TODO: auto http_user_agent = req.get_header_value("User-Agent", "-"); - // NOTE: From NGINX defualt access log format + // NOTE: From NGINX default access log format // log_format combined '$remote_addr - $remote_user [$time_local] ' // '"$request" $status $body_bytes_sent ' // '"$http_referer" "$http_user_agent"'; From 2b5d1eea8d7e9f881ac4be04ce31df782b26b6a9 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Wed, 19 Feb 2025 18:47:56 +0100 Subject: [PATCH 0389/1222] build(meson): automatically use poll or select as needed (#2067) Follow-up to 6e73a63153e936e753211a5608acf336fb848a37 --- meson.build | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 9fa1919c8b..4ae18c0fb9 100644 --- a/meson.build +++ b/meson.build @@ -16,17 +16,26 @@ project( meson_version: '>=0.62.0' ) +cxx = meson.get_compiler('cpp') + # Check just in case downstream decides to edit the source # and add a project version version = meson.project_version() if version == 'undefined' - cxx = meson.get_compiler('cpp') version = cxx.get_define('CPPHTTPLIB_VERSION', prefix: '#include ', include_directories: include_directories('.')).strip('"') assert(version != '', 'failed to get version from httplib.h') endif +if cxx.has_function('poll', prefix: '#include ') + # Use poll if present + add_project_arguments('-DCPPHTTPLIB_USE_POLL', language: 'cpp') +else if cxx.has_function('select', prefix: '#include ') + # Use select otherwise + add_project_arguments('-DCPPHTTPLIB_USE_SELECT', language: 'cpp') +endif + deps = [dependency('threads')] args = [] From 5c0135fa5d7e059f5dd4c3a1a8a2d767d30ac491 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 19 Feb 2025 22:20:44 +0100 Subject: [PATCH 0390/1222] Fix typo in meson.build (#2070) --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 4ae18c0fb9..15bc918e5b 100644 --- a/meson.build +++ b/meson.build @@ -31,7 +31,7 @@ endif if cxx.has_function('poll', prefix: '#include ') # Use poll if present add_project_arguments('-DCPPHTTPLIB_USE_POLL', language: 'cpp') -else if cxx.has_function('select', prefix: '#include ') +elif cxx.has_function('select', prefix: '#include ') # Use select otherwise add_project_arguments('-DCPPHTTPLIB_USE_SELECT', language: 'cpp') endif From a4b2c61a65f4ff3d18b3dd24d2abae40f30be7de Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 19 Feb 2025 22:19:02 -0500 Subject: [PATCH 0391/1222] Max timeout test refactoring (#2071) * Simplify code * Adjust threshold --- test/test.cc | 142 ++++++--------------------------------------------- 1 file changed, 15 insertions(+), 127 deletions(-) diff --git a/test/test.cc b/test/test.cc index 423762b7a6..a154441230 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8207,9 +8207,8 @@ TEST(Expect100ContinueTest, ServerClosesConnection) { } #endif -TEST(MaxTimeoutTest, ContentStream) { - Server svr; - +template +inline void max_timeout_test(S &svr, C &cli, time_t timeout, time_t threshold) { svr.Get("/stream", [&](const Request &, Response &res) { auto data = new std::string("01234567890123456789"); @@ -8279,10 +8278,6 @@ TEST(MaxTimeoutTest, ContentStream) { svr.wait_until_ready(); - const time_t timeout = 2000; - const time_t threshold = 200; - - Client cli("localhost", PORT); cli.set_max_timeout(std::chrono::milliseconds(timeout)); { @@ -8334,132 +8329,25 @@ TEST(MaxTimeoutTest, ContentStream) { } } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -TEST(MaxTimeoutTest, ContentStreamSSL) { - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); - - svr.Get("/stream", [&](const Request &, Response &res) { - auto data = new std::string("01234567890123456789"); - - res.set_content_provider( - data->size(), "text/plain", - [&, data](size_t offset, size_t length, DataSink &sink) { - const size_t DATA_CHUNK_SIZE = 4; - const auto &d = *data; - std::this_thread::sleep_for(std::chrono::seconds(1)); - sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE)); - return true; - }, - [data](bool success) { - EXPECT_FALSE(success); - delete data; - }); - }); - - svr.Get("/stream_without_length", [&](const Request &, Response &res) { - auto i = new size_t(0); - - res.set_content_provider( - "text/plain", - [i](size_t, DataSink &sink) { - if (*i < 5) { - std::this_thread::sleep_for(std::chrono::seconds(1)); - sink.write("abcd", 4); - (*i)++; - } else { - sink.done(); - } - return true; - }, - [i](bool success) { - EXPECT_FALSE(success); - delete i; - }); - }); - - svr.Get("/chunked", [&](const Request &, Response &res) { - auto i = new size_t(0); - - res.set_chunked_content_provider( - "text/plain", - [i](size_t, DataSink &sink) { - if (*i < 5) { - std::this_thread::sleep_for(std::chrono::seconds(1)); - sink.os << "abcd"; - (*i)++; - } else { - sink.done(); - } - return true; - }, - [i](bool success) { - EXPECT_FALSE(success); - delete i; - }); - }); +TEST(MaxTimeoutTest, ContentStream) { + time_t timeout = 2000; + time_t threshold = 200; - auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); - auto se = detail::scope_exit([&] { - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); - }); + Server svr; + Client cli("localhost", PORT); + max_timeout_test(svr, cli, timeout, threshold); +} - svr.wait_until_ready(); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(MaxTimeoutTest, ContentStreamSSL) { + time_t timeout = 2000; + time_t threshold = 500; // SSL_shutdown is slow on some operating systems. - const time_t timeout = 2000; - const time_t threshold = 1000; // SSL_shutdown is slow... + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); SSLClient cli("localhost", PORT); cli.enable_server_certificate_verification(false); - cli.set_max_timeout(std::chrono::milliseconds(timeout)); - - { - auto start = std::chrono::steady_clock::now(); - - auto res = cli.Get("/stream"); - - auto elapsed = std::chrono::duration_cast( - std::chrono::steady_clock::now() - start) - .count(); - - ASSERT_FALSE(res); - EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) - << "Timeout exceeded by " << (elapsed - timeout) << "ms"; - } - { - auto start = std::chrono::steady_clock::now(); - - auto res = cli.Get("/stream_without_length"); - - auto elapsed = std::chrono::duration_cast( - std::chrono::steady_clock::now() - start) - .count(); - - ASSERT_FALSE(res); - EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) - << "Timeout exceeded by " << (elapsed - timeout) << "ms"; - } - - { - auto start = std::chrono::steady_clock::now(); - - auto res = cli.Get("/chunked", [&](const char *data, size_t data_length) { - EXPECT_EQ("abcd", string(data, data_length)); - return true; - }); - - auto elapsed = std::chrono::duration_cast( - std::chrono::steady_clock::now() - start) - .count(); - - ASSERT_FALSE(res); - EXPECT_EQ(Error::Read, res.error()); - EXPECT_TRUE(timeout <= elapsed && elapsed < timeout + threshold) - << "Timeout exceeded by " << (elapsed - timeout) << "ms"; - } + max_timeout_test(svr, cli, timeout, threshold); } #endif From 550f728165f5a6b8a578b119769b3bd311fac2b0 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 20 Feb 2025 18:56:39 +0100 Subject: [PATCH 0392/1222] Refactor streams: rename is_* to wait_* for clarity (#2069) - Replace is_readable() with wait_readable() and is_writable() with wait_writable() in the Stream interface. - Implement a new is_readable() function with semantics that more closely reflect its name. It returns immediately whether data is available for reading, without waiting. - Update call sites of is_writable(), removing redundant checks. --- httplib.h | 64 ++++++++++++++++++++--------------- test/fuzzing/server_fuzzer.cc | 4 ++- test/test.cc | 10 +++--- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/httplib.h b/httplib.h index b833e18b97..999a069ed6 100644 --- a/httplib.h +++ b/httplib.h @@ -751,7 +751,8 @@ class Stream { virtual ~Stream() = default; virtual bool is_readable() const = 0; - virtual bool is_writable() const = 0; + virtual bool wait_readable() const = 0; + virtual bool wait_writable() const = 0; virtual ssize_t read(char *ptr, size_t size) = 0; virtual ssize_t write(const char *ptr, size_t size) = 0; @@ -2466,7 +2467,8 @@ class BufferStream final : public Stream { ~BufferStream() override = default; bool is_readable() const override; - bool is_writable() const override; + bool wait_readable() const override; + bool wait_writable() const override; ssize_t read(char *ptr, size_t size) override; ssize_t write(const char *ptr, size_t size) override; void get_remote_ip_and_port(std::string &ip, int &port) const override; @@ -3380,7 +3382,8 @@ class SocketStream final : public Stream { ~SocketStream() override; bool is_readable() const override; - bool is_writable() const override; + bool wait_readable() const override; + bool wait_writable() const override; ssize_t read(char *ptr, size_t size) override; ssize_t write(const char *ptr, size_t size) override; void get_remote_ip_and_port(std::string &ip, int &port) const override; @@ -3416,7 +3419,8 @@ class SSLSocketStream final : public Stream { ~SSLSocketStream() override; bool is_readable() const override; - bool is_writable() const override; + bool wait_readable() const override; + bool wait_writable() const override; ssize_t read(char *ptr, size_t size) override; ssize_t write(const char *ptr, size_t size) override; void get_remote_ip_and_port(std::string &ip, int &port) const override; @@ -4578,7 +4582,7 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, data_sink.write = [&](const char *d, size_t l) -> bool { if (ok) { - if (strm.is_writable() && write_data(strm, d, l)) { + if (write_data(strm, d, l)) { offset += l; } else { ok = false; @@ -4587,10 +4591,10 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, return ok; }; - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); }; while (offset < end_offset && !is_shutting_down()) { - if (!strm.is_writable()) { + if (!strm.wait_writable()) { error = Error::Write; return false; } else if (!content_provider(offset, end_offset - offset, data_sink)) { @@ -4628,17 +4632,17 @@ write_content_without_length(Stream &strm, data_sink.write = [&](const char *d, size_t l) -> bool { if (ok) { offset += l; - if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; } + if (!write_data(strm, d, l)) { ok = false; } } return ok; }; - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); }; data_sink.done = [&](void) { data_available = false; }; while (data_available && !is_shutting_down()) { - if (!strm.is_writable()) { + if (!strm.wait_writable()) { return false; } else if (!content_provider(offset, 0, data_sink)) { return false; @@ -4673,10 +4677,7 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, // Emit chunked response header and footer for each chunk auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!strm.is_writable() || - !write_data(strm, chunk.data(), chunk.size())) { - ok = false; - } + if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; } } } else { ok = false; @@ -4685,7 +4686,7 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, return ok; }; - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + data_sink.is_writable = [&]() -> bool { return strm.wait_writable(); }; auto done_with_trailer = [&](const Headers *trailer) { if (!ok) { return; } @@ -4705,8 +4706,7 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, if (!payload.empty()) { // Emit chunked response header and footer for each chunk auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!strm.is_writable() || - !write_data(strm, chunk.data(), chunk.size())) { + if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; return; } @@ -4738,7 +4738,7 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, }; while (data_available && !is_shutting_down()) { - if (!strm.is_writable()) { + if (!strm.wait_writable()) { error = Error::Write; return false; } else if (!content_provider(offset, 0, data_sink)) { @@ -6029,6 +6029,10 @@ inline SocketStream::SocketStream( inline SocketStream::~SocketStream() = default; inline bool SocketStream::is_readable() const { + return read_buff_off_ < read_buff_content_size_; +} + +inline bool SocketStream::wait_readable() const { if (max_timeout_msec_ <= 0) { return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; } @@ -6041,7 +6045,7 @@ inline bool SocketStream::is_readable() const { return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; } -inline bool SocketStream::is_writable() const { +inline bool SocketStream::wait_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && is_socket_alive(sock_); } @@ -6068,7 +6072,7 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { } } - if (!is_readable()) { return -1; } + if (!wait_readable()) { return -1; } read_buff_off_ = 0; read_buff_content_size_ = 0; @@ -6093,7 +6097,7 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { } inline ssize_t SocketStream::write(const char *ptr, size_t size) { - if (!is_writable()) { return -1; } + if (!wait_writable()) { return -1; } #if defined(_WIN32) && !defined(_WIN64) size = @@ -6124,7 +6128,9 @@ inline time_t SocketStream::duration() const { // Buffer stream implementation inline bool BufferStream::is_readable() const { return true; } -inline bool BufferStream::is_writable() const { return true; } +inline bool BufferStream::wait_readable() const { return true; } + +inline bool BufferStream::wait_writable() const { return true; } inline ssize_t BufferStream::read(char *ptr, size_t size) { #if defined(_MSC_VER) && _MSC_VER < 1910 @@ -9161,6 +9167,10 @@ inline SSLSocketStream::SSLSocketStream( inline SSLSocketStream::~SSLSocketStream() = default; inline bool SSLSocketStream::is_readable() const { + return SSL_pending(ssl_) > 0; +} + +inline bool SSLSocketStream::wait_readable() const { if (max_timeout_msec_ <= 0) { return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; } @@ -9173,7 +9183,7 @@ inline bool SSLSocketStream::is_readable() const { return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; } -inline bool SSLSocketStream::is_writable() const { +inline bool SSLSocketStream::wait_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_); } @@ -9181,7 +9191,7 @@ inline bool SSLSocketStream::is_writable() const { inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (SSL_pending(ssl_) > 0) { return SSL_read(ssl_, ptr, static_cast(size)); - } else if (is_readable()) { + } else if (wait_readable()) { auto ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret < 0) { auto err = SSL_get_error(ssl_, ret); @@ -9195,7 +9205,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { #endif if (SSL_pending(ssl_) > 0) { return SSL_read(ssl_, ptr, static_cast(size)); - } else if (is_readable()) { + } else if (wait_readable()) { std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret >= 0) { return ret; } @@ -9212,7 +9222,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { } inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { - if (is_writable()) { + if (wait_writable()) { auto handle_size = static_cast( std::min(size, (std::numeric_limits::max)())); @@ -9227,7 +9237,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { #else while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { #endif - if (is_writable()) { + if (wait_writable()) { std::this_thread::sleep_for(std::chrono::microseconds{10}); ret = SSL_write(ssl_, ptr, static_cast(handle_size)); if (ret >= 0) { return ret; } diff --git a/test/fuzzing/server_fuzzer.cc b/test/fuzzing/server_fuzzer.cc index b1ba3dbf85..a0f7c0eb83 100644 --- a/test/fuzzing/server_fuzzer.cc +++ b/test/fuzzing/server_fuzzer.cc @@ -25,7 +25,9 @@ class FuzzedStream : public httplib::Stream { bool is_readable() const override { return true; } - bool is_writable() const override { return true; } + bool wait_readable() const override { return true; } + + bool wait_writable() const override { return true; } void get_remote_ip_and_port(std::string &ip, int &port) const override { ip = "127.0.0.1"; diff --git a/test/test.cc b/test/test.cc index a154441230..30d9f8c46a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -156,7 +156,7 @@ TEST_F(UnixSocketTest, abstract) { } #endif -TEST(SocketStream, is_writable_UNIX) { +TEST(SocketStream, wait_writable_UNIX) { int fds[2]; ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); @@ -167,17 +167,17 @@ TEST(SocketStream, is_writable_UNIX) { }; asSocketStream(fds[0], [&](Stream &s0) { EXPECT_EQ(s0.socket(), fds[0]); - EXPECT_TRUE(s0.is_writable()); + EXPECT_TRUE(s0.wait_writable()); EXPECT_EQ(0, close(fds[1])); - EXPECT_FALSE(s0.is_writable()); + EXPECT_FALSE(s0.wait_writable()); return true; }); EXPECT_EQ(0, close(fds[0])); } -TEST(SocketStream, is_writable_INET) { +TEST(SocketStream, wait_writable_INET) { sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; @@ -212,7 +212,7 @@ TEST(SocketStream, is_writable_INET) { }; asSocketStream(disconnected_svr_sock, [&](Stream &ss) { EXPECT_EQ(ss.socket(), disconnected_svr_sock); - EXPECT_FALSE(ss.is_writable()); + EXPECT_FALSE(ss.wait_writable()); return true; }); From b944f942ee842759d12f7d76c95eee37208dcfcc Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 20 Feb 2025 18:59:38 +0100 Subject: [PATCH 0393/1222] Correct default thread pool size in README.md (#2077) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a1416ccac1..fcdae7e053 100644 --- a/README.md +++ b/README.md @@ -462,7 +462,7 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e ### Default thread pool support -`ThreadPool` is used as a **default** task queue, and the default thread count is 8, or `std::thread::hardware_concurrency()`. You can change it with `CPPHTTPLIB_THREAD_POOL_COUNT`. +`ThreadPool` is used as the **default** task queue, with a default thread count of 8 or `std::thread::hardware_concurrency() - 1`, whichever is greater. You can change it with `CPPHTTPLIB_THREAD_POOL_COUNT`. If you want to set the thread count at runtime, there is no convenient way... But here is how. From 22d90c29b47cb318febeefad96cddc6a99b00366 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 21 Feb 2025 00:51:35 +0100 Subject: [PATCH 0394/1222] Remove `select()` and use `poll()` (#2078) * Revert "Fix typo in meson.build (#2070)" This reverts commit 5c0135fa5d7e059f5dd4c3a1a8a2d767d30ac491. * Revert "build(meson): automatically use poll or select as needed (#2067)" This reverts commit 2b5d1eea8d7e9f881ac4be04ce31df782b26b6a9. * Revert "Make poll() the default (#2065)" This reverts commit 6e73a63153e936e753211a5608acf336fb848a37. * Remove select() and use poll() --- .github/workflows/test.yaml | 42 ++----------------- CMakeLists.txt | 5 --- README.md | 5 --- httplib.h | 84 +------------------------------------ meson.build | 11 +---- test/Makefile | 4 -- 6 files changed, 7 insertions(+), 144 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 463dbf0a1e..4fc6339b78 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,21 +43,14 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') - strategy: - matrix: - select_impl: ['select', 'poll'] steps: - name: checkout uses: actions/checkout@v4 - name: install libraries run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev - name: build and run tests - env: - SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make - name: run fuzz test target - env: - SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make fuzz_test macos: @@ -67,19 +60,12 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true') - strategy: - matrix: - select_impl: ['select', 'poll'] steps: - name: checkout uses: actions/checkout@v4 - name: build and run tests - env: - SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make - name: run fuzz test target - env: - SELECT_IMPL: ${{ matrix.select_impl }} run: cd test && make fuzz_test windows: @@ -89,9 +75,6 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true') - strategy: - matrix: - select_impl: ['select', 'poll'] steps: - name: Prepare Git for Checkout on Windows run: | @@ -113,33 +96,16 @@ jobs: choco install openssl - name: Configure CMake with SSL - run: > - cmake -B build -S . - -DCMAKE_BUILD_TYPE=Release - -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake - -DHTTPLIB_TEST=ON - -DHTTPLIB_REQUIRE_OPENSSL=ON - -DHTTPLIB_REQUIRE_ZLIB=ON - -DHTTPLIB_REQUIRE_BROTLI=ON - -DHTTPLIB_USE_SELECT=${{ matrix.select_impl == 'select' && 'ON' || 'OFF' }} + run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON - name: Build with with SSL - run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine /nologo + run: cmake --build build --config Release - name: Run tests with SSL run: ctest --output-on-failure --test-dir build -C Release - name: Configure CMake without SSL - run: > - cmake -B build-no-ssl -S . - -DCMAKE_BUILD_TYPE=Release - -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - -DHTTPLIB_TEST=ON - -DHTTPLIB_REQUIRE_OPENSSL=OFF - -DHTTPLIB_REQUIRE_ZLIB=ON - -DHTTPLIB_REQUIRE_BROTLI=ON - -DHTTPLIB_USE_SELECT=${{ matrix.select_impl == 'select' && 'ON' || 'OFF' }} + run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=OFF -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON - name: Build without SSL - run: cmake --build build-no-ssl --config Release -- /v:m /clp:ShowCommandLine /nologo + run: cmake --build build-no-ssl --config Release - name: Run tests without SSL run: ctest --output-on-failure --test-dir build-no-ssl -C Release env: diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ffa31f2c3..61419c63a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,6 @@ * HTTPLIB_REQUIRE_ZLIB (default off) * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) - * HTTPLIB_USE_SELECT (default off) choose between select() and poll() * HTTPLIB_COMPILE (default off) * HTTPLIB_INSTALL (default on) * HTTPLIB_TEST (default off) @@ -47,7 +46,6 @@ * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled. * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. * HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled. - * HTTPLIB_IS_USING_SELECT - a bool for if select() is used instead of poll(). * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only. * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include). * HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so). @@ -103,7 +101,6 @@ option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON) -option(HTTPLIB_USE_SELECT "Uses select() instead of poll()." OFF) # Defaults to static library option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) @@ -115,7 +112,6 @@ endif() # Set some variables that are used in-tree and while building based on our options set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}) -set(HTTPLIB_IS_USING_SELECT ${HTTPLIB_USE_SELECT}) # Threads needed for on some systems, and for on Linux set(THREADS_PREFER_PTHREAD_FLAG TRUE) @@ -242,7 +238,6 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_ZLIB_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> $<$,$,$>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> - $<$:CPPHTTPLIB_USE_SELECT> ) # CMake configuration files installation directory diff --git a/README.md b/README.md index fcdae7e053..aed19ca6c4 100644 --- a/README.md +++ b/README.md @@ -872,11 +872,6 @@ res->body; // Compressed data ``` -Use `select()` instead of `poll()` ----------------------------------- - -cpp-httplib defaults to the widely supported `poll()` system call. If your OS lacks support for `poll()`, define `CPPHTTPLIB_USE_SELECT` to use `select()` instead. - Unix Domain Socket Support -------------------------- diff --git a/httplib.h b/httplib.h index 999a069ed6..6296e2de82 100644 --- a/httplib.h +++ b/httplib.h @@ -145,12 +145,6 @@ #define CPPHTTPLIB_LISTEN_BACKLOG 5 #endif -#if !defined(CPPHTTPLIB_USE_POLL) && !defined(CPPHTTPLIB_USE_SELECT) -#define CPPHTTPLIB_USE_POLL -#elif defined(CPPHTTPLIB_USE_POLL) && defined(CPPHTTPLIB_USE_SELECT) -#error "CPPHTTPLIB_USE_POLL and CPPHTTPLIB_USE_SELECT are mutually exclusive" -#endif - /* * Headers */ @@ -200,9 +194,7 @@ using ssize_t = long; using socket_t = SOCKET; using socklen_t = int; -#ifdef CPPHTTPLIB_USE_POLL #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) -#endif #else // not _WIN32 @@ -222,16 +214,11 @@ using socklen_t = int; #ifdef __linux__ #include #endif +#include #include -#ifdef CPPHTTPLIB_USE_POLL #include -#endif -#include #include #include -#ifndef __VMS -#include -#endif #include #include #include @@ -3267,7 +3254,6 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, template inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL struct pollfd pfd; pfd.fd = sock; pfd.events = (Read ? POLLIN : POLLOUT); @@ -3275,25 +3261,6 @@ inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { auto timeout = static_cast(sec * 1000 + usec / 1000); return handle_EINTR([&]() { return poll(&pfd, 1, timeout); }); -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return -1; } -#endif - - fd_set fds, *rfds, *wfds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - rfds = (Read ? &fds : nullptr); - wfds = (Read ? nullptr : &fds); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - return handle_EINTR([&]() { - return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); - }); -#endif } inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { @@ -3306,7 +3273,6 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL struct pollfd pfd_read; pfd_read.fd = sock; pfd_read.events = POLLIN | POLLOUT; @@ -3327,38 +3293,6 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, } return Error::Connection; -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return Error::Connection; } -#endif - - fd_set fdsr; - FD_ZERO(&fdsr); - FD_SET(sock, &fdsr); - - auto fdsw = fdsr; - auto fdse = fdsr; - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - auto ret = handle_EINTR([&]() { - return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); - }); - - if (ret == 0) { return Error::ConnectionTimeout; } - - if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } - return Error::Connection; -#endif } inline bool is_socket_alive(socket_t sock) { @@ -7208,20 +7142,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr, res.version = "HTTP/1.1"; res.headers = default_headers_; -#ifdef _WIN32 - // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). -#else -#ifndef CPPHTTPLIB_USE_POLL - // Socket file descriptor exceeded FD_SETSIZE... - if (strm.socket() >= FD_SETSIZE) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::InternalServerError_500; - return write_response(strm, close_connection, req, res); - } -#endif -#endif - // Request line and headers if (!parse_request_line(line_reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { @@ -10456,7 +10376,7 @@ inline SSL_CTX *Client::ssl_context() const { } // namespace httplib -#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL) +#ifdef _WIN32 #undef poll #endif diff --git a/meson.build b/meson.build index 15bc918e5b..9fa1919c8b 100644 --- a/meson.build +++ b/meson.build @@ -16,26 +16,17 @@ project( meson_version: '>=0.62.0' ) -cxx = meson.get_compiler('cpp') - # Check just in case downstream decides to edit the source # and add a project version version = meson.project_version() if version == 'undefined' + cxx = meson.get_compiler('cpp') version = cxx.get_define('CPPHTTPLIB_VERSION', prefix: '#include ', include_directories: include_directories('.')).strip('"') assert(version != '', 'failed to get version from httplib.h') endif -if cxx.has_function('poll', prefix: '#include ') - # Use poll if present - add_project_arguments('-DCPPHTTPLIB_USE_POLL', language: 'cpp') -elif cxx.has_function('select', prefix: '#include ') - # Use select otherwise - add_project_arguments('-DCPPHTTPLIB_USE_SELECT', language: 'cpp') -endif - deps = [dependency('threads')] args = [] diff --git a/test/Makefile b/test/Makefile index d4f10d12f2..8b061b4a12 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,10 +1,6 @@ CXX = clang++ CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address -ifeq ($(SELECT_IMPL),select) - CXXFLAGS += -DCPPHTTPLIB_USE_SELECT -endif - PREFIX ?= $(shell brew --prefix) OPENSSL_DIR = $(PREFIX)/opt/openssl@3 From ebe7efa1cce4af45012e476f39ae8f6ccfdee610 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 21 Feb 2025 02:57:18 +0100 Subject: [PATCH 0395/1222] Parallelize testing with/without SSL on Windows & set concurrency group (#2079) * Parallelize testing with/without SSL on Windows * Set concurrency group in workflows --- .github/workflows/abidiff.yaml | 4 +++ .github/workflows/cifuzz.yaml | 6 +++++ .github/workflows/test.yaml | 45 ++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/.github/workflows/abidiff.yaml b/.github/workflows/abidiff.yaml index 215d63a4f3..186e4fcc8b 100644 --- a/.github/workflows/abidiff.yaml +++ b/.github/workflows/abidiff.yaml @@ -5,6 +5,10 @@ name: abidiff on: [push, pull_request] +concurrency: + group: ${{ github.workflow }}-${{ github.ref || github.run_id }} + cancel-in-progress: true + defaults: run: shell: sh diff --git a/.github/workflows/cifuzz.yaml b/.github/workflows/cifuzz.yaml index 43e1de3a39..422b58da7a 100644 --- a/.github/workflows/cifuzz.yaml +++ b/.github/workflows/cifuzz.yaml @@ -1,5 +1,11 @@ name: CIFuzz + on: [pull_request] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref || github.run_id }} + cancel-in-progress: true + jobs: Fuzzing: runs-on: ubuntu-latest diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4fc6339b78..8935f81dc9 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,6 +20,10 @@ on: type: boolean default: true +concurrency: + group: ${{ github.workflow }}-${{ github.ref || github.run_id }} + cancel-in-progress: true + env: GTEST_FILTER: ${{ github.event.inputs.gtest_filter || '*' }} @@ -75,6 +79,14 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_windows == 'true') + strategy: + matrix: + config: + - with_ssl: false + name: without SSL + - with_ssl: true + name: with SSL + name: windows ${{ matrix.config.name }} steps: - name: Prepare Git for Checkout on Windows run: | @@ -90,24 +102,25 @@ jobs: core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); - name: Setup msbuild on windows uses: microsoft/setup-msbuild@v2 - - name: Install libraries - run: | - vcpkg install gtest curl zlib brotli - choco install openssl - - - name: Configure CMake with SSL - run: cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=ON -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON - - name: Build with with SSL - run: cmake --build build --config Release - - name: Run tests with SSL + - name: Install vcpkg dependencies + run: vcpkg install gtest curl zlib brotli + - name: Install OpenSSL + if: ${{ matrix.config.with_ssl }} + run: choco install openssl + - name: Configure CMake ${{ matrix.config.name }} + run: > + cmake -B build -S . + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake + -DHTTPLIB_TEST=ON + -DHTTPLIB_REQUIRE_ZLIB=ON + -DHTTPLIB_REQUIRE_BROTLI=ON + -DHTTPLIB_REQUIRE_OPENSSL=${{ matrix.config.with_ssl && 'ON' || 'OFF' }} + - name: Build ${{ matrix.config.name }} + run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine + - name: Run tests ${{ matrix.config.name }} run: ctest --output-on-failure --test-dir build -C Release - - name: Configure CMake without SSL - run: cmake -B build-no-ssl -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_REQUIRE_OPENSSL=OFF -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON - - name: Build without SSL - run: cmake --build build-no-ssl --config Release - - name: Run tests without SSL - run: ctest --output-on-failure --test-dir build-no-ssl -C Release env: VCPKG_ROOT: "C:/vcpkg" VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite" From 71ba7e7b1b328fe0de6cfbd3e94e5e0ddd4b4073 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 20 Feb 2025 23:45:21 -0500 Subject: [PATCH 0396/1222] Fix #2068 (#2080) * Fix #2068 * Add unit test --- httplib.h | 10 +++++++ test/test.cc | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/httplib.h b/httplib.h index 6296e2de82..60da33f34e 100644 --- a/httplib.h +++ b/httplib.h @@ -7333,6 +7333,16 @@ inline ClientImpl::ClientImpl(const std::string &host, int port, client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { + // Wait until all the requests in flight are handled. + size_t retry_count = 10; + while (retry_count-- > 0) { + { + std::lock_guard guard(socket_mutex_); + if (socket_requests_in_flight_ == 0) { break; } + } + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } + std::lock_guard guard(socket_mutex_); shutdown_socket(socket_); close_socket(socket_); diff --git a/test/test.cc b/test/test.cc index 30d9f8c46a..04eb2ea70c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8351,3 +8351,86 @@ TEST(MaxTimeoutTest, ContentStreamSSL) { max_timeout_test(svr, cli, timeout, threshold); } #endif + +class EventDispatcher { +public: + EventDispatcher() {} + + void wait_event(DataSink *sink) { + unique_lock lk(m_); + int id = id_; + cv_.wait(lk, [&] { return cid_ == id; }); + sink->write(message_.data(), message_.size()); + } + + void send_event(const string &message) { + lock_guard lk(m_); + cid_ = id_++; + message_ = message; + cv_.notify_all(); + } + +private: + mutex m_; + condition_variable cv_; + atomic_int id_{0}; + atomic_int cid_{-1}; + string message_; +}; + +TEST(ClientInThreadTest, Issue2068) { + EventDispatcher ed; + + Server svr; + svr.Get("/event1", [&](const Request & /*req*/, Response &res) { + res.set_chunked_content_provider("text/event-stream", + [&](size_t /*offset*/, DataSink &sink) { + ed.wait_event(&sink); + return true; + }); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); + + svr.wait_until_ready(); + + thread event_thread([&] { + int id = 0; + while (svr.is_running()) { + this_thread::sleep_for(chrono::milliseconds(500)); + + std::stringstream ss; + ss << "data: " << id << "\n\n"; + ed.send_event(ss.str()); + id++; + } + }); + + auto se = detail::scope_exit([&] { + svr.stop(); + + listen_thread.join(); + event_thread.join(); + + ASSERT_FALSE(svr.is_running()); + }); + + { + auto client = detail::make_unique(HOST, PORT); + client->set_read_timeout(std::chrono::minutes(10)); + + std::atomic stop{false}; + + std::thread t([&] { + client->Get("/event1", + [&](const char *, size_t) -> bool { return !stop; }); + }); + + std::this_thread::sleep_for(std::chrono::seconds(2)); + stop = true; + client->stop(); + client.reset(); + + t.join(); + } +} From ee0bee39072529051628f4c13d7c74f83416d54f Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 6 Mar 2025 13:17:05 +0100 Subject: [PATCH 0397/1222] Fix HttpWatch tests (#2089) HttpWatch changed the formatting of the returned JSON. Normalize it by removing all whitespace. --- test/test.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/test.cc b/test/test.cc index 04eb2ea70c..bf1325e855 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1361,6 +1361,14 @@ TEST(CancelTest, WithCancelLargePayloadDelete) { EXPECT_EQ(Error::Canceled, res.error()); } +static std::string remove_whitespace(const std::string &input) { + std::string output; + output.reserve(input.size()); + std::copy_if(input.begin(), input.end(), std::back_inserter(output), + [](unsigned char c) { return !std::isspace(c); }); + return output; +} + TEST(BaseAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpbin.org"; @@ -1388,8 +1396,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { auto res = cli.Get(path, {make_basic_authentication_header("hello", "world")}); ASSERT_TRUE(res); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ("{\"authenticated\":true,\"user\":\"hello\"}", + remove_whitespace(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -1397,8 +1405,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { cli.set_basic_auth("hello", "world"); auto res = cli.Get(path); ASSERT_TRUE(res); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ("{\"authenticated\":true,\"user\":\"hello\"}", + remove_whitespace(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -1454,8 +1462,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { for (const auto &path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ("{\"authenticated\":true,\"user\":\"hello\"}", + remove_whitespace(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } From f2928d71525dc22043df02cae9cc25472b1440fb Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 6 Mar 2025 17:55:11 +0100 Subject: [PATCH 0398/1222] Switch redirect tests to httpbingo.org (#2090) Redirect tests fail using httpbin.org or nghttp2.org/httpbin. The location header value contains a string representation of a Python byte string (e.g., b'http://www.google.com/'), which results in a 404 error. --- test/test.cc | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/test/test.cc b/test/test.cc index bf1325e855..71c311b529 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1594,36 +1594,40 @@ TEST(YahooRedirectTest, Redirect_Online) { EXPECT_EQ("https://www.yahoo.com/", res->location); } +// Previously "nghttp2.org" "/httpbin/redirect-to" +#define REDIR_HOST "httpbingo.org" +#define REDIR_PATH "/redirect-to" + TEST(HttpsToHttpRedirectTest, Redirect_Online) { - SSLClient cli("nghttp2.org"); + SSLClient cli(REDIR_HOST); cli.set_follow_location(true); - auto res = cli.Get( - "/httpbin/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302"); + auto res = + cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302"); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest2, Redirect_Online) { - SSLClient cli("nghttp2.org"); + SSLClient cli(REDIR_HOST); cli.set_follow_location(true); Params params; - params.emplace("url", "http://www.google.com"); + params.emplace("url", "http://example.com"); params.emplace("status_code", "302"); - auto res = cli.Get("/httpbin/redirect-to", params, Headers{}); + auto res = cli.Get(REDIR_PATH, params, Headers{}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest3, Redirect_Online) { - SSLClient cli("nghttp2.org"); + SSLClient cli(REDIR_HOST); cli.set_follow_location(true); Params params; - params.emplace("url", "http://www.google.com"); + params.emplace("url", "http://example.com"); - auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{}); + auto res = cli.Get(REDIR_PATH "?status_code=302", params, Headers{}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -6817,38 +6821,41 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) { } #endif +// Previously "https://nghttp2.org" "/httpbin/redirect-to" +#undef REDIR_HOST // Silence compiler warning +#define REDIR_HOST "https://httpbingo.org" + TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) { - Client cli("https://nghttp2.org"); + Client cli(REDIR_HOST); cli.set_follow_location(true); auto res = - cli.Get("/httpbin/" - "redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302"); + cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302"); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) { - Client cli("https://nghttp2.org"); + Client cli(REDIR_HOST); cli.set_follow_location(true); Params params; - params.emplace("url", "http://www.google.com"); + params.emplace("url", "http://example.com"); params.emplace("status_code", "302"); - auto res = cli.Get("/httpbin/redirect-to", params, Headers{}); + auto res = cli.Get(REDIR_PATH, params, Headers{}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) { - Client cli("https://nghttp2.org"); + Client cli(REDIR_HOST); cli.set_follow_location(true); Params params; - params.emplace("url", "http://www.google.com"); + params.emplace("url", "http://example.com"); - auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{}); + auto res = cli.Get(REDIR_PATH "?status_code=302", params, Headers{}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); } From 85b5cdd78d9b0dd14e35d5eaf88c39055e191928 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 6 Mar 2025 17:58:55 +0100 Subject: [PATCH 0399/1222] Move detail::read_file() to test/test.cc (#2092) The unit test code is the only user of the function. read_file() now throws an exception if the file isn't found. --- httplib.h | 12 ------------ test/test.cc | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 60da33f34e..3b3beabe65 100644 --- a/httplib.h +++ b/httplib.h @@ -240,7 +240,6 @@ using socket_t = int; #include #include #include -#include #include #include #include @@ -2387,8 +2386,6 @@ std::string encode_query_param(const std::string &value); std::string decode_url(const std::string &s, bool convert_plus_to_space); -void read_file(const std::string &path, std::string &out); - std::string trim_copy(const std::string &s); void divide( @@ -2916,15 +2913,6 @@ inline std::string decode_url(const std::string &s, return result; } -inline void read_file(const std::string &path, std::string &out) { - std::ifstream fs(path, std::ios_base::binary); - fs.seekg(0, std::ios_base::end); - auto size = fs.tellg(); - fs.seekg(0); - out.resize(static_cast(size)); - fs.read(&out[0], static_cast(size)); -} - inline std::string file_extension(const std::string &path) { std::smatch m; static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); diff --git a/test/test.cc b/test/test.cc index 71c311b529..fce0545932 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -59,6 +60,16 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files, #endif } +static void read_file(const std::string &path, std::string &out) { + std::ifstream fs(path, std::ios_base::binary); + if (!fs) throw std::runtime_error("File not found: " + path); + fs.seekg(0, std::ios_base::end); + auto size = fs.tellg(); + fs.seekg(0); + out.resize(static_cast(size)); + fs.read(&out[0], static_cast(size)); +} + #ifndef _WIN32 class UnixSocketTest : public ::testing::Test { protected: @@ -729,7 +740,7 @@ TEST(ChunkedEncodingTest, FromHTTPWatch_Online) { ASSERT_TRUE(res); std::string out; - detail::read_file("./image.jpg", out); + read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, res->body); @@ -782,7 +793,7 @@ TEST(ChunkedEncodingTest, WithContentReceiver_Online) { ASSERT_TRUE(res); std::string out; - detail::read_file("./image.jpg", out); + read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, body); @@ -814,7 +825,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { ASSERT_TRUE(res); std::string out; - detail::read_file("./image.jpg", out); + read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, body); @@ -6176,7 +6187,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) { TEST(SSLClientTest, ServerCertificateVerification5_Online) { std::string cert; - detail::read_file(CA_CERT_FILE, cert); + read_file(CA_CERT_FILE, cert); SSLClient cli("google.com"); cli.load_ca_cert_store(cert.data(), cert.size()); From 5a1ecc3958f4412073a22e8b23f4d9f83e442dc0 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Fri, 7 Mar 2025 03:17:41 +0100 Subject: [PATCH 0400/1222] Run 32-bit compiled unit tests on Ubuntu (#2095) --- .github/workflows/test.yaml | 21 ++++++++++++++++++--- test/Makefile | 4 +++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8935f81dc9..02f46beff6 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -47,15 +47,30 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') + strategy: + matrix: + config: + - arch_flags: -m32 + arch_suffix: :i386 + name: (32-bit) + - arch_flags: + arch_suffix: + name: (64-bit) + name: ubuntu ${{ matrix.config.name }} steps: - name: checkout uses: actions/checkout@v4 - name: install libraries - run: sudo apt-get update && sudo apt-get install -y libbrotli-dev libcurl4-openssl-dev + run: | + sudo dpkg --add-architecture i386 + sudo apt-get update + sudo apt-get install -y libc6-dev${{ matrix.config.arch_suffix }} libstdc++-13-dev${{ matrix.config.arch_suffix }} \ + libssl-dev${{ matrix.config.arch_suffix }} libcurl4-openssl-dev${{ matrix.config.arch_suffix }} \ + zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} - name: build and run tests - run: cd test && make + run: cd test && make EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}" - name: run fuzz test target - run: cd test && make fuzz_test + run: cd test && make EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}" fuzz_test macos: runs-on: macos-latest diff --git a/test/Makefile b/test/Makefile index 8b061b4a12..46c4a9104b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ CXX = clang++ -CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address +CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address PREFIX ?= $(shell brew --prefix) @@ -41,6 +41,7 @@ proxy : test_proxy test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS) + @file $@ # Note: The intention of test_split is to verify that it works to compile and # link the split httplib.h, so there is normally no need to execute it. @@ -83,6 +84,7 @@ fuzz_test: server_fuzzer # Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use. server_fuzzer : fuzzing/server_fuzzer.cc ../httplib.h standalone_fuzz_target_runner.o $(CXX) -o $@ -I.. $(CXXFLAGS) $< $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread + @file $@ # Standalone fuzz runner, which just reads inputs from fuzzing/corpus/ dir and # feeds it to server_fuzzer. From 48084d55f22905b308a1e58077f9138593da1d4c Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 10 Mar 2025 23:31:51 -0400 Subject: [PATCH 0401/1222] Fix #2096 --- httplib.h | 3 +++ test/test.cc | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/httplib.h b/httplib.h index 3b3beabe65..dfa6c0e821 100644 --- a/httplib.h +++ b/httplib.h @@ -4170,6 +4170,9 @@ inline bool parse_header(const char *beg, const char *end, T fn) { p++; } + auto name = std::string(beg, p); + if (!detail::fields::is_field_name(name)) { return false; } + if (p == end) { return false; } auto key_end = p; diff --git a/test/test.cc b/test/test.cc index fce0545932..81a5e33a6c 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5156,6 +5156,14 @@ TEST(ServerRequestParsingTest, InvalidFieldValueContains_LF) { EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); } +TEST(ServerRequestParsingTest, InvalidFieldNameContains_PreceedingSpaces) { + std::string out; + std::string request( + "GET /header_field_value_check HTTP/1.1\r\n Test: val\r\n\r\n", 55); + test_raw_request(request, &out); + EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); +} + TEST(ServerRequestParsingTest, EmptyFieldValue) { std::string out; From 37399af9960eb0fef6f5270d60c14903648629d9 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Tue, 11 Mar 2025 18:10:30 +0100 Subject: [PATCH 0402/1222] build(meson): copy MountTest.MultibytesPathName files (#2098) Fixes the test suite in Meson. --- test/www/meson.build | 1 + .../www/\346\227\245\346\234\254\350\252\236Dir/meson.build" | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 "test/www/\346\227\245\346\234\254\350\252\236Dir/meson.build" diff --git a/test/www/meson.build b/test/www/meson.build index ed3d357716..be2b047b79 100644 --- a/test/www/meson.build +++ b/test/www/meson.build @@ -5,3 +5,4 @@ configure_file(input: 'empty_file', output: 'empty_file', copy: true) configure_file(input: 'file', output: 'file', copy: true) subdir('dir') +subdir('日本語Dir') diff --git "a/test/www/\346\227\245\346\234\254\350\252\236Dir/meson.build" "b/test/www/\346\227\245\346\234\254\350\252\236Dir/meson.build" new file mode 100644 index 0000000000..4cd4150e16 --- /dev/null +++ "b/test/www/\346\227\245\346\234\254\350\252\236Dir/meson.build" @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2025 Andrea Pappacoda +# SPDX-License-Identifier: MIT + +configure_file(input: '日本語File.txt', output: '日本語File.txt', copy: true) From a9ba0a4dff624523ed712f5d73b9ac3e563fb029 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 12 Mar 2025 17:10:02 +0100 Subject: [PATCH 0403/1222] Remove SSLInit (#2102) Quote: "As of version 1.1.0 OpenSSL will automatically allocate all resources that it needs so no explicit initialisation is required." --- httplib.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/httplib.h b/httplib.h index dfa6c0e821..f6b758242d 100644 --- a/httplib.h +++ b/httplib.h @@ -9063,14 +9063,6 @@ inline bool process_client_socket_ssl( return callback(strm); } -class SSLInit { -public: - SSLInit() { - OPENSSL_init_ssl( - OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); - } -}; - // SSL socket stream implementation inline SSLSocketStream::SSLSocketStream( socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, @@ -9191,8 +9183,6 @@ inline time_t SSLSocketStream::duration() const { .count(); } -static SSLInit sslinit_; - } // namespace detail // SSL HTTP server implementation From 2f39723d08eab78aec1e8d167b24bf127ddd067c Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 12 Mar 2025 17:12:03 +0100 Subject: [PATCH 0404/1222] Wrap poll()/WSAPoll() in a function and build compiled library on Windows (#2107) * Wrap poll()/WSAPoll() in a function Instead of using a macro for poll() on Windows, which breaks when the implementation is compiled separately, add a detail::poll_wrapper() function that dispatches to either ::poll() or ::WSAPoll(). * Build compiled library on Windows --- .github/workflows/test.yaml | 10 ++++++++++ httplib.h | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 02f46beff6..7763d379f5 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -98,9 +98,17 @@ jobs: matrix: config: - with_ssl: false + compiled: false + run_tests: true name: without SSL - with_ssl: true + compiled: false + run_tests: true name: with SSL + - with_ssl: false + compiled: true + run_tests: false + name: compiled name: windows ${{ matrix.config.name }} steps: - name: Prepare Git for Checkout on Windows @@ -128,12 +136,14 @@ jobs: -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON + -DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }} -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON -DHTTPLIB_REQUIRE_OPENSSL=${{ matrix.config.with_ssl && 'ON' || 'OFF' }} - name: Build ${{ matrix.config.name }} run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine - name: Run tests ${{ matrix.config.name }} + if: ${{ matrix.config.run_tests }} run: ctest --output-on-failure --test-dir build -C Release env: diff --git a/httplib.h b/httplib.h index f6b758242d..e009971435 100644 --- a/httplib.h +++ b/httplib.h @@ -192,9 +192,9 @@ using ssize_t = long; #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 #endif +using nfds_t = unsigned long; using socket_t = SOCKET; using socklen_t = int; -#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) #else // not _WIN32 @@ -3240,6 +3240,14 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, }); } +inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { +#ifdef _WIN32 + return ::WSAPoll(fds, nfds, timeout); +#else + return ::poll(fds, nfds, timeout); +#endif +} + template inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { struct pollfd pfd; @@ -3248,7 +3256,7 @@ inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { auto timeout = static_cast(sec * 1000 + usec / 1000); - return handle_EINTR([&]() { return poll(&pfd, 1, timeout); }); + return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); }); } inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { @@ -3267,7 +3275,8 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, auto timeout = static_cast(sec * 1000 + usec / 1000); - auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); + auto poll_res = + handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); }); if (poll_res == 0) { return Error::ConnectionTimeout; } @@ -10367,8 +10376,4 @@ inline SSL_CTX *Client::ssl_context() const { } // namespace httplib -#ifdef _WIN32 -#undef poll -#endif - #endif // CPPHTTPLIB_HTTPLIB_H From a8d6172250ef0dd3927c0f98e3029e743c1aa459 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 12 Mar 2025 17:12:54 +0100 Subject: [PATCH 0405/1222] Avoid static std::string (#2103) Replace static std::string objects with constexpr character arrays and use compile-time string length calculations. --- httplib.h | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/httplib.h b/httplib.h index e009971435..0949e1d2f2 100644 --- a/httplib.h +++ b/httplib.h @@ -2050,6 +2050,10 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { callback(static_cast(sec), static_cast(usec)); } +template inline constexpr size_t str_len(const char (&)[N]) { + return N - 1; +} + inline bool is_numeric(const std::string &str) { return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); } @@ -2209,9 +2213,9 @@ inline const char *status_message(int status) { inline std::string get_bearer_token_auth(const Request &req) { if (req.has_header("Authorization")) { - static std::string BearerHeaderPrefix = "Bearer "; + constexpr auto bearer_header_prefix_len = detail::str_len("Bearer "); return req.get_header_value("Authorization") - .substr(BearerHeaderPrefix.length()); + .substr(bearer_header_prefix_len); } return ""; } @@ -4646,10 +4650,8 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, } } - static const std::string done_marker("0\r\n"); - if (!write_data(strm, done_marker.data(), done_marker.size())) { - ok = false; - } + constexpr const char done_marker[] = "0\r\n"; + if (!write_data(strm, done_marker, str_len(done_marker))) { ok = false; } // Trailer if (trailer) { @@ -4661,8 +4663,8 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, } } - static const std::string crlf("\r\n"); - if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; } + constexpr const char crlf[] = "\r\n"; + if (!write_data(strm, crlf, str_len(crlf))) { ok = false; } }; data_sink.done = [&](void) { done_with_trailer(nullptr); }; @@ -4904,11 +4906,11 @@ class MultipartFormDataParser { return false; } - static const std::string header_content_type = "Content-Type:"; + constexpr const char header_content_type[] = "Content-Type:"; if (start_with_case_ignore(header, header_content_type)) { file_.content_type = - trim_copy(header.substr(header_content_type.size())); + trim_copy(header.substr(str_len(header_content_type))); } else { static const std::regex re_content_disposition( R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", @@ -5005,10 +5007,10 @@ class MultipartFormDataParser { file_.content_type.clear(); } - bool start_with_case_ignore(const std::string &a, - const std::string &b) const { - if (a.size() < b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { + bool start_with_case_ignore(const std::string &a, const char *b) const { + const auto b_len = strlen(b); + if (a.size() < b_len) { return false; } + for (size_t i = 0; i < b_len; i++) { if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { return false; } @@ -5095,7 +5097,7 @@ class MultipartFormDataParser { }; inline std::string random_string(size_t length) { - static const char data[] = + constexpr const char data[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // std::random_device might actually be deterministic on some @@ -6094,7 +6096,7 @@ inline time_t BufferStream::duration() const { return 0; } inline const std::string &BufferStream::get_buffer() const { return buffer; } inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { - static constexpr char marker[] = "/:"; + constexpr const char marker[] = "/:"; // One past the last ending position of a path param substring std::size_t last_param_end = 0; @@ -6115,7 +6117,7 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { static_fragments_.push_back( pattern.substr(last_param_end, marker_pos - last_param_end + 1)); - const auto param_name_start = marker_pos + 2; + const auto param_name_start = marker_pos + str_len(marker); auto sep_pos = pattern.find(separator, param_name_start); if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } From 94a40288218935977e94f932c5a6eb92261614fc Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Wed, 12 Mar 2025 17:16:27 +0100 Subject: [PATCH 0406/1222] Update vendored gtest to 1.12.1 (#2100) Update googletest to the last version supporting C++11. --- test/Makefile | 2 +- test/gtest/gtest-all.cc | 12501 ---------------- test/gtest/gtest.h | 12377 --------------- .../include/gtest/gtest-assertion-result.h | 237 + test/gtest/include/gtest/gtest-death-test.h | 345 + test/gtest/include/gtest/gtest-matchers.h | 956 ++ test/gtest/include/gtest/gtest-message.h | 218 + test/gtest/include/gtest/gtest-param-test.h | 510 + test/gtest/include/gtest/gtest-printers.h | 1048 ++ test/gtest/include/gtest/gtest-spi.h | 248 + test/gtest/include/gtest/gtest-test-part.h | 190 + test/gtest/include/gtest/gtest-typed-test.h | 331 + test/gtest/include/gtest/gtest.h | 2297 +++ test/gtest/include/gtest/gtest_pred_impl.h | 279 + test/gtest/include/gtest/gtest_prod.h | 60 + .../include/gtest/internal/custom/README.md | 44 + .../gtest/internal/custom/gtest-port.h | 37 + .../gtest/internal/custom/gtest-printers.h | 42 + .../include/gtest/internal/custom/gtest.h | 37 + .../internal/gtest-death-test-internal.h | 306 + .../include/gtest/internal/gtest-filepath.h | 210 + .../include/gtest/internal/gtest-internal.h | 1570 ++ .../include/gtest/internal/gtest-param-util.h | 956 ++ .../include/gtest/internal/gtest-port-arch.h | 116 + .../gtest/include/gtest/internal/gtest-port.h | 2413 +++ .../include/gtest/internal/gtest-string.h | 177 + .../include/gtest/internal/gtest-type-util.h | 186 + test/gtest/src/gtest-all.cc | 49 + test/gtest/src/gtest-assertion-result.cc | 77 + test/gtest/src/gtest-death-test.cc | 1620 ++ test/gtest/src/gtest-filepath.cc | 367 + test/gtest/src/gtest-internal-inl.h | 1212 ++ test/gtest/src/gtest-matchers.cc | 98 + test/gtest/src/gtest-port.cc | 1394 ++ test/gtest/src/gtest-printers.cc | 553 + test/gtest/src/gtest-test-part.cc | 105 + test/gtest/src/gtest-typed-test.cc | 104 + test/gtest/src/gtest.cc | 6795 +++++++++ test/gtest/{ => src}/gtest_main.cc | 5 +- 39 files changed, 25190 insertions(+), 24882 deletions(-) delete mode 100644 test/gtest/gtest-all.cc delete mode 100644 test/gtest/gtest.h create mode 100644 test/gtest/include/gtest/gtest-assertion-result.h create mode 100644 test/gtest/include/gtest/gtest-death-test.h create mode 100644 test/gtest/include/gtest/gtest-matchers.h create mode 100644 test/gtest/include/gtest/gtest-message.h create mode 100644 test/gtest/include/gtest/gtest-param-test.h create mode 100644 test/gtest/include/gtest/gtest-printers.h create mode 100644 test/gtest/include/gtest/gtest-spi.h create mode 100644 test/gtest/include/gtest/gtest-test-part.h create mode 100644 test/gtest/include/gtest/gtest-typed-test.h create mode 100644 test/gtest/include/gtest/gtest.h create mode 100644 test/gtest/include/gtest/gtest_pred_impl.h create mode 100644 test/gtest/include/gtest/gtest_prod.h create mode 100644 test/gtest/include/gtest/internal/custom/README.md create mode 100644 test/gtest/include/gtest/internal/custom/gtest-port.h create mode 100644 test/gtest/include/gtest/internal/custom/gtest-printers.h create mode 100644 test/gtest/include/gtest/internal/custom/gtest.h create mode 100644 test/gtest/include/gtest/internal/gtest-death-test-internal.h create mode 100644 test/gtest/include/gtest/internal/gtest-filepath.h create mode 100644 test/gtest/include/gtest/internal/gtest-internal.h create mode 100644 test/gtest/include/gtest/internal/gtest-param-util.h create mode 100644 test/gtest/include/gtest/internal/gtest-port-arch.h create mode 100644 test/gtest/include/gtest/internal/gtest-port.h create mode 100644 test/gtest/include/gtest/internal/gtest-string.h create mode 100644 test/gtest/include/gtest/internal/gtest-type-util.h create mode 100644 test/gtest/src/gtest-all.cc create mode 100644 test/gtest/src/gtest-assertion-result.cc create mode 100644 test/gtest/src/gtest-death-test.cc create mode 100644 test/gtest/src/gtest-filepath.cc create mode 100644 test/gtest/src/gtest-internal-inl.h create mode 100644 test/gtest/src/gtest-matchers.cc create mode 100644 test/gtest/src/gtest-port.cc create mode 100644 test/gtest/src/gtest-printers.cc create mode 100644 test/gtest/src/gtest-test-part.cc create mode 100644 test/gtest/src/gtest-typed-test.cc create mode 100644 test/gtest/src/gtest.cc rename test/gtest/{ => src}/gtest_main.cc (97%) diff --git a/test/Makefile b/test/Makefile index 46c4a9104b..48cd3abb2e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -TEST_ARGS = gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl +TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl # By default, use standalone_fuzz_target_runner. # This runner does no fuzzing, but simply executes the inputs diff --git a/test/gtest/gtest-all.cc b/test/gtest/gtest-all.cc deleted file mode 100644 index 24e669e1e3..0000000000 --- a/test/gtest/gtest-all.cc +++ /dev/null @@ -1,12501 +0,0 @@ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// Google C++ Testing and Mocking Framework (Google Test) -// -// Sometimes it's desirable to build Google Test by compiling a single file. -// This file serves this purpose. - -// This line ensures that gtest.h can be compiled on its own, even -// when it's fused. -#include "gtest/gtest.h" - -// The following lines pull in the real gtest *.cc files. -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// The Google C++ Testing and Mocking Framework (Google Test) - -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// Utilities for testing Google Test itself and code that uses Google Test -// (e.g. frameworks built on top of Google Test). - -// GOOGLETEST_CM0004 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ - - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -namespace testing { - -// This helper class can be used to mock out Google Test failure reporting -// so that we can test Google Test or code that builds on Google Test. -// -// An object of this class appends a TestPartResult object to the -// TestPartResultArray object given in the constructor whenever a Google Test -// failure is reported. It can either intercept only failures that are -// generated in the same thread that created this object or it can intercept -// all generated failures. The scope of this mock object can be controlled with -// the second argument to the two arguments constructor. -class GTEST_API_ ScopedFakeTestPartResultReporter - : public TestPartResultReporterInterface { - public: - // The two possible mocking modes of this object. - enum InterceptMode { - INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. - INTERCEPT_ALL_THREADS // Intercepts all failures. - }; - - // The c'tor sets this object as the test part result reporter used - // by Google Test. The 'result' parameter specifies where to report the - // results. This reporter will only catch failures generated in the current - // thread. DEPRECATED - explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); - - // Same as above, but you can choose the interception scope of this object. - ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, - TestPartResultArray* result); - - // The d'tor restores the previous test part result reporter. - ~ScopedFakeTestPartResultReporter() override; - - // Appends the TestPartResult object to the TestPartResultArray - // received in the constructor. - // - // This method is from the TestPartResultReporterInterface - // interface. - void ReportTestPartResult(const TestPartResult& result) override; - - private: - void Init(); - - const InterceptMode intercept_mode_; - TestPartResultReporterInterface* old_reporter_; - TestPartResultArray* const result_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter); -}; - -namespace internal { - -// A helper class for implementing EXPECT_FATAL_FAILURE() and -// EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given -// TestPartResultArray contains exactly one failure that has the given -// type and contains the given substring. If that's not the case, a -// non-fatal failure will be generated. -class GTEST_API_ SingleFailureChecker { - public: - // The constructor remembers the arguments. - SingleFailureChecker(const TestPartResultArray* results, - TestPartResult::Type type, const std::string& substr); - ~SingleFailureChecker(); - private: - const TestPartResultArray* const results_; - const TestPartResult::Type type_; - const std::string substr_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker); -}; - -} // namespace internal - -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -// A set of macros for testing Google Test assertions or code that's expected -// to generate Google Test fatal failures. It verifies that the given -// statement will cause exactly one fatal Google Test failure with 'substr' -// being part of the failure message. -// -// There are two different versions of this macro. EXPECT_FATAL_FAILURE only -// affects and considers failures generated in the current thread and -// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. -// -// The verification of the assertion is done correctly even when the statement -// throws an exception or aborts the current function. -// -// Known restrictions: -// - 'statement' cannot reference local non-static variables or -// non-static members of the current object. -// - 'statement' cannot return a value. -// - You cannot stream a failure message to this macro. -// -// Note that even though the implementations of the following two -// macros are much alike, we cannot refactor them to use a common -// helper macro, due to some peculiarity in how the preprocessor -// works. The AcceptsMacroThatExpandsToUnprotectedComma test in -// gtest_unittest.cc will fail to compile if we do that. -#define EXPECT_FATAL_FAILURE(statement, substr) \ - do { \ - class GTestExpectFatalFailureHelper {\ - public:\ - static void Execute() { statement; }\ - };\ - ::testing::TestPartResultArray gtest_failures;\ - ::testing::internal::SingleFailureChecker gtest_checker(\ - >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ - {\ - ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ - ::testing::ScopedFakeTestPartResultReporter:: \ - INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ - GTestExpectFatalFailureHelper::Execute();\ - }\ - } while (::testing::internal::AlwaysFalse()) - -#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ - do { \ - class GTestExpectFatalFailureHelper {\ - public:\ - static void Execute() { statement; }\ - };\ - ::testing::TestPartResultArray gtest_failures;\ - ::testing::internal::SingleFailureChecker gtest_checker(\ - >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ - {\ - ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ - ::testing::ScopedFakeTestPartResultReporter:: \ - INTERCEPT_ALL_THREADS, >est_failures);\ - GTestExpectFatalFailureHelper::Execute();\ - }\ - } while (::testing::internal::AlwaysFalse()) - -// A macro for testing Google Test assertions or code that's expected to -// generate Google Test non-fatal failures. It asserts that the given -// statement will cause exactly one non-fatal Google Test failure with 'substr' -// being part of the failure message. -// -// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only -// affects and considers failures generated in the current thread and -// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. -// -// 'statement' is allowed to reference local variables and members of -// the current object. -// -// The verification of the assertion is done correctly even when the statement -// throws an exception or aborts the current function. -// -// Known restrictions: -// - You cannot stream a failure message to this macro. -// -// Note that even though the implementations of the following two -// macros are much alike, we cannot refactor them to use a common -// helper macro, due to some peculiarity in how the preprocessor -// works. If we do that, the code won't compile when the user gives -// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that -// expands to code containing an unprotected comma. The -// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc -// catches that. -// -// For the same reason, we have to write -// if (::testing::internal::AlwaysTrue()) { statement; } -// instead of -// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) -// to avoid an MSVC warning on unreachable code. -#define EXPECT_NONFATAL_FAILURE(statement, substr) \ - do {\ - ::testing::TestPartResultArray gtest_failures;\ - ::testing::internal::SingleFailureChecker gtest_checker(\ - >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ - (substr));\ - {\ - ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ - ::testing::ScopedFakeTestPartResultReporter:: \ - INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ - if (::testing::internal::AlwaysTrue()) { statement; }\ - }\ - } while (::testing::internal::AlwaysFalse()) - -#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ - do {\ - ::testing::TestPartResultArray gtest_failures;\ - ::testing::internal::SingleFailureChecker gtest_checker(\ - >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ - (substr));\ - {\ - ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ - ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ - >est_failures);\ - if (::testing::internal::AlwaysTrue()) { statement; }\ - }\ - } while (::testing::internal::AlwaysFalse()) - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include // NOLINT -#include -#include -#include -#include -#include -#include -#include // NOLINT -#include -#include - -#if GTEST_OS_LINUX - -# include // NOLINT -# include // NOLINT -# include // NOLINT -// Declares vsnprintf(). This header is not available on Windows. -# include // NOLINT -# include // NOLINT -# include // NOLINT -# include // NOLINT -# include - -#elif GTEST_OS_ZOS -# include // NOLINT - -// On z/OS we additionally need strings.h for strcasecmp. -# include // NOLINT - -#elif GTEST_OS_WINDOWS_MOBILE // We are on Windows CE. - -# include // NOLINT -# undef min - -#elif GTEST_OS_WINDOWS // We are on Windows proper. - -# include // NOLINT -# undef min - -#ifdef _MSC_VER -# include // NOLINT -#endif - -# include // NOLINT -# include // NOLINT -# include // NOLINT -# include // NOLINT - -# if GTEST_OS_WINDOWS_MINGW -# include // NOLINT -# endif // GTEST_OS_WINDOWS_MINGW - -#else - -// cpplint thinks that the header is already included, so we want to -// silence it. -# include // NOLINT -# include // NOLINT - -#endif // GTEST_OS_LINUX - -#if GTEST_HAS_EXCEPTIONS -# include -#endif - -#if GTEST_CAN_STREAM_RESULTS_ -# include // NOLINT -# include // NOLINT -# include // NOLINT -# include // NOLINT -#endif - -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Utility functions and classes used by the Google C++ testing framework.// -// This file contains purely Google Test's internal implementation. Please -// DO NOT #INCLUDE IT IN A USER PROGRAM. - -#ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ -#define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ - -#ifndef _WIN32_WCE -# include -#endif // !_WIN32_WCE -#include -#include // For strtoll/_strtoul64/malloc/free. -#include // For memmove. - -#include -#include -#include -#include -#include - - -#if GTEST_CAN_STREAM_RESULTS_ -# include // NOLINT -# include // NOLINT -#endif - -#if GTEST_OS_WINDOWS -# include // NOLINT -#endif // GTEST_OS_WINDOWS - - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -namespace testing { - -// Declares the flags. -// -// We don't want the users to modify this flag in the code, but want -// Google Test's own unit tests to be able to access it. Therefore we -// declare it here as opposed to in gtest.h. -GTEST_DECLARE_bool_(death_test_use_fork); - -namespace internal { - -// The value of GetTestTypeId() as seen from within the Google Test -// library. This is solely for testing GetTestTypeId(). -GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest; - -// Names of the flags (needed for parsing Google Test flags). -const char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests"; -const char kBreakOnFailureFlag[] = "break_on_failure"; -const char kCatchExceptionsFlag[] = "catch_exceptions"; -const char kColorFlag[] = "color"; -const char kFailFast[] = "fail_fast"; -const char kFilterFlag[] = "filter"; -const char kListTestsFlag[] = "list_tests"; -const char kOutputFlag[] = "output"; -const char kBriefFlag[] = "brief"; -const char kPrintTimeFlag[] = "print_time"; -const char kPrintUTF8Flag[] = "print_utf8"; -const char kRandomSeedFlag[] = "random_seed"; -const char kRepeatFlag[] = "repeat"; -const char kShuffleFlag[] = "shuffle"; -const char kStackTraceDepthFlag[] = "stack_trace_depth"; -const char kStreamResultToFlag[] = "stream_result_to"; -const char kThrowOnFailureFlag[] = "throw_on_failure"; -const char kFlagfileFlag[] = "flagfile"; - -// A valid random seed must be in [1, kMaxRandomSeed]. -const int kMaxRandomSeed = 99999; - -// g_help_flag is true if and only if the --help flag or an equivalent form -// is specified on the command line. -GTEST_API_ extern bool g_help_flag; - -// Returns the current time in milliseconds. -GTEST_API_ TimeInMillis GetTimeInMillis(); - -// Returns true if and only if Google Test should use colors in the output. -GTEST_API_ bool ShouldUseColor(bool stdout_is_tty); - -// Formats the given time in milliseconds as seconds. -GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); - -// Converts the given time in milliseconds to a date string in the ISO 8601 -// format, without the timezone information. N.B.: due to the use the -// non-reentrant localtime() function, this function is not thread safe. Do -// not use it in any code that can be called from multiple threads. -GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms); - -// Parses a string for an Int32 flag, in the form of "--flag=value". -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -GTEST_API_ bool ParseInt32Flag( - const char* str, const char* flag, int32_t* value); - -// Returns a random seed in range [1, kMaxRandomSeed] based on the -// given --gtest_random_seed flag value. -inline int GetRandomSeedFromFlag(int32_t random_seed_flag) { - const unsigned int raw_seed = (random_seed_flag == 0) ? - static_cast(GetTimeInMillis()) : - static_cast(random_seed_flag); - - // Normalizes the actual seed to range [1, kMaxRandomSeed] such that - // it's easy to type. - const int normalized_seed = - static_cast((raw_seed - 1U) % - static_cast(kMaxRandomSeed)) + 1; - return normalized_seed; -} - -// Returns the first valid random seed after 'seed'. The behavior is -// undefined if 'seed' is invalid. The seed after kMaxRandomSeed is -// considered to be 1. -inline int GetNextRandomSeed(int seed) { - GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed) - << "Invalid random seed " << seed << " - must be in [1, " - << kMaxRandomSeed << "]."; - const int next_seed = seed + 1; - return (next_seed > kMaxRandomSeed) ? 1 : next_seed; -} - -// This class saves the values of all Google Test flags in its c'tor, and -// restores them in its d'tor. -class GTestFlagSaver { - public: - // The c'tor. - GTestFlagSaver() { - also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests); - break_on_failure_ = GTEST_FLAG(break_on_failure); - catch_exceptions_ = GTEST_FLAG(catch_exceptions); - color_ = GTEST_FLAG(color); - death_test_style_ = GTEST_FLAG(death_test_style); - death_test_use_fork_ = GTEST_FLAG(death_test_use_fork); - fail_fast_ = GTEST_FLAG(fail_fast); - filter_ = GTEST_FLAG(filter); - internal_run_death_test_ = GTEST_FLAG(internal_run_death_test); - list_tests_ = GTEST_FLAG(list_tests); - output_ = GTEST_FLAG(output); - brief_ = GTEST_FLAG(brief); - print_time_ = GTEST_FLAG(print_time); - print_utf8_ = GTEST_FLAG(print_utf8); - random_seed_ = GTEST_FLAG(random_seed); - repeat_ = GTEST_FLAG(repeat); - shuffle_ = GTEST_FLAG(shuffle); - stack_trace_depth_ = GTEST_FLAG(stack_trace_depth); - stream_result_to_ = GTEST_FLAG(stream_result_to); - throw_on_failure_ = GTEST_FLAG(throw_on_failure); - } - - // The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS. - ~GTestFlagSaver() { - GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_; - GTEST_FLAG(break_on_failure) = break_on_failure_; - GTEST_FLAG(catch_exceptions) = catch_exceptions_; - GTEST_FLAG(color) = color_; - GTEST_FLAG(death_test_style) = death_test_style_; - GTEST_FLAG(death_test_use_fork) = death_test_use_fork_; - GTEST_FLAG(filter) = filter_; - GTEST_FLAG(fail_fast) = fail_fast_; - GTEST_FLAG(internal_run_death_test) = internal_run_death_test_; - GTEST_FLAG(list_tests) = list_tests_; - GTEST_FLAG(output) = output_; - GTEST_FLAG(brief) = brief_; - GTEST_FLAG(print_time) = print_time_; - GTEST_FLAG(print_utf8) = print_utf8_; - GTEST_FLAG(random_seed) = random_seed_; - GTEST_FLAG(repeat) = repeat_; - GTEST_FLAG(shuffle) = shuffle_; - GTEST_FLAG(stack_trace_depth) = stack_trace_depth_; - GTEST_FLAG(stream_result_to) = stream_result_to_; - GTEST_FLAG(throw_on_failure) = throw_on_failure_; - } - - private: - // Fields for saving the original values of flags. - bool also_run_disabled_tests_; - bool break_on_failure_; - bool catch_exceptions_; - std::string color_; - std::string death_test_style_; - bool death_test_use_fork_; - bool fail_fast_; - std::string filter_; - std::string internal_run_death_test_; - bool list_tests_; - std::string output_; - bool brief_; - bool print_time_; - bool print_utf8_; - int32_t random_seed_; - int32_t repeat_; - bool shuffle_; - int32_t stack_trace_depth_; - std::string stream_result_to_; - bool throw_on_failure_; -} GTEST_ATTRIBUTE_UNUSED_; - -// Converts a Unicode code point to a narrow string in UTF-8 encoding. -// code_point parameter is of type UInt32 because wchar_t may not be -// wide enough to contain a code point. -// If the code_point is not a valid Unicode code point -// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted -// to "(Invalid Unicode 0xXXXXXXXX)". -GTEST_API_ std::string CodePointToUtf8(uint32_t code_point); - -// Converts a wide string to a narrow string in UTF-8 encoding. -// The wide string is assumed to have the following encoding: -// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin) -// UTF-32 if sizeof(wchar_t) == 4 (on Linux) -// Parameter str points to a null-terminated wide string. -// Parameter num_chars may additionally limit the number -// of wchar_t characters processed. -1 is used when the entire string -// should be processed. -// If the string contains code points that are not valid Unicode code points -// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output -// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding -// and contains invalid UTF-16 surrogate pairs, values in those pairs -// will be encoded as individual Unicode characters from Basic Normal Plane. -GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars); - -// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file -// if the variable is present. If a file already exists at this location, this -// function will write over it. If the variable is present, but the file cannot -// be created, prints an error and exits. -void WriteToShardStatusFileIfNeeded(); - -// Checks whether sharding is enabled by examining the relevant -// environment variable values. If the variables are present, -// but inconsistent (e.g., shard_index >= total_shards), prints -// an error and exits. If in_subprocess_for_death_test, sharding is -// disabled because it must only be applied to the original test -// process. Otherwise, we could filter out death tests we intended to execute. -GTEST_API_ bool ShouldShard(const char* total_shards_str, - const char* shard_index_str, - bool in_subprocess_for_death_test); - -// Parses the environment variable var as a 32-bit integer. If it is unset, -// returns default_val. If it is not a 32-bit integer, prints an error and -// and aborts. -GTEST_API_ int32_t Int32FromEnvOrDie(const char* env_var, int32_t default_val); - -// Given the total number of shards, the shard index, and the test id, -// returns true if and only if the test should be run on this shard. The test id -// is some arbitrary but unique non-negative integer assigned to each test -// method. Assumes that 0 <= shard_index < total_shards. -GTEST_API_ bool ShouldRunTestOnShard( - int total_shards, int shard_index, int test_id); - -// STL container utilities. - -// Returns the number of elements in the given container that satisfy -// the given predicate. -template -inline int CountIf(const Container& c, Predicate predicate) { - // Implemented as an explicit loop since std::count_if() in libCstd on - // Solaris has a non-standard signature. - int count = 0; - for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) { - if (predicate(*it)) - ++count; - } - return count; -} - -// Applies a function/functor to each element in the container. -template -void ForEach(const Container& c, Functor functor) { - std::for_each(c.begin(), c.end(), functor); -} - -// Returns the i-th element of the vector, or default_value if i is not -// in range [0, v.size()). -template -inline E GetElementOr(const std::vector& v, int i, E default_value) { - return (i < 0 || i >= static_cast(v.size())) ? default_value - : v[static_cast(i)]; -} - -// Performs an in-place shuffle of a range of the vector's elements. -// 'begin' and 'end' are element indices as an STL-style range; -// i.e. [begin, end) are shuffled, where 'end' == size() means to -// shuffle to the end of the vector. -template -void ShuffleRange(internal::Random* random, int begin, int end, - std::vector* v) { - const int size = static_cast(v->size()); - GTEST_CHECK_(0 <= begin && begin <= size) - << "Invalid shuffle range start " << begin << ": must be in range [0, " - << size << "]."; - GTEST_CHECK_(begin <= end && end <= size) - << "Invalid shuffle range finish " << end << ": must be in range [" - << begin << ", " << size << "]."; - - // Fisher-Yates shuffle, from - // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle - for (int range_width = end - begin; range_width >= 2; range_width--) { - const int last_in_range = begin + range_width - 1; - const int selected = - begin + - static_cast(random->Generate(static_cast(range_width))); - std::swap((*v)[static_cast(selected)], - (*v)[static_cast(last_in_range)]); - } -} - -// Performs an in-place shuffle of the vector's elements. -template -inline void Shuffle(internal::Random* random, std::vector* v) { - ShuffleRange(random, 0, static_cast(v->size()), v); -} - -// A function for deleting an object. Handy for being used as a -// functor. -template -static void Delete(T* x) { - delete x; -} - -// A predicate that checks the key of a TestProperty against a known key. -// -// TestPropertyKeyIs is copyable. -class TestPropertyKeyIs { - public: - // Constructor. - // - // TestPropertyKeyIs has NO default constructor. - explicit TestPropertyKeyIs(const std::string& key) : key_(key) {} - - // Returns true if and only if the test name of test property matches on key_. - bool operator()(const TestProperty& test_property) const { - return test_property.key() == key_; - } - - private: - std::string key_; -}; - -// Class UnitTestOptions. -// -// This class contains functions for processing options the user -// specifies when running the tests. It has only static members. -// -// In most cases, the user can specify an option using either an -// environment variable or a command line flag. E.g. you can set the -// test filter using either GTEST_FILTER or --gtest_filter. If both -// the variable and the flag are present, the latter overrides the -// former. -class GTEST_API_ UnitTestOptions { - public: - // Functions for processing the gtest_output flag. - - // Returns the output format, or "" for normal printed output. - static std::string GetOutputFormat(); - - // Returns the absolute path of the requested output file, or the - // default (test_detail.xml in the original working directory) if - // none was explicitly specified. - static std::string GetAbsolutePathToOutputFile(); - - // Functions for processing the gtest_filter flag. - - // Returns true if and only if the user-specified filter matches the test - // suite name and the test name. - static bool FilterMatchesTest(const std::string& test_suite_name, - const std::string& test_name); - -#if GTEST_OS_WINDOWS - // Function for supporting the gtest_catch_exception flag. - - // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the - // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. - // This function is useful as an __except condition. - static int GTestShouldProcessSEH(DWORD exception_code); -#endif // GTEST_OS_WINDOWS - - // Returns true if "name" matches the ':' separated list of glob-style - // filters in "filter". - static bool MatchesFilter(const std::string& name, const char* filter); -}; - -// Returns the current application's name, removing directory path if that -// is present. Used by UnitTestOptions::GetOutputFile. -GTEST_API_ FilePath GetCurrentExecutableName(); - -// The role interface for getting the OS stack trace as a string. -class OsStackTraceGetterInterface { - public: - OsStackTraceGetterInterface() {} - virtual ~OsStackTraceGetterInterface() {} - - // Returns the current OS stack trace as an std::string. Parameters: - // - // max_depth - the maximum number of stack frames to be included - // in the trace. - // skip_count - the number of top frames to be skipped; doesn't count - // against max_depth. - virtual std::string CurrentStackTrace(int max_depth, int skip_count) = 0; - - // UponLeavingGTest() should be called immediately before Google Test calls - // user code. It saves some information about the current stack that - // CurrentStackTrace() will use to find and hide Google Test stack frames. - virtual void UponLeavingGTest() = 0; - - // This string is inserted in place of stack frames that are part of - // Google Test's implementation. - static const char* const kElidedFramesMarker; - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface); -}; - -// A working implementation of the OsStackTraceGetterInterface interface. -class OsStackTraceGetter : public OsStackTraceGetterInterface { - public: - OsStackTraceGetter() {} - - std::string CurrentStackTrace(int max_depth, int skip_count) override; - void UponLeavingGTest() override; - - private: -#if GTEST_HAS_ABSL - Mutex mutex_; // Protects all internal state. - - // We save the stack frame below the frame that calls user code. - // We do this because the address of the frame immediately below - // the user code changes between the call to UponLeavingGTest() - // and any calls to the stack trace code from within the user code. - void* caller_frame_ = nullptr; -#endif // GTEST_HAS_ABSL - - GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter); -}; - -// Information about a Google Test trace point. -struct TraceInfo { - const char* file; - int line; - std::string message; -}; - -// This is the default global test part result reporter used in UnitTestImpl. -// This class should only be used by UnitTestImpl. -class DefaultGlobalTestPartResultReporter - : public TestPartResultReporterInterface { - public: - explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test); - // Implements the TestPartResultReporterInterface. Reports the test part - // result in the current test. - void ReportTestPartResult(const TestPartResult& result) override; - - private: - UnitTestImpl* const unit_test_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter); -}; - -// This is the default per thread test part result reporter used in -// UnitTestImpl. This class should only be used by UnitTestImpl. -class DefaultPerThreadTestPartResultReporter - : public TestPartResultReporterInterface { - public: - explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test); - // Implements the TestPartResultReporterInterface. The implementation just - // delegates to the current global test part result reporter of *unit_test_. - void ReportTestPartResult(const TestPartResult& result) override; - - private: - UnitTestImpl* const unit_test_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter); -}; - -// The private implementation of the UnitTest class. We don't protect -// the methods under a mutex, as this class is not accessible by a -// user and the UnitTest class that delegates work to this class does -// proper locking. -class GTEST_API_ UnitTestImpl { - public: - explicit UnitTestImpl(UnitTest* parent); - virtual ~UnitTestImpl(); - - // There are two different ways to register your own TestPartResultReporter. - // You can register your own repoter to listen either only for test results - // from the current thread or for results from all threads. - // By default, each per-thread test result repoter just passes a new - // TestPartResult to the global test result reporter, which registers the - // test part result for the currently running test. - - // Returns the global test part result reporter. - TestPartResultReporterInterface* GetGlobalTestPartResultReporter(); - - // Sets the global test part result reporter. - void SetGlobalTestPartResultReporter( - TestPartResultReporterInterface* reporter); - - // Returns the test part result reporter for the current thread. - TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread(); - - // Sets the test part result reporter for the current thread. - void SetTestPartResultReporterForCurrentThread( - TestPartResultReporterInterface* reporter); - - // Gets the number of successful test suites. - int successful_test_suite_count() const; - - // Gets the number of failed test suites. - int failed_test_suite_count() const; - - // Gets the number of all test suites. - int total_test_suite_count() const; - - // Gets the number of all test suites that contain at least one test - // that should run. - int test_suite_to_run_count() const; - - // Gets the number of successful tests. - int successful_test_count() const; - - // Gets the number of skipped tests. - int skipped_test_count() const; - - // Gets the number of failed tests. - int failed_test_count() const; - - // Gets the number of disabled tests that will be reported in the XML report. - int reportable_disabled_test_count() const; - - // Gets the number of disabled tests. - int disabled_test_count() const; - - // Gets the number of tests to be printed in the XML report. - int reportable_test_count() const; - - // Gets the number of all tests. - int total_test_count() const; - - // Gets the number of tests that should run. - int test_to_run_count() const; - - // Gets the time of the test program start, in ms from the start of the - // UNIX epoch. - TimeInMillis start_timestamp() const { return start_timestamp_; } - - // Gets the elapsed time, in milliseconds. - TimeInMillis elapsed_time() const { return elapsed_time_; } - - // Returns true if and only if the unit test passed (i.e. all test suites - // passed). - bool Passed() const { return !Failed(); } - - // Returns true if and only if the unit test failed (i.e. some test suite - // failed or something outside of all tests failed). - bool Failed() const { - return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed(); - } - - // Gets the i-th test suite among all the test suites. i can range from 0 to - // total_test_suite_count() - 1. If i is not in that range, returns NULL. - const TestSuite* GetTestSuite(int i) const { - const int index = GetElementOr(test_suite_indices_, i, -1); - return index < 0 ? nullptr : test_suites_[static_cast(i)]; - } - - // Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - const TestCase* GetTestCase(int i) const { return GetTestSuite(i); } -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Gets the i-th test suite among all the test suites. i can range from 0 to - // total_test_suite_count() - 1. If i is not in that range, returns NULL. - TestSuite* GetMutableSuiteCase(int i) { - const int index = GetElementOr(test_suite_indices_, i, -1); - return index < 0 ? nullptr : test_suites_[static_cast(index)]; - } - - // Provides access to the event listener list. - TestEventListeners* listeners() { return &listeners_; } - - // Returns the TestResult for the test that's currently running, or - // the TestResult for the ad hoc test if no test is running. - TestResult* current_test_result(); - - // Returns the TestResult for the ad hoc test. - const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; } - - // Sets the OS stack trace getter. - // - // Does nothing if the input and the current OS stack trace getter - // are the same; otherwise, deletes the old getter and makes the - // input the current getter. - void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter); - - // Returns the current OS stack trace getter if it is not NULL; - // otherwise, creates an OsStackTraceGetter, makes it the current - // getter, and returns it. - OsStackTraceGetterInterface* os_stack_trace_getter(); - - // Returns the current OS stack trace as an std::string. - // - // The maximum number of stack frames to be included is specified by - // the gtest_stack_trace_depth flag. The skip_count parameter - // specifies the number of top frames to be skipped, which doesn't - // count against the number of frames to be included. - // - // For example, if Foo() calls Bar(), which in turn calls - // CurrentOsStackTraceExceptTop(1), Foo() will be included in the - // trace but Bar() and CurrentOsStackTraceExceptTop() won't. - std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_; - - // Finds and returns a TestSuite with the given name. If one doesn't - // exist, creates one and returns it. - // - // Arguments: - // - // test_suite_name: name of the test suite - // type_param: the name of the test's type parameter, or NULL if - // this is not a typed or a type-parameterized test. - // set_up_tc: pointer to the function that sets up the test suite - // tear_down_tc: pointer to the function that tears down the test suite - TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param, - internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc); - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - TestCase* GetTestCase(const char* test_case_name, const char* type_param, - internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc) { - return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc); - } -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Adds a TestInfo to the unit test. - // - // Arguments: - // - // set_up_tc: pointer to the function that sets up the test suite - // tear_down_tc: pointer to the function that tears down the test suite - // test_info: the TestInfo object - void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc, - TestInfo* test_info) { -#if GTEST_HAS_DEATH_TEST - // In order to support thread-safe death tests, we need to - // remember the original working directory when the test program - // was first invoked. We cannot do this in RUN_ALL_TESTS(), as - // the user may have changed the current directory before calling - // RUN_ALL_TESTS(). Therefore we capture the current directory in - // AddTestInfo(), which is called to register a TEST or TEST_F - // before main() is reached. - if (original_working_dir_.IsEmpty()) { - original_working_dir_.Set(FilePath::GetCurrentDir()); - GTEST_CHECK_(!original_working_dir_.IsEmpty()) - << "Failed to get the current working directory."; - } -#endif // GTEST_HAS_DEATH_TEST - - GetTestSuite(test_info->test_suite_name(), test_info->type_param(), - set_up_tc, tear_down_tc) - ->AddTestInfo(test_info); - } - - // Returns ParameterizedTestSuiteRegistry object used to keep track of - // value-parameterized tests and instantiate and register them. - internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() { - return parameterized_test_registry_; - } - - std::set* ignored_parameterized_test_suites() { - return &ignored_parameterized_test_suites_; - } - - // Returns TypeParameterizedTestSuiteRegistry object used to keep track of - // type-parameterized tests and instantiations of them. - internal::TypeParameterizedTestSuiteRegistry& - type_parameterized_test_registry() { - return type_parameterized_test_registry_; - } - - // Sets the TestSuite object for the test that's currently running. - void set_current_test_suite(TestSuite* a_current_test_suite) { - current_test_suite_ = a_current_test_suite; - } - - // Sets the TestInfo object for the test that's currently running. If - // current_test_info is NULL, the assertion results will be stored in - // ad_hoc_test_result_. - void set_current_test_info(TestInfo* a_current_test_info) { - current_test_info_ = a_current_test_info; - } - - // Registers all parameterized tests defined using TEST_P and - // INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter - // combination. This method can be called more then once; it has guards - // protecting from registering the tests more then once. If - // value-parameterized tests are disabled, RegisterParameterizedTests is - // present but does nothing. - void RegisterParameterizedTests(); - - // Runs all tests in this UnitTest object, prints the result, and - // returns true if all tests are successful. If any exception is - // thrown during a test, this test is considered to be failed, but - // the rest of the tests will still be run. - bool RunAllTests(); - - // Clears the results of all tests, except the ad hoc tests. - void ClearNonAdHocTestResult() { - ForEach(test_suites_, TestSuite::ClearTestSuiteResult); - } - - // Clears the results of ad-hoc test assertions. - void ClearAdHocTestResult() { - ad_hoc_test_result_.Clear(); - } - - // Adds a TestProperty to the current TestResult object when invoked in a - // context of a test or a test suite, or to the global property set. If the - // result already contains a property with the same key, the value will be - // updated. - void RecordProperty(const TestProperty& test_property); - - enum ReactionToSharding { - HONOR_SHARDING_PROTOCOL, - IGNORE_SHARDING_PROTOCOL - }; - - // Matches the full name of each test against the user-specified - // filter to decide whether the test should run, then records the - // result in each TestSuite and TestInfo object. - // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests - // based on sharding variables in the environment. - // Returns the number of tests that should run. - int FilterTests(ReactionToSharding shard_tests); - - // Prints the names of the tests matching the user-specified filter flag. - void ListTestsMatchingFilter(); - - const TestSuite* current_test_suite() const { return current_test_suite_; } - TestInfo* current_test_info() { return current_test_info_; } - const TestInfo* current_test_info() const { return current_test_info_; } - - // Returns the vector of environments that need to be set-up/torn-down - // before/after the tests are run. - std::vector& environments() { return environments_; } - - // Getters for the per-thread Google Test trace stack. - std::vector& gtest_trace_stack() { - return *(gtest_trace_stack_.pointer()); - } - const std::vector& gtest_trace_stack() const { - return gtest_trace_stack_.get(); - } - -#if GTEST_HAS_DEATH_TEST - void InitDeathTestSubprocessControlInfo() { - internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag()); - } - // Returns a pointer to the parsed --gtest_internal_run_death_test - // flag, or NULL if that flag was not specified. - // This information is useful only in a death test child process. - // Must not be called before a call to InitGoogleTest. - const InternalRunDeathTestFlag* internal_run_death_test_flag() const { - return internal_run_death_test_flag_.get(); - } - - // Returns a pointer to the current death test factory. - internal::DeathTestFactory* death_test_factory() { - return death_test_factory_.get(); - } - - void SuppressTestEventsIfInSubprocess(); - - friend class ReplaceDeathTestFactory; -#endif // GTEST_HAS_DEATH_TEST - - // Initializes the event listener performing XML output as specified by - // UnitTestOptions. Must not be called before InitGoogleTest. - void ConfigureXmlOutput(); - -#if GTEST_CAN_STREAM_RESULTS_ - // Initializes the event listener for streaming test results to a socket. - // Must not be called before InitGoogleTest. - void ConfigureStreamingOutput(); -#endif - - // Performs initialization dependent upon flag values obtained in - // ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to - // ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest - // this function is also called from RunAllTests. Since this function can be - // called more than once, it has to be idempotent. - void PostFlagParsingInit(); - - // Gets the random seed used at the start of the current test iteration. - int random_seed() const { return random_seed_; } - - // Gets the random number generator. - internal::Random* random() { return &random_; } - - // Shuffles all test suites, and the tests within each test suite, - // making sure that death tests are still run first. - void ShuffleTests(); - - // Restores the test suites and tests to their order before the first shuffle. - void UnshuffleTests(); - - // Returns the value of GTEST_FLAG(catch_exceptions) at the moment - // UnitTest::Run() starts. - bool catch_exceptions() const { return catch_exceptions_; } - - private: - friend class ::testing::UnitTest; - - // Used by UnitTest::Run() to capture the state of - // GTEST_FLAG(catch_exceptions) at the moment it starts. - void set_catch_exceptions(bool value) { catch_exceptions_ = value; } - - // The UnitTest object that owns this implementation object. - UnitTest* const parent_; - - // The working directory when the first TEST() or TEST_F() was - // executed. - internal::FilePath original_working_dir_; - - // The default test part result reporters. - DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_; - DefaultPerThreadTestPartResultReporter - default_per_thread_test_part_result_reporter_; - - // Points to (but doesn't own) the global test part result reporter. - TestPartResultReporterInterface* global_test_part_result_repoter_; - - // Protects read and write access to global_test_part_result_reporter_. - internal::Mutex global_test_part_result_reporter_mutex_; - - // Points to (but doesn't own) the per-thread test part result reporter. - internal::ThreadLocal - per_thread_test_part_result_reporter_; - - // The vector of environments that need to be set-up/torn-down - // before/after the tests are run. - std::vector environments_; - - // The vector of TestSuites in their original order. It owns the - // elements in the vector. - std::vector test_suites_; - - // Provides a level of indirection for the test suite list to allow - // easy shuffling and restoring the test suite order. The i-th - // element of this vector is the index of the i-th test suite in the - // shuffled order. - std::vector test_suite_indices_; - - // ParameterizedTestRegistry object used to register value-parameterized - // tests. - internal::ParameterizedTestSuiteRegistry parameterized_test_registry_; - internal::TypeParameterizedTestSuiteRegistry - type_parameterized_test_registry_; - - // The set holding the name of parameterized - // test suites that may go uninstantiated. - std::set ignored_parameterized_test_suites_; - - // Indicates whether RegisterParameterizedTests() has been called already. - bool parameterized_tests_registered_; - - // Index of the last death test suite registered. Initially -1. - int last_death_test_suite_; - - // This points to the TestSuite for the currently running test. It - // changes as Google Test goes through one test suite after another. - // When no test is running, this is set to NULL and Google Test - // stores assertion results in ad_hoc_test_result_. Initially NULL. - TestSuite* current_test_suite_; - - // This points to the TestInfo for the currently running test. It - // changes as Google Test goes through one test after another. When - // no test is running, this is set to NULL and Google Test stores - // assertion results in ad_hoc_test_result_. Initially NULL. - TestInfo* current_test_info_; - - // Normally, a user only writes assertions inside a TEST or TEST_F, - // or inside a function called by a TEST or TEST_F. Since Google - // Test keeps track of which test is current running, it can - // associate such an assertion with the test it belongs to. - // - // If an assertion is encountered when no TEST or TEST_F is running, - // Google Test attributes the assertion result to an imaginary "ad hoc" - // test, and records the result in ad_hoc_test_result_. - TestResult ad_hoc_test_result_; - - // The list of event listeners that can be used to track events inside - // Google Test. - TestEventListeners listeners_; - - // The OS stack trace getter. Will be deleted when the UnitTest - // object is destructed. By default, an OsStackTraceGetter is used, - // but the user can set this field to use a custom getter if that is - // desired. - OsStackTraceGetterInterface* os_stack_trace_getter_; - - // True if and only if PostFlagParsingInit() has been called. - bool post_flag_parse_init_performed_; - - // The random number seed used at the beginning of the test run. - int random_seed_; - - // Our random number generator. - internal::Random random_; - - // The time of the test program start, in ms from the start of the - // UNIX epoch. - TimeInMillis start_timestamp_; - - // How long the test took to run, in milliseconds. - TimeInMillis elapsed_time_; - -#if GTEST_HAS_DEATH_TEST - // The decomposed components of the gtest_internal_run_death_test flag, - // parsed when RUN_ALL_TESTS is called. - std::unique_ptr internal_run_death_test_flag_; - std::unique_ptr death_test_factory_; -#endif // GTEST_HAS_DEATH_TEST - - // A per-thread stack of traces created by the SCOPED_TRACE() macro. - internal::ThreadLocal > gtest_trace_stack_; - - // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests() - // starts. - bool catch_exceptions_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl); -}; // class UnitTestImpl - -// Convenience function for accessing the global UnitTest -// implementation object. -inline UnitTestImpl* GetUnitTestImpl() { - return UnitTest::GetInstance()->impl(); -} - -#if GTEST_USES_SIMPLE_RE - -// Internal helper functions for implementing the simple regular -// expression matcher. -GTEST_API_ bool IsInSet(char ch, const char* str); -GTEST_API_ bool IsAsciiDigit(char ch); -GTEST_API_ bool IsAsciiPunct(char ch); -GTEST_API_ bool IsRepeat(char ch); -GTEST_API_ bool IsAsciiWhiteSpace(char ch); -GTEST_API_ bool IsAsciiWordChar(char ch); -GTEST_API_ bool IsValidEscape(char ch); -GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch); -GTEST_API_ bool ValidateRegex(const char* regex); -GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str); -GTEST_API_ bool MatchRepetitionAndRegexAtHead( - bool escaped, char ch, char repeat, const char* regex, const char* str); -GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str); - -#endif // GTEST_USES_SIMPLE_RE - -// Parses the command line for Google Test flags, without initializing -// other parts of Google Test. -GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv); -GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv); - -#if GTEST_HAS_DEATH_TEST - -// Returns the message describing the last system error, regardless of the -// platform. -GTEST_API_ std::string GetLastErrnoDescription(); - -// Attempts to parse a string into a positive integer pointed to by the -// number parameter. Returns true if that is possible. -// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use -// it here. -template -bool ParseNaturalNumber(const ::std::string& str, Integer* number) { - // Fail fast if the given string does not begin with a digit; - // this bypasses strtoXXX's "optional leading whitespace and plus - // or minus sign" semantics, which are undesirable here. - if (str.empty() || !IsDigit(str[0])) { - return false; - } - errno = 0; - - char* end; - // BiggestConvertible is the largest integer type that system-provided - // string-to-number conversion routines can return. - using BiggestConvertible = unsigned long long; // NOLINT - - const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); // NOLINT - const bool parse_success = *end == '\0' && errno == 0; - - GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed)); - - const Integer result = static_cast(parsed); - if (parse_success && static_cast(result) == parsed) { - *number = result; - return true; - } - return false; -} -#endif // GTEST_HAS_DEATH_TEST - -// TestResult contains some private methods that should be hidden from -// Google Test user but are required for testing. This class allow our tests -// to access them. -// -// This class is supplied only for the purpose of testing Google Test's own -// constructs. Do not use it in user tests, either directly or indirectly. -class TestResultAccessor { - public: - static void RecordProperty(TestResult* test_result, - const std::string& xml_element, - const TestProperty& property) { - test_result->RecordProperty(xml_element, property); - } - - static void ClearTestPartResults(TestResult* test_result) { - test_result->ClearTestPartResults(); - } - - static const std::vector& test_part_results( - const TestResult& test_result) { - return test_result.test_part_results(); - } -}; - -#if GTEST_CAN_STREAM_RESULTS_ - -// Streams test results to the given port on the given host machine. -class StreamingListener : public EmptyTestEventListener { - public: - // Abstract base class for writing strings to a socket. - class AbstractSocketWriter { - public: - virtual ~AbstractSocketWriter() {} - - // Sends a string to the socket. - virtual void Send(const std::string& message) = 0; - - // Closes the socket. - virtual void CloseConnection() {} - - // Sends a string and a newline to the socket. - void SendLn(const std::string& message) { Send(message + "\n"); } - }; - - // Concrete class for actually writing strings to a socket. - class SocketWriter : public AbstractSocketWriter { - public: - SocketWriter(const std::string& host, const std::string& port) - : sockfd_(-1), host_name_(host), port_num_(port) { - MakeConnection(); - } - - ~SocketWriter() override { - if (sockfd_ != -1) - CloseConnection(); - } - - // Sends a string to the socket. - void Send(const std::string& message) override { - GTEST_CHECK_(sockfd_ != -1) - << "Send() can be called only when there is a connection."; - - const auto len = static_cast(message.length()); - if (write(sockfd_, message.c_str(), len) != static_cast(len)) { - GTEST_LOG_(WARNING) - << "stream_result_to: failed to stream to " - << host_name_ << ":" << port_num_; - } - } - - private: - // Creates a client socket and connects to the server. - void MakeConnection(); - - // Closes the socket. - void CloseConnection() override { - GTEST_CHECK_(sockfd_ != -1) - << "CloseConnection() can be called only when there is a connection."; - - close(sockfd_); - sockfd_ = -1; - } - - int sockfd_; // socket file descriptor - const std::string host_name_; - const std::string port_num_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(SocketWriter); - }; // class SocketWriter - - // Escapes '=', '&', '%', and '\n' characters in str as "%xx". - static std::string UrlEncode(const char* str); - - StreamingListener(const std::string& host, const std::string& port) - : socket_writer_(new SocketWriter(host, port)) { - Start(); - } - - explicit StreamingListener(AbstractSocketWriter* socket_writer) - : socket_writer_(socket_writer) { Start(); } - - void OnTestProgramStart(const UnitTest& /* unit_test */) override { - SendLn("event=TestProgramStart"); - } - - void OnTestProgramEnd(const UnitTest& unit_test) override { - // Note that Google Test current only report elapsed time for each - // test iteration, not for the entire test program. - SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed())); - - // Notify the streaming server to stop. - socket_writer_->CloseConnection(); - } - - void OnTestIterationStart(const UnitTest& /* unit_test */, - int iteration) override { - SendLn("event=TestIterationStart&iteration=" + - StreamableToString(iteration)); - } - - void OnTestIterationEnd(const UnitTest& unit_test, - int /* iteration */) override { - SendLn("event=TestIterationEnd&passed=" + - FormatBool(unit_test.Passed()) + "&elapsed_time=" + - StreamableToString(unit_test.elapsed_time()) + "ms"); - } - - // Note that "event=TestCaseStart" is a wire format and has to remain - // "case" for compatibility - void OnTestCaseStart(const TestCase& test_case) override { - SendLn(std::string("event=TestCaseStart&name=") + test_case.name()); - } - - // Note that "event=TestCaseEnd" is a wire format and has to remain - // "case" for compatibility - void OnTestCaseEnd(const TestCase& test_case) override { - SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) + - "&elapsed_time=" + StreamableToString(test_case.elapsed_time()) + - "ms"); - } - - void OnTestStart(const TestInfo& test_info) override { - SendLn(std::string("event=TestStart&name=") + test_info.name()); - } - - void OnTestEnd(const TestInfo& test_info) override { - SendLn("event=TestEnd&passed=" + - FormatBool((test_info.result())->Passed()) + - "&elapsed_time=" + - StreamableToString((test_info.result())->elapsed_time()) + "ms"); - } - - void OnTestPartResult(const TestPartResult& test_part_result) override { - const char* file_name = test_part_result.file_name(); - if (file_name == nullptr) file_name = ""; - SendLn("event=TestPartResult&file=" + UrlEncode(file_name) + - "&line=" + StreamableToString(test_part_result.line_number()) + - "&message=" + UrlEncode(test_part_result.message())); - } - - private: - // Sends the given message and a newline to the socket. - void SendLn(const std::string& message) { socket_writer_->SendLn(message); } - - // Called at the start of streaming to notify the receiver what - // protocol we are using. - void Start() { SendLn("gtest_streaming_protocol_version=1.0"); } - - std::string FormatBool(bool value) { return value ? "1" : "0"; } - - const std::unique_ptr socket_writer_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener); -}; // class StreamingListener - -#endif // GTEST_CAN_STREAM_RESULTS_ - -} // namespace internal -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -#endif // GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ - -#if GTEST_OS_WINDOWS -# define vsnprintf _vsnprintf -#endif // GTEST_OS_WINDOWS - -#if GTEST_OS_MAC -#ifndef GTEST_OS_IOS -#include -#endif -#endif - -#if GTEST_HAS_ABSL -#include "absl/debugging/failure_signal_handler.h" -#include "absl/debugging/stacktrace.h" -#include "absl/debugging/symbolize.h" -#include "absl/strings/str_cat.h" -#endif // GTEST_HAS_ABSL - -namespace testing { - -using internal::CountIf; -using internal::ForEach; -using internal::GetElementOr; -using internal::Shuffle; - -// Constants. - -// A test whose test suite name or test name matches this filter is -// disabled and not run. -static const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*"; - -// A test suite whose name matches this filter is considered a death -// test suite and will be run before test suites whose name doesn't -// match this filter. -static const char kDeathTestSuiteFilter[] = "*DeathTest:*DeathTest/*"; - -// A test filter that matches everything. -static const char kUniversalFilter[] = "*"; - -// The default output format. -static const char kDefaultOutputFormat[] = "xml"; -// The default output file. -static const char kDefaultOutputFile[] = "test_detail"; - -// The environment variable name for the test shard index. -static const char kTestShardIndex[] = "GTEST_SHARD_INDEX"; -// The environment variable name for the total number of test shards. -static const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS"; -// The environment variable name for the test shard status file. -static const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE"; - -namespace internal { - -// The text used in failure messages to indicate the start of the -// stack trace. -const char kStackTraceMarker[] = "\nStack trace:\n"; - -// g_help_flag is true if and only if the --help flag or an equivalent form -// is specified on the command line. -bool g_help_flag = false; - -// Utilty function to Open File for Writing -static FILE* OpenFileForWriting(const std::string& output_file) { - FILE* fileout = nullptr; - FilePath output_file_path(output_file); - FilePath output_dir(output_file_path.RemoveFileName()); - - if (output_dir.CreateDirectoriesRecursively()) { - fileout = posix::FOpen(output_file.c_str(), "w"); - } - if (fileout == nullptr) { - GTEST_LOG_(FATAL) << "Unable to open file \"" << output_file << "\""; - } - return fileout; -} - -} // namespace internal - -// Bazel passes in the argument to '--test_filter' via the TESTBRIDGE_TEST_ONLY -// environment variable. -static const char* GetDefaultFilter() { - const char* const testbridge_test_only = - internal::posix::GetEnv("TESTBRIDGE_TEST_ONLY"); - if (testbridge_test_only != nullptr) { - return testbridge_test_only; - } - return kUniversalFilter; -} - -// Bazel passes in the argument to '--test_runner_fail_fast' via the -// TESTBRIDGE_TEST_RUNNER_FAIL_FAST environment variable. -static bool GetDefaultFailFast() { - const char* const testbridge_test_runner_fail_fast = - internal::posix::GetEnv("TESTBRIDGE_TEST_RUNNER_FAIL_FAST"); - if (testbridge_test_runner_fail_fast != nullptr) { - return strcmp(testbridge_test_runner_fail_fast, "1") == 0; - } - return false; -} - -GTEST_DEFINE_bool_( - fail_fast, internal::BoolFromGTestEnv("fail_fast", GetDefaultFailFast()), - "True if and only if a test failure should stop further test execution."); - -GTEST_DEFINE_bool_( - also_run_disabled_tests, - internal::BoolFromGTestEnv("also_run_disabled_tests", false), - "Run disabled tests too, in addition to the tests normally being run."); - -GTEST_DEFINE_bool_( - break_on_failure, internal::BoolFromGTestEnv("break_on_failure", false), - "True if and only if a failed assertion should be a debugger " - "break-point."); - -GTEST_DEFINE_bool_(catch_exceptions, - internal::BoolFromGTestEnv("catch_exceptions", true), - "True if and only if " GTEST_NAME_ - " should catch exceptions and treat them as test failures."); - -GTEST_DEFINE_string_( - color, - internal::StringFromGTestEnv("color", "auto"), - "Whether to use colors in the output. Valid values: yes, no, " - "and auto. 'auto' means to use colors if the output is " - "being sent to a terminal and the TERM environment variable " - "is set to a terminal type that supports colors."); - -GTEST_DEFINE_string_( - filter, - internal::StringFromGTestEnv("filter", GetDefaultFilter()), - "A colon-separated list of glob (not regex) patterns " - "for filtering the tests to run, optionally followed by a " - "'-' and a : separated list of negative patterns (tests to " - "exclude). A test is run if it matches one of the positive " - "patterns and does not match any of the negative patterns."); - -GTEST_DEFINE_bool_( - install_failure_signal_handler, - internal::BoolFromGTestEnv("install_failure_signal_handler", false), - "If true and supported on the current platform, " GTEST_NAME_ " should " - "install a signal handler that dumps debugging information when fatal " - "signals are raised."); - -GTEST_DEFINE_bool_(list_tests, false, - "List all tests without running them."); - -// The net priority order after flag processing is thus: -// --gtest_output command line flag -// GTEST_OUTPUT environment variable -// XML_OUTPUT_FILE environment variable -// '' -GTEST_DEFINE_string_( - output, - internal::StringFromGTestEnv("output", - internal::OutputFlagAlsoCheckEnvVar().c_str()), - "A format (defaults to \"xml\" but can be specified to be \"json\"), " - "optionally followed by a colon and an output file name or directory. " - "A directory is indicated by a trailing pathname separator. " - "Examples: \"xml:filename.xml\", \"xml::directoryname/\". " - "If a directory is specified, output files will be created " - "within that directory, with file-names based on the test " - "executable's name and, if necessary, made unique by adding " - "digits."); - -GTEST_DEFINE_bool_( - brief, internal::BoolFromGTestEnv("brief", false), - "True if only test failures should be displayed in text output."); - -GTEST_DEFINE_bool_(print_time, internal::BoolFromGTestEnv("print_time", true), - "True if and only if " GTEST_NAME_ - " should display elapsed time in text output."); - -GTEST_DEFINE_bool_(print_utf8, internal::BoolFromGTestEnv("print_utf8", true), - "True if and only if " GTEST_NAME_ - " prints UTF8 characters as text."); - -GTEST_DEFINE_int32_( - random_seed, - internal::Int32FromGTestEnv("random_seed", 0), - "Random number seed to use when shuffling test orders. Must be in range " - "[1, 99999], or 0 to use a seed based on the current time."); - -GTEST_DEFINE_int32_( - repeat, - internal::Int32FromGTestEnv("repeat", 1), - "How many times to repeat each test. Specify a negative number " - "for repeating forever. Useful for shaking out flaky tests."); - -GTEST_DEFINE_bool_(show_internal_stack_frames, false, - "True if and only if " GTEST_NAME_ - " should include internal stack frames when " - "printing test failure stack traces."); - -GTEST_DEFINE_bool_(shuffle, internal::BoolFromGTestEnv("shuffle", false), - "True if and only if " GTEST_NAME_ - " should randomize tests' order on every run."); - -GTEST_DEFINE_int32_( - stack_trace_depth, - internal::Int32FromGTestEnv("stack_trace_depth", kMaxStackTraceDepth), - "The maximum number of stack frames to print when an " - "assertion fails. The valid range is 0 through 100, inclusive."); - -GTEST_DEFINE_string_( - stream_result_to, - internal::StringFromGTestEnv("stream_result_to", ""), - "This flag specifies the host name and the port number on which to stream " - "test results. Example: \"localhost:555\". The flag is effective only on " - "Linux."); - -GTEST_DEFINE_bool_( - throw_on_failure, - internal::BoolFromGTestEnv("throw_on_failure", false), - "When this flag is specified, a failed assertion will throw an exception " - "if exceptions are enabled or exit the program with a non-zero code " - "otherwise. For use with an external test framework."); - -#if GTEST_USE_OWN_FLAGFILE_FLAG_ -GTEST_DEFINE_string_( - flagfile, - internal::StringFromGTestEnv("flagfile", ""), - "This flag specifies the flagfile to read command-line flags from."); -#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ - -namespace internal { - -// Generates a random number from [0, range), using a Linear -// Congruential Generator (LCG). Crashes if 'range' is 0 or greater -// than kMaxRange. -uint32_t Random::Generate(uint32_t range) { - // These constants are the same as are used in glibc's rand(3). - // Use wider types than necessary to prevent unsigned overflow diagnostics. - state_ = static_cast(1103515245ULL*state_ + 12345U) % kMaxRange; - - GTEST_CHECK_(range > 0) - << "Cannot generate a number in the range [0, 0)."; - GTEST_CHECK_(range <= kMaxRange) - << "Generation of a number in [0, " << range << ") was requested, " - << "but this can only generate numbers in [0, " << kMaxRange << ")."; - - // Converting via modulus introduces a bit of downward bias, but - // it's simple, and a linear congruential generator isn't too good - // to begin with. - return state_ % range; -} - -// GTestIsInitialized() returns true if and only if the user has initialized -// Google Test. Useful for catching the user mistake of not initializing -// Google Test before calling RUN_ALL_TESTS(). -static bool GTestIsInitialized() { return GetArgvs().size() > 0; } - -// Iterates over a vector of TestSuites, keeping a running sum of the -// results of calling a given int-returning method on each. -// Returns the sum. -static int SumOverTestSuiteList(const std::vector& case_list, - int (TestSuite::*method)() const) { - int sum = 0; - for (size_t i = 0; i < case_list.size(); i++) { - sum += (case_list[i]->*method)(); - } - return sum; -} - -// Returns true if and only if the test suite passed. -static bool TestSuitePassed(const TestSuite* test_suite) { - return test_suite->should_run() && test_suite->Passed(); -} - -// Returns true if and only if the test suite failed. -static bool TestSuiteFailed(const TestSuite* test_suite) { - return test_suite->should_run() && test_suite->Failed(); -} - -// Returns true if and only if test_suite contains at least one test that -// should run. -static bool ShouldRunTestSuite(const TestSuite* test_suite) { - return test_suite->should_run(); -} - -// AssertHelper constructor. -AssertHelper::AssertHelper(TestPartResult::Type type, - const char* file, - int line, - const char* message) - : data_(new AssertHelperData(type, file, line, message)) { -} - -AssertHelper::~AssertHelper() { - delete data_; -} - -// Message assignment, for assertion streaming support. -void AssertHelper::operator=(const Message& message) const { - UnitTest::GetInstance()-> - AddTestPartResult(data_->type, data_->file, data_->line, - AppendUserMessage(data_->message, message), - UnitTest::GetInstance()->impl() - ->CurrentOsStackTraceExceptTop(1) - // Skips the stack frame for this function itself. - ); // NOLINT -} - -namespace { - -// When TEST_P is found without a matching INSTANTIATE_TEST_SUITE_P -// to creates test cases for it, a synthetic test case is -// inserted to report ether an error or a log message. -// -// This configuration bit will likely be removed at some point. -constexpr bool kErrorOnUninstantiatedParameterizedTest = true; -constexpr bool kErrorOnUninstantiatedTypeParameterizedTest = true; - -// A test that fails at a given file/line location with a given message. -class FailureTest : public Test { - public: - explicit FailureTest(const CodeLocation& loc, std::string error_message, - bool as_error) - : loc_(loc), - error_message_(std::move(error_message)), - as_error_(as_error) {} - - void TestBody() override { - if (as_error_) { - AssertHelper(TestPartResult::kNonFatalFailure, loc_.file.c_str(), - loc_.line, "") = Message() << error_message_; - } else { - std::cout << error_message_ << std::endl; - } - } - - private: - const CodeLocation loc_; - const std::string error_message_; - const bool as_error_; -}; - - -} // namespace - -std::set* GetIgnoredParameterizedTestSuites() { - return UnitTest::GetInstance()->impl()->ignored_parameterized_test_suites(); -} - -// Add a given test_suit to the list of them allow to go un-instantiated. -MarkAsIgnored::MarkAsIgnored(const char* test_suite) { - GetIgnoredParameterizedTestSuites()->insert(test_suite); -} - -// If this parameterized test suite has no instantiations (and that -// has not been marked as okay), emit a test case reporting that. -void InsertSyntheticTestCase(const std::string& name, CodeLocation location, - bool has_test_p) { - const auto& ignored = *GetIgnoredParameterizedTestSuites(); - if (ignored.find(name) != ignored.end()) return; - - const char kMissingInstantiation[] = // - " is defined via TEST_P, but never instantiated. None of the test cases " - "will run. Either no INSTANTIATE_TEST_SUITE_P is provided or the only " - "ones provided expand to nothing." - "\n\n" - "Ideally, TEST_P definitions should only ever be included as part of " - "binaries that intend to use them. (As opposed to, for example, being " - "placed in a library that may be linked in to get other utilities.)"; - - const char kMissingTestCase[] = // - " is instantiated via INSTANTIATE_TEST_SUITE_P, but no tests are " - "defined via TEST_P . No test cases will run." - "\n\n" - "Ideally, INSTANTIATE_TEST_SUITE_P should only ever be invoked from " - "code that always depend on code that provides TEST_P. Failing to do " - "so is often an indication of dead code, e.g. the last TEST_P was " - "removed but the rest got left behind."; - - std::string message = - "Parameterized test suite " + name + - (has_test_p ? kMissingInstantiation : kMissingTestCase) + - "\n\n" - "To suppress this error for this test suite, insert the following line " - "(in a non-header) in the namespace it is defined in:" - "\n\n" - "GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(" + name + ");"; - - std::string full_name = "UninstantiatedParameterizedTestSuite<" + name + ">"; - RegisterTest( // - "GoogleTestVerification", full_name.c_str(), - nullptr, // No type parameter. - nullptr, // No value parameter. - location.file.c_str(), location.line, [message, location] { - return new FailureTest(location, message, - kErrorOnUninstantiatedParameterizedTest); - }); -} - -void RegisterTypeParameterizedTestSuite(const char* test_suite_name, - CodeLocation code_location) { - GetUnitTestImpl()->type_parameterized_test_registry().RegisterTestSuite( - test_suite_name, code_location); -} - -void RegisterTypeParameterizedTestSuiteInstantiation(const char* case_name) { - GetUnitTestImpl() - ->type_parameterized_test_registry() - .RegisterInstantiation(case_name); -} - -void TypeParameterizedTestSuiteRegistry::RegisterTestSuite( - const char* test_suite_name, CodeLocation code_location) { - suites_.emplace(std::string(test_suite_name), - TypeParameterizedTestSuiteInfo(code_location)); -} - -void TypeParameterizedTestSuiteRegistry::RegisterInstantiation( - const char* test_suite_name) { - auto it = suites_.find(std::string(test_suite_name)); - if (it != suites_.end()) { - it->second.instantiated = true; - } else { - GTEST_LOG_(ERROR) << "Unknown type parameterized test suit '" - << test_suite_name << "'"; - } -} - -void TypeParameterizedTestSuiteRegistry::CheckForInstantiations() { - const auto& ignored = *GetIgnoredParameterizedTestSuites(); - for (const auto& testcase : suites_) { - if (testcase.second.instantiated) continue; - if (ignored.find(testcase.first) != ignored.end()) continue; - - std::string message = - "Type parameterized test suite " + testcase.first + - " is defined via REGISTER_TYPED_TEST_SUITE_P, but never instantiated " - "via INSTANTIATE_TYPED_TEST_SUITE_P. None of the test cases will run." - "\n\n" - "Ideally, TYPED_TEST_P definitions should only ever be included as " - "part of binaries that intend to use them. (As opposed to, for " - "example, being placed in a library that may be linked in to get other " - "utilities.)" - "\n\n" - "To suppress this error for this test suite, insert the following line " - "(in a non-header) in the namespace it is defined in:" - "\n\n" - "GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(" + - testcase.first + ");"; - - std::string full_name = - "UninstantiatedTypeParameterizedTestSuite<" + testcase.first + ">"; - RegisterTest( // - "GoogleTestVerification", full_name.c_str(), - nullptr, // No type parameter. - nullptr, // No value parameter. - testcase.second.code_location.file.c_str(), - testcase.second.code_location.line, [message, testcase] { - return new FailureTest(testcase.second.code_location, message, - kErrorOnUninstantiatedTypeParameterizedTest); - }); - } -} - -// A copy of all command line arguments. Set by InitGoogleTest(). -static ::std::vector g_argvs; - -::std::vector GetArgvs() { -#if defined(GTEST_CUSTOM_GET_ARGVS_) - // GTEST_CUSTOM_GET_ARGVS_() may return a container of std::string or - // ::string. This code converts it to the appropriate type. - const auto& custom = GTEST_CUSTOM_GET_ARGVS_(); - return ::std::vector(custom.begin(), custom.end()); -#else // defined(GTEST_CUSTOM_GET_ARGVS_) - return g_argvs; -#endif // defined(GTEST_CUSTOM_GET_ARGVS_) -} - -// Returns the current application's name, removing directory path if that -// is present. -FilePath GetCurrentExecutableName() { - FilePath result; - -#if GTEST_OS_WINDOWS || GTEST_OS_OS2 - result.Set(FilePath(GetArgvs()[0]).RemoveExtension("exe")); -#else - result.Set(FilePath(GetArgvs()[0])); -#endif // GTEST_OS_WINDOWS - - return result.RemoveDirectoryName(); -} - -// Functions for processing the gtest_output flag. - -// Returns the output format, or "" for normal printed output. -std::string UnitTestOptions::GetOutputFormat() { - const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); - const char* const colon = strchr(gtest_output_flag, ':'); - return (colon == nullptr) - ? std::string(gtest_output_flag) - : std::string(gtest_output_flag, - static_cast(colon - gtest_output_flag)); -} - -// Returns the name of the requested output file, or the default if none -// was explicitly specified. -std::string UnitTestOptions::GetAbsolutePathToOutputFile() { - const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); - - std::string format = GetOutputFormat(); - if (format.empty()) - format = std::string(kDefaultOutputFormat); - - const char* const colon = strchr(gtest_output_flag, ':'); - if (colon == nullptr) - return internal::FilePath::MakeFileName( - internal::FilePath( - UnitTest::GetInstance()->original_working_dir()), - internal::FilePath(kDefaultOutputFile), 0, - format.c_str()).string(); - - internal::FilePath output_name(colon + 1); - if (!output_name.IsAbsolutePath()) - output_name = internal::FilePath::ConcatPaths( - internal::FilePath(UnitTest::GetInstance()->original_working_dir()), - internal::FilePath(colon + 1)); - - if (!output_name.IsDirectory()) - return output_name.string(); - - internal::FilePath result(internal::FilePath::GenerateUniqueFileName( - output_name, internal::GetCurrentExecutableName(), - GetOutputFormat().c_str())); - return result.string(); -} - -// Returns true if and only if the wildcard pattern matches the string. Each -// pattern consists of regular characters, single-character wildcards (?), and -// multi-character wildcards (*). -// -// This function implements a linear-time string globbing algorithm based on -// https://research.swtch.com/glob. -static bool PatternMatchesString(const std::string& name_str, - const char* pattern, const char* pattern_end) { - const char* name = name_str.c_str(); - const char* const name_begin = name; - const char* const name_end = name + name_str.size(); - - const char* pattern_next = pattern; - const char* name_next = name; - - while (pattern < pattern_end || name < name_end) { - if (pattern < pattern_end) { - switch (*pattern) { - default: // Match an ordinary character. - if (name < name_end && *name == *pattern) { - ++pattern; - ++name; - continue; - } - break; - case '?': // Match any single character. - if (name < name_end) { - ++pattern; - ++name; - continue; - } - break; - case '*': - // Match zero or more characters. Start by skipping over the wildcard - // and matching zero characters from name. If that fails, restart and - // match one more character than the last attempt. - pattern_next = pattern; - name_next = name + 1; - ++pattern; - continue; - } - } - // Failed to match a character. Restart if possible. - if (name_begin < name_next && name_next <= name_end) { - pattern = pattern_next; - name = name_next; - continue; - } - return false; - } - return true; -} - -bool UnitTestOptions::MatchesFilter(const std::string& name_str, - const char* filter) { - // The filter is a list of patterns separated by colons (:). - const char* pattern = filter; - while (true) { - // Find the bounds of this pattern. - const char* const next_sep = strchr(pattern, ':'); - const char* const pattern_end = - next_sep != nullptr ? next_sep : pattern + strlen(pattern); - - // Check if this pattern matches name_str. - if (PatternMatchesString(name_str, pattern, pattern_end)) { - return true; - } - - // Give up on this pattern. However, if we found a pattern separator (:), - // advance to the next pattern (skipping over the separator) and restart. - if (next_sep == nullptr) { - return false; - } - pattern = next_sep + 1; - } - return true; -} - -// Returns true if and only if the user-specified filter matches the test -// suite name and the test name. -bool UnitTestOptions::FilterMatchesTest(const std::string& test_suite_name, - const std::string& test_name) { - const std::string& full_name = test_suite_name + "." + test_name.c_str(); - - // Split --gtest_filter at '-', if there is one, to separate into - // positive filter and negative filter portions - const char* const p = GTEST_FLAG(filter).c_str(); - const char* const dash = strchr(p, '-'); - std::string positive; - std::string negative; - if (dash == nullptr) { - positive = GTEST_FLAG(filter).c_str(); // Whole string is a positive filter - negative = ""; - } else { - positive = std::string(p, dash); // Everything up to the dash - negative = std::string(dash + 1); // Everything after the dash - if (positive.empty()) { - // Treat '-test1' as the same as '*-test1' - positive = kUniversalFilter; - } - } - - // A filter is a colon-separated list of patterns. It matches a - // test if any pattern in it matches the test. - return (MatchesFilter(full_name, positive.c_str()) && - !MatchesFilter(full_name, negative.c_str())); -} - -#if GTEST_HAS_SEH -// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the -// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. -// This function is useful as an __except condition. -int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) { - // Google Test should handle a SEH exception if: - // 1. the user wants it to, AND - // 2. this is not a breakpoint exception, AND - // 3. this is not a C++ exception (VC++ implements them via SEH, - // apparently). - // - // SEH exception code for C++ exceptions. - // (see http://support.microsoft.com/kb/185294 for more information). - const DWORD kCxxExceptionCode = 0xe06d7363; - - bool should_handle = true; - - if (!GTEST_FLAG(catch_exceptions)) - should_handle = false; - else if (exception_code == EXCEPTION_BREAKPOINT) - should_handle = false; - else if (exception_code == kCxxExceptionCode) - should_handle = false; - - return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; -} -#endif // GTEST_HAS_SEH - -} // namespace internal - -// The c'tor sets this object as the test part result reporter used by -// Google Test. The 'result' parameter specifies where to report the -// results. Intercepts only failures from the current thread. -ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( - TestPartResultArray* result) - : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD), - result_(result) { - Init(); -} - -// The c'tor sets this object as the test part result reporter used by -// Google Test. The 'result' parameter specifies where to report the -// results. -ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( - InterceptMode intercept_mode, TestPartResultArray* result) - : intercept_mode_(intercept_mode), - result_(result) { - Init(); -} - -void ScopedFakeTestPartResultReporter::Init() { - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - if (intercept_mode_ == INTERCEPT_ALL_THREADS) { - old_reporter_ = impl->GetGlobalTestPartResultReporter(); - impl->SetGlobalTestPartResultReporter(this); - } else { - old_reporter_ = impl->GetTestPartResultReporterForCurrentThread(); - impl->SetTestPartResultReporterForCurrentThread(this); - } -} - -// The d'tor restores the test part result reporter used by Google Test -// before. -ScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() { - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - if (intercept_mode_ == INTERCEPT_ALL_THREADS) { - impl->SetGlobalTestPartResultReporter(old_reporter_); - } else { - impl->SetTestPartResultReporterForCurrentThread(old_reporter_); - } -} - -// Increments the test part result count and remembers the result. -// This method is from the TestPartResultReporterInterface interface. -void ScopedFakeTestPartResultReporter::ReportTestPartResult( - const TestPartResult& result) { - result_->Append(result); -} - -namespace internal { - -// Returns the type ID of ::testing::Test. We should always call this -// instead of GetTypeId< ::testing::Test>() to get the type ID of -// testing::Test. This is to work around a suspected linker bug when -// using Google Test as a framework on Mac OS X. The bug causes -// GetTypeId< ::testing::Test>() to return different values depending -// on whether the call is from the Google Test framework itself or -// from user test code. GetTestTypeId() is guaranteed to always -// return the same value, as it always calls GetTypeId<>() from the -// gtest.cc, which is within the Google Test framework. -TypeId GetTestTypeId() { - return GetTypeId(); -} - -// The value of GetTestTypeId() as seen from within the Google Test -// library. This is solely for testing GetTestTypeId(). -extern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId(); - -// This predicate-formatter checks that 'results' contains a test part -// failure of the given type and that the failure message contains the -// given substring. -static AssertionResult HasOneFailure(const char* /* results_expr */, - const char* /* type_expr */, - const char* /* substr_expr */, - const TestPartResultArray& results, - TestPartResult::Type type, - const std::string& substr) { - const std::string expected(type == TestPartResult::kFatalFailure ? - "1 fatal failure" : - "1 non-fatal failure"); - Message msg; - if (results.size() != 1) { - msg << "Expected: " << expected << "\n" - << " Actual: " << results.size() << " failures"; - for (int i = 0; i < results.size(); i++) { - msg << "\n" << results.GetTestPartResult(i); - } - return AssertionFailure() << msg; - } - - const TestPartResult& r = results.GetTestPartResult(0); - if (r.type() != type) { - return AssertionFailure() << "Expected: " << expected << "\n" - << " Actual:\n" - << r; - } - - if (strstr(r.message(), substr.c_str()) == nullptr) { - return AssertionFailure() << "Expected: " << expected << " containing \"" - << substr << "\"\n" - << " Actual:\n" - << r; - } - - return AssertionSuccess(); -} - -// The constructor of SingleFailureChecker remembers where to look up -// test part results, what type of failure we expect, and what -// substring the failure message should contain. -SingleFailureChecker::SingleFailureChecker(const TestPartResultArray* results, - TestPartResult::Type type, - const std::string& substr) - : results_(results), type_(type), substr_(substr) {} - -// The destructor of SingleFailureChecker verifies that the given -// TestPartResultArray contains exactly one failure that has the given -// type and contains the given substring. If that's not the case, a -// non-fatal failure will be generated. -SingleFailureChecker::~SingleFailureChecker() { - EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_); -} - -DefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter( - UnitTestImpl* unit_test) : unit_test_(unit_test) {} - -void DefaultGlobalTestPartResultReporter::ReportTestPartResult( - const TestPartResult& result) { - unit_test_->current_test_result()->AddTestPartResult(result); - unit_test_->listeners()->repeater()->OnTestPartResult(result); -} - -DefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter( - UnitTestImpl* unit_test) : unit_test_(unit_test) {} - -void DefaultPerThreadTestPartResultReporter::ReportTestPartResult( - const TestPartResult& result) { - unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result); -} - -// Returns the global test part result reporter. -TestPartResultReporterInterface* -UnitTestImpl::GetGlobalTestPartResultReporter() { - internal::MutexLock lock(&global_test_part_result_reporter_mutex_); - return global_test_part_result_repoter_; -} - -// Sets the global test part result reporter. -void UnitTestImpl::SetGlobalTestPartResultReporter( - TestPartResultReporterInterface* reporter) { - internal::MutexLock lock(&global_test_part_result_reporter_mutex_); - global_test_part_result_repoter_ = reporter; -} - -// Returns the test part result reporter for the current thread. -TestPartResultReporterInterface* -UnitTestImpl::GetTestPartResultReporterForCurrentThread() { - return per_thread_test_part_result_reporter_.get(); -} - -// Sets the test part result reporter for the current thread. -void UnitTestImpl::SetTestPartResultReporterForCurrentThread( - TestPartResultReporterInterface* reporter) { - per_thread_test_part_result_reporter_.set(reporter); -} - -// Gets the number of successful test suites. -int UnitTestImpl::successful_test_suite_count() const { - return CountIf(test_suites_, TestSuitePassed); -} - -// Gets the number of failed test suites. -int UnitTestImpl::failed_test_suite_count() const { - return CountIf(test_suites_, TestSuiteFailed); -} - -// Gets the number of all test suites. -int UnitTestImpl::total_test_suite_count() const { - return static_cast(test_suites_.size()); -} - -// Gets the number of all test suites that contain at least one test -// that should run. -int UnitTestImpl::test_suite_to_run_count() const { - return CountIf(test_suites_, ShouldRunTestSuite); -} - -// Gets the number of successful tests. -int UnitTestImpl::successful_test_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::successful_test_count); -} - -// Gets the number of skipped tests. -int UnitTestImpl::skipped_test_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::skipped_test_count); -} - -// Gets the number of failed tests. -int UnitTestImpl::failed_test_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::failed_test_count); -} - -// Gets the number of disabled tests that will be reported in the XML report. -int UnitTestImpl::reportable_disabled_test_count() const { - return SumOverTestSuiteList(test_suites_, - &TestSuite::reportable_disabled_test_count); -} - -// Gets the number of disabled tests. -int UnitTestImpl::disabled_test_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::disabled_test_count); -} - -// Gets the number of tests to be printed in the XML report. -int UnitTestImpl::reportable_test_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::reportable_test_count); -} - -// Gets the number of all tests. -int UnitTestImpl::total_test_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::total_test_count); -} - -// Gets the number of tests that should run. -int UnitTestImpl::test_to_run_count() const { - return SumOverTestSuiteList(test_suites_, &TestSuite::test_to_run_count); -} - -// Returns the current OS stack trace as an std::string. -// -// The maximum number of stack frames to be included is specified by -// the gtest_stack_trace_depth flag. The skip_count parameter -// specifies the number of top frames to be skipped, which doesn't -// count against the number of frames to be included. -// -// For example, if Foo() calls Bar(), which in turn calls -// CurrentOsStackTraceExceptTop(1), Foo() will be included in the -// trace but Bar() and CurrentOsStackTraceExceptTop() won't. -std::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) { - return os_stack_trace_getter()->CurrentStackTrace( - static_cast(GTEST_FLAG(stack_trace_depth)), - skip_count + 1 - // Skips the user-specified number of frames plus this function - // itself. - ); // NOLINT -} - -// A helper class for measuring elapsed times. -class Timer { - public: - Timer() : start_(std::chrono::steady_clock::now()) {} - - // Return time elapsed in milliseconds since the timer was created. - TimeInMillis Elapsed() { - return std::chrono::duration_cast( - std::chrono::steady_clock::now() - start_) - .count(); - } - - private: - std::chrono::steady_clock::time_point start_; -}; - -// Returns a timestamp as milliseconds since the epoch. Note this time may jump -// around subject to adjustments by the system, to measure elapsed time use -// Timer instead. -TimeInMillis GetTimeInMillis() { - return std::chrono::duration_cast( - std::chrono::system_clock::now() - - std::chrono::system_clock::from_time_t(0)) - .count(); -} - -// Utilities - -// class String. - -#if GTEST_OS_WINDOWS_MOBILE -// Creates a UTF-16 wide string from the given ANSI string, allocating -// memory using new. The caller is responsible for deleting the return -// value using delete[]. Returns the wide string, or NULL if the -// input is NULL. -LPCWSTR String::AnsiToUtf16(const char* ansi) { - if (!ansi) return nullptr; - const int length = strlen(ansi); - const int unicode_length = - MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0); - WCHAR* unicode = new WCHAR[unicode_length + 1]; - MultiByteToWideChar(CP_ACP, 0, ansi, length, - unicode, unicode_length); - unicode[unicode_length] = 0; - return unicode; -} - -// Creates an ANSI string from the given wide string, allocating -// memory using new. The caller is responsible for deleting the return -// value using delete[]. Returns the ANSI string, or NULL if the -// input is NULL. -const char* String::Utf16ToAnsi(LPCWSTR utf16_str) { - if (!utf16_str) return nullptr; - const int ansi_length = WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, nullptr, - 0, nullptr, nullptr); - char* ansi = new char[ansi_length + 1]; - WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, ansi, ansi_length, nullptr, - nullptr); - ansi[ansi_length] = 0; - return ansi; -} - -#endif // GTEST_OS_WINDOWS_MOBILE - -// Compares two C strings. Returns true if and only if they have the same -// content. -// -// Unlike strcmp(), this function can handle NULL argument(s). A NULL -// C string is considered different to any non-NULL C string, -// including the empty string. -bool String::CStringEquals(const char * lhs, const char * rhs) { - if (lhs == nullptr) return rhs == nullptr; - - if (rhs == nullptr) return false; - - return strcmp(lhs, rhs) == 0; -} - -#if GTEST_HAS_STD_WSTRING - -// Converts an array of wide chars to a narrow string using the UTF-8 -// encoding, and streams the result to the given Message object. -static void StreamWideCharsToMessage(const wchar_t* wstr, size_t length, - Message* msg) { - for (size_t i = 0; i != length; ) { // NOLINT - if (wstr[i] != L'\0') { - *msg << WideStringToUtf8(wstr + i, static_cast(length - i)); - while (i != length && wstr[i] != L'\0') - i++; - } else { - *msg << '\0'; - i++; - } - } -} - -#endif // GTEST_HAS_STD_WSTRING - -void SplitString(const ::std::string& str, char delimiter, - ::std::vector< ::std::string>* dest) { - ::std::vector< ::std::string> parsed; - ::std::string::size_type pos = 0; - while (::testing::internal::AlwaysTrue()) { - const ::std::string::size_type colon = str.find(delimiter, pos); - if (colon == ::std::string::npos) { - parsed.push_back(str.substr(pos)); - break; - } else { - parsed.push_back(str.substr(pos, colon - pos)); - pos = colon + 1; - } - } - dest->swap(parsed); -} - -} // namespace internal - -// Constructs an empty Message. -// We allocate the stringstream separately because otherwise each use of -// ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's -// stack frame leading to huge stack frames in some cases; gcc does not reuse -// the stack space. -Message::Message() : ss_(new ::std::stringstream) { - // By default, we want there to be enough precision when printing - // a double to a Message. - *ss_ << std::setprecision(std::numeric_limits::digits10 + 2); -} - -// These two overloads allow streaming a wide C string to a Message -// using the UTF-8 encoding. -Message& Message::operator <<(const wchar_t* wide_c_str) { - return *this << internal::String::ShowWideCString(wide_c_str); -} -Message& Message::operator <<(wchar_t* wide_c_str) { - return *this << internal::String::ShowWideCString(wide_c_str); -} - -#if GTEST_HAS_STD_WSTRING -// Converts the given wide string to a narrow string using the UTF-8 -// encoding, and streams the result to this Message object. -Message& Message::operator <<(const ::std::wstring& wstr) { - internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); - return *this; -} -#endif // GTEST_HAS_STD_WSTRING - -// Gets the text streamed to this object so far as an std::string. -// Each '\0' character in the buffer is replaced with "\\0". -std::string Message::GetString() const { - return internal::StringStreamToString(ss_.get()); -} - -// AssertionResult constructors. -// Used in EXPECT_TRUE/FALSE(assertion_result). -AssertionResult::AssertionResult(const AssertionResult& other) - : success_(other.success_), - message_(other.message_.get() != nullptr - ? new ::std::string(*other.message_) - : static_cast< ::std::string*>(nullptr)) {} - -// Swaps two AssertionResults. -void AssertionResult::swap(AssertionResult& other) { - using std::swap; - swap(success_, other.success_); - swap(message_, other.message_); -} - -// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. -AssertionResult AssertionResult::operator!() const { - AssertionResult negation(!success_); - if (message_.get() != nullptr) negation << *message_; - return negation; -} - -// Makes a successful assertion result. -AssertionResult AssertionSuccess() { - return AssertionResult(true); -} - -// Makes a failed assertion result. -AssertionResult AssertionFailure() { - return AssertionResult(false); -} - -// Makes a failed assertion result with the given failure message. -// Deprecated; use AssertionFailure() << message. -AssertionResult AssertionFailure(const Message& message) { - return AssertionFailure() << message; -} - -namespace internal { - -namespace edit_distance { -std::vector CalculateOptimalEdits(const std::vector& left, - const std::vector& right) { - std::vector > costs( - left.size() + 1, std::vector(right.size() + 1)); - std::vector > best_move( - left.size() + 1, std::vector(right.size() + 1)); - - // Populate for empty right. - for (size_t l_i = 0; l_i < costs.size(); ++l_i) { - costs[l_i][0] = static_cast(l_i); - best_move[l_i][0] = kRemove; - } - // Populate for empty left. - for (size_t r_i = 1; r_i < costs[0].size(); ++r_i) { - costs[0][r_i] = static_cast(r_i); - best_move[0][r_i] = kAdd; - } - - for (size_t l_i = 0; l_i < left.size(); ++l_i) { - for (size_t r_i = 0; r_i < right.size(); ++r_i) { - if (left[l_i] == right[r_i]) { - // Found a match. Consume it. - costs[l_i + 1][r_i + 1] = costs[l_i][r_i]; - best_move[l_i + 1][r_i + 1] = kMatch; - continue; - } - - const double add = costs[l_i + 1][r_i]; - const double remove = costs[l_i][r_i + 1]; - const double replace = costs[l_i][r_i]; - if (add < remove && add < replace) { - costs[l_i + 1][r_i + 1] = add + 1; - best_move[l_i + 1][r_i + 1] = kAdd; - } else if (remove < add && remove < replace) { - costs[l_i + 1][r_i + 1] = remove + 1; - best_move[l_i + 1][r_i + 1] = kRemove; - } else { - // We make replace a little more expensive than add/remove to lower - // their priority. - costs[l_i + 1][r_i + 1] = replace + 1.00001; - best_move[l_i + 1][r_i + 1] = kReplace; - } - } - } - - // Reconstruct the best path. We do it in reverse order. - std::vector best_path; - for (size_t l_i = left.size(), r_i = right.size(); l_i > 0 || r_i > 0;) { - EditType move = best_move[l_i][r_i]; - best_path.push_back(move); - l_i -= move != kAdd; - r_i -= move != kRemove; - } - std::reverse(best_path.begin(), best_path.end()); - return best_path; -} - -namespace { - -// Helper class to convert string into ids with deduplication. -class InternalStrings { - public: - size_t GetId(const std::string& str) { - IdMap::iterator it = ids_.find(str); - if (it != ids_.end()) return it->second; - size_t id = ids_.size(); - return ids_[str] = id; - } - - private: - typedef std::map IdMap; - IdMap ids_; -}; - -} // namespace - -std::vector CalculateOptimalEdits( - const std::vector& left, - const std::vector& right) { - std::vector left_ids, right_ids; - { - InternalStrings intern_table; - for (size_t i = 0; i < left.size(); ++i) { - left_ids.push_back(intern_table.GetId(left[i])); - } - for (size_t i = 0; i < right.size(); ++i) { - right_ids.push_back(intern_table.GetId(right[i])); - } - } - return CalculateOptimalEdits(left_ids, right_ids); -} - -namespace { - -// Helper class that holds the state for one hunk and prints it out to the -// stream. -// It reorders adds/removes when possible to group all removes before all -// adds. It also adds the hunk header before printint into the stream. -class Hunk { - public: - Hunk(size_t left_start, size_t right_start) - : left_start_(left_start), - right_start_(right_start), - adds_(), - removes_(), - common_() {} - - void PushLine(char edit, const char* line) { - switch (edit) { - case ' ': - ++common_; - FlushEdits(); - hunk_.push_back(std::make_pair(' ', line)); - break; - case '-': - ++removes_; - hunk_removes_.push_back(std::make_pair('-', line)); - break; - case '+': - ++adds_; - hunk_adds_.push_back(std::make_pair('+', line)); - break; - } - } - - void PrintTo(std::ostream* os) { - PrintHeader(os); - FlushEdits(); - for (std::list >::const_iterator it = - hunk_.begin(); - it != hunk_.end(); ++it) { - *os << it->first << it->second << "\n"; - } - } - - bool has_edits() const { return adds_ || removes_; } - - private: - void FlushEdits() { - hunk_.splice(hunk_.end(), hunk_removes_); - hunk_.splice(hunk_.end(), hunk_adds_); - } - - // Print a unified diff header for one hunk. - // The format is - // "@@ -, +, @@" - // where the left/right parts are omitted if unnecessary. - void PrintHeader(std::ostream* ss) const { - *ss << "@@ "; - if (removes_) { - *ss << "-" << left_start_ << "," << (removes_ + common_); - } - if (removes_ && adds_) { - *ss << " "; - } - if (adds_) { - *ss << "+" << right_start_ << "," << (adds_ + common_); - } - *ss << " @@\n"; - } - - size_t left_start_, right_start_; - size_t adds_, removes_, common_; - std::list > hunk_, hunk_adds_, hunk_removes_; -}; - -} // namespace - -// Create a list of diff hunks in Unified diff format. -// Each hunk has a header generated by PrintHeader above plus a body with -// lines prefixed with ' ' for no change, '-' for deletion and '+' for -// addition. -// 'context' represents the desired unchanged prefix/suffix around the diff. -// If two hunks are close enough that their contexts overlap, then they are -// joined into one hunk. -std::string CreateUnifiedDiff(const std::vector& left, - const std::vector& right, - size_t context) { - const std::vector edits = CalculateOptimalEdits(left, right); - - size_t l_i = 0, r_i = 0, edit_i = 0; - std::stringstream ss; - while (edit_i < edits.size()) { - // Find first edit. - while (edit_i < edits.size() && edits[edit_i] == kMatch) { - ++l_i; - ++r_i; - ++edit_i; - } - - // Find the first line to include in the hunk. - const size_t prefix_context = std::min(l_i, context); - Hunk hunk(l_i - prefix_context + 1, r_i - prefix_context + 1); - for (size_t i = prefix_context; i > 0; --i) { - hunk.PushLine(' ', left[l_i - i].c_str()); - } - - // Iterate the edits until we found enough suffix for the hunk or the input - // is over. - size_t n_suffix = 0; - for (; edit_i < edits.size(); ++edit_i) { - if (n_suffix >= context) { - // Continue only if the next hunk is very close. - auto it = edits.begin() + static_cast(edit_i); - while (it != edits.end() && *it == kMatch) ++it; - if (it == edits.end() || - static_cast(it - edits.begin()) - edit_i >= context) { - // There is no next edit or it is too far away. - break; - } - } - - EditType edit = edits[edit_i]; - // Reset count when a non match is found. - n_suffix = edit == kMatch ? n_suffix + 1 : 0; - - if (edit == kMatch || edit == kRemove || edit == kReplace) { - hunk.PushLine(edit == kMatch ? ' ' : '-', left[l_i].c_str()); - } - if (edit == kAdd || edit == kReplace) { - hunk.PushLine('+', right[r_i].c_str()); - } - - // Advance indices, depending on edit type. - l_i += edit != kAdd; - r_i += edit != kRemove; - } - - if (!hunk.has_edits()) { - // We are done. We don't want this hunk. - break; - } - - hunk.PrintTo(&ss); - } - return ss.str(); -} - -} // namespace edit_distance - -namespace { - -// The string representation of the values received in EqFailure() are already -// escaped. Split them on escaped '\n' boundaries. Leave all other escaped -// characters the same. -std::vector SplitEscapedString(const std::string& str) { - std::vector lines; - size_t start = 0, end = str.size(); - if (end > 2 && str[0] == '"' && str[end - 1] == '"') { - ++start; - --end; - } - bool escaped = false; - for (size_t i = start; i + 1 < end; ++i) { - if (escaped) { - escaped = false; - if (str[i] == 'n') { - lines.push_back(str.substr(start, i - start - 1)); - start = i + 1; - } - } else { - escaped = str[i] == '\\'; - } - } - lines.push_back(str.substr(start, end - start)); - return lines; -} - -} // namespace - -// Constructs and returns the message for an equality assertion -// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. -// -// The first four parameters are the expressions used in the assertion -// and their values, as strings. For example, for ASSERT_EQ(foo, bar) -// where foo is 5 and bar is 6, we have: -// -// lhs_expression: "foo" -// rhs_expression: "bar" -// lhs_value: "5" -// rhs_value: "6" -// -// The ignoring_case parameter is true if and only if the assertion is a -// *_STRCASEEQ*. When it's true, the string "Ignoring case" will -// be inserted into the message. -AssertionResult EqFailure(const char* lhs_expression, - const char* rhs_expression, - const std::string& lhs_value, - const std::string& rhs_value, - bool ignoring_case) { - Message msg; - msg << "Expected equality of these values:"; - msg << "\n " << lhs_expression; - if (lhs_value != lhs_expression) { - msg << "\n Which is: " << lhs_value; - } - msg << "\n " << rhs_expression; - if (rhs_value != rhs_expression) { - msg << "\n Which is: " << rhs_value; - } - - if (ignoring_case) { - msg << "\nIgnoring case"; - } - - if (!lhs_value.empty() && !rhs_value.empty()) { - const std::vector lhs_lines = - SplitEscapedString(lhs_value); - const std::vector rhs_lines = - SplitEscapedString(rhs_value); - if (lhs_lines.size() > 1 || rhs_lines.size() > 1) { - msg << "\nWith diff:\n" - << edit_distance::CreateUnifiedDiff(lhs_lines, rhs_lines); - } - } - - return AssertionFailure() << msg; -} - -// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. -std::string GetBoolAssertionFailureMessage( - const AssertionResult& assertion_result, - const char* expression_text, - const char* actual_predicate_value, - const char* expected_predicate_value) { - const char* actual_message = assertion_result.message(); - Message msg; - msg << "Value of: " << expression_text - << "\n Actual: " << actual_predicate_value; - if (actual_message[0] != '\0') - msg << " (" << actual_message << ")"; - msg << "\nExpected: " << expected_predicate_value; - return msg.GetString(); -} - -// Helper function for implementing ASSERT_NEAR. -AssertionResult DoubleNearPredFormat(const char* expr1, - const char* expr2, - const char* abs_error_expr, - double val1, - double val2, - double abs_error) { - const double diff = fabs(val1 - val2); - if (diff <= abs_error) return AssertionSuccess(); - - // Find the value which is closest to zero. - const double min_abs = std::min(fabs(val1), fabs(val2)); - // Find the distance to the next double from that value. - const double epsilon = - nextafter(min_abs, std::numeric_limits::infinity()) - min_abs; - // Detect the case where abs_error is so small that EXPECT_NEAR is - // effectively the same as EXPECT_EQUAL, and give an informative error - // message so that the situation can be more easily understood without - // requiring exotic floating-point knowledge. - // Don't do an epsilon check if abs_error is zero because that implies - // that an equality check was actually intended. - if (!(std::isnan)(val1) && !(std::isnan)(val2) && abs_error > 0 && - abs_error < epsilon) { - return AssertionFailure() - << "The difference between " << expr1 << " and " << expr2 << " is " - << diff << ", where\n" - << expr1 << " evaluates to " << val1 << ",\n" - << expr2 << " evaluates to " << val2 << ".\nThe abs_error parameter " - << abs_error_expr << " evaluates to " << abs_error - << " which is smaller than the minimum distance between doubles for " - "numbers of this magnitude which is " - << epsilon - << ", thus making this EXPECT_NEAR check equivalent to " - "EXPECT_EQUAL. Consider using EXPECT_DOUBLE_EQ instead."; - } - return AssertionFailure() - << "The difference between " << expr1 << " and " << expr2 - << " is " << diff << ", which exceeds " << abs_error_expr << ", where\n" - << expr1 << " evaluates to " << val1 << ",\n" - << expr2 << " evaluates to " << val2 << ", and\n" - << abs_error_expr << " evaluates to " << abs_error << "."; -} - - -// Helper template for implementing FloatLE() and DoubleLE(). -template -AssertionResult FloatingPointLE(const char* expr1, - const char* expr2, - RawType val1, - RawType val2) { - // Returns success if val1 is less than val2, - if (val1 < val2) { - return AssertionSuccess(); - } - - // or if val1 is almost equal to val2. - const FloatingPoint lhs(val1), rhs(val2); - if (lhs.AlmostEquals(rhs)) { - return AssertionSuccess(); - } - - // Note that the above two checks will both fail if either val1 or - // val2 is NaN, as the IEEE floating-point standard requires that - // any predicate involving a NaN must return false. - - ::std::stringstream val1_ss; - val1_ss << std::setprecision(std::numeric_limits::digits10 + 2) - << val1; - - ::std::stringstream val2_ss; - val2_ss << std::setprecision(std::numeric_limits::digits10 + 2) - << val2; - - return AssertionFailure() - << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n" - << " Actual: " << StringStreamToString(&val1_ss) << " vs " - << StringStreamToString(&val2_ss); -} - -} // namespace internal - -// Asserts that val1 is less than, or almost equal to, val2. Fails -// otherwise. In particular, it fails if either val1 or val2 is NaN. -AssertionResult FloatLE(const char* expr1, const char* expr2, - float val1, float val2) { - return internal::FloatingPointLE(expr1, expr2, val1, val2); -} - -// Asserts that val1 is less than, or almost equal to, val2. Fails -// otherwise. In particular, it fails if either val1 or val2 is NaN. -AssertionResult DoubleLE(const char* expr1, const char* expr2, - double val1, double val2) { - return internal::FloatingPointLE(expr1, expr2, val1, val2); -} - -namespace internal { - -// The helper function for {ASSERT|EXPECT}_STREQ. -AssertionResult CmpHelperSTREQ(const char* lhs_expression, - const char* rhs_expression, - const char* lhs, - const char* rhs) { - if (String::CStringEquals(lhs, rhs)) { - return AssertionSuccess(); - } - - return EqFailure(lhs_expression, - rhs_expression, - PrintToString(lhs), - PrintToString(rhs), - false); -} - -// The helper function for {ASSERT|EXPECT}_STRCASEEQ. -AssertionResult CmpHelperSTRCASEEQ(const char* lhs_expression, - const char* rhs_expression, - const char* lhs, - const char* rhs) { - if (String::CaseInsensitiveCStringEquals(lhs, rhs)) { - return AssertionSuccess(); - } - - return EqFailure(lhs_expression, - rhs_expression, - PrintToString(lhs), - PrintToString(rhs), - true); -} - -// The helper function for {ASSERT|EXPECT}_STRNE. -AssertionResult CmpHelperSTRNE(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2) { - if (!String::CStringEquals(s1, s2)) { - return AssertionSuccess(); - } else { - return AssertionFailure() << "Expected: (" << s1_expression << ") != (" - << s2_expression << "), actual: \"" - << s1 << "\" vs \"" << s2 << "\""; - } -} - -// The helper function for {ASSERT|EXPECT}_STRCASENE. -AssertionResult CmpHelperSTRCASENE(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2) { - if (!String::CaseInsensitiveCStringEquals(s1, s2)) { - return AssertionSuccess(); - } else { - return AssertionFailure() - << "Expected: (" << s1_expression << ") != (" - << s2_expression << ") (ignoring case), actual: \"" - << s1 << "\" vs \"" << s2 << "\""; - } -} - -} // namespace internal - -namespace { - -// Helper functions for implementing IsSubString() and IsNotSubstring(). - -// This group of overloaded functions return true if and only if needle -// is a substring of haystack. NULL is considered a substring of -// itself only. - -bool IsSubstringPred(const char* needle, const char* haystack) { - if (needle == nullptr || haystack == nullptr) return needle == haystack; - - return strstr(haystack, needle) != nullptr; -} - -bool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) { - if (needle == nullptr || haystack == nullptr) return needle == haystack; - - return wcsstr(haystack, needle) != nullptr; -} - -// StringType here can be either ::std::string or ::std::wstring. -template -bool IsSubstringPred(const StringType& needle, - const StringType& haystack) { - return haystack.find(needle) != StringType::npos; -} - -// This function implements either IsSubstring() or IsNotSubstring(), -// depending on the value of the expected_to_be_substring parameter. -// StringType here can be const char*, const wchar_t*, ::std::string, -// or ::std::wstring. -template -AssertionResult IsSubstringImpl( - bool expected_to_be_substring, - const char* needle_expr, const char* haystack_expr, - const StringType& needle, const StringType& haystack) { - if (IsSubstringPred(needle, haystack) == expected_to_be_substring) - return AssertionSuccess(); - - const bool is_wide_string = sizeof(needle[0]) > 1; - const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; - return AssertionFailure() - << "Value of: " << needle_expr << "\n" - << " Actual: " << begin_string_quote << needle << "\"\n" - << "Expected: " << (expected_to_be_substring ? "" : "not ") - << "a substring of " << haystack_expr << "\n" - << "Which is: " << begin_string_quote << haystack << "\""; -} - -} // namespace - -// IsSubstring() and IsNotSubstring() check whether needle is a -// substring of haystack (NULL is considered a substring of itself -// only), and return an appropriate error message when they fail. - -AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const char* needle, const char* haystack) { - return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); -} - -AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const wchar_t* needle, const wchar_t* haystack) { - return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); -} - -AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const char* needle, const char* haystack) { - return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); -} - -AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const wchar_t* needle, const wchar_t* haystack) { - return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); -} - -AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::string& needle, const ::std::string& haystack) { - return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); -} - -AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::string& needle, const ::std::string& haystack) { - return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); -} - -#if GTEST_HAS_STD_WSTRING -AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::wstring& needle, const ::std::wstring& haystack) { - return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); -} - -AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::wstring& needle, const ::std::wstring& haystack) { - return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); -} -#endif // GTEST_HAS_STD_WSTRING - -namespace internal { - -#if GTEST_OS_WINDOWS - -namespace { - -// Helper function for IsHRESULT{SuccessFailure} predicates -AssertionResult HRESULTFailureHelper(const char* expr, - const char* expected, - long hr) { // NOLINT -# if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_TV_TITLE - - // Windows CE doesn't support FormatMessage. - const char error_text[] = ""; - -# else - - // Looks up the human-readable system message for the HRESULT code - // and since we're not passing any params to FormatMessage, we don't - // want inserts expanded. - const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS; - const DWORD kBufSize = 4096; - // Gets the system's human readable message string for this HRESULT. - char error_text[kBufSize] = { '\0' }; - DWORD message_length = ::FormatMessageA(kFlags, - 0, // no source, we're asking system - static_cast(hr), // the error - 0, // no line width restrictions - error_text, // output buffer - kBufSize, // buf size - nullptr); // no arguments for inserts - // Trims tailing white space (FormatMessage leaves a trailing CR-LF) - for (; message_length && IsSpace(error_text[message_length - 1]); - --message_length) { - error_text[message_length - 1] = '\0'; - } - -# endif // GTEST_OS_WINDOWS_MOBILE - - const std::string error_hex("0x" + String::FormatHexInt(hr)); - return ::testing::AssertionFailure() - << "Expected: " << expr << " " << expected << ".\n" - << " Actual: " << error_hex << " " << error_text << "\n"; -} - -} // namespace - -AssertionResult IsHRESULTSuccess(const char* expr, long hr) { // NOLINT - if (SUCCEEDED(hr)) { - return AssertionSuccess(); - } - return HRESULTFailureHelper(expr, "succeeds", hr); -} - -AssertionResult IsHRESULTFailure(const char* expr, long hr) { // NOLINT - if (FAILED(hr)) { - return AssertionSuccess(); - } - return HRESULTFailureHelper(expr, "fails", hr); -} - -#endif // GTEST_OS_WINDOWS - -// Utility functions for encoding Unicode text (wide strings) in -// UTF-8. - -// A Unicode code-point can have up to 21 bits, and is encoded in UTF-8 -// like this: -// -// Code-point length Encoding -// 0 - 7 bits 0xxxxxxx -// 8 - 11 bits 110xxxxx 10xxxxxx -// 12 - 16 bits 1110xxxx 10xxxxxx 10xxxxxx -// 17 - 21 bits 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - -// The maximum code-point a one-byte UTF-8 sequence can represent. -constexpr uint32_t kMaxCodePoint1 = (static_cast(1) << 7) - 1; - -// The maximum code-point a two-byte UTF-8 sequence can represent. -constexpr uint32_t kMaxCodePoint2 = (static_cast(1) << (5 + 6)) - 1; - -// The maximum code-point a three-byte UTF-8 sequence can represent. -constexpr uint32_t kMaxCodePoint3 = (static_cast(1) << (4 + 2*6)) - 1; - -// The maximum code-point a four-byte UTF-8 sequence can represent. -constexpr uint32_t kMaxCodePoint4 = (static_cast(1) << (3 + 3*6)) - 1; - -// Chops off the n lowest bits from a bit pattern. Returns the n -// lowest bits. As a side effect, the original bit pattern will be -// shifted to the right by n bits. -inline uint32_t ChopLowBits(uint32_t* bits, int n) { - const uint32_t low_bits = *bits & ((static_cast(1) << n) - 1); - *bits >>= n; - return low_bits; -} - -// Converts a Unicode code point to a narrow string in UTF-8 encoding. -// code_point parameter is of type uint32_t because wchar_t may not be -// wide enough to contain a code point. -// If the code_point is not a valid Unicode code point -// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted -// to "(Invalid Unicode 0xXXXXXXXX)". -std::string CodePointToUtf8(uint32_t code_point) { - if (code_point > kMaxCodePoint4) { - return "(Invalid Unicode 0x" + String::FormatHexUInt32(code_point) + ")"; - } - - char str[5]; // Big enough for the largest valid code point. - if (code_point <= kMaxCodePoint1) { - str[1] = '\0'; - str[0] = static_cast(code_point); // 0xxxxxxx - } else if (code_point <= kMaxCodePoint2) { - str[2] = '\0'; - str[1] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx - str[0] = static_cast(0xC0 | code_point); // 110xxxxx - } else if (code_point <= kMaxCodePoint3) { - str[3] = '\0'; - str[2] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx - str[1] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx - str[0] = static_cast(0xE0 | code_point); // 1110xxxx - } else { // code_point <= kMaxCodePoint4 - str[4] = '\0'; - str[3] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx - str[2] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx - str[1] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx - str[0] = static_cast(0xF0 | code_point); // 11110xxx - } - return str; -} - -// The following two functions only make sense if the system -// uses UTF-16 for wide string encoding. All supported systems -// with 16 bit wchar_t (Windows, Cygwin) do use UTF-16. - -// Determines if the arguments constitute UTF-16 surrogate pair -// and thus should be combined into a single Unicode code point -// using CreateCodePointFromUtf16SurrogatePair. -inline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) { - return sizeof(wchar_t) == 2 && - (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00; -} - -// Creates a Unicode code point from UTF16 surrogate pair. -inline uint32_t CreateCodePointFromUtf16SurrogatePair(wchar_t first, - wchar_t second) { - const auto first_u = static_cast(first); - const auto second_u = static_cast(second); - const uint32_t mask = (1 << 10) - 1; - return (sizeof(wchar_t) == 2) - ? (((first_u & mask) << 10) | (second_u & mask)) + 0x10000 - : - // This function should not be called when the condition is - // false, but we provide a sensible default in case it is. - first_u; -} - -// Converts a wide string to a narrow string in UTF-8 encoding. -// The wide string is assumed to have the following encoding: -// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin) -// UTF-32 if sizeof(wchar_t) == 4 (on Linux) -// Parameter str points to a null-terminated wide string. -// Parameter num_chars may additionally limit the number -// of wchar_t characters processed. -1 is used when the entire string -// should be processed. -// If the string contains code points that are not valid Unicode code points -// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output -// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding -// and contains invalid UTF-16 surrogate pairs, values in those pairs -// will be encoded as individual Unicode characters from Basic Normal Plane. -std::string WideStringToUtf8(const wchar_t* str, int num_chars) { - if (num_chars == -1) - num_chars = static_cast(wcslen(str)); - - ::std::stringstream stream; - for (int i = 0; i < num_chars; ++i) { - uint32_t unicode_code_point; - - if (str[i] == L'\0') { - break; - } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) { - unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i], - str[i + 1]); - i++; - } else { - unicode_code_point = static_cast(str[i]); - } - - stream << CodePointToUtf8(unicode_code_point); - } - return StringStreamToString(&stream); -} - -// Converts a wide C string to an std::string using the UTF-8 encoding. -// NULL will be converted to "(null)". -std::string String::ShowWideCString(const wchar_t * wide_c_str) { - if (wide_c_str == nullptr) return "(null)"; - - return internal::WideStringToUtf8(wide_c_str, -1); -} - -// Compares two wide C strings. Returns true if and only if they have the -// same content. -// -// Unlike wcscmp(), this function can handle NULL argument(s). A NULL -// C string is considered different to any non-NULL C string, -// including the empty string. -bool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) { - if (lhs == nullptr) return rhs == nullptr; - - if (rhs == nullptr) return false; - - return wcscmp(lhs, rhs) == 0; -} - -// Helper function for *_STREQ on wide strings. -AssertionResult CmpHelperSTREQ(const char* lhs_expression, - const char* rhs_expression, - const wchar_t* lhs, - const wchar_t* rhs) { - if (String::WideCStringEquals(lhs, rhs)) { - return AssertionSuccess(); - } - - return EqFailure(lhs_expression, - rhs_expression, - PrintToString(lhs), - PrintToString(rhs), - false); -} - -// Helper function for *_STRNE on wide strings. -AssertionResult CmpHelperSTRNE(const char* s1_expression, - const char* s2_expression, - const wchar_t* s1, - const wchar_t* s2) { - if (!String::WideCStringEquals(s1, s2)) { - return AssertionSuccess(); - } - - return AssertionFailure() << "Expected: (" << s1_expression << ") != (" - << s2_expression << "), actual: " - << PrintToString(s1) - << " vs " << PrintToString(s2); -} - -// Compares two C strings, ignoring case. Returns true if and only if they have -// the same content. -// -// Unlike strcasecmp(), this function can handle NULL argument(s). A -// NULL C string is considered different to any non-NULL C string, -// including the empty string. -bool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) { - if (lhs == nullptr) return rhs == nullptr; - if (rhs == nullptr) return false; - return posix::StrCaseCmp(lhs, rhs) == 0; -} - -// Compares two wide C strings, ignoring case. Returns true if and only if they -// have the same content. -// -// Unlike wcscasecmp(), this function can handle NULL argument(s). -// A NULL C string is considered different to any non-NULL wide C string, -// including the empty string. -// NB: The implementations on different platforms slightly differ. -// On windows, this method uses _wcsicmp which compares according to LC_CTYPE -// environment variable. On GNU platform this method uses wcscasecmp -// which compares according to LC_CTYPE category of the current locale. -// On MacOS X, it uses towlower, which also uses LC_CTYPE category of the -// current locale. -bool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs, - const wchar_t* rhs) { - if (lhs == nullptr) return rhs == nullptr; - - if (rhs == nullptr) return false; - -#if GTEST_OS_WINDOWS - return _wcsicmp(lhs, rhs) == 0; -#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID - return wcscasecmp(lhs, rhs) == 0; -#else - // Android, Mac OS X and Cygwin don't define wcscasecmp. - // Other unknown OSes may not define it either. - wint_t left, right; - do { - left = towlower(static_cast(*lhs++)); - right = towlower(static_cast(*rhs++)); - } while (left && left == right); - return left == right; -#endif // OS selector -} - -// Returns true if and only if str ends with the given suffix, ignoring case. -// Any string is considered to end with an empty suffix. -bool String::EndsWithCaseInsensitive( - const std::string& str, const std::string& suffix) { - const size_t str_len = str.length(); - const size_t suffix_len = suffix.length(); - return (str_len >= suffix_len) && - CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len, - suffix.c_str()); -} - -// Formats an int value as "%02d". -std::string String::FormatIntWidth2(int value) { - return FormatIntWidthN(value, 2); -} - -// Formats an int value to given width with leading zeros. -std::string String::FormatIntWidthN(int value, int width) { - std::stringstream ss; - ss << std::setfill('0') << std::setw(width) << value; - return ss.str(); -} - -// Formats an int value as "%X". -std::string String::FormatHexUInt32(uint32_t value) { - std::stringstream ss; - ss << std::hex << std::uppercase << value; - return ss.str(); -} - -// Formats an int value as "%X". -std::string String::FormatHexInt(int value) { - return FormatHexUInt32(static_cast(value)); -} - -// Formats a byte as "%02X". -std::string String::FormatByte(unsigned char value) { - std::stringstream ss; - ss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase - << static_cast(value); - return ss.str(); -} - -// Converts the buffer in a stringstream to an std::string, converting NUL -// bytes to "\\0" along the way. -std::string StringStreamToString(::std::stringstream* ss) { - const ::std::string& str = ss->str(); - const char* const start = str.c_str(); - const char* const end = start + str.length(); - - std::string result; - result.reserve(static_cast(2 * (end - start))); - for (const char* ch = start; ch != end; ++ch) { - if (*ch == '\0') { - result += "\\0"; // Replaces NUL with "\\0"; - } else { - result += *ch; - } - } - - return result; -} - -// Appends the user-supplied message to the Google-Test-generated message. -std::string AppendUserMessage(const std::string& gtest_msg, - const Message& user_msg) { - // Appends the user message if it's non-empty. - const std::string user_msg_string = user_msg.GetString(); - if (user_msg_string.empty()) { - return gtest_msg; - } - if (gtest_msg.empty()) { - return user_msg_string; - } - return gtest_msg + "\n" + user_msg_string; -} - -} // namespace internal - -// class TestResult - -// Creates an empty TestResult. -TestResult::TestResult() - : death_test_count_(0), start_timestamp_(0), elapsed_time_(0) {} - -// D'tor. -TestResult::~TestResult() { -} - -// Returns the i-th test part result among all the results. i can -// range from 0 to total_part_count() - 1. If i is not in that range, -// aborts the program. -const TestPartResult& TestResult::GetTestPartResult(int i) const { - if (i < 0 || i >= total_part_count()) - internal::posix::Abort(); - return test_part_results_.at(static_cast(i)); -} - -// Returns the i-th test property. i can range from 0 to -// test_property_count() - 1. If i is not in that range, aborts the -// program. -const TestProperty& TestResult::GetTestProperty(int i) const { - if (i < 0 || i >= test_property_count()) - internal::posix::Abort(); - return test_properties_.at(static_cast(i)); -} - -// Clears the test part results. -void TestResult::ClearTestPartResults() { - test_part_results_.clear(); -} - -// Adds a test part result to the list. -void TestResult::AddTestPartResult(const TestPartResult& test_part_result) { - test_part_results_.push_back(test_part_result); -} - -// Adds a test property to the list. If a property with the same key as the -// supplied property is already represented, the value of this test_property -// replaces the old value for that key. -void TestResult::RecordProperty(const std::string& xml_element, - const TestProperty& test_property) { - if (!ValidateTestProperty(xml_element, test_property)) { - return; - } - internal::MutexLock lock(&test_properties_mutex_); - const std::vector::iterator property_with_matching_key = - std::find_if(test_properties_.begin(), test_properties_.end(), - internal::TestPropertyKeyIs(test_property.key())); - if (property_with_matching_key == test_properties_.end()) { - test_properties_.push_back(test_property); - return; - } - property_with_matching_key->SetValue(test_property.value()); -} - -// The list of reserved attributes used in the element of XML -// output. -static const char* const kReservedTestSuitesAttributes[] = { - "disabled", - "errors", - "failures", - "name", - "random_seed", - "tests", - "time", - "timestamp" -}; - -// The list of reserved attributes used in the element of XML -// output. -static const char* const kReservedTestSuiteAttributes[] = { - "disabled", "errors", "failures", "name", - "tests", "time", "timestamp", "skipped"}; - -// The list of reserved attributes used in the element of XML output. -static const char* const kReservedTestCaseAttributes[] = { - "classname", "name", "status", "time", "type_param", - "value_param", "file", "line"}; - -// Use a slightly different set for allowed output to ensure existing tests can -// still RecordProperty("result") or "RecordProperty(timestamp") -static const char* const kReservedOutputTestCaseAttributes[] = { - "classname", "name", "status", "time", "type_param", - "value_param", "file", "line", "result", "timestamp"}; - -template -std::vector ArrayAsVector(const char* const (&array)[kSize]) { - return std::vector(array, array + kSize); -} - -static std::vector GetReservedAttributesForElement( - const std::string& xml_element) { - if (xml_element == "testsuites") { - return ArrayAsVector(kReservedTestSuitesAttributes); - } else if (xml_element == "testsuite") { - return ArrayAsVector(kReservedTestSuiteAttributes); - } else if (xml_element == "testcase") { - return ArrayAsVector(kReservedTestCaseAttributes); - } else { - GTEST_CHECK_(false) << "Unrecognized xml_element provided: " << xml_element; - } - // This code is unreachable but some compilers may not realizes that. - return std::vector(); -} - -// TODO(jdesprez): Merge the two getReserved attributes once skip is improved -static std::vector GetReservedOutputAttributesForElement( - const std::string& xml_element) { - if (xml_element == "testsuites") { - return ArrayAsVector(kReservedTestSuitesAttributes); - } else if (xml_element == "testsuite") { - return ArrayAsVector(kReservedTestSuiteAttributes); - } else if (xml_element == "testcase") { - return ArrayAsVector(kReservedOutputTestCaseAttributes); - } else { - GTEST_CHECK_(false) << "Unrecognized xml_element provided: " << xml_element; - } - // This code is unreachable but some compilers may not realizes that. - return std::vector(); -} - -static std::string FormatWordList(const std::vector& words) { - Message word_list; - for (size_t i = 0; i < words.size(); ++i) { - if (i > 0 && words.size() > 2) { - word_list << ", "; - } - if (i == words.size() - 1) { - word_list << "and "; - } - word_list << "'" << words[i] << "'"; - } - return word_list.GetString(); -} - -static bool ValidateTestPropertyName( - const std::string& property_name, - const std::vector& reserved_names) { - if (std::find(reserved_names.begin(), reserved_names.end(), property_name) != - reserved_names.end()) { - ADD_FAILURE() << "Reserved key used in RecordProperty(): " << property_name - << " (" << FormatWordList(reserved_names) - << " are reserved by " << GTEST_NAME_ << ")"; - return false; - } - return true; -} - -// Adds a failure if the key is a reserved attribute of the element named -// xml_element. Returns true if the property is valid. -bool TestResult::ValidateTestProperty(const std::string& xml_element, - const TestProperty& test_property) { - return ValidateTestPropertyName(test_property.key(), - GetReservedAttributesForElement(xml_element)); -} - -// Clears the object. -void TestResult::Clear() { - test_part_results_.clear(); - test_properties_.clear(); - death_test_count_ = 0; - elapsed_time_ = 0; -} - -// Returns true off the test part was skipped. -static bool TestPartSkipped(const TestPartResult& result) { - return result.skipped(); -} - -// Returns true if and only if the test was skipped. -bool TestResult::Skipped() const { - return !Failed() && CountIf(test_part_results_, TestPartSkipped) > 0; -} - -// Returns true if and only if the test failed. -bool TestResult::Failed() const { - for (int i = 0; i < total_part_count(); ++i) { - if (GetTestPartResult(i).failed()) - return true; - } - return false; -} - -// Returns true if and only if the test part fatally failed. -static bool TestPartFatallyFailed(const TestPartResult& result) { - return result.fatally_failed(); -} - -// Returns true if and only if the test fatally failed. -bool TestResult::HasFatalFailure() const { - return CountIf(test_part_results_, TestPartFatallyFailed) > 0; -} - -// Returns true if and only if the test part non-fatally failed. -static bool TestPartNonfatallyFailed(const TestPartResult& result) { - return result.nonfatally_failed(); -} - -// Returns true if and only if the test has a non-fatal failure. -bool TestResult::HasNonfatalFailure() const { - return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0; -} - -// Gets the number of all test parts. This is the sum of the number -// of successful test parts and the number of failed test parts. -int TestResult::total_part_count() const { - return static_cast(test_part_results_.size()); -} - -// Returns the number of the test properties. -int TestResult::test_property_count() const { - return static_cast(test_properties_.size()); -} - -// class Test - -// Creates a Test object. - -// The c'tor saves the states of all flags. -Test::Test() - : gtest_flag_saver_(new GTEST_FLAG_SAVER_) { -} - -// The d'tor restores the states of all flags. The actual work is -// done by the d'tor of the gtest_flag_saver_ field, and thus not -// visible here. -Test::~Test() { -} - -// Sets up the test fixture. -// -// A sub-class may override this. -void Test::SetUp() { -} - -// Tears down the test fixture. -// -// A sub-class may override this. -void Test::TearDown() { -} - -// Allows user supplied key value pairs to be recorded for later output. -void Test::RecordProperty(const std::string& key, const std::string& value) { - UnitTest::GetInstance()->RecordProperty(key, value); -} - -// Allows user supplied key value pairs to be recorded for later output. -void Test::RecordProperty(const std::string& key, int value) { - Message value_message; - value_message << value; - RecordProperty(key, value_message.GetString().c_str()); -} - -namespace internal { - -void ReportFailureInUnknownLocation(TestPartResult::Type result_type, - const std::string& message) { - // This function is a friend of UnitTest and as such has access to - // AddTestPartResult. - UnitTest::GetInstance()->AddTestPartResult( - result_type, - nullptr, // No info about the source file where the exception occurred. - -1, // We have no info on which line caused the exception. - message, - ""); // No stack trace, either. -} - -} // namespace internal - -// Google Test requires all tests in the same test suite to use the same test -// fixture class. This function checks if the current test has the -// same fixture class as the first test in the current test suite. If -// yes, it returns true; otherwise it generates a Google Test failure and -// returns false. -bool Test::HasSameFixtureClass() { - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - const TestSuite* const test_suite = impl->current_test_suite(); - - // Info about the first test in the current test suite. - const TestInfo* const first_test_info = test_suite->test_info_list()[0]; - const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_; - const char* const first_test_name = first_test_info->name(); - - // Info about the current test. - const TestInfo* const this_test_info = impl->current_test_info(); - const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_; - const char* const this_test_name = this_test_info->name(); - - if (this_fixture_id != first_fixture_id) { - // Is the first test defined using TEST? - const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId(); - // Is this test defined using TEST? - const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId(); - - if (first_is_TEST || this_is_TEST) { - // Both TEST and TEST_F appear in same test suite, which is incorrect. - // Tell the user how to fix this. - - // Gets the name of the TEST and the name of the TEST_F. Note - // that first_is_TEST and this_is_TEST cannot both be true, as - // the fixture IDs are different for the two tests. - const char* const TEST_name = - first_is_TEST ? first_test_name : this_test_name; - const char* const TEST_F_name = - first_is_TEST ? this_test_name : first_test_name; - - ADD_FAILURE() - << "All tests in the same test suite must use the same test fixture\n" - << "class, so mixing TEST_F and TEST in the same test suite is\n" - << "illegal. In test suite " << this_test_info->test_suite_name() - << ",\n" - << "test " << TEST_F_name << " is defined using TEST_F but\n" - << "test " << TEST_name << " is defined using TEST. You probably\n" - << "want to change the TEST to TEST_F or move it to another test\n" - << "case."; - } else { - // Two fixture classes with the same name appear in two different - // namespaces, which is not allowed. Tell the user how to fix this. - ADD_FAILURE() - << "All tests in the same test suite must use the same test fixture\n" - << "class. However, in test suite " - << this_test_info->test_suite_name() << ",\n" - << "you defined test " << first_test_name << " and test " - << this_test_name << "\n" - << "using two different test fixture classes. This can happen if\n" - << "the two classes are from different namespaces or translation\n" - << "units and have the same name. You should probably rename one\n" - << "of the classes to put the tests into different test suites."; - } - return false; - } - - return true; -} - -#if GTEST_HAS_SEH - -// Adds an "exception thrown" fatal failure to the current test. This -// function returns its result via an output parameter pointer because VC++ -// prohibits creation of objects with destructors on stack in functions -// using __try (see error C2712). -static std::string* FormatSehExceptionMessage(DWORD exception_code, - const char* location) { - Message message; - message << "SEH exception with code 0x" << std::setbase(16) << - exception_code << std::setbase(10) << " thrown in " << location << "."; - - return new std::string(message.GetString()); -} - -#endif // GTEST_HAS_SEH - -namespace internal { - -#if GTEST_HAS_EXCEPTIONS - -// Adds an "exception thrown" fatal failure to the current test. -static std::string FormatCxxExceptionMessage(const char* description, - const char* location) { - Message message; - if (description != nullptr) { - message << "C++ exception with description \"" << description << "\""; - } else { - message << "Unknown C++ exception"; - } - message << " thrown in " << location << "."; - - return message.GetString(); -} - -static std::string PrintTestPartResultToString( - const TestPartResult& test_part_result); - -GoogleTestFailureException::GoogleTestFailureException( - const TestPartResult& failure) - : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {} - -#endif // GTEST_HAS_EXCEPTIONS - -// We put these helper functions in the internal namespace as IBM's xlC -// compiler rejects the code if they were declared static. - -// Runs the given method and handles SEH exceptions it throws, when -// SEH is supported; returns the 0-value for type Result in case of an -// SEH exception. (Microsoft compilers cannot handle SEH and C++ -// exceptions in the same function. Therefore, we provide a separate -// wrapper function for handling SEH exceptions.) -template -Result HandleSehExceptionsInMethodIfSupported( - T* object, Result (T::*method)(), const char* location) { -#if GTEST_HAS_SEH - __try { - return (object->*method)(); - } __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT - GetExceptionCode())) { - // We create the exception message on the heap because VC++ prohibits - // creation of objects with destructors on stack in functions using __try - // (see error C2712). - std::string* exception_message = FormatSehExceptionMessage( - GetExceptionCode(), location); - internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure, - *exception_message); - delete exception_message; - return static_cast(0); - } -#else - (void)location; - return (object->*method)(); -#endif // GTEST_HAS_SEH -} - -// Runs the given method and catches and reports C++ and/or SEH-style -// exceptions, if they are supported; returns the 0-value for type -// Result in case of an SEH exception. -template -Result HandleExceptionsInMethodIfSupported( - T* object, Result (T::*method)(), const char* location) { - // NOTE: The user code can affect the way in which Google Test handles - // exceptions by setting GTEST_FLAG(catch_exceptions), but only before - // RUN_ALL_TESTS() starts. It is technically possible to check the flag - // after the exception is caught and either report or re-throw the - // exception based on the flag's value: - // - // try { - // // Perform the test method. - // } catch (...) { - // if (GTEST_FLAG(catch_exceptions)) - // // Report the exception as failure. - // else - // throw; // Re-throws the original exception. - // } - // - // However, the purpose of this flag is to allow the program to drop into - // the debugger when the exception is thrown. On most platforms, once the - // control enters the catch block, the exception origin information is - // lost and the debugger will stop the program at the point of the - // re-throw in this function -- instead of at the point of the original - // throw statement in the code under test. For this reason, we perform - // the check early, sacrificing the ability to affect Google Test's - // exception handling in the method where the exception is thrown. - if (internal::GetUnitTestImpl()->catch_exceptions()) { -#if GTEST_HAS_EXCEPTIONS - try { - return HandleSehExceptionsInMethodIfSupported(object, method, location); - } catch (const AssertionException&) { // NOLINT - // This failure was reported already. - } catch (const internal::GoogleTestFailureException&) { // NOLINT - // This exception type can only be thrown by a failed Google - // Test assertion with the intention of letting another testing - // framework catch it. Therefore we just re-throw it. - throw; - } catch (const std::exception& e) { // NOLINT - internal::ReportFailureInUnknownLocation( - TestPartResult::kFatalFailure, - FormatCxxExceptionMessage(e.what(), location)); - } catch (...) { // NOLINT - internal::ReportFailureInUnknownLocation( - TestPartResult::kFatalFailure, - FormatCxxExceptionMessage(nullptr, location)); - } - return static_cast(0); -#else - return HandleSehExceptionsInMethodIfSupported(object, method, location); -#endif // GTEST_HAS_EXCEPTIONS - } else { - return (object->*method)(); - } -} - -} // namespace internal - -// Runs the test and updates the test result. -void Test::Run() { - if (!HasSameFixtureClass()) return; - - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - impl->os_stack_trace_getter()->UponLeavingGTest(); - internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()"); - // We will run the test only if SetUp() was successful and didn't call - // GTEST_SKIP(). - if (!HasFatalFailure() && !IsSkipped()) { - impl->os_stack_trace_getter()->UponLeavingGTest(); - internal::HandleExceptionsInMethodIfSupported( - this, &Test::TestBody, "the test body"); - } - - // However, we want to clean up as much as possible. Hence we will - // always call TearDown(), even if SetUp() or the test body has - // failed. - impl->os_stack_trace_getter()->UponLeavingGTest(); - internal::HandleExceptionsInMethodIfSupported( - this, &Test::TearDown, "TearDown()"); -} - -// Returns true if and only if the current test has a fatal failure. -bool Test::HasFatalFailure() { - return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure(); -} - -// Returns true if and only if the current test has a non-fatal failure. -bool Test::HasNonfatalFailure() { - return internal::GetUnitTestImpl()->current_test_result()-> - HasNonfatalFailure(); -} - -// Returns true if and only if the current test was skipped. -bool Test::IsSkipped() { - return internal::GetUnitTestImpl()->current_test_result()->Skipped(); -} - -// class TestInfo - -// Constructs a TestInfo object. It assumes ownership of the test factory -// object. -TestInfo::TestInfo(const std::string& a_test_suite_name, - const std::string& a_name, const char* a_type_param, - const char* a_value_param, - internal::CodeLocation a_code_location, - internal::TypeId fixture_class_id, - internal::TestFactoryBase* factory) - : test_suite_name_(a_test_suite_name), - name_(a_name), - type_param_(a_type_param ? new std::string(a_type_param) : nullptr), - value_param_(a_value_param ? new std::string(a_value_param) : nullptr), - location_(a_code_location), - fixture_class_id_(fixture_class_id), - should_run_(false), - is_disabled_(false), - matches_filter_(false), - is_in_another_shard_(false), - factory_(factory), - result_() {} - -// Destructs a TestInfo object. -TestInfo::~TestInfo() { delete factory_; } - -namespace internal { - -// Creates a new TestInfo object and registers it with Google Test; -// returns the created object. -// -// Arguments: -// -// test_suite_name: name of the test suite -// name: name of the test -// type_param: the name of the test's type parameter, or NULL if -// this is not a typed or a type-parameterized test. -// value_param: text representation of the test's value parameter, -// or NULL if this is not a value-parameterized test. -// code_location: code location where the test is defined -// fixture_class_id: ID of the test fixture class -// set_up_tc: pointer to the function that sets up the test suite -// tear_down_tc: pointer to the function that tears down the test suite -// factory: pointer to the factory that creates a test object. -// The newly created TestInfo instance will assume -// ownership of the factory object. -TestInfo* MakeAndRegisterTestInfo( - const char* test_suite_name, const char* name, const char* type_param, - const char* value_param, CodeLocation code_location, - TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, - TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory) { - TestInfo* const test_info = - new TestInfo(test_suite_name, name, type_param, value_param, - code_location, fixture_class_id, factory); - GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info); - return test_info; -} - -void ReportInvalidTestSuiteType(const char* test_suite_name, - CodeLocation code_location) { - Message errors; - errors - << "Attempted redefinition of test suite " << test_suite_name << ".\n" - << "All tests in the same test suite must use the same test fixture\n" - << "class. However, in test suite " << test_suite_name << ", you tried\n" - << "to define a test using a fixture class different from the one\n" - << "used earlier. This can happen if the two fixture classes are\n" - << "from different namespaces and have the same name. You should\n" - << "probably rename one of the classes to put the tests into different\n" - << "test suites."; - - GTEST_LOG_(ERROR) << FormatFileLocation(code_location.file.c_str(), - code_location.line) - << " " << errors.GetString(); -} -} // namespace internal - -namespace { - -// A predicate that checks the test name of a TestInfo against a known -// value. -// -// This is used for implementation of the TestSuite class only. We put -// it in the anonymous namespace to prevent polluting the outer -// namespace. -// -// TestNameIs is copyable. -class TestNameIs { - public: - // Constructor. - // - // TestNameIs has NO default constructor. - explicit TestNameIs(const char* name) - : name_(name) {} - - // Returns true if and only if the test name of test_info matches name_. - bool operator()(const TestInfo * test_info) const { - return test_info && test_info->name() == name_; - } - - private: - std::string name_; -}; - -} // namespace - -namespace internal { - -// This method expands all parameterized tests registered with macros TEST_P -// and INSTANTIATE_TEST_SUITE_P into regular tests and registers those. -// This will be done just once during the program runtime. -void UnitTestImpl::RegisterParameterizedTests() { - if (!parameterized_tests_registered_) { - parameterized_test_registry_.RegisterTests(); - type_parameterized_test_registry_.CheckForInstantiations(); - parameterized_tests_registered_ = true; - } -} - -} // namespace internal - -// Creates the test object, runs it, records its result, and then -// deletes it. -void TestInfo::Run() { - if (!should_run_) return; - - // Tells UnitTest where to store test result. - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - impl->set_current_test_info(this); - - TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); - - // Notifies the unit test event listeners that a test is about to start. - repeater->OnTestStart(*this); - - result_.set_start_timestamp(internal::GetTimeInMillis()); - internal::Timer timer; - - impl->os_stack_trace_getter()->UponLeavingGTest(); - - // Creates the test object. - Test* const test = internal::HandleExceptionsInMethodIfSupported( - factory_, &internal::TestFactoryBase::CreateTest, - "the test fixture's constructor"); - - // Runs the test if the constructor didn't generate a fatal failure or invoke - // GTEST_SKIP(). - // Note that the object will not be null - if (!Test::HasFatalFailure() && !Test::IsSkipped()) { - // This doesn't throw as all user code that can throw are wrapped into - // exception handling code. - test->Run(); - } - - if (test != nullptr) { - // Deletes the test object. - impl->os_stack_trace_getter()->UponLeavingGTest(); - internal::HandleExceptionsInMethodIfSupported( - test, &Test::DeleteSelf_, "the test fixture's destructor"); - } - - result_.set_elapsed_time(timer.Elapsed()); - - // Notifies the unit test event listener that a test has just finished. - repeater->OnTestEnd(*this); - - // Tells UnitTest to stop associating assertion results to this - // test. - impl->set_current_test_info(nullptr); -} - -// Skip and records a skipped test result for this object. -void TestInfo::Skip() { - if (!should_run_) return; - - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - impl->set_current_test_info(this); - - TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); - - // Notifies the unit test event listeners that a test is about to start. - repeater->OnTestStart(*this); - - const TestPartResult test_part_result = - TestPartResult(TestPartResult::kSkip, this->file(), this->line(), ""); - impl->GetTestPartResultReporterForCurrentThread()->ReportTestPartResult( - test_part_result); - - // Notifies the unit test event listener that a test has just finished. - repeater->OnTestEnd(*this); - impl->set_current_test_info(nullptr); -} - -// class TestSuite - -// Gets the number of successful tests in this test suite. -int TestSuite::successful_test_count() const { - return CountIf(test_info_list_, TestPassed); -} - -// Gets the number of successful tests in this test suite. -int TestSuite::skipped_test_count() const { - return CountIf(test_info_list_, TestSkipped); -} - -// Gets the number of failed tests in this test suite. -int TestSuite::failed_test_count() const { - return CountIf(test_info_list_, TestFailed); -} - -// Gets the number of disabled tests that will be reported in the XML report. -int TestSuite::reportable_disabled_test_count() const { - return CountIf(test_info_list_, TestReportableDisabled); -} - -// Gets the number of disabled tests in this test suite. -int TestSuite::disabled_test_count() const { - return CountIf(test_info_list_, TestDisabled); -} - -// Gets the number of tests to be printed in the XML report. -int TestSuite::reportable_test_count() const { - return CountIf(test_info_list_, TestReportable); -} - -// Get the number of tests in this test suite that should run. -int TestSuite::test_to_run_count() const { - return CountIf(test_info_list_, ShouldRunTest); -} - -// Gets the number of all tests. -int TestSuite::total_test_count() const { - return static_cast(test_info_list_.size()); -} - -// Creates a TestSuite with the given name. -// -// Arguments: -// -// a_name: name of the test suite -// a_type_param: the name of the test suite's type parameter, or NULL if -// this is not a typed or a type-parameterized test suite. -// set_up_tc: pointer to the function that sets up the test suite -// tear_down_tc: pointer to the function that tears down the test suite -TestSuite::TestSuite(const char* a_name, const char* a_type_param, - internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc) - : name_(a_name), - type_param_(a_type_param ? new std::string(a_type_param) : nullptr), - set_up_tc_(set_up_tc), - tear_down_tc_(tear_down_tc), - should_run_(false), - start_timestamp_(0), - elapsed_time_(0) {} - -// Destructor of TestSuite. -TestSuite::~TestSuite() { - // Deletes every Test in the collection. - ForEach(test_info_list_, internal::Delete); -} - -// Returns the i-th test among all the tests. i can range from 0 to -// total_test_count() - 1. If i is not in that range, returns NULL. -const TestInfo* TestSuite::GetTestInfo(int i) const { - const int index = GetElementOr(test_indices_, i, -1); - return index < 0 ? nullptr : test_info_list_[static_cast(index)]; -} - -// Returns the i-th test among all the tests. i can range from 0 to -// total_test_count() - 1. If i is not in that range, returns NULL. -TestInfo* TestSuite::GetMutableTestInfo(int i) { - const int index = GetElementOr(test_indices_, i, -1); - return index < 0 ? nullptr : test_info_list_[static_cast(index)]; -} - -// Adds a test to this test suite. Will delete the test upon -// destruction of the TestSuite object. -void TestSuite::AddTestInfo(TestInfo* test_info) { - test_info_list_.push_back(test_info); - test_indices_.push_back(static_cast(test_indices_.size())); -} - -// Runs every test in this TestSuite. -void TestSuite::Run() { - if (!should_run_) return; - - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - impl->set_current_test_suite(this); - - TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); - - // Call both legacy and the new API - repeater->OnTestSuiteStart(*this); -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - repeater->OnTestCaseStart(*this); -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - impl->os_stack_trace_getter()->UponLeavingGTest(); - internal::HandleExceptionsInMethodIfSupported( - this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()"); - - start_timestamp_ = internal::GetTimeInMillis(); - internal::Timer timer; - for (int i = 0; i < total_test_count(); i++) { - GetMutableTestInfo(i)->Run(); - if (GTEST_FLAG(fail_fast) && GetMutableTestInfo(i)->result()->Failed()) { - for (int j = i + 1; j < total_test_count(); j++) { - GetMutableTestInfo(j)->Skip(); - } - break; - } - } - elapsed_time_ = timer.Elapsed(); - - impl->os_stack_trace_getter()->UponLeavingGTest(); - internal::HandleExceptionsInMethodIfSupported( - this, &TestSuite::RunTearDownTestSuite, "TearDownTestSuite()"); - - // Call both legacy and the new API - repeater->OnTestSuiteEnd(*this); -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - repeater->OnTestCaseEnd(*this); -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - impl->set_current_test_suite(nullptr); -} - -// Skips all tests under this TestSuite. -void TestSuite::Skip() { - if (!should_run_) return; - - internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); - impl->set_current_test_suite(this); - - TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); - - // Call both legacy and the new API - repeater->OnTestSuiteStart(*this); -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - repeater->OnTestCaseStart(*this); -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - for (int i = 0; i < total_test_count(); i++) { - GetMutableTestInfo(i)->Skip(); - } - - // Call both legacy and the new API - repeater->OnTestSuiteEnd(*this); - // Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - repeater->OnTestCaseEnd(*this); -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - impl->set_current_test_suite(nullptr); -} - -// Clears the results of all tests in this test suite. -void TestSuite::ClearResult() { - ad_hoc_test_result_.Clear(); - ForEach(test_info_list_, TestInfo::ClearTestResult); -} - -// Shuffles the tests in this test suite. -void TestSuite::ShuffleTests(internal::Random* random) { - Shuffle(random, &test_indices_); -} - -// Restores the test order to before the first shuffle. -void TestSuite::UnshuffleTests() { - for (size_t i = 0; i < test_indices_.size(); i++) { - test_indices_[i] = static_cast(i); - } -} - -// Formats a countable noun. Depending on its quantity, either the -// singular form or the plural form is used. e.g. -// -// FormatCountableNoun(1, "formula", "formuli") returns "1 formula". -// FormatCountableNoun(5, "book", "books") returns "5 books". -static std::string FormatCountableNoun(int count, - const char * singular_form, - const char * plural_form) { - return internal::StreamableToString(count) + " " + - (count == 1 ? singular_form : plural_form); -} - -// Formats the count of tests. -static std::string FormatTestCount(int test_count) { - return FormatCountableNoun(test_count, "test", "tests"); -} - -// Formats the count of test suites. -static std::string FormatTestSuiteCount(int test_suite_count) { - return FormatCountableNoun(test_suite_count, "test suite", "test suites"); -} - -// Converts a TestPartResult::Type enum to human-friendly string -// representation. Both kNonFatalFailure and kFatalFailure are translated -// to "Failure", as the user usually doesn't care about the difference -// between the two when viewing the test result. -static const char * TestPartResultTypeToString(TestPartResult::Type type) { - switch (type) { - case TestPartResult::kSkip: - return "Skipped\n"; - case TestPartResult::kSuccess: - return "Success"; - - case TestPartResult::kNonFatalFailure: - case TestPartResult::kFatalFailure: -#ifdef _MSC_VER - return "error: "; -#else - return "Failure\n"; -#endif - default: - return "Unknown result type"; - } -} - -namespace internal { -namespace { -enum class GTestColor { kDefault, kRed, kGreen, kYellow }; -} // namespace - -// Prints a TestPartResult to an std::string. -static std::string PrintTestPartResultToString( - const TestPartResult& test_part_result) { - return (Message() - << internal::FormatFileLocation(test_part_result.file_name(), - test_part_result.line_number()) - << " " << TestPartResultTypeToString(test_part_result.type()) - << test_part_result.message()).GetString(); -} - -// Prints a TestPartResult. -static void PrintTestPartResult(const TestPartResult& test_part_result) { - const std::string& result = - PrintTestPartResultToString(test_part_result); - printf("%s\n", result.c_str()); - fflush(stdout); - // If the test program runs in Visual Studio or a debugger, the - // following statements add the test part result message to the Output - // window such that the user can double-click on it to jump to the - // corresponding source code location; otherwise they do nothing. -#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE - // We don't call OutputDebugString*() on Windows Mobile, as printing - // to stdout is done by OutputDebugString() there already - we don't - // want the same message printed twice. - ::OutputDebugStringA(result.c_str()); - ::OutputDebugStringA("\n"); -#endif -} - -// class PrettyUnitTestResultPrinter -#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && \ - !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW - -// Returns the character attribute for the given color. -static WORD GetColorAttribute(GTestColor color) { - switch (color) { - case GTestColor::kRed: - return FOREGROUND_RED; - case GTestColor::kGreen: - return FOREGROUND_GREEN; - case GTestColor::kYellow: - return FOREGROUND_RED | FOREGROUND_GREEN; - default: return 0; - } -} - -static int GetBitOffset(WORD color_mask) { - if (color_mask == 0) return 0; - - int bitOffset = 0; - while ((color_mask & 1) == 0) { - color_mask >>= 1; - ++bitOffset; - } - return bitOffset; -} - -static WORD GetNewColor(GTestColor color, WORD old_color_attrs) { - // Let's reuse the BG - static const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | - BACKGROUND_RED | BACKGROUND_INTENSITY; - static const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN | - FOREGROUND_RED | FOREGROUND_INTENSITY; - const WORD existing_bg = old_color_attrs & background_mask; - - WORD new_color = - GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY; - static const int bg_bitOffset = GetBitOffset(background_mask); - static const int fg_bitOffset = GetBitOffset(foreground_mask); - - if (((new_color & background_mask) >> bg_bitOffset) == - ((new_color & foreground_mask) >> fg_bitOffset)) { - new_color ^= FOREGROUND_INTENSITY; // invert intensity - } - return new_color; -} - -#else - -// Returns the ANSI color code for the given color. GTestColor::kDefault is -// an invalid input. -static const char* GetAnsiColorCode(GTestColor color) { - switch (color) { - case GTestColor::kRed: - return "1"; - case GTestColor::kGreen: - return "2"; - case GTestColor::kYellow: - return "3"; - default: - return nullptr; - } -} - -#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE - -// Returns true if and only if Google Test should use colors in the output. -bool ShouldUseColor(bool stdout_is_tty) { - const char* const gtest_color = GTEST_FLAG(color).c_str(); - - if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) { -#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW - // On Windows the TERM variable is usually not set, but the - // console there does support colors. - return stdout_is_tty; -#else - // On non-Windows platforms, we rely on the TERM variable. - const char* const term = posix::GetEnv("TERM"); - const bool term_supports_color = - String::CStringEquals(term, "xterm") || - String::CStringEquals(term, "xterm-color") || - String::CStringEquals(term, "xterm-256color") || - String::CStringEquals(term, "screen") || - String::CStringEquals(term, "screen-256color") || - String::CStringEquals(term, "tmux") || - String::CStringEquals(term, "tmux-256color") || - String::CStringEquals(term, "rxvt-unicode") || - String::CStringEquals(term, "rxvt-unicode-256color") || - String::CStringEquals(term, "linux") || - String::CStringEquals(term, "cygwin"); - return stdout_is_tty && term_supports_color; -#endif // GTEST_OS_WINDOWS - } - - return String::CaseInsensitiveCStringEquals(gtest_color, "yes") || - String::CaseInsensitiveCStringEquals(gtest_color, "true") || - String::CaseInsensitiveCStringEquals(gtest_color, "t") || - String::CStringEquals(gtest_color, "1"); - // We take "yes", "true", "t", and "1" as meaning "yes". If the - // value is neither one of these nor "auto", we treat it as "no" to - // be conservative. -} - -// Helpers for printing colored strings to stdout. Note that on Windows, we -// cannot simply emit special characters and have the terminal change colors. -// This routine must actually emit the characters rather than return a string -// that would be colored when printed, as can be done on Linux. - -GTEST_ATTRIBUTE_PRINTF_(2, 3) -static void ColoredPrintf(GTestColor color, const char *fmt, ...) { - va_list args; - va_start(args, fmt); - -#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \ - GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM) - const bool use_color = AlwaysFalse(); -#else - static const bool in_color_mode = - ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0); - const bool use_color = in_color_mode && (color != GTestColor::kDefault); -#endif // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS - - if (!use_color) { - vprintf(fmt, args); - va_end(args); - return; - } - -#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && \ - !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW - const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); - - // Gets the current text color. - CONSOLE_SCREEN_BUFFER_INFO buffer_info; - GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); - const WORD old_color_attrs = buffer_info.wAttributes; - const WORD new_color = GetNewColor(color, old_color_attrs); - - // We need to flush the stream buffers into the console before each - // SetConsoleTextAttribute call lest it affect the text that is already - // printed but has not yet reached the console. - fflush(stdout); - SetConsoleTextAttribute(stdout_handle, new_color); - - vprintf(fmt, args); - - fflush(stdout); - // Restores the text color. - SetConsoleTextAttribute(stdout_handle, old_color_attrs); -#else - printf("\033[0;3%sm", GetAnsiColorCode(color)); - vprintf(fmt, args); - printf("\033[m"); // Resets the terminal to default. -#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE - va_end(args); -} - -// Text printed in Google Test's text output and --gtest_list_tests -// output to label the type parameter and value parameter for a test. -static const char kTypeParamLabel[] = "TypeParam"; -static const char kValueParamLabel[] = "GetParam()"; - -static void PrintFullTestCommentIfPresent(const TestInfo& test_info) { - const char* const type_param = test_info.type_param(); - const char* const value_param = test_info.value_param(); - - if (type_param != nullptr || value_param != nullptr) { - printf(", where "); - if (type_param != nullptr) { - printf("%s = %s", kTypeParamLabel, type_param); - if (value_param != nullptr) printf(" and "); - } - if (value_param != nullptr) { - printf("%s = %s", kValueParamLabel, value_param); - } - } -} - -// This class implements the TestEventListener interface. -// -// Class PrettyUnitTestResultPrinter is copyable. -class PrettyUnitTestResultPrinter : public TestEventListener { - public: - PrettyUnitTestResultPrinter() {} - static void PrintTestName(const char* test_suite, const char* test) { - printf("%s.%s", test_suite, test); - } - - // The following methods override what's in the TestEventListener class. - void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} - void OnTestIterationStart(const UnitTest& unit_test, int iteration) override; - void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override; - void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseStart(const TestCase& test_case) override; -#else - void OnTestSuiteStart(const TestSuite& test_suite) override; -#endif // OnTestCaseStart - - void OnTestStart(const TestInfo& test_info) override; - - void OnTestPartResult(const TestPartResult& result) override; - void OnTestEnd(const TestInfo& test_info) override; -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseEnd(const TestCase& test_case) override; -#else - void OnTestSuiteEnd(const TestSuite& test_suite) override; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override; - void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} - void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; - void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} - - private: - static void PrintFailedTests(const UnitTest& unit_test); - static void PrintFailedTestSuites(const UnitTest& unit_test); - static void PrintSkippedTests(const UnitTest& unit_test); -}; - - // Fired before each iteration of tests starts. -void PrettyUnitTestResultPrinter::OnTestIterationStart( - const UnitTest& unit_test, int iteration) { - if (GTEST_FLAG(repeat) != 1) - printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1); - - const char* const filter = GTEST_FLAG(filter).c_str(); - - // Prints the filter if it's not *. This reminds the user that some - // tests may be skipped. - if (!String::CStringEquals(filter, kUniversalFilter)) { - ColoredPrintf(GTestColor::kYellow, "Note: %s filter = %s\n", GTEST_NAME_, - filter); - } - - if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) { - const int32_t shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); - ColoredPrintf(GTestColor::kYellow, "Note: This is test shard %d of %s.\n", - static_cast(shard_index) + 1, - internal::posix::GetEnv(kTestTotalShards)); - } - - if (GTEST_FLAG(shuffle)) { - ColoredPrintf(GTestColor::kYellow, - "Note: Randomizing tests' orders with a seed of %d .\n", - unit_test.random_seed()); - } - - ColoredPrintf(GTestColor::kGreen, "[==========] "); - printf("Running %s from %s.\n", - FormatTestCount(unit_test.test_to_run_count()).c_str(), - FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); - fflush(stdout); -} - -void PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart( - const UnitTest& /*unit_test*/) { - ColoredPrintf(GTestColor::kGreen, "[----------] "); - printf("Global test environment set-up.\n"); - fflush(stdout); -} - -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -void PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) { - const std::string counts = - FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); - ColoredPrintf(GTestColor::kGreen, "[----------] "); - printf("%s from %s", counts.c_str(), test_case.name()); - if (test_case.type_param() == nullptr) { - printf("\n"); - } else { - printf(", where %s = %s\n", kTypeParamLabel, test_case.type_param()); - } - fflush(stdout); -} -#else -void PrettyUnitTestResultPrinter::OnTestSuiteStart( - const TestSuite& test_suite) { - const std::string counts = - FormatCountableNoun(test_suite.test_to_run_count(), "test", "tests"); - ColoredPrintf(GTestColor::kGreen, "[----------] "); - printf("%s from %s", counts.c_str(), test_suite.name()); - if (test_suite.type_param() == nullptr) { - printf("\n"); - } else { - printf(", where %s = %s\n", kTypeParamLabel, test_suite.type_param()); - } - fflush(stdout); -} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { - ColoredPrintf(GTestColor::kGreen, "[ RUN ] "); - PrintTestName(test_info.test_suite_name(), test_info.name()); - printf("\n"); - fflush(stdout); -} - -// Called after an assertion failure. -void PrettyUnitTestResultPrinter::OnTestPartResult( - const TestPartResult& result) { - switch (result.type()) { - // If the test part succeeded, we don't need to do anything. - case TestPartResult::kSuccess: - return; - default: - // Print failure message from the assertion - // (e.g. expected this and got that). - PrintTestPartResult(result); - fflush(stdout); - } -} - -void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { - if (test_info.result()->Passed()) { - ColoredPrintf(GTestColor::kGreen, "[ OK ] "); - } else if (test_info.result()->Skipped()) { - ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); - } else { - ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); - } - PrintTestName(test_info.test_suite_name(), test_info.name()); - if (test_info.result()->Failed()) - PrintFullTestCommentIfPresent(test_info); - - if (GTEST_FLAG(print_time)) { - printf(" (%s ms)\n", internal::StreamableToString( - test_info.result()->elapsed_time()).c_str()); - } else { - printf("\n"); - } - fflush(stdout); -} - -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -void PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) { - if (!GTEST_FLAG(print_time)) return; - - const std::string counts = - FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); - ColoredPrintf(GTestColor::kGreen, "[----------] "); - printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_case.name(), - internal::StreamableToString(test_case.elapsed_time()).c_str()); - fflush(stdout); -} -#else -void PrettyUnitTestResultPrinter::OnTestSuiteEnd(const TestSuite& test_suite) { - if (!GTEST_FLAG(print_time)) return; - - const std::string counts = - FormatCountableNoun(test_suite.test_to_run_count(), "test", "tests"); - ColoredPrintf(GTestColor::kGreen, "[----------] "); - printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_suite.name(), - internal::StreamableToString(test_suite.elapsed_time()).c_str()); - fflush(stdout); -} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -void PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart( - const UnitTest& /*unit_test*/) { - ColoredPrintf(GTestColor::kGreen, "[----------] "); - printf("Global test environment tear-down\n"); - fflush(stdout); -} - -// Internal helper for printing the list of failed tests. -void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) { - const int failed_test_count = unit_test.failed_test_count(); - ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); - printf("%s, listed below:\n", FormatTestCount(failed_test_count).c_str()); - - for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { - const TestSuite& test_suite = *unit_test.GetTestSuite(i); - if (!test_suite.should_run() || (test_suite.failed_test_count() == 0)) { - continue; - } - for (int j = 0; j < test_suite.total_test_count(); ++j) { - const TestInfo& test_info = *test_suite.GetTestInfo(j); - if (!test_info.should_run() || !test_info.result()->Failed()) { - continue; - } - ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); - printf("%s.%s", test_suite.name(), test_info.name()); - PrintFullTestCommentIfPresent(test_info); - printf("\n"); - } - } - printf("\n%2d FAILED %s\n", failed_test_count, - failed_test_count == 1 ? "TEST" : "TESTS"); -} - -// Internal helper for printing the list of test suite failures not covered by -// PrintFailedTests. -void PrettyUnitTestResultPrinter::PrintFailedTestSuites( - const UnitTest& unit_test) { - int suite_failure_count = 0; - for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { - const TestSuite& test_suite = *unit_test.GetTestSuite(i); - if (!test_suite.should_run()) { - continue; - } - if (test_suite.ad_hoc_test_result().Failed()) { - ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); - printf("%s: SetUpTestSuite or TearDownTestSuite\n", test_suite.name()); - ++suite_failure_count; - } - } - if (suite_failure_count > 0) { - printf("\n%2d FAILED TEST %s\n", suite_failure_count, - suite_failure_count == 1 ? "SUITE" : "SUITES"); - } -} - -// Internal helper for printing the list of skipped tests. -void PrettyUnitTestResultPrinter::PrintSkippedTests(const UnitTest& unit_test) { - const int skipped_test_count = unit_test.skipped_test_count(); - if (skipped_test_count == 0) { - return; - } - - for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { - const TestSuite& test_suite = *unit_test.GetTestSuite(i); - if (!test_suite.should_run() || (test_suite.skipped_test_count() == 0)) { - continue; - } - for (int j = 0; j < test_suite.total_test_count(); ++j) { - const TestInfo& test_info = *test_suite.GetTestInfo(j); - if (!test_info.should_run() || !test_info.result()->Skipped()) { - continue; - } - ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); - printf("%s.%s", test_suite.name(), test_info.name()); - printf("\n"); - } - } -} - -void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, - int /*iteration*/) { - ColoredPrintf(GTestColor::kGreen, "[==========] "); - printf("%s from %s ran.", - FormatTestCount(unit_test.test_to_run_count()).c_str(), - FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); - if (GTEST_FLAG(print_time)) { - printf(" (%s ms total)", - internal::StreamableToString(unit_test.elapsed_time()).c_str()); - } - printf("\n"); - ColoredPrintf(GTestColor::kGreen, "[ PASSED ] "); - printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); - - const int skipped_test_count = unit_test.skipped_test_count(); - if (skipped_test_count > 0) { - ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); - printf("%s, listed below:\n", FormatTestCount(skipped_test_count).c_str()); - PrintSkippedTests(unit_test); - } - - if (!unit_test.Passed()) { - PrintFailedTests(unit_test); - PrintFailedTestSuites(unit_test); - } - - int num_disabled = unit_test.reportable_disabled_test_count(); - if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) { - if (unit_test.Passed()) { - printf("\n"); // Add a spacer if no FAILURE banner is displayed. - } - ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n", - num_disabled, num_disabled == 1 ? "TEST" : "TESTS"); - } - // Ensure that Google Test output is printed before, e.g., heapchecker output. - fflush(stdout); -} - -// End PrettyUnitTestResultPrinter - -// This class implements the TestEventListener interface. -// -// Class BriefUnitTestResultPrinter is copyable. -class BriefUnitTestResultPrinter : public TestEventListener { - public: - BriefUnitTestResultPrinter() {} - static void PrintTestName(const char* test_suite, const char* test) { - printf("%s.%s", test_suite, test); - } - - // The following methods override what's in the TestEventListener class. - void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} - void OnTestIterationStart(const UnitTest& /*unit_test*/, - int /*iteration*/) override {} - void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} - void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseStart(const TestCase& /*test_case*/) override {} -#else - void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} -#endif // OnTestCaseStart - - void OnTestStart(const TestInfo& /*test_info*/) override {} - - void OnTestPartResult(const TestPartResult& result) override; - void OnTestEnd(const TestInfo& test_info) override; -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseEnd(const TestCase& /*test_case*/) override {} -#else - void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} - void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} - void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; - void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} -}; - -// Called after an assertion failure. -void BriefUnitTestResultPrinter::OnTestPartResult( - const TestPartResult& result) { - switch (result.type()) { - // If the test part succeeded, we don't need to do anything. - case TestPartResult::kSuccess: - return; - default: - // Print failure message from the assertion - // (e.g. expected this and got that). - PrintTestPartResult(result); - fflush(stdout); - } -} - -void BriefUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { - if (test_info.result()->Failed()) { - ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); - PrintTestName(test_info.test_suite_name(), test_info.name()); - PrintFullTestCommentIfPresent(test_info); - - if (GTEST_FLAG(print_time)) { - printf(" (%s ms)\n", - internal::StreamableToString(test_info.result()->elapsed_time()) - .c_str()); - } else { - printf("\n"); - } - fflush(stdout); - } -} - -void BriefUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, - int /*iteration*/) { - ColoredPrintf(GTestColor::kGreen, "[==========] "); - printf("%s from %s ran.", - FormatTestCount(unit_test.test_to_run_count()).c_str(), - FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); - if (GTEST_FLAG(print_time)) { - printf(" (%s ms total)", - internal::StreamableToString(unit_test.elapsed_time()).c_str()); - } - printf("\n"); - ColoredPrintf(GTestColor::kGreen, "[ PASSED ] "); - printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); - - const int skipped_test_count = unit_test.skipped_test_count(); - if (skipped_test_count > 0) { - ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); - printf("%s.\n", FormatTestCount(skipped_test_count).c_str()); - } - - int num_disabled = unit_test.reportable_disabled_test_count(); - if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) { - if (unit_test.Passed()) { - printf("\n"); // Add a spacer if no FAILURE banner is displayed. - } - ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n", - num_disabled, num_disabled == 1 ? "TEST" : "TESTS"); - } - // Ensure that Google Test output is printed before, e.g., heapchecker output. - fflush(stdout); -} - -// End BriefUnitTestResultPrinter - -// class TestEventRepeater -// -// This class forwards events to other event listeners. -class TestEventRepeater : public TestEventListener { - public: - TestEventRepeater() : forwarding_enabled_(true) {} - ~TestEventRepeater() override; - void Append(TestEventListener *listener); - TestEventListener* Release(TestEventListener* listener); - - // Controls whether events will be forwarded to listeners_. Set to false - // in death test child processes. - bool forwarding_enabled() const { return forwarding_enabled_; } - void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; } - - void OnTestProgramStart(const UnitTest& unit_test) override; - void OnTestIterationStart(const UnitTest& unit_test, int iteration) override; - void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override; - void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) override; -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseStart(const TestSuite& parameter) override; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestSuiteStart(const TestSuite& parameter) override; - void OnTestStart(const TestInfo& test_info) override; - void OnTestPartResult(const TestPartResult& result) override; - void OnTestEnd(const TestInfo& test_info) override; -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseEnd(const TestCase& parameter) override; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestSuiteEnd(const TestSuite& parameter) override; - void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override; - void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) override; - void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; - void OnTestProgramEnd(const UnitTest& unit_test) override; - - private: - // Controls whether events will be forwarded to listeners_. Set to false - // in death test child processes. - bool forwarding_enabled_; - // The list of listeners that receive events. - std::vector listeners_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater); -}; - -TestEventRepeater::~TestEventRepeater() { - ForEach(listeners_, Delete); -} - -void TestEventRepeater::Append(TestEventListener *listener) { - listeners_.push_back(listener); -} - -TestEventListener* TestEventRepeater::Release(TestEventListener *listener) { - for (size_t i = 0; i < listeners_.size(); ++i) { - if (listeners_[i] == listener) { - listeners_.erase(listeners_.begin() + static_cast(i)); - return listener; - } - } - - return nullptr; -} - -// Since most methods are very similar, use macros to reduce boilerplate. -// This defines a member that forwards the call to all listeners. -#define GTEST_REPEATER_METHOD_(Name, Type) \ -void TestEventRepeater::Name(const Type& parameter) { \ - if (forwarding_enabled_) { \ - for (size_t i = 0; i < listeners_.size(); i++) { \ - listeners_[i]->Name(parameter); \ - } \ - } \ -} -// This defines a member that forwards the call to all listeners in reverse -// order. -#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \ - void TestEventRepeater::Name(const Type& parameter) { \ - if (forwarding_enabled_) { \ - for (size_t i = listeners_.size(); i != 0; i--) { \ - listeners_[i - 1]->Name(parameter); \ - } \ - } \ - } - -GTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest) -GTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest) -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -GTEST_REPEATER_METHOD_(OnTestCaseStart, TestSuite) -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -GTEST_REPEATER_METHOD_(OnTestSuiteStart, TestSuite) -GTEST_REPEATER_METHOD_(OnTestStart, TestInfo) -GTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult) -GTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest) -GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest) -GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest) -GTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo) -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -GTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestSuite) -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -GTEST_REVERSE_REPEATER_METHOD_(OnTestSuiteEnd, TestSuite) -GTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest) - -#undef GTEST_REPEATER_METHOD_ -#undef GTEST_REVERSE_REPEATER_METHOD_ - -void TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test, - int iteration) { - if (forwarding_enabled_) { - for (size_t i = 0; i < listeners_.size(); i++) { - listeners_[i]->OnTestIterationStart(unit_test, iteration); - } - } -} - -void TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test, - int iteration) { - if (forwarding_enabled_) { - for (size_t i = listeners_.size(); i > 0; i--) { - listeners_[i - 1]->OnTestIterationEnd(unit_test, iteration); - } - } -} - -// End TestEventRepeater - -// This class generates an XML output file. -class XmlUnitTestResultPrinter : public EmptyTestEventListener { - public: - explicit XmlUnitTestResultPrinter(const char* output_file); - - void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; - void ListTestsMatchingFilter(const std::vector& test_suites); - - // Prints an XML summary of all unit tests. - static void PrintXmlTestsList(std::ostream* stream, - const std::vector& test_suites); - - private: - // Is c a whitespace character that is normalized to a space character - // when it appears in an XML attribute value? - static bool IsNormalizableWhitespace(char c) { - return c == 0x9 || c == 0xA || c == 0xD; - } - - // May c appear in a well-formed XML document? - static bool IsValidXmlCharacter(char c) { - return IsNormalizableWhitespace(c) || c >= 0x20; - } - - // Returns an XML-escaped copy of the input string str. If - // is_attribute is true, the text is meant to appear as an attribute - // value, and normalizable whitespace is preserved by replacing it - // with character references. - static std::string EscapeXml(const std::string& str, bool is_attribute); - - // Returns the given string with all characters invalid in XML removed. - static std::string RemoveInvalidXmlCharacters(const std::string& str); - - // Convenience wrapper around EscapeXml when str is an attribute value. - static std::string EscapeXmlAttribute(const std::string& str) { - return EscapeXml(str, true); - } - - // Convenience wrapper around EscapeXml when str is not an attribute value. - static std::string EscapeXmlText(const char* str) { - return EscapeXml(str, false); - } - - // Verifies that the given attribute belongs to the given element and - // streams the attribute as XML. - static void OutputXmlAttribute(std::ostream* stream, - const std::string& element_name, - const std::string& name, - const std::string& value); - - // Streams an XML CDATA section, escaping invalid CDATA sequences as needed. - static void OutputXmlCDataSection(::std::ostream* stream, const char* data); - - // Streams a test suite XML stanza containing the given test result. - // - // Requires: result.Failed() - static void OutputXmlTestSuiteForTestResult(::std::ostream* stream, - const TestResult& result); - - // Streams an XML representation of a TestResult object. - static void OutputXmlTestResult(::std::ostream* stream, - const TestResult& result); - - // Streams an XML representation of a TestInfo object. - static void OutputXmlTestInfo(::std::ostream* stream, - const char* test_suite_name, - const TestInfo& test_info); - - // Prints an XML representation of a TestSuite object - static void PrintXmlTestSuite(::std::ostream* stream, - const TestSuite& test_suite); - - // Prints an XML summary of unit_test to output stream out. - static void PrintXmlUnitTest(::std::ostream* stream, - const UnitTest& unit_test); - - // Produces a string representing the test properties in a result as space - // delimited XML attributes based on the property key="value" pairs. - // When the std::string is not empty, it includes a space at the beginning, - // to delimit this attribute from prior attributes. - static std::string TestPropertiesAsXmlAttributes(const TestResult& result); - - // Streams an XML representation of the test properties of a TestResult - // object. - static void OutputXmlTestProperties(std::ostream* stream, - const TestResult& result); - - // The output file. - const std::string output_file_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter); -}; - -// Creates a new XmlUnitTestResultPrinter. -XmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file) - : output_file_(output_file) { - if (output_file_.empty()) { - GTEST_LOG_(FATAL) << "XML output file may not be null"; - } -} - -// Called after the unit test ends. -void XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, - int /*iteration*/) { - FILE* xmlout = OpenFileForWriting(output_file_); - std::stringstream stream; - PrintXmlUnitTest(&stream, unit_test); - fprintf(xmlout, "%s", StringStreamToString(&stream).c_str()); - fclose(xmlout); -} - -void XmlUnitTestResultPrinter::ListTestsMatchingFilter( - const std::vector& test_suites) { - FILE* xmlout = OpenFileForWriting(output_file_); - std::stringstream stream; - PrintXmlTestsList(&stream, test_suites); - fprintf(xmlout, "%s", StringStreamToString(&stream).c_str()); - fclose(xmlout); -} - -// Returns an XML-escaped copy of the input string str. If is_attribute -// is true, the text is meant to appear as an attribute value, and -// normalizable whitespace is preserved by replacing it with character -// references. -// -// Invalid XML characters in str, if any, are stripped from the output. -// It is expected that most, if not all, of the text processed by this -// module will consist of ordinary English text. -// If this module is ever modified to produce version 1.1 XML output, -// most invalid characters can be retained using character references. -std::string XmlUnitTestResultPrinter::EscapeXml( - const std::string& str, bool is_attribute) { - Message m; - - for (size_t i = 0; i < str.size(); ++i) { - const char ch = str[i]; - switch (ch) { - case '<': - m << "<"; - break; - case '>': - m << ">"; - break; - case '&': - m << "&"; - break; - case '\'': - if (is_attribute) - m << "'"; - else - m << '\''; - break; - case '"': - if (is_attribute) - m << """; - else - m << '"'; - break; - default: - if (IsValidXmlCharacter(ch)) { - if (is_attribute && IsNormalizableWhitespace(ch)) - m << "&#x" << String::FormatByte(static_cast(ch)) - << ";"; - else - m << ch; - } - break; - } - } - - return m.GetString(); -} - -// Returns the given string with all characters invalid in XML removed. -// Currently invalid characters are dropped from the string. An -// alternative is to replace them with certain characters such as . or ?. -std::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters( - const std::string& str) { - std::string output; - output.reserve(str.size()); - for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) - if (IsValidXmlCharacter(*it)) - output.push_back(*it); - - return output; -} - -// The following routines generate an XML representation of a UnitTest -// object. -// GOOGLETEST_CM0009 DO NOT DELETE -// -// This is how Google Test concepts map to the DTD: -// -// <-- corresponds to a UnitTest object -// <-- corresponds to a TestSuite object -// <-- corresponds to a TestInfo object -// ... -// ... -// ... -// <-- individual assertion failures -// -// -// - -// Formats the given time in milliseconds as seconds. -std::string FormatTimeInMillisAsSeconds(TimeInMillis ms) { - ::std::stringstream ss; - ss << (static_cast(ms) * 1e-3); - return ss.str(); -} - -static bool PortableLocaltime(time_t seconds, struct tm* out) { -#if defined(_MSC_VER) - return localtime_s(out, &seconds) == 0; -#elif defined(__MINGW32__) || defined(__MINGW64__) - // MINGW provides neither localtime_r nor localtime_s, but uses - // Windows' localtime(), which has a thread-local tm buffer. - struct tm* tm_ptr = localtime(&seconds); // NOLINT - if (tm_ptr == nullptr) return false; - *out = *tm_ptr; - return true; -#elif defined(__STDC_LIB_EXT1__) - // Uses localtime_s when available as localtime_r is only available from - // C23 standard. - return localtime_s(&seconds, out) != nullptr; -#else - return localtime_r(&seconds, out) != nullptr; -#endif -} - -// Converts the given epoch time in milliseconds to a date string in the ISO -// 8601 format, without the timezone information. -std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) { - struct tm time_struct; - if (!PortableLocaltime(static_cast(ms / 1000), &time_struct)) - return ""; - // YYYY-MM-DDThh:mm:ss.sss - return StreamableToString(time_struct.tm_year + 1900) + "-" + - String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" + - String::FormatIntWidth2(time_struct.tm_mday) + "T" + - String::FormatIntWidth2(time_struct.tm_hour) + ":" + - String::FormatIntWidth2(time_struct.tm_min) + ":" + - String::FormatIntWidth2(time_struct.tm_sec) + "." + - String::FormatIntWidthN(static_cast(ms % 1000), 3); -} - -// Streams an XML CDATA section, escaping invalid CDATA sequences as needed. -void XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream, - const char* data) { - const char* segment = data; - *stream << ""); - if (next_segment != nullptr) { - stream->write( - segment, static_cast(next_segment - segment)); - *stream << "]]>]]>"); - } else { - *stream << segment; - break; - } - } - *stream << "]]>"; -} - -void XmlUnitTestResultPrinter::OutputXmlAttribute( - std::ostream* stream, - const std::string& element_name, - const std::string& name, - const std::string& value) { - const std::vector& allowed_names = - GetReservedOutputAttributesForElement(element_name); - - GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != - allowed_names.end()) - << "Attribute " << name << " is not allowed for element <" << element_name - << ">."; - - *stream << " " << name << "=\"" << EscapeXmlAttribute(value) << "\""; -} - -// Streams a test suite XML stanza containing the given test result. -void XmlUnitTestResultPrinter::OutputXmlTestSuiteForTestResult( - ::std::ostream* stream, const TestResult& result) { - // Output the boilerplate for a minimal test suite with one test. - *stream << " "; - - // Output the boilerplate for a minimal test case with a single test. - *stream << " \n"; -} - -// Prints an XML representation of a TestInfo object. -void XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream, - const char* test_suite_name, - const TestInfo& test_info) { - const TestResult& result = *test_info.result(); - const std::string kTestsuite = "testcase"; - - if (test_info.is_in_another_shard()) { - return; - } - - *stream << " \n"; - return; - } - - OutputXmlAttribute(stream, kTestsuite, "status", - test_info.should_run() ? "run" : "notrun"); - OutputXmlAttribute(stream, kTestsuite, "result", - test_info.should_run() - ? (result.Skipped() ? "skipped" : "completed") - : "suppressed"); - OutputXmlAttribute(stream, kTestsuite, "time", - FormatTimeInMillisAsSeconds(result.elapsed_time())); - OutputXmlAttribute( - stream, kTestsuite, "timestamp", - FormatEpochTimeInMillisAsIso8601(result.start_timestamp())); - OutputXmlAttribute(stream, kTestsuite, "classname", test_suite_name); - - OutputXmlTestResult(stream, result); -} - -void XmlUnitTestResultPrinter::OutputXmlTestResult(::std::ostream* stream, - const TestResult& result) { - int failures = 0; - int skips = 0; - for (int i = 0; i < result.total_part_count(); ++i) { - const TestPartResult& part = result.GetTestPartResult(i); - if (part.failed()) { - if (++failures == 1 && skips == 0) { - *stream << ">\n"; - } - const std::string location = - internal::FormatCompilerIndependentFileLocation(part.file_name(), - part.line_number()); - const std::string summary = location + "\n" + part.summary(); - *stream << " "; - const std::string detail = location + "\n" + part.message(); - OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str()); - *stream << "\n"; - } else if (part.skipped()) { - if (++skips == 1 && failures == 0) { - *stream << ">\n"; - } - const std::string location = - internal::FormatCompilerIndependentFileLocation(part.file_name(), - part.line_number()); - const std::string summary = location + "\n" + part.summary(); - *stream << " "; - const std::string detail = location + "\n" + part.message(); - OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str()); - *stream << "\n"; - } - } - - if (failures == 0 && skips == 0 && result.test_property_count() == 0) { - *stream << " />\n"; - } else { - if (failures == 0 && skips == 0) { - *stream << ">\n"; - } - OutputXmlTestProperties(stream, result); - *stream << " \n"; - } -} - -// Prints an XML representation of a TestSuite object -void XmlUnitTestResultPrinter::PrintXmlTestSuite(std::ostream* stream, - const TestSuite& test_suite) { - const std::string kTestsuite = "testsuite"; - *stream << " <" << kTestsuite; - OutputXmlAttribute(stream, kTestsuite, "name", test_suite.name()); - OutputXmlAttribute(stream, kTestsuite, "tests", - StreamableToString(test_suite.reportable_test_count())); - if (!GTEST_FLAG(list_tests)) { - OutputXmlAttribute(stream, kTestsuite, "failures", - StreamableToString(test_suite.failed_test_count())); - OutputXmlAttribute( - stream, kTestsuite, "disabled", - StreamableToString(test_suite.reportable_disabled_test_count())); - OutputXmlAttribute(stream, kTestsuite, "skipped", - StreamableToString(test_suite.skipped_test_count())); - - OutputXmlAttribute(stream, kTestsuite, "errors", "0"); - - OutputXmlAttribute(stream, kTestsuite, "time", - FormatTimeInMillisAsSeconds(test_suite.elapsed_time())); - OutputXmlAttribute( - stream, kTestsuite, "timestamp", - FormatEpochTimeInMillisAsIso8601(test_suite.start_timestamp())); - *stream << TestPropertiesAsXmlAttributes(test_suite.ad_hoc_test_result()); - } - *stream << ">\n"; - for (int i = 0; i < test_suite.total_test_count(); ++i) { - if (test_suite.GetTestInfo(i)->is_reportable()) - OutputXmlTestInfo(stream, test_suite.name(), *test_suite.GetTestInfo(i)); - } - *stream << " \n"; -} - -// Prints an XML summary of unit_test to output stream out. -void XmlUnitTestResultPrinter::PrintXmlUnitTest(std::ostream* stream, - const UnitTest& unit_test) { - const std::string kTestsuites = "testsuites"; - - *stream << "\n"; - *stream << "<" << kTestsuites; - - OutputXmlAttribute(stream, kTestsuites, "tests", - StreamableToString(unit_test.reportable_test_count())); - OutputXmlAttribute(stream, kTestsuites, "failures", - StreamableToString(unit_test.failed_test_count())); - OutputXmlAttribute( - stream, kTestsuites, "disabled", - StreamableToString(unit_test.reportable_disabled_test_count())); - OutputXmlAttribute(stream, kTestsuites, "errors", "0"); - OutputXmlAttribute(stream, kTestsuites, "time", - FormatTimeInMillisAsSeconds(unit_test.elapsed_time())); - OutputXmlAttribute( - stream, kTestsuites, "timestamp", - FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp())); - - if (GTEST_FLAG(shuffle)) { - OutputXmlAttribute(stream, kTestsuites, "random_seed", - StreamableToString(unit_test.random_seed())); - } - *stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result()); - - OutputXmlAttribute(stream, kTestsuites, "name", "AllTests"); - *stream << ">\n"; - - for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { - if (unit_test.GetTestSuite(i)->reportable_test_count() > 0) - PrintXmlTestSuite(stream, *unit_test.GetTestSuite(i)); - } - - // If there was a test failure outside of one of the test suites (like in a - // test environment) include that in the output. - if (unit_test.ad_hoc_test_result().Failed()) { - OutputXmlTestSuiteForTestResult(stream, unit_test.ad_hoc_test_result()); - } - - *stream << "\n"; -} - -void XmlUnitTestResultPrinter::PrintXmlTestsList( - std::ostream* stream, const std::vector& test_suites) { - const std::string kTestsuites = "testsuites"; - - *stream << "\n"; - *stream << "<" << kTestsuites; - - int total_tests = 0; - for (auto test_suite : test_suites) { - total_tests += test_suite->total_test_count(); - } - OutputXmlAttribute(stream, kTestsuites, "tests", - StreamableToString(total_tests)); - OutputXmlAttribute(stream, kTestsuites, "name", "AllTests"); - *stream << ">\n"; - - for (auto test_suite : test_suites) { - PrintXmlTestSuite(stream, *test_suite); - } - *stream << "\n"; -} - -// Produces a string representing the test properties in a result as space -// delimited XML attributes based on the property key="value" pairs. -std::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes( - const TestResult& result) { - Message attributes; - for (int i = 0; i < result.test_property_count(); ++i) { - const TestProperty& property = result.GetTestProperty(i); - attributes << " " << property.key() << "=" - << "\"" << EscapeXmlAttribute(property.value()) << "\""; - } - return attributes.GetString(); -} - -void XmlUnitTestResultPrinter::OutputXmlTestProperties( - std::ostream* stream, const TestResult& result) { - const std::string kProperties = "properties"; - const std::string kProperty = "property"; - - if (result.test_property_count() <= 0) { - return; - } - - *stream << "<" << kProperties << ">\n"; - for (int i = 0; i < result.test_property_count(); ++i) { - const TestProperty& property = result.GetTestProperty(i); - *stream << "<" << kProperty; - *stream << " name=\"" << EscapeXmlAttribute(property.key()) << "\""; - *stream << " value=\"" << EscapeXmlAttribute(property.value()) << "\""; - *stream << "/>\n"; - } - *stream << "\n"; -} - -// End XmlUnitTestResultPrinter - -// This class generates an JSON output file. -class JsonUnitTestResultPrinter : public EmptyTestEventListener { - public: - explicit JsonUnitTestResultPrinter(const char* output_file); - - void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; - - // Prints an JSON summary of all unit tests. - static void PrintJsonTestList(::std::ostream* stream, - const std::vector& test_suites); - - private: - // Returns an JSON-escaped copy of the input string str. - static std::string EscapeJson(const std::string& str); - - //// Verifies that the given attribute belongs to the given element and - //// streams the attribute as JSON. - static void OutputJsonKey(std::ostream* stream, - const std::string& element_name, - const std::string& name, - const std::string& value, - const std::string& indent, - bool comma = true); - static void OutputJsonKey(std::ostream* stream, - const std::string& element_name, - const std::string& name, - int value, - const std::string& indent, - bool comma = true); - - // Streams a test suite JSON stanza containing the given test result. - // - // Requires: result.Failed() - static void OutputJsonTestSuiteForTestResult(::std::ostream* stream, - const TestResult& result); - - // Streams a JSON representation of a TestResult object. - static void OutputJsonTestResult(::std::ostream* stream, - const TestResult& result); - - // Streams a JSON representation of a TestInfo object. - static void OutputJsonTestInfo(::std::ostream* stream, - const char* test_suite_name, - const TestInfo& test_info); - - // Prints a JSON representation of a TestSuite object - static void PrintJsonTestSuite(::std::ostream* stream, - const TestSuite& test_suite); - - // Prints a JSON summary of unit_test to output stream out. - static void PrintJsonUnitTest(::std::ostream* stream, - const UnitTest& unit_test); - - // Produces a string representing the test properties in a result as - // a JSON dictionary. - static std::string TestPropertiesAsJson(const TestResult& result, - const std::string& indent); - - // The output file. - const std::string output_file_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(JsonUnitTestResultPrinter); -}; - -// Creates a new JsonUnitTestResultPrinter. -JsonUnitTestResultPrinter::JsonUnitTestResultPrinter(const char* output_file) - : output_file_(output_file) { - if (output_file_.empty()) { - GTEST_LOG_(FATAL) << "JSON output file may not be null"; - } -} - -void JsonUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, - int /*iteration*/) { - FILE* jsonout = OpenFileForWriting(output_file_); - std::stringstream stream; - PrintJsonUnitTest(&stream, unit_test); - fprintf(jsonout, "%s", StringStreamToString(&stream).c_str()); - fclose(jsonout); -} - -// Returns an JSON-escaped copy of the input string str. -std::string JsonUnitTestResultPrinter::EscapeJson(const std::string& str) { - Message m; - - for (size_t i = 0; i < str.size(); ++i) { - const char ch = str[i]; - switch (ch) { - case '\\': - case '"': - case '/': - m << '\\' << ch; - break; - case '\b': - m << "\\b"; - break; - case '\t': - m << "\\t"; - break; - case '\n': - m << "\\n"; - break; - case '\f': - m << "\\f"; - break; - case '\r': - m << "\\r"; - break; - default: - if (ch < ' ') { - m << "\\u00" << String::FormatByte(static_cast(ch)); - } else { - m << ch; - } - break; - } - } - - return m.GetString(); -} - -// The following routines generate an JSON representation of a UnitTest -// object. - -// Formats the given time in milliseconds as seconds. -static std::string FormatTimeInMillisAsDuration(TimeInMillis ms) { - ::std::stringstream ss; - ss << (static_cast(ms) * 1e-3) << "s"; - return ss.str(); -} - -// Converts the given epoch time in milliseconds to a date string in the -// RFC3339 format, without the timezone information. -static std::string FormatEpochTimeInMillisAsRFC3339(TimeInMillis ms) { - struct tm time_struct; - if (!PortableLocaltime(static_cast(ms / 1000), &time_struct)) - return ""; - // YYYY-MM-DDThh:mm:ss - return StreamableToString(time_struct.tm_year + 1900) + "-" + - String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" + - String::FormatIntWidth2(time_struct.tm_mday) + "T" + - String::FormatIntWidth2(time_struct.tm_hour) + ":" + - String::FormatIntWidth2(time_struct.tm_min) + ":" + - String::FormatIntWidth2(time_struct.tm_sec) + "Z"; -} - -static inline std::string Indent(size_t width) { - return std::string(width, ' '); -} - -void JsonUnitTestResultPrinter::OutputJsonKey( - std::ostream* stream, - const std::string& element_name, - const std::string& name, - const std::string& value, - const std::string& indent, - bool comma) { - const std::vector& allowed_names = - GetReservedOutputAttributesForElement(element_name); - - GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != - allowed_names.end()) - << "Key \"" << name << "\" is not allowed for value \"" << element_name - << "\"."; - - *stream << indent << "\"" << name << "\": \"" << EscapeJson(value) << "\""; - if (comma) - *stream << ",\n"; -} - -void JsonUnitTestResultPrinter::OutputJsonKey( - std::ostream* stream, - const std::string& element_name, - const std::string& name, - int value, - const std::string& indent, - bool comma) { - const std::vector& allowed_names = - GetReservedOutputAttributesForElement(element_name); - - GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != - allowed_names.end()) - << "Key \"" << name << "\" is not allowed for value \"" << element_name - << "\"."; - - *stream << indent << "\"" << name << "\": " << StreamableToString(value); - if (comma) - *stream << ",\n"; -} - -// Streams a test suite JSON stanza containing the given test result. -void JsonUnitTestResultPrinter::OutputJsonTestSuiteForTestResult( - ::std::ostream* stream, const TestResult& result) { - // Output the boilerplate for a new test suite. - *stream << Indent(4) << "{\n"; - OutputJsonKey(stream, "testsuite", "name", "NonTestSuiteFailure", Indent(6)); - OutputJsonKey(stream, "testsuite", "tests", 1, Indent(6)); - if (!GTEST_FLAG(list_tests)) { - OutputJsonKey(stream, "testsuite", "failures", 1, Indent(6)); - OutputJsonKey(stream, "testsuite", "disabled", 0, Indent(6)); - OutputJsonKey(stream, "testsuite", "skipped", 0, Indent(6)); - OutputJsonKey(stream, "testsuite", "errors", 0, Indent(6)); - OutputJsonKey(stream, "testsuite", "time", - FormatTimeInMillisAsDuration(result.elapsed_time()), - Indent(6)); - OutputJsonKey(stream, "testsuite", "timestamp", - FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), - Indent(6)); - } - *stream << Indent(6) << "\"testsuite\": [\n"; - - // Output the boilerplate for a new test case. - *stream << Indent(8) << "{\n"; - OutputJsonKey(stream, "testcase", "name", "", Indent(10)); - OutputJsonKey(stream, "testcase", "status", "RUN", Indent(10)); - OutputJsonKey(stream, "testcase", "result", "COMPLETED", Indent(10)); - OutputJsonKey(stream, "testcase", "timestamp", - FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), - Indent(10)); - OutputJsonKey(stream, "testcase", "time", - FormatTimeInMillisAsDuration(result.elapsed_time()), - Indent(10)); - OutputJsonKey(stream, "testcase", "classname", "", Indent(10), false); - *stream << TestPropertiesAsJson(result, Indent(10)); - - // Output the actual test result. - OutputJsonTestResult(stream, result); - - // Finish the test suite. - *stream << "\n" << Indent(6) << "]\n" << Indent(4) << "}"; -} - -// Prints a JSON representation of a TestInfo object. -void JsonUnitTestResultPrinter::OutputJsonTestInfo(::std::ostream* stream, - const char* test_suite_name, - const TestInfo& test_info) { - const TestResult& result = *test_info.result(); - const std::string kTestsuite = "testcase"; - const std::string kIndent = Indent(10); - - *stream << Indent(8) << "{\n"; - OutputJsonKey(stream, kTestsuite, "name", test_info.name(), kIndent); - - if (test_info.value_param() != nullptr) { - OutputJsonKey(stream, kTestsuite, "value_param", test_info.value_param(), - kIndent); - } - if (test_info.type_param() != nullptr) { - OutputJsonKey(stream, kTestsuite, "type_param", test_info.type_param(), - kIndent); - } - if (GTEST_FLAG(list_tests)) { - OutputJsonKey(stream, kTestsuite, "file", test_info.file(), kIndent); - OutputJsonKey(stream, kTestsuite, "line", test_info.line(), kIndent, false); - *stream << "\n" << Indent(8) << "}"; - return; - } - - OutputJsonKey(stream, kTestsuite, "status", - test_info.should_run() ? "RUN" : "NOTRUN", kIndent); - OutputJsonKey(stream, kTestsuite, "result", - test_info.should_run() - ? (result.Skipped() ? "SKIPPED" : "COMPLETED") - : "SUPPRESSED", - kIndent); - OutputJsonKey(stream, kTestsuite, "timestamp", - FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), - kIndent); - OutputJsonKey(stream, kTestsuite, "time", - FormatTimeInMillisAsDuration(result.elapsed_time()), kIndent); - OutputJsonKey(stream, kTestsuite, "classname", test_suite_name, kIndent, - false); - *stream << TestPropertiesAsJson(result, kIndent); - - OutputJsonTestResult(stream, result); -} - -void JsonUnitTestResultPrinter::OutputJsonTestResult(::std::ostream* stream, - const TestResult& result) { - const std::string kIndent = Indent(10); - - int failures = 0; - for (int i = 0; i < result.total_part_count(); ++i) { - const TestPartResult& part = result.GetTestPartResult(i); - if (part.failed()) { - *stream << ",\n"; - if (++failures == 1) { - *stream << kIndent << "\"" << "failures" << "\": [\n"; - } - const std::string location = - internal::FormatCompilerIndependentFileLocation(part.file_name(), - part.line_number()); - const std::string message = EscapeJson(location + "\n" + part.message()); - *stream << kIndent << " {\n" - << kIndent << " \"failure\": \"" << message << "\",\n" - << kIndent << " \"type\": \"\"\n" - << kIndent << " }"; - } - } - - if (failures > 0) - *stream << "\n" << kIndent << "]"; - *stream << "\n" << Indent(8) << "}"; -} - -// Prints an JSON representation of a TestSuite object -void JsonUnitTestResultPrinter::PrintJsonTestSuite( - std::ostream* stream, const TestSuite& test_suite) { - const std::string kTestsuite = "testsuite"; - const std::string kIndent = Indent(6); - - *stream << Indent(4) << "{\n"; - OutputJsonKey(stream, kTestsuite, "name", test_suite.name(), kIndent); - OutputJsonKey(stream, kTestsuite, "tests", test_suite.reportable_test_count(), - kIndent); - if (!GTEST_FLAG(list_tests)) { - OutputJsonKey(stream, kTestsuite, "failures", - test_suite.failed_test_count(), kIndent); - OutputJsonKey(stream, kTestsuite, "disabled", - test_suite.reportable_disabled_test_count(), kIndent); - OutputJsonKey(stream, kTestsuite, "errors", 0, kIndent); - OutputJsonKey( - stream, kTestsuite, "timestamp", - FormatEpochTimeInMillisAsRFC3339(test_suite.start_timestamp()), - kIndent); - OutputJsonKey(stream, kTestsuite, "time", - FormatTimeInMillisAsDuration(test_suite.elapsed_time()), - kIndent, false); - *stream << TestPropertiesAsJson(test_suite.ad_hoc_test_result(), kIndent) - << ",\n"; - } - - *stream << kIndent << "\"" << kTestsuite << "\": [\n"; - - bool comma = false; - for (int i = 0; i < test_suite.total_test_count(); ++i) { - if (test_suite.GetTestInfo(i)->is_reportable()) { - if (comma) { - *stream << ",\n"; - } else { - comma = true; - } - OutputJsonTestInfo(stream, test_suite.name(), *test_suite.GetTestInfo(i)); - } - } - *stream << "\n" << kIndent << "]\n" << Indent(4) << "}"; -} - -// Prints a JSON summary of unit_test to output stream out. -void JsonUnitTestResultPrinter::PrintJsonUnitTest(std::ostream* stream, - const UnitTest& unit_test) { - const std::string kTestsuites = "testsuites"; - const std::string kIndent = Indent(2); - *stream << "{\n"; - - OutputJsonKey(stream, kTestsuites, "tests", unit_test.reportable_test_count(), - kIndent); - OutputJsonKey(stream, kTestsuites, "failures", unit_test.failed_test_count(), - kIndent); - OutputJsonKey(stream, kTestsuites, "disabled", - unit_test.reportable_disabled_test_count(), kIndent); - OutputJsonKey(stream, kTestsuites, "errors", 0, kIndent); - if (GTEST_FLAG(shuffle)) { - OutputJsonKey(stream, kTestsuites, "random_seed", unit_test.random_seed(), - kIndent); - } - OutputJsonKey(stream, kTestsuites, "timestamp", - FormatEpochTimeInMillisAsRFC3339(unit_test.start_timestamp()), - kIndent); - OutputJsonKey(stream, kTestsuites, "time", - FormatTimeInMillisAsDuration(unit_test.elapsed_time()), kIndent, - false); - - *stream << TestPropertiesAsJson(unit_test.ad_hoc_test_result(), kIndent) - << ",\n"; - - OutputJsonKey(stream, kTestsuites, "name", "AllTests", kIndent); - *stream << kIndent << "\"" << kTestsuites << "\": [\n"; - - bool comma = false; - for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { - if (unit_test.GetTestSuite(i)->reportable_test_count() > 0) { - if (comma) { - *stream << ",\n"; - } else { - comma = true; - } - PrintJsonTestSuite(stream, *unit_test.GetTestSuite(i)); - } - } - - // If there was a test failure outside of one of the test suites (like in a - // test environment) include that in the output. - if (unit_test.ad_hoc_test_result().Failed()) { - OutputJsonTestSuiteForTestResult(stream, unit_test.ad_hoc_test_result()); - } - - *stream << "\n" << kIndent << "]\n" << "}\n"; -} - -void JsonUnitTestResultPrinter::PrintJsonTestList( - std::ostream* stream, const std::vector& test_suites) { - const std::string kTestsuites = "testsuites"; - const std::string kIndent = Indent(2); - *stream << "{\n"; - int total_tests = 0; - for (auto test_suite : test_suites) { - total_tests += test_suite->total_test_count(); - } - OutputJsonKey(stream, kTestsuites, "tests", total_tests, kIndent); - - OutputJsonKey(stream, kTestsuites, "name", "AllTests", kIndent); - *stream << kIndent << "\"" << kTestsuites << "\": [\n"; - - for (size_t i = 0; i < test_suites.size(); ++i) { - if (i != 0) { - *stream << ",\n"; - } - PrintJsonTestSuite(stream, *test_suites[i]); - } - - *stream << "\n" - << kIndent << "]\n" - << "}\n"; -} -// Produces a string representing the test properties in a result as -// a JSON dictionary. -std::string JsonUnitTestResultPrinter::TestPropertiesAsJson( - const TestResult& result, const std::string& indent) { - Message attributes; - for (int i = 0; i < result.test_property_count(); ++i) { - const TestProperty& property = result.GetTestProperty(i); - attributes << ",\n" << indent << "\"" << property.key() << "\": " - << "\"" << EscapeJson(property.value()) << "\""; - } - return attributes.GetString(); -} - -// End JsonUnitTestResultPrinter - -#if GTEST_CAN_STREAM_RESULTS_ - -// Checks if str contains '=', '&', '%' or '\n' characters. If yes, -// replaces them by "%xx" where xx is their hexadecimal value. For -// example, replaces "=" with "%3D". This algorithm is O(strlen(str)) -// in both time and space -- important as the input str may contain an -// arbitrarily long test failure message and stack trace. -std::string StreamingListener::UrlEncode(const char* str) { - std::string result; - result.reserve(strlen(str) + 1); - for (char ch = *str; ch != '\0'; ch = *++str) { - switch (ch) { - case '%': - case '=': - case '&': - case '\n': - result.append("%" + String::FormatByte(static_cast(ch))); - break; - default: - result.push_back(ch); - break; - } - } - return result; -} - -void StreamingListener::SocketWriter::MakeConnection() { - GTEST_CHECK_(sockfd_ == -1) - << "MakeConnection() can't be called when there is already a connection."; - - addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses. - hints.ai_socktype = SOCK_STREAM; - addrinfo* servinfo = nullptr; - - // Use the getaddrinfo() to get a linked list of IP addresses for - // the given host name. - const int error_num = getaddrinfo( - host_name_.c_str(), port_num_.c_str(), &hints, &servinfo); - if (error_num != 0) { - GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: " - << gai_strerror(error_num); - } - - // Loop through all the results and connect to the first we can. - for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != nullptr; - cur_addr = cur_addr->ai_next) { - sockfd_ = socket( - cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol); - if (sockfd_ != -1) { - // Connect the client socket to the server socket. - if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) { - close(sockfd_); - sockfd_ = -1; - } - } - } - - freeaddrinfo(servinfo); // all done with this structure - - if (sockfd_ == -1) { - GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to " - << host_name_ << ":" << port_num_; - } -} - -// End of class Streaming Listener -#endif // GTEST_CAN_STREAM_RESULTS__ - -// class OsStackTraceGetter - -const char* const OsStackTraceGetterInterface::kElidedFramesMarker = - "... " GTEST_NAME_ " internal frames ..."; - -std::string OsStackTraceGetter::CurrentStackTrace(int max_depth, int skip_count) - GTEST_LOCK_EXCLUDED_(mutex_) { -#if GTEST_HAS_ABSL - std::string result; - - if (max_depth <= 0) { - return result; - } - - max_depth = std::min(max_depth, kMaxStackTraceDepth); - - std::vector raw_stack(max_depth); - // Skips the frames requested by the caller, plus this function. - const int raw_stack_size = - absl::GetStackTrace(&raw_stack[0], max_depth, skip_count + 1); - - void* caller_frame = nullptr; - { - MutexLock lock(&mutex_); - caller_frame = caller_frame_; - } - - for (int i = 0; i < raw_stack_size; ++i) { - if (raw_stack[i] == caller_frame && - !GTEST_FLAG(show_internal_stack_frames)) { - // Add a marker to the trace and stop adding frames. - absl::StrAppend(&result, kElidedFramesMarker, "\n"); - break; - } - - char tmp[1024]; - const char* symbol = "(unknown)"; - if (absl::Symbolize(raw_stack[i], tmp, sizeof(tmp))) { - symbol = tmp; - } - - char line[1024]; - snprintf(line, sizeof(line), " %p: %s\n", raw_stack[i], symbol); - result += line; - } - - return result; - -#else // !GTEST_HAS_ABSL - static_cast(max_depth); - static_cast(skip_count); - return ""; -#endif // GTEST_HAS_ABSL -} - -void OsStackTraceGetter::UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_) { -#if GTEST_HAS_ABSL - void* caller_frame = nullptr; - if (absl::GetStackTrace(&caller_frame, 1, 3) <= 0) { - caller_frame = nullptr; - } - - MutexLock lock(&mutex_); - caller_frame_ = caller_frame; -#endif // GTEST_HAS_ABSL -} - -// A helper class that creates the premature-exit file in its -// constructor and deletes the file in its destructor. -class ScopedPrematureExitFile { - public: - explicit ScopedPrematureExitFile(const char* premature_exit_filepath) - : premature_exit_filepath_(premature_exit_filepath ? - premature_exit_filepath : "") { - // If a path to the premature-exit file is specified... - if (!premature_exit_filepath_.empty()) { - // create the file with a single "0" character in it. I/O - // errors are ignored as there's nothing better we can do and we - // don't want to fail the test because of this. - FILE* pfile = posix::FOpen(premature_exit_filepath, "w"); - fwrite("0", 1, 1, pfile); - fclose(pfile); - } - } - - ~ScopedPrematureExitFile() { -#if !defined GTEST_OS_ESP8266 - if (!premature_exit_filepath_.empty()) { - int retval = remove(premature_exit_filepath_.c_str()); - if (retval) { - GTEST_LOG_(ERROR) << "Failed to remove premature exit filepath \"" - << premature_exit_filepath_ << "\" with error " - << retval; - } - } -#endif - } - - private: - const std::string premature_exit_filepath_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedPrematureExitFile); -}; - -} // namespace internal - -// class TestEventListeners - -TestEventListeners::TestEventListeners() - : repeater_(new internal::TestEventRepeater()), - default_result_printer_(nullptr), - default_xml_generator_(nullptr) {} - -TestEventListeners::~TestEventListeners() { delete repeater_; } - -// Returns the standard listener responsible for the default console -// output. Can be removed from the listeners list to shut down default -// console output. Note that removing this object from the listener list -// with Release transfers its ownership to the user. -void TestEventListeners::Append(TestEventListener* listener) { - repeater_->Append(listener); -} - -// Removes the given event listener from the list and returns it. It then -// becomes the caller's responsibility to delete the listener. Returns -// NULL if the listener is not found in the list. -TestEventListener* TestEventListeners::Release(TestEventListener* listener) { - if (listener == default_result_printer_) - default_result_printer_ = nullptr; - else if (listener == default_xml_generator_) - default_xml_generator_ = nullptr; - return repeater_->Release(listener); -} - -// Returns repeater that broadcasts the TestEventListener events to all -// subscribers. -TestEventListener* TestEventListeners::repeater() { return repeater_; } - -// Sets the default_result_printer attribute to the provided listener. -// The listener is also added to the listener list and previous -// default_result_printer is removed from it and deleted. The listener can -// also be NULL in which case it will not be added to the list. Does -// nothing if the previous and the current listener objects are the same. -void TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) { - if (default_result_printer_ != listener) { - // It is an error to pass this method a listener that is already in the - // list. - delete Release(default_result_printer_); - default_result_printer_ = listener; - if (listener != nullptr) Append(listener); - } -} - -// Sets the default_xml_generator attribute to the provided listener. The -// listener is also added to the listener list and previous -// default_xml_generator is removed from it and deleted. The listener can -// also be NULL in which case it will not be added to the list. Does -// nothing if the previous and the current listener objects are the same. -void TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) { - if (default_xml_generator_ != listener) { - // It is an error to pass this method a listener that is already in the - // list. - delete Release(default_xml_generator_); - default_xml_generator_ = listener; - if (listener != nullptr) Append(listener); - } -} - -// Controls whether events will be forwarded by the repeater to the -// listeners in the list. -bool TestEventListeners::EventForwardingEnabled() const { - return repeater_->forwarding_enabled(); -} - -void TestEventListeners::SuppressEventForwarding() { - repeater_->set_forwarding_enabled(false); -} - -// class UnitTest - -// Gets the singleton UnitTest object. The first time this method is -// called, a UnitTest object is constructed and returned. Consecutive -// calls will return the same object. -// -// We don't protect this under mutex_ as a user is not supposed to -// call this before main() starts, from which point on the return -// value will never change. -UnitTest* UnitTest::GetInstance() { - // CodeGear C++Builder insists on a public destructor for the - // default implementation. Use this implementation to keep good OO - // design with private destructor. - -#if defined(__BORLANDC__) - static UnitTest* const instance = new UnitTest; - return instance; -#else - static UnitTest instance; - return &instance; -#endif // defined(__BORLANDC__) -} - -// Gets the number of successful test suites. -int UnitTest::successful_test_suite_count() const { - return impl()->successful_test_suite_count(); -} - -// Gets the number of failed test suites. -int UnitTest::failed_test_suite_count() const { - return impl()->failed_test_suite_count(); -} - -// Gets the number of all test suites. -int UnitTest::total_test_suite_count() const { - return impl()->total_test_suite_count(); -} - -// Gets the number of all test suites that contain at least one test -// that should run. -int UnitTest::test_suite_to_run_count() const { - return impl()->test_suite_to_run_count(); -} - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -int UnitTest::successful_test_case_count() const { - return impl()->successful_test_suite_count(); -} -int UnitTest::failed_test_case_count() const { - return impl()->failed_test_suite_count(); -} -int UnitTest::total_test_case_count() const { - return impl()->total_test_suite_count(); -} -int UnitTest::test_case_to_run_count() const { - return impl()->test_suite_to_run_count(); -} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -// Gets the number of successful tests. -int UnitTest::successful_test_count() const { - return impl()->successful_test_count(); -} - -// Gets the number of skipped tests. -int UnitTest::skipped_test_count() const { - return impl()->skipped_test_count(); -} - -// Gets the number of failed tests. -int UnitTest::failed_test_count() const { return impl()->failed_test_count(); } - -// Gets the number of disabled tests that will be reported in the XML report. -int UnitTest::reportable_disabled_test_count() const { - return impl()->reportable_disabled_test_count(); -} - -// Gets the number of disabled tests. -int UnitTest::disabled_test_count() const { - return impl()->disabled_test_count(); -} - -// Gets the number of tests to be printed in the XML report. -int UnitTest::reportable_test_count() const { - return impl()->reportable_test_count(); -} - -// Gets the number of all tests. -int UnitTest::total_test_count() const { return impl()->total_test_count(); } - -// Gets the number of tests that should run. -int UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); } - -// Gets the time of the test program start, in ms from the start of the -// UNIX epoch. -internal::TimeInMillis UnitTest::start_timestamp() const { - return impl()->start_timestamp(); -} - -// Gets the elapsed time, in milliseconds. -internal::TimeInMillis UnitTest::elapsed_time() const { - return impl()->elapsed_time(); -} - -// Returns true if and only if the unit test passed (i.e. all test suites -// passed). -bool UnitTest::Passed() const { return impl()->Passed(); } - -// Returns true if and only if the unit test failed (i.e. some test suite -// failed or something outside of all tests failed). -bool UnitTest::Failed() const { return impl()->Failed(); } - -// Gets the i-th test suite among all the test suites. i can range from 0 to -// total_test_suite_count() - 1. If i is not in that range, returns NULL. -const TestSuite* UnitTest::GetTestSuite(int i) const { - return impl()->GetTestSuite(i); -} - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -const TestCase* UnitTest::GetTestCase(int i) const { - return impl()->GetTestCase(i); -} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -// Returns the TestResult containing information on test failures and -// properties logged outside of individual test suites. -const TestResult& UnitTest::ad_hoc_test_result() const { - return *impl()->ad_hoc_test_result(); -} - -// Gets the i-th test suite among all the test suites. i can range from 0 to -// total_test_suite_count() - 1. If i is not in that range, returns NULL. -TestSuite* UnitTest::GetMutableTestSuite(int i) { - return impl()->GetMutableSuiteCase(i); -} - -// Returns the list of event listeners that can be used to track events -// inside Google Test. -TestEventListeners& UnitTest::listeners() { - return *impl()->listeners(); -} - -// Registers and returns a global test environment. When a test -// program is run, all global test environments will be set-up in the -// order they were registered. After all tests in the program have -// finished, all global test environments will be torn-down in the -// *reverse* order they were registered. -// -// The UnitTest object takes ownership of the given environment. -// -// We don't protect this under mutex_, as we only support calling it -// from the main thread. -Environment* UnitTest::AddEnvironment(Environment* env) { - if (env == nullptr) { - return nullptr; - } - - impl_->environments().push_back(env); - return env; -} - -// Adds a TestPartResult to the current TestResult object. All Google Test -// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call -// this to report their results. The user code should use the -// assertion macros instead of calling this directly. -void UnitTest::AddTestPartResult( - TestPartResult::Type result_type, - const char* file_name, - int line_number, - const std::string& message, - const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_) { - Message msg; - msg << message; - - internal::MutexLock lock(&mutex_); - if (impl_->gtest_trace_stack().size() > 0) { - msg << "\n" << GTEST_NAME_ << " trace:"; - - for (size_t i = impl_->gtest_trace_stack().size(); i > 0; --i) { - const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1]; - msg << "\n" << internal::FormatFileLocation(trace.file, trace.line) - << " " << trace.message; - } - } - - if (os_stack_trace.c_str() != nullptr && !os_stack_trace.empty()) { - msg << internal::kStackTraceMarker << os_stack_trace; - } - - const TestPartResult result = TestPartResult( - result_type, file_name, line_number, msg.GetString().c_str()); - impl_->GetTestPartResultReporterForCurrentThread()-> - ReportTestPartResult(result); - - if (result_type != TestPartResult::kSuccess && - result_type != TestPartResult::kSkip) { - // gtest_break_on_failure takes precedence over - // gtest_throw_on_failure. This allows a user to set the latter - // in the code (perhaps in order to use Google Test assertions - // with another testing framework) and specify the former on the - // command line for debugging. - if (GTEST_FLAG(break_on_failure)) { -#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT - // Using DebugBreak on Windows allows gtest to still break into a debugger - // when a failure happens and both the --gtest_break_on_failure and - // the --gtest_catch_exceptions flags are specified. - DebugBreak(); -#elif (!defined(__native_client__)) && \ - ((defined(__clang__) || defined(__GNUC__)) && \ - (defined(__x86_64__) || defined(__i386__))) - // with clang/gcc we can achieve the same effect on x86 by invoking int3 - asm("int3"); -#else - // Dereference nullptr through a volatile pointer to prevent the compiler - // from removing. We use this rather than abort() or __builtin_trap() for - // portability: some debuggers don't correctly trap abort(). - *static_cast(nullptr) = 1; -#endif // GTEST_OS_WINDOWS - } else if (GTEST_FLAG(throw_on_failure)) { -#if GTEST_HAS_EXCEPTIONS - throw internal::GoogleTestFailureException(result); -#else - // We cannot call abort() as it generates a pop-up in debug mode - // that cannot be suppressed in VC 7.1 or below. - exit(1); -#endif - } - } -} - -// Adds a TestProperty to the current TestResult object when invoked from -// inside a test, to current TestSuite's ad_hoc_test_result_ when invoked -// from SetUpTestSuite or TearDownTestSuite, or to the global property set -// when invoked elsewhere. If the result already contains a property with -// the same key, the value will be updated. -void UnitTest::RecordProperty(const std::string& key, - const std::string& value) { - impl_->RecordProperty(TestProperty(key, value)); -} - -// Runs all tests in this UnitTest object and prints the result. -// Returns 0 if successful, or 1 otherwise. -// -// We don't protect this under mutex_, as we only support calling it -// from the main thread. -int UnitTest::Run() { - const bool in_death_test_child_process = - internal::GTEST_FLAG(internal_run_death_test).length() > 0; - - // Google Test implements this protocol for catching that a test - // program exits before returning control to Google Test: - // - // 1. Upon start, Google Test creates a file whose absolute path - // is specified by the environment variable - // TEST_PREMATURE_EXIT_FILE. - // 2. When Google Test has finished its work, it deletes the file. - // - // This allows a test runner to set TEST_PREMATURE_EXIT_FILE before - // running a Google-Test-based test program and check the existence - // of the file at the end of the test execution to see if it has - // exited prematurely. - - // If we are in the child process of a death test, don't - // create/delete the premature exit file, as doing so is unnecessary - // and will confuse the parent process. Otherwise, create/delete - // the file upon entering/leaving this function. If the program - // somehow exits before this function has a chance to return, the - // premature-exit file will be left undeleted, causing a test runner - // that understands the premature-exit-file protocol to report the - // test as having failed. - const internal::ScopedPrematureExitFile premature_exit_file( - in_death_test_child_process - ? nullptr - : internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE")); - - // Captures the value of GTEST_FLAG(catch_exceptions). This value will be - // used for the duration of the program. - impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions)); - -#if GTEST_OS_WINDOWS - // Either the user wants Google Test to catch exceptions thrown by the - // tests or this is executing in the context of death test child - // process. In either case the user does not want to see pop-up dialogs - // about crashes - they are expected. - if (impl()->catch_exceptions() || in_death_test_child_process) { -# if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT - // SetErrorMode doesn't exist on CE. - SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | - SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); -# endif // !GTEST_OS_WINDOWS_MOBILE - -# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE - // Death test children can be terminated with _abort(). On Windows, - // _abort() can show a dialog with a warning message. This forces the - // abort message to go to stderr instead. - _set_error_mode(_OUT_TO_STDERR); -# endif - -# if defined(_MSC_VER) && !GTEST_OS_WINDOWS_MOBILE - // In the debug version, Visual Studio pops up a separate dialog - // offering a choice to debug the aborted program. We need to suppress - // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement - // executed. Google Test will notify the user of any unexpected - // failure via stderr. - if (!GTEST_FLAG(break_on_failure)) - _set_abort_behavior( - 0x0, // Clear the following flags: - _WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump. - - // In debug mode, the Windows CRT can crash with an assertion over invalid - // input (e.g. passing an invalid file descriptor). The default handling - // for these assertions is to pop up a dialog and wait for user input. - // Instead ask the CRT to dump such assertions to stderr non-interactively. - if (!IsDebuggerPresent()) { - (void)_CrtSetReportMode(_CRT_ASSERT, - _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); - (void)_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); - } -# endif - } -#endif // GTEST_OS_WINDOWS - - return internal::HandleExceptionsInMethodIfSupported( - impl(), - &internal::UnitTestImpl::RunAllTests, - "auxiliary test code (environments or event listeners)") ? 0 : 1; -} - -// Returns the working directory when the first TEST() or TEST_F() was -// executed. -const char* UnitTest::original_working_dir() const { - return impl_->original_working_dir_.c_str(); -} - -// Returns the TestSuite object for the test that's currently running, -// or NULL if no test is running. -const TestSuite* UnitTest::current_test_suite() const - GTEST_LOCK_EXCLUDED_(mutex_) { - internal::MutexLock lock(&mutex_); - return impl_->current_test_suite(); -} - -// Legacy API is still available but deprecated -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -const TestCase* UnitTest::current_test_case() const - GTEST_LOCK_EXCLUDED_(mutex_) { - internal::MutexLock lock(&mutex_); - return impl_->current_test_suite(); -} -#endif - -// Returns the TestInfo object for the test that's currently running, -// or NULL if no test is running. -const TestInfo* UnitTest::current_test_info() const - GTEST_LOCK_EXCLUDED_(mutex_) { - internal::MutexLock lock(&mutex_); - return impl_->current_test_info(); -} - -// Returns the random seed used at the start of the current test run. -int UnitTest::random_seed() const { return impl_->random_seed(); } - -// Returns ParameterizedTestSuiteRegistry object used to keep track of -// value-parameterized tests and instantiate and register them. -internal::ParameterizedTestSuiteRegistry& -UnitTest::parameterized_test_registry() GTEST_LOCK_EXCLUDED_(mutex_) { - return impl_->parameterized_test_registry(); -} - -// Creates an empty UnitTest. -UnitTest::UnitTest() { - impl_ = new internal::UnitTestImpl(this); -} - -// Destructor of UnitTest. -UnitTest::~UnitTest() { - delete impl_; -} - -// Pushes a trace defined by SCOPED_TRACE() on to the per-thread -// Google Test trace stack. -void UnitTest::PushGTestTrace(const internal::TraceInfo& trace) - GTEST_LOCK_EXCLUDED_(mutex_) { - internal::MutexLock lock(&mutex_); - impl_->gtest_trace_stack().push_back(trace); -} - -// Pops a trace from the per-thread Google Test trace stack. -void UnitTest::PopGTestTrace() - GTEST_LOCK_EXCLUDED_(mutex_) { - internal::MutexLock lock(&mutex_); - impl_->gtest_trace_stack().pop_back(); -} - -namespace internal { - -UnitTestImpl::UnitTestImpl(UnitTest* parent) - : parent_(parent), - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355 /* using this in initializer */) - default_global_test_part_result_reporter_(this), - default_per_thread_test_part_result_reporter_(this), - GTEST_DISABLE_MSC_WARNINGS_POP_() global_test_part_result_repoter_( - &default_global_test_part_result_reporter_), - per_thread_test_part_result_reporter_( - &default_per_thread_test_part_result_reporter_), - parameterized_test_registry_(), - parameterized_tests_registered_(false), - last_death_test_suite_(-1), - current_test_suite_(nullptr), - current_test_info_(nullptr), - ad_hoc_test_result_(), - os_stack_trace_getter_(nullptr), - post_flag_parse_init_performed_(false), - random_seed_(0), // Will be overridden by the flag before first use. - random_(0), // Will be reseeded before first use. - start_timestamp_(0), - elapsed_time_(0), -#if GTEST_HAS_DEATH_TEST - death_test_factory_(new DefaultDeathTestFactory), -#endif - // Will be overridden by the flag before first use. - catch_exceptions_(false) { - listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter); -} - -UnitTestImpl::~UnitTestImpl() { - // Deletes every TestSuite. - ForEach(test_suites_, internal::Delete); - - // Deletes every Environment. - ForEach(environments_, internal::Delete); - - delete os_stack_trace_getter_; -} - -// Adds a TestProperty to the current TestResult object when invoked in a -// context of a test, to current test suite's ad_hoc_test_result when invoke -// from SetUpTestSuite/TearDownTestSuite, or to the global property set -// otherwise. If the result already contains a property with the same key, -// the value will be updated. -void UnitTestImpl::RecordProperty(const TestProperty& test_property) { - std::string xml_element; - TestResult* test_result; // TestResult appropriate for property recording. - - if (current_test_info_ != nullptr) { - xml_element = "testcase"; - test_result = &(current_test_info_->result_); - } else if (current_test_suite_ != nullptr) { - xml_element = "testsuite"; - test_result = &(current_test_suite_->ad_hoc_test_result_); - } else { - xml_element = "testsuites"; - test_result = &ad_hoc_test_result_; - } - test_result->RecordProperty(xml_element, test_property); -} - -#if GTEST_HAS_DEATH_TEST -// Disables event forwarding if the control is currently in a death test -// subprocess. Must not be called before InitGoogleTest. -void UnitTestImpl::SuppressTestEventsIfInSubprocess() { - if (internal_run_death_test_flag_.get() != nullptr) - listeners()->SuppressEventForwarding(); -} -#endif // GTEST_HAS_DEATH_TEST - -// Initializes event listeners performing XML output as specified by -// UnitTestOptions. Must not be called before InitGoogleTest. -void UnitTestImpl::ConfigureXmlOutput() { - const std::string& output_format = UnitTestOptions::GetOutputFormat(); - if (output_format == "xml") { - listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter( - UnitTestOptions::GetAbsolutePathToOutputFile().c_str())); - } else if (output_format == "json") { - listeners()->SetDefaultXmlGenerator(new JsonUnitTestResultPrinter( - UnitTestOptions::GetAbsolutePathToOutputFile().c_str())); - } else if (output_format != "") { - GTEST_LOG_(WARNING) << "WARNING: unrecognized output format \"" - << output_format << "\" ignored."; - } -} - -#if GTEST_CAN_STREAM_RESULTS_ -// Initializes event listeners for streaming test results in string form. -// Must not be called before InitGoogleTest. -void UnitTestImpl::ConfigureStreamingOutput() { - const std::string& target = GTEST_FLAG(stream_result_to); - if (!target.empty()) { - const size_t pos = target.find(':'); - if (pos != std::string::npos) { - listeners()->Append(new StreamingListener(target.substr(0, pos), - target.substr(pos+1))); - } else { - GTEST_LOG_(WARNING) << "unrecognized streaming target \"" << target - << "\" ignored."; - } - } -} -#endif // GTEST_CAN_STREAM_RESULTS_ - -// Performs initialization dependent upon flag values obtained in -// ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to -// ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest -// this function is also called from RunAllTests. Since this function can be -// called more than once, it has to be idempotent. -void UnitTestImpl::PostFlagParsingInit() { - // Ensures that this function does not execute more than once. - if (!post_flag_parse_init_performed_) { - post_flag_parse_init_performed_ = true; - -#if defined(GTEST_CUSTOM_TEST_EVENT_LISTENER_) - // Register to send notifications about key process state changes. - listeners()->Append(new GTEST_CUSTOM_TEST_EVENT_LISTENER_()); -#endif // defined(GTEST_CUSTOM_TEST_EVENT_LISTENER_) - -#if GTEST_HAS_DEATH_TEST - InitDeathTestSubprocessControlInfo(); - SuppressTestEventsIfInSubprocess(); -#endif // GTEST_HAS_DEATH_TEST - - // Registers parameterized tests. This makes parameterized tests - // available to the UnitTest reflection API without running - // RUN_ALL_TESTS. - RegisterParameterizedTests(); - - // Configures listeners for XML output. This makes it possible for users - // to shut down the default XML output before invoking RUN_ALL_TESTS. - ConfigureXmlOutput(); - - if (GTEST_FLAG(brief)) { - listeners()->SetDefaultResultPrinter(new BriefUnitTestResultPrinter); - } - -#if GTEST_CAN_STREAM_RESULTS_ - // Configures listeners for streaming test results to the specified server. - ConfigureStreamingOutput(); -#endif // GTEST_CAN_STREAM_RESULTS_ - -#if GTEST_HAS_ABSL - if (GTEST_FLAG(install_failure_signal_handler)) { - absl::FailureSignalHandlerOptions options; - absl::InstallFailureSignalHandler(options); - } -#endif // GTEST_HAS_ABSL - } -} - -// A predicate that checks the name of a TestSuite against a known -// value. -// -// This is used for implementation of the UnitTest class only. We put -// it in the anonymous namespace to prevent polluting the outer -// namespace. -// -// TestSuiteNameIs is copyable. -class TestSuiteNameIs { - public: - // Constructor. - explicit TestSuiteNameIs(const std::string& name) : name_(name) {} - - // Returns true if and only if the name of test_suite matches name_. - bool operator()(const TestSuite* test_suite) const { - return test_suite != nullptr && - strcmp(test_suite->name(), name_.c_str()) == 0; - } - - private: - std::string name_; -}; - -// Finds and returns a TestSuite with the given name. If one doesn't -// exist, creates one and returns it. It's the CALLER'S -// RESPONSIBILITY to ensure that this function is only called WHEN THE -// TESTS ARE NOT SHUFFLED. -// -// Arguments: -// -// test_suite_name: name of the test suite -// type_param: the name of the test suite's type parameter, or NULL if -// this is not a typed or a type-parameterized test suite. -// set_up_tc: pointer to the function that sets up the test suite -// tear_down_tc: pointer to the function that tears down the test suite -TestSuite* UnitTestImpl::GetTestSuite( - const char* test_suite_name, const char* type_param, - internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc) { - // Can we find a TestSuite with the given name? - const auto test_suite = - std::find_if(test_suites_.rbegin(), test_suites_.rend(), - TestSuiteNameIs(test_suite_name)); - - if (test_suite != test_suites_.rend()) return *test_suite; - - // No. Let's create one. - auto* const new_test_suite = - new TestSuite(test_suite_name, type_param, set_up_tc, tear_down_tc); - - // Is this a death test suite? - if (internal::UnitTestOptions::MatchesFilter(test_suite_name, - kDeathTestSuiteFilter)) { - // Yes. Inserts the test suite after the last death test suite - // defined so far. This only works when the test suites haven't - // been shuffled. Otherwise we may end up running a death test - // after a non-death test. - ++last_death_test_suite_; - test_suites_.insert(test_suites_.begin() + last_death_test_suite_, - new_test_suite); - } else { - // No. Appends to the end of the list. - test_suites_.push_back(new_test_suite); - } - - test_suite_indices_.push_back(static_cast(test_suite_indices_.size())); - return new_test_suite; -} - -// Helpers for setting up / tearing down the given environment. They -// are for use in the ForEach() function. -static void SetUpEnvironment(Environment* env) { env->SetUp(); } -static void TearDownEnvironment(Environment* env) { env->TearDown(); } - -// Runs all tests in this UnitTest object, prints the result, and -// returns true if all tests are successful. If any exception is -// thrown during a test, the test is considered to be failed, but the -// rest of the tests will still be run. -// -// When parameterized tests are enabled, it expands and registers -// parameterized tests first in RegisterParameterizedTests(). -// All other functions called from RunAllTests() may safely assume that -// parameterized tests are ready to be counted and run. -bool UnitTestImpl::RunAllTests() { - // True if and only if Google Test is initialized before RUN_ALL_TESTS() is - // called. - const bool gtest_is_initialized_before_run_all_tests = GTestIsInitialized(); - - // Do not run any test if the --help flag was specified. - if (g_help_flag) - return true; - - // Repeats the call to the post-flag parsing initialization in case the - // user didn't call InitGoogleTest. - PostFlagParsingInit(); - - // Even if sharding is not on, test runners may want to use the - // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding - // protocol. - internal::WriteToShardStatusFileIfNeeded(); - - // True if and only if we are in a subprocess for running a thread-safe-style - // death test. - bool in_subprocess_for_death_test = false; - -#if GTEST_HAS_DEATH_TEST - in_subprocess_for_death_test = - (internal_run_death_test_flag_.get() != nullptr); -# if defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) - if (in_subprocess_for_death_test) { - GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_(); - } -# endif // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) -#endif // GTEST_HAS_DEATH_TEST - - const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex, - in_subprocess_for_death_test); - - // Compares the full test names with the filter to decide which - // tests to run. - const bool has_tests_to_run = FilterTests(should_shard - ? HONOR_SHARDING_PROTOCOL - : IGNORE_SHARDING_PROTOCOL) > 0; - - // Lists the tests and exits if the --gtest_list_tests flag was specified. - if (GTEST_FLAG(list_tests)) { - // This must be called *after* FilterTests() has been called. - ListTestsMatchingFilter(); - return true; - } - - random_seed_ = GTEST_FLAG(shuffle) ? - GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0; - - // True if and only if at least one test has failed. - bool failed = false; - - TestEventListener* repeater = listeners()->repeater(); - - start_timestamp_ = GetTimeInMillis(); - repeater->OnTestProgramStart(*parent_); - - // How many times to repeat the tests? We don't want to repeat them - // when we are inside the subprocess of a death test. - const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat); - // Repeats forever if the repeat count is negative. - const bool gtest_repeat_forever = repeat < 0; - for (int i = 0; gtest_repeat_forever || i != repeat; i++) { - // We want to preserve failures generated by ad-hoc test - // assertions executed before RUN_ALL_TESTS(). - ClearNonAdHocTestResult(); - - Timer timer; - - // Shuffles test suites and tests if requested. - if (has_tests_to_run && GTEST_FLAG(shuffle)) { - random()->Reseed(static_cast(random_seed_)); - // This should be done before calling OnTestIterationStart(), - // such that a test event listener can see the actual test order - // in the event. - ShuffleTests(); - } - - // Tells the unit test event listeners that the tests are about to start. - repeater->OnTestIterationStart(*parent_, i); - - // Runs each test suite if there is at least one test to run. - if (has_tests_to_run) { - // Sets up all environments beforehand. - repeater->OnEnvironmentsSetUpStart(*parent_); - ForEach(environments_, SetUpEnvironment); - repeater->OnEnvironmentsSetUpEnd(*parent_); - - // Runs the tests only if there was no fatal failure or skip triggered - // during global set-up. - if (Test::IsSkipped()) { - // Emit diagnostics when global set-up calls skip, as it will not be - // emitted by default. - TestResult& test_result = - *internal::GetUnitTestImpl()->current_test_result(); - for (int j = 0; j < test_result.total_part_count(); ++j) { - const TestPartResult& test_part_result = - test_result.GetTestPartResult(j); - if (test_part_result.type() == TestPartResult::kSkip) { - const std::string& result = test_part_result.message(); - printf("%s\n", result.c_str()); - } - } - fflush(stdout); - } else if (!Test::HasFatalFailure()) { - for (int test_index = 0; test_index < total_test_suite_count(); - test_index++) { - GetMutableSuiteCase(test_index)->Run(); - if (GTEST_FLAG(fail_fast) && - GetMutableSuiteCase(test_index)->Failed()) { - for (int j = test_index + 1; j < total_test_suite_count(); j++) { - GetMutableSuiteCase(j)->Skip(); - } - break; - } - } - } else if (Test::HasFatalFailure()) { - // If there was a fatal failure during the global setup then we know we - // aren't going to run any tests. Explicitly mark all of the tests as - // skipped to make this obvious in the output. - for (int test_index = 0; test_index < total_test_suite_count(); - test_index++) { - GetMutableSuiteCase(test_index)->Skip(); - } - } - - // Tears down all environments in reverse order afterwards. - repeater->OnEnvironmentsTearDownStart(*parent_); - std::for_each(environments_.rbegin(), environments_.rend(), - TearDownEnvironment); - repeater->OnEnvironmentsTearDownEnd(*parent_); - } - - elapsed_time_ = timer.Elapsed(); - - // Tells the unit test event listener that the tests have just finished. - repeater->OnTestIterationEnd(*parent_, i); - - // Gets the result and clears it. - if (!Passed()) { - failed = true; - } - - // Restores the original test order after the iteration. This - // allows the user to quickly repro a failure that happens in the - // N-th iteration without repeating the first (N - 1) iterations. - // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in - // case the user somehow changes the value of the flag somewhere - // (it's always safe to unshuffle the tests). - UnshuffleTests(); - - if (GTEST_FLAG(shuffle)) { - // Picks a new random seed for each iteration. - random_seed_ = GetNextRandomSeed(random_seed_); - } - } - - repeater->OnTestProgramEnd(*parent_); - - if (!gtest_is_initialized_before_run_all_tests) { - ColoredPrintf( - GTestColor::kRed, - "\nIMPORTANT NOTICE - DO NOT IGNORE:\n" - "This test program did NOT call " GTEST_INIT_GOOGLE_TEST_NAME_ - "() before calling RUN_ALL_TESTS(). This is INVALID. Soon " GTEST_NAME_ - " will start to enforce the valid usage. " - "Please fix it ASAP, or IT WILL START TO FAIL.\n"); // NOLINT -#if GTEST_FOR_GOOGLE_ - ColoredPrintf(GTestColor::kRed, - "For more details, see http://wiki/Main/ValidGUnitMain.\n"); -#endif // GTEST_FOR_GOOGLE_ - } - - return !failed; -} - -// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file -// if the variable is present. If a file already exists at this location, this -// function will write over it. If the variable is present, but the file cannot -// be created, prints an error and exits. -void WriteToShardStatusFileIfNeeded() { - const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile); - if (test_shard_file != nullptr) { - FILE* const file = posix::FOpen(test_shard_file, "w"); - if (file == nullptr) { - ColoredPrintf(GTestColor::kRed, - "Could not write to the test shard status file \"%s\" " - "specified by the %s environment variable.\n", - test_shard_file, kTestShardStatusFile); - fflush(stdout); - exit(EXIT_FAILURE); - } - fclose(file); - } -} - -// Checks whether sharding is enabled by examining the relevant -// environment variable values. If the variables are present, -// but inconsistent (i.e., shard_index >= total_shards), prints -// an error and exits. If in_subprocess_for_death_test, sharding is -// disabled because it must only be applied to the original test -// process. Otherwise, we could filter out death tests we intended to execute. -bool ShouldShard(const char* total_shards_env, - const char* shard_index_env, - bool in_subprocess_for_death_test) { - if (in_subprocess_for_death_test) { - return false; - } - - const int32_t total_shards = Int32FromEnvOrDie(total_shards_env, -1); - const int32_t shard_index = Int32FromEnvOrDie(shard_index_env, -1); - - if (total_shards == -1 && shard_index == -1) { - return false; - } else if (total_shards == -1 && shard_index != -1) { - const Message msg = Message() - << "Invalid environment variables: you have " - << kTestShardIndex << " = " << shard_index - << ", but have left " << kTestTotalShards << " unset.\n"; - ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); - fflush(stdout); - exit(EXIT_FAILURE); - } else if (total_shards != -1 && shard_index == -1) { - const Message msg = Message() - << "Invalid environment variables: you have " - << kTestTotalShards << " = " << total_shards - << ", but have left " << kTestShardIndex << " unset.\n"; - ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); - fflush(stdout); - exit(EXIT_FAILURE); - } else if (shard_index < 0 || shard_index >= total_shards) { - const Message msg = Message() - << "Invalid environment variables: we require 0 <= " - << kTestShardIndex << " < " << kTestTotalShards - << ", but you have " << kTestShardIndex << "=" << shard_index - << ", " << kTestTotalShards << "=" << total_shards << ".\n"; - ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); - fflush(stdout); - exit(EXIT_FAILURE); - } - - return total_shards > 1; -} - -// Parses the environment variable var as an Int32. If it is unset, -// returns default_val. If it is not an Int32, prints an error -// and aborts. -int32_t Int32FromEnvOrDie(const char* var, int32_t default_val) { - const char* str_val = posix::GetEnv(var); - if (str_val == nullptr) { - return default_val; - } - - int32_t result; - if (!ParseInt32(Message() << "The value of environment variable " << var, - str_val, &result)) { - exit(EXIT_FAILURE); - } - return result; -} - -// Given the total number of shards, the shard index, and the test id, -// returns true if and only if the test should be run on this shard. The test id -// is some arbitrary but unique non-negative integer assigned to each test -// method. Assumes that 0 <= shard_index < total_shards. -bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) { - return (test_id % total_shards) == shard_index; -} - -// Compares the name of each test with the user-specified filter to -// decide whether the test should be run, then records the result in -// each TestSuite and TestInfo object. -// If shard_tests == true, further filters tests based on sharding -// variables in the environment - see -// https://github.com/google/googletest/blob/master/googletest/docs/advanced.md -// . Returns the number of tests that should run. -int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { - const int32_t total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ? - Int32FromEnvOrDie(kTestTotalShards, -1) : -1; - const int32_t shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ? - Int32FromEnvOrDie(kTestShardIndex, -1) : -1; - - // num_runnable_tests are the number of tests that will - // run across all shards (i.e., match filter and are not disabled). - // num_selected_tests are the number of tests to be run on - // this shard. - int num_runnable_tests = 0; - int num_selected_tests = 0; - for (auto* test_suite : test_suites_) { - const std::string& test_suite_name = test_suite->name(); - test_suite->set_should_run(false); - - for (size_t j = 0; j < test_suite->test_info_list().size(); j++) { - TestInfo* const test_info = test_suite->test_info_list()[j]; - const std::string test_name(test_info->name()); - // A test is disabled if test suite name or test name matches - // kDisableTestFilter. - const bool is_disabled = internal::UnitTestOptions::MatchesFilter( - test_suite_name, kDisableTestFilter) || - internal::UnitTestOptions::MatchesFilter( - test_name, kDisableTestFilter); - test_info->is_disabled_ = is_disabled; - - const bool matches_filter = internal::UnitTestOptions::FilterMatchesTest( - test_suite_name, test_name); - test_info->matches_filter_ = matches_filter; - - const bool is_runnable = - (GTEST_FLAG(also_run_disabled_tests) || !is_disabled) && - matches_filter; - - const bool is_in_another_shard = - shard_tests != IGNORE_SHARDING_PROTOCOL && - !ShouldRunTestOnShard(total_shards, shard_index, num_runnable_tests); - test_info->is_in_another_shard_ = is_in_another_shard; - const bool is_selected = is_runnable && !is_in_another_shard; - - num_runnable_tests += is_runnable; - num_selected_tests += is_selected; - - test_info->should_run_ = is_selected; - test_suite->set_should_run(test_suite->should_run() || is_selected); - } - } - return num_selected_tests; -} - -// Prints the given C-string on a single line by replacing all '\n' -// characters with string "\\n". If the output takes more than -// max_length characters, only prints the first max_length characters -// and "...". -static void PrintOnOneLine(const char* str, int max_length) { - if (str != nullptr) { - for (int i = 0; *str != '\0'; ++str) { - if (i >= max_length) { - printf("..."); - break; - } - if (*str == '\n') { - printf("\\n"); - i += 2; - } else { - printf("%c", *str); - ++i; - } - } - } -} - -// Prints the names of the tests matching the user-specified filter flag. -void UnitTestImpl::ListTestsMatchingFilter() { - // Print at most this many characters for each type/value parameter. - const int kMaxParamLength = 250; - - for (auto* test_suite : test_suites_) { - bool printed_test_suite_name = false; - - for (size_t j = 0; j < test_suite->test_info_list().size(); j++) { - const TestInfo* const test_info = test_suite->test_info_list()[j]; - if (test_info->matches_filter_) { - if (!printed_test_suite_name) { - printed_test_suite_name = true; - printf("%s.", test_suite->name()); - if (test_suite->type_param() != nullptr) { - printf(" # %s = ", kTypeParamLabel); - // We print the type parameter on a single line to make - // the output easy to parse by a program. - PrintOnOneLine(test_suite->type_param(), kMaxParamLength); - } - printf("\n"); - } - printf(" %s", test_info->name()); - if (test_info->value_param() != nullptr) { - printf(" # %s = ", kValueParamLabel); - // We print the value parameter on a single line to make the - // output easy to parse by a program. - PrintOnOneLine(test_info->value_param(), kMaxParamLength); - } - printf("\n"); - } - } - } - fflush(stdout); - const std::string& output_format = UnitTestOptions::GetOutputFormat(); - if (output_format == "xml" || output_format == "json") { - FILE* fileout = OpenFileForWriting( - UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); - std::stringstream stream; - if (output_format == "xml") { - XmlUnitTestResultPrinter( - UnitTestOptions::GetAbsolutePathToOutputFile().c_str()) - .PrintXmlTestsList(&stream, test_suites_); - } else if (output_format == "json") { - JsonUnitTestResultPrinter( - UnitTestOptions::GetAbsolutePathToOutputFile().c_str()) - .PrintJsonTestList(&stream, test_suites_); - } - fprintf(fileout, "%s", StringStreamToString(&stream).c_str()); - fclose(fileout); - } -} - -// Sets the OS stack trace getter. -// -// Does nothing if the input and the current OS stack trace getter are -// the same; otherwise, deletes the old getter and makes the input the -// current getter. -void UnitTestImpl::set_os_stack_trace_getter( - OsStackTraceGetterInterface* getter) { - if (os_stack_trace_getter_ != getter) { - delete os_stack_trace_getter_; - os_stack_trace_getter_ = getter; - } -} - -// Returns the current OS stack trace getter if it is not NULL; -// otherwise, creates an OsStackTraceGetter, makes it the current -// getter, and returns it. -OsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() { - if (os_stack_trace_getter_ == nullptr) { -#ifdef GTEST_OS_STACK_TRACE_GETTER_ - os_stack_trace_getter_ = new GTEST_OS_STACK_TRACE_GETTER_; -#else - os_stack_trace_getter_ = new OsStackTraceGetter; -#endif // GTEST_OS_STACK_TRACE_GETTER_ - } - - return os_stack_trace_getter_; -} - -// Returns the most specific TestResult currently running. -TestResult* UnitTestImpl::current_test_result() { - if (current_test_info_ != nullptr) { - return ¤t_test_info_->result_; - } - if (current_test_suite_ != nullptr) { - return ¤t_test_suite_->ad_hoc_test_result_; - } - return &ad_hoc_test_result_; -} - -// Shuffles all test suites, and the tests within each test suite, -// making sure that death tests are still run first. -void UnitTestImpl::ShuffleTests() { - // Shuffles the death test suites. - ShuffleRange(random(), 0, last_death_test_suite_ + 1, &test_suite_indices_); - - // Shuffles the non-death test suites. - ShuffleRange(random(), last_death_test_suite_ + 1, - static_cast(test_suites_.size()), &test_suite_indices_); - - // Shuffles the tests inside each test suite. - for (auto& test_suite : test_suites_) { - test_suite->ShuffleTests(random()); - } -} - -// Restores the test suites and tests to their order before the first shuffle. -void UnitTestImpl::UnshuffleTests() { - for (size_t i = 0; i < test_suites_.size(); i++) { - // Unshuffles the tests in each test suite. - test_suites_[i]->UnshuffleTests(); - // Resets the index of each test suite. - test_suite_indices_[i] = static_cast(i); - } -} - -// Returns the current OS stack trace as an std::string. -// -// The maximum number of stack frames to be included is specified by -// the gtest_stack_trace_depth flag. The skip_count parameter -// specifies the number of top frames to be skipped, which doesn't -// count against the number of frames to be included. -// -// For example, if Foo() calls Bar(), which in turn calls -// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in -// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. -std::string GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, - int skip_count) { - // We pass skip_count + 1 to skip this wrapper function in addition - // to what the user really wants to skip. - return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1); -} - -// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to -// suppress unreachable code warnings. -namespace { -class ClassUniqueToAlwaysTrue {}; -} - -bool IsTrue(bool condition) { return condition; } - -bool AlwaysTrue() { -#if GTEST_HAS_EXCEPTIONS - // This condition is always false so AlwaysTrue() never actually throws, - // but it makes the compiler think that it may throw. - if (IsTrue(false)) - throw ClassUniqueToAlwaysTrue(); -#endif // GTEST_HAS_EXCEPTIONS - return true; -} - -// If *pstr starts with the given prefix, modifies *pstr to be right -// past the prefix and returns true; otherwise leaves *pstr unchanged -// and returns false. None of pstr, *pstr, and prefix can be NULL. -bool SkipPrefix(const char* prefix, const char** pstr) { - const size_t prefix_len = strlen(prefix); - if (strncmp(*pstr, prefix, prefix_len) == 0) { - *pstr += prefix_len; - return true; - } - return false; -} - -// Parses a string as a command line flag. The string should have -// the format "--flag=value". When def_optional is true, the "=value" -// part can be omitted. -// -// Returns the value of the flag, or NULL if the parsing failed. -static const char* ParseFlagValue(const char* str, const char* flag, - bool def_optional) { - // str and flag must not be NULL. - if (str == nullptr || flag == nullptr) return nullptr; - - // The flag must start with "--" followed by GTEST_FLAG_PREFIX_. - const std::string flag_str = std::string("--") + GTEST_FLAG_PREFIX_ + flag; - const size_t flag_len = flag_str.length(); - if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr; - - // Skips the flag name. - const char* flag_end = str + flag_len; - - // When def_optional is true, it's OK to not have a "=value" part. - if (def_optional && (flag_end[0] == '\0')) { - return flag_end; - } - - // If def_optional is true and there are more characters after the - // flag name, or if def_optional is false, there must be a '=' after - // the flag name. - if (flag_end[0] != '=') return nullptr; - - // Returns the string after "=". - return flag_end + 1; -} - -// Parses a string for a bool flag, in the form of either -// "--flag=value" or "--flag". -// -// In the former case, the value is taken as true as long as it does -// not start with '0', 'f', or 'F'. -// -// In the latter case, the value is taken as true. -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -static bool ParseBoolFlag(const char* str, const char* flag, bool* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, true); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - // Converts the string value to a bool. - *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); - return true; -} - -// Parses a string for an int32_t flag, in the form of "--flag=value". -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, false); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - // Sets *value to the value of the flag. - return ParseInt32(Message() << "The value of flag --" << flag, - value_str, value); -} - -// Parses a string for a string flag, in the form of "--flag=value". -// -// On success, stores the value of the flag in *value, and returns -// true. On failure, returns false without changing *value. -template -static bool ParseStringFlag(const char* str, const char* flag, String* value) { - // Gets the value of the flag as a string. - const char* const value_str = ParseFlagValue(str, flag, false); - - // Aborts if the parsing failed. - if (value_str == nullptr) return false; - - // Sets *value to the value of the flag. - *value = value_str; - return true; -} - -// Determines whether a string has a prefix that Google Test uses for its -// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_. -// If Google Test detects that a command line flag has its prefix but is not -// recognized, it will print its help message. Flags starting with -// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test -// internal flags and do not trigger the help message. -static bool HasGoogleTestFlagPrefix(const char* str) { - return (SkipPrefix("--", &str) || - SkipPrefix("-", &str) || - SkipPrefix("/", &str)) && - !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) && - (SkipPrefix(GTEST_FLAG_PREFIX_, &str) || - SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str)); -} - -// Prints a string containing code-encoded text. The following escape -// sequences can be used in the string to control the text color: -// -// @@ prints a single '@' character. -// @R changes the color to red. -// @G changes the color to green. -// @Y changes the color to yellow. -// @D changes to the default terminal text color. -// -static void PrintColorEncoded(const char* str) { - GTestColor color = GTestColor::kDefault; // The current color. - - // Conceptually, we split the string into segments divided by escape - // sequences. Then we print one segment at a time. At the end of - // each iteration, the str pointer advances to the beginning of the - // next segment. - for (;;) { - const char* p = strchr(str, '@'); - if (p == nullptr) { - ColoredPrintf(color, "%s", str); - return; - } - - ColoredPrintf(color, "%s", std::string(str, p).c_str()); - - const char ch = p[1]; - str = p + 2; - if (ch == '@') { - ColoredPrintf(color, "@"); - } else if (ch == 'D') { - color = GTestColor::kDefault; - } else if (ch == 'R') { - color = GTestColor::kRed; - } else if (ch == 'G') { - color = GTestColor::kGreen; - } else if (ch == 'Y') { - color = GTestColor::kYellow; - } else { - --str; - } - } -} - -static const char kColorEncodedHelpMessage[] = - "This program contains tests written using " GTEST_NAME_ - ". You can use the\n" - "following command line flags to control its behavior:\n" - "\n" - "Test Selection:\n" - " @G--" GTEST_FLAG_PREFIX_ - "list_tests@D\n" - " List the names of all tests instead of running them. The name of\n" - " TEST(Foo, Bar) is \"Foo.Bar\".\n" - " @G--" GTEST_FLAG_PREFIX_ - "filter=@YPOSITIVE_PATTERNS" - "[@G-@YNEGATIVE_PATTERNS]@D\n" - " Run only the tests whose name matches one of the positive patterns " - "but\n" - " none of the negative patterns. '?' matches any single character; " - "'*'\n" - " matches any substring; ':' separates two patterns.\n" - " @G--" GTEST_FLAG_PREFIX_ - "also_run_disabled_tests@D\n" - " Run all disabled tests too.\n" - "\n" - "Test Execution:\n" - " @G--" GTEST_FLAG_PREFIX_ - "repeat=@Y[COUNT]@D\n" - " Run the tests repeatedly; use a negative count to repeat forever.\n" - " @G--" GTEST_FLAG_PREFIX_ - "shuffle@D\n" - " Randomize tests' orders on every iteration.\n" - " @G--" GTEST_FLAG_PREFIX_ - "random_seed=@Y[NUMBER]@D\n" - " Random number seed to use for shuffling test orders (between 1 and\n" - " 99999, or 0 to use a seed based on the current time).\n" - "\n" - "Test Output:\n" - " @G--" GTEST_FLAG_PREFIX_ - "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n" - " Enable/disable colored output. The default is @Gauto@D.\n" - " @G--" GTEST_FLAG_PREFIX_ - "brief=1@D\n" - " Only print test failures.\n" - " @G--" GTEST_FLAG_PREFIX_ - "print_time=0@D\n" - " Don't print the elapsed time of each test.\n" - " @G--" GTEST_FLAG_PREFIX_ - "output=@Y(@Gjson@Y|@Gxml@Y)[@G:@YDIRECTORY_PATH@G" GTEST_PATH_SEP_ - "@Y|@G:@YFILE_PATH]@D\n" - " Generate a JSON or XML report in the given directory or with the " - "given\n" - " file name. @YFILE_PATH@D defaults to @Gtest_detail.xml@D.\n" -# if GTEST_CAN_STREAM_RESULTS_ - " @G--" GTEST_FLAG_PREFIX_ - "stream_result_to=@YHOST@G:@YPORT@D\n" - " Stream test results to the given server.\n" -# endif // GTEST_CAN_STREAM_RESULTS_ - "\n" - "Assertion Behavior:\n" -# if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS - " @G--" GTEST_FLAG_PREFIX_ - "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n" - " Set the default death test style.\n" -# endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS - " @G--" GTEST_FLAG_PREFIX_ - "break_on_failure@D\n" - " Turn assertion failures into debugger break-points.\n" - " @G--" GTEST_FLAG_PREFIX_ - "throw_on_failure@D\n" - " Turn assertion failures into C++ exceptions for use by an external\n" - " test framework.\n" - " @G--" GTEST_FLAG_PREFIX_ - "catch_exceptions=0@D\n" - " Do not report exceptions as test failures. Instead, allow them\n" - " to crash the program or throw a pop-up (on Windows).\n" - "\n" - "Except for @G--" GTEST_FLAG_PREFIX_ - "list_tests@D, you can alternatively set " - "the corresponding\n" - "environment variable of a flag (all letters in upper-case). For example, " - "to\n" - "disable colored text output, you can either specify " - "@G--" GTEST_FLAG_PREFIX_ - "color=no@D or set\n" - "the @G" GTEST_FLAG_PREFIX_UPPER_ - "COLOR@D environment variable to @Gno@D.\n" - "\n" - "For more information, please read the " GTEST_NAME_ - " documentation at\n" - "@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ - "\n" - "(not one in your own code or tests), please report it to\n" - "@G<" GTEST_DEV_EMAIL_ ">@D.\n"; - -static bool ParseGoogleTestFlag(const char* const arg) { - return ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag, - >EST_FLAG(also_run_disabled_tests)) || - ParseBoolFlag(arg, kBreakOnFailureFlag, - >EST_FLAG(break_on_failure)) || - ParseBoolFlag(arg, kCatchExceptionsFlag, - >EST_FLAG(catch_exceptions)) || - ParseStringFlag(arg, kColorFlag, >EST_FLAG(color)) || - ParseStringFlag(arg, kDeathTestStyleFlag, - >EST_FLAG(death_test_style)) || - ParseBoolFlag(arg, kDeathTestUseFork, - >EST_FLAG(death_test_use_fork)) || - ParseBoolFlag(arg, kFailFast, >EST_FLAG(fail_fast)) || - ParseStringFlag(arg, kFilterFlag, >EST_FLAG(filter)) || - ParseStringFlag(arg, kInternalRunDeathTestFlag, - >EST_FLAG(internal_run_death_test)) || - ParseBoolFlag(arg, kListTestsFlag, >EST_FLAG(list_tests)) || - ParseStringFlag(arg, kOutputFlag, >EST_FLAG(output)) || - ParseBoolFlag(arg, kBriefFlag, >EST_FLAG(brief)) || - ParseBoolFlag(arg, kPrintTimeFlag, >EST_FLAG(print_time)) || - ParseBoolFlag(arg, kPrintUTF8Flag, >EST_FLAG(print_utf8)) || - ParseInt32Flag(arg, kRandomSeedFlag, >EST_FLAG(random_seed)) || - ParseInt32Flag(arg, kRepeatFlag, >EST_FLAG(repeat)) || - ParseBoolFlag(arg, kShuffleFlag, >EST_FLAG(shuffle)) || - ParseInt32Flag(arg, kStackTraceDepthFlag, - >EST_FLAG(stack_trace_depth)) || - ParseStringFlag(arg, kStreamResultToFlag, - >EST_FLAG(stream_result_to)) || - ParseBoolFlag(arg, kThrowOnFailureFlag, >EST_FLAG(throw_on_failure)); -} - -#if GTEST_USE_OWN_FLAGFILE_FLAG_ -static void LoadFlagsFromFile(const std::string& path) { - FILE* flagfile = posix::FOpen(path.c_str(), "r"); - if (!flagfile) { - GTEST_LOG_(FATAL) << "Unable to open file \"" << GTEST_FLAG(flagfile) - << "\""; - } - std::string contents(ReadEntireFile(flagfile)); - posix::FClose(flagfile); - std::vector lines; - SplitString(contents, '\n', &lines); - for (size_t i = 0; i < lines.size(); ++i) { - if (lines[i].empty()) - continue; - if (!ParseGoogleTestFlag(lines[i].c_str())) - g_help_flag = true; - } -} -#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ - -// Parses the command line for Google Test flags, without initializing -// other parts of Google Test. The type parameter CharType can be -// instantiated to either char or wchar_t. -template -void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { - for (int i = 1; i < *argc; i++) { - const std::string arg_string = StreamableToString(argv[i]); - const char* const arg = arg_string.c_str(); - - using internal::ParseBoolFlag; - using internal::ParseInt32Flag; - using internal::ParseStringFlag; - - bool remove_flag = false; - if (ParseGoogleTestFlag(arg)) { - remove_flag = true; -#if GTEST_USE_OWN_FLAGFILE_FLAG_ - } else if (ParseStringFlag(arg, kFlagfileFlag, >EST_FLAG(flagfile))) { - LoadFlagsFromFile(GTEST_FLAG(flagfile)); - remove_flag = true; -#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ - } else if (arg_string == "--help" || arg_string == "-h" || - arg_string == "-?" || arg_string == "/?" || - HasGoogleTestFlagPrefix(arg)) { - // Both help flag and unrecognized Google Test flags (excluding - // internal ones) trigger help display. - g_help_flag = true; - } - - if (remove_flag) { - // Shift the remainder of the argv list left by one. Note - // that argv has (*argc + 1) elements, the last one always being - // NULL. The following loop moves the trailing NULL element as - // well. - for (int j = i; j != *argc; j++) { - argv[j] = argv[j + 1]; - } - - // Decrements the argument count. - (*argc)--; - - // We also need to decrement the iterator as we just removed - // an element. - i--; - } - } - - if (g_help_flag) { - // We print the help here instead of in RUN_ALL_TESTS(), as the - // latter may not be called at all if the user is using Google - // Test with another testing framework. - PrintColorEncoded(kColorEncodedHelpMessage); - } -} - -// Parses the command line for Google Test flags, without initializing -// other parts of Google Test. -void ParseGoogleTestFlagsOnly(int* argc, char** argv) { - ParseGoogleTestFlagsOnlyImpl(argc, argv); - - // Fix the value of *_NSGetArgc() on macOS, but if and only if - // *_NSGetArgv() == argv - // Only applicable to char** version of argv -#if GTEST_OS_MAC -#ifndef GTEST_OS_IOS - if (*_NSGetArgv() == argv) { - *_NSGetArgc() = *argc; - } -#endif -#endif -} -void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) { - ParseGoogleTestFlagsOnlyImpl(argc, argv); -} - -// The internal implementation of InitGoogleTest(). -// -// The type parameter CharType can be instantiated to either char or -// wchar_t. -template -void InitGoogleTestImpl(int* argc, CharType** argv) { - // We don't want to run the initialization code twice. - if (GTestIsInitialized()) return; - - if (*argc <= 0) return; - - g_argvs.clear(); - for (int i = 0; i != *argc; i++) { - g_argvs.push_back(StreamableToString(argv[i])); - } - -#if GTEST_HAS_ABSL - absl::InitializeSymbolizer(g_argvs[0].c_str()); -#endif // GTEST_HAS_ABSL - - ParseGoogleTestFlagsOnly(argc, argv); - GetUnitTestImpl()->PostFlagParsingInit(); -} - -} // namespace internal - -// Initializes Google Test. This must be called before calling -// RUN_ALL_TESTS(). In particular, it parses a command line for the -// flags that Google Test recognizes. Whenever a Google Test flag is -// seen, it is removed from argv, and *argc is decremented. -// -// No value is returned. Instead, the Google Test flag variables are -// updated. -// -// Calling the function for the second time has no user-visible effect. -void InitGoogleTest(int* argc, char** argv) { -#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) - GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv); -#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) - internal::InitGoogleTestImpl(argc, argv); -#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) -} - -// This overloaded version can be used in Windows programs compiled in -// UNICODE mode. -void InitGoogleTest(int* argc, wchar_t** argv) { -#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) - GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv); -#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) - internal::InitGoogleTestImpl(argc, argv); -#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) -} - -// This overloaded version can be used on Arduino/embedded platforms where -// there is no argc/argv. -void InitGoogleTest() { - // Since Arduino doesn't have a command line, fake out the argc/argv arguments - int argc = 1; - const auto arg0 = "dummy"; - char* argv0 = const_cast(arg0); - char** argv = &argv0; - -#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) - GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(&argc, argv); -#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) - internal::InitGoogleTestImpl(&argc, argv); -#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) -} - -std::string TempDir() { -#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) - return GTEST_CUSTOM_TEMPDIR_FUNCTION_(); -#elif GTEST_OS_WINDOWS_MOBILE - return "\\temp\\"; -#elif GTEST_OS_WINDOWS - const char* temp_dir = internal::posix::GetEnv("TEMP"); - if (temp_dir == nullptr || temp_dir[0] == '\0') { - return "\\temp\\"; - } else if (temp_dir[strlen(temp_dir) - 1] == '\\') { - return temp_dir; - } else { - return std::string(temp_dir) + "\\"; - } -#elif GTEST_OS_LINUX_ANDROID - const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR"); - if (temp_dir == nullptr || temp_dir[0] == '\0') { - return "/data/local/tmp/"; - } else { - return temp_dir; - } -#elif GTEST_OS_LINUX - const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR"); - if (temp_dir == nullptr || temp_dir[0] == '\0') { - return "/tmp/"; - } else { - return temp_dir; - } -#else - return "/tmp/"; -#endif // GTEST_OS_WINDOWS_MOBILE -} - -// Class ScopedTrace - -// Pushes the given source file location and message onto a per-thread -// trace stack maintained by Google Test. -void ScopedTrace::PushTrace(const char* file, int line, std::string message) { - internal::TraceInfo trace; - trace.file = file; - trace.line = line; - trace.message.swap(message); - - UnitTest::GetInstance()->PushGTestTrace(trace); -} - -// Pops the info pushed by the c'tor. -ScopedTrace::~ScopedTrace() - GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) { - UnitTest::GetInstance()->PopGTestTrace(); -} - -} // namespace testing -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// This file implements death tests. - - -#include -#include - - -#if GTEST_HAS_DEATH_TEST - -# if GTEST_OS_MAC -# include -# endif // GTEST_OS_MAC - -# include -# include -# include - -# if GTEST_OS_LINUX -# include -# endif // GTEST_OS_LINUX - -# include - -# if GTEST_OS_WINDOWS -# include -# else -# include -# include -# endif // GTEST_OS_WINDOWS - -# if GTEST_OS_QNX -# include -# endif // GTEST_OS_QNX - -# if GTEST_OS_FUCHSIA -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# endif // GTEST_OS_FUCHSIA - -#endif // GTEST_HAS_DEATH_TEST - - -namespace testing { - -// Constants. - -// The default death test style. -// -// This is defined in internal/gtest-port.h as "fast", but can be overridden by -// a definition in internal/custom/gtest-port.h. The recommended value, which is -// used internally at Google, is "threadsafe". -static const char kDefaultDeathTestStyle[] = GTEST_DEFAULT_DEATH_TEST_STYLE; - -GTEST_DEFINE_string_( - death_test_style, - internal::StringFromGTestEnv("death_test_style", kDefaultDeathTestStyle), - "Indicates how to run a death test in a forked child process: " - "\"threadsafe\" (child process re-executes the test binary " - "from the beginning, running only the specific death test) or " - "\"fast\" (child process runs the death test immediately " - "after forking)."); - -GTEST_DEFINE_bool_( - death_test_use_fork, - internal::BoolFromGTestEnv("death_test_use_fork", false), - "Instructs to use fork()/_exit() instead of clone() in death tests. " - "Ignored and always uses fork() on POSIX systems where clone() is not " - "implemented. Useful when running under valgrind or similar tools if " - "those do not support clone(). Valgrind 3.3.1 will just fail if " - "it sees an unsupported combination of clone() flags. " - "It is not recommended to use this flag w/o valgrind though it will " - "work in 99% of the cases. Once valgrind is fixed, this flag will " - "most likely be removed."); - -namespace internal { -GTEST_DEFINE_string_( - internal_run_death_test, "", - "Indicates the file, line number, temporal index of " - "the single death test to run, and a file descriptor to " - "which a success code may be sent, all separated by " - "the '|' characters. This flag is specified if and only if the " - "current process is a sub-process launched for running a thread-safe " - "death test. FOR INTERNAL USE ONLY."); -} // namespace internal - -#if GTEST_HAS_DEATH_TEST - -namespace internal { - -// Valid only for fast death tests. Indicates the code is running in the -// child process of a fast style death test. -# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA -static bool g_in_fast_death_test_child = false; -# endif - -// Returns a Boolean value indicating whether the caller is currently -// executing in the context of the death test child process. Tools such as -// Valgrind heap checkers may need this to modify their behavior in death -// tests. IMPORTANT: This is an internal utility. Using it may break the -// implementation of death tests. User code MUST NOT use it. -bool InDeathTestChild() { -# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA - - // On Windows and Fuchsia, death tests are thread-safe regardless of the value - // of the death_test_style flag. - return !GTEST_FLAG(internal_run_death_test).empty(); - -# else - - if (GTEST_FLAG(death_test_style) == "threadsafe") - return !GTEST_FLAG(internal_run_death_test).empty(); - else - return g_in_fast_death_test_child; -#endif -} - -} // namespace internal - -// ExitedWithCode constructor. -ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) { -} - -// ExitedWithCode function-call operator. -bool ExitedWithCode::operator()(int exit_status) const { -# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA - - return exit_status == exit_code_; - -# else - - return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_; - -# endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA -} - -# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA -// KilledBySignal constructor. -KilledBySignal::KilledBySignal(int signum) : signum_(signum) { -} - -// KilledBySignal function-call operator. -bool KilledBySignal::operator()(int exit_status) const { -# if defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_) - { - bool result; - if (GTEST_KILLED_BY_SIGNAL_OVERRIDE_(signum_, exit_status, &result)) { - return result; - } - } -# endif // defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_) - return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_; -} -# endif // !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA - -namespace internal { - -// Utilities needed for death tests. - -// Generates a textual description of a given exit code, in the format -// specified by wait(2). -static std::string ExitSummary(int exit_code) { - Message m; - -# if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA - - m << "Exited with exit status " << exit_code; - -# else - - if (WIFEXITED(exit_code)) { - m << "Exited with exit status " << WEXITSTATUS(exit_code); - } else if (WIFSIGNALED(exit_code)) { - m << "Terminated by signal " << WTERMSIG(exit_code); - } -# ifdef WCOREDUMP - if (WCOREDUMP(exit_code)) { - m << " (core dumped)"; - } -# endif -# endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA - - return m.GetString(); -} - -// Returns true if exit_status describes a process that was terminated -// by a signal, or exited normally with a nonzero exit code. -bool ExitedUnsuccessfully(int exit_status) { - return !ExitedWithCode(0)(exit_status); -} - -# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA -// Generates a textual failure message when a death test finds more than -// one thread running, or cannot determine the number of threads, prior -// to executing the given statement. It is the responsibility of the -// caller not to pass a thread_count of 1. -static std::string DeathTestThreadWarning(size_t thread_count) { - Message msg; - msg << "Death tests use fork(), which is unsafe particularly" - << " in a threaded context. For this test, " << GTEST_NAME_ << " "; - if (thread_count == 0) { - msg << "couldn't detect the number of threads."; - } else { - msg << "detected " << thread_count << " threads."; - } - msg << " See " - "https://github.com/google/googletest/blob/master/docs/" - "advanced.md#death-tests-and-threads" - << " for more explanation and suggested solutions, especially if" - << " this is the last message you see before your test times out."; - return msg.GetString(); -} -# endif // !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA - -// Flag characters for reporting a death test that did not die. -static const char kDeathTestLived = 'L'; -static const char kDeathTestReturned = 'R'; -static const char kDeathTestThrew = 'T'; -static const char kDeathTestInternalError = 'I'; - -#if GTEST_OS_FUCHSIA - -// File descriptor used for the pipe in the child process. -static const int kFuchsiaReadPipeFd = 3; - -#endif - -// An enumeration describing all of the possible ways that a death test can -// conclude. DIED means that the process died while executing the test -// code; LIVED means that process lived beyond the end of the test code; -// RETURNED means that the test statement attempted to execute a return -// statement, which is not allowed; THREW means that the test statement -// returned control by throwing an exception. IN_PROGRESS means the test -// has not yet concluded. -enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW }; - -// Routine for aborting the program which is safe to call from an -// exec-style death test child process, in which case the error -// message is propagated back to the parent process. Otherwise, the -// message is simply printed to stderr. In either case, the program -// then exits with status 1. -static void DeathTestAbort(const std::string& message) { - // On a POSIX system, this function may be called from a threadsafe-style - // death test child process, which operates on a very small stack. Use - // the heap for any additional non-minuscule memory requirements. - const InternalRunDeathTestFlag* const flag = - GetUnitTestImpl()->internal_run_death_test_flag(); - if (flag != nullptr) { - FILE* parent = posix::FDOpen(flag->write_fd(), "w"); - fputc(kDeathTestInternalError, parent); - fprintf(parent, "%s", message.c_str()); - fflush(parent); - _exit(1); - } else { - fprintf(stderr, "%s", message.c_str()); - fflush(stderr); - posix::Abort(); - } -} - -// A replacement for CHECK that calls DeathTestAbort if the assertion -// fails. -# define GTEST_DEATH_TEST_CHECK_(expression) \ - do { \ - if (!::testing::internal::IsTrue(expression)) { \ - DeathTestAbort( \ - ::std::string("CHECK failed: File ") + __FILE__ + ", line " \ - + ::testing::internal::StreamableToString(__LINE__) + ": " \ - + #expression); \ - } \ - } while (::testing::internal::AlwaysFalse()) - -// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for -// evaluating any system call that fulfills two conditions: it must return -// -1 on failure, and set errno to EINTR when it is interrupted and -// should be tried again. The macro expands to a loop that repeatedly -// evaluates the expression as long as it evaluates to -1 and sets -// errno to EINTR. If the expression evaluates to -1 but errno is -// something other than EINTR, DeathTestAbort is called. -# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \ - do { \ - int gtest_retval; \ - do { \ - gtest_retval = (expression); \ - } while (gtest_retval == -1 && errno == EINTR); \ - if (gtest_retval == -1) { \ - DeathTestAbort( \ - ::std::string("CHECK failed: File ") + __FILE__ + ", line " \ - + ::testing::internal::StreamableToString(__LINE__) + ": " \ - + #expression + " != -1"); \ - } \ - } while (::testing::internal::AlwaysFalse()) - -// Returns the message describing the last system error in errno. -std::string GetLastErrnoDescription() { - return errno == 0 ? "" : posix::StrError(errno); -} - -// This is called from a death test parent process to read a failure -// message from the death test child process and log it with the FATAL -// severity. On Windows, the message is read from a pipe handle. On other -// platforms, it is read from a file descriptor. -static void FailFromInternalError(int fd) { - Message error; - char buffer[256]; - int num_read; - - do { - while ((num_read = posix::Read(fd, buffer, 255)) > 0) { - buffer[num_read] = '\0'; - error << buffer; - } - } while (num_read == -1 && errno == EINTR); - - if (num_read == 0) { - GTEST_LOG_(FATAL) << error.GetString(); - } else { - const int last_error = errno; - GTEST_LOG_(FATAL) << "Error while reading death test internal: " - << GetLastErrnoDescription() << " [" << last_error << "]"; - } -} - -// Death test constructor. Increments the running death test count -// for the current test. -DeathTest::DeathTest() { - TestInfo* const info = GetUnitTestImpl()->current_test_info(); - if (info == nullptr) { - DeathTestAbort("Cannot run a death test outside of a TEST or " - "TEST_F construct"); - } -} - -// Creates and returns a death test by dispatching to the current -// death test factory. -bool DeathTest::Create(const char* statement, - Matcher matcher, const char* file, - int line, DeathTest** test) { - return GetUnitTestImpl()->death_test_factory()->Create( - statement, std::move(matcher), file, line, test); -} - -const char* DeathTest::LastMessage() { - return last_death_test_message_.c_str(); -} - -void DeathTest::set_last_death_test_message(const std::string& message) { - last_death_test_message_ = message; -} - -std::string DeathTest::last_death_test_message_; - -// Provides cross platform implementation for some death functionality. -class DeathTestImpl : public DeathTest { - protected: - DeathTestImpl(const char* a_statement, Matcher matcher) - : statement_(a_statement), - matcher_(std::move(matcher)), - spawned_(false), - status_(-1), - outcome_(IN_PROGRESS), - read_fd_(-1), - write_fd_(-1) {} - - // read_fd_ is expected to be closed and cleared by a derived class. - ~DeathTestImpl() override { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); } - - void Abort(AbortReason reason) override; - bool Passed(bool status_ok) override; - - const char* statement() const { return statement_; } - bool spawned() const { return spawned_; } - void set_spawned(bool is_spawned) { spawned_ = is_spawned; } - int status() const { return status_; } - void set_status(int a_status) { status_ = a_status; } - DeathTestOutcome outcome() const { return outcome_; } - void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; } - int read_fd() const { return read_fd_; } - void set_read_fd(int fd) { read_fd_ = fd; } - int write_fd() const { return write_fd_; } - void set_write_fd(int fd) { write_fd_ = fd; } - - // Called in the parent process only. Reads the result code of the death - // test child process via a pipe, interprets it to set the outcome_ - // member, and closes read_fd_. Outputs diagnostics and terminates in - // case of unexpected codes. - void ReadAndInterpretStatusByte(); - - // Returns stderr output from the child process. - virtual std::string GetErrorLogs(); - - private: - // The textual content of the code this object is testing. This class - // doesn't own this string and should not attempt to delete it. - const char* const statement_; - // A matcher that's expected to match the stderr output by the child process. - Matcher matcher_; - // True if the death test child process has been successfully spawned. - bool spawned_; - // The exit status of the child process. - int status_; - // How the death test concluded. - DeathTestOutcome outcome_; - // Descriptor to the read end of the pipe to the child process. It is - // always -1 in the child process. The child keeps its write end of the - // pipe in write_fd_. - int read_fd_; - // Descriptor to the child's write end of the pipe to the parent process. - // It is always -1 in the parent process. The parent keeps its end of the - // pipe in read_fd_. - int write_fd_; -}; - -// Called in the parent process only. Reads the result code of the death -// test child process via a pipe, interprets it to set the outcome_ -// member, and closes read_fd_. Outputs diagnostics and terminates in -// case of unexpected codes. -void DeathTestImpl::ReadAndInterpretStatusByte() { - char flag; - int bytes_read; - - // The read() here blocks until data is available (signifying the - // failure of the death test) or until the pipe is closed (signifying - // its success), so it's okay to call this in the parent before - // the child process has exited. - do { - bytes_read = posix::Read(read_fd(), &flag, 1); - } while (bytes_read == -1 && errno == EINTR); - - if (bytes_read == 0) { - set_outcome(DIED); - } else if (bytes_read == 1) { - switch (flag) { - case kDeathTestReturned: - set_outcome(RETURNED); - break; - case kDeathTestThrew: - set_outcome(THREW); - break; - case kDeathTestLived: - set_outcome(LIVED); - break; - case kDeathTestInternalError: - FailFromInternalError(read_fd()); // Does not return. - break; - default: - GTEST_LOG_(FATAL) << "Death test child process reported " - << "unexpected status byte (" - << static_cast(flag) << ")"; - } - } else { - GTEST_LOG_(FATAL) << "Read from death test child process failed: " - << GetLastErrnoDescription(); - } - GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd())); - set_read_fd(-1); -} - -std::string DeathTestImpl::GetErrorLogs() { - return GetCapturedStderr(); -} - -// Signals that the death test code which should have exited, didn't. -// Should be called only in a death test child process. -// Writes a status byte to the child's status file descriptor, then -// calls _exit(1). -void DeathTestImpl::Abort(AbortReason reason) { - // The parent process considers the death test to be a failure if - // it finds any data in our pipe. So, here we write a single flag byte - // to the pipe, then exit. - const char status_ch = - reason == TEST_DID_NOT_DIE ? kDeathTestLived : - reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned; - - GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1)); - // We are leaking the descriptor here because on some platforms (i.e., - // when built as Windows DLL), destructors of global objects will still - // run after calling _exit(). On such systems, write_fd_ will be - // indirectly closed from the destructor of UnitTestImpl, causing double - // close if it is also closed here. On debug configurations, double close - // may assert. As there are no in-process buffers to flush here, we are - // relying on the OS to close the descriptor after the process terminates - // when the destructors are not run. - _exit(1); // Exits w/o any normal exit hooks (we were supposed to crash) -} - -// Returns an indented copy of stderr output for a death test. -// This makes distinguishing death test output lines from regular log lines -// much easier. -static ::std::string FormatDeathTestOutput(const ::std::string& output) { - ::std::string ret; - for (size_t at = 0; ; ) { - const size_t line_end = output.find('\n', at); - ret += "[ DEATH ] "; - if (line_end == ::std::string::npos) { - ret += output.substr(at); - break; - } - ret += output.substr(at, line_end + 1 - at); - at = line_end + 1; - } - return ret; -} - -// Assesses the success or failure of a death test, using both private -// members which have previously been set, and one argument: -// -// Private data members: -// outcome: An enumeration describing how the death test -// concluded: DIED, LIVED, THREW, or RETURNED. The death test -// fails in the latter three cases. -// status: The exit status of the child process. On *nix, it is in the -// in the format specified by wait(2). On Windows, this is the -// value supplied to the ExitProcess() API or a numeric code -// of the exception that terminated the program. -// matcher_: A matcher that's expected to match the stderr output by the child -// process. -// -// Argument: -// status_ok: true if exit_status is acceptable in the context of -// this particular death test, which fails if it is false -// -// Returns true if and only if all of the above conditions are met. Otherwise, -// the first failing condition, in the order given above, is the one that is -// reported. Also sets the last death test message string. -bool DeathTestImpl::Passed(bool status_ok) { - if (!spawned()) - return false; - - const std::string error_message = GetErrorLogs(); - - bool success = false; - Message buffer; - - buffer << "Death test: " << statement() << "\n"; - switch (outcome()) { - case LIVED: - buffer << " Result: failed to die.\n" - << " Error msg:\n" << FormatDeathTestOutput(error_message); - break; - case THREW: - buffer << " Result: threw an exception.\n" - << " Error msg:\n" << FormatDeathTestOutput(error_message); - break; - case RETURNED: - buffer << " Result: illegal return in test statement.\n" - << " Error msg:\n" << FormatDeathTestOutput(error_message); - break; - case DIED: - if (status_ok) { - if (matcher_.Matches(error_message)) { - success = true; - } else { - std::ostringstream stream; - matcher_.DescribeTo(&stream); - buffer << " Result: died but not with expected error.\n" - << " Expected: " << stream.str() << "\n" - << "Actual msg:\n" - << FormatDeathTestOutput(error_message); - } - } else { - buffer << " Result: died but not with expected exit code:\n" - << " " << ExitSummary(status()) << "\n" - << "Actual msg:\n" << FormatDeathTestOutput(error_message); - } - break; - case IN_PROGRESS: - default: - GTEST_LOG_(FATAL) - << "DeathTest::Passed somehow called before conclusion of test"; - } - - DeathTest::set_last_death_test_message(buffer.GetString()); - return success; -} - -# if GTEST_OS_WINDOWS -// WindowsDeathTest implements death tests on Windows. Due to the -// specifics of starting new processes on Windows, death tests there are -// always threadsafe, and Google Test considers the -// --gtest_death_test_style=fast setting to be equivalent to -// --gtest_death_test_style=threadsafe there. -// -// A few implementation notes: Like the Linux version, the Windows -// implementation uses pipes for child-to-parent communication. But due to -// the specifics of pipes on Windows, some extra steps are required: -// -// 1. The parent creates a communication pipe and stores handles to both -// ends of it. -// 2. The parent starts the child and provides it with the information -// necessary to acquire the handle to the write end of the pipe. -// 3. The child acquires the write end of the pipe and signals the parent -// using a Windows event. -// 4. Now the parent can release the write end of the pipe on its side. If -// this is done before step 3, the object's reference count goes down to -// 0 and it is destroyed, preventing the child from acquiring it. The -// parent now has to release it, or read operations on the read end of -// the pipe will not return when the child terminates. -// 5. The parent reads child's output through the pipe (outcome code and -// any possible error messages) from the pipe, and its stderr and then -// determines whether to fail the test. -// -// Note: to distinguish Win32 API calls from the local method and function -// calls, the former are explicitly resolved in the global namespace. -// -class WindowsDeathTest : public DeathTestImpl { - public: - WindowsDeathTest(const char* a_statement, Matcher matcher, - const char* file, int line) - : DeathTestImpl(a_statement, std::move(matcher)), - file_(file), - line_(line) {} - - // All of these virtual functions are inherited from DeathTest. - virtual int Wait(); - virtual TestRole AssumeRole(); - - private: - // The name of the file in which the death test is located. - const char* const file_; - // The line number on which the death test is located. - const int line_; - // Handle to the write end of the pipe to the child process. - AutoHandle write_handle_; - // Child process handle. - AutoHandle child_handle_; - // Event the child process uses to signal the parent that it has - // acquired the handle to the write end of the pipe. After seeing this - // event the parent can release its own handles to make sure its - // ReadFile() calls return when the child terminates. - AutoHandle event_handle_; -}; - -// Waits for the child in a death test to exit, returning its exit -// status, or 0 if no child process exists. As a side effect, sets the -// outcome data member. -int WindowsDeathTest::Wait() { - if (!spawned()) - return 0; - - // Wait until the child either signals that it has acquired the write end - // of the pipe or it dies. - const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() }; - switch (::WaitForMultipleObjects(2, - wait_handles, - FALSE, // Waits for any of the handles. - INFINITE)) { - case WAIT_OBJECT_0: - case WAIT_OBJECT_0 + 1: - break; - default: - GTEST_DEATH_TEST_CHECK_(false); // Should not get here. - } - - // The child has acquired the write end of the pipe or exited. - // We release the handle on our side and continue. - write_handle_.Reset(); - event_handle_.Reset(); - - ReadAndInterpretStatusByte(); - - // Waits for the child process to exit if it haven't already. This - // returns immediately if the child has already exited, regardless of - // whether previous calls to WaitForMultipleObjects synchronized on this - // handle or not. - GTEST_DEATH_TEST_CHECK_( - WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(), - INFINITE)); - DWORD status_code; - GTEST_DEATH_TEST_CHECK_( - ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE); - child_handle_.Reset(); - set_status(static_cast(status_code)); - return status(); -} - -// The AssumeRole process for a Windows death test. It creates a child -// process with the same executable as the current process to run the -// death test. The child process is given the --gtest_filter and -// --gtest_internal_run_death_test flags such that it knows to run the -// current death test only. -DeathTest::TestRole WindowsDeathTest::AssumeRole() { - const UnitTestImpl* const impl = GetUnitTestImpl(); - const InternalRunDeathTestFlag* const flag = - impl->internal_run_death_test_flag(); - const TestInfo* const info = impl->current_test_info(); - const int death_test_index = info->result()->death_test_count(); - - if (flag != nullptr) { - // ParseInternalRunDeathTestFlag() has performed all the necessary - // processing. - set_write_fd(flag->write_fd()); - return EXECUTE_TEST; - } - - // WindowsDeathTest uses an anonymous pipe to communicate results of - // a death test. - SECURITY_ATTRIBUTES handles_are_inheritable = {sizeof(SECURITY_ATTRIBUTES), - nullptr, TRUE}; - HANDLE read_handle, write_handle; - GTEST_DEATH_TEST_CHECK_( - ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable, - 0) // Default buffer size. - != FALSE); - set_read_fd(::_open_osfhandle(reinterpret_cast(read_handle), - O_RDONLY)); - write_handle_.Reset(write_handle); - event_handle_.Reset(::CreateEvent( - &handles_are_inheritable, - TRUE, // The event will automatically reset to non-signaled state. - FALSE, // The initial state is non-signalled. - nullptr)); // The even is unnamed. - GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != nullptr); - const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + - kFilterFlag + "=" + info->test_suite_name() + - "." + info->name(); - const std::string internal_flag = - std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + - "=" + file_ + "|" + StreamableToString(line_) + "|" + - StreamableToString(death_test_index) + "|" + - StreamableToString(static_cast(::GetCurrentProcessId())) + - // size_t has the same width as pointers on both 32-bit and 64-bit - // Windows platforms. - // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx. - "|" + StreamableToString(reinterpret_cast(write_handle)) + - "|" + StreamableToString(reinterpret_cast(event_handle_.Get())); - - char executable_path[_MAX_PATH + 1]; // NOLINT - GTEST_DEATH_TEST_CHECK_(_MAX_PATH + 1 != ::GetModuleFileNameA(nullptr, - executable_path, - _MAX_PATH)); - - std::string command_line = - std::string(::GetCommandLineA()) + " " + filter_flag + " \"" + - internal_flag + "\""; - - DeathTest::set_last_death_test_message(""); - - CaptureStderr(); - // Flush the log buffers since the log streams are shared with the child. - FlushInfoLog(); - - // The child process will share the standard handles with the parent. - STARTUPINFOA startup_info; - memset(&startup_info, 0, sizeof(STARTUPINFO)); - startup_info.dwFlags = STARTF_USESTDHANDLES; - startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE); - startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); - startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE); - - PROCESS_INFORMATION process_info; - GTEST_DEATH_TEST_CHECK_( - ::CreateProcessA( - executable_path, const_cast(command_line.c_str()), - nullptr, // Retuned process handle is not inheritable. - nullptr, // Retuned thread handle is not inheritable. - TRUE, // Child inherits all inheritable handles (for write_handle_). - 0x0, // Default creation flags. - nullptr, // Inherit the parent's environment. - UnitTest::GetInstance()->original_working_dir(), &startup_info, - &process_info) != FALSE); - child_handle_.Reset(process_info.hProcess); - ::CloseHandle(process_info.hThread); - set_spawned(true); - return OVERSEE_TEST; -} - -# elif GTEST_OS_FUCHSIA - -class FuchsiaDeathTest : public DeathTestImpl { - public: - FuchsiaDeathTest(const char* a_statement, Matcher matcher, - const char* file, int line) - : DeathTestImpl(a_statement, std::move(matcher)), - file_(file), - line_(line) {} - - // All of these virtual functions are inherited from DeathTest. - int Wait() override; - TestRole AssumeRole() override; - std::string GetErrorLogs() override; - - private: - // The name of the file in which the death test is located. - const char* const file_; - // The line number on which the death test is located. - const int line_; - // The stderr data captured by the child process. - std::string captured_stderr_; - - zx::process child_process_; - zx::channel exception_channel_; - zx::socket stderr_socket_; -}; - -// Utility class for accumulating command-line arguments. -class Arguments { - public: - Arguments() { args_.push_back(nullptr); } - - ~Arguments() { - for (std::vector::iterator i = args_.begin(); i != args_.end(); - ++i) { - free(*i); - } - } - void AddArgument(const char* argument) { - args_.insert(args_.end() - 1, posix::StrDup(argument)); - } - - template - void AddArguments(const ::std::vector& arguments) { - for (typename ::std::vector::const_iterator i = arguments.begin(); - i != arguments.end(); - ++i) { - args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); - } - } - char* const* Argv() { - return &args_[0]; - } - - int size() { - return static_cast(args_.size()) - 1; - } - - private: - std::vector args_; -}; - -// Waits for the child in a death test to exit, returning its exit -// status, or 0 if no child process exists. As a side effect, sets the -// outcome data member. -int FuchsiaDeathTest::Wait() { - const int kProcessKey = 0; - const int kSocketKey = 1; - const int kExceptionKey = 2; - - if (!spawned()) - return 0; - - // Create a port to wait for socket/task/exception events. - zx_status_t status_zx; - zx::port port; - status_zx = zx::port::create(0, &port); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - - // Register to wait for the child process to terminate. - status_zx = child_process_.wait_async( - port, kProcessKey, ZX_PROCESS_TERMINATED, 0); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - - // Register to wait for the socket to be readable or closed. - status_zx = stderr_socket_.wait_async( - port, kSocketKey, ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED, 0); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - - // Register to wait for an exception. - status_zx = exception_channel_.wait_async( - port, kExceptionKey, ZX_CHANNEL_READABLE, 0); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - - bool process_terminated = false; - bool socket_closed = false; - do { - zx_port_packet_t packet = {}; - status_zx = port.wait(zx::time::infinite(), &packet); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - - if (packet.key == kExceptionKey) { - // Process encountered an exception. Kill it directly rather than - // letting other handlers process the event. We will get a kProcessKey - // event when the process actually terminates. - status_zx = child_process_.kill(); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - } else if (packet.key == kProcessKey) { - // Process terminated. - GTEST_DEATH_TEST_CHECK_(ZX_PKT_IS_SIGNAL_ONE(packet.type)); - GTEST_DEATH_TEST_CHECK_(packet.signal.observed & ZX_PROCESS_TERMINATED); - process_terminated = true; - } else if (packet.key == kSocketKey) { - GTEST_DEATH_TEST_CHECK_(ZX_PKT_IS_SIGNAL_ONE(packet.type)); - if (packet.signal.observed & ZX_SOCKET_READABLE) { - // Read data from the socket. - constexpr size_t kBufferSize = 1024; - do { - size_t old_length = captured_stderr_.length(); - size_t bytes_read = 0; - captured_stderr_.resize(old_length + kBufferSize); - status_zx = stderr_socket_.read( - 0, &captured_stderr_.front() + old_length, kBufferSize, - &bytes_read); - captured_stderr_.resize(old_length + bytes_read); - } while (status_zx == ZX_OK); - if (status_zx == ZX_ERR_PEER_CLOSED) { - socket_closed = true; - } else { - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_ERR_SHOULD_WAIT); - status_zx = stderr_socket_.wait_async( - port, kSocketKey, ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED, 0); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - } - } else { - GTEST_DEATH_TEST_CHECK_(packet.signal.observed & ZX_SOCKET_PEER_CLOSED); - socket_closed = true; - } - } - } while (!process_terminated && !socket_closed); - - ReadAndInterpretStatusByte(); - - zx_info_process_t buffer; - status_zx = child_process_.get_info(ZX_INFO_PROCESS, &buffer, sizeof(buffer), - nullptr, nullptr); - GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); - - GTEST_DEATH_TEST_CHECK_(buffer.flags & ZX_INFO_PROCESS_FLAG_EXITED); - set_status(static_cast(buffer.return_code)); - return status(); -} - -// The AssumeRole process for a Fuchsia death test. It creates a child -// process with the same executable as the current process to run the -// death test. The child process is given the --gtest_filter and -// --gtest_internal_run_death_test flags such that it knows to run the -// current death test only. -DeathTest::TestRole FuchsiaDeathTest::AssumeRole() { - const UnitTestImpl* const impl = GetUnitTestImpl(); - const InternalRunDeathTestFlag* const flag = - impl->internal_run_death_test_flag(); - const TestInfo* const info = impl->current_test_info(); - const int death_test_index = info->result()->death_test_count(); - - if (flag != nullptr) { - // ParseInternalRunDeathTestFlag() has performed all the necessary - // processing. - set_write_fd(kFuchsiaReadPipeFd); - return EXECUTE_TEST; - } - - // Flush the log buffers since the log streams are shared with the child. - FlushInfoLog(); - - // Build the child process command line. - const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + - kFilterFlag + "=" + info->test_suite_name() + - "." + info->name(); - const std::string internal_flag = - std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + "=" - + file_ + "|" - + StreamableToString(line_) + "|" - + StreamableToString(death_test_index); - Arguments args; - args.AddArguments(GetInjectableArgvs()); - args.AddArgument(filter_flag.c_str()); - args.AddArgument(internal_flag.c_str()); - - // Build the pipe for communication with the child. - zx_status_t status; - zx_handle_t child_pipe_handle; - int child_pipe_fd; - status = fdio_pipe_half(&child_pipe_fd, &child_pipe_handle); - GTEST_DEATH_TEST_CHECK_(status == ZX_OK); - set_read_fd(child_pipe_fd); - - // Set the pipe handle for the child. - fdio_spawn_action_t spawn_actions[2] = {}; - fdio_spawn_action_t* add_handle_action = &spawn_actions[0]; - add_handle_action->action = FDIO_SPAWN_ACTION_ADD_HANDLE; - add_handle_action->h.id = PA_HND(PA_FD, kFuchsiaReadPipeFd); - add_handle_action->h.handle = child_pipe_handle; - - // Create a socket pair will be used to receive the child process' stderr. - zx::socket stderr_producer_socket; - status = - zx::socket::create(0, &stderr_producer_socket, &stderr_socket_); - GTEST_DEATH_TEST_CHECK_(status >= 0); - int stderr_producer_fd = -1; - status = - fdio_fd_create(stderr_producer_socket.release(), &stderr_producer_fd); - GTEST_DEATH_TEST_CHECK_(status >= 0); - - // Make the stderr socket nonblocking. - GTEST_DEATH_TEST_CHECK_(fcntl(stderr_producer_fd, F_SETFL, 0) == 0); - - fdio_spawn_action_t* add_stderr_action = &spawn_actions[1]; - add_stderr_action->action = FDIO_SPAWN_ACTION_CLONE_FD; - add_stderr_action->fd.local_fd = stderr_producer_fd; - add_stderr_action->fd.target_fd = STDERR_FILENO; - - // Create a child job. - zx_handle_t child_job = ZX_HANDLE_INVALID; - status = zx_job_create(zx_job_default(), 0, & child_job); - GTEST_DEATH_TEST_CHECK_(status == ZX_OK); - zx_policy_basic_t policy; - policy.condition = ZX_POL_NEW_ANY; - policy.policy = ZX_POL_ACTION_ALLOW; - status = zx_job_set_policy( - child_job, ZX_JOB_POL_RELATIVE, ZX_JOB_POL_BASIC, &policy, 1); - GTEST_DEATH_TEST_CHECK_(status == ZX_OK); - - // Create an exception channel attached to the |child_job|, to allow - // us to suppress the system default exception handler from firing. - status = - zx_task_create_exception_channel( - child_job, 0, exception_channel_.reset_and_get_address()); - GTEST_DEATH_TEST_CHECK_(status == ZX_OK); - - // Spawn the child process. - status = fdio_spawn_etc( - child_job, FDIO_SPAWN_CLONE_ALL, args.Argv()[0], args.Argv(), nullptr, - 2, spawn_actions, child_process_.reset_and_get_address(), nullptr); - GTEST_DEATH_TEST_CHECK_(status == ZX_OK); - - set_spawned(true); - return OVERSEE_TEST; -} - -std::string FuchsiaDeathTest::GetErrorLogs() { - return captured_stderr_; -} - -#else // We are neither on Windows, nor on Fuchsia. - -// ForkingDeathTest provides implementations for most of the abstract -// methods of the DeathTest interface. Only the AssumeRole method is -// left undefined. -class ForkingDeathTest : public DeathTestImpl { - public: - ForkingDeathTest(const char* statement, Matcher matcher); - - // All of these virtual functions are inherited from DeathTest. - int Wait() override; - - protected: - void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; } - - private: - // PID of child process during death test; 0 in the child process itself. - pid_t child_pid_; -}; - -// Constructs a ForkingDeathTest. -ForkingDeathTest::ForkingDeathTest(const char* a_statement, - Matcher matcher) - : DeathTestImpl(a_statement, std::move(matcher)), child_pid_(-1) {} - -// Waits for the child in a death test to exit, returning its exit -// status, or 0 if no child process exists. As a side effect, sets the -// outcome data member. -int ForkingDeathTest::Wait() { - if (!spawned()) - return 0; - - ReadAndInterpretStatusByte(); - - int status_value; - GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0)); - set_status(status_value); - return status_value; -} - -// A concrete death test class that forks, then immediately runs the test -// in the child process. -class NoExecDeathTest : public ForkingDeathTest { - public: - NoExecDeathTest(const char* a_statement, Matcher matcher) - : ForkingDeathTest(a_statement, std::move(matcher)) {} - TestRole AssumeRole() override; -}; - -// The AssumeRole process for a fork-and-run death test. It implements a -// straightforward fork, with a simple pipe to transmit the status byte. -DeathTest::TestRole NoExecDeathTest::AssumeRole() { - const size_t thread_count = GetThreadCount(); - if (thread_count != 1) { - GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count); - } - - int pipe_fd[2]; - GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); - - DeathTest::set_last_death_test_message(""); - CaptureStderr(); - // When we fork the process below, the log file buffers are copied, but the - // file descriptors are shared. We flush all log files here so that closing - // the file descriptors in the child process doesn't throw off the - // synchronization between descriptors and buffers in the parent process. - // This is as close to the fork as possible to avoid a race condition in case - // there are multiple threads running before the death test, and another - // thread writes to the log file. - FlushInfoLog(); - - const pid_t child_pid = fork(); - GTEST_DEATH_TEST_CHECK_(child_pid != -1); - set_child_pid(child_pid); - if (child_pid == 0) { - GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0])); - set_write_fd(pipe_fd[1]); - // Redirects all logging to stderr in the child process to prevent - // concurrent writes to the log files. We capture stderr in the parent - // process and append the child process' output to a log. - LogToStderr(); - // Event forwarding to the listeners of event listener API mush be shut - // down in death test subprocesses. - GetUnitTestImpl()->listeners()->SuppressEventForwarding(); - g_in_fast_death_test_child = true; - return EXECUTE_TEST; - } else { - GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); - set_read_fd(pipe_fd[0]); - set_spawned(true); - return OVERSEE_TEST; - } -} - -// A concrete death test class that forks and re-executes the main -// program from the beginning, with command-line flags set that cause -// only this specific death test to be run. -class ExecDeathTest : public ForkingDeathTest { - public: - ExecDeathTest(const char* a_statement, Matcher matcher, - const char* file, int line) - : ForkingDeathTest(a_statement, std::move(matcher)), - file_(file), - line_(line) {} - TestRole AssumeRole() override; - - private: - static ::std::vector GetArgvsForDeathTestChildProcess() { - ::std::vector args = GetInjectableArgvs(); -# if defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_) - ::std::vector extra_args = - GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_(); - args.insert(args.end(), extra_args.begin(), extra_args.end()); -# endif // defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_) - return args; - } - // The name of the file in which the death test is located. - const char* const file_; - // The line number on which the death test is located. - const int line_; -}; - -// Utility class for accumulating command-line arguments. -class Arguments { - public: - Arguments() { args_.push_back(nullptr); } - - ~Arguments() { - for (std::vector::iterator i = args_.begin(); i != args_.end(); - ++i) { - free(*i); - } - } - void AddArgument(const char* argument) { - args_.insert(args_.end() - 1, posix::StrDup(argument)); - } - - template - void AddArguments(const ::std::vector& arguments) { - for (typename ::std::vector::const_iterator i = arguments.begin(); - i != arguments.end(); - ++i) { - args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); - } - } - char* const* Argv() { - return &args_[0]; - } - - private: - std::vector args_; -}; - -// A struct that encompasses the arguments to the child process of a -// threadsafe-style death test process. -struct ExecDeathTestArgs { - char* const* argv; // Command-line arguments for the child's call to exec - int close_fd; // File descriptor to close; the read end of a pipe -}; - -# if GTEST_OS_QNX -extern "C" char** environ; -# else // GTEST_OS_QNX -// The main function for a threadsafe-style death test child process. -// This function is called in a clone()-ed process and thus must avoid -// any potentially unsafe operations like malloc or libc functions. -static int ExecDeathTestChildMain(void* child_arg) { - ExecDeathTestArgs* const args = static_cast(child_arg); - GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd)); - - // We need to execute the test program in the same environment where - // it was originally invoked. Therefore we change to the original - // working directory first. - const char* const original_dir = - UnitTest::GetInstance()->original_working_dir(); - // We can safely call chdir() as it's a direct system call. - if (chdir(original_dir) != 0) { - DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " + - GetLastErrnoDescription()); - return EXIT_FAILURE; - } - - // We can safely call execv() as it's almost a direct system call. We - // cannot use execvp() as it's a libc function and thus potentially - // unsafe. Since execv() doesn't search the PATH, the user must - // invoke the test program via a valid path that contains at least - // one path separator. - execv(args->argv[0], args->argv); - DeathTestAbort(std::string("execv(") + args->argv[0] + ", ...) in " + - original_dir + " failed: " + - GetLastErrnoDescription()); - return EXIT_FAILURE; -} -# endif // GTEST_OS_QNX - -# if GTEST_HAS_CLONE -// Two utility routines that together determine the direction the stack -// grows. -// This could be accomplished more elegantly by a single recursive -// function, but we want to guard against the unlikely possibility of -// a smart compiler optimizing the recursion away. -// -// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining -// StackLowerThanAddress into StackGrowsDown, which then doesn't give -// correct answer. -static void StackLowerThanAddress(const void* ptr, - bool* result) GTEST_NO_INLINE_; -// Make sure sanitizers do not tamper with the stack here. -// Ideally, we want to use `__builtin_frame_address` instead of a local variable -// address with sanitizer disabled, but it does not work when the -// compiler optimizes the stack frame out, which happens on PowerPC targets. -// HWAddressSanitizer add a random tag to the MSB of the local variable address, -// making comparison result unpredictable. -GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -static void StackLowerThanAddress(const void* ptr, bool* result) { - int dummy = 0; - *result = std::less()(&dummy, ptr); -} - -// Make sure AddressSanitizer does not tamper with the stack here. -GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -static bool StackGrowsDown() { - int dummy = 0; - bool result; - StackLowerThanAddress(&dummy, &result); - return result; -} -# endif // GTEST_HAS_CLONE - -// Spawns a child process with the same executable as the current process in -// a thread-safe manner and instructs it to run the death test. The -// implementation uses fork(2) + exec. On systems where clone(2) is -// available, it is used instead, being slightly more thread-safe. On QNX, -// fork supports only single-threaded environments, so this function uses -// spawn(2) there instead. The function dies with an error message if -// anything goes wrong. -static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) { - ExecDeathTestArgs args = { argv, close_fd }; - pid_t child_pid = -1; - -# if GTEST_OS_QNX - // Obtains the current directory and sets it to be closed in the child - // process. - const int cwd_fd = open(".", O_RDONLY); - GTEST_DEATH_TEST_CHECK_(cwd_fd != -1); - GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC)); - // We need to execute the test program in the same environment where - // it was originally invoked. Therefore we change to the original - // working directory first. - const char* const original_dir = - UnitTest::GetInstance()->original_working_dir(); - // We can safely call chdir() as it's a direct system call. - if (chdir(original_dir) != 0) { - DeathTestAbort(std::string("chdir(\"") + original_dir + "\") failed: " + - GetLastErrnoDescription()); - return EXIT_FAILURE; - } - - int fd_flags; - // Set close_fd to be closed after spawn. - GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD)); - GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(close_fd, F_SETFD, - fd_flags | FD_CLOEXEC)); - struct inheritance inherit = {0}; - // spawn is a system call. - child_pid = spawn(args.argv[0], 0, nullptr, &inherit, args.argv, environ); - // Restores the current working directory. - GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1); - GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd)); - -# else // GTEST_OS_QNX -# if GTEST_OS_LINUX - // When a SIGPROF signal is received while fork() or clone() are executing, - // the process may hang. To avoid this, we ignore SIGPROF here and re-enable - // it after the call to fork()/clone() is complete. - struct sigaction saved_sigprof_action; - struct sigaction ignore_sigprof_action; - memset(&ignore_sigprof_action, 0, sizeof(ignore_sigprof_action)); - sigemptyset(&ignore_sigprof_action.sa_mask); - ignore_sigprof_action.sa_handler = SIG_IGN; - GTEST_DEATH_TEST_CHECK_SYSCALL_(sigaction( - SIGPROF, &ignore_sigprof_action, &saved_sigprof_action)); -# endif // GTEST_OS_LINUX - -# if GTEST_HAS_CLONE - const bool use_fork = GTEST_FLAG(death_test_use_fork); - - if (!use_fork) { - static const bool stack_grows_down = StackGrowsDown(); - const auto stack_size = static_cast(getpagesize() * 2); - // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead. - void* const stack = mmap(nullptr, stack_size, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); - GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED); - - // Maximum stack alignment in bytes: For a downward-growing stack, this - // amount is subtracted from size of the stack space to get an address - // that is within the stack space and is aligned on all systems we care - // about. As far as I know there is no ABI with stack alignment greater - // than 64. We assume stack and stack_size already have alignment of - // kMaxStackAlignment. - const size_t kMaxStackAlignment = 64; - void* const stack_top = - static_cast(stack) + - (stack_grows_down ? stack_size - kMaxStackAlignment : 0); - GTEST_DEATH_TEST_CHECK_( - static_cast(stack_size) > kMaxStackAlignment && - reinterpret_cast(stack_top) % kMaxStackAlignment == 0); - - child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args); - - GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1); - } -# else - const bool use_fork = true; -# endif // GTEST_HAS_CLONE - - if (use_fork && (child_pid = fork()) == 0) { - ExecDeathTestChildMain(&args); - _exit(0); - } -# endif // GTEST_OS_QNX -# if GTEST_OS_LINUX - GTEST_DEATH_TEST_CHECK_SYSCALL_( - sigaction(SIGPROF, &saved_sigprof_action, nullptr)); -# endif // GTEST_OS_LINUX - - GTEST_DEATH_TEST_CHECK_(child_pid != -1); - return child_pid; -} - -// The AssumeRole process for a fork-and-exec death test. It re-executes the -// main program from the beginning, setting the --gtest_filter -// and --gtest_internal_run_death_test flags to cause only the current -// death test to be re-run. -DeathTest::TestRole ExecDeathTest::AssumeRole() { - const UnitTestImpl* const impl = GetUnitTestImpl(); - const InternalRunDeathTestFlag* const flag = - impl->internal_run_death_test_flag(); - const TestInfo* const info = impl->current_test_info(); - const int death_test_index = info->result()->death_test_count(); - - if (flag != nullptr) { - set_write_fd(flag->write_fd()); - return EXECUTE_TEST; - } - - int pipe_fd[2]; - GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); - // Clear the close-on-exec flag on the write end of the pipe, lest - // it be closed when the child process does an exec: - GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1); - - const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + - kFilterFlag + "=" + info->test_suite_name() + - "." + info->name(); - const std::string internal_flag = - std::string("--") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + "=" - + file_ + "|" + StreamableToString(line_) + "|" - + StreamableToString(death_test_index) + "|" - + StreamableToString(pipe_fd[1]); - Arguments args; - args.AddArguments(GetArgvsForDeathTestChildProcess()); - args.AddArgument(filter_flag.c_str()); - args.AddArgument(internal_flag.c_str()); - - DeathTest::set_last_death_test_message(""); - - CaptureStderr(); - // See the comment in NoExecDeathTest::AssumeRole for why the next line - // is necessary. - FlushInfoLog(); - - const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]); - GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); - set_child_pid(child_pid); - set_read_fd(pipe_fd[0]); - set_spawned(true); - return OVERSEE_TEST; -} - -# endif // !GTEST_OS_WINDOWS - -// Creates a concrete DeathTest-derived class that depends on the -// --gtest_death_test_style flag, and sets the pointer pointed to -// by the "test" argument to its address. If the test should be -// skipped, sets that pointer to NULL. Returns true, unless the -// flag is set to an invalid value. -bool DefaultDeathTestFactory::Create(const char* statement, - Matcher matcher, - const char* file, int line, - DeathTest** test) { - UnitTestImpl* const impl = GetUnitTestImpl(); - const InternalRunDeathTestFlag* const flag = - impl->internal_run_death_test_flag(); - const int death_test_index = impl->current_test_info() - ->increment_death_test_count(); - - if (flag != nullptr) { - if (death_test_index > flag->index()) { - DeathTest::set_last_death_test_message( - "Death test count (" + StreamableToString(death_test_index) - + ") somehow exceeded expected maximum (" - + StreamableToString(flag->index()) + ")"); - return false; - } - - if (!(flag->file() == file && flag->line() == line && - flag->index() == death_test_index)) { - *test = nullptr; - return true; - } - } - -# if GTEST_OS_WINDOWS - - if (GTEST_FLAG(death_test_style) == "threadsafe" || - GTEST_FLAG(death_test_style) == "fast") { - *test = new WindowsDeathTest(statement, std::move(matcher), file, line); - } - -# elif GTEST_OS_FUCHSIA - - if (GTEST_FLAG(death_test_style) == "threadsafe" || - GTEST_FLAG(death_test_style) == "fast") { - *test = new FuchsiaDeathTest(statement, std::move(matcher), file, line); - } - -# else - - if (GTEST_FLAG(death_test_style) == "threadsafe") { - *test = new ExecDeathTest(statement, std::move(matcher), file, line); - } else if (GTEST_FLAG(death_test_style) == "fast") { - *test = new NoExecDeathTest(statement, std::move(matcher)); - } - -# endif // GTEST_OS_WINDOWS - - else { // NOLINT - this is more readable than unbalanced brackets inside #if. - DeathTest::set_last_death_test_message( - "Unknown death test style \"" + GTEST_FLAG(death_test_style) - + "\" encountered"); - return false; - } - - return true; -} - -# if GTEST_OS_WINDOWS -// Recreates the pipe and event handles from the provided parameters, -// signals the event, and returns a file descriptor wrapped around the pipe -// handle. This function is called in the child process only. -static int GetStatusFileDescriptor(unsigned int parent_process_id, - size_t write_handle_as_size_t, - size_t event_handle_as_size_t) { - AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE, - FALSE, // Non-inheritable. - parent_process_id)); - if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) { - DeathTestAbort("Unable to open parent process " + - StreamableToString(parent_process_id)); - } - - GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t)); - - const HANDLE write_handle = - reinterpret_cast(write_handle_as_size_t); - HANDLE dup_write_handle; - - // The newly initialized handle is accessible only in the parent - // process. To obtain one accessible within the child, we need to use - // DuplicateHandle. - if (!::DuplicateHandle(parent_process_handle.Get(), write_handle, - ::GetCurrentProcess(), &dup_write_handle, - 0x0, // Requested privileges ignored since - // DUPLICATE_SAME_ACCESS is used. - FALSE, // Request non-inheritable handler. - DUPLICATE_SAME_ACCESS)) { - DeathTestAbort("Unable to duplicate the pipe handle " + - StreamableToString(write_handle_as_size_t) + - " from the parent process " + - StreamableToString(parent_process_id)); - } - - const HANDLE event_handle = reinterpret_cast(event_handle_as_size_t); - HANDLE dup_event_handle; - - if (!::DuplicateHandle(parent_process_handle.Get(), event_handle, - ::GetCurrentProcess(), &dup_event_handle, - 0x0, - FALSE, - DUPLICATE_SAME_ACCESS)) { - DeathTestAbort("Unable to duplicate the event handle " + - StreamableToString(event_handle_as_size_t) + - " from the parent process " + - StreamableToString(parent_process_id)); - } - - const int write_fd = - ::_open_osfhandle(reinterpret_cast(dup_write_handle), O_APPEND); - if (write_fd == -1) { - DeathTestAbort("Unable to convert pipe handle " + - StreamableToString(write_handle_as_size_t) + - " to a file descriptor"); - } - - // Signals the parent that the write end of the pipe has been acquired - // so the parent can release its own write end. - ::SetEvent(dup_event_handle); - - return write_fd; -} -# endif // GTEST_OS_WINDOWS - -// Returns a newly created InternalRunDeathTestFlag object with fields -// initialized from the GTEST_FLAG(internal_run_death_test) flag if -// the flag is specified; otherwise returns NULL. -InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() { - if (GTEST_FLAG(internal_run_death_test) == "") return nullptr; - - // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we - // can use it here. - int line = -1; - int index = -1; - ::std::vector< ::std::string> fields; - SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields); - int write_fd = -1; - -# if GTEST_OS_WINDOWS - - unsigned int parent_process_id = 0; - size_t write_handle_as_size_t = 0; - size_t event_handle_as_size_t = 0; - - if (fields.size() != 6 - || !ParseNaturalNumber(fields[1], &line) - || !ParseNaturalNumber(fields[2], &index) - || !ParseNaturalNumber(fields[3], &parent_process_id) - || !ParseNaturalNumber(fields[4], &write_handle_as_size_t) - || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) { - DeathTestAbort("Bad --gtest_internal_run_death_test flag: " + - GTEST_FLAG(internal_run_death_test)); - } - write_fd = GetStatusFileDescriptor(parent_process_id, - write_handle_as_size_t, - event_handle_as_size_t); - -# elif GTEST_OS_FUCHSIA - - if (fields.size() != 3 - || !ParseNaturalNumber(fields[1], &line) - || !ParseNaturalNumber(fields[2], &index)) { - DeathTestAbort("Bad --gtest_internal_run_death_test flag: " - + GTEST_FLAG(internal_run_death_test)); - } - -# else - - if (fields.size() != 4 - || !ParseNaturalNumber(fields[1], &line) - || !ParseNaturalNumber(fields[2], &index) - || !ParseNaturalNumber(fields[3], &write_fd)) { - DeathTestAbort("Bad --gtest_internal_run_death_test flag: " - + GTEST_FLAG(internal_run_death_test)); - } - -# endif // GTEST_OS_WINDOWS - - return new InternalRunDeathTestFlag(fields[0], line, index, write_fd); -} - -} // namespace internal - -#endif // GTEST_HAS_DEATH_TEST - -} // namespace testing -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -#include - -#if GTEST_OS_WINDOWS_MOBILE -# include -#elif GTEST_OS_WINDOWS -# include -# include -#else -# include -# include // Some Linux distributions define PATH_MAX here. -#endif // GTEST_OS_WINDOWS_MOBILE - - -#if GTEST_OS_WINDOWS -# define GTEST_PATH_MAX_ _MAX_PATH -#elif defined(PATH_MAX) -# define GTEST_PATH_MAX_ PATH_MAX -#elif defined(_XOPEN_PATH_MAX) -# define GTEST_PATH_MAX_ _XOPEN_PATH_MAX -#else -# define GTEST_PATH_MAX_ _POSIX_PATH_MAX -#endif // GTEST_OS_WINDOWS - -namespace testing { -namespace internal { - -#if GTEST_OS_WINDOWS -// On Windows, '\\' is the standard path separator, but many tools and the -// Windows API also accept '/' as an alternate path separator. Unless otherwise -// noted, a file path can contain either kind of path separators, or a mixture -// of them. -const char kPathSeparator = '\\'; -const char kAlternatePathSeparator = '/'; -const char kAlternatePathSeparatorString[] = "/"; -# if GTEST_OS_WINDOWS_MOBILE -// Windows CE doesn't have a current directory. You should not use -// the current directory in tests on Windows CE, but this at least -// provides a reasonable fallback. -const char kCurrentDirectoryString[] = "\\"; -// Windows CE doesn't define INVALID_FILE_ATTRIBUTES -const DWORD kInvalidFileAttributes = 0xffffffff; -# else -const char kCurrentDirectoryString[] = ".\\"; -# endif // GTEST_OS_WINDOWS_MOBILE -#else -const char kPathSeparator = '/'; -const char kCurrentDirectoryString[] = "./"; -#endif // GTEST_OS_WINDOWS - -// Returns whether the given character is a valid path separator. -static bool IsPathSeparator(char c) { -#if GTEST_HAS_ALT_PATH_SEP_ - return (c == kPathSeparator) || (c == kAlternatePathSeparator); -#else - return c == kPathSeparator; -#endif -} - -// Returns the current working directory, or "" if unsuccessful. -FilePath FilePath::GetCurrentDir() { -#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ - GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32 || \ - GTEST_OS_XTENSA - // These platforms do not have a current directory, so we just return - // something reasonable. - return FilePath(kCurrentDirectoryString); -#elif GTEST_OS_WINDOWS - char cwd[GTEST_PATH_MAX_ + 1] = { '\0' }; - return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd); -#else - char cwd[GTEST_PATH_MAX_ + 1] = { '\0' }; - char* result = getcwd(cwd, sizeof(cwd)); -# if GTEST_OS_NACL - // getcwd will likely fail in NaCl due to the sandbox, so return something - // reasonable. The user may have provided a shim implementation for getcwd, - // however, so fallback only when failure is detected. - return FilePath(result == nullptr ? kCurrentDirectoryString : cwd); -# endif // GTEST_OS_NACL - return FilePath(result == nullptr ? "" : cwd); -#endif // GTEST_OS_WINDOWS_MOBILE -} - -// Returns a copy of the FilePath with the case-insensitive extension removed. -// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns -// FilePath("dir/file"). If a case-insensitive extension is not -// found, returns a copy of the original FilePath. -FilePath FilePath::RemoveExtension(const char* extension) const { - const std::string dot_extension = std::string(".") + extension; - if (String::EndsWithCaseInsensitive(pathname_, dot_extension)) { - return FilePath(pathname_.substr( - 0, pathname_.length() - dot_extension.length())); - } - return *this; -} - -// Returns a pointer to the last occurrence of a valid path separator in -// the FilePath. On Windows, for example, both '/' and '\' are valid path -// separators. Returns NULL if no path separator was found. -const char* FilePath::FindLastPathSeparator() const { - const char* const last_sep = strrchr(c_str(), kPathSeparator); -#if GTEST_HAS_ALT_PATH_SEP_ - const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator); - // Comparing two pointers of which only one is NULL is undefined. - if (last_alt_sep != nullptr && - (last_sep == nullptr || last_alt_sep > last_sep)) { - return last_alt_sep; - } -#endif - return last_sep; -} - -// Returns a copy of the FilePath with the directory part removed. -// Example: FilePath("path/to/file").RemoveDirectoryName() returns -// FilePath("file"). If there is no directory part ("just_a_file"), it returns -// the FilePath unmodified. If there is no file part ("just_a_dir/") it -// returns an empty FilePath (""). -// On Windows platform, '\' is the path separator, otherwise it is '/'. -FilePath FilePath::RemoveDirectoryName() const { - const char* const last_sep = FindLastPathSeparator(); - return last_sep ? FilePath(last_sep + 1) : *this; -} - -// RemoveFileName returns the directory path with the filename removed. -// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". -// If the FilePath is "a_file" or "/a_file", RemoveFileName returns -// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does -// not have a file, like "just/a/dir/", it returns the FilePath unmodified. -// On Windows platform, '\' is the path separator, otherwise it is '/'. -FilePath FilePath::RemoveFileName() const { - const char* const last_sep = FindLastPathSeparator(); - std::string dir; - if (last_sep) { - dir = std::string(c_str(), static_cast(last_sep + 1 - c_str())); - } else { - dir = kCurrentDirectoryString; - } - return FilePath(dir); -} - -// Helper functions for naming files in a directory for xml output. - -// Given directory = "dir", base_name = "test", number = 0, -// extension = "xml", returns "dir/test.xml". If number is greater -// than zero (e.g., 12), returns "dir/test_12.xml". -// On Windows platform, uses \ as the separator rather than /. -FilePath FilePath::MakeFileName(const FilePath& directory, - const FilePath& base_name, - int number, - const char* extension) { - std::string file; - if (number == 0) { - file = base_name.string() + "." + extension; - } else { - file = base_name.string() + "_" + StreamableToString(number) - + "." + extension; - } - return ConcatPaths(directory, FilePath(file)); -} - -// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml". -// On Windows, uses \ as the separator rather than /. -FilePath FilePath::ConcatPaths(const FilePath& directory, - const FilePath& relative_path) { - if (directory.IsEmpty()) - return relative_path; - const FilePath dir(directory.RemoveTrailingPathSeparator()); - return FilePath(dir.string() + kPathSeparator + relative_path.string()); -} - -// Returns true if pathname describes something findable in the file-system, -// either a file, directory, or whatever. -bool FilePath::FileOrDirectoryExists() const { -#if GTEST_OS_WINDOWS_MOBILE - LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str()); - const DWORD attributes = GetFileAttributes(unicode); - delete [] unicode; - return attributes != kInvalidFileAttributes; -#else - posix::StatStruct file_stat{}; - return posix::Stat(pathname_.c_str(), &file_stat) == 0; -#endif // GTEST_OS_WINDOWS_MOBILE -} - -// Returns true if pathname describes a directory in the file-system -// that exists. -bool FilePath::DirectoryExists() const { - bool result = false; -#if GTEST_OS_WINDOWS - // Don't strip off trailing separator if path is a root directory on - // Windows (like "C:\\"). - const FilePath& path(IsRootDirectory() ? *this : - RemoveTrailingPathSeparator()); -#else - const FilePath& path(*this); -#endif - -#if GTEST_OS_WINDOWS_MOBILE - LPCWSTR unicode = String::AnsiToUtf16(path.c_str()); - const DWORD attributes = GetFileAttributes(unicode); - delete [] unicode; - if ((attributes != kInvalidFileAttributes) && - (attributes & FILE_ATTRIBUTE_DIRECTORY)) { - result = true; - } -#else - posix::StatStruct file_stat{}; - result = posix::Stat(path.c_str(), &file_stat) == 0 && - posix::IsDir(file_stat); -#endif // GTEST_OS_WINDOWS_MOBILE - - return result; -} - -// Returns true if pathname describes a root directory. (Windows has one -// root directory per disk drive.) -bool FilePath::IsRootDirectory() const { -#if GTEST_OS_WINDOWS - return pathname_.length() == 3 && IsAbsolutePath(); -#else - return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]); -#endif -} - -// Returns true if pathname describes an absolute path. -bool FilePath::IsAbsolutePath() const { - const char* const name = pathname_.c_str(); -#if GTEST_OS_WINDOWS - return pathname_.length() >= 3 && - ((name[0] >= 'a' && name[0] <= 'z') || - (name[0] >= 'A' && name[0] <= 'Z')) && - name[1] == ':' && - IsPathSeparator(name[2]); -#else - return IsPathSeparator(name[0]); -#endif -} - -// Returns a pathname for a file that does not currently exist. The pathname -// will be directory/base_name.extension or -// directory/base_name_.extension if directory/base_name.extension -// already exists. The number will be incremented until a pathname is found -// that does not already exist. -// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. -// There could be a race condition if two or more processes are calling this -// function at the same time -- they could both pick the same filename. -FilePath FilePath::GenerateUniqueFileName(const FilePath& directory, - const FilePath& base_name, - const char* extension) { - FilePath full_pathname; - int number = 0; - do { - full_pathname.Set(MakeFileName(directory, base_name, number++, extension)); - } while (full_pathname.FileOrDirectoryExists()); - return full_pathname; -} - -// Returns true if FilePath ends with a path separator, which indicates that -// it is intended to represent a directory. Returns false otherwise. -// This does NOT check that a directory (or file) actually exists. -bool FilePath::IsDirectory() const { - return !pathname_.empty() && - IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]); -} - -// Create directories so that path exists. Returns true if successful or if -// the directories already exist; returns false if unable to create directories -// for any reason. -bool FilePath::CreateDirectoriesRecursively() const { - if (!this->IsDirectory()) { - return false; - } - - if (pathname_.length() == 0 || this->DirectoryExists()) { - return true; - } - - const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName()); - return parent.CreateDirectoriesRecursively() && this->CreateFolder(); -} - -// Create the directory so that path exists. Returns true if successful or -// if the directory already exists; returns false if unable to create the -// directory for any reason, including if the parent directory does not -// exist. Not named "CreateDirectory" because that's a macro on Windows. -bool FilePath::CreateFolder() const { -#if GTEST_OS_WINDOWS_MOBILE - FilePath removed_sep(this->RemoveTrailingPathSeparator()); - LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str()); - int result = CreateDirectory(unicode, nullptr) ? 0 : -1; - delete [] unicode; -#elif GTEST_OS_WINDOWS - int result = _mkdir(pathname_.c_str()); -#elif GTEST_OS_ESP8266 || GTEST_OS_XTENSA - // do nothing - int result = 0; -#else - int result = mkdir(pathname_.c_str(), 0777); -#endif // GTEST_OS_WINDOWS_MOBILE - - if (result == -1) { - return this->DirectoryExists(); // An error is OK if the directory exists. - } - return true; // No error. -} - -// If input name has a trailing separator character, remove it and return the -// name, otherwise return the name string unmodified. -// On Windows platform, uses \ as the separator, other platforms use /. -FilePath FilePath::RemoveTrailingPathSeparator() const { - return IsDirectory() - ? FilePath(pathname_.substr(0, pathname_.length() - 1)) - : *this; -} - -// Removes any redundant separators that might be in the pathname. -// For example, "bar///foo" becomes "bar/foo". Does not eliminate other -// redundancies that might be in a pathname involving "." or "..". -void FilePath::Normalize() { - auto out = pathname_.begin(); - - for (const char character : pathname_) { - if (!IsPathSeparator(character)) { - *(out++) = character; - } else if (out == pathname_.begin() || *std::prev(out) != kPathSeparator) { - *(out++) = kPathSeparator; - } else { - continue; - } - } - - pathname_.erase(out, pathname_.end()); -} - -} // namespace internal -} // namespace testing -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This file implements just enough of the matcher interface to allow -// EXPECT_DEATH and friends to accept a matcher argument. - - -#include - -namespace testing { - -// Constructs a matcher that matches a const std::string& whose value is -// equal to s. -Matcher::Matcher(const std::string& s) { *this = Eq(s); } - -// Constructs a matcher that matches a const std::string& whose value is -// equal to s. -Matcher::Matcher(const char* s) { - *this = Eq(std::string(s)); -} - -// Constructs a matcher that matches a std::string whose value is equal to -// s. -Matcher::Matcher(const std::string& s) { *this = Eq(s); } - -// Constructs a matcher that matches a std::string whose value is equal to -// s. -Matcher::Matcher(const char* s) { *this = Eq(std::string(s)); } - -#if GTEST_INTERNAL_HAS_STRING_VIEW -// Constructs a matcher that matches a const StringView& whose value is -// equal to s. -Matcher::Matcher(const std::string& s) { - *this = Eq(s); -} - -// Constructs a matcher that matches a const StringView& whose value is -// equal to s. -Matcher::Matcher(const char* s) { - *this = Eq(std::string(s)); -} - -// Constructs a matcher that matches a const StringView& whose value is -// equal to s. -Matcher::Matcher(internal::StringView s) { - *this = Eq(std::string(s)); -} - -// Constructs a matcher that matches a StringView whose value is equal to -// s. -Matcher::Matcher(const std::string& s) { *this = Eq(s); } - -// Constructs a matcher that matches a StringView whose value is equal to -// s. -Matcher::Matcher(const char* s) { - *this = Eq(std::string(s)); -} - -// Constructs a matcher that matches a StringView whose value is equal to -// s. -Matcher::Matcher(internal::StringView s) { - *this = Eq(std::string(s)); -} -#endif // GTEST_INTERNAL_HAS_STRING_VIEW - -} // namespace testing -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - - -#include -#include -#include -#include -#include -#include -#include - -#if GTEST_OS_WINDOWS -# include -# include -# include -# include // Used in ThreadLocal. -# ifdef _MSC_VER -# include -# endif // _MSC_VER -#else -# include -#endif // GTEST_OS_WINDOWS - -#if GTEST_OS_MAC -# include -# include -# include -#endif // GTEST_OS_MAC - -#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ - GTEST_OS_NETBSD || GTEST_OS_OPENBSD -# include -# if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD -# include -# endif -#endif - -#if GTEST_OS_QNX -# include -# include -# include -#endif // GTEST_OS_QNX - -#if GTEST_OS_AIX -# include -# include -#endif // GTEST_OS_AIX - -#if GTEST_OS_FUCHSIA -# include -# include -#endif // GTEST_OS_FUCHSIA - - -namespace testing { -namespace internal { - -#if defined(_MSC_VER) || defined(__BORLANDC__) -// MSVC and C++Builder do not provide a definition of STDERR_FILENO. -const int kStdOutFileno = 1; -const int kStdErrFileno = 2; -#else -const int kStdOutFileno = STDOUT_FILENO; -const int kStdErrFileno = STDERR_FILENO; -#endif // _MSC_VER - -#if GTEST_OS_LINUX - -namespace { -template -T ReadProcFileField(const std::string& filename, int field) { - std::string dummy; - std::ifstream file(filename.c_str()); - while (field-- > 0) { - file >> dummy; - } - T output = 0; - file >> output; - return output; -} -} // namespace - -// Returns the number of active threads, or 0 when there is an error. -size_t GetThreadCount() { - const std::string filename = - (Message() << "/proc/" << getpid() << "/stat").GetString(); - return ReadProcFileField(filename, 19); -} - -#elif GTEST_OS_MAC - -size_t GetThreadCount() { - const task_t task = mach_task_self(); - mach_msg_type_number_t thread_count; - thread_act_array_t thread_list; - const kern_return_t status = task_threads(task, &thread_list, &thread_count); - if (status == KERN_SUCCESS) { - // task_threads allocates resources in thread_list and we need to free them - // to avoid leaks. - vm_deallocate(task, - reinterpret_cast(thread_list), - sizeof(thread_t) * thread_count); - return static_cast(thread_count); - } else { - return 0; - } -} - -#elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ - GTEST_OS_NETBSD - -#if GTEST_OS_NETBSD -#undef KERN_PROC -#define KERN_PROC KERN_PROC2 -#define kinfo_proc kinfo_proc2 -#endif - -#if GTEST_OS_DRAGONFLY -#define KP_NLWP(kp) (kp.kp_nthreads) -#elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD -#define KP_NLWP(kp) (kp.ki_numthreads) -#elif GTEST_OS_NETBSD -#define KP_NLWP(kp) (kp.p_nlwps) -#endif - -// Returns the number of threads running in the process, or 0 to indicate that -// we cannot detect it. -size_t GetThreadCount() { - int mib[] = { - CTL_KERN, - KERN_PROC, - KERN_PROC_PID, - getpid(), -#if GTEST_OS_NETBSD - sizeof(struct kinfo_proc), - 1, -#endif - }; - u_int miblen = sizeof(mib) / sizeof(mib[0]); - struct kinfo_proc info; - size_t size = sizeof(info); - if (sysctl(mib, miblen, &info, &size, NULL, 0)) { - return 0; - } - return static_cast(KP_NLWP(info)); -} -#elif GTEST_OS_OPENBSD - -// Returns the number of threads running in the process, or 0 to indicate that -// we cannot detect it. -size_t GetThreadCount() { - int mib[] = { - CTL_KERN, - KERN_PROC, - KERN_PROC_PID | KERN_PROC_SHOW_THREADS, - getpid(), - sizeof(struct kinfo_proc), - 0, - }; - u_int miblen = sizeof(mib) / sizeof(mib[0]); - - // get number of structs - size_t size; - if (sysctl(mib, miblen, NULL, &size, NULL, 0)) { - return 0; - } - - mib[5] = static_cast(size / static_cast(mib[4])); - - // populate array of structs - struct kinfo_proc info[mib[5]]; - if (sysctl(mib, miblen, &info, &size, NULL, 0)) { - return 0; - } - - // exclude empty members - size_t nthreads = 0; - for (size_t i = 0; i < size / static_cast(mib[4]); i++) { - if (info[i].p_tid != -1) - nthreads++; - } - return nthreads; -} - -#elif GTEST_OS_QNX - -// Returns the number of threads running in the process, or 0 to indicate that -// we cannot detect it. -size_t GetThreadCount() { - const int fd = open("/proc/self/as", O_RDONLY); - if (fd < 0) { - return 0; - } - procfs_info process_info; - const int status = - devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), nullptr); - close(fd); - if (status == EOK) { - return static_cast(process_info.num_threads); - } else { - return 0; - } -} - -#elif GTEST_OS_AIX - -size_t GetThreadCount() { - struct procentry64 entry; - pid_t pid = getpid(); - int status = getprocs64(&entry, sizeof(entry), nullptr, 0, &pid, 1); - if (status == 1) { - return entry.pi_thcount; - } else { - return 0; - } -} - -#elif GTEST_OS_FUCHSIA - -size_t GetThreadCount() { - int dummy_buffer; - size_t avail; - zx_status_t status = zx_object_get_info( - zx_process_self(), - ZX_INFO_PROCESS_THREADS, - &dummy_buffer, - 0, - nullptr, - &avail); - if (status == ZX_OK) { - return avail; - } else { - return 0; - } -} - -#else - -size_t GetThreadCount() { - // There's no portable way to detect the number of threads, so we just - // return 0 to indicate that we cannot detect it. - return 0; -} - -#endif // GTEST_OS_LINUX - -#if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS - -void SleepMilliseconds(int n) { - ::Sleep(static_cast(n)); -} - -AutoHandle::AutoHandle() - : handle_(INVALID_HANDLE_VALUE) {} - -AutoHandle::AutoHandle(Handle handle) - : handle_(handle) {} - -AutoHandle::~AutoHandle() { - Reset(); -} - -AutoHandle::Handle AutoHandle::Get() const { - return handle_; -} - -void AutoHandle::Reset() { - Reset(INVALID_HANDLE_VALUE); -} - -void AutoHandle::Reset(HANDLE handle) { - // Resetting with the same handle we already own is invalid. - if (handle_ != handle) { - if (IsCloseable()) { - ::CloseHandle(handle_); - } - handle_ = handle; - } else { - GTEST_CHECK_(!IsCloseable()) - << "Resetting a valid handle to itself is likely a programmer error " - "and thus not allowed."; - } -} - -bool AutoHandle::IsCloseable() const { - // Different Windows APIs may use either of these values to represent an - // invalid handle. - return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE; -} - -Notification::Notification() - : event_(::CreateEvent(nullptr, // Default security attributes. - TRUE, // Do not reset automatically. - FALSE, // Initially unset. - nullptr)) { // Anonymous event. - GTEST_CHECK_(event_.Get() != nullptr); -} - -void Notification::Notify() { - GTEST_CHECK_(::SetEvent(event_.Get()) != FALSE); -} - -void Notification::WaitForNotification() { - GTEST_CHECK_( - ::WaitForSingleObject(event_.Get(), INFINITE) == WAIT_OBJECT_0); -} - -Mutex::Mutex() - : owner_thread_id_(0), - type_(kDynamic), - critical_section_init_phase_(0), - critical_section_(new CRITICAL_SECTION) { - ::InitializeCriticalSection(critical_section_); -} - -Mutex::~Mutex() { - // Static mutexes are leaked intentionally. It is not thread-safe to try - // to clean them up. - if (type_ == kDynamic) { - ::DeleteCriticalSection(critical_section_); - delete critical_section_; - critical_section_ = nullptr; - } -} - -void Mutex::Lock() { - ThreadSafeLazyInit(); - ::EnterCriticalSection(critical_section_); - owner_thread_id_ = ::GetCurrentThreadId(); -} - -void Mutex::Unlock() { - ThreadSafeLazyInit(); - // We don't protect writing to owner_thread_id_ here, as it's the - // caller's responsibility to ensure that the current thread holds the - // mutex when this is called. - owner_thread_id_ = 0; - ::LeaveCriticalSection(critical_section_); -} - -// Does nothing if the current thread holds the mutex. Otherwise, crashes -// with high probability. -void Mutex::AssertHeld() { - ThreadSafeLazyInit(); - GTEST_CHECK_(owner_thread_id_ == ::GetCurrentThreadId()) - << "The current thread is not holding the mutex @" << this; -} - -namespace { - -#ifdef _MSC_VER -// Use the RAII idiom to flag mem allocs that are intentionally never -// deallocated. The motivation is to silence the false positive mem leaks -// that are reported by the debug version of MS's CRT which can only detect -// if an alloc is missing a matching deallocation. -// Example: -// MemoryIsNotDeallocated memory_is_not_deallocated; -// critical_section_ = new CRITICAL_SECTION; -// -class MemoryIsNotDeallocated -{ - public: - MemoryIsNotDeallocated() : old_crtdbg_flag_(0) { - old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); - // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT - // doesn't report mem leak if there's no matching deallocation. - _CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF); - } - - ~MemoryIsNotDeallocated() { - // Restore the original _CRTDBG_ALLOC_MEM_DF flag - _CrtSetDbgFlag(old_crtdbg_flag_); - } - - private: - int old_crtdbg_flag_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(MemoryIsNotDeallocated); -}; -#endif // _MSC_VER - -} // namespace - -// Initializes owner_thread_id_ and critical_section_ in static mutexes. -void Mutex::ThreadSafeLazyInit() { - // Dynamic mutexes are initialized in the constructor. - if (type_ == kStatic) { - switch ( - ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) { - case 0: - // If critical_section_init_phase_ was 0 before the exchange, we - // are the first to test it and need to perform the initialization. - owner_thread_id_ = 0; - { - // Use RAII to flag that following mem alloc is never deallocated. -#ifdef _MSC_VER - MemoryIsNotDeallocated memory_is_not_deallocated; -#endif // _MSC_VER - critical_section_ = new CRITICAL_SECTION; - } - ::InitializeCriticalSection(critical_section_); - // Updates the critical_section_init_phase_ to 2 to signal - // initialization complete. - GTEST_CHECK_(::InterlockedCompareExchange( - &critical_section_init_phase_, 2L, 1L) == - 1L); - break; - case 1: - // Somebody else is already initializing the mutex; spin until they - // are done. - while (::InterlockedCompareExchange(&critical_section_init_phase_, - 2L, - 2L) != 2L) { - // Possibly yields the rest of the thread's time slice to other - // threads. - ::Sleep(0); - } - break; - - case 2: - break; // The mutex is already initialized and ready for use. - - default: - GTEST_CHECK_(false) - << "Unexpected value of critical_section_init_phase_ " - << "while initializing a static mutex."; - } - } -} - -namespace { - -class ThreadWithParamSupport : public ThreadWithParamBase { - public: - static HANDLE CreateThread(Runnable* runnable, - Notification* thread_can_start) { - ThreadMainParam* param = new ThreadMainParam(runnable, thread_can_start); - DWORD thread_id; - HANDLE thread_handle = ::CreateThread( - nullptr, // Default security. - 0, // Default stack size. - &ThreadWithParamSupport::ThreadMain, - param, // Parameter to ThreadMainStatic - 0x0, // Default creation flags. - &thread_id); // Need a valid pointer for the call to work under Win98. - GTEST_CHECK_(thread_handle != nullptr) - << "CreateThread failed with error " << ::GetLastError() << "."; - if (thread_handle == nullptr) { - delete param; - } - return thread_handle; - } - - private: - struct ThreadMainParam { - ThreadMainParam(Runnable* runnable, Notification* thread_can_start) - : runnable_(runnable), - thread_can_start_(thread_can_start) { - } - std::unique_ptr runnable_; - // Does not own. - Notification* thread_can_start_; - }; - - static DWORD WINAPI ThreadMain(void* ptr) { - // Transfers ownership. - std::unique_ptr param(static_cast(ptr)); - if (param->thread_can_start_ != nullptr) - param->thread_can_start_->WaitForNotification(); - param->runnable_->Run(); - return 0; - } - - // Prohibit instantiation. - ThreadWithParamSupport(); - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParamSupport); -}; - -} // namespace - -ThreadWithParamBase::ThreadWithParamBase(Runnable *runnable, - Notification* thread_can_start) - : thread_(ThreadWithParamSupport::CreateThread(runnable, - thread_can_start)) { -} - -ThreadWithParamBase::~ThreadWithParamBase() { - Join(); -} - -void ThreadWithParamBase::Join() { - GTEST_CHECK_(::WaitForSingleObject(thread_.Get(), INFINITE) == WAIT_OBJECT_0) - << "Failed to join the thread with error " << ::GetLastError() << "."; -} - -// Maps a thread to a set of ThreadIdToThreadLocals that have values -// instantiated on that thread and notifies them when the thread exits. A -// ThreadLocal instance is expected to persist until all threads it has -// values on have terminated. -class ThreadLocalRegistryImpl { - public: - // Registers thread_local_instance as having value on the current thread. - // Returns a value that can be used to identify the thread from other threads. - static ThreadLocalValueHolderBase* GetValueOnCurrentThread( - const ThreadLocalBase* thread_local_instance) { -#ifdef _MSC_VER - MemoryIsNotDeallocated memory_is_not_deallocated; -#endif // _MSC_VER - DWORD current_thread = ::GetCurrentThreadId(); - MutexLock lock(&mutex_); - ThreadIdToThreadLocals* const thread_to_thread_locals = - GetThreadLocalsMapLocked(); - ThreadIdToThreadLocals::iterator thread_local_pos = - thread_to_thread_locals->find(current_thread); - if (thread_local_pos == thread_to_thread_locals->end()) { - thread_local_pos = thread_to_thread_locals->insert( - std::make_pair(current_thread, ThreadLocalValues())).first; - StartWatcherThreadFor(current_thread); - } - ThreadLocalValues& thread_local_values = thread_local_pos->second; - ThreadLocalValues::iterator value_pos = - thread_local_values.find(thread_local_instance); - if (value_pos == thread_local_values.end()) { - value_pos = - thread_local_values - .insert(std::make_pair( - thread_local_instance, - std::shared_ptr( - thread_local_instance->NewValueForCurrentThread()))) - .first; - } - return value_pos->second.get(); - } - - static void OnThreadLocalDestroyed( - const ThreadLocalBase* thread_local_instance) { - std::vector > value_holders; - // Clean up the ThreadLocalValues data structure while holding the lock, but - // defer the destruction of the ThreadLocalValueHolderBases. - { - MutexLock lock(&mutex_); - ThreadIdToThreadLocals* const thread_to_thread_locals = - GetThreadLocalsMapLocked(); - for (ThreadIdToThreadLocals::iterator it = - thread_to_thread_locals->begin(); - it != thread_to_thread_locals->end(); - ++it) { - ThreadLocalValues& thread_local_values = it->second; - ThreadLocalValues::iterator value_pos = - thread_local_values.find(thread_local_instance); - if (value_pos != thread_local_values.end()) { - value_holders.push_back(value_pos->second); - thread_local_values.erase(value_pos); - // This 'if' can only be successful at most once, so theoretically we - // could break out of the loop here, but we don't bother doing so. - } - } - } - // Outside the lock, let the destructor for 'value_holders' deallocate the - // ThreadLocalValueHolderBases. - } - - static void OnThreadExit(DWORD thread_id) { - GTEST_CHECK_(thread_id != 0) << ::GetLastError(); - std::vector > value_holders; - // Clean up the ThreadIdToThreadLocals data structure while holding the - // lock, but defer the destruction of the ThreadLocalValueHolderBases. - { - MutexLock lock(&mutex_); - ThreadIdToThreadLocals* const thread_to_thread_locals = - GetThreadLocalsMapLocked(); - ThreadIdToThreadLocals::iterator thread_local_pos = - thread_to_thread_locals->find(thread_id); - if (thread_local_pos != thread_to_thread_locals->end()) { - ThreadLocalValues& thread_local_values = thread_local_pos->second; - for (ThreadLocalValues::iterator value_pos = - thread_local_values.begin(); - value_pos != thread_local_values.end(); - ++value_pos) { - value_holders.push_back(value_pos->second); - } - thread_to_thread_locals->erase(thread_local_pos); - } - } - // Outside the lock, let the destructor for 'value_holders' deallocate the - // ThreadLocalValueHolderBases. - } - - private: - // In a particular thread, maps a ThreadLocal object to its value. - typedef std::map > - ThreadLocalValues; - // Stores all ThreadIdToThreadLocals having values in a thread, indexed by - // thread's ID. - typedef std::map ThreadIdToThreadLocals; - - // Holds the thread id and thread handle that we pass from - // StartWatcherThreadFor to WatcherThreadFunc. - typedef std::pair ThreadIdAndHandle; - - static void StartWatcherThreadFor(DWORD thread_id) { - // The returned handle will be kept in thread_map and closed by - // watcher_thread in WatcherThreadFunc. - HANDLE thread = ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION, - FALSE, - thread_id); - GTEST_CHECK_(thread != nullptr); - // We need to pass a valid thread ID pointer into CreateThread for it - // to work correctly under Win98. - DWORD watcher_thread_id; - HANDLE watcher_thread = ::CreateThread( - nullptr, // Default security. - 0, // Default stack size - &ThreadLocalRegistryImpl::WatcherThreadFunc, - reinterpret_cast(new ThreadIdAndHandle(thread_id, thread)), - CREATE_SUSPENDED, &watcher_thread_id); - GTEST_CHECK_(watcher_thread != nullptr); - // Give the watcher thread the same priority as ours to avoid being - // blocked by it. - ::SetThreadPriority(watcher_thread, - ::GetThreadPriority(::GetCurrentThread())); - ::ResumeThread(watcher_thread); - ::CloseHandle(watcher_thread); - } - - // Monitors exit from a given thread and notifies those - // ThreadIdToThreadLocals about thread termination. - static DWORD WINAPI WatcherThreadFunc(LPVOID param) { - const ThreadIdAndHandle* tah = - reinterpret_cast(param); - GTEST_CHECK_( - ::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0); - OnThreadExit(tah->first); - ::CloseHandle(tah->second); - delete tah; - return 0; - } - - // Returns map of thread local instances. - static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() { - mutex_.AssertHeld(); -#ifdef _MSC_VER - MemoryIsNotDeallocated memory_is_not_deallocated; -#endif // _MSC_VER - static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals(); - return map; - } - - // Protects access to GetThreadLocalsMapLocked() and its return value. - static Mutex mutex_; - // Protects access to GetThreadMapLocked() and its return value. - static Mutex thread_map_mutex_; -}; - -Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT -Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex); // NOLINT - -ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread( - const ThreadLocalBase* thread_local_instance) { - return ThreadLocalRegistryImpl::GetValueOnCurrentThread( - thread_local_instance); -} - -void ThreadLocalRegistry::OnThreadLocalDestroyed( - const ThreadLocalBase* thread_local_instance) { - ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance); -} - -#endif // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS - -#if GTEST_USES_POSIX_RE - -// Implements RE. Currently only needed for death tests. - -RE::~RE() { - if (is_valid_) { - // regfree'ing an invalid regex might crash because the content - // of the regex is undefined. Since the regex's are essentially - // the same, one cannot be valid (or invalid) without the other - // being so too. - regfree(&partial_regex_); - regfree(&full_regex_); - } - free(const_cast(pattern_)); -} - -// Returns true if and only if regular expression re matches the entire str. -bool RE::FullMatch(const char* str, const RE& re) { - if (!re.is_valid_) return false; - - regmatch_t match; - return regexec(&re.full_regex_, str, 1, &match, 0) == 0; -} - -// Returns true if and only if regular expression re matches a substring of -// str (including str itself). -bool RE::PartialMatch(const char* str, const RE& re) { - if (!re.is_valid_) return false; - - regmatch_t match; - return regexec(&re.partial_regex_, str, 1, &match, 0) == 0; -} - -// Initializes an RE from its string representation. -void RE::Init(const char* regex) { - pattern_ = posix::StrDup(regex); - - // Reserves enough bytes to hold the regular expression used for a - // full match. - const size_t full_regex_len = strlen(regex) + 10; - char* const full_pattern = new char[full_regex_len]; - - snprintf(full_pattern, full_regex_len, "^(%s)$", regex); - is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0; - // We want to call regcomp(&partial_regex_, ...) even if the - // previous expression returns false. Otherwise partial_regex_ may - // not be properly initialized can may cause trouble when it's - // freed. - // - // Some implementation of POSIX regex (e.g. on at least some - // versions of Cygwin) doesn't accept the empty string as a valid - // regex. We change it to an equivalent form "()" to be safe. - if (is_valid_) { - const char* const partial_regex = (*regex == '\0') ? "()" : regex; - is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0; - } - EXPECT_TRUE(is_valid_) - << "Regular expression \"" << regex - << "\" is not a valid POSIX Extended regular expression."; - - delete[] full_pattern; -} - -#elif GTEST_USES_SIMPLE_RE - -// Returns true if and only if ch appears anywhere in str (excluding the -// terminating '\0' character). -bool IsInSet(char ch, const char* str) { - return ch != '\0' && strchr(str, ch) != nullptr; -} - -// Returns true if and only if ch belongs to the given classification. -// Unlike similar functions in , these aren't affected by the -// current locale. -bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; } -bool IsAsciiPunct(char ch) { - return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"); -} -bool IsRepeat(char ch) { return IsInSet(ch, "?*+"); } -bool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); } -bool IsAsciiWordChar(char ch) { - return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || - ('0' <= ch && ch <= '9') || ch == '_'; -} - -// Returns true if and only if "\\c" is a supported escape sequence. -bool IsValidEscape(char c) { - return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW")); -} - -// Returns true if and only if the given atom (specified by escaped and -// pattern) matches ch. The result is undefined if the atom is invalid. -bool AtomMatchesChar(bool escaped, char pattern_char, char ch) { - if (escaped) { // "\\p" where p is pattern_char. - switch (pattern_char) { - case 'd': return IsAsciiDigit(ch); - case 'D': return !IsAsciiDigit(ch); - case 'f': return ch == '\f'; - case 'n': return ch == '\n'; - case 'r': return ch == '\r'; - case 's': return IsAsciiWhiteSpace(ch); - case 'S': return !IsAsciiWhiteSpace(ch); - case 't': return ch == '\t'; - case 'v': return ch == '\v'; - case 'w': return IsAsciiWordChar(ch); - case 'W': return !IsAsciiWordChar(ch); - } - return IsAsciiPunct(pattern_char) && pattern_char == ch; - } - - return (pattern_char == '.' && ch != '\n') || pattern_char == ch; -} - -// Helper function used by ValidateRegex() to format error messages. -static std::string FormatRegexSyntaxError(const char* regex, int index) { - return (Message() << "Syntax error at index " << index - << " in simple regular expression \"" << regex << "\": ").GetString(); -} - -// Generates non-fatal failures and returns false if regex is invalid; -// otherwise returns true. -bool ValidateRegex(const char* regex) { - if (regex == nullptr) { - ADD_FAILURE() << "NULL is not a valid simple regular expression."; - return false; - } - - bool is_valid = true; - - // True if and only if ?, *, or + can follow the previous atom. - bool prev_repeatable = false; - for (int i = 0; regex[i]; i++) { - if (regex[i] == '\\') { // An escape sequence - i++; - if (regex[i] == '\0') { - ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) - << "'\\' cannot appear at the end."; - return false; - } - - if (!IsValidEscape(regex[i])) { - ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) - << "invalid escape sequence \"\\" << regex[i] << "\"."; - is_valid = false; - } - prev_repeatable = true; - } else { // Not an escape sequence. - const char ch = regex[i]; - - if (ch == '^' && i > 0) { - ADD_FAILURE() << FormatRegexSyntaxError(regex, i) - << "'^' can only appear at the beginning."; - is_valid = false; - } else if (ch == '$' && regex[i + 1] != '\0') { - ADD_FAILURE() << FormatRegexSyntaxError(regex, i) - << "'$' can only appear at the end."; - is_valid = false; - } else if (IsInSet(ch, "()[]{}|")) { - ADD_FAILURE() << FormatRegexSyntaxError(regex, i) - << "'" << ch << "' is unsupported."; - is_valid = false; - } else if (IsRepeat(ch) && !prev_repeatable) { - ADD_FAILURE() << FormatRegexSyntaxError(regex, i) - << "'" << ch << "' can only follow a repeatable token."; - is_valid = false; - } - - prev_repeatable = !IsInSet(ch, "^$?*+"); - } - } - - return is_valid; -} - -// Matches a repeated regex atom followed by a valid simple regular -// expression. The regex atom is defined as c if escaped is false, -// or \c otherwise. repeat is the repetition meta character (?, *, -// or +). The behavior is undefined if str contains too many -// characters to be indexable by size_t, in which case the test will -// probably time out anyway. We are fine with this limitation as -// std::string has it too. -bool MatchRepetitionAndRegexAtHead( - bool escaped, char c, char repeat, const char* regex, - const char* str) { - const size_t min_count = (repeat == '+') ? 1 : 0; - const size_t max_count = (repeat == '?') ? 1 : - static_cast(-1) - 1; - // We cannot call numeric_limits::max() as it conflicts with the - // max() macro on Windows. - - for (size_t i = 0; i <= max_count; ++i) { - // We know that the atom matches each of the first i characters in str. - if (i >= min_count && MatchRegexAtHead(regex, str + i)) { - // We have enough matches at the head, and the tail matches too. - // Since we only care about *whether* the pattern matches str - // (as opposed to *how* it matches), there is no need to find a - // greedy match. - return true; - } - if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i])) - return false; - } - return false; -} - -// Returns true if and only if regex matches a prefix of str. regex must -// be a valid simple regular expression and not start with "^", or the -// result is undefined. -bool MatchRegexAtHead(const char* regex, const char* str) { - if (*regex == '\0') // An empty regex matches a prefix of anything. - return true; - - // "$" only matches the end of a string. Note that regex being - // valid guarantees that there's nothing after "$" in it. - if (*regex == '$') - return *str == '\0'; - - // Is the first thing in regex an escape sequence? - const bool escaped = *regex == '\\'; - if (escaped) - ++regex; - if (IsRepeat(regex[1])) { - // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so - // here's an indirect recursion. It terminates as the regex gets - // shorter in each recursion. - return MatchRepetitionAndRegexAtHead( - escaped, regex[0], regex[1], regex + 2, str); - } else { - // regex isn't empty, isn't "$", and doesn't start with a - // repetition. We match the first atom of regex with the first - // character of str and recurse. - return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) && - MatchRegexAtHead(regex + 1, str + 1); - } -} - -// Returns true if and only if regex matches any substring of str. regex must -// be a valid simple regular expression, or the result is undefined. -// -// The algorithm is recursive, but the recursion depth doesn't exceed -// the regex length, so we won't need to worry about running out of -// stack space normally. In rare cases the time complexity can be -// exponential with respect to the regex length + the string length, -// but usually it's must faster (often close to linear). -bool MatchRegexAnywhere(const char* regex, const char* str) { - if (regex == nullptr || str == nullptr) return false; - - if (*regex == '^') - return MatchRegexAtHead(regex + 1, str); - - // A successful match can be anywhere in str. - do { - if (MatchRegexAtHead(regex, str)) - return true; - } while (*str++ != '\0'); - return false; -} - -// Implements the RE class. - -RE::~RE() { - free(const_cast(pattern_)); - free(const_cast(full_pattern_)); -} - -// Returns true if and only if regular expression re matches the entire str. -bool RE::FullMatch(const char* str, const RE& re) { - return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str); -} - -// Returns true if and only if regular expression re matches a substring of -// str (including str itself). -bool RE::PartialMatch(const char* str, const RE& re) { - return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str); -} - -// Initializes an RE from its string representation. -void RE::Init(const char* regex) { - pattern_ = full_pattern_ = nullptr; - if (regex != nullptr) { - pattern_ = posix::StrDup(regex); - } - - is_valid_ = ValidateRegex(regex); - if (!is_valid_) { - // No need to calculate the full pattern when the regex is invalid. - return; - } - - const size_t len = strlen(regex); - // Reserves enough bytes to hold the regular expression used for a - // full match: we need space to prepend a '^', append a '$', and - // terminate the string with '\0'. - char* buffer = static_cast(malloc(len + 3)); - full_pattern_ = buffer; - - if (*regex != '^') - *buffer++ = '^'; // Makes sure full_pattern_ starts with '^'. - - // We don't use snprintf or strncpy, as they trigger a warning when - // compiled with VC++ 8.0. - memcpy(buffer, regex, len); - buffer += len; - - if (len == 0 || regex[len - 1] != '$') - *buffer++ = '$'; // Makes sure full_pattern_ ends with '$'. - - *buffer = '\0'; -} - -#endif // GTEST_USES_POSIX_RE - -const char kUnknownFile[] = "unknown file"; - -// Formats a source file path and a line number as they would appear -// in an error message from the compiler used to compile this code. -GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) { - const std::string file_name(file == nullptr ? kUnknownFile : file); - - if (line < 0) { - return file_name + ":"; - } -#ifdef _MSC_VER - return file_name + "(" + StreamableToString(line) + "):"; -#else - return file_name + ":" + StreamableToString(line) + ":"; -#endif // _MSC_VER -} - -// Formats a file location for compiler-independent XML output. -// Although this function is not platform dependent, we put it next to -// FormatFileLocation in order to contrast the two functions. -// Note that FormatCompilerIndependentFileLocation() does NOT append colon -// to the file location it produces, unlike FormatFileLocation(). -GTEST_API_ ::std::string FormatCompilerIndependentFileLocation( - const char* file, int line) { - const std::string file_name(file == nullptr ? kUnknownFile : file); - - if (line < 0) - return file_name; - else - return file_name + ":" + StreamableToString(line); -} - -GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line) - : severity_(severity) { - const char* const marker = - severity == GTEST_INFO ? "[ INFO ]" : - severity == GTEST_WARNING ? "[WARNING]" : - severity == GTEST_ERROR ? "[ ERROR ]" : "[ FATAL ]"; - GetStream() << ::std::endl << marker << " " - << FormatFileLocation(file, line).c_str() << ": "; -} - -// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. -GTestLog::~GTestLog() { - GetStream() << ::std::endl; - if (severity_ == GTEST_FATAL) { - fflush(stderr); - posix::Abort(); - } -} - -// Disable Microsoft deprecation warnings for POSIX functions called from -// this class (creat, dup, dup2, and close) -GTEST_DISABLE_MSC_DEPRECATED_PUSH_() - -#if GTEST_HAS_STREAM_REDIRECTION - -// Object that captures an output stream (stdout/stderr). -class CapturedStream { - public: - // The ctor redirects the stream to a temporary file. - explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) { -# if GTEST_OS_WINDOWS - char temp_dir_path[MAX_PATH + 1] = { '\0' }; // NOLINT - char temp_file_path[MAX_PATH + 1] = { '\0' }; // NOLINT - - ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path); - const UINT success = ::GetTempFileNameA(temp_dir_path, - "gtest_redir", - 0, // Generate unique file name. - temp_file_path); - GTEST_CHECK_(success != 0) - << "Unable to create a temporary file in " << temp_dir_path; - const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE); - GTEST_CHECK_(captured_fd != -1) << "Unable to open temporary file " - << temp_file_path; - filename_ = temp_file_path; -# else - // There's no guarantee that a test has write access to the current - // directory, so we create the temporary file in a temporary directory. - std::string name_template; - -# if GTEST_OS_LINUX_ANDROID - // Note: Android applications are expected to call the framework's - // Context.getExternalStorageDirectory() method through JNI to get - // the location of the world-writable SD Card directory. However, - // this requires a Context handle, which cannot be retrieved - // globally from native code. Doing so also precludes running the - // code as part of a regular standalone executable, which doesn't - // run in a Dalvik process (e.g. when running it through 'adb shell'). - // - // The location /data/local/tmp is directly accessible from native code. - // '/sdcard' and other variants cannot be relied on, as they are not - // guaranteed to be mounted, or may have a delay in mounting. - name_template = "/data/local/tmp/"; -# elif GTEST_OS_IOS - char user_temp_dir[PATH_MAX + 1]; - - // Documented alternative to NSTemporaryDirectory() (for obtaining creating - // a temporary directory) at - // https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10 - // - // _CS_DARWIN_USER_TEMP_DIR (as well as _CS_DARWIN_USER_CACHE_DIR) is not - // documented in the confstr() man page at - // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/confstr.3.html#//apple_ref/doc/man/3/confstr - // but are still available, according to the WebKit patches at - // https://trac.webkit.org/changeset/262004/webkit - // https://trac.webkit.org/changeset/263705/webkit - // - // The confstr() implementation falls back to getenv("TMPDIR"). See - // https://opensource.apple.com/source/Libc/Libc-1439.100.3/gen/confstr.c.auto.html - ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir)); - - name_template = user_temp_dir; - if (name_template.back() != GTEST_PATH_SEP_[0]) - name_template.push_back(GTEST_PATH_SEP_[0]); -# else - name_template = "/tmp/"; -# endif - name_template.append("gtest_captured_stream.XXXXXX"); - - // mkstemp() modifies the string bytes in place, and does not go beyond the - // string's length. This results in well-defined behavior in C++17. - // - // The const_cast is needed below C++17. The constraints on std::string - // implementations in C++11 and above make assumption behind the const_cast - // fairly safe. - const int captured_fd = ::mkstemp(const_cast(name_template.data())); - if (captured_fd == -1) { - GTEST_LOG_(WARNING) - << "Failed to create tmp file " << name_template - << " for test; does the test have access to the /tmp directory?"; - } - filename_ = std::move(name_template); -# endif // GTEST_OS_WINDOWS - fflush(nullptr); - dup2(captured_fd, fd_); - close(captured_fd); - } - - ~CapturedStream() { - remove(filename_.c_str()); - } - - std::string GetCapturedString() { - if (uncaptured_fd_ != -1) { - // Restores the original stream. - fflush(nullptr); - dup2(uncaptured_fd_, fd_); - close(uncaptured_fd_); - uncaptured_fd_ = -1; - } - - FILE* const file = posix::FOpen(filename_.c_str(), "r"); - if (file == nullptr) { - GTEST_LOG_(FATAL) << "Failed to open tmp file " << filename_ - << " for capturing stream."; - } - const std::string content = ReadEntireFile(file); - posix::FClose(file); - return content; - } - - private: - const int fd_; // A stream to capture. - int uncaptured_fd_; - // Name of the temporary file holding the stderr output. - ::std::string filename_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream); -}; - -GTEST_DISABLE_MSC_DEPRECATED_POP_() - -static CapturedStream* g_captured_stderr = nullptr; -static CapturedStream* g_captured_stdout = nullptr; - -// Starts capturing an output stream (stdout/stderr). -static void CaptureStream(int fd, const char* stream_name, - CapturedStream** stream) { - if (*stream != nullptr) { - GTEST_LOG_(FATAL) << "Only one " << stream_name - << " capturer can exist at a time."; - } - *stream = new CapturedStream(fd); -} - -// Stops capturing the output stream and returns the captured string. -static std::string GetCapturedStream(CapturedStream** captured_stream) { - const std::string content = (*captured_stream)->GetCapturedString(); - - delete *captured_stream; - *captured_stream = nullptr; - - return content; -} - -// Starts capturing stdout. -void CaptureStdout() { - CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout); -} - -// Starts capturing stderr. -void CaptureStderr() { - CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr); -} - -// Stops capturing stdout and returns the captured string. -std::string GetCapturedStdout() { - return GetCapturedStream(&g_captured_stdout); -} - -// Stops capturing stderr and returns the captured string. -std::string GetCapturedStderr() { - return GetCapturedStream(&g_captured_stderr); -} - -#endif // GTEST_HAS_STREAM_REDIRECTION - - - - - -size_t GetFileSize(FILE* file) { - fseek(file, 0, SEEK_END); - return static_cast(ftell(file)); -} - -std::string ReadEntireFile(FILE* file) { - const size_t file_size = GetFileSize(file); - char* const buffer = new char[file_size]; - - size_t bytes_last_read = 0; // # of bytes read in the last fread() - size_t bytes_read = 0; // # of bytes read so far - - fseek(file, 0, SEEK_SET); - - // Keeps reading the file until we cannot read further or the - // pre-determined file size is reached. - do { - bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file); - bytes_read += bytes_last_read; - } while (bytes_last_read > 0 && bytes_read < file_size); - - const std::string content(buffer, bytes_read); - delete[] buffer; - - return content; -} - -#if GTEST_HAS_DEATH_TEST -static const std::vector* g_injected_test_argvs = - nullptr; // Owned. - -std::vector GetInjectableArgvs() { - if (g_injected_test_argvs != nullptr) { - return *g_injected_test_argvs; - } - return GetArgvs(); -} - -void SetInjectableArgvs(const std::vector* new_argvs) { - if (g_injected_test_argvs != new_argvs) delete g_injected_test_argvs; - g_injected_test_argvs = new_argvs; -} - -void SetInjectableArgvs(const std::vector& new_argvs) { - SetInjectableArgvs( - new std::vector(new_argvs.begin(), new_argvs.end())); -} - -void ClearInjectableArgvs() { - delete g_injected_test_argvs; - g_injected_test_argvs = nullptr; -} -#endif // GTEST_HAS_DEATH_TEST - -#if GTEST_OS_WINDOWS_MOBILE -namespace posix { -void Abort() { - DebugBreak(); - TerminateProcess(GetCurrentProcess(), 1); -} -} // namespace posix -#endif // GTEST_OS_WINDOWS_MOBILE - -// Returns the name of the environment variable corresponding to the -// given flag. For example, FlagToEnvVar("foo") will return -// "GTEST_FOO" in the open-source version. -static std::string FlagToEnvVar(const char* flag) { - const std::string full_flag = - (Message() << GTEST_FLAG_PREFIX_ << flag).GetString(); - - Message env_var; - for (size_t i = 0; i != full_flag.length(); i++) { - env_var << ToUpper(full_flag.c_str()[i]); - } - - return env_var.GetString(); -} - -// Parses 'str' for a 32-bit signed integer. If successful, writes -// the result to *value and returns true; otherwise leaves *value -// unchanged and returns false. -bool ParseInt32(const Message& src_text, const char* str, int32_t* value) { - // Parses the environment variable as a decimal integer. - char* end = nullptr; - const long long_value = strtol(str, &end, 10); // NOLINT - - // Has strtol() consumed all characters in the string? - if (*end != '\0') { - // No - an invalid character was encountered. - Message msg; - msg << "WARNING: " << src_text - << " is expected to be a 32-bit integer, but actually" - << " has value \"" << str << "\".\n"; - printf("%s", msg.GetString().c_str()); - fflush(stdout); - return false; - } - - // Is the parsed value in the range of an int32_t? - const auto result = static_cast(long_value); - if (long_value == LONG_MAX || long_value == LONG_MIN || - // The parsed value overflows as a long. (strtol() returns - // LONG_MAX or LONG_MIN when the input overflows.) - result != long_value - // The parsed value overflows as an int32_t. - ) { - Message msg; - msg << "WARNING: " << src_text - << " is expected to be a 32-bit integer, but actually" - << " has value " << str << ", which overflows.\n"; - printf("%s", msg.GetString().c_str()); - fflush(stdout); - return false; - } - - *value = result; - return true; -} - -// Reads and returns the Boolean environment variable corresponding to -// the given flag; if it's not set, returns default_value. -// -// The value is considered true if and only if it's not "0". -bool BoolFromGTestEnv(const char* flag, bool default_value) { -#if defined(GTEST_GET_BOOL_FROM_ENV_) - return GTEST_GET_BOOL_FROM_ENV_(flag, default_value); -#else - const std::string env_var = FlagToEnvVar(flag); - const char* const string_value = posix::GetEnv(env_var.c_str()); - return string_value == nullptr ? default_value - : strcmp(string_value, "0") != 0; -#endif // defined(GTEST_GET_BOOL_FROM_ENV_) -} - -// Reads and returns a 32-bit integer stored in the environment -// variable corresponding to the given flag; if it isn't set or -// doesn't represent a valid 32-bit integer, returns default_value. -int32_t Int32FromGTestEnv(const char* flag, int32_t default_value) { -#if defined(GTEST_GET_INT32_FROM_ENV_) - return GTEST_GET_INT32_FROM_ENV_(flag, default_value); -#else - const std::string env_var = FlagToEnvVar(flag); - const char* const string_value = posix::GetEnv(env_var.c_str()); - if (string_value == nullptr) { - // The environment variable is not set. - return default_value; - } - - int32_t result = default_value; - if (!ParseInt32(Message() << "Environment variable " << env_var, - string_value, &result)) { - printf("The default value %s is used.\n", - (Message() << default_value).GetString().c_str()); - fflush(stdout); - return default_value; - } - - return result; -#endif // defined(GTEST_GET_INT32_FROM_ENV_) -} - -// As a special case for the 'output' flag, if GTEST_OUTPUT is not -// set, we look for XML_OUTPUT_FILE, which is set by the Bazel build -// system. The value of XML_OUTPUT_FILE is a filename without the -// "xml:" prefix of GTEST_OUTPUT. -// Note that this is meant to be called at the call site so it does -// not check that the flag is 'output' -// In essence this checks an env variable called XML_OUTPUT_FILE -// and if it is set we prepend "xml:" to its value, if it not set we return "" -std::string OutputFlagAlsoCheckEnvVar(){ - std::string default_value_for_output_flag = ""; - const char* xml_output_file_env = posix::GetEnv("XML_OUTPUT_FILE"); - if (nullptr != xml_output_file_env) { - default_value_for_output_flag = std::string("xml:") + xml_output_file_env; - } - return default_value_for_output_flag; -} - -// Reads and returns the string environment variable corresponding to -// the given flag; if it's not set, returns default_value. -const char* StringFromGTestEnv(const char* flag, const char* default_value) { -#if defined(GTEST_GET_STRING_FROM_ENV_) - return GTEST_GET_STRING_FROM_ENV_(flag, default_value); -#else - const std::string env_var = FlagToEnvVar(flag); - const char* const value = posix::GetEnv(env_var.c_str()); - return value == nullptr ? default_value : value; -#endif // defined(GTEST_GET_STRING_FROM_ENV_) -} - -} // namespace internal -} // namespace testing -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Google Test - The Google C++ Testing and Mocking Framework -// -// This file implements a universal value printer that can print a -// value of any type T: -// -// void ::testing::internal::UniversalPrinter::Print(value, ostream_ptr); -// -// It uses the << operator when possible, and prints the bytes in the -// object otherwise. A user can override its behavior for a class -// type Foo by defining either operator<<(::std::ostream&, const Foo&) -// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that -// defines Foo. - - -#include - -#include -#include -#include -#include // NOLINT -#include -#include - - -namespace testing { - -namespace { - -using ::std::ostream; - -// Prints a segment of bytes in the given object. -GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ -GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ -void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, - size_t count, ostream* os) { - char text[5] = ""; - for (size_t i = 0; i != count; i++) { - const size_t j = start + i; - if (i != 0) { - // Organizes the bytes into groups of 2 for easy parsing by - // human. - if ((j % 2) == 0) - *os << ' '; - else - *os << '-'; - } - GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]); - *os << text; - } -} - -// Prints the bytes in the given value to the given ostream. -void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count, - ostream* os) { - // Tells the user how big the object is. - *os << count << "-byte object <"; - - const size_t kThreshold = 132; - const size_t kChunkSize = 64; - // If the object size is bigger than kThreshold, we'll have to omit - // some details by printing only the first and the last kChunkSize - // bytes. - if (count < kThreshold) { - PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); - } else { - PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); - *os << " ... "; - // Rounds up to 2-byte boundary. - const size_t resume_pos = (count - kChunkSize + 1)/2*2; - PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); - } - *os << ">"; -} - -// Helpers for widening a character to char32_t. Since the standard does not -// specify if char / wchar_t is signed or unsigned, it is important to first -// convert it to the unsigned type of the same width before widening it to -// char32_t. -template -char32_t ToChar32(CharType in) { - return static_cast( - static_cast::type>(in)); -} - -} // namespace - -namespace internal { - -// Delegates to PrintBytesInObjectToImpl() to print the bytes in the -// given object. The delegation simplifies the implementation, which -// uses the << operator and thus is easier done outside of the -// ::testing::internal namespace, which contains a << operator that -// sometimes conflicts with the one in STL. -void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, - ostream* os) { - PrintBytesInObjectToImpl(obj_bytes, count, os); -} - -// Depending on the value of a char (or wchar_t), we print it in one -// of three formats: -// - as is if it's a printable ASCII (e.g. 'a', '2', ' '), -// - as a hexadecimal escape sequence (e.g. '\x7F'), or -// - as a special escape sequence (e.g. '\r', '\n'). -enum CharFormat { - kAsIs, - kHexEscape, - kSpecialEscape -}; - -// Returns true if c is a printable ASCII character. We test the -// value of c directly instead of calling isprint(), which is buggy on -// Windows Mobile. -inline bool IsPrintableAscii(char32_t c) { return 0x20 <= c && c <= 0x7E; } - -// Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a -// character literal without the quotes, escaping it when necessary; returns how -// c was formatted. -template -static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) { - const char32_t u_c = ToChar32(c); - switch (u_c) { - case L'\0': - *os << "\\0"; - break; - case L'\'': - *os << "\\'"; - break; - case L'\\': - *os << "\\\\"; - break; - case L'\a': - *os << "\\a"; - break; - case L'\b': - *os << "\\b"; - break; - case L'\f': - *os << "\\f"; - break; - case L'\n': - *os << "\\n"; - break; - case L'\r': - *os << "\\r"; - break; - case L'\t': - *os << "\\t"; - break; - case L'\v': - *os << "\\v"; - break; - default: - if (IsPrintableAscii(u_c)) { - *os << static_cast(c); - return kAsIs; - } else { - ostream::fmtflags flags = os->flags(); - *os << "\\x" << std::hex << std::uppercase << static_cast(u_c); - os->flags(flags); - return kHexEscape; - } - } - return kSpecialEscape; -} - -// Prints a char32_t c as if it's part of a string literal, escaping it when -// necessary; returns how c was formatted. -static CharFormat PrintAsStringLiteralTo(char32_t c, ostream* os) { - switch (c) { - case L'\'': - *os << "'"; - return kAsIs; - case L'"': - *os << "\\\""; - return kSpecialEscape; - default: - return PrintAsCharLiteralTo(c, os); - } -} - -static const char* GetCharWidthPrefix(char) { - return ""; -} - -static const char* GetCharWidthPrefix(signed char) { - return ""; -} - -static const char* GetCharWidthPrefix(unsigned char) { - return ""; -} - -#ifdef __cpp_char8_t -static const char* GetCharWidthPrefix(char8_t) { - return "u8"; -} -#endif - -static const char* GetCharWidthPrefix(char16_t) { - return "u"; -} - -static const char* GetCharWidthPrefix(char32_t) { - return "U"; -} - -static const char* GetCharWidthPrefix(wchar_t) { - return "L"; -} - -// Prints a char c as if it's part of a string literal, escaping it when -// necessary; returns how c was formatted. -static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { - return PrintAsStringLiteralTo(ToChar32(c), os); -} - -#ifdef __cpp_char8_t -static CharFormat PrintAsStringLiteralTo(char8_t c, ostream* os) { - return PrintAsStringLiteralTo(ToChar32(c), os); -} -#endif - -static CharFormat PrintAsStringLiteralTo(char16_t c, ostream* os) { - return PrintAsStringLiteralTo(ToChar32(c), os); -} - -static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) { - return PrintAsStringLiteralTo(ToChar32(c), os); -} - -// Prints a character c (of type char, char8_t, char16_t, char32_t, or wchar_t) -// and its code. '\0' is printed as "'\\0'", other unprintable characters are -// also properly escaped using the standard C++ escape sequence. -template -void PrintCharAndCodeTo(Char c, ostream* os) { - // First, print c as a literal in the most readable form we can find. - *os << GetCharWidthPrefix(c) << "'"; - const CharFormat format = PrintAsCharLiteralTo(c, os); - *os << "'"; - - // To aid user debugging, we also print c's code in decimal, unless - // it's 0 (in which case c was printed as '\\0', making the code - // obvious). - if (c == 0) - return; - *os << " (" << static_cast(c); - - // For more convenience, we print c's code again in hexadecimal, - // unless c was already printed in the form '\x##' or the code is in - // [1, 9]. - if (format == kHexEscape || (1 <= c && c <= 9)) { - // Do nothing. - } else { - *os << ", 0x" << String::FormatHexInt(static_cast(c)); - } - *os << ")"; -} - -void PrintTo(unsigned char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); } -void PrintTo(signed char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); } - -// Prints a wchar_t as a symbol if it is printable or as its internal -// code otherwise and also as its code. L'\0' is printed as "L'\\0'". -void PrintTo(wchar_t wc, ostream* os) { PrintCharAndCodeTo(wc, os); } - -// TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well. -void PrintTo(char32_t c, ::std::ostream* os) { - *os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4) - << static_cast(c); -} - -// Prints the given array of characters to the ostream. CharType must be either -// char, char8_t, char16_t, char32_t, or wchar_t. -// The array starts at begin, the length is len, it may include '\0' characters -// and may not be NUL-terminated. -template -GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ -GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ -static CharFormat PrintCharsAsStringTo( - const CharType* begin, size_t len, ostream* os) { - const char* const quote_prefix = GetCharWidthPrefix(*begin); - *os << quote_prefix << "\""; - bool is_previous_hex = false; - CharFormat print_format = kAsIs; - for (size_t index = 0; index < len; ++index) { - const CharType cur = begin[index]; - if (is_previous_hex && IsXDigit(cur)) { - // Previous character is of '\x..' form and this character can be - // interpreted as another hexadecimal digit in its number. Break string to - // disambiguate. - *os << "\" " << quote_prefix << "\""; - } - is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape; - // Remember if any characters required hex escaping. - if (is_previous_hex) { - print_format = kHexEscape; - } - } - *os << "\""; - return print_format; -} - -// Prints a (const) char/wchar_t array of 'len' elements, starting at address -// 'begin'. CharType must be either char or wchar_t. -template -GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ -GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ -static void UniversalPrintCharArray( - const CharType* begin, size_t len, ostream* os) { - // The code - // const char kFoo[] = "foo"; - // generates an array of 4, not 3, elements, with the last one being '\0'. - // - // Therefore when printing a char array, we don't print the last element if - // it's '\0', such that the output matches the string literal as it's - // written in the source code. - if (len > 0 && begin[len - 1] == '\0') { - PrintCharsAsStringTo(begin, len - 1, os); - return; - } - - // If, however, the last element in the array is not '\0', e.g. - // const char kFoo[] = { 'f', 'o', 'o' }; - // we must print the entire array. We also print a message to indicate - // that the array is not NUL-terminated. - PrintCharsAsStringTo(begin, len, os); - *os << " (no terminating NUL)"; -} - -// Prints a (const) char array of 'len' elements, starting at address 'begin'. -void UniversalPrintArray(const char* begin, size_t len, ostream* os) { - UniversalPrintCharArray(begin, len, os); -} - -#ifdef __cpp_char8_t -// Prints a (const) char8_t array of 'len' elements, starting at address -// 'begin'. -void UniversalPrintArray(const char8_t* begin, size_t len, ostream* os) { - UniversalPrintCharArray(begin, len, os); -} -#endif - -// Prints a (const) char16_t array of 'len' elements, starting at address -// 'begin'. -void UniversalPrintArray(const char16_t* begin, size_t len, ostream* os) { - UniversalPrintCharArray(begin, len, os); -} - -// Prints a (const) char32_t array of 'len' elements, starting at address -// 'begin'. -void UniversalPrintArray(const char32_t* begin, size_t len, ostream* os) { - UniversalPrintCharArray(begin, len, os); -} - -// Prints a (const) wchar_t array of 'len' elements, starting at address -// 'begin'. -void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) { - UniversalPrintCharArray(begin, len, os); -} - -namespace { - -// Prints a null-terminated C-style string to the ostream. -template -void PrintCStringTo(const Char* s, ostream* os) { - if (s == nullptr) { - *os << "NULL"; - } else { - *os << ImplicitCast_(s) << " pointing to "; - PrintCharsAsStringTo(s, std::char_traits::length(s), os); - } -} - -} // anonymous namespace - -void PrintTo(const char* s, ostream* os) { PrintCStringTo(s, os); } - -#ifdef __cpp_char8_t -void PrintTo(const char8_t* s, ostream* os) { PrintCStringTo(s, os); } -#endif - -void PrintTo(const char16_t* s, ostream* os) { PrintCStringTo(s, os); } - -void PrintTo(const char32_t* s, ostream* os) { PrintCStringTo(s, os); } - -// MSVC compiler can be configured to define whar_t as a typedef -// of unsigned short. Defining an overload for const wchar_t* in that case -// would cause pointers to unsigned shorts be printed as wide strings, -// possibly accessing more memory than intended and causing invalid -// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when -// wchar_t is implemented as a native type. -#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) -// Prints the given wide C string to the ostream. -void PrintTo(const wchar_t* s, ostream* os) { PrintCStringTo(s, os); } -#endif // wchar_t is native - -namespace { - -bool ContainsUnprintableControlCodes(const char* str, size_t length) { - const unsigned char *s = reinterpret_cast(str); - - for (size_t i = 0; i < length; i++) { - unsigned char ch = *s++; - if (std::iscntrl(ch)) { - switch (ch) { - case '\t': - case '\n': - case '\r': - break; - default: - return true; - } - } - } - return false; -} - -bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t<= 0xbf; } - -bool IsValidUTF8(const char* str, size_t length) { - const unsigned char *s = reinterpret_cast(str); - - for (size_t i = 0; i < length;) { - unsigned char lead = s[i++]; - - if (lead <= 0x7f) { - continue; // single-byte character (ASCII) 0..7F - } - if (lead < 0xc2) { - return false; // trail byte or non-shortest form - } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) { - ++i; // 2-byte character - } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length && - IsUTF8TrailByte(s[i]) && - IsUTF8TrailByte(s[i + 1]) && - // check for non-shortest form and surrogate - (lead != 0xe0 || s[i] >= 0xa0) && - (lead != 0xed || s[i] < 0xa0)) { - i += 2; // 3-byte character - } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length && - IsUTF8TrailByte(s[i]) && - IsUTF8TrailByte(s[i + 1]) && - IsUTF8TrailByte(s[i + 2]) && - // check for non-shortest form - (lead != 0xf0 || s[i] >= 0x90) && - (lead != 0xf4 || s[i] < 0x90)) { - i += 3; // 4-byte character - } else { - return false; - } - } - return true; -} - -void ConditionalPrintAsText(const char* str, size_t length, ostream* os) { - if (!ContainsUnprintableControlCodes(str, length) && - IsValidUTF8(str, length)) { - *os << "\n As Text: \"" << str << "\""; - } -} - -} // anonymous namespace - -void PrintStringTo(const ::std::string& s, ostream* os) { - if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) { - if (GTEST_FLAG(print_utf8)) { - ConditionalPrintAsText(s.data(), s.size(), os); - } - } -} - -#ifdef __cpp_char8_t -void PrintU8StringTo(const ::std::u8string& s, ostream* os) { - PrintCharsAsStringTo(s.data(), s.size(), os); -} -#endif - -void PrintU16StringTo(const ::std::u16string& s, ostream* os) { - PrintCharsAsStringTo(s.data(), s.size(), os); -} - -void PrintU32StringTo(const ::std::u32string& s, ostream* os) { - PrintCharsAsStringTo(s.data(), s.size(), os); -} - -#if GTEST_HAS_STD_WSTRING -void PrintWideStringTo(const ::std::wstring& s, ostream* os) { - PrintCharsAsStringTo(s.data(), s.size(), os); -} -#endif // GTEST_HAS_STD_WSTRING - -} // namespace internal - -} // namespace testing -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// The Google C++ Testing and Mocking Framework (Google Test) - - - -namespace testing { - -using internal::GetUnitTestImpl; - -// Gets the summary of the failure message by omitting the stack trace -// in it. -std::string TestPartResult::ExtractSummary(const char* message) { - const char* const stack_trace = strstr(message, internal::kStackTraceMarker); - return stack_trace == nullptr ? message : std::string(message, stack_trace); -} - -// Prints a TestPartResult object. -std::ostream& operator<<(std::ostream& os, const TestPartResult& result) { - return os << internal::FormatFileLocation(result.file_name(), - result.line_number()) - << " " - << (result.type() == TestPartResult::kSuccess - ? "Success" - : result.type() == TestPartResult::kSkip - ? "Skipped" - : result.type() == TestPartResult::kFatalFailure - ? "Fatal failure" - : "Non-fatal failure") - << ":\n" - << result.message() << std::endl; -} - -// Appends a TestPartResult to the array. -void TestPartResultArray::Append(const TestPartResult& result) { - array_.push_back(result); -} - -// Returns the TestPartResult at the given index (0-based). -const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const { - if (index < 0 || index >= size()) { - printf("\nInvalid index (%d) into TestPartResultArray.\n", index); - internal::posix::Abort(); - } - - return array_[static_cast(index)]; -} - -// Returns the number of TestPartResult objects in the array. -int TestPartResultArray::size() const { - return static_cast(array_.size()); -} - -namespace internal { - -HasNewFatalFailureHelper::HasNewFatalFailureHelper() - : has_new_fatal_failure_(false), - original_reporter_(GetUnitTestImpl()-> - GetTestPartResultReporterForCurrentThread()) { - GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this); -} - -HasNewFatalFailureHelper::~HasNewFatalFailureHelper() { - GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread( - original_reporter_); -} - -void HasNewFatalFailureHelper::ReportTestPartResult( - const TestPartResult& result) { - if (result.fatally_failed()) - has_new_fatal_failure_ = true; - original_reporter_->ReportTestPartResult(result); -} - -} // namespace internal - -} // namespace testing -// Copyright 2008 Google Inc. -// All Rights Reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - - - -namespace testing { -namespace internal { - -// Skips to the first non-space char in str. Returns an empty string if str -// contains only whitespace characters. -static const char* SkipSpaces(const char* str) { - while (IsSpace(*str)) - str++; - return str; -} - -static std::vector SplitIntoTestNames(const char* src) { - std::vector name_vec; - src = SkipSpaces(src); - for (; src != nullptr; src = SkipComma(src)) { - name_vec.push_back(StripTrailingSpaces(GetPrefixUntilComma(src))); - } - return name_vec; -} - -// Verifies that registered_tests match the test names in -// registered_tests_; returns registered_tests if successful, or -// aborts the program otherwise. -const char* TypedTestSuitePState::VerifyRegisteredTestNames( - const char* test_suite_name, const char* file, int line, - const char* registered_tests) { - RegisterTypeParameterizedTestSuite(test_suite_name, CodeLocation(file, line)); - - typedef RegisteredTestsMap::const_iterator RegisteredTestIter; - registered_ = true; - - std::vector name_vec = SplitIntoTestNames(registered_tests); - - Message errors; - - std::set tests; - for (std::vector::const_iterator name_it = name_vec.begin(); - name_it != name_vec.end(); ++name_it) { - const std::string& name = *name_it; - if (tests.count(name) != 0) { - errors << "Test " << name << " is listed more than once.\n"; - continue; - } - - if (registered_tests_.count(name) != 0) { - tests.insert(name); - } else { - errors << "No test named " << name - << " can be found in this test suite.\n"; - } - } - - for (RegisteredTestIter it = registered_tests_.begin(); - it != registered_tests_.end(); - ++it) { - if (tests.count(it->first) == 0) { - errors << "You forgot to list test " << it->first << ".\n"; - } - } - - const std::string& errors_str = errors.GetString(); - if (errors_str != "") { - fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(), - errors_str.c_str()); - fflush(stderr); - posix::Abort(); - } - - return registered_tests; -} - -} // namespace internal -} // namespace testing diff --git a/test/gtest/gtest.h b/test/gtest/gtest.h deleted file mode 100644 index e7490573ac..0000000000 --- a/test/gtest/gtest.h +++ /dev/null @@ -1,12377 +0,0 @@ -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file defines the public API for Google Test. It should be -// included by any test program that uses Google Test. -// -// IMPORTANT NOTE: Due to limitation of the C++ language, we have to -// leave some internal implementation details in this header file. -// They are clearly marked by comments like this: -// -// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -// -// Such code is NOT meant to be used by a user directly, and is subject -// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user -// program! -// -// Acknowledgment: Google Test borrowed the idea of automatic test -// registration from Barthelemy Dagenais' (barthelemy@prologique.com) -// easyUnit framework. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_H_ - -#include -#include -#include -#include -#include -#include - -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file declares functions and macros used internally by -// Google Test. They are subject to change without notice. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ - -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Low-level types and utilities for porting Google Test to various -// platforms. All macros ending with _ and symbols defined in an -// internal namespace are subject to change without notice. Code -// outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't -// end with _ are part of Google Test's public API and can be used by -// code outside Google Test. -// -// This file is fundamental to Google Test. All other Google Test source -// files are expected to #include this. Therefore, it cannot #include -// any other Google Test header. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ - -// Environment-describing macros -// ----------------------------- -// -// Google Test can be used in many different environments. Macros in -// this section tell Google Test what kind of environment it is being -// used in, such that Google Test can provide environment-specific -// features and implementations. -// -// Google Test tries to automatically detect the properties of its -// environment, so users usually don't need to worry about these -// macros. However, the automatic detection is not perfect. -// Sometimes it's necessary for a user to define some of the following -// macros in the build script to override Google Test's decisions. -// -// If the user doesn't define a macro in the list, Google Test will -// provide a default definition. After this header is #included, all -// macros in this list will be defined to either 1 or 0. -// -// Notes to maintainers: -// - Each macro here is a user-tweakable knob; do not grow the list -// lightly. -// - Use #if to key off these macros. Don't use #ifdef or "#if -// defined(...)", which will not work as these macros are ALWAYS -// defined. -// -// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) -// is/isn't available. -// GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions -// are enabled. -// GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular -// expressions are/aren't available. -// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that -// is/isn't available. -// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't -// enabled. -// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that -// std::wstring does/doesn't work (Google Test can -// be used where std::wstring is unavailable). -// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the -// compiler supports Microsoft's "Structured -// Exception Handling". -// GTEST_HAS_STREAM_REDIRECTION -// - Define it to 1/0 to indicate whether the -// platform supports I/O stream redirection using -// dup() and dup2(). -// GTEST_LINKED_AS_SHARED_LIBRARY -// - Define to 1 when compiling tests that use -// Google Test as a shared library (known as -// DLL on Windows). -// GTEST_CREATE_SHARED_LIBRARY -// - Define to 1 when compiling Google Test itself -// as a shared library. -// GTEST_DEFAULT_DEATH_TEST_STYLE -// - The default value of --gtest_death_test_style. -// The legacy default has been "fast" in the open -// source version since 2008. The recommended value -// is "threadsafe", and can be set in -// custom/gtest-port.h. - -// Platform-indicating macros -// -------------------------- -// -// Macros indicating the platform on which Google Test is being used -// (a macro is defined to 1 if compiled on the given platform; -// otherwise UNDEFINED -- it's never defined to 0.). Google Test -// defines these macros automatically. Code outside Google Test MUST -// NOT define them. -// -// GTEST_OS_AIX - IBM AIX -// GTEST_OS_CYGWIN - Cygwin -// GTEST_OS_DRAGONFLY - DragonFlyBSD -// GTEST_OS_FREEBSD - FreeBSD -// GTEST_OS_FUCHSIA - Fuchsia -// GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD -// GTEST_OS_HAIKU - Haiku -// GTEST_OS_HPUX - HP-UX -// GTEST_OS_LINUX - Linux -// GTEST_OS_LINUX_ANDROID - Google Android -// GTEST_OS_MAC - Mac OS X -// GTEST_OS_IOS - iOS -// GTEST_OS_NACL - Google Native Client (NaCl) -// GTEST_OS_NETBSD - NetBSD -// GTEST_OS_OPENBSD - OpenBSD -// GTEST_OS_OS2 - OS/2 -// GTEST_OS_QNX - QNX -// GTEST_OS_SOLARIS - Sun Solaris -// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) -// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop -// GTEST_OS_WINDOWS_MINGW - MinGW -// GTEST_OS_WINDOWS_MOBILE - Windows Mobile -// GTEST_OS_WINDOWS_PHONE - Windows Phone -// GTEST_OS_WINDOWS_RT - Windows Store App/WinRT -// GTEST_OS_ZOS - z/OS -// -// Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the -// most stable support. Since core members of the Google Test project -// don't have access to other platforms, support for them may be less -// stable. If you notice any problems on your platform, please notify -// googletestframework@googlegroups.com (patches for fixing them are -// even more welcome!). -// -// It is possible that none of the GTEST_OS_* macros are defined. - -// Feature-indicating macros -// ------------------------- -// -// Macros indicating which Google Test features are available (a macro -// is defined to 1 if the corresponding feature is supported; -// otherwise UNDEFINED -- it's never defined to 0.). Google Test -// defines these macros automatically. Code outside Google Test MUST -// NOT define them. -// -// These macros are public so that portable tests can be written. -// Such tests typically surround code using a feature with an #if -// which controls that code. For example: -// -// #if GTEST_HAS_DEATH_TEST -// EXPECT_DEATH(DoSomethingDeadly()); -// #endif -// -// GTEST_HAS_DEATH_TEST - death tests -// GTEST_HAS_TYPED_TEST - typed tests -// GTEST_HAS_TYPED_TEST_P - type-parameterized tests -// GTEST_IS_THREADSAFE - Google Test is thread-safe. -// GOOGLETEST_CM0007 DO NOT DELETE -// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with -// GTEST_HAS_POSIX_RE (see above) which users can -// define themselves. -// GTEST_USES_SIMPLE_RE - our own simple regex is used; -// the above RE\b(s) are mutually exclusive. - -// Misc public macros -// ------------------ -// -// GTEST_FLAG(flag_name) - references the variable corresponding to -// the given Google Test flag. - -// Internal utilities -// ------------------ -// -// The following macros and utilities are for Google Test's INTERNAL -// use only. Code outside Google Test MUST NOT USE THEM DIRECTLY. -// -// Macros for basic C++ coding: -// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. -// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a -// variable don't have to be used. -// GTEST_DISALLOW_ASSIGN_ - disables copy operator=. -// GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=. -// GTEST_DISALLOW_MOVE_ASSIGN_ - disables move operator=. -// GTEST_DISALLOW_MOVE_AND_ASSIGN_ - disables move ctor and operator=. -// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. -// GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is -// suppressed (constant conditional). -// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 -// is suppressed. -// GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter or -// UniversalPrinter specializations. -// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter -// or -// UniversalPrinter -// specializations. -// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher or -// Matcher -// specializations. -// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter or -// UniversalPrinter -// specializations. -// -// Synchronization: -// Mutex, MutexLock, ThreadLocal, GetThreadCount() -// - synchronization primitives. -// -// Regular expressions: -// RE - a simple regular expression class using the POSIX -// Extended Regular Expression syntax on UNIX-like platforms -// GOOGLETEST_CM0008 DO NOT DELETE -// or a reduced regular exception syntax on other -// platforms, including Windows. -// Logging: -// GTEST_LOG_() - logs messages at the specified severity level. -// LogToStderr() - directs all log messages to stderr. -// FlushInfoLog() - flushes informational log messages. -// -// Stdout and stderr capturing: -// CaptureStdout() - starts capturing stdout. -// GetCapturedStdout() - stops capturing stdout and returns the captured -// string. -// CaptureStderr() - starts capturing stderr. -// GetCapturedStderr() - stops capturing stderr and returns the captured -// string. -// -// Integer types: -// TypeWithSize - maps an integer to a int type. -// TimeInMillis - integers of known sizes. -// BiggestInt - the biggest signed integer type. -// -// Command-line utilities: -// GTEST_DECLARE_*() - declares a flag. -// GTEST_DEFINE_*() - defines a flag. -// GetInjectableArgvs() - returns the command line as a vector of strings. -// -// Environment variable utilities: -// GetEnv() - gets the value of an environment variable. -// BoolFromGTestEnv() - parses a bool environment variable. -// Int32FromGTestEnv() - parses an int32_t environment variable. -// StringFromGTestEnv() - parses a string environment variable. -// -// Deprecation warnings: -// GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as -// deprecated; calling a marked function -// should generate a compiler warning - -#include // for isspace, etc -#include // for ptrdiff_t -#include -#include -#include - -#include -#include -#include -#include - -#ifndef _WIN32_WCE -# include -# include -#endif // !_WIN32_WCE - -#if defined __APPLE__ -# include -# include -#endif - -#include // NOLINT -#include -#include -#include // NOLINT -#include -#include // NOLINT - -// Copyright 2015, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Injection point for custom user configurations. See README for details -// -// ** Custom implementation starts here ** - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_ - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_ -// Copyright 2015, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file defines the GTEST_OS_* macro. -// It is separate from gtest-port.h so that custom/gtest-port.h can include it. - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ - -// Determines the platform on which Google Test is compiled. -#ifdef __CYGWIN__ -# define GTEST_OS_CYGWIN 1 -# elif defined(__MINGW__) || defined(__MINGW32__) || defined(__MINGW64__) -# define GTEST_OS_WINDOWS_MINGW 1 -# define GTEST_OS_WINDOWS 1 -#elif defined _WIN32 -# define GTEST_OS_WINDOWS 1 -# ifdef _WIN32_WCE -# define GTEST_OS_WINDOWS_MOBILE 1 -# elif defined(WINAPI_FAMILY) -# include -# if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) -# define GTEST_OS_WINDOWS_DESKTOP 1 -# elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP) -# define GTEST_OS_WINDOWS_PHONE 1 -# elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) -# define GTEST_OS_WINDOWS_RT 1 -# elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_TV_TITLE) -# define GTEST_OS_WINDOWS_PHONE 1 -# define GTEST_OS_WINDOWS_TV_TITLE 1 -# else - // WINAPI_FAMILY defined but no known partition matched. - // Default to desktop. -# define GTEST_OS_WINDOWS_DESKTOP 1 -# endif -# else -# define GTEST_OS_WINDOWS_DESKTOP 1 -# endif // _WIN32_WCE -#elif defined __OS2__ -# define GTEST_OS_OS2 1 -#elif defined __APPLE__ -# define GTEST_OS_MAC 1 -# include -# if TARGET_OS_IPHONE -# define GTEST_OS_IOS 1 -# endif -#elif defined __DragonFly__ -# define GTEST_OS_DRAGONFLY 1 -#elif defined __FreeBSD__ -# define GTEST_OS_FREEBSD 1 -#elif defined __Fuchsia__ -# define GTEST_OS_FUCHSIA 1 -#elif defined(__GLIBC__) && defined(__FreeBSD_kernel__) -# define GTEST_OS_GNU_KFREEBSD 1 -#elif defined __linux__ -# define GTEST_OS_LINUX 1 -# if defined __ANDROID__ -# define GTEST_OS_LINUX_ANDROID 1 -# endif -#elif defined __MVS__ -# define GTEST_OS_ZOS 1 -#elif defined(__sun) && defined(__SVR4) -# define GTEST_OS_SOLARIS 1 -#elif defined(_AIX) -# define GTEST_OS_AIX 1 -#elif defined(__hpux) -# define GTEST_OS_HPUX 1 -#elif defined __native_client__ -# define GTEST_OS_NACL 1 -#elif defined __NetBSD__ -# define GTEST_OS_NETBSD 1 -#elif defined __OpenBSD__ -# define GTEST_OS_OPENBSD 1 -#elif defined __QNX__ -# define GTEST_OS_QNX 1 -#elif defined(__HAIKU__) -#define GTEST_OS_HAIKU 1 -#elif defined ESP8266 -#define GTEST_OS_ESP8266 1 -#elif defined ESP32 -#define GTEST_OS_ESP32 1 -#elif defined(__XTENSA__) -#define GTEST_OS_XTENSA 1 -#endif // __CYGWIN__ - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ - -#if !defined(GTEST_DEV_EMAIL_) -# define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" -# define GTEST_FLAG_PREFIX_ "gtest_" -# define GTEST_FLAG_PREFIX_DASH_ "gtest-" -# define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" -# define GTEST_NAME_ "Google Test" -# define GTEST_PROJECT_URL_ "https://github.com/google/googletest/" -#endif // !defined(GTEST_DEV_EMAIL_) - -#if !defined(GTEST_INIT_GOOGLE_TEST_NAME_) -# define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest" -#endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_) - -// Determines the version of gcc that is used to compile this. -#ifdef __GNUC__ -// 40302 means version 4.3.2. -# define GTEST_GCC_VER_ \ - (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__) -#endif // __GNUC__ - -// Macros for disabling Microsoft Visual C++ warnings. -// -// GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385) -// /* code that triggers warnings C4800 and C4385 */ -// GTEST_DISABLE_MSC_WARNINGS_POP_() -#if defined(_MSC_VER) -# define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \ - __pragma(warning(push)) \ - __pragma(warning(disable: warnings)) -# define GTEST_DISABLE_MSC_WARNINGS_POP_() \ - __pragma(warning(pop)) -#else -// Not all compilers are MSVC -# define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) -# define GTEST_DISABLE_MSC_WARNINGS_POP_() -#endif - -// Clang on Windows does not understand MSVC's pragma warning. -// We need clang-specific way to disable function deprecation warning. -#ifdef __clang__ -# define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \ - _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"") -#define GTEST_DISABLE_MSC_DEPRECATED_POP_() \ - _Pragma("clang diagnostic pop") -#else -# define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996) -# define GTEST_DISABLE_MSC_DEPRECATED_POP_() \ - GTEST_DISABLE_MSC_WARNINGS_POP_() -#endif - -// Brings in definitions for functions used in the testing::internal::posix -// namespace (read, write, close, chdir, isatty, stat). We do not currently -// use them on Windows Mobile. -#if GTEST_OS_WINDOWS -# if !GTEST_OS_WINDOWS_MOBILE -# include -# include -# endif -// In order to avoid having to include , use forward declaration -#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR) -// MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two -// separate (equivalent) structs, instead of using typedef -typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION; -#else -// Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION. -// This assumption is verified by -// WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION. -typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; -#endif -#elif GTEST_OS_XTENSA -#include -// Xtensa toolchains define strcasecmp in the string.h header instead of -// strings.h. string.h is already included. -#else -// This assumes that non-Windows OSes provide unistd.h. For OSes where this -// is not the case, we need to include headers that provide the functions -// mentioned above. -# include -# include -#endif // GTEST_OS_WINDOWS - -#if GTEST_OS_LINUX_ANDROID -// Used to define __ANDROID_API__ matching the target NDK API level. -# include // NOLINT -#endif - -// Defines this to true if and only if Google Test can use POSIX regular -// expressions. -#ifndef GTEST_HAS_POSIX_RE -# if GTEST_OS_LINUX_ANDROID -// On Android, is only available starting with Gingerbread. -# define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9) -# else -#define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS && !GTEST_OS_XTENSA) -# endif -#endif - -#if GTEST_USES_PCRE -// The appropriate headers have already been included. - -#elif GTEST_HAS_POSIX_RE - -// On some platforms, needs someone to define size_t, and -// won't compile otherwise. We can #include it here as we already -// included , which is guaranteed to define size_t through -// . -# include // NOLINT - -# define GTEST_USES_POSIX_RE 1 - -#elif GTEST_OS_WINDOWS - -// is not available on Windows. Use our own simple regex -// implementation instead. -# define GTEST_USES_SIMPLE_RE 1 - -#else - -// may not be available on this platform. Use our own -// simple regex implementation instead. -# define GTEST_USES_SIMPLE_RE 1 - -#endif // GTEST_USES_PCRE - -#ifndef GTEST_HAS_EXCEPTIONS -// The user didn't tell us whether exceptions are enabled, so we need -// to figure it out. -# if defined(_MSC_VER) && defined(_CPPUNWIND) -// MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled. -# define GTEST_HAS_EXCEPTIONS 1 -# elif defined(__BORLANDC__) -// C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS -// macro to enable exceptions, so we'll do the same. -// Assumes that exceptions are enabled by default. -# ifndef _HAS_EXCEPTIONS -# define _HAS_EXCEPTIONS 1 -# endif // _HAS_EXCEPTIONS -# define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS -# elif defined(__clang__) -// clang defines __EXCEPTIONS if and only if exceptions are enabled before clang -// 220714, but if and only if cleanups are enabled after that. In Obj-C++ files, -// there can be cleanups for ObjC exceptions which also need cleanups, even if -// C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which -// checks for C++ exceptions starting at clang r206352, but which checked for -// cleanups prior to that. To reliably check for C++ exception availability with -// clang, check for -// __EXCEPTIONS && __has_feature(cxx_exceptions). -# define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions)) -# elif defined(__GNUC__) && __EXCEPTIONS -// gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled. -# define GTEST_HAS_EXCEPTIONS 1 -# elif defined(__SUNPRO_CC) -// Sun Pro CC supports exceptions. However, there is no compile-time way of -// detecting whether they are enabled or not. Therefore, we assume that -// they are enabled unless the user tells us otherwise. -# define GTEST_HAS_EXCEPTIONS 1 -# elif defined(__IBMCPP__) && __EXCEPTIONS -// xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled. -# define GTEST_HAS_EXCEPTIONS 1 -# elif defined(__HP_aCC) -// Exception handling is in effect by default in HP aCC compiler. It has to -// be turned of by +noeh compiler option if desired. -# define GTEST_HAS_EXCEPTIONS 1 -# else -// For other compilers, we assume exceptions are disabled to be -// conservative. -# define GTEST_HAS_EXCEPTIONS 0 -# endif // defined(_MSC_VER) || defined(__BORLANDC__) -#endif // GTEST_HAS_EXCEPTIONS - -#ifndef GTEST_HAS_STD_WSTRING -// The user didn't tell us whether ::std::wstring is available, so we need -// to figure it out. -// Cygwin 1.7 and below doesn't support ::std::wstring. -// Solaris' libc++ doesn't support it either. Android has -// no support for it at least as recent as Froyo (2.2). -#define GTEST_HAS_STD_WSTRING \ - (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ - GTEST_OS_HAIKU || GTEST_OS_ESP32 || GTEST_OS_ESP8266 || GTEST_OS_XTENSA)) - -#endif // GTEST_HAS_STD_WSTRING - -// Determines whether RTTI is available. -#ifndef GTEST_HAS_RTTI -// The user didn't tell us whether RTTI is enabled, so we need to -// figure it out. - -# ifdef _MSC_VER - -#ifdef _CPPRTTI // MSVC defines this macro if and only if RTTI is enabled. -# define GTEST_HAS_RTTI 1 -# else -# define GTEST_HAS_RTTI 0 -# endif - -// Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is -// enabled. -# elif defined(__GNUC__) - -# ifdef __GXX_RTTI -// When building against STLport with the Android NDK and with -// -frtti -fno-exceptions, the build fails at link time with undefined -// references to __cxa_bad_typeid. Note sure if STL or toolchain bug, -// so disable RTTI when detected. -# if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \ - !defined(__EXCEPTIONS) -# define GTEST_HAS_RTTI 0 -# else -# define GTEST_HAS_RTTI 1 -# endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS -# else -# define GTEST_HAS_RTTI 0 -# endif // __GXX_RTTI - -// Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends -// using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the -// first version with C++ support. -# elif defined(__clang__) - -# define GTEST_HAS_RTTI __has_feature(cxx_rtti) - -// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if -// both the typeid and dynamic_cast features are present. -# elif defined(__IBMCPP__) && (__IBMCPP__ >= 900) - -# ifdef __RTTI_ALL__ -# define GTEST_HAS_RTTI 1 -# else -# define GTEST_HAS_RTTI 0 -# endif - -# else - -// For all other compilers, we assume RTTI is enabled. -# define GTEST_HAS_RTTI 1 - -# endif // _MSC_VER - -#endif // GTEST_HAS_RTTI - -// It's this header's responsibility to #include when RTTI -// is enabled. -#if GTEST_HAS_RTTI -# include -#endif - -// Determines whether Google Test can use the pthreads library. -#ifndef GTEST_HAS_PTHREAD -// The user didn't tell us explicitly, so we make reasonable assumptions about -// which platforms have pthreads support. -// -// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 -// to your compiler flags. -#define GTEST_HAS_PTHREAD \ - (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \ - GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \ - GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD || \ - GTEST_OS_HAIKU) -#endif // GTEST_HAS_PTHREAD - -#if GTEST_HAS_PTHREAD -// gtest-port.h guarantees to #include when GTEST_HAS_PTHREAD is -// true. -# include // NOLINT - -// For timespec and nanosleep, used below. -# include // NOLINT -#endif - -// Determines whether clone(2) is supported. -// Usually it will only be available on Linux, excluding -// Linux on the Itanium architecture. -// Also see http://linux.die.net/man/2/clone. -#ifndef GTEST_HAS_CLONE -// The user didn't tell us, so we need to figure it out. - -# if GTEST_OS_LINUX && !defined(__ia64__) -# if GTEST_OS_LINUX_ANDROID -// On Android, clone() became available at different API levels for each 32-bit -// architecture. -# if defined(__LP64__) || \ - (defined(__arm__) && __ANDROID_API__ >= 9) || \ - (defined(__mips__) && __ANDROID_API__ >= 12) || \ - (defined(__i386__) && __ANDROID_API__ >= 17) -# define GTEST_HAS_CLONE 1 -# else -# define GTEST_HAS_CLONE 0 -# endif -# else -# define GTEST_HAS_CLONE 1 -# endif -# else -# define GTEST_HAS_CLONE 0 -# endif // GTEST_OS_LINUX && !defined(__ia64__) - -#endif // GTEST_HAS_CLONE - -// Determines whether to support stream redirection. This is used to test -// output correctness and to implement death tests. -#ifndef GTEST_HAS_STREAM_REDIRECTION -// By default, we assume that stream redirection is supported on all -// platforms except known mobile ones. -#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ - GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA -# define GTEST_HAS_STREAM_REDIRECTION 0 -# else -# define GTEST_HAS_STREAM_REDIRECTION 1 -# endif // !GTEST_OS_WINDOWS_MOBILE -#endif // GTEST_HAS_STREAM_REDIRECTION - -// Determines whether to support death tests. -// pops up a dialog window that cannot be suppressed programmatically. -#if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ - (GTEST_OS_MAC && !GTEST_OS_IOS) || \ - (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || GTEST_OS_WINDOWS_MINGW || \ - GTEST_OS_AIX || GTEST_OS_HPUX || GTEST_OS_OPENBSD || GTEST_OS_QNX || \ - GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \ - GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU) -# define GTEST_HAS_DEATH_TEST 1 -#endif - -// Determines whether to support type-driven tests. - -// Typed tests need and variadic macros, which GCC, VC++ 8.0, -// Sun Pro CC, IBM Visual Age, and HP aCC support. -#if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \ - defined(__IBMCPP__) || defined(__HP_aCC) -# define GTEST_HAS_TYPED_TEST 1 -# define GTEST_HAS_TYPED_TEST_P 1 -#endif - -// Determines whether the system compiler uses UTF-16 for encoding wide strings. -#define GTEST_WIDE_STRING_USES_UTF16_ \ - (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_AIX || GTEST_OS_OS2) - -// Determines whether test results can be streamed to a socket. -#if GTEST_OS_LINUX || GTEST_OS_GNU_KFREEBSD || GTEST_OS_DRAGONFLY || \ - GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_OPENBSD -# define GTEST_CAN_STREAM_RESULTS_ 1 -#endif - -// Defines some utility macros. - -// The GNU compiler emits a warning if nested "if" statements are followed by -// an "else" statement and braces are not used to explicitly disambiguate the -// "else" binding. This leads to problems with code like: -// -// if (gate) -// ASSERT_*(condition) << "Some message"; -// -// The "switch (0) case 0:" idiom is used to suppress this. -#ifdef __INTEL_COMPILER -# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ -#else -# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT -#endif - -// Use this annotation at the end of a struct/class definition to -// prevent the compiler from optimizing away instances that are never -// used. This is useful when all interesting logic happens inside the -// c'tor and / or d'tor. Example: -// -// struct Foo { -// Foo() { ... } -// } GTEST_ATTRIBUTE_UNUSED_; -// -// Also use it after a variable or parameter declaration to tell the -// compiler the variable/parameter does not have to be used. -#if defined(__GNUC__) && !defined(COMPILER_ICC) -# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) -#elif defined(__clang__) -# if __has_attribute(unused) -# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) -# endif -#endif -#ifndef GTEST_ATTRIBUTE_UNUSED_ -# define GTEST_ATTRIBUTE_UNUSED_ -#endif - -// Use this annotation before a function that takes a printf format string. -#if (defined(__GNUC__) || defined(__clang__)) && !defined(COMPILER_ICC) -# if defined(__MINGW_PRINTF_FORMAT) -// MinGW has two different printf implementations. Ensure the format macro -// matches the selected implementation. See -// https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/. -# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ - __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \ - first_to_check))) -# else -# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ - __attribute__((__format__(__printf__, string_index, first_to_check))) -# endif -#else -# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) -#endif - - -// A macro to disallow copy operator= -// This should be used in the private: declarations for a class. -#define GTEST_DISALLOW_ASSIGN_(type) \ - type& operator=(type const &) = delete - -// A macro to disallow copy constructor and operator= -// This should be used in the private: declarations for a class. -#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type) \ - type(type const&) = delete; \ - type& operator=(type const&) = delete - -// A macro to disallow move operator= -// This should be used in the private: declarations for a class. -#define GTEST_DISALLOW_MOVE_ASSIGN_(type) \ - type& operator=(type &&) noexcept = delete - -// A macro to disallow move constructor and operator= -// This should be used in the private: declarations for a class. -#define GTEST_DISALLOW_MOVE_AND_ASSIGN_(type) \ - type(type&&) noexcept = delete; \ - type& operator=(type&&) noexcept = delete - -// Tell the compiler to warn about unused return values for functions declared -// with this macro. The macro should be used on function declarations -// following the argument list: -// -// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; -#if defined(__GNUC__) && !defined(COMPILER_ICC) -# define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result)) -#else -# define GTEST_MUST_USE_RESULT_ -#endif // __GNUC__ && !COMPILER_ICC - -// MS C++ compiler emits warning when a conditional expression is compile time -// constant. In some contexts this warning is false positive and needs to be -// suppressed. Use the following two macros in such cases: -// -// GTEST_INTENTIONAL_CONST_COND_PUSH_() -// while (true) { -// GTEST_INTENTIONAL_CONST_COND_POP_() -// } -# define GTEST_INTENTIONAL_CONST_COND_PUSH_() \ - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127) -# define GTEST_INTENTIONAL_CONST_COND_POP_() \ - GTEST_DISABLE_MSC_WARNINGS_POP_() - -// Determine whether the compiler supports Microsoft's Structured Exception -// Handling. This is supported by several Windows compilers but generally -// does not exist on any other system. -#ifndef GTEST_HAS_SEH -// The user didn't tell us, so we need to figure it out. - -# if defined(_MSC_VER) || defined(__BORLANDC__) -// These two compilers are known to support SEH. -# define GTEST_HAS_SEH 1 -# else -// Assume no SEH. -# define GTEST_HAS_SEH 0 -# endif - -#endif // GTEST_HAS_SEH - -#ifndef GTEST_IS_THREADSAFE - -#define GTEST_IS_THREADSAFE \ - (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \ - (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) || \ - GTEST_HAS_PTHREAD) - -#endif // GTEST_IS_THREADSAFE - -// GTEST_API_ qualifies all symbols that must be exported. The definitions below -// are guarded by #ifndef to give embedders a chance to define GTEST_API_ in -// gtest/internal/custom/gtest-port.h -#ifndef GTEST_API_ - -#ifdef _MSC_VER -# if GTEST_LINKED_AS_SHARED_LIBRARY -# define GTEST_API_ __declspec(dllimport) -# elif GTEST_CREATE_SHARED_LIBRARY -# define GTEST_API_ __declspec(dllexport) -# endif -#elif __GNUC__ >= 4 || defined(__clang__) -# define GTEST_API_ __attribute__((visibility ("default"))) -#endif // _MSC_VER - -#endif // GTEST_API_ - -#ifndef GTEST_API_ -# define GTEST_API_ -#endif // GTEST_API_ - -#ifndef GTEST_DEFAULT_DEATH_TEST_STYLE -# define GTEST_DEFAULT_DEATH_TEST_STYLE "fast" -#endif // GTEST_DEFAULT_DEATH_TEST_STYLE - -#ifdef __GNUC__ -// Ask the compiler to never inline a given function. -# define GTEST_NO_INLINE_ __attribute__((noinline)) -#else -# define GTEST_NO_INLINE_ -#endif - -// _LIBCPP_VERSION is defined by the libc++ library from the LLVM project. -#if !defined(GTEST_HAS_CXXABI_H_) -# if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER)) -# define GTEST_HAS_CXXABI_H_ 1 -# else -# define GTEST_HAS_CXXABI_H_ 0 -# endif -#endif - -// A function level attribute to disable checking for use of uninitialized -// memory when built with MemorySanitizer. -#if defined(__clang__) -# if __has_feature(memory_sanitizer) -# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \ - __attribute__((no_sanitize_memory)) -# else -# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ -# endif // __has_feature(memory_sanitizer) -#else -# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ -#endif // __clang__ - -// A function level attribute to disable AddressSanitizer instrumentation. -#if defined(__clang__) -# if __has_feature(address_sanitizer) -# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \ - __attribute__((no_sanitize_address)) -# else -# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -# endif // __has_feature(address_sanitizer) -#else -# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ -#endif // __clang__ - -// A function level attribute to disable HWAddressSanitizer instrumentation. -#if defined(__clang__) -# if __has_feature(hwaddress_sanitizer) -# define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \ - __attribute__((no_sanitize("hwaddress"))) -# else -# define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -# endif // __has_feature(hwaddress_sanitizer) -#else -# define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ -#endif // __clang__ - -// A function level attribute to disable ThreadSanitizer instrumentation. -#if defined(__clang__) -# if __has_feature(thread_sanitizer) -# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \ - __attribute__((no_sanitize_thread)) -# else -# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ -# endif // __has_feature(thread_sanitizer) -#else -# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ -#endif // __clang__ - -namespace testing { - -class Message; - -// Legacy imports for backwards compatibility. -// New code should use std:: names directly. -using std::get; -using std::make_tuple; -using std::tuple; -using std::tuple_element; -using std::tuple_size; - -namespace internal { - -// A secret type that Google Test users don't know about. It has no -// definition on purpose. Therefore it's impossible to create a -// Secret object, which is what we want. -class Secret; - -// The GTEST_COMPILE_ASSERT_ is a legacy macro used to verify that a compile -// time expression is true (in new code, use static_assert instead). For -// example, you could use it to verify the size of a static array: -// -// GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES, -// names_incorrect_size); -// -// The second argument to the macro must be a valid C++ identifier. If the -// expression is false, compiler will issue an error containing this identifier. -#define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg) - -// A helper for suppressing warnings on constant condition. It just -// returns 'condition'. -GTEST_API_ bool IsTrue(bool condition); - -// Defines RE. - -#if GTEST_USES_PCRE -// if used, PCRE is injected by custom/gtest-port.h -#elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE - -// A simple C++ wrapper for . It uses the POSIX Extended -// Regular Expression syntax. -class GTEST_API_ RE { - public: - // A copy constructor is required by the Standard to initialize object - // references from r-values. - RE(const RE& other) { Init(other.pattern()); } - - // Constructs an RE from a string. - RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT - - RE(const char* regex) { Init(regex); } // NOLINT - ~RE(); - - // Returns the string representation of the regex. - const char* pattern() const { return pattern_; } - - // FullMatch(str, re) returns true if and only if regular expression re - // matches the entire str. - // PartialMatch(str, re) returns true if and only if regular expression re - // matches a substring of str (including str itself). - static bool FullMatch(const ::std::string& str, const RE& re) { - return FullMatch(str.c_str(), re); - } - static bool PartialMatch(const ::std::string& str, const RE& re) { - return PartialMatch(str.c_str(), re); - } - - static bool FullMatch(const char* str, const RE& re); - static bool PartialMatch(const char* str, const RE& re); - - private: - void Init(const char* regex); - const char* pattern_; - bool is_valid_; - -# if GTEST_USES_POSIX_RE - - regex_t full_regex_; // For FullMatch(). - regex_t partial_regex_; // For PartialMatch(). - -# else // GTEST_USES_SIMPLE_RE - - const char* full_pattern_; // For FullMatch(); - -# endif -}; - -#endif // GTEST_USES_PCRE - -// Formats a source file path and a line number as they would appear -// in an error message from the compiler used to compile this code. -GTEST_API_ ::std::string FormatFileLocation(const char* file, int line); - -// Formats a file location for compiler-independent XML output. -// Although this function is not platform dependent, we put it next to -// FormatFileLocation in order to contrast the two functions. -GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, - int line); - -// Defines logging utilities: -// GTEST_LOG_(severity) - logs messages at the specified severity level. The -// message itself is streamed into the macro. -// LogToStderr() - directs all log messages to stderr. -// FlushInfoLog() - flushes informational log messages. - -enum GTestLogSeverity { - GTEST_INFO, - GTEST_WARNING, - GTEST_ERROR, - GTEST_FATAL -}; - -// Formats log entry severity, provides a stream object for streaming the -// log message, and terminates the message with a newline when going out of -// scope. -class GTEST_API_ GTestLog { - public: - GTestLog(GTestLogSeverity severity, const char* file, int line); - - // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. - ~GTestLog(); - - ::std::ostream& GetStream() { return ::std::cerr; } - - private: - const GTestLogSeverity severity_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog); -}; - -#if !defined(GTEST_LOG_) - -# define GTEST_LOG_(severity) \ - ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ - __FILE__, __LINE__).GetStream() - -inline void LogToStderr() {} -inline void FlushInfoLog() { fflush(nullptr); } - -#endif // !defined(GTEST_LOG_) - -#if !defined(GTEST_CHECK_) -// INTERNAL IMPLEMENTATION - DO NOT USE. -// -// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition -// is not satisfied. -// Synopsys: -// GTEST_CHECK_(boolean_condition); -// or -// GTEST_CHECK_(boolean_condition) << "Additional message"; -// -// This checks the condition and if the condition is not satisfied -// it prints message about the condition violation, including the -// condition itself, plus additional message streamed into it, if any, -// and then it aborts the program. It aborts the program irrespective of -// whether it is built in the debug mode or not. -# define GTEST_CHECK_(condition) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::IsTrue(condition)) \ - ; \ - else \ - GTEST_LOG_(FATAL) << "Condition " #condition " failed. " -#endif // !defined(GTEST_CHECK_) - -// An all-mode assert to verify that the given POSIX-style function -// call returns 0 (indicating success). Known limitation: this -// doesn't expand to a balanced 'if' statement, so enclose the macro -// in {} if you need to use it as the only statement in an 'if' -// branch. -#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ - if (const int gtest_error = (posix_call)) \ - GTEST_LOG_(FATAL) << #posix_call << "failed with error " \ - << gtest_error - -// Transforms "T" into "const T&" according to standard reference collapsing -// rules (this is only needed as a backport for C++98 compilers that do not -// support reference collapsing). Specifically, it transforms: -// -// char ==> const char& -// const char ==> const char& -// char& ==> char& -// const char& ==> const char& -// -// Note that the non-const reference will not have "const" added. This is -// standard, and necessary so that "T" can always bind to "const T&". -template -struct ConstRef { typedef const T& type; }; -template -struct ConstRef { typedef T& type; }; - -// The argument T must depend on some template parameters. -#define GTEST_REFERENCE_TO_CONST_(T) \ - typename ::testing::internal::ConstRef::type - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Use ImplicitCast_ as a safe version of static_cast for upcasting in -// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a -// const Foo*). When you use ImplicitCast_, the compiler checks that -// the cast is safe. Such explicit ImplicitCast_s are necessary in -// surprisingly many situations where C++ demands an exact type match -// instead of an argument type convertable to a target type. -// -// The syntax for using ImplicitCast_ is the same as for static_cast: -// -// ImplicitCast_(expr) -// -// ImplicitCast_ would have been part of the C++ standard library, -// but the proposal was submitted too late. It will probably make -// its way into the language in the future. -// -// This relatively ugly name is intentional. It prevents clashes with -// similar functions users may have (e.g., implicit_cast). The internal -// namespace alone is not enough because the function can be found by ADL. -template -inline To ImplicitCast_(To x) { return x; } - -// When you upcast (that is, cast a pointer from type Foo to type -// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts -// always succeed. When you downcast (that is, cast a pointer from -// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because -// how do you know the pointer is really of type SubclassOfFoo? It -// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, -// when you downcast, you should use this macro. In debug mode, we -// use dynamic_cast<> to double-check the downcast is legal (we die -// if it's not). In normal mode, we do the efficient static_cast<> -// instead. Thus, it's important to test in debug mode to make sure -// the cast is legal! -// This is the only place in the code we should use dynamic_cast<>. -// In particular, you SHOULDN'T be using dynamic_cast<> in order to -// do RTTI (eg code like this: -// if (dynamic_cast(foo)) HandleASubclass1Object(foo); -// if (dynamic_cast(foo)) HandleASubclass2Object(foo); -// You should design the code some other way not to need this. -// -// This relatively ugly name is intentional. It prevents clashes with -// similar functions users may have (e.g., down_cast). The internal -// namespace alone is not enough because the function can be found by ADL. -template // use like this: DownCast_(foo); -inline To DownCast_(From* f) { // so we only accept pointers - // Ensures that To is a sub-type of From *. This test is here only - // for compile-time type checking, and has no overhead in an - // optimized build at run-time, as it will be optimized away - // completely. - GTEST_INTENTIONAL_CONST_COND_PUSH_() - if (false) { - GTEST_INTENTIONAL_CONST_COND_POP_() - const To to = nullptr; - ::testing::internal::ImplicitCast_(to); - } - -#if GTEST_HAS_RTTI - // RTTI: debug mode only! - GTEST_CHECK_(f == nullptr || dynamic_cast(f) != nullptr); -#endif - return static_cast(f); -} - -// Downcasts the pointer of type Base to Derived. -// Derived must be a subclass of Base. The parameter MUST -// point to a class of type Derived, not any subclass of it. -// When RTTI is available, the function performs a runtime -// check to enforce this. -template -Derived* CheckedDowncastToActualType(Base* base) { -#if GTEST_HAS_RTTI - GTEST_CHECK_(typeid(*base) == typeid(Derived)); -#endif - -#if GTEST_HAS_DOWNCAST_ - return ::down_cast(base); -#elif GTEST_HAS_RTTI - return dynamic_cast(base); // NOLINT -#else - return static_cast(base); // Poor man's downcast. -#endif -} - -#if GTEST_HAS_STREAM_REDIRECTION - -// Defines the stderr capturer: -// CaptureStdout - starts capturing stdout. -// GetCapturedStdout - stops capturing stdout and returns the captured string. -// CaptureStderr - starts capturing stderr. -// GetCapturedStderr - stops capturing stderr and returns the captured string. -// -GTEST_API_ void CaptureStdout(); -GTEST_API_ std::string GetCapturedStdout(); -GTEST_API_ void CaptureStderr(); -GTEST_API_ std::string GetCapturedStderr(); - -#endif // GTEST_HAS_STREAM_REDIRECTION -// Returns the size (in bytes) of a file. -GTEST_API_ size_t GetFileSize(FILE* file); - -// Reads the entire content of a file as a string. -GTEST_API_ std::string ReadEntireFile(FILE* file); - -// All command line arguments. -GTEST_API_ std::vector GetArgvs(); - -#if GTEST_HAS_DEATH_TEST - -std::vector GetInjectableArgvs(); -// Deprecated: pass the args vector by value instead. -void SetInjectableArgvs(const std::vector* new_argvs); -void SetInjectableArgvs(const std::vector& new_argvs); -void ClearInjectableArgvs(); - -#endif // GTEST_HAS_DEATH_TEST - -// Defines synchronization primitives. -#if GTEST_IS_THREADSAFE -# if GTEST_HAS_PTHREAD -// Sleeps for (roughly) n milliseconds. This function is only for testing -// Google Test's own constructs. Don't use it in user tests, either -// directly or indirectly. -inline void SleepMilliseconds(int n) { - const timespec time = { - 0, // 0 seconds. - n * 1000L * 1000L, // And n ms. - }; - nanosleep(&time, nullptr); -} -# endif // GTEST_HAS_PTHREAD - -# if GTEST_HAS_NOTIFICATION_ -// Notification has already been imported into the namespace. -// Nothing to do here. - -# elif GTEST_HAS_PTHREAD -// Allows a controller thread to pause execution of newly created -// threads until notified. Instances of this class must be created -// and destroyed in the controller thread. -// -// This class is only for testing Google Test's own constructs. Do not -// use it in user tests, either directly or indirectly. -class Notification { - public: - Notification() : notified_(false) { - GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); - } - ~Notification() { - pthread_mutex_destroy(&mutex_); - } - - // Notifies all threads created with this notification to start. Must - // be called from the controller thread. - void Notify() { - pthread_mutex_lock(&mutex_); - notified_ = true; - pthread_mutex_unlock(&mutex_); - } - - // Blocks until the controller thread notifies. Must be called from a test - // thread. - void WaitForNotification() { - for (;;) { - pthread_mutex_lock(&mutex_); - const bool notified = notified_; - pthread_mutex_unlock(&mutex_); - if (notified) - break; - SleepMilliseconds(10); - } - } - - private: - pthread_mutex_t mutex_; - bool notified_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); -}; - -# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT - -GTEST_API_ void SleepMilliseconds(int n); - -// Provides leak-safe Windows kernel handle ownership. -// Used in death tests and in threading support. -class GTEST_API_ AutoHandle { - public: - // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to - // avoid including in this header file. Including is - // undesirable because it defines a lot of symbols and macros that tend to - // conflict with client code. This assumption is verified by - // WindowsTypesTest.HANDLEIsVoidStar. - typedef void* Handle; - AutoHandle(); - explicit AutoHandle(Handle handle); - - ~AutoHandle(); - - Handle Get() const; - void Reset(); - void Reset(Handle handle); - - private: - // Returns true if and only if the handle is a valid handle object that can be - // closed. - bool IsCloseable() const; - - Handle handle_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle); -}; - -// Allows a controller thread to pause execution of newly created -// threads until notified. Instances of this class must be created -// and destroyed in the controller thread. -// -// This class is only for testing Google Test's own constructs. Do not -// use it in user tests, either directly or indirectly. -class GTEST_API_ Notification { - public: - Notification(); - void Notify(); - void WaitForNotification(); - - private: - AutoHandle event_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); -}; -# endif // GTEST_HAS_NOTIFICATION_ - -// On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD -// defined, but we don't want to use MinGW's pthreads implementation, which -// has conformance problems with some versions of the POSIX standard. -# if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW - -// As a C-function, ThreadFuncWithCLinkage cannot be templated itself. -// Consequently, it cannot select a correct instantiation of ThreadWithParam -// in order to call its Run(). Introducing ThreadWithParamBase as a -// non-templated base class for ThreadWithParam allows us to bypass this -// problem. -class ThreadWithParamBase { - public: - virtual ~ThreadWithParamBase() {} - virtual void Run() = 0; -}; - -// pthread_create() accepts a pointer to a function type with the C linkage. -// According to the Standard (7.5/1), function types with different linkages -// are different even if they are otherwise identical. Some compilers (for -// example, SunStudio) treat them as different types. Since class methods -// cannot be defined with C-linkage we need to define a free C-function to -// pass into pthread_create(). -extern "C" inline void* ThreadFuncWithCLinkage(void* thread) { - static_cast(thread)->Run(); - return nullptr; -} - -// Helper class for testing Google Test's multi-threading constructs. -// To use it, write: -// -// void ThreadFunc(int param) { /* Do things with param */ } -// Notification thread_can_start; -// ... -// // The thread_can_start parameter is optional; you can supply NULL. -// ThreadWithParam thread(&ThreadFunc, 5, &thread_can_start); -// thread_can_start.Notify(); -// -// These classes are only for testing Google Test's own constructs. Do -// not use them in user tests, either directly or indirectly. -template -class ThreadWithParam : public ThreadWithParamBase { - public: - typedef void UserThreadFunc(T); - - ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) - : func_(func), - param_(param), - thread_can_start_(thread_can_start), - finished_(false) { - ThreadWithParamBase* const base = this; - // The thread can be created only after all fields except thread_ - // have been initialized. - GTEST_CHECK_POSIX_SUCCESS_( - pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base)); - } - ~ThreadWithParam() override { Join(); } - - void Join() { - if (!finished_) { - GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr)); - finished_ = true; - } - } - - void Run() override { - if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification(); - func_(param_); - } - - private: - UserThreadFunc* const func_; // User-supplied thread function. - const T param_; // User-supplied parameter to the thread function. - // When non-NULL, used to block execution until the controller thread - // notifies. - Notification* const thread_can_start_; - bool finished_; // true if and only if we know that the thread function has - // finished. - pthread_t thread_; // The native thread object. - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); -}; -# endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD || - // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - -# if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ -// Mutex and ThreadLocal have already been imported into the namespace. -// Nothing to do here. - -# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT - -// Mutex implements mutex on Windows platforms. It is used in conjunction -// with class MutexLock: -// -// Mutex mutex; -// ... -// MutexLock lock(&mutex); // Acquires the mutex and releases it at the -// // end of the current scope. -// -// A static Mutex *must* be defined or declared using one of the following -// macros: -// GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); -// GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); -// -// (A non-static Mutex is defined/declared in the usual way). -class GTEST_API_ Mutex { - public: - enum MutexType { kStatic = 0, kDynamic = 1 }; - // We rely on kStaticMutex being 0 as it is to what the linker initializes - // type_ in static mutexes. critical_section_ will be initialized lazily - // in ThreadSafeLazyInit(). - enum StaticConstructorSelector { kStaticMutex = 0 }; - - // This constructor intentionally does nothing. It relies on type_ being - // statically initialized to 0 (effectively setting it to kStatic) and on - // ThreadSafeLazyInit() to lazily initialize the rest of the members. - explicit Mutex(StaticConstructorSelector /*dummy*/) {} - - Mutex(); - ~Mutex(); - - void Lock(); - - void Unlock(); - - // Does nothing if the current thread holds the mutex. Otherwise, crashes - // with high probability. - void AssertHeld(); - - private: - // Initializes owner_thread_id_ and critical_section_ in static mutexes. - void ThreadSafeLazyInit(); - - // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503, - // we assume that 0 is an invalid value for thread IDs. - unsigned int owner_thread_id_; - - // For static mutexes, we rely on these members being initialized to zeros - // by the linker. - MutexType type_; - long critical_section_init_phase_; // NOLINT - GTEST_CRITICAL_SECTION* critical_section_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); -}; - -# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ - extern ::testing::internal::Mutex mutex - -# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ - ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex) - -// We cannot name this class MutexLock because the ctor declaration would -// conflict with a macro named MutexLock, which is defined on some -// platforms. That macro is used as a defensive measure to prevent against -// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than -// "MutexLock l(&mu)". Hence the typedef trick below. -class GTestMutexLock { - public: - explicit GTestMutexLock(Mutex* mutex) - : mutex_(mutex) { mutex_->Lock(); } - - ~GTestMutexLock() { mutex_->Unlock(); } - - private: - Mutex* const mutex_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); -}; - -typedef GTestMutexLock MutexLock; - -// Base class for ValueHolder. Allows a caller to hold and delete a value -// without knowing its type. -class ThreadLocalValueHolderBase { - public: - virtual ~ThreadLocalValueHolderBase() {} -}; - -// Provides a way for a thread to send notifications to a ThreadLocal -// regardless of its parameter type. -class ThreadLocalBase { - public: - // Creates a new ValueHolder object holding a default value passed to - // this ThreadLocal's constructor and returns it. It is the caller's - // responsibility not to call this when the ThreadLocal instance already - // has a value on the current thread. - virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0; - - protected: - ThreadLocalBase() {} - virtual ~ThreadLocalBase() {} - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase); -}; - -// Maps a thread to a set of ThreadLocals that have values instantiated on that -// thread and notifies them when the thread exits. A ThreadLocal instance is -// expected to persist until all threads it has values on have terminated. -class GTEST_API_ ThreadLocalRegistry { - public: - // Registers thread_local_instance as having value on the current thread. - // Returns a value that can be used to identify the thread from other threads. - static ThreadLocalValueHolderBase* GetValueOnCurrentThread( - const ThreadLocalBase* thread_local_instance); - - // Invoked when a ThreadLocal instance is destroyed. - static void OnThreadLocalDestroyed( - const ThreadLocalBase* thread_local_instance); -}; - -class GTEST_API_ ThreadWithParamBase { - public: - void Join(); - - protected: - class Runnable { - public: - virtual ~Runnable() {} - virtual void Run() = 0; - }; - - ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start); - virtual ~ThreadWithParamBase(); - - private: - AutoHandle thread_; -}; - -// Helper class for testing Google Test's multi-threading constructs. -template -class ThreadWithParam : public ThreadWithParamBase { - public: - typedef void UserThreadFunc(T); - - ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) - : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) { - } - virtual ~ThreadWithParam() {} - - private: - class RunnableImpl : public Runnable { - public: - RunnableImpl(UserThreadFunc* func, T param) - : func_(func), - param_(param) { - } - virtual ~RunnableImpl() {} - virtual void Run() { - func_(param_); - } - - private: - UserThreadFunc* const func_; - const T param_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl); - }; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); -}; - -// Implements thread-local storage on Windows systems. -// -// // Thread 1 -// ThreadLocal tl(100); // 100 is the default value for each thread. -// -// // Thread 2 -// tl.set(150); // Changes the value for thread 2 only. -// EXPECT_EQ(150, tl.get()); -// -// // Thread 1 -// EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. -// tl.set(200); -// EXPECT_EQ(200, tl.get()); -// -// The template type argument T must have a public copy constructor. -// In addition, the default ThreadLocal constructor requires T to have -// a public default constructor. -// -// The users of a TheadLocal instance have to make sure that all but one -// threads (including the main one) using that instance have exited before -// destroying it. Otherwise, the per-thread objects managed for them by the -// ThreadLocal instance are not guaranteed to be destroyed on all platforms. -// -// Google Test only uses global ThreadLocal objects. That means they -// will die after main() has returned. Therefore, no per-thread -// object managed by Google Test will be leaked as long as all threads -// using Google Test have exited when main() returns. -template -class ThreadLocal : public ThreadLocalBase { - public: - ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {} - explicit ThreadLocal(const T& value) - : default_factory_(new InstanceValueHolderFactory(value)) {} - - ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); } - - T* pointer() { return GetOrCreateValue(); } - const T* pointer() const { return GetOrCreateValue(); } - const T& get() const { return *pointer(); } - void set(const T& value) { *pointer() = value; } - - private: - // Holds a value of T. Can be deleted via its base class without the caller - // knowing the type of T. - class ValueHolder : public ThreadLocalValueHolderBase { - public: - ValueHolder() : value_() {} - explicit ValueHolder(const T& value) : value_(value) {} - - T* pointer() { return &value_; } - - private: - T value_; - GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder); - }; - - - T* GetOrCreateValue() const { - return static_cast( - ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer(); - } - - virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const { - return default_factory_->MakeNewHolder(); - } - - class ValueHolderFactory { - public: - ValueHolderFactory() {} - virtual ~ValueHolderFactory() {} - virtual ValueHolder* MakeNewHolder() const = 0; - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory); - }; - - class DefaultValueHolderFactory : public ValueHolderFactory { - public: - DefaultValueHolderFactory() {} - ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory); - }; - - class InstanceValueHolderFactory : public ValueHolderFactory { - public: - explicit InstanceValueHolderFactory(const T& value) : value_(value) {} - ValueHolder* MakeNewHolder() const override { - return new ValueHolder(value_); - } - - private: - const T value_; // The value for each thread. - - GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory); - }; - - std::unique_ptr default_factory_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); -}; - -# elif GTEST_HAS_PTHREAD - -// MutexBase and Mutex implement mutex on pthreads-based platforms. -class MutexBase { - public: - // Acquires this mutex. - void Lock() { - GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); - owner_ = pthread_self(); - has_owner_ = true; - } - - // Releases this mutex. - void Unlock() { - // Since the lock is being released the owner_ field should no longer be - // considered valid. We don't protect writing to has_owner_ here, as it's - // the caller's responsibility to ensure that the current thread holds the - // mutex when this is called. - has_owner_ = false; - GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); - } - - // Does nothing if the current thread holds the mutex. Otherwise, crashes - // with high probability. - void AssertHeld() const { - GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self())) - << "The current thread is not holding the mutex @" << this; - } - - // A static mutex may be used before main() is entered. It may even - // be used before the dynamic initialization stage. Therefore we - // must be able to initialize a static mutex object at link time. - // This means MutexBase has to be a POD and its member variables - // have to be public. - public: - pthread_mutex_t mutex_; // The underlying pthread mutex. - // has_owner_ indicates whether the owner_ field below contains a valid thread - // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All - // accesses to the owner_ field should be protected by a check of this field. - // An alternative might be to memset() owner_ to all zeros, but there's no - // guarantee that a zero'd pthread_t is necessarily invalid or even different - // from pthread_self(). - bool has_owner_; - pthread_t owner_; // The thread holding the mutex. -}; - -// Forward-declares a static mutex. -# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ - extern ::testing::internal::MutexBase mutex - -// Defines and statically (i.e. at link time) initializes a static mutex. -// The initialization list here does not explicitly initialize each field, -// instead relying on default initialization for the unspecified fields. In -// particular, the owner_ field (a pthread_t) is not explicitly initialized. -// This allows initialization to work whether pthread_t is a scalar or struct. -// The flag -Wmissing-field-initializers must not be specified for this to work. -#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ - ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} - -// The Mutex class can only be used for mutexes created at runtime. It -// shares its API with MutexBase otherwise. -class Mutex : public MutexBase { - public: - Mutex() { - GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); - has_owner_ = false; - } - ~Mutex() { - GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); - } - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); -}; - -// We cannot name this class MutexLock because the ctor declaration would -// conflict with a macro named MutexLock, which is defined on some -// platforms. That macro is used as a defensive measure to prevent against -// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than -// "MutexLock l(&mu)". Hence the typedef trick below. -class GTestMutexLock { - public: - explicit GTestMutexLock(MutexBase* mutex) - : mutex_(mutex) { mutex_->Lock(); } - - ~GTestMutexLock() { mutex_->Unlock(); } - - private: - MutexBase* const mutex_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); -}; - -typedef GTestMutexLock MutexLock; - -// Helpers for ThreadLocal. - -// pthread_key_create() requires DeleteThreadLocalValue() to have -// C-linkage. Therefore it cannot be templatized to access -// ThreadLocal. Hence the need for class -// ThreadLocalValueHolderBase. -class ThreadLocalValueHolderBase { - public: - virtual ~ThreadLocalValueHolderBase() {} -}; - -// Called by pthread to delete thread-local data stored by -// pthread_setspecific(). -extern "C" inline void DeleteThreadLocalValue(void* value_holder) { - delete static_cast(value_holder); -} - -// Implements thread-local storage on pthreads-based systems. -template -class GTEST_API_ ThreadLocal { - public: - ThreadLocal() - : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {} - explicit ThreadLocal(const T& value) - : key_(CreateKey()), - default_factory_(new InstanceValueHolderFactory(value)) {} - - ~ThreadLocal() { - // Destroys the managed object for the current thread, if any. - DeleteThreadLocalValue(pthread_getspecific(key_)); - - // Releases resources associated with the key. This will *not* - // delete managed objects for other threads. - GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); - } - - T* pointer() { return GetOrCreateValue(); } - const T* pointer() const { return GetOrCreateValue(); } - const T& get() const { return *pointer(); } - void set(const T& value) { *pointer() = value; } - - private: - // Holds a value of type T. - class ValueHolder : public ThreadLocalValueHolderBase { - public: - ValueHolder() : value_() {} - explicit ValueHolder(const T& value) : value_(value) {} - - T* pointer() { return &value_; } - - private: - T value_; - GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder); - }; - - static pthread_key_t CreateKey() { - pthread_key_t key; - // When a thread exits, DeleteThreadLocalValue() will be called on - // the object managed for that thread. - GTEST_CHECK_POSIX_SUCCESS_( - pthread_key_create(&key, &DeleteThreadLocalValue)); - return key; - } - - T* GetOrCreateValue() const { - ThreadLocalValueHolderBase* const holder = - static_cast(pthread_getspecific(key_)); - if (holder != nullptr) { - return CheckedDowncastToActualType(holder)->pointer(); - } - - ValueHolder* const new_holder = default_factory_->MakeNewHolder(); - ThreadLocalValueHolderBase* const holder_base = new_holder; - GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base)); - return new_holder->pointer(); - } - - class ValueHolderFactory { - public: - ValueHolderFactory() {} - virtual ~ValueHolderFactory() {} - virtual ValueHolder* MakeNewHolder() const = 0; - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory); - }; - - class DefaultValueHolderFactory : public ValueHolderFactory { - public: - DefaultValueHolderFactory() {} - ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory); - }; - - class InstanceValueHolderFactory : public ValueHolderFactory { - public: - explicit InstanceValueHolderFactory(const T& value) : value_(value) {} - ValueHolder* MakeNewHolder() const override { - return new ValueHolder(value_); - } - - private: - const T value_; // The value for each thread. - - GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory); - }; - - // A key pthreads uses for looking up per-thread values. - const pthread_key_t key_; - std::unique_ptr default_factory_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); -}; - -# endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - -#else // GTEST_IS_THREADSAFE - -// A dummy implementation of synchronization primitives (mutex, lock, -// and thread-local variable). Necessary for compiling Google Test where -// mutex is not supported - using Google Test in multiple threads is not -// supported on such platforms. - -class Mutex { - public: - Mutex() {} - void Lock() {} - void Unlock() {} - void AssertHeld() const {} -}; - -# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ - extern ::testing::internal::Mutex mutex - -# define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex - -// We cannot name this class MutexLock because the ctor declaration would -// conflict with a macro named MutexLock, which is defined on some -// platforms. That macro is used as a defensive measure to prevent against -// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than -// "MutexLock l(&mu)". Hence the typedef trick below. -class GTestMutexLock { - public: - explicit GTestMutexLock(Mutex*) {} // NOLINT -}; - -typedef GTestMutexLock MutexLock; - -template -class GTEST_API_ ThreadLocal { - public: - ThreadLocal() : value_() {} - explicit ThreadLocal(const T& value) : value_(value) {} - T* pointer() { return &value_; } - const T* pointer() const { return &value_; } - const T& get() const { return value_; } - void set(const T& value) { value_ = value; } - private: - T value_; -}; - -#endif // GTEST_IS_THREADSAFE - -// Returns the number of threads running in the process, or 0 to indicate that -// we cannot detect it. -GTEST_API_ size_t GetThreadCount(); - -#if GTEST_OS_WINDOWS -# define GTEST_PATH_SEP_ "\\" -# define GTEST_HAS_ALT_PATH_SEP_ 1 -#else -# define GTEST_PATH_SEP_ "/" -# define GTEST_HAS_ALT_PATH_SEP_ 0 -#endif // GTEST_OS_WINDOWS - -// Utilities for char. - -// isspace(int ch) and friends accept an unsigned char or EOF. char -// may be signed, depending on the compiler (or compiler flags). -// Therefore we need to cast a char to unsigned char before calling -// isspace(), etc. - -inline bool IsAlpha(char ch) { - return isalpha(static_cast(ch)) != 0; -} -inline bool IsAlNum(char ch) { - return isalnum(static_cast(ch)) != 0; -} -inline bool IsDigit(char ch) { - return isdigit(static_cast(ch)) != 0; -} -inline bool IsLower(char ch) { - return islower(static_cast(ch)) != 0; -} -inline bool IsSpace(char ch) { - return isspace(static_cast(ch)) != 0; -} -inline bool IsUpper(char ch) { - return isupper(static_cast(ch)) != 0; -} -inline bool IsXDigit(char ch) { - return isxdigit(static_cast(ch)) != 0; -} -#ifdef __cpp_char8_t -inline bool IsXDigit(char8_t ch) { - return isxdigit(static_cast(ch)) != 0; -} -#endif -inline bool IsXDigit(char16_t ch) { - const unsigned char low_byte = static_cast(ch); - return ch == low_byte && isxdigit(low_byte) != 0; -} -inline bool IsXDigit(char32_t ch) { - const unsigned char low_byte = static_cast(ch); - return ch == low_byte && isxdigit(low_byte) != 0; -} -inline bool IsXDigit(wchar_t ch) { - const unsigned char low_byte = static_cast(ch); - return ch == low_byte && isxdigit(low_byte) != 0; -} - -inline char ToLower(char ch) { - return static_cast(tolower(static_cast(ch))); -} -inline char ToUpper(char ch) { - return static_cast(toupper(static_cast(ch))); -} - -inline std::string StripTrailingSpaces(std::string str) { - std::string::iterator it = str.end(); - while (it != str.begin() && IsSpace(*--it)) - it = str.erase(it); - return str; -} - -// The testing::internal::posix namespace holds wrappers for common -// POSIX functions. These wrappers hide the differences between -// Windows/MSVC and POSIX systems. Since some compilers define these -// standard functions as macros, the wrapper cannot have the same name -// as the wrapped function. - -namespace posix { - -// Functions with a different name on Windows. - -#if GTEST_OS_WINDOWS - -typedef struct _stat StatStruct; - -# ifdef __BORLANDC__ -inline int DoIsATTY(int fd) { return isatty(fd); } -inline int StrCaseCmp(const char* s1, const char* s2) { - return stricmp(s1, s2); -} -inline char* StrDup(const char* src) { return strdup(src); } -# else // !__BORLANDC__ -# if GTEST_OS_WINDOWS_MOBILE -inline int DoIsATTY(int /* fd */) { return 0; } -# else -inline int DoIsATTY(int fd) { return _isatty(fd); } -# endif // GTEST_OS_WINDOWS_MOBILE -inline int StrCaseCmp(const char* s1, const char* s2) { - return _stricmp(s1, s2); -} -inline char* StrDup(const char* src) { return _strdup(src); } -# endif // __BORLANDC__ - -# if GTEST_OS_WINDOWS_MOBILE -inline int FileNo(FILE* file) { return reinterpret_cast(_fileno(file)); } -// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this -// time and thus not defined there. -# else -inline int FileNo(FILE* file) { return _fileno(file); } -inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } -inline int RmDir(const char* dir) { return _rmdir(dir); } -inline bool IsDir(const StatStruct& st) { - return (_S_IFDIR & st.st_mode) != 0; -} -# endif // GTEST_OS_WINDOWS_MOBILE - -#elif GTEST_OS_ESP8266 -typedef struct stat StatStruct; - -inline int FileNo(FILE* file) { return fileno(file); } -inline int DoIsATTY(int fd) { return isatty(fd); } -inline int Stat(const char* path, StatStruct* buf) { - // stat function not implemented on ESP8266 - return 0; -} -inline int StrCaseCmp(const char* s1, const char* s2) { - return strcasecmp(s1, s2); -} -inline char* StrDup(const char* src) { return strdup(src); } -inline int RmDir(const char* dir) { return rmdir(dir); } -inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } - -#else - -typedef struct stat StatStruct; - -inline int FileNo(FILE* file) { return fileno(file); } -inline int DoIsATTY(int fd) { return isatty(fd); } -inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } -inline int StrCaseCmp(const char* s1, const char* s2) { - return strcasecmp(s1, s2); -} -inline char* StrDup(const char* src) { return strdup(src); } -inline int RmDir(const char* dir) { return rmdir(dir); } -inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } - -#endif // GTEST_OS_WINDOWS - -inline int IsATTY(int fd) { - // DoIsATTY might change errno (for example ENOTTY in case you redirect stdout - // to a file on Linux), which is unexpected, so save the previous value, and - // restore it after the call. - int savedErrno = errno; - int isAttyValue = DoIsATTY(fd); - errno = savedErrno; - - return isAttyValue; -} - -// Functions deprecated by MSVC 8.0. - -GTEST_DISABLE_MSC_DEPRECATED_PUSH_() - -// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and -// StrError() aren't needed on Windows CE at this time and thus not -// defined there. - -#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \ - !GTEST_OS_WINDOWS_RT && !GTEST_OS_ESP8266 && !GTEST_OS_XTENSA -inline int ChDir(const char* dir) { return chdir(dir); } -#endif -inline FILE* FOpen(const char* path, const char* mode) { -#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW - struct wchar_codecvt : public std::codecvt {}; - std::wstring_convert converter; - std::wstring wide_path = converter.from_bytes(path); - std::wstring wide_mode = converter.from_bytes(mode); - return _wfopen(wide_path.c_str(), wide_mode.c_str()); -#else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW - return fopen(path, mode); -#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW -} -#if !GTEST_OS_WINDOWS_MOBILE -inline FILE *FReopen(const char* path, const char* mode, FILE* stream) { - return freopen(path, mode, stream); -} -inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } -#endif -inline int FClose(FILE* fp) { return fclose(fp); } -#if !GTEST_OS_WINDOWS_MOBILE -inline int Read(int fd, void* buf, unsigned int count) { - return static_cast(read(fd, buf, count)); -} -inline int Write(int fd, const void* buf, unsigned int count) { - return static_cast(write(fd, buf, count)); -} -inline int Close(int fd) { return close(fd); } -inline const char* StrError(int errnum) { return strerror(errnum); } -#endif -inline const char* GetEnv(const char* name) { -#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ - GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA - // We are on an embedded platform, which has no environment variables. - static_cast(name); // To prevent 'unused argument' warning. - return nullptr; -#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) - // Environment variables which we programmatically clear will be set to the - // empty string rather than unset (NULL). Handle that case. - const char* const env = getenv(name); - return (env != nullptr && env[0] != '\0') ? env : nullptr; -#else - return getenv(name); -#endif -} - -GTEST_DISABLE_MSC_DEPRECATED_POP_() - -#if GTEST_OS_WINDOWS_MOBILE -// Windows CE has no C library. The abort() function is used in -// several places in Google Test. This implementation provides a reasonable -// imitation of standard behaviour. -[[noreturn]] void Abort(); -#else -[[noreturn]] inline void Abort() { abort(); } -#endif // GTEST_OS_WINDOWS_MOBILE - -} // namespace posix - -// MSVC "deprecates" snprintf and issues warnings wherever it is used. In -// order to avoid these warnings, we need to use _snprintf or _snprintf_s on -// MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate -// function in order to achieve that. We use macro definition here because -// snprintf is a variadic function. -#if _MSC_VER && !GTEST_OS_WINDOWS_MOBILE -// MSVC 2005 and above support variadic macros. -# define GTEST_SNPRINTF_(buffer, size, format, ...) \ - _snprintf_s(buffer, size, size, format, __VA_ARGS__) -#elif defined(_MSC_VER) -// Windows CE does not define _snprintf_s -# define GTEST_SNPRINTF_ _snprintf -#else -# define GTEST_SNPRINTF_ snprintf -#endif - -// The biggest signed integer type the compiler supports. -// -// long long is guaranteed to be at least 64-bits in C++11. -using BiggestInt = long long; // NOLINT - -// The maximum number a BiggestInt can represent. -constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits::max)(); - -// This template class serves as a compile-time function from size to -// type. It maps a size in bytes to a primitive type with that -// size. e.g. -// -// TypeWithSize<4>::UInt -// -// is typedef-ed to be unsigned int (unsigned integer made up of 4 -// bytes). -// -// Such functionality should belong to STL, but I cannot find it -// there. -// -// Google Test uses this class in the implementation of floating-point -// comparison. -// -// For now it only handles UInt (unsigned int) as that's all Google Test -// needs. Other types can be easily added in the future if need -// arises. -template -class TypeWithSize { - public: - // This prevents the user from using TypeWithSize with incorrect - // values of N. - using UInt = void; -}; - -// The specialization for size 4. -template <> -class TypeWithSize<4> { - public: - using Int = std::int32_t; - using UInt = std::uint32_t; -}; - -// The specialization for size 8. -template <> -class TypeWithSize<8> { - public: - using Int = std::int64_t; - using UInt = std::uint64_t; -}; - -// Integer types of known sizes. -using TimeInMillis = int64_t; // Represents time in milliseconds. - -// Utilities for command line flags and environment variables. - -// Macro for referencing flags. -#if !defined(GTEST_FLAG) -# define GTEST_FLAG(name) FLAGS_gtest_##name -#endif // !defined(GTEST_FLAG) - -#if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_) -# define GTEST_USE_OWN_FLAGFILE_FLAG_ 1 -#endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_) - -#if !defined(GTEST_DECLARE_bool_) -# define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver - -// Macros for declaring flags. -# define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name) -# define GTEST_DECLARE_int32_(name) \ - GTEST_API_ extern std::int32_t GTEST_FLAG(name) -# define GTEST_DECLARE_string_(name) \ - GTEST_API_ extern ::std::string GTEST_FLAG(name) - -// Macros for defining flags. -# define GTEST_DEFINE_bool_(name, default_val, doc) \ - GTEST_API_ bool GTEST_FLAG(name) = (default_val) -# define GTEST_DEFINE_int32_(name, default_val, doc) \ - GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val) -# define GTEST_DEFINE_string_(name, default_val, doc) \ - GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val) - -#endif // !defined(GTEST_DECLARE_bool_) - -// Thread annotations -#if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) -# define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks) -# define GTEST_LOCK_EXCLUDED_(locks) -#endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) - -// Parses 'str' for a 32-bit signed integer. If successful, writes the result -// to *value and returns true; otherwise leaves *value unchanged and returns -// false. -GTEST_API_ bool ParseInt32(const Message& src_text, const char* str, - int32_t* value); - -// Parses a bool/int32_t/string from the environment variable -// corresponding to the given Google Test flag. -bool BoolFromGTestEnv(const char* flag, bool default_val); -GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val); -std::string OutputFlagAlsoCheckEnvVar(); -const char* StringFromGTestEnv(const char* flag, const char* default_val); - -} // namespace internal -} // namespace testing - -#if !defined(GTEST_INTERNAL_DEPRECATED) - -// Internal Macro to mark an API deprecated, for googletest usage only -// Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or -// GTEST_INTERNAL_DEPRECATED(message) myFunction(); Every usage of -// a deprecated entity will trigger a warning when compiled with -// `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler). -// For msvc /W3 option will need to be used -// Note that for 'other' compilers this macro evaluates to nothing to prevent -// compilations errors. -#if defined(_MSC_VER) -#define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message)) -#elif defined(__GNUC__) -#define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message))) -#else -#define GTEST_INTERNAL_DEPRECATED(message) -#endif - -#endif // !defined(GTEST_INTERNAL_DEPRECATED) - -#if GTEST_HAS_ABSL -// Always use absl::any for UniversalPrinter<> specializations if googletest -// is built with absl support. -#define GTEST_INTERNAL_HAS_ANY 1 -#include "absl/types/any.h" -namespace testing { -namespace internal { -using Any = ::absl::any; -} // namespace internal -} // namespace testing -#else -#ifdef __has_include -#if __has_include() && __cplusplus >= 201703L -// Otherwise for C++17 and higher use std::any for UniversalPrinter<> -// specializations. -#define GTEST_INTERNAL_HAS_ANY 1 -#include -namespace testing { -namespace internal { -using Any = ::std::any; -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::any is not -// supported. -#endif // __has_include() && __cplusplus >= 201703L -#endif // __has_include -#endif // GTEST_HAS_ABSL - -#if GTEST_HAS_ABSL -// Always use absl::optional for UniversalPrinter<> specializations if -// googletest is built with absl support. -#define GTEST_INTERNAL_HAS_OPTIONAL 1 -#include "absl/types/optional.h" -namespace testing { -namespace internal { -template -using Optional = ::absl::optional; -} // namespace internal -} // namespace testing -#else -#ifdef __has_include -#if __has_include() && __cplusplus >= 201703L -// Otherwise for C++17 and higher use std::optional for UniversalPrinter<> -// specializations. -#define GTEST_INTERNAL_HAS_OPTIONAL 1 -#include -namespace testing { -namespace internal { -template -using Optional = ::std::optional; -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::optional is not -// supported. -#endif // __has_include() && __cplusplus >= 201703L -#endif // __has_include -#endif // GTEST_HAS_ABSL - -#if GTEST_HAS_ABSL -// Always use absl::string_view for Matcher<> specializations if googletest -// is built with absl support. -# define GTEST_INTERNAL_HAS_STRING_VIEW 1 -#include "absl/strings/string_view.h" -namespace testing { -namespace internal { -using StringView = ::absl::string_view; -} // namespace internal -} // namespace testing -#else -# ifdef __has_include -# if __has_include() && __cplusplus >= 201703L -// Otherwise for C++17 and higher use std::string_view for Matcher<> -// specializations. -# define GTEST_INTERNAL_HAS_STRING_VIEW 1 -#include -namespace testing { -namespace internal { -using StringView = ::std::string_view; -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::string_view is not -// supported. -# endif // __has_include() && __cplusplus >= 201703L -# endif // __has_include -#endif // GTEST_HAS_ABSL - -#if GTEST_HAS_ABSL -// Always use absl::variant for UniversalPrinter<> specializations if googletest -// is built with absl support. -#define GTEST_INTERNAL_HAS_VARIANT 1 -#include "absl/types/variant.h" -namespace testing { -namespace internal { -template -using Variant = ::absl::variant; -} // namespace internal -} // namespace testing -#else -#ifdef __has_include -#if __has_include() && __cplusplus >= 201703L -// Otherwise for C++17 and higher use std::variant for UniversalPrinter<> -// specializations. -#define GTEST_INTERNAL_HAS_VARIANT 1 -#include -namespace testing { -namespace internal { -template -using Variant = ::std::variant; -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::variant is not supported. -#endif // __has_include() && __cplusplus >= 201703L -#endif // __has_include -#endif // GTEST_HAS_ABSL - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ - -#if GTEST_OS_LINUX -# include -# include -# include -# include -#endif // GTEST_OS_LINUX - -#if GTEST_HAS_EXCEPTIONS -# include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file defines the Message class. -// -// IMPORTANT NOTE: Due to limitation of the C++ language, we have to -// leave some internal implementation details in this header file. -// They are clearly marked by comments like this: -// -// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -// -// Such code is NOT meant to be used by a user directly, and is subject -// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user -// program! - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ - -#include -#include -#include - - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -// Ensures that there is at least one operator<< in the global namespace. -// See Message& operator<<(...) below for why. -void operator<<(const testing::internal::Secret&, int); - -namespace testing { - -// The Message class works like an ostream repeater. -// -// Typical usage: -// -// 1. You stream a bunch of values to a Message object. -// It will remember the text in a stringstream. -// 2. Then you stream the Message object to an ostream. -// This causes the text in the Message to be streamed -// to the ostream. -// -// For example; -// -// testing::Message foo; -// foo << 1 << " != " << 2; -// std::cout << foo; -// -// will print "1 != 2". -// -// Message is not intended to be inherited from. In particular, its -// destructor is not virtual. -// -// Note that stringstream behaves differently in gcc and in MSVC. You -// can stream a NULL char pointer to it in the former, but not in the -// latter (it causes an access violation if you do). The Message -// class hides this difference by treating a NULL char pointer as -// "(null)". -class GTEST_API_ Message { - private: - // The type of basic IO manipulators (endl, ends, and flush) for - // narrow streams. - typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&); - - public: - // Constructs an empty Message. - Message(); - - // Copy constructor. - Message(const Message& msg) : ss_(new ::std::stringstream) { // NOLINT - *ss_ << msg.GetString(); - } - - // Constructs a Message from a C-string. - explicit Message(const char* str) : ss_(new ::std::stringstream) { - *ss_ << str; - } - - // Streams a non-pointer value to this object. - template - inline Message& operator <<(const T& val) { - // Some libraries overload << for STL containers. These - // overloads are defined in the global namespace instead of ::std. - // - // C++'s symbol lookup rule (i.e. Koenig lookup) says that these - // overloads are visible in either the std namespace or the global - // namespace, but not other namespaces, including the testing - // namespace which Google Test's Message class is in. - // - // To allow STL containers (and other types that has a << operator - // defined in the global namespace) to be used in Google Test - // assertions, testing::Message must access the custom << operator - // from the global namespace. With this using declaration, - // overloads of << defined in the global namespace and those - // visible via Koenig lookup are both exposed in this function. - using ::operator <<; - *ss_ << val; - return *this; - } - - // Streams a pointer value to this object. - // - // This function is an overload of the previous one. When you - // stream a pointer to a Message, this definition will be used as it - // is more specialized. (The C++ Standard, section - // [temp.func.order].) If you stream a non-pointer, then the - // previous definition will be used. - // - // The reason for this overload is that streaming a NULL pointer to - // ostream is undefined behavior. Depending on the compiler, you - // may get "0", "(nil)", "(null)", or an access violation. To - // ensure consistent result across compilers, we always treat NULL - // as "(null)". - template - inline Message& operator <<(T* const& pointer) { // NOLINT - if (pointer == nullptr) { - *ss_ << "(null)"; - } else { - *ss_ << pointer; - } - return *this; - } - - // Since the basic IO manipulators are overloaded for both narrow - // and wide streams, we have to provide this specialized definition - // of operator <<, even though its body is the same as the - // templatized version above. Without this definition, streaming - // endl or other basic IO manipulators to Message will confuse the - // compiler. - Message& operator <<(BasicNarrowIoManip val) { - *ss_ << val; - return *this; - } - - // Instead of 1/0, we want to see true/false for bool values. - Message& operator <<(bool b) { - return *this << (b ? "true" : "false"); - } - - // These two overloads allow streaming a wide C string to a Message - // using the UTF-8 encoding. - Message& operator <<(const wchar_t* wide_c_str); - Message& operator <<(wchar_t* wide_c_str); - -#if GTEST_HAS_STD_WSTRING - // Converts the given wide string to a narrow string using the UTF-8 - // encoding, and streams the result to this Message object. - Message& operator <<(const ::std::wstring& wstr); -#endif // GTEST_HAS_STD_WSTRING - - // Gets the text streamed to this object so far as an std::string. - // Each '\0' character in the buffer is replaced with "\\0". - // - // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - std::string GetString() const; - - private: - // We'll hold the text streamed to this object here. - const std::unique_ptr< ::std::stringstream> ss_; - - // We declare (but don't implement) this to prevent the compiler - // from implementing the assignment operator. - void operator=(const Message&); -}; - -// Streams a Message to an ostream. -inline std::ostream& operator <<(std::ostream& os, const Message& sb) { - return os << sb.GetString(); -} - -namespace internal { - -// Converts a streamable value to an std::string. A NULL pointer is -// converted to "(null)". When the input value is a ::string, -// ::std::string, ::wstring, or ::std::wstring object, each NUL -// character in it is replaced with "\\0". -template -std::string StreamableToString(const T& streamable) { - return (Message() << streamable).GetString(); -} - -} // namespace internal -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Google Test filepath utilities -// -// This header file declares classes and functions used internally by -// Google Test. They are subject to change without notice. -// -// This file is #included in gtest/internal/gtest-internal.h. -// Do not include this header file separately! - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ - -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file declares the String class and functions used internally by -// Google Test. They are subject to change without notice. They should not used -// by code external to Google Test. -// -// This header file is #included by gtest-internal.h. -// It should not be #included by other files. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ - -#ifdef __BORLANDC__ -// string.h is not guaranteed to provide strcpy on C++ Builder. -# include -#endif - -#include -#include -#include - - -namespace testing { -namespace internal { - -// String - an abstract class holding static string utilities. -class GTEST_API_ String { - public: - // Static utility methods - - // Clones a 0-terminated C string, allocating memory using new. The - // caller is responsible for deleting the return value using - // delete[]. Returns the cloned string, or NULL if the input is - // NULL. - // - // This is different from strdup() in string.h, which allocates - // memory using malloc(). - static const char* CloneCString(const char* c_str); - -#if GTEST_OS_WINDOWS_MOBILE - // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be - // able to pass strings to Win32 APIs on CE we need to convert them - // to 'Unicode', UTF-16. - - // Creates a UTF-16 wide string from the given ANSI string, allocating - // memory using new. The caller is responsible for deleting the return - // value using delete[]. Returns the wide string, or NULL if the - // input is NULL. - // - // The wide string is created using the ANSI codepage (CP_ACP) to - // match the behaviour of the ANSI versions of Win32 calls and the - // C runtime. - static LPCWSTR AnsiToUtf16(const char* c_str); - - // Creates an ANSI string from the given wide string, allocating - // memory using new. The caller is responsible for deleting the return - // value using delete[]. Returns the ANSI string, or NULL if the - // input is NULL. - // - // The returned string is created using the ANSI codepage (CP_ACP) to - // match the behaviour of the ANSI versions of Win32 calls and the - // C runtime. - static const char* Utf16ToAnsi(LPCWSTR utf16_str); -#endif - - // Compares two C strings. Returns true if and only if they have the same - // content. - // - // Unlike strcmp(), this function can handle NULL argument(s). A - // NULL C string is considered different to any non-NULL C string, - // including the empty string. - static bool CStringEquals(const char* lhs, const char* rhs); - - // Converts a wide C string to a String using the UTF-8 encoding. - // NULL will be converted to "(null)". If an error occurred during - // the conversion, "(failed to convert from wide string)" is - // returned. - static std::string ShowWideCString(const wchar_t* wide_c_str); - - // Compares two wide C strings. Returns true if and only if they have the - // same content. - // - // Unlike wcscmp(), this function can handle NULL argument(s). A - // NULL C string is considered different to any non-NULL C string, - // including the empty string. - static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); - - // Compares two C strings, ignoring case. Returns true if and only if - // they have the same content. - // - // Unlike strcasecmp(), this function can handle NULL argument(s). - // A NULL C string is considered different to any non-NULL C string, - // including the empty string. - static bool CaseInsensitiveCStringEquals(const char* lhs, - const char* rhs); - - // Compares two wide C strings, ignoring case. Returns true if and only if - // they have the same content. - // - // Unlike wcscasecmp(), this function can handle NULL argument(s). - // A NULL C string is considered different to any non-NULL wide C string, - // including the empty string. - // NB: The implementations on different platforms slightly differ. - // On windows, this method uses _wcsicmp which compares according to LC_CTYPE - // environment variable. On GNU platform this method uses wcscasecmp - // which compares according to LC_CTYPE category of the current locale. - // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the - // current locale. - static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, - const wchar_t* rhs); - - // Returns true if and only if the given string ends with the given suffix, - // ignoring case. Any string is considered to end with an empty suffix. - static bool EndsWithCaseInsensitive( - const std::string& str, const std::string& suffix); - - // Formats an int value as "%02d". - static std::string FormatIntWidth2(int value); // "%02d" for width == 2 - - // Formats an int value to given width with leading zeros. - static std::string FormatIntWidthN(int value, int width); - - // Formats an int value as "%X". - static std::string FormatHexInt(int value); - - // Formats an int value as "%X". - static std::string FormatHexUInt32(uint32_t value); - - // Formats a byte as "%02X". - static std::string FormatByte(unsigned char value); - - private: - String(); // Not meant to be instantiated. -}; // class String - -// Gets the content of the stringstream's buffer as an std::string. Each '\0' -// character in the buffer is replaced with "\\0". -GTEST_API_ std::string StringStreamToString(::std::stringstream* stream); - -} // namespace internal -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -namespace testing { -namespace internal { - -// FilePath - a class for file and directory pathname manipulation which -// handles platform-specific conventions (like the pathname separator). -// Used for helper functions for naming files in a directory for xml output. -// Except for Set methods, all methods are const or static, which provides an -// "immutable value object" -- useful for peace of mind. -// A FilePath with a value ending in a path separator ("like/this/") represents -// a directory, otherwise it is assumed to represent a file. In either case, -// it may or may not represent an actual file or directory in the file system. -// Names are NOT checked for syntax correctness -- no checking for illegal -// characters, malformed paths, etc. - -class GTEST_API_ FilePath { - public: - FilePath() : pathname_("") { } - FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { } - - explicit FilePath(const std::string& pathname) : pathname_(pathname) { - Normalize(); - } - - FilePath& operator=(const FilePath& rhs) { - Set(rhs); - return *this; - } - - void Set(const FilePath& rhs) { - pathname_ = rhs.pathname_; - } - - const std::string& string() const { return pathname_; } - const char* c_str() const { return pathname_.c_str(); } - - // Returns the current working directory, or "" if unsuccessful. - static FilePath GetCurrentDir(); - - // Given directory = "dir", base_name = "test", number = 0, - // extension = "xml", returns "dir/test.xml". If number is greater - // than zero (e.g., 12), returns "dir/test_12.xml". - // On Windows platform, uses \ as the separator rather than /. - static FilePath MakeFileName(const FilePath& directory, - const FilePath& base_name, - int number, - const char* extension); - - // Given directory = "dir", relative_path = "test.xml", - // returns "dir/test.xml". - // On Windows, uses \ as the separator rather than /. - static FilePath ConcatPaths(const FilePath& directory, - const FilePath& relative_path); - - // Returns a pathname for a file that does not currently exist. The pathname - // will be directory/base_name.extension or - // directory/base_name_.extension if directory/base_name.extension - // already exists. The number will be incremented until a pathname is found - // that does not already exist. - // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. - // There could be a race condition if two or more processes are calling this - // function at the same time -- they could both pick the same filename. - static FilePath GenerateUniqueFileName(const FilePath& directory, - const FilePath& base_name, - const char* extension); - - // Returns true if and only if the path is "". - bool IsEmpty() const { return pathname_.empty(); } - - // If input name has a trailing separator character, removes it and returns - // the name, otherwise return the name string unmodified. - // On Windows platform, uses \ as the separator, other platforms use /. - FilePath RemoveTrailingPathSeparator() const; - - // Returns a copy of the FilePath with the directory part removed. - // Example: FilePath("path/to/file").RemoveDirectoryName() returns - // FilePath("file"). If there is no directory part ("just_a_file"), it returns - // the FilePath unmodified. If there is no file part ("just_a_dir/") it - // returns an empty FilePath (""). - // On Windows platform, '\' is the path separator, otherwise it is '/'. - FilePath RemoveDirectoryName() const; - - // RemoveFileName returns the directory path with the filename removed. - // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". - // If the FilePath is "a_file" or "/a_file", RemoveFileName returns - // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does - // not have a file, like "just/a/dir/", it returns the FilePath unmodified. - // On Windows platform, '\' is the path separator, otherwise it is '/'. - FilePath RemoveFileName() const; - - // Returns a copy of the FilePath with the case-insensitive extension removed. - // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns - // FilePath("dir/file"). If a case-insensitive extension is not - // found, returns a copy of the original FilePath. - FilePath RemoveExtension(const char* extension) const; - - // Creates directories so that path exists. Returns true if successful or if - // the directories already exist; returns false if unable to create - // directories for any reason. Will also return false if the FilePath does - // not represent a directory (that is, it doesn't end with a path separator). - bool CreateDirectoriesRecursively() const; - - // Create the directory so that path exists. Returns true if successful or - // if the directory already exists; returns false if unable to create the - // directory for any reason, including if the parent directory does not - // exist. Not named "CreateDirectory" because that's a macro on Windows. - bool CreateFolder() const; - - // Returns true if FilePath describes something in the file-system, - // either a file, directory, or whatever, and that something exists. - bool FileOrDirectoryExists() const; - - // Returns true if pathname describes a directory in the file-system - // that exists. - bool DirectoryExists() const; - - // Returns true if FilePath ends with a path separator, which indicates that - // it is intended to represent a directory. Returns false otherwise. - // This does NOT check that a directory (or file) actually exists. - bool IsDirectory() const; - - // Returns true if pathname describes a root directory. (Windows has one - // root directory per disk drive.) - bool IsRootDirectory() const; - - // Returns true if pathname describes an absolute path. - bool IsAbsolutePath() const; - - private: - // Replaces multiple consecutive separators with a single separator. - // For example, "bar///foo" becomes "bar/foo". Does not eliminate other - // redundancies that might be in a pathname involving "." or "..". - // - // A pathname with multiple consecutive separators may occur either through - // user error or as a result of some scripts or APIs that generate a pathname - // with a trailing separator. On other platforms the same API or script - // may NOT generate a pathname with a trailing "/". Then elsewhere that - // pathname may have another "/" and pathname components added to it, - // without checking for the separator already being there. - // The script language and operating system may allow paths like "foo//bar" - // but some of the functions in FilePath will not handle that correctly. In - // particular, RemoveTrailingPathSeparator() only removes one separator, and - // it is called in CreateDirectoriesRecursively() assuming that it will change - // a pathname from directory syntax (trailing separator) to filename syntax. - // - // On Windows this method also replaces the alternate path separator '/' with - // the primary path separator '\\', so that for example "bar\\/\\foo" becomes - // "bar\\foo". - - void Normalize(); - - // Returns a pointer to the last occurrence of a valid path separator in - // the FilePath. On Windows, for example, both '/' and '\' are valid path - // separators. Returns NULL if no path separator was found. - const char* FindLastPathSeparator() const; - - std::string pathname_; -}; // class FilePath - -} // namespace internal -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ -// Copyright 2008 Google Inc. -// All Rights Reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Type utilities needed for implementing typed and type-parameterized -// tests. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ - - -// #ifdef __GNUC__ is too general here. It is possible to use gcc without using -// libstdc++ (which is where cxxabi.h comes from). -# if GTEST_HAS_CXXABI_H_ -# include -# elif defined(__HP_aCC) -# include -# endif // GTEST_HASH_CXXABI_H_ - -namespace testing { -namespace internal { - -// Canonicalizes a given name with respect to the Standard C++ Library. -// This handles removing the inline namespace within `std` that is -// used by various standard libraries (e.g., `std::__1`). Names outside -// of namespace std are returned unmodified. -inline std::string CanonicalizeForStdLibVersioning(std::string s) { - static const char prefix[] = "std::__"; - if (s.compare(0, strlen(prefix), prefix) == 0) { - std::string::size_type end = s.find("::", strlen(prefix)); - if (end != s.npos) { - // Erase everything between the initial `std` and the second `::`. - s.erase(strlen("std"), end - strlen("std")); - } - } - return s; -} - -#if GTEST_HAS_RTTI -// GetTypeName(const std::type_info&) returns a human-readable name of type T. -inline std::string GetTypeName(const std::type_info& type) { - const char* const name = type.name(); -#if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC) - int status = 0; - // gcc's implementation of typeid(T).name() mangles the type name, - // so we have to demangle it. -#if GTEST_HAS_CXXABI_H_ - using abi::__cxa_demangle; -#endif // GTEST_HAS_CXXABI_H_ - char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status); - const std::string name_str(status == 0 ? readable_name : name); - free(readable_name); - return CanonicalizeForStdLibVersioning(name_str); -#else - return name; -#endif // GTEST_HAS_CXXABI_H_ || __HP_aCC -} -#endif // GTEST_HAS_RTTI - -// GetTypeName() returns a human-readable name of type T if and only if -// RTTI is enabled, otherwise it returns a dummy type name. -// NB: This function is also used in Google Mock, so don't move it inside of -// the typed-test-only section below. -template -std::string GetTypeName() { -#if GTEST_HAS_RTTI - return GetTypeName(typeid(T)); -#else - return ""; -#endif // GTEST_HAS_RTTI -} - -// A unique type indicating an empty node -struct None {}; - -# define GTEST_TEMPLATE_ template class - -// The template "selector" struct TemplateSel is used to -// represent Tmpl, which must be a class template with one type -// parameter, as a type. TemplateSel::Bind::type is defined -// as the type Tmpl. This allows us to actually instantiate the -// template "selected" by TemplateSel. -// -// This trick is necessary for simulating typedef for class templates, -// which C++ doesn't support directly. -template -struct TemplateSel { - template - struct Bind { - typedef Tmpl type; - }; -}; - -# define GTEST_BIND_(TmplSel, T) \ - TmplSel::template Bind::type - -template -struct Templates { - using Head = TemplateSel; - using Tail = Templates; -}; - -template -struct Templates { - using Head = TemplateSel; - using Tail = None; -}; - -// Tuple-like type lists -template -struct Types { - using Head = Head_; - using Tail = Types; -}; - -template -struct Types { - using Head = Head_; - using Tail = None; -}; - -// Helper metafunctions to tell apart a single type from types -// generated by ::testing::Types -template -struct ProxyTypeList { - using type = Types; -}; - -template -struct is_proxy_type_list : std::false_type {}; - -template -struct is_proxy_type_list> : std::true_type {}; - -// Generator which conditionally creates type lists. -// It recognizes if a requested type list should be created -// and prevents creating a new type list nested within another one. -template -struct GenerateTypeList { - private: - using proxy = typename std::conditional::value, T, - ProxyTypeList>::type; - - public: - using type = typename proxy::type; -}; - -} // namespace internal - -template -using Types = internal::ProxyTypeList; - -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ - -// Due to C++ preprocessor weirdness, we need double indirection to -// concatenate two tokens when one of them is __LINE__. Writing -// -// foo ## __LINE__ -// -// will result in the token foo__LINE__, instead of foo followed by -// the current line number. For more details, see -// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6 -#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) -#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar - -// Stringifies its argument. -// Work around a bug in visual studio which doesn't accept code like this: -// -// #define GTEST_STRINGIFY_(name) #name -// #define MACRO(a, b, c) ... GTEST_STRINGIFY_(a) ... -// MACRO(, x, y) -// -// Complaining about the argument to GTEST_STRINGIFY_ being empty. -// This is allowed by the spec. -#define GTEST_STRINGIFY_HELPER_(name, ...) #name -#define GTEST_STRINGIFY_(...) GTEST_STRINGIFY_HELPER_(__VA_ARGS__, ) - -namespace proto2 { -class MessageLite; -} - -namespace testing { - -// Forward declarations. - -class AssertionResult; // Result of an assertion. -class Message; // Represents a failure message. -class Test; // Represents a test. -class TestInfo; // Information about a test. -class TestPartResult; // Result of a test part. -class UnitTest; // A collection of test suites. - -template -::std::string PrintToString(const T& value); - -namespace internal { - -struct TraceInfo; // Information about a trace point. -class TestInfoImpl; // Opaque implementation of TestInfo -class UnitTestImpl; // Opaque implementation of UnitTest - -// The text used in failure messages to indicate the start of the -// stack trace. -GTEST_API_ extern const char kStackTraceMarker[]; - -// An IgnoredValue object can be implicitly constructed from ANY value. -class IgnoredValue { - struct Sink {}; - public: - // This constructor template allows any value to be implicitly - // converted to IgnoredValue. The object has no data member and - // doesn't try to remember anything about the argument. We - // deliberately omit the 'explicit' keyword in order to allow the - // conversion to be implicit. - // Disable the conversion if T already has a magical conversion operator. - // Otherwise we get ambiguity. - template ::value, - int>::type = 0> - IgnoredValue(const T& /* ignored */) {} // NOLINT(runtime/explicit) -}; - -// Appends the user-supplied message to the Google-Test-generated message. -GTEST_API_ std::string AppendUserMessage( - const std::string& gtest_msg, const Message& user_msg); - -#if GTEST_HAS_EXCEPTIONS - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4275 \ -/* an exported class was derived from a class that was not exported */) - -// This exception is thrown by (and only by) a failed Google Test -// assertion when GTEST_FLAG(throw_on_failure) is true (if exceptions -// are enabled). We derive it from std::runtime_error, which is for -// errors presumably detectable only at run time. Since -// std::runtime_error inherits from std::exception, many testing -// frameworks know how to extract and print the message inside it. -class GTEST_API_ GoogleTestFailureException : public ::std::runtime_error { - public: - explicit GoogleTestFailureException(const TestPartResult& failure); -}; - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4275 - -#endif // GTEST_HAS_EXCEPTIONS - -namespace edit_distance { -// Returns the optimal edits to go from 'left' to 'right'. -// All edits cost the same, with replace having lower priority than -// add/remove. -// Simple implementation of the Wagner-Fischer algorithm. -// See http://en.wikipedia.org/wiki/Wagner-Fischer_algorithm -enum EditType { kMatch, kAdd, kRemove, kReplace }; -GTEST_API_ std::vector CalculateOptimalEdits( - const std::vector& left, const std::vector& right); - -// Same as above, but the input is represented as strings. -GTEST_API_ std::vector CalculateOptimalEdits( - const std::vector& left, - const std::vector& right); - -// Create a diff of the input strings in Unified diff format. -GTEST_API_ std::string CreateUnifiedDiff(const std::vector& left, - const std::vector& right, - size_t context = 2); - -} // namespace edit_distance - -// Calculate the diff between 'left' and 'right' and return it in unified diff -// format. -// If not null, stores in 'total_line_count' the total number of lines found -// in left + right. -GTEST_API_ std::string DiffStrings(const std::string& left, - const std::string& right, - size_t* total_line_count); - -// Constructs and returns the message for an equality assertion -// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. -// -// The first four parameters are the expressions used in the assertion -// and their values, as strings. For example, for ASSERT_EQ(foo, bar) -// where foo is 5 and bar is 6, we have: -// -// expected_expression: "foo" -// actual_expression: "bar" -// expected_value: "5" -// actual_value: "6" -// -// The ignoring_case parameter is true if and only if the assertion is a -// *_STRCASEEQ*. When it's true, the string " (ignoring case)" will -// be inserted into the message. -GTEST_API_ AssertionResult EqFailure(const char* expected_expression, - const char* actual_expression, - const std::string& expected_value, - const std::string& actual_value, - bool ignoring_case); - -// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. -GTEST_API_ std::string GetBoolAssertionFailureMessage( - const AssertionResult& assertion_result, - const char* expression_text, - const char* actual_predicate_value, - const char* expected_predicate_value); - -// This template class represents an IEEE floating-point number -// (either single-precision or double-precision, depending on the -// template parameters). -// -// The purpose of this class is to do more sophisticated number -// comparison. (Due to round-off error, etc, it's very unlikely that -// two floating-points will be equal exactly. Hence a naive -// comparison by the == operation often doesn't work.) -// -// Format of IEEE floating-point: -// -// The most-significant bit being the leftmost, an IEEE -// floating-point looks like -// -// sign_bit exponent_bits fraction_bits -// -// Here, sign_bit is a single bit that designates the sign of the -// number. -// -// For float, there are 8 exponent bits and 23 fraction bits. -// -// For double, there are 11 exponent bits and 52 fraction bits. -// -// More details can be found at -// http://en.wikipedia.org/wiki/IEEE_floating-point_standard. -// -// Template parameter: -// -// RawType: the raw floating-point type (either float or double) -template -class FloatingPoint { - public: - // Defines the unsigned integer type that has the same size as the - // floating point number. - typedef typename TypeWithSize::UInt Bits; - - // Constants. - - // # of bits in a number. - static const size_t kBitCount = 8*sizeof(RawType); - - // # of fraction bits in a number. - static const size_t kFractionBitCount = - std::numeric_limits::digits - 1; - - // # of exponent bits in a number. - static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount; - - // The mask for the sign bit. - static const Bits kSignBitMask = static_cast(1) << (kBitCount - 1); - - // The mask for the fraction bits. - static const Bits kFractionBitMask = - ~static_cast(0) >> (kExponentBitCount + 1); - - // The mask for the exponent bits. - static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask); - - // How many ULP's (Units in the Last Place) we want to tolerate when - // comparing two numbers. The larger the value, the more error we - // allow. A 0 value means that two numbers must be exactly the same - // to be considered equal. - // - // The maximum error of a single floating-point operation is 0.5 - // units in the last place. On Intel CPU's, all floating-point - // calculations are done with 80-bit precision, while double has 64 - // bits. Therefore, 4 should be enough for ordinary use. - // - // See the following article for more details on ULP: - // http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ - static const uint32_t kMaxUlps = 4; - - // Constructs a FloatingPoint from a raw floating-point number. - // - // On an Intel CPU, passing a non-normalized NAN (Not a Number) - // around may change its bits, although the new value is guaranteed - // to be also a NAN. Therefore, don't expect this constructor to - // preserve the bits in x when x is a NAN. - explicit FloatingPoint(const RawType& x) { u_.value_ = x; } - - // Static methods - - // Reinterprets a bit pattern as a floating-point number. - // - // This function is needed to test the AlmostEquals() method. - static RawType ReinterpretBits(const Bits bits) { - FloatingPoint fp(0); - fp.u_.bits_ = bits; - return fp.u_.value_; - } - - // Returns the floating-point number that represent positive infinity. - static RawType Infinity() { - return ReinterpretBits(kExponentBitMask); - } - - // Returns the maximum representable finite floating-point number. - static RawType Max(); - - // Non-static methods - - // Returns the bits that represents this number. - const Bits &bits() const { return u_.bits_; } - - // Returns the exponent bits of this number. - Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } - - // Returns the fraction bits of this number. - Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } - - // Returns the sign bit of this number. - Bits sign_bit() const { return kSignBitMask & u_.bits_; } - - // Returns true if and only if this is NAN (not a number). - bool is_nan() const { - // It's a NAN if the exponent bits are all ones and the fraction - // bits are not entirely zeros. - return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0); - } - - // Returns true if and only if this number is at most kMaxUlps ULP's away - // from rhs. In particular, this function: - // - // - returns false if either number is (or both are) NAN. - // - treats really large numbers as almost equal to infinity. - // - thinks +0.0 and -0.0 are 0 DLP's apart. - bool AlmostEquals(const FloatingPoint& rhs) const { - // The IEEE standard says that any comparison operation involving - // a NAN must return false. - if (is_nan() || rhs.is_nan()) return false; - - return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) - <= kMaxUlps; - } - - private: - // The data type used to store the actual floating-point number. - union FloatingPointUnion { - RawType value_; // The raw floating-point number. - Bits bits_; // The bits that represent the number. - }; - - // Converts an integer from the sign-and-magnitude representation to - // the biased representation. More precisely, let N be 2 to the - // power of (kBitCount - 1), an integer x is represented by the - // unsigned number x + N. - // - // For instance, - // - // -N + 1 (the most negative number representable using - // sign-and-magnitude) is represented by 1; - // 0 is represented by N; and - // N - 1 (the biggest number representable using - // sign-and-magnitude) is represented by 2N - 1. - // - // Read http://en.wikipedia.org/wiki/Signed_number_representations - // for more details on signed number representations. - static Bits SignAndMagnitudeToBiased(const Bits &sam) { - if (kSignBitMask & sam) { - // sam represents a negative number. - return ~sam + 1; - } else { - // sam represents a positive number. - return kSignBitMask | sam; - } - } - - // Given two numbers in the sign-and-magnitude representation, - // returns the distance between them as an unsigned number. - static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1, - const Bits &sam2) { - const Bits biased1 = SignAndMagnitudeToBiased(sam1); - const Bits biased2 = SignAndMagnitudeToBiased(sam2); - return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); - } - - FloatingPointUnion u_; -}; - -// We cannot use std::numeric_limits::max() as it clashes with the max() -// macro defined by . -template <> -inline float FloatingPoint::Max() { return FLT_MAX; } -template <> -inline double FloatingPoint::Max() { return DBL_MAX; } - -// Typedefs the instances of the FloatingPoint template class that we -// care to use. -typedef FloatingPoint Float; -typedef FloatingPoint Double; - -// In order to catch the mistake of putting tests that use different -// test fixture classes in the same test suite, we need to assign -// unique IDs to fixture classes and compare them. The TypeId type is -// used to hold such IDs. The user should treat TypeId as an opaque -// type: the only operation allowed on TypeId values is to compare -// them for equality using the == operator. -typedef const void* TypeId; - -template -class TypeIdHelper { - public: - // dummy_ must not have a const type. Otherwise an overly eager - // compiler (e.g. MSVC 7.1 & 8.0) may try to merge - // TypeIdHelper::dummy_ for different Ts as an "optimization". - static bool dummy_; -}; - -template -bool TypeIdHelper::dummy_ = false; - -// GetTypeId() returns the ID of type T. Different values will be -// returned for different types. Calling the function twice with the -// same type argument is guaranteed to return the same ID. -template -TypeId GetTypeId() { - // The compiler is required to allocate a different - // TypeIdHelper::dummy_ variable for each T used to instantiate - // the template. Therefore, the address of dummy_ is guaranteed to - // be unique. - return &(TypeIdHelper::dummy_); -} - -// Returns the type ID of ::testing::Test. Always call this instead -// of GetTypeId< ::testing::Test>() to get the type ID of -// ::testing::Test, as the latter may give the wrong result due to a -// suspected linker bug when compiling Google Test as a Mac OS X -// framework. -GTEST_API_ TypeId GetTestTypeId(); - -// Defines the abstract factory interface that creates instances -// of a Test object. -class TestFactoryBase { - public: - virtual ~TestFactoryBase() {} - - // Creates a test instance to run. The instance is both created and destroyed - // within TestInfoImpl::Run() - virtual Test* CreateTest() = 0; - - protected: - TestFactoryBase() {} - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase); -}; - -// This class provides implementation of TeastFactoryBase interface. -// It is used in TEST and TEST_F macros. -template -class TestFactoryImpl : public TestFactoryBase { - public: - Test* CreateTest() override { return new TestClass; } -}; - -#if GTEST_OS_WINDOWS - -// Predicate-formatters for implementing the HRESULT checking macros -// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED} -// We pass a long instead of HRESULT to avoid causing an -// include dependency for the HRESULT type. -GTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr, - long hr); // NOLINT -GTEST_API_ AssertionResult IsHRESULTFailure(const char* expr, - long hr); // NOLINT - -#endif // GTEST_OS_WINDOWS - -// Types of SetUpTestSuite() and TearDownTestSuite() functions. -using SetUpTestSuiteFunc = void (*)(); -using TearDownTestSuiteFunc = void (*)(); - -struct CodeLocation { - CodeLocation(const std::string& a_file, int a_line) - : file(a_file), line(a_line) {} - - std::string file; - int line; -}; - -// Helper to identify which setup function for TestCase / TestSuite to call. -// Only one function is allowed, either TestCase or TestSute but not both. - -// Utility functions to help SuiteApiResolver -using SetUpTearDownSuiteFuncType = void (*)(); - -inline SetUpTearDownSuiteFuncType GetNotDefaultOrNull( - SetUpTearDownSuiteFuncType a, SetUpTearDownSuiteFuncType def) { - return a == def ? nullptr : a; -} - -template -// Note that SuiteApiResolver inherits from T because -// SetUpTestSuite()/TearDownTestSuite() could be protected. Ths way -// SuiteApiResolver can access them. -struct SuiteApiResolver : T { - // testing::Test is only forward declared at this point. So we make it a - // dependend class for the compiler to be OK with it. - using Test = - typename std::conditional::type; - - static SetUpTearDownSuiteFuncType GetSetUpCaseOrSuite(const char* filename, - int line_num) { -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - SetUpTearDownSuiteFuncType test_case_fp = - GetNotDefaultOrNull(&T::SetUpTestCase, &Test::SetUpTestCase); - SetUpTearDownSuiteFuncType test_suite_fp = - GetNotDefaultOrNull(&T::SetUpTestSuite, &Test::SetUpTestSuite); - - GTEST_CHECK_(!test_case_fp || !test_suite_fp) - << "Test can not provide both SetUpTestSuite and SetUpTestCase, please " - "make sure there is only one present at " - << filename << ":" << line_num; - - return test_case_fp != nullptr ? test_case_fp : test_suite_fp; -#else - (void)(filename); - (void)(line_num); - return &T::SetUpTestSuite; -#endif - } - - static SetUpTearDownSuiteFuncType GetTearDownCaseOrSuite(const char* filename, - int line_num) { -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - SetUpTearDownSuiteFuncType test_case_fp = - GetNotDefaultOrNull(&T::TearDownTestCase, &Test::TearDownTestCase); - SetUpTearDownSuiteFuncType test_suite_fp = - GetNotDefaultOrNull(&T::TearDownTestSuite, &Test::TearDownTestSuite); - - GTEST_CHECK_(!test_case_fp || !test_suite_fp) - << "Test can not provide both TearDownTestSuite and TearDownTestCase," - " please make sure there is only one present at" - << filename << ":" << line_num; - - return test_case_fp != nullptr ? test_case_fp : test_suite_fp; -#else - (void)(filename); - (void)(line_num); - return &T::TearDownTestSuite; -#endif - } -}; - -// Creates a new TestInfo object and registers it with Google Test; -// returns the created object. -// -// Arguments: -// -// test_suite_name: name of the test suite -// name: name of the test -// type_param: the name of the test's type parameter, or NULL if -// this is not a typed or a type-parameterized test. -// value_param: text representation of the test's value parameter, -// or NULL if this is not a type-parameterized test. -// code_location: code location where the test is defined -// fixture_class_id: ID of the test fixture class -// set_up_tc: pointer to the function that sets up the test suite -// tear_down_tc: pointer to the function that tears down the test suite -// factory: pointer to the factory that creates a test object. -// The newly created TestInfo instance will assume -// ownership of the factory object. -GTEST_API_ TestInfo* MakeAndRegisterTestInfo( - const char* test_suite_name, const char* name, const char* type_param, - const char* value_param, CodeLocation code_location, - TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, - TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory); - -// If *pstr starts with the given prefix, modifies *pstr to be right -// past the prefix and returns true; otherwise leaves *pstr unchanged -// and returns false. None of pstr, *pstr, and prefix can be NULL. -GTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr); - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -// State of the definition of a type-parameterized test suite. -class GTEST_API_ TypedTestSuitePState { - public: - TypedTestSuitePState() : registered_(false) {} - - // Adds the given test name to defined_test_names_ and return true - // if the test suite hasn't been registered; otherwise aborts the - // program. - bool AddTestName(const char* file, int line, const char* case_name, - const char* test_name) { - if (registered_) { - fprintf(stderr, - "%s Test %s must be defined before " - "REGISTER_TYPED_TEST_SUITE_P(%s, ...).\n", - FormatFileLocation(file, line).c_str(), test_name, case_name); - fflush(stderr); - posix::Abort(); - } - registered_tests_.insert( - ::std::make_pair(test_name, CodeLocation(file, line))); - return true; - } - - bool TestExists(const std::string& test_name) const { - return registered_tests_.count(test_name) > 0; - } - - const CodeLocation& GetCodeLocation(const std::string& test_name) const { - RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name); - GTEST_CHECK_(it != registered_tests_.end()); - return it->second; - } - - // Verifies that registered_tests match the test names in - // defined_test_names_; returns registered_tests if successful, or - // aborts the program otherwise. - const char* VerifyRegisteredTestNames(const char* test_suite_name, - const char* file, int line, - const char* registered_tests); - - private: - typedef ::std::map RegisteredTestsMap; - - bool registered_; - RegisteredTestsMap registered_tests_; -}; - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -using TypedTestCasePState = TypedTestSuitePState; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -// Skips to the first non-space char after the first comma in 'str'; -// returns NULL if no comma is found in 'str'. -inline const char* SkipComma(const char* str) { - const char* comma = strchr(str, ','); - if (comma == nullptr) { - return nullptr; - } - while (IsSpace(*(++comma))) {} - return comma; -} - -// Returns the prefix of 'str' before the first comma in it; returns -// the entire string if it contains no comma. -inline std::string GetPrefixUntilComma(const char* str) { - const char* comma = strchr(str, ','); - return comma == nullptr ? str : std::string(str, comma); -} - -// Splits a given string on a given delimiter, populating a given -// vector with the fields. -void SplitString(const ::std::string& str, char delimiter, - ::std::vector< ::std::string>* dest); - -// The default argument to the template below for the case when the user does -// not provide a name generator. -struct DefaultNameGenerator { - template - static std::string GetName(int i) { - return StreamableToString(i); - } -}; - -template -struct NameGeneratorSelector { - typedef Provided type; -}; - -template -void GenerateNamesRecursively(internal::None, std::vector*, int) {} - -template -void GenerateNamesRecursively(Types, std::vector* result, int i) { - result->push_back(NameGenerator::template GetName(i)); - GenerateNamesRecursively(typename Types::Tail(), result, - i + 1); -} - -template -std::vector GenerateNames() { - std::vector result; - GenerateNamesRecursively(Types(), &result, 0); - return result; -} - -// TypeParameterizedTest::Register() -// registers a list of type-parameterized tests with Google Test. The -// return value is insignificant - we just need to return something -// such that we can call this function in a namespace scope. -// -// Implementation note: The GTEST_TEMPLATE_ macro declares a template -// template parameter. It's defined in gtest-type-util.h. -template -class TypeParameterizedTest { - public: - // 'index' is the index of the test in the type list 'Types' - // specified in INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, TestSuite, - // Types). Valid values for 'index' are [0, N - 1] where N is the - // length of Types. - static bool Register(const char* prefix, const CodeLocation& code_location, - const char* case_name, const char* test_names, int index, - const std::vector& type_names = - GenerateNames()) { - typedef typename Types::Head Type; - typedef Fixture FixtureClass; - typedef typename GTEST_BIND_(TestSel, Type) TestClass; - - // First, registers the first type-parameterized test in the type - // list. - MakeAndRegisterTestInfo( - (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + - "/" + type_names[static_cast(index)]) - .c_str(), - StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(), - GetTypeName().c_str(), - nullptr, // No value parameter. - code_location, GetTypeId(), - SuiteApiResolver::GetSetUpCaseOrSuite( - code_location.file.c_str(), code_location.line), - SuiteApiResolver::GetTearDownCaseOrSuite( - code_location.file.c_str(), code_location.line), - new TestFactoryImpl); - - // Next, recurses (at compile time) with the tail of the type list. - return TypeParameterizedTest::Register(prefix, - code_location, - case_name, - test_names, - index + 1, - type_names); - } -}; - -// The base case for the compile time recursion. -template -class TypeParameterizedTest { - public: - static bool Register(const char* /*prefix*/, const CodeLocation&, - const char* /*case_name*/, const char* /*test_names*/, - int /*index*/, - const std::vector& = - std::vector() /*type_names*/) { - return true; - } -}; - -GTEST_API_ void RegisterTypeParameterizedTestSuite(const char* test_suite_name, - CodeLocation code_location); -GTEST_API_ void RegisterTypeParameterizedTestSuiteInstantiation( - const char* case_name); - -// TypeParameterizedTestSuite::Register() -// registers *all combinations* of 'Tests' and 'Types' with Google -// Test. The return value is insignificant - we just need to return -// something such that we can call this function in a namespace scope. -template -class TypeParameterizedTestSuite { - public: - static bool Register(const char* prefix, CodeLocation code_location, - const TypedTestSuitePState* state, const char* case_name, - const char* test_names, - const std::vector& type_names = - GenerateNames()) { - RegisterTypeParameterizedTestSuiteInstantiation(case_name); - std::string test_name = StripTrailingSpaces( - GetPrefixUntilComma(test_names)); - if (!state->TestExists(test_name)) { - fprintf(stderr, "Failed to get code location for test %s.%s at %s.", - case_name, test_name.c_str(), - FormatFileLocation(code_location.file.c_str(), - code_location.line).c_str()); - fflush(stderr); - posix::Abort(); - } - const CodeLocation& test_location = state->GetCodeLocation(test_name); - - typedef typename Tests::Head Head; - - // First, register the first test in 'Test' for each type in 'Types'. - TypeParameterizedTest::Register( - prefix, test_location, case_name, test_names, 0, type_names); - - // Next, recurses (at compile time) with the tail of the test list. - return TypeParameterizedTestSuite::Register(prefix, code_location, - state, case_name, - SkipComma(test_names), - type_names); - } -}; - -// The base case for the compile time recursion. -template -class TypeParameterizedTestSuite { - public: - static bool Register(const char* /*prefix*/, const CodeLocation&, - const TypedTestSuitePState* /*state*/, - const char* /*case_name*/, const char* /*test_names*/, - const std::vector& = - std::vector() /*type_names*/) { - return true; - } -}; - -// Returns the current OS stack trace as an std::string. -// -// The maximum number of stack frames to be included is specified by -// the gtest_stack_trace_depth flag. The skip_count parameter -// specifies the number of top frames to be skipped, which doesn't -// count against the number of frames to be included. -// -// For example, if Foo() calls Bar(), which in turn calls -// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in -// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. -GTEST_API_ std::string GetCurrentOsStackTraceExceptTop( - UnitTest* unit_test, int skip_count); - -// Helpers for suppressing warnings on unreachable code or constant -// condition. - -// Always returns true. -GTEST_API_ bool AlwaysTrue(); - -// Always returns false. -inline bool AlwaysFalse() { return !AlwaysTrue(); } - -// Helper for suppressing false warning from Clang on a const char* -// variable declared in a conditional expression always being NULL in -// the else branch. -struct GTEST_API_ ConstCharPtr { - ConstCharPtr(const char* str) : value(str) {} - operator bool() const { return true; } - const char* value; -}; - -// Helper for declaring std::string within 'if' statement -// in pre C++17 build environment. -struct TrueWithString { - TrueWithString() = default; - explicit TrueWithString(const char* str) : value(str) {} - explicit TrueWithString(const std::string& str) : value(str) {} - explicit operator bool() const { return true; } - std::string value; -}; - -// A simple Linear Congruential Generator for generating random -// numbers with a uniform distribution. Unlike rand() and srand(), it -// doesn't use global state (and therefore can't interfere with user -// code). Unlike rand_r(), it's portable. An LCG isn't very random, -// but it's good enough for our purposes. -class GTEST_API_ Random { - public: - static const uint32_t kMaxRange = 1u << 31; - - explicit Random(uint32_t seed) : state_(seed) {} - - void Reseed(uint32_t seed) { state_ = seed; } - - // Generates a random number from [0, range). Crashes if 'range' is - // 0 or greater than kMaxRange. - uint32_t Generate(uint32_t range); - - private: - uint32_t state_; - GTEST_DISALLOW_COPY_AND_ASSIGN_(Random); -}; - -// Turns const U&, U&, const U, and U all into U. -#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ - typename std::remove_const::type>::type - -// HasDebugStringAndShortDebugString::value is a compile-time bool constant -// that's true if and only if T has methods DebugString() and ShortDebugString() -// that return std::string. -template -class HasDebugStringAndShortDebugString { - private: - template - static auto CheckDebugString(C*) -> typename std::is_same< - std::string, decltype(std::declval().DebugString())>::type; - template - static std::false_type CheckDebugString(...); - - template - static auto CheckShortDebugString(C*) -> typename std::is_same< - std::string, decltype(std::declval().ShortDebugString())>::type; - template - static std::false_type CheckShortDebugString(...); - - using HasDebugStringType = decltype(CheckDebugString(nullptr)); - using HasShortDebugStringType = decltype(CheckShortDebugString(nullptr)); - - public: - static constexpr bool value = - HasDebugStringType::value && HasShortDebugStringType::value; -}; - -template -constexpr bool HasDebugStringAndShortDebugString::value; - -// When the compiler sees expression IsContainerTest(0), if C is an -// STL-style container class, the first overload of IsContainerTest -// will be viable (since both C::iterator* and C::const_iterator* are -// valid types and NULL can be implicitly converted to them). It will -// be picked over the second overload as 'int' is a perfect match for -// the type of argument 0. If C::iterator or C::const_iterator is not -// a valid type, the first overload is not viable, and the second -// overload will be picked. Therefore, we can determine whether C is -// a container class by checking the type of IsContainerTest(0). -// The value of the expression is insignificant. -// -// In C++11 mode we check the existence of a const_iterator and that an -// iterator is properly implemented for the container. -// -// For pre-C++11 that we look for both C::iterator and C::const_iterator. -// The reason is that C++ injects the name of a class as a member of the -// class itself (e.g. you can refer to class iterator as either -// 'iterator' or 'iterator::iterator'). If we look for C::iterator -// only, for example, we would mistakenly think that a class named -// iterator is an STL container. -// -// Also note that the simpler approach of overloading -// IsContainerTest(typename C::const_iterator*) and -// IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++. -typedef int IsContainer; -template ().begin()), - class = decltype(::std::declval().end()), - class = decltype(++::std::declval()), - class = decltype(*::std::declval()), - class = typename C::const_iterator> -IsContainer IsContainerTest(int /* dummy */) { - return 0; -} - -typedef char IsNotContainer; -template -IsNotContainer IsContainerTest(long /* dummy */) { return '\0'; } - -// Trait to detect whether a type T is a hash table. -// The heuristic used is that the type contains an inner type `hasher` and does -// not contain an inner type `reverse_iterator`. -// If the container is iterable in reverse, then order might actually matter. -template -struct IsHashTable { - private: - template - static char test(typename U::hasher*, typename U::reverse_iterator*); - template - static int test(typename U::hasher*, ...); - template - static char test(...); - - public: - static const bool value = sizeof(test(nullptr, nullptr)) == sizeof(int); -}; - -template -const bool IsHashTable::value; - -template (0)) == sizeof(IsContainer)> -struct IsRecursiveContainerImpl; - -template -struct IsRecursiveContainerImpl : public std::false_type {}; - -// Since the IsRecursiveContainerImpl depends on the IsContainerTest we need to -// obey the same inconsistencies as the IsContainerTest, namely check if -// something is a container is relying on only const_iterator in C++11 and -// is relying on both const_iterator and iterator otherwise -template -struct IsRecursiveContainerImpl { - using value_type = decltype(*std::declval()); - using type = - std::is_same::type>::type, - C>; -}; - -// IsRecursiveContainer is a unary compile-time predicate that -// evaluates whether C is a recursive container type. A recursive container -// type is a container type whose value_type is equal to the container type -// itself. An example for a recursive container type is -// boost::filesystem::path, whose iterator has a value_type that is equal to -// boost::filesystem::path. -template -struct IsRecursiveContainer : public IsRecursiveContainerImpl::type {}; - -// Utilities for native arrays. - -// ArrayEq() compares two k-dimensional native arrays using the -// elements' operator==, where k can be any integer >= 0. When k is -// 0, ArrayEq() degenerates into comparing a single pair of values. - -template -bool ArrayEq(const T* lhs, size_t size, const U* rhs); - -// This generic version is used when k is 0. -template -inline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; } - -// This overload is used when k >= 1. -template -inline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) { - return internal::ArrayEq(lhs, N, rhs); -} - -// This helper reduces code bloat. If we instead put its logic inside -// the previous ArrayEq() function, arrays with different sizes would -// lead to different copies of the template code. -template -bool ArrayEq(const T* lhs, size_t size, const U* rhs) { - for (size_t i = 0; i != size; i++) { - if (!internal::ArrayEq(lhs[i], rhs[i])) - return false; - } - return true; -} - -// Finds the first element in the iterator range [begin, end) that -// equals elem. Element may be a native array type itself. -template -Iter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { - for (Iter it = begin; it != end; ++it) { - if (internal::ArrayEq(*it, elem)) - return it; - } - return end; -} - -// CopyArray() copies a k-dimensional native array using the elements' -// operator=, where k can be any integer >= 0. When k is 0, -// CopyArray() degenerates into copying a single value. - -template -void CopyArray(const T* from, size_t size, U* to); - -// This generic version is used when k is 0. -template -inline void CopyArray(const T& from, U* to) { *to = from; } - -// This overload is used when k >= 1. -template -inline void CopyArray(const T(&from)[N], U(*to)[N]) { - internal::CopyArray(from, N, *to); -} - -// This helper reduces code bloat. If we instead put its logic inside -// the previous CopyArray() function, arrays with different sizes -// would lead to different copies of the template code. -template -void CopyArray(const T* from, size_t size, U* to) { - for (size_t i = 0; i != size; i++) { - internal::CopyArray(from[i], to + i); - } -} - -// The relation between an NativeArray object (see below) and the -// native array it represents. -// We use 2 different structs to allow non-copyable types to be used, as long -// as RelationToSourceReference() is passed. -struct RelationToSourceReference {}; -struct RelationToSourceCopy {}; - -// Adapts a native array to a read-only STL-style container. Instead -// of the complete STL container concept, this adaptor only implements -// members useful for Google Mock's container matchers. New members -// should be added as needed. To simplify the implementation, we only -// support Element being a raw type (i.e. having no top-level const or -// reference modifier). It's the client's responsibility to satisfy -// this requirement. Element can be an array type itself (hence -// multi-dimensional arrays are supported). -template -class NativeArray { - public: - // STL-style container typedefs. - typedef Element value_type; - typedef Element* iterator; - typedef const Element* const_iterator; - - // Constructs from a native array. References the source. - NativeArray(const Element* array, size_t count, RelationToSourceReference) { - InitRef(array, count); - } - - // Constructs from a native array. Copies the source. - NativeArray(const Element* array, size_t count, RelationToSourceCopy) { - InitCopy(array, count); - } - - // Copy constructor. - NativeArray(const NativeArray& rhs) { - (this->*rhs.clone_)(rhs.array_, rhs.size_); - } - - ~NativeArray() { - if (clone_ != &NativeArray::InitRef) - delete[] array_; - } - - // STL-style container methods. - size_t size() const { return size_; } - const_iterator begin() const { return array_; } - const_iterator end() const { return array_ + size_; } - bool operator==(const NativeArray& rhs) const { - return size() == rhs.size() && - ArrayEq(begin(), size(), rhs.begin()); - } - - private: - static_assert(!std::is_const::value, "Type must not be const"); - static_assert(!std::is_reference::value, - "Type must not be a reference"); - - // Initializes this object with a copy of the input. - void InitCopy(const Element* array, size_t a_size) { - Element* const copy = new Element[a_size]; - CopyArray(array, a_size, copy); - array_ = copy; - size_ = a_size; - clone_ = &NativeArray::InitCopy; - } - - // Initializes this object with a reference of the input. - void InitRef(const Element* array, size_t a_size) { - array_ = array; - size_ = a_size; - clone_ = &NativeArray::InitRef; - } - - const Element* array_; - size_t size_; - void (NativeArray::*clone_)(const Element*, size_t); -}; - -// Backport of std::index_sequence. -template -struct IndexSequence { - using type = IndexSequence; -}; - -// Double the IndexSequence, and one if plus_one is true. -template -struct DoubleSequence; -template -struct DoubleSequence, sizeofT> { - using type = IndexSequence; -}; -template -struct DoubleSequence, sizeofT> { - using type = IndexSequence; -}; - -// Backport of std::make_index_sequence. -// It uses O(ln(N)) instantiation depth. -template -struct MakeIndexSequenceImpl - : DoubleSequence::type, - N / 2>::type {}; - -template <> -struct MakeIndexSequenceImpl<0> : IndexSequence<> {}; - -template -using MakeIndexSequence = typename MakeIndexSequenceImpl::type; - -template -using IndexSequenceFor = typename MakeIndexSequence::type; - -template -struct Ignore { - Ignore(...); // NOLINT -}; - -template -struct ElemFromListImpl; -template -struct ElemFromListImpl> { - // We make Ignore a template to solve a problem with MSVC. - // A non-template Ignore would work fine with `decltype(Ignore(I))...`, but - // MSVC doesn't understand how to deal with that pack expansion. - // Use `0 * I` to have a single instantiation of Ignore. - template - static R Apply(Ignore<0 * I>..., R (*)(), ...); -}; - -template -struct ElemFromList { - using type = - decltype(ElemFromListImpl::type>::Apply( - static_cast(nullptr)...)); -}; - -struct FlatTupleConstructTag {}; - -template -class FlatTuple; - -template -struct FlatTupleElemBase; - -template -struct FlatTupleElemBase, I> { - using value_type = typename ElemFromList::type; - FlatTupleElemBase() = default; - template - explicit FlatTupleElemBase(FlatTupleConstructTag, Arg&& t) - : value(std::forward(t)) {} - value_type value; -}; - -template -struct FlatTupleBase; - -template -struct FlatTupleBase, IndexSequence> - : FlatTupleElemBase, Idx>... { - using Indices = IndexSequence; - FlatTupleBase() = default; - template - explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args) - : FlatTupleElemBase, Idx>(FlatTupleConstructTag{}, - std::forward(args))... {} - - template - const typename ElemFromList::type& Get() const { - return FlatTupleElemBase, I>::value; - } - - template - typename ElemFromList::type& Get() { - return FlatTupleElemBase, I>::value; - } - - template - auto Apply(F&& f) -> decltype(std::forward(f)(this->Get()...)) { - return std::forward(f)(Get()...); - } - - template - auto Apply(F&& f) const -> decltype(std::forward(f)(this->Get()...)) { - return std::forward(f)(Get()...); - } -}; - -// Analog to std::tuple but with different tradeoffs. -// This class minimizes the template instantiation depth, thus allowing more -// elements than std::tuple would. std::tuple has been seen to require an -// instantiation depth of more than 10x the number of elements in some -// implementations. -// FlatTuple and ElemFromList are not recursive and have a fixed depth -// regardless of T... -// MakeIndexSequence, on the other hand, it is recursive but with an -// instantiation depth of O(ln(N)). -template -class FlatTuple - : private FlatTupleBase, - typename MakeIndexSequence::type> { - using Indices = typename FlatTupleBase< - FlatTuple, typename MakeIndexSequence::type>::Indices; - - public: - FlatTuple() = default; - template - explicit FlatTuple(FlatTupleConstructTag tag, Args&&... args) - : FlatTuple::FlatTupleBase(tag, std::forward(args)...) {} - - using FlatTuple::FlatTupleBase::Apply; - using FlatTuple::FlatTupleBase::Get; -}; - -// Utility functions to be called with static_assert to induce deprecation -// warnings. -GTEST_INTERNAL_DEPRECATED( - "INSTANTIATE_TEST_CASE_P is deprecated, please use " - "INSTANTIATE_TEST_SUITE_P") -constexpr bool InstantiateTestCase_P_IsDeprecated() { return true; } - -GTEST_INTERNAL_DEPRECATED( - "TYPED_TEST_CASE_P is deprecated, please use " - "TYPED_TEST_SUITE_P") -constexpr bool TypedTestCase_P_IsDeprecated() { return true; } - -GTEST_INTERNAL_DEPRECATED( - "TYPED_TEST_CASE is deprecated, please use " - "TYPED_TEST_SUITE") -constexpr bool TypedTestCaseIsDeprecated() { return true; } - -GTEST_INTERNAL_DEPRECATED( - "REGISTER_TYPED_TEST_CASE_P is deprecated, please use " - "REGISTER_TYPED_TEST_SUITE_P") -constexpr bool RegisterTypedTestCase_P_IsDeprecated() { return true; } - -GTEST_INTERNAL_DEPRECATED( - "INSTANTIATE_TYPED_TEST_CASE_P is deprecated, please use " - "INSTANTIATE_TYPED_TEST_SUITE_P") -constexpr bool InstantiateTypedTestCase_P_IsDeprecated() { return true; } - -} // namespace internal -} // namespace testing - -namespace std { -// Some standard library implementations use `struct tuple_size` and some use -// `class tuple_size`. Clang warns about the mismatch. -// https://reviews.llvm.org/D55466 -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wmismatched-tags" -#endif -template -struct tuple_size> - : std::integral_constant {}; -#ifdef __clang__ -#pragma clang diagnostic pop -#endif -} // namespace std - -#define GTEST_MESSAGE_AT_(file, line, message, result_type) \ - ::testing::internal::AssertHelper(result_type, file, line, message) \ - = ::testing::Message() - -#define GTEST_MESSAGE_(message, result_type) \ - GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) - -#define GTEST_FATAL_FAILURE_(message) \ - return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) - -#define GTEST_NONFATAL_FAILURE_(message) \ - GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) - -#define GTEST_SUCCESS_(message) \ - GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) - -#define GTEST_SKIP_(message) \ - return GTEST_MESSAGE_(message, ::testing::TestPartResult::kSkip) - -// Suppress MSVC warning 4072 (unreachable code) for the code following -// statement if it returns or throws (or doesn't return or throw in some -// situations). -// NOTE: The "else" is important to keep this expansion to prevent a top-level -// "else" from attaching to our "if". -#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ - if (::testing::internal::AlwaysTrue()) { \ - statement; \ - } else /* NOLINT */ \ - static_assert(true, "") // User must have a semicolon after expansion. - -#if GTEST_HAS_EXCEPTIONS - -namespace testing { -namespace internal { - -class NeverThrown { - public: - const char* what() const noexcept { - return "this exception should never be thrown"; - } -}; - -} // namespace internal -} // namespace testing - -#if GTEST_HAS_RTTI - -#define GTEST_EXCEPTION_TYPE_(e) ::testing::internal::GetTypeName(typeid(e)) - -#else // GTEST_HAS_RTTI - -#define GTEST_EXCEPTION_TYPE_(e) \ - std::string { "an std::exception-derived error" } - -#endif // GTEST_HAS_RTTI - -#define GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) \ - catch (typename std::conditional< \ - std::is_same::type>::type, \ - std::exception>::value, \ - const ::testing::internal::NeverThrown&, const std::exception&>::type \ - e) { \ - gtest_msg.value = "Expected: " #statement \ - " throws an exception of type " #expected_exception \ - ".\n Actual: it throws "; \ - gtest_msg.value += GTEST_EXCEPTION_TYPE_(e); \ - gtest_msg.value += " with description \""; \ - gtest_msg.value += e.what(); \ - gtest_msg.value += "\"."; \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ - } - -#else // GTEST_HAS_EXCEPTIONS - -#define GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) - -#endif // GTEST_HAS_EXCEPTIONS - -#define GTEST_TEST_THROW_(statement, expected_exception, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::TrueWithString gtest_msg{}) { \ - bool gtest_caught_expected = false; \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } catch (expected_exception const&) { \ - gtest_caught_expected = true; \ - } \ - GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) \ - catch (...) { \ - gtest_msg.value = "Expected: " #statement \ - " throws an exception of type " #expected_exception \ - ".\n Actual: it throws a different type."; \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ - } \ - if (!gtest_caught_expected) { \ - gtest_msg.value = "Expected: " #statement \ - " throws an exception of type " #expected_exception \ - ".\n Actual: it throws nothing."; \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ - } \ - } else /*NOLINT*/ \ - GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \ - : fail(gtest_msg.value.c_str()) - -#if GTEST_HAS_EXCEPTIONS - -#define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \ - catch (std::exception const& e) { \ - gtest_msg.value = "it throws "; \ - gtest_msg.value += GTEST_EXCEPTION_TYPE_(e); \ - gtest_msg.value += " with description \""; \ - gtest_msg.value += e.what(); \ - gtest_msg.value += "\"."; \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ - } - -#else // GTEST_HAS_EXCEPTIONS - -#define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() - -#endif // GTEST_HAS_EXCEPTIONS - -#define GTEST_TEST_NO_THROW_(statement, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::TrueWithString gtest_msg{}) { \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } \ - GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \ - catch (...) { \ - gtest_msg.value = "it throws."; \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ - fail(("Expected: " #statement " doesn't throw an exception.\n" \ - " Actual: " + gtest_msg.value).c_str()) - -#define GTEST_TEST_ANY_THROW_(statement, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::AlwaysTrue()) { \ - bool gtest_caught_any = false; \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } \ - catch (...) { \ - gtest_caught_any = true; \ - } \ - if (!gtest_caught_any) { \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \ - fail("Expected: " #statement " throws an exception.\n" \ - " Actual: it doesn't.") - - -// Implements Boolean test assertions such as EXPECT_TRUE. expression can be -// either a boolean expression or an AssertionResult. text is a textual -// representation of expression as it was passed into the EXPECT_TRUE. -#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (const ::testing::AssertionResult gtest_ar_ = \ - ::testing::AssertionResult(expression)) \ - ; \ - else \ - fail(::testing::internal::GetBoolAssertionFailureMessage(\ - gtest_ar_, text, #actual, #expected).c_str()) - -#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::AlwaysTrue()) { \ - ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ - goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \ - fail("Expected: " #statement " doesn't generate new fatal " \ - "failures in the current thread.\n" \ - " Actual: it does.") - -// Expands to the name of the class that implements the given test. -#define GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ - test_suite_name##_##test_name##_Test - -// Helper macro for defining tests. -#define GTEST_TEST_(test_suite_name, test_name, parent_class, parent_id) \ - static_assert(sizeof(GTEST_STRINGIFY_(test_suite_name)) > 1, \ - "test_suite_name must not be empty"); \ - static_assert(sizeof(GTEST_STRINGIFY_(test_name)) > 1, \ - "test_name must not be empty"); \ - class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ - : public parent_class { \ - public: \ - GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() = default; \ - ~GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() override = default; \ - GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_suite_name, \ - test_name)); \ - GTEST_DISALLOW_MOVE_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_suite_name, \ - test_name)); \ - \ - private: \ - void TestBody() override; \ - static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ - }; \ - \ - ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \ - test_name)::test_info_ = \ - ::testing::internal::MakeAndRegisterTestInfo( \ - #test_suite_name, #test_name, nullptr, nullptr, \ - ::testing::internal::CodeLocation(__FILE__, __LINE__), (parent_id), \ - ::testing::internal::SuiteApiResolver< \ - parent_class>::GetSetUpCaseOrSuite(__FILE__, __LINE__), \ - ::testing::internal::SuiteApiResolver< \ - parent_class>::GetTearDownCaseOrSuite(__FILE__, __LINE__), \ - new ::testing::internal::TestFactoryImpl); \ - void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody() - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file defines the public API for death tests. It is -// #included by gtest.h so a user doesn't need to include this -// directly. -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ - -// Copyright 2005, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This header file defines internal utilities needed for implementing -// death tests. They are subject to change without notice. -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ - -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// The Google C++ Testing and Mocking Framework (Google Test) -// -// This file implements just enough of the matcher interface to allow -// EXPECT_DEATH and friends to accept a matcher argument. - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ - -#include -#include -#include -#include -#include - -// Copyright 2007, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Google Test - The Google C++ Testing and Mocking Framework -// -// This file implements a universal value printer that can print a -// value of any type T: -// -// void ::testing::internal::UniversalPrinter::Print(value, ostream_ptr); -// -// A user can teach this function how to print a class type T by -// defining either operator<<() or PrintTo() in the namespace that -// defines T. More specifically, the FIRST defined function in the -// following list will be used (assuming T is defined in namespace -// foo): -// -// 1. foo::PrintTo(const T&, ostream*) -// 2. operator<<(ostream&, const T&) defined in either foo or the -// global namespace. -// -// However if T is an STL-style container then it is printed element-wise -// unless foo::PrintTo(const T&, ostream*) is defined. Note that -// operator<<() is ignored for container types. -// -// If none of the above is defined, it will print the debug string of -// the value if it is a protocol buffer, or print the raw bytes in the -// value otherwise. -// -// To aid debugging: when T is a reference type, the address of the -// value is also printed; when T is a (const) char pointer, both the -// pointer value and the NUL-terminated string it points to are -// printed. -// -// We also provide some convenient wrappers: -// -// // Prints a value to a string. For a (const or not) char -// // pointer, the NUL-terminated string (but not the pointer) is -// // printed. -// std::string ::testing::PrintToString(const T& value); -// -// // Prints a value tersely: for a reference type, the referenced -// // value (but not the address) is printed; for a (const or not) char -// // pointer, the NUL-terminated string (but not the pointer) is -// // printed. -// void ::testing::internal::UniversalTersePrint(const T& value, ostream*); -// -// // Prints value using the type inferred by the compiler. The difference -// // from UniversalTersePrint() is that this function prints both the -// // pointer and the NUL-terminated string for a (const or not) char pointer. -// void ::testing::internal::UniversalPrint(const T& value, ostream*); -// -// // Prints the fields of a tuple tersely to a string vector, one -// // element for each field. Tuple support must be enabled in -// // gtest-port.h. -// std::vector UniversalTersePrintTupleFieldsToStrings( -// const Tuple& value); -// -// Known limitation: -// -// The print primitives print the elements of an STL-style container -// using the compiler-inferred type of *iter where iter is a -// const_iterator of the container. When const_iterator is an input -// iterator but not a forward iterator, this inferred type may not -// match value_type, and the print output may be incorrect. In -// practice, this is rarely a problem as for most containers -// const_iterator is a forward iterator. We'll fix this if there's an -// actual need for it. Note that this fix cannot rely on value_type -// being defined as many user-defined container types don't have -// value_type. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ - -#include -#include -#include // NOLINT -#include -#include -#include -#include -#include -#include - - -namespace testing { - -// Definitions in the internal* namespaces are subject to change without notice. -// DO NOT USE THEM IN USER CODE! -namespace internal { - -template -void UniversalPrint(const T& value, ::std::ostream* os); - -// Used to print an STL-style container when the user doesn't define -// a PrintTo() for it. -struct ContainerPrinter { - template (0)) == sizeof(IsContainer)) && - !IsRecursiveContainer::value>::type> - static void PrintValue(const T& container, std::ostream* os) { - const size_t kMaxCount = 32; // The maximum number of elements to print. - *os << '{'; - size_t count = 0; - for (auto&& elem : container) { - if (count > 0) { - *os << ','; - if (count == kMaxCount) { // Enough has been printed. - *os << " ..."; - break; - } - } - *os << ' '; - // We cannot call PrintTo(elem, os) here as PrintTo() doesn't - // handle `elem` being a native array. - internal::UniversalPrint(elem, os); - ++count; - } - - if (count > 0) { - *os << ' '; - } - *os << '}'; - } -}; - -// Used to print a pointer that is neither a char pointer nor a member -// pointer, when the user doesn't define PrintTo() for it. (A member -// variable pointer or member function pointer doesn't really point to -// a location in the address space. Their representation is -// implementation-defined. Therefore they will be printed as raw -// bytes.) -struct FunctionPointerPrinter { - template ::value>::type> - static void PrintValue(T* p, ::std::ostream* os) { - if (p == nullptr) { - *os << "NULL"; - } else { - // T is a function type, so '*os << p' doesn't do what we want - // (it just prints p as bool). We want to print p as a const - // void*. - *os << reinterpret_cast(p); - } - } -}; - -struct PointerPrinter { - template - static void PrintValue(T* p, ::std::ostream* os) { - if (p == nullptr) { - *os << "NULL"; - } else { - // T is not a function type. We just call << to print p, - // relying on ADL to pick up user-defined << for their pointer - // types, if any. - *os << p; - } - } -}; - -namespace internal_stream_operator_without_lexical_name_lookup { - -// The presence of an operator<< here will terminate lexical scope lookup -// straight away (even though it cannot be a match because of its argument -// types). Thus, the two operator<< calls in StreamPrinter will find only ADL -// candidates. -struct LookupBlocker {}; -void operator<<(LookupBlocker, LookupBlocker); - -struct StreamPrinter { - template ::value>::type, - // Only accept types for which we can find a streaming operator via - // ADL (possibly involving implicit conversions). - typename = decltype(std::declval() - << std::declval())> - static void PrintValue(const T& value, ::std::ostream* os) { - // Call streaming operator found by ADL, possibly with implicit conversions - // of the arguments. - *os << value; - } -}; - -} // namespace internal_stream_operator_without_lexical_name_lookup - -struct ProtobufPrinter { - // We print a protobuf using its ShortDebugString() when the string - // doesn't exceed this many characters; otherwise we print it using - // DebugString() for better readability. - static const size_t kProtobufOneLinerMaxLength = 50; - - template ::value>::type> - static void PrintValue(const T& value, ::std::ostream* os) { - std::string pretty_str = value.ShortDebugString(); - if (pretty_str.length() > kProtobufOneLinerMaxLength) { - pretty_str = "\n" + value.DebugString(); - } - *os << ("<" + pretty_str + ">"); - } -}; - -struct ConvertibleToIntegerPrinter { - // Since T has no << operator or PrintTo() but can be implicitly - // converted to BiggestInt, we print it as a BiggestInt. - // - // Most likely T is an enum type (either named or unnamed), in which - // case printing it as an integer is the desired behavior. In case - // T is not an enum, printing it as an integer is the best we can do - // given that it has no user-defined printer. - static void PrintValue(internal::BiggestInt value, ::std::ostream* os) { - *os << value; - } -}; - -struct ConvertibleToStringViewPrinter { -#if GTEST_INTERNAL_HAS_STRING_VIEW - static void PrintValue(internal::StringView value, ::std::ostream* os) { - internal::UniversalPrint(value, os); - } -#endif -}; - - -// Prints the given number of bytes in the given object to the given -// ostream. -GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, - size_t count, - ::std::ostream* os); -struct RawBytesPrinter { - // SFINAE on `sizeof` to make sure we have a complete type. - template - static void PrintValue(const T& value, ::std::ostream* os) { - PrintBytesInObjectTo( - static_cast( - // Load bearing cast to void* to support iOS - reinterpret_cast(std::addressof(value))), - sizeof(value), os); - } -}; - -struct FallbackPrinter { - template - static void PrintValue(const T&, ::std::ostream* os) { - *os << "(incomplete type)"; - } -}; - -// Try every printer in order and return the first one that works. -template -struct FindFirstPrinter : FindFirstPrinter {}; - -template -struct FindFirstPrinter< - T, decltype(Printer::PrintValue(std::declval(), nullptr)), - Printer, Printers...> { - using type = Printer; -}; - -// Select the best printer in the following order: -// - Print containers (they have begin/end/etc). -// - Print function pointers. -// - Print object pointers. -// - Use the stream operator, if available. -// - Print protocol buffers. -// - Print types convertible to BiggestInt. -// - Print types convertible to StringView, if available. -// - Fallback to printing the raw bytes of the object. -template -void PrintWithFallback(const T& value, ::std::ostream* os) { - using Printer = typename FindFirstPrinter< - T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, - internal_stream_operator_without_lexical_name_lookup::StreamPrinter, - ProtobufPrinter, ConvertibleToIntegerPrinter, - ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type; - Printer::PrintValue(value, os); -} - -// FormatForComparison::Format(value) formats a -// value of type ToPrint that is an operand of a comparison assertion -// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in -// the comparison, and is used to help determine the best way to -// format the value. In particular, when the value is a C string -// (char pointer) and the other operand is an STL string object, we -// want to format the C string as a string, since we know it is -// compared by value with the string object. If the value is a char -// pointer but the other operand is not an STL string object, we don't -// know whether the pointer is supposed to point to a NUL-terminated -// string, and thus want to print it as a pointer to be safe. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - -// The default case. -template -class FormatForComparison { - public: - static ::std::string Format(const ToPrint& value) { - return ::testing::PrintToString(value); - } -}; - -// Array. -template -class FormatForComparison { - public: - static ::std::string Format(const ToPrint* value) { - return FormatForComparison::Format(value); - } -}; - -// By default, print C string as pointers to be safe, as we don't know -// whether they actually point to a NUL-terminated string. - -#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ - template \ - class FormatForComparison { \ - public: \ - static ::std::string Format(CharType* value) { \ - return ::testing::PrintToString(static_cast(value)); \ - } \ - } - -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); -#ifdef __cpp_char8_t -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t); -#endif -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t); -GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t); - -#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ - -// If a C string is compared with an STL string object, we know it's meant -// to point to a NUL-terminated string, and thus can print it as a string. - -#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ - template <> \ - class FormatForComparison { \ - public: \ - static ::std::string Format(CharType* value) { \ - return ::testing::PrintToString(value); \ - } \ - } - -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); -#ifdef __cpp_char8_t -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string); -#endif -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string); - -#if GTEST_HAS_STD_WSTRING -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); -GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); -#endif - -#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ - -// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) -// operand to be used in a failure message. The type (but not value) -// of the other operand may affect the format. This allows us to -// print a char* as a raw pointer when it is compared against another -// char* or void*, and print it as a C string when it is compared -// against an std::string object, for example. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -template -std::string FormatForComparisonFailureMessage( - const T1& value, const T2& /* other_operand */) { - return FormatForComparison::Format(value); -} - -// UniversalPrinter::Print(value, ostream_ptr) prints the given -// value to the given ostream. The caller must ensure that -// 'ostream_ptr' is not NULL, or the behavior is undefined. -// -// We define UniversalPrinter as a class template (as opposed to a -// function template), as we need to partially specialize it for -// reference types, which cannot be done with function templates. -template -class UniversalPrinter; - -// Prints the given value using the << operator if it has one; -// otherwise prints the bytes in it. This is what -// UniversalPrinter::Print() does when PrintTo() is not specialized -// or overloaded for type T. -// -// A user can override this behavior for a class type Foo by defining -// an overload of PrintTo() in the namespace where Foo is defined. We -// give the user this option as sometimes defining a << operator for -// Foo is not desirable (e.g. the coding style may prevent doing it, -// or there is already a << operator but it doesn't do what the user -// wants). -template -void PrintTo(const T& value, ::std::ostream* os) { - internal::PrintWithFallback(value, os); -} - -// The following list of PrintTo() overloads tells -// UniversalPrinter::Print() how to print standard types (built-in -// types, strings, plain arrays, and pointers). - -// Overloads for various char types. -GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os); -GTEST_API_ void PrintTo(signed char c, ::std::ostream* os); -inline void PrintTo(char c, ::std::ostream* os) { - // When printing a plain char, we always treat it as unsigned. This - // way, the output won't be affected by whether the compiler thinks - // char is signed or not. - PrintTo(static_cast(c), os); -} - -// Overloads for other simple built-in types. -inline void PrintTo(bool x, ::std::ostream* os) { - *os << (x ? "true" : "false"); -} - -// Overload for wchar_t type. -// Prints a wchar_t as a symbol if it is printable or as its internal -// code otherwise and also as its decimal code (except for L'\0'). -// The L'\0' char is printed as "L'\\0'". The decimal code is printed -// as signed integer when wchar_t is implemented by the compiler -// as a signed type and is printed as an unsigned integer when wchar_t -// is implemented as an unsigned type. -GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os); - -GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os); -inline void PrintTo(char16_t c, ::std::ostream* os) { - PrintTo(ImplicitCast_(c), os); -} -#ifdef __cpp_char8_t -inline void PrintTo(char8_t c, ::std::ostream* os) { - PrintTo(ImplicitCast_(c), os); -} -#endif - -// Overloads for C strings. -GTEST_API_ void PrintTo(const char* s, ::std::ostream* os); -inline void PrintTo(char* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} - -// signed/unsigned char is often used for representing binary data, so -// we print pointers to it as void* to be safe. -inline void PrintTo(const signed char* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -inline void PrintTo(signed char* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -inline void PrintTo(const unsigned char* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -inline void PrintTo(unsigned char* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -#ifdef __cpp_char8_t -// Overloads for u8 strings. -GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os); -inline void PrintTo(char8_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -#endif -// Overloads for u16 strings. -GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os); -inline void PrintTo(char16_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -// Overloads for u32 strings. -GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os); -inline void PrintTo(char32_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} - -// MSVC can be configured to define wchar_t as a typedef of unsigned -// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native -// type. When wchar_t is a typedef, defining an overload for const -// wchar_t* would cause unsigned short* be printed as a wide string, -// possibly causing invalid memory accesses. -#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) -// Overloads for wide C strings -GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os); -inline void PrintTo(wchar_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_(s), os); -} -#endif - -// Overload for C arrays. Multi-dimensional arrays are printed -// properly. - -// Prints the given number of elements in an array, without printing -// the curly braces. -template -void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) { - UniversalPrint(a[0], os); - for (size_t i = 1; i != count; i++) { - *os << ", "; - UniversalPrint(a[i], os); - } -} - -// Overloads for ::std::string. -GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os); -inline void PrintTo(const ::std::string& s, ::std::ostream* os) { - PrintStringTo(s, os); -} - -// Overloads for ::std::u8string -#ifdef __cpp_char8_t -GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os); -inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) { - PrintU8StringTo(s, os); -} -#endif - -// Overloads for ::std::u16string -GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os); -inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) { - PrintU16StringTo(s, os); -} - -// Overloads for ::std::u32string -GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os); -inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) { - PrintU32StringTo(s, os); -} - -// Overloads for ::std::wstring. -#if GTEST_HAS_STD_WSTRING -GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os); -inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { - PrintWideStringTo(s, os); -} -#endif // GTEST_HAS_STD_WSTRING - -#if GTEST_INTERNAL_HAS_STRING_VIEW -// Overload for internal::StringView. -inline void PrintTo(internal::StringView sp, ::std::ostream* os) { - PrintTo(::std::string(sp), os); -} -#endif // GTEST_INTERNAL_HAS_STRING_VIEW - -inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; } - -template -void PrintTo(std::reference_wrapper ref, ::std::ostream* os) { - UniversalPrinter::Print(ref.get(), os); -} - -inline const void* VoidifyPointer(const void* p) { return p; } -inline const void* VoidifyPointer(volatile const void* p) { - return const_cast(p); -} - -template -void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) { - if (ptr == nullptr) { - *os << "(nullptr)"; - } else { - // We can't print the value. Just print the pointer.. - *os << "(" << (VoidifyPointer)(ptr.get()) << ")"; - } -} -template ::value && - !std::is_array::value>::type> -void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) { - if (ptr == nullptr) { - *os << "(nullptr)"; - } else { - *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = "; - UniversalPrinter::Print(*ptr, os); - *os << ")"; - } -} - -template -void PrintTo(const std::unique_ptr& ptr, std::ostream* os) { - (PrintSmartPointer)(ptr, os, 0); -} - -template -void PrintTo(const std::shared_ptr& ptr, std::ostream* os) { - (PrintSmartPointer)(ptr, os, 0); -} - -// Helper function for printing a tuple. T must be instantiated with -// a tuple type. -template -void PrintTupleTo(const T&, std::integral_constant, - ::std::ostream*) {} - -template -void PrintTupleTo(const T& t, std::integral_constant, - ::std::ostream* os) { - PrintTupleTo(t, std::integral_constant(), os); - GTEST_INTENTIONAL_CONST_COND_PUSH_() - if (I > 1) { - GTEST_INTENTIONAL_CONST_COND_POP_() - *os << ", "; - } - UniversalPrinter::type>::Print( - std::get(t), os); -} - -template -void PrintTo(const ::std::tuple& t, ::std::ostream* os) { - *os << "("; - PrintTupleTo(t, std::integral_constant(), os); - *os << ")"; -} - -// Overload for std::pair. -template -void PrintTo(const ::std::pair& value, ::std::ostream* os) { - *os << '('; - // We cannot use UniversalPrint(value.first, os) here, as T1 may be - // a reference type. The same for printing value.second. - UniversalPrinter::Print(value.first, os); - *os << ", "; - UniversalPrinter::Print(value.second, os); - *os << ')'; -} - -// Implements printing a non-reference type T by letting the compiler -// pick the right overload of PrintTo() for T. -template -class UniversalPrinter { - public: - // MSVC warns about adding const to a function type, so we want to - // disable the warning. - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) - - // Note: we deliberately don't call this PrintTo(), as that name - // conflicts with ::testing::internal::PrintTo in the body of the - // function. - static void Print(const T& value, ::std::ostream* os) { - // By default, ::testing::internal::PrintTo() is used for printing - // the value. - // - // Thanks to Koenig look-up, if T is a class and has its own - // PrintTo() function defined in its namespace, that function will - // be visible here. Since it is more specific than the generic ones - // in ::testing::internal, it will be picked by the compiler in the - // following statement - exactly what we want. - PrintTo(value, os); - } - - GTEST_DISABLE_MSC_WARNINGS_POP_() -}; - -// Remove any const-qualifiers before passing a type to UniversalPrinter. -template -class UniversalPrinter : public UniversalPrinter {}; - -#if GTEST_INTERNAL_HAS_ANY - -// Printer for std::any / absl::any - -template <> -class UniversalPrinter { - public: - static void Print(const Any& value, ::std::ostream* os) { - if (value.has_value()) { - *os << "value of type " << GetTypeName(value); - } else { - *os << "no value"; - } - } - - private: - static std::string GetTypeName(const Any& value) { -#if GTEST_HAS_RTTI - return internal::GetTypeName(value.type()); -#else - static_cast(value); // possibly unused - return ""; -#endif // GTEST_HAS_RTTI - } -}; - -#endif // GTEST_INTERNAL_HAS_ANY - -#if GTEST_INTERNAL_HAS_OPTIONAL - -// Printer for std::optional / absl::optional - -template -class UniversalPrinter> { - public: - static void Print(const Optional& value, ::std::ostream* os) { - *os << '('; - if (!value) { - *os << "nullopt"; - } else { - UniversalPrint(*value, os); - } - *os << ')'; - } -}; - -#endif // GTEST_INTERNAL_HAS_OPTIONAL - -#if GTEST_INTERNAL_HAS_VARIANT - -// Printer for std::variant / absl::variant - -template -class UniversalPrinter> { - public: - static void Print(const Variant& value, ::std::ostream* os) { - *os << '('; -#if GTEST_HAS_ABSL - absl::visit(Visitor{os, value.index()}, value); -#else - std::visit(Visitor{os, value.index()}, value); -#endif // GTEST_HAS_ABSL - *os << ')'; - } - - private: - struct Visitor { - template - void operator()(const U& u) const { - *os << "'" << GetTypeName() << "(index = " << index - << ")' with value "; - UniversalPrint(u, os); - } - ::std::ostream* os; - std::size_t index; - }; -}; - -#endif // GTEST_INTERNAL_HAS_VARIANT - -// UniversalPrintArray(begin, len, os) prints an array of 'len' -// elements, starting at address 'begin'. -template -void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { - if (len == 0) { - *os << "{}"; - } else { - *os << "{ "; - const size_t kThreshold = 18; - const size_t kChunkSize = 8; - // If the array has more than kThreshold elements, we'll have to - // omit some details by printing only the first and the last - // kChunkSize elements. - if (len <= kThreshold) { - PrintRawArrayTo(begin, len, os); - } else { - PrintRawArrayTo(begin, kChunkSize, os); - *os << ", ..., "; - PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os); - } - *os << " }"; - } -} -// This overload prints a (const) char array compactly. -GTEST_API_ void UniversalPrintArray( - const char* begin, size_t len, ::std::ostream* os); - -#ifdef __cpp_char8_t -// This overload prints a (const) char8_t array compactly. -GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len, - ::std::ostream* os); -#endif - -// This overload prints a (const) char16_t array compactly. -GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len, - ::std::ostream* os); - -// This overload prints a (const) char32_t array compactly. -GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len, - ::std::ostream* os); - -// This overload prints a (const) wchar_t array compactly. -GTEST_API_ void UniversalPrintArray( - const wchar_t* begin, size_t len, ::std::ostream* os); - -// Implements printing an array type T[N]. -template -class UniversalPrinter { - public: - // Prints the given array, omitting some elements when there are too - // many. - static void Print(const T (&a)[N], ::std::ostream* os) { - UniversalPrintArray(a, N, os); - } -}; - -// Implements printing a reference type T&. -template -class UniversalPrinter { - public: - // MSVC warns about adding const to a function type, so we want to - // disable the warning. - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) - - static void Print(const T& value, ::std::ostream* os) { - // Prints the address of the value. We use reinterpret_cast here - // as static_cast doesn't compile when T is a function type. - *os << "@" << reinterpret_cast(&value) << " "; - - // Then prints the value itself. - UniversalPrint(value, os); - } - - GTEST_DISABLE_MSC_WARNINGS_POP_() -}; - -// Prints a value tersely: for a reference type, the referenced value -// (but not the address) is printed; for a (const) char pointer, the -// NUL-terminated string (but not the pointer) is printed. - -template -class UniversalTersePrinter { - public: - static void Print(const T& value, ::std::ostream* os) { - UniversalPrint(value, os); - } -}; -template -class UniversalTersePrinter { - public: - static void Print(const T& value, ::std::ostream* os) { - UniversalPrint(value, os); - } -}; -template -class UniversalTersePrinter { - public: - static void Print(const T (&value)[N], ::std::ostream* os) { - UniversalPrinter::Print(value, os); - } -}; -template <> -class UniversalTersePrinter { - public: - static void Print(const char* str, ::std::ostream* os) { - if (str == nullptr) { - *os << "NULL"; - } else { - UniversalPrint(std::string(str), os); - } - } -}; -template <> -class UniversalTersePrinter : public UniversalTersePrinter { -}; - -#ifdef __cpp_char8_t -template <> -class UniversalTersePrinter { - public: - static void Print(const char8_t* str, ::std::ostream* os) { - if (str == nullptr) { - *os << "NULL"; - } else { - UniversalPrint(::std::u8string(str), os); - } - } -}; -template <> -class UniversalTersePrinter - : public UniversalTersePrinter {}; -#endif - -template <> -class UniversalTersePrinter { - public: - static void Print(const char16_t* str, ::std::ostream* os) { - if (str == nullptr) { - *os << "NULL"; - } else { - UniversalPrint(::std::u16string(str), os); - } - } -}; -template <> -class UniversalTersePrinter - : public UniversalTersePrinter {}; - -template <> -class UniversalTersePrinter { - public: - static void Print(const char32_t* str, ::std::ostream* os) { - if (str == nullptr) { - *os << "NULL"; - } else { - UniversalPrint(::std::u32string(str), os); - } - } -}; -template <> -class UniversalTersePrinter - : public UniversalTersePrinter {}; - -#if GTEST_HAS_STD_WSTRING -template <> -class UniversalTersePrinter { - public: - static void Print(const wchar_t* str, ::std::ostream* os) { - if (str == nullptr) { - *os << "NULL"; - } else { - UniversalPrint(::std::wstring(str), os); - } - } -}; -#endif - -template <> -class UniversalTersePrinter { - public: - static void Print(wchar_t* str, ::std::ostream* os) { - UniversalTersePrinter::Print(str, os); - } -}; - -template -void UniversalTersePrint(const T& value, ::std::ostream* os) { - UniversalTersePrinter::Print(value, os); -} - -// Prints a value using the type inferred by the compiler. The -// difference between this and UniversalTersePrint() is that for a -// (const) char pointer, this prints both the pointer and the -// NUL-terminated string. -template -void UniversalPrint(const T& value, ::std::ostream* os) { - // A workarond for the bug in VC++ 7.1 that prevents us from instantiating - // UniversalPrinter with T directly. - typedef T T1; - UniversalPrinter::Print(value, os); -} - -typedef ::std::vector< ::std::string> Strings; - - // Tersely prints the first N fields of a tuple to a string vector, - // one element for each field. -template -void TersePrintPrefixToStrings(const Tuple&, std::integral_constant, - Strings*) {} -template -void TersePrintPrefixToStrings(const Tuple& t, - std::integral_constant, - Strings* strings) { - TersePrintPrefixToStrings(t, std::integral_constant(), - strings); - ::std::stringstream ss; - UniversalTersePrint(std::get(t), &ss); - strings->push_back(ss.str()); -} - -// Prints the fields of a tuple tersely to a string vector, one -// element for each field. See the comment before -// UniversalTersePrint() for how we define "tersely". -template -Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { - Strings result; - TersePrintPrefixToStrings( - value, std::integral_constant::value>(), - &result); - return result; -} - -} // namespace internal - -template -::std::string PrintToString(const T& value) { - ::std::stringstream ss; - internal::UniversalTersePrinter::Print(value, &ss); - return ss.str(); -} - -} // namespace testing - -// Include any custom printer added by the local installation. -// We must include this header at the end to make sure it can use the -// declarations from this file. -// Copyright 2015, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// This file provides an injection point for custom printers in a local -// installation of gTest. -// It will be included from gtest-printers.h and the overrides in this file -// will be visible to everyone. -// -// Injection point for custom user configurations. See README for details -// -// ** Custom implementation starts here ** - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ - -// MSVC warning C5046 is new as of VS2017 version 15.8. -#if defined(_MSC_VER) && _MSC_VER >= 1915 -#define GTEST_MAYBE_5046_ 5046 -#else -#define GTEST_MAYBE_5046_ -#endif - -GTEST_DISABLE_MSC_WARNINGS_PUSH_( - 4251 GTEST_MAYBE_5046_ /* class A needs to have dll-interface to be used by - clients of class B */ - /* Symbol involving type with internal linkage not defined */) - -namespace testing { - -// To implement a matcher Foo for type T, define: -// 1. a class FooMatcherMatcher that implements the matcher interface: -// using is_gtest_matcher = void; -// bool MatchAndExplain(const T&, std::ostream*); -// (MatchResultListener* can also be used instead of std::ostream*) -// void DescribeTo(std::ostream*); -// void DescribeNegationTo(std::ostream*); -// -// 2. a factory function that creates a Matcher object from a -// FooMatcherMatcher. - -class MatchResultListener { - public: - // Creates a listener object with the given underlying ostream. The - // listener does not own the ostream, and does not dereference it - // in the constructor or destructor. - explicit MatchResultListener(::std::ostream* os) : stream_(os) {} - virtual ~MatchResultListener() = 0; // Makes this class abstract. - - // Streams x to the underlying ostream; does nothing if the ostream - // is NULL. - template - MatchResultListener& operator<<(const T& x) { - if (stream_ != nullptr) *stream_ << x; - return *this; - } - - // Returns the underlying ostream. - ::std::ostream* stream() { return stream_; } - - // Returns true if and only if the listener is interested in an explanation - // of the match result. A matcher's MatchAndExplain() method can use - // this information to avoid generating the explanation when no one - // intends to hear it. - bool IsInterested() const { return stream_ != nullptr; } - - private: - ::std::ostream* const stream_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener); -}; - -inline MatchResultListener::~MatchResultListener() { -} - -// An instance of a subclass of this knows how to describe itself as a -// matcher. -class GTEST_API_ MatcherDescriberInterface { - public: - virtual ~MatcherDescriberInterface() {} - - // Describes this matcher to an ostream. The function should print - // a verb phrase that describes the property a value matching this - // matcher should have. The subject of the verb phrase is the value - // being matched. For example, the DescribeTo() method of the Gt(7) - // matcher prints "is greater than 7". - virtual void DescribeTo(::std::ostream* os) const = 0; - - // Describes the negation of this matcher to an ostream. For - // example, if the description of this matcher is "is greater than - // 7", the negated description could be "is not greater than 7". - // You are not required to override this when implementing - // MatcherInterface, but it is highly advised so that your matcher - // can produce good error messages. - virtual void DescribeNegationTo(::std::ostream* os) const { - *os << "not ("; - DescribeTo(os); - *os << ")"; - } -}; - -// The implementation of a matcher. -template -class MatcherInterface : public MatcherDescriberInterface { - public: - // Returns true if and only if the matcher matches x; also explains the - // match result to 'listener' if necessary (see the next paragraph), in - // the form of a non-restrictive relative clause ("which ...", - // "whose ...", etc) that describes x. For example, the - // MatchAndExplain() method of the Pointee(...) matcher should - // generate an explanation like "which points to ...". - // - // Implementations of MatchAndExplain() should add an explanation of - // the match result *if and only if* they can provide additional - // information that's not already present (or not obvious) in the - // print-out of x and the matcher's description. Whether the match - // succeeds is not a factor in deciding whether an explanation is - // needed, as sometimes the caller needs to print a failure message - // when the match succeeds (e.g. when the matcher is used inside - // Not()). - // - // For example, a "has at least 10 elements" matcher should explain - // what the actual element count is, regardless of the match result, - // as it is useful information to the reader; on the other hand, an - // "is empty" matcher probably only needs to explain what the actual - // size is when the match fails, as it's redundant to say that the - // size is 0 when the value is already known to be empty. - // - // You should override this method when defining a new matcher. - // - // It's the responsibility of the caller (Google Test) to guarantee - // that 'listener' is not NULL. This helps to simplify a matcher's - // implementation when it doesn't care about the performance, as it - // can talk to 'listener' without checking its validity first. - // However, in order to implement dummy listeners efficiently, - // listener->stream() may be NULL. - virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0; - - // Inherits these methods from MatcherDescriberInterface: - // virtual void DescribeTo(::std::ostream* os) const = 0; - // virtual void DescribeNegationTo(::std::ostream* os) const; -}; - -namespace internal { - -struct AnyEq { - template - bool operator()(const A& a, const B& b) const { return a == b; } -}; -struct AnyNe { - template - bool operator()(const A& a, const B& b) const { return a != b; } -}; -struct AnyLt { - template - bool operator()(const A& a, const B& b) const { return a < b; } -}; -struct AnyGt { - template - bool operator()(const A& a, const B& b) const { return a > b; } -}; -struct AnyLe { - template - bool operator()(const A& a, const B& b) const { return a <= b; } -}; -struct AnyGe { - template - bool operator()(const A& a, const B& b) const { return a >= b; } -}; - -// A match result listener that ignores the explanation. -class DummyMatchResultListener : public MatchResultListener { - public: - DummyMatchResultListener() : MatchResultListener(nullptr) {} - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener); -}; - -// A match result listener that forwards the explanation to a given -// ostream. The difference between this and MatchResultListener is -// that the former is concrete. -class StreamMatchResultListener : public MatchResultListener { - public: - explicit StreamMatchResultListener(::std::ostream* os) - : MatchResultListener(os) {} - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener); -}; - -struct SharedPayloadBase { - std::atomic ref{1}; - void Ref() { ref.fetch_add(1, std::memory_order_relaxed); } - bool Unref() { return ref.fetch_sub(1, std::memory_order_acq_rel) == 1; } -}; - -template -struct SharedPayload : SharedPayloadBase { - explicit SharedPayload(const T& v) : value(v) {} - explicit SharedPayload(T&& v) : value(std::move(v)) {} - - static void Destroy(SharedPayloadBase* shared) { - delete static_cast(shared); - } - - T value; -}; - -// An internal class for implementing Matcher, which will derive -// from it. We put functionalities common to all Matcher -// specializations here to avoid code duplication. -template -class MatcherBase : private MatcherDescriberInterface { - public: - // Returns true if and only if the matcher matches x; also explains the - // match result to 'listener'. - bool MatchAndExplain(const T& x, MatchResultListener* listener) const { - GTEST_CHECK_(vtable_ != nullptr); - return vtable_->match_and_explain(*this, x, listener); - } - - // Returns true if and only if this matcher matches x. - bool Matches(const T& x) const { - DummyMatchResultListener dummy; - return MatchAndExplain(x, &dummy); - } - - // Describes this matcher to an ostream. - void DescribeTo(::std::ostream* os) const final { - GTEST_CHECK_(vtable_ != nullptr); - vtable_->describe(*this, os, false); - } - - // Describes the negation of this matcher to an ostream. - void DescribeNegationTo(::std::ostream* os) const final { - GTEST_CHECK_(vtable_ != nullptr); - vtable_->describe(*this, os, true); - } - - // Explains why x matches, or doesn't match, the matcher. - void ExplainMatchResultTo(const T& x, ::std::ostream* os) const { - StreamMatchResultListener listener(os); - MatchAndExplain(x, &listener); - } - - // Returns the describer for this matcher object; retains ownership - // of the describer, which is only guaranteed to be alive when - // this matcher object is alive. - const MatcherDescriberInterface* GetDescriber() const { - if (vtable_ == nullptr) return nullptr; - return vtable_->get_describer(*this); - } - - protected: - MatcherBase() : vtable_(nullptr) {} - - // Constructs a matcher from its implementation. - template - explicit MatcherBase(const MatcherInterface* impl) { - Init(impl); - } - - template ::type::is_gtest_matcher> - MatcherBase(M&& m) { // NOLINT - Init(std::forward(m)); - } - - MatcherBase(const MatcherBase& other) - : vtable_(other.vtable_), buffer_(other.buffer_) { - if (IsShared()) buffer_.shared->Ref(); - } - - MatcherBase& operator=(const MatcherBase& other) { - if (this == &other) return *this; - Destroy(); - vtable_ = other.vtable_; - buffer_ = other.buffer_; - if (IsShared()) buffer_.shared->Ref(); - return *this; - } - - MatcherBase(MatcherBase&& other) - : vtable_(other.vtable_), buffer_(other.buffer_) { - other.vtable_ = nullptr; - } - - MatcherBase& operator=(MatcherBase&& other) { - if (this == &other) return *this; - Destroy(); - vtable_ = other.vtable_; - buffer_ = other.buffer_; - other.vtable_ = nullptr; - return *this; - } - - ~MatcherBase() override { Destroy(); } - - private: - struct VTable { - bool (*match_and_explain)(const MatcherBase&, const T&, - MatchResultListener*); - void (*describe)(const MatcherBase&, std::ostream*, bool negation); - // Returns the captured object if it implements the interface, otherwise - // returns the MatcherBase itself. - const MatcherDescriberInterface* (*get_describer)(const MatcherBase&); - // Called on shared instances when the reference count reaches 0. - void (*shared_destroy)(SharedPayloadBase*); - }; - - bool IsShared() const { - return vtable_ != nullptr && vtable_->shared_destroy != nullptr; - } - - // If the implementation uses a listener, call that. - template - static auto MatchAndExplainImpl(const MatcherBase& m, const T& value, - MatchResultListener* listener) - -> decltype(P::Get(m).MatchAndExplain(value, listener->stream())) { - return P::Get(m).MatchAndExplain(value, listener->stream()); - } - - template - static auto MatchAndExplainImpl(const MatcherBase& m, const T& value, - MatchResultListener* listener) - -> decltype(P::Get(m).MatchAndExplain(value, listener)) { - return P::Get(m).MatchAndExplain(value, listener); - } - - template - static void DescribeImpl(const MatcherBase& m, std::ostream* os, - bool negation) { - if (negation) { - P::Get(m).DescribeNegationTo(os); - } else { - P::Get(m).DescribeTo(os); - } - } - - template - static const MatcherDescriberInterface* GetDescriberImpl( - const MatcherBase& m) { - // If the impl is a MatcherDescriberInterface, then return it. - // Otherwise use MatcherBase itself. - // This allows us to implement the GetDescriber() function without support - // from the impl, but some users really want to get their impl back when - // they call GetDescriber(). - // We use std::get on a tuple as a workaround of not having `if constexpr`. - return std::get<( - std::is_convertible::value - ? 1 - : 0)>(std::make_tuple(&m, &P::Get(m))); - } - - template - const VTable* GetVTable() { - static constexpr VTable kVTable = {&MatchAndExplainImpl

, - &DescribeImpl

, &GetDescriberImpl

, - P::shared_destroy}; - return &kVTable; - } - - union Buffer { - // Add some types to give Buffer some common alignment/size use cases. - void* ptr; - double d; - int64_t i; - // And add one for the out-of-line cases. - SharedPayloadBase* shared; - }; - - void Destroy() { - if (IsShared() && buffer_.shared->Unref()) { - vtable_->shared_destroy(buffer_.shared); - } - } - - template - static constexpr bool IsInlined() { - return sizeof(M) <= sizeof(Buffer) && alignof(M) <= alignof(Buffer) && - std::is_trivially_copy_constructible::value && - std::is_trivially_destructible::value; - } - - template ()> - struct ValuePolicy { - static const M& Get(const MatcherBase& m) { - // When inlined along with Init, need to be explicit to avoid violating - // strict aliasing rules. - const M *ptr = static_cast( - static_cast(&m.buffer_)); - return *ptr; - } - static void Init(MatcherBase& m, M impl) { - ::new (static_cast(&m.buffer_)) M(impl); - } - static constexpr auto shared_destroy = nullptr; - }; - - template - struct ValuePolicy { - using Shared = SharedPayload; - static const M& Get(const MatcherBase& m) { - return static_cast(m.buffer_.shared)->value; - } - template - static void Init(MatcherBase& m, Arg&& arg) { - m.buffer_.shared = new Shared(std::forward(arg)); - } - static constexpr auto shared_destroy = &Shared::Destroy; - }; - - template - struct ValuePolicy*, B> { - using M = const MatcherInterface; - using Shared = SharedPayload>; - static const M& Get(const MatcherBase& m) { - return *static_cast(m.buffer_.shared)->value; - } - static void Init(MatcherBase& m, M* impl) { - m.buffer_.shared = new Shared(std::unique_ptr(impl)); - } - - static constexpr auto shared_destroy = &Shared::Destroy; - }; - - template - void Init(M&& m) { - using MM = typename std::decay::type; - using Policy = ValuePolicy; - vtable_ = GetVTable(); - Policy::Init(*this, std::forward(m)); - } - - const VTable* vtable_; - Buffer buffer_; -}; - -} // namespace internal - -// A Matcher is a copyable and IMMUTABLE (except by assignment) -// object that can check whether a value of type T matches. The -// implementation of Matcher is just a std::shared_ptr to const -// MatcherInterface. Don't inherit from Matcher! -template -class Matcher : public internal::MatcherBase { - public: - // Constructs a null matcher. Needed for storing Matcher objects in STL - // containers. A default-constructed matcher is not yet initialized. You - // cannot use it until a valid value has been assigned to it. - explicit Matcher() {} // NOLINT - - // Constructs a matcher from its implementation. - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - - template - explicit Matcher( - const MatcherInterface* impl, - typename std::enable_if::value>::type* = - nullptr) - : internal::MatcherBase(impl) {} - - template ::type::is_gtest_matcher> - Matcher(M&& m) : internal::MatcherBase(std::forward(m)) {} // NOLINT - - // Implicit constructor here allows people to write - // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes - Matcher(T value); // NOLINT -}; - -// The following two specializations allow the user to write str -// instead of Eq(str) and "foo" instead of Eq("foo") when a std::string -// matcher is expected. -template <> -class GTEST_API_ Matcher - : public internal::MatcherBase { - public: - Matcher() {} - - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - - template ::type::is_gtest_matcher> - Matcher(M&& m) // NOLINT - : internal::MatcherBase(std::forward(m)) {} - - // Allows the user to write str instead of Eq(str) sometimes, where - // str is a std::string object. - Matcher(const std::string& s); // NOLINT - - // Allows the user to write "foo" instead of Eq("foo") sometimes. - Matcher(const char* s); // NOLINT -}; - -template <> -class GTEST_API_ Matcher - : public internal::MatcherBase { - public: - Matcher() {} - - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - - template ::type::is_gtest_matcher> - Matcher(M&& m) // NOLINT - : internal::MatcherBase(std::forward(m)) {} - - // Allows the user to write str instead of Eq(str) sometimes, where - // str is a string object. - Matcher(const std::string& s); // NOLINT - - // Allows the user to write "foo" instead of Eq("foo") sometimes. - Matcher(const char* s); // NOLINT -}; - -#if GTEST_INTERNAL_HAS_STRING_VIEW -// The following two specializations allow the user to write str -// instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view -// matcher is expected. -template <> -class GTEST_API_ Matcher - : public internal::MatcherBase { - public: - Matcher() {} - - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - - template ::type::is_gtest_matcher> - Matcher(M&& m) // NOLINT - : internal::MatcherBase(std::forward(m)) { - } - - // Allows the user to write str instead of Eq(str) sometimes, where - // str is a std::string object. - Matcher(const std::string& s); // NOLINT - - // Allows the user to write "foo" instead of Eq("foo") sometimes. - Matcher(const char* s); // NOLINT - - // Allows the user to pass absl::string_views or std::string_views directly. - Matcher(internal::StringView s); // NOLINT -}; - -template <> -class GTEST_API_ Matcher - : public internal::MatcherBase { - public: - Matcher() {} - - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - explicit Matcher(const MatcherInterface* impl) - : internal::MatcherBase(impl) {} - - template ::type::is_gtest_matcher> - Matcher(M&& m) // NOLINT - : internal::MatcherBase(std::forward(m)) {} - - // Allows the user to write str instead of Eq(str) sometimes, where - // str is a std::string object. - Matcher(const std::string& s); // NOLINT - - // Allows the user to write "foo" instead of Eq("foo") sometimes. - Matcher(const char* s); // NOLINT - - // Allows the user to pass absl::string_views or std::string_views directly. - Matcher(internal::StringView s); // NOLINT -}; -#endif // GTEST_INTERNAL_HAS_STRING_VIEW - -// Prints a matcher in a human-readable format. -template -std::ostream& operator<<(std::ostream& os, const Matcher& matcher) { - matcher.DescribeTo(&os); - return os; -} - -// The PolymorphicMatcher class template makes it easy to implement a -// polymorphic matcher (i.e. a matcher that can match values of more -// than one type, e.g. Eq(n) and NotNull()). -// -// To define a polymorphic matcher, a user should provide an Impl -// class that has a DescribeTo() method and a DescribeNegationTo() -// method, and define a member function (or member function template) -// -// bool MatchAndExplain(const Value& value, -// MatchResultListener* listener) const; -// -// See the definition of NotNull() for a complete example. -template -class PolymorphicMatcher { - public: - explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {} - - // Returns a mutable reference to the underlying matcher - // implementation object. - Impl& mutable_impl() { return impl_; } - - // Returns an immutable reference to the underlying matcher - // implementation object. - const Impl& impl() const { return impl_; } - - template - operator Matcher() const { - return Matcher(new MonomorphicImpl(impl_)); - } - - private: - template - class MonomorphicImpl : public MatcherInterface { - public: - explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} - - void DescribeTo(::std::ostream* os) const override { impl_.DescribeTo(os); } - - void DescribeNegationTo(::std::ostream* os) const override { - impl_.DescribeNegationTo(os); - } - - bool MatchAndExplain(T x, MatchResultListener* listener) const override { - return impl_.MatchAndExplain(x, listener); - } - - private: - const Impl impl_; - }; - - Impl impl_; -}; - -// Creates a matcher from its implementation. -// DEPRECATED: Especially in the generic code, prefer: -// Matcher(new MyMatcherImpl(...)); -// -// MakeMatcher may create a Matcher that accepts its argument by value, which -// leads to unnecessary copies & lack of support for non-copyable types. -template -inline Matcher MakeMatcher(const MatcherInterface* impl) { - return Matcher(impl); -} - -// Creates a polymorphic matcher from its implementation. This is -// easier to use than the PolymorphicMatcher constructor as it -// doesn't require you to explicitly write the template argument, e.g. -// -// MakePolymorphicMatcher(foo); -// vs -// PolymorphicMatcher(foo); -template -inline PolymorphicMatcher MakePolymorphicMatcher(const Impl& impl) { - return PolymorphicMatcher(impl); -} - -namespace internal { -// Implements a matcher that compares a given value with a -// pre-supplied value using one of the ==, <=, <, etc, operators. The -// two values being compared don't have to have the same type. -// -// The matcher defined here is polymorphic (for example, Eq(5) can be -// used to match an int, a short, a double, etc). Therefore we use -// a template type conversion operator in the implementation. -// -// The following template definition assumes that the Rhs parameter is -// a "bare" type (i.e. neither 'const T' nor 'T&'). -template -class ComparisonBase { - public: - explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {} - - using is_gtest_matcher = void; - - template - bool MatchAndExplain(const Lhs& lhs, std::ostream*) const { - return Op()(lhs, Unwrap(rhs_)); - } - void DescribeTo(std::ostream* os) const { - *os << D::Desc() << " "; - UniversalPrint(Unwrap(rhs_), os); - } - void DescribeNegationTo(std::ostream* os) const { - *os << D::NegatedDesc() << " "; - UniversalPrint(Unwrap(rhs_), os); - } - - private: - template - static const T& Unwrap(const T& v) { - return v; - } - template - static const T& Unwrap(std::reference_wrapper v) { - return v; - } - - Rhs rhs_; -}; - -template -class EqMatcher : public ComparisonBase, Rhs, AnyEq> { - public: - explicit EqMatcher(const Rhs& rhs) - : ComparisonBase, Rhs, AnyEq>(rhs) { } - static const char* Desc() { return "is equal to"; } - static const char* NegatedDesc() { return "isn't equal to"; } -}; -template -class NeMatcher : public ComparisonBase, Rhs, AnyNe> { - public: - explicit NeMatcher(const Rhs& rhs) - : ComparisonBase, Rhs, AnyNe>(rhs) { } - static const char* Desc() { return "isn't equal to"; } - static const char* NegatedDesc() { return "is equal to"; } -}; -template -class LtMatcher : public ComparisonBase, Rhs, AnyLt> { - public: - explicit LtMatcher(const Rhs& rhs) - : ComparisonBase, Rhs, AnyLt>(rhs) { } - static const char* Desc() { return "is <"; } - static const char* NegatedDesc() { return "isn't <"; } -}; -template -class GtMatcher : public ComparisonBase, Rhs, AnyGt> { - public: - explicit GtMatcher(const Rhs& rhs) - : ComparisonBase, Rhs, AnyGt>(rhs) { } - static const char* Desc() { return "is >"; } - static const char* NegatedDesc() { return "isn't >"; } -}; -template -class LeMatcher : public ComparisonBase, Rhs, AnyLe> { - public: - explicit LeMatcher(const Rhs& rhs) - : ComparisonBase, Rhs, AnyLe>(rhs) { } - static const char* Desc() { return "is <="; } - static const char* NegatedDesc() { return "isn't <="; } -}; -template -class GeMatcher : public ComparisonBase, Rhs, AnyGe> { - public: - explicit GeMatcher(const Rhs& rhs) - : ComparisonBase, Rhs, AnyGe>(rhs) { } - static const char* Desc() { return "is >="; } - static const char* NegatedDesc() { return "isn't >="; } -}; - -template ::value>::type> -using StringLike = T; - -// Implements polymorphic matchers MatchesRegex(regex) and -// ContainsRegex(regex), which can be used as a Matcher as long as -// T can be converted to a string. -class MatchesRegexMatcher { - public: - MatchesRegexMatcher(const RE* regex, bool full_match) - : regex_(regex), full_match_(full_match) {} - -#if GTEST_INTERNAL_HAS_STRING_VIEW - bool MatchAndExplain(const internal::StringView& s, - MatchResultListener* listener) const { - return MatchAndExplain(std::string(s), listener); - } -#endif // GTEST_INTERNAL_HAS_STRING_VIEW - - // Accepts pointer types, particularly: - // const char* - // char* - // const wchar_t* - // wchar_t* - template - bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { - return s != nullptr && MatchAndExplain(std::string(s), listener); - } - - // Matches anything that can convert to std::string. - // - // This is a template, not just a plain function with const std::string&, - // because absl::string_view has some interfering non-explicit constructors. - template - bool MatchAndExplain(const MatcheeStringType& s, - MatchResultListener* /* listener */) const { - const std::string& s2(s); - return full_match_ ? RE::FullMatch(s2, *regex_) - : RE::PartialMatch(s2, *regex_); - } - - void DescribeTo(::std::ostream* os) const { - *os << (full_match_ ? "matches" : "contains") << " regular expression "; - UniversalPrinter::Print(regex_->pattern(), os); - } - - void DescribeNegationTo(::std::ostream* os) const { - *os << "doesn't " << (full_match_ ? "match" : "contain") - << " regular expression "; - UniversalPrinter::Print(regex_->pattern(), os); - } - - private: - const std::shared_ptr regex_; - const bool full_match_; -}; -} // namespace internal - -// Matches a string that fully matches regular expression 'regex'. -// The matcher takes ownership of 'regex'. -inline PolymorphicMatcher MatchesRegex( - const internal::RE* regex) { - return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); -} -template -PolymorphicMatcher MatchesRegex( - const internal::StringLike& regex) { - return MatchesRegex(new internal::RE(std::string(regex))); -} - -// Matches a string that contains regular expression 'regex'. -// The matcher takes ownership of 'regex'. -inline PolymorphicMatcher ContainsRegex( - const internal::RE* regex) { - return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); -} -template -PolymorphicMatcher ContainsRegex( - const internal::StringLike& regex) { - return ContainsRegex(new internal::RE(std::string(regex))); -} - -// Creates a polymorphic matcher that matches anything equal to x. -// Note: if the parameter of Eq() were declared as const T&, Eq("foo") -// wouldn't compile. -template -inline internal::EqMatcher Eq(T x) { return internal::EqMatcher(x); } - -// Constructs a Matcher from a 'value' of type T. The constructed -// matcher matches any value that's equal to 'value'. -template -Matcher::Matcher(T value) { *this = Eq(value); } - -// Creates a monomorphic matcher that matches anything with type Lhs -// and equal to rhs. A user may need to use this instead of Eq(...) -// in order to resolve an overloading ambiguity. -// -// TypedEq(x) is just a convenient short-hand for Matcher(Eq(x)) -// or Matcher(x), but more readable than the latter. -// -// We could define similar monomorphic matchers for other comparison -// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do -// it yet as those are used much less than Eq() in practice. A user -// can always write Matcher(Lt(5)) to be explicit about the type, -// for example. -template -inline Matcher TypedEq(const Rhs& rhs) { return Eq(rhs); } - -// Creates a polymorphic matcher that matches anything >= x. -template -inline internal::GeMatcher Ge(Rhs x) { - return internal::GeMatcher(x); -} - -// Creates a polymorphic matcher that matches anything > x. -template -inline internal::GtMatcher Gt(Rhs x) { - return internal::GtMatcher(x); -} - -// Creates a polymorphic matcher that matches anything <= x. -template -inline internal::LeMatcher Le(Rhs x) { - return internal::LeMatcher(x); -} - -// Creates a polymorphic matcher that matches anything < x. -template -inline internal::LtMatcher Lt(Rhs x) { - return internal::LtMatcher(x); -} - -// Creates a polymorphic matcher that matches anything != x. -template -inline internal::NeMatcher Ne(Rhs x) { - return internal::NeMatcher(x); -} -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 5046 - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ - -#include -#include - -namespace testing { -namespace internal { - -GTEST_DECLARE_string_(internal_run_death_test); - -// Names of the flags (needed for parsing Google Test flags). -const char kDeathTestStyleFlag[] = "death_test_style"; -const char kDeathTestUseFork[] = "death_test_use_fork"; -const char kInternalRunDeathTestFlag[] = "internal_run_death_test"; - -#if GTEST_HAS_DEATH_TEST - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -// DeathTest is a class that hides much of the complexity of the -// GTEST_DEATH_TEST_ macro. It is abstract; its static Create method -// returns a concrete class that depends on the prevailing death test -// style, as defined by the --gtest_death_test_style and/or -// --gtest_internal_run_death_test flags. - -// In describing the results of death tests, these terms are used with -// the corresponding definitions: -// -// exit status: The integer exit information in the format specified -// by wait(2) -// exit code: The integer code passed to exit(3), _exit(2), or -// returned from main() -class GTEST_API_ DeathTest { - public: - // Create returns false if there was an error determining the - // appropriate action to take for the current death test; for example, - // if the gtest_death_test_style flag is set to an invalid value. - // The LastMessage method will return a more detailed message in that - // case. Otherwise, the DeathTest pointer pointed to by the "test" - // argument is set. If the death test should be skipped, the pointer - // is set to NULL; otherwise, it is set to the address of a new concrete - // DeathTest object that controls the execution of the current test. - static bool Create(const char* statement, Matcher matcher, - const char* file, int line, DeathTest** test); - DeathTest(); - virtual ~DeathTest() { } - - // A helper class that aborts a death test when it's deleted. - class ReturnSentinel { - public: - explicit ReturnSentinel(DeathTest* test) : test_(test) { } - ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); } - private: - DeathTest* const test_; - GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel); - } GTEST_ATTRIBUTE_UNUSED_; - - // An enumeration of possible roles that may be taken when a death - // test is encountered. EXECUTE means that the death test logic should - // be executed immediately. OVERSEE means that the program should prepare - // the appropriate environment for a child process to execute the death - // test, then wait for it to complete. - enum TestRole { OVERSEE_TEST, EXECUTE_TEST }; - - // An enumeration of the three reasons that a test might be aborted. - enum AbortReason { - TEST_ENCOUNTERED_RETURN_STATEMENT, - TEST_THREW_EXCEPTION, - TEST_DID_NOT_DIE - }; - - // Assumes one of the above roles. - virtual TestRole AssumeRole() = 0; - - // Waits for the death test to finish and returns its status. - virtual int Wait() = 0; - - // Returns true if the death test passed; that is, the test process - // exited during the test, its exit status matches a user-supplied - // predicate, and its stderr output matches a user-supplied regular - // expression. - // The user-supplied predicate may be a macro expression rather - // than a function pointer or functor, or else Wait and Passed could - // be combined. - virtual bool Passed(bool exit_status_ok) = 0; - - // Signals that the death test did not die as expected. - virtual void Abort(AbortReason reason) = 0; - - // Returns a human-readable outcome message regarding the outcome of - // the last death test. - static const char* LastMessage(); - - static void set_last_death_test_message(const std::string& message); - - private: - // A string containing a description of the outcome of the last death test. - static std::string last_death_test_message_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest); -}; - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -// Factory interface for death tests. May be mocked out for testing. -class DeathTestFactory { - public: - virtual ~DeathTestFactory() { } - virtual bool Create(const char* statement, - Matcher matcher, const char* file, - int line, DeathTest** test) = 0; -}; - -// A concrete DeathTestFactory implementation for normal use. -class DefaultDeathTestFactory : public DeathTestFactory { - public: - bool Create(const char* statement, Matcher matcher, - const char* file, int line, DeathTest** test) override; -}; - -// Returns true if exit_status describes a process that was terminated -// by a signal, or exited normally with a nonzero exit code. -GTEST_API_ bool ExitedUnsuccessfully(int exit_status); - -// A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads -// and interpreted as a regex (rather than an Eq matcher) for legacy -// compatibility. -inline Matcher MakeDeathTestMatcher( - ::testing::internal::RE regex) { - return ContainsRegex(regex.pattern()); -} -inline Matcher MakeDeathTestMatcher(const char* regex) { - return ContainsRegex(regex); -} -inline Matcher MakeDeathTestMatcher( - const ::std::string& regex) { - return ContainsRegex(regex); -} - -// If a Matcher is passed to EXPECT_DEATH (etc.), it's -// used directly. -inline Matcher MakeDeathTestMatcher( - Matcher matcher) { - return matcher; -} - -// Traps C++ exceptions escaping statement and reports them as test -// failures. Note that trapping SEH exceptions is not implemented here. -# if GTEST_HAS_EXCEPTIONS -# define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ - try { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } catch (const ::std::exception& gtest_exception) { \ - fprintf(\ - stderr, \ - "\n%s: Caught std::exception-derived exception escaping the " \ - "death test statement. Exception message: %s\n", \ - ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \ - gtest_exception.what()); \ - fflush(stderr); \ - death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ - } catch (...) { \ - death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ - } - -# else -# define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) - -# endif - -// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*, -// ASSERT_EXIT*, and EXPECT_EXIT*. -#define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::AlwaysTrue()) { \ - ::testing::internal::DeathTest* gtest_dt; \ - if (!::testing::internal::DeathTest::Create( \ - #statement, \ - ::testing::internal::MakeDeathTestMatcher(regex_or_matcher), \ - __FILE__, __LINE__, >est_dt)) { \ - goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ - } \ - if (gtest_dt != nullptr) { \ - std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(gtest_dt); \ - switch (gtest_dt->AssumeRole()) { \ - case ::testing::internal::DeathTest::OVERSEE_TEST: \ - if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \ - goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ - } \ - break; \ - case ::testing::internal::DeathTest::EXECUTE_TEST: { \ - ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \ - gtest_dt); \ - GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \ - gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ - break; \ - } \ - default: \ - break; \ - } \ - } \ - } else \ - GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__) \ - : fail(::testing::internal::DeathTest::LastMessage()) -// The symbol "fail" here expands to something into which a message -// can be streamed. - -// This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in -// NDEBUG mode. In this case we need the statements to be executed and the macro -// must accept a streamed message even though the message is never printed. -// The regex object is not evaluated, but it is used to prevent "unused" -// warnings and to avoid an expression that doesn't compile in debug mode. -#define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::AlwaysTrue()) { \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - } else if (!::testing::internal::AlwaysTrue()) { \ - ::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \ - } else \ - ::testing::Message() - -// A class representing the parsed contents of the -// --gtest_internal_run_death_test flag, as it existed when -// RUN_ALL_TESTS was called. -class InternalRunDeathTestFlag { - public: - InternalRunDeathTestFlag(const std::string& a_file, - int a_line, - int an_index, - int a_write_fd) - : file_(a_file), line_(a_line), index_(an_index), - write_fd_(a_write_fd) {} - - ~InternalRunDeathTestFlag() { - if (write_fd_ >= 0) - posix::Close(write_fd_); - } - - const std::string& file() const { return file_; } - int line() const { return line_; } - int index() const { return index_; } - int write_fd() const { return write_fd_; } - - private: - std::string file_; - int line_; - int index_; - int write_fd_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag); -}; - -// Returns a newly created InternalRunDeathTestFlag object with fields -// initialized from the GTEST_FLAG(internal_run_death_test) flag if -// the flag is specified; otherwise returns NULL. -InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag(); - -#endif // GTEST_HAS_DEATH_TEST - -} // namespace internal -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ - -namespace testing { - -// This flag controls the style of death tests. Valid values are "threadsafe", -// meaning that the death test child process will re-execute the test binary -// from the start, running only a single death test, or "fast", -// meaning that the child process will execute the test logic immediately -// after forking. -GTEST_DECLARE_string_(death_test_style); - -#if GTEST_HAS_DEATH_TEST - -namespace internal { - -// Returns a Boolean value indicating whether the caller is currently -// executing in the context of the death test child process. Tools such as -// Valgrind heap checkers may need this to modify their behavior in death -// tests. IMPORTANT: This is an internal utility. Using it may break the -// implementation of death tests. User code MUST NOT use it. -GTEST_API_ bool InDeathTestChild(); - -} // namespace internal - -// The following macros are useful for writing death tests. - -// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is -// executed: -// -// 1. It generates a warning if there is more than one active -// thread. This is because it's safe to fork() or clone() only -// when there is a single thread. -// -// 2. The parent process clone()s a sub-process and runs the death -// test in it; the sub-process exits with code 0 at the end of the -// death test, if it hasn't exited already. -// -// 3. The parent process waits for the sub-process to terminate. -// -// 4. The parent process checks the exit code and error message of -// the sub-process. -// -// Examples: -// -// ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number"); -// for (int i = 0; i < 5; i++) { -// EXPECT_DEATH(server.ProcessRequest(i), -// "Invalid request .* in ProcessRequest()") -// << "Failed to die on request " << i; -// } -// -// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); -// -// bool KilledBySIGHUP(int exit_code) { -// return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP; -// } -// -// ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!"); -// -// The final parameter to each of these macros is a matcher applied to any data -// the sub-process wrote to stderr. For compatibility with existing tests, a -// bare string is interpreted as a regular expression matcher. -// -// On the regular expressions used in death tests: -// -// GOOGLETEST_CM0005 DO NOT DELETE -// On POSIX-compliant systems (*nix), we use the library, -// which uses the POSIX extended regex syntax. -// -// On other platforms (e.g. Windows or Mac), we only support a simple regex -// syntax implemented as part of Google Test. This limited -// implementation should be enough most of the time when writing -// death tests; though it lacks many features you can find in PCRE -// or POSIX extended regex syntax. For example, we don't support -// union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and -// repetition count ("x{5,7}"), among others. -// -// Below is the syntax that we do support. We chose it to be a -// subset of both PCRE and POSIX extended regex, so it's easy to -// learn wherever you come from. In the following: 'A' denotes a -// literal character, period (.), or a single \\ escape sequence; -// 'x' and 'y' denote regular expressions; 'm' and 'n' are for -// natural numbers. -// -// c matches any literal character c -// \\d matches any decimal digit -// \\D matches any character that's not a decimal digit -// \\f matches \f -// \\n matches \n -// \\r matches \r -// \\s matches any ASCII whitespace, including \n -// \\S matches any character that's not a whitespace -// \\t matches \t -// \\v matches \v -// \\w matches any letter, _, or decimal digit -// \\W matches any character that \\w doesn't match -// \\c matches any literal character c, which must be a punctuation -// . matches any single character except \n -// A? matches 0 or 1 occurrences of A -// A* matches 0 or many occurrences of A -// A+ matches 1 or many occurrences of A -// ^ matches the beginning of a string (not that of each line) -// $ matches the end of a string (not that of each line) -// xy matches x followed by y -// -// If you accidentally use PCRE or POSIX extended regex features -// not implemented by us, you will get a run-time failure. In that -// case, please try to rewrite your regular expression within the -// above syntax. -// -// This implementation is *not* meant to be as highly tuned or robust -// as a compiled regex library, but should perform well enough for a -// death test, which already incurs significant overhead by launching -// a child process. -// -// Known caveats: -// -// A "threadsafe" style death test obtains the path to the test -// program from argv[0] and re-executes it in the sub-process. For -// simplicity, the current implementation doesn't search the PATH -// when launching the sub-process. This means that the user must -// invoke the test program via a path that contains at least one -// path separator (e.g. path/to/foo_test and -// /absolute/path/to/bar_test are fine, but foo_test is not). This -// is rarely a problem as people usually don't put the test binary -// directory in PATH. -// - -// Asserts that a given `statement` causes the program to exit, with an -// integer exit status that satisfies `predicate`, and emitting error output -// that matches `matcher`. -# define ASSERT_EXIT(statement, predicate, matcher) \ - GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_FATAL_FAILURE_) - -// Like `ASSERT_EXIT`, but continues on to successive tests in the -// test suite, if any: -# define EXPECT_EXIT(statement, predicate, matcher) \ - GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_NONFATAL_FAILURE_) - -// Asserts that a given `statement` causes the program to exit, either by -// explicitly exiting with a nonzero exit code or being killed by a -// signal, and emitting error output that matches `matcher`. -# define ASSERT_DEATH(statement, matcher) \ - ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher) - -// Like `ASSERT_DEATH`, but continues on to successive tests in the -// test suite, if any: -# define EXPECT_DEATH(statement, matcher) \ - EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher) - -// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*: - -// Tests that an exit code describes a normal exit with a given exit code. -class GTEST_API_ ExitedWithCode { - public: - explicit ExitedWithCode(int exit_code); - ExitedWithCode(const ExitedWithCode&) = default; - void operator=(const ExitedWithCode& other) = delete; - bool operator()(int exit_status) const; - private: - const int exit_code_; -}; - -# if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA -// Tests that an exit code describes an exit due to termination by a -// given signal. -// GOOGLETEST_CM0006 DO NOT DELETE -class GTEST_API_ KilledBySignal { - public: - explicit KilledBySignal(int signum); - bool operator()(int exit_status) const; - private: - const int signum_; -}; -# endif // !GTEST_OS_WINDOWS - -// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode. -// The death testing framework causes this to have interesting semantics, -// since the sideeffects of the call are only visible in opt mode, and not -// in debug mode. -// -// In practice, this can be used to test functions that utilize the -// LOG(DFATAL) macro using the following style: -// -// int DieInDebugOr12(int* sideeffect) { -// if (sideeffect) { -// *sideeffect = 12; -// } -// LOG(DFATAL) << "death"; -// return 12; -// } -// -// TEST(TestSuite, TestDieOr12WorksInDgbAndOpt) { -// int sideeffect = 0; -// // Only asserts in dbg. -// EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death"); -// -// #ifdef NDEBUG -// // opt-mode has sideeffect visible. -// EXPECT_EQ(12, sideeffect); -// #else -// // dbg-mode no visible sideeffect. -// EXPECT_EQ(0, sideeffect); -// #endif -// } -// -// This will assert that DieInDebugReturn12InOpt() crashes in debug -// mode, usually due to a DCHECK or LOG(DFATAL), but returns the -// appropriate fallback value (12 in this case) in opt mode. If you -// need to test that a function has appropriate side-effects in opt -// mode, include assertions against the side-effects. A general -// pattern for this is: -// -// EXPECT_DEBUG_DEATH({ -// // Side-effects here will have an effect after this statement in -// // opt mode, but none in debug mode. -// EXPECT_EQ(12, DieInDebugOr12(&sideeffect)); -// }, "death"); -// -# ifdef NDEBUG - -# define EXPECT_DEBUG_DEATH(statement, regex) \ - GTEST_EXECUTE_STATEMENT_(statement, regex) - -# define ASSERT_DEBUG_DEATH(statement, regex) \ - GTEST_EXECUTE_STATEMENT_(statement, regex) - -# else - -# define EXPECT_DEBUG_DEATH(statement, regex) \ - EXPECT_DEATH(statement, regex) - -# define ASSERT_DEBUG_DEATH(statement, regex) \ - ASSERT_DEATH(statement, regex) - -# endif // NDEBUG for EXPECT_DEBUG_DEATH -#endif // GTEST_HAS_DEATH_TEST - -// This macro is used for implementing macros such as -// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where -// death tests are not supported. Those macros must compile on such systems -// if and only if EXPECT_DEATH and ASSERT_DEATH compile with the same parameters -// on systems that support death tests. This allows one to write such a macro on -// a system that does not support death tests and be sure that it will compile -// on a death-test supporting system. It is exposed publicly so that systems -// that have death-tests with stricter requirements than GTEST_HAS_DEATH_TEST -// can write their own equivalent of EXPECT_DEATH_IF_SUPPORTED and -// ASSERT_DEATH_IF_SUPPORTED. -// -// Parameters: -// statement - A statement that a macro such as EXPECT_DEATH would test -// for program termination. This macro has to make sure this -// statement is compiled but not executed, to ensure that -// EXPECT_DEATH_IF_SUPPORTED compiles with a certain -// parameter if and only if EXPECT_DEATH compiles with it. -// regex - A regex that a macro such as EXPECT_DEATH would use to test -// the output of statement. This parameter has to be -// compiled but not evaluated by this macro, to ensure that -// this macro only accepts expressions that a macro such as -// EXPECT_DEATH would accept. -// terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED -// and a return statement for ASSERT_DEATH_IF_SUPPORTED. -// This ensures that ASSERT_DEATH_IF_SUPPORTED will not -// compile inside functions where ASSERT_DEATH doesn't -// compile. -// -// The branch that has an always false condition is used to ensure that -// statement and regex are compiled (and thus syntactically correct) but -// never executed. The unreachable code macro protects the terminator -// statement from generating an 'unreachable code' warning in case -// statement unconditionally returns or throws. The Message constructor at -// the end allows the syntax of streaming additional messages into the -// macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. -# define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, terminator) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (::testing::internal::AlwaysTrue()) { \ - GTEST_LOG_(WARNING) \ - << "Death tests are not supported on this platform.\n" \ - << "Statement '" #statement "' cannot be verified."; \ - } else if (::testing::internal::AlwaysFalse()) { \ - ::testing::internal::RE::PartialMatch(".*", (regex)); \ - GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ - terminator; \ - } else \ - ::testing::Message() - -// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and -// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if -// death tests are supported; otherwise they just issue a warning. This is -// useful when you are combining death test assertions with normal test -// assertions in one test. -#if GTEST_HAS_DEATH_TEST -# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ - EXPECT_DEATH(statement, regex) -# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ - ASSERT_DEATH(statement, regex) -#else -# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ - GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, ) -# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ - GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, return) -#endif - -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Macros and functions for implementing parameterized tests -// in Google C++ Testing and Mocking Framework (Google Test) -// -// GOOGLETEST_CM0001 DO NOT DELETE -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ - -// Value-parameterized tests allow you to test your code with different -// parameters without writing multiple copies of the same test. -// -// Here is how you use value-parameterized tests: - -#if 0 - -// To write value-parameterized tests, first you should define a fixture -// class. It is usually derived from testing::TestWithParam (see below for -// another inheritance scheme that's sometimes useful in more complicated -// class hierarchies), where the type of your parameter values. -// TestWithParam is itself derived from testing::Test. T can be any -// copyable type. If it's a raw pointer, you are responsible for managing the -// lifespan of the pointed values. - -class FooTest : public ::testing::TestWithParam { - // You can implement all the usual class fixture members here. -}; - -// Then, use the TEST_P macro to define as many parameterized tests -// for this fixture as you want. The _P suffix is for "parameterized" -// or "pattern", whichever you prefer to think. - -TEST_P(FooTest, DoesBlah) { - // Inside a test, access the test parameter with the GetParam() method - // of the TestWithParam class: - EXPECT_TRUE(foo.Blah(GetParam())); - ... -} - -TEST_P(FooTest, HasBlahBlah) { - ... -} - -// Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test -// case with any set of parameters you want. Google Test defines a number -// of functions for generating test parameters. They return what we call -// (surprise!) parameter generators. Here is a summary of them, which -// are all in the testing namespace: -// -// -// Range(begin, end [, step]) - Yields values {begin, begin+step, -// begin+step+step, ...}. The values do not -// include end. step defaults to 1. -// Values(v1, v2, ..., vN) - Yields values {v1, v2, ..., vN}. -// ValuesIn(container) - Yields values from a C-style array, an STL -// ValuesIn(begin,end) container, or an iterator range [begin, end). -// Bool() - Yields sequence {false, true}. -// Combine(g1, g2, ..., gN) - Yields all combinations (the Cartesian product -// for the math savvy) of the values generated -// by the N generators. -// -// For more details, see comments at the definitions of these functions below -// in this file. -// -// The following statement will instantiate tests from the FooTest test suite -// each with parameter values "meeny", "miny", and "moe". - -INSTANTIATE_TEST_SUITE_P(InstantiationName, - FooTest, - Values("meeny", "miny", "moe")); - -// To distinguish different instances of the pattern, (yes, you -// can instantiate it more than once) the first argument to the -// INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the -// actual test suite name. Remember to pick unique prefixes for different -// instantiations. The tests from the instantiation above will have -// these names: -// -// * InstantiationName/FooTest.DoesBlah/0 for "meeny" -// * InstantiationName/FooTest.DoesBlah/1 for "miny" -// * InstantiationName/FooTest.DoesBlah/2 for "moe" -// * InstantiationName/FooTest.HasBlahBlah/0 for "meeny" -// * InstantiationName/FooTest.HasBlahBlah/1 for "miny" -// * InstantiationName/FooTest.HasBlahBlah/2 for "moe" -// -// You can use these names in --gtest_filter. -// -// This statement will instantiate all tests from FooTest again, each -// with parameter values "cat" and "dog": - -const char* pets[] = {"cat", "dog"}; -INSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets)); - -// The tests from the instantiation above will have these names: -// -// * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat" -// * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog" -// * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat" -// * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog" -// -// Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests -// in the given test suite, whether their definitions come before or -// AFTER the INSTANTIATE_TEST_SUITE_P statement. -// -// Please also note that generator expressions (including parameters to the -// generators) are evaluated in InitGoogleTest(), after main() has started. -// This allows the user on one hand, to adjust generator parameters in order -// to dynamically determine a set of tests to run and on the other hand, -// give the user a chance to inspect the generated tests with Google Test -// reflection API before RUN_ALL_TESTS() is executed. -// -// You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc -// for more examples. -// -// In the future, we plan to publish the API for defining new parameter -// generators. But for now this interface remains part of the internal -// implementation and is subject to change. -// -// -// A parameterized test fixture must be derived from testing::Test and from -// testing::WithParamInterface, where T is the type of the parameter -// values. Inheriting from TestWithParam satisfies that requirement because -// TestWithParam inherits from both Test and WithParamInterface. In more -// complicated hierarchies, however, it is occasionally useful to inherit -// separately from Test and WithParamInterface. For example: - -class BaseTest : public ::testing::Test { - // You can inherit all the usual members for a non-parameterized test - // fixture here. -}; - -class DerivedTest : public BaseTest, public ::testing::WithParamInterface { - // The usual test fixture members go here too. -}; - -TEST_F(BaseTest, HasFoo) { - // This is an ordinary non-parameterized test. -} - -TEST_P(DerivedTest, DoesBlah) { - // GetParam works just the same here as if you inherit from TestWithParam. - EXPECT_TRUE(foo.Blah(GetParam())); -} - -#endif // 0 - -#include -#include - -// Copyright 2008 Google Inc. -// All Rights Reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Type and function utilities for implementing parameterized tests. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ -#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -// Copyright 2008, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ - -#include -#include - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -namespace testing { - -// A copyable object representing the result of a test part (i.e. an -// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). -// -// Don't inherit from TestPartResult as its destructor is not virtual. -class GTEST_API_ TestPartResult { - public: - // The possible outcomes of a test part (i.e. an assertion or an - // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). - enum Type { - kSuccess, // Succeeded. - kNonFatalFailure, // Failed but the test can continue. - kFatalFailure, // Failed and the test should be terminated. - kSkip // Skipped. - }; - - // C'tor. TestPartResult does NOT have a default constructor. - // Always use this constructor (with parameters) to create a - // TestPartResult object. - TestPartResult(Type a_type, const char* a_file_name, int a_line_number, - const char* a_message) - : type_(a_type), - file_name_(a_file_name == nullptr ? "" : a_file_name), - line_number_(a_line_number), - summary_(ExtractSummary(a_message)), - message_(a_message) {} - - // Gets the outcome of the test part. - Type type() const { return type_; } - - // Gets the name of the source file where the test part took place, or - // NULL if it's unknown. - const char* file_name() const { - return file_name_.empty() ? nullptr : file_name_.c_str(); - } - - // Gets the line in the source file where the test part took place, - // or -1 if it's unknown. - int line_number() const { return line_number_; } - - // Gets the summary of the failure message. - const char* summary() const { return summary_.c_str(); } - - // Gets the message associated with the test part. - const char* message() const { return message_.c_str(); } - - // Returns true if and only if the test part was skipped. - bool skipped() const { return type_ == kSkip; } - - // Returns true if and only if the test part passed. - bool passed() const { return type_ == kSuccess; } - - // Returns true if and only if the test part non-fatally failed. - bool nonfatally_failed() const { return type_ == kNonFatalFailure; } - - // Returns true if and only if the test part fatally failed. - bool fatally_failed() const { return type_ == kFatalFailure; } - - // Returns true if and only if the test part failed. - bool failed() const { return fatally_failed() || nonfatally_failed(); } - - private: - Type type_; - - // Gets the summary of the failure message by omitting the stack - // trace in it. - static std::string ExtractSummary(const char* message); - - // The name of the source file where the test part took place, or - // "" if the source file is unknown. - std::string file_name_; - // The line in the source file where the test part took place, or -1 - // if the line number is unknown. - int line_number_; - std::string summary_; // The test failure summary. - std::string message_; // The test failure message. -}; - -// Prints a TestPartResult object. -std::ostream& operator<<(std::ostream& os, const TestPartResult& result); - -// An array of TestPartResult objects. -// -// Don't inherit from TestPartResultArray as its destructor is not -// virtual. -class GTEST_API_ TestPartResultArray { - public: - TestPartResultArray() {} - - // Appends the given TestPartResult to the array. - void Append(const TestPartResult& result); - - // Returns the TestPartResult at the given index (0-based). - const TestPartResult& GetTestPartResult(int index) const; - - // Returns the number of TestPartResult objects in the array. - int size() const; - - private: - std::vector array_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray); -}; - -// This interface knows how to report a test part result. -class GTEST_API_ TestPartResultReporterInterface { - public: - virtual ~TestPartResultReporterInterface() {} - - virtual void ReportTestPartResult(const TestPartResult& result) = 0; -}; - -namespace internal { - -// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a -// statement generates new fatal failures. To do so it registers itself as the -// current test part result reporter. Besides checking if fatal failures were -// reported, it only delegates the reporting to the former result reporter. -// The original result reporter is restored in the destructor. -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -class GTEST_API_ HasNewFatalFailureHelper - : public TestPartResultReporterInterface { - public: - HasNewFatalFailureHelper(); - ~HasNewFatalFailureHelper() override; - void ReportTestPartResult(const TestPartResult& result) override; - bool has_new_fatal_failure() const { return has_new_fatal_failure_; } - private: - bool has_new_fatal_failure_; - TestPartResultReporterInterface* original_reporter_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper); -}; - -} // namespace internal - -} // namespace testing - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ - -namespace testing { -// Input to a parameterized test name generator, describing a test parameter. -// Consists of the parameter value and the integer parameter index. -template -struct TestParamInfo { - TestParamInfo(const ParamType& a_param, size_t an_index) : - param(a_param), - index(an_index) {} - ParamType param; - size_t index; -}; - -// A builtin parameterized test name generator which returns the result of -// testing::PrintToString. -struct PrintToStringParamName { - template - std::string operator()(const TestParamInfo& info) const { - return PrintToString(info.param); - } -}; - -namespace internal { - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// Utility Functions - -// Outputs a message explaining invalid registration of different -// fixture class for the same test suite. This may happen when -// TEST_P macro is used to define two tests with the same name -// but in different namespaces. -GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name, - CodeLocation code_location); - -template class ParamGeneratorInterface; -template class ParamGenerator; - -// Interface for iterating over elements provided by an implementation -// of ParamGeneratorInterface. -template -class ParamIteratorInterface { - public: - virtual ~ParamIteratorInterface() {} - // A pointer to the base generator instance. - // Used only for the purposes of iterator comparison - // to make sure that two iterators belong to the same generator. - virtual const ParamGeneratorInterface* BaseGenerator() const = 0; - // Advances iterator to point to the next element - // provided by the generator. The caller is responsible - // for not calling Advance() on an iterator equal to - // BaseGenerator()->End(). - virtual void Advance() = 0; - // Clones the iterator object. Used for implementing copy semantics - // of ParamIterator. - virtual ParamIteratorInterface* Clone() const = 0; - // Dereferences the current iterator and provides (read-only) access - // to the pointed value. It is the caller's responsibility not to call - // Current() on an iterator equal to BaseGenerator()->End(). - // Used for implementing ParamGenerator::operator*(). - virtual const T* Current() const = 0; - // Determines whether the given iterator and other point to the same - // element in the sequence generated by the generator. - // Used for implementing ParamGenerator::operator==(). - virtual bool Equals(const ParamIteratorInterface& other) const = 0; -}; - -// Class iterating over elements provided by an implementation of -// ParamGeneratorInterface. It wraps ParamIteratorInterface -// and implements the const forward iterator concept. -template -class ParamIterator { - public: - typedef T value_type; - typedef const T& reference; - typedef ptrdiff_t difference_type; - - // ParamIterator assumes ownership of the impl_ pointer. - ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {} - ParamIterator& operator=(const ParamIterator& other) { - if (this != &other) - impl_.reset(other.impl_->Clone()); - return *this; - } - - const T& operator*() const { return *impl_->Current(); } - const T* operator->() const { return impl_->Current(); } - // Prefix version of operator++. - ParamIterator& operator++() { - impl_->Advance(); - return *this; - } - // Postfix version of operator++. - ParamIterator operator++(int /*unused*/) { - ParamIteratorInterface* clone = impl_->Clone(); - impl_->Advance(); - return ParamIterator(clone); - } - bool operator==(const ParamIterator& other) const { - return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_); - } - bool operator!=(const ParamIterator& other) const { - return !(*this == other); - } - - private: - friend class ParamGenerator; - explicit ParamIterator(ParamIteratorInterface* impl) : impl_(impl) {} - std::unique_ptr > impl_; -}; - -// ParamGeneratorInterface is the binary interface to access generators -// defined in other translation units. -template -class ParamGeneratorInterface { - public: - typedef T ParamType; - - virtual ~ParamGeneratorInterface() {} - - // Generator interface definition - virtual ParamIteratorInterface* Begin() const = 0; - virtual ParamIteratorInterface* End() const = 0; -}; - -// Wraps ParamGeneratorInterface and provides general generator syntax -// compatible with the STL Container concept. -// This class implements copy initialization semantics and the contained -// ParamGeneratorInterface instance is shared among all copies -// of the original object. This is possible because that instance is immutable. -template -class ParamGenerator { - public: - typedef ParamIterator iterator; - - explicit ParamGenerator(ParamGeneratorInterface* impl) : impl_(impl) {} - ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {} - - ParamGenerator& operator=(const ParamGenerator& other) { - impl_ = other.impl_; - return *this; - } - - iterator begin() const { return iterator(impl_->Begin()); } - iterator end() const { return iterator(impl_->End()); } - - private: - std::shared_ptr > impl_; -}; - -// Generates values from a range of two comparable values. Can be used to -// generate sequences of user-defined types that implement operator+() and -// operator<(). -// This class is used in the Range() function. -template -class RangeGenerator : public ParamGeneratorInterface { - public: - RangeGenerator(T begin, T end, IncrementT step) - : begin_(begin), end_(end), - step_(step), end_index_(CalculateEndIndex(begin, end, step)) {} - ~RangeGenerator() override {} - - ParamIteratorInterface* Begin() const override { - return new Iterator(this, begin_, 0, step_); - } - ParamIteratorInterface* End() const override { - return new Iterator(this, end_, end_index_, step_); - } - - private: - class Iterator : public ParamIteratorInterface { - public: - Iterator(const ParamGeneratorInterface* base, T value, int index, - IncrementT step) - : base_(base), value_(value), index_(index), step_(step) {} - ~Iterator() override {} - - const ParamGeneratorInterface* BaseGenerator() const override { - return base_; - } - void Advance() override { - value_ = static_cast(value_ + step_); - index_++; - } - ParamIteratorInterface* Clone() const override { - return new Iterator(*this); - } - const T* Current() const override { return &value_; } - bool Equals(const ParamIteratorInterface& other) const override { - // Having the same base generator guarantees that the other - // iterator is of the same type and we can downcast. - GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) - << "The program attempted to compare iterators " - << "from different generators." << std::endl; - const int other_index = - CheckedDowncastToActualType(&other)->index_; - return index_ == other_index; - } - - private: - Iterator(const Iterator& other) - : ParamIteratorInterface(), - base_(other.base_), value_(other.value_), index_(other.index_), - step_(other.step_) {} - - // No implementation - assignment is unsupported. - void operator=(const Iterator& other); - - const ParamGeneratorInterface* const base_; - T value_; - int index_; - const IncrementT step_; - }; // class RangeGenerator::Iterator - - static int CalculateEndIndex(const T& begin, - const T& end, - const IncrementT& step) { - int end_index = 0; - for (T i = begin; i < end; i = static_cast(i + step)) - end_index++; - return end_index; - } - - // No implementation - assignment is unsupported. - void operator=(const RangeGenerator& other); - - const T begin_; - const T end_; - const IncrementT step_; - // The index for the end() iterator. All the elements in the generated - // sequence are indexed (0-based) to aid iterator comparison. - const int end_index_; -}; // class RangeGenerator - - -// Generates values from a pair of STL-style iterators. Used in the -// ValuesIn() function. The elements are copied from the source range -// since the source can be located on the stack, and the generator -// is likely to persist beyond that stack frame. -template -class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface { - public: - template - ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end) - : container_(begin, end) {} - ~ValuesInIteratorRangeGenerator() override {} - - ParamIteratorInterface* Begin() const override { - return new Iterator(this, container_.begin()); - } - ParamIteratorInterface* End() const override { - return new Iterator(this, container_.end()); - } - - private: - typedef typename ::std::vector ContainerType; - - class Iterator : public ParamIteratorInterface { - public: - Iterator(const ParamGeneratorInterface* base, - typename ContainerType::const_iterator iterator) - : base_(base), iterator_(iterator) {} - ~Iterator() override {} - - const ParamGeneratorInterface* BaseGenerator() const override { - return base_; - } - void Advance() override { - ++iterator_; - value_.reset(); - } - ParamIteratorInterface* Clone() const override { - return new Iterator(*this); - } - // We need to use cached value referenced by iterator_ because *iterator_ - // can return a temporary object (and of type other then T), so just - // having "return &*iterator_;" doesn't work. - // value_ is updated here and not in Advance() because Advance() - // can advance iterator_ beyond the end of the range, and we cannot - // detect that fact. The client code, on the other hand, is - // responsible for not calling Current() on an out-of-range iterator. - const T* Current() const override { - if (value_.get() == nullptr) value_.reset(new T(*iterator_)); - return value_.get(); - } - bool Equals(const ParamIteratorInterface& other) const override { - // Having the same base generator guarantees that the other - // iterator is of the same type and we can downcast. - GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) - << "The program attempted to compare iterators " - << "from different generators." << std::endl; - return iterator_ == - CheckedDowncastToActualType(&other)->iterator_; - } - - private: - Iterator(const Iterator& other) - // The explicit constructor call suppresses a false warning - // emitted by gcc when supplied with the -Wextra option. - : ParamIteratorInterface(), - base_(other.base_), - iterator_(other.iterator_) {} - - const ParamGeneratorInterface* const base_; - typename ContainerType::const_iterator iterator_; - // A cached value of *iterator_. We keep it here to allow access by - // pointer in the wrapping iterator's operator->(). - // value_ needs to be mutable to be accessed in Current(). - // Use of std::unique_ptr helps manage cached value's lifetime, - // which is bound by the lifespan of the iterator itself. - mutable std::unique_ptr value_; - }; // class ValuesInIteratorRangeGenerator::Iterator - - // No implementation - assignment is unsupported. - void operator=(const ValuesInIteratorRangeGenerator& other); - - const ContainerType container_; -}; // class ValuesInIteratorRangeGenerator - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Default parameterized test name generator, returns a string containing the -// integer test parameter index. -template -std::string DefaultParamName(const TestParamInfo& info) { - Message name_stream; - name_stream << info.index; - return name_stream.GetString(); -} - -template -void TestNotEmpty() { - static_assert(sizeof(T) == 0, "Empty arguments are not allowed."); -} -template -void TestNotEmpty(const T&) {} - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Stores a parameter value and later creates tests parameterized with that -// value. -template -class ParameterizedTestFactory : public TestFactoryBase { - public: - typedef typename TestClass::ParamType ParamType; - explicit ParameterizedTestFactory(ParamType parameter) : - parameter_(parameter) {} - Test* CreateTest() override { - TestClass::SetParam(¶meter_); - return new TestClass(); - } - - private: - const ParamType parameter_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory); -}; - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// TestMetaFactoryBase is a base class for meta-factories that create -// test factories for passing into MakeAndRegisterTestInfo function. -template -class TestMetaFactoryBase { - public: - virtual ~TestMetaFactoryBase() {} - - virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0; -}; - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// TestMetaFactory creates test factories for passing into -// MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives -// ownership of test factory pointer, same factory object cannot be passed -// into that method twice. But ParameterizedTestSuiteInfo is going to call -// it for each Test/Parameter value combination. Thus it needs meta factory -// creator class. -template -class TestMetaFactory - : public TestMetaFactoryBase { - public: - using ParamType = typename TestSuite::ParamType; - - TestMetaFactory() {} - - TestFactoryBase* CreateTestFactory(ParamType parameter) override { - return new ParameterizedTestFactory(parameter); - } - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory); -}; - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// ParameterizedTestSuiteInfoBase is a generic interface -// to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase -// accumulates test information provided by TEST_P macro invocations -// and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations -// and uses that information to register all resulting test instances -// in RegisterTests method. The ParameterizeTestSuiteRegistry class holds -// a collection of pointers to the ParameterizedTestSuiteInfo objects -// and calls RegisterTests() on each of them when asked. -class ParameterizedTestSuiteInfoBase { - public: - virtual ~ParameterizedTestSuiteInfoBase() {} - - // Base part of test suite name for display purposes. - virtual const std::string& GetTestSuiteName() const = 0; - // Test suite id to verify identity. - virtual TypeId GetTestSuiteTypeId() const = 0; - // UnitTest class invokes this method to register tests in this - // test suite right before running them in RUN_ALL_TESTS macro. - // This method should not be called more than once on any single - // instance of a ParameterizedTestSuiteInfoBase derived class. - virtual void RegisterTests() = 0; - - protected: - ParameterizedTestSuiteInfoBase() {} - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestSuiteInfoBase); -}; - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Report a the name of a test_suit as safe to ignore -// as the side effect of construction of this type. -struct GTEST_API_ MarkAsIgnored { - explicit MarkAsIgnored(const char* test_suite); -}; - -GTEST_API_ void InsertSyntheticTestCase(const std::string& name, - CodeLocation location, bool has_test_p); - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P -// macro invocations for a particular test suite and generators -// obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that -// test suite. It registers tests with all values generated by all -// generators when asked. -template -class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase { - public: - // ParamType and GeneratorCreationFunc are private types but are required - // for declarations of public methods AddTestPattern() and - // AddTestSuiteInstantiation(). - using ParamType = typename TestSuite::ParamType; - // A function that returns an instance of appropriate generator type. - typedef ParamGenerator(GeneratorCreationFunc)(); - using ParamNameGeneratorFunc = std::string(const TestParamInfo&); - - explicit ParameterizedTestSuiteInfo(const char* name, - CodeLocation code_location) - : test_suite_name_(name), code_location_(code_location) {} - - // Test suite base name for display purposes. - const std::string& GetTestSuiteName() const override { - return test_suite_name_; - } - // Test suite id to verify identity. - TypeId GetTestSuiteTypeId() const override { return GetTypeId(); } - // TEST_P macro uses AddTestPattern() to record information - // about a single test in a LocalTestInfo structure. - // test_suite_name is the base name of the test suite (without invocation - // prefix). test_base_name is the name of an individual test without - // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is - // test suite base name and DoBar is test base name. - void AddTestPattern(const char* test_suite_name, const char* test_base_name, - TestMetaFactoryBase* meta_factory, - CodeLocation code_location) { - tests_.push_back(std::shared_ptr(new TestInfo( - test_suite_name, test_base_name, meta_factory, code_location))); - } - // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information - // about a generator. - int AddTestSuiteInstantiation(const std::string& instantiation_name, - GeneratorCreationFunc* func, - ParamNameGeneratorFunc* name_func, - const char* file, int line) { - instantiations_.push_back( - InstantiationInfo(instantiation_name, func, name_func, file, line)); - return 0; // Return value used only to run this method in namespace scope. - } - // UnitTest class invokes this method to register tests in this test suite - // right before running tests in RUN_ALL_TESTS macro. - // This method should not be called more than once on any single - // instance of a ParameterizedTestSuiteInfoBase derived class. - // UnitTest has a guard to prevent from calling this method more than once. - void RegisterTests() override { - bool generated_instantiations = false; - - for (typename TestInfoContainer::iterator test_it = tests_.begin(); - test_it != tests_.end(); ++test_it) { - std::shared_ptr test_info = *test_it; - for (typename InstantiationContainer::iterator gen_it = - instantiations_.begin(); gen_it != instantiations_.end(); - ++gen_it) { - const std::string& instantiation_name = gen_it->name; - ParamGenerator generator((*gen_it->generator)()); - ParamNameGeneratorFunc* name_func = gen_it->name_func; - const char* file = gen_it->file; - int line = gen_it->line; - - std::string test_suite_name; - if ( !instantiation_name.empty() ) - test_suite_name = instantiation_name + "/"; - test_suite_name += test_info->test_suite_base_name; - - size_t i = 0; - std::set test_param_names; - for (typename ParamGenerator::iterator param_it = - generator.begin(); - param_it != generator.end(); ++param_it, ++i) { - generated_instantiations = true; - - Message test_name_stream; - - std::string param_name = name_func( - TestParamInfo(*param_it, i)); - - GTEST_CHECK_(IsValidParamName(param_name)) - << "Parameterized test name '" << param_name - << "' is invalid, in " << file - << " line " << line << std::endl; - - GTEST_CHECK_(test_param_names.count(param_name) == 0) - << "Duplicate parameterized test name '" << param_name - << "', in " << file << " line " << line << std::endl; - - test_param_names.insert(param_name); - - if (!test_info->test_base_name.empty()) { - test_name_stream << test_info->test_base_name << "/"; - } - test_name_stream << param_name; - MakeAndRegisterTestInfo( - test_suite_name.c_str(), test_name_stream.GetString().c_str(), - nullptr, // No type parameter. - PrintToString(*param_it).c_str(), test_info->code_location, - GetTestSuiteTypeId(), - SuiteApiResolver::GetSetUpCaseOrSuite(file, line), - SuiteApiResolver::GetTearDownCaseOrSuite(file, line), - test_info->test_meta_factory->CreateTestFactory(*param_it)); - } // for param_it - } // for gen_it - } // for test_it - - if (!generated_instantiations) { - // There are no generaotrs, or they all generate nothing ... - InsertSyntheticTestCase(GetTestSuiteName(), code_location_, - !tests_.empty()); - } - } // RegisterTests - - private: - // LocalTestInfo structure keeps information about a single test registered - // with TEST_P macro. - struct TestInfo { - TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name, - TestMetaFactoryBase* a_test_meta_factory, - CodeLocation a_code_location) - : test_suite_base_name(a_test_suite_base_name), - test_base_name(a_test_base_name), - test_meta_factory(a_test_meta_factory), - code_location(a_code_location) {} - - const std::string test_suite_base_name; - const std::string test_base_name; - const std::unique_ptr > test_meta_factory; - const CodeLocation code_location; - }; - using TestInfoContainer = ::std::vector >; - // Records data received from INSTANTIATE_TEST_SUITE_P macros: - // - struct InstantiationInfo { - InstantiationInfo(const std::string &name_in, - GeneratorCreationFunc* generator_in, - ParamNameGeneratorFunc* name_func_in, - const char* file_in, - int line_in) - : name(name_in), - generator(generator_in), - name_func(name_func_in), - file(file_in), - line(line_in) {} - - std::string name; - GeneratorCreationFunc* generator; - ParamNameGeneratorFunc* name_func; - const char* file; - int line; - }; - typedef ::std::vector InstantiationContainer; - - static bool IsValidParamName(const std::string& name) { - // Check for empty string - if (name.empty()) - return false; - - // Check for invalid characters - for (std::string::size_type index = 0; index < name.size(); ++index) { - if (!IsAlNum(name[index]) && name[index] != '_') - return false; - } - - return true; - } - - const std::string test_suite_name_; - CodeLocation code_location_; - TestInfoContainer tests_; - InstantiationContainer instantiations_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestSuiteInfo); -}; // class ParameterizedTestSuiteInfo - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -template -using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// ParameterizedTestSuiteRegistry contains a map of -// ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P -// and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding -// ParameterizedTestSuiteInfo descriptors. -class ParameterizedTestSuiteRegistry { - public: - ParameterizedTestSuiteRegistry() {} - ~ParameterizedTestSuiteRegistry() { - for (auto& test_suite_info : test_suite_infos_) { - delete test_suite_info; - } - } - - // Looks up or creates and returns a structure containing information about - // tests and instantiations of a particular test suite. - template - ParameterizedTestSuiteInfo* GetTestSuitePatternHolder( - const char* test_suite_name, CodeLocation code_location) { - ParameterizedTestSuiteInfo* typed_test_info = nullptr; - for (auto& test_suite_info : test_suite_infos_) { - if (test_suite_info->GetTestSuiteName() == test_suite_name) { - if (test_suite_info->GetTestSuiteTypeId() != GetTypeId()) { - // Complain about incorrect usage of Google Test facilities - // and terminate the program since we cannot guaranty correct - // test suite setup and tear-down in this case. - ReportInvalidTestSuiteType(test_suite_name, code_location); - posix::Abort(); - } else { - // At this point we are sure that the object we found is of the same - // type we are looking for, so we downcast it to that type - // without further checks. - typed_test_info = CheckedDowncastToActualType< - ParameterizedTestSuiteInfo >(test_suite_info); - } - break; - } - } - if (typed_test_info == nullptr) { - typed_test_info = new ParameterizedTestSuiteInfo( - test_suite_name, code_location); - test_suite_infos_.push_back(typed_test_info); - } - return typed_test_info; - } - void RegisterTests() { - for (auto& test_suite_info : test_suite_infos_) { - test_suite_info->RegisterTests(); - } - } -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - template - ParameterizedTestCaseInfo* GetTestCasePatternHolder( - const char* test_case_name, CodeLocation code_location) { - return GetTestSuitePatternHolder(test_case_name, code_location); - } - -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - private: - using TestSuiteInfoContainer = ::std::vector; - - TestSuiteInfoContainer test_suite_infos_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestSuiteRegistry); -}; - -// Keep track of what type-parameterized test suite are defined and -// where as well as which are intatiated. This allows susequently -// identifying suits that are defined but never used. -class TypeParameterizedTestSuiteRegistry { - public: - // Add a suite definition - void RegisterTestSuite(const char* test_suite_name, - CodeLocation code_location); - - // Add an instantiation of a suit. - void RegisterInstantiation(const char* test_suite_name); - - // For each suit repored as defined but not reported as instantiation, - // emit a test that reports that fact (configurably, as an error). - void CheckForInstantiations(); - - private: - struct TypeParameterizedTestSuiteInfo { - explicit TypeParameterizedTestSuiteInfo(CodeLocation c) - : code_location(c), instantiated(false) {} - - CodeLocation code_location; - bool instantiated; - }; - - std::map suites_; -}; - -} // namespace internal - -// Forward declarations of ValuesIn(), which is implemented in -// include/gtest/gtest-param-test.h. -template -internal::ParamGenerator ValuesIn( - const Container& container); - -namespace internal { -// Used in the Values() function to provide polymorphic capabilities. - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable : 4100) -#endif - -template -class ValueArray { - public: - explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {} - - template - operator ParamGenerator() const { // NOLINT - return ValuesIn(MakeVector(MakeIndexSequence())); - } - - private: - template - std::vector MakeVector(IndexSequence) const { - return std::vector{static_cast(v_.template Get())...}; - } - - FlatTuple v_; -}; - -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -template -class CartesianProductGenerator - : public ParamGeneratorInterface<::std::tuple> { - public: - typedef ::std::tuple ParamType; - - CartesianProductGenerator(const std::tuple...>& g) - : generators_(g) {} - ~CartesianProductGenerator() override {} - - ParamIteratorInterface* Begin() const override { - return new Iterator(this, generators_, false); - } - ParamIteratorInterface* End() const override { - return new Iterator(this, generators_, true); - } - - private: - template - class IteratorImpl; - template - class IteratorImpl> - : public ParamIteratorInterface { - public: - IteratorImpl(const ParamGeneratorInterface* base, - const std::tuple...>& generators, bool is_end) - : base_(base), - begin_(std::get(generators).begin()...), - end_(std::get(generators).end()...), - current_(is_end ? end_ : begin_) { - ComputeCurrentValue(); - } - ~IteratorImpl() override {} - - const ParamGeneratorInterface* BaseGenerator() const override { - return base_; - } - // Advance should not be called on beyond-of-range iterators - // so no component iterators must be beyond end of range, either. - void Advance() override { - assert(!AtEnd()); - // Advance the last iterator. - ++std::get(current_); - // if that reaches end, propagate that up. - AdvanceIfEnd(); - ComputeCurrentValue(); - } - ParamIteratorInterface* Clone() const override { - return new IteratorImpl(*this); - } - - const ParamType* Current() const override { return current_value_.get(); } - - bool Equals(const ParamIteratorInterface& other) const override { - // Having the same base generator guarantees that the other - // iterator is of the same type and we can downcast. - GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) - << "The program attempted to compare iterators " - << "from different generators." << std::endl; - const IteratorImpl* typed_other = - CheckedDowncastToActualType(&other); - - // We must report iterators equal if they both point beyond their - // respective ranges. That can happen in a variety of fashions, - // so we have to consult AtEnd(). - if (AtEnd() && typed_other->AtEnd()) return true; - - bool same = true; - bool dummy[] = { - (same = same && std::get(current_) == - std::get(typed_other->current_))...}; - (void)dummy; - return same; - } - - private: - template - void AdvanceIfEnd() { - if (std::get(current_) != std::get(end_)) return; - - bool last = ThisI == 0; - if (last) { - // We are done. Nothing else to propagate. - return; - } - - constexpr size_t NextI = ThisI - (ThisI != 0); - std::get(current_) = std::get(begin_); - ++std::get(current_); - AdvanceIfEnd(); - } - - void ComputeCurrentValue() { - if (!AtEnd()) - current_value_ = std::make_shared(*std::get(current_)...); - } - bool AtEnd() const { - bool at_end = false; - bool dummy[] = { - (at_end = at_end || std::get(current_) == std::get(end_))...}; - (void)dummy; - return at_end; - } - - const ParamGeneratorInterface* const base_; - std::tuple::iterator...> begin_; - std::tuple::iterator...> end_; - std::tuple::iterator...> current_; - std::shared_ptr current_value_; - }; - - using Iterator = IteratorImpl::type>; - - std::tuple...> generators_; -}; - -template -class CartesianProductHolder { - public: - CartesianProductHolder(const Gen&... g) : generators_(g...) {} - template - operator ParamGenerator<::std::tuple>() const { - return ParamGenerator<::std::tuple>( - new CartesianProductGenerator(generators_)); - } - - private: - std::tuple generators_; -}; - -} // namespace internal -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ - -namespace testing { - -// Functions producing parameter generators. -// -// Google Test uses these generators to produce parameters for value- -// parameterized tests. When a parameterized test suite is instantiated -// with a particular generator, Google Test creates and runs tests -// for each element in the sequence produced by the generator. -// -// In the following sample, tests from test suite FooTest are instantiated -// each three times with parameter values 3, 5, and 8: -// -// class FooTest : public TestWithParam { ... }; -// -// TEST_P(FooTest, TestThis) { -// } -// TEST_P(FooTest, TestThat) { -// } -// INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8)); -// - -// Range() returns generators providing sequences of values in a range. -// -// Synopsis: -// Range(start, end) -// - returns a generator producing a sequence of values {start, start+1, -// start+2, ..., }. -// Range(start, end, step) -// - returns a generator producing a sequence of values {start, start+step, -// start+step+step, ..., }. -// Notes: -// * The generated sequences never include end. For example, Range(1, 5) -// returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2) -// returns a generator producing {1, 3, 5, 7}. -// * start and end must have the same type. That type may be any integral or -// floating-point type or a user defined type satisfying these conditions: -// * It must be assignable (have operator=() defined). -// * It must have operator+() (operator+(int-compatible type) for -// two-operand version). -// * It must have operator<() defined. -// Elements in the resulting sequences will also have that type. -// * Condition start < end must be satisfied in order for resulting sequences -// to contain any elements. -// -template -internal::ParamGenerator Range(T start, T end, IncrementT step) { - return internal::ParamGenerator( - new internal::RangeGenerator(start, end, step)); -} - -template -internal::ParamGenerator Range(T start, T end) { - return Range(start, end, 1); -} - -// ValuesIn() function allows generation of tests with parameters coming from -// a container. -// -// Synopsis: -// ValuesIn(const T (&array)[N]) -// - returns a generator producing sequences with elements from -// a C-style array. -// ValuesIn(const Container& container) -// - returns a generator producing sequences with elements from -// an STL-style container. -// ValuesIn(Iterator begin, Iterator end) -// - returns a generator producing sequences with elements from -// a range [begin, end) defined by a pair of STL-style iterators. These -// iterators can also be plain C pointers. -// -// Please note that ValuesIn copies the values from the containers -// passed in and keeps them to generate tests in RUN_ALL_TESTS(). -// -// Examples: -// -// This instantiates tests from test suite StringTest -// each with C-string values of "foo", "bar", and "baz": -// -// const char* strings[] = {"foo", "bar", "baz"}; -// INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings)); -// -// This instantiates tests from test suite StlStringTest -// each with STL strings with values "a" and "b": -// -// ::std::vector< ::std::string> GetParameterStrings() { -// ::std::vector< ::std::string> v; -// v.push_back("a"); -// v.push_back("b"); -// return v; -// } -// -// INSTANTIATE_TEST_SUITE_P(CharSequence, -// StlStringTest, -// ValuesIn(GetParameterStrings())); -// -// -// This will also instantiate tests from CharTest -// each with parameter values 'a' and 'b': -// -// ::std::list GetParameterChars() { -// ::std::list list; -// list.push_back('a'); -// list.push_back('b'); -// return list; -// } -// ::std::list l = GetParameterChars(); -// INSTANTIATE_TEST_SUITE_P(CharSequence2, -// CharTest, -// ValuesIn(l.begin(), l.end())); -// -template -internal::ParamGenerator< - typename std::iterator_traits::value_type> -ValuesIn(ForwardIterator begin, ForwardIterator end) { - typedef typename std::iterator_traits::value_type ParamType; - return internal::ParamGenerator( - new internal::ValuesInIteratorRangeGenerator(begin, end)); -} - -template -internal::ParamGenerator ValuesIn(const T (&array)[N]) { - return ValuesIn(array, array + N); -} - -template -internal::ParamGenerator ValuesIn( - const Container& container) { - return ValuesIn(container.begin(), container.end()); -} - -// Values() allows generating tests from explicitly specified list of -// parameters. -// -// Synopsis: -// Values(T v1, T v2, ..., T vN) -// - returns a generator producing sequences with elements v1, v2, ..., vN. -// -// For example, this instantiates tests from test suite BarTest each -// with values "one", "two", and "three": -// -// INSTANTIATE_TEST_SUITE_P(NumSequence, -// BarTest, -// Values("one", "two", "three")); -// -// This instantiates tests from test suite BazTest each with values 1, 2, 3.5. -// The exact type of values will depend on the type of parameter in BazTest. -// -// INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5)); -// -// -template -internal::ValueArray Values(T... v) { - return internal::ValueArray(std::move(v)...); -} - -// Bool() allows generating tests with parameters in a set of (false, true). -// -// Synopsis: -// Bool() -// - returns a generator producing sequences with elements {false, true}. -// -// It is useful when testing code that depends on Boolean flags. Combinations -// of multiple flags can be tested when several Bool()'s are combined using -// Combine() function. -// -// In the following example all tests in the test suite FlagDependentTest -// will be instantiated twice with parameters false and true. -// -// class FlagDependentTest : public testing::TestWithParam { -// virtual void SetUp() { -// external_flag = GetParam(); -// } -// } -// INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool()); -// -inline internal::ParamGenerator Bool() { - return Values(false, true); -} - -// Combine() allows the user to combine two or more sequences to produce -// values of a Cartesian product of those sequences' elements. -// -// Synopsis: -// Combine(gen1, gen2, ..., genN) -// - returns a generator producing sequences with elements coming from -// the Cartesian product of elements from the sequences generated by -// gen1, gen2, ..., genN. The sequence elements will have a type of -// std::tuple where T1, T2, ..., TN are the types -// of elements from sequences produces by gen1, gen2, ..., genN. -// -// Example: -// -// This will instantiate tests in test suite AnimalTest each one with -// the parameter values tuple("cat", BLACK), tuple("cat", WHITE), -// tuple("dog", BLACK), and tuple("dog", WHITE): -// -// enum Color { BLACK, GRAY, WHITE }; -// class AnimalTest -// : public testing::TestWithParam > {...}; -// -// TEST_P(AnimalTest, AnimalLooksNice) {...} -// -// INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest, -// Combine(Values("cat", "dog"), -// Values(BLACK, WHITE))); -// -// This will instantiate tests in FlagDependentTest with all variations of two -// Boolean flags: -// -// class FlagDependentTest -// : public testing::TestWithParam > { -// virtual void SetUp() { -// // Assigns external_flag_1 and external_flag_2 values from the tuple. -// std::tie(external_flag_1, external_flag_2) = GetParam(); -// } -// }; -// -// TEST_P(FlagDependentTest, TestFeature1) { -// // Test your code using external_flag_1 and external_flag_2 here. -// } -// INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest, -// Combine(Bool(), Bool())); -// -template -internal::CartesianProductHolder Combine(const Generator&... g) { - return internal::CartesianProductHolder(g...); -} - -#define TEST_P(test_suite_name, test_name) \ - class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ - : public test_suite_name { \ - public: \ - GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \ - void TestBody() override; \ - \ - private: \ - static int AddToRegistry() { \ - ::testing::UnitTest::GetInstance() \ - ->parameterized_test_registry() \ - .GetTestSuitePatternHolder( \ - GTEST_STRINGIFY_(test_suite_name), \ - ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ - ->AddTestPattern( \ - GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name), \ - new ::testing::internal::TestMetaFactory(), \ - ::testing::internal::CodeLocation(__FILE__, __LINE__)); \ - return 0; \ - } \ - static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \ - GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_suite_name, \ - test_name)); \ - }; \ - int GTEST_TEST_CLASS_NAME_(test_suite_name, \ - test_name)::gtest_registering_dummy_ = \ - GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry(); \ - void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody() - -// The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify -// generator and an optional function or functor that generates custom test name -// suffixes based on the test parameters. Such a function or functor should -// accept one argument of type testing::TestParamInfo, and -// return std::string. -// -// testing::PrintToStringParamName is a builtin test suffix generator that -// returns the value of testing::PrintToString(GetParam()). -// -// Note: test names must be non-empty, unique, and may only contain ASCII -// alphanumeric characters or underscore. Because PrintToString adds quotes -// to std::string and C strings, it won't work for these types. - -#define GTEST_EXPAND_(arg) arg -#define GTEST_GET_FIRST_(first, ...) first -#define GTEST_GET_SECOND_(first, second, ...) second - -#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \ - static ::testing::internal::ParamGenerator \ - gtest_##prefix##test_suite_name##_EvalGenerator_() { \ - return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \ - } \ - static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \ - const ::testing::TestParamInfo& info) { \ - if (::testing::internal::AlwaysFalse()) { \ - ::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \ - __VA_ARGS__, \ - ::testing::internal::DefaultParamName, \ - DUMMY_PARAM_))); \ - auto t = std::make_tuple(__VA_ARGS__); \ - static_assert(std::tuple_size::value <= 2, \ - "Too Many Args!"); \ - } \ - return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \ - __VA_ARGS__, \ - ::testing::internal::DefaultParamName, \ - DUMMY_PARAM_))))(info); \ - } \ - static int gtest_##prefix##test_suite_name##_dummy_ \ - GTEST_ATTRIBUTE_UNUSED_ = \ - ::testing::UnitTest::GetInstance() \ - ->parameterized_test_registry() \ - .GetTestSuitePatternHolder( \ - GTEST_STRINGIFY_(test_suite_name), \ - ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ - ->AddTestSuiteInstantiation( \ - GTEST_STRINGIFY_(prefix), \ - >est_##prefix##test_suite_name##_EvalGenerator_, \ - >est_##prefix##test_suite_name##_EvalGenerateName_, \ - __FILE__, __LINE__) - - -// Allow Marking a Parameterized test class as not needing to be instantiated. -#define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \ - namespace gtest_do_not_use_outside_namespace_scope {} \ - static const ::testing::internal::MarkAsIgnored gtest_allow_ignore_##T( \ - GTEST_STRINGIFY_(T)) - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -#define INSTANTIATE_TEST_CASE_P \ - static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \ - ""); \ - INSTANTIATE_TEST_SUITE_P -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ -// Copyright 2006, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// -// Google C++ Testing and Mocking Framework definitions useful in production code. -// GOOGLETEST_CM0003 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PROD_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_PROD_H_ - -// When you need to test the private or protected members of a class, -// use the FRIEND_TEST macro to declare your tests as friends of the -// class. For example: -// -// class MyClass { -// private: -// void PrivateMethod(); -// FRIEND_TEST(MyClassTest, PrivateMethodWorks); -// }; -// -// class MyClassTest : public testing::Test { -// // ... -// }; -// -// TEST_F(MyClassTest, PrivateMethodWorks) { -// // Can call MyClass::PrivateMethod() here. -// } -// -// Note: The test class must be in the same namespace as the class being tested. -// For example, putting MyClassTest in an anonymous namespace will not work. - -#define FRIEND_TEST(test_case_name, test_name)\ -friend class test_case_name##_##test_name##_Test - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PROD_H_ -// Copyright 2008 Google Inc. -// All Rights Reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ - -// This header implements typed tests and type-parameterized tests. - -// Typed (aka type-driven) tests repeat the same test for types in a -// list. You must know which types you want to test with when writing -// typed tests. Here's how you do it: - -#if 0 - -// First, define a fixture class template. It should be parameterized -// by a type. Remember to derive it from testing::Test. -template -class FooTest : public testing::Test { - public: - ... - typedef std::list List; - static T shared_; - T value_; -}; - -// Next, associate a list of types with the test suite, which will be -// repeated for each type in the list. The typedef is necessary for -// the macro to parse correctly. -typedef testing::Types MyTypes; -TYPED_TEST_SUITE(FooTest, MyTypes); - -// If the type list contains only one type, you can write that type -// directly without Types<...>: -// TYPED_TEST_SUITE(FooTest, int); - -// Then, use TYPED_TEST() instead of TEST_F() to define as many typed -// tests for this test suite as you want. -TYPED_TEST(FooTest, DoesBlah) { - // Inside a test, refer to the special name TypeParam to get the type - // parameter. Since we are inside a derived class template, C++ requires - // us to visit the members of FooTest via 'this'. - TypeParam n = this->value_; - - // To visit static members of the fixture, add the TestFixture:: - // prefix. - n += TestFixture::shared_; - - // To refer to typedefs in the fixture, add the "typename - // TestFixture::" prefix. - typename TestFixture::List values; - values.push_back(n); - ... -} - -TYPED_TEST(FooTest, HasPropertyA) { ... } - -// TYPED_TEST_SUITE takes an optional third argument which allows to specify a -// class that generates custom test name suffixes based on the type. This should -// be a class which has a static template function GetName(int index) returning -// a string for each type. The provided integer index equals the index of the -// type in the provided type list. In many cases the index can be ignored. -// -// For example: -// class MyTypeNames { -// public: -// template -// static std::string GetName(int) { -// if (std::is_same()) return "char"; -// if (std::is_same()) return "int"; -// if (std::is_same()) return "unsignedInt"; -// } -// }; -// TYPED_TEST_SUITE(FooTest, MyTypes, MyTypeNames); - -#endif // 0 - -// Type-parameterized tests are abstract test patterns parameterized -// by a type. Compared with typed tests, type-parameterized tests -// allow you to define the test pattern without knowing what the type -// parameters are. The defined pattern can be instantiated with -// different types any number of times, in any number of translation -// units. -// -// If you are designing an interface or concept, you can define a -// suite of type-parameterized tests to verify properties that any -// valid implementation of the interface/concept should have. Then, -// each implementation can easily instantiate the test suite to verify -// that it conforms to the requirements, without having to write -// similar tests repeatedly. Here's an example: - -#if 0 - -// First, define a fixture class template. It should be parameterized -// by a type. Remember to derive it from testing::Test. -template -class FooTest : public testing::Test { - ... -}; - -// Next, declare that you will define a type-parameterized test suite -// (the _P suffix is for "parameterized" or "pattern", whichever you -// prefer): -TYPED_TEST_SUITE_P(FooTest); - -// Then, use TYPED_TEST_P() to define as many type-parameterized tests -// for this type-parameterized test suite as you want. -TYPED_TEST_P(FooTest, DoesBlah) { - // Inside a test, refer to TypeParam to get the type parameter. - TypeParam n = 0; - ... -} - -TYPED_TEST_P(FooTest, HasPropertyA) { ... } - -// Now the tricky part: you need to register all test patterns before -// you can instantiate them. The first argument of the macro is the -// test suite name; the rest are the names of the tests in this test -// case. -REGISTER_TYPED_TEST_SUITE_P(FooTest, - DoesBlah, HasPropertyA); - -// Finally, you are free to instantiate the pattern with the types you -// want. If you put the above code in a header file, you can #include -// it in multiple C++ source files and instantiate it multiple times. -// -// To distinguish different instances of the pattern, the first -// argument to the INSTANTIATE_* macro is a prefix that will be added -// to the actual test suite name. Remember to pick unique prefixes for -// different instances. -typedef testing::Types MyTypes; -INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); - -// If the type list contains only one type, you can write that type -// directly without Types<...>: -// INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, int); -// -// Similar to the optional argument of TYPED_TEST_SUITE above, -// INSTANTIATE_TEST_SUITE_P takes an optional fourth argument which allows to -// generate custom names. -// INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes, MyTypeNames); - -#endif // 0 - - -// Implements typed tests. - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Expands to the name of the typedef for the type parameters of the -// given test suite. -#define GTEST_TYPE_PARAMS_(TestSuiteName) gtest_type_params_##TestSuiteName##_ - -// Expands to the name of the typedef for the NameGenerator, responsible for -// creating the suffixes of the name. -#define GTEST_NAME_GENERATOR_(TestSuiteName) \ - gtest_type_params_##TestSuiteName##_NameGenerator - -#define TYPED_TEST_SUITE(CaseName, Types, ...) \ - typedef ::testing::internal::GenerateTypeList::type \ - GTEST_TYPE_PARAMS_(CaseName); \ - typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \ - GTEST_NAME_GENERATOR_(CaseName) - -#define TYPED_TEST(CaseName, TestName) \ - static_assert(sizeof(GTEST_STRINGIFY_(TestName)) > 1, \ - "test-name must not be empty"); \ - template \ - class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ - : public CaseName { \ - private: \ - typedef CaseName TestFixture; \ - typedef gtest_TypeParam_ TypeParam; \ - void TestBody() override; \ - }; \ - static bool gtest_##CaseName##_##TestName##_registered_ \ - GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \ - CaseName, \ - ::testing::internal::TemplateSel, \ - GTEST_TYPE_PARAMS_( \ - CaseName)>::Register("", \ - ::testing::internal::CodeLocation( \ - __FILE__, __LINE__), \ - GTEST_STRINGIFY_(CaseName), \ - GTEST_STRINGIFY_(TestName), 0, \ - ::testing::internal::GenerateNames< \ - GTEST_NAME_GENERATOR_(CaseName), \ - GTEST_TYPE_PARAMS_(CaseName)>()); \ - template \ - void GTEST_TEST_CLASS_NAME_(CaseName, \ - TestName)::TestBody() - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -#define TYPED_TEST_CASE \ - static_assert(::testing::internal::TypedTestCaseIsDeprecated(), ""); \ - TYPED_TEST_SUITE -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -// Implements type-parameterized tests. - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Expands to the namespace name that the type-parameterized tests for -// the given type-parameterized test suite are defined in. The exact -// name of the namespace is subject to change without notice. -#define GTEST_SUITE_NAMESPACE_(TestSuiteName) gtest_suite_##TestSuiteName##_ - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// -// Expands to the name of the variable used to remember the names of -// the defined tests in the given test suite. -#define GTEST_TYPED_TEST_SUITE_P_STATE_(TestSuiteName) \ - gtest_typed_test_suite_p_state_##TestSuiteName##_ - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY. -// -// Expands to the name of the variable used to remember the names of -// the registered tests in the given test suite. -#define GTEST_REGISTERED_TEST_NAMES_(TestSuiteName) \ - gtest_registered_test_names_##TestSuiteName##_ - -// The variables defined in the type-parameterized test macros are -// static as typically these macros are used in a .h file that can be -// #included in multiple translation units linked together. -#define TYPED_TEST_SUITE_P(SuiteName) \ - static ::testing::internal::TypedTestSuitePState \ - GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName) - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -#define TYPED_TEST_CASE_P \ - static_assert(::testing::internal::TypedTestCase_P_IsDeprecated(), ""); \ - TYPED_TEST_SUITE_P -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -#define TYPED_TEST_P(SuiteName, TestName) \ - namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ - template \ - class TestName : public SuiteName { \ - private: \ - typedef SuiteName TestFixture; \ - typedef gtest_TypeParam_ TypeParam; \ - void TestBody() override; \ - }; \ - static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \ - GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \ - __FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \ - GTEST_STRINGIFY_(TestName)); \ - } \ - template \ - void GTEST_SUITE_NAMESPACE_( \ - SuiteName)::TestName::TestBody() - -// Note: this won't work correctly if the trailing arguments are macros. -#define REGISTER_TYPED_TEST_SUITE_P(SuiteName, ...) \ - namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ - typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \ - } \ - static const char* const GTEST_REGISTERED_TEST_NAMES_( \ - SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \ - GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \ - GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__) - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -#define REGISTER_TYPED_TEST_CASE_P \ - static_assert(::testing::internal::RegisterTypedTestCase_P_IsDeprecated(), \ - ""); \ - REGISTER_TYPED_TEST_SUITE_P -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \ - static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \ - "test-suit-prefix must not be empty"); \ - static bool gtest_##Prefix##_##SuiteName GTEST_ATTRIBUTE_UNUSED_ = \ - ::testing::internal::TypeParameterizedTestSuite< \ - SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \ - ::testing::internal::GenerateTypeList::type>:: \ - Register(GTEST_STRINGIFY_(Prefix), \ - ::testing::internal::CodeLocation(__FILE__, __LINE__), \ - >EST_TYPED_TEST_SUITE_P_STATE_(SuiteName), \ - GTEST_STRINGIFY_(SuiteName), \ - GTEST_REGISTERED_TEST_NAMES_(SuiteName), \ - ::testing::internal::GenerateNames< \ - ::testing::internal::NameGeneratorSelector< \ - __VA_ARGS__>::type, \ - ::testing::internal::GenerateTypeList::type>()) - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -#define INSTANTIATE_TYPED_TEST_CASE_P \ - static_assert( \ - ::testing::internal::InstantiateTypedTestCase_P_IsDeprecated(), ""); \ - INSTANTIATE_TYPED_TEST_SUITE_P -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ - -GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ -/* class A needs to have dll-interface to be used by clients of class B */) - -namespace testing { - -// Silence C4100 (unreferenced formal parameter) and 4805 -// unsafe mix of type 'const int' and type 'const bool' -#ifdef _MSC_VER -# pragma warning(push) -# pragma warning(disable:4805) -# pragma warning(disable:4100) -#endif - - -// Declares the flags. - -// This flag temporary enables the disabled tests. -GTEST_DECLARE_bool_(also_run_disabled_tests); - -// This flag brings the debugger on an assertion failure. -GTEST_DECLARE_bool_(break_on_failure); - -// This flag controls whether Google Test catches all test-thrown exceptions -// and logs them as failures. -GTEST_DECLARE_bool_(catch_exceptions); - -// This flag enables using colors in terminal output. Available values are -// "yes" to enable colors, "no" (disable colors), or "auto" (the default) -// to let Google Test decide. -GTEST_DECLARE_string_(color); - -// This flag controls whether the test runner should continue execution past -// first failure. -GTEST_DECLARE_bool_(fail_fast); - -// This flag sets up the filter to select by name using a glob pattern -// the tests to run. If the filter is not given all tests are executed. -GTEST_DECLARE_string_(filter); - -// This flag controls whether Google Test installs a signal handler that dumps -// debugging information when fatal signals are raised. -GTEST_DECLARE_bool_(install_failure_signal_handler); - -// This flag causes the Google Test to list tests. None of the tests listed -// are actually run if the flag is provided. -GTEST_DECLARE_bool_(list_tests); - -// This flag controls whether Google Test emits a detailed XML report to a file -// in addition to its normal textual output. -GTEST_DECLARE_string_(output); - -// This flags control whether Google Test prints only test failures. -GTEST_DECLARE_bool_(brief); - -// This flags control whether Google Test prints the elapsed time for each -// test. -GTEST_DECLARE_bool_(print_time); - -// This flags control whether Google Test prints UTF8 characters as text. -GTEST_DECLARE_bool_(print_utf8); - -// This flag specifies the random number seed. -GTEST_DECLARE_int32_(random_seed); - -// This flag sets how many times the tests are repeated. The default value -// is 1. If the value is -1 the tests are repeating forever. -GTEST_DECLARE_int32_(repeat); - -// This flag controls whether Google Test includes Google Test internal -// stack frames in failure stack traces. -GTEST_DECLARE_bool_(show_internal_stack_frames); - -// When this flag is specified, tests' order is randomized on every iteration. -GTEST_DECLARE_bool_(shuffle); - -// This flag specifies the maximum number of stack frames to be -// printed in a failure message. -GTEST_DECLARE_int32_(stack_trace_depth); - -// When this flag is specified, a failed assertion will throw an -// exception if exceptions are enabled, or exit the program with a -// non-zero code otherwise. For use with an external test framework. -GTEST_DECLARE_bool_(throw_on_failure); - -// When this flag is set with a "host:port" string, on supported -// platforms test results are streamed to the specified port on -// the specified host machine. -GTEST_DECLARE_string_(stream_result_to); - -#if GTEST_USE_OWN_FLAGFILE_FLAG_ -GTEST_DECLARE_string_(flagfile); -#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ - -// The upper limit for valid stack trace depths. -const int kMaxStackTraceDepth = 100; - -namespace internal { - -class AssertHelper; -class DefaultGlobalTestPartResultReporter; -class ExecDeathTest; -class NoExecDeathTest; -class FinalSuccessChecker; -class GTestFlagSaver; -class StreamingListenerTest; -class TestResultAccessor; -class TestEventListenersAccessor; -class TestEventRepeater; -class UnitTestRecordPropertyTestHelper; -class WindowsDeathTest; -class FuchsiaDeathTest; -class UnitTestImpl* GetUnitTestImpl(); -void ReportFailureInUnknownLocation(TestPartResult::Type result_type, - const std::string& message); -std::set* GetIgnoredParameterizedTestSuites(); - -} // namespace internal - -// The friend relationship of some of these classes is cyclic. -// If we don't forward declare them the compiler might confuse the classes -// in friendship clauses with same named classes on the scope. -class Test; -class TestSuite; - -// Old API is still available but deprecated -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ -using TestCase = TestSuite; -#endif -class TestInfo; -class UnitTest; - -// A class for indicating whether an assertion was successful. When -// the assertion wasn't successful, the AssertionResult object -// remembers a non-empty message that describes how it failed. -// -// To create an instance of this class, use one of the factory functions -// (AssertionSuccess() and AssertionFailure()). -// -// This class is useful for two purposes: -// 1. Defining predicate functions to be used with Boolean test assertions -// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts -// 2. Defining predicate-format functions to be -// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). -// -// For example, if you define IsEven predicate: -// -// testing::AssertionResult IsEven(int n) { -// if ((n % 2) == 0) -// return testing::AssertionSuccess(); -// else -// return testing::AssertionFailure() << n << " is odd"; -// } -// -// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) -// will print the message -// -// Value of: IsEven(Fib(5)) -// Actual: false (5 is odd) -// Expected: true -// -// instead of a more opaque -// -// Value of: IsEven(Fib(5)) -// Actual: false -// Expected: true -// -// in case IsEven is a simple Boolean predicate. -// -// If you expect your predicate to be reused and want to support informative -// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up -// about half as often as positive ones in our tests), supply messages for -// both success and failure cases: -// -// testing::AssertionResult IsEven(int n) { -// if ((n % 2) == 0) -// return testing::AssertionSuccess() << n << " is even"; -// else -// return testing::AssertionFailure() << n << " is odd"; -// } -// -// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print -// -// Value of: IsEven(Fib(6)) -// Actual: true (8 is even) -// Expected: false -// -// NB: Predicates that support negative Boolean assertions have reduced -// performance in positive ones so be careful not to use them in tests -// that have lots (tens of thousands) of positive Boolean assertions. -// -// To use this class with EXPECT_PRED_FORMAT assertions such as: -// -// // Verifies that Foo() returns an even number. -// EXPECT_PRED_FORMAT1(IsEven, Foo()); -// -// you need to define: -// -// testing::AssertionResult IsEven(const char* expr, int n) { -// if ((n % 2) == 0) -// return testing::AssertionSuccess(); -// else -// return testing::AssertionFailure() -// << "Expected: " << expr << " is even\n Actual: it's " << n; -// } -// -// If Foo() returns 5, you will see the following message: -// -// Expected: Foo() is even -// Actual: it's 5 -// -class GTEST_API_ AssertionResult { - public: - // Copy constructor. - // Used in EXPECT_TRUE/FALSE(assertion_result). - AssertionResult(const AssertionResult& other); - -// C4800 is a level 3 warning in Visual Studio 2015 and earlier. -// This warning is not emitted in Visual Studio 2017. -// This warning is off by default starting in Visual Studio 2019 but can be -// enabled with command-line options. -#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) - GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) -#endif - - // Used in the EXPECT_TRUE/FALSE(bool_expression). - // - // T must be contextually convertible to bool. - // - // The second parameter prevents this overload from being considered if - // the argument is implicitly convertible to AssertionResult. In that case - // we want AssertionResult's copy constructor to be used. - template - explicit AssertionResult( - const T& success, - typename std::enable_if< - !std::is_convertible::value>::type* - /*enabler*/ - = nullptr) - : success_(success) {} - -#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) - GTEST_DISABLE_MSC_WARNINGS_POP_() -#endif - - // Assignment operator. - AssertionResult& operator=(AssertionResult other) { - swap(other); - return *this; - } - - // Returns true if and only if the assertion succeeded. - operator bool() const { return success_; } // NOLINT - - // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. - AssertionResult operator!() const; - - // Returns the text streamed into this AssertionResult. Test assertions - // use it when they fail (i.e., the predicate's outcome doesn't match the - // assertion's expectation). When nothing has been streamed into the - // object, returns an empty string. - const char* message() const { - return message_.get() != nullptr ? message_->c_str() : ""; - } - // Deprecated; please use message() instead. - const char* failure_message() const { return message(); } - - // Streams a custom failure message into this object. - template AssertionResult& operator<<(const T& value) { - AppendMessage(Message() << value); - return *this; - } - - // Allows streaming basic output manipulators such as endl or flush into - // this object. - AssertionResult& operator<<( - ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { - AppendMessage(Message() << basic_manipulator); - return *this; - } - - private: - // Appends the contents of message to message_. - void AppendMessage(const Message& a_message) { - if (message_.get() == nullptr) message_.reset(new ::std::string); - message_->append(a_message.GetString().c_str()); - } - - // Swap the contents of this AssertionResult with other. - void swap(AssertionResult& other); - - // Stores result of the assertion predicate. - bool success_; - // Stores the message describing the condition in case the expectation - // construct is not satisfied with the predicate's outcome. - // Referenced via a pointer to avoid taking too much stack frame space - // with test assertions. - std::unique_ptr< ::std::string> message_; -}; - -// Makes a successful assertion result. -GTEST_API_ AssertionResult AssertionSuccess(); - -// Makes a failed assertion result. -GTEST_API_ AssertionResult AssertionFailure(); - -// Makes a failed assertion result with the given failure message. -// Deprecated; use AssertionFailure() << msg. -GTEST_API_ AssertionResult AssertionFailure(const Message& msg); - -} // namespace testing - -// Includes the auto-generated header that implements a family of generic -// predicate assertion macros. This include comes late because it relies on -// APIs declared above. -// Copyright 2006, Google Inc. -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// This file is AUTOMATICALLY GENERATED on 01/02/2019 by command -// 'gen_gtest_pred_impl.py 5'. DO NOT EDIT BY HAND! -// -// Implements a family of generic predicate assertion macros. -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ -#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ - - -namespace testing { - -// This header implements a family of generic predicate assertion -// macros: -// -// ASSERT_PRED_FORMAT1(pred_format, v1) -// ASSERT_PRED_FORMAT2(pred_format, v1, v2) -// ... -// -// where pred_format is a function or functor that takes n (in the -// case of ASSERT_PRED_FORMATn) values and their source expression -// text, and returns a testing::AssertionResult. See the definition -// of ASSERT_EQ in gtest.h for an example. -// -// If you don't care about formatting, you can use the more -// restrictive version: -// -// ASSERT_PRED1(pred, v1) -// ASSERT_PRED2(pred, v1, v2) -// ... -// -// where pred is an n-ary function or functor that returns bool, -// and the values v1, v2, ..., must support the << operator for -// streaming to std::ostream. -// -// We also define the EXPECT_* variations. -// -// For now we only support predicates whose arity is at most 5. -// Please email googletestframework@googlegroups.com if you need -// support for higher arities. - -// GTEST_ASSERT_ is the basic statement to which all of the assertions -// in this file reduce. Don't use this in your code. - -#define GTEST_ASSERT_(expression, on_failure) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (const ::testing::AssertionResult gtest_ar = (expression)) \ - ; \ - else \ - on_failure(gtest_ar.failure_message()) - - -// Helper function for implementing {EXPECT|ASSERT}_PRED1. Don't use -// this in your code. -template -AssertionResult AssertPred1Helper(const char* pred_text, - const char* e1, - Pred pred, - const T1& v1) { - if (pred(v1)) return AssertionSuccess(); - - return AssertionFailure() - << pred_text << "(" << e1 << ") evaluates to false, where" - << "\n" - << e1 << " evaluates to " << ::testing::PrintToString(v1); -} - -// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT1. -// Don't use this in your code. -#define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure)\ - GTEST_ASSERT_(pred_format(#v1, v1), \ - on_failure) - -// Internal macro for implementing {EXPECT|ASSERT}_PRED1. Don't use -// this in your code. -#define GTEST_PRED1_(pred, v1, on_failure)\ - GTEST_ASSERT_(::testing::AssertPred1Helper(#pred, \ - #v1, \ - pred, \ - v1), on_failure) - -// Unary predicate assertion macros. -#define EXPECT_PRED_FORMAT1(pred_format, v1) \ - GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_) -#define EXPECT_PRED1(pred, v1) \ - GTEST_PRED1_(pred, v1, GTEST_NONFATAL_FAILURE_) -#define ASSERT_PRED_FORMAT1(pred_format, v1) \ - GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_) -#define ASSERT_PRED1(pred, v1) \ - GTEST_PRED1_(pred, v1, GTEST_FATAL_FAILURE_) - - - -// Helper function for implementing {EXPECT|ASSERT}_PRED2. Don't use -// this in your code. -template -AssertionResult AssertPred2Helper(const char* pred_text, - const char* e1, - const char* e2, - Pred pred, - const T1& v1, - const T2& v2) { - if (pred(v1, v2)) return AssertionSuccess(); - - return AssertionFailure() - << pred_text << "(" << e1 << ", " << e2 - << ") evaluates to false, where" - << "\n" - << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" - << e2 << " evaluates to " << ::testing::PrintToString(v2); -} - -// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2. -// Don't use this in your code. -#define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure)\ - GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), \ - on_failure) - -// Internal macro for implementing {EXPECT|ASSERT}_PRED2. Don't use -// this in your code. -#define GTEST_PRED2_(pred, v1, v2, on_failure)\ - GTEST_ASSERT_(::testing::AssertPred2Helper(#pred, \ - #v1, \ - #v2, \ - pred, \ - v1, \ - v2), on_failure) - -// Binary predicate assertion macros. -#define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \ - GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_) -#define EXPECT_PRED2(pred, v1, v2) \ - GTEST_PRED2_(pred, v1, v2, GTEST_NONFATAL_FAILURE_) -#define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \ - GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_) -#define ASSERT_PRED2(pred, v1, v2) \ - GTEST_PRED2_(pred, v1, v2, GTEST_FATAL_FAILURE_) - - - -// Helper function for implementing {EXPECT|ASSERT}_PRED3. Don't use -// this in your code. -template -AssertionResult AssertPred3Helper(const char* pred_text, - const char* e1, - const char* e2, - const char* e3, - Pred pred, - const T1& v1, - const T2& v2, - const T3& v3) { - if (pred(v1, v2, v3)) return AssertionSuccess(); - - return AssertionFailure() - << pred_text << "(" << e1 << ", " << e2 << ", " << e3 - << ") evaluates to false, where" - << "\n" - << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" - << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n" - << e3 << " evaluates to " << ::testing::PrintToString(v3); -} - -// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT3. -// Don't use this in your code. -#define GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, on_failure)\ - GTEST_ASSERT_(pred_format(#v1, #v2, #v3, v1, v2, v3), \ - on_failure) - -// Internal macro for implementing {EXPECT|ASSERT}_PRED3. Don't use -// this in your code. -#define GTEST_PRED3_(pred, v1, v2, v3, on_failure)\ - GTEST_ASSERT_(::testing::AssertPred3Helper(#pred, \ - #v1, \ - #v2, \ - #v3, \ - pred, \ - v1, \ - v2, \ - v3), on_failure) - -// Ternary predicate assertion macros. -#define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \ - GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_NONFATAL_FAILURE_) -#define EXPECT_PRED3(pred, v1, v2, v3) \ - GTEST_PRED3_(pred, v1, v2, v3, GTEST_NONFATAL_FAILURE_) -#define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \ - GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_FATAL_FAILURE_) -#define ASSERT_PRED3(pred, v1, v2, v3) \ - GTEST_PRED3_(pred, v1, v2, v3, GTEST_FATAL_FAILURE_) - - - -// Helper function for implementing {EXPECT|ASSERT}_PRED4. Don't use -// this in your code. -template -AssertionResult AssertPred4Helper(const char* pred_text, - const char* e1, - const char* e2, - const char* e3, - const char* e4, - Pred pred, - const T1& v1, - const T2& v2, - const T3& v3, - const T4& v4) { - if (pred(v1, v2, v3, v4)) return AssertionSuccess(); - - return AssertionFailure() - << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4 - << ") evaluates to false, where" - << "\n" - << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" - << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n" - << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n" - << e4 << " evaluates to " << ::testing::PrintToString(v4); -} - -// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT4. -// Don't use this in your code. -#define GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, on_failure)\ - GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4), \ - on_failure) - -// Internal macro for implementing {EXPECT|ASSERT}_PRED4. Don't use -// this in your code. -#define GTEST_PRED4_(pred, v1, v2, v3, v4, on_failure)\ - GTEST_ASSERT_(::testing::AssertPred4Helper(#pred, \ - #v1, \ - #v2, \ - #v3, \ - #v4, \ - pred, \ - v1, \ - v2, \ - v3, \ - v4), on_failure) - -// 4-ary predicate assertion macros. -#define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \ - GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_) -#define EXPECT_PRED4(pred, v1, v2, v3, v4) \ - GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_) -#define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \ - GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_FATAL_FAILURE_) -#define ASSERT_PRED4(pred, v1, v2, v3, v4) \ - GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_FATAL_FAILURE_) - - - -// Helper function for implementing {EXPECT|ASSERT}_PRED5. Don't use -// this in your code. -template -AssertionResult AssertPred5Helper(const char* pred_text, - const char* e1, - const char* e2, - const char* e3, - const char* e4, - const char* e5, - Pred pred, - const T1& v1, - const T2& v2, - const T3& v3, - const T4& v4, - const T5& v5) { - if (pred(v1, v2, v3, v4, v5)) return AssertionSuccess(); - - return AssertionFailure() - << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4 - << ", " << e5 << ") evaluates to false, where" - << "\n" - << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" - << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n" - << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n" - << e4 << " evaluates to " << ::testing::PrintToString(v4) << "\n" - << e5 << " evaluates to " << ::testing::PrintToString(v5); -} - -// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT5. -// Don't use this in your code. -#define GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, on_failure)\ - GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5), \ - on_failure) - -// Internal macro for implementing {EXPECT|ASSERT}_PRED5. Don't use -// this in your code. -#define GTEST_PRED5_(pred, v1, v2, v3, v4, v5, on_failure)\ - GTEST_ASSERT_(::testing::AssertPred5Helper(#pred, \ - #v1, \ - #v2, \ - #v3, \ - #v4, \ - #v5, \ - pred, \ - v1, \ - v2, \ - v3, \ - v4, \ - v5), on_failure) - -// 5-ary predicate assertion macros. -#define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \ - GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_) -#define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \ - GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_) -#define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \ - GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_) -#define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \ - GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_) - - - -} // namespace testing - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ - -namespace testing { - -// The abstract class that all tests inherit from. -// -// In Google Test, a unit test program contains one or many TestSuites, and -// each TestSuite contains one or many Tests. -// -// When you define a test using the TEST macro, you don't need to -// explicitly derive from Test - the TEST macro automatically does -// this for you. -// -// The only time you derive from Test is when defining a test fixture -// to be used in a TEST_F. For example: -// -// class FooTest : public testing::Test { -// protected: -// void SetUp() override { ... } -// void TearDown() override { ... } -// ... -// }; -// -// TEST_F(FooTest, Bar) { ... } -// TEST_F(FooTest, Baz) { ... } -// -// Test is not copyable. -class GTEST_API_ Test { - public: - friend class TestInfo; - - // The d'tor is virtual as we intend to inherit from Test. - virtual ~Test(); - - // Sets up the stuff shared by all tests in this test suite. - // - // Google Test will call Foo::SetUpTestSuite() before running the first - // test in test suite Foo. Hence a sub-class can define its own - // SetUpTestSuite() method to shadow the one defined in the super - // class. - static void SetUpTestSuite() {} - - // Tears down the stuff shared by all tests in this test suite. - // - // Google Test will call Foo::TearDownTestSuite() after running the last - // test in test suite Foo. Hence a sub-class can define its own - // TearDownTestSuite() method to shadow the one defined in the super - // class. - static void TearDownTestSuite() {} - - // Legacy API is deprecated but still available. Use SetUpTestSuite and - // TearDownTestSuite instead. -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - static void TearDownTestCase() {} - static void SetUpTestCase() {} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Returns true if and only if the current test has a fatal failure. - static bool HasFatalFailure(); - - // Returns true if and only if the current test has a non-fatal failure. - static bool HasNonfatalFailure(); - - // Returns true if and only if the current test was skipped. - static bool IsSkipped(); - - // Returns true if and only if the current test has a (either fatal or - // non-fatal) failure. - static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } - - // Logs a property for the current test, test suite, or for the entire - // invocation of the test program when used outside of the context of a - // test suite. Only the last value for a given key is remembered. These - // are public static so they can be called from utility functions that are - // not members of the test fixture. Calls to RecordProperty made during - // lifespan of the test (from the moment its constructor starts to the - // moment its destructor finishes) will be output in XML as attributes of - // the element. Properties recorded from fixture's - // SetUpTestSuite or TearDownTestSuite are logged as attributes of the - // corresponding element. Calls to RecordProperty made in the - // global context (before or after invocation of RUN_ALL_TESTS and from - // SetUp/TearDown method of Environment objects registered with Google - // Test) will be output as attributes of the element. - static void RecordProperty(const std::string& key, const std::string& value); - static void RecordProperty(const std::string& key, int value); - - protected: - // Creates a Test object. - Test(); - - // Sets up the test fixture. - virtual void SetUp(); - - // Tears down the test fixture. - virtual void TearDown(); - - private: - // Returns true if and only if the current test has the same fixture class - // as the first test in the current test suite. - static bool HasSameFixtureClass(); - - // Runs the test after the test fixture has been set up. - // - // A sub-class must implement this to define the test logic. - // - // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. - // Instead, use the TEST or TEST_F macro. - virtual void TestBody() = 0; - - // Sets up, executes, and tears down the test. - void Run(); - - // Deletes self. We deliberately pick an unusual name for this - // internal method to avoid clashing with names used in user TESTs. - void DeleteSelf_() { delete this; } - - const std::unique_ptr gtest_flag_saver_; - - // Often a user misspells SetUp() as Setup() and spends a long time - // wondering why it is never called by Google Test. The declaration of - // the following method is solely for catching such an error at - // compile time: - // - // - The return type is deliberately chosen to be not void, so it - // will be a conflict if void Setup() is declared in the user's - // test fixture. - // - // - This method is private, so it will be another compiler error - // if the method is called from the user's test fixture. - // - // DO NOT OVERRIDE THIS FUNCTION. - // - // If you see an error about overriding the following function or - // about it being private, you have mis-spelled SetUp() as Setup(). - struct Setup_should_be_spelled_SetUp {}; - virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } - - // We disallow copying Tests. - GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); -}; - -typedef internal::TimeInMillis TimeInMillis; - -// A copyable object representing a user specified test property which can be -// output as a key/value string pair. -// -// Don't inherit from TestProperty as its destructor is not virtual. -class TestProperty { - public: - // C'tor. TestProperty does NOT have a default constructor. - // Always use this constructor (with parameters) to create a - // TestProperty object. - TestProperty(const std::string& a_key, const std::string& a_value) : - key_(a_key), value_(a_value) { - } - - // Gets the user supplied key. - const char* key() const { - return key_.c_str(); - } - - // Gets the user supplied value. - const char* value() const { - return value_.c_str(); - } - - // Sets a new value, overriding the one supplied in the constructor. - void SetValue(const std::string& new_value) { - value_ = new_value; - } - - private: - // The key supplied by the user. - std::string key_; - // The value supplied by the user. - std::string value_; -}; - -// The result of a single Test. This includes a list of -// TestPartResults, a list of TestProperties, a count of how many -// death tests there are in the Test, and how much time it took to run -// the Test. -// -// TestResult is not copyable. -class GTEST_API_ TestResult { - public: - // Creates an empty TestResult. - TestResult(); - - // D'tor. Do not inherit from TestResult. - ~TestResult(); - - // Gets the number of all test parts. This is the sum of the number - // of successful test parts and the number of failed test parts. - int total_part_count() const; - - // Returns the number of the test properties. - int test_property_count() const; - - // Returns true if and only if the test passed (i.e. no test part failed). - bool Passed() const { return !Skipped() && !Failed(); } - - // Returns true if and only if the test was skipped. - bool Skipped() const; - - // Returns true if and only if the test failed. - bool Failed() const; - - // Returns true if and only if the test fatally failed. - bool HasFatalFailure() const; - - // Returns true if and only if the test has a non-fatal failure. - bool HasNonfatalFailure() const; - - // Returns the elapsed time, in milliseconds. - TimeInMillis elapsed_time() const { return elapsed_time_; } - - // Gets the time of the test case start, in ms from the start of the - // UNIX epoch. - TimeInMillis start_timestamp() const { return start_timestamp_; } - - // Returns the i-th test part result among all the results. i can range from 0 - // to total_part_count() - 1. If i is not in that range, aborts the program. - const TestPartResult& GetTestPartResult(int i) const; - - // Returns the i-th test property. i can range from 0 to - // test_property_count() - 1. If i is not in that range, aborts the - // program. - const TestProperty& GetTestProperty(int i) const; - - private: - friend class TestInfo; - friend class TestSuite; - friend class UnitTest; - friend class internal::DefaultGlobalTestPartResultReporter; - friend class internal::ExecDeathTest; - friend class internal::TestResultAccessor; - friend class internal::UnitTestImpl; - friend class internal::WindowsDeathTest; - friend class internal::FuchsiaDeathTest; - - // Gets the vector of TestPartResults. - const std::vector& test_part_results() const { - return test_part_results_; - } - - // Gets the vector of TestProperties. - const std::vector& test_properties() const { - return test_properties_; - } - - // Sets the start time. - void set_start_timestamp(TimeInMillis start) { start_timestamp_ = start; } - - // Sets the elapsed time. - void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } - - // Adds a test property to the list. The property is validated and may add - // a non-fatal failure if invalid (e.g., if it conflicts with reserved - // key names). If a property is already recorded for the same key, the - // value will be updated, rather than storing multiple values for the same - // key. xml_element specifies the element for which the property is being - // recorded and is used for validation. - void RecordProperty(const std::string& xml_element, - const TestProperty& test_property); - - // Adds a failure if the key is a reserved attribute of Google Test - // testsuite tags. Returns true if the property is valid. - // FIXME: Validate attribute names are legal and human readable. - static bool ValidateTestProperty(const std::string& xml_element, - const TestProperty& test_property); - - // Adds a test part result to the list. - void AddTestPartResult(const TestPartResult& test_part_result); - - // Returns the death test count. - int death_test_count() const { return death_test_count_; } - - // Increments the death test count, returning the new count. - int increment_death_test_count() { return ++death_test_count_; } - - // Clears the test part results. - void ClearTestPartResults(); - - // Clears the object. - void Clear(); - - // Protects mutable state of the property vector and of owned - // properties, whose values may be updated. - internal::Mutex test_properties_mutex_; - - // The vector of TestPartResults - std::vector test_part_results_; - // The vector of TestProperties - std::vector test_properties_; - // Running count of death tests. - int death_test_count_; - // The start time, in milliseconds since UNIX Epoch. - TimeInMillis start_timestamp_; - // The elapsed time, in milliseconds. - TimeInMillis elapsed_time_; - - // We disallow copying TestResult. - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); -}; // class TestResult - -// A TestInfo object stores the following information about a test: -// -// Test suite name -// Test name -// Whether the test should be run -// A function pointer that creates the test object when invoked -// Test result -// -// The constructor of TestInfo registers itself with the UnitTest -// singleton such that the RUN_ALL_TESTS() macro knows which tests to -// run. -class GTEST_API_ TestInfo { - public: - // Destructs a TestInfo object. This function is not virtual, so - // don't inherit from TestInfo. - ~TestInfo(); - - // Returns the test suite name. - const char* test_suite_name() const { return test_suite_name_.c_str(); } - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - const char* test_case_name() const { return test_suite_name(); } -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Returns the test name. - const char* name() const { return name_.c_str(); } - - // Returns the name of the parameter type, or NULL if this is not a typed - // or a type-parameterized test. - const char* type_param() const { - if (type_param_.get() != nullptr) return type_param_->c_str(); - return nullptr; - } - - // Returns the text representation of the value parameter, or NULL if this - // is not a value-parameterized test. - const char* value_param() const { - if (value_param_.get() != nullptr) return value_param_->c_str(); - return nullptr; - } - - // Returns the file name where this test is defined. - const char* file() const { return location_.file.c_str(); } - - // Returns the line where this test is defined. - int line() const { return location_.line; } - - // Return true if this test should not be run because it's in another shard. - bool is_in_another_shard() const { return is_in_another_shard_; } - - // Returns true if this test should run, that is if the test is not - // disabled (or it is disabled but the also_run_disabled_tests flag has - // been specified) and its full name matches the user-specified filter. - // - // Google Test allows the user to filter the tests by their full names. - // The full name of a test Bar in test suite Foo is defined as - // "Foo.Bar". Only the tests that match the filter will run. - // - // A filter is a colon-separated list of glob (not regex) patterns, - // optionally followed by a '-' and a colon-separated list of - // negative patterns (tests to exclude). A test is run if it - // matches one of the positive patterns and does not match any of - // the negative patterns. - // - // For example, *A*:Foo.* is a filter that matches any string that - // contains the character 'A' or starts with "Foo.". - bool should_run() const { return should_run_; } - - // Returns true if and only if this test will appear in the XML report. - bool is_reportable() const { - // The XML report includes tests matching the filter, excluding those - // run in other shards. - return matches_filter_ && !is_in_another_shard_; - } - - // Returns the result of the test. - const TestResult* result() const { return &result_; } - - private: -#if GTEST_HAS_DEATH_TEST - friend class internal::DefaultDeathTestFactory; -#endif // GTEST_HAS_DEATH_TEST - friend class Test; - friend class TestSuite; - friend class internal::UnitTestImpl; - friend class internal::StreamingListenerTest; - friend TestInfo* internal::MakeAndRegisterTestInfo( - const char* test_suite_name, const char* name, const char* type_param, - const char* value_param, internal::CodeLocation code_location, - internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc, - internal::TestFactoryBase* factory); - - // Constructs a TestInfo object. The newly constructed instance assumes - // ownership of the factory object. - TestInfo(const std::string& test_suite_name, const std::string& name, - const char* a_type_param, // NULL if not a type-parameterized test - const char* a_value_param, // NULL if not a value-parameterized test - internal::CodeLocation a_code_location, - internal::TypeId fixture_class_id, - internal::TestFactoryBase* factory); - - // Increments the number of death tests encountered in this test so - // far. - int increment_death_test_count() { - return result_.increment_death_test_count(); - } - - // Creates the test object, runs it, records its result, and then - // deletes it. - void Run(); - - // Skip and records the test result for this object. - void Skip(); - - static void ClearTestResult(TestInfo* test_info) { - test_info->result_.Clear(); - } - - // These fields are immutable properties of the test. - const std::string test_suite_name_; // test suite name - const std::string name_; // Test name - // Name of the parameter type, or NULL if this is not a typed or a - // type-parameterized test. - const std::unique_ptr type_param_; - // Text representation of the value parameter, or NULL if this is not a - // value-parameterized test. - const std::unique_ptr value_param_; - internal::CodeLocation location_; - const internal::TypeId fixture_class_id_; // ID of the test fixture class - bool should_run_; // True if and only if this test should run - bool is_disabled_; // True if and only if this test is disabled - bool matches_filter_; // True if this test matches the - // user-specified filter. - bool is_in_another_shard_; // Will be run in another shard. - internal::TestFactoryBase* const factory_; // The factory that creates - // the test object - - // This field is mutable and needs to be reset before running the - // test for the second time. - TestResult result_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); -}; - -// A test suite, which consists of a vector of TestInfos. -// -// TestSuite is not copyable. -class GTEST_API_ TestSuite { - public: - // Creates a TestSuite with the given name. - // - // TestSuite does NOT have a default constructor. Always use this - // constructor to create a TestSuite object. - // - // Arguments: - // - // name: name of the test suite - // a_type_param: the name of the test's type parameter, or NULL if - // this is not a type-parameterized test. - // set_up_tc: pointer to the function that sets up the test suite - // tear_down_tc: pointer to the function that tears down the test suite - TestSuite(const char* name, const char* a_type_param, - internal::SetUpTestSuiteFunc set_up_tc, - internal::TearDownTestSuiteFunc tear_down_tc); - - // Destructor of TestSuite. - virtual ~TestSuite(); - - // Gets the name of the TestSuite. - const char* name() const { return name_.c_str(); } - - // Returns the name of the parameter type, or NULL if this is not a - // type-parameterized test suite. - const char* type_param() const { - if (type_param_.get() != nullptr) return type_param_->c_str(); - return nullptr; - } - - // Returns true if any test in this test suite should run. - bool should_run() const { return should_run_; } - - // Gets the number of successful tests in this test suite. - int successful_test_count() const; - - // Gets the number of skipped tests in this test suite. - int skipped_test_count() const; - - // Gets the number of failed tests in this test suite. - int failed_test_count() const; - - // Gets the number of disabled tests that will be reported in the XML report. - int reportable_disabled_test_count() const; - - // Gets the number of disabled tests in this test suite. - int disabled_test_count() const; - - // Gets the number of tests to be printed in the XML report. - int reportable_test_count() const; - - // Get the number of tests in this test suite that should run. - int test_to_run_count() const; - - // Gets the number of all tests in this test suite. - int total_test_count() const; - - // Returns true if and only if the test suite passed. - bool Passed() const { return !Failed(); } - - // Returns true if and only if the test suite failed. - bool Failed() const { - return failed_test_count() > 0 || ad_hoc_test_result().Failed(); - } - - // Returns the elapsed time, in milliseconds. - TimeInMillis elapsed_time() const { return elapsed_time_; } - - // Gets the time of the test suite start, in ms from the start of the - // UNIX epoch. - TimeInMillis start_timestamp() const { return start_timestamp_; } - - // Returns the i-th test among all the tests. i can range from 0 to - // total_test_count() - 1. If i is not in that range, returns NULL. - const TestInfo* GetTestInfo(int i) const; - - // Returns the TestResult that holds test properties recorded during - // execution of SetUpTestSuite and TearDownTestSuite. - const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } - - private: - friend class Test; - friend class internal::UnitTestImpl; - - // Gets the (mutable) vector of TestInfos in this TestSuite. - std::vector& test_info_list() { return test_info_list_; } - - // Gets the (immutable) vector of TestInfos in this TestSuite. - const std::vector& test_info_list() const { - return test_info_list_; - } - - // Returns the i-th test among all the tests. i can range from 0 to - // total_test_count() - 1. If i is not in that range, returns NULL. - TestInfo* GetMutableTestInfo(int i); - - // Sets the should_run member. - void set_should_run(bool should) { should_run_ = should; } - - // Adds a TestInfo to this test suite. Will delete the TestInfo upon - // destruction of the TestSuite object. - void AddTestInfo(TestInfo * test_info); - - // Clears the results of all tests in this test suite. - void ClearResult(); - - // Clears the results of all tests in the given test suite. - static void ClearTestSuiteResult(TestSuite* test_suite) { - test_suite->ClearResult(); - } - - // Runs every test in this TestSuite. - void Run(); - - // Skips the execution of tests under this TestSuite - void Skip(); - - // Runs SetUpTestSuite() for this TestSuite. This wrapper is needed - // for catching exceptions thrown from SetUpTestSuite(). - void RunSetUpTestSuite() { - if (set_up_tc_ != nullptr) { - (*set_up_tc_)(); - } - } - - // Runs TearDownTestSuite() for this TestSuite. This wrapper is - // needed for catching exceptions thrown from TearDownTestSuite(). - void RunTearDownTestSuite() { - if (tear_down_tc_ != nullptr) { - (*tear_down_tc_)(); - } - } - - // Returns true if and only if test passed. - static bool TestPassed(const TestInfo* test_info) { - return test_info->should_run() && test_info->result()->Passed(); - } - - // Returns true if and only if test skipped. - static bool TestSkipped(const TestInfo* test_info) { - return test_info->should_run() && test_info->result()->Skipped(); - } - - // Returns true if and only if test failed. - static bool TestFailed(const TestInfo* test_info) { - return test_info->should_run() && test_info->result()->Failed(); - } - - // Returns true if and only if the test is disabled and will be reported in - // the XML report. - static bool TestReportableDisabled(const TestInfo* test_info) { - return test_info->is_reportable() && test_info->is_disabled_; - } - - // Returns true if and only if test is disabled. - static bool TestDisabled(const TestInfo* test_info) { - return test_info->is_disabled_; - } - - // Returns true if and only if this test will appear in the XML report. - static bool TestReportable(const TestInfo* test_info) { - return test_info->is_reportable(); - } - - // Returns true if the given test should run. - static bool ShouldRunTest(const TestInfo* test_info) { - return test_info->should_run(); - } - - // Shuffles the tests in this test suite. - void ShuffleTests(internal::Random* random); - - // Restores the test order to before the first shuffle. - void UnshuffleTests(); - - // Name of the test suite. - std::string name_; - // Name of the parameter type, or NULL if this is not a typed or a - // type-parameterized test. - const std::unique_ptr type_param_; - // The vector of TestInfos in their original order. It owns the - // elements in the vector. - std::vector test_info_list_; - // Provides a level of indirection for the test list to allow easy - // shuffling and restoring the test order. The i-th element in this - // vector is the index of the i-th test in the shuffled test list. - std::vector test_indices_; - // Pointer to the function that sets up the test suite. - internal::SetUpTestSuiteFunc set_up_tc_; - // Pointer to the function that tears down the test suite. - internal::TearDownTestSuiteFunc tear_down_tc_; - // True if and only if any test in this test suite should run. - bool should_run_; - // The start time, in milliseconds since UNIX Epoch. - TimeInMillis start_timestamp_; - // Elapsed time, in milliseconds. - TimeInMillis elapsed_time_; - // Holds test properties recorded during execution of SetUpTestSuite and - // TearDownTestSuite. - TestResult ad_hoc_test_result_; - - // We disallow copying TestSuites. - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestSuite); -}; - -// An Environment object is capable of setting up and tearing down an -// environment. You should subclass this to define your own -// environment(s). -// -// An Environment object does the set-up and tear-down in virtual -// methods SetUp() and TearDown() instead of the constructor and the -// destructor, as: -// -// 1. You cannot safely throw from a destructor. This is a problem -// as in some cases Google Test is used where exceptions are enabled, and -// we may want to implement ASSERT_* using exceptions where they are -// available. -// 2. You cannot use ASSERT_* directly in a constructor or -// destructor. -class Environment { - public: - // The d'tor is virtual as we need to subclass Environment. - virtual ~Environment() {} - - // Override this to define how to set up the environment. - virtual void SetUp() {} - - // Override this to define how to tear down the environment. - virtual void TearDown() {} - private: - // If you see an error about overriding the following function or - // about it being private, you have mis-spelled SetUp() as Setup(). - struct Setup_should_be_spelled_SetUp {}; - virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } -}; - -#if GTEST_HAS_EXCEPTIONS - -// Exception which can be thrown from TestEventListener::OnTestPartResult. -class GTEST_API_ AssertionException - : public internal::GoogleTestFailureException { - public: - explicit AssertionException(const TestPartResult& result) - : GoogleTestFailureException(result) {} -}; - -#endif // GTEST_HAS_EXCEPTIONS - -// The interface for tracing execution of tests. The methods are organized in -// the order the corresponding events are fired. -class TestEventListener { - public: - virtual ~TestEventListener() {} - - // Fired before any test activity starts. - virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; - - // Fired before each iteration of tests starts. There may be more than - // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration - // index, starting from 0. - virtual void OnTestIterationStart(const UnitTest& unit_test, - int iteration) = 0; - - // Fired before environment set-up for each iteration of tests starts. - virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; - - // Fired after environment set-up for each iteration of tests ends. - virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; - - // Fired before the test suite starts. - virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {} - - // Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Fired before the test starts. - virtual void OnTestStart(const TestInfo& test_info) = 0; - - // Fired after a failed assertion or a SUCCEED() invocation. - // If you want to throw an exception from this function to skip to the next - // TEST, it must be AssertionException defined above, or inherited from it. - virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; - - // Fired after the test ends. - virtual void OnTestEnd(const TestInfo& test_info) = 0; - - // Fired after the test suite ends. - virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {} - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Fired before environment tear-down for each iteration of tests starts. - virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; - - // Fired after environment tear-down for each iteration of tests ends. - virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; - - // Fired after each iteration of tests finishes. - virtual void OnTestIterationEnd(const UnitTest& unit_test, - int iteration) = 0; - - // Fired after all test activities have ended. - virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; -}; - -// The convenience class for users who need to override just one or two -// methods and are not concerned that a possible change to a signature of -// the methods they override will not be caught during the build. For -// comments about each method please see the definition of TestEventListener -// above. -class EmptyTestEventListener : public TestEventListener { - public: - void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} - void OnTestIterationStart(const UnitTest& /*unit_test*/, - int /*iteration*/) override {} - void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} - void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} - void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseStart(const TestCase& /*test_case*/) override {} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - void OnTestStart(const TestInfo& /*test_info*/) override {} - void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {} - void OnTestEnd(const TestInfo& /*test_info*/) override {} - void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - void OnTestCaseEnd(const TestCase& /*test_case*/) override {} -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} - void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} - void OnTestIterationEnd(const UnitTest& /*unit_test*/, - int /*iteration*/) override {} - void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} -}; - -// TestEventListeners lets users add listeners to track events in Google Test. -class GTEST_API_ TestEventListeners { - public: - TestEventListeners(); - ~TestEventListeners(); - - // Appends an event listener to the end of the list. Google Test assumes - // the ownership of the listener (i.e. it will delete the listener when - // the test program finishes). - void Append(TestEventListener* listener); - - // Removes the given event listener from the list and returns it. It then - // becomes the caller's responsibility to delete the listener. Returns - // NULL if the listener is not found in the list. - TestEventListener* Release(TestEventListener* listener); - - // Returns the standard listener responsible for the default console - // output. Can be removed from the listeners list to shut down default - // console output. Note that removing this object from the listener list - // with Release transfers its ownership to the caller and makes this - // function return NULL the next time. - TestEventListener* default_result_printer() const { - return default_result_printer_; - } - - // Returns the standard listener responsible for the default XML output - // controlled by the --gtest_output=xml flag. Can be removed from the - // listeners list by users who want to shut down the default XML output - // controlled by this flag and substitute it with custom one. Note that - // removing this object from the listener list with Release transfers its - // ownership to the caller and makes this function return NULL the next - // time. - TestEventListener* default_xml_generator() const { - return default_xml_generator_; - } - - private: - friend class TestSuite; - friend class TestInfo; - friend class internal::DefaultGlobalTestPartResultReporter; - friend class internal::NoExecDeathTest; - friend class internal::TestEventListenersAccessor; - friend class internal::UnitTestImpl; - - // Returns repeater that broadcasts the TestEventListener events to all - // subscribers. - TestEventListener* repeater(); - - // Sets the default_result_printer attribute to the provided listener. - // The listener is also added to the listener list and previous - // default_result_printer is removed from it and deleted. The listener can - // also be NULL in which case it will not be added to the list. Does - // nothing if the previous and the current listener objects are the same. - void SetDefaultResultPrinter(TestEventListener* listener); - - // Sets the default_xml_generator attribute to the provided listener. The - // listener is also added to the listener list and previous - // default_xml_generator is removed from it and deleted. The listener can - // also be NULL in which case it will not be added to the list. Does - // nothing if the previous and the current listener objects are the same. - void SetDefaultXmlGenerator(TestEventListener* listener); - - // Controls whether events will be forwarded by the repeater to the - // listeners in the list. - bool EventForwardingEnabled() const; - void SuppressEventForwarding(); - - // The actual list of listeners. - internal::TestEventRepeater* repeater_; - // Listener responsible for the standard result output. - TestEventListener* default_result_printer_; - // Listener responsible for the creation of the XML output file. - TestEventListener* default_xml_generator_; - - // We disallow copying TestEventListeners. - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners); -}; - -// A UnitTest consists of a vector of TestSuites. -// -// This is a singleton class. The only instance of UnitTest is -// created when UnitTest::GetInstance() is first called. This -// instance is never deleted. -// -// UnitTest is not copyable. -// -// This class is thread-safe as long as the methods are called -// according to their specification. -class GTEST_API_ UnitTest { - public: - // Gets the singleton UnitTest object. The first time this method - // is called, a UnitTest object is constructed and returned. - // Consecutive calls will return the same object. - static UnitTest* GetInstance(); - - // Runs all tests in this UnitTest object and prints the result. - // Returns 0 if successful, or 1 otherwise. - // - // This method can only be called from the main thread. - // - // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - int Run() GTEST_MUST_USE_RESULT_; - - // Returns the working directory when the first TEST() or TEST_F() - // was executed. The UnitTest object owns the string. - const char* original_working_dir() const; - - // Returns the TestSuite object for the test that's currently running, - // or NULL if no test is running. - const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_); - -// Legacy API is still available but deprecated -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_); -#endif - - // Returns the TestInfo object for the test that's currently running, - // or NULL if no test is running. - const TestInfo* current_test_info() const - GTEST_LOCK_EXCLUDED_(mutex_); - - // Returns the random seed used at the start of the current test run. - int random_seed() const; - - // Returns the ParameterizedTestSuiteRegistry object used to keep track of - // value-parameterized tests and instantiate and register them. - // - // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() - GTEST_LOCK_EXCLUDED_(mutex_); - - // Gets the number of successful test suites. - int successful_test_suite_count() const; - - // Gets the number of failed test suites. - int failed_test_suite_count() const; - - // Gets the number of all test suites. - int total_test_suite_count() const; - - // Gets the number of all test suites that contain at least one test - // that should run. - int test_suite_to_run_count() const; - - // Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - int successful_test_case_count() const; - int failed_test_case_count() const; - int total_test_case_count() const; - int test_case_to_run_count() const; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Gets the number of successful tests. - int successful_test_count() const; - - // Gets the number of skipped tests. - int skipped_test_count() const; - - // Gets the number of failed tests. - int failed_test_count() const; - - // Gets the number of disabled tests that will be reported in the XML report. - int reportable_disabled_test_count() const; - - // Gets the number of disabled tests. - int disabled_test_count() const; - - // Gets the number of tests to be printed in the XML report. - int reportable_test_count() const; - - // Gets the number of all tests. - int total_test_count() const; - - // Gets the number of tests that should run. - int test_to_run_count() const; - - // Gets the time of the test program start, in ms from the start of the - // UNIX epoch. - TimeInMillis start_timestamp() const; - - // Gets the elapsed time, in milliseconds. - TimeInMillis elapsed_time() const; - - // Returns true if and only if the unit test passed (i.e. all test suites - // passed). - bool Passed() const; - - // Returns true if and only if the unit test failed (i.e. some test suite - // failed or something outside of all tests failed). - bool Failed() const; - - // Gets the i-th test suite among all the test suites. i can range from 0 to - // total_test_suite_count() - 1. If i is not in that range, returns NULL. - const TestSuite* GetTestSuite(int i) const; - -// Legacy API is deprecated but still available -#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - const TestCase* GetTestCase(int i) const; -#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ - - // Returns the TestResult containing information on test failures and - // properties logged outside of individual test suites. - const TestResult& ad_hoc_test_result() const; - - // Returns the list of event listeners that can be used to track events - // inside Google Test. - TestEventListeners& listeners(); - - private: - // Registers and returns a global test environment. When a test - // program is run, all global test environments will be set-up in - // the order they were registered. After all tests in the program - // have finished, all global test environments will be torn-down in - // the *reverse* order they were registered. - // - // The UnitTest object takes ownership of the given environment. - // - // This method can only be called from the main thread. - Environment* AddEnvironment(Environment* env); - - // Adds a TestPartResult to the current TestResult object. All - // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) - // eventually call this to report their results. The user code - // should use the assertion macros instead of calling this directly. - void AddTestPartResult(TestPartResult::Type result_type, - const char* file_name, - int line_number, - const std::string& message, - const std::string& os_stack_trace) - GTEST_LOCK_EXCLUDED_(mutex_); - - // Adds a TestProperty to the current TestResult object when invoked from - // inside a test, to current TestSuite's ad_hoc_test_result_ when invoked - // from SetUpTestSuite or TearDownTestSuite, or to the global property set - // when invoked elsewhere. If the result already contains a property with - // the same key, the value will be updated. - void RecordProperty(const std::string& key, const std::string& value); - - // Gets the i-th test suite among all the test suites. i can range from 0 to - // total_test_suite_count() - 1. If i is not in that range, returns NULL. - TestSuite* GetMutableTestSuite(int i); - - // Accessors for the implementation object. - internal::UnitTestImpl* impl() { return impl_; } - const internal::UnitTestImpl* impl() const { return impl_; } - - // These classes and functions are friends as they need to access private - // members of UnitTest. - friend class ScopedTrace; - friend class Test; - friend class internal::AssertHelper; - friend class internal::StreamingListenerTest; - friend class internal::UnitTestRecordPropertyTestHelper; - friend Environment* AddGlobalTestEnvironment(Environment* env); - friend std::set* internal::GetIgnoredParameterizedTestSuites(); - friend internal::UnitTestImpl* internal::GetUnitTestImpl(); - friend void internal::ReportFailureInUnknownLocation( - TestPartResult::Type result_type, - const std::string& message); - - // Creates an empty UnitTest. - UnitTest(); - - // D'tor - virtual ~UnitTest(); - - // Pushes a trace defined by SCOPED_TRACE() on to the per-thread - // Google Test trace stack. - void PushGTestTrace(const internal::TraceInfo& trace) - GTEST_LOCK_EXCLUDED_(mutex_); - - // Pops a trace from the per-thread Google Test trace stack. - void PopGTestTrace() - GTEST_LOCK_EXCLUDED_(mutex_); - - // Protects mutable state in *impl_. This is mutable as some const - // methods need to lock it too. - mutable internal::Mutex mutex_; - - // Opaque implementation object. This field is never changed once - // the object is constructed. We don't mark it as const here, as - // doing so will cause a warning in the constructor of UnitTest. - // Mutable state in *impl_ is protected by mutex_. - internal::UnitTestImpl* impl_; - - // We disallow copying UnitTest. - GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest); -}; - -// A convenient wrapper for adding an environment for the test -// program. -// -// You should call this before RUN_ALL_TESTS() is called, probably in -// main(). If you use gtest_main, you need to call this before main() -// starts for it to take effect. For example, you can define a global -// variable like this: -// -// testing::Environment* const foo_env = -// testing::AddGlobalTestEnvironment(new FooEnvironment); -// -// However, we strongly recommend you to write your own main() and -// call AddGlobalTestEnvironment() there, as relying on initialization -// of global variables makes the code harder to read and may cause -// problems when you register multiple environments from different -// translation units and the environments have dependencies among them -// (remember that the compiler doesn't guarantee the order in which -// global variables from different translation units are initialized). -inline Environment* AddGlobalTestEnvironment(Environment* env) { - return UnitTest::GetInstance()->AddEnvironment(env); -} - -// Initializes Google Test. This must be called before calling -// RUN_ALL_TESTS(). In particular, it parses a command line for the -// flags that Google Test recognizes. Whenever a Google Test flag is -// seen, it is removed from argv, and *argc is decremented. -// -// No value is returned. Instead, the Google Test flag variables are -// updated. -// -// Calling the function for the second time has no user-visible effect. -GTEST_API_ void InitGoogleTest(int* argc, char** argv); - -// This overloaded version can be used in Windows programs compiled in -// UNICODE mode. -GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); - -// This overloaded version can be used on Arduino/embedded platforms where -// there is no argc/argv. -GTEST_API_ void InitGoogleTest(); - -namespace internal { - -// Separate the error generating code from the code path to reduce the stack -// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers -// when calling EXPECT_* in a tight loop. -template -AssertionResult CmpHelperEQFailure(const char* lhs_expression, - const char* rhs_expression, - const T1& lhs, const T2& rhs) { - return EqFailure(lhs_expression, - rhs_expression, - FormatForComparisonFailureMessage(lhs, rhs), - FormatForComparisonFailureMessage(rhs, lhs), - false); -} - -// This block of code defines operator==/!= -// to block lexical scope lookup. -// It prevents using invalid operator==/!= defined at namespace scope. -struct faketype {}; -inline bool operator==(faketype, faketype) { return true; } -inline bool operator!=(faketype, faketype) { return false; } - -// The helper function for {ASSERT|EXPECT}_EQ. -template -AssertionResult CmpHelperEQ(const char* lhs_expression, - const char* rhs_expression, - const T1& lhs, - const T2& rhs) { - if (lhs == rhs) { - return AssertionSuccess(); - } - - return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs); -} - -class EqHelper { - public: - // This templatized version is for the general case. - template < - typename T1, typename T2, - // Disable this overload for cases where one argument is a pointer - // and the other is the null pointer constant. - typename std::enable_if::value || - !std::is_pointer::value>::type* = nullptr> - static AssertionResult Compare(const char* lhs_expression, - const char* rhs_expression, const T1& lhs, - const T2& rhs) { - return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); - } - - // With this overloaded version, we allow anonymous enums to be used - // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous - // enums can be implicitly cast to BiggestInt. - // - // Even though its body looks the same as the above version, we - // cannot merge the two, as it will make anonymous enums unhappy. - static AssertionResult Compare(const char* lhs_expression, - const char* rhs_expression, - BiggestInt lhs, - BiggestInt rhs) { - return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); - } - - template - static AssertionResult Compare( - const char* lhs_expression, const char* rhs_expression, - // Handle cases where '0' is used as a null pointer literal. - std::nullptr_t /* lhs */, T* rhs) { - // We already know that 'lhs' is a null pointer. - return CmpHelperEQ(lhs_expression, rhs_expression, static_cast(nullptr), - rhs); - } -}; - -// Separate the error generating code from the code path to reduce the stack -// frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers -// when calling EXPECT_OP in a tight loop. -template -AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, - const T1& val1, const T2& val2, - const char* op) { - return AssertionFailure() - << "Expected: (" << expr1 << ") " << op << " (" << expr2 - << "), actual: " << FormatForComparisonFailureMessage(val1, val2) - << " vs " << FormatForComparisonFailureMessage(val2, val1); -} - -// A macro for implementing the helper functions needed to implement -// ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste -// of similar code. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - -#define GTEST_IMPL_CMP_HELPER_(op_name, op)\ -template \ -AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ - const T1& val1, const T2& val2) {\ - if (val1 op val2) {\ - return AssertionSuccess();\ - } else {\ - return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\ - }\ -} - -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. - -// Implements the helper function for {ASSERT|EXPECT}_NE -GTEST_IMPL_CMP_HELPER_(NE, !=) -// Implements the helper function for {ASSERT|EXPECT}_LE -GTEST_IMPL_CMP_HELPER_(LE, <=) -// Implements the helper function for {ASSERT|EXPECT}_LT -GTEST_IMPL_CMP_HELPER_(LT, <) -// Implements the helper function for {ASSERT|EXPECT}_GE -GTEST_IMPL_CMP_HELPER_(GE, >=) -// Implements the helper function for {ASSERT|EXPECT}_GT -GTEST_IMPL_CMP_HELPER_(GT, >) - -#undef GTEST_IMPL_CMP_HELPER_ - -// The helper function for {ASSERT|EXPECT}_STREQ. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2); - -// The helper function for {ASSERT|EXPECT}_STRCASEEQ. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2); - -// The helper function for {ASSERT|EXPECT}_STRNE. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2); - -// The helper function for {ASSERT|EXPECT}_STRCASENE. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, - const char* s2_expression, - const char* s1, - const char* s2); - - -// Helper function for *_STREQ on wide strings. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, - const char* s2_expression, - const wchar_t* s1, - const wchar_t* s2); - -// Helper function for *_STRNE on wide strings. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, - const char* s2_expression, - const wchar_t* s1, - const wchar_t* s2); - -} // namespace internal - -// IsSubstring() and IsNotSubstring() are intended to be used as the -// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by -// themselves. They check whether needle is a substring of haystack -// (NULL is considered a substring of itself only), and return an -// appropriate error message when they fail. -// -// The {needle,haystack}_expr arguments are the stringified -// expressions that generated the two real arguments. -GTEST_API_ AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const char* needle, const char* haystack); -GTEST_API_ AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const wchar_t* needle, const wchar_t* haystack); -GTEST_API_ AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const char* needle, const char* haystack); -GTEST_API_ AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const wchar_t* needle, const wchar_t* haystack); -GTEST_API_ AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::string& needle, const ::std::string& haystack); -GTEST_API_ AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::string& needle, const ::std::string& haystack); - -#if GTEST_HAS_STD_WSTRING -GTEST_API_ AssertionResult IsSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::wstring& needle, const ::std::wstring& haystack); -GTEST_API_ AssertionResult IsNotSubstring( - const char* needle_expr, const char* haystack_expr, - const ::std::wstring& needle, const ::std::wstring& haystack); -#endif // GTEST_HAS_STD_WSTRING - -namespace internal { - -// Helper template function for comparing floating-points. -// -// Template parameter: -// -// RawType: the raw floating-point type (either float or double) -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -template -AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression, - const char* rhs_expression, - RawType lhs_value, - RawType rhs_value) { - const FloatingPoint lhs(lhs_value), rhs(rhs_value); - - if (lhs.AlmostEquals(rhs)) { - return AssertionSuccess(); - } - - ::std::stringstream lhs_ss; - lhs_ss << std::setprecision(std::numeric_limits::digits10 + 2) - << lhs_value; - - ::std::stringstream rhs_ss; - rhs_ss << std::setprecision(std::numeric_limits::digits10 + 2) - << rhs_value; - - return EqFailure(lhs_expression, - rhs_expression, - StringStreamToString(&lhs_ss), - StringStreamToString(&rhs_ss), - false); -} - -// Helper function for implementing ASSERT_NEAR. -// -// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. -GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, - const char* expr2, - const char* abs_error_expr, - double val1, - double val2, - double abs_error); - -// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. -// A class that enables one to stream messages to assertion macros -class GTEST_API_ AssertHelper { - public: - // Constructor. - AssertHelper(TestPartResult::Type type, - const char* file, - int line, - const char* message); - ~AssertHelper(); - - // Message assignment is a semantic trick to enable assertion - // streaming; see the GTEST_MESSAGE_ macro below. - void operator=(const Message& message) const; - - private: - // We put our data in a struct so that the size of the AssertHelper class can - // be as small as possible. This is important because gcc is incapable of - // re-using stack space even for temporary variables, so every EXPECT_EQ - // reserves stack space for another AssertHelper. - struct AssertHelperData { - AssertHelperData(TestPartResult::Type t, - const char* srcfile, - int line_num, - const char* msg) - : type(t), file(srcfile), line(line_num), message(msg) { } - - TestPartResult::Type const type; - const char* const file; - int const line; - std::string const message; - - private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); - }; - - AssertHelperData* const data_; - - GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); -}; - -} // namespace internal - -// The pure interface class that all value-parameterized tests inherit from. -// A value-parameterized class must inherit from both ::testing::Test and -// ::testing::WithParamInterface. In most cases that just means inheriting -// from ::testing::TestWithParam, but more complicated test hierarchies -// may need to inherit from Test and WithParamInterface at different levels. -// -// This interface has support for accessing the test parameter value via -// the GetParam() method. -// -// Use it with one of the parameter generator defining functions, like Range(), -// Values(), ValuesIn(), Bool(), and Combine(). -// -// class FooTest : public ::testing::TestWithParam { -// protected: -// FooTest() { -// // Can use GetParam() here. -// } -// ~FooTest() override { -// // Can use GetParam() here. -// } -// void SetUp() override { -// // Can use GetParam() here. -// } -// void TearDown override { -// // Can use GetParam() here. -// } -// }; -// TEST_P(FooTest, DoesBar) { -// // Can use GetParam() method here. -// Foo foo; -// ASSERT_TRUE(foo.DoesBar(GetParam())); -// } -// INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); - -template -class WithParamInterface { - public: - typedef T ParamType; - virtual ~WithParamInterface() {} - - // The current parameter value. Is also available in the test fixture's - // constructor. - static const ParamType& GetParam() { - GTEST_CHECK_(parameter_ != nullptr) - << "GetParam() can only be called inside a value-parameterized test " - << "-- did you intend to write TEST_P instead of TEST_F?"; - return *parameter_; - } - - private: - // Sets parameter value. The caller is responsible for making sure the value - // remains alive and unchanged throughout the current test. - static void SetParam(const ParamType* parameter) { - parameter_ = parameter; - } - - // Static value used for accessing parameter during a test lifetime. - static const ParamType* parameter_; - - // TestClass must be a subclass of WithParamInterface and Test. - template friend class internal::ParameterizedTestFactory; -}; - -template -const T* WithParamInterface::parameter_ = nullptr; - -// Most value-parameterized classes can ignore the existence of -// WithParamInterface, and can just inherit from ::testing::TestWithParam. - -template -class TestWithParam : public Test, public WithParamInterface { -}; - -// Macros for indicating success/failure in test code. - -// Skips test in runtime. -// Skipping test aborts current function. -// Skipped tests are neither successful nor failed. -#define GTEST_SKIP() GTEST_SKIP_("") - -// ADD_FAILURE unconditionally adds a failure to the current test. -// SUCCEED generates a success - it doesn't automatically make the -// current test successful, as a test is only successful when it has -// no failure. -// -// EXPECT_* verifies that a certain condition is satisfied. If not, -// it behaves like ADD_FAILURE. In particular: -// -// EXPECT_TRUE verifies that a Boolean condition is true. -// EXPECT_FALSE verifies that a Boolean condition is false. -// -// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except -// that they will also abort the current function on failure. People -// usually want the fail-fast behavior of FAIL and ASSERT_*, but those -// writing data-driven tests often find themselves using ADD_FAILURE -// and EXPECT_* more. - -// Generates a nonfatal failure with a generic message. -#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") - -// Generates a nonfatal failure at the given source file location with -// a generic message. -#define ADD_FAILURE_AT(file, line) \ - GTEST_MESSAGE_AT_(file, line, "Failed", \ - ::testing::TestPartResult::kNonFatalFailure) - -// Generates a fatal failure with a generic message. -#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") - -// Like GTEST_FAIL(), but at the given source file location. -#define GTEST_FAIL_AT(file, line) \ - GTEST_MESSAGE_AT_(file, line, "Failed", \ - ::testing::TestPartResult::kFatalFailure) - -// Define this macro to 1 to omit the definition of FAIL(), which is a -// generic name and clashes with some other libraries. -#if !GTEST_DONT_DEFINE_FAIL -# define FAIL() GTEST_FAIL() -#endif - -// Generates a success with a generic message. -#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") - -// Define this macro to 1 to omit the definition of SUCCEED(), which -// is a generic name and clashes with some other libraries. -#if !GTEST_DONT_DEFINE_SUCCEED -# define SUCCEED() GTEST_SUCCEED() -#endif - -// Macros for testing exceptions. -// -// * {ASSERT|EXPECT}_THROW(statement, expected_exception): -// Tests that the statement throws the expected exception. -// * {ASSERT|EXPECT}_NO_THROW(statement): -// Tests that the statement doesn't throw any exception. -// * {ASSERT|EXPECT}_ANY_THROW(statement): -// Tests that the statement throws an exception. - -#define EXPECT_THROW(statement, expected_exception) \ - GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) -#define EXPECT_NO_THROW(statement) \ - GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) -#define EXPECT_ANY_THROW(statement) \ - GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) -#define ASSERT_THROW(statement, expected_exception) \ - GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) -#define ASSERT_NO_THROW(statement) \ - GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) -#define ASSERT_ANY_THROW(statement) \ - GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) - -// Boolean assertions. Condition can be either a Boolean expression or an -// AssertionResult. For more information on how to use AssertionResult with -// these macros see comments on that class. -#define GTEST_EXPECT_TRUE(condition) \ - GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ - GTEST_NONFATAL_FAILURE_) -#define GTEST_EXPECT_FALSE(condition) \ - GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ - GTEST_NONFATAL_FAILURE_) -#define GTEST_ASSERT_TRUE(condition) \ - GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ - GTEST_FATAL_FAILURE_) -#define GTEST_ASSERT_FALSE(condition) \ - GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ - GTEST_FATAL_FAILURE_) - -// Define these macros to 1 to omit the definition of the corresponding -// EXPECT or ASSERT, which clashes with some users' own code. - -#if !GTEST_DONT_DEFINE_EXPECT_TRUE -#define EXPECT_TRUE(condition) GTEST_EXPECT_TRUE(condition) -#endif - -#if !GTEST_DONT_DEFINE_EXPECT_FALSE -#define EXPECT_FALSE(condition) GTEST_EXPECT_FALSE(condition) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_TRUE -#define ASSERT_TRUE(condition) GTEST_ASSERT_TRUE(condition) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_FALSE -#define ASSERT_FALSE(condition) GTEST_ASSERT_FALSE(condition) -#endif - -// Macros for testing equalities and inequalities. -// -// * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2 -// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 -// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 -// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 -// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 -// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 -// -// When they are not, Google Test prints both the tested expressions and -// their actual values. The values must be compatible built-in types, -// or you will get a compiler error. By "compatible" we mean that the -// values can be compared by the respective operator. -// -// Note: -// -// 1. It is possible to make a user-defined type work with -// {ASSERT|EXPECT}_??(), but that requires overloading the -// comparison operators and is thus discouraged by the Google C++ -// Usage Guide. Therefore, you are advised to use the -// {ASSERT|EXPECT}_TRUE() macro to assert that two objects are -// equal. -// -// 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on -// pointers (in particular, C strings). Therefore, if you use it -// with two C strings, you are testing how their locations in memory -// are related, not how their content is related. To compare two C -// strings by content, use {ASSERT|EXPECT}_STR*(). -// -// 3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to -// {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you -// what the actual value is when it fails, and similarly for the -// other comparisons. -// -// 4. Do not depend on the order in which {ASSERT|EXPECT}_??() -// evaluate their arguments, which is undefined. -// -// 5. These macros evaluate their arguments exactly once. -// -// Examples: -// -// EXPECT_NE(Foo(), 5); -// EXPECT_EQ(a_pointer, NULL); -// ASSERT_LT(i, array_size); -// ASSERT_GT(records.size(), 0) << "There is no record left."; - -#define EXPECT_EQ(val1, val2) \ - EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2) -#define EXPECT_NE(val1, val2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) -#define EXPECT_LE(val1, val2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) -#define EXPECT_LT(val1, val2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) -#define EXPECT_GE(val1, val2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) -#define EXPECT_GT(val1, val2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) - -#define GTEST_ASSERT_EQ(val1, val2) \ - ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2) -#define GTEST_ASSERT_NE(val1, val2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) -#define GTEST_ASSERT_LE(val1, val2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) -#define GTEST_ASSERT_LT(val1, val2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) -#define GTEST_ASSERT_GE(val1, val2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) -#define GTEST_ASSERT_GT(val1, val2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) - -// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of -// ASSERT_XY(), which clashes with some users' own code. - -#if !GTEST_DONT_DEFINE_ASSERT_EQ -# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_NE -# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_LE -# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_LT -# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_GE -# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) -#endif - -#if !GTEST_DONT_DEFINE_ASSERT_GT -# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) -#endif - -// C-string Comparisons. All tests treat NULL and any non-NULL string -// as different. Two NULLs are equal. -// -// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 -// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 -// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case -// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case -// -// For wide or narrow string objects, you can use the -// {ASSERT|EXPECT}_??() macros. -// -// Don't depend on the order in which the arguments are evaluated, -// which is undefined. -// -// These macros evaluate their arguments exactly once. - -#define EXPECT_STREQ(s1, s2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) -#define EXPECT_STRNE(s1, s2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) -#define EXPECT_STRCASEEQ(s1, s2) \ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) -#define EXPECT_STRCASENE(s1, s2)\ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) - -#define ASSERT_STREQ(s1, s2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) -#define ASSERT_STRNE(s1, s2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) -#define ASSERT_STRCASEEQ(s1, s2) \ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) -#define ASSERT_STRCASENE(s1, s2)\ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) - -// Macros for comparing floating-point numbers. -// -// * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2): -// Tests that two float values are almost equal. -// * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2): -// Tests that two double values are almost equal. -// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): -// Tests that v1 and v2 are within the given distance to each other. -// -// Google Test uses ULP-based comparison to automatically pick a default -// error bound that is appropriate for the operands. See the -// FloatingPoint template class in gtest-internal.h if you are -// interested in the implementation details. - -#define EXPECT_FLOAT_EQ(val1, val2)\ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ - val1, val2) - -#define EXPECT_DOUBLE_EQ(val1, val2)\ - EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ - val1, val2) - -#define ASSERT_FLOAT_EQ(val1, val2)\ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ - val1, val2) - -#define ASSERT_DOUBLE_EQ(val1, val2)\ - ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ - val1, val2) - -#define EXPECT_NEAR(val1, val2, abs_error)\ - EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ - val1, val2, abs_error) - -#define ASSERT_NEAR(val1, val2, abs_error)\ - ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ - val1, val2, abs_error) - -// These predicate format functions work on floating-point values, and -// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. -// -// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); - -// Asserts that val1 is less than, or almost equal to, val2. Fails -// otherwise. In particular, it fails if either val1 or val2 is NaN. -GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, - float val1, float val2); -GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, - double val1, double val2); - - -#if GTEST_OS_WINDOWS - -// Macros that test for HRESULT failure and success, these are only useful -// on Windows, and rely on Windows SDK macros and APIs to compile. -// -// * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) -// -// When expr unexpectedly fails or succeeds, Google Test prints the -// expected result and the actual result with both a human-readable -// string representation of the error, if available, as well as the -// hex result code. -# define EXPECT_HRESULT_SUCCEEDED(expr) \ - EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) - -# define ASSERT_HRESULT_SUCCEEDED(expr) \ - ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) - -# define EXPECT_HRESULT_FAILED(expr) \ - EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) - -# define ASSERT_HRESULT_FAILED(expr) \ - ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) - -#endif // GTEST_OS_WINDOWS - -// Macros that execute statement and check that it doesn't generate new fatal -// failures in the current thread. -// -// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); -// -// Examples: -// -// EXPECT_NO_FATAL_FAILURE(Process()); -// ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; -// -#define ASSERT_NO_FATAL_FAILURE(statement) \ - GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) -#define EXPECT_NO_FATAL_FAILURE(statement) \ - GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) - -// Causes a trace (including the given source file path and line number, -// and the given message) to be included in every test failure message generated -// by code in the scope of the lifetime of an instance of this class. The effect -// is undone with the destruction of the instance. -// -// The message argument can be anything streamable to std::ostream. -// -// Example: -// testing::ScopedTrace trace("file.cc", 123, "message"); -// -class GTEST_API_ ScopedTrace { - public: - // The c'tor pushes the given source file location and message onto - // a trace stack maintained by Google Test. - - // Template version. Uses Message() to convert the values into strings. - // Slow, but flexible. - template - ScopedTrace(const char* file, int line, const T& message) { - PushTrace(file, line, (Message() << message).GetString()); - } - - // Optimize for some known types. - ScopedTrace(const char* file, int line, const char* message) { - PushTrace(file, line, message ? message : "(null)"); - } - - ScopedTrace(const char* file, int line, const std::string& message) { - PushTrace(file, line, message); - } - - // The d'tor pops the info pushed by the c'tor. - // - // Note that the d'tor is not virtual in order to be efficient. - // Don't inherit from ScopedTrace! - ~ScopedTrace(); - - private: - void PushTrace(const char* file, int line, std::string message); - - GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace); -} GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its - // c'tor and d'tor. Therefore it doesn't - // need to be used otherwise. - -// Causes a trace (including the source file path, the current line -// number, and the given message) to be included in every test failure -// message generated by code in the current scope. The effect is -// undone when the control leaves the current scope. -// -// The message argument can be anything streamable to std::ostream. -// -// In the implementation, we include the current line number as part -// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s -// to appear in the same block - as long as they are on different -// lines. -// -// Assuming that each thread maintains its own stack of traces. -// Therefore, a SCOPED_TRACE() would (correctly) only affect the -// assertions in its own thread. -#define SCOPED_TRACE(message) \ - ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ - __FILE__, __LINE__, (message)) - -// Compile-time assertion for type equality. -// StaticAssertTypeEq() compiles if and only if type1 and type2 -// are the same type. The value it returns is not interesting. -// -// Instead of making StaticAssertTypeEq a class template, we make it a -// function template that invokes a helper class template. This -// prevents a user from misusing StaticAssertTypeEq by -// defining objects of that type. -// -// CAVEAT: -// -// When used inside a method of a class template, -// StaticAssertTypeEq() is effective ONLY IF the method is -// instantiated. For example, given: -// -// template class Foo { -// public: -// void Bar() { testing::StaticAssertTypeEq(); } -// }; -// -// the code: -// -// void Test1() { Foo foo; } -// -// will NOT generate a compiler error, as Foo::Bar() is never -// actually instantiated. Instead, you need: -// -// void Test2() { Foo foo; foo.Bar(); } -// -// to cause a compiler error. -template -constexpr bool StaticAssertTypeEq() noexcept { - static_assert(std::is_same::value, "T1 and T2 are not the same type"); - return true; -} - -// Defines a test. -// -// The first parameter is the name of the test suite, and the second -// parameter is the name of the test within the test suite. -// -// The convention is to end the test suite name with "Test". For -// example, a test suite for the Foo class can be named FooTest. -// -// Test code should appear between braces after an invocation of -// this macro. Example: -// -// TEST(FooTest, InitializesCorrectly) { -// Foo foo; -// EXPECT_TRUE(foo.StatusIsOK()); -// } - -// Note that we call GetTestTypeId() instead of GetTypeId< -// ::testing::Test>() here to get the type ID of testing::Test. This -// is to work around a suspected linker bug when using Google Test as -// a framework on Mac OS X. The bug causes GetTypeId< -// ::testing::Test>() to return different values depending on whether -// the call is from the Google Test framework itself or from user test -// code. GetTestTypeId() is guaranteed to always return the same -// value, as it always calls GetTypeId<>() from the Google Test -// framework. -#define GTEST_TEST(test_suite_name, test_name) \ - GTEST_TEST_(test_suite_name, test_name, ::testing::Test, \ - ::testing::internal::GetTestTypeId()) - -// Define this macro to 1 to omit the definition of TEST(), which -// is a generic name and clashes with some other libraries. -#if !GTEST_DONT_DEFINE_TEST -#define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name) -#endif - -// Defines a test that uses a test fixture. -// -// The first parameter is the name of the test fixture class, which -// also doubles as the test suite name. The second parameter is the -// name of the test within the test suite. -// -// A test fixture class must be declared earlier. The user should put -// the test code between braces after using this macro. Example: -// -// class FooTest : public testing::Test { -// protected: -// void SetUp() override { b_.AddElement(3); } -// -// Foo a_; -// Foo b_; -// }; -// -// TEST_F(FooTest, InitializesCorrectly) { -// EXPECT_TRUE(a_.StatusIsOK()); -// } -// -// TEST_F(FooTest, ReturnsElementCountCorrectly) { -// EXPECT_EQ(a_.size(), 0); -// EXPECT_EQ(b_.size(), 1); -// } -// -// GOOGLETEST_CM0011 DO NOT DELETE -#if !GTEST_DONT_DEFINE_TEST -#define TEST_F(test_fixture, test_name)\ - GTEST_TEST_(test_fixture, test_name, test_fixture, \ - ::testing::internal::GetTypeId()) -#endif // !GTEST_DONT_DEFINE_TEST - -// Returns a path to temporary directory. -// Tries to determine an appropriate directory for the platform. -GTEST_API_ std::string TempDir(); - -#ifdef _MSC_VER -# pragma warning(pop) -#endif - -// Dynamically registers a test with the framework. -// -// This is an advanced API only to be used when the `TEST` macros are -// insufficient. The macros should be preferred when possible, as they avoid -// most of the complexity of calling this function. -// -// The `factory` argument is a factory callable (move-constructible) object or -// function pointer that creates a new instance of the Test object. It -// handles ownership to the caller. The signature of the callable is -// `Fixture*()`, where `Fixture` is the test fixture class for the test. All -// tests registered with the same `test_suite_name` must return the same -// fixture type. This is checked at runtime. -// -// The framework will infer the fixture class from the factory and will call -// the `SetUpTestSuite` and `TearDownTestSuite` for it. -// -// Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is -// undefined. -// -// Use case example: -// -// class MyFixture : public ::testing::Test { -// public: -// // All of these optional, just like in regular macro usage. -// static void SetUpTestSuite() { ... } -// static void TearDownTestSuite() { ... } -// void SetUp() override { ... } -// void TearDown() override { ... } -// }; -// -// class MyTest : public MyFixture { -// public: -// explicit MyTest(int data) : data_(data) {} -// void TestBody() override { ... } -// -// private: -// int data_; -// }; -// -// void RegisterMyTests(const std::vector& values) { -// for (int v : values) { -// ::testing::RegisterTest( -// "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, -// std::to_string(v).c_str(), -// __FILE__, __LINE__, -// // Important to use the fixture type as the return type here. -// [=]() -> MyFixture* { return new MyTest(v); }); -// } -// } -// ... -// int main(int argc, char** argv) { -// std::vector values_to_test = LoadValuesFromConfig(); -// RegisterMyTests(values_to_test); -// ... -// return RUN_ALL_TESTS(); -// } -// -template -TestInfo* RegisterTest(const char* test_suite_name, const char* test_name, - const char* type_param, const char* value_param, - const char* file, int line, Factory factory) { - using TestT = typename std::remove_pointer::type; - - class FactoryImpl : public internal::TestFactoryBase { - public: - explicit FactoryImpl(Factory f) : factory_(std::move(f)) {} - Test* CreateTest() override { return factory_(); } - - private: - Factory factory_; - }; - - return internal::MakeAndRegisterTestInfo( - test_suite_name, test_name, type_param, value_param, - internal::CodeLocation(file, line), internal::GetTypeId(), - internal::SuiteApiResolver::GetSetUpCaseOrSuite(file, line), - internal::SuiteApiResolver::GetTearDownCaseOrSuite(file, line), - new FactoryImpl{std::move(factory)}); -} - -} // namespace testing - -// Use this function in main() to run all tests. It returns 0 if all -// tests are successful, or 1 otherwise. -// -// RUN_ALL_TESTS() should be invoked after the command line has been -// parsed by InitGoogleTest(). -// -// This function was formerly a macro; thus, it is in the global -// namespace and has an all-caps name. -int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; - -inline int RUN_ALL_TESTS() { - return ::testing::UnitTest::GetInstance()->Run(); -} - -GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 - -#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_H_ diff --git a/test/gtest/include/gtest/gtest-assertion-result.h b/test/gtest/include/gtest/gtest-assertion-result.h new file mode 100644 index 0000000000..addbb59c64 --- /dev/null +++ b/test/gtest/include/gtest/gtest-assertion-result.h @@ -0,0 +1,237 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This file implements the AssertionResult type. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ + +#include +#include +#include +#include + +#include "gtest/gtest-message.h" +#include "gtest/internal/gtest-port.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +namespace testing { + +// A class for indicating whether an assertion was successful. When +// the assertion wasn't successful, the AssertionResult object +// remembers a non-empty message that describes how it failed. +// +// To create an instance of this class, use one of the factory functions +// (AssertionSuccess() and AssertionFailure()). +// +// This class is useful for two purposes: +// 1. Defining predicate functions to be used with Boolean test assertions +// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts +// 2. Defining predicate-format functions to be +// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). +// +// For example, if you define IsEven predicate: +// +// testing::AssertionResult IsEven(int n) { +// if ((n % 2) == 0) +// return testing::AssertionSuccess(); +// else +// return testing::AssertionFailure() << n << " is odd"; +// } +// +// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) +// will print the message +// +// Value of: IsEven(Fib(5)) +// Actual: false (5 is odd) +// Expected: true +// +// instead of a more opaque +// +// Value of: IsEven(Fib(5)) +// Actual: false +// Expected: true +// +// in case IsEven is a simple Boolean predicate. +// +// If you expect your predicate to be reused and want to support informative +// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up +// about half as often as positive ones in our tests), supply messages for +// both success and failure cases: +// +// testing::AssertionResult IsEven(int n) { +// if ((n % 2) == 0) +// return testing::AssertionSuccess() << n << " is even"; +// else +// return testing::AssertionFailure() << n << " is odd"; +// } +// +// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print +// +// Value of: IsEven(Fib(6)) +// Actual: true (8 is even) +// Expected: false +// +// NB: Predicates that support negative Boolean assertions have reduced +// performance in positive ones so be careful not to use them in tests +// that have lots (tens of thousands) of positive Boolean assertions. +// +// To use this class with EXPECT_PRED_FORMAT assertions such as: +// +// // Verifies that Foo() returns an even number. +// EXPECT_PRED_FORMAT1(IsEven, Foo()); +// +// you need to define: +// +// testing::AssertionResult IsEven(const char* expr, int n) { +// if ((n % 2) == 0) +// return testing::AssertionSuccess(); +// else +// return testing::AssertionFailure() +// << "Expected: " << expr << " is even\n Actual: it's " << n; +// } +// +// If Foo() returns 5, you will see the following message: +// +// Expected: Foo() is even +// Actual: it's 5 +// +class GTEST_API_ AssertionResult { + public: + // Copy constructor. + // Used in EXPECT_TRUE/FALSE(assertion_result). + AssertionResult(const AssertionResult& other); + +// C4800 is a level 3 warning in Visual Studio 2015 and earlier. +// This warning is not emitted in Visual Studio 2017. +// This warning is off by default starting in Visual Studio 2019 but can be +// enabled with command-line options. +#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) +#endif + + // Used in the EXPECT_TRUE/FALSE(bool_expression). + // + // T must be contextually convertible to bool. + // + // The second parameter prevents this overload from being considered if + // the argument is implicitly convertible to AssertionResult. In that case + // we want AssertionResult's copy constructor to be used. + template + explicit AssertionResult( + const T& success, + typename std::enable_if< + !std::is_convertible::value>::type* + /*enabler*/ + = nullptr) + : success_(success) {} + +#if defined(_MSC_VER) && (_MSC_VER < 1910 || _MSC_VER >= 1920) + GTEST_DISABLE_MSC_WARNINGS_POP_() +#endif + + // Assignment operator. + AssertionResult& operator=(AssertionResult other) { + swap(other); + return *this; + } + + // Returns true if and only if the assertion succeeded. + operator bool() const { return success_; } // NOLINT + + // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. + AssertionResult operator!() const; + + // Returns the text streamed into this AssertionResult. Test assertions + // use it when they fail (i.e., the predicate's outcome doesn't match the + // assertion's expectation). When nothing has been streamed into the + // object, returns an empty string. + const char* message() const { + return message_.get() != nullptr ? message_->c_str() : ""; + } + // Deprecated; please use message() instead. + const char* failure_message() const { return message(); } + + // Streams a custom failure message into this object. + template + AssertionResult& operator<<(const T& value) { + AppendMessage(Message() << value); + return *this; + } + + // Allows streaming basic output manipulators such as endl or flush into + // this object. + AssertionResult& operator<<( + ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { + AppendMessage(Message() << basic_manipulator); + return *this; + } + + private: + // Appends the contents of message to message_. + void AppendMessage(const Message& a_message) { + if (message_.get() == nullptr) message_.reset(new ::std::string); + message_->append(a_message.GetString().c_str()); + } + + // Swap the contents of this AssertionResult with other. + void swap(AssertionResult& other); + + // Stores result of the assertion predicate. + bool success_; + // Stores the message describing the condition in case the expectation + // construct is not satisfied with the predicate's outcome. + // Referenced via a pointer to avoid taking too much stack frame space + // with test assertions. + std::unique_ptr< ::std::string> message_; +}; + +// Makes a successful assertion result. +GTEST_API_ AssertionResult AssertionSuccess(); + +// Makes a failed assertion result. +GTEST_API_ AssertionResult AssertionFailure(); + +// Makes a failed assertion result with the given failure message. +// Deprecated; use AssertionFailure() << msg. +GTEST_API_ AssertionResult AssertionFailure(const Message& msg); + +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_ASSERTION_RESULT_H_ diff --git a/test/gtest/include/gtest/gtest-death-test.h b/test/gtest/include/gtest/gtest-death-test.h new file mode 100644 index 0000000000..84e5a5bbd3 --- /dev/null +++ b/test/gtest/include/gtest/gtest-death-test.h @@ -0,0 +1,345 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file defines the public API for death tests. It is +// #included by gtest.h so a user doesn't need to include this +// directly. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ + +#include "gtest/internal/gtest-death-test-internal.h" + +// This flag controls the style of death tests. Valid values are "threadsafe", +// meaning that the death test child process will re-execute the test binary +// from the start, running only a single death test, or "fast", +// meaning that the child process will execute the test logic immediately +// after forking. +GTEST_DECLARE_string_(death_test_style); + +namespace testing { + +#if GTEST_HAS_DEATH_TEST + +namespace internal { + +// Returns a Boolean value indicating whether the caller is currently +// executing in the context of the death test child process. Tools such as +// Valgrind heap checkers may need this to modify their behavior in death +// tests. IMPORTANT: This is an internal utility. Using it may break the +// implementation of death tests. User code MUST NOT use it. +GTEST_API_ bool InDeathTestChild(); + +} // namespace internal + +// The following macros are useful for writing death tests. + +// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is +// executed: +// +// 1. It generates a warning if there is more than one active +// thread. This is because it's safe to fork() or clone() only +// when there is a single thread. +// +// 2. The parent process clone()s a sub-process and runs the death +// test in it; the sub-process exits with code 0 at the end of the +// death test, if it hasn't exited already. +// +// 3. The parent process waits for the sub-process to terminate. +// +// 4. The parent process checks the exit code and error message of +// the sub-process. +// +// Examples: +// +// ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number"); +// for (int i = 0; i < 5; i++) { +// EXPECT_DEATH(server.ProcessRequest(i), +// "Invalid request .* in ProcessRequest()") +// << "Failed to die on request " << i; +// } +// +// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); +// +// bool KilledBySIGHUP(int exit_code) { +// return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP; +// } +// +// ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!"); +// +// The final parameter to each of these macros is a matcher applied to any data +// the sub-process wrote to stderr. For compatibility with existing tests, a +// bare string is interpreted as a regular expression matcher. +// +// On the regular expressions used in death tests: +// +// On POSIX-compliant systems (*nix), we use the library, +// which uses the POSIX extended regex syntax. +// +// On other platforms (e.g. Windows or Mac), we only support a simple regex +// syntax implemented as part of Google Test. This limited +// implementation should be enough most of the time when writing +// death tests; though it lacks many features you can find in PCRE +// or POSIX extended regex syntax. For example, we don't support +// union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and +// repetition count ("x{5,7}"), among others. +// +// Below is the syntax that we do support. We chose it to be a +// subset of both PCRE and POSIX extended regex, so it's easy to +// learn wherever you come from. In the following: 'A' denotes a +// literal character, period (.), or a single \\ escape sequence; +// 'x' and 'y' denote regular expressions; 'm' and 'n' are for +// natural numbers. +// +// c matches any literal character c +// \\d matches any decimal digit +// \\D matches any character that's not a decimal digit +// \\f matches \f +// \\n matches \n +// \\r matches \r +// \\s matches any ASCII whitespace, including \n +// \\S matches any character that's not a whitespace +// \\t matches \t +// \\v matches \v +// \\w matches any letter, _, or decimal digit +// \\W matches any character that \\w doesn't match +// \\c matches any literal character c, which must be a punctuation +// . matches any single character except \n +// A? matches 0 or 1 occurrences of A +// A* matches 0 or many occurrences of A +// A+ matches 1 or many occurrences of A +// ^ matches the beginning of a string (not that of each line) +// $ matches the end of a string (not that of each line) +// xy matches x followed by y +// +// If you accidentally use PCRE or POSIX extended regex features +// not implemented by us, you will get a run-time failure. In that +// case, please try to rewrite your regular expression within the +// above syntax. +// +// This implementation is *not* meant to be as highly tuned or robust +// as a compiled regex library, but should perform well enough for a +// death test, which already incurs significant overhead by launching +// a child process. +// +// Known caveats: +// +// A "threadsafe" style death test obtains the path to the test +// program from argv[0] and re-executes it in the sub-process. For +// simplicity, the current implementation doesn't search the PATH +// when launching the sub-process. This means that the user must +// invoke the test program via a path that contains at least one +// path separator (e.g. path/to/foo_test and +// /absolute/path/to/bar_test are fine, but foo_test is not). This +// is rarely a problem as people usually don't put the test binary +// directory in PATH. +// + +// Asserts that a given `statement` causes the program to exit, with an +// integer exit status that satisfies `predicate`, and emitting error output +// that matches `matcher`. +#define ASSERT_EXIT(statement, predicate, matcher) \ + GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_FATAL_FAILURE_) + +// Like `ASSERT_EXIT`, but continues on to successive tests in the +// test suite, if any: +#define EXPECT_EXIT(statement, predicate, matcher) \ + GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_NONFATAL_FAILURE_) + +// Asserts that a given `statement` causes the program to exit, either by +// explicitly exiting with a nonzero exit code or being killed by a +// signal, and emitting error output that matches `matcher`. +#define ASSERT_DEATH(statement, matcher) \ + ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher) + +// Like `ASSERT_DEATH`, but continues on to successive tests in the +// test suite, if any: +#define EXPECT_DEATH(statement, matcher) \ + EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher) + +// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*: + +// Tests that an exit code describes a normal exit with a given exit code. +class GTEST_API_ ExitedWithCode { + public: + explicit ExitedWithCode(int exit_code); + ExitedWithCode(const ExitedWithCode&) = default; + void operator=(const ExitedWithCode& other) = delete; + bool operator()(int exit_status) const; + + private: + const int exit_code_; +}; + +#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA +// Tests that an exit code describes an exit due to termination by a +// given signal. +class GTEST_API_ KilledBySignal { + public: + explicit KilledBySignal(int signum); + bool operator()(int exit_status) const; + + private: + const int signum_; +}; +#endif // !GTEST_OS_WINDOWS + +// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode. +// The death testing framework causes this to have interesting semantics, +// since the sideeffects of the call are only visible in opt mode, and not +// in debug mode. +// +// In practice, this can be used to test functions that utilize the +// LOG(DFATAL) macro using the following style: +// +// int DieInDebugOr12(int* sideeffect) { +// if (sideeffect) { +// *sideeffect = 12; +// } +// LOG(DFATAL) << "death"; +// return 12; +// } +// +// TEST(TestSuite, TestDieOr12WorksInDgbAndOpt) { +// int sideeffect = 0; +// // Only asserts in dbg. +// EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death"); +// +// #ifdef NDEBUG +// // opt-mode has sideeffect visible. +// EXPECT_EQ(12, sideeffect); +// #else +// // dbg-mode no visible sideeffect. +// EXPECT_EQ(0, sideeffect); +// #endif +// } +// +// This will assert that DieInDebugReturn12InOpt() crashes in debug +// mode, usually due to a DCHECK or LOG(DFATAL), but returns the +// appropriate fallback value (12 in this case) in opt mode. If you +// need to test that a function has appropriate side-effects in opt +// mode, include assertions against the side-effects. A general +// pattern for this is: +// +// EXPECT_DEBUG_DEATH({ +// // Side-effects here will have an effect after this statement in +// // opt mode, but none in debug mode. +// EXPECT_EQ(12, DieInDebugOr12(&sideeffect)); +// }, "death"); +// +#ifdef NDEBUG + +#define EXPECT_DEBUG_DEATH(statement, regex) \ + GTEST_EXECUTE_STATEMENT_(statement, regex) + +#define ASSERT_DEBUG_DEATH(statement, regex) \ + GTEST_EXECUTE_STATEMENT_(statement, regex) + +#else + +#define EXPECT_DEBUG_DEATH(statement, regex) EXPECT_DEATH(statement, regex) + +#define ASSERT_DEBUG_DEATH(statement, regex) ASSERT_DEATH(statement, regex) + +#endif // NDEBUG for EXPECT_DEBUG_DEATH +#endif // GTEST_HAS_DEATH_TEST + +// This macro is used for implementing macros such as +// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where +// death tests are not supported. Those macros must compile on such systems +// if and only if EXPECT_DEATH and ASSERT_DEATH compile with the same parameters +// on systems that support death tests. This allows one to write such a macro on +// a system that does not support death tests and be sure that it will compile +// on a death-test supporting system. It is exposed publicly so that systems +// that have death-tests with stricter requirements than GTEST_HAS_DEATH_TEST +// can write their own equivalent of EXPECT_DEATH_IF_SUPPORTED and +// ASSERT_DEATH_IF_SUPPORTED. +// +// Parameters: +// statement - A statement that a macro such as EXPECT_DEATH would test +// for program termination. This macro has to make sure this +// statement is compiled but not executed, to ensure that +// EXPECT_DEATH_IF_SUPPORTED compiles with a certain +// parameter if and only if EXPECT_DEATH compiles with it. +// regex - A regex that a macro such as EXPECT_DEATH would use to test +// the output of statement. This parameter has to be +// compiled but not evaluated by this macro, to ensure that +// this macro only accepts expressions that a macro such as +// EXPECT_DEATH would accept. +// terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED +// and a return statement for ASSERT_DEATH_IF_SUPPORTED. +// This ensures that ASSERT_DEATH_IF_SUPPORTED will not +// compile inside functions where ASSERT_DEATH doesn't +// compile. +// +// The branch that has an always false condition is used to ensure that +// statement and regex are compiled (and thus syntactically correct) but +// never executed. The unreachable code macro protects the terminator +// statement from generating an 'unreachable code' warning in case +// statement unconditionally returns or throws. The Message constructor at +// the end allows the syntax of streaming additional messages into the +// macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. +#define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, terminator) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::AlwaysTrue()) { \ + GTEST_LOG_(WARNING) << "Death tests are not supported on this platform.\n" \ + << "Statement '" #statement "' cannot be verified."; \ + } else if (::testing::internal::AlwaysFalse()) { \ + ::testing::internal::RE::PartialMatch(".*", (regex)); \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + terminator; \ + } else \ + ::testing::Message() + +// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and +// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if +// death tests are supported; otherwise they just issue a warning. This is +// useful when you are combining death test assertions with normal test +// assertions in one test. +#if GTEST_HAS_DEATH_TEST +#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ + EXPECT_DEATH(statement, regex) +#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ + ASSERT_DEATH(statement, regex) +#else +#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ + GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, ) +#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ + GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, return) +#endif + +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ diff --git a/test/gtest/include/gtest/gtest-matchers.h b/test/gtest/include/gtest/gtest-matchers.h new file mode 100644 index 0000000000..bffa00c533 --- /dev/null +++ b/test/gtest/include/gtest/gtest-matchers.h @@ -0,0 +1,956 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This file implements just enough of the matcher interface to allow +// EXPECT_DEATH and friends to accept a matcher argument. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ + +#include +#include +#include +#include +#include + +#include "gtest/gtest-printers.h" +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-port.h" + +// MSVC warning C5046 is new as of VS2017 version 15.8. +#if defined(_MSC_VER) && _MSC_VER >= 1915 +#define GTEST_MAYBE_5046_ 5046 +#else +#define GTEST_MAYBE_5046_ +#endif + +GTEST_DISABLE_MSC_WARNINGS_PUSH_( + 4251 GTEST_MAYBE_5046_ /* class A needs to have dll-interface to be used by + clients of class B */ + /* Symbol involving type with internal linkage not defined */) + +namespace testing { + +// To implement a matcher Foo for type T, define: +// 1. a class FooMatcherMatcher that implements the matcher interface: +// using is_gtest_matcher = void; +// bool MatchAndExplain(const T&, std::ostream*); +// (MatchResultListener* can also be used instead of std::ostream*) +// void DescribeTo(std::ostream*); +// void DescribeNegationTo(std::ostream*); +// +// 2. a factory function that creates a Matcher object from a +// FooMatcherMatcher. + +class MatchResultListener { + public: + // Creates a listener object with the given underlying ostream. The + // listener does not own the ostream, and does not dereference it + // in the constructor or destructor. + explicit MatchResultListener(::std::ostream* os) : stream_(os) {} + virtual ~MatchResultListener() = 0; // Makes this class abstract. + + // Streams x to the underlying ostream; does nothing if the ostream + // is NULL. + template + MatchResultListener& operator<<(const T& x) { + if (stream_ != nullptr) *stream_ << x; + return *this; + } + + // Returns the underlying ostream. + ::std::ostream* stream() { return stream_; } + + // Returns true if and only if the listener is interested in an explanation + // of the match result. A matcher's MatchAndExplain() method can use + // this information to avoid generating the explanation when no one + // intends to hear it. + bool IsInterested() const { return stream_ != nullptr; } + + private: + ::std::ostream* const stream_; + + MatchResultListener(const MatchResultListener&) = delete; + MatchResultListener& operator=(const MatchResultListener&) = delete; +}; + +inline MatchResultListener::~MatchResultListener() {} + +// An instance of a subclass of this knows how to describe itself as a +// matcher. +class GTEST_API_ MatcherDescriberInterface { + public: + virtual ~MatcherDescriberInterface() {} + + // Describes this matcher to an ostream. The function should print + // a verb phrase that describes the property a value matching this + // matcher should have. The subject of the verb phrase is the value + // being matched. For example, the DescribeTo() method of the Gt(7) + // matcher prints "is greater than 7". + virtual void DescribeTo(::std::ostream* os) const = 0; + + // Describes the negation of this matcher to an ostream. For + // example, if the description of this matcher is "is greater than + // 7", the negated description could be "is not greater than 7". + // You are not required to override this when implementing + // MatcherInterface, but it is highly advised so that your matcher + // can produce good error messages. + virtual void DescribeNegationTo(::std::ostream* os) const { + *os << "not ("; + DescribeTo(os); + *os << ")"; + } +}; + +// The implementation of a matcher. +template +class MatcherInterface : public MatcherDescriberInterface { + public: + // Returns true if and only if the matcher matches x; also explains the + // match result to 'listener' if necessary (see the next paragraph), in + // the form of a non-restrictive relative clause ("which ...", + // "whose ...", etc) that describes x. For example, the + // MatchAndExplain() method of the Pointee(...) matcher should + // generate an explanation like "which points to ...". + // + // Implementations of MatchAndExplain() should add an explanation of + // the match result *if and only if* they can provide additional + // information that's not already present (or not obvious) in the + // print-out of x and the matcher's description. Whether the match + // succeeds is not a factor in deciding whether an explanation is + // needed, as sometimes the caller needs to print a failure message + // when the match succeeds (e.g. when the matcher is used inside + // Not()). + // + // For example, a "has at least 10 elements" matcher should explain + // what the actual element count is, regardless of the match result, + // as it is useful information to the reader; on the other hand, an + // "is empty" matcher probably only needs to explain what the actual + // size is when the match fails, as it's redundant to say that the + // size is 0 when the value is already known to be empty. + // + // You should override this method when defining a new matcher. + // + // It's the responsibility of the caller (Google Test) to guarantee + // that 'listener' is not NULL. This helps to simplify a matcher's + // implementation when it doesn't care about the performance, as it + // can talk to 'listener' without checking its validity first. + // However, in order to implement dummy listeners efficiently, + // listener->stream() may be NULL. + virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0; + + // Inherits these methods from MatcherDescriberInterface: + // virtual void DescribeTo(::std::ostream* os) const = 0; + // virtual void DescribeNegationTo(::std::ostream* os) const; +}; + +namespace internal { + +struct AnyEq { + template + bool operator()(const A& a, const B& b) const { + return a == b; + } +}; +struct AnyNe { + template + bool operator()(const A& a, const B& b) const { + return a != b; + } +}; +struct AnyLt { + template + bool operator()(const A& a, const B& b) const { + return a < b; + } +}; +struct AnyGt { + template + bool operator()(const A& a, const B& b) const { + return a > b; + } +}; +struct AnyLe { + template + bool operator()(const A& a, const B& b) const { + return a <= b; + } +}; +struct AnyGe { + template + bool operator()(const A& a, const B& b) const { + return a >= b; + } +}; + +// A match result listener that ignores the explanation. +class DummyMatchResultListener : public MatchResultListener { + public: + DummyMatchResultListener() : MatchResultListener(nullptr) {} + + private: + DummyMatchResultListener(const DummyMatchResultListener&) = delete; + DummyMatchResultListener& operator=(const DummyMatchResultListener&) = delete; +}; + +// A match result listener that forwards the explanation to a given +// ostream. The difference between this and MatchResultListener is +// that the former is concrete. +class StreamMatchResultListener : public MatchResultListener { + public: + explicit StreamMatchResultListener(::std::ostream* os) + : MatchResultListener(os) {} + + private: + StreamMatchResultListener(const StreamMatchResultListener&) = delete; + StreamMatchResultListener& operator=(const StreamMatchResultListener&) = + delete; +}; + +struct SharedPayloadBase { + std::atomic ref{1}; + void Ref() { ref.fetch_add(1, std::memory_order_relaxed); } + bool Unref() { return ref.fetch_sub(1, std::memory_order_acq_rel) == 1; } +}; + +template +struct SharedPayload : SharedPayloadBase { + explicit SharedPayload(const T& v) : value(v) {} + explicit SharedPayload(T&& v) : value(std::move(v)) {} + + static void Destroy(SharedPayloadBase* shared) { + delete static_cast(shared); + } + + T value; +}; + +// An internal class for implementing Matcher, which will derive +// from it. We put functionalities common to all Matcher +// specializations here to avoid code duplication. +template +class MatcherBase : private MatcherDescriberInterface { + public: + // Returns true if and only if the matcher matches x; also explains the + // match result to 'listener'. + bool MatchAndExplain(const T& x, MatchResultListener* listener) const { + GTEST_CHECK_(vtable_ != nullptr); + return vtable_->match_and_explain(*this, x, listener); + } + + // Returns true if and only if this matcher matches x. + bool Matches(const T& x) const { + DummyMatchResultListener dummy; + return MatchAndExplain(x, &dummy); + } + + // Describes this matcher to an ostream. + void DescribeTo(::std::ostream* os) const final { + GTEST_CHECK_(vtable_ != nullptr); + vtable_->describe(*this, os, false); + } + + // Describes the negation of this matcher to an ostream. + void DescribeNegationTo(::std::ostream* os) const final { + GTEST_CHECK_(vtable_ != nullptr); + vtable_->describe(*this, os, true); + } + + // Explains why x matches, or doesn't match, the matcher. + void ExplainMatchResultTo(const T& x, ::std::ostream* os) const { + StreamMatchResultListener listener(os); + MatchAndExplain(x, &listener); + } + + // Returns the describer for this matcher object; retains ownership + // of the describer, which is only guaranteed to be alive when + // this matcher object is alive. + const MatcherDescriberInterface* GetDescriber() const { + if (vtable_ == nullptr) return nullptr; + return vtable_->get_describer(*this); + } + + protected: + MatcherBase() : vtable_(nullptr), buffer_() {} + + // Constructs a matcher from its implementation. + template + explicit MatcherBase(const MatcherInterface* impl) + : vtable_(nullptr), buffer_() { + Init(impl); + } + + template ::type::is_gtest_matcher> + MatcherBase(M&& m) : vtable_(nullptr), buffer_() { // NOLINT + Init(std::forward(m)); + } + + MatcherBase(const MatcherBase& other) + : vtable_(other.vtable_), buffer_(other.buffer_) { + if (IsShared()) buffer_.shared->Ref(); + } + + MatcherBase& operator=(const MatcherBase& other) { + if (this == &other) return *this; + Destroy(); + vtable_ = other.vtable_; + buffer_ = other.buffer_; + if (IsShared()) buffer_.shared->Ref(); + return *this; + } + + MatcherBase(MatcherBase&& other) + : vtable_(other.vtable_), buffer_(other.buffer_) { + other.vtable_ = nullptr; + } + + MatcherBase& operator=(MatcherBase&& other) { + if (this == &other) return *this; + Destroy(); + vtable_ = other.vtable_; + buffer_ = other.buffer_; + other.vtable_ = nullptr; + return *this; + } + + ~MatcherBase() override { Destroy(); } + + private: + struct VTable { + bool (*match_and_explain)(const MatcherBase&, const T&, + MatchResultListener*); + void (*describe)(const MatcherBase&, std::ostream*, bool negation); + // Returns the captured object if it implements the interface, otherwise + // returns the MatcherBase itself. + const MatcherDescriberInterface* (*get_describer)(const MatcherBase&); + // Called on shared instances when the reference count reaches 0. + void (*shared_destroy)(SharedPayloadBase*); + }; + + bool IsShared() const { + return vtable_ != nullptr && vtable_->shared_destroy != nullptr; + } + + // If the implementation uses a listener, call that. + template + static auto MatchAndExplainImpl(const MatcherBase& m, const T& value, + MatchResultListener* listener) + -> decltype(P::Get(m).MatchAndExplain(value, listener->stream())) { + return P::Get(m).MatchAndExplain(value, listener->stream()); + } + + template + static auto MatchAndExplainImpl(const MatcherBase& m, const T& value, + MatchResultListener* listener) + -> decltype(P::Get(m).MatchAndExplain(value, listener)) { + return P::Get(m).MatchAndExplain(value, listener); + } + + template + static void DescribeImpl(const MatcherBase& m, std::ostream* os, + bool negation) { + if (negation) { + P::Get(m).DescribeNegationTo(os); + } else { + P::Get(m).DescribeTo(os); + } + } + + template + static const MatcherDescriberInterface* GetDescriberImpl( + const MatcherBase& m) { + // If the impl is a MatcherDescriberInterface, then return it. + // Otherwise use MatcherBase itself. + // This allows us to implement the GetDescriber() function without support + // from the impl, but some users really want to get their impl back when + // they call GetDescriber(). + // We use std::get on a tuple as a workaround of not having `if constexpr`. + return std::get<( + std::is_convertible::value + ? 1 + : 0)>(std::make_tuple(&m, &P::Get(m))); + } + + template + const VTable* GetVTable() { + static constexpr VTable kVTable = {&MatchAndExplainImpl

, + &DescribeImpl

, &GetDescriberImpl

, + P::shared_destroy}; + return &kVTable; + } + + union Buffer { + // Add some types to give Buffer some common alignment/size use cases. + void* ptr; + double d; + int64_t i; + // And add one for the out-of-line cases. + SharedPayloadBase* shared; + }; + + void Destroy() { + if (IsShared() && buffer_.shared->Unref()) { + vtable_->shared_destroy(buffer_.shared); + } + } + + template + static constexpr bool IsInlined() { + return sizeof(M) <= sizeof(Buffer) && alignof(M) <= alignof(Buffer) && + std::is_trivially_copy_constructible::value && + std::is_trivially_destructible::value; + } + + template ()> + struct ValuePolicy { + static const M& Get(const MatcherBase& m) { + // When inlined along with Init, need to be explicit to avoid violating + // strict aliasing rules. + const M* ptr = + static_cast(static_cast(&m.buffer_)); + return *ptr; + } + static void Init(MatcherBase& m, M impl) { + ::new (static_cast(&m.buffer_)) M(impl); + } + static constexpr auto shared_destroy = nullptr; + }; + + template + struct ValuePolicy { + using Shared = SharedPayload; + static const M& Get(const MatcherBase& m) { + return static_cast(m.buffer_.shared)->value; + } + template + static void Init(MatcherBase& m, Arg&& arg) { + m.buffer_.shared = new Shared(std::forward(arg)); + } + static constexpr auto shared_destroy = &Shared::Destroy; + }; + + template + struct ValuePolicy*, B> { + using M = const MatcherInterface; + using Shared = SharedPayload>; + static const M& Get(const MatcherBase& m) { + return *static_cast(m.buffer_.shared)->value; + } + static void Init(MatcherBase& m, M* impl) { + m.buffer_.shared = new Shared(std::unique_ptr(impl)); + } + + static constexpr auto shared_destroy = &Shared::Destroy; + }; + + template + void Init(M&& m) { + using MM = typename std::decay::type; + using Policy = ValuePolicy; + vtable_ = GetVTable(); + Policy::Init(*this, std::forward(m)); + } + + const VTable* vtable_; + Buffer buffer_; +}; + +} // namespace internal + +// A Matcher is a copyable and IMMUTABLE (except by assignment) +// object that can check whether a value of type T matches. The +// implementation of Matcher is just a std::shared_ptr to const +// MatcherInterface. Don't inherit from Matcher! +template +class Matcher : public internal::MatcherBase { + public: + // Constructs a null matcher. Needed for storing Matcher objects in STL + // containers. A default-constructed matcher is not yet initialized. You + // cannot use it until a valid value has been assigned to it. + explicit Matcher() {} // NOLINT + + // Constructs a matcher from its implementation. + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + + template + explicit Matcher( + const MatcherInterface* impl, + typename std::enable_if::value>::type* = + nullptr) + : internal::MatcherBase(impl) {} + + template ::type::is_gtest_matcher> + Matcher(M&& m) : internal::MatcherBase(std::forward(m)) {} // NOLINT + + // Implicit constructor here allows people to write + // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes + Matcher(T value); // NOLINT +}; + +// The following two specializations allow the user to write str +// instead of Eq(str) and "foo" instead of Eq("foo") when a std::string +// matcher is expected. +template <> +class GTEST_API_ Matcher + : public internal::MatcherBase { + public: + Matcher() {} + + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + + template ::type::is_gtest_matcher> + Matcher(M&& m) // NOLINT + : internal::MatcherBase(std::forward(m)) {} + + // Allows the user to write str instead of Eq(str) sometimes, where + // str is a std::string object. + Matcher(const std::string& s); // NOLINT + + // Allows the user to write "foo" instead of Eq("foo") sometimes. + Matcher(const char* s); // NOLINT +}; + +template <> +class GTEST_API_ Matcher + : public internal::MatcherBase { + public: + Matcher() {} + + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + + template ::type::is_gtest_matcher> + Matcher(M&& m) // NOLINT + : internal::MatcherBase(std::forward(m)) {} + + // Allows the user to write str instead of Eq(str) sometimes, where + // str is a string object. + Matcher(const std::string& s); // NOLINT + + // Allows the user to write "foo" instead of Eq("foo") sometimes. + Matcher(const char* s); // NOLINT +}; + +#if GTEST_INTERNAL_HAS_STRING_VIEW +// The following two specializations allow the user to write str +// instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view +// matcher is expected. +template <> +class GTEST_API_ Matcher + : public internal::MatcherBase { + public: + Matcher() {} + + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + + template ::type::is_gtest_matcher> + Matcher(M&& m) // NOLINT + : internal::MatcherBase(std::forward(m)) { + } + + // Allows the user to write str instead of Eq(str) sometimes, where + // str is a std::string object. + Matcher(const std::string& s); // NOLINT + + // Allows the user to write "foo" instead of Eq("foo") sometimes. + Matcher(const char* s); // NOLINT + + // Allows the user to pass absl::string_views or std::string_views directly. + Matcher(internal::StringView s); // NOLINT +}; + +template <> +class GTEST_API_ Matcher + : public internal::MatcherBase { + public: + Matcher() {} + + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + explicit Matcher(const MatcherInterface* impl) + : internal::MatcherBase(impl) {} + + template ::type::is_gtest_matcher> + Matcher(M&& m) // NOLINT + : internal::MatcherBase(std::forward(m)) {} + + // Allows the user to write str instead of Eq(str) sometimes, where + // str is a std::string object. + Matcher(const std::string& s); // NOLINT + + // Allows the user to write "foo" instead of Eq("foo") sometimes. + Matcher(const char* s); // NOLINT + + // Allows the user to pass absl::string_views or std::string_views directly. + Matcher(internal::StringView s); // NOLINT +}; +#endif // GTEST_INTERNAL_HAS_STRING_VIEW + +// Prints a matcher in a human-readable format. +template +std::ostream& operator<<(std::ostream& os, const Matcher& matcher) { + matcher.DescribeTo(&os); + return os; +} + +// The PolymorphicMatcher class template makes it easy to implement a +// polymorphic matcher (i.e. a matcher that can match values of more +// than one type, e.g. Eq(n) and NotNull()). +// +// To define a polymorphic matcher, a user should provide an Impl +// class that has a DescribeTo() method and a DescribeNegationTo() +// method, and define a member function (or member function template) +// +// bool MatchAndExplain(const Value& value, +// MatchResultListener* listener) const; +// +// See the definition of NotNull() for a complete example. +template +class PolymorphicMatcher { + public: + explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {} + + // Returns a mutable reference to the underlying matcher + // implementation object. + Impl& mutable_impl() { return impl_; } + + // Returns an immutable reference to the underlying matcher + // implementation object. + const Impl& impl() const { return impl_; } + + template + operator Matcher() const { + return Matcher(new MonomorphicImpl(impl_)); + } + + private: + template + class MonomorphicImpl : public MatcherInterface { + public: + explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} + + void DescribeTo(::std::ostream* os) const override { impl_.DescribeTo(os); } + + void DescribeNegationTo(::std::ostream* os) const override { + impl_.DescribeNegationTo(os); + } + + bool MatchAndExplain(T x, MatchResultListener* listener) const override { + return impl_.MatchAndExplain(x, listener); + } + + private: + const Impl impl_; + }; + + Impl impl_; +}; + +// Creates a matcher from its implementation. +// DEPRECATED: Especially in the generic code, prefer: +// Matcher(new MyMatcherImpl(...)); +// +// MakeMatcher may create a Matcher that accepts its argument by value, which +// leads to unnecessary copies & lack of support for non-copyable types. +template +inline Matcher MakeMatcher(const MatcherInterface* impl) { + return Matcher(impl); +} + +// Creates a polymorphic matcher from its implementation. This is +// easier to use than the PolymorphicMatcher constructor as it +// doesn't require you to explicitly write the template argument, e.g. +// +// MakePolymorphicMatcher(foo); +// vs +// PolymorphicMatcher(foo); +template +inline PolymorphicMatcher MakePolymorphicMatcher(const Impl& impl) { + return PolymorphicMatcher(impl); +} + +namespace internal { +// Implements a matcher that compares a given value with a +// pre-supplied value using one of the ==, <=, <, etc, operators. The +// two values being compared don't have to have the same type. +// +// The matcher defined here is polymorphic (for example, Eq(5) can be +// used to match an int, a short, a double, etc). Therefore we use +// a template type conversion operator in the implementation. +// +// The following template definition assumes that the Rhs parameter is +// a "bare" type (i.e. neither 'const T' nor 'T&'). +template +class ComparisonBase { + public: + explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {} + + using is_gtest_matcher = void; + + template + bool MatchAndExplain(const Lhs& lhs, std::ostream*) const { + return Op()(lhs, Unwrap(rhs_)); + } + void DescribeTo(std::ostream* os) const { + *os << D::Desc() << " "; + UniversalPrint(Unwrap(rhs_), os); + } + void DescribeNegationTo(std::ostream* os) const { + *os << D::NegatedDesc() << " "; + UniversalPrint(Unwrap(rhs_), os); + } + + private: + template + static const T& Unwrap(const T& v) { + return v; + } + template + static const T& Unwrap(std::reference_wrapper v) { + return v; + } + + Rhs rhs_; +}; + +template +class EqMatcher : public ComparisonBase, Rhs, AnyEq> { + public: + explicit EqMatcher(const Rhs& rhs) + : ComparisonBase, Rhs, AnyEq>(rhs) {} + static const char* Desc() { return "is equal to"; } + static const char* NegatedDesc() { return "isn't equal to"; } +}; +template +class NeMatcher : public ComparisonBase, Rhs, AnyNe> { + public: + explicit NeMatcher(const Rhs& rhs) + : ComparisonBase, Rhs, AnyNe>(rhs) {} + static const char* Desc() { return "isn't equal to"; } + static const char* NegatedDesc() { return "is equal to"; } +}; +template +class LtMatcher : public ComparisonBase, Rhs, AnyLt> { + public: + explicit LtMatcher(const Rhs& rhs) + : ComparisonBase, Rhs, AnyLt>(rhs) {} + static const char* Desc() { return "is <"; } + static const char* NegatedDesc() { return "isn't <"; } +}; +template +class GtMatcher : public ComparisonBase, Rhs, AnyGt> { + public: + explicit GtMatcher(const Rhs& rhs) + : ComparisonBase, Rhs, AnyGt>(rhs) {} + static const char* Desc() { return "is >"; } + static const char* NegatedDesc() { return "isn't >"; } +}; +template +class LeMatcher : public ComparisonBase, Rhs, AnyLe> { + public: + explicit LeMatcher(const Rhs& rhs) + : ComparisonBase, Rhs, AnyLe>(rhs) {} + static const char* Desc() { return "is <="; } + static const char* NegatedDesc() { return "isn't <="; } +}; +template +class GeMatcher : public ComparisonBase, Rhs, AnyGe> { + public: + explicit GeMatcher(const Rhs& rhs) + : ComparisonBase, Rhs, AnyGe>(rhs) {} + static const char* Desc() { return "is >="; } + static const char* NegatedDesc() { return "isn't >="; } +}; + +template ::value>::type> +using StringLike = T; + +// Implements polymorphic matchers MatchesRegex(regex) and +// ContainsRegex(regex), which can be used as a Matcher as long as +// T can be converted to a string. +class MatchesRegexMatcher { + public: + MatchesRegexMatcher(const RE* regex, bool full_match) + : regex_(regex), full_match_(full_match) {} + +#if GTEST_INTERNAL_HAS_STRING_VIEW + bool MatchAndExplain(const internal::StringView& s, + MatchResultListener* listener) const { + return MatchAndExplain(std::string(s), listener); + } +#endif // GTEST_INTERNAL_HAS_STRING_VIEW + + // Accepts pointer types, particularly: + // const char* + // char* + // const wchar_t* + // wchar_t* + template + bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { + return s != nullptr && MatchAndExplain(std::string(s), listener); + } + + // Matches anything that can convert to std::string. + // + // This is a template, not just a plain function with const std::string&, + // because absl::string_view has some interfering non-explicit constructors. + template + bool MatchAndExplain(const MatcheeStringType& s, + MatchResultListener* /* listener */) const { + const std::string& s2(s); + return full_match_ ? RE::FullMatch(s2, *regex_) + : RE::PartialMatch(s2, *regex_); + } + + void DescribeTo(::std::ostream* os) const { + *os << (full_match_ ? "matches" : "contains") << " regular expression "; + UniversalPrinter::Print(regex_->pattern(), os); + } + + void DescribeNegationTo(::std::ostream* os) const { + *os << "doesn't " << (full_match_ ? "match" : "contain") + << " regular expression "; + UniversalPrinter::Print(regex_->pattern(), os); + } + + private: + const std::shared_ptr regex_; + const bool full_match_; +}; +} // namespace internal + +// Matches a string that fully matches regular expression 'regex'. +// The matcher takes ownership of 'regex'. +inline PolymorphicMatcher MatchesRegex( + const internal::RE* regex) { + return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); +} +template +PolymorphicMatcher MatchesRegex( + const internal::StringLike& regex) { + return MatchesRegex(new internal::RE(std::string(regex))); +} + +// Matches a string that contains regular expression 'regex'. +// The matcher takes ownership of 'regex'. +inline PolymorphicMatcher ContainsRegex( + const internal::RE* regex) { + return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); +} +template +PolymorphicMatcher ContainsRegex( + const internal::StringLike& regex) { + return ContainsRegex(new internal::RE(std::string(regex))); +} + +// Creates a polymorphic matcher that matches anything equal to x. +// Note: if the parameter of Eq() were declared as const T&, Eq("foo") +// wouldn't compile. +template +inline internal::EqMatcher Eq(T x) { + return internal::EqMatcher(x); +} + +// Constructs a Matcher from a 'value' of type T. The constructed +// matcher matches any value that's equal to 'value'. +template +Matcher::Matcher(T value) { + *this = Eq(value); +} + +// Creates a monomorphic matcher that matches anything with type Lhs +// and equal to rhs. A user may need to use this instead of Eq(...) +// in order to resolve an overloading ambiguity. +// +// TypedEq(x) is just a convenient short-hand for Matcher(Eq(x)) +// or Matcher(x), but more readable than the latter. +// +// We could define similar monomorphic matchers for other comparison +// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do +// it yet as those are used much less than Eq() in practice. A user +// can always write Matcher(Lt(5)) to be explicit about the type, +// for example. +template +inline Matcher TypedEq(const Rhs& rhs) { + return Eq(rhs); +} + +// Creates a polymorphic matcher that matches anything >= x. +template +inline internal::GeMatcher Ge(Rhs x) { + return internal::GeMatcher(x); +} + +// Creates a polymorphic matcher that matches anything > x. +template +inline internal::GtMatcher Gt(Rhs x) { + return internal::GtMatcher(x); +} + +// Creates a polymorphic matcher that matches anything <= x. +template +inline internal::LeMatcher Le(Rhs x) { + return internal::LeMatcher(x); +} + +// Creates a polymorphic matcher that matches anything < x. +template +inline internal::LtMatcher Lt(Rhs x) { + return internal::LtMatcher(x); +} + +// Creates a polymorphic matcher that matches anything != x. +template +inline internal::NeMatcher Ne(Rhs x) { + return internal::NeMatcher(x); +} +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 5046 + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MATCHERS_H_ diff --git a/test/gtest/include/gtest/gtest-message.h b/test/gtest/include/gtest/gtest-message.h new file mode 100644 index 0000000000..6c8bf90009 --- /dev/null +++ b/test/gtest/include/gtest/gtest-message.h @@ -0,0 +1,218 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file defines the Message class. +// +// IMPORTANT NOTE: Due to limitation of the C++ language, we have to +// leave some internal implementation details in this header file. +// They are clearly marked by comments like this: +// +// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +// +// Such code is NOT meant to be used by a user directly, and is subject +// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user +// program! + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ + +#include +#include +#include + +#include "gtest/internal/gtest-port.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +// Ensures that there is at least one operator<< in the global namespace. +// See Message& operator<<(...) below for why. +void operator<<(const testing::internal::Secret&, int); + +namespace testing { + +// The Message class works like an ostream repeater. +// +// Typical usage: +// +// 1. You stream a bunch of values to a Message object. +// It will remember the text in a stringstream. +// 2. Then you stream the Message object to an ostream. +// This causes the text in the Message to be streamed +// to the ostream. +// +// For example; +// +// testing::Message foo; +// foo << 1 << " != " << 2; +// std::cout << foo; +// +// will print "1 != 2". +// +// Message is not intended to be inherited from. In particular, its +// destructor is not virtual. +// +// Note that stringstream behaves differently in gcc and in MSVC. You +// can stream a NULL char pointer to it in the former, but not in the +// latter (it causes an access violation if you do). The Message +// class hides this difference by treating a NULL char pointer as +// "(null)". +class GTEST_API_ Message { + private: + // The type of basic IO manipulators (endl, ends, and flush) for + // narrow streams. + typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&); + + public: + // Constructs an empty Message. + Message(); + + // Copy constructor. + Message(const Message& msg) : ss_(new ::std::stringstream) { // NOLINT + *ss_ << msg.GetString(); + } + + // Constructs a Message from a C-string. + explicit Message(const char* str) : ss_(new ::std::stringstream) { + *ss_ << str; + } + + // Streams a non-pointer value to this object. + template + inline Message& operator<<(const T& val) { + // Some libraries overload << for STL containers. These + // overloads are defined in the global namespace instead of ::std. + // + // C++'s symbol lookup rule (i.e. Koenig lookup) says that these + // overloads are visible in either the std namespace or the global + // namespace, but not other namespaces, including the testing + // namespace which Google Test's Message class is in. + // + // To allow STL containers (and other types that has a << operator + // defined in the global namespace) to be used in Google Test + // assertions, testing::Message must access the custom << operator + // from the global namespace. With this using declaration, + // overloads of << defined in the global namespace and those + // visible via Koenig lookup are both exposed in this function. + using ::operator<<; + *ss_ << val; + return *this; + } + + // Streams a pointer value to this object. + // + // This function is an overload of the previous one. When you + // stream a pointer to a Message, this definition will be used as it + // is more specialized. (The C++ Standard, section + // [temp.func.order].) If you stream a non-pointer, then the + // previous definition will be used. + // + // The reason for this overload is that streaming a NULL pointer to + // ostream is undefined behavior. Depending on the compiler, you + // may get "0", "(nil)", "(null)", or an access violation. To + // ensure consistent result across compilers, we always treat NULL + // as "(null)". + template + inline Message& operator<<(T* const& pointer) { // NOLINT + if (pointer == nullptr) { + *ss_ << "(null)"; + } else { + *ss_ << pointer; + } + return *this; + } + + // Since the basic IO manipulators are overloaded for both narrow + // and wide streams, we have to provide this specialized definition + // of operator <<, even though its body is the same as the + // templatized version above. Without this definition, streaming + // endl or other basic IO manipulators to Message will confuse the + // compiler. + Message& operator<<(BasicNarrowIoManip val) { + *ss_ << val; + return *this; + } + + // Instead of 1/0, we want to see true/false for bool values. + Message& operator<<(bool b) { return *this << (b ? "true" : "false"); } + + // These two overloads allow streaming a wide C string to a Message + // using the UTF-8 encoding. + Message& operator<<(const wchar_t* wide_c_str); + Message& operator<<(wchar_t* wide_c_str); + +#if GTEST_HAS_STD_WSTRING + // Converts the given wide string to a narrow string using the UTF-8 + // encoding, and streams the result to this Message object. + Message& operator<<(const ::std::wstring& wstr); +#endif // GTEST_HAS_STD_WSTRING + + // Gets the text streamed to this object so far as an std::string. + // Each '\0' character in the buffer is replaced with "\\0". + // + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + std::string GetString() const; + + private: + // We'll hold the text streamed to this object here. + const std::unique_ptr< ::std::stringstream> ss_; + + // We declare (but don't implement) this to prevent the compiler + // from implementing the assignment operator. + void operator=(const Message&); +}; + +// Streams a Message to an ostream. +inline std::ostream& operator<<(std::ostream& os, const Message& sb) { + return os << sb.GetString(); +} + +namespace internal { + +// Converts a streamable value to an std::string. A NULL pointer is +// converted to "(null)". When the input value is a ::string, +// ::std::string, ::wstring, or ::std::wstring object, each NUL +// character in it is replaced with "\\0". +template +std::string StreamableToString(const T& streamable) { + return (Message() << streamable).GetString(); +} + +} // namespace internal +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ diff --git a/test/gtest/include/gtest/gtest-param-test.h b/test/gtest/include/gtest/gtest-param-test.h new file mode 100644 index 0000000000..b55119ac62 --- /dev/null +++ b/test/gtest/include/gtest/gtest-param-test.h @@ -0,0 +1,510 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Macros and functions for implementing parameterized tests +// in Google C++ Testing and Mocking Framework (Google Test) + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ + +// Value-parameterized tests allow you to test your code with different +// parameters without writing multiple copies of the same test. +// +// Here is how you use value-parameterized tests: + +#if 0 + +// To write value-parameterized tests, first you should define a fixture +// class. It is usually derived from testing::TestWithParam (see below for +// another inheritance scheme that's sometimes useful in more complicated +// class hierarchies), where the type of your parameter values. +// TestWithParam is itself derived from testing::Test. T can be any +// copyable type. If it's a raw pointer, you are responsible for managing the +// lifespan of the pointed values. + +class FooTest : public ::testing::TestWithParam { + // You can implement all the usual class fixture members here. +}; + +// Then, use the TEST_P macro to define as many parameterized tests +// for this fixture as you want. The _P suffix is for "parameterized" +// or "pattern", whichever you prefer to think. + +TEST_P(FooTest, DoesBlah) { + // Inside a test, access the test parameter with the GetParam() method + // of the TestWithParam class: + EXPECT_TRUE(foo.Blah(GetParam())); + ... +} + +TEST_P(FooTest, HasBlahBlah) { + ... +} + +// Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test +// case with any set of parameters you want. Google Test defines a number +// of functions for generating test parameters. They return what we call +// (surprise!) parameter generators. Here is a summary of them, which +// are all in the testing namespace: +// +// +// Range(begin, end [, step]) - Yields values {begin, begin+step, +// begin+step+step, ...}. The values do not +// include end. step defaults to 1. +// Values(v1, v2, ..., vN) - Yields values {v1, v2, ..., vN}. +// ValuesIn(container) - Yields values from a C-style array, an STL +// ValuesIn(begin,end) container, or an iterator range [begin, end). +// Bool() - Yields sequence {false, true}. +// Combine(g1, g2, ..., gN) - Yields all combinations (the Cartesian product +// for the math savvy) of the values generated +// by the N generators. +// +// For more details, see comments at the definitions of these functions below +// in this file. +// +// The following statement will instantiate tests from the FooTest test suite +// each with parameter values "meeny", "miny", and "moe". + +INSTANTIATE_TEST_SUITE_P(InstantiationName, + FooTest, + Values("meeny", "miny", "moe")); + +// To distinguish different instances of the pattern, (yes, you +// can instantiate it more than once) the first argument to the +// INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the +// actual test suite name. Remember to pick unique prefixes for different +// instantiations. The tests from the instantiation above will have +// these names: +// +// * InstantiationName/FooTest.DoesBlah/0 for "meeny" +// * InstantiationName/FooTest.DoesBlah/1 for "miny" +// * InstantiationName/FooTest.DoesBlah/2 for "moe" +// * InstantiationName/FooTest.HasBlahBlah/0 for "meeny" +// * InstantiationName/FooTest.HasBlahBlah/1 for "miny" +// * InstantiationName/FooTest.HasBlahBlah/2 for "moe" +// +// You can use these names in --gtest_filter. +// +// This statement will instantiate all tests from FooTest again, each +// with parameter values "cat" and "dog": + +const char* pets[] = {"cat", "dog"}; +INSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets)); + +// The tests from the instantiation above will have these names: +// +// * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat" +// * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog" +// * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat" +// * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog" +// +// Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests +// in the given test suite, whether their definitions come before or +// AFTER the INSTANTIATE_TEST_SUITE_P statement. +// +// Please also note that generator expressions (including parameters to the +// generators) are evaluated in InitGoogleTest(), after main() has started. +// This allows the user on one hand, to adjust generator parameters in order +// to dynamically determine a set of tests to run and on the other hand, +// give the user a chance to inspect the generated tests with Google Test +// reflection API before RUN_ALL_TESTS() is executed. +// +// You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc +// for more examples. +// +// In the future, we plan to publish the API for defining new parameter +// generators. But for now this interface remains part of the internal +// implementation and is subject to change. +// +// +// A parameterized test fixture must be derived from testing::Test and from +// testing::WithParamInterface, where T is the type of the parameter +// values. Inheriting from TestWithParam satisfies that requirement because +// TestWithParam inherits from both Test and WithParamInterface. In more +// complicated hierarchies, however, it is occasionally useful to inherit +// separately from Test and WithParamInterface. For example: + +class BaseTest : public ::testing::Test { + // You can inherit all the usual members for a non-parameterized test + // fixture here. +}; + +class DerivedTest : public BaseTest, public ::testing::WithParamInterface { + // The usual test fixture members go here too. +}; + +TEST_F(BaseTest, HasFoo) { + // This is an ordinary non-parameterized test. +} + +TEST_P(DerivedTest, DoesBlah) { + // GetParam works just the same here as if you inherit from TestWithParam. + EXPECT_TRUE(foo.Blah(GetParam())); +} + +#endif // 0 + +#include +#include + +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-param-util.h" +#include "gtest/internal/gtest-port.h" + +namespace testing { + +// Functions producing parameter generators. +// +// Google Test uses these generators to produce parameters for value- +// parameterized tests. When a parameterized test suite is instantiated +// with a particular generator, Google Test creates and runs tests +// for each element in the sequence produced by the generator. +// +// In the following sample, tests from test suite FooTest are instantiated +// each three times with parameter values 3, 5, and 8: +// +// class FooTest : public TestWithParam { ... }; +// +// TEST_P(FooTest, TestThis) { +// } +// TEST_P(FooTest, TestThat) { +// } +// INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8)); +// + +// Range() returns generators providing sequences of values in a range. +// +// Synopsis: +// Range(start, end) +// - returns a generator producing a sequence of values {start, start+1, +// start+2, ..., }. +// Range(start, end, step) +// - returns a generator producing a sequence of values {start, start+step, +// start+step+step, ..., }. +// Notes: +// * The generated sequences never include end. For example, Range(1, 5) +// returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2) +// returns a generator producing {1, 3, 5, 7}. +// * start and end must have the same type. That type may be any integral or +// floating-point type or a user defined type satisfying these conditions: +// * It must be assignable (have operator=() defined). +// * It must have operator+() (operator+(int-compatible type) for +// two-operand version). +// * It must have operator<() defined. +// Elements in the resulting sequences will also have that type. +// * Condition start < end must be satisfied in order for resulting sequences +// to contain any elements. +// +template +internal::ParamGenerator Range(T start, T end, IncrementT step) { + return internal::ParamGenerator( + new internal::RangeGenerator(start, end, step)); +} + +template +internal::ParamGenerator Range(T start, T end) { + return Range(start, end, 1); +} + +// ValuesIn() function allows generation of tests with parameters coming from +// a container. +// +// Synopsis: +// ValuesIn(const T (&array)[N]) +// - returns a generator producing sequences with elements from +// a C-style array. +// ValuesIn(const Container& container) +// - returns a generator producing sequences with elements from +// an STL-style container. +// ValuesIn(Iterator begin, Iterator end) +// - returns a generator producing sequences with elements from +// a range [begin, end) defined by a pair of STL-style iterators. These +// iterators can also be plain C pointers. +// +// Please note that ValuesIn copies the values from the containers +// passed in and keeps them to generate tests in RUN_ALL_TESTS(). +// +// Examples: +// +// This instantiates tests from test suite StringTest +// each with C-string values of "foo", "bar", and "baz": +// +// const char* strings[] = {"foo", "bar", "baz"}; +// INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings)); +// +// This instantiates tests from test suite StlStringTest +// each with STL strings with values "a" and "b": +// +// ::std::vector< ::std::string> GetParameterStrings() { +// ::std::vector< ::std::string> v; +// v.push_back("a"); +// v.push_back("b"); +// return v; +// } +// +// INSTANTIATE_TEST_SUITE_P(CharSequence, +// StlStringTest, +// ValuesIn(GetParameterStrings())); +// +// +// This will also instantiate tests from CharTest +// each with parameter values 'a' and 'b': +// +// ::std::list GetParameterChars() { +// ::std::list list; +// list.push_back('a'); +// list.push_back('b'); +// return list; +// } +// ::std::list l = GetParameterChars(); +// INSTANTIATE_TEST_SUITE_P(CharSequence2, +// CharTest, +// ValuesIn(l.begin(), l.end())); +// +template +internal::ParamGenerator< + typename std::iterator_traits::value_type> +ValuesIn(ForwardIterator begin, ForwardIterator end) { + typedef typename std::iterator_traits::value_type ParamType; + return internal::ParamGenerator( + new internal::ValuesInIteratorRangeGenerator(begin, end)); +} + +template +internal::ParamGenerator ValuesIn(const T (&array)[N]) { + return ValuesIn(array, array + N); +} + +template +internal::ParamGenerator ValuesIn( + const Container& container) { + return ValuesIn(container.begin(), container.end()); +} + +// Values() allows generating tests from explicitly specified list of +// parameters. +// +// Synopsis: +// Values(T v1, T v2, ..., T vN) +// - returns a generator producing sequences with elements v1, v2, ..., vN. +// +// For example, this instantiates tests from test suite BarTest each +// with values "one", "two", and "three": +// +// INSTANTIATE_TEST_SUITE_P(NumSequence, +// BarTest, +// Values("one", "two", "three")); +// +// This instantiates tests from test suite BazTest each with values 1, 2, 3.5. +// The exact type of values will depend on the type of parameter in BazTest. +// +// INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5)); +// +// +template +internal::ValueArray Values(T... v) { + return internal::ValueArray(std::move(v)...); +} + +// Bool() allows generating tests with parameters in a set of (false, true). +// +// Synopsis: +// Bool() +// - returns a generator producing sequences with elements {false, true}. +// +// It is useful when testing code that depends on Boolean flags. Combinations +// of multiple flags can be tested when several Bool()'s are combined using +// Combine() function. +// +// In the following example all tests in the test suite FlagDependentTest +// will be instantiated twice with parameters false and true. +// +// class FlagDependentTest : public testing::TestWithParam { +// virtual void SetUp() { +// external_flag = GetParam(); +// } +// } +// INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool()); +// +inline internal::ParamGenerator Bool() { return Values(false, true); } + +// Combine() allows the user to combine two or more sequences to produce +// values of a Cartesian product of those sequences' elements. +// +// Synopsis: +// Combine(gen1, gen2, ..., genN) +// - returns a generator producing sequences with elements coming from +// the Cartesian product of elements from the sequences generated by +// gen1, gen2, ..., genN. The sequence elements will have a type of +// std::tuple where T1, T2, ..., TN are the types +// of elements from sequences produces by gen1, gen2, ..., genN. +// +// Example: +// +// This will instantiate tests in test suite AnimalTest each one with +// the parameter values tuple("cat", BLACK), tuple("cat", WHITE), +// tuple("dog", BLACK), and tuple("dog", WHITE): +// +// enum Color { BLACK, GRAY, WHITE }; +// class AnimalTest +// : public testing::TestWithParam > {...}; +// +// TEST_P(AnimalTest, AnimalLooksNice) {...} +// +// INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest, +// Combine(Values("cat", "dog"), +// Values(BLACK, WHITE))); +// +// This will instantiate tests in FlagDependentTest with all variations of two +// Boolean flags: +// +// class FlagDependentTest +// : public testing::TestWithParam > { +// virtual void SetUp() { +// // Assigns external_flag_1 and external_flag_2 values from the tuple. +// std::tie(external_flag_1, external_flag_2) = GetParam(); +// } +// }; +// +// TEST_P(FlagDependentTest, TestFeature1) { +// // Test your code using external_flag_1 and external_flag_2 here. +// } +// INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest, +// Combine(Bool(), Bool())); +// +template +internal::CartesianProductHolder Combine(const Generator&... g) { + return internal::CartesianProductHolder(g...); +} + +#define TEST_P(test_suite_name, test_name) \ + class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + : public test_suite_name { \ + public: \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \ + void TestBody() override; \ + \ + private: \ + static int AddToRegistry() { \ + ::testing::UnitTest::GetInstance() \ + ->parameterized_test_registry() \ + .GetTestSuitePatternHolder( \ + GTEST_STRINGIFY_(test_suite_name), \ + ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ + ->AddTestPattern( \ + GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name), \ + new ::testing::internal::TestMetaFactory(), \ + ::testing::internal::CodeLocation(__FILE__, __LINE__)); \ + return 0; \ + } \ + static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + (const GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &) = delete; \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \ + const GTEST_TEST_CLASS_NAME_(test_suite_name, \ + test_name) &) = delete; /* NOLINT */ \ + }; \ + int GTEST_TEST_CLASS_NAME_(test_suite_name, \ + test_name)::gtest_registering_dummy_ = \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry(); \ + void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody() + +// The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify +// generator and an optional function or functor that generates custom test name +// suffixes based on the test parameters. Such a function or functor should +// accept one argument of type testing::TestParamInfo, and +// return std::string. +// +// testing::PrintToStringParamName is a builtin test suffix generator that +// returns the value of testing::PrintToString(GetParam()). +// +// Note: test names must be non-empty, unique, and may only contain ASCII +// alphanumeric characters or underscore. Because PrintToString adds quotes +// to std::string and C strings, it won't work for these types. + +#define GTEST_EXPAND_(arg) arg +#define GTEST_GET_FIRST_(first, ...) first +#define GTEST_GET_SECOND_(first, second, ...) second + +#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \ + static ::testing::internal::ParamGenerator \ + gtest_##prefix##test_suite_name##_EvalGenerator_() { \ + return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \ + } \ + static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \ + const ::testing::TestParamInfo& info) { \ + if (::testing::internal::AlwaysFalse()) { \ + ::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \ + __VA_ARGS__, \ + ::testing::internal::DefaultParamName, \ + DUMMY_PARAM_))); \ + auto t = std::make_tuple(__VA_ARGS__); \ + static_assert(std::tuple_size::value <= 2, \ + "Too Many Args!"); \ + } \ + return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \ + __VA_ARGS__, \ + ::testing::internal::DefaultParamName, \ + DUMMY_PARAM_))))(info); \ + } \ + static int gtest_##prefix##test_suite_name##_dummy_ \ + GTEST_ATTRIBUTE_UNUSED_ = \ + ::testing::UnitTest::GetInstance() \ + ->parameterized_test_registry() \ + .GetTestSuitePatternHolder( \ + GTEST_STRINGIFY_(test_suite_name), \ + ::testing::internal::CodeLocation(__FILE__, __LINE__)) \ + ->AddTestSuiteInstantiation( \ + GTEST_STRINGIFY_(prefix), \ + >est_##prefix##test_suite_name##_EvalGenerator_, \ + >est_##prefix##test_suite_name##_EvalGenerateName_, \ + __FILE__, __LINE__) + +// Allow Marking a Parameterized test class as not needing to be instantiated. +#define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \ + namespace gtest_do_not_use_outside_namespace_scope {} \ + static const ::testing::internal::MarkAsIgnored gtest_allow_ignore_##T( \ + GTEST_STRINGIFY_(T)) + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +#define INSTANTIATE_TEST_CASE_P \ + static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \ + ""); \ + INSTANTIATE_TEST_SUITE_P +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_ diff --git a/test/gtest/include/gtest/gtest-printers.h b/test/gtest/include/gtest/gtest-printers.h new file mode 100644 index 0000000000..a91e8b8b10 --- /dev/null +++ b/test/gtest/include/gtest/gtest-printers.h @@ -0,0 +1,1048 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Google Test - The Google C++ Testing and Mocking Framework +// +// This file implements a universal value printer that can print a +// value of any type T: +// +// void ::testing::internal::UniversalPrinter::Print(value, ostream_ptr); +// +// A user can teach this function how to print a class type T by +// defining either operator<<() or PrintTo() in the namespace that +// defines T. More specifically, the FIRST defined function in the +// following list will be used (assuming T is defined in namespace +// foo): +// +// 1. foo::PrintTo(const T&, ostream*) +// 2. operator<<(ostream&, const T&) defined in either foo or the +// global namespace. +// +// However if T is an STL-style container then it is printed element-wise +// unless foo::PrintTo(const T&, ostream*) is defined. Note that +// operator<<() is ignored for container types. +// +// If none of the above is defined, it will print the debug string of +// the value if it is a protocol buffer, or print the raw bytes in the +// value otherwise. +// +// To aid debugging: when T is a reference type, the address of the +// value is also printed; when T is a (const) char pointer, both the +// pointer value and the NUL-terminated string it points to are +// printed. +// +// We also provide some convenient wrappers: +// +// // Prints a value to a string. For a (const or not) char +// // pointer, the NUL-terminated string (but not the pointer) is +// // printed. +// std::string ::testing::PrintToString(const T& value); +// +// // Prints a value tersely: for a reference type, the referenced +// // value (but not the address) is printed; for a (const or not) char +// // pointer, the NUL-terminated string (but not the pointer) is +// // printed. +// void ::testing::internal::UniversalTersePrint(const T& value, ostream*); +// +// // Prints value using the type inferred by the compiler. The difference +// // from UniversalTersePrint() is that this function prints both the +// // pointer and the NUL-terminated string for a (const or not) char pointer. +// void ::testing::internal::UniversalPrint(const T& value, ostream*); +// +// // Prints the fields of a tuple tersely to a string vector, one +// // element for each field. Tuple support must be enabled in +// // gtest-port.h. +// std::vector UniversalTersePrintTupleFieldsToStrings( +// const Tuple& value); +// +// Known limitation: +// +// The print primitives print the elements of an STL-style container +// using the compiler-inferred type of *iter where iter is a +// const_iterator of the container. When const_iterator is an input +// iterator but not a forward iterator, this inferred type may not +// match value_type, and the print output may be incorrect. In +// practice, this is rarely a problem as for most containers +// const_iterator is a forward iterator. We'll fix this if there's an +// actual need for it. Note that this fix cannot rely on value_type +// being defined as many user-defined container types don't have +// value_type. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ + +#include +#include +#include // NOLINT +#include +#include +#include +#include +#include +#include + +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-port.h" + +namespace testing { + +// Definitions in the internal* namespaces are subject to change without notice. +// DO NOT USE THEM IN USER CODE! +namespace internal { + +template +void UniversalPrint(const T& value, ::std::ostream* os); + +// Used to print an STL-style container when the user doesn't define +// a PrintTo() for it. +struct ContainerPrinter { + template (0)) == sizeof(IsContainer)) && + !IsRecursiveContainer::value>::type> + static void PrintValue(const T& container, std::ostream* os) { + const size_t kMaxCount = 32; // The maximum number of elements to print. + *os << '{'; + size_t count = 0; + for (auto&& elem : container) { + if (count > 0) { + *os << ','; + if (count == kMaxCount) { // Enough has been printed. + *os << " ..."; + break; + } + } + *os << ' '; + // We cannot call PrintTo(elem, os) here as PrintTo() doesn't + // handle `elem` being a native array. + internal::UniversalPrint(elem, os); + ++count; + } + + if (count > 0) { + *os << ' '; + } + *os << '}'; + } +}; + +// Used to print a pointer that is neither a char pointer nor a member +// pointer, when the user doesn't define PrintTo() for it. (A member +// variable pointer or member function pointer doesn't really point to +// a location in the address space. Their representation is +// implementation-defined. Therefore they will be printed as raw +// bytes.) +struct FunctionPointerPrinter { + template ::value>::type> + static void PrintValue(T* p, ::std::ostream* os) { + if (p == nullptr) { + *os << "NULL"; + } else { + // T is a function type, so '*os << p' doesn't do what we want + // (it just prints p as bool). We want to print p as a const + // void*. + *os << reinterpret_cast(p); + } + } +}; + +struct PointerPrinter { + template + static void PrintValue(T* p, ::std::ostream* os) { + if (p == nullptr) { + *os << "NULL"; + } else { + // T is not a function type. We just call << to print p, + // relying on ADL to pick up user-defined << for their pointer + // types, if any. + *os << p; + } + } +}; + +namespace internal_stream_operator_without_lexical_name_lookup { + +// The presence of an operator<< here will terminate lexical scope lookup +// straight away (even though it cannot be a match because of its argument +// types). Thus, the two operator<< calls in StreamPrinter will find only ADL +// candidates. +struct LookupBlocker {}; +void operator<<(LookupBlocker, LookupBlocker); + +struct StreamPrinter { + template ::value>::type, + // Only accept types for which we can find a streaming operator via + // ADL (possibly involving implicit conversions). + typename = decltype(std::declval() + << std::declval())> + static void PrintValue(const T& value, ::std::ostream* os) { + // Call streaming operator found by ADL, possibly with implicit conversions + // of the arguments. + *os << value; + } +}; + +} // namespace internal_stream_operator_without_lexical_name_lookup + +struct ProtobufPrinter { + // We print a protobuf using its ShortDebugString() when the string + // doesn't exceed this many characters; otherwise we print it using + // DebugString() for better readability. + static const size_t kProtobufOneLinerMaxLength = 50; + + template ::value>::type> + static void PrintValue(const T& value, ::std::ostream* os) { + std::string pretty_str = value.ShortDebugString(); + if (pretty_str.length() > kProtobufOneLinerMaxLength) { + pretty_str = "\n" + value.DebugString(); + } + *os << ("<" + pretty_str + ">"); + } +}; + +struct ConvertibleToIntegerPrinter { + // Since T has no << operator or PrintTo() but can be implicitly + // converted to BiggestInt, we print it as a BiggestInt. + // + // Most likely T is an enum type (either named or unnamed), in which + // case printing it as an integer is the desired behavior. In case + // T is not an enum, printing it as an integer is the best we can do + // given that it has no user-defined printer. + static void PrintValue(internal::BiggestInt value, ::std::ostream* os) { + *os << value; + } +}; + +struct ConvertibleToStringViewPrinter { +#if GTEST_INTERNAL_HAS_STRING_VIEW + static void PrintValue(internal::StringView value, ::std::ostream* os) { + internal::UniversalPrint(value, os); + } +#endif +}; + +// Prints the given number of bytes in the given object to the given +// ostream. +GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, + size_t count, ::std::ostream* os); +struct RawBytesPrinter { + // SFINAE on `sizeof` to make sure we have a complete type. + template + static void PrintValue(const T& value, ::std::ostream* os) { + PrintBytesInObjectTo( + static_cast( + // Load bearing cast to void* to support iOS + reinterpret_cast(std::addressof(value))), + sizeof(value), os); + } +}; + +struct FallbackPrinter { + template + static void PrintValue(const T&, ::std::ostream* os) { + *os << "(incomplete type)"; + } +}; + +// Try every printer in order and return the first one that works. +template +struct FindFirstPrinter : FindFirstPrinter {}; + +template +struct FindFirstPrinter< + T, decltype(Printer::PrintValue(std::declval(), nullptr)), + Printer, Printers...> { + using type = Printer; +}; + +// Select the best printer in the following order: +// - Print containers (they have begin/end/etc). +// - Print function pointers. +// - Print object pointers. +// - Use the stream operator, if available. +// - Print protocol buffers. +// - Print types convertible to BiggestInt. +// - Print types convertible to StringView, if available. +// - Fallback to printing the raw bytes of the object. +template +void PrintWithFallback(const T& value, ::std::ostream* os) { + using Printer = typename FindFirstPrinter< + T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, + internal_stream_operator_without_lexical_name_lookup::StreamPrinter, + ProtobufPrinter, ConvertibleToIntegerPrinter, + ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type; + Printer::PrintValue(value, os); +} + +// FormatForComparison::Format(value) formats a +// value of type ToPrint that is an operand of a comparison assertion +// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in +// the comparison, and is used to help determine the best way to +// format the value. In particular, when the value is a C string +// (char pointer) and the other operand is an STL string object, we +// want to format the C string as a string, since we know it is +// compared by value with the string object. If the value is a char +// pointer but the other operand is not an STL string object, we don't +// know whether the pointer is supposed to point to a NUL-terminated +// string, and thus want to print it as a pointer to be safe. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + +// The default case. +template +class FormatForComparison { + public: + static ::std::string Format(const ToPrint& value) { + return ::testing::PrintToString(value); + } +}; + +// Array. +template +class FormatForComparison { + public: + static ::std::string Format(const ToPrint* value) { + return FormatForComparison::Format(value); + } +}; + +// By default, print C string as pointers to be safe, as we don't know +// whether they actually point to a NUL-terminated string. + +#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ + template \ + class FormatForComparison { \ + public: \ + static ::std::string Format(CharType* value) { \ + return ::testing::PrintToString(static_cast(value)); \ + } \ + } + +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); +#ifdef __cpp_lib_char8_t +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t); +#endif +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t); +GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t); + +#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ + +// If a C string is compared with an STL string object, we know it's meant +// to point to a NUL-terminated string, and thus can print it as a string. + +#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ + template <> \ + class FormatForComparison { \ + public: \ + static ::std::string Format(CharType* value) { \ + return ::testing::PrintToString(value); \ + } \ + } + +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); +#ifdef __cpp_char8_t +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string); +#endif +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string); + +#if GTEST_HAS_STD_WSTRING +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); +GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); +#endif + +#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ + +// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) +// operand to be used in a failure message. The type (but not value) +// of the other operand may affect the format. This allows us to +// print a char* as a raw pointer when it is compared against another +// char* or void*, and print it as a C string when it is compared +// against an std::string object, for example. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +template +std::string FormatForComparisonFailureMessage(const T1& value, + const T2& /* other_operand */) { + return FormatForComparison::Format(value); +} + +// UniversalPrinter::Print(value, ostream_ptr) prints the given +// value to the given ostream. The caller must ensure that +// 'ostream_ptr' is not NULL, or the behavior is undefined. +// +// We define UniversalPrinter as a class template (as opposed to a +// function template), as we need to partially specialize it for +// reference types, which cannot be done with function templates. +template +class UniversalPrinter; + +// Prints the given value using the << operator if it has one; +// otherwise prints the bytes in it. This is what +// UniversalPrinter::Print() does when PrintTo() is not specialized +// or overloaded for type T. +// +// A user can override this behavior for a class type Foo by defining +// an overload of PrintTo() in the namespace where Foo is defined. We +// give the user this option as sometimes defining a << operator for +// Foo is not desirable (e.g. the coding style may prevent doing it, +// or there is already a << operator but it doesn't do what the user +// wants). +template +void PrintTo(const T& value, ::std::ostream* os) { + internal::PrintWithFallback(value, os); +} + +// The following list of PrintTo() overloads tells +// UniversalPrinter::Print() how to print standard types (built-in +// types, strings, plain arrays, and pointers). + +// Overloads for various char types. +GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os); +GTEST_API_ void PrintTo(signed char c, ::std::ostream* os); +inline void PrintTo(char c, ::std::ostream* os) { + // When printing a plain char, we always treat it as unsigned. This + // way, the output won't be affected by whether the compiler thinks + // char is signed or not. + PrintTo(static_cast(c), os); +} + +// Overloads for other simple built-in types. +inline void PrintTo(bool x, ::std::ostream* os) { + *os << (x ? "true" : "false"); +} + +// Overload for wchar_t type. +// Prints a wchar_t as a symbol if it is printable or as its internal +// code otherwise and also as its decimal code (except for L'\0'). +// The L'\0' char is printed as "L'\\0'". The decimal code is printed +// as signed integer when wchar_t is implemented by the compiler +// as a signed type and is printed as an unsigned integer when wchar_t +// is implemented as an unsigned type. +GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os); + +GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os); +inline void PrintTo(char16_t c, ::std::ostream* os) { + PrintTo(ImplicitCast_(c), os); +} +#ifdef __cpp_char8_t +inline void PrintTo(char8_t c, ::std::ostream* os) { + PrintTo(ImplicitCast_(c), os); +} +#endif + +// gcc/clang __{u,}int128_t +#if defined(__SIZEOF_INT128__) +GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os); +GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os); +#endif // __SIZEOF_INT128__ + +// Overloads for C strings. +GTEST_API_ void PrintTo(const char* s, ::std::ostream* os); +inline void PrintTo(char* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} + +// signed/unsigned char is often used for representing binary data, so +// we print pointers to it as void* to be safe. +inline void PrintTo(const signed char* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +inline void PrintTo(signed char* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +inline void PrintTo(const unsigned char* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +inline void PrintTo(unsigned char* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +#ifdef __cpp_char8_t +// Overloads for u8 strings. +GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os); +inline void PrintTo(char8_t* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +#endif +// Overloads for u16 strings. +GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os); +inline void PrintTo(char16_t* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +// Overloads for u32 strings. +GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os); +inline void PrintTo(char32_t* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} + +// MSVC can be configured to define wchar_t as a typedef of unsigned +// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native +// type. When wchar_t is a typedef, defining an overload for const +// wchar_t* would cause unsigned short* be printed as a wide string, +// possibly causing invalid memory accesses. +#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) +// Overloads for wide C strings +GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os); +inline void PrintTo(wchar_t* s, ::std::ostream* os) { + PrintTo(ImplicitCast_(s), os); +} +#endif + +// Overload for C arrays. Multi-dimensional arrays are printed +// properly. + +// Prints the given number of elements in an array, without printing +// the curly braces. +template +void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) { + UniversalPrint(a[0], os); + for (size_t i = 1; i != count; i++) { + *os << ", "; + UniversalPrint(a[i], os); + } +} + +// Overloads for ::std::string. +GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os); +inline void PrintTo(const ::std::string& s, ::std::ostream* os) { + PrintStringTo(s, os); +} + +// Overloads for ::std::u8string +#ifdef __cpp_char8_t +GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os); +inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) { + PrintU8StringTo(s, os); +} +#endif + +// Overloads for ::std::u16string +GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os); +inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) { + PrintU16StringTo(s, os); +} + +// Overloads for ::std::u32string +GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os); +inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) { + PrintU32StringTo(s, os); +} + +// Overloads for ::std::wstring. +#if GTEST_HAS_STD_WSTRING +GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os); +inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { + PrintWideStringTo(s, os); +} +#endif // GTEST_HAS_STD_WSTRING + +#if GTEST_INTERNAL_HAS_STRING_VIEW +// Overload for internal::StringView. +inline void PrintTo(internal::StringView sp, ::std::ostream* os) { + PrintTo(::std::string(sp), os); +} +#endif // GTEST_INTERNAL_HAS_STRING_VIEW + +inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; } + +#if GTEST_HAS_RTTI +inline void PrintTo(const std::type_info& info, std::ostream* os) { + *os << internal::GetTypeName(info); +} +#endif // GTEST_HAS_RTTI + +template +void PrintTo(std::reference_wrapper ref, ::std::ostream* os) { + UniversalPrinter::Print(ref.get(), os); +} + +inline const void* VoidifyPointer(const void* p) { return p; } +inline const void* VoidifyPointer(volatile const void* p) { + return const_cast(p); +} + +template +void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) { + if (ptr == nullptr) { + *os << "(nullptr)"; + } else { + // We can't print the value. Just print the pointer.. + *os << "(" << (VoidifyPointer)(ptr.get()) << ")"; + } +} +template ::value && + !std::is_array::value>::type> +void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) { + if (ptr == nullptr) { + *os << "(nullptr)"; + } else { + *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = "; + UniversalPrinter::Print(*ptr, os); + *os << ")"; + } +} + +template +void PrintTo(const std::unique_ptr& ptr, std::ostream* os) { + (PrintSmartPointer)(ptr, os, 0); +} + +template +void PrintTo(const std::shared_ptr& ptr, std::ostream* os) { + (PrintSmartPointer)(ptr, os, 0); +} + +// Helper function for printing a tuple. T must be instantiated with +// a tuple type. +template +void PrintTupleTo(const T&, std::integral_constant, + ::std::ostream*) {} + +template +void PrintTupleTo(const T& t, std::integral_constant, + ::std::ostream* os) { + PrintTupleTo(t, std::integral_constant(), os); + GTEST_INTENTIONAL_CONST_COND_PUSH_() + if (I > 1) { + GTEST_INTENTIONAL_CONST_COND_POP_() + *os << ", "; + } + UniversalPrinter::type>::Print( + std::get(t), os); +} + +template +void PrintTo(const ::std::tuple& t, ::std::ostream* os) { + *os << "("; + PrintTupleTo(t, std::integral_constant(), os); + *os << ")"; +} + +// Overload for std::pair. +template +void PrintTo(const ::std::pair& value, ::std::ostream* os) { + *os << '('; + // We cannot use UniversalPrint(value.first, os) here, as T1 may be + // a reference type. The same for printing value.second. + UniversalPrinter::Print(value.first, os); + *os << ", "; + UniversalPrinter::Print(value.second, os); + *os << ')'; +} + +// Implements printing a non-reference type T by letting the compiler +// pick the right overload of PrintTo() for T. +template +class UniversalPrinter { + public: + // MSVC warns about adding const to a function type, so we want to + // disable the warning. + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) + + // Note: we deliberately don't call this PrintTo(), as that name + // conflicts with ::testing::internal::PrintTo in the body of the + // function. + static void Print(const T& value, ::std::ostream* os) { + // By default, ::testing::internal::PrintTo() is used for printing + // the value. + // + // Thanks to Koenig look-up, if T is a class and has its own + // PrintTo() function defined in its namespace, that function will + // be visible here. Since it is more specific than the generic ones + // in ::testing::internal, it will be picked by the compiler in the + // following statement - exactly what we want. + PrintTo(value, os); + } + + GTEST_DISABLE_MSC_WARNINGS_POP_() +}; + +// Remove any const-qualifiers before passing a type to UniversalPrinter. +template +class UniversalPrinter : public UniversalPrinter {}; + +#if GTEST_INTERNAL_HAS_ANY + +// Printer for std::any / absl::any + +template <> +class UniversalPrinter { + public: + static void Print(const Any& value, ::std::ostream* os) { + if (value.has_value()) { + *os << "value of type " << GetTypeName(value); + } else { + *os << "no value"; + } + } + + private: + static std::string GetTypeName(const Any& value) { +#if GTEST_HAS_RTTI + return internal::GetTypeName(value.type()); +#else + static_cast(value); // possibly unused + return ""; +#endif // GTEST_HAS_RTTI + } +}; + +#endif // GTEST_INTERNAL_HAS_ANY + +#if GTEST_INTERNAL_HAS_OPTIONAL + +// Printer for std::optional / absl::optional + +template +class UniversalPrinter> { + public: + static void Print(const Optional& value, ::std::ostream* os) { + *os << '('; + if (!value) { + *os << "nullopt"; + } else { + UniversalPrint(*value, os); + } + *os << ')'; + } +}; + +template <> +class UniversalPrinter { + public: + static void Print(decltype(Nullopt()), ::std::ostream* os) { + *os << "(nullopt)"; + } +}; + +#endif // GTEST_INTERNAL_HAS_OPTIONAL + +#if GTEST_INTERNAL_HAS_VARIANT + +// Printer for std::variant / absl::variant + +template +class UniversalPrinter> { + public: + static void Print(const Variant& value, ::std::ostream* os) { + *os << '('; +#if GTEST_HAS_ABSL + absl::visit(Visitor{os, value.index()}, value); +#else + std::visit(Visitor{os, value.index()}, value); +#endif // GTEST_HAS_ABSL + *os << ')'; + } + + private: + struct Visitor { + template + void operator()(const U& u) const { + *os << "'" << GetTypeName() << "(index = " << index + << ")' with value "; + UniversalPrint(u, os); + } + ::std::ostream* os; + std::size_t index; + }; +}; + +#endif // GTEST_INTERNAL_HAS_VARIANT + +// UniversalPrintArray(begin, len, os) prints an array of 'len' +// elements, starting at address 'begin'. +template +void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { + if (len == 0) { + *os << "{}"; + } else { + *os << "{ "; + const size_t kThreshold = 18; + const size_t kChunkSize = 8; + // If the array has more than kThreshold elements, we'll have to + // omit some details by printing only the first and the last + // kChunkSize elements. + if (len <= kThreshold) { + PrintRawArrayTo(begin, len, os); + } else { + PrintRawArrayTo(begin, kChunkSize, os); + *os << ", ..., "; + PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os); + } + *os << " }"; + } +} +// This overload prints a (const) char array compactly. +GTEST_API_ void UniversalPrintArray(const char* begin, size_t len, + ::std::ostream* os); + +#ifdef __cpp_char8_t +// This overload prints a (const) char8_t array compactly. +GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len, + ::std::ostream* os); +#endif + +// This overload prints a (const) char16_t array compactly. +GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len, + ::std::ostream* os); + +// This overload prints a (const) char32_t array compactly. +GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len, + ::std::ostream* os); + +// This overload prints a (const) wchar_t array compactly. +GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len, + ::std::ostream* os); + +// Implements printing an array type T[N]. +template +class UniversalPrinter { + public: + // Prints the given array, omitting some elements when there are too + // many. + static void Print(const T (&a)[N], ::std::ostream* os) { + UniversalPrintArray(a, N, os); + } +}; + +// Implements printing a reference type T&. +template +class UniversalPrinter { + public: + // MSVC warns about adding const to a function type, so we want to + // disable the warning. + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) + + static void Print(const T& value, ::std::ostream* os) { + // Prints the address of the value. We use reinterpret_cast here + // as static_cast doesn't compile when T is a function type. + *os << "@" << reinterpret_cast(&value) << " "; + + // Then prints the value itself. + UniversalPrint(value, os); + } + + GTEST_DISABLE_MSC_WARNINGS_POP_() +}; + +// Prints a value tersely: for a reference type, the referenced value +// (but not the address) is printed; for a (const) char pointer, the +// NUL-terminated string (but not the pointer) is printed. + +template +class UniversalTersePrinter { + public: + static void Print(const T& value, ::std::ostream* os) { + UniversalPrint(value, os); + } +}; +template +class UniversalTersePrinter { + public: + static void Print(const T& value, ::std::ostream* os) { + UniversalPrint(value, os); + } +}; +template +class UniversalTersePrinter { + public: + static void Print(const T (&value)[N], ::std::ostream* os) { + UniversalPrinter::Print(value, os); + } +}; +template <> +class UniversalTersePrinter { + public: + static void Print(const char* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(std::string(str), os); + } + } +}; +template <> +class UniversalTersePrinter : public UniversalTersePrinter { +}; + +#ifdef __cpp_char8_t +template <> +class UniversalTersePrinter { + public: + static void Print(const char8_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::u8string(str), os); + } + } +}; +template <> +class UniversalTersePrinter + : public UniversalTersePrinter {}; +#endif + +template <> +class UniversalTersePrinter { + public: + static void Print(const char16_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::u16string(str), os); + } + } +}; +template <> +class UniversalTersePrinter + : public UniversalTersePrinter {}; + +template <> +class UniversalTersePrinter { + public: + static void Print(const char32_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::u32string(str), os); + } + } +}; +template <> +class UniversalTersePrinter + : public UniversalTersePrinter {}; + +#if GTEST_HAS_STD_WSTRING +template <> +class UniversalTersePrinter { + public: + static void Print(const wchar_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::wstring(str), os); + } + } +}; +#endif + +template <> +class UniversalTersePrinter { + public: + static void Print(wchar_t* str, ::std::ostream* os) { + UniversalTersePrinter::Print(str, os); + } +}; + +template +void UniversalTersePrint(const T& value, ::std::ostream* os) { + UniversalTersePrinter::Print(value, os); +} + +// Prints a value using the type inferred by the compiler. The +// difference between this and UniversalTersePrint() is that for a +// (const) char pointer, this prints both the pointer and the +// NUL-terminated string. +template +void UniversalPrint(const T& value, ::std::ostream* os) { + // A workarond for the bug in VC++ 7.1 that prevents us from instantiating + // UniversalPrinter with T directly. + typedef T T1; + UniversalPrinter::Print(value, os); +} + +typedef ::std::vector<::std::string> Strings; + +// Tersely prints the first N fields of a tuple to a string vector, +// one element for each field. +template +void TersePrintPrefixToStrings(const Tuple&, std::integral_constant, + Strings*) {} +template +void TersePrintPrefixToStrings(const Tuple& t, + std::integral_constant, + Strings* strings) { + TersePrintPrefixToStrings(t, std::integral_constant(), + strings); + ::std::stringstream ss; + UniversalTersePrint(std::get(t), &ss); + strings->push_back(ss.str()); +} + +// Prints the fields of a tuple tersely to a string vector, one +// element for each field. See the comment before +// UniversalTersePrint() for how we define "tersely". +template +Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { + Strings result; + TersePrintPrefixToStrings( + value, std::integral_constant::value>(), + &result); + return result; +} + +} // namespace internal + +template +::std::string PrintToString(const T& value) { + ::std::stringstream ss; + internal::UniversalTersePrinter::Print(value, &ss); + return ss.str(); +} + +} // namespace testing + +// Include any custom printer added by the local installation. +// We must include this header at the end to make sure it can use the +// declarations from this file. +#include "gtest/internal/custom/gtest-printers.h" + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ diff --git a/test/gtest/include/gtest/gtest-spi.h b/test/gtest/include/gtest/gtest-spi.h new file mode 100644 index 0000000000..bec8c4810b --- /dev/null +++ b/test/gtest/include/gtest/gtest-spi.h @@ -0,0 +1,248 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Utilities for testing Google Test itself and code that uses Google Test +// (e.g. frameworks built on top of Google Test). + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ + +#include "gtest/gtest.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +namespace testing { + +// This helper class can be used to mock out Google Test failure reporting +// so that we can test Google Test or code that builds on Google Test. +// +// An object of this class appends a TestPartResult object to the +// TestPartResultArray object given in the constructor whenever a Google Test +// failure is reported. It can either intercept only failures that are +// generated in the same thread that created this object or it can intercept +// all generated failures. The scope of this mock object can be controlled with +// the second argument to the two arguments constructor. +class GTEST_API_ ScopedFakeTestPartResultReporter + : public TestPartResultReporterInterface { + public: + // The two possible mocking modes of this object. + enum InterceptMode { + INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. + INTERCEPT_ALL_THREADS // Intercepts all failures. + }; + + // The c'tor sets this object as the test part result reporter used + // by Google Test. The 'result' parameter specifies where to report the + // results. This reporter will only catch failures generated in the current + // thread. DEPRECATED + explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); + + // Same as above, but you can choose the interception scope of this object. + ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, + TestPartResultArray* result); + + // The d'tor restores the previous test part result reporter. + ~ScopedFakeTestPartResultReporter() override; + + // Appends the TestPartResult object to the TestPartResultArray + // received in the constructor. + // + // This method is from the TestPartResultReporterInterface + // interface. + void ReportTestPartResult(const TestPartResult& result) override; + + private: + void Init(); + + const InterceptMode intercept_mode_; + TestPartResultReporterInterface* old_reporter_; + TestPartResultArray* const result_; + + ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) = + delete; + ScopedFakeTestPartResultReporter& operator=( + const ScopedFakeTestPartResultReporter&) = delete; +}; + +namespace internal { + +// A helper class for implementing EXPECT_FATAL_FAILURE() and +// EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given +// TestPartResultArray contains exactly one failure that has the given +// type and contains the given substring. If that's not the case, a +// non-fatal failure will be generated. +class GTEST_API_ SingleFailureChecker { + public: + // The constructor remembers the arguments. + SingleFailureChecker(const TestPartResultArray* results, + TestPartResult::Type type, const std::string& substr); + ~SingleFailureChecker(); + + private: + const TestPartResultArray* const results_; + const TestPartResult::Type type_; + const std::string substr_; + + SingleFailureChecker(const SingleFailureChecker&) = delete; + SingleFailureChecker& operator=(const SingleFailureChecker&) = delete; +}; + +} // namespace internal + +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +// A set of macros for testing Google Test assertions or code that's expected +// to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but +// not a non-fatal failure, as from EXPECT_EQ). It verifies that the given +// statement will cause exactly one fatal Google Test failure with 'substr' +// being part of the failure message. +// +// There are two different versions of this macro. EXPECT_FATAL_FAILURE only +// affects and considers failures generated in the current thread and +// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. +// +// The verification of the assertion is done correctly even when the statement +// throws an exception or aborts the current function. +// +// Known restrictions: +// - 'statement' cannot reference local non-static variables or +// non-static members of the current object. +// - 'statement' cannot return a value. +// - You cannot stream a failure message to this macro. +// +// Note that even though the implementations of the following two +// macros are much alike, we cannot refactor them to use a common +// helper macro, due to some peculiarity in how the preprocessor +// works. The AcceptsMacroThatExpandsToUnprotectedComma test in +// gtest_unittest.cc will fail to compile if we do that. +#define EXPECT_FATAL_FAILURE(statement, substr) \ + do { \ + class GTestExpectFatalFailureHelper { \ + public: \ + static void Execute() { statement; } \ + }; \ + ::testing::TestPartResultArray gtest_failures; \ + ::testing::internal::SingleFailureChecker gtest_checker( \ + >est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \ + { \ + ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ + ::testing::ScopedFakeTestPartResultReporter:: \ + INTERCEPT_ONLY_CURRENT_THREAD, \ + >est_failures); \ + GTestExpectFatalFailureHelper::Execute(); \ + } \ + } while (::testing::internal::AlwaysFalse()) + +#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ + do { \ + class GTestExpectFatalFailureHelper { \ + public: \ + static void Execute() { statement; } \ + }; \ + ::testing::TestPartResultArray gtest_failures; \ + ::testing::internal::SingleFailureChecker gtest_checker( \ + >est_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \ + { \ + ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ + ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ + >est_failures); \ + GTestExpectFatalFailureHelper::Execute(); \ + } \ + } while (::testing::internal::AlwaysFalse()) + +// A macro for testing Google Test assertions or code that's expected to +// generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ, +// but not from an ASSERT_EQ). It asserts that the given statement will cause +// exactly one non-fatal Google Test failure with 'substr' being part of the +// failure message. +// +// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only +// affects and considers failures generated in the current thread and +// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. +// +// 'statement' is allowed to reference local variables and members of +// the current object. +// +// The verification of the assertion is done correctly even when the statement +// throws an exception or aborts the current function. +// +// Known restrictions: +// - You cannot stream a failure message to this macro. +// +// Note that even though the implementations of the following two +// macros are much alike, we cannot refactor them to use a common +// helper macro, due to some peculiarity in how the preprocessor +// works. If we do that, the code won't compile when the user gives +// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that +// expands to code containing an unprotected comma. The +// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc +// catches that. +// +// For the same reason, we have to write +// if (::testing::internal::AlwaysTrue()) { statement; } +// instead of +// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) +// to avoid an MSVC warning on unreachable code. +#define EXPECT_NONFATAL_FAILURE(statement, substr) \ + do { \ + ::testing::TestPartResultArray gtest_failures; \ + ::testing::internal::SingleFailureChecker gtest_checker( \ + >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ + (substr)); \ + { \ + ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ + ::testing::ScopedFakeTestPartResultReporter:: \ + INTERCEPT_ONLY_CURRENT_THREAD, \ + >est_failures); \ + if (::testing::internal::AlwaysTrue()) { \ + statement; \ + } \ + } \ + } while (::testing::internal::AlwaysFalse()) + +#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ + do { \ + ::testing::TestPartResultArray gtest_failures; \ + ::testing::internal::SingleFailureChecker gtest_checker( \ + >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ + (substr)); \ + { \ + ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \ + ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ + >est_failures); \ + if (::testing::internal::AlwaysTrue()) { \ + statement; \ + } \ + } \ + } while (::testing::internal::AlwaysFalse()) + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_ diff --git a/test/gtest/include/gtest/gtest-test-part.h b/test/gtest/include/gtest/gtest-test-part.h new file mode 100644 index 0000000000..09cc8c34f0 --- /dev/null +++ b/test/gtest/include/gtest/gtest-test-part.h @@ -0,0 +1,190 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ + +#include +#include + +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-string.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +namespace testing { + +// A copyable object representing the result of a test part (i.e. an +// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). +// +// Don't inherit from TestPartResult as its destructor is not virtual. +class GTEST_API_ TestPartResult { + public: + // The possible outcomes of a test part (i.e. an assertion or an + // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). + enum Type { + kSuccess, // Succeeded. + kNonFatalFailure, // Failed but the test can continue. + kFatalFailure, // Failed and the test should be terminated. + kSkip // Skipped. + }; + + // C'tor. TestPartResult does NOT have a default constructor. + // Always use this constructor (with parameters) to create a + // TestPartResult object. + TestPartResult(Type a_type, const char* a_file_name, int a_line_number, + const char* a_message) + : type_(a_type), + file_name_(a_file_name == nullptr ? "" : a_file_name), + line_number_(a_line_number), + summary_(ExtractSummary(a_message)), + message_(a_message) {} + + // Gets the outcome of the test part. + Type type() const { return type_; } + + // Gets the name of the source file where the test part took place, or + // NULL if it's unknown. + const char* file_name() const { + return file_name_.empty() ? nullptr : file_name_.c_str(); + } + + // Gets the line in the source file where the test part took place, + // or -1 if it's unknown. + int line_number() const { return line_number_; } + + // Gets the summary of the failure message. + const char* summary() const { return summary_.c_str(); } + + // Gets the message associated with the test part. + const char* message() const { return message_.c_str(); } + + // Returns true if and only if the test part was skipped. + bool skipped() const { return type_ == kSkip; } + + // Returns true if and only if the test part passed. + bool passed() const { return type_ == kSuccess; } + + // Returns true if and only if the test part non-fatally failed. + bool nonfatally_failed() const { return type_ == kNonFatalFailure; } + + // Returns true if and only if the test part fatally failed. + bool fatally_failed() const { return type_ == kFatalFailure; } + + // Returns true if and only if the test part failed. + bool failed() const { return fatally_failed() || nonfatally_failed(); } + + private: + Type type_; + + // Gets the summary of the failure message by omitting the stack + // trace in it. + static std::string ExtractSummary(const char* message); + + // The name of the source file where the test part took place, or + // "" if the source file is unknown. + std::string file_name_; + // The line in the source file where the test part took place, or -1 + // if the line number is unknown. + int line_number_; + std::string summary_; // The test failure summary. + std::string message_; // The test failure message. +}; + +// Prints a TestPartResult object. +std::ostream& operator<<(std::ostream& os, const TestPartResult& result); + +// An array of TestPartResult objects. +// +// Don't inherit from TestPartResultArray as its destructor is not +// virtual. +class GTEST_API_ TestPartResultArray { + public: + TestPartResultArray() {} + + // Appends the given TestPartResult to the array. + void Append(const TestPartResult& result); + + // Returns the TestPartResult at the given index (0-based). + const TestPartResult& GetTestPartResult(int index) const; + + // Returns the number of TestPartResult objects in the array. + int size() const; + + private: + std::vector array_; + + TestPartResultArray(const TestPartResultArray&) = delete; + TestPartResultArray& operator=(const TestPartResultArray&) = delete; +}; + +// This interface knows how to report a test part result. +class GTEST_API_ TestPartResultReporterInterface { + public: + virtual ~TestPartResultReporterInterface() {} + + virtual void ReportTestPartResult(const TestPartResult& result) = 0; +}; + +namespace internal { + +// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a +// statement generates new fatal failures. To do so it registers itself as the +// current test part result reporter. Besides checking if fatal failures were +// reported, it only delegates the reporting to the former result reporter. +// The original result reporter is restored in the destructor. +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +class GTEST_API_ HasNewFatalFailureHelper + : public TestPartResultReporterInterface { + public: + HasNewFatalFailureHelper(); + ~HasNewFatalFailureHelper() override; + void ReportTestPartResult(const TestPartResult& result) override; + bool has_new_fatal_failure() const { return has_new_fatal_failure_; } + + private: + bool has_new_fatal_failure_; + TestPartResultReporterInterface* original_reporter_; + + HasNewFatalFailureHelper(const HasNewFatalFailureHelper&) = delete; + HasNewFatalFailureHelper& operator=(const HasNewFatalFailureHelper&) = delete; +}; + +} // namespace internal + +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ diff --git a/test/gtest/include/gtest/gtest-typed-test.h b/test/gtest/include/gtest/gtest-typed-test.h new file mode 100644 index 0000000000..bd35a32660 --- /dev/null +++ b/test/gtest/include/gtest/gtest-typed-test.h @@ -0,0 +1,331 @@ +// Copyright 2008 Google Inc. +// All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ + +// This header implements typed tests and type-parameterized tests. + +// Typed (aka type-driven) tests repeat the same test for types in a +// list. You must know which types you want to test with when writing +// typed tests. Here's how you do it: + +#if 0 + +// First, define a fixture class template. It should be parameterized +// by a type. Remember to derive it from testing::Test. +template +class FooTest : public testing::Test { + public: + ... + typedef std::list List; + static T shared_; + T value_; +}; + +// Next, associate a list of types with the test suite, which will be +// repeated for each type in the list. The typedef is necessary for +// the macro to parse correctly. +typedef testing::Types MyTypes; +TYPED_TEST_SUITE(FooTest, MyTypes); + +// If the type list contains only one type, you can write that type +// directly without Types<...>: +// TYPED_TEST_SUITE(FooTest, int); + +// Then, use TYPED_TEST() instead of TEST_F() to define as many typed +// tests for this test suite as you want. +TYPED_TEST(FooTest, DoesBlah) { + // Inside a test, refer to the special name TypeParam to get the type + // parameter. Since we are inside a derived class template, C++ requires + // us to visit the members of FooTest via 'this'. + TypeParam n = this->value_; + + // To visit static members of the fixture, add the TestFixture:: + // prefix. + n += TestFixture::shared_; + + // To refer to typedefs in the fixture, add the "typename + // TestFixture::" prefix. + typename TestFixture::List values; + values.push_back(n); + ... +} + +TYPED_TEST(FooTest, HasPropertyA) { ... } + +// TYPED_TEST_SUITE takes an optional third argument which allows to specify a +// class that generates custom test name suffixes based on the type. This should +// be a class which has a static template function GetName(int index) returning +// a string for each type. The provided integer index equals the index of the +// type in the provided type list. In many cases the index can be ignored. +// +// For example: +// class MyTypeNames { +// public: +// template +// static std::string GetName(int) { +// if (std::is_same()) return "char"; +// if (std::is_same()) return "int"; +// if (std::is_same()) return "unsignedInt"; +// } +// }; +// TYPED_TEST_SUITE(FooTest, MyTypes, MyTypeNames); + +#endif // 0 + +// Type-parameterized tests are abstract test patterns parameterized +// by a type. Compared with typed tests, type-parameterized tests +// allow you to define the test pattern without knowing what the type +// parameters are. The defined pattern can be instantiated with +// different types any number of times, in any number of translation +// units. +// +// If you are designing an interface or concept, you can define a +// suite of type-parameterized tests to verify properties that any +// valid implementation of the interface/concept should have. Then, +// each implementation can easily instantiate the test suite to verify +// that it conforms to the requirements, without having to write +// similar tests repeatedly. Here's an example: + +#if 0 + +// First, define a fixture class template. It should be parameterized +// by a type. Remember to derive it from testing::Test. +template +class FooTest : public testing::Test { + ... +}; + +// Next, declare that you will define a type-parameterized test suite +// (the _P suffix is for "parameterized" or "pattern", whichever you +// prefer): +TYPED_TEST_SUITE_P(FooTest); + +// Then, use TYPED_TEST_P() to define as many type-parameterized tests +// for this type-parameterized test suite as you want. +TYPED_TEST_P(FooTest, DoesBlah) { + // Inside a test, refer to TypeParam to get the type parameter. + TypeParam n = 0; + ... +} + +TYPED_TEST_P(FooTest, HasPropertyA) { ... } + +// Now the tricky part: you need to register all test patterns before +// you can instantiate them. The first argument of the macro is the +// test suite name; the rest are the names of the tests in this test +// case. +REGISTER_TYPED_TEST_SUITE_P(FooTest, + DoesBlah, HasPropertyA); + +// Finally, you are free to instantiate the pattern with the types you +// want. If you put the above code in a header file, you can #include +// it in multiple C++ source files and instantiate it multiple times. +// +// To distinguish different instances of the pattern, the first +// argument to the INSTANTIATE_* macro is a prefix that will be added +// to the actual test suite name. Remember to pick unique prefixes for +// different instances. +typedef testing::Types MyTypes; +INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); + +// If the type list contains only one type, you can write that type +// directly without Types<...>: +// INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, int); +// +// Similar to the optional argument of TYPED_TEST_SUITE above, +// INSTANTIATE_TEST_SUITE_P takes an optional fourth argument which allows to +// generate custom names. +// INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes, MyTypeNames); + +#endif // 0 + +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-port.h" +#include "gtest/internal/gtest-type-util.h" + +// Implements typed tests. + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Expands to the name of the typedef for the type parameters of the +// given test suite. +#define GTEST_TYPE_PARAMS_(TestSuiteName) gtest_type_params_##TestSuiteName##_ + +// Expands to the name of the typedef for the NameGenerator, responsible for +// creating the suffixes of the name. +#define GTEST_NAME_GENERATOR_(TestSuiteName) \ + gtest_type_params_##TestSuiteName##_NameGenerator + +#define TYPED_TEST_SUITE(CaseName, Types, ...) \ + typedef ::testing::internal::GenerateTypeList::type \ + GTEST_TYPE_PARAMS_(CaseName); \ + typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \ + GTEST_NAME_GENERATOR_(CaseName) + +#define TYPED_TEST(CaseName, TestName) \ + static_assert(sizeof(GTEST_STRINGIFY_(TestName)) > 1, \ + "test-name must not be empty"); \ + template \ + class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ + : public CaseName { \ + private: \ + typedef CaseName TestFixture; \ + typedef gtest_TypeParam_ TypeParam; \ + void TestBody() override; \ + }; \ + static bool gtest_##CaseName##_##TestName##_registered_ \ + GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \ + CaseName, \ + ::testing::internal::TemplateSel, \ + GTEST_TYPE_PARAMS_( \ + CaseName)>::Register("", \ + ::testing::internal::CodeLocation( \ + __FILE__, __LINE__), \ + GTEST_STRINGIFY_(CaseName), \ + GTEST_STRINGIFY_(TestName), 0, \ + ::testing::internal::GenerateNames< \ + GTEST_NAME_GENERATOR_(CaseName), \ + GTEST_TYPE_PARAMS_(CaseName)>()); \ + template \ + void GTEST_TEST_CLASS_NAME_(CaseName, \ + TestName)::TestBody() + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +#define TYPED_TEST_CASE \ + static_assert(::testing::internal::TypedTestCaseIsDeprecated(), ""); \ + TYPED_TEST_SUITE +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +// Implements type-parameterized tests. + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Expands to the namespace name that the type-parameterized tests for +// the given type-parameterized test suite are defined in. The exact +// name of the namespace is subject to change without notice. +#define GTEST_SUITE_NAMESPACE_(TestSuiteName) gtest_suite_##TestSuiteName##_ + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Expands to the name of the variable used to remember the names of +// the defined tests in the given test suite. +#define GTEST_TYPED_TEST_SUITE_P_STATE_(TestSuiteName) \ + gtest_typed_test_suite_p_state_##TestSuiteName##_ + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY. +// +// Expands to the name of the variable used to remember the names of +// the registered tests in the given test suite. +#define GTEST_REGISTERED_TEST_NAMES_(TestSuiteName) \ + gtest_registered_test_names_##TestSuiteName##_ + +// The variables defined in the type-parameterized test macros are +// static as typically these macros are used in a .h file that can be +// #included in multiple translation units linked together. +#define TYPED_TEST_SUITE_P(SuiteName) \ + static ::testing::internal::TypedTestSuitePState \ + GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName) + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +#define TYPED_TEST_CASE_P \ + static_assert(::testing::internal::TypedTestCase_P_IsDeprecated(), ""); \ + TYPED_TEST_SUITE_P +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +#define TYPED_TEST_P(SuiteName, TestName) \ + namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ + template \ + class TestName : public SuiteName { \ + private: \ + typedef SuiteName TestFixture; \ + typedef gtest_TypeParam_ TypeParam; \ + void TestBody() override; \ + }; \ + static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \ + GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \ + __FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \ + GTEST_STRINGIFY_(TestName)); \ + } \ + template \ + void GTEST_SUITE_NAMESPACE_( \ + SuiteName)::TestName::TestBody() + +// Note: this won't work correctly if the trailing arguments are macros. +#define REGISTER_TYPED_TEST_SUITE_P(SuiteName, ...) \ + namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ + typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \ + } \ + static const char* const GTEST_REGISTERED_TEST_NAMES_( \ + SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \ + GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \ + GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__) + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +#define REGISTER_TYPED_TEST_CASE_P \ + static_assert(::testing::internal::RegisterTypedTestCase_P_IsDeprecated(), \ + ""); \ + REGISTER_TYPED_TEST_SUITE_P +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \ + static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \ + "test-suit-prefix must not be empty"); \ + static bool gtest_##Prefix##_##SuiteName GTEST_ATTRIBUTE_UNUSED_ = \ + ::testing::internal::TypeParameterizedTestSuite< \ + SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \ + ::testing::internal::GenerateTypeList::type>:: \ + Register(GTEST_STRINGIFY_(Prefix), \ + ::testing::internal::CodeLocation(__FILE__, __LINE__), \ + >EST_TYPED_TEST_SUITE_P_STATE_(SuiteName), \ + GTEST_STRINGIFY_(SuiteName), \ + GTEST_REGISTERED_TEST_NAMES_(SuiteName), \ + ::testing::internal::GenerateNames< \ + ::testing::internal::NameGeneratorSelector< \ + __VA_ARGS__>::type, \ + ::testing::internal::GenerateTypeList::type>()) + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +#define INSTANTIATE_TYPED_TEST_CASE_P \ + static_assert( \ + ::testing::internal::InstantiateTypedTestCase_P_IsDeprecated(), ""); \ + INSTANTIATE_TYPED_TEST_SUITE_P +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ diff --git a/test/gtest/include/gtest/gtest.h b/test/gtest/include/gtest/gtest.h new file mode 100644 index 0000000000..d19a587a18 --- /dev/null +++ b/test/gtest/include/gtest/gtest.h @@ -0,0 +1,2297 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file defines the public API for Google Test. It should be +// included by any test program that uses Google Test. +// +// IMPORTANT NOTE: Due to limitation of the C++ language, we have to +// leave some internal implementation details in this header file. +// They are clearly marked by comments like this: +// +// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +// +// Such code is NOT meant to be used by a user directly, and is subject +// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user +// program! +// +// Acknowledgment: Google Test borrowed the idea of automatic test +// registration from Barthelemy Dagenais' (barthelemy@prologique.com) +// easyUnit framework. + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_H_ + +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest-assertion-result.h" +#include "gtest/gtest-death-test.h" +#include "gtest/gtest-matchers.h" +#include "gtest/gtest-message.h" +#include "gtest/gtest-param-test.h" +#include "gtest/gtest-printers.h" +#include "gtest/gtest-test-part.h" +#include "gtest/gtest-typed-test.h" +#include "gtest/gtest_pred_impl.h" +#include "gtest/gtest_prod.h" +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-string.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +// Declares the flags. + +// This flag temporary enables the disabled tests. +GTEST_DECLARE_bool_(also_run_disabled_tests); + +// This flag brings the debugger on an assertion failure. +GTEST_DECLARE_bool_(break_on_failure); + +// This flag controls whether Google Test catches all test-thrown exceptions +// and logs them as failures. +GTEST_DECLARE_bool_(catch_exceptions); + +// This flag enables using colors in terminal output. Available values are +// "yes" to enable colors, "no" (disable colors), or "auto" (the default) +// to let Google Test decide. +GTEST_DECLARE_string_(color); + +// This flag controls whether the test runner should continue execution past +// first failure. +GTEST_DECLARE_bool_(fail_fast); + +// This flag sets up the filter to select by name using a glob pattern +// the tests to run. If the filter is not given all tests are executed. +GTEST_DECLARE_string_(filter); + +// This flag controls whether Google Test installs a signal handler that dumps +// debugging information when fatal signals are raised. +GTEST_DECLARE_bool_(install_failure_signal_handler); + +// This flag causes the Google Test to list tests. None of the tests listed +// are actually run if the flag is provided. +GTEST_DECLARE_bool_(list_tests); + +// This flag controls whether Google Test emits a detailed XML report to a file +// in addition to its normal textual output. +GTEST_DECLARE_string_(output); + +// This flags control whether Google Test prints only test failures. +GTEST_DECLARE_bool_(brief); + +// This flags control whether Google Test prints the elapsed time for each +// test. +GTEST_DECLARE_bool_(print_time); + +// This flags control whether Google Test prints UTF8 characters as text. +GTEST_DECLARE_bool_(print_utf8); + +// This flag specifies the random number seed. +GTEST_DECLARE_int32_(random_seed); + +// This flag sets how many times the tests are repeated. The default value +// is 1. If the value is -1 the tests are repeating forever. +GTEST_DECLARE_int32_(repeat); + +// This flag controls whether Google Test Environments are recreated for each +// repeat of the tests. The default value is true. If set to false the global +// test Environment objects are only set up once, for the first iteration, and +// only torn down once, for the last. +GTEST_DECLARE_bool_(recreate_environments_when_repeating); + +// This flag controls whether Google Test includes Google Test internal +// stack frames in failure stack traces. +GTEST_DECLARE_bool_(show_internal_stack_frames); + +// When this flag is specified, tests' order is randomized on every iteration. +GTEST_DECLARE_bool_(shuffle); + +// This flag specifies the maximum number of stack frames to be +// printed in a failure message. +GTEST_DECLARE_int32_(stack_trace_depth); + +// When this flag is specified, a failed assertion will throw an +// exception if exceptions are enabled, or exit the program with a +// non-zero code otherwise. For use with an external test framework. +GTEST_DECLARE_bool_(throw_on_failure); + +// When this flag is set with a "host:port" string, on supported +// platforms test results are streamed to the specified port on +// the specified host machine. +GTEST_DECLARE_string_(stream_result_to); + +#if GTEST_USE_OWN_FLAGFILE_FLAG_ +GTEST_DECLARE_string_(flagfile); +#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ + +namespace testing { + +// Silence C4100 (unreferenced formal parameter) and 4805 +// unsafe mix of type 'const int' and type 'const bool' +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4805) +#pragma warning(disable : 4100) +#endif + +// The upper limit for valid stack trace depths. +const int kMaxStackTraceDepth = 100; + +namespace internal { + +class AssertHelper; +class DefaultGlobalTestPartResultReporter; +class ExecDeathTest; +class NoExecDeathTest; +class FinalSuccessChecker; +class GTestFlagSaver; +class StreamingListenerTest; +class TestResultAccessor; +class TestEventListenersAccessor; +class TestEventRepeater; +class UnitTestRecordPropertyTestHelper; +class WindowsDeathTest; +class FuchsiaDeathTest; +class UnitTestImpl* GetUnitTestImpl(); +void ReportFailureInUnknownLocation(TestPartResult::Type result_type, + const std::string& message); +std::set* GetIgnoredParameterizedTestSuites(); + +} // namespace internal + +// The friend relationship of some of these classes is cyclic. +// If we don't forward declare them the compiler might confuse the classes +// in friendship clauses with same named classes on the scope. +class Test; +class TestSuite; + +// Old API is still available but deprecated +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +using TestCase = TestSuite; +#endif +class TestInfo; +class UnitTest; + +// The abstract class that all tests inherit from. +// +// In Google Test, a unit test program contains one or many TestSuites, and +// each TestSuite contains one or many Tests. +// +// When you define a test using the TEST macro, you don't need to +// explicitly derive from Test - the TEST macro automatically does +// this for you. +// +// The only time you derive from Test is when defining a test fixture +// to be used in a TEST_F. For example: +// +// class FooTest : public testing::Test { +// protected: +// void SetUp() override { ... } +// void TearDown() override { ... } +// ... +// }; +// +// TEST_F(FooTest, Bar) { ... } +// TEST_F(FooTest, Baz) { ... } +// +// Test is not copyable. +class GTEST_API_ Test { + public: + friend class TestInfo; + + // The d'tor is virtual as we intend to inherit from Test. + virtual ~Test(); + + // Sets up the stuff shared by all tests in this test suite. + // + // Google Test will call Foo::SetUpTestSuite() before running the first + // test in test suite Foo. Hence a sub-class can define its own + // SetUpTestSuite() method to shadow the one defined in the super + // class. + static void SetUpTestSuite() {} + + // Tears down the stuff shared by all tests in this test suite. + // + // Google Test will call Foo::TearDownTestSuite() after running the last + // test in test suite Foo. Hence a sub-class can define its own + // TearDownTestSuite() method to shadow the one defined in the super + // class. + static void TearDownTestSuite() {} + + // Legacy API is deprecated but still available. Use SetUpTestSuite and + // TearDownTestSuite instead. +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + static void TearDownTestCase() {} + static void SetUpTestCase() {} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Returns true if and only if the current test has a fatal failure. + static bool HasFatalFailure(); + + // Returns true if and only if the current test has a non-fatal failure. + static bool HasNonfatalFailure(); + + // Returns true if and only if the current test was skipped. + static bool IsSkipped(); + + // Returns true if and only if the current test has a (either fatal or + // non-fatal) failure. + static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } + + // Logs a property for the current test, test suite, or for the entire + // invocation of the test program when used outside of the context of a + // test suite. Only the last value for a given key is remembered. These + // are public static so they can be called from utility functions that are + // not members of the test fixture. Calls to RecordProperty made during + // lifespan of the test (from the moment its constructor starts to the + // moment its destructor finishes) will be output in XML as attributes of + // the element. Properties recorded from fixture's + // SetUpTestSuite or TearDownTestSuite are logged as attributes of the + // corresponding element. Calls to RecordProperty made in the + // global context (before or after invocation of RUN_ALL_TESTS and from + // SetUp/TearDown method of Environment objects registered with Google + // Test) will be output as attributes of the element. + static void RecordProperty(const std::string& key, const std::string& value); + static void RecordProperty(const std::string& key, int value); + + protected: + // Creates a Test object. + Test(); + + // Sets up the test fixture. + virtual void SetUp(); + + // Tears down the test fixture. + virtual void TearDown(); + + private: + // Returns true if and only if the current test has the same fixture class + // as the first test in the current test suite. + static bool HasSameFixtureClass(); + + // Runs the test after the test fixture has been set up. + // + // A sub-class must implement this to define the test logic. + // + // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. + // Instead, use the TEST or TEST_F macro. + virtual void TestBody() = 0; + + // Sets up, executes, and tears down the test. + void Run(); + + // Deletes self. We deliberately pick an unusual name for this + // internal method to avoid clashing with names used in user TESTs. + void DeleteSelf_() { delete this; } + + const std::unique_ptr gtest_flag_saver_; + + // Often a user misspells SetUp() as Setup() and spends a long time + // wondering why it is never called by Google Test. The declaration of + // the following method is solely for catching such an error at + // compile time: + // + // - The return type is deliberately chosen to be not void, so it + // will be a conflict if void Setup() is declared in the user's + // test fixture. + // + // - This method is private, so it will be another compiler error + // if the method is called from the user's test fixture. + // + // DO NOT OVERRIDE THIS FUNCTION. + // + // If you see an error about overriding the following function or + // about it being private, you have mis-spelled SetUp() as Setup(). + struct Setup_should_be_spelled_SetUp {}; + virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } + + // We disallow copying Tests. + Test(const Test&) = delete; + Test& operator=(const Test&) = delete; +}; + +typedef internal::TimeInMillis TimeInMillis; + +// A copyable object representing a user specified test property which can be +// output as a key/value string pair. +// +// Don't inherit from TestProperty as its destructor is not virtual. +class TestProperty { + public: + // C'tor. TestProperty does NOT have a default constructor. + // Always use this constructor (with parameters) to create a + // TestProperty object. + TestProperty(const std::string& a_key, const std::string& a_value) + : key_(a_key), value_(a_value) {} + + // Gets the user supplied key. + const char* key() const { return key_.c_str(); } + + // Gets the user supplied value. + const char* value() const { return value_.c_str(); } + + // Sets a new value, overriding the one supplied in the constructor. + void SetValue(const std::string& new_value) { value_ = new_value; } + + private: + // The key supplied by the user. + std::string key_; + // The value supplied by the user. + std::string value_; +}; + +// The result of a single Test. This includes a list of +// TestPartResults, a list of TestProperties, a count of how many +// death tests there are in the Test, and how much time it took to run +// the Test. +// +// TestResult is not copyable. +class GTEST_API_ TestResult { + public: + // Creates an empty TestResult. + TestResult(); + + // D'tor. Do not inherit from TestResult. + ~TestResult(); + + // Gets the number of all test parts. This is the sum of the number + // of successful test parts and the number of failed test parts. + int total_part_count() const; + + // Returns the number of the test properties. + int test_property_count() const; + + // Returns true if and only if the test passed (i.e. no test part failed). + bool Passed() const { return !Skipped() && !Failed(); } + + // Returns true if and only if the test was skipped. + bool Skipped() const; + + // Returns true if and only if the test failed. + bool Failed() const; + + // Returns true if and only if the test fatally failed. + bool HasFatalFailure() const; + + // Returns true if and only if the test has a non-fatal failure. + bool HasNonfatalFailure() const; + + // Returns the elapsed time, in milliseconds. + TimeInMillis elapsed_time() const { return elapsed_time_; } + + // Gets the time of the test case start, in ms from the start of the + // UNIX epoch. + TimeInMillis start_timestamp() const { return start_timestamp_; } + + // Returns the i-th test part result among all the results. i can range from 0 + // to total_part_count() - 1. If i is not in that range, aborts the program. + const TestPartResult& GetTestPartResult(int i) const; + + // Returns the i-th test property. i can range from 0 to + // test_property_count() - 1. If i is not in that range, aborts the + // program. + const TestProperty& GetTestProperty(int i) const; + + private: + friend class TestInfo; + friend class TestSuite; + friend class UnitTest; + friend class internal::DefaultGlobalTestPartResultReporter; + friend class internal::ExecDeathTest; + friend class internal::TestResultAccessor; + friend class internal::UnitTestImpl; + friend class internal::WindowsDeathTest; + friend class internal::FuchsiaDeathTest; + + // Gets the vector of TestPartResults. + const std::vector& test_part_results() const { + return test_part_results_; + } + + // Gets the vector of TestProperties. + const std::vector& test_properties() const { + return test_properties_; + } + + // Sets the start time. + void set_start_timestamp(TimeInMillis start) { start_timestamp_ = start; } + + // Sets the elapsed time. + void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } + + // Adds a test property to the list. The property is validated and may add + // a non-fatal failure if invalid (e.g., if it conflicts with reserved + // key names). If a property is already recorded for the same key, the + // value will be updated, rather than storing multiple values for the same + // key. xml_element specifies the element for which the property is being + // recorded and is used for validation. + void RecordProperty(const std::string& xml_element, + const TestProperty& test_property); + + // Adds a failure if the key is a reserved attribute of Google Test + // testsuite tags. Returns true if the property is valid. + // FIXME: Validate attribute names are legal and human readable. + static bool ValidateTestProperty(const std::string& xml_element, + const TestProperty& test_property); + + // Adds a test part result to the list. + void AddTestPartResult(const TestPartResult& test_part_result); + + // Returns the death test count. + int death_test_count() const { return death_test_count_; } + + // Increments the death test count, returning the new count. + int increment_death_test_count() { return ++death_test_count_; } + + // Clears the test part results. + void ClearTestPartResults(); + + // Clears the object. + void Clear(); + + // Protects mutable state of the property vector and of owned + // properties, whose values may be updated. + internal::Mutex test_properties_mutex_; + + // The vector of TestPartResults + std::vector test_part_results_; + // The vector of TestProperties + std::vector test_properties_; + // Running count of death tests. + int death_test_count_; + // The start time, in milliseconds since UNIX Epoch. + TimeInMillis start_timestamp_; + // The elapsed time, in milliseconds. + TimeInMillis elapsed_time_; + + // We disallow copying TestResult. + TestResult(const TestResult&) = delete; + TestResult& operator=(const TestResult&) = delete; +}; // class TestResult + +// A TestInfo object stores the following information about a test: +// +// Test suite name +// Test name +// Whether the test should be run +// A function pointer that creates the test object when invoked +// Test result +// +// The constructor of TestInfo registers itself with the UnitTest +// singleton such that the RUN_ALL_TESTS() macro knows which tests to +// run. +class GTEST_API_ TestInfo { + public: + // Destructs a TestInfo object. This function is not virtual, so + // don't inherit from TestInfo. + ~TestInfo(); + + // Returns the test suite name. + const char* test_suite_name() const { return test_suite_name_.c_str(); } + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + const char* test_case_name() const { return test_suite_name(); } +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Returns the test name. + const char* name() const { return name_.c_str(); } + + // Returns the name of the parameter type, or NULL if this is not a typed + // or a type-parameterized test. + const char* type_param() const { + if (type_param_.get() != nullptr) return type_param_->c_str(); + return nullptr; + } + + // Returns the text representation of the value parameter, or NULL if this + // is not a value-parameterized test. + const char* value_param() const { + if (value_param_.get() != nullptr) return value_param_->c_str(); + return nullptr; + } + + // Returns the file name where this test is defined. + const char* file() const { return location_.file.c_str(); } + + // Returns the line where this test is defined. + int line() const { return location_.line; } + + // Return true if this test should not be run because it's in another shard. + bool is_in_another_shard() const { return is_in_another_shard_; } + + // Returns true if this test should run, that is if the test is not + // disabled (or it is disabled but the also_run_disabled_tests flag has + // been specified) and its full name matches the user-specified filter. + // + // Google Test allows the user to filter the tests by their full names. + // The full name of a test Bar in test suite Foo is defined as + // "Foo.Bar". Only the tests that match the filter will run. + // + // A filter is a colon-separated list of glob (not regex) patterns, + // optionally followed by a '-' and a colon-separated list of + // negative patterns (tests to exclude). A test is run if it + // matches one of the positive patterns and does not match any of + // the negative patterns. + // + // For example, *A*:Foo.* is a filter that matches any string that + // contains the character 'A' or starts with "Foo.". + bool should_run() const { return should_run_; } + + // Returns true if and only if this test will appear in the XML report. + bool is_reportable() const { + // The XML report includes tests matching the filter, excluding those + // run in other shards. + return matches_filter_ && !is_in_another_shard_; + } + + // Returns the result of the test. + const TestResult* result() const { return &result_; } + + private: +#if GTEST_HAS_DEATH_TEST + friend class internal::DefaultDeathTestFactory; +#endif // GTEST_HAS_DEATH_TEST + friend class Test; + friend class TestSuite; + friend class internal::UnitTestImpl; + friend class internal::StreamingListenerTest; + friend TestInfo* internal::MakeAndRegisterTestInfo( + const char* test_suite_name, const char* name, const char* type_param, + const char* value_param, internal::CodeLocation code_location, + internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc, + internal::TestFactoryBase* factory); + + // Constructs a TestInfo object. The newly constructed instance assumes + // ownership of the factory object. + TestInfo(const std::string& test_suite_name, const std::string& name, + const char* a_type_param, // NULL if not a type-parameterized test + const char* a_value_param, // NULL if not a value-parameterized test + internal::CodeLocation a_code_location, + internal::TypeId fixture_class_id, + internal::TestFactoryBase* factory); + + // Increments the number of death tests encountered in this test so + // far. + int increment_death_test_count() { + return result_.increment_death_test_count(); + } + + // Creates the test object, runs it, records its result, and then + // deletes it. + void Run(); + + // Skip and records the test result for this object. + void Skip(); + + static void ClearTestResult(TestInfo* test_info) { + test_info->result_.Clear(); + } + + // These fields are immutable properties of the test. + const std::string test_suite_name_; // test suite name + const std::string name_; // Test name + // Name of the parameter type, or NULL if this is not a typed or a + // type-parameterized test. + const std::unique_ptr type_param_; + // Text representation of the value parameter, or NULL if this is not a + // value-parameterized test. + const std::unique_ptr value_param_; + internal::CodeLocation location_; + const internal::TypeId fixture_class_id_; // ID of the test fixture class + bool should_run_; // True if and only if this test should run + bool is_disabled_; // True if and only if this test is disabled + bool matches_filter_; // True if this test matches the + // user-specified filter. + bool is_in_another_shard_; // Will be run in another shard. + internal::TestFactoryBase* const factory_; // The factory that creates + // the test object + + // This field is mutable and needs to be reset before running the + // test for the second time. + TestResult result_; + + TestInfo(const TestInfo&) = delete; + TestInfo& operator=(const TestInfo&) = delete; +}; + +// A test suite, which consists of a vector of TestInfos. +// +// TestSuite is not copyable. +class GTEST_API_ TestSuite { + public: + // Creates a TestSuite with the given name. + // + // TestSuite does NOT have a default constructor. Always use this + // constructor to create a TestSuite object. + // + // Arguments: + // + // name: name of the test suite + // a_type_param: the name of the test's type parameter, or NULL if + // this is not a type-parameterized test. + // set_up_tc: pointer to the function that sets up the test suite + // tear_down_tc: pointer to the function that tears down the test suite + TestSuite(const char* name, const char* a_type_param, + internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc); + + // Destructor of TestSuite. + virtual ~TestSuite(); + + // Gets the name of the TestSuite. + const char* name() const { return name_.c_str(); } + + // Returns the name of the parameter type, or NULL if this is not a + // type-parameterized test suite. + const char* type_param() const { + if (type_param_.get() != nullptr) return type_param_->c_str(); + return nullptr; + } + + // Returns true if any test in this test suite should run. + bool should_run() const { return should_run_; } + + // Gets the number of successful tests in this test suite. + int successful_test_count() const; + + // Gets the number of skipped tests in this test suite. + int skipped_test_count() const; + + // Gets the number of failed tests in this test suite. + int failed_test_count() const; + + // Gets the number of disabled tests that will be reported in the XML report. + int reportable_disabled_test_count() const; + + // Gets the number of disabled tests in this test suite. + int disabled_test_count() const; + + // Gets the number of tests to be printed in the XML report. + int reportable_test_count() const; + + // Get the number of tests in this test suite that should run. + int test_to_run_count() const; + + // Gets the number of all tests in this test suite. + int total_test_count() const; + + // Returns true if and only if the test suite passed. + bool Passed() const { return !Failed(); } + + // Returns true if and only if the test suite failed. + bool Failed() const { + return failed_test_count() > 0 || ad_hoc_test_result().Failed(); + } + + // Returns the elapsed time, in milliseconds. + TimeInMillis elapsed_time() const { return elapsed_time_; } + + // Gets the time of the test suite start, in ms from the start of the + // UNIX epoch. + TimeInMillis start_timestamp() const { return start_timestamp_; } + + // Returns the i-th test among all the tests. i can range from 0 to + // total_test_count() - 1. If i is not in that range, returns NULL. + const TestInfo* GetTestInfo(int i) const; + + // Returns the TestResult that holds test properties recorded during + // execution of SetUpTestSuite and TearDownTestSuite. + const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } + + private: + friend class Test; + friend class internal::UnitTestImpl; + + // Gets the (mutable) vector of TestInfos in this TestSuite. + std::vector& test_info_list() { return test_info_list_; } + + // Gets the (immutable) vector of TestInfos in this TestSuite. + const std::vector& test_info_list() const { + return test_info_list_; + } + + // Returns the i-th test among all the tests. i can range from 0 to + // total_test_count() - 1. If i is not in that range, returns NULL. + TestInfo* GetMutableTestInfo(int i); + + // Sets the should_run member. + void set_should_run(bool should) { should_run_ = should; } + + // Adds a TestInfo to this test suite. Will delete the TestInfo upon + // destruction of the TestSuite object. + void AddTestInfo(TestInfo* test_info); + + // Clears the results of all tests in this test suite. + void ClearResult(); + + // Clears the results of all tests in the given test suite. + static void ClearTestSuiteResult(TestSuite* test_suite) { + test_suite->ClearResult(); + } + + // Runs every test in this TestSuite. + void Run(); + + // Skips the execution of tests under this TestSuite + void Skip(); + + // Runs SetUpTestSuite() for this TestSuite. This wrapper is needed + // for catching exceptions thrown from SetUpTestSuite(). + void RunSetUpTestSuite() { + if (set_up_tc_ != nullptr) { + (*set_up_tc_)(); + } + } + + // Runs TearDownTestSuite() for this TestSuite. This wrapper is + // needed for catching exceptions thrown from TearDownTestSuite(). + void RunTearDownTestSuite() { + if (tear_down_tc_ != nullptr) { + (*tear_down_tc_)(); + } + } + + // Returns true if and only if test passed. + static bool TestPassed(const TestInfo* test_info) { + return test_info->should_run() && test_info->result()->Passed(); + } + + // Returns true if and only if test skipped. + static bool TestSkipped(const TestInfo* test_info) { + return test_info->should_run() && test_info->result()->Skipped(); + } + + // Returns true if and only if test failed. + static bool TestFailed(const TestInfo* test_info) { + return test_info->should_run() && test_info->result()->Failed(); + } + + // Returns true if and only if the test is disabled and will be reported in + // the XML report. + static bool TestReportableDisabled(const TestInfo* test_info) { + return test_info->is_reportable() && test_info->is_disabled_; + } + + // Returns true if and only if test is disabled. + static bool TestDisabled(const TestInfo* test_info) { + return test_info->is_disabled_; + } + + // Returns true if and only if this test will appear in the XML report. + static bool TestReportable(const TestInfo* test_info) { + return test_info->is_reportable(); + } + + // Returns true if the given test should run. + static bool ShouldRunTest(const TestInfo* test_info) { + return test_info->should_run(); + } + + // Shuffles the tests in this test suite. + void ShuffleTests(internal::Random* random); + + // Restores the test order to before the first shuffle. + void UnshuffleTests(); + + // Name of the test suite. + std::string name_; + // Name of the parameter type, or NULL if this is not a typed or a + // type-parameterized test. + const std::unique_ptr type_param_; + // The vector of TestInfos in their original order. It owns the + // elements in the vector. + std::vector test_info_list_; + // Provides a level of indirection for the test list to allow easy + // shuffling and restoring the test order. The i-th element in this + // vector is the index of the i-th test in the shuffled test list. + std::vector test_indices_; + // Pointer to the function that sets up the test suite. + internal::SetUpTestSuiteFunc set_up_tc_; + // Pointer to the function that tears down the test suite. + internal::TearDownTestSuiteFunc tear_down_tc_; + // True if and only if any test in this test suite should run. + bool should_run_; + // The start time, in milliseconds since UNIX Epoch. + TimeInMillis start_timestamp_; + // Elapsed time, in milliseconds. + TimeInMillis elapsed_time_; + // Holds test properties recorded during execution of SetUpTestSuite and + // TearDownTestSuite. + TestResult ad_hoc_test_result_; + + // We disallow copying TestSuites. + TestSuite(const TestSuite&) = delete; + TestSuite& operator=(const TestSuite&) = delete; +}; + +// An Environment object is capable of setting up and tearing down an +// environment. You should subclass this to define your own +// environment(s). +// +// An Environment object does the set-up and tear-down in virtual +// methods SetUp() and TearDown() instead of the constructor and the +// destructor, as: +// +// 1. You cannot safely throw from a destructor. This is a problem +// as in some cases Google Test is used where exceptions are enabled, and +// we may want to implement ASSERT_* using exceptions where they are +// available. +// 2. You cannot use ASSERT_* directly in a constructor or +// destructor. +class Environment { + public: + // The d'tor is virtual as we need to subclass Environment. + virtual ~Environment() {} + + // Override this to define how to set up the environment. + virtual void SetUp() {} + + // Override this to define how to tear down the environment. + virtual void TearDown() {} + + private: + // If you see an error about overriding the following function or + // about it being private, you have mis-spelled SetUp() as Setup(). + struct Setup_should_be_spelled_SetUp {}; + virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } +}; + +#if GTEST_HAS_EXCEPTIONS + +// Exception which can be thrown from TestEventListener::OnTestPartResult. +class GTEST_API_ AssertionException + : public internal::GoogleTestFailureException { + public: + explicit AssertionException(const TestPartResult& result) + : GoogleTestFailureException(result) {} +}; + +#endif // GTEST_HAS_EXCEPTIONS + +// The interface for tracing execution of tests. The methods are organized in +// the order the corresponding events are fired. +class TestEventListener { + public: + virtual ~TestEventListener() {} + + // Fired before any test activity starts. + virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; + + // Fired before each iteration of tests starts. There may be more than + // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration + // index, starting from 0. + virtual void OnTestIterationStart(const UnitTest& unit_test, + int iteration) = 0; + + // Fired before environment set-up for each iteration of tests starts. + virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; + + // Fired after environment set-up for each iteration of tests ends. + virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; + + // Fired before the test suite starts. + virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {} + + // Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Fired before the test starts. + virtual void OnTestStart(const TestInfo& test_info) = 0; + + // Fired when a test is disabled + virtual void OnTestDisabled(const TestInfo& /*test_info*/) {} + + // Fired after a failed assertion or a SUCCEED() invocation. + // If you want to throw an exception from this function to skip to the next + // TEST, it must be AssertionException defined above, or inherited from it. + virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; + + // Fired after the test ends. + virtual void OnTestEnd(const TestInfo& test_info) = 0; + + // Fired after the test suite ends. + virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {} + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Fired before environment tear-down for each iteration of tests starts. + virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; + + // Fired after environment tear-down for each iteration of tests ends. + virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; + + // Fired after each iteration of tests finishes. + virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration) = 0; + + // Fired after all test activities have ended. + virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; +}; + +// The convenience class for users who need to override just one or two +// methods and are not concerned that a possible change to a signature of +// the methods they override will not be caught during the build. For +// comments about each method please see the definition of TestEventListener +// above. +class EmptyTestEventListener : public TestEventListener { + public: + void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} + void OnTestIterationStart(const UnitTest& /*unit_test*/, + int /*iteration*/) override {} + void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} + void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} + void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseStart(const TestCase& /*test_case*/) override {} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + void OnTestStart(const TestInfo& /*test_info*/) override {} + void OnTestDisabled(const TestInfo& /*test_info*/) override {} + void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {} + void OnTestEnd(const TestInfo& /*test_info*/) override {} + void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseEnd(const TestCase& /*test_case*/) override {} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} + void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} + void OnTestIterationEnd(const UnitTest& /*unit_test*/, + int /*iteration*/) override {} + void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} +}; + +// TestEventListeners lets users add listeners to track events in Google Test. +class GTEST_API_ TestEventListeners { + public: + TestEventListeners(); + ~TestEventListeners(); + + // Appends an event listener to the end of the list. Google Test assumes + // the ownership of the listener (i.e. it will delete the listener when + // the test program finishes). + void Append(TestEventListener* listener); + + // Removes the given event listener from the list and returns it. It then + // becomes the caller's responsibility to delete the listener. Returns + // NULL if the listener is not found in the list. + TestEventListener* Release(TestEventListener* listener); + + // Returns the standard listener responsible for the default console + // output. Can be removed from the listeners list to shut down default + // console output. Note that removing this object from the listener list + // with Release transfers its ownership to the caller and makes this + // function return NULL the next time. + TestEventListener* default_result_printer() const { + return default_result_printer_; + } + + // Returns the standard listener responsible for the default XML output + // controlled by the --gtest_output=xml flag. Can be removed from the + // listeners list by users who want to shut down the default XML output + // controlled by this flag and substitute it with custom one. Note that + // removing this object from the listener list with Release transfers its + // ownership to the caller and makes this function return NULL the next + // time. + TestEventListener* default_xml_generator() const { + return default_xml_generator_; + } + + private: + friend class TestSuite; + friend class TestInfo; + friend class internal::DefaultGlobalTestPartResultReporter; + friend class internal::NoExecDeathTest; + friend class internal::TestEventListenersAccessor; + friend class internal::UnitTestImpl; + + // Returns repeater that broadcasts the TestEventListener events to all + // subscribers. + TestEventListener* repeater(); + + // Sets the default_result_printer attribute to the provided listener. + // The listener is also added to the listener list and previous + // default_result_printer is removed from it and deleted. The listener can + // also be NULL in which case it will not be added to the list. Does + // nothing if the previous and the current listener objects are the same. + void SetDefaultResultPrinter(TestEventListener* listener); + + // Sets the default_xml_generator attribute to the provided listener. The + // listener is also added to the listener list and previous + // default_xml_generator is removed from it and deleted. The listener can + // also be NULL in which case it will not be added to the list. Does + // nothing if the previous and the current listener objects are the same. + void SetDefaultXmlGenerator(TestEventListener* listener); + + // Controls whether events will be forwarded by the repeater to the + // listeners in the list. + bool EventForwardingEnabled() const; + void SuppressEventForwarding(); + + // The actual list of listeners. + internal::TestEventRepeater* repeater_; + // Listener responsible for the standard result output. + TestEventListener* default_result_printer_; + // Listener responsible for the creation of the XML output file. + TestEventListener* default_xml_generator_; + + // We disallow copying TestEventListeners. + TestEventListeners(const TestEventListeners&) = delete; + TestEventListeners& operator=(const TestEventListeners&) = delete; +}; + +// A UnitTest consists of a vector of TestSuites. +// +// This is a singleton class. The only instance of UnitTest is +// created when UnitTest::GetInstance() is first called. This +// instance is never deleted. +// +// UnitTest is not copyable. +// +// This class is thread-safe as long as the methods are called +// according to their specification. +class GTEST_API_ UnitTest { + public: + // Gets the singleton UnitTest object. The first time this method + // is called, a UnitTest object is constructed and returned. + // Consecutive calls will return the same object. + static UnitTest* GetInstance(); + + // Runs all tests in this UnitTest object and prints the result. + // Returns 0 if successful, or 1 otherwise. + // + // This method can only be called from the main thread. + // + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + int Run() GTEST_MUST_USE_RESULT_; + + // Returns the working directory when the first TEST() or TEST_F() + // was executed. The UnitTest object owns the string. + const char* original_working_dir() const; + + // Returns the TestSuite object for the test that's currently running, + // or NULL if no test is running. + const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_); + +// Legacy API is still available but deprecated +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_); +#endif + + // Returns the TestInfo object for the test that's currently running, + // or NULL if no test is running. + const TestInfo* current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_); + + // Returns the random seed used at the start of the current test run. + int random_seed() const; + + // Returns the ParameterizedTestSuiteRegistry object used to keep track of + // value-parameterized tests and instantiate and register them. + // + // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() + GTEST_LOCK_EXCLUDED_(mutex_); + + // Gets the number of successful test suites. + int successful_test_suite_count() const; + + // Gets the number of failed test suites. + int failed_test_suite_count() const; + + // Gets the number of all test suites. + int total_test_suite_count() const; + + // Gets the number of all test suites that contain at least one test + // that should run. + int test_suite_to_run_count() const; + + // Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + int successful_test_case_count() const; + int failed_test_case_count() const; + int total_test_case_count() const; + int test_case_to_run_count() const; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Gets the number of successful tests. + int successful_test_count() const; + + // Gets the number of skipped tests. + int skipped_test_count() const; + + // Gets the number of failed tests. + int failed_test_count() const; + + // Gets the number of disabled tests that will be reported in the XML report. + int reportable_disabled_test_count() const; + + // Gets the number of disabled tests. + int disabled_test_count() const; + + // Gets the number of tests to be printed in the XML report. + int reportable_test_count() const; + + // Gets the number of all tests. + int total_test_count() const; + + // Gets the number of tests that should run. + int test_to_run_count() const; + + // Gets the time of the test program start, in ms from the start of the + // UNIX epoch. + TimeInMillis start_timestamp() const; + + // Gets the elapsed time, in milliseconds. + TimeInMillis elapsed_time() const; + + // Returns true if and only if the unit test passed (i.e. all test suites + // passed). + bool Passed() const; + + // Returns true if and only if the unit test failed (i.e. some test suite + // failed or something outside of all tests failed). + bool Failed() const; + + // Gets the i-th test suite among all the test suites. i can range from 0 to + // total_test_suite_count() - 1. If i is not in that range, returns NULL. + const TestSuite* GetTestSuite(int i) const; + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + const TestCase* GetTestCase(int i) const; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Returns the TestResult containing information on test failures and + // properties logged outside of individual test suites. + const TestResult& ad_hoc_test_result() const; + + // Returns the list of event listeners that can be used to track events + // inside Google Test. + TestEventListeners& listeners(); + + private: + // Registers and returns a global test environment. When a test + // program is run, all global test environments will be set-up in + // the order they were registered. After all tests in the program + // have finished, all global test environments will be torn-down in + // the *reverse* order they were registered. + // + // The UnitTest object takes ownership of the given environment. + // + // This method can only be called from the main thread. + Environment* AddEnvironment(Environment* env); + + // Adds a TestPartResult to the current TestResult object. All + // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) + // eventually call this to report their results. The user code + // should use the assertion macros instead of calling this directly. + void AddTestPartResult(TestPartResult::Type result_type, + const char* file_name, int line_number, + const std::string& message, + const std::string& os_stack_trace) + GTEST_LOCK_EXCLUDED_(mutex_); + + // Adds a TestProperty to the current TestResult object when invoked from + // inside a test, to current TestSuite's ad_hoc_test_result_ when invoked + // from SetUpTestSuite or TearDownTestSuite, or to the global property set + // when invoked elsewhere. If the result already contains a property with + // the same key, the value will be updated. + void RecordProperty(const std::string& key, const std::string& value); + + // Gets the i-th test suite among all the test suites. i can range from 0 to + // total_test_suite_count() - 1. If i is not in that range, returns NULL. + TestSuite* GetMutableTestSuite(int i); + + // Accessors for the implementation object. + internal::UnitTestImpl* impl() { return impl_; } + const internal::UnitTestImpl* impl() const { return impl_; } + + // These classes and functions are friends as they need to access private + // members of UnitTest. + friend class ScopedTrace; + friend class Test; + friend class internal::AssertHelper; + friend class internal::StreamingListenerTest; + friend class internal::UnitTestRecordPropertyTestHelper; + friend Environment* AddGlobalTestEnvironment(Environment* env); + friend std::set* internal::GetIgnoredParameterizedTestSuites(); + friend internal::UnitTestImpl* internal::GetUnitTestImpl(); + friend void internal::ReportFailureInUnknownLocation( + TestPartResult::Type result_type, const std::string& message); + + // Creates an empty UnitTest. + UnitTest(); + + // D'tor + virtual ~UnitTest(); + + // Pushes a trace defined by SCOPED_TRACE() on to the per-thread + // Google Test trace stack. + void PushGTestTrace(const internal::TraceInfo& trace) + GTEST_LOCK_EXCLUDED_(mutex_); + + // Pops a trace from the per-thread Google Test trace stack. + void PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_); + + // Protects mutable state in *impl_. This is mutable as some const + // methods need to lock it too. + mutable internal::Mutex mutex_; + + // Opaque implementation object. This field is never changed once + // the object is constructed. We don't mark it as const here, as + // doing so will cause a warning in the constructor of UnitTest. + // Mutable state in *impl_ is protected by mutex_. + internal::UnitTestImpl* impl_; + + // We disallow copying UnitTest. + UnitTest(const UnitTest&) = delete; + UnitTest& operator=(const UnitTest&) = delete; +}; + +// A convenient wrapper for adding an environment for the test +// program. +// +// You should call this before RUN_ALL_TESTS() is called, probably in +// main(). If you use gtest_main, you need to call this before main() +// starts for it to take effect. For example, you can define a global +// variable like this: +// +// testing::Environment* const foo_env = +// testing::AddGlobalTestEnvironment(new FooEnvironment); +// +// However, we strongly recommend you to write your own main() and +// call AddGlobalTestEnvironment() there, as relying on initialization +// of global variables makes the code harder to read and may cause +// problems when you register multiple environments from different +// translation units and the environments have dependencies among them +// (remember that the compiler doesn't guarantee the order in which +// global variables from different translation units are initialized). +inline Environment* AddGlobalTestEnvironment(Environment* env) { + return UnitTest::GetInstance()->AddEnvironment(env); +} + +// Initializes Google Test. This must be called before calling +// RUN_ALL_TESTS(). In particular, it parses a command line for the +// flags that Google Test recognizes. Whenever a Google Test flag is +// seen, it is removed from argv, and *argc is decremented. +// +// No value is returned. Instead, the Google Test flag variables are +// updated. +// +// Calling the function for the second time has no user-visible effect. +GTEST_API_ void InitGoogleTest(int* argc, char** argv); + +// This overloaded version can be used in Windows programs compiled in +// UNICODE mode. +GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); + +// This overloaded version can be used on Arduino/embedded platforms where +// there is no argc/argv. +GTEST_API_ void InitGoogleTest(); + +namespace internal { + +// Separate the error generating code from the code path to reduce the stack +// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers +// when calling EXPECT_* in a tight loop. +template +AssertionResult CmpHelperEQFailure(const char* lhs_expression, + const char* rhs_expression, const T1& lhs, + const T2& rhs) { + return EqFailure(lhs_expression, rhs_expression, + FormatForComparisonFailureMessage(lhs, rhs), + FormatForComparisonFailureMessage(rhs, lhs), false); +} + +// This block of code defines operator==/!= +// to block lexical scope lookup. +// It prevents using invalid operator==/!= defined at namespace scope. +struct faketype {}; +inline bool operator==(faketype, faketype) { return true; } +inline bool operator!=(faketype, faketype) { return false; } + +// The helper function for {ASSERT|EXPECT}_EQ. +template +AssertionResult CmpHelperEQ(const char* lhs_expression, + const char* rhs_expression, const T1& lhs, + const T2& rhs) { + if (lhs == rhs) { + return AssertionSuccess(); + } + + return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs); +} + +class EqHelper { + public: + // This templatized version is for the general case. + template < + typename T1, typename T2, + // Disable this overload for cases where one argument is a pointer + // and the other is the null pointer constant. + typename std::enable_if::value || + !std::is_pointer::value>::type* = nullptr> + static AssertionResult Compare(const char* lhs_expression, + const char* rhs_expression, const T1& lhs, + const T2& rhs) { + return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); + } + + // With this overloaded version, we allow anonymous enums to be used + // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous + // enums can be implicitly cast to BiggestInt. + // + // Even though its body looks the same as the above version, we + // cannot merge the two, as it will make anonymous enums unhappy. + static AssertionResult Compare(const char* lhs_expression, + const char* rhs_expression, BiggestInt lhs, + BiggestInt rhs) { + return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); + } + + template + static AssertionResult Compare( + const char* lhs_expression, const char* rhs_expression, + // Handle cases where '0' is used as a null pointer literal. + std::nullptr_t /* lhs */, T* rhs) { + // We already know that 'lhs' is a null pointer. + return CmpHelperEQ(lhs_expression, rhs_expression, static_cast(nullptr), + rhs); + } +}; + +// Separate the error generating code from the code path to reduce the stack +// frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers +// when calling EXPECT_OP in a tight loop. +template +AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, + const T1& val1, const T2& val2, + const char* op) { + return AssertionFailure() + << "Expected: (" << expr1 << ") " << op << " (" << expr2 + << "), actual: " << FormatForComparisonFailureMessage(val1, val2) + << " vs " << FormatForComparisonFailureMessage(val2, val1); +} + +// A macro for implementing the helper functions needed to implement +// ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste +// of similar code. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + +#define GTEST_IMPL_CMP_HELPER_(op_name, op) \ + template \ + AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ + const T1& val1, const T2& val2) { \ + if (val1 op val2) { \ + return AssertionSuccess(); \ + } else { \ + return CmpHelperOpFailure(expr1, expr2, val1, val2, #op); \ + } \ + } + +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. + +// Implements the helper function for {ASSERT|EXPECT}_NE +GTEST_IMPL_CMP_HELPER_(NE, !=) +// Implements the helper function for {ASSERT|EXPECT}_LE +GTEST_IMPL_CMP_HELPER_(LE, <=) +// Implements the helper function for {ASSERT|EXPECT}_LT +GTEST_IMPL_CMP_HELPER_(LT, <) +// Implements the helper function for {ASSERT|EXPECT}_GE +GTEST_IMPL_CMP_HELPER_(GE, >=) +// Implements the helper function for {ASSERT|EXPECT}_GT +GTEST_IMPL_CMP_HELPER_(GT, >) + +#undef GTEST_IMPL_CMP_HELPER_ + +// The helper function for {ASSERT|EXPECT}_STREQ. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, + const char* s2_expression, + const char* s1, const char* s2); + +// The helper function for {ASSERT|EXPECT}_STRCASEEQ. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression, + const char* s2_expression, + const char* s1, const char* s2); + +// The helper function for {ASSERT|EXPECT}_STRNE. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, + const char* s1, const char* s2); + +// The helper function for {ASSERT|EXPECT}_STRCASENE. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, + const char* s2_expression, + const char* s1, const char* s2); + +// Helper function for *_STREQ on wide strings. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, + const char* s2_expression, + const wchar_t* s1, const wchar_t* s2); + +// Helper function for *_STRNE on wide strings. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, + const wchar_t* s1, const wchar_t* s2); + +} // namespace internal + +// IsSubstring() and IsNotSubstring() are intended to be used as the +// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by +// themselves. They check whether needle is a substring of haystack +// (NULL is considered a substring of itself only), and return an +// appropriate error message when they fail. +// +// The {needle,haystack}_expr arguments are the stringified +// expressions that generated the two real arguments. +GTEST_API_ AssertionResult IsSubstring(const char* needle_expr, + const char* haystack_expr, + const char* needle, + const char* haystack); +GTEST_API_ AssertionResult IsSubstring(const char* needle_expr, + const char* haystack_expr, + const wchar_t* needle, + const wchar_t* haystack); +GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, + const char* needle, + const char* haystack); +GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, + const wchar_t* needle, + const wchar_t* haystack); +GTEST_API_ AssertionResult IsSubstring(const char* needle_expr, + const char* haystack_expr, + const ::std::string& needle, + const ::std::string& haystack); +GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, + const ::std::string& needle, + const ::std::string& haystack); + +#if GTEST_HAS_STD_WSTRING +GTEST_API_ AssertionResult IsSubstring(const char* needle_expr, + const char* haystack_expr, + const ::std::wstring& needle, + const ::std::wstring& haystack); +GTEST_API_ AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, + const ::std::wstring& needle, + const ::std::wstring& haystack); +#endif // GTEST_HAS_STD_WSTRING + +namespace internal { + +// Helper template function for comparing floating-points. +// +// Template parameter: +// +// RawType: the raw floating-point type (either float or double) +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +template +AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression, + const char* rhs_expression, + RawType lhs_value, RawType rhs_value) { + const FloatingPoint lhs(lhs_value), rhs(rhs_value); + + if (lhs.AlmostEquals(rhs)) { + return AssertionSuccess(); + } + + ::std::stringstream lhs_ss; + lhs_ss << std::setprecision(std::numeric_limits::digits10 + 2) + << lhs_value; + + ::std::stringstream rhs_ss; + rhs_ss << std::setprecision(std::numeric_limits::digits10 + 2) + << rhs_value; + + return EqFailure(lhs_expression, rhs_expression, + StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss), + false); +} + +// Helper function for implementing ASSERT_NEAR. +// +// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. +GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, + const char* expr2, + const char* abs_error_expr, + double val1, double val2, + double abs_error); + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// A class that enables one to stream messages to assertion macros +class GTEST_API_ AssertHelper { + public: + // Constructor. + AssertHelper(TestPartResult::Type type, const char* file, int line, + const char* message); + ~AssertHelper(); + + // Message assignment is a semantic trick to enable assertion + // streaming; see the GTEST_MESSAGE_ macro below. + void operator=(const Message& message) const; + + private: + // We put our data in a struct so that the size of the AssertHelper class can + // be as small as possible. This is important because gcc is incapable of + // re-using stack space even for temporary variables, so every EXPECT_EQ + // reserves stack space for another AssertHelper. + struct AssertHelperData { + AssertHelperData(TestPartResult::Type t, const char* srcfile, int line_num, + const char* msg) + : type(t), file(srcfile), line(line_num), message(msg) {} + + TestPartResult::Type const type; + const char* const file; + int const line; + std::string const message; + + private: + AssertHelperData(const AssertHelperData&) = delete; + AssertHelperData& operator=(const AssertHelperData&) = delete; + }; + + AssertHelperData* const data_; + + AssertHelper(const AssertHelper&) = delete; + AssertHelper& operator=(const AssertHelper&) = delete; +}; + +} // namespace internal + +// The pure interface class that all value-parameterized tests inherit from. +// A value-parameterized class must inherit from both ::testing::Test and +// ::testing::WithParamInterface. In most cases that just means inheriting +// from ::testing::TestWithParam, but more complicated test hierarchies +// may need to inherit from Test and WithParamInterface at different levels. +// +// This interface has support for accessing the test parameter value via +// the GetParam() method. +// +// Use it with one of the parameter generator defining functions, like Range(), +// Values(), ValuesIn(), Bool(), and Combine(). +// +// class FooTest : public ::testing::TestWithParam { +// protected: +// FooTest() { +// // Can use GetParam() here. +// } +// ~FooTest() override { +// // Can use GetParam() here. +// } +// void SetUp() override { +// // Can use GetParam() here. +// } +// void TearDown override { +// // Can use GetParam() here. +// } +// }; +// TEST_P(FooTest, DoesBar) { +// // Can use GetParam() method here. +// Foo foo; +// ASSERT_TRUE(foo.DoesBar(GetParam())); +// } +// INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); + +template +class WithParamInterface { + public: + typedef T ParamType; + virtual ~WithParamInterface() {} + + // The current parameter value. Is also available in the test fixture's + // constructor. + static const ParamType& GetParam() { + GTEST_CHECK_(parameter_ != nullptr) + << "GetParam() can only be called inside a value-parameterized test " + << "-- did you intend to write TEST_P instead of TEST_F?"; + return *parameter_; + } + + private: + // Sets parameter value. The caller is responsible for making sure the value + // remains alive and unchanged throughout the current test. + static void SetParam(const ParamType* parameter) { parameter_ = parameter; } + + // Static value used for accessing parameter during a test lifetime. + static const ParamType* parameter_; + + // TestClass must be a subclass of WithParamInterface and Test. + template + friend class internal::ParameterizedTestFactory; +}; + +template +const T* WithParamInterface::parameter_ = nullptr; + +// Most value-parameterized classes can ignore the existence of +// WithParamInterface, and can just inherit from ::testing::TestWithParam. + +template +class TestWithParam : public Test, public WithParamInterface {}; + +// Macros for indicating success/failure in test code. + +// Skips test in runtime. +// Skipping test aborts current function. +// Skipped tests are neither successful nor failed. +#define GTEST_SKIP() GTEST_SKIP_("") + +// ADD_FAILURE unconditionally adds a failure to the current test. +// SUCCEED generates a success - it doesn't automatically make the +// current test successful, as a test is only successful when it has +// no failure. +// +// EXPECT_* verifies that a certain condition is satisfied. If not, +// it behaves like ADD_FAILURE. In particular: +// +// EXPECT_TRUE verifies that a Boolean condition is true. +// EXPECT_FALSE verifies that a Boolean condition is false. +// +// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except +// that they will also abort the current function on failure. People +// usually want the fail-fast behavior of FAIL and ASSERT_*, but those +// writing data-driven tests often find themselves using ADD_FAILURE +// and EXPECT_* more. + +// Generates a nonfatal failure with a generic message. +#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") + +// Generates a nonfatal failure at the given source file location with +// a generic message. +#define ADD_FAILURE_AT(file, line) \ + GTEST_MESSAGE_AT_(file, line, "Failed", \ + ::testing::TestPartResult::kNonFatalFailure) + +// Generates a fatal failure with a generic message. +#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") + +// Like GTEST_FAIL(), but at the given source file location. +#define GTEST_FAIL_AT(file, line) \ + GTEST_MESSAGE_AT_(file, line, "Failed", \ + ::testing::TestPartResult::kFatalFailure) + +// Define this macro to 1 to omit the definition of FAIL(), which is a +// generic name and clashes with some other libraries. +#if !GTEST_DONT_DEFINE_FAIL +#define FAIL() GTEST_FAIL() +#endif + +// Generates a success with a generic message. +#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") + +// Define this macro to 1 to omit the definition of SUCCEED(), which +// is a generic name and clashes with some other libraries. +#if !GTEST_DONT_DEFINE_SUCCEED +#define SUCCEED() GTEST_SUCCEED() +#endif + +// Macros for testing exceptions. +// +// * {ASSERT|EXPECT}_THROW(statement, expected_exception): +// Tests that the statement throws the expected exception. +// * {ASSERT|EXPECT}_NO_THROW(statement): +// Tests that the statement doesn't throw any exception. +// * {ASSERT|EXPECT}_ANY_THROW(statement): +// Tests that the statement throws an exception. + +#define EXPECT_THROW(statement, expected_exception) \ + GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) +#define EXPECT_NO_THROW(statement) \ + GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) +#define EXPECT_ANY_THROW(statement) \ + GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) +#define ASSERT_THROW(statement, expected_exception) \ + GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) +#define ASSERT_NO_THROW(statement) \ + GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) +#define ASSERT_ANY_THROW(statement) \ + GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) + +// Boolean assertions. Condition can be either a Boolean expression or an +// AssertionResult. For more information on how to use AssertionResult with +// these macros see comments on that class. +#define GTEST_EXPECT_TRUE(condition) \ + GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ + GTEST_NONFATAL_FAILURE_) +#define GTEST_EXPECT_FALSE(condition) \ + GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ + GTEST_NONFATAL_FAILURE_) +#define GTEST_ASSERT_TRUE(condition) \ + GTEST_TEST_BOOLEAN_(condition, #condition, false, true, GTEST_FATAL_FAILURE_) +#define GTEST_ASSERT_FALSE(condition) \ + GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ + GTEST_FATAL_FAILURE_) + +// Define these macros to 1 to omit the definition of the corresponding +// EXPECT or ASSERT, which clashes with some users' own code. + +#if !GTEST_DONT_DEFINE_EXPECT_TRUE +#define EXPECT_TRUE(condition) GTEST_EXPECT_TRUE(condition) +#endif + +#if !GTEST_DONT_DEFINE_EXPECT_FALSE +#define EXPECT_FALSE(condition) GTEST_EXPECT_FALSE(condition) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_TRUE +#define ASSERT_TRUE(condition) GTEST_ASSERT_TRUE(condition) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_FALSE +#define ASSERT_FALSE(condition) GTEST_ASSERT_FALSE(condition) +#endif + +// Macros for testing equalities and inequalities. +// +// * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2 +// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 +// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 +// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 +// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 +// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 +// +// When they are not, Google Test prints both the tested expressions and +// their actual values. The values must be compatible built-in types, +// or you will get a compiler error. By "compatible" we mean that the +// values can be compared by the respective operator. +// +// Note: +// +// 1. It is possible to make a user-defined type work with +// {ASSERT|EXPECT}_??(), but that requires overloading the +// comparison operators and is thus discouraged by the Google C++ +// Usage Guide. Therefore, you are advised to use the +// {ASSERT|EXPECT}_TRUE() macro to assert that two objects are +// equal. +// +// 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on +// pointers (in particular, C strings). Therefore, if you use it +// with two C strings, you are testing how their locations in memory +// are related, not how their content is related. To compare two C +// strings by content, use {ASSERT|EXPECT}_STR*(). +// +// 3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to +// {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you +// what the actual value is when it fails, and similarly for the +// other comparisons. +// +// 4. Do not depend on the order in which {ASSERT|EXPECT}_??() +// evaluate their arguments, which is undefined. +// +// 5. These macros evaluate their arguments exactly once. +// +// Examples: +// +// EXPECT_NE(Foo(), 5); +// EXPECT_EQ(a_pointer, NULL); +// ASSERT_LT(i, array_size); +// ASSERT_GT(records.size(), 0) << "There is no record left."; + +#define EXPECT_EQ(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2) +#define EXPECT_NE(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) +#define EXPECT_LE(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) +#define EXPECT_LT(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) +#define EXPECT_GE(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) +#define EXPECT_GT(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) + +#define GTEST_ASSERT_EQ(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::EqHelper::Compare, val1, val2) +#define GTEST_ASSERT_NE(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) +#define GTEST_ASSERT_LE(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) +#define GTEST_ASSERT_LT(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) +#define GTEST_ASSERT_GE(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) +#define GTEST_ASSERT_GT(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) + +// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of +// ASSERT_XY(), which clashes with some users' own code. + +#if !GTEST_DONT_DEFINE_ASSERT_EQ +#define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_NE +#define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_LE +#define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_LT +#define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_GE +#define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) +#endif + +#if !GTEST_DONT_DEFINE_ASSERT_GT +#define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) +#endif + +// C-string Comparisons. All tests treat NULL and any non-NULL string +// as different. Two NULLs are equal. +// +// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 +// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 +// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case +// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case +// +// For wide or narrow string objects, you can use the +// {ASSERT|EXPECT}_??() macros. +// +// Don't depend on the order in which the arguments are evaluated, +// which is undefined. +// +// These macros evaluate their arguments exactly once. + +#define EXPECT_STREQ(s1, s2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) +#define EXPECT_STRNE(s1, s2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) +#define EXPECT_STRCASEEQ(s1, s2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) +#define EXPECT_STRCASENE(s1, s2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) + +#define ASSERT_STREQ(s1, s2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) +#define ASSERT_STRNE(s1, s2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) +#define ASSERT_STRCASEEQ(s1, s2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) +#define ASSERT_STRCASENE(s1, s2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) + +// Macros for comparing floating-point numbers. +// +// * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2): +// Tests that two float values are almost equal. +// * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2): +// Tests that two double values are almost equal. +// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): +// Tests that v1 and v2 are within the given distance to each other. +// +// Google Test uses ULP-based comparison to automatically pick a default +// error bound that is appropriate for the operands. See the +// FloatingPoint template class in gtest-internal.h if you are +// interested in the implementation details. + +#define EXPECT_FLOAT_EQ(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + val1, val2) + +#define EXPECT_DOUBLE_EQ(val1, val2) \ + EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + val1, val2) + +#define ASSERT_FLOAT_EQ(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + val1, val2) + +#define ASSERT_DOUBLE_EQ(val1, val2) \ + ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ, \ + val1, val2) + +#define EXPECT_NEAR(val1, val2, abs_error) \ + EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \ + abs_error) + +#define ASSERT_NEAR(val1, val2, abs_error) \ + ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \ + abs_error) + +// These predicate format functions work on floating-point values, and +// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. +// +// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); + +// Asserts that val1 is less than, or almost equal to, val2. Fails +// otherwise. In particular, it fails if either val1 or val2 is NaN. +GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, + float val1, float val2); +GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, + double val1, double val2); + +#if GTEST_OS_WINDOWS + +// Macros that test for HRESULT failure and success, these are only useful +// on Windows, and rely on Windows SDK macros and APIs to compile. +// +// * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) +// +// When expr unexpectedly fails or succeeds, Google Test prints the +// expected result and the actual result with both a human-readable +// string representation of the error, if available, as well as the +// hex result code. +#define EXPECT_HRESULT_SUCCEEDED(expr) \ + EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) + +#define ASSERT_HRESULT_SUCCEEDED(expr) \ + ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) + +#define EXPECT_HRESULT_FAILED(expr) \ + EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) + +#define ASSERT_HRESULT_FAILED(expr) \ + ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) + +#endif // GTEST_OS_WINDOWS + +// Macros that execute statement and check that it doesn't generate new fatal +// failures in the current thread. +// +// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); +// +// Examples: +// +// EXPECT_NO_FATAL_FAILURE(Process()); +// ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; +// +#define ASSERT_NO_FATAL_FAILURE(statement) \ + GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) +#define EXPECT_NO_FATAL_FAILURE(statement) \ + GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) + +// Causes a trace (including the given source file path and line number, +// and the given message) to be included in every test failure message generated +// by code in the scope of the lifetime of an instance of this class. The effect +// is undone with the destruction of the instance. +// +// The message argument can be anything streamable to std::ostream. +// +// Example: +// testing::ScopedTrace trace("file.cc", 123, "message"); +// +class GTEST_API_ ScopedTrace { + public: + // The c'tor pushes the given source file location and message onto + // a trace stack maintained by Google Test. + + // Template version. Uses Message() to convert the values into strings. + // Slow, but flexible. + template + ScopedTrace(const char* file, int line, const T& message) { + PushTrace(file, line, (Message() << message).GetString()); + } + + // Optimize for some known types. + ScopedTrace(const char* file, int line, const char* message) { + PushTrace(file, line, message ? message : "(null)"); + } + + ScopedTrace(const char* file, int line, const std::string& message) { + PushTrace(file, line, message); + } + + // The d'tor pops the info pushed by the c'tor. + // + // Note that the d'tor is not virtual in order to be efficient. + // Don't inherit from ScopedTrace! + ~ScopedTrace(); + + private: + void PushTrace(const char* file, int line, std::string message); + + ScopedTrace(const ScopedTrace&) = delete; + ScopedTrace& operator=(const ScopedTrace&) = delete; +} GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its + // c'tor and d'tor. Therefore it doesn't + // need to be used otherwise. + +// Causes a trace (including the source file path, the current line +// number, and the given message) to be included in every test failure +// message generated by code in the current scope. The effect is +// undone when the control leaves the current scope. +// +// The message argument can be anything streamable to std::ostream. +// +// In the implementation, we include the current line number as part +// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s +// to appear in the same block - as long as they are on different +// lines. +// +// Assuming that each thread maintains its own stack of traces. +// Therefore, a SCOPED_TRACE() would (correctly) only affect the +// assertions in its own thread. +#define SCOPED_TRACE(message) \ + ::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)( \ + __FILE__, __LINE__, (message)) + +// Compile-time assertion for type equality. +// StaticAssertTypeEq() compiles if and only if type1 and type2 +// are the same type. The value it returns is not interesting. +// +// Instead of making StaticAssertTypeEq a class template, we make it a +// function template that invokes a helper class template. This +// prevents a user from misusing StaticAssertTypeEq by +// defining objects of that type. +// +// CAVEAT: +// +// When used inside a method of a class template, +// StaticAssertTypeEq() is effective ONLY IF the method is +// instantiated. For example, given: +// +// template class Foo { +// public: +// void Bar() { testing::StaticAssertTypeEq(); } +// }; +// +// the code: +// +// void Test1() { Foo foo; } +// +// will NOT generate a compiler error, as Foo::Bar() is never +// actually instantiated. Instead, you need: +// +// void Test2() { Foo foo; foo.Bar(); } +// +// to cause a compiler error. +template +constexpr bool StaticAssertTypeEq() noexcept { + static_assert(std::is_same::value, "T1 and T2 are not the same type"); + return true; +} + +// Defines a test. +// +// The first parameter is the name of the test suite, and the second +// parameter is the name of the test within the test suite. +// +// The convention is to end the test suite name with "Test". For +// example, a test suite for the Foo class can be named FooTest. +// +// Test code should appear between braces after an invocation of +// this macro. Example: +// +// TEST(FooTest, InitializesCorrectly) { +// Foo foo; +// EXPECT_TRUE(foo.StatusIsOK()); +// } + +// Note that we call GetTestTypeId() instead of GetTypeId< +// ::testing::Test>() here to get the type ID of testing::Test. This +// is to work around a suspected linker bug when using Google Test as +// a framework on Mac OS X. The bug causes GetTypeId< +// ::testing::Test>() to return different values depending on whether +// the call is from the Google Test framework itself or from user test +// code. GetTestTypeId() is guaranteed to always return the same +// value, as it always calls GetTypeId<>() from the Google Test +// framework. +#define GTEST_TEST(test_suite_name, test_name) \ + GTEST_TEST_(test_suite_name, test_name, ::testing::Test, \ + ::testing::internal::GetTestTypeId()) + +// Define this macro to 1 to omit the definition of TEST(), which +// is a generic name and clashes with some other libraries. +#if !GTEST_DONT_DEFINE_TEST +#define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name) +#endif + +// Defines a test that uses a test fixture. +// +// The first parameter is the name of the test fixture class, which +// also doubles as the test suite name. The second parameter is the +// name of the test within the test suite. +// +// A test fixture class must be declared earlier. The user should put +// the test code between braces after using this macro. Example: +// +// class FooTest : public testing::Test { +// protected: +// void SetUp() override { b_.AddElement(3); } +// +// Foo a_; +// Foo b_; +// }; +// +// TEST_F(FooTest, InitializesCorrectly) { +// EXPECT_TRUE(a_.StatusIsOK()); +// } +// +// TEST_F(FooTest, ReturnsElementCountCorrectly) { +// EXPECT_EQ(a_.size(), 0); +// EXPECT_EQ(b_.size(), 1); +// } +#define GTEST_TEST_F(test_fixture, test_name) \ + GTEST_TEST_(test_fixture, test_name, test_fixture, \ + ::testing::internal::GetTypeId()) +#if !GTEST_DONT_DEFINE_TEST_F +#define TEST_F(test_fixture, test_name) GTEST_TEST_F(test_fixture, test_name) +#endif + +// Returns a path to temporary directory. +// Tries to determine an appropriate directory for the platform. +GTEST_API_ std::string TempDir(); + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +// Dynamically registers a test with the framework. +// +// This is an advanced API only to be used when the `TEST` macros are +// insufficient. The macros should be preferred when possible, as they avoid +// most of the complexity of calling this function. +// +// The `factory` argument is a factory callable (move-constructible) object or +// function pointer that creates a new instance of the Test object. It +// handles ownership to the caller. The signature of the callable is +// `Fixture*()`, where `Fixture` is the test fixture class for the test. All +// tests registered with the same `test_suite_name` must return the same +// fixture type. This is checked at runtime. +// +// The framework will infer the fixture class from the factory and will call +// the `SetUpTestSuite` and `TearDownTestSuite` for it. +// +// Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is +// undefined. +// +// Use case example: +// +// class MyFixture : public ::testing::Test { +// public: +// // All of these optional, just like in regular macro usage. +// static void SetUpTestSuite() { ... } +// static void TearDownTestSuite() { ... } +// void SetUp() override { ... } +// void TearDown() override { ... } +// }; +// +// class MyTest : public MyFixture { +// public: +// explicit MyTest(int data) : data_(data) {} +// void TestBody() override { ... } +// +// private: +// int data_; +// }; +// +// void RegisterMyTests(const std::vector& values) { +// for (int v : values) { +// ::testing::RegisterTest( +// "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, +// std::to_string(v).c_str(), +// __FILE__, __LINE__, +// // Important to use the fixture type as the return type here. +// [=]() -> MyFixture* { return new MyTest(v); }); +// } +// } +// ... +// int main(int argc, char** argv) { +// ::testing::InitGoogleTest(&argc, argv); +// std::vector values_to_test = LoadValuesFromConfig(); +// RegisterMyTests(values_to_test); +// ... +// return RUN_ALL_TESTS(); +// } +// +template +TestInfo* RegisterTest(const char* test_suite_name, const char* test_name, + const char* type_param, const char* value_param, + const char* file, int line, Factory factory) { + using TestT = typename std::remove_pointer::type; + + class FactoryImpl : public internal::TestFactoryBase { + public: + explicit FactoryImpl(Factory f) : factory_(std::move(f)) {} + Test* CreateTest() override { return factory_(); } + + private: + Factory factory_; + }; + + return internal::MakeAndRegisterTestInfo( + test_suite_name, test_name, type_param, value_param, + internal::CodeLocation(file, line), internal::GetTypeId(), + internal::SuiteApiResolver::GetSetUpCaseOrSuite(file, line), + internal::SuiteApiResolver::GetTearDownCaseOrSuite(file, line), + new FactoryImpl{std::move(factory)}); +} + +} // namespace testing + +// Use this function in main() to run all tests. It returns 0 if all +// tests are successful, or 1 otherwise. +// +// RUN_ALL_TESTS() should be invoked after the command line has been +// parsed by InitGoogleTest(). +// +// This function was formerly a macro; thus, it is in the global +// namespace and has an all-caps name. +int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; + +inline int RUN_ALL_TESTS() { return ::testing::UnitTest::GetInstance()->Run(); } + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_H_ diff --git a/test/gtest/include/gtest/gtest_pred_impl.h b/test/gtest/include/gtest/gtest_pred_impl.h new file mode 100644 index 0000000000..47a24aa687 --- /dev/null +++ b/test/gtest/include/gtest/gtest_pred_impl.h @@ -0,0 +1,279 @@ +// Copyright 2006, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Implements a family of generic predicate assertion macros. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ + +#include "gtest/gtest-assertion-result.h" +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-port.h" + +namespace testing { + +// This header implements a family of generic predicate assertion +// macros: +// +// ASSERT_PRED_FORMAT1(pred_format, v1) +// ASSERT_PRED_FORMAT2(pred_format, v1, v2) +// ... +// +// where pred_format is a function or functor that takes n (in the +// case of ASSERT_PRED_FORMATn) values and their source expression +// text, and returns a testing::AssertionResult. See the definition +// of ASSERT_EQ in gtest.h for an example. +// +// If you don't care about formatting, you can use the more +// restrictive version: +// +// ASSERT_PRED1(pred, v1) +// ASSERT_PRED2(pred, v1, v2) +// ... +// +// where pred is an n-ary function or functor that returns bool, +// and the values v1, v2, ..., must support the << operator for +// streaming to std::ostream. +// +// We also define the EXPECT_* variations. +// +// For now we only support predicates whose arity is at most 5. +// Please email googletestframework@googlegroups.com if you need +// support for higher arities. + +// GTEST_ASSERT_ is the basic statement to which all of the assertions +// in this file reduce. Don't use this in your code. + +#define GTEST_ASSERT_(expression, on_failure) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (const ::testing::AssertionResult gtest_ar = (expression)) \ + ; \ + else \ + on_failure(gtest_ar.failure_message()) + +// Helper function for implementing {EXPECT|ASSERT}_PRED1. Don't use +// this in your code. +template +AssertionResult AssertPred1Helper(const char* pred_text, const char* e1, + Pred pred, const T1& v1) { + if (pred(v1)) return AssertionSuccess(); + + return AssertionFailure() + << pred_text << "(" << e1 << ") evaluates to false, where" + << "\n" + << e1 << " evaluates to " << ::testing::PrintToString(v1); +} + +// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT1. +// Don't use this in your code. +#define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure) \ + GTEST_ASSERT_(pred_format(#v1, v1), on_failure) + +// Internal macro for implementing {EXPECT|ASSERT}_PRED1. Don't use +// this in your code. +#define GTEST_PRED1_(pred, v1, on_failure) \ + GTEST_ASSERT_(::testing::AssertPred1Helper(#pred, #v1, pred, v1), on_failure) + +// Unary predicate assertion macros. +#define EXPECT_PRED_FORMAT1(pred_format, v1) \ + GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_) +#define EXPECT_PRED1(pred, v1) GTEST_PRED1_(pred, v1, GTEST_NONFATAL_FAILURE_) +#define ASSERT_PRED_FORMAT1(pred_format, v1) \ + GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_) +#define ASSERT_PRED1(pred, v1) GTEST_PRED1_(pred, v1, GTEST_FATAL_FAILURE_) + +// Helper function for implementing {EXPECT|ASSERT}_PRED2. Don't use +// this in your code. +template +AssertionResult AssertPred2Helper(const char* pred_text, const char* e1, + const char* e2, Pred pred, const T1& v1, + const T2& v2) { + if (pred(v1, v2)) return AssertionSuccess(); + + return AssertionFailure() + << pred_text << "(" << e1 << ", " << e2 + << ") evaluates to false, where" + << "\n" + << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" + << e2 << " evaluates to " << ::testing::PrintToString(v2); +} + +// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2. +// Don't use this in your code. +#define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure) \ + GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), on_failure) + +// Internal macro for implementing {EXPECT|ASSERT}_PRED2. Don't use +// this in your code. +#define GTEST_PRED2_(pred, v1, v2, on_failure) \ + GTEST_ASSERT_(::testing::AssertPred2Helper(#pred, #v1, #v2, pred, v1, v2), \ + on_failure) + +// Binary predicate assertion macros. +#define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \ + GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_) +#define EXPECT_PRED2(pred, v1, v2) \ + GTEST_PRED2_(pred, v1, v2, GTEST_NONFATAL_FAILURE_) +#define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \ + GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_) +#define ASSERT_PRED2(pred, v1, v2) \ + GTEST_PRED2_(pred, v1, v2, GTEST_FATAL_FAILURE_) + +// Helper function for implementing {EXPECT|ASSERT}_PRED3. Don't use +// this in your code. +template +AssertionResult AssertPred3Helper(const char* pred_text, const char* e1, + const char* e2, const char* e3, Pred pred, + const T1& v1, const T2& v2, const T3& v3) { + if (pred(v1, v2, v3)) return AssertionSuccess(); + + return AssertionFailure() + << pred_text << "(" << e1 << ", " << e2 << ", " << e3 + << ") evaluates to false, where" + << "\n" + << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" + << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n" + << e3 << " evaluates to " << ::testing::PrintToString(v3); +} + +// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT3. +// Don't use this in your code. +#define GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, on_failure) \ + GTEST_ASSERT_(pred_format(#v1, #v2, #v3, v1, v2, v3), on_failure) + +// Internal macro for implementing {EXPECT|ASSERT}_PRED3. Don't use +// this in your code. +#define GTEST_PRED3_(pred, v1, v2, v3, on_failure) \ + GTEST_ASSERT_( \ + ::testing::AssertPred3Helper(#pred, #v1, #v2, #v3, pred, v1, v2, v3), \ + on_failure) + +// Ternary predicate assertion macros. +#define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \ + GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_NONFATAL_FAILURE_) +#define EXPECT_PRED3(pred, v1, v2, v3) \ + GTEST_PRED3_(pred, v1, v2, v3, GTEST_NONFATAL_FAILURE_) +#define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \ + GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_FATAL_FAILURE_) +#define ASSERT_PRED3(pred, v1, v2, v3) \ + GTEST_PRED3_(pred, v1, v2, v3, GTEST_FATAL_FAILURE_) + +// Helper function for implementing {EXPECT|ASSERT}_PRED4. Don't use +// this in your code. +template +AssertionResult AssertPred4Helper(const char* pred_text, const char* e1, + const char* e2, const char* e3, + const char* e4, Pred pred, const T1& v1, + const T2& v2, const T3& v3, const T4& v4) { + if (pred(v1, v2, v3, v4)) return AssertionSuccess(); + + return AssertionFailure() + << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4 + << ") evaluates to false, where" + << "\n" + << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" + << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n" + << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n" + << e4 << " evaluates to " << ::testing::PrintToString(v4); +} + +// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT4. +// Don't use this in your code. +#define GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, on_failure) \ + GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4), on_failure) + +// Internal macro for implementing {EXPECT|ASSERT}_PRED4. Don't use +// this in your code. +#define GTEST_PRED4_(pred, v1, v2, v3, v4, on_failure) \ + GTEST_ASSERT_(::testing::AssertPred4Helper(#pred, #v1, #v2, #v3, #v4, pred, \ + v1, v2, v3, v4), \ + on_failure) + +// 4-ary predicate assertion macros. +#define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \ + GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_) +#define EXPECT_PRED4(pred, v1, v2, v3, v4) \ + GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_) +#define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \ + GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_FATAL_FAILURE_) +#define ASSERT_PRED4(pred, v1, v2, v3, v4) \ + GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_FATAL_FAILURE_) + +// Helper function for implementing {EXPECT|ASSERT}_PRED5. Don't use +// this in your code. +template +AssertionResult AssertPred5Helper(const char* pred_text, const char* e1, + const char* e2, const char* e3, + const char* e4, const char* e5, Pred pred, + const T1& v1, const T2& v2, const T3& v3, + const T4& v4, const T5& v5) { + if (pred(v1, v2, v3, v4, v5)) return AssertionSuccess(); + + return AssertionFailure() + << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4 + << ", " << e5 << ") evaluates to false, where" + << "\n" + << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n" + << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n" + << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n" + << e4 << " evaluates to " << ::testing::PrintToString(v4) << "\n" + << e5 << " evaluates to " << ::testing::PrintToString(v5); +} + +// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT5. +// Don't use this in your code. +#define GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, on_failure) \ + GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5), \ + on_failure) + +// Internal macro for implementing {EXPECT|ASSERT}_PRED5. Don't use +// this in your code. +#define GTEST_PRED5_(pred, v1, v2, v3, v4, v5, on_failure) \ + GTEST_ASSERT_(::testing::AssertPred5Helper(#pred, #v1, #v2, #v3, #v4, #v5, \ + pred, v1, v2, v3, v4, v5), \ + on_failure) + +// 5-ary predicate assertion macros. +#define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \ + GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_) +#define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \ + GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_) +#define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \ + GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_) +#define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \ + GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_) + +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ diff --git a/test/gtest/include/gtest/gtest_prod.h b/test/gtest/include/gtest/gtest_prod.h new file mode 100644 index 0000000000..1f37dc31c3 --- /dev/null +++ b/test/gtest/include/gtest/gtest_prod.h @@ -0,0 +1,60 @@ +// Copyright 2006, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Google C++ Testing and Mocking Framework definitions useful in production +// code. + +#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PROD_H_ +#define GOOGLETEST_INCLUDE_GTEST_GTEST_PROD_H_ + +// When you need to test the private or protected members of a class, +// use the FRIEND_TEST macro to declare your tests as friends of the +// class. For example: +// +// class MyClass { +// private: +// void PrivateMethod(); +// FRIEND_TEST(MyClassTest, PrivateMethodWorks); +// }; +// +// class MyClassTest : public testing::Test { +// // ... +// }; +// +// TEST_F(MyClassTest, PrivateMethodWorks) { +// // Can call MyClass::PrivateMethod() here. +// } +// +// Note: The test class must be in the same namespace as the class being tested. +// For example, putting MyClassTest in an anonymous namespace will not work. + +#define FRIEND_TEST(test_case_name, test_name) \ + friend class test_case_name##_##test_name##_Test + +#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PROD_H_ diff --git a/test/gtest/include/gtest/internal/custom/README.md b/test/gtest/include/gtest/internal/custom/README.md new file mode 100644 index 0000000000..cb49e2c754 --- /dev/null +++ b/test/gtest/include/gtest/internal/custom/README.md @@ -0,0 +1,44 @@ +# Customization Points + +The custom directory is an injection point for custom user configurations. + +## Header `gtest.h` + +### The following macros can be defined: + +* `GTEST_OS_STACK_TRACE_GETTER_` - The name of an implementation of + `OsStackTraceGetterInterface`. +* `GTEST_CUSTOM_TEMPDIR_FUNCTION_` - An override for `testing::TempDir()`. See + `testing::TempDir` for semantics and signature. + +## Header `gtest-port.h` + +The following macros can be defined: + +### Logging: + +* `GTEST_LOG_(severity)` +* `GTEST_CHECK_(condition)` +* Functions `LogToStderr()` and `FlushInfoLog()` have to be provided too. + +### Threading: + +* `GTEST_HAS_NOTIFICATION_` - Enabled if Notification is already provided. +* `GTEST_HAS_MUTEX_AND_THREAD_LOCAL_` - Enabled if `Mutex` and `ThreadLocal` + are already provided. Must also provide `GTEST_DECLARE_STATIC_MUTEX_(mutex)` + and `GTEST_DEFINE_STATIC_MUTEX_(mutex)` +* `GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)` +* `GTEST_LOCK_EXCLUDED_(locks)` + +### Underlying library support features + +* `GTEST_HAS_CXXABI_H_` + +### Exporting API symbols: + +* `GTEST_API_` - Specifier for exported symbols. + +## Header `gtest-printers.h` + +* See documentation at `gtest/gtest-printers.h` for details on how to define a + custom printer. diff --git a/test/gtest/include/gtest/internal/custom/gtest-port.h b/test/gtest/include/gtest/internal/custom/gtest-port.h new file mode 100644 index 0000000000..db02881c0c --- /dev/null +++ b/test/gtest/include/gtest/internal/custom/gtest-port.h @@ -0,0 +1,37 @@ +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Injection point for custom user configurations. See README for details +// +// ** Custom implementation starts here ** + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_ + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_ diff --git a/test/gtest/include/gtest/internal/custom/gtest-printers.h b/test/gtest/include/gtest/internal/custom/gtest-printers.h new file mode 100644 index 0000000000..b9495d8378 --- /dev/null +++ b/test/gtest/include/gtest/internal/custom/gtest-printers.h @@ -0,0 +1,42 @@ +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// This file provides an injection point for custom printers in a local +// installation of gTest. +// It will be included from gtest-printers.h and the overrides in this file +// will be visible to everyone. +// +// Injection point for custom user configurations. See README for details +// +// ** Custom implementation starts here ** + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_ diff --git a/test/gtest/include/gtest/internal/custom/gtest.h b/test/gtest/include/gtest/internal/custom/gtest.h new file mode 100644 index 0000000000..afaaf17ba2 --- /dev/null +++ b/test/gtest/include/gtest/internal/custom/gtest.h @@ -0,0 +1,37 @@ +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Injection point for custom user configurations. See README for details +// +// ** Custom implementation starts here ** + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_H_ + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_H_ diff --git a/test/gtest/include/gtest/internal/gtest-death-test-internal.h b/test/gtest/include/gtest/internal/gtest-death-test-internal.h new file mode 100644 index 0000000000..45580ae805 --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-death-test-internal.h @@ -0,0 +1,306 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file defines internal utilities needed for implementing +// death tests. They are subject to change without notice. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ + +#include + +#include + +#include "gtest/gtest-matchers.h" +#include "gtest/internal/gtest-internal.h" + +GTEST_DECLARE_string_(internal_run_death_test); + +namespace testing { +namespace internal { + +// Names of the flags (needed for parsing Google Test flags). +const char kDeathTestStyleFlag[] = "death_test_style"; +const char kDeathTestUseFork[] = "death_test_use_fork"; +const char kInternalRunDeathTestFlag[] = "internal_run_death_test"; + +#if GTEST_HAS_DEATH_TEST + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +// DeathTest is a class that hides much of the complexity of the +// GTEST_DEATH_TEST_ macro. It is abstract; its static Create method +// returns a concrete class that depends on the prevailing death test +// style, as defined by the --gtest_death_test_style and/or +// --gtest_internal_run_death_test flags. + +// In describing the results of death tests, these terms are used with +// the corresponding definitions: +// +// exit status: The integer exit information in the format specified +// by wait(2) +// exit code: The integer code passed to exit(3), _exit(2), or +// returned from main() +class GTEST_API_ DeathTest { + public: + // Create returns false if there was an error determining the + // appropriate action to take for the current death test; for example, + // if the gtest_death_test_style flag is set to an invalid value. + // The LastMessage method will return a more detailed message in that + // case. Otherwise, the DeathTest pointer pointed to by the "test" + // argument is set. If the death test should be skipped, the pointer + // is set to NULL; otherwise, it is set to the address of a new concrete + // DeathTest object that controls the execution of the current test. + static bool Create(const char* statement, Matcher matcher, + const char* file, int line, DeathTest** test); + DeathTest(); + virtual ~DeathTest() {} + + // A helper class that aborts a death test when it's deleted. + class ReturnSentinel { + public: + explicit ReturnSentinel(DeathTest* test) : test_(test) {} + ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); } + + private: + DeathTest* const test_; + ReturnSentinel(const ReturnSentinel&) = delete; + ReturnSentinel& operator=(const ReturnSentinel&) = delete; + } GTEST_ATTRIBUTE_UNUSED_; + + // An enumeration of possible roles that may be taken when a death + // test is encountered. EXECUTE means that the death test logic should + // be executed immediately. OVERSEE means that the program should prepare + // the appropriate environment for a child process to execute the death + // test, then wait for it to complete. + enum TestRole { OVERSEE_TEST, EXECUTE_TEST }; + + // An enumeration of the three reasons that a test might be aborted. + enum AbortReason { + TEST_ENCOUNTERED_RETURN_STATEMENT, + TEST_THREW_EXCEPTION, + TEST_DID_NOT_DIE + }; + + // Assumes one of the above roles. + virtual TestRole AssumeRole() = 0; + + // Waits for the death test to finish and returns its status. + virtual int Wait() = 0; + + // Returns true if the death test passed; that is, the test process + // exited during the test, its exit status matches a user-supplied + // predicate, and its stderr output matches a user-supplied regular + // expression. + // The user-supplied predicate may be a macro expression rather + // than a function pointer or functor, or else Wait and Passed could + // be combined. + virtual bool Passed(bool exit_status_ok) = 0; + + // Signals that the death test did not die as expected. + virtual void Abort(AbortReason reason) = 0; + + // Returns a human-readable outcome message regarding the outcome of + // the last death test. + static const char* LastMessage(); + + static void set_last_death_test_message(const std::string& message); + + private: + // A string containing a description of the outcome of the last death test. + static std::string last_death_test_message_; + + DeathTest(const DeathTest&) = delete; + DeathTest& operator=(const DeathTest&) = delete; +}; + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +// Factory interface for death tests. May be mocked out for testing. +class DeathTestFactory { + public: + virtual ~DeathTestFactory() {} + virtual bool Create(const char* statement, + Matcher matcher, const char* file, + int line, DeathTest** test) = 0; +}; + +// A concrete DeathTestFactory implementation for normal use. +class DefaultDeathTestFactory : public DeathTestFactory { + public: + bool Create(const char* statement, Matcher matcher, + const char* file, int line, DeathTest** test) override; +}; + +// Returns true if exit_status describes a process that was terminated +// by a signal, or exited normally with a nonzero exit code. +GTEST_API_ bool ExitedUnsuccessfully(int exit_status); + +// A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads +// and interpreted as a regex (rather than an Eq matcher) for legacy +// compatibility. +inline Matcher MakeDeathTestMatcher( + ::testing::internal::RE regex) { + return ContainsRegex(regex.pattern()); +} +inline Matcher MakeDeathTestMatcher(const char* regex) { + return ContainsRegex(regex); +} +inline Matcher MakeDeathTestMatcher( + const ::std::string& regex) { + return ContainsRegex(regex); +} + +// If a Matcher is passed to EXPECT_DEATH (etc.), it's +// used directly. +inline Matcher MakeDeathTestMatcher( + Matcher matcher) { + return matcher; +} + +// Traps C++ exceptions escaping statement and reports them as test +// failures. Note that trapping SEH exceptions is not implemented here. +#if GTEST_HAS_EXCEPTIONS +#define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } catch (const ::std::exception& gtest_exception) { \ + fprintf( \ + stderr, \ + "\n%s: Caught std::exception-derived exception escaping the " \ + "death test statement. Exception message: %s\n", \ + ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \ + gtest_exception.what()); \ + fflush(stderr); \ + death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ + } catch (...) { \ + death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ + } + +#else +#define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) + +#endif + +// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*, +// ASSERT_EXIT*, and EXPECT_EXIT*. +#define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::AlwaysTrue()) { \ + ::testing::internal::DeathTest* gtest_dt; \ + if (!::testing::internal::DeathTest::Create( \ + #statement, \ + ::testing::internal::MakeDeathTestMatcher(regex_or_matcher), \ + __FILE__, __LINE__, >est_dt)) { \ + goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ + } \ + if (gtest_dt != nullptr) { \ + std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(gtest_dt); \ + switch (gtest_dt->AssumeRole()) { \ + case ::testing::internal::DeathTest::OVERSEE_TEST: \ + if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \ + goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ + } \ + break; \ + case ::testing::internal::DeathTest::EXECUTE_TEST: { \ + ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \ + gtest_dt); \ + GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \ + gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ + break; \ + } \ + } \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__) \ + : fail(::testing::internal::DeathTest::LastMessage()) +// The symbol "fail" here expands to something into which a message +// can be streamed. + +// This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in +// NDEBUG mode. In this case we need the statements to be executed and the macro +// must accept a streamed message even though the message is never printed. +// The regex object is not evaluated, but it is used to prevent "unused" +// warnings and to avoid an expression that doesn't compile in debug mode. +#define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::AlwaysTrue()) { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } else if (!::testing::internal::AlwaysTrue()) { \ + ::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \ + } else \ + ::testing::Message() + +// A class representing the parsed contents of the +// --gtest_internal_run_death_test flag, as it existed when +// RUN_ALL_TESTS was called. +class InternalRunDeathTestFlag { + public: + InternalRunDeathTestFlag(const std::string& a_file, int a_line, int an_index, + int a_write_fd) + : file_(a_file), line_(a_line), index_(an_index), write_fd_(a_write_fd) {} + + ~InternalRunDeathTestFlag() { + if (write_fd_ >= 0) posix::Close(write_fd_); + } + + const std::string& file() const { return file_; } + int line() const { return line_; } + int index() const { return index_; } + int write_fd() const { return write_fd_; } + + private: + std::string file_; + int line_; + int index_; + int write_fd_; + + InternalRunDeathTestFlag(const InternalRunDeathTestFlag&) = delete; + InternalRunDeathTestFlag& operator=(const InternalRunDeathTestFlag&) = delete; +}; + +// Returns a newly created InternalRunDeathTestFlag object with fields +// initialized from the GTEST_FLAG(internal_run_death_test) flag if +// the flag is specified; otherwise returns NULL. +InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag(); + +#endif // GTEST_HAS_DEATH_TEST + +} // namespace internal +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ diff --git a/test/gtest/include/gtest/internal/gtest-filepath.h b/test/gtest/include/gtest/internal/gtest-filepath.h new file mode 100644 index 0000000000..a2a60a962b --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-filepath.h @@ -0,0 +1,210 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Google Test filepath utilities +// +// This header file declares classes and functions used internally by +// Google Test. They are subject to change without notice. +// +// This file is #included in gtest/internal/gtest-internal.h. +// Do not include this header file separately! + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ + +#include "gtest/internal/gtest-string.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +namespace testing { +namespace internal { + +// FilePath - a class for file and directory pathname manipulation which +// handles platform-specific conventions (like the pathname separator). +// Used for helper functions for naming files in a directory for xml output. +// Except for Set methods, all methods are const or static, which provides an +// "immutable value object" -- useful for peace of mind. +// A FilePath with a value ending in a path separator ("like/this/") represents +// a directory, otherwise it is assumed to represent a file. In either case, +// it may or may not represent an actual file or directory in the file system. +// Names are NOT checked for syntax correctness -- no checking for illegal +// characters, malformed paths, etc. + +class GTEST_API_ FilePath { + public: + FilePath() : pathname_("") {} + FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {} + + explicit FilePath(const std::string& pathname) : pathname_(pathname) { + Normalize(); + } + + FilePath& operator=(const FilePath& rhs) { + Set(rhs); + return *this; + } + + void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; } + + const std::string& string() const { return pathname_; } + const char* c_str() const { return pathname_.c_str(); } + + // Returns the current working directory, or "" if unsuccessful. + static FilePath GetCurrentDir(); + + // Given directory = "dir", base_name = "test", number = 0, + // extension = "xml", returns "dir/test.xml". If number is greater + // than zero (e.g., 12), returns "dir/test_12.xml". + // On Windows platform, uses \ as the separator rather than /. + static FilePath MakeFileName(const FilePath& directory, + const FilePath& base_name, int number, + const char* extension); + + // Given directory = "dir", relative_path = "test.xml", + // returns "dir/test.xml". + // On Windows, uses \ as the separator rather than /. + static FilePath ConcatPaths(const FilePath& directory, + const FilePath& relative_path); + + // Returns a pathname for a file that does not currently exist. The pathname + // will be directory/base_name.extension or + // directory/base_name_.extension if directory/base_name.extension + // already exists. The number will be incremented until a pathname is found + // that does not already exist. + // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. + // There could be a race condition if two or more processes are calling this + // function at the same time -- they could both pick the same filename. + static FilePath GenerateUniqueFileName(const FilePath& directory, + const FilePath& base_name, + const char* extension); + + // Returns true if and only if the path is "". + bool IsEmpty() const { return pathname_.empty(); } + + // If input name has a trailing separator character, removes it and returns + // the name, otherwise return the name string unmodified. + // On Windows platform, uses \ as the separator, other platforms use /. + FilePath RemoveTrailingPathSeparator() const; + + // Returns a copy of the FilePath with the directory part removed. + // Example: FilePath("path/to/file").RemoveDirectoryName() returns + // FilePath("file"). If there is no directory part ("just_a_file"), it returns + // the FilePath unmodified. If there is no file part ("just_a_dir/") it + // returns an empty FilePath (""). + // On Windows platform, '\' is the path separator, otherwise it is '/'. + FilePath RemoveDirectoryName() const; + + // RemoveFileName returns the directory path with the filename removed. + // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". + // If the FilePath is "a_file" or "/a_file", RemoveFileName returns + // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does + // not have a file, like "just/a/dir/", it returns the FilePath unmodified. + // On Windows platform, '\' is the path separator, otherwise it is '/'. + FilePath RemoveFileName() const; + + // Returns a copy of the FilePath with the case-insensitive extension removed. + // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns + // FilePath("dir/file"). If a case-insensitive extension is not + // found, returns a copy of the original FilePath. + FilePath RemoveExtension(const char* extension) const; + + // Creates directories so that path exists. Returns true if successful or if + // the directories already exist; returns false if unable to create + // directories for any reason. Will also return false if the FilePath does + // not represent a directory (that is, it doesn't end with a path separator). + bool CreateDirectoriesRecursively() const; + + // Create the directory so that path exists. Returns true if successful or + // if the directory already exists; returns false if unable to create the + // directory for any reason, including if the parent directory does not + // exist. Not named "CreateDirectory" because that's a macro on Windows. + bool CreateFolder() const; + + // Returns true if FilePath describes something in the file-system, + // either a file, directory, or whatever, and that something exists. + bool FileOrDirectoryExists() const; + + // Returns true if pathname describes a directory in the file-system + // that exists. + bool DirectoryExists() const; + + // Returns true if FilePath ends with a path separator, which indicates that + // it is intended to represent a directory. Returns false otherwise. + // This does NOT check that a directory (or file) actually exists. + bool IsDirectory() const; + + // Returns true if pathname describes a root directory. (Windows has one + // root directory per disk drive.) + bool IsRootDirectory() const; + + // Returns true if pathname describes an absolute path. + bool IsAbsolutePath() const; + + private: + // Replaces multiple consecutive separators with a single separator. + // For example, "bar///foo" becomes "bar/foo". Does not eliminate other + // redundancies that might be in a pathname involving "." or "..". + // + // A pathname with multiple consecutive separators may occur either through + // user error or as a result of some scripts or APIs that generate a pathname + // with a trailing separator. On other platforms the same API or script + // may NOT generate a pathname with a trailing "/". Then elsewhere that + // pathname may have another "/" and pathname components added to it, + // without checking for the separator already being there. + // The script language and operating system may allow paths like "foo//bar" + // but some of the functions in FilePath will not handle that correctly. In + // particular, RemoveTrailingPathSeparator() only removes one separator, and + // it is called in CreateDirectoriesRecursively() assuming that it will change + // a pathname from directory syntax (trailing separator) to filename syntax. + // + // On Windows this method also replaces the alternate path separator '/' with + // the primary path separator '\\', so that for example "bar\\/\\foo" becomes + // "bar\\foo". + + void Normalize(); + + // Returns a pointer to the last occurrence of a valid path separator in + // the FilePath. On Windows, for example, both '/' and '\' are valid path + // separators. Returns NULL if no path separator was found. + const char* FindLastPathSeparator() const; + + std::string pathname_; +}; // class FilePath + +} // namespace internal +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ diff --git a/test/gtest/include/gtest/internal/gtest-internal.h b/test/gtest/include/gtest/internal/gtest-internal.h new file mode 100644 index 0000000000..9b04e4c85f --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-internal.h @@ -0,0 +1,1570 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file declares functions and macros used internally by +// Google Test. They are subject to change without notice. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ + +#include "gtest/internal/gtest-port.h" + +#if GTEST_OS_LINUX +#include +#include +#include +#include +#endif // GTEST_OS_LINUX + +#if GTEST_HAS_EXCEPTIONS +#include +#endif + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest-message.h" +#include "gtest/internal/gtest-filepath.h" +#include "gtest/internal/gtest-string.h" +#include "gtest/internal/gtest-type-util.h" + +// Due to C++ preprocessor weirdness, we need double indirection to +// concatenate two tokens when one of them is __LINE__. Writing +// +// foo ## __LINE__ +// +// will result in the token foo__LINE__, instead of foo followed by +// the current line number. For more details, see +// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6 +#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) +#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo##bar + +// Stringifies its argument. +// Work around a bug in visual studio which doesn't accept code like this: +// +// #define GTEST_STRINGIFY_(name) #name +// #define MACRO(a, b, c) ... GTEST_STRINGIFY_(a) ... +// MACRO(, x, y) +// +// Complaining about the argument to GTEST_STRINGIFY_ being empty. +// This is allowed by the spec. +#define GTEST_STRINGIFY_HELPER_(name, ...) #name +#define GTEST_STRINGIFY_(...) GTEST_STRINGIFY_HELPER_(__VA_ARGS__, ) + +namespace proto2 { +class MessageLite; +} + +namespace testing { + +// Forward declarations. + +class AssertionResult; // Result of an assertion. +class Message; // Represents a failure message. +class Test; // Represents a test. +class TestInfo; // Information about a test. +class TestPartResult; // Result of a test part. +class UnitTest; // A collection of test suites. + +template +::std::string PrintToString(const T& value); + +namespace internal { + +struct TraceInfo; // Information about a trace point. +class TestInfoImpl; // Opaque implementation of TestInfo +class UnitTestImpl; // Opaque implementation of UnitTest + +// The text used in failure messages to indicate the start of the +// stack trace. +GTEST_API_ extern const char kStackTraceMarker[]; + +// An IgnoredValue object can be implicitly constructed from ANY value. +class IgnoredValue { + struct Sink {}; + + public: + // This constructor template allows any value to be implicitly + // converted to IgnoredValue. The object has no data member and + // doesn't try to remember anything about the argument. We + // deliberately omit the 'explicit' keyword in order to allow the + // conversion to be implicit. + // Disable the conversion if T already has a magical conversion operator. + // Otherwise we get ambiguity. + template ::value, + int>::type = 0> + IgnoredValue(const T& /* ignored */) {} // NOLINT(runtime/explicit) +}; + +// Appends the user-supplied message to the Google-Test-generated message. +GTEST_API_ std::string AppendUserMessage(const std::string& gtest_msg, + const Message& user_msg); + +#if GTEST_HAS_EXCEPTIONS + +GTEST_DISABLE_MSC_WARNINGS_PUSH_( + 4275 /* an exported class was derived from a class that was not exported */) + +// This exception is thrown by (and only by) a failed Google Test +// assertion when GTEST_FLAG(throw_on_failure) is true (if exceptions +// are enabled). We derive it from std::runtime_error, which is for +// errors presumably detectable only at run time. Since +// std::runtime_error inherits from std::exception, many testing +// frameworks know how to extract and print the message inside it. +class GTEST_API_ GoogleTestFailureException : public ::std::runtime_error { + public: + explicit GoogleTestFailureException(const TestPartResult& failure); +}; + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4275 + +#endif // GTEST_HAS_EXCEPTIONS + +namespace edit_distance { +// Returns the optimal edits to go from 'left' to 'right'. +// All edits cost the same, with replace having lower priority than +// add/remove. +// Simple implementation of the Wagner-Fischer algorithm. +// See http://en.wikipedia.org/wiki/Wagner-Fischer_algorithm +enum EditType { kMatch, kAdd, kRemove, kReplace }; +GTEST_API_ std::vector CalculateOptimalEdits( + const std::vector& left, const std::vector& right); + +// Same as above, but the input is represented as strings. +GTEST_API_ std::vector CalculateOptimalEdits( + const std::vector& left, + const std::vector& right); + +// Create a diff of the input strings in Unified diff format. +GTEST_API_ std::string CreateUnifiedDiff(const std::vector& left, + const std::vector& right, + size_t context = 2); + +} // namespace edit_distance + +// Constructs and returns the message for an equality assertion +// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. +// +// The first four parameters are the expressions used in the assertion +// and their values, as strings. For example, for ASSERT_EQ(foo, bar) +// where foo is 5 and bar is 6, we have: +// +// expected_expression: "foo" +// actual_expression: "bar" +// expected_value: "5" +// actual_value: "6" +// +// The ignoring_case parameter is true if and only if the assertion is a +// *_STRCASEEQ*. When it's true, the string " (ignoring case)" will +// be inserted into the message. +GTEST_API_ AssertionResult EqFailure(const char* expected_expression, + const char* actual_expression, + const std::string& expected_value, + const std::string& actual_value, + bool ignoring_case); + +// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. +GTEST_API_ std::string GetBoolAssertionFailureMessage( + const AssertionResult& assertion_result, const char* expression_text, + const char* actual_predicate_value, const char* expected_predicate_value); + +// This template class represents an IEEE floating-point number +// (either single-precision or double-precision, depending on the +// template parameters). +// +// The purpose of this class is to do more sophisticated number +// comparison. (Due to round-off error, etc, it's very unlikely that +// two floating-points will be equal exactly. Hence a naive +// comparison by the == operation often doesn't work.) +// +// Format of IEEE floating-point: +// +// The most-significant bit being the leftmost, an IEEE +// floating-point looks like +// +// sign_bit exponent_bits fraction_bits +// +// Here, sign_bit is a single bit that designates the sign of the +// number. +// +// For float, there are 8 exponent bits and 23 fraction bits. +// +// For double, there are 11 exponent bits and 52 fraction bits. +// +// More details can be found at +// http://en.wikipedia.org/wiki/IEEE_floating-point_standard. +// +// Template parameter: +// +// RawType: the raw floating-point type (either float or double) +template +class FloatingPoint { + public: + // Defines the unsigned integer type that has the same size as the + // floating point number. + typedef typename TypeWithSize::UInt Bits; + + // Constants. + + // # of bits in a number. + static const size_t kBitCount = 8 * sizeof(RawType); + + // # of fraction bits in a number. + static const size_t kFractionBitCount = + std::numeric_limits::digits - 1; + + // # of exponent bits in a number. + static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount; + + // The mask for the sign bit. + static const Bits kSignBitMask = static_cast(1) << (kBitCount - 1); + + // The mask for the fraction bits. + static const Bits kFractionBitMask = ~static_cast(0) >> + (kExponentBitCount + 1); + + // The mask for the exponent bits. + static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask); + + // How many ULP's (Units in the Last Place) we want to tolerate when + // comparing two numbers. The larger the value, the more error we + // allow. A 0 value means that two numbers must be exactly the same + // to be considered equal. + // + // The maximum error of a single floating-point operation is 0.5 + // units in the last place. On Intel CPU's, all floating-point + // calculations are done with 80-bit precision, while double has 64 + // bits. Therefore, 4 should be enough for ordinary use. + // + // See the following article for more details on ULP: + // http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ + static const uint32_t kMaxUlps = 4; + + // Constructs a FloatingPoint from a raw floating-point number. + // + // On an Intel CPU, passing a non-normalized NAN (Not a Number) + // around may change its bits, although the new value is guaranteed + // to be also a NAN. Therefore, don't expect this constructor to + // preserve the bits in x when x is a NAN. + explicit FloatingPoint(const RawType& x) { u_.value_ = x; } + + // Static methods + + // Reinterprets a bit pattern as a floating-point number. + // + // This function is needed to test the AlmostEquals() method. + static RawType ReinterpretBits(const Bits bits) { + FloatingPoint fp(0); + fp.u_.bits_ = bits; + return fp.u_.value_; + } + + // Returns the floating-point number that represent positive infinity. + static RawType Infinity() { return ReinterpretBits(kExponentBitMask); } + + // Returns the maximum representable finite floating-point number. + static RawType Max(); + + // Non-static methods + + // Returns the bits that represents this number. + const Bits& bits() const { return u_.bits_; } + + // Returns the exponent bits of this number. + Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } + + // Returns the fraction bits of this number. + Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } + + // Returns the sign bit of this number. + Bits sign_bit() const { return kSignBitMask & u_.bits_; } + + // Returns true if and only if this is NAN (not a number). + bool is_nan() const { + // It's a NAN if the exponent bits are all ones and the fraction + // bits are not entirely zeros. + return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0); + } + + // Returns true if and only if this number is at most kMaxUlps ULP's away + // from rhs. In particular, this function: + // + // - returns false if either number is (or both are) NAN. + // - treats really large numbers as almost equal to infinity. + // - thinks +0.0 and -0.0 are 0 DLP's apart. + bool AlmostEquals(const FloatingPoint& rhs) const { + // The IEEE standard says that any comparison operation involving + // a NAN must return false. + if (is_nan() || rhs.is_nan()) return false; + + return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) <= + kMaxUlps; + } + + private: + // The data type used to store the actual floating-point number. + union FloatingPointUnion { + RawType value_; // The raw floating-point number. + Bits bits_; // The bits that represent the number. + }; + + // Converts an integer from the sign-and-magnitude representation to + // the biased representation. More precisely, let N be 2 to the + // power of (kBitCount - 1), an integer x is represented by the + // unsigned number x + N. + // + // For instance, + // + // -N + 1 (the most negative number representable using + // sign-and-magnitude) is represented by 1; + // 0 is represented by N; and + // N - 1 (the biggest number representable using + // sign-and-magnitude) is represented by 2N - 1. + // + // Read http://en.wikipedia.org/wiki/Signed_number_representations + // for more details on signed number representations. + static Bits SignAndMagnitudeToBiased(const Bits& sam) { + if (kSignBitMask & sam) { + // sam represents a negative number. + return ~sam + 1; + } else { + // sam represents a positive number. + return kSignBitMask | sam; + } + } + + // Given two numbers in the sign-and-magnitude representation, + // returns the distance between them as an unsigned number. + static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits& sam1, + const Bits& sam2) { + const Bits biased1 = SignAndMagnitudeToBiased(sam1); + const Bits biased2 = SignAndMagnitudeToBiased(sam2); + return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); + } + + FloatingPointUnion u_; +}; + +// We cannot use std::numeric_limits::max() as it clashes with the max() +// macro defined by . +template <> +inline float FloatingPoint::Max() { + return FLT_MAX; +} +template <> +inline double FloatingPoint::Max() { + return DBL_MAX; +} + +// Typedefs the instances of the FloatingPoint template class that we +// care to use. +typedef FloatingPoint Float; +typedef FloatingPoint Double; + +// In order to catch the mistake of putting tests that use different +// test fixture classes in the same test suite, we need to assign +// unique IDs to fixture classes and compare them. The TypeId type is +// used to hold such IDs. The user should treat TypeId as an opaque +// type: the only operation allowed on TypeId values is to compare +// them for equality using the == operator. +typedef const void* TypeId; + +template +class TypeIdHelper { + public: + // dummy_ must not have a const type. Otherwise an overly eager + // compiler (e.g. MSVC 7.1 & 8.0) may try to merge + // TypeIdHelper::dummy_ for different Ts as an "optimization". + static bool dummy_; +}; + +template +bool TypeIdHelper::dummy_ = false; + +// GetTypeId() returns the ID of type T. Different values will be +// returned for different types. Calling the function twice with the +// same type argument is guaranteed to return the same ID. +template +TypeId GetTypeId() { + // The compiler is required to allocate a different + // TypeIdHelper::dummy_ variable for each T used to instantiate + // the template. Therefore, the address of dummy_ is guaranteed to + // be unique. + return &(TypeIdHelper::dummy_); +} + +// Returns the type ID of ::testing::Test. Always call this instead +// of GetTypeId< ::testing::Test>() to get the type ID of +// ::testing::Test, as the latter may give the wrong result due to a +// suspected linker bug when compiling Google Test as a Mac OS X +// framework. +GTEST_API_ TypeId GetTestTypeId(); + +// Defines the abstract factory interface that creates instances +// of a Test object. +class TestFactoryBase { + public: + virtual ~TestFactoryBase() {} + + // Creates a test instance to run. The instance is both created and destroyed + // within TestInfoImpl::Run() + virtual Test* CreateTest() = 0; + + protected: + TestFactoryBase() {} + + private: + TestFactoryBase(const TestFactoryBase&) = delete; + TestFactoryBase& operator=(const TestFactoryBase&) = delete; +}; + +// This class provides implementation of TeastFactoryBase interface. +// It is used in TEST and TEST_F macros. +template +class TestFactoryImpl : public TestFactoryBase { + public: + Test* CreateTest() override { return new TestClass; } +}; + +#if GTEST_OS_WINDOWS + +// Predicate-formatters for implementing the HRESULT checking macros +// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED} +// We pass a long instead of HRESULT to avoid causing an +// include dependency for the HRESULT type. +GTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr, + long hr); // NOLINT +GTEST_API_ AssertionResult IsHRESULTFailure(const char* expr, + long hr); // NOLINT + +#endif // GTEST_OS_WINDOWS + +// Types of SetUpTestSuite() and TearDownTestSuite() functions. +using SetUpTestSuiteFunc = void (*)(); +using TearDownTestSuiteFunc = void (*)(); + +struct CodeLocation { + CodeLocation(const std::string& a_file, int a_line) + : file(a_file), line(a_line) {} + + std::string file; + int line; +}; + +// Helper to identify which setup function for TestCase / TestSuite to call. +// Only one function is allowed, either TestCase or TestSute but not both. + +// Utility functions to help SuiteApiResolver +using SetUpTearDownSuiteFuncType = void (*)(); + +inline SetUpTearDownSuiteFuncType GetNotDefaultOrNull( + SetUpTearDownSuiteFuncType a, SetUpTearDownSuiteFuncType def) { + return a == def ? nullptr : a; +} + +template +// Note that SuiteApiResolver inherits from T because +// SetUpTestSuite()/TearDownTestSuite() could be protected. This way +// SuiteApiResolver can access them. +struct SuiteApiResolver : T { + // testing::Test is only forward declared at this point. So we make it a + // dependent class for the compiler to be OK with it. + using Test = + typename std::conditional::type; + + static SetUpTearDownSuiteFuncType GetSetUpCaseOrSuite(const char* filename, + int line_num) { +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + SetUpTearDownSuiteFuncType test_case_fp = + GetNotDefaultOrNull(&T::SetUpTestCase, &Test::SetUpTestCase); + SetUpTearDownSuiteFuncType test_suite_fp = + GetNotDefaultOrNull(&T::SetUpTestSuite, &Test::SetUpTestSuite); + + GTEST_CHECK_(!test_case_fp || !test_suite_fp) + << "Test can not provide both SetUpTestSuite and SetUpTestCase, please " + "make sure there is only one present at " + << filename << ":" << line_num; + + return test_case_fp != nullptr ? test_case_fp : test_suite_fp; +#else + (void)(filename); + (void)(line_num); + return &T::SetUpTestSuite; +#endif + } + + static SetUpTearDownSuiteFuncType GetTearDownCaseOrSuite(const char* filename, + int line_num) { +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + SetUpTearDownSuiteFuncType test_case_fp = + GetNotDefaultOrNull(&T::TearDownTestCase, &Test::TearDownTestCase); + SetUpTearDownSuiteFuncType test_suite_fp = + GetNotDefaultOrNull(&T::TearDownTestSuite, &Test::TearDownTestSuite); + + GTEST_CHECK_(!test_case_fp || !test_suite_fp) + << "Test can not provide both TearDownTestSuite and TearDownTestCase," + " please make sure there is only one present at" + << filename << ":" << line_num; + + return test_case_fp != nullptr ? test_case_fp : test_suite_fp; +#else + (void)(filename); + (void)(line_num); + return &T::TearDownTestSuite; +#endif + } +}; + +// Creates a new TestInfo object and registers it with Google Test; +// returns the created object. +// +// Arguments: +// +// test_suite_name: name of the test suite +// name: name of the test +// type_param: the name of the test's type parameter, or NULL if +// this is not a typed or a type-parameterized test. +// value_param: text representation of the test's value parameter, +// or NULL if this is not a type-parameterized test. +// code_location: code location where the test is defined +// fixture_class_id: ID of the test fixture class +// set_up_tc: pointer to the function that sets up the test suite +// tear_down_tc: pointer to the function that tears down the test suite +// factory: pointer to the factory that creates a test object. +// The newly created TestInfo instance will assume +// ownership of the factory object. +GTEST_API_ TestInfo* MakeAndRegisterTestInfo( + const char* test_suite_name, const char* name, const char* type_param, + const char* value_param, CodeLocation code_location, + TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, + TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory); + +// If *pstr starts with the given prefix, modifies *pstr to be right +// past the prefix and returns true; otherwise leaves *pstr unchanged +// and returns false. None of pstr, *pstr, and prefix can be NULL. +GTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr); + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +// State of the definition of a type-parameterized test suite. +class GTEST_API_ TypedTestSuitePState { + public: + TypedTestSuitePState() : registered_(false) {} + + // Adds the given test name to defined_test_names_ and return true + // if the test suite hasn't been registered; otherwise aborts the + // program. + bool AddTestName(const char* file, int line, const char* case_name, + const char* test_name) { + if (registered_) { + fprintf(stderr, + "%s Test %s must be defined before " + "REGISTER_TYPED_TEST_SUITE_P(%s, ...).\n", + FormatFileLocation(file, line).c_str(), test_name, case_name); + fflush(stderr); + posix::Abort(); + } + registered_tests_.insert( + ::std::make_pair(test_name, CodeLocation(file, line))); + return true; + } + + bool TestExists(const std::string& test_name) const { + return registered_tests_.count(test_name) > 0; + } + + const CodeLocation& GetCodeLocation(const std::string& test_name) const { + RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name); + GTEST_CHECK_(it != registered_tests_.end()); + return it->second; + } + + // Verifies that registered_tests match the test names in + // defined_test_names_; returns registered_tests if successful, or + // aborts the program otherwise. + const char* VerifyRegisteredTestNames(const char* test_suite_name, + const char* file, int line, + const char* registered_tests); + + private: + typedef ::std::map RegisteredTestsMap; + + bool registered_; + RegisteredTestsMap registered_tests_; +}; + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +using TypedTestCasePState = TypedTestSuitePState; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +// Skips to the first non-space char after the first comma in 'str'; +// returns NULL if no comma is found in 'str'. +inline const char* SkipComma(const char* str) { + const char* comma = strchr(str, ','); + if (comma == nullptr) { + return nullptr; + } + while (IsSpace(*(++comma))) { + } + return comma; +} + +// Returns the prefix of 'str' before the first comma in it; returns +// the entire string if it contains no comma. +inline std::string GetPrefixUntilComma(const char* str) { + const char* comma = strchr(str, ','); + return comma == nullptr ? str : std::string(str, comma); +} + +// Splits a given string on a given delimiter, populating a given +// vector with the fields. +void SplitString(const ::std::string& str, char delimiter, + ::std::vector<::std::string>* dest); + +// The default argument to the template below for the case when the user does +// not provide a name generator. +struct DefaultNameGenerator { + template + static std::string GetName(int i) { + return StreamableToString(i); + } +}; + +template +struct NameGeneratorSelector { + typedef Provided type; +}; + +template +void GenerateNamesRecursively(internal::None, std::vector*, int) {} + +template +void GenerateNamesRecursively(Types, std::vector* result, int i) { + result->push_back(NameGenerator::template GetName(i)); + GenerateNamesRecursively(typename Types::Tail(), result, + i + 1); +} + +template +std::vector GenerateNames() { + std::vector result; + GenerateNamesRecursively(Types(), &result, 0); + return result; +} + +// TypeParameterizedTest::Register() +// registers a list of type-parameterized tests with Google Test. The +// return value is insignificant - we just need to return something +// such that we can call this function in a namespace scope. +// +// Implementation note: The GTEST_TEMPLATE_ macro declares a template +// template parameter. It's defined in gtest-type-util.h. +template +class TypeParameterizedTest { + public: + // 'index' is the index of the test in the type list 'Types' + // specified in INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, TestSuite, + // Types). Valid values for 'index' are [0, N - 1] where N is the + // length of Types. + static bool Register(const char* prefix, const CodeLocation& code_location, + const char* case_name, const char* test_names, int index, + const std::vector& type_names = + GenerateNames()) { + typedef typename Types::Head Type; + typedef Fixture FixtureClass; + typedef typename GTEST_BIND_(TestSel, Type) TestClass; + + // First, registers the first type-parameterized test in the type + // list. + MakeAndRegisterTestInfo( + (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + + "/" + type_names[static_cast(index)]) + .c_str(), + StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(), + GetTypeName().c_str(), + nullptr, // No value parameter. + code_location, GetTypeId(), + SuiteApiResolver::GetSetUpCaseOrSuite( + code_location.file.c_str(), code_location.line), + SuiteApiResolver::GetTearDownCaseOrSuite( + code_location.file.c_str(), code_location.line), + new TestFactoryImpl); + + // Next, recurses (at compile time) with the tail of the type list. + return TypeParameterizedTest::Register(prefix, + code_location, + case_name, + test_names, + index + 1, + type_names); + } +}; + +// The base case for the compile time recursion. +template +class TypeParameterizedTest { + public: + static bool Register(const char* /*prefix*/, const CodeLocation&, + const char* /*case_name*/, const char* /*test_names*/, + int /*index*/, + const std::vector& = + std::vector() /*type_names*/) { + return true; + } +}; + +GTEST_API_ void RegisterTypeParameterizedTestSuite(const char* test_suite_name, + CodeLocation code_location); +GTEST_API_ void RegisterTypeParameterizedTestSuiteInstantiation( + const char* case_name); + +// TypeParameterizedTestSuite::Register() +// registers *all combinations* of 'Tests' and 'Types' with Google +// Test. The return value is insignificant - we just need to return +// something such that we can call this function in a namespace scope. +template +class TypeParameterizedTestSuite { + public: + static bool Register(const char* prefix, CodeLocation code_location, + const TypedTestSuitePState* state, const char* case_name, + const char* test_names, + const std::vector& type_names = + GenerateNames()) { + RegisterTypeParameterizedTestSuiteInstantiation(case_name); + std::string test_name = + StripTrailingSpaces(GetPrefixUntilComma(test_names)); + if (!state->TestExists(test_name)) { + fprintf(stderr, "Failed to get code location for test %s.%s at %s.", + case_name, test_name.c_str(), + FormatFileLocation(code_location.file.c_str(), code_location.line) + .c_str()); + fflush(stderr); + posix::Abort(); + } + const CodeLocation& test_location = state->GetCodeLocation(test_name); + + typedef typename Tests::Head Head; + + // First, register the first test in 'Test' for each type in 'Types'. + TypeParameterizedTest::Register( + prefix, test_location, case_name, test_names, 0, type_names); + + // Next, recurses (at compile time) with the tail of the test list. + return TypeParameterizedTestSuite::Register(prefix, code_location, + state, case_name, + SkipComma(test_names), + type_names); + } +}; + +// The base case for the compile time recursion. +template +class TypeParameterizedTestSuite { + public: + static bool Register(const char* /*prefix*/, const CodeLocation&, + const TypedTestSuitePState* /*state*/, + const char* /*case_name*/, const char* /*test_names*/, + const std::vector& = + std::vector() /*type_names*/) { + return true; + } +}; + +// Returns the current OS stack trace as an std::string. +// +// The maximum number of stack frames to be included is specified by +// the gtest_stack_trace_depth flag. The skip_count parameter +// specifies the number of top frames to be skipped, which doesn't +// count against the number of frames to be included. +// +// For example, if Foo() calls Bar(), which in turn calls +// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in +// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. +GTEST_API_ std::string GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, + int skip_count); + +// Helpers for suppressing warnings on unreachable code or constant +// condition. + +// Always returns true. +GTEST_API_ bool AlwaysTrue(); + +// Always returns false. +inline bool AlwaysFalse() { return !AlwaysTrue(); } + +// Helper for suppressing false warning from Clang on a const char* +// variable declared in a conditional expression always being NULL in +// the else branch. +struct GTEST_API_ ConstCharPtr { + ConstCharPtr(const char* str) : value(str) {} + operator bool() const { return true; } + const char* value; +}; + +// Helper for declaring std::string within 'if' statement +// in pre C++17 build environment. +struct TrueWithString { + TrueWithString() = default; + explicit TrueWithString(const char* str) : value(str) {} + explicit TrueWithString(const std::string& str) : value(str) {} + explicit operator bool() const { return true; } + std::string value; +}; + +// A simple Linear Congruential Generator for generating random +// numbers with a uniform distribution. Unlike rand() and srand(), it +// doesn't use global state (and therefore can't interfere with user +// code). Unlike rand_r(), it's portable. An LCG isn't very random, +// but it's good enough for our purposes. +class GTEST_API_ Random { + public: + static const uint32_t kMaxRange = 1u << 31; + + explicit Random(uint32_t seed) : state_(seed) {} + + void Reseed(uint32_t seed) { state_ = seed; } + + // Generates a random number from [0, range). Crashes if 'range' is + // 0 or greater than kMaxRange. + uint32_t Generate(uint32_t range); + + private: + uint32_t state_; + Random(const Random&) = delete; + Random& operator=(const Random&) = delete; +}; + +// Turns const U&, U&, const U, and U all into U. +#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ + typename std::remove_const::type>::type + +// HasDebugStringAndShortDebugString::value is a compile-time bool constant +// that's true if and only if T has methods DebugString() and ShortDebugString() +// that return std::string. +template +class HasDebugStringAndShortDebugString { + private: + template + static auto CheckDebugString(C*) -> typename std::is_same< + std::string, decltype(std::declval().DebugString())>::type; + template + static std::false_type CheckDebugString(...); + + template + static auto CheckShortDebugString(C*) -> typename std::is_same< + std::string, decltype(std::declval().ShortDebugString())>::type; + template + static std::false_type CheckShortDebugString(...); + + using HasDebugStringType = decltype(CheckDebugString(nullptr)); + using HasShortDebugStringType = decltype(CheckShortDebugString(nullptr)); + + public: + static constexpr bool value = + HasDebugStringType::value && HasShortDebugStringType::value; +}; + +template +constexpr bool HasDebugStringAndShortDebugString::value; + +// When the compiler sees expression IsContainerTest(0), if C is an +// STL-style container class, the first overload of IsContainerTest +// will be viable (since both C::iterator* and C::const_iterator* are +// valid types and NULL can be implicitly converted to them). It will +// be picked over the second overload as 'int' is a perfect match for +// the type of argument 0. If C::iterator or C::const_iterator is not +// a valid type, the first overload is not viable, and the second +// overload will be picked. Therefore, we can determine whether C is +// a container class by checking the type of IsContainerTest(0). +// The value of the expression is insignificant. +// +// In C++11 mode we check the existence of a const_iterator and that an +// iterator is properly implemented for the container. +// +// For pre-C++11 that we look for both C::iterator and C::const_iterator. +// The reason is that C++ injects the name of a class as a member of the +// class itself (e.g. you can refer to class iterator as either +// 'iterator' or 'iterator::iterator'). If we look for C::iterator +// only, for example, we would mistakenly think that a class named +// iterator is an STL container. +// +// Also note that the simpler approach of overloading +// IsContainerTest(typename C::const_iterator*) and +// IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++. +typedef int IsContainer; +template ().begin()), + class = decltype(::std::declval().end()), + class = decltype(++::std::declval()), + class = decltype(*::std::declval()), + class = typename C::const_iterator> +IsContainer IsContainerTest(int /* dummy */) { + return 0; +} + +typedef char IsNotContainer; +template +IsNotContainer IsContainerTest(long /* dummy */) { + return '\0'; +} + +// Trait to detect whether a type T is a hash table. +// The heuristic used is that the type contains an inner type `hasher` and does +// not contain an inner type `reverse_iterator`. +// If the container is iterable in reverse, then order might actually matter. +template +struct IsHashTable { + private: + template + static char test(typename U::hasher*, typename U::reverse_iterator*); + template + static int test(typename U::hasher*, ...); + template + static char test(...); + + public: + static const bool value = sizeof(test(nullptr, nullptr)) == sizeof(int); +}; + +template +const bool IsHashTable::value; + +template (0)) == sizeof(IsContainer)> +struct IsRecursiveContainerImpl; + +template +struct IsRecursiveContainerImpl : public std::false_type {}; + +// Since the IsRecursiveContainerImpl depends on the IsContainerTest we need to +// obey the same inconsistencies as the IsContainerTest, namely check if +// something is a container is relying on only const_iterator in C++11 and +// is relying on both const_iterator and iterator otherwise +template +struct IsRecursiveContainerImpl { + using value_type = decltype(*std::declval()); + using type = + std::is_same::type>::type, + C>; +}; + +// IsRecursiveContainer is a unary compile-time predicate that +// evaluates whether C is a recursive container type. A recursive container +// type is a container type whose value_type is equal to the container type +// itself. An example for a recursive container type is +// boost::filesystem::path, whose iterator has a value_type that is equal to +// boost::filesystem::path. +template +struct IsRecursiveContainer : public IsRecursiveContainerImpl::type {}; + +// Utilities for native arrays. + +// ArrayEq() compares two k-dimensional native arrays using the +// elements' operator==, where k can be any integer >= 0. When k is +// 0, ArrayEq() degenerates into comparing a single pair of values. + +template +bool ArrayEq(const T* lhs, size_t size, const U* rhs); + +// This generic version is used when k is 0. +template +inline bool ArrayEq(const T& lhs, const U& rhs) { + return lhs == rhs; +} + +// This overload is used when k >= 1. +template +inline bool ArrayEq(const T (&lhs)[N], const U (&rhs)[N]) { + return internal::ArrayEq(lhs, N, rhs); +} + +// This helper reduces code bloat. If we instead put its logic inside +// the previous ArrayEq() function, arrays with different sizes would +// lead to different copies of the template code. +template +bool ArrayEq(const T* lhs, size_t size, const U* rhs) { + for (size_t i = 0; i != size; i++) { + if (!internal::ArrayEq(lhs[i], rhs[i])) return false; + } + return true; +} + +// Finds the first element in the iterator range [begin, end) that +// equals elem. Element may be a native array type itself. +template +Iter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { + for (Iter it = begin; it != end; ++it) { + if (internal::ArrayEq(*it, elem)) return it; + } + return end; +} + +// CopyArray() copies a k-dimensional native array using the elements' +// operator=, where k can be any integer >= 0. When k is 0, +// CopyArray() degenerates into copying a single value. + +template +void CopyArray(const T* from, size_t size, U* to); + +// This generic version is used when k is 0. +template +inline void CopyArray(const T& from, U* to) { + *to = from; +} + +// This overload is used when k >= 1. +template +inline void CopyArray(const T (&from)[N], U (*to)[N]) { + internal::CopyArray(from, N, *to); +} + +// This helper reduces code bloat. If we instead put its logic inside +// the previous CopyArray() function, arrays with different sizes +// would lead to different copies of the template code. +template +void CopyArray(const T* from, size_t size, U* to) { + for (size_t i = 0; i != size; i++) { + internal::CopyArray(from[i], to + i); + } +} + +// The relation between an NativeArray object (see below) and the +// native array it represents. +// We use 2 different structs to allow non-copyable types to be used, as long +// as RelationToSourceReference() is passed. +struct RelationToSourceReference {}; +struct RelationToSourceCopy {}; + +// Adapts a native array to a read-only STL-style container. Instead +// of the complete STL container concept, this adaptor only implements +// members useful for Google Mock's container matchers. New members +// should be added as needed. To simplify the implementation, we only +// support Element being a raw type (i.e. having no top-level const or +// reference modifier). It's the client's responsibility to satisfy +// this requirement. Element can be an array type itself (hence +// multi-dimensional arrays are supported). +template +class NativeArray { + public: + // STL-style container typedefs. + typedef Element value_type; + typedef Element* iterator; + typedef const Element* const_iterator; + + // Constructs from a native array. References the source. + NativeArray(const Element* array, size_t count, RelationToSourceReference) { + InitRef(array, count); + } + + // Constructs from a native array. Copies the source. + NativeArray(const Element* array, size_t count, RelationToSourceCopy) { + InitCopy(array, count); + } + + // Copy constructor. + NativeArray(const NativeArray& rhs) { + (this->*rhs.clone_)(rhs.array_, rhs.size_); + } + + ~NativeArray() { + if (clone_ != &NativeArray::InitRef) delete[] array_; + } + + // STL-style container methods. + size_t size() const { return size_; } + const_iterator begin() const { return array_; } + const_iterator end() const { return array_ + size_; } + bool operator==(const NativeArray& rhs) const { + return size() == rhs.size() && ArrayEq(begin(), size(), rhs.begin()); + } + + private: + static_assert(!std::is_const::value, "Type must not be const"); + static_assert(!std::is_reference::value, + "Type must not be a reference"); + + // Initializes this object with a copy of the input. + void InitCopy(const Element* array, size_t a_size) { + Element* const copy = new Element[a_size]; + CopyArray(array, a_size, copy); + array_ = copy; + size_ = a_size; + clone_ = &NativeArray::InitCopy; + } + + // Initializes this object with a reference of the input. + void InitRef(const Element* array, size_t a_size) { + array_ = array; + size_ = a_size; + clone_ = &NativeArray::InitRef; + } + + const Element* array_; + size_t size_; + void (NativeArray::*clone_)(const Element*, size_t); +}; + +// Backport of std::index_sequence. +template +struct IndexSequence { + using type = IndexSequence; +}; + +// Double the IndexSequence, and one if plus_one is true. +template +struct DoubleSequence; +template +struct DoubleSequence, sizeofT> { + using type = IndexSequence; +}; +template +struct DoubleSequence, sizeofT> { + using type = IndexSequence; +}; + +// Backport of std::make_index_sequence. +// It uses O(ln(N)) instantiation depth. +template +struct MakeIndexSequenceImpl + : DoubleSequence::type, + N / 2>::type {}; + +template <> +struct MakeIndexSequenceImpl<0> : IndexSequence<> {}; + +template +using MakeIndexSequence = typename MakeIndexSequenceImpl::type; + +template +using IndexSequenceFor = typename MakeIndexSequence::type; + +template +struct Ignore { + Ignore(...); // NOLINT +}; + +template +struct ElemFromListImpl; +template +struct ElemFromListImpl> { + // We make Ignore a template to solve a problem with MSVC. + // A non-template Ignore would work fine with `decltype(Ignore(I))...`, but + // MSVC doesn't understand how to deal with that pack expansion. + // Use `0 * I` to have a single instantiation of Ignore. + template + static R Apply(Ignore<0 * I>..., R (*)(), ...); +}; + +template +struct ElemFromList { + using type = + decltype(ElemFromListImpl::type>::Apply( + static_cast(nullptr)...)); +}; + +struct FlatTupleConstructTag {}; + +template +class FlatTuple; + +template +struct FlatTupleElemBase; + +template +struct FlatTupleElemBase, I> { + using value_type = typename ElemFromList::type; + FlatTupleElemBase() = default; + template + explicit FlatTupleElemBase(FlatTupleConstructTag, Arg&& t) + : value(std::forward(t)) {} + value_type value; +}; + +template +struct FlatTupleBase; + +template +struct FlatTupleBase, IndexSequence> + : FlatTupleElemBase, Idx>... { + using Indices = IndexSequence; + FlatTupleBase() = default; + template + explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args) + : FlatTupleElemBase, Idx>(FlatTupleConstructTag{}, + std::forward(args))... {} + + template + const typename ElemFromList::type& Get() const { + return FlatTupleElemBase, I>::value; + } + + template + typename ElemFromList::type& Get() { + return FlatTupleElemBase, I>::value; + } + + template + auto Apply(F&& f) -> decltype(std::forward(f)(this->Get()...)) { + return std::forward(f)(Get()...); + } + + template + auto Apply(F&& f) const -> decltype(std::forward(f)(this->Get()...)) { + return std::forward(f)(Get()...); + } +}; + +// Analog to std::tuple but with different tradeoffs. +// This class minimizes the template instantiation depth, thus allowing more +// elements than std::tuple would. std::tuple has been seen to require an +// instantiation depth of more than 10x the number of elements in some +// implementations. +// FlatTuple and ElemFromList are not recursive and have a fixed depth +// regardless of T... +// MakeIndexSequence, on the other hand, it is recursive but with an +// instantiation depth of O(ln(N)). +template +class FlatTuple + : private FlatTupleBase, + typename MakeIndexSequence::type> { + using Indices = typename FlatTupleBase< + FlatTuple, typename MakeIndexSequence::type>::Indices; + + public: + FlatTuple() = default; + template + explicit FlatTuple(FlatTupleConstructTag tag, Args&&... args) + : FlatTuple::FlatTupleBase(tag, std::forward(args)...) {} + + using FlatTuple::FlatTupleBase::Apply; + using FlatTuple::FlatTupleBase::Get; +}; + +// Utility functions to be called with static_assert to induce deprecation +// warnings. +GTEST_INTERNAL_DEPRECATED( + "INSTANTIATE_TEST_CASE_P is deprecated, please use " + "INSTANTIATE_TEST_SUITE_P") +constexpr bool InstantiateTestCase_P_IsDeprecated() { return true; } + +GTEST_INTERNAL_DEPRECATED( + "TYPED_TEST_CASE_P is deprecated, please use " + "TYPED_TEST_SUITE_P") +constexpr bool TypedTestCase_P_IsDeprecated() { return true; } + +GTEST_INTERNAL_DEPRECATED( + "TYPED_TEST_CASE is deprecated, please use " + "TYPED_TEST_SUITE") +constexpr bool TypedTestCaseIsDeprecated() { return true; } + +GTEST_INTERNAL_DEPRECATED( + "REGISTER_TYPED_TEST_CASE_P is deprecated, please use " + "REGISTER_TYPED_TEST_SUITE_P") +constexpr bool RegisterTypedTestCase_P_IsDeprecated() { return true; } + +GTEST_INTERNAL_DEPRECATED( + "INSTANTIATE_TYPED_TEST_CASE_P is deprecated, please use " + "INSTANTIATE_TYPED_TEST_SUITE_P") +constexpr bool InstantiateTypedTestCase_P_IsDeprecated() { return true; } + +} // namespace internal +} // namespace testing + +namespace std { +// Some standard library implementations use `struct tuple_size` and some use +// `class tuple_size`. Clang warns about the mismatch. +// https://reviews.llvm.org/D55466 +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wmismatched-tags" +#endif +template +struct tuple_size> + : std::integral_constant {}; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +} // namespace std + +#define GTEST_MESSAGE_AT_(file, line, message, result_type) \ + ::testing::internal::AssertHelper(result_type, file, line, message) = \ + ::testing::Message() + +#define GTEST_MESSAGE_(message, result_type) \ + GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) + +#define GTEST_FATAL_FAILURE_(message) \ + return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) + +#define GTEST_NONFATAL_FAILURE_(message) \ + GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) + +#define GTEST_SUCCESS_(message) \ + GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) + +#define GTEST_SKIP_(message) \ + return GTEST_MESSAGE_(message, ::testing::TestPartResult::kSkip) + +// Suppress MSVC warning 4072 (unreachable code) for the code following +// statement if it returns or throws (or doesn't return or throw in some +// situations). +// NOTE: The "else" is important to keep this expansion to prevent a top-level +// "else" from attaching to our "if". +#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ + if (::testing::internal::AlwaysTrue()) { \ + statement; \ + } else /* NOLINT */ \ + static_assert(true, "") // User must have a semicolon after expansion. + +#if GTEST_HAS_EXCEPTIONS + +namespace testing { +namespace internal { + +class NeverThrown { + public: + const char* what() const noexcept { + return "this exception should never be thrown"; + } +}; + +} // namespace internal +} // namespace testing + +#if GTEST_HAS_RTTI + +#define GTEST_EXCEPTION_TYPE_(e) ::testing::internal::GetTypeName(typeid(e)) + +#else // GTEST_HAS_RTTI + +#define GTEST_EXCEPTION_TYPE_(e) \ + std::string { "an std::exception-derived error" } + +#endif // GTEST_HAS_RTTI + +#define GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) \ + catch (typename std::conditional< \ + std::is_same::type>::type, \ + std::exception>::value, \ + const ::testing::internal::NeverThrown&, const std::exception&>::type \ + e) { \ + gtest_msg.value = "Expected: " #statement \ + " throws an exception of type " #expected_exception \ + ".\n Actual: it throws "; \ + gtest_msg.value += GTEST_EXCEPTION_TYPE_(e); \ + gtest_msg.value += " with description \""; \ + gtest_msg.value += e.what(); \ + gtest_msg.value += "\"."; \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } + +#else // GTEST_HAS_EXCEPTIONS + +#define GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) + +#endif // GTEST_HAS_EXCEPTIONS + +#define GTEST_TEST_THROW_(statement, expected_exception, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::TrueWithString gtest_msg{}) { \ + bool gtest_caught_expected = false; \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } catch (expected_exception const&) { \ + gtest_caught_expected = true; \ + } \ + GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) \ + catch (...) { \ + gtest_msg.value = "Expected: " #statement \ + " throws an exception of type " #expected_exception \ + ".\n Actual: it throws a different type."; \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } \ + if (!gtest_caught_expected) { \ + gtest_msg.value = "Expected: " #statement \ + " throws an exception of type " #expected_exception \ + ".\n Actual: it throws nothing."; \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ + } \ + } else /*NOLINT*/ \ + GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \ + : fail(gtest_msg.value.c_str()) + +#if GTEST_HAS_EXCEPTIONS + +#define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \ + catch (std::exception const& e) { \ + gtest_msg.value = "it throws "; \ + gtest_msg.value += GTEST_EXCEPTION_TYPE_(e); \ + gtest_msg.value += " with description \""; \ + gtest_msg.value += e.what(); \ + gtest_msg.value += "\"."; \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ + } + +#else // GTEST_HAS_EXCEPTIONS + +#define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() + +#endif // GTEST_HAS_EXCEPTIONS + +#define GTEST_TEST_NO_THROW_(statement, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::TrueWithString gtest_msg{}) { \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } \ + GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \ + catch (...) { \ + gtest_msg.value = "it throws."; \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__) \ + : fail(("Expected: " #statement " doesn't throw an exception.\n" \ + " Actual: " + \ + gtest_msg.value) \ + .c_str()) + +#define GTEST_TEST_ANY_THROW_(statement, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::AlwaysTrue()) { \ + bool gtest_caught_any = false; \ + try { \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + } catch (...) { \ + gtest_caught_any = true; \ + } \ + if (!gtest_caught_any) { \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__) \ + : fail("Expected: " #statement \ + " throws an exception.\n" \ + " Actual: it doesn't.") + +// Implements Boolean test assertions such as EXPECT_TRUE. expression can be +// either a boolean expression or an AssertionResult. text is a textual +// representation of expression as it was passed into the EXPECT_TRUE. +#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (const ::testing::AssertionResult gtest_ar_ = \ + ::testing::AssertionResult(expression)) \ + ; \ + else \ + fail(::testing::internal::GetBoolAssertionFailureMessage( \ + gtest_ar_, text, #actual, #expected) \ + .c_str()) + +#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::AlwaysTrue()) { \ + ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ + GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ + if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ + goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ + } \ + } else \ + GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__) \ + : fail("Expected: " #statement \ + " doesn't generate new fatal " \ + "failures in the current thread.\n" \ + " Actual: it does.") + +// Expands to the name of the class that implements the given test. +#define GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + test_suite_name##_##test_name##_Test + +// Helper macro for defining tests. +#define GTEST_TEST_(test_suite_name, test_name, parent_class, parent_id) \ + static_assert(sizeof(GTEST_STRINGIFY_(test_suite_name)) > 1, \ + "test_suite_name must not be empty"); \ + static_assert(sizeof(GTEST_STRINGIFY_(test_name)) > 1, \ + "test_name must not be empty"); \ + class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + : public parent_class { \ + public: \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() = default; \ + ~GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() override = default; \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + (const GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &) = delete; \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \ + const GTEST_TEST_CLASS_NAME_(test_suite_name, \ + test_name) &) = delete; /* NOLINT */ \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + (GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &&) noexcept = delete; \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \ + GTEST_TEST_CLASS_NAME_(test_suite_name, \ + test_name) &&) noexcept = delete; /* NOLINT */ \ + \ + private: \ + void TestBody() override; \ + static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ + }; \ + \ + ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \ + test_name)::test_info_ = \ + ::testing::internal::MakeAndRegisterTestInfo( \ + #test_suite_name, #test_name, nullptr, nullptr, \ + ::testing::internal::CodeLocation(__FILE__, __LINE__), (parent_id), \ + ::testing::internal::SuiteApiResolver< \ + parent_class>::GetSetUpCaseOrSuite(__FILE__, __LINE__), \ + ::testing::internal::SuiteApiResolver< \ + parent_class>::GetTearDownCaseOrSuite(__FILE__, __LINE__), \ + new ::testing::internal::TestFactoryImpl); \ + void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody() + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ diff --git a/test/gtest/include/gtest/internal/gtest-param-util.h b/test/gtest/include/gtest/internal/gtest-param-util.h new file mode 100644 index 0000000000..e7af2f904a --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-param-util.h @@ -0,0 +1,956 @@ +// Copyright 2008 Google Inc. +// All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Type and function utilities for implementing parameterized tests. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest-printers.h" +#include "gtest/gtest-test-part.h" +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-port.h" + +namespace testing { +// Input to a parameterized test name generator, describing a test parameter. +// Consists of the parameter value and the integer parameter index. +template +struct TestParamInfo { + TestParamInfo(const ParamType& a_param, size_t an_index) + : param(a_param), index(an_index) {} + ParamType param; + size_t index; +}; + +// A builtin parameterized test name generator which returns the result of +// testing::PrintToString. +struct PrintToStringParamName { + template + std::string operator()(const TestParamInfo& info) const { + return PrintToString(info.param); + } +}; + +namespace internal { + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// Utility Functions + +// Outputs a message explaining invalid registration of different +// fixture class for the same test suite. This may happen when +// TEST_P macro is used to define two tests with the same name +// but in different namespaces. +GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name, + CodeLocation code_location); + +template +class ParamGeneratorInterface; +template +class ParamGenerator; + +// Interface for iterating over elements provided by an implementation +// of ParamGeneratorInterface. +template +class ParamIteratorInterface { + public: + virtual ~ParamIteratorInterface() {} + // A pointer to the base generator instance. + // Used only for the purposes of iterator comparison + // to make sure that two iterators belong to the same generator. + virtual const ParamGeneratorInterface* BaseGenerator() const = 0; + // Advances iterator to point to the next element + // provided by the generator. The caller is responsible + // for not calling Advance() on an iterator equal to + // BaseGenerator()->End(). + virtual void Advance() = 0; + // Clones the iterator object. Used for implementing copy semantics + // of ParamIterator. + virtual ParamIteratorInterface* Clone() const = 0; + // Dereferences the current iterator and provides (read-only) access + // to the pointed value. It is the caller's responsibility not to call + // Current() on an iterator equal to BaseGenerator()->End(). + // Used for implementing ParamGenerator::operator*(). + virtual const T* Current() const = 0; + // Determines whether the given iterator and other point to the same + // element in the sequence generated by the generator. + // Used for implementing ParamGenerator::operator==(). + virtual bool Equals(const ParamIteratorInterface& other) const = 0; +}; + +// Class iterating over elements provided by an implementation of +// ParamGeneratorInterface. It wraps ParamIteratorInterface +// and implements the const forward iterator concept. +template +class ParamIterator { + public: + typedef T value_type; + typedef const T& reference; + typedef ptrdiff_t difference_type; + + // ParamIterator assumes ownership of the impl_ pointer. + ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {} + ParamIterator& operator=(const ParamIterator& other) { + if (this != &other) impl_.reset(other.impl_->Clone()); + return *this; + } + + const T& operator*() const { return *impl_->Current(); } + const T* operator->() const { return impl_->Current(); } + // Prefix version of operator++. + ParamIterator& operator++() { + impl_->Advance(); + return *this; + } + // Postfix version of operator++. + ParamIterator operator++(int /*unused*/) { + ParamIteratorInterface* clone = impl_->Clone(); + impl_->Advance(); + return ParamIterator(clone); + } + bool operator==(const ParamIterator& other) const { + return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_); + } + bool operator!=(const ParamIterator& other) const { + return !(*this == other); + } + + private: + friend class ParamGenerator; + explicit ParamIterator(ParamIteratorInterface* impl) : impl_(impl) {} + std::unique_ptr> impl_; +}; + +// ParamGeneratorInterface is the binary interface to access generators +// defined in other translation units. +template +class ParamGeneratorInterface { + public: + typedef T ParamType; + + virtual ~ParamGeneratorInterface() {} + + // Generator interface definition + virtual ParamIteratorInterface* Begin() const = 0; + virtual ParamIteratorInterface* End() const = 0; +}; + +// Wraps ParamGeneratorInterface and provides general generator syntax +// compatible with the STL Container concept. +// This class implements copy initialization semantics and the contained +// ParamGeneratorInterface instance is shared among all copies +// of the original object. This is possible because that instance is immutable. +template +class ParamGenerator { + public: + typedef ParamIterator iterator; + + explicit ParamGenerator(ParamGeneratorInterface* impl) : impl_(impl) {} + ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {} + + ParamGenerator& operator=(const ParamGenerator& other) { + impl_ = other.impl_; + return *this; + } + + iterator begin() const { return iterator(impl_->Begin()); } + iterator end() const { return iterator(impl_->End()); } + + private: + std::shared_ptr> impl_; +}; + +// Generates values from a range of two comparable values. Can be used to +// generate sequences of user-defined types that implement operator+() and +// operator<(). +// This class is used in the Range() function. +template +class RangeGenerator : public ParamGeneratorInterface { + public: + RangeGenerator(T begin, T end, IncrementT step) + : begin_(begin), + end_(end), + step_(step), + end_index_(CalculateEndIndex(begin, end, step)) {} + ~RangeGenerator() override {} + + ParamIteratorInterface* Begin() const override { + return new Iterator(this, begin_, 0, step_); + } + ParamIteratorInterface* End() const override { + return new Iterator(this, end_, end_index_, step_); + } + + private: + class Iterator : public ParamIteratorInterface { + public: + Iterator(const ParamGeneratorInterface* base, T value, int index, + IncrementT step) + : base_(base), value_(value), index_(index), step_(step) {} + ~Iterator() override {} + + const ParamGeneratorInterface* BaseGenerator() const override { + return base_; + } + void Advance() override { + value_ = static_cast(value_ + step_); + index_++; + } + ParamIteratorInterface* Clone() const override { + return new Iterator(*this); + } + const T* Current() const override { return &value_; } + bool Equals(const ParamIteratorInterface& other) const override { + // Having the same base generator guarantees that the other + // iterator is of the same type and we can downcast. + GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) + << "The program attempted to compare iterators " + << "from different generators." << std::endl; + const int other_index = + CheckedDowncastToActualType(&other)->index_; + return index_ == other_index; + } + + private: + Iterator(const Iterator& other) + : ParamIteratorInterface(), + base_(other.base_), + value_(other.value_), + index_(other.index_), + step_(other.step_) {} + + // No implementation - assignment is unsupported. + void operator=(const Iterator& other); + + const ParamGeneratorInterface* const base_; + T value_; + int index_; + const IncrementT step_; + }; // class RangeGenerator::Iterator + + static int CalculateEndIndex(const T& begin, const T& end, + const IncrementT& step) { + int end_index = 0; + for (T i = begin; i < end; i = static_cast(i + step)) end_index++; + return end_index; + } + + // No implementation - assignment is unsupported. + void operator=(const RangeGenerator& other); + + const T begin_; + const T end_; + const IncrementT step_; + // The index for the end() iterator. All the elements in the generated + // sequence are indexed (0-based) to aid iterator comparison. + const int end_index_; +}; // class RangeGenerator + +// Generates values from a pair of STL-style iterators. Used in the +// ValuesIn() function. The elements are copied from the source range +// since the source can be located on the stack, and the generator +// is likely to persist beyond that stack frame. +template +class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface { + public: + template + ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end) + : container_(begin, end) {} + ~ValuesInIteratorRangeGenerator() override {} + + ParamIteratorInterface* Begin() const override { + return new Iterator(this, container_.begin()); + } + ParamIteratorInterface* End() const override { + return new Iterator(this, container_.end()); + } + + private: + typedef typename ::std::vector ContainerType; + + class Iterator : public ParamIteratorInterface { + public: + Iterator(const ParamGeneratorInterface* base, + typename ContainerType::const_iterator iterator) + : base_(base), iterator_(iterator) {} + ~Iterator() override {} + + const ParamGeneratorInterface* BaseGenerator() const override { + return base_; + } + void Advance() override { + ++iterator_; + value_.reset(); + } + ParamIteratorInterface* Clone() const override { + return new Iterator(*this); + } + // We need to use cached value referenced by iterator_ because *iterator_ + // can return a temporary object (and of type other then T), so just + // having "return &*iterator_;" doesn't work. + // value_ is updated here and not in Advance() because Advance() + // can advance iterator_ beyond the end of the range, and we cannot + // detect that fact. The client code, on the other hand, is + // responsible for not calling Current() on an out-of-range iterator. + const T* Current() const override { + if (value_.get() == nullptr) value_.reset(new T(*iterator_)); + return value_.get(); + } + bool Equals(const ParamIteratorInterface& other) const override { + // Having the same base generator guarantees that the other + // iterator is of the same type and we can downcast. + GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) + << "The program attempted to compare iterators " + << "from different generators." << std::endl; + return iterator_ == + CheckedDowncastToActualType(&other)->iterator_; + } + + private: + Iterator(const Iterator& other) + // The explicit constructor call suppresses a false warning + // emitted by gcc when supplied with the -Wextra option. + : ParamIteratorInterface(), + base_(other.base_), + iterator_(other.iterator_) {} + + const ParamGeneratorInterface* const base_; + typename ContainerType::const_iterator iterator_; + // A cached value of *iterator_. We keep it here to allow access by + // pointer in the wrapping iterator's operator->(). + // value_ needs to be mutable to be accessed in Current(). + // Use of std::unique_ptr helps manage cached value's lifetime, + // which is bound by the lifespan of the iterator itself. + mutable std::unique_ptr value_; + }; // class ValuesInIteratorRangeGenerator::Iterator + + // No implementation - assignment is unsupported. + void operator=(const ValuesInIteratorRangeGenerator& other); + + const ContainerType container_; +}; // class ValuesInIteratorRangeGenerator + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Default parameterized test name generator, returns a string containing the +// integer test parameter index. +template +std::string DefaultParamName(const TestParamInfo& info) { + Message name_stream; + name_stream << info.index; + return name_stream.GetString(); +} + +template +void TestNotEmpty() { + static_assert(sizeof(T) == 0, "Empty arguments are not allowed."); +} +template +void TestNotEmpty(const T&) {} + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Stores a parameter value and later creates tests parameterized with that +// value. +template +class ParameterizedTestFactory : public TestFactoryBase { + public: + typedef typename TestClass::ParamType ParamType; + explicit ParameterizedTestFactory(ParamType parameter) + : parameter_(parameter) {} + Test* CreateTest() override { + TestClass::SetParam(¶meter_); + return new TestClass(); + } + + private: + const ParamType parameter_; + + ParameterizedTestFactory(const ParameterizedTestFactory&) = delete; + ParameterizedTestFactory& operator=(const ParameterizedTestFactory&) = delete; +}; + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// TestMetaFactoryBase is a base class for meta-factories that create +// test factories for passing into MakeAndRegisterTestInfo function. +template +class TestMetaFactoryBase { + public: + virtual ~TestMetaFactoryBase() {} + + virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0; +}; + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// TestMetaFactory creates test factories for passing into +// MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives +// ownership of test factory pointer, same factory object cannot be passed +// into that method twice. But ParameterizedTestSuiteInfo is going to call +// it for each Test/Parameter value combination. Thus it needs meta factory +// creator class. +template +class TestMetaFactory + : public TestMetaFactoryBase { + public: + using ParamType = typename TestSuite::ParamType; + + TestMetaFactory() {} + + TestFactoryBase* CreateTestFactory(ParamType parameter) override { + return new ParameterizedTestFactory(parameter); + } + + private: + TestMetaFactory(const TestMetaFactory&) = delete; + TestMetaFactory& operator=(const TestMetaFactory&) = delete; +}; + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// ParameterizedTestSuiteInfoBase is a generic interface +// to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase +// accumulates test information provided by TEST_P macro invocations +// and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations +// and uses that information to register all resulting test instances +// in RegisterTests method. The ParameterizeTestSuiteRegistry class holds +// a collection of pointers to the ParameterizedTestSuiteInfo objects +// and calls RegisterTests() on each of them when asked. +class ParameterizedTestSuiteInfoBase { + public: + virtual ~ParameterizedTestSuiteInfoBase() {} + + // Base part of test suite name for display purposes. + virtual const std::string& GetTestSuiteName() const = 0; + // Test suite id to verify identity. + virtual TypeId GetTestSuiteTypeId() const = 0; + // UnitTest class invokes this method to register tests in this + // test suite right before running them in RUN_ALL_TESTS macro. + // This method should not be called more than once on any single + // instance of a ParameterizedTestSuiteInfoBase derived class. + virtual void RegisterTests() = 0; + + protected: + ParameterizedTestSuiteInfoBase() {} + + private: + ParameterizedTestSuiteInfoBase(const ParameterizedTestSuiteInfoBase&) = + delete; + ParameterizedTestSuiteInfoBase& operator=( + const ParameterizedTestSuiteInfoBase&) = delete; +}; + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Report a the name of a test_suit as safe to ignore +// as the side effect of construction of this type. +struct GTEST_API_ MarkAsIgnored { + explicit MarkAsIgnored(const char* test_suite); +}; + +GTEST_API_ void InsertSyntheticTestCase(const std::string& name, + CodeLocation location, bool has_test_p); + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P +// macro invocations for a particular test suite and generators +// obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that +// test suite. It registers tests with all values generated by all +// generators when asked. +template +class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase { + public: + // ParamType and GeneratorCreationFunc are private types but are required + // for declarations of public methods AddTestPattern() and + // AddTestSuiteInstantiation(). + using ParamType = typename TestSuite::ParamType; + // A function that returns an instance of appropriate generator type. + typedef ParamGenerator(GeneratorCreationFunc)(); + using ParamNameGeneratorFunc = std::string(const TestParamInfo&); + + explicit ParameterizedTestSuiteInfo(const char* name, + CodeLocation code_location) + : test_suite_name_(name), code_location_(code_location) {} + + // Test suite base name for display purposes. + const std::string& GetTestSuiteName() const override { + return test_suite_name_; + } + // Test suite id to verify identity. + TypeId GetTestSuiteTypeId() const override { return GetTypeId(); } + // TEST_P macro uses AddTestPattern() to record information + // about a single test in a LocalTestInfo structure. + // test_suite_name is the base name of the test suite (without invocation + // prefix). test_base_name is the name of an individual test without + // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is + // test suite base name and DoBar is test base name. + void AddTestPattern(const char* test_suite_name, const char* test_base_name, + TestMetaFactoryBase* meta_factory, + CodeLocation code_location) { + tests_.push_back(std::shared_ptr(new TestInfo( + test_suite_name, test_base_name, meta_factory, code_location))); + } + // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information + // about a generator. + int AddTestSuiteInstantiation(const std::string& instantiation_name, + GeneratorCreationFunc* func, + ParamNameGeneratorFunc* name_func, + const char* file, int line) { + instantiations_.push_back( + InstantiationInfo(instantiation_name, func, name_func, file, line)); + return 0; // Return value used only to run this method in namespace scope. + } + // UnitTest class invokes this method to register tests in this test suite + // right before running tests in RUN_ALL_TESTS macro. + // This method should not be called more than once on any single + // instance of a ParameterizedTestSuiteInfoBase derived class. + // UnitTest has a guard to prevent from calling this method more than once. + void RegisterTests() override { + bool generated_instantiations = false; + + for (typename TestInfoContainer::iterator test_it = tests_.begin(); + test_it != tests_.end(); ++test_it) { + std::shared_ptr test_info = *test_it; + for (typename InstantiationContainer::iterator gen_it = + instantiations_.begin(); + gen_it != instantiations_.end(); ++gen_it) { + const std::string& instantiation_name = gen_it->name; + ParamGenerator generator((*gen_it->generator)()); + ParamNameGeneratorFunc* name_func = gen_it->name_func; + const char* file = gen_it->file; + int line = gen_it->line; + + std::string test_suite_name; + if (!instantiation_name.empty()) + test_suite_name = instantiation_name + "/"; + test_suite_name += test_info->test_suite_base_name; + + size_t i = 0; + std::set test_param_names; + for (typename ParamGenerator::iterator param_it = + generator.begin(); + param_it != generator.end(); ++param_it, ++i) { + generated_instantiations = true; + + Message test_name_stream; + + std::string param_name = + name_func(TestParamInfo(*param_it, i)); + + GTEST_CHECK_(IsValidParamName(param_name)) + << "Parameterized test name '" << param_name + << "' is invalid, in " << file << " line " << line << std::endl; + + GTEST_CHECK_(test_param_names.count(param_name) == 0) + << "Duplicate parameterized test name '" << param_name << "', in " + << file << " line " << line << std::endl; + + test_param_names.insert(param_name); + + if (!test_info->test_base_name.empty()) { + test_name_stream << test_info->test_base_name << "/"; + } + test_name_stream << param_name; + MakeAndRegisterTestInfo( + test_suite_name.c_str(), test_name_stream.GetString().c_str(), + nullptr, // No type parameter. + PrintToString(*param_it).c_str(), test_info->code_location, + GetTestSuiteTypeId(), + SuiteApiResolver::GetSetUpCaseOrSuite(file, line), + SuiteApiResolver::GetTearDownCaseOrSuite(file, line), + test_info->test_meta_factory->CreateTestFactory(*param_it)); + } // for param_it + } // for gen_it + } // for test_it + + if (!generated_instantiations) { + // There are no generaotrs, or they all generate nothing ... + InsertSyntheticTestCase(GetTestSuiteName(), code_location_, + !tests_.empty()); + } + } // RegisterTests + + private: + // LocalTestInfo structure keeps information about a single test registered + // with TEST_P macro. + struct TestInfo { + TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name, + TestMetaFactoryBase* a_test_meta_factory, + CodeLocation a_code_location) + : test_suite_base_name(a_test_suite_base_name), + test_base_name(a_test_base_name), + test_meta_factory(a_test_meta_factory), + code_location(a_code_location) {} + + const std::string test_suite_base_name; + const std::string test_base_name; + const std::unique_ptr> test_meta_factory; + const CodeLocation code_location; + }; + using TestInfoContainer = ::std::vector>; + // Records data received from INSTANTIATE_TEST_SUITE_P macros: + // + struct InstantiationInfo { + InstantiationInfo(const std::string& name_in, + GeneratorCreationFunc* generator_in, + ParamNameGeneratorFunc* name_func_in, const char* file_in, + int line_in) + : name(name_in), + generator(generator_in), + name_func(name_func_in), + file(file_in), + line(line_in) {} + + std::string name; + GeneratorCreationFunc* generator; + ParamNameGeneratorFunc* name_func; + const char* file; + int line; + }; + typedef ::std::vector InstantiationContainer; + + static bool IsValidParamName(const std::string& name) { + // Check for empty string + if (name.empty()) return false; + + // Check for invalid characters + for (std::string::size_type index = 0; index < name.size(); ++index) { + if (!IsAlNum(name[index]) && name[index] != '_') return false; + } + + return true; + } + + const std::string test_suite_name_; + CodeLocation code_location_; + TestInfoContainer tests_; + InstantiationContainer instantiations_; + + ParameterizedTestSuiteInfo(const ParameterizedTestSuiteInfo&) = delete; + ParameterizedTestSuiteInfo& operator=(const ParameterizedTestSuiteInfo&) = + delete; +}; // class ParameterizedTestSuiteInfo + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +template +using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// ParameterizedTestSuiteRegistry contains a map of +// ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P +// and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding +// ParameterizedTestSuiteInfo descriptors. +class ParameterizedTestSuiteRegistry { + public: + ParameterizedTestSuiteRegistry() {} + ~ParameterizedTestSuiteRegistry() { + for (auto& test_suite_info : test_suite_infos_) { + delete test_suite_info; + } + } + + // Looks up or creates and returns a structure containing information about + // tests and instantiations of a particular test suite. + template + ParameterizedTestSuiteInfo* GetTestSuitePatternHolder( + const char* test_suite_name, CodeLocation code_location) { + ParameterizedTestSuiteInfo* typed_test_info = nullptr; + for (auto& test_suite_info : test_suite_infos_) { + if (test_suite_info->GetTestSuiteName() == test_suite_name) { + if (test_suite_info->GetTestSuiteTypeId() != GetTypeId()) { + // Complain about incorrect usage of Google Test facilities + // and terminate the program since we cannot guaranty correct + // test suite setup and tear-down in this case. + ReportInvalidTestSuiteType(test_suite_name, code_location); + posix::Abort(); + } else { + // At this point we are sure that the object we found is of the same + // type we are looking for, so we downcast it to that type + // without further checks. + typed_test_info = CheckedDowncastToActualType< + ParameterizedTestSuiteInfo>(test_suite_info); + } + break; + } + } + if (typed_test_info == nullptr) { + typed_test_info = new ParameterizedTestSuiteInfo( + test_suite_name, code_location); + test_suite_infos_.push_back(typed_test_info); + } + return typed_test_info; + } + void RegisterTests() { + for (auto& test_suite_info : test_suite_infos_) { + test_suite_info->RegisterTests(); + } + } +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + template + ParameterizedTestCaseInfo* GetTestCasePatternHolder( + const char* test_case_name, CodeLocation code_location) { + return GetTestSuitePatternHolder(test_case_name, code_location); + } + +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + private: + using TestSuiteInfoContainer = ::std::vector; + + TestSuiteInfoContainer test_suite_infos_; + + ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) = + delete; + ParameterizedTestSuiteRegistry& operator=( + const ParameterizedTestSuiteRegistry&) = delete; +}; + +// Keep track of what type-parameterized test suite are defined and +// where as well as which are intatiated. This allows susequently +// identifying suits that are defined but never used. +class TypeParameterizedTestSuiteRegistry { + public: + // Add a suite definition + void RegisterTestSuite(const char* test_suite_name, + CodeLocation code_location); + + // Add an instantiation of a suit. + void RegisterInstantiation(const char* test_suite_name); + + // For each suit repored as defined but not reported as instantiation, + // emit a test that reports that fact (configurably, as an error). + void CheckForInstantiations(); + + private: + struct TypeParameterizedTestSuiteInfo { + explicit TypeParameterizedTestSuiteInfo(CodeLocation c) + : code_location(c), instantiated(false) {} + + CodeLocation code_location; + bool instantiated; + }; + + std::map suites_; +}; + +} // namespace internal + +// Forward declarations of ValuesIn(), which is implemented in +// include/gtest/gtest-param-test.h. +template +internal::ParamGenerator ValuesIn( + const Container& container); + +namespace internal { +// Used in the Values() function to provide polymorphic capabilities. + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4100) +#endif + +template +class ValueArray { + public: + explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {} + + template + operator ParamGenerator() const { // NOLINT + return ValuesIn(MakeVector(MakeIndexSequence())); + } + + private: + template + std::vector MakeVector(IndexSequence) const { + return std::vector{static_cast(v_.template Get())...}; + } + + FlatTuple v_; +}; + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +template +class CartesianProductGenerator + : public ParamGeneratorInterface<::std::tuple> { + public: + typedef ::std::tuple ParamType; + + CartesianProductGenerator(const std::tuple...>& g) + : generators_(g) {} + ~CartesianProductGenerator() override {} + + ParamIteratorInterface* Begin() const override { + return new Iterator(this, generators_, false); + } + ParamIteratorInterface* End() const override { + return new Iterator(this, generators_, true); + } + + private: + template + class IteratorImpl; + template + class IteratorImpl> + : public ParamIteratorInterface { + public: + IteratorImpl(const ParamGeneratorInterface* base, + const std::tuple...>& generators, + bool is_end) + : base_(base), + begin_(std::get(generators).begin()...), + end_(std::get(generators).end()...), + current_(is_end ? end_ : begin_) { + ComputeCurrentValue(); + } + ~IteratorImpl() override {} + + const ParamGeneratorInterface* BaseGenerator() const override { + return base_; + } + // Advance should not be called on beyond-of-range iterators + // so no component iterators must be beyond end of range, either. + void Advance() override { + assert(!AtEnd()); + // Advance the last iterator. + ++std::get(current_); + // if that reaches end, propagate that up. + AdvanceIfEnd(); + ComputeCurrentValue(); + } + ParamIteratorInterface* Clone() const override { + return new IteratorImpl(*this); + } + + const ParamType* Current() const override { return current_value_.get(); } + + bool Equals(const ParamIteratorInterface& other) const override { + // Having the same base generator guarantees that the other + // iterator is of the same type and we can downcast. + GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) + << "The program attempted to compare iterators " + << "from different generators." << std::endl; + const IteratorImpl* typed_other = + CheckedDowncastToActualType(&other); + + // We must report iterators equal if they both point beyond their + // respective ranges. That can happen in a variety of fashions, + // so we have to consult AtEnd(). + if (AtEnd() && typed_other->AtEnd()) return true; + + bool same = true; + bool dummy[] = { + (same = same && std::get(current_) == + std::get(typed_other->current_))...}; + (void)dummy; + return same; + } + + private: + template + void AdvanceIfEnd() { + if (std::get(current_) != std::get(end_)) return; + + bool last = ThisI == 0; + if (last) { + // We are done. Nothing else to propagate. + return; + } + + constexpr size_t NextI = ThisI - (ThisI != 0); + std::get(current_) = std::get(begin_); + ++std::get(current_); + AdvanceIfEnd(); + } + + void ComputeCurrentValue() { + if (!AtEnd()) + current_value_ = std::make_shared(*std::get(current_)...); + } + bool AtEnd() const { + bool at_end = false; + bool dummy[] = { + (at_end = at_end || std::get(current_) == std::get(end_))...}; + (void)dummy; + return at_end; + } + + const ParamGeneratorInterface* const base_; + std::tuple::iterator...> begin_; + std::tuple::iterator...> end_; + std::tuple::iterator...> current_; + std::shared_ptr current_value_; + }; + + using Iterator = IteratorImpl::type>; + + std::tuple...> generators_; +}; + +template +class CartesianProductHolder { + public: + CartesianProductHolder(const Gen&... g) : generators_(g...) {} + template + operator ParamGenerator<::std::tuple>() const { + return ParamGenerator<::std::tuple>( + new CartesianProductGenerator(generators_)); + } + + private: + std::tuple generators_; +}; + +} // namespace internal +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ diff --git a/test/gtest/include/gtest/internal/gtest-port-arch.h b/test/gtest/include/gtest/internal/gtest-port-arch.h new file mode 100644 index 0000000000..f025db76ad --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-port-arch.h @@ -0,0 +1,116 @@ +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file defines the GTEST_OS_* macro. +// It is separate from gtest-port.h so that custom/gtest-port.h can include it. + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ + +// Determines the platform on which Google Test is compiled. +#ifdef __CYGWIN__ +#define GTEST_OS_CYGWIN 1 +#elif defined(__MINGW__) || defined(__MINGW32__) || defined(__MINGW64__) +#define GTEST_OS_WINDOWS_MINGW 1 +#define GTEST_OS_WINDOWS 1 +#elif defined _WIN32 +#define GTEST_OS_WINDOWS 1 +#ifdef _WIN32_WCE +#define GTEST_OS_WINDOWS_MOBILE 1 +#elif defined(WINAPI_FAMILY) +#include +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) +#define GTEST_OS_WINDOWS_DESKTOP 1 +#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP) +#define GTEST_OS_WINDOWS_PHONE 1 +#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) +#define GTEST_OS_WINDOWS_RT 1 +#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_TV_TITLE) +#define GTEST_OS_WINDOWS_PHONE 1 +#define GTEST_OS_WINDOWS_TV_TITLE 1 +#else +// WINAPI_FAMILY defined but no known partition matched. +// Default to desktop. +#define GTEST_OS_WINDOWS_DESKTOP 1 +#endif +#else +#define GTEST_OS_WINDOWS_DESKTOP 1 +#endif // _WIN32_WCE +#elif defined __OS2__ +#define GTEST_OS_OS2 1 +#elif defined __APPLE__ +#define GTEST_OS_MAC 1 +#include +#if TARGET_OS_IPHONE +#define GTEST_OS_IOS 1 +#endif +#elif defined __DragonFly__ +#define GTEST_OS_DRAGONFLY 1 +#elif defined __FreeBSD__ +#define GTEST_OS_FREEBSD 1 +#elif defined __Fuchsia__ +#define GTEST_OS_FUCHSIA 1 +#elif defined(__GNU__) +#define GTEST_OS_GNU_HURD 1 +#elif defined(__GLIBC__) && defined(__FreeBSD_kernel__) +#define GTEST_OS_GNU_KFREEBSD 1 +#elif defined __linux__ +#define GTEST_OS_LINUX 1 +#if defined __ANDROID__ +#define GTEST_OS_LINUX_ANDROID 1 +#endif +#elif defined __MVS__ +#define GTEST_OS_ZOS 1 +#elif defined(__sun) && defined(__SVR4) +#define GTEST_OS_SOLARIS 1 +#elif defined(_AIX) +#define GTEST_OS_AIX 1 +#elif defined(__hpux) +#define GTEST_OS_HPUX 1 +#elif defined __native_client__ +#define GTEST_OS_NACL 1 +#elif defined __NetBSD__ +#define GTEST_OS_NETBSD 1 +#elif defined __OpenBSD__ +#define GTEST_OS_OPENBSD 1 +#elif defined __QNX__ +#define GTEST_OS_QNX 1 +#elif defined(__HAIKU__) +#define GTEST_OS_HAIKU 1 +#elif defined ESP8266 +#define GTEST_OS_ESP8266 1 +#elif defined ESP32 +#define GTEST_OS_ESP32 1 +#elif defined(__XTENSA__) +#define GTEST_OS_XTENSA 1 +#endif // __CYGWIN__ + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_ diff --git a/test/gtest/include/gtest/internal/gtest-port.h b/test/gtest/include/gtest/internal/gtest-port.h new file mode 100644 index 0000000000..0003d27658 --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-port.h @@ -0,0 +1,2413 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Low-level types and utilities for porting Google Test to various +// platforms. All macros ending with _ and symbols defined in an +// internal namespace are subject to change without notice. Code +// outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't +// end with _ are part of Google Test's public API and can be used by +// code outside Google Test. +// +// This file is fundamental to Google Test. All other Google Test source +// files are expected to #include this. Therefore, it cannot #include +// any other Google Test header. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ + +// Environment-describing macros +// ----------------------------- +// +// Google Test can be used in many different environments. Macros in +// this section tell Google Test what kind of environment it is being +// used in, such that Google Test can provide environment-specific +// features and implementations. +// +// Google Test tries to automatically detect the properties of its +// environment, so users usually don't need to worry about these +// macros. However, the automatic detection is not perfect. +// Sometimes it's necessary for a user to define some of the following +// macros in the build script to override Google Test's decisions. +// +// If the user doesn't define a macro in the list, Google Test will +// provide a default definition. After this header is #included, all +// macros in this list will be defined to either 1 or 0. +// +// Notes to maintainers: +// - Each macro here is a user-tweakable knob; do not grow the list +// lightly. +// - Use #if to key off these macros. Don't use #ifdef or "#if +// defined(...)", which will not work as these macros are ALWAYS +// defined. +// +// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) +// is/isn't available. +// GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions +// are enabled. +// GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular +// expressions are/aren't available. +// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that +// is/isn't available. +// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't +// enabled. +// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that +// std::wstring does/doesn't work (Google Test can +// be used where std::wstring is unavailable). +// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the +// compiler supports Microsoft's "Structured +// Exception Handling". +// GTEST_HAS_STREAM_REDIRECTION +// - Define it to 1/0 to indicate whether the +// platform supports I/O stream redirection using +// dup() and dup2(). +// GTEST_LINKED_AS_SHARED_LIBRARY +// - Define to 1 when compiling tests that use +// Google Test as a shared library (known as +// DLL on Windows). +// GTEST_CREATE_SHARED_LIBRARY +// - Define to 1 when compiling Google Test itself +// as a shared library. +// GTEST_DEFAULT_DEATH_TEST_STYLE +// - The default value of --gtest_death_test_style. +// The legacy default has been "fast" in the open +// source version since 2008. The recommended value +// is "threadsafe", and can be set in +// custom/gtest-port.h. + +// Platform-indicating macros +// -------------------------- +// +// Macros indicating the platform on which Google Test is being used +// (a macro is defined to 1 if compiled on the given platform; +// otherwise UNDEFINED -- it's never defined to 0.). Google Test +// defines these macros automatically. Code outside Google Test MUST +// NOT define them. +// +// GTEST_OS_AIX - IBM AIX +// GTEST_OS_CYGWIN - Cygwin +// GTEST_OS_DRAGONFLY - DragonFlyBSD +// GTEST_OS_FREEBSD - FreeBSD +// GTEST_OS_FUCHSIA - Fuchsia +// GTEST_OS_GNU_HURD - GNU/Hurd +// GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD +// GTEST_OS_HAIKU - Haiku +// GTEST_OS_HPUX - HP-UX +// GTEST_OS_LINUX - Linux +// GTEST_OS_LINUX_ANDROID - Google Android +// GTEST_OS_MAC - Mac OS X +// GTEST_OS_IOS - iOS +// GTEST_OS_NACL - Google Native Client (NaCl) +// GTEST_OS_NETBSD - NetBSD +// GTEST_OS_OPENBSD - OpenBSD +// GTEST_OS_OS2 - OS/2 +// GTEST_OS_QNX - QNX +// GTEST_OS_SOLARIS - Sun Solaris +// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) +// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop +// GTEST_OS_WINDOWS_MINGW - MinGW +// GTEST_OS_WINDOWS_MOBILE - Windows Mobile +// GTEST_OS_WINDOWS_PHONE - Windows Phone +// GTEST_OS_WINDOWS_RT - Windows Store App/WinRT +// GTEST_OS_ZOS - z/OS +// +// Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the +// most stable support. Since core members of the Google Test project +// don't have access to other platforms, support for them may be less +// stable. If you notice any problems on your platform, please notify +// googletestframework@googlegroups.com (patches for fixing them are +// even more welcome!). +// +// It is possible that none of the GTEST_OS_* macros are defined. + +// Feature-indicating macros +// ------------------------- +// +// Macros indicating which Google Test features are available (a macro +// is defined to 1 if the corresponding feature is supported; +// otherwise UNDEFINED -- it's never defined to 0.). Google Test +// defines these macros automatically. Code outside Google Test MUST +// NOT define them. +// +// These macros are public so that portable tests can be written. +// Such tests typically surround code using a feature with an #if +// which controls that code. For example: +// +// #if GTEST_HAS_DEATH_TEST +// EXPECT_DEATH(DoSomethingDeadly()); +// #endif +// +// GTEST_HAS_DEATH_TEST - death tests +// GTEST_HAS_TYPED_TEST - typed tests +// GTEST_HAS_TYPED_TEST_P - type-parameterized tests +// GTEST_IS_THREADSAFE - Google Test is thread-safe. +// GTEST_USES_RE2 - the RE2 regular expression library is used +// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with +// GTEST_HAS_POSIX_RE (see above) which users can +// define themselves. +// GTEST_USES_SIMPLE_RE - our own simple regex is used; +// the above RE\b(s) are mutually exclusive. + +// Misc public macros +// ------------------ +// +// GTEST_FLAG(flag_name) - references the variable corresponding to +// the given Google Test flag. + +// Internal utilities +// ------------------ +// +// The following macros and utilities are for Google Test's INTERNAL +// use only. Code outside Google Test MUST NOT USE THEM DIRECTLY. +// +// Macros for basic C++ coding: +// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. +// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a +// variable don't have to be used. +// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. +// GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is +// suppressed (constant conditional). +// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 +// is suppressed. +// GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter or +// UniversalPrinter specializations. +// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter +// or +// UniversalPrinter +// specializations. +// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher or +// Matcher +// specializations. +// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter or +// UniversalPrinter +// specializations. +// +// Synchronization: +// Mutex, MutexLock, ThreadLocal, GetThreadCount() +// - synchronization primitives. +// +// Regular expressions: +// RE - a simple regular expression class using +// 1) the RE2 syntax on all platforms when built with RE2 +// and Abseil as dependencies +// 2) the POSIX Extended Regular Expression syntax on +// UNIX-like platforms, +// 3) A reduced regular exception syntax on other platforms, +// including Windows. +// Logging: +// GTEST_LOG_() - logs messages at the specified severity level. +// LogToStderr() - directs all log messages to stderr. +// FlushInfoLog() - flushes informational log messages. +// +// Stdout and stderr capturing: +// CaptureStdout() - starts capturing stdout. +// GetCapturedStdout() - stops capturing stdout and returns the captured +// string. +// CaptureStderr() - starts capturing stderr. +// GetCapturedStderr() - stops capturing stderr and returns the captured +// string. +// +// Integer types: +// TypeWithSize - maps an integer to a int type. +// TimeInMillis - integers of known sizes. +// BiggestInt - the biggest signed integer type. +// +// Command-line utilities: +// GetInjectableArgvs() - returns the command line as a vector of strings. +// +// Environment variable utilities: +// GetEnv() - gets the value of an environment variable. +// BoolFromGTestEnv() - parses a bool environment variable. +// Int32FromGTestEnv() - parses an int32_t environment variable. +// StringFromGTestEnv() - parses a string environment variable. +// +// Deprecation warnings: +// GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as +// deprecated; calling a marked function +// should generate a compiler warning + +#include // for isspace, etc +#include // for ptrdiff_t +#include +#include +#include + +#include +// #include // Guarded by GTEST_IS_THREADSAFE below +#include +#include +#include +#include +#include +#include +// #include // Guarded by GTEST_IS_THREADSAFE below +#include +#include +#include + +#ifndef _WIN32_WCE +#include +#include +#endif // !_WIN32_WCE + +#if defined __APPLE__ +#include +#include +#endif + +#include "gtest/internal/custom/gtest-port.h" +#include "gtest/internal/gtest-port-arch.h" + +#if GTEST_HAS_ABSL +#include "absl/flags/declare.h" +#include "absl/flags/flag.h" +#include "absl/flags/reflection.h" +#endif + +#if !defined(GTEST_DEV_EMAIL_) +#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" +#define GTEST_FLAG_PREFIX_ "gtest_" +#define GTEST_FLAG_PREFIX_DASH_ "gtest-" +#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" +#define GTEST_NAME_ "Google Test" +#define GTEST_PROJECT_URL_ "https://github.com/google/googletest/" +#endif // !defined(GTEST_DEV_EMAIL_) + +#if !defined(GTEST_INIT_GOOGLE_TEST_NAME_) +#define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest" +#endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_) + +// Determines the version of gcc that is used to compile this. +#ifdef __GNUC__ +// 40302 means version 4.3.2. +#define GTEST_GCC_VER_ \ + (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) +#endif // __GNUC__ + +// Macros for disabling Microsoft Visual C++ warnings. +// +// GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385) +// /* code that triggers warnings C4800 and C4385 */ +// GTEST_DISABLE_MSC_WARNINGS_POP_() +#if defined(_MSC_VER) +#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \ + __pragma(warning(push)) __pragma(warning(disable : warnings)) +#define GTEST_DISABLE_MSC_WARNINGS_POP_() __pragma(warning(pop)) +#else +// Not all compilers are MSVC +#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) +#define GTEST_DISABLE_MSC_WARNINGS_POP_() +#endif + +// Clang on Windows does not understand MSVC's pragma warning. +// We need clang-specific way to disable function deprecation warning. +#ifdef __clang__ +#define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \ + _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"") +#define GTEST_DISABLE_MSC_DEPRECATED_POP_() _Pragma("clang diagnostic pop") +#else +#define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996) +#define GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() +#endif + +// Brings in definitions for functions used in the testing::internal::posix +// namespace (read, write, close, chdir, isatty, stat). We do not currently +// use them on Windows Mobile. +#if GTEST_OS_WINDOWS +#if !GTEST_OS_WINDOWS_MOBILE +#include +#include +#endif +// In order to avoid having to include , use forward declaration +#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR) +// MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two +// separate (equivalent) structs, instead of using typedef +typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION; +#else +// Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION. +// This assumption is verified by +// WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION. +typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; +#endif +#elif GTEST_OS_XTENSA +#include +// Xtensa toolchains define strcasecmp in the string.h header instead of +// strings.h. string.h is already included. +#else +// This assumes that non-Windows OSes provide unistd.h. For OSes where this +// is not the case, we need to include headers that provide the functions +// mentioned above. +#include +#include +#endif // GTEST_OS_WINDOWS + +#if GTEST_OS_LINUX_ANDROID +// Used to define __ANDROID_API__ matching the target NDK API level. +#include // NOLINT +#endif + +// Defines this to true if and only if Google Test can use POSIX regular +// expressions. +#ifndef GTEST_HAS_POSIX_RE +#if GTEST_OS_LINUX_ANDROID +// On Android, is only available starting with Gingerbread. +#define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9) +#else +#define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS && !GTEST_OS_XTENSA) +#endif +#endif + +// Select the regular expression implementation. +#if GTEST_HAS_ABSL +// When using Abseil, RE2 is required. +#include "absl/strings/string_view.h" +#include "re2/re2.h" +#define GTEST_USES_RE2 1 +#elif GTEST_HAS_POSIX_RE +#include // NOLINT +#define GTEST_USES_POSIX_RE 1 +#else +// Use our own simple regex implementation. +#define GTEST_USES_SIMPLE_RE 1 +#endif + +#ifndef GTEST_HAS_EXCEPTIONS +// The user didn't tell us whether exceptions are enabled, so we need +// to figure it out. +#if defined(_MSC_VER) && defined(_CPPUNWIND) +// MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled. +#define GTEST_HAS_EXCEPTIONS 1 +#elif defined(__BORLANDC__) +// C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS +// macro to enable exceptions, so we'll do the same. +// Assumes that exceptions are enabled by default. +#ifndef _HAS_EXCEPTIONS +#define _HAS_EXCEPTIONS 1 +#endif // _HAS_EXCEPTIONS +#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS +#elif defined(__clang__) +// clang defines __EXCEPTIONS if and only if exceptions are enabled before clang +// 220714, but if and only if cleanups are enabled after that. In Obj-C++ files, +// there can be cleanups for ObjC exceptions which also need cleanups, even if +// C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which +// checks for C++ exceptions starting at clang r206352, but which checked for +// cleanups prior to that. To reliably check for C++ exception availability with +// clang, check for +// __EXCEPTIONS && __has_feature(cxx_exceptions). +#define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions)) +#elif defined(__GNUC__) && __EXCEPTIONS +// gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled. +#define GTEST_HAS_EXCEPTIONS 1 +#elif defined(__SUNPRO_CC) +// Sun Pro CC supports exceptions. However, there is no compile-time way of +// detecting whether they are enabled or not. Therefore, we assume that +// they are enabled unless the user tells us otherwise. +#define GTEST_HAS_EXCEPTIONS 1 +#elif defined(__IBMCPP__) && __EXCEPTIONS +// xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled. +#define GTEST_HAS_EXCEPTIONS 1 +#elif defined(__HP_aCC) +// Exception handling is in effect by default in HP aCC compiler. It has to +// be turned of by +noeh compiler option if desired. +#define GTEST_HAS_EXCEPTIONS 1 +#else +// For other compilers, we assume exceptions are disabled to be +// conservative. +#define GTEST_HAS_EXCEPTIONS 0 +#endif // defined(_MSC_VER) || defined(__BORLANDC__) +#endif // GTEST_HAS_EXCEPTIONS + +#ifndef GTEST_HAS_STD_WSTRING +// The user didn't tell us whether ::std::wstring is available, so we need +// to figure it out. +// Cygwin 1.7 and below doesn't support ::std::wstring. +// Solaris' libc++ doesn't support it either. Android has +// no support for it at least as recent as Froyo (2.2). +#define GTEST_HAS_STD_WSTRING \ + (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ + GTEST_OS_HAIKU || GTEST_OS_ESP32 || GTEST_OS_ESP8266 || GTEST_OS_XTENSA)) + +#endif // GTEST_HAS_STD_WSTRING + +// Determines whether RTTI is available. +#ifndef GTEST_HAS_RTTI +// The user didn't tell us whether RTTI is enabled, so we need to +// figure it out. + +#ifdef _MSC_VER + +#ifdef _CPPRTTI // MSVC defines this macro if and only if RTTI is enabled. +#define GTEST_HAS_RTTI 1 +#else +#define GTEST_HAS_RTTI 0 +#endif + +// Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is +// enabled. +#elif defined(__GNUC__) + +#ifdef __GXX_RTTI +// When building against STLport with the Android NDK and with +// -frtti -fno-exceptions, the build fails at link time with undefined +// references to __cxa_bad_typeid. Note sure if STL or toolchain bug, +// so disable RTTI when detected. +#if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && !defined(__EXCEPTIONS) +#define GTEST_HAS_RTTI 0 +#else +#define GTEST_HAS_RTTI 1 +#endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS +#else +#define GTEST_HAS_RTTI 0 +#endif // __GXX_RTTI + +// Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends +// using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the +// first version with C++ support. +#elif defined(__clang__) + +#define GTEST_HAS_RTTI __has_feature(cxx_rtti) + +// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if +// both the typeid and dynamic_cast features are present. +#elif defined(__IBMCPP__) && (__IBMCPP__ >= 900) + +#ifdef __RTTI_ALL__ +#define GTEST_HAS_RTTI 1 +#else +#define GTEST_HAS_RTTI 0 +#endif + +#else + +// For all other compilers, we assume RTTI is enabled. +#define GTEST_HAS_RTTI 1 + +#endif // _MSC_VER + +#endif // GTEST_HAS_RTTI + +// It's this header's responsibility to #include when RTTI +// is enabled. +#if GTEST_HAS_RTTI +#include +#endif + +// Determines whether Google Test can use the pthreads library. +#ifndef GTEST_HAS_PTHREAD +// The user didn't tell us explicitly, so we make reasonable assumptions about +// which platforms have pthreads support. +// +// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 +// to your compiler flags. +#define GTEST_HAS_PTHREAD \ + (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \ + GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \ + GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD || \ + GTEST_OS_HAIKU || GTEST_OS_GNU_HURD) +#endif // GTEST_HAS_PTHREAD + +#if GTEST_HAS_PTHREAD +// gtest-port.h guarantees to #include when GTEST_HAS_PTHREAD is +// true. +#include // NOLINT + +// For timespec and nanosleep, used below. +#include // NOLINT +#endif + +// Determines whether clone(2) is supported. +// Usually it will only be available on Linux, excluding +// Linux on the Itanium architecture. +// Also see http://linux.die.net/man/2/clone. +#ifndef GTEST_HAS_CLONE +// The user didn't tell us, so we need to figure it out. + +#if GTEST_OS_LINUX && !defined(__ia64__) +#if GTEST_OS_LINUX_ANDROID +// On Android, clone() became available at different API levels for each 32-bit +// architecture. +#if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \ + (defined(__mips__) && __ANDROID_API__ >= 12) || \ + (defined(__i386__) && __ANDROID_API__ >= 17) +#define GTEST_HAS_CLONE 1 +#else +#define GTEST_HAS_CLONE 0 +#endif +#else +#define GTEST_HAS_CLONE 1 +#endif +#else +#define GTEST_HAS_CLONE 0 +#endif // GTEST_OS_LINUX && !defined(__ia64__) + +#endif // GTEST_HAS_CLONE + +// Determines whether to support stream redirection. This is used to test +// output correctness and to implement death tests. +#ifndef GTEST_HAS_STREAM_REDIRECTION +// By default, we assume that stream redirection is supported on all +// platforms except known mobile ones. +#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ + GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA +#define GTEST_HAS_STREAM_REDIRECTION 0 +#else +#define GTEST_HAS_STREAM_REDIRECTION 1 +#endif // !GTEST_OS_WINDOWS_MOBILE +#endif // GTEST_HAS_STREAM_REDIRECTION + +// Determines whether to support death tests. +// pops up a dialog window that cannot be suppressed programmatically. +#if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ + (GTEST_OS_MAC && !GTEST_OS_IOS) || \ + (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || GTEST_OS_WINDOWS_MINGW || \ + GTEST_OS_AIX || GTEST_OS_HPUX || GTEST_OS_OPENBSD || GTEST_OS_QNX || \ + GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \ + GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU || \ + GTEST_OS_GNU_HURD) +#define GTEST_HAS_DEATH_TEST 1 +#endif + +// Determines whether to support type-driven tests. + +// Typed tests need and variadic macros, which GCC, VC++ 8.0, +// Sun Pro CC, IBM Visual Age, and HP aCC support. +#if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \ + defined(__IBMCPP__) || defined(__HP_aCC) +#define GTEST_HAS_TYPED_TEST 1 +#define GTEST_HAS_TYPED_TEST_P 1 +#endif + +// Determines whether the system compiler uses UTF-16 for encoding wide strings. +#define GTEST_WIDE_STRING_USES_UTF16_ \ + (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_AIX || GTEST_OS_OS2) + +// Determines whether test results can be streamed to a socket. +#if GTEST_OS_LINUX || GTEST_OS_GNU_KFREEBSD || GTEST_OS_DRAGONFLY || \ + GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_OPENBSD || \ + GTEST_OS_GNU_HURD +#define GTEST_CAN_STREAM_RESULTS_ 1 +#endif + +// Defines some utility macros. + +// The GNU compiler emits a warning if nested "if" statements are followed by +// an "else" statement and braces are not used to explicitly disambiguate the +// "else" binding. This leads to problems with code like: +// +// if (gate) +// ASSERT_*(condition) << "Some message"; +// +// The "switch (0) case 0:" idiom is used to suppress this. +#ifdef __INTEL_COMPILER +#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ +#else +#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + switch (0) \ + case 0: \ + default: // NOLINT +#endif + +// Use this annotation at the end of a struct/class definition to +// prevent the compiler from optimizing away instances that are never +// used. This is useful when all interesting logic happens inside the +// c'tor and / or d'tor. Example: +// +// struct Foo { +// Foo() { ... } +// } GTEST_ATTRIBUTE_UNUSED_; +// +// Also use it after a variable or parameter declaration to tell the +// compiler the variable/parameter does not have to be used. +#if defined(__GNUC__) && !defined(COMPILER_ICC) +#define GTEST_ATTRIBUTE_UNUSED_ __attribute__((unused)) +#elif defined(__clang__) +#if __has_attribute(unused) +#define GTEST_ATTRIBUTE_UNUSED_ __attribute__((unused)) +#endif +#endif +#ifndef GTEST_ATTRIBUTE_UNUSED_ +#define GTEST_ATTRIBUTE_UNUSED_ +#endif + +// Use this annotation before a function that takes a printf format string. +#if (defined(__GNUC__) || defined(__clang__)) && !defined(COMPILER_ICC) +#if defined(__MINGW_PRINTF_FORMAT) +// MinGW has two different printf implementations. Ensure the format macro +// matches the selected implementation. See +// https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/. +#define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ + __attribute__(( \ + __format__(__MINGW_PRINTF_FORMAT, string_index, first_to_check))) +#else +#define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ + __attribute__((__format__(__printf__, string_index, first_to_check))) +#endif +#else +#define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) +#endif + +// Tell the compiler to warn about unused return values for functions declared +// with this macro. The macro should be used on function declarations +// following the argument list: +// +// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; +#if defined(__GNUC__) && !defined(COMPILER_ICC) +#define GTEST_MUST_USE_RESULT_ __attribute__((warn_unused_result)) +#else +#define GTEST_MUST_USE_RESULT_ +#endif // __GNUC__ && !COMPILER_ICC + +// MS C++ compiler emits warning when a conditional expression is compile time +// constant. In some contexts this warning is false positive and needs to be +// suppressed. Use the following two macros in such cases: +// +// GTEST_INTENTIONAL_CONST_COND_PUSH_() +// while (true) { +// GTEST_INTENTIONAL_CONST_COND_POP_() +// } +#define GTEST_INTENTIONAL_CONST_COND_PUSH_() \ + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127) +#define GTEST_INTENTIONAL_CONST_COND_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() + +// Determine whether the compiler supports Microsoft's Structured Exception +// Handling. This is supported by several Windows compilers but generally +// does not exist on any other system. +#ifndef GTEST_HAS_SEH +// The user didn't tell us, so we need to figure it out. + +#if defined(_MSC_VER) || defined(__BORLANDC__) +// These two compilers are known to support SEH. +#define GTEST_HAS_SEH 1 +#else +// Assume no SEH. +#define GTEST_HAS_SEH 0 +#endif + +#endif // GTEST_HAS_SEH + +#ifndef GTEST_IS_THREADSAFE + +#define GTEST_IS_THREADSAFE \ + (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \ + (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) || \ + GTEST_HAS_PTHREAD) + +#endif // GTEST_IS_THREADSAFE + +#if GTEST_IS_THREADSAFE +// Some platforms don't support including these threading related headers. +#include // NOLINT +#include // NOLINT +#endif // GTEST_IS_THREADSAFE + +// GTEST_API_ qualifies all symbols that must be exported. The definitions below +// are guarded by #ifndef to give embedders a chance to define GTEST_API_ in +// gtest/internal/custom/gtest-port.h +#ifndef GTEST_API_ + +#ifdef _MSC_VER +#if GTEST_LINKED_AS_SHARED_LIBRARY +#define GTEST_API_ __declspec(dllimport) +#elif GTEST_CREATE_SHARED_LIBRARY +#define GTEST_API_ __declspec(dllexport) +#endif +#elif __GNUC__ >= 4 || defined(__clang__) +#define GTEST_API_ __attribute__((visibility("default"))) +#endif // _MSC_VER + +#endif // GTEST_API_ + +#ifndef GTEST_API_ +#define GTEST_API_ +#endif // GTEST_API_ + +#ifndef GTEST_DEFAULT_DEATH_TEST_STYLE +#define GTEST_DEFAULT_DEATH_TEST_STYLE "fast" +#endif // GTEST_DEFAULT_DEATH_TEST_STYLE + +#ifdef __GNUC__ +// Ask the compiler to never inline a given function. +#define GTEST_NO_INLINE_ __attribute__((noinline)) +#else +#define GTEST_NO_INLINE_ +#endif + +#if defined(__clang__) +// Nested ifs to avoid triggering MSVC warning. +#if __has_attribute(disable_tail_calls) +// Ask the compiler not to perform tail call optimization inside +// the marked function. +#define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls)) +#endif +#elif __GNUC__ +#define GTEST_NO_TAIL_CALL_ \ + __attribute__((optimize("no-optimize-sibling-calls"))) +#else +#define GTEST_NO_TAIL_CALL_ +#endif + +// _LIBCPP_VERSION is defined by the libc++ library from the LLVM project. +#if !defined(GTEST_HAS_CXXABI_H_) +#if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER)) +#define GTEST_HAS_CXXABI_H_ 1 +#else +#define GTEST_HAS_CXXABI_H_ 0 +#endif +#endif + +// A function level attribute to disable checking for use of uninitialized +// memory when built with MemorySanitizer. +#if defined(__clang__) +#if __has_feature(memory_sanitizer) +#define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ __attribute__((no_sanitize_memory)) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ +#endif // __has_feature(memory_sanitizer) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ +#endif // __clang__ + +// A function level attribute to disable AddressSanitizer instrumentation. +#if defined(__clang__) +#if __has_feature(address_sanitizer) +#define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \ + __attribute__((no_sanitize_address)) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ +#endif // __has_feature(address_sanitizer) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ +#endif // __clang__ + +// A function level attribute to disable HWAddressSanitizer instrumentation. +#if defined(__clang__) +#if __has_feature(hwaddress_sanitizer) +#define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \ + __attribute__((no_sanitize("hwaddress"))) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ +#endif // __has_feature(hwaddress_sanitizer) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ +#endif // __clang__ + +// A function level attribute to disable ThreadSanitizer instrumentation. +#if defined(__clang__) +#if __has_feature(thread_sanitizer) +#define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ __attribute__((no_sanitize_thread)) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ +#endif // __has_feature(thread_sanitizer) +#else +#define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ +#endif // __clang__ + +namespace testing { + +class Message; + +// Legacy imports for backwards compatibility. +// New code should use std:: names directly. +using std::get; +using std::make_tuple; +using std::tuple; +using std::tuple_element; +using std::tuple_size; + +namespace internal { + +// A secret type that Google Test users don't know about. It has no +// definition on purpose. Therefore it's impossible to create a +// Secret object, which is what we want. +class Secret; + +// A helper for suppressing warnings on constant condition. It just +// returns 'condition'. +GTEST_API_ bool IsTrue(bool condition); + +// Defines RE. + +#if GTEST_USES_RE2 + +// This is almost `using RE = ::RE2`, except it is copy-constructible, and it +// needs to disambiguate the `std::string`, `absl::string_view`, and `const +// char*` constructors. +class GTEST_API_ RE { + public: + RE(absl::string_view regex) : regex_(regex) {} // NOLINT + RE(const char* regex) : RE(absl::string_view(regex)) {} // NOLINT + RE(const std::string& regex) : RE(absl::string_view(regex)) {} // NOLINT + RE(const RE& other) : RE(other.pattern()) {} + + const std::string& pattern() const { return regex_.pattern(); } + + static bool FullMatch(absl::string_view str, const RE& re) { + return RE2::FullMatch(str, re.regex_); + } + static bool PartialMatch(absl::string_view str, const RE& re) { + return RE2::PartialMatch(str, re.regex_); + } + + private: + RE2 regex_; +}; + +#elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE + +// A simple C++ wrapper for . It uses the POSIX Extended +// Regular Expression syntax. +class GTEST_API_ RE { + public: + // A copy constructor is required by the Standard to initialize object + // references from r-values. + RE(const RE& other) { Init(other.pattern()); } + + // Constructs an RE from a string. + RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT + + RE(const char* regex) { Init(regex); } // NOLINT + ~RE(); + + // Returns the string representation of the regex. + const char* pattern() const { return pattern_; } + + // FullMatch(str, re) returns true if and only if regular expression re + // matches the entire str. + // PartialMatch(str, re) returns true if and only if regular expression re + // matches a substring of str (including str itself). + static bool FullMatch(const ::std::string& str, const RE& re) { + return FullMatch(str.c_str(), re); + } + static bool PartialMatch(const ::std::string& str, const RE& re) { + return PartialMatch(str.c_str(), re); + } + + static bool FullMatch(const char* str, const RE& re); + static bool PartialMatch(const char* str, const RE& re); + + private: + void Init(const char* regex); + const char* pattern_; + bool is_valid_; + +#if GTEST_USES_POSIX_RE + + regex_t full_regex_; // For FullMatch(). + regex_t partial_regex_; // For PartialMatch(). + +#else // GTEST_USES_SIMPLE_RE + + const char* full_pattern_; // For FullMatch(); + +#endif +}; + +#endif // ::testing::internal::RE implementation + +// Formats a source file path and a line number as they would appear +// in an error message from the compiler used to compile this code. +GTEST_API_ ::std::string FormatFileLocation(const char* file, int line); + +// Formats a file location for compiler-independent XML output. +// Although this function is not platform dependent, we put it next to +// FormatFileLocation in order to contrast the two functions. +GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, + int line); + +// Defines logging utilities: +// GTEST_LOG_(severity) - logs messages at the specified severity level. The +// message itself is streamed into the macro. +// LogToStderr() - directs all log messages to stderr. +// FlushInfoLog() - flushes informational log messages. + +enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL }; + +// Formats log entry severity, provides a stream object for streaming the +// log message, and terminates the message with a newline when going out of +// scope. +class GTEST_API_ GTestLog { + public: + GTestLog(GTestLogSeverity severity, const char* file, int line); + + // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. + ~GTestLog(); + + ::std::ostream& GetStream() { return ::std::cerr; } + + private: + const GTestLogSeverity severity_; + + GTestLog(const GTestLog&) = delete; + GTestLog& operator=(const GTestLog&) = delete; +}; + +#if !defined(GTEST_LOG_) + +#define GTEST_LOG_(severity) \ + ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ + __FILE__, __LINE__) \ + .GetStream() + +inline void LogToStderr() {} +inline void FlushInfoLog() { fflush(nullptr); } + +#endif // !defined(GTEST_LOG_) + +#if !defined(GTEST_CHECK_) +// INTERNAL IMPLEMENTATION - DO NOT USE. +// +// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition +// is not satisfied. +// Synopsis: +// GTEST_CHECK_(boolean_condition); +// or +// GTEST_CHECK_(boolean_condition) << "Additional message"; +// +// This checks the condition and if the condition is not satisfied +// it prints message about the condition violation, including the +// condition itself, plus additional message streamed into it, if any, +// and then it aborts the program. It aborts the program irrespective of +// whether it is built in the debug mode or not. +#define GTEST_CHECK_(condition) \ + GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ + if (::testing::internal::IsTrue(condition)) \ + ; \ + else \ + GTEST_LOG_(FATAL) << "Condition " #condition " failed. " +#endif // !defined(GTEST_CHECK_) + +// An all-mode assert to verify that the given POSIX-style function +// call returns 0 (indicating success). Known limitation: this +// doesn't expand to a balanced 'if' statement, so enclose the macro +// in {} if you need to use it as the only statement in an 'if' +// branch. +#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ + if (const int gtest_error = (posix_call)) \ + GTEST_LOG_(FATAL) << #posix_call << "failed with error " << gtest_error + +// Transforms "T" into "const T&" according to standard reference collapsing +// rules (this is only needed as a backport for C++98 compilers that do not +// support reference collapsing). Specifically, it transforms: +// +// char ==> const char& +// const char ==> const char& +// char& ==> char& +// const char& ==> const char& +// +// Note that the non-const reference will not have "const" added. This is +// standard, and necessary so that "T" can always bind to "const T&". +template +struct ConstRef { + typedef const T& type; +}; +template +struct ConstRef { + typedef T& type; +}; + +// The argument T must depend on some template parameters. +#define GTEST_REFERENCE_TO_CONST_(T) \ + typename ::testing::internal::ConstRef::type + +// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. +// +// Use ImplicitCast_ as a safe version of static_cast for upcasting in +// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a +// const Foo*). When you use ImplicitCast_, the compiler checks that +// the cast is safe. Such explicit ImplicitCast_s are necessary in +// surprisingly many situations where C++ demands an exact type match +// instead of an argument type convertible to a target type. +// +// The syntax for using ImplicitCast_ is the same as for static_cast: +// +// ImplicitCast_(expr) +// +// ImplicitCast_ would have been part of the C++ standard library, +// but the proposal was submitted too late. It will probably make +// its way into the language in the future. +// +// This relatively ugly name is intentional. It prevents clashes with +// similar functions users may have (e.g., implicit_cast). The internal +// namespace alone is not enough because the function can be found by ADL. +template +inline To ImplicitCast_(To x) { + return x; +} + +// When you upcast (that is, cast a pointer from type Foo to type +// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts +// always succeed. When you downcast (that is, cast a pointer from +// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because +// how do you know the pointer is really of type SubclassOfFoo? It +// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, +// when you downcast, you should use this macro. In debug mode, we +// use dynamic_cast<> to double-check the downcast is legal (we die +// if it's not). In normal mode, we do the efficient static_cast<> +// instead. Thus, it's important to test in debug mode to make sure +// the cast is legal! +// This is the only place in the code we should use dynamic_cast<>. +// In particular, you SHOULDN'T be using dynamic_cast<> in order to +// do RTTI (eg code like this: +// if (dynamic_cast(foo)) HandleASubclass1Object(foo); +// if (dynamic_cast(foo)) HandleASubclass2Object(foo); +// You should design the code some other way not to need this. +// +// This relatively ugly name is intentional. It prevents clashes with +// similar functions users may have (e.g., down_cast). The internal +// namespace alone is not enough because the function can be found by ADL. +template // use like this: DownCast_(foo); +inline To DownCast_(From* f) { // so we only accept pointers + // Ensures that To is a sub-type of From *. This test is here only + // for compile-time type checking, and has no overhead in an + // optimized build at run-time, as it will be optimized away + // completely. + GTEST_INTENTIONAL_CONST_COND_PUSH_() + if (false) { + GTEST_INTENTIONAL_CONST_COND_POP_() + const To to = nullptr; + ::testing::internal::ImplicitCast_(to); + } + +#if GTEST_HAS_RTTI + // RTTI: debug mode only! + GTEST_CHECK_(f == nullptr || dynamic_cast(f) != nullptr); +#endif + return static_cast(f); +} + +// Downcasts the pointer of type Base to Derived. +// Derived must be a subclass of Base. The parameter MUST +// point to a class of type Derived, not any subclass of it. +// When RTTI is available, the function performs a runtime +// check to enforce this. +template +Derived* CheckedDowncastToActualType(Base* base) { +#if GTEST_HAS_RTTI + GTEST_CHECK_(typeid(*base) == typeid(Derived)); +#endif + +#if GTEST_HAS_DOWNCAST_ + return ::down_cast(base); +#elif GTEST_HAS_RTTI + return dynamic_cast(base); // NOLINT +#else + return static_cast(base); // Poor man's downcast. +#endif +} + +#if GTEST_HAS_STREAM_REDIRECTION + +// Defines the stderr capturer: +// CaptureStdout - starts capturing stdout. +// GetCapturedStdout - stops capturing stdout and returns the captured string. +// CaptureStderr - starts capturing stderr. +// GetCapturedStderr - stops capturing stderr and returns the captured string. +// +GTEST_API_ void CaptureStdout(); +GTEST_API_ std::string GetCapturedStdout(); +GTEST_API_ void CaptureStderr(); +GTEST_API_ std::string GetCapturedStderr(); + +#endif // GTEST_HAS_STREAM_REDIRECTION +// Returns the size (in bytes) of a file. +GTEST_API_ size_t GetFileSize(FILE* file); + +// Reads the entire content of a file as a string. +GTEST_API_ std::string ReadEntireFile(FILE* file); + +// All command line arguments. +GTEST_API_ std::vector GetArgvs(); + +#if GTEST_HAS_DEATH_TEST + +std::vector GetInjectableArgvs(); +// Deprecated: pass the args vector by value instead. +void SetInjectableArgvs(const std::vector* new_argvs); +void SetInjectableArgvs(const std::vector& new_argvs); +void ClearInjectableArgvs(); + +#endif // GTEST_HAS_DEATH_TEST + +// Defines synchronization primitives. +#if GTEST_IS_THREADSAFE + +#if GTEST_OS_WINDOWS +// Provides leak-safe Windows kernel handle ownership. +// Used in death tests and in threading support. +class GTEST_API_ AutoHandle { + public: + // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to + // avoid including in this header file. Including is + // undesirable because it defines a lot of symbols and macros that tend to + // conflict with client code. This assumption is verified by + // WindowsTypesTest.HANDLEIsVoidStar. + typedef void* Handle; + AutoHandle(); + explicit AutoHandle(Handle handle); + + ~AutoHandle(); + + Handle Get() const; + void Reset(); + void Reset(Handle handle); + + private: + // Returns true if and only if the handle is a valid handle object that can be + // closed. + bool IsCloseable() const; + + Handle handle_; + + AutoHandle(const AutoHandle&) = delete; + AutoHandle& operator=(const AutoHandle&) = delete; +}; +#endif + +#if GTEST_HAS_NOTIFICATION_ +// Notification has already been imported into the namespace. +// Nothing to do here. + +#else +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +// Allows a controller thread to pause execution of newly created +// threads until notified. Instances of this class must be created +// and destroyed in the controller thread. +// +// This class is only for testing Google Test's own constructs. Do not +// use it in user tests, either directly or indirectly. +// TODO(b/203539622): Replace unconditionally with absl::Notification. +class GTEST_API_ Notification { + public: + Notification() : notified_(false) {} + Notification(const Notification&) = delete; + Notification& operator=(const Notification&) = delete; + + // Notifies all threads created with this notification to start. Must + // be called from the controller thread. + void Notify() { + std::lock_guard lock(mu_); + notified_ = true; + cv_.notify_all(); + } + + // Blocks until the controller thread notifies. Must be called from a test + // thread. + void WaitForNotification() { + std::unique_lock lock(mu_); + cv_.wait(lock, [this]() { return notified_; }); + } + + private: + std::mutex mu_; + std::condition_variable cv_; + bool notified_; +}; +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 +#endif // GTEST_HAS_NOTIFICATION_ + +// On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD +// defined, but we don't want to use MinGW's pthreads implementation, which +// has conformance problems with some versions of the POSIX standard. +#if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW + +// As a C-function, ThreadFuncWithCLinkage cannot be templated itself. +// Consequently, it cannot select a correct instantiation of ThreadWithParam +// in order to call its Run(). Introducing ThreadWithParamBase as a +// non-templated base class for ThreadWithParam allows us to bypass this +// problem. +class ThreadWithParamBase { + public: + virtual ~ThreadWithParamBase() {} + virtual void Run() = 0; +}; + +// pthread_create() accepts a pointer to a function type with the C linkage. +// According to the Standard (7.5/1), function types with different linkages +// are different even if they are otherwise identical. Some compilers (for +// example, SunStudio) treat them as different types. Since class methods +// cannot be defined with C-linkage we need to define a free C-function to +// pass into pthread_create(). +extern "C" inline void* ThreadFuncWithCLinkage(void* thread) { + static_cast(thread)->Run(); + return nullptr; +} + +// Helper class for testing Google Test's multi-threading constructs. +// To use it, write: +// +// void ThreadFunc(int param) { /* Do things with param */ } +// Notification thread_can_start; +// ... +// // The thread_can_start parameter is optional; you can supply NULL. +// ThreadWithParam thread(&ThreadFunc, 5, &thread_can_start); +// thread_can_start.Notify(); +// +// These classes are only for testing Google Test's own constructs. Do +// not use them in user tests, either directly or indirectly. +template +class ThreadWithParam : public ThreadWithParamBase { + public: + typedef void UserThreadFunc(T); + + ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) + : func_(func), + param_(param), + thread_can_start_(thread_can_start), + finished_(false) { + ThreadWithParamBase* const base = this; + // The thread can be created only after all fields except thread_ + // have been initialized. + GTEST_CHECK_POSIX_SUCCESS_( + pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base)); + } + ~ThreadWithParam() override { Join(); } + + void Join() { + if (!finished_) { + GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr)); + finished_ = true; + } + } + + void Run() override { + if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification(); + func_(param_); + } + + private: + UserThreadFunc* const func_; // User-supplied thread function. + const T param_; // User-supplied parameter to the thread function. + // When non-NULL, used to block execution until the controller thread + // notifies. + Notification* const thread_can_start_; + bool finished_; // true if and only if we know that the thread function has + // finished. + pthread_t thread_; // The native thread object. + + ThreadWithParam(const ThreadWithParam&) = delete; + ThreadWithParam& operator=(const ThreadWithParam&) = delete; +}; +#endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD || + // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ + +#if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ +// Mutex and ThreadLocal have already been imported into the namespace. +// Nothing to do here. + +#elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT + +// Mutex implements mutex on Windows platforms. It is used in conjunction +// with class MutexLock: +// +// Mutex mutex; +// ... +// MutexLock lock(&mutex); // Acquires the mutex and releases it at the +// // end of the current scope. +// +// A static Mutex *must* be defined or declared using one of the following +// macros: +// GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); +// GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); +// +// (A non-static Mutex is defined/declared in the usual way). +class GTEST_API_ Mutex { + public: + enum MutexType { kStatic = 0, kDynamic = 1 }; + // We rely on kStaticMutex being 0 as it is to what the linker initializes + // type_ in static mutexes. critical_section_ will be initialized lazily + // in ThreadSafeLazyInit(). + enum StaticConstructorSelector { kStaticMutex = 0 }; + + // This constructor intentionally does nothing. It relies on type_ being + // statically initialized to 0 (effectively setting it to kStatic) and on + // ThreadSafeLazyInit() to lazily initialize the rest of the members. + explicit Mutex(StaticConstructorSelector /*dummy*/) {} + + Mutex(); + ~Mutex(); + + void Lock(); + + void Unlock(); + + // Does nothing if the current thread holds the mutex. Otherwise, crashes + // with high probability. + void AssertHeld(); + + private: + // Initializes owner_thread_id_ and critical_section_ in static mutexes. + void ThreadSafeLazyInit(); + + // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503, + // we assume that 0 is an invalid value for thread IDs. + unsigned int owner_thread_id_; + + // For static mutexes, we rely on these members being initialized to zeros + // by the linker. + MutexType type_; + long critical_section_init_phase_; // NOLINT + GTEST_CRITICAL_SECTION* critical_section_; + + Mutex(const Mutex&) = delete; + Mutex& operator=(const Mutex&) = delete; +}; + +#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ + extern ::testing::internal::Mutex mutex + +#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ + ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex) + +// We cannot name this class MutexLock because the ctor declaration would +// conflict with a macro named MutexLock, which is defined on some +// platforms. That macro is used as a defensive measure to prevent against +// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than +// "MutexLock l(&mu)". Hence the typedef trick below. +class GTestMutexLock { + public: + explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } + + ~GTestMutexLock() { mutex_->Unlock(); } + + private: + Mutex* const mutex_; + + GTestMutexLock(const GTestMutexLock&) = delete; + GTestMutexLock& operator=(const GTestMutexLock&) = delete; +}; + +typedef GTestMutexLock MutexLock; + +// Base class for ValueHolder. Allows a caller to hold and delete a value +// without knowing its type. +class ThreadLocalValueHolderBase { + public: + virtual ~ThreadLocalValueHolderBase() {} +}; + +// Provides a way for a thread to send notifications to a ThreadLocal +// regardless of its parameter type. +class ThreadLocalBase { + public: + // Creates a new ValueHolder object holding a default value passed to + // this ThreadLocal's constructor and returns it. It is the caller's + // responsibility not to call this when the ThreadLocal instance already + // has a value on the current thread. + virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0; + + protected: + ThreadLocalBase() {} + virtual ~ThreadLocalBase() {} + + private: + ThreadLocalBase(const ThreadLocalBase&) = delete; + ThreadLocalBase& operator=(const ThreadLocalBase&) = delete; +}; + +// Maps a thread to a set of ThreadLocals that have values instantiated on that +// thread and notifies them when the thread exits. A ThreadLocal instance is +// expected to persist until all threads it has values on have terminated. +class GTEST_API_ ThreadLocalRegistry { + public: + // Registers thread_local_instance as having value on the current thread. + // Returns a value that can be used to identify the thread from other threads. + static ThreadLocalValueHolderBase* GetValueOnCurrentThread( + const ThreadLocalBase* thread_local_instance); + + // Invoked when a ThreadLocal instance is destroyed. + static void OnThreadLocalDestroyed( + const ThreadLocalBase* thread_local_instance); +}; + +class GTEST_API_ ThreadWithParamBase { + public: + void Join(); + + protected: + class Runnable { + public: + virtual ~Runnable() {} + virtual void Run() = 0; + }; + + ThreadWithParamBase(Runnable* runnable, Notification* thread_can_start); + virtual ~ThreadWithParamBase(); + + private: + AutoHandle thread_; +}; + +// Helper class for testing Google Test's multi-threading constructs. +template +class ThreadWithParam : public ThreadWithParamBase { + public: + typedef void UserThreadFunc(T); + + ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) + : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {} + virtual ~ThreadWithParam() {} + + private: + class RunnableImpl : public Runnable { + public: + RunnableImpl(UserThreadFunc* func, T param) : func_(func), param_(param) {} + virtual ~RunnableImpl() {} + virtual void Run() { func_(param_); } + + private: + UserThreadFunc* const func_; + const T param_; + + RunnableImpl(const RunnableImpl&) = delete; + RunnableImpl& operator=(const RunnableImpl&) = delete; + }; + + ThreadWithParam(const ThreadWithParam&) = delete; + ThreadWithParam& operator=(const ThreadWithParam&) = delete; +}; + +// Implements thread-local storage on Windows systems. +// +// // Thread 1 +// ThreadLocal tl(100); // 100 is the default value for each thread. +// +// // Thread 2 +// tl.set(150); // Changes the value for thread 2 only. +// EXPECT_EQ(150, tl.get()); +// +// // Thread 1 +// EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. +// tl.set(200); +// EXPECT_EQ(200, tl.get()); +// +// The template type argument T must have a public copy constructor. +// In addition, the default ThreadLocal constructor requires T to have +// a public default constructor. +// +// The users of a TheadLocal instance have to make sure that all but one +// threads (including the main one) using that instance have exited before +// destroying it. Otherwise, the per-thread objects managed for them by the +// ThreadLocal instance are not guaranteed to be destroyed on all platforms. +// +// Google Test only uses global ThreadLocal objects. That means they +// will die after main() has returned. Therefore, no per-thread +// object managed by Google Test will be leaked as long as all threads +// using Google Test have exited when main() returns. +template +class ThreadLocal : public ThreadLocalBase { + public: + ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {} + explicit ThreadLocal(const T& value) + : default_factory_(new InstanceValueHolderFactory(value)) {} + + ~ThreadLocal() override { ThreadLocalRegistry::OnThreadLocalDestroyed(this); } + + T* pointer() { return GetOrCreateValue(); } + const T* pointer() const { return GetOrCreateValue(); } + const T& get() const { return *pointer(); } + void set(const T& value) { *pointer() = value; } + + private: + // Holds a value of T. Can be deleted via its base class without the caller + // knowing the type of T. + class ValueHolder : public ThreadLocalValueHolderBase { + public: + ValueHolder() : value_() {} + explicit ValueHolder(const T& value) : value_(value) {} + + T* pointer() { return &value_; } + + private: + T value_; + ValueHolder(const ValueHolder&) = delete; + ValueHolder& operator=(const ValueHolder&) = delete; + }; + + T* GetOrCreateValue() const { + return static_cast( + ThreadLocalRegistry::GetValueOnCurrentThread(this)) + ->pointer(); + } + + ThreadLocalValueHolderBase* NewValueForCurrentThread() const override { + return default_factory_->MakeNewHolder(); + } + + class ValueHolderFactory { + public: + ValueHolderFactory() {} + virtual ~ValueHolderFactory() {} + virtual ValueHolder* MakeNewHolder() const = 0; + + private: + ValueHolderFactory(const ValueHolderFactory&) = delete; + ValueHolderFactory& operator=(const ValueHolderFactory&) = delete; + }; + + class DefaultValueHolderFactory : public ValueHolderFactory { + public: + DefaultValueHolderFactory() {} + ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } + + private: + DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete; + DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) = + delete; + }; + + class InstanceValueHolderFactory : public ValueHolderFactory { + public: + explicit InstanceValueHolderFactory(const T& value) : value_(value) {} + ValueHolder* MakeNewHolder() const override { + return new ValueHolder(value_); + } + + private: + const T value_; // The value for each thread. + + InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete; + InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) = + delete; + }; + + std::unique_ptr default_factory_; + + ThreadLocal(const ThreadLocal&) = delete; + ThreadLocal& operator=(const ThreadLocal&) = delete; +}; + +#elif GTEST_HAS_PTHREAD + +// MutexBase and Mutex implement mutex on pthreads-based platforms. +class MutexBase { + public: + // Acquires this mutex. + void Lock() { + GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); + owner_ = pthread_self(); + has_owner_ = true; + } + + // Releases this mutex. + void Unlock() { + // Since the lock is being released the owner_ field should no longer be + // considered valid. We don't protect writing to has_owner_ here, as it's + // the caller's responsibility to ensure that the current thread holds the + // mutex when this is called. + has_owner_ = false; + GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); + } + + // Does nothing if the current thread holds the mutex. Otherwise, crashes + // with high probability. + void AssertHeld() const { + GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self())) + << "The current thread is not holding the mutex @" << this; + } + + // A static mutex may be used before main() is entered. It may even + // be used before the dynamic initialization stage. Therefore we + // must be able to initialize a static mutex object at link time. + // This means MutexBase has to be a POD and its member variables + // have to be public. + public: + pthread_mutex_t mutex_; // The underlying pthread mutex. + // has_owner_ indicates whether the owner_ field below contains a valid thread + // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All + // accesses to the owner_ field should be protected by a check of this field. + // An alternative might be to memset() owner_ to all zeros, but there's no + // guarantee that a zero'd pthread_t is necessarily invalid or even different + // from pthread_self(). + bool has_owner_; + pthread_t owner_; // The thread holding the mutex. +}; + +// Forward-declares a static mutex. +#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ + extern ::testing::internal::MutexBase mutex + +// Defines and statically (i.e. at link time) initializes a static mutex. +// The initialization list here does not explicitly initialize each field, +// instead relying on default initialization for the unspecified fields. In +// particular, the owner_ field (a pthread_t) is not explicitly initialized. +// This allows initialization to work whether pthread_t is a scalar or struct. +// The flag -Wmissing-field-initializers must not be specified for this to work. +#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ + ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} + +// The Mutex class can only be used for mutexes created at runtime. It +// shares its API with MutexBase otherwise. +class Mutex : public MutexBase { + public: + Mutex() { + GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); + has_owner_ = false; + } + ~Mutex() { GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); } + + private: + Mutex(const Mutex&) = delete; + Mutex& operator=(const Mutex&) = delete; +}; + +// We cannot name this class MutexLock because the ctor declaration would +// conflict with a macro named MutexLock, which is defined on some +// platforms. That macro is used as a defensive measure to prevent against +// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than +// "MutexLock l(&mu)". Hence the typedef trick below. +class GTestMutexLock { + public: + explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); } + + ~GTestMutexLock() { mutex_->Unlock(); } + + private: + MutexBase* const mutex_; + + GTestMutexLock(const GTestMutexLock&) = delete; + GTestMutexLock& operator=(const GTestMutexLock&) = delete; +}; + +typedef GTestMutexLock MutexLock; + +// Helpers for ThreadLocal. + +// pthread_key_create() requires DeleteThreadLocalValue() to have +// C-linkage. Therefore it cannot be templatized to access +// ThreadLocal. Hence the need for class +// ThreadLocalValueHolderBase. +class ThreadLocalValueHolderBase { + public: + virtual ~ThreadLocalValueHolderBase() {} +}; + +// Called by pthread to delete thread-local data stored by +// pthread_setspecific(). +extern "C" inline void DeleteThreadLocalValue(void* value_holder) { + delete static_cast(value_holder); +} + +// Implements thread-local storage on pthreads-based systems. +template +class GTEST_API_ ThreadLocal { + public: + ThreadLocal() + : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {} + explicit ThreadLocal(const T& value) + : key_(CreateKey()), + default_factory_(new InstanceValueHolderFactory(value)) {} + + ~ThreadLocal() { + // Destroys the managed object for the current thread, if any. + DeleteThreadLocalValue(pthread_getspecific(key_)); + + // Releases resources associated with the key. This will *not* + // delete managed objects for other threads. + GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); + } + + T* pointer() { return GetOrCreateValue(); } + const T* pointer() const { return GetOrCreateValue(); } + const T& get() const { return *pointer(); } + void set(const T& value) { *pointer() = value; } + + private: + // Holds a value of type T. + class ValueHolder : public ThreadLocalValueHolderBase { + public: + ValueHolder() : value_() {} + explicit ValueHolder(const T& value) : value_(value) {} + + T* pointer() { return &value_; } + + private: + T value_; + ValueHolder(const ValueHolder&) = delete; + ValueHolder& operator=(const ValueHolder&) = delete; + }; + + static pthread_key_t CreateKey() { + pthread_key_t key; + // When a thread exits, DeleteThreadLocalValue() will be called on + // the object managed for that thread. + GTEST_CHECK_POSIX_SUCCESS_( + pthread_key_create(&key, &DeleteThreadLocalValue)); + return key; + } + + T* GetOrCreateValue() const { + ThreadLocalValueHolderBase* const holder = + static_cast(pthread_getspecific(key_)); + if (holder != nullptr) { + return CheckedDowncastToActualType(holder)->pointer(); + } + + ValueHolder* const new_holder = default_factory_->MakeNewHolder(); + ThreadLocalValueHolderBase* const holder_base = new_holder; + GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base)); + return new_holder->pointer(); + } + + class ValueHolderFactory { + public: + ValueHolderFactory() {} + virtual ~ValueHolderFactory() {} + virtual ValueHolder* MakeNewHolder() const = 0; + + private: + ValueHolderFactory(const ValueHolderFactory&) = delete; + ValueHolderFactory& operator=(const ValueHolderFactory&) = delete; + }; + + class DefaultValueHolderFactory : public ValueHolderFactory { + public: + DefaultValueHolderFactory() {} + ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } + + private: + DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete; + DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) = + delete; + }; + + class InstanceValueHolderFactory : public ValueHolderFactory { + public: + explicit InstanceValueHolderFactory(const T& value) : value_(value) {} + ValueHolder* MakeNewHolder() const override { + return new ValueHolder(value_); + } + + private: + const T value_; // The value for each thread. + + InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete; + InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) = + delete; + }; + + // A key pthreads uses for looking up per-thread values. + const pthread_key_t key_; + std::unique_ptr default_factory_; + + ThreadLocal(const ThreadLocal&) = delete; + ThreadLocal& operator=(const ThreadLocal&) = delete; +}; + +#endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ + +#else // GTEST_IS_THREADSAFE + +// A dummy implementation of synchronization primitives (mutex, lock, +// and thread-local variable). Necessary for compiling Google Test where +// mutex is not supported - using Google Test in multiple threads is not +// supported on such platforms. + +class Mutex { + public: + Mutex() {} + void Lock() {} + void Unlock() {} + void AssertHeld() const {} +}; + +#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ + extern ::testing::internal::Mutex mutex + +#define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex + +// We cannot name this class MutexLock because the ctor declaration would +// conflict with a macro named MutexLock, which is defined on some +// platforms. That macro is used as a defensive measure to prevent against +// inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than +// "MutexLock l(&mu)". Hence the typedef trick below. +class GTestMutexLock { + public: + explicit GTestMutexLock(Mutex*) {} // NOLINT +}; + +typedef GTestMutexLock MutexLock; + +template +class GTEST_API_ ThreadLocal { + public: + ThreadLocal() : value_() {} + explicit ThreadLocal(const T& value) : value_(value) {} + T* pointer() { return &value_; } + const T* pointer() const { return &value_; } + const T& get() const { return value_; } + void set(const T& value) { value_ = value; } + + private: + T value_; +}; + +#endif // GTEST_IS_THREADSAFE + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +GTEST_API_ size_t GetThreadCount(); + +#if GTEST_OS_WINDOWS +#define GTEST_PATH_SEP_ "\\" +#define GTEST_HAS_ALT_PATH_SEP_ 1 +#else +#define GTEST_PATH_SEP_ "/" +#define GTEST_HAS_ALT_PATH_SEP_ 0 +#endif // GTEST_OS_WINDOWS + +// Utilities for char. + +// isspace(int ch) and friends accept an unsigned char or EOF. char +// may be signed, depending on the compiler (or compiler flags). +// Therefore we need to cast a char to unsigned char before calling +// isspace(), etc. + +inline bool IsAlpha(char ch) { + return isalpha(static_cast(ch)) != 0; +} +inline bool IsAlNum(char ch) { + return isalnum(static_cast(ch)) != 0; +} +inline bool IsDigit(char ch) { + return isdigit(static_cast(ch)) != 0; +} +inline bool IsLower(char ch) { + return islower(static_cast(ch)) != 0; +} +inline bool IsSpace(char ch) { + return isspace(static_cast(ch)) != 0; +} +inline bool IsUpper(char ch) { + return isupper(static_cast(ch)) != 0; +} +inline bool IsXDigit(char ch) { + return isxdigit(static_cast(ch)) != 0; +} +#ifdef __cpp_char8_t +inline bool IsXDigit(char8_t ch) { + return isxdigit(static_cast(ch)) != 0; +} +#endif +inline bool IsXDigit(char16_t ch) { + const unsigned char low_byte = static_cast(ch); + return ch == low_byte && isxdigit(low_byte) != 0; +} +inline bool IsXDigit(char32_t ch) { + const unsigned char low_byte = static_cast(ch); + return ch == low_byte && isxdigit(low_byte) != 0; +} +inline bool IsXDigit(wchar_t ch) { + const unsigned char low_byte = static_cast(ch); + return ch == low_byte && isxdigit(low_byte) != 0; +} + +inline char ToLower(char ch) { + return static_cast(tolower(static_cast(ch))); +} +inline char ToUpper(char ch) { + return static_cast(toupper(static_cast(ch))); +} + +inline std::string StripTrailingSpaces(std::string str) { + std::string::iterator it = str.end(); + while (it != str.begin() && IsSpace(*--it)) it = str.erase(it); + return str; +} + +// The testing::internal::posix namespace holds wrappers for common +// POSIX functions. These wrappers hide the differences between +// Windows/MSVC and POSIX systems. Since some compilers define these +// standard functions as macros, the wrapper cannot have the same name +// as the wrapped function. + +namespace posix { + +// Functions with a different name on Windows. + +#if GTEST_OS_WINDOWS + +typedef struct _stat StatStruct; + +#ifdef __BORLANDC__ +inline int DoIsATTY(int fd) { return isatty(fd); } +inline int StrCaseCmp(const char* s1, const char* s2) { + return stricmp(s1, s2); +} +inline char* StrDup(const char* src) { return strdup(src); } +#else // !__BORLANDC__ +#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_ZOS || GTEST_OS_IOS || \ + GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT || defined(ESP_PLATFORM) +inline int DoIsATTY(int /* fd */) { return 0; } +#else +inline int DoIsATTY(int fd) { return _isatty(fd); } +#endif // GTEST_OS_WINDOWS_MOBILE +inline int StrCaseCmp(const char* s1, const char* s2) { + return _stricmp(s1, s2); +} +inline char* StrDup(const char* src) { return _strdup(src); } +#endif // __BORLANDC__ + +#if GTEST_OS_WINDOWS_MOBILE +inline int FileNo(FILE* file) { return reinterpret_cast(_fileno(file)); } +// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this +// time and thus not defined there. +#else +inline int FileNo(FILE* file) { return _fileno(file); } +inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } +inline int RmDir(const char* dir) { return _rmdir(dir); } +inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; } +#endif // GTEST_OS_WINDOWS_MOBILE + +#elif GTEST_OS_ESP8266 +typedef struct stat StatStruct; + +inline int FileNo(FILE* file) { return fileno(file); } +inline int DoIsATTY(int fd) { return isatty(fd); } +inline int Stat(const char* path, StatStruct* buf) { + // stat function not implemented on ESP8266 + return 0; +} +inline int StrCaseCmp(const char* s1, const char* s2) { + return strcasecmp(s1, s2); +} +inline char* StrDup(const char* src) { return strdup(src); } +inline int RmDir(const char* dir) { return rmdir(dir); } +inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } + +#else + +typedef struct stat StatStruct; + +inline int FileNo(FILE* file) { return fileno(file); } +inline int DoIsATTY(int fd) { return isatty(fd); } +inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } +inline int StrCaseCmp(const char* s1, const char* s2) { + return strcasecmp(s1, s2); +} +inline char* StrDup(const char* src) { return strdup(src); } +inline int RmDir(const char* dir) { return rmdir(dir); } +inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } + +#endif // GTEST_OS_WINDOWS + +inline int IsATTY(int fd) { + // DoIsATTY might change errno (for example ENOTTY in case you redirect stdout + // to a file on Linux), which is unexpected, so save the previous value, and + // restore it after the call. + int savedErrno = errno; + int isAttyValue = DoIsATTY(fd); + errno = savedErrno; + + return isAttyValue; +} + +// Functions deprecated by MSVC 8.0. + +GTEST_DISABLE_MSC_DEPRECATED_PUSH_() + +// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and +// StrError() aren't needed on Windows CE at this time and thus not +// defined there. + +#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \ + !GTEST_OS_WINDOWS_RT && !GTEST_OS_ESP8266 && !GTEST_OS_XTENSA +inline int ChDir(const char* dir) { return chdir(dir); } +#endif +inline FILE* FOpen(const char* path, const char* mode) { +#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW + struct wchar_codecvt : public std::codecvt {}; + std::wstring_convert converter; + std::wstring wide_path = converter.from_bytes(path); + std::wstring wide_mode = converter.from_bytes(mode); + return _wfopen(wide_path.c_str(), wide_mode.c_str()); +#else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW + return fopen(path, mode); +#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW +} +#if !GTEST_OS_WINDOWS_MOBILE +inline FILE* FReopen(const char* path, const char* mode, FILE* stream) { + return freopen(path, mode, stream); +} +inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } +#endif +inline int FClose(FILE* fp) { return fclose(fp); } +#if !GTEST_OS_WINDOWS_MOBILE +inline int Read(int fd, void* buf, unsigned int count) { + return static_cast(read(fd, buf, count)); +} +inline int Write(int fd, const void* buf, unsigned int count) { + return static_cast(write(fd, buf, count)); +} +inline int Close(int fd) { return close(fd); } +inline const char* StrError(int errnum) { return strerror(errnum); } +#endif +inline const char* GetEnv(const char* name) { +#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ + GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_XTENSA + // We are on an embedded platform, which has no environment variables. + static_cast(name); // To prevent 'unused argument' warning. + return nullptr; +#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) + // Environment variables which we programmatically clear will be set to the + // empty string rather than unset (NULL). Handle that case. + const char* const env = getenv(name); + return (env != nullptr && env[0] != '\0') ? env : nullptr; +#else + return getenv(name); +#endif +} + +GTEST_DISABLE_MSC_DEPRECATED_POP_() + +#if GTEST_OS_WINDOWS_MOBILE +// Windows CE has no C library. The abort() function is used in +// several places in Google Test. This implementation provides a reasonable +// imitation of standard behaviour. +[[noreturn]] void Abort(); +#else +[[noreturn]] inline void Abort() { abort(); } +#endif // GTEST_OS_WINDOWS_MOBILE + +} // namespace posix + +// MSVC "deprecates" snprintf and issues warnings wherever it is used. In +// order to avoid these warnings, we need to use _snprintf or _snprintf_s on +// MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate +// function in order to achieve that. We use macro definition here because +// snprintf is a variadic function. +#if _MSC_VER && !GTEST_OS_WINDOWS_MOBILE +// MSVC 2005 and above support variadic macros. +#define GTEST_SNPRINTF_(buffer, size, format, ...) \ + _snprintf_s(buffer, size, size, format, __VA_ARGS__) +#elif defined(_MSC_VER) +// Windows CE does not define _snprintf_s +#define GTEST_SNPRINTF_ _snprintf +#else +#define GTEST_SNPRINTF_ snprintf +#endif + +// The biggest signed integer type the compiler supports. +// +// long long is guaranteed to be at least 64-bits in C++11. +using BiggestInt = long long; // NOLINT + +// The maximum number a BiggestInt can represent. +constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits::max)(); + +// This template class serves as a compile-time function from size to +// type. It maps a size in bytes to a primitive type with that +// size. e.g. +// +// TypeWithSize<4>::UInt +// +// is typedef-ed to be unsigned int (unsigned integer made up of 4 +// bytes). +// +// Such functionality should belong to STL, but I cannot find it +// there. +// +// Google Test uses this class in the implementation of floating-point +// comparison. +// +// For now it only handles UInt (unsigned int) as that's all Google Test +// needs. Other types can be easily added in the future if need +// arises. +template +class TypeWithSize { + public: + // This prevents the user from using TypeWithSize with incorrect + // values of N. + using UInt = void; +}; + +// The specialization for size 4. +template <> +class TypeWithSize<4> { + public: + using Int = std::int32_t; + using UInt = std::uint32_t; +}; + +// The specialization for size 8. +template <> +class TypeWithSize<8> { + public: + using Int = std::int64_t; + using UInt = std::uint64_t; +}; + +// Integer types of known sizes. +using TimeInMillis = int64_t; // Represents time in milliseconds. + +// Utilities for command line flags and environment variables. + +// Macro for referencing flags. +#if !defined(GTEST_FLAG) +#define GTEST_FLAG_NAME_(name) gtest_##name +#define GTEST_FLAG(name) FLAGS_gtest_##name +#endif // !defined(GTEST_FLAG) + +// Pick a command line flags implementation. +#if GTEST_HAS_ABSL + +// Macros for defining flags. +#define GTEST_DEFINE_bool_(name, default_val, doc) \ + ABSL_FLAG(bool, GTEST_FLAG_NAME_(name), default_val, doc) +#define GTEST_DEFINE_int32_(name, default_val, doc) \ + ABSL_FLAG(int32_t, GTEST_FLAG_NAME_(name), default_val, doc) +#define GTEST_DEFINE_string_(name, default_val, doc) \ + ABSL_FLAG(std::string, GTEST_FLAG_NAME_(name), default_val, doc) + +// Macros for declaring flags. +#define GTEST_DECLARE_bool_(name) \ + ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name)) +#define GTEST_DECLARE_int32_(name) \ + ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name)) +#define GTEST_DECLARE_string_(name) \ + ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name)) + +#define GTEST_FLAG_SAVER_ ::absl::FlagSaver + +#define GTEST_FLAG_GET(name) ::absl::GetFlag(GTEST_FLAG(name)) +#define GTEST_FLAG_SET(name, value) \ + (void)(::absl::SetFlag(>EST_FLAG(name), value)) +#define GTEST_USE_OWN_FLAGFILE_FLAG_ 0 + +#else // GTEST_HAS_ABSL + +// Macros for defining flags. +#define GTEST_DEFINE_bool_(name, default_val, doc) \ + namespace testing { \ + GTEST_API_ bool GTEST_FLAG(name) = (default_val); \ + } \ + static_assert(true, "no-op to require trailing semicolon") +#define GTEST_DEFINE_int32_(name, default_val, doc) \ + namespace testing { \ + GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val); \ + } \ + static_assert(true, "no-op to require trailing semicolon") +#define GTEST_DEFINE_string_(name, default_val, doc) \ + namespace testing { \ + GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val); \ + } \ + static_assert(true, "no-op to require trailing semicolon") + +// Macros for declaring flags. +#define GTEST_DECLARE_bool_(name) \ + namespace testing { \ + GTEST_API_ extern bool GTEST_FLAG(name); \ + } \ + static_assert(true, "no-op to require trailing semicolon") +#define GTEST_DECLARE_int32_(name) \ + namespace testing { \ + GTEST_API_ extern std::int32_t GTEST_FLAG(name); \ + } \ + static_assert(true, "no-op to require trailing semicolon") +#define GTEST_DECLARE_string_(name) \ + namespace testing { \ + GTEST_API_ extern ::std::string GTEST_FLAG(name); \ + } \ + static_assert(true, "no-op to require trailing semicolon") + +#define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver + +#define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name) +#define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value) +#define GTEST_USE_OWN_FLAGFILE_FLAG_ 1 + +#endif // GTEST_HAS_ABSL + +// Thread annotations +#if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) +#define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks) +#define GTEST_LOCK_EXCLUDED_(locks) +#endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) + +// Parses 'str' for a 32-bit signed integer. If successful, writes the result +// to *value and returns true; otherwise leaves *value unchanged and returns +// false. +GTEST_API_ bool ParseInt32(const Message& src_text, const char* str, + int32_t* value); + +// Parses a bool/int32_t/string from the environment variable +// corresponding to the given Google Test flag. +bool BoolFromGTestEnv(const char* flag, bool default_val); +GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val); +std::string OutputFlagAlsoCheckEnvVar(); +const char* StringFromGTestEnv(const char* flag, const char* default_val); + +} // namespace internal +} // namespace testing + +#if !defined(GTEST_INTERNAL_DEPRECATED) + +// Internal Macro to mark an API deprecated, for googletest usage only +// Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or +// GTEST_INTERNAL_DEPRECATED(message) myFunction(); Every usage of +// a deprecated entity will trigger a warning when compiled with +// `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler). +// For msvc /W3 option will need to be used +// Note that for 'other' compilers this macro evaluates to nothing to prevent +// compilations errors. +#if defined(_MSC_VER) +#define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message)) +#elif defined(__GNUC__) +#define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message))) +#else +#define GTEST_INTERNAL_DEPRECATED(message) +#endif + +#endif // !defined(GTEST_INTERNAL_DEPRECATED) + +#if GTEST_HAS_ABSL +// Always use absl::any for UniversalPrinter<> specializations if googletest +// is built with absl support. +#define GTEST_INTERNAL_HAS_ANY 1 +#include "absl/types/any.h" +namespace testing { +namespace internal { +using Any = ::absl::any; +} // namespace internal +} // namespace testing +#else +#ifdef __has_include +#if __has_include() && __cplusplus >= 201703L +// Otherwise for C++17 and higher use std::any for UniversalPrinter<> +// specializations. +#define GTEST_INTERNAL_HAS_ANY 1 +#include +namespace testing { +namespace internal { +using Any = ::std::any; +} // namespace internal +} // namespace testing +// The case where absl is configured NOT to alias std::any is not +// supported. +#endif // __has_include() && __cplusplus >= 201703L +#endif // __has_include +#endif // GTEST_HAS_ABSL + +#if GTEST_HAS_ABSL +// Always use absl::optional for UniversalPrinter<> specializations if +// googletest is built with absl support. +#define GTEST_INTERNAL_HAS_OPTIONAL 1 +#include "absl/types/optional.h" +namespace testing { +namespace internal { +template +using Optional = ::absl::optional; +inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; } +} // namespace internal +} // namespace testing +#else +#ifdef __has_include +#if __has_include() && __cplusplus >= 201703L +// Otherwise for C++17 and higher use std::optional for UniversalPrinter<> +// specializations. +#define GTEST_INTERNAL_HAS_OPTIONAL 1 +#include +namespace testing { +namespace internal { +template +using Optional = ::std::optional; +inline ::std::nullopt_t Nullopt() { return ::std::nullopt; } +} // namespace internal +} // namespace testing +// The case where absl is configured NOT to alias std::optional is not +// supported. +#endif // __has_include() && __cplusplus >= 201703L +#endif // __has_include +#endif // GTEST_HAS_ABSL + +#if GTEST_HAS_ABSL +// Always use absl::string_view for Matcher<> specializations if googletest +// is built with absl support. +#define GTEST_INTERNAL_HAS_STRING_VIEW 1 +#include "absl/strings/string_view.h" +namespace testing { +namespace internal { +using StringView = ::absl::string_view; +} // namespace internal +} // namespace testing +#else +#ifdef __has_include +#if __has_include() && __cplusplus >= 201703L +// Otherwise for C++17 and higher use std::string_view for Matcher<> +// specializations. +#define GTEST_INTERNAL_HAS_STRING_VIEW 1 +#include +namespace testing { +namespace internal { +using StringView = ::std::string_view; +} // namespace internal +} // namespace testing +// The case where absl is configured NOT to alias std::string_view is not +// supported. +#endif // __has_include() && __cplusplus >= 201703L +#endif // __has_include +#endif // GTEST_HAS_ABSL + +#if GTEST_HAS_ABSL +// Always use absl::variant for UniversalPrinter<> specializations if googletest +// is built with absl support. +#define GTEST_INTERNAL_HAS_VARIANT 1 +#include "absl/types/variant.h" +namespace testing { +namespace internal { +template +using Variant = ::absl::variant; +} // namespace internal +} // namespace testing +#else +#ifdef __has_include +#if __has_include() && __cplusplus >= 201703L +// Otherwise for C++17 and higher use std::variant for UniversalPrinter<> +// specializations. +#define GTEST_INTERNAL_HAS_VARIANT 1 +#include +namespace testing { +namespace internal { +template +using Variant = ::std::variant; +} // namespace internal +} // namespace testing +// The case where absl is configured NOT to alias std::variant is not supported. +#endif // __has_include() && __cplusplus >= 201703L +#endif // __has_include +#endif // GTEST_HAS_ABSL + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ diff --git a/test/gtest/include/gtest/internal/gtest-string.h b/test/gtest/include/gtest/internal/gtest-string.h new file mode 100644 index 0000000000..cca2e1f2ad --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-string.h @@ -0,0 +1,177 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This header file declares the String class and functions used internally by +// Google Test. They are subject to change without notice. They should not used +// by code external to Google Test. +// +// This header file is #included by gtest-internal.h. +// It should not be #included by other files. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ + +#ifdef __BORLANDC__ +// string.h is not guaranteed to provide strcpy on C++ Builder. +#include +#endif + +#include + +#include +#include + +#include "gtest/internal/gtest-port.h" + +namespace testing { +namespace internal { + +// String - an abstract class holding static string utilities. +class GTEST_API_ String { + public: + // Static utility methods + + // Clones a 0-terminated C string, allocating memory using new. The + // caller is responsible for deleting the return value using + // delete[]. Returns the cloned string, or NULL if the input is + // NULL. + // + // This is different from strdup() in string.h, which allocates + // memory using malloc(). + static const char* CloneCString(const char* c_str); + +#if GTEST_OS_WINDOWS_MOBILE + // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be + // able to pass strings to Win32 APIs on CE we need to convert them + // to 'Unicode', UTF-16. + + // Creates a UTF-16 wide string from the given ANSI string, allocating + // memory using new. The caller is responsible for deleting the return + // value using delete[]. Returns the wide string, or NULL if the + // input is NULL. + // + // The wide string is created using the ANSI codepage (CP_ACP) to + // match the behaviour of the ANSI versions of Win32 calls and the + // C runtime. + static LPCWSTR AnsiToUtf16(const char* c_str); + + // Creates an ANSI string from the given wide string, allocating + // memory using new. The caller is responsible for deleting the return + // value using delete[]. Returns the ANSI string, or NULL if the + // input is NULL. + // + // The returned string is created using the ANSI codepage (CP_ACP) to + // match the behaviour of the ANSI versions of Win32 calls and the + // C runtime. + static const char* Utf16ToAnsi(LPCWSTR utf16_str); +#endif + + // Compares two C strings. Returns true if and only if they have the same + // content. + // + // Unlike strcmp(), this function can handle NULL argument(s). A + // NULL C string is considered different to any non-NULL C string, + // including the empty string. + static bool CStringEquals(const char* lhs, const char* rhs); + + // Converts a wide C string to a String using the UTF-8 encoding. + // NULL will be converted to "(null)". If an error occurred during + // the conversion, "(failed to convert from wide string)" is + // returned. + static std::string ShowWideCString(const wchar_t* wide_c_str); + + // Compares two wide C strings. Returns true if and only if they have the + // same content. + // + // Unlike wcscmp(), this function can handle NULL argument(s). A + // NULL C string is considered different to any non-NULL C string, + // including the empty string. + static bool WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs); + + // Compares two C strings, ignoring case. Returns true if and only if + // they have the same content. + // + // Unlike strcasecmp(), this function can handle NULL argument(s). + // A NULL C string is considered different to any non-NULL C string, + // including the empty string. + static bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs); + + // Compares two wide C strings, ignoring case. Returns true if and only if + // they have the same content. + // + // Unlike wcscasecmp(), this function can handle NULL argument(s). + // A NULL C string is considered different to any non-NULL wide C string, + // including the empty string. + // NB: The implementations on different platforms slightly differ. + // On windows, this method uses _wcsicmp which compares according to LC_CTYPE + // environment variable. On GNU platform this method uses wcscasecmp + // which compares according to LC_CTYPE category of the current locale. + // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the + // current locale. + static bool CaseInsensitiveWideCStringEquals(const wchar_t* lhs, + const wchar_t* rhs); + + // Returns true if and only if the given string ends with the given suffix, + // ignoring case. Any string is considered to end with an empty suffix. + static bool EndsWithCaseInsensitive(const std::string& str, + const std::string& suffix); + + // Formats an int value as "%02d". + static std::string FormatIntWidth2(int value); // "%02d" for width == 2 + + // Formats an int value to given width with leading zeros. + static std::string FormatIntWidthN(int value, int width); + + // Formats an int value as "%X". + static std::string FormatHexInt(int value); + + // Formats an int value as "%X". + static std::string FormatHexUInt32(uint32_t value); + + // Formats a byte as "%02X". + static std::string FormatByte(unsigned char value); + + private: + String(); // Not meant to be instantiated. +}; // class String + +// Gets the content of the stringstream's buffer as an std::string. Each '\0' +// character in the buffer is replaced with "\\0". +GTEST_API_ std::string StringStreamToString(::std::stringstream* stream); + +} // namespace internal +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_ diff --git a/test/gtest/include/gtest/internal/gtest-type-util.h b/test/gtest/include/gtest/internal/gtest-type-util.h new file mode 100644 index 0000000000..6bc02a7de3 --- /dev/null +++ b/test/gtest/include/gtest/internal/gtest-type-util.h @@ -0,0 +1,186 @@ +// Copyright 2008 Google Inc. +// All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Type utilities needed for implementing typed and type-parameterized +// tests. + +// IWYU pragma: private, include "gtest/gtest.h" +// IWYU pragma: friend gtest/.* +// IWYU pragma: friend gmock/.* + +#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ +#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ + +#include "gtest/internal/gtest-port.h" + +// #ifdef __GNUC__ is too general here. It is possible to use gcc without using +// libstdc++ (which is where cxxabi.h comes from). +#if GTEST_HAS_CXXABI_H_ +#include +#elif defined(__HP_aCC) +#include +#endif // GTEST_HASH_CXXABI_H_ + +namespace testing { +namespace internal { + +// Canonicalizes a given name with respect to the Standard C++ Library. +// This handles removing the inline namespace within `std` that is +// used by various standard libraries (e.g., `std::__1`). Names outside +// of namespace std are returned unmodified. +inline std::string CanonicalizeForStdLibVersioning(std::string s) { + static const char prefix[] = "std::__"; + if (s.compare(0, strlen(prefix), prefix) == 0) { + std::string::size_type end = s.find("::", strlen(prefix)); + if (end != s.npos) { + // Erase everything between the initial `std` and the second `::`. + s.erase(strlen("std"), end - strlen("std")); + } + } + return s; +} + +#if GTEST_HAS_RTTI +// GetTypeName(const std::type_info&) returns a human-readable name of type T. +inline std::string GetTypeName(const std::type_info& type) { + const char* const name = type.name(); +#if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC) + int status = 0; + // gcc's implementation of typeid(T).name() mangles the type name, + // so we have to demangle it. +#if GTEST_HAS_CXXABI_H_ + using abi::__cxa_demangle; +#endif // GTEST_HAS_CXXABI_H_ + char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status); + const std::string name_str(status == 0 ? readable_name : name); + free(readable_name); + return CanonicalizeForStdLibVersioning(name_str); +#else + return name; +#endif // GTEST_HAS_CXXABI_H_ || __HP_aCC +} +#endif // GTEST_HAS_RTTI + +// GetTypeName() returns a human-readable name of type T if and only if +// RTTI is enabled, otherwise it returns a dummy type name. +// NB: This function is also used in Google Mock, so don't move it inside of +// the typed-test-only section below. +template +std::string GetTypeName() { +#if GTEST_HAS_RTTI + return GetTypeName(typeid(T)); +#else + return ""; +#endif // GTEST_HAS_RTTI +} + +// A unique type indicating an empty node +struct None {}; + +#define GTEST_TEMPLATE_ \ + template \ + class + +// The template "selector" struct TemplateSel is used to +// represent Tmpl, which must be a class template with one type +// parameter, as a type. TemplateSel::Bind::type is defined +// as the type Tmpl. This allows us to actually instantiate the +// template "selected" by TemplateSel. +// +// This trick is necessary for simulating typedef for class templates, +// which C++ doesn't support directly. +template +struct TemplateSel { + template + struct Bind { + typedef Tmpl type; + }; +}; + +#define GTEST_BIND_(TmplSel, T) TmplSel::template Bind::type + +template +struct Templates { + using Head = TemplateSel; + using Tail = Templates; +}; + +template +struct Templates { + using Head = TemplateSel; + using Tail = None; +}; + +// Tuple-like type lists +template +struct Types { + using Head = Head_; + using Tail = Types; +}; + +template +struct Types { + using Head = Head_; + using Tail = None; +}; + +// Helper metafunctions to tell apart a single type from types +// generated by ::testing::Types +template +struct ProxyTypeList { + using type = Types; +}; + +template +struct is_proxy_type_list : std::false_type {}; + +template +struct is_proxy_type_list> : std::true_type {}; + +// Generator which conditionally creates type lists. +// It recognizes if a requested type list should be created +// and prevents creating a new type list nested within another one. +template +struct GenerateTypeList { + private: + using proxy = typename std::conditional::value, T, + ProxyTypeList>::type; + + public: + using type = typename proxy::type; +}; + +} // namespace internal + +template +using Types = internal::ProxyTypeList; + +} // namespace testing + +#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ diff --git a/test/gtest/src/gtest-all.cc b/test/gtest/src/gtest-all.cc new file mode 100644 index 0000000000..2a70ed88c7 --- /dev/null +++ b/test/gtest/src/gtest-all.cc @@ -0,0 +1,49 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// +// Google C++ Testing and Mocking Framework (Google Test) +// +// Sometimes it's desirable to build Google Test by compiling a single file. +// This file serves this purpose. + +// This line ensures that gtest.h can be compiled on its own, even +// when it's fused. +#include "gtest/gtest.h" + +// The following lines pull in the real gtest *.cc files. +#include "src/gtest-assertion-result.cc" +#include "src/gtest-death-test.cc" +#include "src/gtest-filepath.cc" +#include "src/gtest-matchers.cc" +#include "src/gtest-port.cc" +#include "src/gtest-printers.cc" +#include "src/gtest-test-part.cc" +#include "src/gtest-typed-test.cc" +#include "src/gtest.cc" diff --git a/test/gtest/src/gtest-assertion-result.cc b/test/gtest/src/gtest-assertion-result.cc new file mode 100644 index 0000000000..f1c0b10dc9 --- /dev/null +++ b/test/gtest/src/gtest-assertion-result.cc @@ -0,0 +1,77 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This file defines the AssertionResult type. + +#include "gtest/gtest-assertion-result.h" + +#include +#include + +#include "gtest/gtest-message.h" + +namespace testing { + +// AssertionResult constructors. +// Used in EXPECT_TRUE/FALSE(assertion_result). +AssertionResult::AssertionResult(const AssertionResult& other) + : success_(other.success_), + message_(other.message_.get() != nullptr + ? new ::std::string(*other.message_) + : static_cast< ::std::string*>(nullptr)) {} + +// Swaps two AssertionResults. +void AssertionResult::swap(AssertionResult& other) { + using std::swap; + swap(success_, other.success_); + swap(message_, other.message_); +} + +// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. +AssertionResult AssertionResult::operator!() const { + AssertionResult negation(!success_); + if (message_.get() != nullptr) negation << *message_; + return negation; +} + +// Makes a successful assertion result. +AssertionResult AssertionSuccess() { return AssertionResult(true); } + +// Makes a failed assertion result. +AssertionResult AssertionFailure() { return AssertionResult(false); } + +// Makes a failed assertion result with the given failure message. +// Deprecated; use AssertionFailure() << message. +AssertionResult AssertionFailure(const Message& message) { + return AssertionFailure() << message; +} + +} // namespace testing diff --git a/test/gtest/src/gtest-death-test.cc b/test/gtest/src/gtest-death-test.cc new file mode 100644 index 0000000000..e6abc6278a --- /dev/null +++ b/test/gtest/src/gtest-death-test.cc @@ -0,0 +1,1620 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// +// This file implements death tests. + +#include "gtest/gtest-death-test.h" + +#include +#include + +#include "gtest/internal/custom/gtest.h" +#include "gtest/internal/gtest-port.h" + +#if GTEST_HAS_DEATH_TEST + +#if GTEST_OS_MAC +#include +#endif // GTEST_OS_MAC + +#include +#include +#include + +#if GTEST_OS_LINUX +#include +#endif // GTEST_OS_LINUX + +#include + +#if GTEST_OS_WINDOWS +#include +#else +#include +#include +#endif // GTEST_OS_WINDOWS + +#if GTEST_OS_QNX +#include +#endif // GTEST_OS_QNX + +#if GTEST_OS_FUCHSIA +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif // GTEST_OS_FUCHSIA + +#endif // GTEST_HAS_DEATH_TEST + +#include "gtest/gtest-message.h" +#include "gtest/internal/gtest-string.h" +#include "src/gtest-internal-inl.h" + +namespace testing { + +// Constants. + +// The default death test style. +// +// This is defined in internal/gtest-port.h as "fast", but can be overridden by +// a definition in internal/custom/gtest-port.h. The recommended value, which is +// used internally at Google, is "threadsafe". +static const char kDefaultDeathTestStyle[] = GTEST_DEFAULT_DEATH_TEST_STYLE; + +} // namespace testing + +GTEST_DEFINE_string_( + death_test_style, + testing::internal::StringFromGTestEnv("death_test_style", + testing::kDefaultDeathTestStyle), + "Indicates how to run a death test in a forked child process: " + "\"threadsafe\" (child process re-executes the test binary " + "from the beginning, running only the specific death test) or " + "\"fast\" (child process runs the death test immediately " + "after forking)."); + +GTEST_DEFINE_bool_( + death_test_use_fork, + testing::internal::BoolFromGTestEnv("death_test_use_fork", false), + "Instructs to use fork()/_exit() instead of clone() in death tests. " + "Ignored and always uses fork() on POSIX systems where clone() is not " + "implemented. Useful when running under valgrind or similar tools if " + "those do not support clone(). Valgrind 3.3.1 will just fail if " + "it sees an unsupported combination of clone() flags. " + "It is not recommended to use this flag w/o valgrind though it will " + "work in 99% of the cases. Once valgrind is fixed, this flag will " + "most likely be removed."); + +GTEST_DEFINE_string_( + internal_run_death_test, "", + "Indicates the file, line number, temporal index of " + "the single death test to run, and a file descriptor to " + "which a success code may be sent, all separated by " + "the '|' characters. This flag is specified if and only if the " + "current process is a sub-process launched for running a thread-safe " + "death test. FOR INTERNAL USE ONLY."); + +namespace testing { + +#if GTEST_HAS_DEATH_TEST + +namespace internal { + +// Valid only for fast death tests. Indicates the code is running in the +// child process of a fast style death test. +#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA +static bool g_in_fast_death_test_child = false; +#endif + +// Returns a Boolean value indicating whether the caller is currently +// executing in the context of the death test child process. Tools such as +// Valgrind heap checkers may need this to modify their behavior in death +// tests. IMPORTANT: This is an internal utility. Using it may break the +// implementation of death tests. User code MUST NOT use it. +bool InDeathTestChild() { +#if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA + + // On Windows and Fuchsia, death tests are thread-safe regardless of the value + // of the death_test_style flag. + return !GTEST_FLAG_GET(internal_run_death_test).empty(); + +#else + + if (GTEST_FLAG_GET(death_test_style) == "threadsafe") + return !GTEST_FLAG_GET(internal_run_death_test).empty(); + else + return g_in_fast_death_test_child; +#endif +} + +} // namespace internal + +// ExitedWithCode constructor. +ExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {} + +// ExitedWithCode function-call operator. +bool ExitedWithCode::operator()(int exit_status) const { +#if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA + + return exit_status == exit_code_; + +#else + + return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_; + +#endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA +} + +#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA +// KilledBySignal constructor. +KilledBySignal::KilledBySignal(int signum) : signum_(signum) {} + +// KilledBySignal function-call operator. +bool KilledBySignal::operator()(int exit_status) const { +#if defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_) + { + bool result; + if (GTEST_KILLED_BY_SIGNAL_OVERRIDE_(signum_, exit_status, &result)) { + return result; + } + } +#endif // defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_) + return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_; +} +#endif // !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA + +namespace internal { + +// Utilities needed for death tests. + +// Generates a textual description of a given exit code, in the format +// specified by wait(2). +static std::string ExitSummary(int exit_code) { + Message m; + +#if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA + + m << "Exited with exit status " << exit_code; + +#else + + if (WIFEXITED(exit_code)) { + m << "Exited with exit status " << WEXITSTATUS(exit_code); + } else if (WIFSIGNALED(exit_code)) { + m << "Terminated by signal " << WTERMSIG(exit_code); + } +#ifdef WCOREDUMP + if (WCOREDUMP(exit_code)) { + m << " (core dumped)"; + } +#endif +#endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA + + return m.GetString(); +} + +// Returns true if exit_status describes a process that was terminated +// by a signal, or exited normally with a nonzero exit code. +bool ExitedUnsuccessfully(int exit_status) { + return !ExitedWithCode(0)(exit_status); +} + +#if !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA +// Generates a textual failure message when a death test finds more than +// one thread running, or cannot determine the number of threads, prior +// to executing the given statement. It is the responsibility of the +// caller not to pass a thread_count of 1. +static std::string DeathTestThreadWarning(size_t thread_count) { + Message msg; + msg << "Death tests use fork(), which is unsafe particularly" + << " in a threaded context. For this test, " << GTEST_NAME_ << " "; + if (thread_count == 0) { + msg << "couldn't detect the number of threads."; + } else { + msg << "detected " << thread_count << " threads."; + } + msg << " See " + "https://github.com/google/googletest/blob/master/docs/" + "advanced.md#death-tests-and-threads" + << " for more explanation and suggested solutions, especially if" + << " this is the last message you see before your test times out."; + return msg.GetString(); +} +#endif // !GTEST_OS_WINDOWS && !GTEST_OS_FUCHSIA + +// Flag characters for reporting a death test that did not die. +static const char kDeathTestLived = 'L'; +static const char kDeathTestReturned = 'R'; +static const char kDeathTestThrew = 'T'; +static const char kDeathTestInternalError = 'I'; + +#if GTEST_OS_FUCHSIA + +// File descriptor used for the pipe in the child process. +static const int kFuchsiaReadPipeFd = 3; + +#endif + +// An enumeration describing all of the possible ways that a death test can +// conclude. DIED means that the process died while executing the test +// code; LIVED means that process lived beyond the end of the test code; +// RETURNED means that the test statement attempted to execute a return +// statement, which is not allowed; THREW means that the test statement +// returned control by throwing an exception. IN_PROGRESS means the test +// has not yet concluded. +enum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW }; + +// Routine for aborting the program which is safe to call from an +// exec-style death test child process, in which case the error +// message is propagated back to the parent process. Otherwise, the +// message is simply printed to stderr. In either case, the program +// then exits with status 1. +static void DeathTestAbort(const std::string& message) { + // On a POSIX system, this function may be called from a threadsafe-style + // death test child process, which operates on a very small stack. Use + // the heap for any additional non-minuscule memory requirements. + const InternalRunDeathTestFlag* const flag = + GetUnitTestImpl()->internal_run_death_test_flag(); + if (flag != nullptr) { + FILE* parent = posix::FDOpen(flag->write_fd(), "w"); + fputc(kDeathTestInternalError, parent); + fprintf(parent, "%s", message.c_str()); + fflush(parent); + _exit(1); + } else { + fprintf(stderr, "%s", message.c_str()); + fflush(stderr); + posix::Abort(); + } +} + +// A replacement for CHECK that calls DeathTestAbort if the assertion +// fails. +#define GTEST_DEATH_TEST_CHECK_(expression) \ + do { \ + if (!::testing::internal::IsTrue(expression)) { \ + DeathTestAbort(::std::string("CHECK failed: File ") + __FILE__ + \ + ", line " + \ + ::testing::internal::StreamableToString(__LINE__) + \ + ": " + #expression); \ + } \ + } while (::testing::internal::AlwaysFalse()) + +// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for +// evaluating any system call that fulfills two conditions: it must return +// -1 on failure, and set errno to EINTR when it is interrupted and +// should be tried again. The macro expands to a loop that repeatedly +// evaluates the expression as long as it evaluates to -1 and sets +// errno to EINTR. If the expression evaluates to -1 but errno is +// something other than EINTR, DeathTestAbort is called. +#define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \ + do { \ + int gtest_retval; \ + do { \ + gtest_retval = (expression); \ + } while (gtest_retval == -1 && errno == EINTR); \ + if (gtest_retval == -1) { \ + DeathTestAbort(::std::string("CHECK failed: File ") + __FILE__ + \ + ", line " + \ + ::testing::internal::StreamableToString(__LINE__) + \ + ": " + #expression + " != -1"); \ + } \ + } while (::testing::internal::AlwaysFalse()) + +// Returns the message describing the last system error in errno. +std::string GetLastErrnoDescription() { + return errno == 0 ? "" : posix::StrError(errno); +} + +// This is called from a death test parent process to read a failure +// message from the death test child process and log it with the FATAL +// severity. On Windows, the message is read from a pipe handle. On other +// platforms, it is read from a file descriptor. +static void FailFromInternalError(int fd) { + Message error; + char buffer[256]; + int num_read; + + do { + while ((num_read = posix::Read(fd, buffer, 255)) > 0) { + buffer[num_read] = '\0'; + error << buffer; + } + } while (num_read == -1 && errno == EINTR); + + if (num_read == 0) { + GTEST_LOG_(FATAL) << error.GetString(); + } else { + const int last_error = errno; + GTEST_LOG_(FATAL) << "Error while reading death test internal: " + << GetLastErrnoDescription() << " [" << last_error << "]"; + } +} + +// Death test constructor. Increments the running death test count +// for the current test. +DeathTest::DeathTest() { + TestInfo* const info = GetUnitTestImpl()->current_test_info(); + if (info == nullptr) { + DeathTestAbort( + "Cannot run a death test outside of a TEST or " + "TEST_F construct"); + } +} + +// Creates and returns a death test by dispatching to the current +// death test factory. +bool DeathTest::Create(const char* statement, + Matcher matcher, const char* file, + int line, DeathTest** test) { + return GetUnitTestImpl()->death_test_factory()->Create( + statement, std::move(matcher), file, line, test); +} + +const char* DeathTest::LastMessage() { + return last_death_test_message_.c_str(); +} + +void DeathTest::set_last_death_test_message(const std::string& message) { + last_death_test_message_ = message; +} + +std::string DeathTest::last_death_test_message_; + +// Provides cross platform implementation for some death functionality. +class DeathTestImpl : public DeathTest { + protected: + DeathTestImpl(const char* a_statement, Matcher matcher) + : statement_(a_statement), + matcher_(std::move(matcher)), + spawned_(false), + status_(-1), + outcome_(IN_PROGRESS), + read_fd_(-1), + write_fd_(-1) {} + + // read_fd_ is expected to be closed and cleared by a derived class. + ~DeathTestImpl() override { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); } + + void Abort(AbortReason reason) override; + bool Passed(bool status_ok) override; + + const char* statement() const { return statement_; } + bool spawned() const { return spawned_; } + void set_spawned(bool is_spawned) { spawned_ = is_spawned; } + int status() const { return status_; } + void set_status(int a_status) { status_ = a_status; } + DeathTestOutcome outcome() const { return outcome_; } + void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; } + int read_fd() const { return read_fd_; } + void set_read_fd(int fd) { read_fd_ = fd; } + int write_fd() const { return write_fd_; } + void set_write_fd(int fd) { write_fd_ = fd; } + + // Called in the parent process only. Reads the result code of the death + // test child process via a pipe, interprets it to set the outcome_ + // member, and closes read_fd_. Outputs diagnostics and terminates in + // case of unexpected codes. + void ReadAndInterpretStatusByte(); + + // Returns stderr output from the child process. + virtual std::string GetErrorLogs(); + + private: + // The textual content of the code this object is testing. This class + // doesn't own this string and should not attempt to delete it. + const char* const statement_; + // A matcher that's expected to match the stderr output by the child process. + Matcher matcher_; + // True if the death test child process has been successfully spawned. + bool spawned_; + // The exit status of the child process. + int status_; + // How the death test concluded. + DeathTestOutcome outcome_; + // Descriptor to the read end of the pipe to the child process. It is + // always -1 in the child process. The child keeps its write end of the + // pipe in write_fd_. + int read_fd_; + // Descriptor to the child's write end of the pipe to the parent process. + // It is always -1 in the parent process. The parent keeps its end of the + // pipe in read_fd_. + int write_fd_; +}; + +// Called in the parent process only. Reads the result code of the death +// test child process via a pipe, interprets it to set the outcome_ +// member, and closes read_fd_. Outputs diagnostics and terminates in +// case of unexpected codes. +void DeathTestImpl::ReadAndInterpretStatusByte() { + char flag; + int bytes_read; + + // The read() here blocks until data is available (signifying the + // failure of the death test) or until the pipe is closed (signifying + // its success), so it's okay to call this in the parent before + // the child process has exited. + do { + bytes_read = posix::Read(read_fd(), &flag, 1); + } while (bytes_read == -1 && errno == EINTR); + + if (bytes_read == 0) { + set_outcome(DIED); + } else if (bytes_read == 1) { + switch (flag) { + case kDeathTestReturned: + set_outcome(RETURNED); + break; + case kDeathTestThrew: + set_outcome(THREW); + break; + case kDeathTestLived: + set_outcome(LIVED); + break; + case kDeathTestInternalError: + FailFromInternalError(read_fd()); // Does not return. + break; + default: + GTEST_LOG_(FATAL) << "Death test child process reported " + << "unexpected status byte (" + << static_cast(flag) << ")"; + } + } else { + GTEST_LOG_(FATAL) << "Read from death test child process failed: " + << GetLastErrnoDescription(); + } + GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd())); + set_read_fd(-1); +} + +std::string DeathTestImpl::GetErrorLogs() { return GetCapturedStderr(); } + +// Signals that the death test code which should have exited, didn't. +// Should be called only in a death test child process. +// Writes a status byte to the child's status file descriptor, then +// calls _exit(1). +void DeathTestImpl::Abort(AbortReason reason) { + // The parent process considers the death test to be a failure if + // it finds any data in our pipe. So, here we write a single flag byte + // to the pipe, then exit. + const char status_ch = reason == TEST_DID_NOT_DIE ? kDeathTestLived + : reason == TEST_THREW_EXCEPTION ? kDeathTestThrew + : kDeathTestReturned; + + GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1)); + // We are leaking the descriptor here because on some platforms (i.e., + // when built as Windows DLL), destructors of global objects will still + // run after calling _exit(). On such systems, write_fd_ will be + // indirectly closed from the destructor of UnitTestImpl, causing double + // close if it is also closed here. On debug configurations, double close + // may assert. As there are no in-process buffers to flush here, we are + // relying on the OS to close the descriptor after the process terminates + // when the destructors are not run. + _exit(1); // Exits w/o any normal exit hooks (we were supposed to crash) +} + +// Returns an indented copy of stderr output for a death test. +// This makes distinguishing death test output lines from regular log lines +// much easier. +static ::std::string FormatDeathTestOutput(const ::std::string& output) { + ::std::string ret; + for (size_t at = 0;;) { + const size_t line_end = output.find('\n', at); + ret += "[ DEATH ] "; + if (line_end == ::std::string::npos) { + ret += output.substr(at); + break; + } + ret += output.substr(at, line_end + 1 - at); + at = line_end + 1; + } + return ret; +} + +// Assesses the success or failure of a death test, using both private +// members which have previously been set, and one argument: +// +// Private data members: +// outcome: An enumeration describing how the death test +// concluded: DIED, LIVED, THREW, or RETURNED. The death test +// fails in the latter three cases. +// status: The exit status of the child process. On *nix, it is in the +// in the format specified by wait(2). On Windows, this is the +// value supplied to the ExitProcess() API or a numeric code +// of the exception that terminated the program. +// matcher_: A matcher that's expected to match the stderr output by the child +// process. +// +// Argument: +// status_ok: true if exit_status is acceptable in the context of +// this particular death test, which fails if it is false +// +// Returns true if and only if all of the above conditions are met. Otherwise, +// the first failing condition, in the order given above, is the one that is +// reported. Also sets the last death test message string. +bool DeathTestImpl::Passed(bool status_ok) { + if (!spawned()) return false; + + const std::string error_message = GetErrorLogs(); + + bool success = false; + Message buffer; + + buffer << "Death test: " << statement() << "\n"; + switch (outcome()) { + case LIVED: + buffer << " Result: failed to die.\n" + << " Error msg:\n" + << FormatDeathTestOutput(error_message); + break; + case THREW: + buffer << " Result: threw an exception.\n" + << " Error msg:\n" + << FormatDeathTestOutput(error_message); + break; + case RETURNED: + buffer << " Result: illegal return in test statement.\n" + << " Error msg:\n" + << FormatDeathTestOutput(error_message); + break; + case DIED: + if (status_ok) { + if (matcher_.Matches(error_message)) { + success = true; + } else { + std::ostringstream stream; + matcher_.DescribeTo(&stream); + buffer << " Result: died but not with expected error.\n" + << " Expected: " << stream.str() << "\n" + << "Actual msg:\n" + << FormatDeathTestOutput(error_message); + } + } else { + buffer << " Result: died but not with expected exit code:\n" + << " " << ExitSummary(status()) << "\n" + << "Actual msg:\n" + << FormatDeathTestOutput(error_message); + } + break; + case IN_PROGRESS: + default: + GTEST_LOG_(FATAL) + << "DeathTest::Passed somehow called before conclusion of test"; + } + + DeathTest::set_last_death_test_message(buffer.GetString()); + return success; +} + +#if GTEST_OS_WINDOWS +// WindowsDeathTest implements death tests on Windows. Due to the +// specifics of starting new processes on Windows, death tests there are +// always threadsafe, and Google Test considers the +// --gtest_death_test_style=fast setting to be equivalent to +// --gtest_death_test_style=threadsafe there. +// +// A few implementation notes: Like the Linux version, the Windows +// implementation uses pipes for child-to-parent communication. But due to +// the specifics of pipes on Windows, some extra steps are required: +// +// 1. The parent creates a communication pipe and stores handles to both +// ends of it. +// 2. The parent starts the child and provides it with the information +// necessary to acquire the handle to the write end of the pipe. +// 3. The child acquires the write end of the pipe and signals the parent +// using a Windows event. +// 4. Now the parent can release the write end of the pipe on its side. If +// this is done before step 3, the object's reference count goes down to +// 0 and it is destroyed, preventing the child from acquiring it. The +// parent now has to release it, or read operations on the read end of +// the pipe will not return when the child terminates. +// 5. The parent reads child's output through the pipe (outcome code and +// any possible error messages) from the pipe, and its stderr and then +// determines whether to fail the test. +// +// Note: to distinguish Win32 API calls from the local method and function +// calls, the former are explicitly resolved in the global namespace. +// +class WindowsDeathTest : public DeathTestImpl { + public: + WindowsDeathTest(const char* a_statement, Matcher matcher, + const char* file, int line) + : DeathTestImpl(a_statement, std::move(matcher)), + file_(file), + line_(line) {} + + // All of these virtual functions are inherited from DeathTest. + virtual int Wait(); + virtual TestRole AssumeRole(); + + private: + // The name of the file in which the death test is located. + const char* const file_; + // The line number on which the death test is located. + const int line_; + // Handle to the write end of the pipe to the child process. + AutoHandle write_handle_; + // Child process handle. + AutoHandle child_handle_; + // Event the child process uses to signal the parent that it has + // acquired the handle to the write end of the pipe. After seeing this + // event the parent can release its own handles to make sure its + // ReadFile() calls return when the child terminates. + AutoHandle event_handle_; +}; + +// Waits for the child in a death test to exit, returning its exit +// status, or 0 if no child process exists. As a side effect, sets the +// outcome data member. +int WindowsDeathTest::Wait() { + if (!spawned()) return 0; + + // Wait until the child either signals that it has acquired the write end + // of the pipe or it dies. + const HANDLE wait_handles[2] = {child_handle_.Get(), event_handle_.Get()}; + switch (::WaitForMultipleObjects(2, wait_handles, + FALSE, // Waits for any of the handles. + INFINITE)) { + case WAIT_OBJECT_0: + case WAIT_OBJECT_0 + 1: + break; + default: + GTEST_DEATH_TEST_CHECK_(false); // Should not get here. + } + + // The child has acquired the write end of the pipe or exited. + // We release the handle on our side and continue. + write_handle_.Reset(); + event_handle_.Reset(); + + ReadAndInterpretStatusByte(); + + // Waits for the child process to exit if it haven't already. This + // returns immediately if the child has already exited, regardless of + // whether previous calls to WaitForMultipleObjects synchronized on this + // handle or not. + GTEST_DEATH_TEST_CHECK_(WAIT_OBJECT_0 == + ::WaitForSingleObject(child_handle_.Get(), INFINITE)); + DWORD status_code; + GTEST_DEATH_TEST_CHECK_( + ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE); + child_handle_.Reset(); + set_status(static_cast(status_code)); + return status(); +} + +// The AssumeRole process for a Windows death test. It creates a child +// process with the same executable as the current process to run the +// death test. The child process is given the --gtest_filter and +// --gtest_internal_run_death_test flags such that it knows to run the +// current death test only. +DeathTest::TestRole WindowsDeathTest::AssumeRole() { + const UnitTestImpl* const impl = GetUnitTestImpl(); + const InternalRunDeathTestFlag* const flag = + impl->internal_run_death_test_flag(); + const TestInfo* const info = impl->current_test_info(); + const int death_test_index = info->result()->death_test_count(); + + if (flag != nullptr) { + // ParseInternalRunDeathTestFlag() has performed all the necessary + // processing. + set_write_fd(flag->write_fd()); + return EXECUTE_TEST; + } + + // WindowsDeathTest uses an anonymous pipe to communicate results of + // a death test. + SECURITY_ATTRIBUTES handles_are_inheritable = {sizeof(SECURITY_ATTRIBUTES), + nullptr, TRUE}; + HANDLE read_handle, write_handle; + GTEST_DEATH_TEST_CHECK_(::CreatePipe(&read_handle, &write_handle, + &handles_are_inheritable, + 0) // Default buffer size. + != FALSE); + set_read_fd( + ::_open_osfhandle(reinterpret_cast(read_handle), O_RDONLY)); + write_handle_.Reset(write_handle); + event_handle_.Reset(::CreateEvent( + &handles_are_inheritable, + TRUE, // The event will automatically reset to non-signaled state. + FALSE, // The initial state is non-signalled. + nullptr)); // The even is unnamed. + GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != nullptr); + const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + + "filter=" + info->test_suite_name() + "." + + info->name(); + const std::string internal_flag = + std::string("--") + GTEST_FLAG_PREFIX_ + + "internal_run_death_test=" + file_ + "|" + StreamableToString(line_) + + "|" + StreamableToString(death_test_index) + "|" + + StreamableToString(static_cast(::GetCurrentProcessId())) + + // size_t has the same width as pointers on both 32-bit and 64-bit + // Windows platforms. + // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx. + "|" + StreamableToString(reinterpret_cast(write_handle)) + "|" + + StreamableToString(reinterpret_cast(event_handle_.Get())); + + char executable_path[_MAX_PATH + 1]; // NOLINT + GTEST_DEATH_TEST_CHECK_(_MAX_PATH + 1 != ::GetModuleFileNameA(nullptr, + executable_path, + _MAX_PATH)); + + std::string command_line = std::string(::GetCommandLineA()) + " " + + filter_flag + " \"" + internal_flag + "\""; + + DeathTest::set_last_death_test_message(""); + + CaptureStderr(); + // Flush the log buffers since the log streams are shared with the child. + FlushInfoLog(); + + // The child process will share the standard handles with the parent. + STARTUPINFOA startup_info; + memset(&startup_info, 0, sizeof(STARTUPINFO)); + startup_info.dwFlags = STARTF_USESTDHANDLES; + startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE); + startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE); + startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE); + + PROCESS_INFORMATION process_info; + GTEST_DEATH_TEST_CHECK_( + ::CreateProcessA( + executable_path, const_cast(command_line.c_str()), + nullptr, // Returned process handle is not inheritable. + nullptr, // Returned thread handle is not inheritable. + TRUE, // Child inherits all inheritable handles (for write_handle_). + 0x0, // Default creation flags. + nullptr, // Inherit the parent's environment. + UnitTest::GetInstance()->original_working_dir(), &startup_info, + &process_info) != FALSE); + child_handle_.Reset(process_info.hProcess); + ::CloseHandle(process_info.hThread); + set_spawned(true); + return OVERSEE_TEST; +} + +#elif GTEST_OS_FUCHSIA + +class FuchsiaDeathTest : public DeathTestImpl { + public: + FuchsiaDeathTest(const char* a_statement, Matcher matcher, + const char* file, int line) + : DeathTestImpl(a_statement, std::move(matcher)), + file_(file), + line_(line) {} + + // All of these virtual functions are inherited from DeathTest. + int Wait() override; + TestRole AssumeRole() override; + std::string GetErrorLogs() override; + + private: + // The name of the file in which the death test is located. + const char* const file_; + // The line number on which the death test is located. + const int line_; + // The stderr data captured by the child process. + std::string captured_stderr_; + + zx::process child_process_; + zx::channel exception_channel_; + zx::socket stderr_socket_; +}; + +// Utility class for accumulating command-line arguments. +class Arguments { + public: + Arguments() { args_.push_back(nullptr); } + + ~Arguments() { + for (std::vector::iterator i = args_.begin(); i != args_.end(); + ++i) { + free(*i); + } + } + void AddArgument(const char* argument) { + args_.insert(args_.end() - 1, posix::StrDup(argument)); + } + + template + void AddArguments(const ::std::vector& arguments) { + for (typename ::std::vector::const_iterator i = arguments.begin(); + i != arguments.end(); ++i) { + args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); + } + } + char* const* Argv() { return &args_[0]; } + + int size() { return static_cast(args_.size()) - 1; } + + private: + std::vector args_; +}; + +// Waits for the child in a death test to exit, returning its exit +// status, or 0 if no child process exists. As a side effect, sets the +// outcome data member. +int FuchsiaDeathTest::Wait() { + const int kProcessKey = 0; + const int kSocketKey = 1; + const int kExceptionKey = 2; + + if (!spawned()) return 0; + + // Create a port to wait for socket/task/exception events. + zx_status_t status_zx; + zx::port port; + status_zx = zx::port::create(0, &port); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + + // Register to wait for the child process to terminate. + status_zx = + child_process_.wait_async(port, kProcessKey, ZX_PROCESS_TERMINATED, 0); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + + // Register to wait for the socket to be readable or closed. + status_zx = stderr_socket_.wait_async( + port, kSocketKey, ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED, 0); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + + // Register to wait for an exception. + status_zx = exception_channel_.wait_async(port, kExceptionKey, + ZX_CHANNEL_READABLE, 0); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + + bool process_terminated = false; + bool socket_closed = false; + do { + zx_port_packet_t packet = {}; + status_zx = port.wait(zx::time::infinite(), &packet); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + + if (packet.key == kExceptionKey) { + // Process encountered an exception. Kill it directly rather than + // letting other handlers process the event. We will get a kProcessKey + // event when the process actually terminates. + status_zx = child_process_.kill(); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + } else if (packet.key == kProcessKey) { + // Process terminated. + GTEST_DEATH_TEST_CHECK_(ZX_PKT_IS_SIGNAL_ONE(packet.type)); + GTEST_DEATH_TEST_CHECK_(packet.signal.observed & ZX_PROCESS_TERMINATED); + process_terminated = true; + } else if (packet.key == kSocketKey) { + GTEST_DEATH_TEST_CHECK_(ZX_PKT_IS_SIGNAL_ONE(packet.type)); + if (packet.signal.observed & ZX_SOCKET_READABLE) { + // Read data from the socket. + constexpr size_t kBufferSize = 1024; + do { + size_t old_length = captured_stderr_.length(); + size_t bytes_read = 0; + captured_stderr_.resize(old_length + kBufferSize); + status_zx = + stderr_socket_.read(0, &captured_stderr_.front() + old_length, + kBufferSize, &bytes_read); + captured_stderr_.resize(old_length + bytes_read); + } while (status_zx == ZX_OK); + if (status_zx == ZX_ERR_PEER_CLOSED) { + socket_closed = true; + } else { + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_ERR_SHOULD_WAIT); + status_zx = stderr_socket_.wait_async( + port, kSocketKey, ZX_SOCKET_READABLE | ZX_SOCKET_PEER_CLOSED, 0); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + } + } else { + GTEST_DEATH_TEST_CHECK_(packet.signal.observed & ZX_SOCKET_PEER_CLOSED); + socket_closed = true; + } + } + } while (!process_terminated && !socket_closed); + + ReadAndInterpretStatusByte(); + + zx_info_process_t buffer; + status_zx = child_process_.get_info(ZX_INFO_PROCESS, &buffer, sizeof(buffer), + nullptr, nullptr); + GTEST_DEATH_TEST_CHECK_(status_zx == ZX_OK); + + GTEST_DEATH_TEST_CHECK_(buffer.flags & ZX_INFO_PROCESS_FLAG_EXITED); + set_status(static_cast(buffer.return_code)); + return status(); +} + +// The AssumeRole process for a Fuchsia death test. It creates a child +// process with the same executable as the current process to run the +// death test. The child process is given the --gtest_filter and +// --gtest_internal_run_death_test flags such that it knows to run the +// current death test only. +DeathTest::TestRole FuchsiaDeathTest::AssumeRole() { + const UnitTestImpl* const impl = GetUnitTestImpl(); + const InternalRunDeathTestFlag* const flag = + impl->internal_run_death_test_flag(); + const TestInfo* const info = impl->current_test_info(); + const int death_test_index = info->result()->death_test_count(); + + if (flag != nullptr) { + // ParseInternalRunDeathTestFlag() has performed all the necessary + // processing. + set_write_fd(kFuchsiaReadPipeFd); + return EXECUTE_TEST; + } + + // Flush the log buffers since the log streams are shared with the child. + FlushInfoLog(); + + // Build the child process command line. + const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + + "filter=" + info->test_suite_name() + "." + + info->name(); + const std::string internal_flag = std::string("--") + GTEST_FLAG_PREFIX_ + + kInternalRunDeathTestFlag + "=" + file_ + + "|" + StreamableToString(line_) + "|" + + StreamableToString(death_test_index); + Arguments args; + args.AddArguments(GetInjectableArgvs()); + args.AddArgument(filter_flag.c_str()); + args.AddArgument(internal_flag.c_str()); + + // Build the pipe for communication with the child. + zx_status_t status; + zx_handle_t child_pipe_handle; + int child_pipe_fd; + status = fdio_pipe_half(&child_pipe_fd, &child_pipe_handle); + GTEST_DEATH_TEST_CHECK_(status == ZX_OK); + set_read_fd(child_pipe_fd); + + // Set the pipe handle for the child. + fdio_spawn_action_t spawn_actions[2] = {}; + fdio_spawn_action_t* add_handle_action = &spawn_actions[0]; + add_handle_action->action = FDIO_SPAWN_ACTION_ADD_HANDLE; + add_handle_action->h.id = PA_HND(PA_FD, kFuchsiaReadPipeFd); + add_handle_action->h.handle = child_pipe_handle; + + // Create a socket pair will be used to receive the child process' stderr. + zx::socket stderr_producer_socket; + status = zx::socket::create(0, &stderr_producer_socket, &stderr_socket_); + GTEST_DEATH_TEST_CHECK_(status >= 0); + int stderr_producer_fd = -1; + status = + fdio_fd_create(stderr_producer_socket.release(), &stderr_producer_fd); + GTEST_DEATH_TEST_CHECK_(status >= 0); + + // Make the stderr socket nonblocking. + GTEST_DEATH_TEST_CHECK_(fcntl(stderr_producer_fd, F_SETFL, 0) == 0); + + fdio_spawn_action_t* add_stderr_action = &spawn_actions[1]; + add_stderr_action->action = FDIO_SPAWN_ACTION_CLONE_FD; + add_stderr_action->fd.local_fd = stderr_producer_fd; + add_stderr_action->fd.target_fd = STDERR_FILENO; + + // Create a child job. + zx_handle_t child_job = ZX_HANDLE_INVALID; + status = zx_job_create(zx_job_default(), 0, &child_job); + GTEST_DEATH_TEST_CHECK_(status == ZX_OK); + zx_policy_basic_t policy; + policy.condition = ZX_POL_NEW_ANY; + policy.policy = ZX_POL_ACTION_ALLOW; + status = zx_job_set_policy(child_job, ZX_JOB_POL_RELATIVE, ZX_JOB_POL_BASIC, + &policy, 1); + GTEST_DEATH_TEST_CHECK_(status == ZX_OK); + + // Create an exception channel attached to the |child_job|, to allow + // us to suppress the system default exception handler from firing. + status = zx_task_create_exception_channel( + child_job, 0, exception_channel_.reset_and_get_address()); + GTEST_DEATH_TEST_CHECK_(status == ZX_OK); + + // Spawn the child process. + status = fdio_spawn_etc(child_job, FDIO_SPAWN_CLONE_ALL, args.Argv()[0], + args.Argv(), nullptr, 2, spawn_actions, + child_process_.reset_and_get_address(), nullptr); + GTEST_DEATH_TEST_CHECK_(status == ZX_OK); + + set_spawned(true); + return OVERSEE_TEST; +} + +std::string FuchsiaDeathTest::GetErrorLogs() { return captured_stderr_; } + +#else // We are neither on Windows, nor on Fuchsia. + +// ForkingDeathTest provides implementations for most of the abstract +// methods of the DeathTest interface. Only the AssumeRole method is +// left undefined. +class ForkingDeathTest : public DeathTestImpl { + public: + ForkingDeathTest(const char* statement, Matcher matcher); + + // All of these virtual functions are inherited from DeathTest. + int Wait() override; + + protected: + void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; } + + private: + // PID of child process during death test; 0 in the child process itself. + pid_t child_pid_; +}; + +// Constructs a ForkingDeathTest. +ForkingDeathTest::ForkingDeathTest(const char* a_statement, + Matcher matcher) + : DeathTestImpl(a_statement, std::move(matcher)), child_pid_(-1) {} + +// Waits for the child in a death test to exit, returning its exit +// status, or 0 if no child process exists. As a side effect, sets the +// outcome data member. +int ForkingDeathTest::Wait() { + if (!spawned()) return 0; + + ReadAndInterpretStatusByte(); + + int status_value; + GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0)); + set_status(status_value); + return status_value; +} + +// A concrete death test class that forks, then immediately runs the test +// in the child process. +class NoExecDeathTest : public ForkingDeathTest { + public: + NoExecDeathTest(const char* a_statement, Matcher matcher) + : ForkingDeathTest(a_statement, std::move(matcher)) {} + TestRole AssumeRole() override; +}; + +// The AssumeRole process for a fork-and-run death test. It implements a +// straightforward fork, with a simple pipe to transmit the status byte. +DeathTest::TestRole NoExecDeathTest::AssumeRole() { + const size_t thread_count = GetThreadCount(); + if (thread_count != 1) { + GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count); + } + + int pipe_fd[2]; + GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); + + DeathTest::set_last_death_test_message(""); + CaptureStderr(); + // When we fork the process below, the log file buffers are copied, but the + // file descriptors are shared. We flush all log files here so that closing + // the file descriptors in the child process doesn't throw off the + // synchronization between descriptors and buffers in the parent process. + // This is as close to the fork as possible to avoid a race condition in case + // there are multiple threads running before the death test, and another + // thread writes to the log file. + FlushInfoLog(); + + const pid_t child_pid = fork(); + GTEST_DEATH_TEST_CHECK_(child_pid != -1); + set_child_pid(child_pid); + if (child_pid == 0) { + GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0])); + set_write_fd(pipe_fd[1]); + // Redirects all logging to stderr in the child process to prevent + // concurrent writes to the log files. We capture stderr in the parent + // process and append the child process' output to a log. + LogToStderr(); + // Event forwarding to the listeners of event listener API mush be shut + // down in death test subprocesses. + GetUnitTestImpl()->listeners()->SuppressEventForwarding(); + g_in_fast_death_test_child = true; + return EXECUTE_TEST; + } else { + GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); + set_read_fd(pipe_fd[0]); + set_spawned(true); + return OVERSEE_TEST; + } +} + +// A concrete death test class that forks and re-executes the main +// program from the beginning, with command-line flags set that cause +// only this specific death test to be run. +class ExecDeathTest : public ForkingDeathTest { + public: + ExecDeathTest(const char* a_statement, Matcher matcher, + const char* file, int line) + : ForkingDeathTest(a_statement, std::move(matcher)), + file_(file), + line_(line) {} + TestRole AssumeRole() override; + + private: + static ::std::vector GetArgvsForDeathTestChildProcess() { + ::std::vector args = GetInjectableArgvs(); +#if defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_) + ::std::vector extra_args = + GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_(); + args.insert(args.end(), extra_args.begin(), extra_args.end()); +#endif // defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_) + return args; + } + // The name of the file in which the death test is located. + const char* const file_; + // The line number on which the death test is located. + const int line_; +}; + +// Utility class for accumulating command-line arguments. +class Arguments { + public: + Arguments() { args_.push_back(nullptr); } + + ~Arguments() { + for (std::vector::iterator i = args_.begin(); i != args_.end(); + ++i) { + free(*i); + } + } + void AddArgument(const char* argument) { + args_.insert(args_.end() - 1, posix::StrDup(argument)); + } + + template + void AddArguments(const ::std::vector& arguments) { + for (typename ::std::vector::const_iterator i = arguments.begin(); + i != arguments.end(); ++i) { + args_.insert(args_.end() - 1, posix::StrDup(i->c_str())); + } + } + char* const* Argv() { return &args_[0]; } + + private: + std::vector args_; +}; + +// A struct that encompasses the arguments to the child process of a +// threadsafe-style death test process. +struct ExecDeathTestArgs { + char* const* argv; // Command-line arguments for the child's call to exec + int close_fd; // File descriptor to close; the read end of a pipe +}; + +#if GTEST_OS_QNX +extern "C" char** environ; +#else // GTEST_OS_QNX +// The main function for a threadsafe-style death test child process. +// This function is called in a clone()-ed process and thus must avoid +// any potentially unsafe operations like malloc or libc functions. +static int ExecDeathTestChildMain(void* child_arg) { + ExecDeathTestArgs* const args = static_cast(child_arg); + GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd)); + + // We need to execute the test program in the same environment where + // it was originally invoked. Therefore we change to the original + // working directory first. + const char* const original_dir = + UnitTest::GetInstance()->original_working_dir(); + // We can safely call chdir() as it's a direct system call. + if (chdir(original_dir) != 0) { + DeathTestAbort(std::string("chdir(\"") + original_dir + + "\") failed: " + GetLastErrnoDescription()); + return EXIT_FAILURE; + } + + // We can safely call execv() as it's almost a direct system call. We + // cannot use execvp() as it's a libc function and thus potentially + // unsafe. Since execv() doesn't search the PATH, the user must + // invoke the test program via a valid path that contains at least + // one path separator. + execv(args->argv[0], args->argv); + DeathTestAbort(std::string("execv(") + args->argv[0] + ", ...) in " + + original_dir + " failed: " + GetLastErrnoDescription()); + return EXIT_FAILURE; +} +#endif // GTEST_OS_QNX + +#if GTEST_HAS_CLONE +// Two utility routines that together determine the direction the stack +// grows. +// This could be accomplished more elegantly by a single recursive +// function, but we want to guard against the unlikely possibility of +// a smart compiler optimizing the recursion away. +// +// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining +// StackLowerThanAddress into StackGrowsDown, which then doesn't give +// correct answer. +static void StackLowerThanAddress(const void* ptr, + bool* result) GTEST_NO_INLINE_; +// Make sure sanitizers do not tamper with the stack here. +// Ideally, we want to use `__builtin_frame_address` instead of a local variable +// address with sanitizer disabled, but it does not work when the +// compiler optimizes the stack frame out, which happens on PowerPC targets. +// HWAddressSanitizer add a random tag to the MSB of the local variable address, +// making comparison result unpredictable. +GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ +GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ +static void StackLowerThanAddress(const void* ptr, bool* result) { + int dummy = 0; + *result = std::less()(&dummy, ptr); +} + +// Make sure AddressSanitizer does not tamper with the stack here. +GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ +GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ +static bool StackGrowsDown() { + int dummy = 0; + bool result; + StackLowerThanAddress(&dummy, &result); + return result; +} +#endif // GTEST_HAS_CLONE + +// Spawns a child process with the same executable as the current process in +// a thread-safe manner and instructs it to run the death test. The +// implementation uses fork(2) + exec. On systems where clone(2) is +// available, it is used instead, being slightly more thread-safe. On QNX, +// fork supports only single-threaded environments, so this function uses +// spawn(2) there instead. The function dies with an error message if +// anything goes wrong. +static pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) { + ExecDeathTestArgs args = {argv, close_fd}; + pid_t child_pid = -1; + +#if GTEST_OS_QNX + // Obtains the current directory and sets it to be closed in the child + // process. + const int cwd_fd = open(".", O_RDONLY); + GTEST_DEATH_TEST_CHECK_(cwd_fd != -1); + GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC)); + // We need to execute the test program in the same environment where + // it was originally invoked. Therefore we change to the original + // working directory first. + const char* const original_dir = + UnitTest::GetInstance()->original_working_dir(); + // We can safely call chdir() as it's a direct system call. + if (chdir(original_dir) != 0) { + DeathTestAbort(std::string("chdir(\"") + original_dir + + "\") failed: " + GetLastErrnoDescription()); + return EXIT_FAILURE; + } + + int fd_flags; + // Set close_fd to be closed after spawn. + GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD)); + GTEST_DEATH_TEST_CHECK_SYSCALL_( + fcntl(close_fd, F_SETFD, fd_flags | FD_CLOEXEC)); + struct inheritance inherit = {0}; + // spawn is a system call. + child_pid = spawn(args.argv[0], 0, nullptr, &inherit, args.argv, environ); + // Restores the current working directory. + GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1); + GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd)); + +#else // GTEST_OS_QNX +#if GTEST_OS_LINUX + // When a SIGPROF signal is received while fork() or clone() are executing, + // the process may hang. To avoid this, we ignore SIGPROF here and re-enable + // it after the call to fork()/clone() is complete. + struct sigaction saved_sigprof_action; + struct sigaction ignore_sigprof_action; + memset(&ignore_sigprof_action, 0, sizeof(ignore_sigprof_action)); + sigemptyset(&ignore_sigprof_action.sa_mask); + ignore_sigprof_action.sa_handler = SIG_IGN; + GTEST_DEATH_TEST_CHECK_SYSCALL_( + sigaction(SIGPROF, &ignore_sigprof_action, &saved_sigprof_action)); +#endif // GTEST_OS_LINUX + +#if GTEST_HAS_CLONE + const bool use_fork = GTEST_FLAG_GET(death_test_use_fork); + + if (!use_fork) { + static const bool stack_grows_down = StackGrowsDown(); + const auto stack_size = static_cast(getpagesize() * 2); + // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead. + void* const stack = mmap(nullptr, stack_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED); + + // Maximum stack alignment in bytes: For a downward-growing stack, this + // amount is subtracted from size of the stack space to get an address + // that is within the stack space and is aligned on all systems we care + // about. As far as I know there is no ABI with stack alignment greater + // than 64. We assume stack and stack_size already have alignment of + // kMaxStackAlignment. + const size_t kMaxStackAlignment = 64; + void* const stack_top = + static_cast(stack) + + (stack_grows_down ? stack_size - kMaxStackAlignment : 0); + GTEST_DEATH_TEST_CHECK_( + static_cast(stack_size) > kMaxStackAlignment && + reinterpret_cast(stack_top) % kMaxStackAlignment == 0); + + child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args); + + GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1); + } +#else + const bool use_fork = true; +#endif // GTEST_HAS_CLONE + + if (use_fork && (child_pid = fork()) == 0) { + ExecDeathTestChildMain(&args); + _exit(0); + } +#endif // GTEST_OS_QNX +#if GTEST_OS_LINUX + GTEST_DEATH_TEST_CHECK_SYSCALL_( + sigaction(SIGPROF, &saved_sigprof_action, nullptr)); +#endif // GTEST_OS_LINUX + + GTEST_DEATH_TEST_CHECK_(child_pid != -1); + return child_pid; +} + +// The AssumeRole process for a fork-and-exec death test. It re-executes the +// main program from the beginning, setting the --gtest_filter +// and --gtest_internal_run_death_test flags to cause only the current +// death test to be re-run. +DeathTest::TestRole ExecDeathTest::AssumeRole() { + const UnitTestImpl* const impl = GetUnitTestImpl(); + const InternalRunDeathTestFlag* const flag = + impl->internal_run_death_test_flag(); + const TestInfo* const info = impl->current_test_info(); + const int death_test_index = info->result()->death_test_count(); + + if (flag != nullptr) { + set_write_fd(flag->write_fd()); + return EXECUTE_TEST; + } + + int pipe_fd[2]; + GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1); + // Clear the close-on-exec flag on the write end of the pipe, lest + // it be closed when the child process does an exec: + GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1); + + const std::string filter_flag = std::string("--") + GTEST_FLAG_PREFIX_ + + "filter=" + info->test_suite_name() + "." + + info->name(); + const std::string internal_flag = std::string("--") + GTEST_FLAG_PREFIX_ + + "internal_run_death_test=" + file_ + "|" + + StreamableToString(line_) + "|" + + StreamableToString(death_test_index) + "|" + + StreamableToString(pipe_fd[1]); + Arguments args; + args.AddArguments(GetArgvsForDeathTestChildProcess()); + args.AddArgument(filter_flag.c_str()); + args.AddArgument(internal_flag.c_str()); + + DeathTest::set_last_death_test_message(""); + + CaptureStderr(); + // See the comment in NoExecDeathTest::AssumeRole for why the next line + // is necessary. + FlushInfoLog(); + + const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]); + GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1])); + set_child_pid(child_pid); + set_read_fd(pipe_fd[0]); + set_spawned(true); + return OVERSEE_TEST; +} + +#endif // !GTEST_OS_WINDOWS + +// Creates a concrete DeathTest-derived class that depends on the +// --gtest_death_test_style flag, and sets the pointer pointed to +// by the "test" argument to its address. If the test should be +// skipped, sets that pointer to NULL. Returns true, unless the +// flag is set to an invalid value. +bool DefaultDeathTestFactory::Create(const char* statement, + Matcher matcher, + const char* file, int line, + DeathTest** test) { + UnitTestImpl* const impl = GetUnitTestImpl(); + const InternalRunDeathTestFlag* const flag = + impl->internal_run_death_test_flag(); + const int death_test_index = + impl->current_test_info()->increment_death_test_count(); + + if (flag != nullptr) { + if (death_test_index > flag->index()) { + DeathTest::set_last_death_test_message( + "Death test count (" + StreamableToString(death_test_index) + + ") somehow exceeded expected maximum (" + + StreamableToString(flag->index()) + ")"); + return false; + } + + if (!(flag->file() == file && flag->line() == line && + flag->index() == death_test_index)) { + *test = nullptr; + return true; + } + } + +#if GTEST_OS_WINDOWS + + if (GTEST_FLAG_GET(death_test_style) == "threadsafe" || + GTEST_FLAG_GET(death_test_style) == "fast") { + *test = new WindowsDeathTest(statement, std::move(matcher), file, line); + } + +#elif GTEST_OS_FUCHSIA + + if (GTEST_FLAG_GET(death_test_style) == "threadsafe" || + GTEST_FLAG_GET(death_test_style) == "fast") { + *test = new FuchsiaDeathTest(statement, std::move(matcher), file, line); + } + +#else + + if (GTEST_FLAG_GET(death_test_style) == "threadsafe") { + *test = new ExecDeathTest(statement, std::move(matcher), file, line); + } else if (GTEST_FLAG_GET(death_test_style) == "fast") { + *test = new NoExecDeathTest(statement, std::move(matcher)); + } + +#endif // GTEST_OS_WINDOWS + + else { // NOLINT - this is more readable than unbalanced brackets inside #if. + DeathTest::set_last_death_test_message("Unknown death test style \"" + + GTEST_FLAG_GET(death_test_style) + + "\" encountered"); + return false; + } + + return true; +} + +#if GTEST_OS_WINDOWS +// Recreates the pipe and event handles from the provided parameters, +// signals the event, and returns a file descriptor wrapped around the pipe +// handle. This function is called in the child process only. +static int GetStatusFileDescriptor(unsigned int parent_process_id, + size_t write_handle_as_size_t, + size_t event_handle_as_size_t) { + AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE, + FALSE, // Non-inheritable. + parent_process_id)); + if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) { + DeathTestAbort("Unable to open parent process " + + StreamableToString(parent_process_id)); + } + + GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t)); + + const HANDLE write_handle = reinterpret_cast(write_handle_as_size_t); + HANDLE dup_write_handle; + + // The newly initialized handle is accessible only in the parent + // process. To obtain one accessible within the child, we need to use + // DuplicateHandle. + if (!::DuplicateHandle(parent_process_handle.Get(), write_handle, + ::GetCurrentProcess(), &dup_write_handle, + 0x0, // Requested privileges ignored since + // DUPLICATE_SAME_ACCESS is used. + FALSE, // Request non-inheritable handler. + DUPLICATE_SAME_ACCESS)) { + DeathTestAbort("Unable to duplicate the pipe handle " + + StreamableToString(write_handle_as_size_t) + + " from the parent process " + + StreamableToString(parent_process_id)); + } + + const HANDLE event_handle = reinterpret_cast(event_handle_as_size_t); + HANDLE dup_event_handle; + + if (!::DuplicateHandle(parent_process_handle.Get(), event_handle, + ::GetCurrentProcess(), &dup_event_handle, 0x0, FALSE, + DUPLICATE_SAME_ACCESS)) { + DeathTestAbort("Unable to duplicate the event handle " + + StreamableToString(event_handle_as_size_t) + + " from the parent process " + + StreamableToString(parent_process_id)); + } + + const int write_fd = + ::_open_osfhandle(reinterpret_cast(dup_write_handle), O_APPEND); + if (write_fd == -1) { + DeathTestAbort("Unable to convert pipe handle " + + StreamableToString(write_handle_as_size_t) + + " to a file descriptor"); + } + + // Signals the parent that the write end of the pipe has been acquired + // so the parent can release its own write end. + ::SetEvent(dup_event_handle); + + return write_fd; +} +#endif // GTEST_OS_WINDOWS + +// Returns a newly created InternalRunDeathTestFlag object with fields +// initialized from the GTEST_FLAG(internal_run_death_test) flag if +// the flag is specified; otherwise returns NULL. +InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() { + if (GTEST_FLAG_GET(internal_run_death_test) == "") return nullptr; + + // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we + // can use it here. + int line = -1; + int index = -1; + ::std::vector< ::std::string> fields; + SplitString(GTEST_FLAG_GET(internal_run_death_test), '|', &fields); + int write_fd = -1; + +#if GTEST_OS_WINDOWS + + unsigned int parent_process_id = 0; + size_t write_handle_as_size_t = 0; + size_t event_handle_as_size_t = 0; + + if (fields.size() != 6 || !ParseNaturalNumber(fields[1], &line) || + !ParseNaturalNumber(fields[2], &index) || + !ParseNaturalNumber(fields[3], &parent_process_id) || + !ParseNaturalNumber(fields[4], &write_handle_as_size_t) || + !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) { + DeathTestAbort("Bad --gtest_internal_run_death_test flag: " + + GTEST_FLAG_GET(internal_run_death_test)); + } + write_fd = GetStatusFileDescriptor(parent_process_id, write_handle_as_size_t, + event_handle_as_size_t); + +#elif GTEST_OS_FUCHSIA + + if (fields.size() != 3 || !ParseNaturalNumber(fields[1], &line) || + !ParseNaturalNumber(fields[2], &index)) { + DeathTestAbort("Bad --gtest_internal_run_death_test flag: " + + GTEST_FLAG_GET(internal_run_death_test)); + } + +#else + + if (fields.size() != 4 || !ParseNaturalNumber(fields[1], &line) || + !ParseNaturalNumber(fields[2], &index) || + !ParseNaturalNumber(fields[3], &write_fd)) { + DeathTestAbort("Bad --gtest_internal_run_death_test flag: " + + GTEST_FLAG_GET(internal_run_death_test)); + } + +#endif // GTEST_OS_WINDOWS + + return new InternalRunDeathTestFlag(fields[0], line, index, write_fd); +} + +} // namespace internal + +#endif // GTEST_HAS_DEATH_TEST + +} // namespace testing diff --git a/test/gtest/src/gtest-filepath.cc b/test/gtest/src/gtest-filepath.cc new file mode 100644 index 0000000000..f6ee90cdb7 --- /dev/null +++ b/test/gtest/src/gtest-filepath.cc @@ -0,0 +1,367 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "gtest/internal/gtest-filepath.h" + +#include + +#include "gtest/gtest-message.h" +#include "gtest/internal/gtest-port.h" + +#if GTEST_OS_WINDOWS_MOBILE +#include +#elif GTEST_OS_WINDOWS +#include +#include +#else +#include + +#include // Some Linux distributions define PATH_MAX here. +#endif // GTEST_OS_WINDOWS_MOBILE + +#include "gtest/internal/gtest-string.h" + +#if GTEST_OS_WINDOWS +#define GTEST_PATH_MAX_ _MAX_PATH +#elif defined(PATH_MAX) +#define GTEST_PATH_MAX_ PATH_MAX +#elif defined(_XOPEN_PATH_MAX) +#define GTEST_PATH_MAX_ _XOPEN_PATH_MAX +#else +#define GTEST_PATH_MAX_ _POSIX_PATH_MAX +#endif // GTEST_OS_WINDOWS + +namespace testing { +namespace internal { + +#if GTEST_OS_WINDOWS +// On Windows, '\\' is the standard path separator, but many tools and the +// Windows API also accept '/' as an alternate path separator. Unless otherwise +// noted, a file path can contain either kind of path separators, or a mixture +// of them. +const char kPathSeparator = '\\'; +const char kAlternatePathSeparator = '/'; +const char kAlternatePathSeparatorString[] = "/"; +#if GTEST_OS_WINDOWS_MOBILE +// Windows CE doesn't have a current directory. You should not use +// the current directory in tests on Windows CE, but this at least +// provides a reasonable fallback. +const char kCurrentDirectoryString[] = "\\"; +// Windows CE doesn't define INVALID_FILE_ATTRIBUTES +const DWORD kInvalidFileAttributes = 0xffffffff; +#else +const char kCurrentDirectoryString[] = ".\\"; +#endif // GTEST_OS_WINDOWS_MOBILE +#else +const char kPathSeparator = '/'; +const char kCurrentDirectoryString[] = "./"; +#endif // GTEST_OS_WINDOWS + +// Returns whether the given character is a valid path separator. +static bool IsPathSeparator(char c) { +#if GTEST_HAS_ALT_PATH_SEP_ + return (c == kPathSeparator) || (c == kAlternatePathSeparator); +#else + return c == kPathSeparator; +#endif +} + +// Returns the current working directory, or "" if unsuccessful. +FilePath FilePath::GetCurrentDir() { +#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || \ + GTEST_OS_WINDOWS_RT || GTEST_OS_ESP8266 || GTEST_OS_ESP32 || \ + GTEST_OS_XTENSA + // These platforms do not have a current directory, so we just return + // something reasonable. + return FilePath(kCurrentDirectoryString); +#elif GTEST_OS_WINDOWS + char cwd[GTEST_PATH_MAX_ + 1] = {'\0'}; + return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd); +#else + char cwd[GTEST_PATH_MAX_ + 1] = {'\0'}; + char* result = getcwd(cwd, sizeof(cwd)); +#if GTEST_OS_NACL + // getcwd will likely fail in NaCl due to the sandbox, so return something + // reasonable. The user may have provided a shim implementation for getcwd, + // however, so fallback only when failure is detected. + return FilePath(result == nullptr ? kCurrentDirectoryString : cwd); +#endif // GTEST_OS_NACL + return FilePath(result == nullptr ? "" : cwd); +#endif // GTEST_OS_WINDOWS_MOBILE +} + +// Returns a copy of the FilePath with the case-insensitive extension removed. +// Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns +// FilePath("dir/file"). If a case-insensitive extension is not +// found, returns a copy of the original FilePath. +FilePath FilePath::RemoveExtension(const char* extension) const { + const std::string dot_extension = std::string(".") + extension; + if (String::EndsWithCaseInsensitive(pathname_, dot_extension)) { + return FilePath( + pathname_.substr(0, pathname_.length() - dot_extension.length())); + } + return *this; +} + +// Returns a pointer to the last occurrence of a valid path separator in +// the FilePath. On Windows, for example, both '/' and '\' are valid path +// separators. Returns NULL if no path separator was found. +const char* FilePath::FindLastPathSeparator() const { + const char* const last_sep = strrchr(c_str(), kPathSeparator); +#if GTEST_HAS_ALT_PATH_SEP_ + const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator); + // Comparing two pointers of which only one is NULL is undefined. + if (last_alt_sep != nullptr && + (last_sep == nullptr || last_alt_sep > last_sep)) { + return last_alt_sep; + } +#endif + return last_sep; +} + +// Returns a copy of the FilePath with the directory part removed. +// Example: FilePath("path/to/file").RemoveDirectoryName() returns +// FilePath("file"). If there is no directory part ("just_a_file"), it returns +// the FilePath unmodified. If there is no file part ("just_a_dir/") it +// returns an empty FilePath (""). +// On Windows platform, '\' is the path separator, otherwise it is '/'. +FilePath FilePath::RemoveDirectoryName() const { + const char* const last_sep = FindLastPathSeparator(); + return last_sep ? FilePath(last_sep + 1) : *this; +} + +// RemoveFileName returns the directory path with the filename removed. +// Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". +// If the FilePath is "a_file" or "/a_file", RemoveFileName returns +// FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does +// not have a file, like "just/a/dir/", it returns the FilePath unmodified. +// On Windows platform, '\' is the path separator, otherwise it is '/'. +FilePath FilePath::RemoveFileName() const { + const char* const last_sep = FindLastPathSeparator(); + std::string dir; + if (last_sep) { + dir = std::string(c_str(), static_cast(last_sep + 1 - c_str())); + } else { + dir = kCurrentDirectoryString; + } + return FilePath(dir); +} + +// Helper functions for naming files in a directory for xml output. + +// Given directory = "dir", base_name = "test", number = 0, +// extension = "xml", returns "dir/test.xml". If number is greater +// than zero (e.g., 12), returns "dir/test_12.xml". +// On Windows platform, uses \ as the separator rather than /. +FilePath FilePath::MakeFileName(const FilePath& directory, + const FilePath& base_name, int number, + const char* extension) { + std::string file; + if (number == 0) { + file = base_name.string() + "." + extension; + } else { + file = + base_name.string() + "_" + StreamableToString(number) + "." + extension; + } + return ConcatPaths(directory, FilePath(file)); +} + +// Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml". +// On Windows, uses \ as the separator rather than /. +FilePath FilePath::ConcatPaths(const FilePath& directory, + const FilePath& relative_path) { + if (directory.IsEmpty()) return relative_path; + const FilePath dir(directory.RemoveTrailingPathSeparator()); + return FilePath(dir.string() + kPathSeparator + relative_path.string()); +} + +// Returns true if pathname describes something findable in the file-system, +// either a file, directory, or whatever. +bool FilePath::FileOrDirectoryExists() const { +#if GTEST_OS_WINDOWS_MOBILE + LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str()); + const DWORD attributes = GetFileAttributes(unicode); + delete[] unicode; + return attributes != kInvalidFileAttributes; +#else + posix::StatStruct file_stat{}; + return posix::Stat(pathname_.c_str(), &file_stat) == 0; +#endif // GTEST_OS_WINDOWS_MOBILE +} + +// Returns true if pathname describes a directory in the file-system +// that exists. +bool FilePath::DirectoryExists() const { + bool result = false; +#if GTEST_OS_WINDOWS + // Don't strip off trailing separator if path is a root directory on + // Windows (like "C:\\"). + const FilePath& path(IsRootDirectory() ? *this + : RemoveTrailingPathSeparator()); +#else + const FilePath& path(*this); +#endif + +#if GTEST_OS_WINDOWS_MOBILE + LPCWSTR unicode = String::AnsiToUtf16(path.c_str()); + const DWORD attributes = GetFileAttributes(unicode); + delete[] unicode; + if ((attributes != kInvalidFileAttributes) && + (attributes & FILE_ATTRIBUTE_DIRECTORY)) { + result = true; + } +#else + posix::StatStruct file_stat{}; + result = + posix::Stat(path.c_str(), &file_stat) == 0 && posix::IsDir(file_stat); +#endif // GTEST_OS_WINDOWS_MOBILE + + return result; +} + +// Returns true if pathname describes a root directory. (Windows has one +// root directory per disk drive.) +bool FilePath::IsRootDirectory() const { +#if GTEST_OS_WINDOWS + return pathname_.length() == 3 && IsAbsolutePath(); +#else + return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]); +#endif +} + +// Returns true if pathname describes an absolute path. +bool FilePath::IsAbsolutePath() const { + const char* const name = pathname_.c_str(); +#if GTEST_OS_WINDOWS + return pathname_.length() >= 3 && + ((name[0] >= 'a' && name[0] <= 'z') || + (name[0] >= 'A' && name[0] <= 'Z')) && + name[1] == ':' && IsPathSeparator(name[2]); +#else + return IsPathSeparator(name[0]); +#endif +} + +// Returns a pathname for a file that does not currently exist. The pathname +// will be directory/base_name.extension or +// directory/base_name_.extension if directory/base_name.extension +// already exists. The number will be incremented until a pathname is found +// that does not already exist. +// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. +// There could be a race condition if two or more processes are calling this +// function at the same time -- they could both pick the same filename. +FilePath FilePath::GenerateUniqueFileName(const FilePath& directory, + const FilePath& base_name, + const char* extension) { + FilePath full_pathname; + int number = 0; + do { + full_pathname.Set(MakeFileName(directory, base_name, number++, extension)); + } while (full_pathname.FileOrDirectoryExists()); + return full_pathname; +} + +// Returns true if FilePath ends with a path separator, which indicates that +// it is intended to represent a directory. Returns false otherwise. +// This does NOT check that a directory (or file) actually exists. +bool FilePath::IsDirectory() const { + return !pathname_.empty() && + IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]); +} + +// Create directories so that path exists. Returns true if successful or if +// the directories already exist; returns false if unable to create directories +// for any reason. +bool FilePath::CreateDirectoriesRecursively() const { + if (!this->IsDirectory()) { + return false; + } + + if (pathname_.length() == 0 || this->DirectoryExists()) { + return true; + } + + const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName()); + return parent.CreateDirectoriesRecursively() && this->CreateFolder(); +} + +// Create the directory so that path exists. Returns true if successful or +// if the directory already exists; returns false if unable to create the +// directory for any reason, including if the parent directory does not +// exist. Not named "CreateDirectory" because that's a macro on Windows. +bool FilePath::CreateFolder() const { +#if GTEST_OS_WINDOWS_MOBILE + FilePath removed_sep(this->RemoveTrailingPathSeparator()); + LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str()); + int result = CreateDirectory(unicode, nullptr) ? 0 : -1; + delete[] unicode; +#elif GTEST_OS_WINDOWS + int result = _mkdir(pathname_.c_str()); +#elif GTEST_OS_ESP8266 || GTEST_OS_XTENSA + // do nothing + int result = 0; +#else + int result = mkdir(pathname_.c_str(), 0777); +#endif // GTEST_OS_WINDOWS_MOBILE + + if (result == -1) { + return this->DirectoryExists(); // An error is OK if the directory exists. + } + return true; // No error. +} + +// If input name has a trailing separator character, remove it and return the +// name, otherwise return the name string unmodified. +// On Windows platform, uses \ as the separator, other platforms use /. +FilePath FilePath::RemoveTrailingPathSeparator() const { + return IsDirectory() ? FilePath(pathname_.substr(0, pathname_.length() - 1)) + : *this; +} + +// Removes any redundant separators that might be in the pathname. +// For example, "bar///foo" becomes "bar/foo". Does not eliminate other +// redundancies that might be in a pathname involving "." or "..". +void FilePath::Normalize() { + auto out = pathname_.begin(); + + for (const char character : pathname_) { + if (!IsPathSeparator(character)) { + *(out++) = character; + } else if (out == pathname_.begin() || *std::prev(out) != kPathSeparator) { + *(out++) = kPathSeparator; + } else { + continue; + } + } + + pathname_.erase(out, pathname_.end()); +} + +} // namespace internal +} // namespace testing diff --git a/test/gtest/src/gtest-internal-inl.h b/test/gtest/src/gtest-internal-inl.h new file mode 100644 index 0000000000..0b9e929c68 --- /dev/null +++ b/test/gtest/src/gtest-internal-inl.h @@ -0,0 +1,1212 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Utility functions and classes used by the Google C++ testing framework.// +// This file contains purely Google Test's internal implementation. Please +// DO NOT #INCLUDE IT IN A USER PROGRAM. + +#ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ +#define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ + +#ifndef _WIN32_WCE +#include +#endif // !_WIN32_WCE +#include +#include // For strtoll/_strtoul64/malloc/free. +#include // For memmove. + +#include +#include +#include +#include +#include + +#include "gtest/internal/gtest-port.h" + +#if GTEST_CAN_STREAM_RESULTS_ +#include // NOLINT +#include // NOLINT +#endif + +#if GTEST_OS_WINDOWS +#include // NOLINT +#endif // GTEST_OS_WINDOWS + +#include "gtest/gtest-spi.h" +#include "gtest/gtest.h" + +GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ +/* class A needs to have dll-interface to be used by clients of class B */) + +// Declares the flags. +// +// We don't want the users to modify this flag in the code, but want +// Google Test's own unit tests to be able to access it. Therefore we +// declare it here as opposed to in gtest.h. +GTEST_DECLARE_bool_(death_test_use_fork); + +namespace testing { +namespace internal { + +// The value of GetTestTypeId() as seen from within the Google Test +// library. This is solely for testing GetTestTypeId(). +GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest; + +// A valid random seed must be in [1, kMaxRandomSeed]. +const int kMaxRandomSeed = 99999; + +// g_help_flag is true if and only if the --help flag or an equivalent form +// is specified on the command line. +GTEST_API_ extern bool g_help_flag; + +// Returns the current time in milliseconds. +GTEST_API_ TimeInMillis GetTimeInMillis(); + +// Returns true if and only if Google Test should use colors in the output. +GTEST_API_ bool ShouldUseColor(bool stdout_is_tty); + +// Formats the given time in milliseconds as seconds. +GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); + +// Converts the given time in milliseconds to a date string in the ISO 8601 +// format, without the timezone information. N.B.: due to the use the +// non-reentrant localtime() function, this function is not thread safe. Do +// not use it in any code that can be called from multiple threads. +GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms); + +// Parses a string for an Int32 flag, in the form of "--flag=value". +// +// On success, stores the value of the flag in *value, and returns +// true. On failure, returns false without changing *value. +GTEST_API_ bool ParseFlag(const char* str, const char* flag, int32_t* value); + +// Returns a random seed in range [1, kMaxRandomSeed] based on the +// given --gtest_random_seed flag value. +inline int GetRandomSeedFromFlag(int32_t random_seed_flag) { + const unsigned int raw_seed = + (random_seed_flag == 0) ? static_cast(GetTimeInMillis()) + : static_cast(random_seed_flag); + + // Normalizes the actual seed to range [1, kMaxRandomSeed] such that + // it's easy to type. + const int normalized_seed = + static_cast((raw_seed - 1U) % + static_cast(kMaxRandomSeed)) + + 1; + return normalized_seed; +} + +// Returns the first valid random seed after 'seed'. The behavior is +// undefined if 'seed' is invalid. The seed after kMaxRandomSeed is +// considered to be 1. +inline int GetNextRandomSeed(int seed) { + GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed) + << "Invalid random seed " << seed << " - must be in [1, " + << kMaxRandomSeed << "]."; + const int next_seed = seed + 1; + return (next_seed > kMaxRandomSeed) ? 1 : next_seed; +} + +// This class saves the values of all Google Test flags in its c'tor, and +// restores them in its d'tor. +class GTestFlagSaver { + public: + // The c'tor. + GTestFlagSaver() { + also_run_disabled_tests_ = GTEST_FLAG_GET(also_run_disabled_tests); + break_on_failure_ = GTEST_FLAG_GET(break_on_failure); + catch_exceptions_ = GTEST_FLAG_GET(catch_exceptions); + color_ = GTEST_FLAG_GET(color); + death_test_style_ = GTEST_FLAG_GET(death_test_style); + death_test_use_fork_ = GTEST_FLAG_GET(death_test_use_fork); + fail_fast_ = GTEST_FLAG_GET(fail_fast); + filter_ = GTEST_FLAG_GET(filter); + internal_run_death_test_ = GTEST_FLAG_GET(internal_run_death_test); + list_tests_ = GTEST_FLAG_GET(list_tests); + output_ = GTEST_FLAG_GET(output); + brief_ = GTEST_FLAG_GET(brief); + print_time_ = GTEST_FLAG_GET(print_time); + print_utf8_ = GTEST_FLAG_GET(print_utf8); + random_seed_ = GTEST_FLAG_GET(random_seed); + repeat_ = GTEST_FLAG_GET(repeat); + recreate_environments_when_repeating_ = + GTEST_FLAG_GET(recreate_environments_when_repeating); + shuffle_ = GTEST_FLAG_GET(shuffle); + stack_trace_depth_ = GTEST_FLAG_GET(stack_trace_depth); + stream_result_to_ = GTEST_FLAG_GET(stream_result_to); + throw_on_failure_ = GTEST_FLAG_GET(throw_on_failure); + } + + // The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS. + ~GTestFlagSaver() { + GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_); + GTEST_FLAG_SET(break_on_failure, break_on_failure_); + GTEST_FLAG_SET(catch_exceptions, catch_exceptions_); + GTEST_FLAG_SET(color, color_); + GTEST_FLAG_SET(death_test_style, death_test_style_); + GTEST_FLAG_SET(death_test_use_fork, death_test_use_fork_); + GTEST_FLAG_SET(filter, filter_); + GTEST_FLAG_SET(fail_fast, fail_fast_); + GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_); + GTEST_FLAG_SET(list_tests, list_tests_); + GTEST_FLAG_SET(output, output_); + GTEST_FLAG_SET(brief, brief_); + GTEST_FLAG_SET(print_time, print_time_); + GTEST_FLAG_SET(print_utf8, print_utf8_); + GTEST_FLAG_SET(random_seed, random_seed_); + GTEST_FLAG_SET(repeat, repeat_); + GTEST_FLAG_SET(recreate_environments_when_repeating, + recreate_environments_when_repeating_); + GTEST_FLAG_SET(shuffle, shuffle_); + GTEST_FLAG_SET(stack_trace_depth, stack_trace_depth_); + GTEST_FLAG_SET(stream_result_to, stream_result_to_); + GTEST_FLAG_SET(throw_on_failure, throw_on_failure_); + } + + private: + // Fields for saving the original values of flags. + bool also_run_disabled_tests_; + bool break_on_failure_; + bool catch_exceptions_; + std::string color_; + std::string death_test_style_; + bool death_test_use_fork_; + bool fail_fast_; + std::string filter_; + std::string internal_run_death_test_; + bool list_tests_; + std::string output_; + bool brief_; + bool print_time_; + bool print_utf8_; + int32_t random_seed_; + int32_t repeat_; + bool recreate_environments_when_repeating_; + bool shuffle_; + int32_t stack_trace_depth_; + std::string stream_result_to_; + bool throw_on_failure_; +} GTEST_ATTRIBUTE_UNUSED_; + +// Converts a Unicode code point to a narrow string in UTF-8 encoding. +// code_point parameter is of type UInt32 because wchar_t may not be +// wide enough to contain a code point. +// If the code_point is not a valid Unicode code point +// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted +// to "(Invalid Unicode 0xXXXXXXXX)". +GTEST_API_ std::string CodePointToUtf8(uint32_t code_point); + +// Converts a wide string to a narrow string in UTF-8 encoding. +// The wide string is assumed to have the following encoding: +// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin) +// UTF-32 if sizeof(wchar_t) == 4 (on Linux) +// Parameter str points to a null-terminated wide string. +// Parameter num_chars may additionally limit the number +// of wchar_t characters processed. -1 is used when the entire string +// should be processed. +// If the string contains code points that are not valid Unicode code points +// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output +// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding +// and contains invalid UTF-16 surrogate pairs, values in those pairs +// will be encoded as individual Unicode characters from Basic Normal Plane. +GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars); + +// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file +// if the variable is present. If a file already exists at this location, this +// function will write over it. If the variable is present, but the file cannot +// be created, prints an error and exits. +void WriteToShardStatusFileIfNeeded(); + +// Checks whether sharding is enabled by examining the relevant +// environment variable values. If the variables are present, +// but inconsistent (e.g., shard_index >= total_shards), prints +// an error and exits. If in_subprocess_for_death_test, sharding is +// disabled because it must only be applied to the original test +// process. Otherwise, we could filter out death tests we intended to execute. +GTEST_API_ bool ShouldShard(const char* total_shards_str, + const char* shard_index_str, + bool in_subprocess_for_death_test); + +// Parses the environment variable var as a 32-bit integer. If it is unset, +// returns default_val. If it is not a 32-bit integer, prints an error and +// and aborts. +GTEST_API_ int32_t Int32FromEnvOrDie(const char* env_var, int32_t default_val); + +// Given the total number of shards, the shard index, and the test id, +// returns true if and only if the test should be run on this shard. The test id +// is some arbitrary but unique non-negative integer assigned to each test +// method. Assumes that 0 <= shard_index < total_shards. +GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index, + int test_id); + +// STL container utilities. + +// Returns the number of elements in the given container that satisfy +// the given predicate. +template +inline int CountIf(const Container& c, Predicate predicate) { + // Implemented as an explicit loop since std::count_if() in libCstd on + // Solaris has a non-standard signature. + int count = 0; + for (auto it = c.begin(); it != c.end(); ++it) { + if (predicate(*it)) ++count; + } + return count; +} + +// Applies a function/functor to each element in the container. +template +void ForEach(const Container& c, Functor functor) { + std::for_each(c.begin(), c.end(), functor); +} + +// Returns the i-th element of the vector, or default_value if i is not +// in range [0, v.size()). +template +inline E GetElementOr(const std::vector& v, int i, E default_value) { + return (i < 0 || i >= static_cast(v.size())) ? default_value + : v[static_cast(i)]; +} + +// Performs an in-place shuffle of a range of the vector's elements. +// 'begin' and 'end' are element indices as an STL-style range; +// i.e. [begin, end) are shuffled, where 'end' == size() means to +// shuffle to the end of the vector. +template +void ShuffleRange(internal::Random* random, int begin, int end, + std::vector* v) { + const int size = static_cast(v->size()); + GTEST_CHECK_(0 <= begin && begin <= size) + << "Invalid shuffle range start " << begin << ": must be in range [0, " + << size << "]."; + GTEST_CHECK_(begin <= end && end <= size) + << "Invalid shuffle range finish " << end << ": must be in range [" + << begin << ", " << size << "]."; + + // Fisher-Yates shuffle, from + // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle + for (int range_width = end - begin; range_width >= 2; range_width--) { + const int last_in_range = begin + range_width - 1; + const int selected = + begin + + static_cast(random->Generate(static_cast(range_width))); + std::swap((*v)[static_cast(selected)], + (*v)[static_cast(last_in_range)]); + } +} + +// Performs an in-place shuffle of the vector's elements. +template +inline void Shuffle(internal::Random* random, std::vector* v) { + ShuffleRange(random, 0, static_cast(v->size()), v); +} + +// A function for deleting an object. Handy for being used as a +// functor. +template +static void Delete(T* x) { + delete x; +} + +// A predicate that checks the key of a TestProperty against a known key. +// +// TestPropertyKeyIs is copyable. +class TestPropertyKeyIs { + public: + // Constructor. + // + // TestPropertyKeyIs has NO default constructor. + explicit TestPropertyKeyIs(const std::string& key) : key_(key) {} + + // Returns true if and only if the test name of test property matches on key_. + bool operator()(const TestProperty& test_property) const { + return test_property.key() == key_; + } + + private: + std::string key_; +}; + +// Class UnitTestOptions. +// +// This class contains functions for processing options the user +// specifies when running the tests. It has only static members. +// +// In most cases, the user can specify an option using either an +// environment variable or a command line flag. E.g. you can set the +// test filter using either GTEST_FILTER or --gtest_filter. If both +// the variable and the flag are present, the latter overrides the +// former. +class GTEST_API_ UnitTestOptions { + public: + // Functions for processing the gtest_output flag. + + // Returns the output format, or "" for normal printed output. + static std::string GetOutputFormat(); + + // Returns the absolute path of the requested output file, or the + // default (test_detail.xml in the original working directory) if + // none was explicitly specified. + static std::string GetAbsolutePathToOutputFile(); + + // Functions for processing the gtest_filter flag. + + // Returns true if and only if the user-specified filter matches the test + // suite name and the test name. + static bool FilterMatchesTest(const std::string& test_suite_name, + const std::string& test_name); + +#if GTEST_OS_WINDOWS + // Function for supporting the gtest_catch_exception flag. + + // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the + // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. + // This function is useful as an __except condition. + static int GTestShouldProcessSEH(DWORD exception_code); +#endif // GTEST_OS_WINDOWS + + // Returns true if "name" matches the ':' separated list of glob-style + // filters in "filter". + static bool MatchesFilter(const std::string& name, const char* filter); +}; + +// Returns the current application's name, removing directory path if that +// is present. Used by UnitTestOptions::GetOutputFile. +GTEST_API_ FilePath GetCurrentExecutableName(); + +// The role interface for getting the OS stack trace as a string. +class OsStackTraceGetterInterface { + public: + OsStackTraceGetterInterface() {} + virtual ~OsStackTraceGetterInterface() {} + + // Returns the current OS stack trace as an std::string. Parameters: + // + // max_depth - the maximum number of stack frames to be included + // in the trace. + // skip_count - the number of top frames to be skipped; doesn't count + // against max_depth. + virtual std::string CurrentStackTrace(int max_depth, int skip_count) = 0; + + // UponLeavingGTest() should be called immediately before Google Test calls + // user code. It saves some information about the current stack that + // CurrentStackTrace() will use to find and hide Google Test stack frames. + virtual void UponLeavingGTest() = 0; + + // This string is inserted in place of stack frames that are part of + // Google Test's implementation. + static const char* const kElidedFramesMarker; + + private: + OsStackTraceGetterInterface(const OsStackTraceGetterInterface&) = delete; + OsStackTraceGetterInterface& operator=(const OsStackTraceGetterInterface&) = + delete; +}; + +// A working implementation of the OsStackTraceGetterInterface interface. +class OsStackTraceGetter : public OsStackTraceGetterInterface { + public: + OsStackTraceGetter() {} + + std::string CurrentStackTrace(int max_depth, int skip_count) override; + void UponLeavingGTest() override; + + private: +#if GTEST_HAS_ABSL + Mutex mutex_; // Protects all internal state. + + // We save the stack frame below the frame that calls user code. + // We do this because the address of the frame immediately below + // the user code changes between the call to UponLeavingGTest() + // and any calls to the stack trace code from within the user code. + void* caller_frame_ = nullptr; +#endif // GTEST_HAS_ABSL + + OsStackTraceGetter(const OsStackTraceGetter&) = delete; + OsStackTraceGetter& operator=(const OsStackTraceGetter&) = delete; +}; + +// Information about a Google Test trace point. +struct TraceInfo { + const char* file; + int line; + std::string message; +}; + +// This is the default global test part result reporter used in UnitTestImpl. +// This class should only be used by UnitTestImpl. +class DefaultGlobalTestPartResultReporter + : public TestPartResultReporterInterface { + public: + explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test); + // Implements the TestPartResultReporterInterface. Reports the test part + // result in the current test. + void ReportTestPartResult(const TestPartResult& result) override; + + private: + UnitTestImpl* const unit_test_; + + DefaultGlobalTestPartResultReporter( + const DefaultGlobalTestPartResultReporter&) = delete; + DefaultGlobalTestPartResultReporter& operator=( + const DefaultGlobalTestPartResultReporter&) = delete; +}; + +// This is the default per thread test part result reporter used in +// UnitTestImpl. This class should only be used by UnitTestImpl. +class DefaultPerThreadTestPartResultReporter + : public TestPartResultReporterInterface { + public: + explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test); + // Implements the TestPartResultReporterInterface. The implementation just + // delegates to the current global test part result reporter of *unit_test_. + void ReportTestPartResult(const TestPartResult& result) override; + + private: + UnitTestImpl* const unit_test_; + + DefaultPerThreadTestPartResultReporter( + const DefaultPerThreadTestPartResultReporter&) = delete; + DefaultPerThreadTestPartResultReporter& operator=( + const DefaultPerThreadTestPartResultReporter&) = delete; +}; + +// The private implementation of the UnitTest class. We don't protect +// the methods under a mutex, as this class is not accessible by a +// user and the UnitTest class that delegates work to this class does +// proper locking. +class GTEST_API_ UnitTestImpl { + public: + explicit UnitTestImpl(UnitTest* parent); + virtual ~UnitTestImpl(); + + // There are two different ways to register your own TestPartResultReporter. + // You can register your own repoter to listen either only for test results + // from the current thread or for results from all threads. + // By default, each per-thread test result repoter just passes a new + // TestPartResult to the global test result reporter, which registers the + // test part result for the currently running test. + + // Returns the global test part result reporter. + TestPartResultReporterInterface* GetGlobalTestPartResultReporter(); + + // Sets the global test part result reporter. + void SetGlobalTestPartResultReporter( + TestPartResultReporterInterface* reporter); + + // Returns the test part result reporter for the current thread. + TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread(); + + // Sets the test part result reporter for the current thread. + void SetTestPartResultReporterForCurrentThread( + TestPartResultReporterInterface* reporter); + + // Gets the number of successful test suites. + int successful_test_suite_count() const; + + // Gets the number of failed test suites. + int failed_test_suite_count() const; + + // Gets the number of all test suites. + int total_test_suite_count() const; + + // Gets the number of all test suites that contain at least one test + // that should run. + int test_suite_to_run_count() const; + + // Gets the number of successful tests. + int successful_test_count() const; + + // Gets the number of skipped tests. + int skipped_test_count() const; + + // Gets the number of failed tests. + int failed_test_count() const; + + // Gets the number of disabled tests that will be reported in the XML report. + int reportable_disabled_test_count() const; + + // Gets the number of disabled tests. + int disabled_test_count() const; + + // Gets the number of tests to be printed in the XML report. + int reportable_test_count() const; + + // Gets the number of all tests. + int total_test_count() const; + + // Gets the number of tests that should run. + int test_to_run_count() const; + + // Gets the time of the test program start, in ms from the start of the + // UNIX epoch. + TimeInMillis start_timestamp() const { return start_timestamp_; } + + // Gets the elapsed time, in milliseconds. + TimeInMillis elapsed_time() const { return elapsed_time_; } + + // Returns true if and only if the unit test passed (i.e. all test suites + // passed). + bool Passed() const { return !Failed(); } + + // Returns true if and only if the unit test failed (i.e. some test suite + // failed or something outside of all tests failed). + bool Failed() const { + return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed(); + } + + // Gets the i-th test suite among all the test suites. i can range from 0 to + // total_test_suite_count() - 1. If i is not in that range, returns NULL. + const TestSuite* GetTestSuite(int i) const { + const int index = GetElementOr(test_suite_indices_, i, -1); + return index < 0 ? nullptr : test_suites_[static_cast(i)]; + } + + // Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + const TestCase* GetTestCase(int i) const { return GetTestSuite(i); } +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Gets the i-th test suite among all the test suites. i can range from 0 to + // total_test_suite_count() - 1. If i is not in that range, returns NULL. + TestSuite* GetMutableSuiteCase(int i) { + const int index = GetElementOr(test_suite_indices_, i, -1); + return index < 0 ? nullptr : test_suites_[static_cast(index)]; + } + + // Provides access to the event listener list. + TestEventListeners* listeners() { return &listeners_; } + + // Returns the TestResult for the test that's currently running, or + // the TestResult for the ad hoc test if no test is running. + TestResult* current_test_result(); + + // Returns the TestResult for the ad hoc test. + const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; } + + // Sets the OS stack trace getter. + // + // Does nothing if the input and the current OS stack trace getter + // are the same; otherwise, deletes the old getter and makes the + // input the current getter. + void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter); + + // Returns the current OS stack trace getter if it is not NULL; + // otherwise, creates an OsStackTraceGetter, makes it the current + // getter, and returns it. + OsStackTraceGetterInterface* os_stack_trace_getter(); + + // Returns the current OS stack trace as an std::string. + // + // The maximum number of stack frames to be included is specified by + // the gtest_stack_trace_depth flag. The skip_count parameter + // specifies the number of top frames to be skipped, which doesn't + // count against the number of frames to be included. + // + // For example, if Foo() calls Bar(), which in turn calls + // CurrentOsStackTraceExceptTop(1), Foo() will be included in the + // trace but Bar() and CurrentOsStackTraceExceptTop() won't. + std::string CurrentOsStackTraceExceptTop(int skip_count) + GTEST_NO_INLINE_ GTEST_NO_TAIL_CALL_; + + // Finds and returns a TestSuite with the given name. If one doesn't + // exist, creates one and returns it. + // + // Arguments: + // + // test_suite_name: name of the test suite + // type_param: the name of the test's type parameter, or NULL if + // this is not a typed or a type-parameterized test. + // set_up_tc: pointer to the function that sets up the test suite + // tear_down_tc: pointer to the function that tears down the test suite + TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param, + internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc); + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + TestCase* GetTestCase(const char* test_case_name, const char* type_param, + internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc) { + return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc); + } +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + // Adds a TestInfo to the unit test. + // + // Arguments: + // + // set_up_tc: pointer to the function that sets up the test suite + // tear_down_tc: pointer to the function that tears down the test suite + // test_info: the TestInfo object + void AddTestInfo(internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc, + TestInfo* test_info) { +#if GTEST_HAS_DEATH_TEST + // In order to support thread-safe death tests, we need to + // remember the original working directory when the test program + // was first invoked. We cannot do this in RUN_ALL_TESTS(), as + // the user may have changed the current directory before calling + // RUN_ALL_TESTS(). Therefore we capture the current directory in + // AddTestInfo(), which is called to register a TEST or TEST_F + // before main() is reached. + if (original_working_dir_.IsEmpty()) { + original_working_dir_.Set(FilePath::GetCurrentDir()); + GTEST_CHECK_(!original_working_dir_.IsEmpty()) + << "Failed to get the current working directory."; + } +#endif // GTEST_HAS_DEATH_TEST + + GetTestSuite(test_info->test_suite_name(), test_info->type_param(), + set_up_tc, tear_down_tc) + ->AddTestInfo(test_info); + } + + // Returns ParameterizedTestSuiteRegistry object used to keep track of + // value-parameterized tests and instantiate and register them. + internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() { + return parameterized_test_registry_; + } + + std::set* ignored_parameterized_test_suites() { + return &ignored_parameterized_test_suites_; + } + + // Returns TypeParameterizedTestSuiteRegistry object used to keep track of + // type-parameterized tests and instantiations of them. + internal::TypeParameterizedTestSuiteRegistry& + type_parameterized_test_registry() { + return type_parameterized_test_registry_; + } + + // Sets the TestSuite object for the test that's currently running. + void set_current_test_suite(TestSuite* a_current_test_suite) { + current_test_suite_ = a_current_test_suite; + } + + // Sets the TestInfo object for the test that's currently running. If + // current_test_info is NULL, the assertion results will be stored in + // ad_hoc_test_result_. + void set_current_test_info(TestInfo* a_current_test_info) { + current_test_info_ = a_current_test_info; + } + + // Registers all parameterized tests defined using TEST_P and + // INSTANTIATE_TEST_SUITE_P, creating regular tests for each test/parameter + // combination. This method can be called more then once; it has guards + // protecting from registering the tests more then once. If + // value-parameterized tests are disabled, RegisterParameterizedTests is + // present but does nothing. + void RegisterParameterizedTests(); + + // Runs all tests in this UnitTest object, prints the result, and + // returns true if all tests are successful. If any exception is + // thrown during a test, this test is considered to be failed, but + // the rest of the tests will still be run. + bool RunAllTests(); + + // Clears the results of all tests, except the ad hoc tests. + void ClearNonAdHocTestResult() { + ForEach(test_suites_, TestSuite::ClearTestSuiteResult); + } + + // Clears the results of ad-hoc test assertions. + void ClearAdHocTestResult() { ad_hoc_test_result_.Clear(); } + + // Adds a TestProperty to the current TestResult object when invoked in a + // context of a test or a test suite, or to the global property set. If the + // result already contains a property with the same key, the value will be + // updated. + void RecordProperty(const TestProperty& test_property); + + enum ReactionToSharding { HONOR_SHARDING_PROTOCOL, IGNORE_SHARDING_PROTOCOL }; + + // Matches the full name of each test against the user-specified + // filter to decide whether the test should run, then records the + // result in each TestSuite and TestInfo object. + // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests + // based on sharding variables in the environment. + // Returns the number of tests that should run. + int FilterTests(ReactionToSharding shard_tests); + + // Prints the names of the tests matching the user-specified filter flag. + void ListTestsMatchingFilter(); + + const TestSuite* current_test_suite() const { return current_test_suite_; } + TestInfo* current_test_info() { return current_test_info_; } + const TestInfo* current_test_info() const { return current_test_info_; } + + // Returns the vector of environments that need to be set-up/torn-down + // before/after the tests are run. + std::vector& environments() { return environments_; } + + // Getters for the per-thread Google Test trace stack. + std::vector& gtest_trace_stack() { + return *(gtest_trace_stack_.pointer()); + } + const std::vector& gtest_trace_stack() const { + return gtest_trace_stack_.get(); + } + +#if GTEST_HAS_DEATH_TEST + void InitDeathTestSubprocessControlInfo() { + internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag()); + } + // Returns a pointer to the parsed --gtest_internal_run_death_test + // flag, or NULL if that flag was not specified. + // This information is useful only in a death test child process. + // Must not be called before a call to InitGoogleTest. + const InternalRunDeathTestFlag* internal_run_death_test_flag() const { + return internal_run_death_test_flag_.get(); + } + + // Returns a pointer to the current death test factory. + internal::DeathTestFactory* death_test_factory() { + return death_test_factory_.get(); + } + + void SuppressTestEventsIfInSubprocess(); + + friend class ReplaceDeathTestFactory; +#endif // GTEST_HAS_DEATH_TEST + + // Initializes the event listener performing XML output as specified by + // UnitTestOptions. Must not be called before InitGoogleTest. + void ConfigureXmlOutput(); + +#if GTEST_CAN_STREAM_RESULTS_ + // Initializes the event listener for streaming test results to a socket. + // Must not be called before InitGoogleTest. + void ConfigureStreamingOutput(); +#endif + + // Performs initialization dependent upon flag values obtained in + // ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to + // ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest + // this function is also called from RunAllTests. Since this function can be + // called more than once, it has to be idempotent. + void PostFlagParsingInit(); + + // Gets the random seed used at the start of the current test iteration. + int random_seed() const { return random_seed_; } + + // Gets the random number generator. + internal::Random* random() { return &random_; } + + // Shuffles all test suites, and the tests within each test suite, + // making sure that death tests are still run first. + void ShuffleTests(); + + // Restores the test suites and tests to their order before the first shuffle. + void UnshuffleTests(); + + // Returns the value of GTEST_FLAG(catch_exceptions) at the moment + // UnitTest::Run() starts. + bool catch_exceptions() const { return catch_exceptions_; } + + private: + friend class ::testing::UnitTest; + + // Used by UnitTest::Run() to capture the state of + // GTEST_FLAG(catch_exceptions) at the moment it starts. + void set_catch_exceptions(bool value) { catch_exceptions_ = value; } + + // The UnitTest object that owns this implementation object. + UnitTest* const parent_; + + // The working directory when the first TEST() or TEST_F() was + // executed. + internal::FilePath original_working_dir_; + + // The default test part result reporters. + DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_; + DefaultPerThreadTestPartResultReporter + default_per_thread_test_part_result_reporter_; + + // Points to (but doesn't own) the global test part result reporter. + TestPartResultReporterInterface* global_test_part_result_repoter_; + + // Protects read and write access to global_test_part_result_reporter_. + internal::Mutex global_test_part_result_reporter_mutex_; + + // Points to (but doesn't own) the per-thread test part result reporter. + internal::ThreadLocal + per_thread_test_part_result_reporter_; + + // The vector of environments that need to be set-up/torn-down + // before/after the tests are run. + std::vector environments_; + + // The vector of TestSuites in their original order. It owns the + // elements in the vector. + std::vector test_suites_; + + // Provides a level of indirection for the test suite list to allow + // easy shuffling and restoring the test suite order. The i-th + // element of this vector is the index of the i-th test suite in the + // shuffled order. + std::vector test_suite_indices_; + + // ParameterizedTestRegistry object used to register value-parameterized + // tests. + internal::ParameterizedTestSuiteRegistry parameterized_test_registry_; + internal::TypeParameterizedTestSuiteRegistry + type_parameterized_test_registry_; + + // The set holding the name of parameterized + // test suites that may go uninstantiated. + std::set ignored_parameterized_test_suites_; + + // Indicates whether RegisterParameterizedTests() has been called already. + bool parameterized_tests_registered_; + + // Index of the last death test suite registered. Initially -1. + int last_death_test_suite_; + + // This points to the TestSuite for the currently running test. It + // changes as Google Test goes through one test suite after another. + // When no test is running, this is set to NULL and Google Test + // stores assertion results in ad_hoc_test_result_. Initially NULL. + TestSuite* current_test_suite_; + + // This points to the TestInfo for the currently running test. It + // changes as Google Test goes through one test after another. When + // no test is running, this is set to NULL and Google Test stores + // assertion results in ad_hoc_test_result_. Initially NULL. + TestInfo* current_test_info_; + + // Normally, a user only writes assertions inside a TEST or TEST_F, + // or inside a function called by a TEST or TEST_F. Since Google + // Test keeps track of which test is current running, it can + // associate such an assertion with the test it belongs to. + // + // If an assertion is encountered when no TEST or TEST_F is running, + // Google Test attributes the assertion result to an imaginary "ad hoc" + // test, and records the result in ad_hoc_test_result_. + TestResult ad_hoc_test_result_; + + // The list of event listeners that can be used to track events inside + // Google Test. + TestEventListeners listeners_; + + // The OS stack trace getter. Will be deleted when the UnitTest + // object is destructed. By default, an OsStackTraceGetter is used, + // but the user can set this field to use a custom getter if that is + // desired. + OsStackTraceGetterInterface* os_stack_trace_getter_; + + // True if and only if PostFlagParsingInit() has been called. + bool post_flag_parse_init_performed_; + + // The random number seed used at the beginning of the test run. + int random_seed_; + + // Our random number generator. + internal::Random random_; + + // The time of the test program start, in ms from the start of the + // UNIX epoch. + TimeInMillis start_timestamp_; + + // How long the test took to run, in milliseconds. + TimeInMillis elapsed_time_; + +#if GTEST_HAS_DEATH_TEST + // The decomposed components of the gtest_internal_run_death_test flag, + // parsed when RUN_ALL_TESTS is called. + std::unique_ptr internal_run_death_test_flag_; + std::unique_ptr death_test_factory_; +#endif // GTEST_HAS_DEATH_TEST + + // A per-thread stack of traces created by the SCOPED_TRACE() macro. + internal::ThreadLocal > gtest_trace_stack_; + + // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests() + // starts. + bool catch_exceptions_; + + UnitTestImpl(const UnitTestImpl&) = delete; + UnitTestImpl& operator=(const UnitTestImpl&) = delete; +}; // class UnitTestImpl + +// Convenience function for accessing the global UnitTest +// implementation object. +inline UnitTestImpl* GetUnitTestImpl() { + return UnitTest::GetInstance()->impl(); +} + +#if GTEST_USES_SIMPLE_RE + +// Internal helper functions for implementing the simple regular +// expression matcher. +GTEST_API_ bool IsInSet(char ch, const char* str); +GTEST_API_ bool IsAsciiDigit(char ch); +GTEST_API_ bool IsAsciiPunct(char ch); +GTEST_API_ bool IsRepeat(char ch); +GTEST_API_ bool IsAsciiWhiteSpace(char ch); +GTEST_API_ bool IsAsciiWordChar(char ch); +GTEST_API_ bool IsValidEscape(char ch); +GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch); +GTEST_API_ bool ValidateRegex(const char* regex); +GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str); +GTEST_API_ bool MatchRepetitionAndRegexAtHead(bool escaped, char ch, + char repeat, const char* regex, + const char* str); +GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str); + +#endif // GTEST_USES_SIMPLE_RE + +// Parses the command line for Google Test flags, without initializing +// other parts of Google Test. +GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv); +GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv); + +#if GTEST_HAS_DEATH_TEST + +// Returns the message describing the last system error, regardless of the +// platform. +GTEST_API_ std::string GetLastErrnoDescription(); + +// Attempts to parse a string into a positive integer pointed to by the +// number parameter. Returns true if that is possible. +// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use +// it here. +template +bool ParseNaturalNumber(const ::std::string& str, Integer* number) { + // Fail fast if the given string does not begin with a digit; + // this bypasses strtoXXX's "optional leading whitespace and plus + // or minus sign" semantics, which are undesirable here. + if (str.empty() || !IsDigit(str[0])) { + return false; + } + errno = 0; + + char* end; + // BiggestConvertible is the largest integer type that system-provided + // string-to-number conversion routines can return. + using BiggestConvertible = unsigned long long; // NOLINT + + const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); // NOLINT + const bool parse_success = *end == '\0' && errno == 0; + + GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed)); + + const Integer result = static_cast(parsed); + if (parse_success && static_cast(result) == parsed) { + *number = result; + return true; + } + return false; +} +#endif // GTEST_HAS_DEATH_TEST + +// TestResult contains some private methods that should be hidden from +// Google Test user but are required for testing. This class allow our tests +// to access them. +// +// This class is supplied only for the purpose of testing Google Test's own +// constructs. Do not use it in user tests, either directly or indirectly. +class TestResultAccessor { + public: + static void RecordProperty(TestResult* test_result, + const std::string& xml_element, + const TestProperty& property) { + test_result->RecordProperty(xml_element, property); + } + + static void ClearTestPartResults(TestResult* test_result) { + test_result->ClearTestPartResults(); + } + + static const std::vector& test_part_results( + const TestResult& test_result) { + return test_result.test_part_results(); + } +}; + +#if GTEST_CAN_STREAM_RESULTS_ + +// Streams test results to the given port on the given host machine. +class StreamingListener : public EmptyTestEventListener { + public: + // Abstract base class for writing strings to a socket. + class AbstractSocketWriter { + public: + virtual ~AbstractSocketWriter() {} + + // Sends a string to the socket. + virtual void Send(const std::string& message) = 0; + + // Closes the socket. + virtual void CloseConnection() {} + + // Sends a string and a newline to the socket. + void SendLn(const std::string& message) { Send(message + "\n"); } + }; + + // Concrete class for actually writing strings to a socket. + class SocketWriter : public AbstractSocketWriter { + public: + SocketWriter(const std::string& host, const std::string& port) + : sockfd_(-1), host_name_(host), port_num_(port) { + MakeConnection(); + } + + ~SocketWriter() override { + if (sockfd_ != -1) CloseConnection(); + } + + // Sends a string to the socket. + void Send(const std::string& message) override { + GTEST_CHECK_(sockfd_ != -1) + << "Send() can be called only when there is a connection."; + + const auto len = static_cast(message.length()); + if (write(sockfd_, message.c_str(), len) != static_cast(len)) { + GTEST_LOG_(WARNING) << "stream_result_to: failed to stream to " + << host_name_ << ":" << port_num_; + } + } + + private: + // Creates a client socket and connects to the server. + void MakeConnection(); + + // Closes the socket. + void CloseConnection() override { + GTEST_CHECK_(sockfd_ != -1) + << "CloseConnection() can be called only when there is a connection."; + + close(sockfd_); + sockfd_ = -1; + } + + int sockfd_; // socket file descriptor + const std::string host_name_; + const std::string port_num_; + + SocketWriter(const SocketWriter&) = delete; + SocketWriter& operator=(const SocketWriter&) = delete; + }; // class SocketWriter + + // Escapes '=', '&', '%', and '\n' characters in str as "%xx". + static std::string UrlEncode(const char* str); + + StreamingListener(const std::string& host, const std::string& port) + : socket_writer_(new SocketWriter(host, port)) { + Start(); + } + + explicit StreamingListener(AbstractSocketWriter* socket_writer) + : socket_writer_(socket_writer) { + Start(); + } + + void OnTestProgramStart(const UnitTest& /* unit_test */) override { + SendLn("event=TestProgramStart"); + } + + void OnTestProgramEnd(const UnitTest& unit_test) override { + // Note that Google Test current only report elapsed time for each + // test iteration, not for the entire test program. + SendLn("event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed())); + + // Notify the streaming server to stop. + socket_writer_->CloseConnection(); + } + + void OnTestIterationStart(const UnitTest& /* unit_test */, + int iteration) override { + SendLn("event=TestIterationStart&iteration=" + + StreamableToString(iteration)); + } + + void OnTestIterationEnd(const UnitTest& unit_test, + int /* iteration */) override { + SendLn("event=TestIterationEnd&passed=" + FormatBool(unit_test.Passed()) + + "&elapsed_time=" + StreamableToString(unit_test.elapsed_time()) + + "ms"); + } + + // Note that "event=TestCaseStart" is a wire format and has to remain + // "case" for compatibility + void OnTestSuiteStart(const TestSuite& test_suite) override { + SendLn(std::string("event=TestCaseStart&name=") + test_suite.name()); + } + + // Note that "event=TestCaseEnd" is a wire format and has to remain + // "case" for compatibility + void OnTestSuiteEnd(const TestSuite& test_suite) override { + SendLn("event=TestCaseEnd&passed=" + FormatBool(test_suite.Passed()) + + "&elapsed_time=" + StreamableToString(test_suite.elapsed_time()) + + "ms"); + } + + void OnTestStart(const TestInfo& test_info) override { + SendLn(std::string("event=TestStart&name=") + test_info.name()); + } + + void OnTestEnd(const TestInfo& test_info) override { + SendLn("event=TestEnd&passed=" + + FormatBool((test_info.result())->Passed()) + "&elapsed_time=" + + StreamableToString((test_info.result())->elapsed_time()) + "ms"); + } + + void OnTestPartResult(const TestPartResult& test_part_result) override { + const char* file_name = test_part_result.file_name(); + if (file_name == nullptr) file_name = ""; + SendLn("event=TestPartResult&file=" + UrlEncode(file_name) + + "&line=" + StreamableToString(test_part_result.line_number()) + + "&message=" + UrlEncode(test_part_result.message())); + } + + private: + // Sends the given message and a newline to the socket. + void SendLn(const std::string& message) { socket_writer_->SendLn(message); } + + // Called at the start of streaming to notify the receiver what + // protocol we are using. + void Start() { SendLn("gtest_streaming_protocol_version=1.0"); } + + std::string FormatBool(bool value) { return value ? "1" : "0"; } + + const std::unique_ptr socket_writer_; + + StreamingListener(const StreamingListener&) = delete; + StreamingListener& operator=(const StreamingListener&) = delete; +}; // class StreamingListener + +#endif // GTEST_CAN_STREAM_RESULTS_ + +} // namespace internal +} // namespace testing + +GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 + +#endif // GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_ diff --git a/test/gtest/src/gtest-matchers.cc b/test/gtest/src/gtest-matchers.cc new file mode 100644 index 0000000000..7e3bcc0cff --- /dev/null +++ b/test/gtest/src/gtest-matchers.cc @@ -0,0 +1,98 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// The Google C++ Testing and Mocking Framework (Google Test) +// +// This file implements just enough of the matcher interface to allow +// EXPECT_DEATH and friends to accept a matcher argument. + +#include "gtest/gtest-matchers.h" + +#include + +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-port.h" + +namespace testing { + +// Constructs a matcher that matches a const std::string& whose value is +// equal to s. +Matcher::Matcher(const std::string& s) { *this = Eq(s); } + +// Constructs a matcher that matches a const std::string& whose value is +// equal to s. +Matcher::Matcher(const char* s) { + *this = Eq(std::string(s)); +} + +// Constructs a matcher that matches a std::string whose value is equal to +// s. +Matcher::Matcher(const std::string& s) { *this = Eq(s); } + +// Constructs a matcher that matches a std::string whose value is equal to +// s. +Matcher::Matcher(const char* s) { *this = Eq(std::string(s)); } + +#if GTEST_INTERNAL_HAS_STRING_VIEW +// Constructs a matcher that matches a const StringView& whose value is +// equal to s. +Matcher::Matcher(const std::string& s) { + *this = Eq(s); +} + +// Constructs a matcher that matches a const StringView& whose value is +// equal to s. +Matcher::Matcher(const char* s) { + *this = Eq(std::string(s)); +} + +// Constructs a matcher that matches a const StringView& whose value is +// equal to s. +Matcher::Matcher(internal::StringView s) { + *this = Eq(std::string(s)); +} + +// Constructs a matcher that matches a StringView whose value is equal to +// s. +Matcher::Matcher(const std::string& s) { *this = Eq(s); } + +// Constructs a matcher that matches a StringView whose value is equal to +// s. +Matcher::Matcher(const char* s) { + *this = Eq(std::string(s)); +} + +// Constructs a matcher that matches a StringView whose value is equal to +// s. +Matcher::Matcher(internal::StringView s) { + *this = Eq(std::string(s)); +} +#endif // GTEST_INTERNAL_HAS_STRING_VIEW + +} // namespace testing diff --git a/test/gtest/src/gtest-port.cc b/test/gtest/src/gtest-port.cc new file mode 100644 index 0000000000..d797fe4d58 --- /dev/null +++ b/test/gtest/src/gtest-port.cc @@ -0,0 +1,1394 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "gtest/internal/gtest-port.h" + +#include +#include +#include +#include + +#include +#include +#include + +#if GTEST_OS_WINDOWS +#include +#include +#include + +#include // Used in ThreadLocal. +#ifdef _MSC_VER +#include +#endif // _MSC_VER +#else +#include +#endif // GTEST_OS_WINDOWS + +#if GTEST_OS_MAC +#include +#include +#include +#endif // GTEST_OS_MAC + +#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ + GTEST_OS_NETBSD || GTEST_OS_OPENBSD +#include +#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD +#include +#endif +#endif + +#if GTEST_OS_QNX +#include +#include +#include +#endif // GTEST_OS_QNX + +#if GTEST_OS_AIX +#include +#include +#endif // GTEST_OS_AIX + +#if GTEST_OS_FUCHSIA +#include +#include +#endif // GTEST_OS_FUCHSIA + +#include "gtest/gtest-message.h" +#include "gtest/gtest-spi.h" +#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-string.h" +#include "src/gtest-internal-inl.h" + +namespace testing { +namespace internal { + +#if GTEST_OS_LINUX || GTEST_OS_GNU_HURD + +namespace { +template +T ReadProcFileField(const std::string& filename, int field) { + std::string dummy; + std::ifstream file(filename.c_str()); + while (field-- > 0) { + file >> dummy; + } + T output = 0; + file >> output; + return output; +} +} // namespace + +// Returns the number of active threads, or 0 when there is an error. +size_t GetThreadCount() { + const std::string filename = + (Message() << "/proc/" << getpid() << "/stat").GetString(); + return ReadProcFileField(filename, 19); +} + +#elif GTEST_OS_MAC + +size_t GetThreadCount() { + const task_t task = mach_task_self(); + mach_msg_type_number_t thread_count; + thread_act_array_t thread_list; + const kern_return_t status = task_threads(task, &thread_list, &thread_count); + if (status == KERN_SUCCESS) { + // task_threads allocates resources in thread_list and we need to free them + // to avoid leaks. + vm_deallocate(task, reinterpret_cast(thread_list), + sizeof(thread_t) * thread_count); + return static_cast(thread_count); + } else { + return 0; + } +} + +#elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \ + GTEST_OS_NETBSD + +#if GTEST_OS_NETBSD +#undef KERN_PROC +#define KERN_PROC KERN_PROC2 +#define kinfo_proc kinfo_proc2 +#endif + +#if GTEST_OS_DRAGONFLY +#define KP_NLWP(kp) (kp.kp_nthreads) +#elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD +#define KP_NLWP(kp) (kp.ki_numthreads) +#elif GTEST_OS_NETBSD +#define KP_NLWP(kp) (kp.p_nlwps) +#endif + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +size_t GetThreadCount() { + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID, + getpid(), +#if GTEST_OS_NETBSD + sizeof(struct kinfo_proc), + 1, +#endif + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + struct kinfo_proc info; + size_t size = sizeof(info); + if (sysctl(mib, miblen, &info, &size, NULL, 0)) { + return 0; + } + return static_cast(KP_NLWP(info)); +} +#elif GTEST_OS_OPENBSD + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +size_t GetThreadCount() { + int mib[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PID | KERN_PROC_SHOW_THREADS, + getpid(), + sizeof(struct kinfo_proc), + 0, + }; + u_int miblen = sizeof(mib) / sizeof(mib[0]); + + // get number of structs + size_t size; + if (sysctl(mib, miblen, NULL, &size, NULL, 0)) { + return 0; + } + + mib[5] = static_cast(size / static_cast(mib[4])); + + // populate array of structs + struct kinfo_proc info[mib[5]]; + if (sysctl(mib, miblen, &info, &size, NULL, 0)) { + return 0; + } + + // exclude empty members + size_t nthreads = 0; + for (size_t i = 0; i < size / static_cast(mib[4]); i++) { + if (info[i].p_tid != -1) nthreads++; + } + return nthreads; +} + +#elif GTEST_OS_QNX + +// Returns the number of threads running in the process, or 0 to indicate that +// we cannot detect it. +size_t GetThreadCount() { + const int fd = open("/proc/self/as", O_RDONLY); + if (fd < 0) { + return 0; + } + procfs_info process_info; + const int status = + devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), nullptr); + close(fd); + if (status == EOK) { + return static_cast(process_info.num_threads); + } else { + return 0; + } +} + +#elif GTEST_OS_AIX + +size_t GetThreadCount() { + struct procentry64 entry; + pid_t pid = getpid(); + int status = getprocs64(&entry, sizeof(entry), nullptr, 0, &pid, 1); + if (status == 1) { + return entry.pi_thcount; + } else { + return 0; + } +} + +#elif GTEST_OS_FUCHSIA + +size_t GetThreadCount() { + int dummy_buffer; + size_t avail; + zx_status_t status = + zx_object_get_info(zx_process_self(), ZX_INFO_PROCESS_THREADS, + &dummy_buffer, 0, nullptr, &avail); + if (status == ZX_OK) { + return avail; + } else { + return 0; + } +} + +#else + +size_t GetThreadCount() { + // There's no portable way to detect the number of threads, so we just + // return 0 to indicate that we cannot detect it. + return 0; +} + +#endif // GTEST_OS_LINUX + +#if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS + +AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {} + +AutoHandle::AutoHandle(Handle handle) : handle_(handle) {} + +AutoHandle::~AutoHandle() { Reset(); } + +AutoHandle::Handle AutoHandle::Get() const { return handle_; } + +void AutoHandle::Reset() { Reset(INVALID_HANDLE_VALUE); } + +void AutoHandle::Reset(HANDLE handle) { + // Resetting with the same handle we already own is invalid. + if (handle_ != handle) { + if (IsCloseable()) { + ::CloseHandle(handle_); + } + handle_ = handle; + } else { + GTEST_CHECK_(!IsCloseable()) + << "Resetting a valid handle to itself is likely a programmer error " + "and thus not allowed."; + } +} + +bool AutoHandle::IsCloseable() const { + // Different Windows APIs may use either of these values to represent an + // invalid handle. + return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE; +} + +Mutex::Mutex() + : owner_thread_id_(0), + type_(kDynamic), + critical_section_init_phase_(0), + critical_section_(new CRITICAL_SECTION) { + ::InitializeCriticalSection(critical_section_); +} + +Mutex::~Mutex() { + // Static mutexes are leaked intentionally. It is not thread-safe to try + // to clean them up. + if (type_ == kDynamic) { + ::DeleteCriticalSection(critical_section_); + delete critical_section_; + critical_section_ = nullptr; + } +} + +void Mutex::Lock() { + ThreadSafeLazyInit(); + ::EnterCriticalSection(critical_section_); + owner_thread_id_ = ::GetCurrentThreadId(); +} + +void Mutex::Unlock() { + ThreadSafeLazyInit(); + // We don't protect writing to owner_thread_id_ here, as it's the + // caller's responsibility to ensure that the current thread holds the + // mutex when this is called. + owner_thread_id_ = 0; + ::LeaveCriticalSection(critical_section_); +} + +// Does nothing if the current thread holds the mutex. Otherwise, crashes +// with high probability. +void Mutex::AssertHeld() { + ThreadSafeLazyInit(); + GTEST_CHECK_(owner_thread_id_ == ::GetCurrentThreadId()) + << "The current thread is not holding the mutex @" << this; +} + +namespace { + +#ifdef _MSC_VER +// Use the RAII idiom to flag mem allocs that are intentionally never +// deallocated. The motivation is to silence the false positive mem leaks +// that are reported by the debug version of MS's CRT which can only detect +// if an alloc is missing a matching deallocation. +// Example: +// MemoryIsNotDeallocated memory_is_not_deallocated; +// critical_section_ = new CRITICAL_SECTION; +// +class MemoryIsNotDeallocated { + public: + MemoryIsNotDeallocated() : old_crtdbg_flag_(0) { + old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); + // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT + // doesn't report mem leak if there's no matching deallocation. + (void)_CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF); + } + + ~MemoryIsNotDeallocated() { + // Restore the original _CRTDBG_ALLOC_MEM_DF flag + (void)_CrtSetDbgFlag(old_crtdbg_flag_); + } + + private: + int old_crtdbg_flag_; + + MemoryIsNotDeallocated(const MemoryIsNotDeallocated&) = delete; + MemoryIsNotDeallocated& operator=(const MemoryIsNotDeallocated&) = delete; +}; +#endif // _MSC_VER + +} // namespace + +// Initializes owner_thread_id_ and critical_section_ in static mutexes. +void Mutex::ThreadSafeLazyInit() { + // Dynamic mutexes are initialized in the constructor. + if (type_ == kStatic) { + switch ( + ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) { + case 0: + // If critical_section_init_phase_ was 0 before the exchange, we + // are the first to test it and need to perform the initialization. + owner_thread_id_ = 0; + { + // Use RAII to flag that following mem alloc is never deallocated. +#ifdef _MSC_VER + MemoryIsNotDeallocated memory_is_not_deallocated; +#endif // _MSC_VER + critical_section_ = new CRITICAL_SECTION; + } + ::InitializeCriticalSection(critical_section_); + // Updates the critical_section_init_phase_ to 2 to signal + // initialization complete. + GTEST_CHECK_(::InterlockedCompareExchange(&critical_section_init_phase_, + 2L, 1L) == 1L); + break; + case 1: + // Somebody else is already initializing the mutex; spin until they + // are done. + while (::InterlockedCompareExchange(&critical_section_init_phase_, 2L, + 2L) != 2L) { + // Possibly yields the rest of the thread's time slice to other + // threads. + ::Sleep(0); + } + break; + + case 2: + break; // The mutex is already initialized and ready for use. + + default: + GTEST_CHECK_(false) + << "Unexpected value of critical_section_init_phase_ " + << "while initializing a static mutex."; + } + } +} + +namespace { + +class ThreadWithParamSupport : public ThreadWithParamBase { + public: + static HANDLE CreateThread(Runnable* runnable, + Notification* thread_can_start) { + ThreadMainParam* param = new ThreadMainParam(runnable, thread_can_start); + DWORD thread_id; + HANDLE thread_handle = ::CreateThread( + nullptr, // Default security. + 0, // Default stack size. + &ThreadWithParamSupport::ThreadMain, + param, // Parameter to ThreadMainStatic + 0x0, // Default creation flags. + &thread_id); // Need a valid pointer for the call to work under Win98. + GTEST_CHECK_(thread_handle != nullptr) + << "CreateThread failed with error " << ::GetLastError() << "."; + if (thread_handle == nullptr) { + delete param; + } + return thread_handle; + } + + private: + struct ThreadMainParam { + ThreadMainParam(Runnable* runnable, Notification* thread_can_start) + : runnable_(runnable), thread_can_start_(thread_can_start) {} + std::unique_ptr runnable_; + // Does not own. + Notification* thread_can_start_; + }; + + static DWORD WINAPI ThreadMain(void* ptr) { + // Transfers ownership. + std::unique_ptr param(static_cast(ptr)); + if (param->thread_can_start_ != nullptr) + param->thread_can_start_->WaitForNotification(); + param->runnable_->Run(); + return 0; + } + + // Prohibit instantiation. + ThreadWithParamSupport(); + + ThreadWithParamSupport(const ThreadWithParamSupport&) = delete; + ThreadWithParamSupport& operator=(const ThreadWithParamSupport&) = delete; +}; + +} // namespace + +ThreadWithParamBase::ThreadWithParamBase(Runnable* runnable, + Notification* thread_can_start) + : thread_( + ThreadWithParamSupport::CreateThread(runnable, thread_can_start)) {} + +ThreadWithParamBase::~ThreadWithParamBase() { Join(); } + +void ThreadWithParamBase::Join() { + GTEST_CHECK_(::WaitForSingleObject(thread_.Get(), INFINITE) == WAIT_OBJECT_0) + << "Failed to join the thread with error " << ::GetLastError() << "."; +} + +// Maps a thread to a set of ThreadIdToThreadLocals that have values +// instantiated on that thread and notifies them when the thread exits. A +// ThreadLocal instance is expected to persist until all threads it has +// values on have terminated. +class ThreadLocalRegistryImpl { + public: + // Registers thread_local_instance as having value on the current thread. + // Returns a value that can be used to identify the thread from other threads. + static ThreadLocalValueHolderBase* GetValueOnCurrentThread( + const ThreadLocalBase* thread_local_instance) { +#ifdef _MSC_VER + MemoryIsNotDeallocated memory_is_not_deallocated; +#endif // _MSC_VER + DWORD current_thread = ::GetCurrentThreadId(); + MutexLock lock(&mutex_); + ThreadIdToThreadLocals* const thread_to_thread_locals = + GetThreadLocalsMapLocked(); + ThreadIdToThreadLocals::iterator thread_local_pos = + thread_to_thread_locals->find(current_thread); + if (thread_local_pos == thread_to_thread_locals->end()) { + thread_local_pos = + thread_to_thread_locals + ->insert(std::make_pair(current_thread, ThreadLocalValues())) + .first; + StartWatcherThreadFor(current_thread); + } + ThreadLocalValues& thread_local_values = thread_local_pos->second; + ThreadLocalValues::iterator value_pos = + thread_local_values.find(thread_local_instance); + if (value_pos == thread_local_values.end()) { + value_pos = + thread_local_values + .insert(std::make_pair( + thread_local_instance, + std::shared_ptr( + thread_local_instance->NewValueForCurrentThread()))) + .first; + } + return value_pos->second.get(); + } + + static void OnThreadLocalDestroyed( + const ThreadLocalBase* thread_local_instance) { + std::vector > value_holders; + // Clean up the ThreadLocalValues data structure while holding the lock, but + // defer the destruction of the ThreadLocalValueHolderBases. + { + MutexLock lock(&mutex_); + ThreadIdToThreadLocals* const thread_to_thread_locals = + GetThreadLocalsMapLocked(); + for (ThreadIdToThreadLocals::iterator it = + thread_to_thread_locals->begin(); + it != thread_to_thread_locals->end(); ++it) { + ThreadLocalValues& thread_local_values = it->second; + ThreadLocalValues::iterator value_pos = + thread_local_values.find(thread_local_instance); + if (value_pos != thread_local_values.end()) { + value_holders.push_back(value_pos->second); + thread_local_values.erase(value_pos); + // This 'if' can only be successful at most once, so theoretically we + // could break out of the loop here, but we don't bother doing so. + } + } + } + // Outside the lock, let the destructor for 'value_holders' deallocate the + // ThreadLocalValueHolderBases. + } + + static void OnThreadExit(DWORD thread_id) { + GTEST_CHECK_(thread_id != 0) << ::GetLastError(); + std::vector > value_holders; + // Clean up the ThreadIdToThreadLocals data structure while holding the + // lock, but defer the destruction of the ThreadLocalValueHolderBases. + { + MutexLock lock(&mutex_); + ThreadIdToThreadLocals* const thread_to_thread_locals = + GetThreadLocalsMapLocked(); + ThreadIdToThreadLocals::iterator thread_local_pos = + thread_to_thread_locals->find(thread_id); + if (thread_local_pos != thread_to_thread_locals->end()) { + ThreadLocalValues& thread_local_values = thread_local_pos->second; + for (ThreadLocalValues::iterator value_pos = + thread_local_values.begin(); + value_pos != thread_local_values.end(); ++value_pos) { + value_holders.push_back(value_pos->second); + } + thread_to_thread_locals->erase(thread_local_pos); + } + } + // Outside the lock, let the destructor for 'value_holders' deallocate the + // ThreadLocalValueHolderBases. + } + + private: + // In a particular thread, maps a ThreadLocal object to its value. + typedef std::map > + ThreadLocalValues; + // Stores all ThreadIdToThreadLocals having values in a thread, indexed by + // thread's ID. + typedef std::map ThreadIdToThreadLocals; + + // Holds the thread id and thread handle that we pass from + // StartWatcherThreadFor to WatcherThreadFunc. + typedef std::pair ThreadIdAndHandle; + + static void StartWatcherThreadFor(DWORD thread_id) { + // The returned handle will be kept in thread_map and closed by + // watcher_thread in WatcherThreadFunc. + HANDLE thread = + ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION, FALSE, thread_id); + GTEST_CHECK_(thread != nullptr); + // We need to pass a valid thread ID pointer into CreateThread for it + // to work correctly under Win98. + DWORD watcher_thread_id; + HANDLE watcher_thread = ::CreateThread( + nullptr, // Default security. + 0, // Default stack size + &ThreadLocalRegistryImpl::WatcherThreadFunc, + reinterpret_cast(new ThreadIdAndHandle(thread_id, thread)), + CREATE_SUSPENDED, &watcher_thread_id); + GTEST_CHECK_(watcher_thread != nullptr) + << "CreateThread failed with error " << ::GetLastError() << "."; + // Give the watcher thread the same priority as ours to avoid being + // blocked by it. + ::SetThreadPriority(watcher_thread, + ::GetThreadPriority(::GetCurrentThread())); + ::ResumeThread(watcher_thread); + ::CloseHandle(watcher_thread); + } + + // Monitors exit from a given thread and notifies those + // ThreadIdToThreadLocals about thread termination. + static DWORD WINAPI WatcherThreadFunc(LPVOID param) { + const ThreadIdAndHandle* tah = + reinterpret_cast(param); + GTEST_CHECK_(::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0); + OnThreadExit(tah->first); + ::CloseHandle(tah->second); + delete tah; + return 0; + } + + // Returns map of thread local instances. + static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() { + mutex_.AssertHeld(); +#ifdef _MSC_VER + MemoryIsNotDeallocated memory_is_not_deallocated; +#endif // _MSC_VER + static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals(); + return map; + } + + // Protects access to GetThreadLocalsMapLocked() and its return value. + static Mutex mutex_; + // Protects access to GetThreadMapLocked() and its return value. + static Mutex thread_map_mutex_; +}; + +Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT +Mutex ThreadLocalRegistryImpl::thread_map_mutex_( + Mutex::kStaticMutex); // NOLINT + +ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread( + const ThreadLocalBase* thread_local_instance) { + return ThreadLocalRegistryImpl::GetValueOnCurrentThread( + thread_local_instance); +} + +void ThreadLocalRegistry::OnThreadLocalDestroyed( + const ThreadLocalBase* thread_local_instance) { + ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance); +} + +#endif // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS + +#if GTEST_USES_POSIX_RE + +// Implements RE. Currently only needed for death tests. + +RE::~RE() { + if (is_valid_) { + // regfree'ing an invalid regex might crash because the content + // of the regex is undefined. Since the regex's are essentially + // the same, one cannot be valid (or invalid) without the other + // being so too. + regfree(&partial_regex_); + regfree(&full_regex_); + } + free(const_cast(pattern_)); +} + +// Returns true if and only if regular expression re matches the entire str. +bool RE::FullMatch(const char* str, const RE& re) { + if (!re.is_valid_) return false; + + regmatch_t match; + return regexec(&re.full_regex_, str, 1, &match, 0) == 0; +} + +// Returns true if and only if regular expression re matches a substring of +// str (including str itself). +bool RE::PartialMatch(const char* str, const RE& re) { + if (!re.is_valid_) return false; + + regmatch_t match; + return regexec(&re.partial_regex_, str, 1, &match, 0) == 0; +} + +// Initializes an RE from its string representation. +void RE::Init(const char* regex) { + pattern_ = posix::StrDup(regex); + + // Reserves enough bytes to hold the regular expression used for a + // full match. + const size_t full_regex_len = strlen(regex) + 10; + char* const full_pattern = new char[full_regex_len]; + + snprintf(full_pattern, full_regex_len, "^(%s)$", regex); + is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0; + // We want to call regcomp(&partial_regex_, ...) even if the + // previous expression returns false. Otherwise partial_regex_ may + // not be properly initialized can may cause trouble when it's + // freed. + // + // Some implementation of POSIX regex (e.g. on at least some + // versions of Cygwin) doesn't accept the empty string as a valid + // regex. We change it to an equivalent form "()" to be safe. + if (is_valid_) { + const char* const partial_regex = (*regex == '\0') ? "()" : regex; + is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0; + } + EXPECT_TRUE(is_valid_) + << "Regular expression \"" << regex + << "\" is not a valid POSIX Extended regular expression."; + + delete[] full_pattern; +} + +#elif GTEST_USES_SIMPLE_RE + +// Returns true if and only if ch appears anywhere in str (excluding the +// terminating '\0' character). +bool IsInSet(char ch, const char* str) { + return ch != '\0' && strchr(str, ch) != nullptr; +} + +// Returns true if and only if ch belongs to the given classification. +// Unlike similar functions in , these aren't affected by the +// current locale. +bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; } +bool IsAsciiPunct(char ch) { + return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"); +} +bool IsRepeat(char ch) { return IsInSet(ch, "?*+"); } +bool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); } +bool IsAsciiWordChar(char ch) { + return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || + ('0' <= ch && ch <= '9') || ch == '_'; +} + +// Returns true if and only if "\\c" is a supported escape sequence. +bool IsValidEscape(char c) { + return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW")); +} + +// Returns true if and only if the given atom (specified by escaped and +// pattern) matches ch. The result is undefined if the atom is invalid. +bool AtomMatchesChar(bool escaped, char pattern_char, char ch) { + if (escaped) { // "\\p" where p is pattern_char. + switch (pattern_char) { + case 'd': + return IsAsciiDigit(ch); + case 'D': + return !IsAsciiDigit(ch); + case 'f': + return ch == '\f'; + case 'n': + return ch == '\n'; + case 'r': + return ch == '\r'; + case 's': + return IsAsciiWhiteSpace(ch); + case 'S': + return !IsAsciiWhiteSpace(ch); + case 't': + return ch == '\t'; + case 'v': + return ch == '\v'; + case 'w': + return IsAsciiWordChar(ch); + case 'W': + return !IsAsciiWordChar(ch); + } + return IsAsciiPunct(pattern_char) && pattern_char == ch; + } + + return (pattern_char == '.' && ch != '\n') || pattern_char == ch; +} + +// Helper function used by ValidateRegex() to format error messages. +static std::string FormatRegexSyntaxError(const char* regex, int index) { + return (Message() << "Syntax error at index " << index + << " in simple regular expression \"" << regex << "\": ") + .GetString(); +} + +// Generates non-fatal failures and returns false if regex is invalid; +// otherwise returns true. +bool ValidateRegex(const char* regex) { + if (regex == nullptr) { + ADD_FAILURE() << "NULL is not a valid simple regular expression."; + return false; + } + + bool is_valid = true; + + // True if and only if ?, *, or + can follow the previous atom. + bool prev_repeatable = false; + for (int i = 0; regex[i]; i++) { + if (regex[i] == '\\') { // An escape sequence + i++; + if (regex[i] == '\0') { + ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) + << "'\\' cannot appear at the end."; + return false; + } + + if (!IsValidEscape(regex[i])) { + ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) + << "invalid escape sequence \"\\" << regex[i] << "\"."; + is_valid = false; + } + prev_repeatable = true; + } else { // Not an escape sequence. + const char ch = regex[i]; + + if (ch == '^' && i > 0) { + ADD_FAILURE() << FormatRegexSyntaxError(regex, i) + << "'^' can only appear at the beginning."; + is_valid = false; + } else if (ch == '$' && regex[i + 1] != '\0') { + ADD_FAILURE() << FormatRegexSyntaxError(regex, i) + << "'$' can only appear at the end."; + is_valid = false; + } else if (IsInSet(ch, "()[]{}|")) { + ADD_FAILURE() << FormatRegexSyntaxError(regex, i) << "'" << ch + << "' is unsupported."; + is_valid = false; + } else if (IsRepeat(ch) && !prev_repeatable) { + ADD_FAILURE() << FormatRegexSyntaxError(regex, i) << "'" << ch + << "' can only follow a repeatable token."; + is_valid = false; + } + + prev_repeatable = !IsInSet(ch, "^$?*+"); + } + } + + return is_valid; +} + +// Matches a repeated regex atom followed by a valid simple regular +// expression. The regex atom is defined as c if escaped is false, +// or \c otherwise. repeat is the repetition meta character (?, *, +// or +). The behavior is undefined if str contains too many +// characters to be indexable by size_t, in which case the test will +// probably time out anyway. We are fine with this limitation as +// std::string has it too. +bool MatchRepetitionAndRegexAtHead(bool escaped, char c, char repeat, + const char* regex, const char* str) { + const size_t min_count = (repeat == '+') ? 1 : 0; + const size_t max_count = (repeat == '?') ? 1 : static_cast(-1) - 1; + // We cannot call numeric_limits::max() as it conflicts with the + // max() macro on Windows. + + for (size_t i = 0; i <= max_count; ++i) { + // We know that the atom matches each of the first i characters in str. + if (i >= min_count && MatchRegexAtHead(regex, str + i)) { + // We have enough matches at the head, and the tail matches too. + // Since we only care about *whether* the pattern matches str + // (as opposed to *how* it matches), there is no need to find a + // greedy match. + return true; + } + if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i])) return false; + } + return false; +} + +// Returns true if and only if regex matches a prefix of str. regex must +// be a valid simple regular expression and not start with "^", or the +// result is undefined. +bool MatchRegexAtHead(const char* regex, const char* str) { + if (*regex == '\0') // An empty regex matches a prefix of anything. + return true; + + // "$" only matches the end of a string. Note that regex being + // valid guarantees that there's nothing after "$" in it. + if (*regex == '$') return *str == '\0'; + + // Is the first thing in regex an escape sequence? + const bool escaped = *regex == '\\'; + if (escaped) ++regex; + if (IsRepeat(regex[1])) { + // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so + // here's an indirect recursion. It terminates as the regex gets + // shorter in each recursion. + return MatchRepetitionAndRegexAtHead(escaped, regex[0], regex[1], regex + 2, + str); + } else { + // regex isn't empty, isn't "$", and doesn't start with a + // repetition. We match the first atom of regex with the first + // character of str and recurse. + return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) && + MatchRegexAtHead(regex + 1, str + 1); + } +} + +// Returns true if and only if regex matches any substring of str. regex must +// be a valid simple regular expression, or the result is undefined. +// +// The algorithm is recursive, but the recursion depth doesn't exceed +// the regex length, so we won't need to worry about running out of +// stack space normally. In rare cases the time complexity can be +// exponential with respect to the regex length + the string length, +// but usually it's must faster (often close to linear). +bool MatchRegexAnywhere(const char* regex, const char* str) { + if (regex == nullptr || str == nullptr) return false; + + if (*regex == '^') return MatchRegexAtHead(regex + 1, str); + + // A successful match can be anywhere in str. + do { + if (MatchRegexAtHead(regex, str)) return true; + } while (*str++ != '\0'); + return false; +} + +// Implements the RE class. + +RE::~RE() { + free(const_cast(pattern_)); + free(const_cast(full_pattern_)); +} + +// Returns true if and only if regular expression re matches the entire str. +bool RE::FullMatch(const char* str, const RE& re) { + return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str); +} + +// Returns true if and only if regular expression re matches a substring of +// str (including str itself). +bool RE::PartialMatch(const char* str, const RE& re) { + return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str); +} + +// Initializes an RE from its string representation. +void RE::Init(const char* regex) { + pattern_ = full_pattern_ = nullptr; + if (regex != nullptr) { + pattern_ = posix::StrDup(regex); + } + + is_valid_ = ValidateRegex(regex); + if (!is_valid_) { + // No need to calculate the full pattern when the regex is invalid. + return; + } + + const size_t len = strlen(regex); + // Reserves enough bytes to hold the regular expression used for a + // full match: we need space to prepend a '^', append a '$', and + // terminate the string with '\0'. + char* buffer = static_cast(malloc(len + 3)); + full_pattern_ = buffer; + + if (*regex != '^') + *buffer++ = '^'; // Makes sure full_pattern_ starts with '^'. + + // We don't use snprintf or strncpy, as they trigger a warning when + // compiled with VC++ 8.0. + memcpy(buffer, regex, len); + buffer += len; + + if (len == 0 || regex[len - 1] != '$') + *buffer++ = '$'; // Makes sure full_pattern_ ends with '$'. + + *buffer = '\0'; +} + +#endif // GTEST_USES_POSIX_RE + +const char kUnknownFile[] = "unknown file"; + +// Formats a source file path and a line number as they would appear +// in an error message from the compiler used to compile this code. +GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) { + const std::string file_name(file == nullptr ? kUnknownFile : file); + + if (line < 0) { + return file_name + ":"; + } +#ifdef _MSC_VER + return file_name + "(" + StreamableToString(line) + "):"; +#else + return file_name + ":" + StreamableToString(line) + ":"; +#endif // _MSC_VER +} + +// Formats a file location for compiler-independent XML output. +// Although this function is not platform dependent, we put it next to +// FormatFileLocation in order to contrast the two functions. +// Note that FormatCompilerIndependentFileLocation() does NOT append colon +// to the file location it produces, unlike FormatFileLocation(). +GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, + int line) { + const std::string file_name(file == nullptr ? kUnknownFile : file); + + if (line < 0) + return file_name; + else + return file_name + ":" + StreamableToString(line); +} + +GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line) + : severity_(severity) { + const char* const marker = severity == GTEST_INFO ? "[ INFO ]" + : severity == GTEST_WARNING ? "[WARNING]" + : severity == GTEST_ERROR ? "[ ERROR ]" + : "[ FATAL ]"; + GetStream() << ::std::endl + << marker << " " << FormatFileLocation(file, line).c_str() + << ": "; +} + +// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. +GTestLog::~GTestLog() { + GetStream() << ::std::endl; + if (severity_ == GTEST_FATAL) { + fflush(stderr); + posix::Abort(); + } +} + +// Disable Microsoft deprecation warnings for POSIX functions called from +// this class (creat, dup, dup2, and close) +GTEST_DISABLE_MSC_DEPRECATED_PUSH_() + +#if GTEST_HAS_STREAM_REDIRECTION + +// Object that captures an output stream (stdout/stderr). +class CapturedStream { + public: + // The ctor redirects the stream to a temporary file. + explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) { +#if GTEST_OS_WINDOWS + char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT + char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT + + ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path); + const UINT success = ::GetTempFileNameA(temp_dir_path, "gtest_redir", + 0, // Generate unique file name. + temp_file_path); + GTEST_CHECK_(success != 0) + << "Unable to create a temporary file in " << temp_dir_path; + const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE); + GTEST_CHECK_(captured_fd != -1) + << "Unable to open temporary file " << temp_file_path; + filename_ = temp_file_path; +#else + // There's no guarantee that a test has write access to the current + // directory, so we create the temporary file in a temporary directory. + std::string name_template; + +#if GTEST_OS_LINUX_ANDROID + // Note: Android applications are expected to call the framework's + // Context.getExternalStorageDirectory() method through JNI to get + // the location of the world-writable SD Card directory. However, + // this requires a Context handle, which cannot be retrieved + // globally from native code. Doing so also precludes running the + // code as part of a regular standalone executable, which doesn't + // run in a Dalvik process (e.g. when running it through 'adb shell'). + // + // The location /data/local/tmp is directly accessible from native code. + // '/sdcard' and other variants cannot be relied on, as they are not + // guaranteed to be mounted, or may have a delay in mounting. + name_template = "/data/local/tmp/"; +#elif GTEST_OS_IOS + char user_temp_dir[PATH_MAX + 1]; + + // Documented alternative to NSTemporaryDirectory() (for obtaining creating + // a temporary directory) at + // https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10 + // + // _CS_DARWIN_USER_TEMP_DIR (as well as _CS_DARWIN_USER_CACHE_DIR) is not + // documented in the confstr() man page at + // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/confstr.3.html#//apple_ref/doc/man/3/confstr + // but are still available, according to the WebKit patches at + // https://trac.webkit.org/changeset/262004/webkit + // https://trac.webkit.org/changeset/263705/webkit + // + // The confstr() implementation falls back to getenv("TMPDIR"). See + // https://opensource.apple.com/source/Libc/Libc-1439.100.3/gen/confstr.c.auto.html + ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir)); + + name_template = user_temp_dir; + if (name_template.back() != GTEST_PATH_SEP_[0]) + name_template.push_back(GTEST_PATH_SEP_[0]); +#else + name_template = "/tmp/"; +#endif + name_template.append("gtest_captured_stream.XXXXXX"); + + // mkstemp() modifies the string bytes in place, and does not go beyond the + // string's length. This results in well-defined behavior in C++17. + // + // The const_cast is needed below C++17. The constraints on std::string + // implementations in C++11 and above make assumption behind the const_cast + // fairly safe. + const int captured_fd = ::mkstemp(const_cast(name_template.data())); + if (captured_fd == -1) { + GTEST_LOG_(WARNING) + << "Failed to create tmp file " << name_template + << " for test; does the test have access to the /tmp directory?"; + } + filename_ = std::move(name_template); +#endif // GTEST_OS_WINDOWS + fflush(nullptr); + dup2(captured_fd, fd_); + close(captured_fd); + } + + ~CapturedStream() { remove(filename_.c_str()); } + + std::string GetCapturedString() { + if (uncaptured_fd_ != -1) { + // Restores the original stream. + fflush(nullptr); + dup2(uncaptured_fd_, fd_); + close(uncaptured_fd_); + uncaptured_fd_ = -1; + } + + FILE* const file = posix::FOpen(filename_.c_str(), "r"); + if (file == nullptr) { + GTEST_LOG_(FATAL) << "Failed to open tmp file " << filename_ + << " for capturing stream."; + } + const std::string content = ReadEntireFile(file); + posix::FClose(file); + return content; + } + + private: + const int fd_; // A stream to capture. + int uncaptured_fd_; + // Name of the temporary file holding the stderr output. + ::std::string filename_; + + CapturedStream(const CapturedStream&) = delete; + CapturedStream& operator=(const CapturedStream&) = delete; +}; + +GTEST_DISABLE_MSC_DEPRECATED_POP_() + +static CapturedStream* g_captured_stderr = nullptr; +static CapturedStream* g_captured_stdout = nullptr; + +// Starts capturing an output stream (stdout/stderr). +static void CaptureStream(int fd, const char* stream_name, + CapturedStream** stream) { + if (*stream != nullptr) { + GTEST_LOG_(FATAL) << "Only one " << stream_name + << " capturer can exist at a time."; + } + *stream = new CapturedStream(fd); +} + +// Stops capturing the output stream and returns the captured string. +static std::string GetCapturedStream(CapturedStream** captured_stream) { + const std::string content = (*captured_stream)->GetCapturedString(); + + delete *captured_stream; + *captured_stream = nullptr; + + return content; +} + +#if defined(_MSC_VER) || defined(__BORLANDC__) +// MSVC and C++Builder do not provide a definition of STDERR_FILENO. +const int kStdOutFileno = 1; +const int kStdErrFileno = 2; +#else +const int kStdOutFileno = STDOUT_FILENO; +const int kStdErrFileno = STDERR_FILENO; +#endif // defined(_MSC_VER) || defined(__BORLANDC__) + +// Starts capturing stdout. +void CaptureStdout() { + CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout); +} + +// Starts capturing stderr. +void CaptureStderr() { + CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr); +} + +// Stops capturing stdout and returns the captured string. +std::string GetCapturedStdout() { + return GetCapturedStream(&g_captured_stdout); +} + +// Stops capturing stderr and returns the captured string. +std::string GetCapturedStderr() { + return GetCapturedStream(&g_captured_stderr); +} + +#endif // GTEST_HAS_STREAM_REDIRECTION + +size_t GetFileSize(FILE* file) { + fseek(file, 0, SEEK_END); + return static_cast(ftell(file)); +} + +std::string ReadEntireFile(FILE* file) { + const size_t file_size = GetFileSize(file); + char* const buffer = new char[file_size]; + + size_t bytes_last_read = 0; // # of bytes read in the last fread() + size_t bytes_read = 0; // # of bytes read so far + + fseek(file, 0, SEEK_SET); + + // Keeps reading the file until we cannot read further or the + // pre-determined file size is reached. + do { + bytes_last_read = + fread(buffer + bytes_read, 1, file_size - bytes_read, file); + bytes_read += bytes_last_read; + } while (bytes_last_read > 0 && bytes_read < file_size); + + const std::string content(buffer, bytes_read); + delete[] buffer; + + return content; +} + +#if GTEST_HAS_DEATH_TEST +static const std::vector* g_injected_test_argvs = + nullptr; // Owned. + +std::vector GetInjectableArgvs() { + if (g_injected_test_argvs != nullptr) { + return *g_injected_test_argvs; + } + return GetArgvs(); +} + +void SetInjectableArgvs(const std::vector* new_argvs) { + if (g_injected_test_argvs != new_argvs) delete g_injected_test_argvs; + g_injected_test_argvs = new_argvs; +} + +void SetInjectableArgvs(const std::vector& new_argvs) { + SetInjectableArgvs( + new std::vector(new_argvs.begin(), new_argvs.end())); +} + +void ClearInjectableArgvs() { + delete g_injected_test_argvs; + g_injected_test_argvs = nullptr; +} +#endif // GTEST_HAS_DEATH_TEST + +#if GTEST_OS_WINDOWS_MOBILE +namespace posix { +void Abort() { + DebugBreak(); + TerminateProcess(GetCurrentProcess(), 1); +} +} // namespace posix +#endif // GTEST_OS_WINDOWS_MOBILE + +// Returns the name of the environment variable corresponding to the +// given flag. For example, FlagToEnvVar("foo") will return +// "GTEST_FOO" in the open-source version. +static std::string FlagToEnvVar(const char* flag) { + const std::string full_flag = + (Message() << GTEST_FLAG_PREFIX_ << flag).GetString(); + + Message env_var; + for (size_t i = 0; i != full_flag.length(); i++) { + env_var << ToUpper(full_flag.c_str()[i]); + } + + return env_var.GetString(); +} + +// Parses 'str' for a 32-bit signed integer. If successful, writes +// the result to *value and returns true; otherwise leaves *value +// unchanged and returns false. +bool ParseInt32(const Message& src_text, const char* str, int32_t* value) { + // Parses the environment variable as a decimal integer. + char* end = nullptr; + const long long_value = strtol(str, &end, 10); // NOLINT + + // Has strtol() consumed all characters in the string? + if (*end != '\0') { + // No - an invalid character was encountered. + Message msg; + msg << "WARNING: " << src_text + << " is expected to be a 32-bit integer, but actually" + << " has value \"" << str << "\".\n"; + printf("%s", msg.GetString().c_str()); + fflush(stdout); + return false; + } + + // Is the parsed value in the range of an int32_t? + const auto result = static_cast(long_value); + if (long_value == LONG_MAX || long_value == LONG_MIN || + // The parsed value overflows as a long. (strtol() returns + // LONG_MAX or LONG_MIN when the input overflows.) + result != long_value + // The parsed value overflows as an int32_t. + ) { + Message msg; + msg << "WARNING: " << src_text + << " is expected to be a 32-bit integer, but actually" + << " has value " << str << ", which overflows.\n"; + printf("%s", msg.GetString().c_str()); + fflush(stdout); + return false; + } + + *value = result; + return true; +} + +// Reads and returns the Boolean environment variable corresponding to +// the given flag; if it's not set, returns default_value. +// +// The value is considered true if and only if it's not "0". +bool BoolFromGTestEnv(const char* flag, bool default_value) { +#if defined(GTEST_GET_BOOL_FROM_ENV_) + return GTEST_GET_BOOL_FROM_ENV_(flag, default_value); +#else + const std::string env_var = FlagToEnvVar(flag); + const char* const string_value = posix::GetEnv(env_var.c_str()); + return string_value == nullptr ? default_value + : strcmp(string_value, "0") != 0; +#endif // defined(GTEST_GET_BOOL_FROM_ENV_) +} + +// Reads and returns a 32-bit integer stored in the environment +// variable corresponding to the given flag; if it isn't set or +// doesn't represent a valid 32-bit integer, returns default_value. +int32_t Int32FromGTestEnv(const char* flag, int32_t default_value) { +#if defined(GTEST_GET_INT32_FROM_ENV_) + return GTEST_GET_INT32_FROM_ENV_(flag, default_value); +#else + const std::string env_var = FlagToEnvVar(flag); + const char* const string_value = posix::GetEnv(env_var.c_str()); + if (string_value == nullptr) { + // The environment variable is not set. + return default_value; + } + + int32_t result = default_value; + if (!ParseInt32(Message() << "Environment variable " << env_var, string_value, + &result)) { + printf("The default value %s is used.\n", + (Message() << default_value).GetString().c_str()); + fflush(stdout); + return default_value; + } + + return result; +#endif // defined(GTEST_GET_INT32_FROM_ENV_) +} + +// As a special case for the 'output' flag, if GTEST_OUTPUT is not +// set, we look for XML_OUTPUT_FILE, which is set by the Bazel build +// system. The value of XML_OUTPUT_FILE is a filename without the +// "xml:" prefix of GTEST_OUTPUT. +// Note that this is meant to be called at the call site so it does +// not check that the flag is 'output' +// In essence this checks an env variable called XML_OUTPUT_FILE +// and if it is set we prepend "xml:" to its value, if it not set we return "" +std::string OutputFlagAlsoCheckEnvVar() { + std::string default_value_for_output_flag = ""; + const char* xml_output_file_env = posix::GetEnv("XML_OUTPUT_FILE"); + if (nullptr != xml_output_file_env) { + default_value_for_output_flag = std::string("xml:") + xml_output_file_env; + } + return default_value_for_output_flag; +} + +// Reads and returns the string environment variable corresponding to +// the given flag; if it's not set, returns default_value. +const char* StringFromGTestEnv(const char* flag, const char* default_value) { +#if defined(GTEST_GET_STRING_FROM_ENV_) + return GTEST_GET_STRING_FROM_ENV_(flag, default_value); +#else + const std::string env_var = FlagToEnvVar(flag); + const char* const value = posix::GetEnv(env_var.c_str()); + return value == nullptr ? default_value : value; +#endif // defined(GTEST_GET_STRING_FROM_ENV_) +} + +} // namespace internal +} // namespace testing diff --git a/test/gtest/src/gtest-printers.cc b/test/gtest/src/gtest-printers.cc new file mode 100644 index 0000000000..f3976d230d --- /dev/null +++ b/test/gtest/src/gtest-printers.cc @@ -0,0 +1,553 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Google Test - The Google C++ Testing and Mocking Framework +// +// This file implements a universal value printer that can print a +// value of any type T: +// +// void ::testing::internal::UniversalPrinter::Print(value, ostream_ptr); +// +// It uses the << operator when possible, and prints the bytes in the +// object otherwise. A user can override its behavior for a class +// type Foo by defining either operator<<(::std::ostream&, const Foo&) +// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that +// defines Foo. + +#include "gtest/gtest-printers.h" + +#include + +#include +#include +#include +#include // NOLINT +#include +#include + +#include "gtest/internal/gtest-port.h" +#include "src/gtest-internal-inl.h" + +namespace testing { + +namespace { + +using ::std::ostream; + +// Prints a segment of bytes in the given object. +GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ +GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ +GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ +GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ +void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, + size_t count, ostream* os) { + char text[5] = ""; + for (size_t i = 0; i != count; i++) { + const size_t j = start + i; + if (i != 0) { + // Organizes the bytes into groups of 2 for easy parsing by + // human. + if ((j % 2) == 0) + *os << ' '; + else + *os << '-'; + } + GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]); + *os << text; + } +} + +// Prints the bytes in the given value to the given ostream. +void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count, + ostream* os) { + // Tells the user how big the object is. + *os << count << "-byte object <"; + + const size_t kThreshold = 132; + const size_t kChunkSize = 64; + // If the object size is bigger than kThreshold, we'll have to omit + // some details by printing only the first and the last kChunkSize + // bytes. + if (count < kThreshold) { + PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); + } else { + PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); + *os << " ... "; + // Rounds up to 2-byte boundary. + const size_t resume_pos = (count - kChunkSize + 1) / 2 * 2; + PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); + } + *os << ">"; +} + +// Helpers for widening a character to char32_t. Since the standard does not +// specify if char / wchar_t is signed or unsigned, it is important to first +// convert it to the unsigned type of the same width before widening it to +// char32_t. +template +char32_t ToChar32(CharType in) { + return static_cast( + static_cast::type>(in)); +} + +} // namespace + +namespace internal { + +// Delegates to PrintBytesInObjectToImpl() to print the bytes in the +// given object. The delegation simplifies the implementation, which +// uses the << operator and thus is easier done outside of the +// ::testing::internal namespace, which contains a << operator that +// sometimes conflicts with the one in STL. +void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, + ostream* os) { + PrintBytesInObjectToImpl(obj_bytes, count, os); +} + +// Depending on the value of a char (or wchar_t), we print it in one +// of three formats: +// - as is if it's a printable ASCII (e.g. 'a', '2', ' '), +// - as a hexadecimal escape sequence (e.g. '\x7F'), or +// - as a special escape sequence (e.g. '\r', '\n'). +enum CharFormat { kAsIs, kHexEscape, kSpecialEscape }; + +// Returns true if c is a printable ASCII character. We test the +// value of c directly instead of calling isprint(), which is buggy on +// Windows Mobile. +inline bool IsPrintableAscii(char32_t c) { return 0x20 <= c && c <= 0x7E; } + +// Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a +// character literal without the quotes, escaping it when necessary; returns how +// c was formatted. +template +static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) { + const char32_t u_c = ToChar32(c); + switch (u_c) { + case L'\0': + *os << "\\0"; + break; + case L'\'': + *os << "\\'"; + break; + case L'\\': + *os << "\\\\"; + break; + case L'\a': + *os << "\\a"; + break; + case L'\b': + *os << "\\b"; + break; + case L'\f': + *os << "\\f"; + break; + case L'\n': + *os << "\\n"; + break; + case L'\r': + *os << "\\r"; + break; + case L'\t': + *os << "\\t"; + break; + case L'\v': + *os << "\\v"; + break; + default: + if (IsPrintableAscii(u_c)) { + *os << static_cast(c); + return kAsIs; + } else { + ostream::fmtflags flags = os->flags(); + *os << "\\x" << std::hex << std::uppercase << static_cast(u_c); + os->flags(flags); + return kHexEscape; + } + } + return kSpecialEscape; +} + +// Prints a char32_t c as if it's part of a string literal, escaping it when +// necessary; returns how c was formatted. +static CharFormat PrintAsStringLiteralTo(char32_t c, ostream* os) { + switch (c) { + case L'\'': + *os << "'"; + return kAsIs; + case L'"': + *os << "\\\""; + return kSpecialEscape; + default: + return PrintAsCharLiteralTo(c, os); + } +} + +static const char* GetCharWidthPrefix(char) { return ""; } + +static const char* GetCharWidthPrefix(signed char) { return ""; } + +static const char* GetCharWidthPrefix(unsigned char) { return ""; } + +#ifdef __cpp_char8_t +static const char* GetCharWidthPrefix(char8_t) { return "u8"; } +#endif + +static const char* GetCharWidthPrefix(char16_t) { return "u"; } + +static const char* GetCharWidthPrefix(char32_t) { return "U"; } + +static const char* GetCharWidthPrefix(wchar_t) { return "L"; } + +// Prints a char c as if it's part of a string literal, escaping it when +// necessary; returns how c was formatted. +static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { + return PrintAsStringLiteralTo(ToChar32(c), os); +} + +#ifdef __cpp_char8_t +static CharFormat PrintAsStringLiteralTo(char8_t c, ostream* os) { + return PrintAsStringLiteralTo(ToChar32(c), os); +} +#endif + +static CharFormat PrintAsStringLiteralTo(char16_t c, ostream* os) { + return PrintAsStringLiteralTo(ToChar32(c), os); +} + +static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) { + return PrintAsStringLiteralTo(ToChar32(c), os); +} + +// Prints a character c (of type char, char8_t, char16_t, char32_t, or wchar_t) +// and its code. '\0' is printed as "'\\0'", other unprintable characters are +// also properly escaped using the standard C++ escape sequence. +template +void PrintCharAndCodeTo(Char c, ostream* os) { + // First, print c as a literal in the most readable form we can find. + *os << GetCharWidthPrefix(c) << "'"; + const CharFormat format = PrintAsCharLiteralTo(c, os); + *os << "'"; + + // To aid user debugging, we also print c's code in decimal, unless + // it's 0 (in which case c was printed as '\\0', making the code + // obvious). + if (c == 0) return; + *os << " (" << static_cast(c); + + // For more convenience, we print c's code again in hexadecimal, + // unless c was already printed in the form '\x##' or the code is in + // [1, 9]. + if (format == kHexEscape || (1 <= c && c <= 9)) { + // Do nothing. + } else { + *os << ", 0x" << String::FormatHexInt(static_cast(c)); + } + *os << ")"; +} + +void PrintTo(unsigned char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); } +void PrintTo(signed char c, ::std::ostream* os) { PrintCharAndCodeTo(c, os); } + +// Prints a wchar_t as a symbol if it is printable or as its internal +// code otherwise and also as its code. L'\0' is printed as "L'\\0'". +void PrintTo(wchar_t wc, ostream* os) { PrintCharAndCodeTo(wc, os); } + +// TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well. +void PrintTo(char32_t c, ::std::ostream* os) { + *os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4) + << static_cast(c); +} + +// gcc/clang __{u,}int128_t +#if defined(__SIZEOF_INT128__) +void PrintTo(__uint128_t v, ::std::ostream* os) { + if (v == 0) { + *os << "0"; + return; + } + + // Buffer large enough for ceil(log10(2^128))==39 and the null terminator + char buf[40]; + char* p = buf + sizeof(buf); + + // Some configurations have a __uint128_t, but no support for built in + // division. Do manual long division instead. + + uint64_t high = static_cast(v >> 64); + uint64_t low = static_cast(v); + + *--p = 0; + while (high != 0 || low != 0) { + uint64_t high_mod = high % 10; + high = high / 10; + // This is the long division algorithm specialized for a divisor of 10 and + // only two elements. + // Notable values: + // 2^64 / 10 == 1844674407370955161 + // 2^64 % 10 == 6 + const uint64_t carry = 6 * high_mod + low % 10; + low = low / 10 + high_mod * 1844674407370955161 + carry / 10; + + char digit = static_cast(carry % 10); + *--p = '0' + digit; + } + *os << p; +} +void PrintTo(__int128_t v, ::std::ostream* os) { + __uint128_t uv = static_cast<__uint128_t>(v); + if (v < 0) { + *os << "-"; + uv = -uv; + } + PrintTo(uv, os); +} +#endif // __SIZEOF_INT128__ + +// Prints the given array of characters to the ostream. CharType must be either +// char, char8_t, char16_t, char32_t, or wchar_t. +// The array starts at begin, the length is len, it may include '\0' characters +// and may not be NUL-terminated. +template +GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ + GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ + GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat + PrintCharsAsStringTo(const CharType* begin, size_t len, ostream* os) { + const char* const quote_prefix = GetCharWidthPrefix(*begin); + *os << quote_prefix << "\""; + bool is_previous_hex = false; + CharFormat print_format = kAsIs; + for (size_t index = 0; index < len; ++index) { + const CharType cur = begin[index]; + if (is_previous_hex && IsXDigit(cur)) { + // Previous character is of '\x..' form and this character can be + // interpreted as another hexadecimal digit in its number. Break string to + // disambiguate. + *os << "\" " << quote_prefix << "\""; + } + is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape; + // Remember if any characters required hex escaping. + if (is_previous_hex) { + print_format = kHexEscape; + } + } + *os << "\""; + return print_format; +} + +// Prints a (const) char/wchar_t array of 'len' elements, starting at address +// 'begin'. CharType must be either char or wchar_t. +template +GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ + GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ + GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static void + UniversalPrintCharArray(const CharType* begin, size_t len, + ostream* os) { + // The code + // const char kFoo[] = "foo"; + // generates an array of 4, not 3, elements, with the last one being '\0'. + // + // Therefore when printing a char array, we don't print the last element if + // it's '\0', such that the output matches the string literal as it's + // written in the source code. + if (len > 0 && begin[len - 1] == '\0') { + PrintCharsAsStringTo(begin, len - 1, os); + return; + } + + // If, however, the last element in the array is not '\0', e.g. + // const char kFoo[] = { 'f', 'o', 'o' }; + // we must print the entire array. We also print a message to indicate + // that the array is not NUL-terminated. + PrintCharsAsStringTo(begin, len, os); + *os << " (no terminating NUL)"; +} + +// Prints a (const) char array of 'len' elements, starting at address 'begin'. +void UniversalPrintArray(const char* begin, size_t len, ostream* os) { + UniversalPrintCharArray(begin, len, os); +} + +#ifdef __cpp_char8_t +// Prints a (const) char8_t array of 'len' elements, starting at address +// 'begin'. +void UniversalPrintArray(const char8_t* begin, size_t len, ostream* os) { + UniversalPrintCharArray(begin, len, os); +} +#endif + +// Prints a (const) char16_t array of 'len' elements, starting at address +// 'begin'. +void UniversalPrintArray(const char16_t* begin, size_t len, ostream* os) { + UniversalPrintCharArray(begin, len, os); +} + +// Prints a (const) char32_t array of 'len' elements, starting at address +// 'begin'. +void UniversalPrintArray(const char32_t* begin, size_t len, ostream* os) { + UniversalPrintCharArray(begin, len, os); +} + +// Prints a (const) wchar_t array of 'len' elements, starting at address +// 'begin'. +void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) { + UniversalPrintCharArray(begin, len, os); +} + +namespace { + +// Prints a null-terminated C-style string to the ostream. +template +void PrintCStringTo(const Char* s, ostream* os) { + if (s == nullptr) { + *os << "NULL"; + } else { + *os << ImplicitCast_(s) << " pointing to "; + PrintCharsAsStringTo(s, std::char_traits::length(s), os); + } +} + +} // anonymous namespace + +void PrintTo(const char* s, ostream* os) { PrintCStringTo(s, os); } + +#ifdef __cpp_char8_t +void PrintTo(const char8_t* s, ostream* os) { PrintCStringTo(s, os); } +#endif + +void PrintTo(const char16_t* s, ostream* os) { PrintCStringTo(s, os); } + +void PrintTo(const char32_t* s, ostream* os) { PrintCStringTo(s, os); } + +// MSVC compiler can be configured to define whar_t as a typedef +// of unsigned short. Defining an overload for const wchar_t* in that case +// would cause pointers to unsigned shorts be printed as wide strings, +// possibly accessing more memory than intended and causing invalid +// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when +// wchar_t is implemented as a native type. +#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) +// Prints the given wide C string to the ostream. +void PrintTo(const wchar_t* s, ostream* os) { PrintCStringTo(s, os); } +#endif // wchar_t is native + +namespace { + +bool ContainsUnprintableControlCodes(const char* str, size_t length) { + const unsigned char* s = reinterpret_cast(str); + + for (size_t i = 0; i < length; i++) { + unsigned char ch = *s++; + if (std::iscntrl(ch)) { + switch (ch) { + case '\t': + case '\n': + case '\r': + break; + default: + return true; + } + } + } + return false; +} + +bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t <= 0xbf; } + +bool IsValidUTF8(const char* str, size_t length) { + const unsigned char* s = reinterpret_cast(str); + + for (size_t i = 0; i < length;) { + unsigned char lead = s[i++]; + + if (lead <= 0x7f) { + continue; // single-byte character (ASCII) 0..7F + } + if (lead < 0xc2) { + return false; // trail byte or non-shortest form + } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) { + ++i; // 2-byte character + } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length && + IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) && + // check for non-shortest form and surrogate + (lead != 0xe0 || s[i] >= 0xa0) && + (lead != 0xed || s[i] < 0xa0)) { + i += 2; // 3-byte character + } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length && + IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) && + IsUTF8TrailByte(s[i + 2]) && + // check for non-shortest form + (lead != 0xf0 || s[i] >= 0x90) && + (lead != 0xf4 || s[i] < 0x90)) { + i += 3; // 4-byte character + } else { + return false; + } + } + return true; +} + +void ConditionalPrintAsText(const char* str, size_t length, ostream* os) { + if (!ContainsUnprintableControlCodes(str, length) && + IsValidUTF8(str, length)) { + *os << "\n As Text: \"" << str << "\""; + } +} + +} // anonymous namespace + +void PrintStringTo(const ::std::string& s, ostream* os) { + if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) { + if (GTEST_FLAG_GET(print_utf8)) { + ConditionalPrintAsText(s.data(), s.size(), os); + } + } +} + +#ifdef __cpp_char8_t +void PrintU8StringTo(const ::std::u8string& s, ostream* os) { + PrintCharsAsStringTo(s.data(), s.size(), os); +} +#endif + +void PrintU16StringTo(const ::std::u16string& s, ostream* os) { + PrintCharsAsStringTo(s.data(), s.size(), os); +} + +void PrintU32StringTo(const ::std::u32string& s, ostream* os) { + PrintCharsAsStringTo(s.data(), s.size(), os); +} + +#if GTEST_HAS_STD_WSTRING +void PrintWideStringTo(const ::std::wstring& s, ostream* os) { + PrintCharsAsStringTo(s.data(), s.size(), os); +} +#endif // GTEST_HAS_STD_WSTRING + +} // namespace internal + +} // namespace testing diff --git a/test/gtest/src/gtest-test-part.cc b/test/gtest/src/gtest-test-part.cc new file mode 100644 index 0000000000..eb7c8d1cf9 --- /dev/null +++ b/test/gtest/src/gtest-test-part.cc @@ -0,0 +1,105 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// +// The Google C++ Testing and Mocking Framework (Google Test) + +#include "gtest/gtest-test-part.h" + +#include "gtest/internal/gtest-port.h" +#include "src/gtest-internal-inl.h" + +namespace testing { + +using internal::GetUnitTestImpl; + +// Gets the summary of the failure message by omitting the stack trace +// in it. +std::string TestPartResult::ExtractSummary(const char* message) { + const char* const stack_trace = strstr(message, internal::kStackTraceMarker); + return stack_trace == nullptr ? message : std::string(message, stack_trace); +} + +// Prints a TestPartResult object. +std::ostream& operator<<(std::ostream& os, const TestPartResult& result) { + return os << internal::FormatFileLocation(result.file_name(), + result.line_number()) + << " " + << (result.type() == TestPartResult::kSuccess ? "Success" + : result.type() == TestPartResult::kSkip ? "Skipped" + : result.type() == TestPartResult::kFatalFailure + ? "Fatal failure" + : "Non-fatal failure") + << ":\n" + << result.message() << std::endl; +} + +// Appends a TestPartResult to the array. +void TestPartResultArray::Append(const TestPartResult& result) { + array_.push_back(result); +} + +// Returns the TestPartResult at the given index (0-based). +const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const { + if (index < 0 || index >= size()) { + printf("\nInvalid index (%d) into TestPartResultArray.\n", index); + internal::posix::Abort(); + } + + return array_[static_cast(index)]; +} + +// Returns the number of TestPartResult objects in the array. +int TestPartResultArray::size() const { + return static_cast(array_.size()); +} + +namespace internal { + +HasNewFatalFailureHelper::HasNewFatalFailureHelper() + : has_new_fatal_failure_(false), + original_reporter_( + GetUnitTestImpl()->GetTestPartResultReporterForCurrentThread()) { + GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this); +} + +HasNewFatalFailureHelper::~HasNewFatalFailureHelper() { + GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread( + original_reporter_); +} + +void HasNewFatalFailureHelper::ReportTestPartResult( + const TestPartResult& result) { + if (result.fatally_failed()) has_new_fatal_failure_ = true; + original_reporter_->ReportTestPartResult(result); +} + +} // namespace internal + +} // namespace testing diff --git a/test/gtest/src/gtest-typed-test.cc b/test/gtest/src/gtest-typed-test.cc new file mode 100644 index 0000000000..a2828b83c6 --- /dev/null +++ b/test/gtest/src/gtest-typed-test.cc @@ -0,0 +1,104 @@ +// Copyright 2008 Google Inc. +// All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "gtest/gtest-typed-test.h" + +#include "gtest/gtest.h" + +namespace testing { +namespace internal { + +// Skips to the first non-space char in str. Returns an empty string if str +// contains only whitespace characters. +static const char* SkipSpaces(const char* str) { + while (IsSpace(*str)) str++; + return str; +} + +static std::vector SplitIntoTestNames(const char* src) { + std::vector name_vec; + src = SkipSpaces(src); + for (; src != nullptr; src = SkipComma(src)) { + name_vec.push_back(StripTrailingSpaces(GetPrefixUntilComma(src))); + } + return name_vec; +} + +// Verifies that registered_tests match the test names in +// registered_tests_; returns registered_tests if successful, or +// aborts the program otherwise. +const char* TypedTestSuitePState::VerifyRegisteredTestNames( + const char* test_suite_name, const char* file, int line, + const char* registered_tests) { + RegisterTypeParameterizedTestSuite(test_suite_name, CodeLocation(file, line)); + + typedef RegisteredTestsMap::const_iterator RegisteredTestIter; + registered_ = true; + + std::vector name_vec = SplitIntoTestNames(registered_tests); + + Message errors; + + std::set tests; + for (std::vector::const_iterator name_it = name_vec.begin(); + name_it != name_vec.end(); ++name_it) { + const std::string& name = *name_it; + if (tests.count(name) != 0) { + errors << "Test " << name << " is listed more than once.\n"; + continue; + } + + if (registered_tests_.count(name) != 0) { + tests.insert(name); + } else { + errors << "No test named " << name + << " can be found in this test suite.\n"; + } + } + + for (RegisteredTestIter it = registered_tests_.begin(); + it != registered_tests_.end(); ++it) { + if (tests.count(it->first) == 0) { + errors << "You forgot to list test " << it->first << ".\n"; + } + } + + const std::string& errors_str = errors.GetString(); + if (errors_str != "") { + fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(), + errors_str.c_str()); + fflush(stderr); + posix::Abort(); + } + + return registered_tests; +} + +} // namespace internal +} // namespace testing diff --git a/test/gtest/src/gtest.cc b/test/gtest/src/gtest.cc new file mode 100644 index 0000000000..6f31dd2260 --- /dev/null +++ b/test/gtest/src/gtest.cc @@ -0,0 +1,6795 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// +// The Google C++ Testing and Mocking Framework (Google Test) + +#include "gtest/gtest.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include // NOLINT +#include +#include +#include +#include +#include +#include +#include +#include +#include // NOLINT +#include +#include +#include + +#include "gtest/gtest-assertion-result.h" +#include "gtest/gtest-spi.h" +#include "gtest/internal/custom/gtest.h" + +#if GTEST_OS_LINUX + +#include // NOLINT +#include // NOLINT +#include // NOLINT +// Declares vsnprintf(). This header is not available on Windows. +#include // NOLINT +#include // NOLINT +#include // NOLINT +#include // NOLINT + +#include + +#elif GTEST_OS_ZOS +#include // NOLINT + +// On z/OS we additionally need strings.h for strcasecmp. +#include // NOLINT + +#elif GTEST_OS_WINDOWS_MOBILE // We are on Windows CE. + +#include // NOLINT +#undef min + +#elif GTEST_OS_WINDOWS // We are on Windows proper. + +#include // NOLINT +#undef min + +#ifdef _MSC_VER +#include // NOLINT +#endif + +#include // NOLINT +#include // NOLINT +#include // NOLINT +#include // NOLINT + +#if GTEST_OS_WINDOWS_MINGW +#include // NOLINT +#endif // GTEST_OS_WINDOWS_MINGW + +#else + +// cpplint thinks that the header is already included, so we want to +// silence it. +#include // NOLINT +#include // NOLINT + +#endif // GTEST_OS_LINUX + +#if GTEST_HAS_EXCEPTIONS +#include +#endif + +#if GTEST_CAN_STREAM_RESULTS_ +#include // NOLINT +#include // NOLINT +#include // NOLINT +#include // NOLINT +#endif + +#include "src/gtest-internal-inl.h" + +#if GTEST_OS_WINDOWS +#define vsnprintf _vsnprintf +#endif // GTEST_OS_WINDOWS + +#if GTEST_OS_MAC +#ifndef GTEST_OS_IOS +#include +#endif +#endif + +#if GTEST_HAS_ABSL +#include "absl/debugging/failure_signal_handler.h" +#include "absl/debugging/stacktrace.h" +#include "absl/debugging/symbolize.h" +#include "absl/flags/parse.h" +#include "absl/flags/usage.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/str_replace.h" +#endif // GTEST_HAS_ABSL + +namespace testing { + +using internal::CountIf; +using internal::ForEach; +using internal::GetElementOr; +using internal::Shuffle; + +// Constants. + +// A test whose test suite name or test name matches this filter is +// disabled and not run. +static const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*"; + +// A test suite whose name matches this filter is considered a death +// test suite and will be run before test suites whose name doesn't +// match this filter. +static const char kDeathTestSuiteFilter[] = "*DeathTest:*DeathTest/*"; + +// A test filter that matches everything. +static const char kUniversalFilter[] = "*"; + +// The default output format. +static const char kDefaultOutputFormat[] = "xml"; +// The default output file. +static const char kDefaultOutputFile[] = "test_detail"; + +// The environment variable name for the test shard index. +static const char kTestShardIndex[] = "GTEST_SHARD_INDEX"; +// The environment variable name for the total number of test shards. +static const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS"; +// The environment variable name for the test shard status file. +static const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE"; + +namespace internal { + +// The text used in failure messages to indicate the start of the +// stack trace. +const char kStackTraceMarker[] = "\nStack trace:\n"; + +// g_help_flag is true if and only if the --help flag or an equivalent form +// is specified on the command line. +bool g_help_flag = false; + +// Utility function to Open File for Writing +static FILE* OpenFileForWriting(const std::string& output_file) { + FILE* fileout = nullptr; + FilePath output_file_path(output_file); + FilePath output_dir(output_file_path.RemoveFileName()); + + if (output_dir.CreateDirectoriesRecursively()) { + fileout = posix::FOpen(output_file.c_str(), "w"); + } + if (fileout == nullptr) { + GTEST_LOG_(FATAL) << "Unable to open file \"" << output_file << "\""; + } + return fileout; +} + +} // namespace internal + +// Bazel passes in the argument to '--test_filter' via the TESTBRIDGE_TEST_ONLY +// environment variable. +static const char* GetDefaultFilter() { + const char* const testbridge_test_only = + internal::posix::GetEnv("TESTBRIDGE_TEST_ONLY"); + if (testbridge_test_only != nullptr) { + return testbridge_test_only; + } + return kUniversalFilter; +} + +// Bazel passes in the argument to '--test_runner_fail_fast' via the +// TESTBRIDGE_TEST_RUNNER_FAIL_FAST environment variable. +static bool GetDefaultFailFast() { + const char* const testbridge_test_runner_fail_fast = + internal::posix::GetEnv("TESTBRIDGE_TEST_RUNNER_FAIL_FAST"); + if (testbridge_test_runner_fail_fast != nullptr) { + return strcmp(testbridge_test_runner_fail_fast, "1") == 0; + } + return false; +} + +} // namespace testing + +GTEST_DEFINE_bool_( + fail_fast, + testing::internal::BoolFromGTestEnv("fail_fast", + testing::GetDefaultFailFast()), + "True if and only if a test failure should stop further test execution."); + +GTEST_DEFINE_bool_( + also_run_disabled_tests, + testing::internal::BoolFromGTestEnv("also_run_disabled_tests", false), + "Run disabled tests too, in addition to the tests normally being run."); + +GTEST_DEFINE_bool_( + break_on_failure, + testing::internal::BoolFromGTestEnv("break_on_failure", false), + "True if and only if a failed assertion should be a debugger " + "break-point."); + +GTEST_DEFINE_bool_(catch_exceptions, + testing::internal::BoolFromGTestEnv("catch_exceptions", + true), + "True if and only if " GTEST_NAME_ + " should catch exceptions and treat them as test failures."); + +GTEST_DEFINE_string_( + color, testing::internal::StringFromGTestEnv("color", "auto"), + "Whether to use colors in the output. Valid values: yes, no, " + "and auto. 'auto' means to use colors if the output is " + "being sent to a terminal and the TERM environment variable " + "is set to a terminal type that supports colors."); + +GTEST_DEFINE_string_( + filter, + testing::internal::StringFromGTestEnv("filter", + testing::GetDefaultFilter()), + "A colon-separated list of glob (not regex) patterns " + "for filtering the tests to run, optionally followed by a " + "'-' and a : separated list of negative patterns (tests to " + "exclude). A test is run if it matches one of the positive " + "patterns and does not match any of the negative patterns."); + +GTEST_DEFINE_bool_( + install_failure_signal_handler, + testing::internal::BoolFromGTestEnv("install_failure_signal_handler", + false), + "If true and supported on the current platform, " GTEST_NAME_ + " should " + "install a signal handler that dumps debugging information when fatal " + "signals are raised."); + +GTEST_DEFINE_bool_(list_tests, false, "List all tests without running them."); + +// The net priority order after flag processing is thus: +// --gtest_output command line flag +// GTEST_OUTPUT environment variable +// XML_OUTPUT_FILE environment variable +// '' +GTEST_DEFINE_string_( + output, + testing::internal::StringFromGTestEnv( + "output", testing::internal::OutputFlagAlsoCheckEnvVar().c_str()), + "A format (defaults to \"xml\" but can be specified to be \"json\"), " + "optionally followed by a colon and an output file name or directory. " + "A directory is indicated by a trailing pathname separator. " + "Examples: \"xml:filename.xml\", \"xml::directoryname/\". " + "If a directory is specified, output files will be created " + "within that directory, with file-names based on the test " + "executable's name and, if necessary, made unique by adding " + "digits."); + +GTEST_DEFINE_bool_( + brief, testing::internal::BoolFromGTestEnv("brief", false), + "True if only test failures should be displayed in text output."); + +GTEST_DEFINE_bool_(print_time, + testing::internal::BoolFromGTestEnv("print_time", true), + "True if and only if " GTEST_NAME_ + " should display elapsed time in text output."); + +GTEST_DEFINE_bool_(print_utf8, + testing::internal::BoolFromGTestEnv("print_utf8", true), + "True if and only if " GTEST_NAME_ + " prints UTF8 characters as text."); + +GTEST_DEFINE_int32_( + random_seed, testing::internal::Int32FromGTestEnv("random_seed", 0), + "Random number seed to use when shuffling test orders. Must be in range " + "[1, 99999], or 0 to use a seed based on the current time."); + +GTEST_DEFINE_int32_( + repeat, testing::internal::Int32FromGTestEnv("repeat", 1), + "How many times to repeat each test. Specify a negative number " + "for repeating forever. Useful for shaking out flaky tests."); + +GTEST_DEFINE_bool_( + recreate_environments_when_repeating, + testing::internal::BoolFromGTestEnv("recreate_environments_when_repeating", + false), + "Controls whether global test environments are recreated for each repeat " + "of the tests. If set to false the global test environments are only set " + "up once, for the first iteration, and only torn down once, for the last. " + "Useful for shaking out flaky tests with stable, expensive test " + "environments. If --gtest_repeat is set to a negative number, meaning " + "there is no last run, the environments will always be recreated to avoid " + "leaks."); + +GTEST_DEFINE_bool_(show_internal_stack_frames, false, + "True if and only if " GTEST_NAME_ + " should include internal stack frames when " + "printing test failure stack traces."); + +GTEST_DEFINE_bool_(shuffle, + testing::internal::BoolFromGTestEnv("shuffle", false), + "True if and only if " GTEST_NAME_ + " should randomize tests' order on every run."); + +GTEST_DEFINE_int32_( + stack_trace_depth, + testing::internal::Int32FromGTestEnv("stack_trace_depth", + testing::kMaxStackTraceDepth), + "The maximum number of stack frames to print when an " + "assertion fails. The valid range is 0 through 100, inclusive."); + +GTEST_DEFINE_string_( + stream_result_to, + testing::internal::StringFromGTestEnv("stream_result_to", ""), + "This flag specifies the host name and the port number on which to stream " + "test results. Example: \"localhost:555\". The flag is effective only on " + "Linux."); + +GTEST_DEFINE_bool_( + throw_on_failure, + testing::internal::BoolFromGTestEnv("throw_on_failure", false), + "When this flag is specified, a failed assertion will throw an exception " + "if exceptions are enabled or exit the program with a non-zero code " + "otherwise. For use with an external test framework."); + +#if GTEST_USE_OWN_FLAGFILE_FLAG_ +GTEST_DEFINE_string_( + flagfile, testing::internal::StringFromGTestEnv("flagfile", ""), + "This flag specifies the flagfile to read command-line flags from."); +#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ + +namespace testing { +namespace internal { + +// Generates a random number from [0, range), using a Linear +// Congruential Generator (LCG). Crashes if 'range' is 0 or greater +// than kMaxRange. +uint32_t Random::Generate(uint32_t range) { + // These constants are the same as are used in glibc's rand(3). + // Use wider types than necessary to prevent unsigned overflow diagnostics. + state_ = static_cast(1103515245ULL * state_ + 12345U) % kMaxRange; + + GTEST_CHECK_(range > 0) << "Cannot generate a number in the range [0, 0)."; + GTEST_CHECK_(range <= kMaxRange) + << "Generation of a number in [0, " << range << ") was requested, " + << "but this can only generate numbers in [0, " << kMaxRange << ")."; + + // Converting via modulus introduces a bit of downward bias, but + // it's simple, and a linear congruential generator isn't too good + // to begin with. + return state_ % range; +} + +// GTestIsInitialized() returns true if and only if the user has initialized +// Google Test. Useful for catching the user mistake of not initializing +// Google Test before calling RUN_ALL_TESTS(). +static bool GTestIsInitialized() { return GetArgvs().size() > 0; } + +// Iterates over a vector of TestSuites, keeping a running sum of the +// results of calling a given int-returning method on each. +// Returns the sum. +static int SumOverTestSuiteList(const std::vector& case_list, + int (TestSuite::*method)() const) { + int sum = 0; + for (size_t i = 0; i < case_list.size(); i++) { + sum += (case_list[i]->*method)(); + } + return sum; +} + +// Returns true if and only if the test suite passed. +static bool TestSuitePassed(const TestSuite* test_suite) { + return test_suite->should_run() && test_suite->Passed(); +} + +// Returns true if and only if the test suite failed. +static bool TestSuiteFailed(const TestSuite* test_suite) { + return test_suite->should_run() && test_suite->Failed(); +} + +// Returns true if and only if test_suite contains at least one test that +// should run. +static bool ShouldRunTestSuite(const TestSuite* test_suite) { + return test_suite->should_run(); +} + +// AssertHelper constructor. +AssertHelper::AssertHelper(TestPartResult::Type type, const char* file, + int line, const char* message) + : data_(new AssertHelperData(type, file, line, message)) {} + +AssertHelper::~AssertHelper() { delete data_; } + +// Message assignment, for assertion streaming support. +void AssertHelper::operator=(const Message& message) const { + UnitTest::GetInstance()->AddTestPartResult( + data_->type, data_->file, data_->line, + AppendUserMessage(data_->message, message), + UnitTest::GetInstance()->impl()->CurrentOsStackTraceExceptTop(1) + // Skips the stack frame for this function itself. + ); // NOLINT +} + +namespace { + +// When TEST_P is found without a matching INSTANTIATE_TEST_SUITE_P +// to creates test cases for it, a synthetic test case is +// inserted to report ether an error or a log message. +// +// This configuration bit will likely be removed at some point. +constexpr bool kErrorOnUninstantiatedParameterizedTest = true; +constexpr bool kErrorOnUninstantiatedTypeParameterizedTest = true; + +// A test that fails at a given file/line location with a given message. +class FailureTest : public Test { + public: + explicit FailureTest(const CodeLocation& loc, std::string error_message, + bool as_error) + : loc_(loc), + error_message_(std::move(error_message)), + as_error_(as_error) {} + + void TestBody() override { + if (as_error_) { + AssertHelper(TestPartResult::kNonFatalFailure, loc_.file.c_str(), + loc_.line, "") = Message() << error_message_; + } else { + std::cout << error_message_ << std::endl; + } + } + + private: + const CodeLocation loc_; + const std::string error_message_; + const bool as_error_; +}; + +} // namespace + +std::set* GetIgnoredParameterizedTestSuites() { + return UnitTest::GetInstance()->impl()->ignored_parameterized_test_suites(); +} + +// Add a given test_suit to the list of them allow to go un-instantiated. +MarkAsIgnored::MarkAsIgnored(const char* test_suite) { + GetIgnoredParameterizedTestSuites()->insert(test_suite); +} + +// If this parameterized test suite has no instantiations (and that +// has not been marked as okay), emit a test case reporting that. +void InsertSyntheticTestCase(const std::string& name, CodeLocation location, + bool has_test_p) { + const auto& ignored = *GetIgnoredParameterizedTestSuites(); + if (ignored.find(name) != ignored.end()) return; + + const char kMissingInstantiation[] = // + " is defined via TEST_P, but never instantiated. None of the test cases " + "will run. Either no INSTANTIATE_TEST_SUITE_P is provided or the only " + "ones provided expand to nothing." + "\n\n" + "Ideally, TEST_P definitions should only ever be included as part of " + "binaries that intend to use them. (As opposed to, for example, being " + "placed in a library that may be linked in to get other utilities.)"; + + const char kMissingTestCase[] = // + " is instantiated via INSTANTIATE_TEST_SUITE_P, but no tests are " + "defined via TEST_P . No test cases will run." + "\n\n" + "Ideally, INSTANTIATE_TEST_SUITE_P should only ever be invoked from " + "code that always depend on code that provides TEST_P. Failing to do " + "so is often an indication of dead code, e.g. the last TEST_P was " + "removed but the rest got left behind."; + + std::string message = + "Parameterized test suite " + name + + (has_test_p ? kMissingInstantiation : kMissingTestCase) + + "\n\n" + "To suppress this error for this test suite, insert the following line " + "(in a non-header) in the namespace it is defined in:" + "\n\n" + "GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(" + + name + ");"; + + std::string full_name = "UninstantiatedParameterizedTestSuite<" + name + ">"; + RegisterTest( // + "GoogleTestVerification", full_name.c_str(), + nullptr, // No type parameter. + nullptr, // No value parameter. + location.file.c_str(), location.line, [message, location] { + return new FailureTest(location, message, + kErrorOnUninstantiatedParameterizedTest); + }); +} + +void RegisterTypeParameterizedTestSuite(const char* test_suite_name, + CodeLocation code_location) { + GetUnitTestImpl()->type_parameterized_test_registry().RegisterTestSuite( + test_suite_name, code_location); +} + +void RegisterTypeParameterizedTestSuiteInstantiation(const char* case_name) { + GetUnitTestImpl()->type_parameterized_test_registry().RegisterInstantiation( + case_name); +} + +void TypeParameterizedTestSuiteRegistry::RegisterTestSuite( + const char* test_suite_name, CodeLocation code_location) { + suites_.emplace(std::string(test_suite_name), + TypeParameterizedTestSuiteInfo(code_location)); +} + +void TypeParameterizedTestSuiteRegistry::RegisterInstantiation( + const char* test_suite_name) { + auto it = suites_.find(std::string(test_suite_name)); + if (it != suites_.end()) { + it->second.instantiated = true; + } else { + GTEST_LOG_(ERROR) << "Unknown type parameterized test suit '" + << test_suite_name << "'"; + } +} + +void TypeParameterizedTestSuiteRegistry::CheckForInstantiations() { + const auto& ignored = *GetIgnoredParameterizedTestSuites(); + for (const auto& testcase : suites_) { + if (testcase.second.instantiated) continue; + if (ignored.find(testcase.first) != ignored.end()) continue; + + std::string message = + "Type parameterized test suite " + testcase.first + + " is defined via REGISTER_TYPED_TEST_SUITE_P, but never instantiated " + "via INSTANTIATE_TYPED_TEST_SUITE_P. None of the test cases will run." + "\n\n" + "Ideally, TYPED_TEST_P definitions should only ever be included as " + "part of binaries that intend to use them. (As opposed to, for " + "example, being placed in a library that may be linked in to get other " + "utilities.)" + "\n\n" + "To suppress this error for this test suite, insert the following line " + "(in a non-header) in the namespace it is defined in:" + "\n\n" + "GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(" + + testcase.first + ");"; + + std::string full_name = + "UninstantiatedTypeParameterizedTestSuite<" + testcase.first + ">"; + RegisterTest( // + "GoogleTestVerification", full_name.c_str(), + nullptr, // No type parameter. + nullptr, // No value parameter. + testcase.second.code_location.file.c_str(), + testcase.second.code_location.line, [message, testcase] { + return new FailureTest(testcase.second.code_location, message, + kErrorOnUninstantiatedTypeParameterizedTest); + }); + } +} + +// A copy of all command line arguments. Set by InitGoogleTest(). +static ::std::vector g_argvs; + +::std::vector GetArgvs() { +#if defined(GTEST_CUSTOM_GET_ARGVS_) + // GTEST_CUSTOM_GET_ARGVS_() may return a container of std::string or + // ::string. This code converts it to the appropriate type. + const auto& custom = GTEST_CUSTOM_GET_ARGVS_(); + return ::std::vector(custom.begin(), custom.end()); +#else // defined(GTEST_CUSTOM_GET_ARGVS_) + return g_argvs; +#endif // defined(GTEST_CUSTOM_GET_ARGVS_) +} + +// Returns the current application's name, removing directory path if that +// is present. +FilePath GetCurrentExecutableName() { + FilePath result; + +#if GTEST_OS_WINDOWS || GTEST_OS_OS2 + result.Set(FilePath(GetArgvs()[0]).RemoveExtension("exe")); +#else + result.Set(FilePath(GetArgvs()[0])); +#endif // GTEST_OS_WINDOWS + + return result.RemoveDirectoryName(); +} + +// Functions for processing the gtest_output flag. + +// Returns the output format, or "" for normal printed output. +std::string UnitTestOptions::GetOutputFormat() { + std::string s = GTEST_FLAG_GET(output); + const char* const gtest_output_flag = s.c_str(); + const char* const colon = strchr(gtest_output_flag, ':'); + return (colon == nullptr) + ? std::string(gtest_output_flag) + : std::string(gtest_output_flag, + static_cast(colon - gtest_output_flag)); +} + +// Returns the name of the requested output file, or the default if none +// was explicitly specified. +std::string UnitTestOptions::GetAbsolutePathToOutputFile() { + std::string s = GTEST_FLAG_GET(output); + const char* const gtest_output_flag = s.c_str(); + + std::string format = GetOutputFormat(); + if (format.empty()) format = std::string(kDefaultOutputFormat); + + const char* const colon = strchr(gtest_output_flag, ':'); + if (colon == nullptr) + return internal::FilePath::MakeFileName( + internal::FilePath( + UnitTest::GetInstance()->original_working_dir()), + internal::FilePath(kDefaultOutputFile), 0, format.c_str()) + .string(); + + internal::FilePath output_name(colon + 1); + if (!output_name.IsAbsolutePath()) + output_name = internal::FilePath::ConcatPaths( + internal::FilePath(UnitTest::GetInstance()->original_working_dir()), + internal::FilePath(colon + 1)); + + if (!output_name.IsDirectory()) return output_name.string(); + + internal::FilePath result(internal::FilePath::GenerateUniqueFileName( + output_name, internal::GetCurrentExecutableName(), + GetOutputFormat().c_str())); + return result.string(); +} + +// Returns true if and only if the wildcard pattern matches the string. Each +// pattern consists of regular characters, single-character wildcards (?), and +// multi-character wildcards (*). +// +// This function implements a linear-time string globbing algorithm based on +// https://research.swtch.com/glob. +static bool PatternMatchesString(const std::string& name_str, + const char* pattern, const char* pattern_end) { + const char* name = name_str.c_str(); + const char* const name_begin = name; + const char* const name_end = name + name_str.size(); + + const char* pattern_next = pattern; + const char* name_next = name; + + while (pattern < pattern_end || name < name_end) { + if (pattern < pattern_end) { + switch (*pattern) { + default: // Match an ordinary character. + if (name < name_end && *name == *pattern) { + ++pattern; + ++name; + continue; + } + break; + case '?': // Match any single character. + if (name < name_end) { + ++pattern; + ++name; + continue; + } + break; + case '*': + // Match zero or more characters. Start by skipping over the wildcard + // and matching zero characters from name. If that fails, restart and + // match one more character than the last attempt. + pattern_next = pattern; + name_next = name + 1; + ++pattern; + continue; + } + } + // Failed to match a character. Restart if possible. + if (name_begin < name_next && name_next <= name_end) { + pattern = pattern_next; + name = name_next; + continue; + } + return false; + } + return true; +} + +namespace { + +bool IsGlobPattern(const std::string& pattern) { + return std::any_of(pattern.begin(), pattern.end(), + [](const char c) { return c == '?' || c == '*'; }); +} + +class UnitTestFilter { + public: + UnitTestFilter() = default; + + // Constructs a filter from a string of patterns separated by `:`. + explicit UnitTestFilter(const std::string& filter) { + // By design "" filter matches "" string. + std::vector all_patterns; + SplitString(filter, ':', &all_patterns); + const auto exact_match_patterns_begin = std::partition( + all_patterns.begin(), all_patterns.end(), &IsGlobPattern); + + glob_patterns_.reserve(static_cast( + std::distance(all_patterns.begin(), exact_match_patterns_begin))); + std::move(all_patterns.begin(), exact_match_patterns_begin, + std::inserter(glob_patterns_, glob_patterns_.begin())); + std::move( + exact_match_patterns_begin, all_patterns.end(), + std::inserter(exact_match_patterns_, exact_match_patterns_.begin())); + } + + // Returns true if and only if name matches at least one of the patterns in + // the filter. + bool MatchesName(const std::string& name) const { + return exact_match_patterns_.count(name) > 0 || + std::any_of(glob_patterns_.begin(), glob_patterns_.end(), + [&name](const std::string& pattern) { + return PatternMatchesString( + name, pattern.c_str(), + pattern.c_str() + pattern.size()); + }); + } + + private: + std::vector glob_patterns_; + std::unordered_set exact_match_patterns_; +}; + +class PositiveAndNegativeUnitTestFilter { + public: + // Constructs a positive and a negative filter from a string. The string + // contains a positive filter optionally followed by a '-' character and a + // negative filter. In case only a negative filter is provided the positive + // filter will be assumed "*". + // A filter is a list of patterns separated by ':'. + explicit PositiveAndNegativeUnitTestFilter(const std::string& filter) { + std::vector positive_and_negative_filters; + + // NOTE: `SplitString` always returns a non-empty container. + SplitString(filter, '-', &positive_and_negative_filters); + const auto& positive_filter = positive_and_negative_filters.front(); + + if (positive_and_negative_filters.size() > 1) { + positive_filter_ = UnitTestFilter( + positive_filter.empty() ? kUniversalFilter : positive_filter); + + // TODO(b/214626361): Fail on multiple '-' characters + // For the moment to preserve old behavior we concatenate the rest of the + // string parts with `-` as separator to generate the negative filter. + auto negative_filter_string = positive_and_negative_filters[1]; + for (std::size_t i = 2; i < positive_and_negative_filters.size(); i++) + negative_filter_string = + negative_filter_string + '-' + positive_and_negative_filters[i]; + negative_filter_ = UnitTestFilter(negative_filter_string); + } else { + // In case we don't have a negative filter and positive filter is "" + // we do not use kUniversalFilter by design as opposed to when we have a + // negative filter. + positive_filter_ = UnitTestFilter(positive_filter); + } + } + + // Returns true if and only if test name (this is generated by appending test + // suit name and test name via a '.' character) matches the positive filter + // and does not match the negative filter. + bool MatchesTest(const std::string& test_suite_name, + const std::string& test_name) const { + return MatchesName(test_suite_name + "." + test_name); + } + + // Returns true if and only if name matches the positive filter and does not + // match the negative filter. + bool MatchesName(const std::string& name) const { + return positive_filter_.MatchesName(name) && + !negative_filter_.MatchesName(name); + } + + private: + UnitTestFilter positive_filter_; + UnitTestFilter negative_filter_; +}; +} // namespace + +bool UnitTestOptions::MatchesFilter(const std::string& name_str, + const char* filter) { + return UnitTestFilter(filter).MatchesName(name_str); +} + +// Returns true if and only if the user-specified filter matches the test +// suite name and the test name. +bool UnitTestOptions::FilterMatchesTest(const std::string& test_suite_name, + const std::string& test_name) { + // Split --gtest_filter at '-', if there is one, to separate into + // positive filter and negative filter portions + return PositiveAndNegativeUnitTestFilter(GTEST_FLAG_GET(filter)) + .MatchesTest(test_suite_name, test_name); +} + +#if GTEST_HAS_SEH +// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the +// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. +// This function is useful as an __except condition. +int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) { + // Google Test should handle a SEH exception if: + // 1. the user wants it to, AND + // 2. this is not a breakpoint exception, AND + // 3. this is not a C++ exception (VC++ implements them via SEH, + // apparently). + // + // SEH exception code for C++ exceptions. + // (see http://support.microsoft.com/kb/185294 for more information). + const DWORD kCxxExceptionCode = 0xe06d7363; + + bool should_handle = true; + + if (!GTEST_FLAG_GET(catch_exceptions)) + should_handle = false; + else if (exception_code == EXCEPTION_BREAKPOINT) + should_handle = false; + else if (exception_code == kCxxExceptionCode) + should_handle = false; + + return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; +} +#endif // GTEST_HAS_SEH + +} // namespace internal + +// The c'tor sets this object as the test part result reporter used by +// Google Test. The 'result' parameter specifies where to report the +// results. Intercepts only failures from the current thread. +ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( + TestPartResultArray* result) + : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD), result_(result) { + Init(); +} + +// The c'tor sets this object as the test part result reporter used by +// Google Test. The 'result' parameter specifies where to report the +// results. +ScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( + InterceptMode intercept_mode, TestPartResultArray* result) + : intercept_mode_(intercept_mode), result_(result) { + Init(); +} + +void ScopedFakeTestPartResultReporter::Init() { + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + if (intercept_mode_ == INTERCEPT_ALL_THREADS) { + old_reporter_ = impl->GetGlobalTestPartResultReporter(); + impl->SetGlobalTestPartResultReporter(this); + } else { + old_reporter_ = impl->GetTestPartResultReporterForCurrentThread(); + impl->SetTestPartResultReporterForCurrentThread(this); + } +} + +// The d'tor restores the test part result reporter used by Google Test +// before. +ScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() { + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + if (intercept_mode_ == INTERCEPT_ALL_THREADS) { + impl->SetGlobalTestPartResultReporter(old_reporter_); + } else { + impl->SetTestPartResultReporterForCurrentThread(old_reporter_); + } +} + +// Increments the test part result count and remembers the result. +// This method is from the TestPartResultReporterInterface interface. +void ScopedFakeTestPartResultReporter::ReportTestPartResult( + const TestPartResult& result) { + result_->Append(result); +} + +namespace internal { + +// Returns the type ID of ::testing::Test. We should always call this +// instead of GetTypeId< ::testing::Test>() to get the type ID of +// testing::Test. This is to work around a suspected linker bug when +// using Google Test as a framework on Mac OS X. The bug causes +// GetTypeId< ::testing::Test>() to return different values depending +// on whether the call is from the Google Test framework itself or +// from user test code. GetTestTypeId() is guaranteed to always +// return the same value, as it always calls GetTypeId<>() from the +// gtest.cc, which is within the Google Test framework. +TypeId GetTestTypeId() { return GetTypeId(); } + +// The value of GetTestTypeId() as seen from within the Google Test +// library. This is solely for testing GetTestTypeId(). +extern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId(); + +// This predicate-formatter checks that 'results' contains a test part +// failure of the given type and that the failure message contains the +// given substring. +static AssertionResult HasOneFailure(const char* /* results_expr */, + const char* /* type_expr */, + const char* /* substr_expr */, + const TestPartResultArray& results, + TestPartResult::Type type, + const std::string& substr) { + const std::string expected(type == TestPartResult::kFatalFailure + ? "1 fatal failure" + : "1 non-fatal failure"); + Message msg; + if (results.size() != 1) { + msg << "Expected: " << expected << "\n" + << " Actual: " << results.size() << " failures"; + for (int i = 0; i < results.size(); i++) { + msg << "\n" << results.GetTestPartResult(i); + } + return AssertionFailure() << msg; + } + + const TestPartResult& r = results.GetTestPartResult(0); + if (r.type() != type) { + return AssertionFailure() << "Expected: " << expected << "\n" + << " Actual:\n" + << r; + } + + if (strstr(r.message(), substr.c_str()) == nullptr) { + return AssertionFailure() + << "Expected: " << expected << " containing \"" << substr << "\"\n" + << " Actual:\n" + << r; + } + + return AssertionSuccess(); +} + +// The constructor of SingleFailureChecker remembers where to look up +// test part results, what type of failure we expect, and what +// substring the failure message should contain. +SingleFailureChecker::SingleFailureChecker(const TestPartResultArray* results, + TestPartResult::Type type, + const std::string& substr) + : results_(results), type_(type), substr_(substr) {} + +// The destructor of SingleFailureChecker verifies that the given +// TestPartResultArray contains exactly one failure that has the given +// type and contains the given substring. If that's not the case, a +// non-fatal failure will be generated. +SingleFailureChecker::~SingleFailureChecker() { + EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_); +} + +DefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter( + UnitTestImpl* unit_test) + : unit_test_(unit_test) {} + +void DefaultGlobalTestPartResultReporter::ReportTestPartResult( + const TestPartResult& result) { + unit_test_->current_test_result()->AddTestPartResult(result); + unit_test_->listeners()->repeater()->OnTestPartResult(result); +} + +DefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter( + UnitTestImpl* unit_test) + : unit_test_(unit_test) {} + +void DefaultPerThreadTestPartResultReporter::ReportTestPartResult( + const TestPartResult& result) { + unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result); +} + +// Returns the global test part result reporter. +TestPartResultReporterInterface* +UnitTestImpl::GetGlobalTestPartResultReporter() { + internal::MutexLock lock(&global_test_part_result_reporter_mutex_); + return global_test_part_result_repoter_; +} + +// Sets the global test part result reporter. +void UnitTestImpl::SetGlobalTestPartResultReporter( + TestPartResultReporterInterface* reporter) { + internal::MutexLock lock(&global_test_part_result_reporter_mutex_); + global_test_part_result_repoter_ = reporter; +} + +// Returns the test part result reporter for the current thread. +TestPartResultReporterInterface* +UnitTestImpl::GetTestPartResultReporterForCurrentThread() { + return per_thread_test_part_result_reporter_.get(); +} + +// Sets the test part result reporter for the current thread. +void UnitTestImpl::SetTestPartResultReporterForCurrentThread( + TestPartResultReporterInterface* reporter) { + per_thread_test_part_result_reporter_.set(reporter); +} + +// Gets the number of successful test suites. +int UnitTestImpl::successful_test_suite_count() const { + return CountIf(test_suites_, TestSuitePassed); +} + +// Gets the number of failed test suites. +int UnitTestImpl::failed_test_suite_count() const { + return CountIf(test_suites_, TestSuiteFailed); +} + +// Gets the number of all test suites. +int UnitTestImpl::total_test_suite_count() const { + return static_cast(test_suites_.size()); +} + +// Gets the number of all test suites that contain at least one test +// that should run. +int UnitTestImpl::test_suite_to_run_count() const { + return CountIf(test_suites_, ShouldRunTestSuite); +} + +// Gets the number of successful tests. +int UnitTestImpl::successful_test_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::successful_test_count); +} + +// Gets the number of skipped tests. +int UnitTestImpl::skipped_test_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::skipped_test_count); +} + +// Gets the number of failed tests. +int UnitTestImpl::failed_test_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::failed_test_count); +} + +// Gets the number of disabled tests that will be reported in the XML report. +int UnitTestImpl::reportable_disabled_test_count() const { + return SumOverTestSuiteList(test_suites_, + &TestSuite::reportable_disabled_test_count); +} + +// Gets the number of disabled tests. +int UnitTestImpl::disabled_test_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::disabled_test_count); +} + +// Gets the number of tests to be printed in the XML report. +int UnitTestImpl::reportable_test_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::reportable_test_count); +} + +// Gets the number of all tests. +int UnitTestImpl::total_test_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::total_test_count); +} + +// Gets the number of tests that should run. +int UnitTestImpl::test_to_run_count() const { + return SumOverTestSuiteList(test_suites_, &TestSuite::test_to_run_count); +} + +// Returns the current OS stack trace as an std::string. +// +// The maximum number of stack frames to be included is specified by +// the gtest_stack_trace_depth flag. The skip_count parameter +// specifies the number of top frames to be skipped, which doesn't +// count against the number of frames to be included. +// +// For example, if Foo() calls Bar(), which in turn calls +// CurrentOsStackTraceExceptTop(1), Foo() will be included in the +// trace but Bar() and CurrentOsStackTraceExceptTop() won't. +std::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) { + return os_stack_trace_getter()->CurrentStackTrace( + static_cast(GTEST_FLAG_GET(stack_trace_depth)), skip_count + 1 + // Skips the user-specified number of frames plus this function + // itself. + ); // NOLINT +} + +// A helper class for measuring elapsed times. +class Timer { + public: + Timer() : start_(std::chrono::steady_clock::now()) {} + + // Return time elapsed in milliseconds since the timer was created. + TimeInMillis Elapsed() { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_) + .count(); + } + + private: + std::chrono::steady_clock::time_point start_; +}; + +// Returns a timestamp as milliseconds since the epoch. Note this time may jump +// around subject to adjustments by the system, to measure elapsed time use +// Timer instead. +TimeInMillis GetTimeInMillis() { + return std::chrono::duration_cast( + std::chrono::system_clock::now() - + std::chrono::system_clock::from_time_t(0)) + .count(); +} + +// Utilities + +// class String. + +#if GTEST_OS_WINDOWS_MOBILE +// Creates a UTF-16 wide string from the given ANSI string, allocating +// memory using new. The caller is responsible for deleting the return +// value using delete[]. Returns the wide string, or NULL if the +// input is NULL. +LPCWSTR String::AnsiToUtf16(const char* ansi) { + if (!ansi) return nullptr; + const int length = strlen(ansi); + const int unicode_length = + MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0); + WCHAR* unicode = new WCHAR[unicode_length + 1]; + MultiByteToWideChar(CP_ACP, 0, ansi, length, unicode, unicode_length); + unicode[unicode_length] = 0; + return unicode; +} + +// Creates an ANSI string from the given wide string, allocating +// memory using new. The caller is responsible for deleting the return +// value using delete[]. Returns the ANSI string, or NULL if the +// input is NULL. +const char* String::Utf16ToAnsi(LPCWSTR utf16_str) { + if (!utf16_str) return nullptr; + const int ansi_length = WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, nullptr, + 0, nullptr, nullptr); + char* ansi = new char[ansi_length + 1]; + WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, ansi, ansi_length, nullptr, + nullptr); + ansi[ansi_length] = 0; + return ansi; +} + +#endif // GTEST_OS_WINDOWS_MOBILE + +// Compares two C strings. Returns true if and only if they have the same +// content. +// +// Unlike strcmp(), this function can handle NULL argument(s). A NULL +// C string is considered different to any non-NULL C string, +// including the empty string. +bool String::CStringEquals(const char* lhs, const char* rhs) { + if (lhs == nullptr) return rhs == nullptr; + + if (rhs == nullptr) return false; + + return strcmp(lhs, rhs) == 0; +} + +#if GTEST_HAS_STD_WSTRING + +// Converts an array of wide chars to a narrow string using the UTF-8 +// encoding, and streams the result to the given Message object. +static void StreamWideCharsToMessage(const wchar_t* wstr, size_t length, + Message* msg) { + for (size_t i = 0; i != length;) { // NOLINT + if (wstr[i] != L'\0') { + *msg << WideStringToUtf8(wstr + i, static_cast(length - i)); + while (i != length && wstr[i] != L'\0') i++; + } else { + *msg << '\0'; + i++; + } + } +} + +#endif // GTEST_HAS_STD_WSTRING + +void SplitString(const ::std::string& str, char delimiter, + ::std::vector< ::std::string>* dest) { + ::std::vector< ::std::string> parsed; + ::std::string::size_type pos = 0; + while (::testing::internal::AlwaysTrue()) { + const ::std::string::size_type colon = str.find(delimiter, pos); + if (colon == ::std::string::npos) { + parsed.push_back(str.substr(pos)); + break; + } else { + parsed.push_back(str.substr(pos, colon - pos)); + pos = colon + 1; + } + } + dest->swap(parsed); +} + +} // namespace internal + +// Constructs an empty Message. +// We allocate the stringstream separately because otherwise each use of +// ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's +// stack frame leading to huge stack frames in some cases; gcc does not reuse +// the stack space. +Message::Message() : ss_(new ::std::stringstream) { + // By default, we want there to be enough precision when printing + // a double to a Message. + *ss_ << std::setprecision(std::numeric_limits::digits10 + 2); +} + +// These two overloads allow streaming a wide C string to a Message +// using the UTF-8 encoding. +Message& Message::operator<<(const wchar_t* wide_c_str) { + return *this << internal::String::ShowWideCString(wide_c_str); +} +Message& Message::operator<<(wchar_t* wide_c_str) { + return *this << internal::String::ShowWideCString(wide_c_str); +} + +#if GTEST_HAS_STD_WSTRING +// Converts the given wide string to a narrow string using the UTF-8 +// encoding, and streams the result to this Message object. +Message& Message::operator<<(const ::std::wstring& wstr) { + internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); + return *this; +} +#endif // GTEST_HAS_STD_WSTRING + +// Gets the text streamed to this object so far as an std::string. +// Each '\0' character in the buffer is replaced with "\\0". +std::string Message::GetString() const { + return internal::StringStreamToString(ss_.get()); +} + +namespace internal { + +namespace edit_distance { +std::vector CalculateOptimalEdits(const std::vector& left, + const std::vector& right) { + std::vector > costs( + left.size() + 1, std::vector(right.size() + 1)); + std::vector > best_move( + left.size() + 1, std::vector(right.size() + 1)); + + // Populate for empty right. + for (size_t l_i = 0; l_i < costs.size(); ++l_i) { + costs[l_i][0] = static_cast(l_i); + best_move[l_i][0] = kRemove; + } + // Populate for empty left. + for (size_t r_i = 1; r_i < costs[0].size(); ++r_i) { + costs[0][r_i] = static_cast(r_i); + best_move[0][r_i] = kAdd; + } + + for (size_t l_i = 0; l_i < left.size(); ++l_i) { + for (size_t r_i = 0; r_i < right.size(); ++r_i) { + if (left[l_i] == right[r_i]) { + // Found a match. Consume it. + costs[l_i + 1][r_i + 1] = costs[l_i][r_i]; + best_move[l_i + 1][r_i + 1] = kMatch; + continue; + } + + const double add = costs[l_i + 1][r_i]; + const double remove = costs[l_i][r_i + 1]; + const double replace = costs[l_i][r_i]; + if (add < remove && add < replace) { + costs[l_i + 1][r_i + 1] = add + 1; + best_move[l_i + 1][r_i + 1] = kAdd; + } else if (remove < add && remove < replace) { + costs[l_i + 1][r_i + 1] = remove + 1; + best_move[l_i + 1][r_i + 1] = kRemove; + } else { + // We make replace a little more expensive than add/remove to lower + // their priority. + costs[l_i + 1][r_i + 1] = replace + 1.00001; + best_move[l_i + 1][r_i + 1] = kReplace; + } + } + } + + // Reconstruct the best path. We do it in reverse order. + std::vector best_path; + for (size_t l_i = left.size(), r_i = right.size(); l_i > 0 || r_i > 0;) { + EditType move = best_move[l_i][r_i]; + best_path.push_back(move); + l_i -= move != kAdd; + r_i -= move != kRemove; + } + std::reverse(best_path.begin(), best_path.end()); + return best_path; +} + +namespace { + +// Helper class to convert string into ids with deduplication. +class InternalStrings { + public: + size_t GetId(const std::string& str) { + IdMap::iterator it = ids_.find(str); + if (it != ids_.end()) return it->second; + size_t id = ids_.size(); + return ids_[str] = id; + } + + private: + typedef std::map IdMap; + IdMap ids_; +}; + +} // namespace + +std::vector CalculateOptimalEdits( + const std::vector& left, + const std::vector& right) { + std::vector left_ids, right_ids; + { + InternalStrings intern_table; + for (size_t i = 0; i < left.size(); ++i) { + left_ids.push_back(intern_table.GetId(left[i])); + } + for (size_t i = 0; i < right.size(); ++i) { + right_ids.push_back(intern_table.GetId(right[i])); + } + } + return CalculateOptimalEdits(left_ids, right_ids); +} + +namespace { + +// Helper class that holds the state for one hunk and prints it out to the +// stream. +// It reorders adds/removes when possible to group all removes before all +// adds. It also adds the hunk header before printint into the stream. +class Hunk { + public: + Hunk(size_t left_start, size_t right_start) + : left_start_(left_start), + right_start_(right_start), + adds_(), + removes_(), + common_() {} + + void PushLine(char edit, const char* line) { + switch (edit) { + case ' ': + ++common_; + FlushEdits(); + hunk_.push_back(std::make_pair(' ', line)); + break; + case '-': + ++removes_; + hunk_removes_.push_back(std::make_pair('-', line)); + break; + case '+': + ++adds_; + hunk_adds_.push_back(std::make_pair('+', line)); + break; + } + } + + void PrintTo(std::ostream* os) { + PrintHeader(os); + FlushEdits(); + for (std::list >::const_iterator it = + hunk_.begin(); + it != hunk_.end(); ++it) { + *os << it->first << it->second << "\n"; + } + } + + bool has_edits() const { return adds_ || removes_; } + + private: + void FlushEdits() { + hunk_.splice(hunk_.end(), hunk_removes_); + hunk_.splice(hunk_.end(), hunk_adds_); + } + + // Print a unified diff header for one hunk. + // The format is + // "@@ -, +, @@" + // where the left/right parts are omitted if unnecessary. + void PrintHeader(std::ostream* ss) const { + *ss << "@@ "; + if (removes_) { + *ss << "-" << left_start_ << "," << (removes_ + common_); + } + if (removes_ && adds_) { + *ss << " "; + } + if (adds_) { + *ss << "+" << right_start_ << "," << (adds_ + common_); + } + *ss << " @@\n"; + } + + size_t left_start_, right_start_; + size_t adds_, removes_, common_; + std::list > hunk_, hunk_adds_, hunk_removes_; +}; + +} // namespace + +// Create a list of diff hunks in Unified diff format. +// Each hunk has a header generated by PrintHeader above plus a body with +// lines prefixed with ' ' for no change, '-' for deletion and '+' for +// addition. +// 'context' represents the desired unchanged prefix/suffix around the diff. +// If two hunks are close enough that their contexts overlap, then they are +// joined into one hunk. +std::string CreateUnifiedDiff(const std::vector& left, + const std::vector& right, + size_t context) { + const std::vector edits = CalculateOptimalEdits(left, right); + + size_t l_i = 0, r_i = 0, edit_i = 0; + std::stringstream ss; + while (edit_i < edits.size()) { + // Find first edit. + while (edit_i < edits.size() && edits[edit_i] == kMatch) { + ++l_i; + ++r_i; + ++edit_i; + } + + // Find the first line to include in the hunk. + const size_t prefix_context = std::min(l_i, context); + Hunk hunk(l_i - prefix_context + 1, r_i - prefix_context + 1); + for (size_t i = prefix_context; i > 0; --i) { + hunk.PushLine(' ', left[l_i - i].c_str()); + } + + // Iterate the edits until we found enough suffix for the hunk or the input + // is over. + size_t n_suffix = 0; + for (; edit_i < edits.size(); ++edit_i) { + if (n_suffix >= context) { + // Continue only if the next hunk is very close. + auto it = edits.begin() + static_cast(edit_i); + while (it != edits.end() && *it == kMatch) ++it; + if (it == edits.end() || + static_cast(it - edits.begin()) - edit_i >= context) { + // There is no next edit or it is too far away. + break; + } + } + + EditType edit = edits[edit_i]; + // Reset count when a non match is found. + n_suffix = edit == kMatch ? n_suffix + 1 : 0; + + if (edit == kMatch || edit == kRemove || edit == kReplace) { + hunk.PushLine(edit == kMatch ? ' ' : '-', left[l_i].c_str()); + } + if (edit == kAdd || edit == kReplace) { + hunk.PushLine('+', right[r_i].c_str()); + } + + // Advance indices, depending on edit type. + l_i += edit != kAdd; + r_i += edit != kRemove; + } + + if (!hunk.has_edits()) { + // We are done. We don't want this hunk. + break; + } + + hunk.PrintTo(&ss); + } + return ss.str(); +} + +} // namespace edit_distance + +namespace { + +// The string representation of the values received in EqFailure() are already +// escaped. Split them on escaped '\n' boundaries. Leave all other escaped +// characters the same. +std::vector SplitEscapedString(const std::string& str) { + std::vector lines; + size_t start = 0, end = str.size(); + if (end > 2 && str[0] == '"' && str[end - 1] == '"') { + ++start; + --end; + } + bool escaped = false; + for (size_t i = start; i + 1 < end; ++i) { + if (escaped) { + escaped = false; + if (str[i] == 'n') { + lines.push_back(str.substr(start, i - start - 1)); + start = i + 1; + } + } else { + escaped = str[i] == '\\'; + } + } + lines.push_back(str.substr(start, end - start)); + return lines; +} + +} // namespace + +// Constructs and returns the message for an equality assertion +// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. +// +// The first four parameters are the expressions used in the assertion +// and their values, as strings. For example, for ASSERT_EQ(foo, bar) +// where foo is 5 and bar is 6, we have: +// +// lhs_expression: "foo" +// rhs_expression: "bar" +// lhs_value: "5" +// rhs_value: "6" +// +// The ignoring_case parameter is true if and only if the assertion is a +// *_STRCASEEQ*. When it's true, the string "Ignoring case" will +// be inserted into the message. +AssertionResult EqFailure(const char* lhs_expression, + const char* rhs_expression, + const std::string& lhs_value, + const std::string& rhs_value, bool ignoring_case) { + Message msg; + msg << "Expected equality of these values:"; + msg << "\n " << lhs_expression; + if (lhs_value != lhs_expression) { + msg << "\n Which is: " << lhs_value; + } + msg << "\n " << rhs_expression; + if (rhs_value != rhs_expression) { + msg << "\n Which is: " << rhs_value; + } + + if (ignoring_case) { + msg << "\nIgnoring case"; + } + + if (!lhs_value.empty() && !rhs_value.empty()) { + const std::vector lhs_lines = SplitEscapedString(lhs_value); + const std::vector rhs_lines = SplitEscapedString(rhs_value); + if (lhs_lines.size() > 1 || rhs_lines.size() > 1) { + msg << "\nWith diff:\n" + << edit_distance::CreateUnifiedDiff(lhs_lines, rhs_lines); + } + } + + return AssertionFailure() << msg; +} + +// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. +std::string GetBoolAssertionFailureMessage( + const AssertionResult& assertion_result, const char* expression_text, + const char* actual_predicate_value, const char* expected_predicate_value) { + const char* actual_message = assertion_result.message(); + Message msg; + msg << "Value of: " << expression_text + << "\n Actual: " << actual_predicate_value; + if (actual_message[0] != '\0') msg << " (" << actual_message << ")"; + msg << "\nExpected: " << expected_predicate_value; + return msg.GetString(); +} + +// Helper function for implementing ASSERT_NEAR. +AssertionResult DoubleNearPredFormat(const char* expr1, const char* expr2, + const char* abs_error_expr, double val1, + double val2, double abs_error) { + const double diff = fabs(val1 - val2); + if (diff <= abs_error) return AssertionSuccess(); + + // Find the value which is closest to zero. + const double min_abs = std::min(fabs(val1), fabs(val2)); + // Find the distance to the next double from that value. + const double epsilon = + nextafter(min_abs, std::numeric_limits::infinity()) - min_abs; + // Detect the case where abs_error is so small that EXPECT_NEAR is + // effectively the same as EXPECT_EQUAL, and give an informative error + // message so that the situation can be more easily understood without + // requiring exotic floating-point knowledge. + // Don't do an epsilon check if abs_error is zero because that implies + // that an equality check was actually intended. + if (!(std::isnan)(val1) && !(std::isnan)(val2) && abs_error > 0 && + abs_error < epsilon) { + return AssertionFailure() + << "The difference between " << expr1 << " and " << expr2 << " is " + << diff << ", where\n" + << expr1 << " evaluates to " << val1 << ",\n" + << expr2 << " evaluates to " << val2 << ".\nThe abs_error parameter " + << abs_error_expr << " evaluates to " << abs_error + << " which is smaller than the minimum distance between doubles for " + "numbers of this magnitude which is " + << epsilon + << ", thus making this EXPECT_NEAR check equivalent to " + "EXPECT_EQUAL. Consider using EXPECT_DOUBLE_EQ instead."; + } + return AssertionFailure() + << "The difference between " << expr1 << " and " << expr2 << " is " + << diff << ", which exceeds " << abs_error_expr << ", where\n" + << expr1 << " evaluates to " << val1 << ",\n" + << expr2 << " evaluates to " << val2 << ", and\n" + << abs_error_expr << " evaluates to " << abs_error << "."; +} + +// Helper template for implementing FloatLE() and DoubleLE(). +template +AssertionResult FloatingPointLE(const char* expr1, const char* expr2, + RawType val1, RawType val2) { + // Returns success if val1 is less than val2, + if (val1 < val2) { + return AssertionSuccess(); + } + + // or if val1 is almost equal to val2. + const FloatingPoint lhs(val1), rhs(val2); + if (lhs.AlmostEquals(rhs)) { + return AssertionSuccess(); + } + + // Note that the above two checks will both fail if either val1 or + // val2 is NaN, as the IEEE floating-point standard requires that + // any predicate involving a NaN must return false. + + ::std::stringstream val1_ss; + val1_ss << std::setprecision(std::numeric_limits::digits10 + 2) + << val1; + + ::std::stringstream val2_ss; + val2_ss << std::setprecision(std::numeric_limits::digits10 + 2) + << val2; + + return AssertionFailure() + << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n" + << " Actual: " << StringStreamToString(&val1_ss) << " vs " + << StringStreamToString(&val2_ss); +} + +} // namespace internal + +// Asserts that val1 is less than, or almost equal to, val2. Fails +// otherwise. In particular, it fails if either val1 or val2 is NaN. +AssertionResult FloatLE(const char* expr1, const char* expr2, float val1, + float val2) { + return internal::FloatingPointLE(expr1, expr2, val1, val2); +} + +// Asserts that val1 is less than, or almost equal to, val2. Fails +// otherwise. In particular, it fails if either val1 or val2 is NaN. +AssertionResult DoubleLE(const char* expr1, const char* expr2, double val1, + double val2) { + return internal::FloatingPointLE(expr1, expr2, val1, val2); +} + +namespace internal { + +// The helper function for {ASSERT|EXPECT}_STREQ. +AssertionResult CmpHelperSTREQ(const char* lhs_expression, + const char* rhs_expression, const char* lhs, + const char* rhs) { + if (String::CStringEquals(lhs, rhs)) { + return AssertionSuccess(); + } + + return EqFailure(lhs_expression, rhs_expression, PrintToString(lhs), + PrintToString(rhs), false); +} + +// The helper function for {ASSERT|EXPECT}_STRCASEEQ. +AssertionResult CmpHelperSTRCASEEQ(const char* lhs_expression, + const char* rhs_expression, const char* lhs, + const char* rhs) { + if (String::CaseInsensitiveCStringEquals(lhs, rhs)) { + return AssertionSuccess(); + } + + return EqFailure(lhs_expression, rhs_expression, PrintToString(lhs), + PrintToString(rhs), true); +} + +// The helper function for {ASSERT|EXPECT}_STRNE. +AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, const char* s1, + const char* s2) { + if (!String::CStringEquals(s1, s2)) { + return AssertionSuccess(); + } else { + return AssertionFailure() + << "Expected: (" << s1_expression << ") != (" << s2_expression + << "), actual: \"" << s1 << "\" vs \"" << s2 << "\""; + } +} + +// The helper function for {ASSERT|EXPECT}_STRCASENE. +AssertionResult CmpHelperSTRCASENE(const char* s1_expression, + const char* s2_expression, const char* s1, + const char* s2) { + if (!String::CaseInsensitiveCStringEquals(s1, s2)) { + return AssertionSuccess(); + } else { + return AssertionFailure() + << "Expected: (" << s1_expression << ") != (" << s2_expression + << ") (ignoring case), actual: \"" << s1 << "\" vs \"" << s2 << "\""; + } +} + +} // namespace internal + +namespace { + +// Helper functions for implementing IsSubString() and IsNotSubstring(). + +// This group of overloaded functions return true if and only if needle +// is a substring of haystack. NULL is considered a substring of +// itself only. + +bool IsSubstringPred(const char* needle, const char* haystack) { + if (needle == nullptr || haystack == nullptr) return needle == haystack; + + return strstr(haystack, needle) != nullptr; +} + +bool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) { + if (needle == nullptr || haystack == nullptr) return needle == haystack; + + return wcsstr(haystack, needle) != nullptr; +} + +// StringType here can be either ::std::string or ::std::wstring. +template +bool IsSubstringPred(const StringType& needle, const StringType& haystack) { + return haystack.find(needle) != StringType::npos; +} + +// This function implements either IsSubstring() or IsNotSubstring(), +// depending on the value of the expected_to_be_substring parameter. +// StringType here can be const char*, const wchar_t*, ::std::string, +// or ::std::wstring. +template +AssertionResult IsSubstringImpl(bool expected_to_be_substring, + const char* needle_expr, + const char* haystack_expr, + const StringType& needle, + const StringType& haystack) { + if (IsSubstringPred(needle, haystack) == expected_to_be_substring) + return AssertionSuccess(); + + const bool is_wide_string = sizeof(needle[0]) > 1; + const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; + return AssertionFailure() + << "Value of: " << needle_expr << "\n" + << " Actual: " << begin_string_quote << needle << "\"\n" + << "Expected: " << (expected_to_be_substring ? "" : "not ") + << "a substring of " << haystack_expr << "\n" + << "Which is: " << begin_string_quote << haystack << "\""; +} + +} // namespace + +// IsSubstring() and IsNotSubstring() check whether needle is a +// substring of haystack (NULL is considered a substring of itself +// only), and return an appropriate error message when they fail. + +AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr, + const char* needle, const char* haystack) { + return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); +} + +AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr, + const wchar_t* needle, const wchar_t* haystack) { + return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); +} + +AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, const char* needle, + const char* haystack) { + return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); +} + +AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, const wchar_t* needle, + const wchar_t* haystack) { + return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); +} + +AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr, + const ::std::string& needle, + const ::std::string& haystack) { + return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); +} + +AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, + const ::std::string& needle, + const ::std::string& haystack) { + return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); +} + +#if GTEST_HAS_STD_WSTRING +AssertionResult IsSubstring(const char* needle_expr, const char* haystack_expr, + const ::std::wstring& needle, + const ::std::wstring& haystack) { + return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); +} + +AssertionResult IsNotSubstring(const char* needle_expr, + const char* haystack_expr, + const ::std::wstring& needle, + const ::std::wstring& haystack) { + return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); +} +#endif // GTEST_HAS_STD_WSTRING + +namespace internal { + +#if GTEST_OS_WINDOWS + +namespace { + +// Helper function for IsHRESULT{SuccessFailure} predicates +AssertionResult HRESULTFailureHelper(const char* expr, const char* expected, + long hr) { // NOLINT +#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_TV_TITLE + + // Windows CE doesn't support FormatMessage. + const char error_text[] = ""; + +#else + + // Looks up the human-readable system message for the HRESULT code + // and since we're not passing any params to FormatMessage, we don't + // want inserts expanded. + const DWORD kFlags = + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; + const DWORD kBufSize = 4096; + // Gets the system's human readable message string for this HRESULT. + char error_text[kBufSize] = {'\0'}; + DWORD message_length = ::FormatMessageA(kFlags, + 0, // no source, we're asking system + static_cast(hr), // the error + 0, // no line width restrictions + error_text, // output buffer + kBufSize, // buf size + nullptr); // no arguments for inserts + // Trims tailing white space (FormatMessage leaves a trailing CR-LF) + for (; message_length && IsSpace(error_text[message_length - 1]); + --message_length) { + error_text[message_length - 1] = '\0'; + } + +#endif // GTEST_OS_WINDOWS_MOBILE + + const std::string error_hex("0x" + String::FormatHexInt(hr)); + return ::testing::AssertionFailure() + << "Expected: " << expr << " " << expected << ".\n" + << " Actual: " << error_hex << " " << error_text << "\n"; +} + +} // namespace + +AssertionResult IsHRESULTSuccess(const char* expr, long hr) { // NOLINT + if (SUCCEEDED(hr)) { + return AssertionSuccess(); + } + return HRESULTFailureHelper(expr, "succeeds", hr); +} + +AssertionResult IsHRESULTFailure(const char* expr, long hr) { // NOLINT + if (FAILED(hr)) { + return AssertionSuccess(); + } + return HRESULTFailureHelper(expr, "fails", hr); +} + +#endif // GTEST_OS_WINDOWS + +// Utility functions for encoding Unicode text (wide strings) in +// UTF-8. + +// A Unicode code-point can have up to 21 bits, and is encoded in UTF-8 +// like this: +// +// Code-point length Encoding +// 0 - 7 bits 0xxxxxxx +// 8 - 11 bits 110xxxxx 10xxxxxx +// 12 - 16 bits 1110xxxx 10xxxxxx 10xxxxxx +// 17 - 21 bits 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + +// The maximum code-point a one-byte UTF-8 sequence can represent. +constexpr uint32_t kMaxCodePoint1 = (static_cast(1) << 7) - 1; + +// The maximum code-point a two-byte UTF-8 sequence can represent. +constexpr uint32_t kMaxCodePoint2 = (static_cast(1) << (5 + 6)) - 1; + +// The maximum code-point a three-byte UTF-8 sequence can represent. +constexpr uint32_t kMaxCodePoint3 = + (static_cast(1) << (4 + 2 * 6)) - 1; + +// The maximum code-point a four-byte UTF-8 sequence can represent. +constexpr uint32_t kMaxCodePoint4 = + (static_cast(1) << (3 + 3 * 6)) - 1; + +// Chops off the n lowest bits from a bit pattern. Returns the n +// lowest bits. As a side effect, the original bit pattern will be +// shifted to the right by n bits. +inline uint32_t ChopLowBits(uint32_t* bits, int n) { + const uint32_t low_bits = *bits & ((static_cast(1) << n) - 1); + *bits >>= n; + return low_bits; +} + +// Converts a Unicode code point to a narrow string in UTF-8 encoding. +// code_point parameter is of type uint32_t because wchar_t may not be +// wide enough to contain a code point. +// If the code_point is not a valid Unicode code point +// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted +// to "(Invalid Unicode 0xXXXXXXXX)". +std::string CodePointToUtf8(uint32_t code_point) { + if (code_point > kMaxCodePoint4) { + return "(Invalid Unicode 0x" + String::FormatHexUInt32(code_point) + ")"; + } + + char str[5]; // Big enough for the largest valid code point. + if (code_point <= kMaxCodePoint1) { + str[1] = '\0'; + str[0] = static_cast(code_point); // 0xxxxxxx + } else if (code_point <= kMaxCodePoint2) { + str[2] = '\0'; + str[1] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx + str[0] = static_cast(0xC0 | code_point); // 110xxxxx + } else if (code_point <= kMaxCodePoint3) { + str[3] = '\0'; + str[2] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx + str[1] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx + str[0] = static_cast(0xE0 | code_point); // 1110xxxx + } else { // code_point <= kMaxCodePoint4 + str[4] = '\0'; + str[3] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx + str[2] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx + str[1] = static_cast(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx + str[0] = static_cast(0xF0 | code_point); // 11110xxx + } + return str; +} + +// The following two functions only make sense if the system +// uses UTF-16 for wide string encoding. All supported systems +// with 16 bit wchar_t (Windows, Cygwin) do use UTF-16. + +// Determines if the arguments constitute UTF-16 surrogate pair +// and thus should be combined into a single Unicode code point +// using CreateCodePointFromUtf16SurrogatePair. +inline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) { + return sizeof(wchar_t) == 2 && (first & 0xFC00) == 0xD800 && + (second & 0xFC00) == 0xDC00; +} + +// Creates a Unicode code point from UTF16 surrogate pair. +inline uint32_t CreateCodePointFromUtf16SurrogatePair(wchar_t first, + wchar_t second) { + const auto first_u = static_cast(first); + const auto second_u = static_cast(second); + const uint32_t mask = (1 << 10) - 1; + return (sizeof(wchar_t) == 2) + ? (((first_u & mask) << 10) | (second_u & mask)) + 0x10000 + : + // This function should not be called when the condition is + // false, but we provide a sensible default in case it is. + first_u; +} + +// Converts a wide string to a narrow string in UTF-8 encoding. +// The wide string is assumed to have the following encoding: +// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin) +// UTF-32 if sizeof(wchar_t) == 4 (on Linux) +// Parameter str points to a null-terminated wide string. +// Parameter num_chars may additionally limit the number +// of wchar_t characters processed. -1 is used when the entire string +// should be processed. +// If the string contains code points that are not valid Unicode code points +// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output +// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding +// and contains invalid UTF-16 surrogate pairs, values in those pairs +// will be encoded as individual Unicode characters from Basic Normal Plane. +std::string WideStringToUtf8(const wchar_t* str, int num_chars) { + if (num_chars == -1) num_chars = static_cast(wcslen(str)); + + ::std::stringstream stream; + for (int i = 0; i < num_chars; ++i) { + uint32_t unicode_code_point; + + if (str[i] == L'\0') { + break; + } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) { + unicode_code_point = + CreateCodePointFromUtf16SurrogatePair(str[i], str[i + 1]); + i++; + } else { + unicode_code_point = static_cast(str[i]); + } + + stream << CodePointToUtf8(unicode_code_point); + } + return StringStreamToString(&stream); +} + +// Converts a wide C string to an std::string using the UTF-8 encoding. +// NULL will be converted to "(null)". +std::string String::ShowWideCString(const wchar_t* wide_c_str) { + if (wide_c_str == nullptr) return "(null)"; + + return internal::WideStringToUtf8(wide_c_str, -1); +} + +// Compares two wide C strings. Returns true if and only if they have the +// same content. +// +// Unlike wcscmp(), this function can handle NULL argument(s). A NULL +// C string is considered different to any non-NULL C string, +// including the empty string. +bool String::WideCStringEquals(const wchar_t* lhs, const wchar_t* rhs) { + if (lhs == nullptr) return rhs == nullptr; + + if (rhs == nullptr) return false; + + return wcscmp(lhs, rhs) == 0; +} + +// Helper function for *_STREQ on wide strings. +AssertionResult CmpHelperSTREQ(const char* lhs_expression, + const char* rhs_expression, const wchar_t* lhs, + const wchar_t* rhs) { + if (String::WideCStringEquals(lhs, rhs)) { + return AssertionSuccess(); + } + + return EqFailure(lhs_expression, rhs_expression, PrintToString(lhs), + PrintToString(rhs), false); +} + +// Helper function for *_STRNE on wide strings. +AssertionResult CmpHelperSTRNE(const char* s1_expression, + const char* s2_expression, const wchar_t* s1, + const wchar_t* s2) { + if (!String::WideCStringEquals(s1, s2)) { + return AssertionSuccess(); + } + + return AssertionFailure() + << "Expected: (" << s1_expression << ") != (" << s2_expression + << "), actual: " << PrintToString(s1) << " vs " << PrintToString(s2); +} + +// Compares two C strings, ignoring case. Returns true if and only if they have +// the same content. +// +// Unlike strcasecmp(), this function can handle NULL argument(s). A +// NULL C string is considered different to any non-NULL C string, +// including the empty string. +bool String::CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) { + if (lhs == nullptr) return rhs == nullptr; + if (rhs == nullptr) return false; + return posix::StrCaseCmp(lhs, rhs) == 0; +} + +// Compares two wide C strings, ignoring case. Returns true if and only if they +// have the same content. +// +// Unlike wcscasecmp(), this function can handle NULL argument(s). +// A NULL C string is considered different to any non-NULL wide C string, +// including the empty string. +// NB: The implementations on different platforms slightly differ. +// On windows, this method uses _wcsicmp which compares according to LC_CTYPE +// environment variable. On GNU platform this method uses wcscasecmp +// which compares according to LC_CTYPE category of the current locale. +// On MacOS X, it uses towlower, which also uses LC_CTYPE category of the +// current locale. +bool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs, + const wchar_t* rhs) { + if (lhs == nullptr) return rhs == nullptr; + + if (rhs == nullptr) return false; + +#if GTEST_OS_WINDOWS + return _wcsicmp(lhs, rhs) == 0; +#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID + return wcscasecmp(lhs, rhs) == 0; +#else + // Android, Mac OS X and Cygwin don't define wcscasecmp. + // Other unknown OSes may not define it either. + wint_t left, right; + do { + left = towlower(static_cast(*lhs++)); + right = towlower(static_cast(*rhs++)); + } while (left && left == right); + return left == right; +#endif // OS selector +} + +// Returns true if and only if str ends with the given suffix, ignoring case. +// Any string is considered to end with an empty suffix. +bool String::EndsWithCaseInsensitive(const std::string& str, + const std::string& suffix) { + const size_t str_len = str.length(); + const size_t suffix_len = suffix.length(); + return (str_len >= suffix_len) && + CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len, + suffix.c_str()); +} + +// Formats an int value as "%02d". +std::string String::FormatIntWidth2(int value) { + return FormatIntWidthN(value, 2); +} + +// Formats an int value to given width with leading zeros. +std::string String::FormatIntWidthN(int value, int width) { + std::stringstream ss; + ss << std::setfill('0') << std::setw(width) << value; + return ss.str(); +} + +// Formats an int value as "%X". +std::string String::FormatHexUInt32(uint32_t value) { + std::stringstream ss; + ss << std::hex << std::uppercase << value; + return ss.str(); +} + +// Formats an int value as "%X". +std::string String::FormatHexInt(int value) { + return FormatHexUInt32(static_cast(value)); +} + +// Formats a byte as "%02X". +std::string String::FormatByte(unsigned char value) { + std::stringstream ss; + ss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase + << static_cast(value); + return ss.str(); +} + +// Converts the buffer in a stringstream to an std::string, converting NUL +// bytes to "\\0" along the way. +std::string StringStreamToString(::std::stringstream* ss) { + const ::std::string& str = ss->str(); + const char* const start = str.c_str(); + const char* const end = start + str.length(); + + std::string result; + result.reserve(static_cast(2 * (end - start))); + for (const char* ch = start; ch != end; ++ch) { + if (*ch == '\0') { + result += "\\0"; // Replaces NUL with "\\0"; + } else { + result += *ch; + } + } + + return result; +} + +// Appends the user-supplied message to the Google-Test-generated message. +std::string AppendUserMessage(const std::string& gtest_msg, + const Message& user_msg) { + // Appends the user message if it's non-empty. + const std::string user_msg_string = user_msg.GetString(); + if (user_msg_string.empty()) { + return gtest_msg; + } + if (gtest_msg.empty()) { + return user_msg_string; + } + return gtest_msg + "\n" + user_msg_string; +} + +} // namespace internal + +// class TestResult + +// Creates an empty TestResult. +TestResult::TestResult() + : death_test_count_(0), start_timestamp_(0), elapsed_time_(0) {} + +// D'tor. +TestResult::~TestResult() {} + +// Returns the i-th test part result among all the results. i can +// range from 0 to total_part_count() - 1. If i is not in that range, +// aborts the program. +const TestPartResult& TestResult::GetTestPartResult(int i) const { + if (i < 0 || i >= total_part_count()) internal::posix::Abort(); + return test_part_results_.at(static_cast(i)); +} + +// Returns the i-th test property. i can range from 0 to +// test_property_count() - 1. If i is not in that range, aborts the +// program. +const TestProperty& TestResult::GetTestProperty(int i) const { + if (i < 0 || i >= test_property_count()) internal::posix::Abort(); + return test_properties_.at(static_cast(i)); +} + +// Clears the test part results. +void TestResult::ClearTestPartResults() { test_part_results_.clear(); } + +// Adds a test part result to the list. +void TestResult::AddTestPartResult(const TestPartResult& test_part_result) { + test_part_results_.push_back(test_part_result); +} + +// Adds a test property to the list. If a property with the same key as the +// supplied property is already represented, the value of this test_property +// replaces the old value for that key. +void TestResult::RecordProperty(const std::string& xml_element, + const TestProperty& test_property) { + if (!ValidateTestProperty(xml_element, test_property)) { + return; + } + internal::MutexLock lock(&test_properties_mutex_); + const std::vector::iterator property_with_matching_key = + std::find_if(test_properties_.begin(), test_properties_.end(), + internal::TestPropertyKeyIs(test_property.key())); + if (property_with_matching_key == test_properties_.end()) { + test_properties_.push_back(test_property); + return; + } + property_with_matching_key->SetValue(test_property.value()); +} + +// The list of reserved attributes used in the element of XML +// output. +static const char* const kReservedTestSuitesAttributes[] = { + "disabled", "errors", "failures", "name", + "random_seed", "tests", "time", "timestamp"}; + +// The list of reserved attributes used in the element of XML +// output. +static const char* const kReservedTestSuiteAttributes[] = { + "disabled", "errors", "failures", "name", + "tests", "time", "timestamp", "skipped"}; + +// The list of reserved attributes used in the element of XML output. +static const char* const kReservedTestCaseAttributes[] = { + "classname", "name", "status", "time", + "type_param", "value_param", "file", "line"}; + +// Use a slightly different set for allowed output to ensure existing tests can +// still RecordProperty("result") or "RecordProperty(timestamp") +static const char* const kReservedOutputTestCaseAttributes[] = { + "classname", "name", "status", "time", "type_param", + "value_param", "file", "line", "result", "timestamp"}; + +template +std::vector ArrayAsVector(const char* const (&array)[kSize]) { + return std::vector(array, array + kSize); +} + +static std::vector GetReservedAttributesForElement( + const std::string& xml_element) { + if (xml_element == "testsuites") { + return ArrayAsVector(kReservedTestSuitesAttributes); + } else if (xml_element == "testsuite") { + return ArrayAsVector(kReservedTestSuiteAttributes); + } else if (xml_element == "testcase") { + return ArrayAsVector(kReservedTestCaseAttributes); + } else { + GTEST_CHECK_(false) << "Unrecognized xml_element provided: " << xml_element; + } + // This code is unreachable but some compilers may not realizes that. + return std::vector(); +} + +// TODO(jdesprez): Merge the two getReserved attributes once skip is improved +static std::vector GetReservedOutputAttributesForElement( + const std::string& xml_element) { + if (xml_element == "testsuites") { + return ArrayAsVector(kReservedTestSuitesAttributes); + } else if (xml_element == "testsuite") { + return ArrayAsVector(kReservedTestSuiteAttributes); + } else if (xml_element == "testcase") { + return ArrayAsVector(kReservedOutputTestCaseAttributes); + } else { + GTEST_CHECK_(false) << "Unrecognized xml_element provided: " << xml_element; + } + // This code is unreachable but some compilers may not realizes that. + return std::vector(); +} + +static std::string FormatWordList(const std::vector& words) { + Message word_list; + for (size_t i = 0; i < words.size(); ++i) { + if (i > 0 && words.size() > 2) { + word_list << ", "; + } + if (i == words.size() - 1) { + word_list << "and "; + } + word_list << "'" << words[i] << "'"; + } + return word_list.GetString(); +} + +static bool ValidateTestPropertyName( + const std::string& property_name, + const std::vector& reserved_names) { + if (std::find(reserved_names.begin(), reserved_names.end(), property_name) != + reserved_names.end()) { + ADD_FAILURE() << "Reserved key used in RecordProperty(): " << property_name + << " (" << FormatWordList(reserved_names) + << " are reserved by " << GTEST_NAME_ << ")"; + return false; + } + return true; +} + +// Adds a failure if the key is a reserved attribute of the element named +// xml_element. Returns true if the property is valid. +bool TestResult::ValidateTestProperty(const std::string& xml_element, + const TestProperty& test_property) { + return ValidateTestPropertyName(test_property.key(), + GetReservedAttributesForElement(xml_element)); +} + +// Clears the object. +void TestResult::Clear() { + test_part_results_.clear(); + test_properties_.clear(); + death_test_count_ = 0; + elapsed_time_ = 0; +} + +// Returns true off the test part was skipped. +static bool TestPartSkipped(const TestPartResult& result) { + return result.skipped(); +} + +// Returns true if and only if the test was skipped. +bool TestResult::Skipped() const { + return !Failed() && CountIf(test_part_results_, TestPartSkipped) > 0; +} + +// Returns true if and only if the test failed. +bool TestResult::Failed() const { + for (int i = 0; i < total_part_count(); ++i) { + if (GetTestPartResult(i).failed()) return true; + } + return false; +} + +// Returns true if and only if the test part fatally failed. +static bool TestPartFatallyFailed(const TestPartResult& result) { + return result.fatally_failed(); +} + +// Returns true if and only if the test fatally failed. +bool TestResult::HasFatalFailure() const { + return CountIf(test_part_results_, TestPartFatallyFailed) > 0; +} + +// Returns true if and only if the test part non-fatally failed. +static bool TestPartNonfatallyFailed(const TestPartResult& result) { + return result.nonfatally_failed(); +} + +// Returns true if and only if the test has a non-fatal failure. +bool TestResult::HasNonfatalFailure() const { + return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0; +} + +// Gets the number of all test parts. This is the sum of the number +// of successful test parts and the number of failed test parts. +int TestResult::total_part_count() const { + return static_cast(test_part_results_.size()); +} + +// Returns the number of the test properties. +int TestResult::test_property_count() const { + return static_cast(test_properties_.size()); +} + +// class Test + +// Creates a Test object. + +// The c'tor saves the states of all flags. +Test::Test() : gtest_flag_saver_(new GTEST_FLAG_SAVER_) {} + +// The d'tor restores the states of all flags. The actual work is +// done by the d'tor of the gtest_flag_saver_ field, and thus not +// visible here. +Test::~Test() {} + +// Sets up the test fixture. +// +// A sub-class may override this. +void Test::SetUp() {} + +// Tears down the test fixture. +// +// A sub-class may override this. +void Test::TearDown() {} + +// Allows user supplied key value pairs to be recorded for later output. +void Test::RecordProperty(const std::string& key, const std::string& value) { + UnitTest::GetInstance()->RecordProperty(key, value); +} + +// Allows user supplied key value pairs to be recorded for later output. +void Test::RecordProperty(const std::string& key, int value) { + Message value_message; + value_message << value; + RecordProperty(key, value_message.GetString().c_str()); +} + +namespace internal { + +void ReportFailureInUnknownLocation(TestPartResult::Type result_type, + const std::string& message) { + // This function is a friend of UnitTest and as such has access to + // AddTestPartResult. + UnitTest::GetInstance()->AddTestPartResult( + result_type, + nullptr, // No info about the source file where the exception occurred. + -1, // We have no info on which line caused the exception. + message, + ""); // No stack trace, either. +} + +} // namespace internal + +// Google Test requires all tests in the same test suite to use the same test +// fixture class. This function checks if the current test has the +// same fixture class as the first test in the current test suite. If +// yes, it returns true; otherwise it generates a Google Test failure and +// returns false. +bool Test::HasSameFixtureClass() { + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + const TestSuite* const test_suite = impl->current_test_suite(); + + // Info about the first test in the current test suite. + const TestInfo* const first_test_info = test_suite->test_info_list()[0]; + const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_; + const char* const first_test_name = first_test_info->name(); + + // Info about the current test. + const TestInfo* const this_test_info = impl->current_test_info(); + const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_; + const char* const this_test_name = this_test_info->name(); + + if (this_fixture_id != first_fixture_id) { + // Is the first test defined using TEST? + const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId(); + // Is this test defined using TEST? + const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId(); + + if (first_is_TEST || this_is_TEST) { + // Both TEST and TEST_F appear in same test suite, which is incorrect. + // Tell the user how to fix this. + + // Gets the name of the TEST and the name of the TEST_F. Note + // that first_is_TEST and this_is_TEST cannot both be true, as + // the fixture IDs are different for the two tests. + const char* const TEST_name = + first_is_TEST ? first_test_name : this_test_name; + const char* const TEST_F_name = + first_is_TEST ? this_test_name : first_test_name; + + ADD_FAILURE() + << "All tests in the same test suite must use the same test fixture\n" + << "class, so mixing TEST_F and TEST in the same test suite is\n" + << "illegal. In test suite " << this_test_info->test_suite_name() + << ",\n" + << "test " << TEST_F_name << " is defined using TEST_F but\n" + << "test " << TEST_name << " is defined using TEST. You probably\n" + << "want to change the TEST to TEST_F or move it to another test\n" + << "case."; + } else { + // Two fixture classes with the same name appear in two different + // namespaces, which is not allowed. Tell the user how to fix this. + ADD_FAILURE() + << "All tests in the same test suite must use the same test fixture\n" + << "class. However, in test suite " + << this_test_info->test_suite_name() << ",\n" + << "you defined test " << first_test_name << " and test " + << this_test_name << "\n" + << "using two different test fixture classes. This can happen if\n" + << "the two classes are from different namespaces or translation\n" + << "units and have the same name. You should probably rename one\n" + << "of the classes to put the tests into different test suites."; + } + return false; + } + + return true; +} + +#if GTEST_HAS_SEH + +// Adds an "exception thrown" fatal failure to the current test. This +// function returns its result via an output parameter pointer because VC++ +// prohibits creation of objects with destructors on stack in functions +// using __try (see error C2712). +static std::string* FormatSehExceptionMessage(DWORD exception_code, + const char* location) { + Message message; + message << "SEH exception with code 0x" << std::setbase(16) << exception_code + << std::setbase(10) << " thrown in " << location << "."; + + return new std::string(message.GetString()); +} + +#endif // GTEST_HAS_SEH + +namespace internal { + +#if GTEST_HAS_EXCEPTIONS + +// Adds an "exception thrown" fatal failure to the current test. +static std::string FormatCxxExceptionMessage(const char* description, + const char* location) { + Message message; + if (description != nullptr) { + message << "C++ exception with description \"" << description << "\""; + } else { + message << "Unknown C++ exception"; + } + message << " thrown in " << location << "."; + + return message.GetString(); +} + +static std::string PrintTestPartResultToString( + const TestPartResult& test_part_result); + +GoogleTestFailureException::GoogleTestFailureException( + const TestPartResult& failure) + : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {} + +#endif // GTEST_HAS_EXCEPTIONS + +// We put these helper functions in the internal namespace as IBM's xlC +// compiler rejects the code if they were declared static. + +// Runs the given method and handles SEH exceptions it throws, when +// SEH is supported; returns the 0-value for type Result in case of an +// SEH exception. (Microsoft compilers cannot handle SEH and C++ +// exceptions in the same function. Therefore, we provide a separate +// wrapper function for handling SEH exceptions.) +template +Result HandleSehExceptionsInMethodIfSupported(T* object, Result (T::*method)(), + const char* location) { +#if GTEST_HAS_SEH + __try { + return (object->*method)(); + } __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT + GetExceptionCode())) { + // We create the exception message on the heap because VC++ prohibits + // creation of objects with destructors on stack in functions using __try + // (see error C2712). + std::string* exception_message = + FormatSehExceptionMessage(GetExceptionCode(), location); + internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure, + *exception_message); + delete exception_message; + return static_cast(0); + } +#else + (void)location; + return (object->*method)(); +#endif // GTEST_HAS_SEH +} + +// Runs the given method and catches and reports C++ and/or SEH-style +// exceptions, if they are supported; returns the 0-value for type +// Result in case of an SEH exception. +template +Result HandleExceptionsInMethodIfSupported(T* object, Result (T::*method)(), + const char* location) { + // NOTE: The user code can affect the way in which Google Test handles + // exceptions by setting GTEST_FLAG(catch_exceptions), but only before + // RUN_ALL_TESTS() starts. It is technically possible to check the flag + // after the exception is caught and either report or re-throw the + // exception based on the flag's value: + // + // try { + // // Perform the test method. + // } catch (...) { + // if (GTEST_FLAG_GET(catch_exceptions)) + // // Report the exception as failure. + // else + // throw; // Re-throws the original exception. + // } + // + // However, the purpose of this flag is to allow the program to drop into + // the debugger when the exception is thrown. On most platforms, once the + // control enters the catch block, the exception origin information is + // lost and the debugger will stop the program at the point of the + // re-throw in this function -- instead of at the point of the original + // throw statement in the code under test. For this reason, we perform + // the check early, sacrificing the ability to affect Google Test's + // exception handling in the method where the exception is thrown. + if (internal::GetUnitTestImpl()->catch_exceptions()) { +#if GTEST_HAS_EXCEPTIONS + try { + return HandleSehExceptionsInMethodIfSupported(object, method, location); + } catch (const AssertionException&) { // NOLINT + // This failure was reported already. + } catch (const internal::GoogleTestFailureException&) { // NOLINT + // This exception type can only be thrown by a failed Google + // Test assertion with the intention of letting another testing + // framework catch it. Therefore we just re-throw it. + throw; + } catch (const std::exception& e) { // NOLINT + internal::ReportFailureInUnknownLocation( + TestPartResult::kFatalFailure, + FormatCxxExceptionMessage(e.what(), location)); + } catch (...) { // NOLINT + internal::ReportFailureInUnknownLocation( + TestPartResult::kFatalFailure, + FormatCxxExceptionMessage(nullptr, location)); + } + return static_cast(0); +#else + return HandleSehExceptionsInMethodIfSupported(object, method, location); +#endif // GTEST_HAS_EXCEPTIONS + } else { + return (object->*method)(); + } +} + +} // namespace internal + +// Runs the test and updates the test result. +void Test::Run() { + if (!HasSameFixtureClass()) return; + + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + impl->os_stack_trace_getter()->UponLeavingGTest(); + internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()"); + // We will run the test only if SetUp() was successful and didn't call + // GTEST_SKIP(). + if (!HasFatalFailure() && !IsSkipped()) { + impl->os_stack_trace_getter()->UponLeavingGTest(); + internal::HandleExceptionsInMethodIfSupported(this, &Test::TestBody, + "the test body"); + } + + // However, we want to clean up as much as possible. Hence we will + // always call TearDown(), even if SetUp() or the test body has + // failed. + impl->os_stack_trace_getter()->UponLeavingGTest(); + internal::HandleExceptionsInMethodIfSupported(this, &Test::TearDown, + "TearDown()"); +} + +// Returns true if and only if the current test has a fatal failure. +bool Test::HasFatalFailure() { + return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure(); +} + +// Returns true if and only if the current test has a non-fatal failure. +bool Test::HasNonfatalFailure() { + return internal::GetUnitTestImpl() + ->current_test_result() + ->HasNonfatalFailure(); +} + +// Returns true if and only if the current test was skipped. +bool Test::IsSkipped() { + return internal::GetUnitTestImpl()->current_test_result()->Skipped(); +} + +// class TestInfo + +// Constructs a TestInfo object. It assumes ownership of the test factory +// object. +TestInfo::TestInfo(const std::string& a_test_suite_name, + const std::string& a_name, const char* a_type_param, + const char* a_value_param, + internal::CodeLocation a_code_location, + internal::TypeId fixture_class_id, + internal::TestFactoryBase* factory) + : test_suite_name_(a_test_suite_name), + name_(a_name), + type_param_(a_type_param ? new std::string(a_type_param) : nullptr), + value_param_(a_value_param ? new std::string(a_value_param) : nullptr), + location_(a_code_location), + fixture_class_id_(fixture_class_id), + should_run_(false), + is_disabled_(false), + matches_filter_(false), + is_in_another_shard_(false), + factory_(factory), + result_() {} + +// Destructs a TestInfo object. +TestInfo::~TestInfo() { delete factory_; } + +namespace internal { + +// Creates a new TestInfo object and registers it with Google Test; +// returns the created object. +// +// Arguments: +// +// test_suite_name: name of the test suite +// name: name of the test +// type_param: the name of the test's type parameter, or NULL if +// this is not a typed or a type-parameterized test. +// value_param: text representation of the test's value parameter, +// or NULL if this is not a value-parameterized test. +// code_location: code location where the test is defined +// fixture_class_id: ID of the test fixture class +// set_up_tc: pointer to the function that sets up the test suite +// tear_down_tc: pointer to the function that tears down the test suite +// factory: pointer to the factory that creates a test object. +// The newly created TestInfo instance will assume +// ownership of the factory object. +TestInfo* MakeAndRegisterTestInfo( + const char* test_suite_name, const char* name, const char* type_param, + const char* value_param, CodeLocation code_location, + TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, + TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory) { + TestInfo* const test_info = + new TestInfo(test_suite_name, name, type_param, value_param, + code_location, fixture_class_id, factory); + GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info); + return test_info; +} + +void ReportInvalidTestSuiteType(const char* test_suite_name, + CodeLocation code_location) { + Message errors; + errors + << "Attempted redefinition of test suite " << test_suite_name << ".\n" + << "All tests in the same test suite must use the same test fixture\n" + << "class. However, in test suite " << test_suite_name << ", you tried\n" + << "to define a test using a fixture class different from the one\n" + << "used earlier. This can happen if the two fixture classes are\n" + << "from different namespaces and have the same name. You should\n" + << "probably rename one of the classes to put the tests into different\n" + << "test suites."; + + GTEST_LOG_(ERROR) << FormatFileLocation(code_location.file.c_str(), + code_location.line) + << " " << errors.GetString(); +} +} // namespace internal + +namespace { + +// A predicate that checks the test name of a TestInfo against a known +// value. +// +// This is used for implementation of the TestSuite class only. We put +// it in the anonymous namespace to prevent polluting the outer +// namespace. +// +// TestNameIs is copyable. +class TestNameIs { + public: + // Constructor. + // + // TestNameIs has NO default constructor. + explicit TestNameIs(const char* name) : name_(name) {} + + // Returns true if and only if the test name of test_info matches name_. + bool operator()(const TestInfo* test_info) const { + return test_info && test_info->name() == name_; + } + + private: + std::string name_; +}; + +} // namespace + +namespace internal { + +// This method expands all parameterized tests registered with macros TEST_P +// and INSTANTIATE_TEST_SUITE_P into regular tests and registers those. +// This will be done just once during the program runtime. +void UnitTestImpl::RegisterParameterizedTests() { + if (!parameterized_tests_registered_) { + parameterized_test_registry_.RegisterTests(); + type_parameterized_test_registry_.CheckForInstantiations(); + parameterized_tests_registered_ = true; + } +} + +} // namespace internal + +// Creates the test object, runs it, records its result, and then +// deletes it. +void TestInfo::Run() { + TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); + if (!should_run_) { + if (is_disabled_ && matches_filter_) repeater->OnTestDisabled(*this); + return; + } + + // Tells UnitTest where to store test result. + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + impl->set_current_test_info(this); + + // Notifies the unit test event listeners that a test is about to start. + repeater->OnTestStart(*this); + result_.set_start_timestamp(internal::GetTimeInMillis()); + internal::Timer timer; + impl->os_stack_trace_getter()->UponLeavingGTest(); + + // Creates the test object. + Test* const test = internal::HandleExceptionsInMethodIfSupported( + factory_, &internal::TestFactoryBase::CreateTest, + "the test fixture's constructor"); + + // Runs the test if the constructor didn't generate a fatal failure or invoke + // GTEST_SKIP(). + // Note that the object will not be null + if (!Test::HasFatalFailure() && !Test::IsSkipped()) { + // This doesn't throw as all user code that can throw are wrapped into + // exception handling code. + test->Run(); + } + + if (test != nullptr) { + // Deletes the test object. + impl->os_stack_trace_getter()->UponLeavingGTest(); + internal::HandleExceptionsInMethodIfSupported( + test, &Test::DeleteSelf_, "the test fixture's destructor"); + } + + result_.set_elapsed_time(timer.Elapsed()); + + // Notifies the unit test event listener that a test has just finished. + repeater->OnTestEnd(*this); + + // Tells UnitTest to stop associating assertion results to this + // test. + impl->set_current_test_info(nullptr); +} + +// Skip and records a skipped test result for this object. +void TestInfo::Skip() { + if (!should_run_) return; + + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + impl->set_current_test_info(this); + + TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); + + // Notifies the unit test event listeners that a test is about to start. + repeater->OnTestStart(*this); + + const TestPartResult test_part_result = + TestPartResult(TestPartResult::kSkip, this->file(), this->line(), ""); + impl->GetTestPartResultReporterForCurrentThread()->ReportTestPartResult( + test_part_result); + + // Notifies the unit test event listener that a test has just finished. + repeater->OnTestEnd(*this); + impl->set_current_test_info(nullptr); +} + +// class TestSuite + +// Gets the number of successful tests in this test suite. +int TestSuite::successful_test_count() const { + return CountIf(test_info_list_, TestPassed); +} + +// Gets the number of successful tests in this test suite. +int TestSuite::skipped_test_count() const { + return CountIf(test_info_list_, TestSkipped); +} + +// Gets the number of failed tests in this test suite. +int TestSuite::failed_test_count() const { + return CountIf(test_info_list_, TestFailed); +} + +// Gets the number of disabled tests that will be reported in the XML report. +int TestSuite::reportable_disabled_test_count() const { + return CountIf(test_info_list_, TestReportableDisabled); +} + +// Gets the number of disabled tests in this test suite. +int TestSuite::disabled_test_count() const { + return CountIf(test_info_list_, TestDisabled); +} + +// Gets the number of tests to be printed in the XML report. +int TestSuite::reportable_test_count() const { + return CountIf(test_info_list_, TestReportable); +} + +// Get the number of tests in this test suite that should run. +int TestSuite::test_to_run_count() const { + return CountIf(test_info_list_, ShouldRunTest); +} + +// Gets the number of all tests. +int TestSuite::total_test_count() const { + return static_cast(test_info_list_.size()); +} + +// Creates a TestSuite with the given name. +// +// Arguments: +// +// a_name: name of the test suite +// a_type_param: the name of the test suite's type parameter, or NULL if +// this is not a typed or a type-parameterized test suite. +// set_up_tc: pointer to the function that sets up the test suite +// tear_down_tc: pointer to the function that tears down the test suite +TestSuite::TestSuite(const char* a_name, const char* a_type_param, + internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc) + : name_(a_name), + type_param_(a_type_param ? new std::string(a_type_param) : nullptr), + set_up_tc_(set_up_tc), + tear_down_tc_(tear_down_tc), + should_run_(false), + start_timestamp_(0), + elapsed_time_(0) {} + +// Destructor of TestSuite. +TestSuite::~TestSuite() { + // Deletes every Test in the collection. + ForEach(test_info_list_, internal::Delete); +} + +// Returns the i-th test among all the tests. i can range from 0 to +// total_test_count() - 1. If i is not in that range, returns NULL. +const TestInfo* TestSuite::GetTestInfo(int i) const { + const int index = GetElementOr(test_indices_, i, -1); + return index < 0 ? nullptr : test_info_list_[static_cast(index)]; +} + +// Returns the i-th test among all the tests. i can range from 0 to +// total_test_count() - 1. If i is not in that range, returns NULL. +TestInfo* TestSuite::GetMutableTestInfo(int i) { + const int index = GetElementOr(test_indices_, i, -1); + return index < 0 ? nullptr : test_info_list_[static_cast(index)]; +} + +// Adds a test to this test suite. Will delete the test upon +// destruction of the TestSuite object. +void TestSuite::AddTestInfo(TestInfo* test_info) { + test_info_list_.push_back(test_info); + test_indices_.push_back(static_cast(test_indices_.size())); +} + +// Runs every test in this TestSuite. +void TestSuite::Run() { + if (!should_run_) return; + + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + impl->set_current_test_suite(this); + + TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); + + // Call both legacy and the new API + repeater->OnTestSuiteStart(*this); +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + repeater->OnTestCaseStart(*this); +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + impl->os_stack_trace_getter()->UponLeavingGTest(); + internal::HandleExceptionsInMethodIfSupported( + this, &TestSuite::RunSetUpTestSuite, "SetUpTestSuite()"); + + const bool skip_all = ad_hoc_test_result().Failed(); + + start_timestamp_ = internal::GetTimeInMillis(); + internal::Timer timer; + for (int i = 0; i < total_test_count(); i++) { + if (skip_all) { + GetMutableTestInfo(i)->Skip(); + } else { + GetMutableTestInfo(i)->Run(); + } + if (GTEST_FLAG_GET(fail_fast) && + GetMutableTestInfo(i)->result()->Failed()) { + for (int j = i + 1; j < total_test_count(); j++) { + GetMutableTestInfo(j)->Skip(); + } + break; + } + } + elapsed_time_ = timer.Elapsed(); + + impl->os_stack_trace_getter()->UponLeavingGTest(); + internal::HandleExceptionsInMethodIfSupported( + this, &TestSuite::RunTearDownTestSuite, "TearDownTestSuite()"); + + // Call both legacy and the new API + repeater->OnTestSuiteEnd(*this); +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + repeater->OnTestCaseEnd(*this); +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + impl->set_current_test_suite(nullptr); +} + +// Skips all tests under this TestSuite. +void TestSuite::Skip() { + if (!should_run_) return; + + internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); + impl->set_current_test_suite(this); + + TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); + + // Call both legacy and the new API + repeater->OnTestSuiteStart(*this); +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + repeater->OnTestCaseStart(*this); +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + for (int i = 0; i < total_test_count(); i++) { + GetMutableTestInfo(i)->Skip(); + } + + // Call both legacy and the new API + repeater->OnTestSuiteEnd(*this); + // Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + repeater->OnTestCaseEnd(*this); +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + impl->set_current_test_suite(nullptr); +} + +// Clears the results of all tests in this test suite. +void TestSuite::ClearResult() { + ad_hoc_test_result_.Clear(); + ForEach(test_info_list_, TestInfo::ClearTestResult); +} + +// Shuffles the tests in this test suite. +void TestSuite::ShuffleTests(internal::Random* random) { + Shuffle(random, &test_indices_); +} + +// Restores the test order to before the first shuffle. +void TestSuite::UnshuffleTests() { + for (size_t i = 0; i < test_indices_.size(); i++) { + test_indices_[i] = static_cast(i); + } +} + +// Formats a countable noun. Depending on its quantity, either the +// singular form or the plural form is used. e.g. +// +// FormatCountableNoun(1, "formula", "formuli") returns "1 formula". +// FormatCountableNoun(5, "book", "books") returns "5 books". +static std::string FormatCountableNoun(int count, const char* singular_form, + const char* plural_form) { + return internal::StreamableToString(count) + " " + + (count == 1 ? singular_form : plural_form); +} + +// Formats the count of tests. +static std::string FormatTestCount(int test_count) { + return FormatCountableNoun(test_count, "test", "tests"); +} + +// Formats the count of test suites. +static std::string FormatTestSuiteCount(int test_suite_count) { + return FormatCountableNoun(test_suite_count, "test suite", "test suites"); +} + +// Converts a TestPartResult::Type enum to human-friendly string +// representation. Both kNonFatalFailure and kFatalFailure are translated +// to "Failure", as the user usually doesn't care about the difference +// between the two when viewing the test result. +static const char* TestPartResultTypeToString(TestPartResult::Type type) { + switch (type) { + case TestPartResult::kSkip: + return "Skipped\n"; + case TestPartResult::kSuccess: + return "Success"; + + case TestPartResult::kNonFatalFailure: + case TestPartResult::kFatalFailure: +#ifdef _MSC_VER + return "error: "; +#else + return "Failure\n"; +#endif + default: + return "Unknown result type"; + } +} + +namespace internal { +namespace { +enum class GTestColor { kDefault, kRed, kGreen, kYellow }; +} // namespace + +// Prints a TestPartResult to an std::string. +static std::string PrintTestPartResultToString( + const TestPartResult& test_part_result) { + return (Message() << internal::FormatFileLocation( + test_part_result.file_name(), + test_part_result.line_number()) + << " " + << TestPartResultTypeToString(test_part_result.type()) + << test_part_result.message()) + .GetString(); +} + +// Prints a TestPartResult. +static void PrintTestPartResult(const TestPartResult& test_part_result) { + const std::string& result = PrintTestPartResultToString(test_part_result); + printf("%s\n", result.c_str()); + fflush(stdout); + // If the test program runs in Visual Studio or a debugger, the + // following statements add the test part result message to the Output + // window such that the user can double-click on it to jump to the + // corresponding source code location; otherwise they do nothing. +#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE + // We don't call OutputDebugString*() on Windows Mobile, as printing + // to stdout is done by OutputDebugString() there already - we don't + // want the same message printed twice. + ::OutputDebugStringA(result.c_str()); + ::OutputDebugStringA("\n"); +#endif +} + +// class PrettyUnitTestResultPrinter +#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \ + !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW + +// Returns the character attribute for the given color. +static WORD GetColorAttribute(GTestColor color) { + switch (color) { + case GTestColor::kRed: + return FOREGROUND_RED; + case GTestColor::kGreen: + return FOREGROUND_GREEN; + case GTestColor::kYellow: + return FOREGROUND_RED | FOREGROUND_GREEN; + default: + return 0; + } +} + +static int GetBitOffset(WORD color_mask) { + if (color_mask == 0) return 0; + + int bitOffset = 0; + while ((color_mask & 1) == 0) { + color_mask >>= 1; + ++bitOffset; + } + return bitOffset; +} + +static WORD GetNewColor(GTestColor color, WORD old_color_attrs) { + // Let's reuse the BG + static const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN | + BACKGROUND_RED | BACKGROUND_INTENSITY; + static const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN | + FOREGROUND_RED | FOREGROUND_INTENSITY; + const WORD existing_bg = old_color_attrs & background_mask; + + WORD new_color = + GetColorAttribute(color) | existing_bg | FOREGROUND_INTENSITY; + static const int bg_bitOffset = GetBitOffset(background_mask); + static const int fg_bitOffset = GetBitOffset(foreground_mask); + + if (((new_color & background_mask) >> bg_bitOffset) == + ((new_color & foreground_mask) >> fg_bitOffset)) { + new_color ^= FOREGROUND_INTENSITY; // invert intensity + } + return new_color; +} + +#else + +// Returns the ANSI color code for the given color. GTestColor::kDefault is +// an invalid input. +static const char* GetAnsiColorCode(GTestColor color) { + switch (color) { + case GTestColor::kRed: + return "1"; + case GTestColor::kGreen: + return "2"; + case GTestColor::kYellow: + return "3"; + default: + return nullptr; + } +} + +#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE + +// Returns true if and only if Google Test should use colors in the output. +bool ShouldUseColor(bool stdout_is_tty) { + std::string c = GTEST_FLAG_GET(color); + const char* const gtest_color = c.c_str(); + + if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) { +#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW + // On Windows the TERM variable is usually not set, but the + // console there does support colors. + return stdout_is_tty; +#else + // On non-Windows platforms, we rely on the TERM variable. + const char* const term = posix::GetEnv("TERM"); + const bool term_supports_color = + String::CStringEquals(term, "xterm") || + String::CStringEquals(term, "xterm-color") || + String::CStringEquals(term, "xterm-256color") || + String::CStringEquals(term, "screen") || + String::CStringEquals(term, "screen-256color") || + String::CStringEquals(term, "tmux") || + String::CStringEquals(term, "tmux-256color") || + String::CStringEquals(term, "rxvt-unicode") || + String::CStringEquals(term, "rxvt-unicode-256color") || + String::CStringEquals(term, "linux") || + String::CStringEquals(term, "cygwin"); + return stdout_is_tty && term_supports_color; +#endif // GTEST_OS_WINDOWS + } + + return String::CaseInsensitiveCStringEquals(gtest_color, "yes") || + String::CaseInsensitiveCStringEquals(gtest_color, "true") || + String::CaseInsensitiveCStringEquals(gtest_color, "t") || + String::CStringEquals(gtest_color, "1"); + // We take "yes", "true", "t", and "1" as meaning "yes". If the + // value is neither one of these nor "auto", we treat it as "no" to + // be conservative. +} + +// Helpers for printing colored strings to stdout. Note that on Windows, we +// cannot simply emit special characters and have the terminal change colors. +// This routine must actually emit the characters rather than return a string +// that would be colored when printed, as can be done on Linux. + +GTEST_ATTRIBUTE_PRINTF_(2, 3) +static void ColoredPrintf(GTestColor color, const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + static const bool in_color_mode = + ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0); + const bool use_color = in_color_mode && (color != GTestColor::kDefault); + + if (!use_color) { + vprintf(fmt, args); + va_end(args); + return; + } + +#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && \ + !GTEST_OS_WINDOWS_RT && !GTEST_OS_WINDOWS_MINGW + const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); + + // Gets the current text color. + CONSOLE_SCREEN_BUFFER_INFO buffer_info; + GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); + const WORD old_color_attrs = buffer_info.wAttributes; + const WORD new_color = GetNewColor(color, old_color_attrs); + + // We need to flush the stream buffers into the console before each + // SetConsoleTextAttribute call lest it affect the text that is already + // printed but has not yet reached the console. + fflush(stdout); + SetConsoleTextAttribute(stdout_handle, new_color); + + vprintf(fmt, args); + + fflush(stdout); + // Restores the text color. + SetConsoleTextAttribute(stdout_handle, old_color_attrs); +#else + printf("\033[0;3%sm", GetAnsiColorCode(color)); + vprintf(fmt, args); + printf("\033[m"); // Resets the terminal to default. +#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE + va_end(args); +} + +// Text printed in Google Test's text output and --gtest_list_tests +// output to label the type parameter and value parameter for a test. +static const char kTypeParamLabel[] = "TypeParam"; +static const char kValueParamLabel[] = "GetParam()"; + +static void PrintFullTestCommentIfPresent(const TestInfo& test_info) { + const char* const type_param = test_info.type_param(); + const char* const value_param = test_info.value_param(); + + if (type_param != nullptr || value_param != nullptr) { + printf(", where "); + if (type_param != nullptr) { + printf("%s = %s", kTypeParamLabel, type_param); + if (value_param != nullptr) printf(" and "); + } + if (value_param != nullptr) { + printf("%s = %s", kValueParamLabel, value_param); + } + } +} + +// This class implements the TestEventListener interface. +// +// Class PrettyUnitTestResultPrinter is copyable. +class PrettyUnitTestResultPrinter : public TestEventListener { + public: + PrettyUnitTestResultPrinter() {} + static void PrintTestName(const char* test_suite, const char* test) { + printf("%s.%s", test_suite, test); + } + + // The following methods override what's in the TestEventListener class. + void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} + void OnTestIterationStart(const UnitTest& unit_test, int iteration) override; + void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override; + void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseStart(const TestCase& test_case) override; +#else + void OnTestSuiteStart(const TestSuite& test_suite) override; +#endif // OnTestCaseStart + + void OnTestStart(const TestInfo& test_info) override; + void OnTestDisabled(const TestInfo& test_info) override; + + void OnTestPartResult(const TestPartResult& result) override; + void OnTestEnd(const TestInfo& test_info) override; +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseEnd(const TestCase& test_case) override; +#else + void OnTestSuiteEnd(const TestSuite& test_suite) override; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override; + void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} + void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; + void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} + + private: + static void PrintFailedTests(const UnitTest& unit_test); + static void PrintFailedTestSuites(const UnitTest& unit_test); + static void PrintSkippedTests(const UnitTest& unit_test); +}; + +// Fired before each iteration of tests starts. +void PrettyUnitTestResultPrinter::OnTestIterationStart( + const UnitTest& unit_test, int iteration) { + if (GTEST_FLAG_GET(repeat) != 1) + printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1); + + std::string f = GTEST_FLAG_GET(filter); + const char* const filter = f.c_str(); + + // Prints the filter if it's not *. This reminds the user that some + // tests may be skipped. + if (!String::CStringEquals(filter, kUniversalFilter)) { + ColoredPrintf(GTestColor::kYellow, "Note: %s filter = %s\n", GTEST_NAME_, + filter); + } + + if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) { + const int32_t shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); + ColoredPrintf(GTestColor::kYellow, "Note: This is test shard %d of %s.\n", + static_cast(shard_index) + 1, + internal::posix::GetEnv(kTestTotalShards)); + } + + if (GTEST_FLAG_GET(shuffle)) { + ColoredPrintf(GTestColor::kYellow, + "Note: Randomizing tests' orders with a seed of %d .\n", + unit_test.random_seed()); + } + + ColoredPrintf(GTestColor::kGreen, "[==========] "); + printf("Running %s from %s.\n", + FormatTestCount(unit_test.test_to_run_count()).c_str(), + FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); + fflush(stdout); +} + +void PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart( + const UnitTest& /*unit_test*/) { + ColoredPrintf(GTestColor::kGreen, "[----------] "); + printf("Global test environment set-up.\n"); + fflush(stdout); +} + +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +void PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) { + const std::string counts = + FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); + ColoredPrintf(GTestColor::kGreen, "[----------] "); + printf("%s from %s", counts.c_str(), test_case.name()); + if (test_case.type_param() == nullptr) { + printf("\n"); + } else { + printf(", where %s = %s\n", kTypeParamLabel, test_case.type_param()); + } + fflush(stdout); +} +#else +void PrettyUnitTestResultPrinter::OnTestSuiteStart( + const TestSuite& test_suite) { + const std::string counts = + FormatCountableNoun(test_suite.test_to_run_count(), "test", "tests"); + ColoredPrintf(GTestColor::kGreen, "[----------] "); + printf("%s from %s", counts.c_str(), test_suite.name()); + if (test_suite.type_param() == nullptr) { + printf("\n"); + } else { + printf(", where %s = %s\n", kTypeParamLabel, test_suite.type_param()); + } + fflush(stdout); +} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { + ColoredPrintf(GTestColor::kGreen, "[ RUN ] "); + PrintTestName(test_info.test_suite_name(), test_info.name()); + printf("\n"); + fflush(stdout); +} + +void PrettyUnitTestResultPrinter::OnTestDisabled(const TestInfo& test_info) { + ColoredPrintf(GTestColor::kYellow, "[ DISABLED ] "); + PrintTestName(test_info.test_suite_name(), test_info.name()); + printf("\n"); + fflush(stdout); +} + +// Called after an assertion failure. +void PrettyUnitTestResultPrinter::OnTestPartResult( + const TestPartResult& result) { + switch (result.type()) { + // If the test part succeeded, we don't need to do anything. + case TestPartResult::kSuccess: + return; + default: + // Print failure message from the assertion + // (e.g. expected this and got that). + PrintTestPartResult(result); + fflush(stdout); + } +} + +void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { + if (test_info.result()->Passed()) { + ColoredPrintf(GTestColor::kGreen, "[ OK ] "); + } else if (test_info.result()->Skipped()) { + ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); + } else { + ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); + } + PrintTestName(test_info.test_suite_name(), test_info.name()); + if (test_info.result()->Failed()) PrintFullTestCommentIfPresent(test_info); + + if (GTEST_FLAG_GET(print_time)) { + printf(" (%s ms)\n", + internal::StreamableToString(test_info.result()->elapsed_time()) + .c_str()); + } else { + printf("\n"); + } + fflush(stdout); +} + +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +void PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) { + if (!GTEST_FLAG_GET(print_time)) return; + + const std::string counts = + FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); + ColoredPrintf(GTestColor::kGreen, "[----------] "); + printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_case.name(), + internal::StreamableToString(test_case.elapsed_time()).c_str()); + fflush(stdout); +} +#else +void PrettyUnitTestResultPrinter::OnTestSuiteEnd(const TestSuite& test_suite) { + if (!GTEST_FLAG_GET(print_time)) return; + + const std::string counts = + FormatCountableNoun(test_suite.test_to_run_count(), "test", "tests"); + ColoredPrintf(GTestColor::kGreen, "[----------] "); + printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_suite.name(), + internal::StreamableToString(test_suite.elapsed_time()).c_str()); + fflush(stdout); +} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +void PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart( + const UnitTest& /*unit_test*/) { + ColoredPrintf(GTestColor::kGreen, "[----------] "); + printf("Global test environment tear-down\n"); + fflush(stdout); +} + +// Internal helper for printing the list of failed tests. +void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) { + const int failed_test_count = unit_test.failed_test_count(); + ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); + printf("%s, listed below:\n", FormatTestCount(failed_test_count).c_str()); + + for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { + const TestSuite& test_suite = *unit_test.GetTestSuite(i); + if (!test_suite.should_run() || (test_suite.failed_test_count() == 0)) { + continue; + } + for (int j = 0; j < test_suite.total_test_count(); ++j) { + const TestInfo& test_info = *test_suite.GetTestInfo(j); + if (!test_info.should_run() || !test_info.result()->Failed()) { + continue; + } + ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); + printf("%s.%s", test_suite.name(), test_info.name()); + PrintFullTestCommentIfPresent(test_info); + printf("\n"); + } + } + printf("\n%2d FAILED %s\n", failed_test_count, + failed_test_count == 1 ? "TEST" : "TESTS"); +} + +// Internal helper for printing the list of test suite failures not covered by +// PrintFailedTests. +void PrettyUnitTestResultPrinter::PrintFailedTestSuites( + const UnitTest& unit_test) { + int suite_failure_count = 0; + for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { + const TestSuite& test_suite = *unit_test.GetTestSuite(i); + if (!test_suite.should_run()) { + continue; + } + if (test_suite.ad_hoc_test_result().Failed()) { + ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); + printf("%s: SetUpTestSuite or TearDownTestSuite\n", test_suite.name()); + ++suite_failure_count; + } + } + if (suite_failure_count > 0) { + printf("\n%2d FAILED TEST %s\n", suite_failure_count, + suite_failure_count == 1 ? "SUITE" : "SUITES"); + } +} + +// Internal helper for printing the list of skipped tests. +void PrettyUnitTestResultPrinter::PrintSkippedTests(const UnitTest& unit_test) { + const int skipped_test_count = unit_test.skipped_test_count(); + if (skipped_test_count == 0) { + return; + } + + for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { + const TestSuite& test_suite = *unit_test.GetTestSuite(i); + if (!test_suite.should_run() || (test_suite.skipped_test_count() == 0)) { + continue; + } + for (int j = 0; j < test_suite.total_test_count(); ++j) { + const TestInfo& test_info = *test_suite.GetTestInfo(j); + if (!test_info.should_run() || !test_info.result()->Skipped()) { + continue; + } + ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); + printf("%s.%s", test_suite.name(), test_info.name()); + printf("\n"); + } + } +} + +void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, + int /*iteration*/) { + ColoredPrintf(GTestColor::kGreen, "[==========] "); + printf("%s from %s ran.", + FormatTestCount(unit_test.test_to_run_count()).c_str(), + FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); + if (GTEST_FLAG_GET(print_time)) { + printf(" (%s ms total)", + internal::StreamableToString(unit_test.elapsed_time()).c_str()); + } + printf("\n"); + ColoredPrintf(GTestColor::kGreen, "[ PASSED ] "); + printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); + + const int skipped_test_count = unit_test.skipped_test_count(); + if (skipped_test_count > 0) { + ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); + printf("%s, listed below:\n", FormatTestCount(skipped_test_count).c_str()); + PrintSkippedTests(unit_test); + } + + if (!unit_test.Passed()) { + PrintFailedTests(unit_test); + PrintFailedTestSuites(unit_test); + } + + int num_disabled = unit_test.reportable_disabled_test_count(); + if (num_disabled && !GTEST_FLAG_GET(also_run_disabled_tests)) { + if (unit_test.Passed()) { + printf("\n"); // Add a spacer if no FAILURE banner is displayed. + } + ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n", + num_disabled, num_disabled == 1 ? "TEST" : "TESTS"); + } + // Ensure that Google Test output is printed before, e.g., heapchecker output. + fflush(stdout); +} + +// End PrettyUnitTestResultPrinter + +// This class implements the TestEventListener interface. +// +// Class BriefUnitTestResultPrinter is copyable. +class BriefUnitTestResultPrinter : public TestEventListener { + public: + BriefUnitTestResultPrinter() {} + static void PrintTestName(const char* test_suite, const char* test) { + printf("%s.%s", test_suite, test); + } + + // The following methods override what's in the TestEventListener class. + void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} + void OnTestIterationStart(const UnitTest& /*unit_test*/, + int /*iteration*/) override {} + void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} + void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseStart(const TestCase& /*test_case*/) override {} +#else + void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} +#endif // OnTestCaseStart + + void OnTestStart(const TestInfo& /*test_info*/) override {} + void OnTestDisabled(const TestInfo& /*test_info*/) override {} + + void OnTestPartResult(const TestPartResult& result) override; + void OnTestEnd(const TestInfo& test_info) override; +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseEnd(const TestCase& /*test_case*/) override {} +#else + void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + + void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} + void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} + void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; + void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} +}; + +// Called after an assertion failure. +void BriefUnitTestResultPrinter::OnTestPartResult( + const TestPartResult& result) { + switch (result.type()) { + // If the test part succeeded, we don't need to do anything. + case TestPartResult::kSuccess: + return; + default: + // Print failure message from the assertion + // (e.g. expected this and got that). + PrintTestPartResult(result); + fflush(stdout); + } +} + +void BriefUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { + if (test_info.result()->Failed()) { + ColoredPrintf(GTestColor::kRed, "[ FAILED ] "); + PrintTestName(test_info.test_suite_name(), test_info.name()); + PrintFullTestCommentIfPresent(test_info); + + if (GTEST_FLAG_GET(print_time)) { + printf(" (%s ms)\n", + internal::StreamableToString(test_info.result()->elapsed_time()) + .c_str()); + } else { + printf("\n"); + } + fflush(stdout); + } +} + +void BriefUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, + int /*iteration*/) { + ColoredPrintf(GTestColor::kGreen, "[==========] "); + printf("%s from %s ran.", + FormatTestCount(unit_test.test_to_run_count()).c_str(), + FormatTestSuiteCount(unit_test.test_suite_to_run_count()).c_str()); + if (GTEST_FLAG_GET(print_time)) { + printf(" (%s ms total)", + internal::StreamableToString(unit_test.elapsed_time()).c_str()); + } + printf("\n"); + ColoredPrintf(GTestColor::kGreen, "[ PASSED ] "); + printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); + + const int skipped_test_count = unit_test.skipped_test_count(); + if (skipped_test_count > 0) { + ColoredPrintf(GTestColor::kGreen, "[ SKIPPED ] "); + printf("%s.\n", FormatTestCount(skipped_test_count).c_str()); + } + + int num_disabled = unit_test.reportable_disabled_test_count(); + if (num_disabled && !GTEST_FLAG_GET(also_run_disabled_tests)) { + if (unit_test.Passed()) { + printf("\n"); // Add a spacer if no FAILURE banner is displayed. + } + ColoredPrintf(GTestColor::kYellow, " YOU HAVE %d DISABLED %s\n\n", + num_disabled, num_disabled == 1 ? "TEST" : "TESTS"); + } + // Ensure that Google Test output is printed before, e.g., heapchecker output. + fflush(stdout); +} + +// End BriefUnitTestResultPrinter + +// class TestEventRepeater +// +// This class forwards events to other event listeners. +class TestEventRepeater : public TestEventListener { + public: + TestEventRepeater() : forwarding_enabled_(true) {} + ~TestEventRepeater() override; + void Append(TestEventListener* listener); + TestEventListener* Release(TestEventListener* listener); + + // Controls whether events will be forwarded to listeners_. Set to false + // in death test child processes. + bool forwarding_enabled() const { return forwarding_enabled_; } + void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; } + + void OnTestProgramStart(const UnitTest& unit_test) override; + void OnTestIterationStart(const UnitTest& unit_test, int iteration) override; + void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override; + void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) override; +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseStart(const TestSuite& parameter) override; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestSuiteStart(const TestSuite& parameter) override; + void OnTestStart(const TestInfo& test_info) override; + void OnTestDisabled(const TestInfo& test_info) override; + void OnTestPartResult(const TestPartResult& result) override; + void OnTestEnd(const TestInfo& test_info) override; +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestCaseEnd(const TestCase& parameter) override; +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + void OnTestSuiteEnd(const TestSuite& parameter) override; + void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override; + void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) override; + void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; + void OnTestProgramEnd(const UnitTest& unit_test) override; + + private: + // Controls whether events will be forwarded to listeners_. Set to false + // in death test child processes. + bool forwarding_enabled_; + // The list of listeners that receive events. + std::vector listeners_; + + TestEventRepeater(const TestEventRepeater&) = delete; + TestEventRepeater& operator=(const TestEventRepeater&) = delete; +}; + +TestEventRepeater::~TestEventRepeater() { + ForEach(listeners_, Delete); +} + +void TestEventRepeater::Append(TestEventListener* listener) { + listeners_.push_back(listener); +} + +TestEventListener* TestEventRepeater::Release(TestEventListener* listener) { + for (size_t i = 0; i < listeners_.size(); ++i) { + if (listeners_[i] == listener) { + listeners_.erase(listeners_.begin() + static_cast(i)); + return listener; + } + } + + return nullptr; +} + +// Since most methods are very similar, use macros to reduce boilerplate. +// This defines a member that forwards the call to all listeners. +#define GTEST_REPEATER_METHOD_(Name, Type) \ + void TestEventRepeater::Name(const Type& parameter) { \ + if (forwarding_enabled_) { \ + for (size_t i = 0; i < listeners_.size(); i++) { \ + listeners_[i]->Name(parameter); \ + } \ + } \ + } +// This defines a member that forwards the call to all listeners in reverse +// order. +#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \ + void TestEventRepeater::Name(const Type& parameter) { \ + if (forwarding_enabled_) { \ + for (size_t i = listeners_.size(); i != 0; i--) { \ + listeners_[i - 1]->Name(parameter); \ + } \ + } \ + } + +GTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest) +GTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest) +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +GTEST_REPEATER_METHOD_(OnTestCaseStart, TestSuite) +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +GTEST_REPEATER_METHOD_(OnTestSuiteStart, TestSuite) +GTEST_REPEATER_METHOD_(OnTestStart, TestInfo) +GTEST_REPEATER_METHOD_(OnTestDisabled, TestInfo) +GTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult) +GTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest) +GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest) +GTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest) +GTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo) +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +GTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestSuite) +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +GTEST_REVERSE_REPEATER_METHOD_(OnTestSuiteEnd, TestSuite) +GTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest) + +#undef GTEST_REPEATER_METHOD_ +#undef GTEST_REVERSE_REPEATER_METHOD_ + +void TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test, + int iteration) { + if (forwarding_enabled_) { + for (size_t i = 0; i < listeners_.size(); i++) { + listeners_[i]->OnTestIterationStart(unit_test, iteration); + } + } +} + +void TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test, + int iteration) { + if (forwarding_enabled_) { + for (size_t i = listeners_.size(); i > 0; i--) { + listeners_[i - 1]->OnTestIterationEnd(unit_test, iteration); + } + } +} + +// End TestEventRepeater + +// This class generates an XML output file. +class XmlUnitTestResultPrinter : public EmptyTestEventListener { + public: + explicit XmlUnitTestResultPrinter(const char* output_file); + + void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; + void ListTestsMatchingFilter(const std::vector& test_suites); + + // Prints an XML summary of all unit tests. + static void PrintXmlTestsList(std::ostream* stream, + const std::vector& test_suites); + + private: + // Is c a whitespace character that is normalized to a space character + // when it appears in an XML attribute value? + static bool IsNormalizableWhitespace(unsigned char c) { + return c == '\t' || c == '\n' || c == '\r'; + } + + // May c appear in a well-formed XML document? + // https://www.w3.org/TR/REC-xml/#charsets + static bool IsValidXmlCharacter(unsigned char c) { + return IsNormalizableWhitespace(c) || c >= 0x20; + } + + // Returns an XML-escaped copy of the input string str. If + // is_attribute is true, the text is meant to appear as an attribute + // value, and normalizable whitespace is preserved by replacing it + // with character references. + static std::string EscapeXml(const std::string& str, bool is_attribute); + + // Returns the given string with all characters invalid in XML removed. + static std::string RemoveInvalidXmlCharacters(const std::string& str); + + // Convenience wrapper around EscapeXml when str is an attribute value. + static std::string EscapeXmlAttribute(const std::string& str) { + return EscapeXml(str, true); + } + + // Convenience wrapper around EscapeXml when str is not an attribute value. + static std::string EscapeXmlText(const char* str) { + return EscapeXml(str, false); + } + + // Verifies that the given attribute belongs to the given element and + // streams the attribute as XML. + static void OutputXmlAttribute(std::ostream* stream, + const std::string& element_name, + const std::string& name, + const std::string& value); + + // Streams an XML CDATA section, escaping invalid CDATA sequences as needed. + static void OutputXmlCDataSection(::std::ostream* stream, const char* data); + + // Streams a test suite XML stanza containing the given test result. + // + // Requires: result.Failed() + static void OutputXmlTestSuiteForTestResult(::std::ostream* stream, + const TestResult& result); + + // Streams an XML representation of a TestResult object. + static void OutputXmlTestResult(::std::ostream* stream, + const TestResult& result); + + // Streams an XML representation of a TestInfo object. + static void OutputXmlTestInfo(::std::ostream* stream, + const char* test_suite_name, + const TestInfo& test_info); + + // Prints an XML representation of a TestSuite object + static void PrintXmlTestSuite(::std::ostream* stream, + const TestSuite& test_suite); + + // Prints an XML summary of unit_test to output stream out. + static void PrintXmlUnitTest(::std::ostream* stream, + const UnitTest& unit_test); + + // Produces a string representing the test properties in a result as space + // delimited XML attributes based on the property key="value" pairs. + // When the std::string is not empty, it includes a space at the beginning, + // to delimit this attribute from prior attributes. + static std::string TestPropertiesAsXmlAttributes(const TestResult& result); + + // Streams an XML representation of the test properties of a TestResult + // object. + static void OutputXmlTestProperties(std::ostream* stream, + const TestResult& result); + + // The output file. + const std::string output_file_; + + XmlUnitTestResultPrinter(const XmlUnitTestResultPrinter&) = delete; + XmlUnitTestResultPrinter& operator=(const XmlUnitTestResultPrinter&) = delete; +}; + +// Creates a new XmlUnitTestResultPrinter. +XmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file) + : output_file_(output_file) { + if (output_file_.empty()) { + GTEST_LOG_(FATAL) << "XML output file may not be null"; + } +} + +// Called after the unit test ends. +void XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, + int /*iteration*/) { + FILE* xmlout = OpenFileForWriting(output_file_); + std::stringstream stream; + PrintXmlUnitTest(&stream, unit_test); + fprintf(xmlout, "%s", StringStreamToString(&stream).c_str()); + fclose(xmlout); +} + +void XmlUnitTestResultPrinter::ListTestsMatchingFilter( + const std::vector& test_suites) { + FILE* xmlout = OpenFileForWriting(output_file_); + std::stringstream stream; + PrintXmlTestsList(&stream, test_suites); + fprintf(xmlout, "%s", StringStreamToString(&stream).c_str()); + fclose(xmlout); +} + +// Returns an XML-escaped copy of the input string str. If is_attribute +// is true, the text is meant to appear as an attribute value, and +// normalizable whitespace is preserved by replacing it with character +// references. +// +// Invalid XML characters in str, if any, are stripped from the output. +// It is expected that most, if not all, of the text processed by this +// module will consist of ordinary English text. +// If this module is ever modified to produce version 1.1 XML output, +// most invalid characters can be retained using character references. +std::string XmlUnitTestResultPrinter::EscapeXml(const std::string& str, + bool is_attribute) { + Message m; + + for (size_t i = 0; i < str.size(); ++i) { + const char ch = str[i]; + switch (ch) { + case '<': + m << "<"; + break; + case '>': + m << ">"; + break; + case '&': + m << "&"; + break; + case '\'': + if (is_attribute) + m << "'"; + else + m << '\''; + break; + case '"': + if (is_attribute) + m << """; + else + m << '"'; + break; + default: + if (IsValidXmlCharacter(static_cast(ch))) { + if (is_attribute && + IsNormalizableWhitespace(static_cast(ch))) + m << "&#x" << String::FormatByte(static_cast(ch)) + << ";"; + else + m << ch; + } + break; + } + } + + return m.GetString(); +} + +// Returns the given string with all characters invalid in XML removed. +// Currently invalid characters are dropped from the string. An +// alternative is to replace them with certain characters such as . or ?. +std::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters( + const std::string& str) { + std::string output; + output.reserve(str.size()); + for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) + if (IsValidXmlCharacter(static_cast(*it))) + output.push_back(*it); + + return output; +} + +// The following routines generate an XML representation of a UnitTest +// object. +// +// This is how Google Test concepts map to the DTD: +// +// <-- corresponds to a UnitTest object +// <-- corresponds to a TestSuite object +// <-- corresponds to a TestInfo object +// ... +// ... +// ... +// <-- individual assertion failures +// +// +// + +// Formats the given time in milliseconds as seconds. +std::string FormatTimeInMillisAsSeconds(TimeInMillis ms) { + ::std::stringstream ss; + ss << (static_cast(ms) * 1e-3); + return ss.str(); +} + +static bool PortableLocaltime(time_t seconds, struct tm* out) { +#if defined(_MSC_VER) + return localtime_s(out, &seconds) == 0; +#elif defined(__MINGW32__) || defined(__MINGW64__) + // MINGW provides neither localtime_r nor localtime_s, but uses + // Windows' localtime(), which has a thread-local tm buffer. + struct tm* tm_ptr = localtime(&seconds); // NOLINT + if (tm_ptr == nullptr) return false; + *out = *tm_ptr; + return true; +#elif defined(__STDC_LIB_EXT1__) + // Uses localtime_s when available as localtime_r is only available from + // C23 standard. + return localtime_s(&seconds, out) != nullptr; +#else + return localtime_r(&seconds, out) != nullptr; +#endif +} + +// Converts the given epoch time in milliseconds to a date string in the ISO +// 8601 format, without the timezone information. +std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) { + struct tm time_struct; + if (!PortableLocaltime(static_cast(ms / 1000), &time_struct)) + return ""; + // YYYY-MM-DDThh:mm:ss.sss + return StreamableToString(time_struct.tm_year + 1900) + "-" + + String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" + + String::FormatIntWidth2(time_struct.tm_mday) + "T" + + String::FormatIntWidth2(time_struct.tm_hour) + ":" + + String::FormatIntWidth2(time_struct.tm_min) + ":" + + String::FormatIntWidth2(time_struct.tm_sec) + "." + + String::FormatIntWidthN(static_cast(ms % 1000), 3); +} + +// Streams an XML CDATA section, escaping invalid CDATA sequences as needed. +void XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream, + const char* data) { + const char* segment = data; + *stream << ""); + if (next_segment != nullptr) { + stream->write(segment, + static_cast(next_segment - segment)); + *stream << "]]>]]>"); + } else { + *stream << segment; + break; + } + } + *stream << "]]>"; +} + +void XmlUnitTestResultPrinter::OutputXmlAttribute( + std::ostream* stream, const std::string& element_name, + const std::string& name, const std::string& value) { + const std::vector& allowed_names = + GetReservedOutputAttributesForElement(element_name); + + GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != + allowed_names.end()) + << "Attribute " << name << " is not allowed for element <" << element_name + << ">."; + + *stream << " " << name << "=\"" << EscapeXmlAttribute(value) << "\""; +} + +// Streams a test suite XML stanza containing the given test result. +void XmlUnitTestResultPrinter::OutputXmlTestSuiteForTestResult( + ::std::ostream* stream, const TestResult& result) { + // Output the boilerplate for a minimal test suite with one test. + *stream << " "; + + // Output the boilerplate for a minimal test case with a single test. + *stream << " \n"; +} + +// Prints an XML representation of a TestInfo object. +void XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream, + const char* test_suite_name, + const TestInfo& test_info) { + const TestResult& result = *test_info.result(); + const std::string kTestsuite = "testcase"; + + if (test_info.is_in_another_shard()) { + return; + } + + *stream << " \n"; + return; + } + + OutputXmlAttribute(stream, kTestsuite, "status", + test_info.should_run() ? "run" : "notrun"); + OutputXmlAttribute(stream, kTestsuite, "result", + test_info.should_run() + ? (result.Skipped() ? "skipped" : "completed") + : "suppressed"); + OutputXmlAttribute(stream, kTestsuite, "time", + FormatTimeInMillisAsSeconds(result.elapsed_time())); + OutputXmlAttribute( + stream, kTestsuite, "timestamp", + FormatEpochTimeInMillisAsIso8601(result.start_timestamp())); + OutputXmlAttribute(stream, kTestsuite, "classname", test_suite_name); + + OutputXmlTestResult(stream, result); +} + +void XmlUnitTestResultPrinter::OutputXmlTestResult(::std::ostream* stream, + const TestResult& result) { + int failures = 0; + int skips = 0; + for (int i = 0; i < result.total_part_count(); ++i) { + const TestPartResult& part = result.GetTestPartResult(i); + if (part.failed()) { + if (++failures == 1 && skips == 0) { + *stream << ">\n"; + } + const std::string location = + internal::FormatCompilerIndependentFileLocation(part.file_name(), + part.line_number()); + const std::string summary = location + "\n" + part.summary(); + *stream << " "; + const std::string detail = location + "\n" + part.message(); + OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str()); + *stream << "\n"; + } else if (part.skipped()) { + if (++skips == 1 && failures == 0) { + *stream << ">\n"; + } + const std::string location = + internal::FormatCompilerIndependentFileLocation(part.file_name(), + part.line_number()); + const std::string summary = location + "\n" + part.summary(); + *stream << " "; + const std::string detail = location + "\n" + part.message(); + OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str()); + *stream << "\n"; + } + } + + if (failures == 0 && skips == 0 && result.test_property_count() == 0) { + *stream << " />\n"; + } else { + if (failures == 0 && skips == 0) { + *stream << ">\n"; + } + OutputXmlTestProperties(stream, result); + *stream << " \n"; + } +} + +// Prints an XML representation of a TestSuite object +void XmlUnitTestResultPrinter::PrintXmlTestSuite(std::ostream* stream, + const TestSuite& test_suite) { + const std::string kTestsuite = "testsuite"; + *stream << " <" << kTestsuite; + OutputXmlAttribute(stream, kTestsuite, "name", test_suite.name()); + OutputXmlAttribute(stream, kTestsuite, "tests", + StreamableToString(test_suite.reportable_test_count())); + if (!GTEST_FLAG_GET(list_tests)) { + OutputXmlAttribute(stream, kTestsuite, "failures", + StreamableToString(test_suite.failed_test_count())); + OutputXmlAttribute( + stream, kTestsuite, "disabled", + StreamableToString(test_suite.reportable_disabled_test_count())); + OutputXmlAttribute(stream, kTestsuite, "skipped", + StreamableToString(test_suite.skipped_test_count())); + + OutputXmlAttribute(stream, kTestsuite, "errors", "0"); + + OutputXmlAttribute(stream, kTestsuite, "time", + FormatTimeInMillisAsSeconds(test_suite.elapsed_time())); + OutputXmlAttribute( + stream, kTestsuite, "timestamp", + FormatEpochTimeInMillisAsIso8601(test_suite.start_timestamp())); + *stream << TestPropertiesAsXmlAttributes(test_suite.ad_hoc_test_result()); + } + *stream << ">\n"; + for (int i = 0; i < test_suite.total_test_count(); ++i) { + if (test_suite.GetTestInfo(i)->is_reportable()) + OutputXmlTestInfo(stream, test_suite.name(), *test_suite.GetTestInfo(i)); + } + *stream << " \n"; +} + +// Prints an XML summary of unit_test to output stream out. +void XmlUnitTestResultPrinter::PrintXmlUnitTest(std::ostream* stream, + const UnitTest& unit_test) { + const std::string kTestsuites = "testsuites"; + + *stream << "\n"; + *stream << "<" << kTestsuites; + + OutputXmlAttribute(stream, kTestsuites, "tests", + StreamableToString(unit_test.reportable_test_count())); + OutputXmlAttribute(stream, kTestsuites, "failures", + StreamableToString(unit_test.failed_test_count())); + OutputXmlAttribute( + stream, kTestsuites, "disabled", + StreamableToString(unit_test.reportable_disabled_test_count())); + OutputXmlAttribute(stream, kTestsuites, "errors", "0"); + OutputXmlAttribute(stream, kTestsuites, "time", + FormatTimeInMillisAsSeconds(unit_test.elapsed_time())); + OutputXmlAttribute( + stream, kTestsuites, "timestamp", + FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp())); + + if (GTEST_FLAG_GET(shuffle)) { + OutputXmlAttribute(stream, kTestsuites, "random_seed", + StreamableToString(unit_test.random_seed())); + } + *stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result()); + + OutputXmlAttribute(stream, kTestsuites, "name", "AllTests"); + *stream << ">\n"; + + for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { + if (unit_test.GetTestSuite(i)->reportable_test_count() > 0) + PrintXmlTestSuite(stream, *unit_test.GetTestSuite(i)); + } + + // If there was a test failure outside of one of the test suites (like in a + // test environment) include that in the output. + if (unit_test.ad_hoc_test_result().Failed()) { + OutputXmlTestSuiteForTestResult(stream, unit_test.ad_hoc_test_result()); + } + + *stream << "\n"; +} + +void XmlUnitTestResultPrinter::PrintXmlTestsList( + std::ostream* stream, const std::vector& test_suites) { + const std::string kTestsuites = "testsuites"; + + *stream << "\n"; + *stream << "<" << kTestsuites; + + int total_tests = 0; + for (auto test_suite : test_suites) { + total_tests += test_suite->total_test_count(); + } + OutputXmlAttribute(stream, kTestsuites, "tests", + StreamableToString(total_tests)); + OutputXmlAttribute(stream, kTestsuites, "name", "AllTests"); + *stream << ">\n"; + + for (auto test_suite : test_suites) { + PrintXmlTestSuite(stream, *test_suite); + } + *stream << "\n"; +} + +// Produces a string representing the test properties in a result as space +// delimited XML attributes based on the property key="value" pairs. +std::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes( + const TestResult& result) { + Message attributes; + for (int i = 0; i < result.test_property_count(); ++i) { + const TestProperty& property = result.GetTestProperty(i); + attributes << " " << property.key() << "=" + << "\"" << EscapeXmlAttribute(property.value()) << "\""; + } + return attributes.GetString(); +} + +void XmlUnitTestResultPrinter::OutputXmlTestProperties( + std::ostream* stream, const TestResult& result) { + const std::string kProperties = "properties"; + const std::string kProperty = "property"; + + if (result.test_property_count() <= 0) { + return; + } + + *stream << " <" << kProperties << ">\n"; + for (int i = 0; i < result.test_property_count(); ++i) { + const TestProperty& property = result.GetTestProperty(i); + *stream << " <" << kProperty; + *stream << " name=\"" << EscapeXmlAttribute(property.key()) << "\""; + *stream << " value=\"" << EscapeXmlAttribute(property.value()) << "\""; + *stream << "/>\n"; + } + *stream << " \n"; +} + +// End XmlUnitTestResultPrinter + +// This class generates an JSON output file. +class JsonUnitTestResultPrinter : public EmptyTestEventListener { + public: + explicit JsonUnitTestResultPrinter(const char* output_file); + + void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override; + + // Prints an JSON summary of all unit tests. + static void PrintJsonTestList(::std::ostream* stream, + const std::vector& test_suites); + + private: + // Returns an JSON-escaped copy of the input string str. + static std::string EscapeJson(const std::string& str); + + //// Verifies that the given attribute belongs to the given element and + //// streams the attribute as JSON. + static void OutputJsonKey(std::ostream* stream, + const std::string& element_name, + const std::string& name, const std::string& value, + const std::string& indent, bool comma = true); + static void OutputJsonKey(std::ostream* stream, + const std::string& element_name, + const std::string& name, int value, + const std::string& indent, bool comma = true); + + // Streams a test suite JSON stanza containing the given test result. + // + // Requires: result.Failed() + static void OutputJsonTestSuiteForTestResult(::std::ostream* stream, + const TestResult& result); + + // Streams a JSON representation of a TestResult object. + static void OutputJsonTestResult(::std::ostream* stream, + const TestResult& result); + + // Streams a JSON representation of a TestInfo object. + static void OutputJsonTestInfo(::std::ostream* stream, + const char* test_suite_name, + const TestInfo& test_info); + + // Prints a JSON representation of a TestSuite object + static void PrintJsonTestSuite(::std::ostream* stream, + const TestSuite& test_suite); + + // Prints a JSON summary of unit_test to output stream out. + static void PrintJsonUnitTest(::std::ostream* stream, + const UnitTest& unit_test); + + // Produces a string representing the test properties in a result as + // a JSON dictionary. + static std::string TestPropertiesAsJson(const TestResult& result, + const std::string& indent); + + // The output file. + const std::string output_file_; + + JsonUnitTestResultPrinter(const JsonUnitTestResultPrinter&) = delete; + JsonUnitTestResultPrinter& operator=(const JsonUnitTestResultPrinter&) = + delete; +}; + +// Creates a new JsonUnitTestResultPrinter. +JsonUnitTestResultPrinter::JsonUnitTestResultPrinter(const char* output_file) + : output_file_(output_file) { + if (output_file_.empty()) { + GTEST_LOG_(FATAL) << "JSON output file may not be null"; + } +} + +void JsonUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, + int /*iteration*/) { + FILE* jsonout = OpenFileForWriting(output_file_); + std::stringstream stream; + PrintJsonUnitTest(&stream, unit_test); + fprintf(jsonout, "%s", StringStreamToString(&stream).c_str()); + fclose(jsonout); +} + +// Returns an JSON-escaped copy of the input string str. +std::string JsonUnitTestResultPrinter::EscapeJson(const std::string& str) { + Message m; + + for (size_t i = 0; i < str.size(); ++i) { + const char ch = str[i]; + switch (ch) { + case '\\': + case '"': + case '/': + m << '\\' << ch; + break; + case '\b': + m << "\\b"; + break; + case '\t': + m << "\\t"; + break; + case '\n': + m << "\\n"; + break; + case '\f': + m << "\\f"; + break; + case '\r': + m << "\\r"; + break; + default: + if (ch < ' ') { + m << "\\u00" << String::FormatByte(static_cast(ch)); + } else { + m << ch; + } + break; + } + } + + return m.GetString(); +} + +// The following routines generate an JSON representation of a UnitTest +// object. + +// Formats the given time in milliseconds as seconds. +static std::string FormatTimeInMillisAsDuration(TimeInMillis ms) { + ::std::stringstream ss; + ss << (static_cast(ms) * 1e-3) << "s"; + return ss.str(); +} + +// Converts the given epoch time in milliseconds to a date string in the +// RFC3339 format, without the timezone information. +static std::string FormatEpochTimeInMillisAsRFC3339(TimeInMillis ms) { + struct tm time_struct; + if (!PortableLocaltime(static_cast(ms / 1000), &time_struct)) + return ""; + // YYYY-MM-DDThh:mm:ss + return StreamableToString(time_struct.tm_year + 1900) + "-" + + String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" + + String::FormatIntWidth2(time_struct.tm_mday) + "T" + + String::FormatIntWidth2(time_struct.tm_hour) + ":" + + String::FormatIntWidth2(time_struct.tm_min) + ":" + + String::FormatIntWidth2(time_struct.tm_sec) + "Z"; +} + +static inline std::string Indent(size_t width) { + return std::string(width, ' '); +} + +void JsonUnitTestResultPrinter::OutputJsonKey(std::ostream* stream, + const std::string& element_name, + const std::string& name, + const std::string& value, + const std::string& indent, + bool comma) { + const std::vector& allowed_names = + GetReservedOutputAttributesForElement(element_name); + + GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != + allowed_names.end()) + << "Key \"" << name << "\" is not allowed for value \"" << element_name + << "\"."; + + *stream << indent << "\"" << name << "\": \"" << EscapeJson(value) << "\""; + if (comma) *stream << ",\n"; +} + +void JsonUnitTestResultPrinter::OutputJsonKey( + std::ostream* stream, const std::string& element_name, + const std::string& name, int value, const std::string& indent, bool comma) { + const std::vector& allowed_names = + GetReservedOutputAttributesForElement(element_name); + + GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) != + allowed_names.end()) + << "Key \"" << name << "\" is not allowed for value \"" << element_name + << "\"."; + + *stream << indent << "\"" << name << "\": " << StreamableToString(value); + if (comma) *stream << ",\n"; +} + +// Streams a test suite JSON stanza containing the given test result. +void JsonUnitTestResultPrinter::OutputJsonTestSuiteForTestResult( + ::std::ostream* stream, const TestResult& result) { + // Output the boilerplate for a new test suite. + *stream << Indent(4) << "{\n"; + OutputJsonKey(stream, "testsuite", "name", "NonTestSuiteFailure", Indent(6)); + OutputJsonKey(stream, "testsuite", "tests", 1, Indent(6)); + if (!GTEST_FLAG_GET(list_tests)) { + OutputJsonKey(stream, "testsuite", "failures", 1, Indent(6)); + OutputJsonKey(stream, "testsuite", "disabled", 0, Indent(6)); + OutputJsonKey(stream, "testsuite", "skipped", 0, Indent(6)); + OutputJsonKey(stream, "testsuite", "errors", 0, Indent(6)); + OutputJsonKey(stream, "testsuite", "time", + FormatTimeInMillisAsDuration(result.elapsed_time()), + Indent(6)); + OutputJsonKey(stream, "testsuite", "timestamp", + FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), + Indent(6)); + } + *stream << Indent(6) << "\"testsuite\": [\n"; + + // Output the boilerplate for a new test case. + *stream << Indent(8) << "{\n"; + OutputJsonKey(stream, "testcase", "name", "", Indent(10)); + OutputJsonKey(stream, "testcase", "status", "RUN", Indent(10)); + OutputJsonKey(stream, "testcase", "result", "COMPLETED", Indent(10)); + OutputJsonKey(stream, "testcase", "timestamp", + FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), + Indent(10)); + OutputJsonKey(stream, "testcase", "time", + FormatTimeInMillisAsDuration(result.elapsed_time()), + Indent(10)); + OutputJsonKey(stream, "testcase", "classname", "", Indent(10), false); + *stream << TestPropertiesAsJson(result, Indent(10)); + + // Output the actual test result. + OutputJsonTestResult(stream, result); + + // Finish the test suite. + *stream << "\n" << Indent(6) << "]\n" << Indent(4) << "}"; +} + +// Prints a JSON representation of a TestInfo object. +void JsonUnitTestResultPrinter::OutputJsonTestInfo(::std::ostream* stream, + const char* test_suite_name, + const TestInfo& test_info) { + const TestResult& result = *test_info.result(); + const std::string kTestsuite = "testcase"; + const std::string kIndent = Indent(10); + + *stream << Indent(8) << "{\n"; + OutputJsonKey(stream, kTestsuite, "name", test_info.name(), kIndent); + + if (test_info.value_param() != nullptr) { + OutputJsonKey(stream, kTestsuite, "value_param", test_info.value_param(), + kIndent); + } + if (test_info.type_param() != nullptr) { + OutputJsonKey(stream, kTestsuite, "type_param", test_info.type_param(), + kIndent); + } + + OutputJsonKey(stream, kTestsuite, "file", test_info.file(), kIndent); + OutputJsonKey(stream, kTestsuite, "line", test_info.line(), kIndent, false); + if (GTEST_FLAG_GET(list_tests)) { + *stream << "\n" << Indent(8) << "}"; + return; + } else { + *stream << ",\n"; + } + + OutputJsonKey(stream, kTestsuite, "status", + test_info.should_run() ? "RUN" : "NOTRUN", kIndent); + OutputJsonKey(stream, kTestsuite, "result", + test_info.should_run() + ? (result.Skipped() ? "SKIPPED" : "COMPLETED") + : "SUPPRESSED", + kIndent); + OutputJsonKey(stream, kTestsuite, "timestamp", + FormatEpochTimeInMillisAsRFC3339(result.start_timestamp()), + kIndent); + OutputJsonKey(stream, kTestsuite, "time", + FormatTimeInMillisAsDuration(result.elapsed_time()), kIndent); + OutputJsonKey(stream, kTestsuite, "classname", test_suite_name, kIndent, + false); + *stream << TestPropertiesAsJson(result, kIndent); + + OutputJsonTestResult(stream, result); +} + +void JsonUnitTestResultPrinter::OutputJsonTestResult(::std::ostream* stream, + const TestResult& result) { + const std::string kIndent = Indent(10); + + int failures = 0; + for (int i = 0; i < result.total_part_count(); ++i) { + const TestPartResult& part = result.GetTestPartResult(i); + if (part.failed()) { + *stream << ",\n"; + if (++failures == 1) { + *stream << kIndent << "\"" + << "failures" + << "\": [\n"; + } + const std::string location = + internal::FormatCompilerIndependentFileLocation(part.file_name(), + part.line_number()); + const std::string message = EscapeJson(location + "\n" + part.message()); + *stream << kIndent << " {\n" + << kIndent << " \"failure\": \"" << message << "\",\n" + << kIndent << " \"type\": \"\"\n" + << kIndent << " }"; + } + } + + if (failures > 0) *stream << "\n" << kIndent << "]"; + *stream << "\n" << Indent(8) << "}"; +} + +// Prints an JSON representation of a TestSuite object +void JsonUnitTestResultPrinter::PrintJsonTestSuite( + std::ostream* stream, const TestSuite& test_suite) { + const std::string kTestsuite = "testsuite"; + const std::string kIndent = Indent(6); + + *stream << Indent(4) << "{\n"; + OutputJsonKey(stream, kTestsuite, "name", test_suite.name(), kIndent); + OutputJsonKey(stream, kTestsuite, "tests", test_suite.reportable_test_count(), + kIndent); + if (!GTEST_FLAG_GET(list_tests)) { + OutputJsonKey(stream, kTestsuite, "failures", + test_suite.failed_test_count(), kIndent); + OutputJsonKey(stream, kTestsuite, "disabled", + test_suite.reportable_disabled_test_count(), kIndent); + OutputJsonKey(stream, kTestsuite, "errors", 0, kIndent); + OutputJsonKey( + stream, kTestsuite, "timestamp", + FormatEpochTimeInMillisAsRFC3339(test_suite.start_timestamp()), + kIndent); + OutputJsonKey(stream, kTestsuite, "time", + FormatTimeInMillisAsDuration(test_suite.elapsed_time()), + kIndent, false); + *stream << TestPropertiesAsJson(test_suite.ad_hoc_test_result(), kIndent) + << ",\n"; + } + + *stream << kIndent << "\"" << kTestsuite << "\": [\n"; + + bool comma = false; + for (int i = 0; i < test_suite.total_test_count(); ++i) { + if (test_suite.GetTestInfo(i)->is_reportable()) { + if (comma) { + *stream << ",\n"; + } else { + comma = true; + } + OutputJsonTestInfo(stream, test_suite.name(), *test_suite.GetTestInfo(i)); + } + } + *stream << "\n" << kIndent << "]\n" << Indent(4) << "}"; +} + +// Prints a JSON summary of unit_test to output stream out. +void JsonUnitTestResultPrinter::PrintJsonUnitTest(std::ostream* stream, + const UnitTest& unit_test) { + const std::string kTestsuites = "testsuites"; + const std::string kIndent = Indent(2); + *stream << "{\n"; + + OutputJsonKey(stream, kTestsuites, "tests", unit_test.reportable_test_count(), + kIndent); + OutputJsonKey(stream, kTestsuites, "failures", unit_test.failed_test_count(), + kIndent); + OutputJsonKey(stream, kTestsuites, "disabled", + unit_test.reportable_disabled_test_count(), kIndent); + OutputJsonKey(stream, kTestsuites, "errors", 0, kIndent); + if (GTEST_FLAG_GET(shuffle)) { + OutputJsonKey(stream, kTestsuites, "random_seed", unit_test.random_seed(), + kIndent); + } + OutputJsonKey(stream, kTestsuites, "timestamp", + FormatEpochTimeInMillisAsRFC3339(unit_test.start_timestamp()), + kIndent); + OutputJsonKey(stream, kTestsuites, "time", + FormatTimeInMillisAsDuration(unit_test.elapsed_time()), kIndent, + false); + + *stream << TestPropertiesAsJson(unit_test.ad_hoc_test_result(), kIndent) + << ",\n"; + + OutputJsonKey(stream, kTestsuites, "name", "AllTests", kIndent); + *stream << kIndent << "\"" << kTestsuites << "\": [\n"; + + bool comma = false; + for (int i = 0; i < unit_test.total_test_suite_count(); ++i) { + if (unit_test.GetTestSuite(i)->reportable_test_count() > 0) { + if (comma) { + *stream << ",\n"; + } else { + comma = true; + } + PrintJsonTestSuite(stream, *unit_test.GetTestSuite(i)); + } + } + + // If there was a test failure outside of one of the test suites (like in a + // test environment) include that in the output. + if (unit_test.ad_hoc_test_result().Failed()) { + OutputJsonTestSuiteForTestResult(stream, unit_test.ad_hoc_test_result()); + } + + *stream << "\n" + << kIndent << "]\n" + << "}\n"; +} + +void JsonUnitTestResultPrinter::PrintJsonTestList( + std::ostream* stream, const std::vector& test_suites) { + const std::string kTestsuites = "testsuites"; + const std::string kIndent = Indent(2); + *stream << "{\n"; + int total_tests = 0; + for (auto test_suite : test_suites) { + total_tests += test_suite->total_test_count(); + } + OutputJsonKey(stream, kTestsuites, "tests", total_tests, kIndent); + + OutputJsonKey(stream, kTestsuites, "name", "AllTests", kIndent); + *stream << kIndent << "\"" << kTestsuites << "\": [\n"; + + for (size_t i = 0; i < test_suites.size(); ++i) { + if (i != 0) { + *stream << ",\n"; + } + PrintJsonTestSuite(stream, *test_suites[i]); + } + + *stream << "\n" + << kIndent << "]\n" + << "}\n"; +} +// Produces a string representing the test properties in a result as +// a JSON dictionary. +std::string JsonUnitTestResultPrinter::TestPropertiesAsJson( + const TestResult& result, const std::string& indent) { + Message attributes; + for (int i = 0; i < result.test_property_count(); ++i) { + const TestProperty& property = result.GetTestProperty(i); + attributes << ",\n" + << indent << "\"" << property.key() << "\": " + << "\"" << EscapeJson(property.value()) << "\""; + } + return attributes.GetString(); +} + +// End JsonUnitTestResultPrinter + +#if GTEST_CAN_STREAM_RESULTS_ + +// Checks if str contains '=', '&', '%' or '\n' characters. If yes, +// replaces them by "%xx" where xx is their hexadecimal value. For +// example, replaces "=" with "%3D". This algorithm is O(strlen(str)) +// in both time and space -- important as the input str may contain an +// arbitrarily long test failure message and stack trace. +std::string StreamingListener::UrlEncode(const char* str) { + std::string result; + result.reserve(strlen(str) + 1); + for (char ch = *str; ch != '\0'; ch = *++str) { + switch (ch) { + case '%': + case '=': + case '&': + case '\n': + result.append("%" + String::FormatByte(static_cast(ch))); + break; + default: + result.push_back(ch); + break; + } + } + return result; +} + +void StreamingListener::SocketWriter::MakeConnection() { + GTEST_CHECK_(sockfd_ == -1) + << "MakeConnection() can't be called when there is already a connection."; + + addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses. + hints.ai_socktype = SOCK_STREAM; + addrinfo* servinfo = nullptr; + + // Use the getaddrinfo() to get a linked list of IP addresses for + // the given host name. + const int error_num = + getaddrinfo(host_name_.c_str(), port_num_.c_str(), &hints, &servinfo); + if (error_num != 0) { + GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: " + << gai_strerror(error_num); + } + + // Loop through all the results and connect to the first we can. + for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != nullptr; + cur_addr = cur_addr->ai_next) { + sockfd_ = socket(cur_addr->ai_family, cur_addr->ai_socktype, + cur_addr->ai_protocol); + if (sockfd_ != -1) { + // Connect the client socket to the server socket. + if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) { + close(sockfd_); + sockfd_ = -1; + } + } + } + + freeaddrinfo(servinfo); // all done with this structure + + if (sockfd_ == -1) { + GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to " + << host_name_ << ":" << port_num_; + } +} + +// End of class Streaming Listener +#endif // GTEST_CAN_STREAM_RESULTS__ + +// class OsStackTraceGetter + +const char* const OsStackTraceGetterInterface::kElidedFramesMarker = + "... " GTEST_NAME_ " internal frames ..."; + +std::string OsStackTraceGetter::CurrentStackTrace(int max_depth, int skip_count) + GTEST_LOCK_EXCLUDED_(mutex_) { +#if GTEST_HAS_ABSL + std::string result; + + if (max_depth <= 0) { + return result; + } + + max_depth = std::min(max_depth, kMaxStackTraceDepth); + + std::vector raw_stack(max_depth); + // Skips the frames requested by the caller, plus this function. + const int raw_stack_size = + absl::GetStackTrace(&raw_stack[0], max_depth, skip_count + 1); + + void* caller_frame = nullptr; + { + MutexLock lock(&mutex_); + caller_frame = caller_frame_; + } + + for (int i = 0; i < raw_stack_size; ++i) { + if (raw_stack[i] == caller_frame && + !GTEST_FLAG_GET(show_internal_stack_frames)) { + // Add a marker to the trace and stop adding frames. + absl::StrAppend(&result, kElidedFramesMarker, "\n"); + break; + } + + char tmp[1024]; + const char* symbol = "(unknown)"; + if (absl::Symbolize(raw_stack[i], tmp, sizeof(tmp))) { + symbol = tmp; + } + + char line[1024]; + snprintf(line, sizeof(line), " %p: %s\n", raw_stack[i], symbol); + result += line; + } + + return result; + +#else // !GTEST_HAS_ABSL + static_cast(max_depth); + static_cast(skip_count); + return ""; +#endif // GTEST_HAS_ABSL +} + +void OsStackTraceGetter::UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_) { +#if GTEST_HAS_ABSL + void* caller_frame = nullptr; + if (absl::GetStackTrace(&caller_frame, 1, 3) <= 0) { + caller_frame = nullptr; + } + + MutexLock lock(&mutex_); + caller_frame_ = caller_frame; +#endif // GTEST_HAS_ABSL +} + +// A helper class that creates the premature-exit file in its +// constructor and deletes the file in its destructor. +class ScopedPrematureExitFile { + public: + explicit ScopedPrematureExitFile(const char* premature_exit_filepath) + : premature_exit_filepath_( + premature_exit_filepath ? premature_exit_filepath : "") { + // If a path to the premature-exit file is specified... + if (!premature_exit_filepath_.empty()) { + // create the file with a single "0" character in it. I/O + // errors are ignored as there's nothing better we can do and we + // don't want to fail the test because of this. + FILE* pfile = posix::FOpen(premature_exit_filepath_.c_str(), "w"); + fwrite("0", 1, 1, pfile); + fclose(pfile); + } + } + + ~ScopedPrematureExitFile() { +#if !defined GTEST_OS_ESP8266 + if (!premature_exit_filepath_.empty()) { + int retval = remove(premature_exit_filepath_.c_str()); + if (retval) { + GTEST_LOG_(ERROR) << "Failed to remove premature exit filepath \"" + << premature_exit_filepath_ << "\" with error " + << retval; + } + } +#endif + } + + private: + const std::string premature_exit_filepath_; + + ScopedPrematureExitFile(const ScopedPrematureExitFile&) = delete; + ScopedPrematureExitFile& operator=(const ScopedPrematureExitFile&) = delete; +}; + +} // namespace internal + +// class TestEventListeners + +TestEventListeners::TestEventListeners() + : repeater_(new internal::TestEventRepeater()), + default_result_printer_(nullptr), + default_xml_generator_(nullptr) {} + +TestEventListeners::~TestEventListeners() { delete repeater_; } + +// Returns the standard listener responsible for the default console +// output. Can be removed from the listeners list to shut down default +// console output. Note that removing this object from the listener list +// with Release transfers its ownership to the user. +void TestEventListeners::Append(TestEventListener* listener) { + repeater_->Append(listener); +} + +// Removes the given event listener from the list and returns it. It then +// becomes the caller's responsibility to delete the listener. Returns +// NULL if the listener is not found in the list. +TestEventListener* TestEventListeners::Release(TestEventListener* listener) { + if (listener == default_result_printer_) + default_result_printer_ = nullptr; + else if (listener == default_xml_generator_) + default_xml_generator_ = nullptr; + return repeater_->Release(listener); +} + +// Returns repeater that broadcasts the TestEventListener events to all +// subscribers. +TestEventListener* TestEventListeners::repeater() { return repeater_; } + +// Sets the default_result_printer attribute to the provided listener. +// The listener is also added to the listener list and previous +// default_result_printer is removed from it and deleted. The listener can +// also be NULL in which case it will not be added to the list. Does +// nothing if the previous and the current listener objects are the same. +void TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) { + if (default_result_printer_ != listener) { + // It is an error to pass this method a listener that is already in the + // list. + delete Release(default_result_printer_); + default_result_printer_ = listener; + if (listener != nullptr) Append(listener); + } +} + +// Sets the default_xml_generator attribute to the provided listener. The +// listener is also added to the listener list and previous +// default_xml_generator is removed from it and deleted. The listener can +// also be NULL in which case it will not be added to the list. Does +// nothing if the previous and the current listener objects are the same. +void TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) { + if (default_xml_generator_ != listener) { + // It is an error to pass this method a listener that is already in the + // list. + delete Release(default_xml_generator_); + default_xml_generator_ = listener; + if (listener != nullptr) Append(listener); + } +} + +// Controls whether events will be forwarded by the repeater to the +// listeners in the list. +bool TestEventListeners::EventForwardingEnabled() const { + return repeater_->forwarding_enabled(); +} + +void TestEventListeners::SuppressEventForwarding() { + repeater_->set_forwarding_enabled(false); +} + +// class UnitTest + +// Gets the singleton UnitTest object. The first time this method is +// called, a UnitTest object is constructed and returned. Consecutive +// calls will return the same object. +// +// We don't protect this under mutex_ as a user is not supposed to +// call this before main() starts, from which point on the return +// value will never change. +UnitTest* UnitTest::GetInstance() { + // CodeGear C++Builder insists on a public destructor for the + // default implementation. Use this implementation to keep good OO + // design with private destructor. + +#if defined(__BORLANDC__) + static UnitTest* const instance = new UnitTest; + return instance; +#else + static UnitTest instance; + return &instance; +#endif // defined(__BORLANDC__) +} + +// Gets the number of successful test suites. +int UnitTest::successful_test_suite_count() const { + return impl()->successful_test_suite_count(); +} + +// Gets the number of failed test suites. +int UnitTest::failed_test_suite_count() const { + return impl()->failed_test_suite_count(); +} + +// Gets the number of all test suites. +int UnitTest::total_test_suite_count() const { + return impl()->total_test_suite_count(); +} + +// Gets the number of all test suites that contain at least one test +// that should run. +int UnitTest::test_suite_to_run_count() const { + return impl()->test_suite_to_run_count(); +} + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +int UnitTest::successful_test_case_count() const { + return impl()->successful_test_suite_count(); +} +int UnitTest::failed_test_case_count() const { + return impl()->failed_test_suite_count(); +} +int UnitTest::total_test_case_count() const { + return impl()->total_test_suite_count(); +} +int UnitTest::test_case_to_run_count() const { + return impl()->test_suite_to_run_count(); +} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +// Gets the number of successful tests. +int UnitTest::successful_test_count() const { + return impl()->successful_test_count(); +} + +// Gets the number of skipped tests. +int UnitTest::skipped_test_count() const { + return impl()->skipped_test_count(); +} + +// Gets the number of failed tests. +int UnitTest::failed_test_count() const { return impl()->failed_test_count(); } + +// Gets the number of disabled tests that will be reported in the XML report. +int UnitTest::reportable_disabled_test_count() const { + return impl()->reportable_disabled_test_count(); +} + +// Gets the number of disabled tests. +int UnitTest::disabled_test_count() const { + return impl()->disabled_test_count(); +} + +// Gets the number of tests to be printed in the XML report. +int UnitTest::reportable_test_count() const { + return impl()->reportable_test_count(); +} + +// Gets the number of all tests. +int UnitTest::total_test_count() const { return impl()->total_test_count(); } + +// Gets the number of tests that should run. +int UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); } + +// Gets the time of the test program start, in ms from the start of the +// UNIX epoch. +internal::TimeInMillis UnitTest::start_timestamp() const { + return impl()->start_timestamp(); +} + +// Gets the elapsed time, in milliseconds. +internal::TimeInMillis UnitTest::elapsed_time() const { + return impl()->elapsed_time(); +} + +// Returns true if and only if the unit test passed (i.e. all test suites +// passed). +bool UnitTest::Passed() const { return impl()->Passed(); } + +// Returns true if and only if the unit test failed (i.e. some test suite +// failed or something outside of all tests failed). +bool UnitTest::Failed() const { return impl()->Failed(); } + +// Gets the i-th test suite among all the test suites. i can range from 0 to +// total_test_suite_count() - 1. If i is not in that range, returns NULL. +const TestSuite* UnitTest::GetTestSuite(int i) const { + return impl()->GetTestSuite(i); +} + +// Legacy API is deprecated but still available +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +const TestCase* UnitTest::GetTestCase(int i) const { + return impl()->GetTestCase(i); +} +#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ + +// Returns the TestResult containing information on test failures and +// properties logged outside of individual test suites. +const TestResult& UnitTest::ad_hoc_test_result() const { + return *impl()->ad_hoc_test_result(); +} + +// Gets the i-th test suite among all the test suites. i can range from 0 to +// total_test_suite_count() - 1. If i is not in that range, returns NULL. +TestSuite* UnitTest::GetMutableTestSuite(int i) { + return impl()->GetMutableSuiteCase(i); +} + +// Returns the list of event listeners that can be used to track events +// inside Google Test. +TestEventListeners& UnitTest::listeners() { return *impl()->listeners(); } + +// Registers and returns a global test environment. When a test +// program is run, all global test environments will be set-up in the +// order they were registered. After all tests in the program have +// finished, all global test environments will be torn-down in the +// *reverse* order they were registered. +// +// The UnitTest object takes ownership of the given environment. +// +// We don't protect this under mutex_, as we only support calling it +// from the main thread. +Environment* UnitTest::AddEnvironment(Environment* env) { + if (env == nullptr) { + return nullptr; + } + + impl_->environments().push_back(env); + return env; +} + +// Adds a TestPartResult to the current TestResult object. All Google Test +// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call +// this to report their results. The user code should use the +// assertion macros instead of calling this directly. +void UnitTest::AddTestPartResult(TestPartResult::Type result_type, + const char* file_name, int line_number, + const std::string& message, + const std::string& os_stack_trace) + GTEST_LOCK_EXCLUDED_(mutex_) { + Message msg; + msg << message; + + internal::MutexLock lock(&mutex_); + if (impl_->gtest_trace_stack().size() > 0) { + msg << "\n" << GTEST_NAME_ << " trace:"; + + for (size_t i = impl_->gtest_trace_stack().size(); i > 0; --i) { + const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1]; + msg << "\n" + << internal::FormatFileLocation(trace.file, trace.line) << " " + << trace.message; + } + } + + if (os_stack_trace.c_str() != nullptr && !os_stack_trace.empty()) { + msg << internal::kStackTraceMarker << os_stack_trace; + } + + const TestPartResult result = TestPartResult( + result_type, file_name, line_number, msg.GetString().c_str()); + impl_->GetTestPartResultReporterForCurrentThread()->ReportTestPartResult( + result); + + if (result_type != TestPartResult::kSuccess && + result_type != TestPartResult::kSkip) { + // gtest_break_on_failure takes precedence over + // gtest_throw_on_failure. This allows a user to set the latter + // in the code (perhaps in order to use Google Test assertions + // with another testing framework) and specify the former on the + // command line for debugging. + if (GTEST_FLAG_GET(break_on_failure)) { +#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT + // Using DebugBreak on Windows allows gtest to still break into a debugger + // when a failure happens and both the --gtest_break_on_failure and + // the --gtest_catch_exceptions flags are specified. + DebugBreak(); +#elif (!defined(__native_client__)) && \ + ((defined(__clang__) || defined(__GNUC__)) && \ + (defined(__x86_64__) || defined(__i386__))) + // with clang/gcc we can achieve the same effect on x86 by invoking int3 + asm("int3"); +#else + // Dereference nullptr through a volatile pointer to prevent the compiler + // from removing. We use this rather than abort() or __builtin_trap() for + // portability: some debuggers don't correctly trap abort(). + *static_cast(nullptr) = 1; +#endif // GTEST_OS_WINDOWS + } else if (GTEST_FLAG_GET(throw_on_failure)) { +#if GTEST_HAS_EXCEPTIONS + throw internal::GoogleTestFailureException(result); +#else + // We cannot call abort() as it generates a pop-up in debug mode + // that cannot be suppressed in VC 7.1 or below. + exit(1); +#endif + } + } +} + +// Adds a TestProperty to the current TestResult object when invoked from +// inside a test, to current TestSuite's ad_hoc_test_result_ when invoked +// from SetUpTestSuite or TearDownTestSuite, or to the global property set +// when invoked elsewhere. If the result already contains a property with +// the same key, the value will be updated. +void UnitTest::RecordProperty(const std::string& key, + const std::string& value) { + impl_->RecordProperty(TestProperty(key, value)); +} + +// Runs all tests in this UnitTest object and prints the result. +// Returns 0 if successful, or 1 otherwise. +// +// We don't protect this under mutex_, as we only support calling it +// from the main thread. +int UnitTest::Run() { + const bool in_death_test_child_process = + GTEST_FLAG_GET(internal_run_death_test).length() > 0; + + // Google Test implements this protocol for catching that a test + // program exits before returning control to Google Test: + // + // 1. Upon start, Google Test creates a file whose absolute path + // is specified by the environment variable + // TEST_PREMATURE_EXIT_FILE. + // 2. When Google Test has finished its work, it deletes the file. + // + // This allows a test runner to set TEST_PREMATURE_EXIT_FILE before + // running a Google-Test-based test program and check the existence + // of the file at the end of the test execution to see if it has + // exited prematurely. + + // If we are in the child process of a death test, don't + // create/delete the premature exit file, as doing so is unnecessary + // and will confuse the parent process. Otherwise, create/delete + // the file upon entering/leaving this function. If the program + // somehow exits before this function has a chance to return, the + // premature-exit file will be left undeleted, causing a test runner + // that understands the premature-exit-file protocol to report the + // test as having failed. + const internal::ScopedPrematureExitFile premature_exit_file( + in_death_test_child_process + ? nullptr + : internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE")); + + // Captures the value of GTEST_FLAG(catch_exceptions). This value will be + // used for the duration of the program. + impl()->set_catch_exceptions(GTEST_FLAG_GET(catch_exceptions)); + +#if GTEST_OS_WINDOWS + // Either the user wants Google Test to catch exceptions thrown by the + // tests or this is executing in the context of death test child + // process. In either case the user does not want to see pop-up dialogs + // about crashes - they are expected. + if (impl()->catch_exceptions() || in_death_test_child_process) { +#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT + // SetErrorMode doesn't exist on CE. + SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | + SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); +#endif // !GTEST_OS_WINDOWS_MOBILE + +#if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE + // Death test children can be terminated with _abort(). On Windows, + // _abort() can show a dialog with a warning message. This forces the + // abort message to go to stderr instead. + _set_error_mode(_OUT_TO_STDERR); +#endif + +#if defined(_MSC_VER) && !GTEST_OS_WINDOWS_MOBILE + // In the debug version, Visual Studio pops up a separate dialog + // offering a choice to debug the aborted program. We need to suppress + // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement + // executed. Google Test will notify the user of any unexpected + // failure via stderr. + if (!GTEST_FLAG_GET(break_on_failure)) + _set_abort_behavior( + 0x0, // Clear the following flags: + _WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump. + + // In debug mode, the Windows CRT can crash with an assertion over invalid + // input (e.g. passing an invalid file descriptor). The default handling + // for these assertions is to pop up a dialog and wait for user input. + // Instead ask the CRT to dump such assertions to stderr non-interactively. + if (!IsDebuggerPresent()) { + (void)_CrtSetReportMode(_CRT_ASSERT, + _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + (void)_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); + } +#endif + } +#endif // GTEST_OS_WINDOWS + + return internal::HandleExceptionsInMethodIfSupported( + impl(), &internal::UnitTestImpl::RunAllTests, + "auxiliary test code (environments or event listeners)") + ? 0 + : 1; +} + +// Returns the working directory when the first TEST() or TEST_F() was +// executed. +const char* UnitTest::original_working_dir() const { + return impl_->original_working_dir_.c_str(); +} + +// Returns the TestSuite object for the test that's currently running, +// or NULL if no test is running. +const TestSuite* UnitTest::current_test_suite() const + GTEST_LOCK_EXCLUDED_(mutex_) { + internal::MutexLock lock(&mutex_); + return impl_->current_test_suite(); +} + +// Legacy API is still available but deprecated +#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ +const TestCase* UnitTest::current_test_case() const + GTEST_LOCK_EXCLUDED_(mutex_) { + internal::MutexLock lock(&mutex_); + return impl_->current_test_suite(); +} +#endif + +// Returns the TestInfo object for the test that's currently running, +// or NULL if no test is running. +const TestInfo* UnitTest::current_test_info() const + GTEST_LOCK_EXCLUDED_(mutex_) { + internal::MutexLock lock(&mutex_); + return impl_->current_test_info(); +} + +// Returns the random seed used at the start of the current test run. +int UnitTest::random_seed() const { return impl_->random_seed(); } + +// Returns ParameterizedTestSuiteRegistry object used to keep track of +// value-parameterized tests and instantiate and register them. +internal::ParameterizedTestSuiteRegistry& +UnitTest::parameterized_test_registry() GTEST_LOCK_EXCLUDED_(mutex_) { + return impl_->parameterized_test_registry(); +} + +// Creates an empty UnitTest. +UnitTest::UnitTest() { impl_ = new internal::UnitTestImpl(this); } + +// Destructor of UnitTest. +UnitTest::~UnitTest() { delete impl_; } + +// Pushes a trace defined by SCOPED_TRACE() on to the per-thread +// Google Test trace stack. +void UnitTest::PushGTestTrace(const internal::TraceInfo& trace) + GTEST_LOCK_EXCLUDED_(mutex_) { + internal::MutexLock lock(&mutex_); + impl_->gtest_trace_stack().push_back(trace); +} + +// Pops a trace from the per-thread Google Test trace stack. +void UnitTest::PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_) { + internal::MutexLock lock(&mutex_); + impl_->gtest_trace_stack().pop_back(); +} + +namespace internal { + +UnitTestImpl::UnitTestImpl(UnitTest* parent) + : parent_(parent), + GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355 /* using this in initializer */) + default_global_test_part_result_reporter_(this), + default_per_thread_test_part_result_reporter_(this), + GTEST_DISABLE_MSC_WARNINGS_POP_() global_test_part_result_repoter_( + &default_global_test_part_result_reporter_), + per_thread_test_part_result_reporter_( + &default_per_thread_test_part_result_reporter_), + parameterized_test_registry_(), + parameterized_tests_registered_(false), + last_death_test_suite_(-1), + current_test_suite_(nullptr), + current_test_info_(nullptr), + ad_hoc_test_result_(), + os_stack_trace_getter_(nullptr), + post_flag_parse_init_performed_(false), + random_seed_(0), // Will be overridden by the flag before first use. + random_(0), // Will be reseeded before first use. + start_timestamp_(0), + elapsed_time_(0), +#if GTEST_HAS_DEATH_TEST + death_test_factory_(new DefaultDeathTestFactory), +#endif + // Will be overridden by the flag before first use. + catch_exceptions_(false) { + listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter); +} + +UnitTestImpl::~UnitTestImpl() { + // Deletes every TestSuite. + ForEach(test_suites_, internal::Delete); + + // Deletes every Environment. + ForEach(environments_, internal::Delete); + + delete os_stack_trace_getter_; +} + +// Adds a TestProperty to the current TestResult object when invoked in a +// context of a test, to current test suite's ad_hoc_test_result when invoke +// from SetUpTestSuite/TearDownTestSuite, or to the global property set +// otherwise. If the result already contains a property with the same key, +// the value will be updated. +void UnitTestImpl::RecordProperty(const TestProperty& test_property) { + std::string xml_element; + TestResult* test_result; // TestResult appropriate for property recording. + + if (current_test_info_ != nullptr) { + xml_element = "testcase"; + test_result = &(current_test_info_->result_); + } else if (current_test_suite_ != nullptr) { + xml_element = "testsuite"; + test_result = &(current_test_suite_->ad_hoc_test_result_); + } else { + xml_element = "testsuites"; + test_result = &ad_hoc_test_result_; + } + test_result->RecordProperty(xml_element, test_property); +} + +#if GTEST_HAS_DEATH_TEST +// Disables event forwarding if the control is currently in a death test +// subprocess. Must not be called before InitGoogleTest. +void UnitTestImpl::SuppressTestEventsIfInSubprocess() { + if (internal_run_death_test_flag_.get() != nullptr) + listeners()->SuppressEventForwarding(); +} +#endif // GTEST_HAS_DEATH_TEST + +// Initializes event listeners performing XML output as specified by +// UnitTestOptions. Must not be called before InitGoogleTest. +void UnitTestImpl::ConfigureXmlOutput() { + const std::string& output_format = UnitTestOptions::GetOutputFormat(); + if (output_format == "xml") { + listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter( + UnitTestOptions::GetAbsolutePathToOutputFile().c_str())); + } else if (output_format == "json") { + listeners()->SetDefaultXmlGenerator(new JsonUnitTestResultPrinter( + UnitTestOptions::GetAbsolutePathToOutputFile().c_str())); + } else if (output_format != "") { + GTEST_LOG_(WARNING) << "WARNING: unrecognized output format \"" + << output_format << "\" ignored."; + } +} + +#if GTEST_CAN_STREAM_RESULTS_ +// Initializes event listeners for streaming test results in string form. +// Must not be called before InitGoogleTest. +void UnitTestImpl::ConfigureStreamingOutput() { + const std::string& target = GTEST_FLAG_GET(stream_result_to); + if (!target.empty()) { + const size_t pos = target.find(':'); + if (pos != std::string::npos) { + listeners()->Append( + new StreamingListener(target.substr(0, pos), target.substr(pos + 1))); + } else { + GTEST_LOG_(WARNING) << "unrecognized streaming target \"" << target + << "\" ignored."; + } + } +} +#endif // GTEST_CAN_STREAM_RESULTS_ + +// Performs initialization dependent upon flag values obtained in +// ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to +// ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest +// this function is also called from RunAllTests. Since this function can be +// called more than once, it has to be idempotent. +void UnitTestImpl::PostFlagParsingInit() { + // Ensures that this function does not execute more than once. + if (!post_flag_parse_init_performed_) { + post_flag_parse_init_performed_ = true; + +#if defined(GTEST_CUSTOM_TEST_EVENT_LISTENER_) + // Register to send notifications about key process state changes. + listeners()->Append(new GTEST_CUSTOM_TEST_EVENT_LISTENER_()); +#endif // defined(GTEST_CUSTOM_TEST_EVENT_LISTENER_) + +#if GTEST_HAS_DEATH_TEST + InitDeathTestSubprocessControlInfo(); + SuppressTestEventsIfInSubprocess(); +#endif // GTEST_HAS_DEATH_TEST + + // Registers parameterized tests. This makes parameterized tests + // available to the UnitTest reflection API without running + // RUN_ALL_TESTS. + RegisterParameterizedTests(); + + // Configures listeners for XML output. This makes it possible for users + // to shut down the default XML output before invoking RUN_ALL_TESTS. + ConfigureXmlOutput(); + + if (GTEST_FLAG_GET(brief)) { + listeners()->SetDefaultResultPrinter(new BriefUnitTestResultPrinter); + } + +#if GTEST_CAN_STREAM_RESULTS_ + // Configures listeners for streaming test results to the specified server. + ConfigureStreamingOutput(); +#endif // GTEST_CAN_STREAM_RESULTS_ + +#if GTEST_HAS_ABSL + if (GTEST_FLAG_GET(install_failure_signal_handler)) { + absl::FailureSignalHandlerOptions options; + absl::InstallFailureSignalHandler(options); + } +#endif // GTEST_HAS_ABSL + } +} + +// A predicate that checks the name of a TestSuite against a known +// value. +// +// This is used for implementation of the UnitTest class only. We put +// it in the anonymous namespace to prevent polluting the outer +// namespace. +// +// TestSuiteNameIs is copyable. +class TestSuiteNameIs { + public: + // Constructor. + explicit TestSuiteNameIs(const std::string& name) : name_(name) {} + + // Returns true if and only if the name of test_suite matches name_. + bool operator()(const TestSuite* test_suite) const { + return test_suite != nullptr && + strcmp(test_suite->name(), name_.c_str()) == 0; + } + + private: + std::string name_; +}; + +// Finds and returns a TestSuite with the given name. If one doesn't +// exist, creates one and returns it. It's the CALLER'S +// RESPONSIBILITY to ensure that this function is only called WHEN THE +// TESTS ARE NOT SHUFFLED. +// +// Arguments: +// +// test_suite_name: name of the test suite +// type_param: the name of the test suite's type parameter, or NULL if +// this is not a typed or a type-parameterized test suite. +// set_up_tc: pointer to the function that sets up the test suite +// tear_down_tc: pointer to the function that tears down the test suite +TestSuite* UnitTestImpl::GetTestSuite( + const char* test_suite_name, const char* type_param, + internal::SetUpTestSuiteFunc set_up_tc, + internal::TearDownTestSuiteFunc tear_down_tc) { + // Can we find a TestSuite with the given name? + const auto test_suite = + std::find_if(test_suites_.rbegin(), test_suites_.rend(), + TestSuiteNameIs(test_suite_name)); + + if (test_suite != test_suites_.rend()) return *test_suite; + + // No. Let's create one. + auto* const new_test_suite = + new TestSuite(test_suite_name, type_param, set_up_tc, tear_down_tc); + + const UnitTestFilter death_test_suite_filter(kDeathTestSuiteFilter); + // Is this a death test suite? + if (death_test_suite_filter.MatchesName(test_suite_name)) { + // Yes. Inserts the test suite after the last death test suite + // defined so far. This only works when the test suites haven't + // been shuffled. Otherwise we may end up running a death test + // after a non-death test. + ++last_death_test_suite_; + test_suites_.insert(test_suites_.begin() + last_death_test_suite_, + new_test_suite); + } else { + // No. Appends to the end of the list. + test_suites_.push_back(new_test_suite); + } + + test_suite_indices_.push_back(static_cast(test_suite_indices_.size())); + return new_test_suite; +} + +// Helpers for setting up / tearing down the given environment. They +// are for use in the ForEach() function. +static void SetUpEnvironment(Environment* env) { env->SetUp(); } +static void TearDownEnvironment(Environment* env) { env->TearDown(); } + +// Runs all tests in this UnitTest object, prints the result, and +// returns true if all tests are successful. If any exception is +// thrown during a test, the test is considered to be failed, but the +// rest of the tests will still be run. +// +// When parameterized tests are enabled, it expands and registers +// parameterized tests first in RegisterParameterizedTests(). +// All other functions called from RunAllTests() may safely assume that +// parameterized tests are ready to be counted and run. +bool UnitTestImpl::RunAllTests() { + // True if and only if Google Test is initialized before RUN_ALL_TESTS() is + // called. + const bool gtest_is_initialized_before_run_all_tests = GTestIsInitialized(); + + // Do not run any test if the --help flag was specified. + if (g_help_flag) return true; + + // Repeats the call to the post-flag parsing initialization in case the + // user didn't call InitGoogleTest. + PostFlagParsingInit(); + + // Even if sharding is not on, test runners may want to use the + // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding + // protocol. + internal::WriteToShardStatusFileIfNeeded(); + + // True if and only if we are in a subprocess for running a thread-safe-style + // death test. + bool in_subprocess_for_death_test = false; + +#if GTEST_HAS_DEATH_TEST + in_subprocess_for_death_test = + (internal_run_death_test_flag_.get() != nullptr); +#if defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) + if (in_subprocess_for_death_test) { + GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_(); + } +#endif // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) +#endif // GTEST_HAS_DEATH_TEST + + const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex, + in_subprocess_for_death_test); + + // Compares the full test names with the filter to decide which + // tests to run. + const bool has_tests_to_run = + FilterTests(should_shard ? HONOR_SHARDING_PROTOCOL + : IGNORE_SHARDING_PROTOCOL) > 0; + + // Lists the tests and exits if the --gtest_list_tests flag was specified. + if (GTEST_FLAG_GET(list_tests)) { + // This must be called *after* FilterTests() has been called. + ListTestsMatchingFilter(); + return true; + } + + random_seed_ = GetRandomSeedFromFlag(GTEST_FLAG_GET(random_seed)); + + // True if and only if at least one test has failed. + bool failed = false; + + TestEventListener* repeater = listeners()->repeater(); + + start_timestamp_ = GetTimeInMillis(); + repeater->OnTestProgramStart(*parent_); + + // How many times to repeat the tests? We don't want to repeat them + // when we are inside the subprocess of a death test. + const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG_GET(repeat); + + // Repeats forever if the repeat count is negative. + const bool gtest_repeat_forever = repeat < 0; + + // Should test environments be set up and torn down for each repeat, or only + // set up on the first and torn down on the last iteration? If there is no + // "last" iteration because the tests will repeat forever, always recreate the + // environments to avoid leaks in case one of the environments is using + // resources that are external to this process. Without this check there would + // be no way to clean up those external resources automatically. + const bool recreate_environments_when_repeating = + GTEST_FLAG_GET(recreate_environments_when_repeating) || + gtest_repeat_forever; + + for (int i = 0; gtest_repeat_forever || i != repeat; i++) { + // We want to preserve failures generated by ad-hoc test + // assertions executed before RUN_ALL_TESTS(). + ClearNonAdHocTestResult(); + + Timer timer; + + // Shuffles test suites and tests if requested. + if (has_tests_to_run && GTEST_FLAG_GET(shuffle)) { + random()->Reseed(static_cast(random_seed_)); + // This should be done before calling OnTestIterationStart(), + // such that a test event listener can see the actual test order + // in the event. + ShuffleTests(); + } + + // Tells the unit test event listeners that the tests are about to start. + repeater->OnTestIterationStart(*parent_, i); + + // Runs each test suite if there is at least one test to run. + if (has_tests_to_run) { + // Sets up all environments beforehand. If test environments aren't + // recreated for each iteration, only do so on the first iteration. + if (i == 0 || recreate_environments_when_repeating) { + repeater->OnEnvironmentsSetUpStart(*parent_); + ForEach(environments_, SetUpEnvironment); + repeater->OnEnvironmentsSetUpEnd(*parent_); + } + + // Runs the tests only if there was no fatal failure or skip triggered + // during global set-up. + if (Test::IsSkipped()) { + // Emit diagnostics when global set-up calls skip, as it will not be + // emitted by default. + TestResult& test_result = + *internal::GetUnitTestImpl()->current_test_result(); + for (int j = 0; j < test_result.total_part_count(); ++j) { + const TestPartResult& test_part_result = + test_result.GetTestPartResult(j); + if (test_part_result.type() == TestPartResult::kSkip) { + const std::string& result = test_part_result.message(); + printf("%s\n", result.c_str()); + } + } + fflush(stdout); + } else if (!Test::HasFatalFailure()) { + for (int test_index = 0; test_index < total_test_suite_count(); + test_index++) { + GetMutableSuiteCase(test_index)->Run(); + if (GTEST_FLAG_GET(fail_fast) && + GetMutableSuiteCase(test_index)->Failed()) { + for (int j = test_index + 1; j < total_test_suite_count(); j++) { + GetMutableSuiteCase(j)->Skip(); + } + break; + } + } + } else if (Test::HasFatalFailure()) { + // If there was a fatal failure during the global setup then we know we + // aren't going to run any tests. Explicitly mark all of the tests as + // skipped to make this obvious in the output. + for (int test_index = 0; test_index < total_test_suite_count(); + test_index++) { + GetMutableSuiteCase(test_index)->Skip(); + } + } + + // Tears down all environments in reverse order afterwards. If test + // environments aren't recreated for each iteration, only do so on the + // last iteration. + if (i == repeat - 1 || recreate_environments_when_repeating) { + repeater->OnEnvironmentsTearDownStart(*parent_); + std::for_each(environments_.rbegin(), environments_.rend(), + TearDownEnvironment); + repeater->OnEnvironmentsTearDownEnd(*parent_); + } + } + + elapsed_time_ = timer.Elapsed(); + + // Tells the unit test event listener that the tests have just finished. + repeater->OnTestIterationEnd(*parent_, i); + + // Gets the result and clears it. + if (!Passed()) { + failed = true; + } + + // Restores the original test order after the iteration. This + // allows the user to quickly repro a failure that happens in the + // N-th iteration without repeating the first (N - 1) iterations. + // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in + // case the user somehow changes the value of the flag somewhere + // (it's always safe to unshuffle the tests). + UnshuffleTests(); + + if (GTEST_FLAG_GET(shuffle)) { + // Picks a new random seed for each iteration. + random_seed_ = GetNextRandomSeed(random_seed_); + } + } + + repeater->OnTestProgramEnd(*parent_); + + if (!gtest_is_initialized_before_run_all_tests) { + ColoredPrintf( + GTestColor::kRed, + "\nIMPORTANT NOTICE - DO NOT IGNORE:\n" + "This test program did NOT call " GTEST_INIT_GOOGLE_TEST_NAME_ + "() before calling RUN_ALL_TESTS(). This is INVALID. Soon " GTEST_NAME_ + " will start to enforce the valid usage. " + "Please fix it ASAP, or IT WILL START TO FAIL.\n"); // NOLINT +#if GTEST_FOR_GOOGLE_ + ColoredPrintf(GTestColor::kRed, + "For more details, see http://wiki/Main/ValidGUnitMain.\n"); +#endif // GTEST_FOR_GOOGLE_ + } + + return !failed; +} + +// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file +// if the variable is present. If a file already exists at this location, this +// function will write over it. If the variable is present, but the file cannot +// be created, prints an error and exits. +void WriteToShardStatusFileIfNeeded() { + const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile); + if (test_shard_file != nullptr) { + FILE* const file = posix::FOpen(test_shard_file, "w"); + if (file == nullptr) { + ColoredPrintf(GTestColor::kRed, + "Could not write to the test shard status file \"%s\" " + "specified by the %s environment variable.\n", + test_shard_file, kTestShardStatusFile); + fflush(stdout); + exit(EXIT_FAILURE); + } + fclose(file); + } +} + +// Checks whether sharding is enabled by examining the relevant +// environment variable values. If the variables are present, +// but inconsistent (i.e., shard_index >= total_shards), prints +// an error and exits. If in_subprocess_for_death_test, sharding is +// disabled because it must only be applied to the original test +// process. Otherwise, we could filter out death tests we intended to execute. +bool ShouldShard(const char* total_shards_env, const char* shard_index_env, + bool in_subprocess_for_death_test) { + if (in_subprocess_for_death_test) { + return false; + } + + const int32_t total_shards = Int32FromEnvOrDie(total_shards_env, -1); + const int32_t shard_index = Int32FromEnvOrDie(shard_index_env, -1); + + if (total_shards == -1 && shard_index == -1) { + return false; + } else if (total_shards == -1 && shard_index != -1) { + const Message msg = Message() << "Invalid environment variables: you have " + << kTestShardIndex << " = " << shard_index + << ", but have left " << kTestTotalShards + << " unset.\n"; + ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); + fflush(stdout); + exit(EXIT_FAILURE); + } else if (total_shards != -1 && shard_index == -1) { + const Message msg = Message() + << "Invalid environment variables: you have " + << kTestTotalShards << " = " << total_shards + << ", but have left " << kTestShardIndex << " unset.\n"; + ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); + fflush(stdout); + exit(EXIT_FAILURE); + } else if (shard_index < 0 || shard_index >= total_shards) { + const Message msg = + Message() << "Invalid environment variables: we require 0 <= " + << kTestShardIndex << " < " << kTestTotalShards + << ", but you have " << kTestShardIndex << "=" << shard_index + << ", " << kTestTotalShards << "=" << total_shards << ".\n"; + ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); + fflush(stdout); + exit(EXIT_FAILURE); + } + + return total_shards > 1; +} + +// Parses the environment variable var as an Int32. If it is unset, +// returns default_val. If it is not an Int32, prints an error +// and aborts. +int32_t Int32FromEnvOrDie(const char* var, int32_t default_val) { + const char* str_val = posix::GetEnv(var); + if (str_val == nullptr) { + return default_val; + } + + int32_t result; + if (!ParseInt32(Message() << "The value of environment variable " << var, + str_val, &result)) { + exit(EXIT_FAILURE); + } + return result; +} + +// Given the total number of shards, the shard index, and the test id, +// returns true if and only if the test should be run on this shard. The test id +// is some arbitrary but unique non-negative integer assigned to each test +// method. Assumes that 0 <= shard_index < total_shards. +bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) { + return (test_id % total_shards) == shard_index; +} + +// Compares the name of each test with the user-specified filter to +// decide whether the test should be run, then records the result in +// each TestSuite and TestInfo object. +// If shard_tests == true, further filters tests based on sharding +// variables in the environment - see +// https://github.com/google/googletest/blob/master/googletest/docs/advanced.md +// . Returns the number of tests that should run. +int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { + const int32_t total_shards = shard_tests == HONOR_SHARDING_PROTOCOL + ? Int32FromEnvOrDie(kTestTotalShards, -1) + : -1; + const int32_t shard_index = shard_tests == HONOR_SHARDING_PROTOCOL + ? Int32FromEnvOrDie(kTestShardIndex, -1) + : -1; + + const PositiveAndNegativeUnitTestFilter gtest_flag_filter( + GTEST_FLAG_GET(filter)); + const UnitTestFilter disable_test_filter(kDisableTestFilter); + // num_runnable_tests are the number of tests that will + // run across all shards (i.e., match filter and are not disabled). + // num_selected_tests are the number of tests to be run on + // this shard. + int num_runnable_tests = 0; + int num_selected_tests = 0; + for (auto* test_suite : test_suites_) { + const std::string& test_suite_name = test_suite->name(); + test_suite->set_should_run(false); + + for (size_t j = 0; j < test_suite->test_info_list().size(); j++) { + TestInfo* const test_info = test_suite->test_info_list()[j]; + const std::string test_name(test_info->name()); + // A test is disabled if test suite name or test name matches + // kDisableTestFilter. + const bool is_disabled = + disable_test_filter.MatchesName(test_suite_name) || + disable_test_filter.MatchesName(test_name); + test_info->is_disabled_ = is_disabled; + + const bool matches_filter = + gtest_flag_filter.MatchesTest(test_suite_name, test_name); + test_info->matches_filter_ = matches_filter; + + const bool is_runnable = + (GTEST_FLAG_GET(also_run_disabled_tests) || !is_disabled) && + matches_filter; + + const bool is_in_another_shard = + shard_tests != IGNORE_SHARDING_PROTOCOL && + !ShouldRunTestOnShard(total_shards, shard_index, num_runnable_tests); + test_info->is_in_another_shard_ = is_in_another_shard; + const bool is_selected = is_runnable && !is_in_another_shard; + + num_runnable_tests += is_runnable; + num_selected_tests += is_selected; + + test_info->should_run_ = is_selected; + test_suite->set_should_run(test_suite->should_run() || is_selected); + } + } + return num_selected_tests; +} + +// Prints the given C-string on a single line by replacing all '\n' +// characters with string "\\n". If the output takes more than +// max_length characters, only prints the first max_length characters +// and "...". +static void PrintOnOneLine(const char* str, int max_length) { + if (str != nullptr) { + for (int i = 0; *str != '\0'; ++str) { + if (i >= max_length) { + printf("..."); + break; + } + if (*str == '\n') { + printf("\\n"); + i += 2; + } else { + printf("%c", *str); + ++i; + } + } + } +} + +// Prints the names of the tests matching the user-specified filter flag. +void UnitTestImpl::ListTestsMatchingFilter() { + // Print at most this many characters for each type/value parameter. + const int kMaxParamLength = 250; + + for (auto* test_suite : test_suites_) { + bool printed_test_suite_name = false; + + for (size_t j = 0; j < test_suite->test_info_list().size(); j++) { + const TestInfo* const test_info = test_suite->test_info_list()[j]; + if (test_info->matches_filter_) { + if (!printed_test_suite_name) { + printed_test_suite_name = true; + printf("%s.", test_suite->name()); + if (test_suite->type_param() != nullptr) { + printf(" # %s = ", kTypeParamLabel); + // We print the type parameter on a single line to make + // the output easy to parse by a program. + PrintOnOneLine(test_suite->type_param(), kMaxParamLength); + } + printf("\n"); + } + printf(" %s", test_info->name()); + if (test_info->value_param() != nullptr) { + printf(" # %s = ", kValueParamLabel); + // We print the value parameter on a single line to make the + // output easy to parse by a program. + PrintOnOneLine(test_info->value_param(), kMaxParamLength); + } + printf("\n"); + } + } + } + fflush(stdout); + const std::string& output_format = UnitTestOptions::GetOutputFormat(); + if (output_format == "xml" || output_format == "json") { + FILE* fileout = OpenFileForWriting( + UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); + std::stringstream stream; + if (output_format == "xml") { + XmlUnitTestResultPrinter( + UnitTestOptions::GetAbsolutePathToOutputFile().c_str()) + .PrintXmlTestsList(&stream, test_suites_); + } else if (output_format == "json") { + JsonUnitTestResultPrinter( + UnitTestOptions::GetAbsolutePathToOutputFile().c_str()) + .PrintJsonTestList(&stream, test_suites_); + } + fprintf(fileout, "%s", StringStreamToString(&stream).c_str()); + fclose(fileout); + } +} + +// Sets the OS stack trace getter. +// +// Does nothing if the input and the current OS stack trace getter are +// the same; otherwise, deletes the old getter and makes the input the +// current getter. +void UnitTestImpl::set_os_stack_trace_getter( + OsStackTraceGetterInterface* getter) { + if (os_stack_trace_getter_ != getter) { + delete os_stack_trace_getter_; + os_stack_trace_getter_ = getter; + } +} + +// Returns the current OS stack trace getter if it is not NULL; +// otherwise, creates an OsStackTraceGetter, makes it the current +// getter, and returns it. +OsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() { + if (os_stack_trace_getter_ == nullptr) { +#ifdef GTEST_OS_STACK_TRACE_GETTER_ + os_stack_trace_getter_ = new GTEST_OS_STACK_TRACE_GETTER_; +#else + os_stack_trace_getter_ = new OsStackTraceGetter; +#endif // GTEST_OS_STACK_TRACE_GETTER_ + } + + return os_stack_trace_getter_; +} + +// Returns the most specific TestResult currently running. +TestResult* UnitTestImpl::current_test_result() { + if (current_test_info_ != nullptr) { + return ¤t_test_info_->result_; + } + if (current_test_suite_ != nullptr) { + return ¤t_test_suite_->ad_hoc_test_result_; + } + return &ad_hoc_test_result_; +} + +// Shuffles all test suites, and the tests within each test suite, +// making sure that death tests are still run first. +void UnitTestImpl::ShuffleTests() { + // Shuffles the death test suites. + ShuffleRange(random(), 0, last_death_test_suite_ + 1, &test_suite_indices_); + + // Shuffles the non-death test suites. + ShuffleRange(random(), last_death_test_suite_ + 1, + static_cast(test_suites_.size()), &test_suite_indices_); + + // Shuffles the tests inside each test suite. + for (auto& test_suite : test_suites_) { + test_suite->ShuffleTests(random()); + } +} + +// Restores the test suites and tests to their order before the first shuffle. +void UnitTestImpl::UnshuffleTests() { + for (size_t i = 0; i < test_suites_.size(); i++) { + // Unshuffles the tests in each test suite. + test_suites_[i]->UnshuffleTests(); + // Resets the index of each test suite. + test_suite_indices_[i] = static_cast(i); + } +} + +// Returns the current OS stack trace as an std::string. +// +// The maximum number of stack frames to be included is specified by +// the gtest_stack_trace_depth flag. The skip_count parameter +// specifies the number of top frames to be skipped, which doesn't +// count against the number of frames to be included. +// +// For example, if Foo() calls Bar(), which in turn calls +// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in +// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. +GTEST_NO_INLINE_ GTEST_NO_TAIL_CALL_ std::string +GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, int skip_count) { + // We pass skip_count + 1 to skip this wrapper function in addition + // to what the user really wants to skip. + return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1); +} + +// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to +// suppress unreachable code warnings. +namespace { +class ClassUniqueToAlwaysTrue {}; +} // namespace + +bool IsTrue(bool condition) { return condition; } + +bool AlwaysTrue() { +#if GTEST_HAS_EXCEPTIONS + // This condition is always false so AlwaysTrue() never actually throws, + // but it makes the compiler think that it may throw. + if (IsTrue(false)) throw ClassUniqueToAlwaysTrue(); +#endif // GTEST_HAS_EXCEPTIONS + return true; +} + +// If *pstr starts with the given prefix, modifies *pstr to be right +// past the prefix and returns true; otherwise leaves *pstr unchanged +// and returns false. None of pstr, *pstr, and prefix can be NULL. +bool SkipPrefix(const char* prefix, const char** pstr) { + const size_t prefix_len = strlen(prefix); + if (strncmp(*pstr, prefix, prefix_len) == 0) { + *pstr += prefix_len; + return true; + } + return false; +} + +// Parses a string as a command line flag. The string should have +// the format "--flag=value". When def_optional is true, the "=value" +// part can be omitted. +// +// Returns the value of the flag, or NULL if the parsing failed. +static const char* ParseFlagValue(const char* str, const char* flag_name, + bool def_optional) { + // str and flag must not be NULL. + if (str == nullptr || flag_name == nullptr) return nullptr; + + // The flag must start with "--" followed by GTEST_FLAG_PREFIX_. + const std::string flag_str = + std::string("--") + GTEST_FLAG_PREFIX_ + flag_name; + const size_t flag_len = flag_str.length(); + if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr; + + // Skips the flag name. + const char* flag_end = str + flag_len; + + // When def_optional is true, it's OK to not have a "=value" part. + if (def_optional && (flag_end[0] == '\0')) { + return flag_end; + } + + // If def_optional is true and there are more characters after the + // flag name, or if def_optional is false, there must be a '=' after + // the flag name. + if (flag_end[0] != '=') return nullptr; + + // Returns the string after "=". + return flag_end + 1; +} + +// Parses a string for a bool flag, in the form of either +// "--flag=value" or "--flag". +// +// In the former case, the value is taken as true as long as it does +// not start with '0', 'f', or 'F'. +// +// In the latter case, the value is taken as true. +// +// On success, stores the value of the flag in *value, and returns +// true. On failure, returns false without changing *value. +static bool ParseFlag(const char* str, const char* flag_name, bool* value) { + // Gets the value of the flag as a string. + const char* const value_str = ParseFlagValue(str, flag_name, true); + + // Aborts if the parsing failed. + if (value_str == nullptr) return false; + + // Converts the string value to a bool. + *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); + return true; +} + +// Parses a string for an int32_t flag, in the form of "--flag=value". +// +// On success, stores the value of the flag in *value, and returns +// true. On failure, returns false without changing *value. +bool ParseFlag(const char* str, const char* flag_name, int32_t* value) { + // Gets the value of the flag as a string. + const char* const value_str = ParseFlagValue(str, flag_name, false); + + // Aborts if the parsing failed. + if (value_str == nullptr) return false; + + // Sets *value to the value of the flag. + return ParseInt32(Message() << "The value of flag --" << flag_name, value_str, + value); +} + +// Parses a string for a string flag, in the form of "--flag=value". +// +// On success, stores the value of the flag in *value, and returns +// true. On failure, returns false without changing *value. +template +static bool ParseFlag(const char* str, const char* flag_name, String* value) { + // Gets the value of the flag as a string. + const char* const value_str = ParseFlagValue(str, flag_name, false); + + // Aborts if the parsing failed. + if (value_str == nullptr) return false; + + // Sets *value to the value of the flag. + *value = value_str; + return true; +} + +// Determines whether a string has a prefix that Google Test uses for its +// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_. +// If Google Test detects that a command line flag has its prefix but is not +// recognized, it will print its help message. Flags starting with +// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test +// internal flags and do not trigger the help message. +static bool HasGoogleTestFlagPrefix(const char* str) { + return (SkipPrefix("--", &str) || SkipPrefix("-", &str) || + SkipPrefix("/", &str)) && + !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) && + (SkipPrefix(GTEST_FLAG_PREFIX_, &str) || + SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str)); +} + +// Prints a string containing code-encoded text. The following escape +// sequences can be used in the string to control the text color: +// +// @@ prints a single '@' character. +// @R changes the color to red. +// @G changes the color to green. +// @Y changes the color to yellow. +// @D changes to the default terminal text color. +// +static void PrintColorEncoded(const char* str) { + GTestColor color = GTestColor::kDefault; // The current color. + + // Conceptually, we split the string into segments divided by escape + // sequences. Then we print one segment at a time. At the end of + // each iteration, the str pointer advances to the beginning of the + // next segment. + for (;;) { + const char* p = strchr(str, '@'); + if (p == nullptr) { + ColoredPrintf(color, "%s", str); + return; + } + + ColoredPrintf(color, "%s", std::string(str, p).c_str()); + + const char ch = p[1]; + str = p + 2; + if (ch == '@') { + ColoredPrintf(color, "@"); + } else if (ch == 'D') { + color = GTestColor::kDefault; + } else if (ch == 'R') { + color = GTestColor::kRed; + } else if (ch == 'G') { + color = GTestColor::kGreen; + } else if (ch == 'Y') { + color = GTestColor::kYellow; + } else { + --str; + } + } +} + +static const char kColorEncodedHelpMessage[] = + "This program contains tests written using " GTEST_NAME_ + ". You can use the\n" + "following command line flags to control its behavior:\n" + "\n" + "Test Selection:\n" + " @G--" GTEST_FLAG_PREFIX_ + "list_tests@D\n" + " List the names of all tests instead of running them. The name of\n" + " TEST(Foo, Bar) is \"Foo.Bar\".\n" + " @G--" GTEST_FLAG_PREFIX_ + "filter=@YPOSITIVE_PATTERNS" + "[@G-@YNEGATIVE_PATTERNS]@D\n" + " Run only the tests whose name matches one of the positive patterns " + "but\n" + " none of the negative patterns. '?' matches any single character; " + "'*'\n" + " matches any substring; ':' separates two patterns.\n" + " @G--" GTEST_FLAG_PREFIX_ + "also_run_disabled_tests@D\n" + " Run all disabled tests too.\n" + "\n" + "Test Execution:\n" + " @G--" GTEST_FLAG_PREFIX_ + "repeat=@Y[COUNT]@D\n" + " Run the tests repeatedly; use a negative count to repeat forever.\n" + " @G--" GTEST_FLAG_PREFIX_ + "shuffle@D\n" + " Randomize tests' orders on every iteration.\n" + " @G--" GTEST_FLAG_PREFIX_ + "random_seed=@Y[NUMBER]@D\n" + " Random number seed to use for shuffling test orders (between 1 and\n" + " 99999, or 0 to use a seed based on the current time).\n" + " @G--" GTEST_FLAG_PREFIX_ + "recreate_environments_when_repeating@D\n" + " Sets up and tears down the global test environment on each repeat\n" + " of the test.\n" + "\n" + "Test Output:\n" + " @G--" GTEST_FLAG_PREFIX_ + "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n" + " Enable/disable colored output. The default is @Gauto@D.\n" + " @G--" GTEST_FLAG_PREFIX_ + "brief=1@D\n" + " Only print test failures.\n" + " @G--" GTEST_FLAG_PREFIX_ + "print_time=0@D\n" + " Don't print the elapsed time of each test.\n" + " @G--" GTEST_FLAG_PREFIX_ + "output=@Y(@Gjson@Y|@Gxml@Y)[@G:@YDIRECTORY_PATH@G" GTEST_PATH_SEP_ + "@Y|@G:@YFILE_PATH]@D\n" + " Generate a JSON or XML report in the given directory or with the " + "given\n" + " file name. @YFILE_PATH@D defaults to @Gtest_detail.xml@D.\n" +#if GTEST_CAN_STREAM_RESULTS_ + " @G--" GTEST_FLAG_PREFIX_ + "stream_result_to=@YHOST@G:@YPORT@D\n" + " Stream test results to the given server.\n" +#endif // GTEST_CAN_STREAM_RESULTS_ + "\n" + "Assertion Behavior:\n" +#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS + " @G--" GTEST_FLAG_PREFIX_ + "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n" + " Set the default death test style.\n" +#endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS + " @G--" GTEST_FLAG_PREFIX_ + "break_on_failure@D\n" + " Turn assertion failures into debugger break-points.\n" + " @G--" GTEST_FLAG_PREFIX_ + "throw_on_failure@D\n" + " Turn assertion failures into C++ exceptions for use by an external\n" + " test framework.\n" + " @G--" GTEST_FLAG_PREFIX_ + "catch_exceptions=0@D\n" + " Do not report exceptions as test failures. Instead, allow them\n" + " to crash the program or throw a pop-up (on Windows).\n" + "\n" + "Except for @G--" GTEST_FLAG_PREFIX_ + "list_tests@D, you can alternatively set " + "the corresponding\n" + "environment variable of a flag (all letters in upper-case). For example, " + "to\n" + "disable colored text output, you can either specify " + "@G--" GTEST_FLAG_PREFIX_ + "color=no@D or set\n" + "the @G" GTEST_FLAG_PREFIX_UPPER_ + "COLOR@D environment variable to @Gno@D.\n" + "\n" + "For more information, please read the " GTEST_NAME_ + " documentation at\n" + "@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ + "\n" + "(not one in your own code or tests), please report it to\n" + "@G<" GTEST_DEV_EMAIL_ ">@D.\n"; + +static bool ParseGoogleTestFlag(const char* const arg) { +#define GTEST_INTERNAL_PARSE_FLAG(flag_name) \ + do { \ + auto value = GTEST_FLAG_GET(flag_name); \ + if (ParseFlag(arg, #flag_name, &value)) { \ + GTEST_FLAG_SET(flag_name, value); \ + return true; \ + } \ + } while (false) + + GTEST_INTERNAL_PARSE_FLAG(also_run_disabled_tests); + GTEST_INTERNAL_PARSE_FLAG(break_on_failure); + GTEST_INTERNAL_PARSE_FLAG(catch_exceptions); + GTEST_INTERNAL_PARSE_FLAG(color); + GTEST_INTERNAL_PARSE_FLAG(death_test_style); + GTEST_INTERNAL_PARSE_FLAG(death_test_use_fork); + GTEST_INTERNAL_PARSE_FLAG(fail_fast); + GTEST_INTERNAL_PARSE_FLAG(filter); + GTEST_INTERNAL_PARSE_FLAG(internal_run_death_test); + GTEST_INTERNAL_PARSE_FLAG(list_tests); + GTEST_INTERNAL_PARSE_FLAG(output); + GTEST_INTERNAL_PARSE_FLAG(brief); + GTEST_INTERNAL_PARSE_FLAG(print_time); + GTEST_INTERNAL_PARSE_FLAG(print_utf8); + GTEST_INTERNAL_PARSE_FLAG(random_seed); + GTEST_INTERNAL_PARSE_FLAG(repeat); + GTEST_INTERNAL_PARSE_FLAG(recreate_environments_when_repeating); + GTEST_INTERNAL_PARSE_FLAG(shuffle); + GTEST_INTERNAL_PARSE_FLAG(stack_trace_depth); + GTEST_INTERNAL_PARSE_FLAG(stream_result_to); + GTEST_INTERNAL_PARSE_FLAG(throw_on_failure); + return false; +} + +#if GTEST_USE_OWN_FLAGFILE_FLAG_ +static void LoadFlagsFromFile(const std::string& path) { + FILE* flagfile = posix::FOpen(path.c_str(), "r"); + if (!flagfile) { + GTEST_LOG_(FATAL) << "Unable to open file \"" << GTEST_FLAG_GET(flagfile) + << "\""; + } + std::string contents(ReadEntireFile(flagfile)); + posix::FClose(flagfile); + std::vector lines; + SplitString(contents, '\n', &lines); + for (size_t i = 0; i < lines.size(); ++i) { + if (lines[i].empty()) continue; + if (!ParseGoogleTestFlag(lines[i].c_str())) g_help_flag = true; + } +} +#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ + +// Parses the command line for Google Test flags, without initializing +// other parts of Google Test. The type parameter CharType can be +// instantiated to either char or wchar_t. +template +void ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { + std::string flagfile_value; + for (int i = 1; i < *argc; i++) { + const std::string arg_string = StreamableToString(argv[i]); + const char* const arg = arg_string.c_str(); + + using internal::ParseFlag; + + bool remove_flag = false; + if (ParseGoogleTestFlag(arg)) { + remove_flag = true; +#if GTEST_USE_OWN_FLAGFILE_FLAG_ + } else if (ParseFlag(arg, "flagfile", &flagfile_value)) { + GTEST_FLAG_SET(flagfile, flagfile_value); + LoadFlagsFromFile(flagfile_value); + remove_flag = true; +#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ + } else if (arg_string == "--help" || HasGoogleTestFlagPrefix(arg)) { + // Both help flag and unrecognized Google Test flags (excluding + // internal ones) trigger help display. + g_help_flag = true; + } + + if (remove_flag) { + // Shift the remainder of the argv list left by one. Note + // that argv has (*argc + 1) elements, the last one always being + // NULL. The following loop moves the trailing NULL element as + // well. + for (int j = i; j != *argc; j++) { + argv[j] = argv[j + 1]; + } + + // Decrements the argument count. + (*argc)--; + + // We also need to decrement the iterator as we just removed + // an element. + i--; + } + } + + if (g_help_flag) { + // We print the help here instead of in RUN_ALL_TESTS(), as the + // latter may not be called at all if the user is using Google + // Test with another testing framework. + PrintColorEncoded(kColorEncodedHelpMessage); + } +} + +// Parses the command line for Google Test flags, without initializing +// other parts of Google Test. +void ParseGoogleTestFlagsOnly(int* argc, char** argv) { +#if GTEST_HAS_ABSL + if (*argc > 0) { + // absl::ParseCommandLine() requires *argc > 0. + auto positional_args = absl::flags_internal::ParseCommandLineImpl( + *argc, argv, absl::flags_internal::ArgvListAction::kRemoveParsedArgs, + absl::flags_internal::UsageFlagsAction::kHandleUsage, + absl::flags_internal::OnUndefinedFlag::kReportUndefined); + // Any command-line positional arguments not part of any command-line flag + // (or arguments to a flag) are copied back out to argv, with the program + // invocation name at position 0, and argc is resized. This includes + // positional arguments after the flag-terminating delimiter '--'. + // See https://abseil.io/docs/cpp/guides/flags. + std::copy(positional_args.begin(), positional_args.end(), argv); + if (static_cast(positional_args.size()) < *argc) { + argv[positional_args.size()] = nullptr; + *argc = static_cast(positional_args.size()); + } + } +#else + ParseGoogleTestFlagsOnlyImpl(argc, argv); +#endif + + // Fix the value of *_NSGetArgc() on macOS, but if and only if + // *_NSGetArgv() == argv + // Only applicable to char** version of argv +#if GTEST_OS_MAC +#ifndef GTEST_OS_IOS + if (*_NSGetArgv() == argv) { + *_NSGetArgc() = *argc; + } +#endif +#endif +} +void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) { + ParseGoogleTestFlagsOnlyImpl(argc, argv); +} + +// The internal implementation of InitGoogleTest(). +// +// The type parameter CharType can be instantiated to either char or +// wchar_t. +template +void InitGoogleTestImpl(int* argc, CharType** argv) { + // We don't want to run the initialization code twice. + if (GTestIsInitialized()) return; + + if (*argc <= 0) return; + + g_argvs.clear(); + for (int i = 0; i != *argc; i++) { + g_argvs.push_back(StreamableToString(argv[i])); + } + +#if GTEST_HAS_ABSL + absl::InitializeSymbolizer(g_argvs[0].c_str()); + + // When using the Abseil Flags library, set the program usage message to the + // help message, but remove the color-encoding from the message first. + absl::SetProgramUsageMessage(absl::StrReplaceAll( + kColorEncodedHelpMessage, + {{"@D", ""}, {"@R", ""}, {"@G", ""}, {"@Y", ""}, {"@@", "@"}})); +#endif // GTEST_HAS_ABSL + + ParseGoogleTestFlagsOnly(argc, argv); + GetUnitTestImpl()->PostFlagParsingInit(); +} + +} // namespace internal + +// Initializes Google Test. This must be called before calling +// RUN_ALL_TESTS(). In particular, it parses a command line for the +// flags that Google Test recognizes. Whenever a Google Test flag is +// seen, it is removed from argv, and *argc is decremented. +// +// No value is returned. Instead, the Google Test flag variables are +// updated. +// +// Calling the function for the second time has no user-visible effect. +void InitGoogleTest(int* argc, char** argv) { +#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) + GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv); +#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) + internal::InitGoogleTestImpl(argc, argv); +#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) +} + +// This overloaded version can be used in Windows programs compiled in +// UNICODE mode. +void InitGoogleTest(int* argc, wchar_t** argv) { +#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) + GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv); +#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) + internal::InitGoogleTestImpl(argc, argv); +#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) +} + +// This overloaded version can be used on Arduino/embedded platforms where +// there is no argc/argv. +void InitGoogleTest() { + // Since Arduino doesn't have a command line, fake out the argc/argv arguments + int argc = 1; + const auto arg0 = "dummy"; + char* argv0 = const_cast(arg0); + char** argv = &argv0; + +#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) + GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(&argc, argv); +#else // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) + internal::InitGoogleTestImpl(&argc, argv); +#endif // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) +} + +#if !defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) +// Return value of first environment variable that is set and contains +// a non-empty string. If there are none, return the "fallback" string. +// Since we like the temporary directory to have a directory separator suffix, +// add it if not provided in the environment variable value. +static std::string GetTempDirFromEnv( + std::initializer_list environment_variables, + const char* fallback, char separator) { + for (const char* variable_name : environment_variables) { + const char* value = internal::posix::GetEnv(variable_name); + if (value != nullptr && value[0] != '\0') { + if (value[strlen(value) - 1] != separator) { + return std::string(value).append(1, separator); + } + return value; + } + } + return fallback; +} +#endif + +std::string TempDir() { +#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_) + return GTEST_CUSTOM_TEMPDIR_FUNCTION_(); +#elif GTEST_OS_WINDOWS || GTEST_OS_WINDOWS_MOBILE + return GetTempDirFromEnv({"TEST_TMPDIR", "TEMP"}, "\\temp\\", '\\'); +#elif GTEST_OS_LINUX_ANDROID + return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/data/local/tmp/", '/'); +#else + return GetTempDirFromEnv({"TEST_TMPDIR", "TMPDIR"}, "/tmp/", '/'); +#endif +} + +// Class ScopedTrace + +// Pushes the given source file location and message onto a per-thread +// trace stack maintained by Google Test. +void ScopedTrace::PushTrace(const char* file, int line, std::string message) { + internal::TraceInfo trace; + trace.file = file; + trace.line = line; + trace.message.swap(message); + + UnitTest::GetInstance()->PushGTestTrace(trace); +} + +// Pops the info pushed by the c'tor. +ScopedTrace::~ScopedTrace() GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) { + UnitTest::GetInstance()->PopGTestTrace(); +} + +} // namespace testing diff --git a/test/gtest/gtest_main.cc b/test/gtest/src/gtest_main.cc similarity index 97% rename from test/gtest/gtest_main.cc rename to test/gtest/src/gtest_main.cc index 46b27c3d7d..44976375c9 100644 --- a/test/gtest/gtest_main.cc +++ b/test/gtest/src/gtest_main.cc @@ -28,15 +28,14 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include + #include "gtest/gtest.h" #if GTEST_OS_ESP8266 || GTEST_OS_ESP32 #if GTEST_OS_ESP8266 extern "C" { #endif -void setup() { - testing::InitGoogleTest(); -} +void setup() { testing::InitGoogleTest(); } void loop() { RUN_ALL_TESTS(); } From 2eaa2ea64f9fb12773306534d461d9ed63cb76b6 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 13 Mar 2025 17:44:43 +0100 Subject: [PATCH 0407/1222] Make random_string() thread-safe (#2110) By making the random engine thread_local, each thread now has its own independent random sequence, ensuring safe concurrent access. Additionally, using an immediately invoked lambda expression to initialize the engine eliminates the need for separate static seed variables. --- httplib.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index 0949e1d2f2..3f2bdaccfd 100644 --- a/httplib.h +++ b/httplib.h @@ -5100,16 +5100,15 @@ inline std::string random_string(size_t length) { constexpr const char data[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - // std::random_device might actually be deterministic on some - // platforms, but due to lack of support in the c++ standard library, - // doing better requires either some ugly hacks or breaking portability. - static std::random_device seed_gen; - - // Request 128 bits of entropy for initialization - static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), - seed_gen()}; - - static std::mt19937 engine(seed_sequence); + static thread_local std::mt19937 engine([]() { + // std::random_device might actually be deterministic on some + // platforms, but due to lack of support in the c++ standard library, + // doing better requires either some ugly hacks or breaking portability. + std::random_device seed_gen; + // Request 128 bits of entropy for initialization + std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; + return std::mt19937(seed_sequence); + }()); std::string result; for (size_t i = 0; i < length; i++) { From 0bda3a7d1a797f2b67953504fefa7391a817a0bd Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 13 Mar 2025 21:54:05 -0400 Subject: [PATCH 0408/1222] Update benchmark --- benchmark/Makefile | 41 +- .../httplib.h | 0 .../main.cpp | 0 benchmark/cpp-httplib-v19/httplib.h | 10475 ++++++++++++++++ benchmark/cpp-httplib-v19/main.cpp | 12 + 5 files changed, 10515 insertions(+), 13 deletions(-) rename benchmark/{cpp-httplib-base => cpp-httplib-v18}/httplib.h (100%) rename benchmark/{cpp-httplib-base => cpp-httplib-v18}/main.cpp (100%) create mode 100644 benchmark/cpp-httplib-v19/httplib.h create mode 100644 benchmark/cpp-httplib-v19/main.cpp diff --git a/benchmark/Makefile b/benchmark/Makefile index 5107664c03..fa4f76c90b 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -18,22 +18,37 @@ run : server @./server server : cpp-httplib/main.cpp ../httplib.h - g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp + @g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp -# cpp-httplib -bench-base: server-base +# cpp-httplib v0.19.0 +bench-v19: server-v19 + @echo "---------------------\n cpp-httplib v0.19.0\n---------------------\n" + @./server-v19 & export PID=$$!; $(BENCH); kill $${PID} + @echo "" + +monitor-v19: server-v19 + @./server-v19 & export PID=$$!; $(MONITOR); kill $${PID} + +run-v19 : server-v19 + @./server-v19 + +server-v19 : cpp-httplib-v19/main.cpp cpp-httplib-v19/httplib.h + @g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib-v19/main.cpp + +# cpp-httplib v0.18.0 +bench-v18: server-v18 @echo "---------------------\n cpp-httplib v0.18.0\n---------------------\n" - @./server-base & export PID=$$!; $(BENCH); kill $${PID} + @./server-v18 & export PID=$$!; $(BENCH); kill $${PID} @echo "" -monitor-base: server-base - @./server-base & export PID=$$!; $(MONITOR); kill $${PID} +monitor-v18: server-v18 + @./server-v18 & export PID=$$!; $(MONITOR); kill $${PID} -run-base : server-base - @./server-base +run-v18 : server-v18 + @./server-v18 -server-base : cpp-httplib-base/main.cpp cpp-httplib-base/httplib.h - g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib-base/main.cpp +server-v18 : cpp-httplib-v18/main.cpp cpp-httplib-v18/httplib.h + @g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib-v18/main.cpp # crow bench-crow: server-crow @@ -48,12 +63,12 @@ run-crow : server-crow @./server-crow server-crow : crow/main.cpp - g++ -o $@ $(CXXFLAGS) crow/main.cpp + @g++ -o $@ $(CXXFLAGS) crow/main.cpp # misc -build: server server-base server-crow +build: server server-v18 server-v19 server-crow -bench-all: bench-crow bench bench-base +bench-all: bench-crow bench bench-v19 bench-v18 issue: bombardier -c 10 -d 30s localhost:8080 diff --git a/benchmark/cpp-httplib-base/httplib.h b/benchmark/cpp-httplib-v18/httplib.h similarity index 100% rename from benchmark/cpp-httplib-base/httplib.h rename to benchmark/cpp-httplib-v18/httplib.h diff --git a/benchmark/cpp-httplib-base/main.cpp b/benchmark/cpp-httplib-v18/main.cpp similarity index 100% rename from benchmark/cpp-httplib-base/main.cpp rename to benchmark/cpp-httplib-v18/main.cpp diff --git a/benchmark/cpp-httplib-v19/httplib.h b/benchmark/cpp-httplib-v19/httplib.h new file mode 100644 index 0000000000..e4799dab79 --- /dev/null +++ b/benchmark/cpp-httplib-v19/httplib.h @@ -0,0 +1,10475 @@ +// +// httplib.h +// +// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// MIT License +// + +#ifndef CPPHTTPLIB_HTTPLIB_H +#define CPPHTTPLIB_HTTPLIB_H + +#define CPPHTTPLIB_VERSION "0.19.0" + +/* + * Configuration + */ + +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND +#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND 10000 +#endif + +#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT +#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND +#define CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND +#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND +#ifdef _WIN32 +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 +#else +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 +#endif +#endif + +#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH +#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_HEADER_MAX_LENGTH +#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT +#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 +#endif + +#ifndef CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT +#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT 1024 +#endif + +#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) +#endif + +#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH +#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 +#endif + +#ifndef CPPHTTPLIB_RANGE_MAX_COUNT +#define CPPHTTPLIB_RANGE_MAX_COUNT 1024 +#endif + +#ifndef CPPHTTPLIB_TCP_NODELAY +#define CPPHTTPLIB_TCP_NODELAY false +#endif + +#ifndef CPPHTTPLIB_IPV6_V6ONLY +#define CPPHTTPLIB_IPV6_V6ONLY false +#endif + +#ifndef CPPHTTPLIB_RECV_BUFSIZ +#define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ +#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) +#endif + +#ifndef CPPHTTPLIB_THREAD_POOL_COUNT +#define CPPHTTPLIB_THREAD_POOL_COUNT \ + ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ + ? std::thread::hardware_concurrency() - 1 \ + : 0)) +#endif + +#ifndef CPPHTTPLIB_RECV_FLAGS +#define CPPHTTPLIB_RECV_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_SEND_FLAGS +#define CPPHTTPLIB_SEND_FLAGS 0 +#endif + +#ifndef CPPHTTPLIB_LISTEN_BACKLOG +#define CPPHTTPLIB_LISTEN_BACKLOG 5 +#endif + +/* + * Headers + */ + +#ifdef _WIN32 +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif //_CRT_SECURE_NO_WARNINGS + +#ifndef _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_NONSTDC_NO_DEPRECATE +#endif //_CRT_NONSTDC_NO_DEPRECATE + +#if defined(_MSC_VER) +#if _MSC_VER < 1900 +#error Sorry, Visual Studio versions prior to 2015 are not supported +#endif + +#pragma comment(lib, "ws2_32.lib") + +#ifdef _WIN64 +using ssize_t = __int64; +#else +using ssize_t = long; +#endif +#endif // _MSC_VER + +#ifndef S_ISREG +#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) +#endif // S_ISREG + +#ifndef S_ISDIR +#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) +#endif // S_ISDIR + +#ifndef NOMINMAX +#define NOMINMAX +#endif // NOMINMAX + +#include +#include +#include + +#ifndef WSA_FLAG_NO_HANDLE_INHERIT +#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 +#endif + +using socket_t = SOCKET; +#ifdef CPPHTTPLIB_USE_POLL +#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) +#endif + +#else // not _WIN32 + +#include +#if !defined(_AIX) && !defined(__MVS__) +#include +#endif +#ifdef __MVS__ +#include +#ifndef NI_MAXHOST +#define NI_MAXHOST 1025 +#endif +#endif +#include +#include +#include +#ifdef __linux__ +#include +#endif +#include +#ifdef CPPHTTPLIB_USE_POLL +#include +#endif +#include +#include +#include +#ifndef __VMS +#include +#endif +#include +#include +#include + +using socket_t = int; +#ifndef INVALID_SOCKET +#define INVALID_SOCKET (-1) +#endif +#endif //_WIN32 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef _WIN32 +#include + +// these are defined in wincrypt.h and it breaks compilation if BoringSSL is +// used +#undef X509_NAME +#undef X509_CERT_PAIR +#undef X509_EXTENSIONS +#undef PKCS7_SIGNER_INFO + +#ifdef _MSC_VER +#pragma comment(lib, "crypt32.lib") +#endif +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#include +#if TARGET_OS_OSX +#include +#include +#endif // TARGET_OS_OSX +#endif // _WIN32 + +#include +#include +#include +#include + +#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) +#include +#endif + +#include +#include + +#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) +#if OPENSSL_VERSION_NUMBER < 0x1010107f +#error Please use OpenSSL or a current version of BoringSSL +#endif +#define SSL_get1_peer_certificate SSL_get_peer_certificate +#elif OPENSSL_VERSION_NUMBER < 0x30000000L +#error Sorry, OpenSSL versions prior to 3.0.0 are not supported +#endif + +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +#include +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +#include +#include +#endif + +/* + * Declaration + */ +namespace httplib { + +namespace detail { + +/* + * Backport std::make_unique from C++14. + * + * NOTE: This code came up with the following stackoverflow post: + * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique + * + */ + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(Args &&...args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +typename std::enable_if::value, std::unique_ptr>::type +make_unique(std::size_t n) { + typedef typename std::remove_extent::type RT; + return std::unique_ptr(new RT[n]); +} + +namespace case_ignore { + +inline unsigned char to_lower(int c) { + const static unsigned char table[256] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, + 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, + 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, + 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, + 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, + 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, + 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226, + 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, + 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224, + 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, + 255, + }; + return table[(unsigned char)(char)c]; +} + +inline bool equal(const std::string &a, const std::string &b) { + return a.size() == b.size() && + std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) { + return to_lower(ca) == to_lower(cb); + }); +} + +struct equal_to { + bool operator()(const std::string &a, const std::string &b) const { + return equal(a, b); + } +}; + +struct hash { + size_t operator()(const std::string &key) const { + return hash_core(key.data(), key.size(), 0); + } + + size_t hash_core(const char *s, size_t l, size_t h) const { + return (l == 0) ? h + : hash_core(s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no + // overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(to_lower(*s))); + } +}; + +} // namespace case_ignore + +// This is based on +// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". + +struct scope_exit { + explicit scope_exit(std::function &&f) + : exit_function(std::move(f)), execute_on_destruction{true} {} + + scope_exit(scope_exit &&rhs) noexcept + : exit_function(std::move(rhs.exit_function)), + execute_on_destruction{rhs.execute_on_destruction} { + rhs.release(); + } + + ~scope_exit() { + if (execute_on_destruction) { this->exit_function(); } + } + + void release() { this->execute_on_destruction = false; } + +private: + scope_exit(const scope_exit &) = delete; + void operator=(const scope_exit &) = delete; + scope_exit &operator=(scope_exit &&) = delete; + + std::function exit_function; + bool execute_on_destruction; +}; + +} // namespace detail + +enum StatusCode { + // Information responses + Continue_100 = 100, + SwitchingProtocol_101 = 101, + Processing_102 = 102, + EarlyHints_103 = 103, + + // Successful responses + OK_200 = 200, + Created_201 = 201, + Accepted_202 = 202, + NonAuthoritativeInformation_203 = 203, + NoContent_204 = 204, + ResetContent_205 = 205, + PartialContent_206 = 206, + MultiStatus_207 = 207, + AlreadyReported_208 = 208, + IMUsed_226 = 226, + + // Redirection messages + MultipleChoices_300 = 300, + MovedPermanently_301 = 301, + Found_302 = 302, + SeeOther_303 = 303, + NotModified_304 = 304, + UseProxy_305 = 305, + unused_306 = 306, + TemporaryRedirect_307 = 307, + PermanentRedirect_308 = 308, + + // Client error responses + BadRequest_400 = 400, + Unauthorized_401 = 401, + PaymentRequired_402 = 402, + Forbidden_403 = 403, + NotFound_404 = 404, + MethodNotAllowed_405 = 405, + NotAcceptable_406 = 406, + ProxyAuthenticationRequired_407 = 407, + RequestTimeout_408 = 408, + Conflict_409 = 409, + Gone_410 = 410, + LengthRequired_411 = 411, + PreconditionFailed_412 = 412, + PayloadTooLarge_413 = 413, + UriTooLong_414 = 414, + UnsupportedMediaType_415 = 415, + RangeNotSatisfiable_416 = 416, + ExpectationFailed_417 = 417, + ImATeapot_418 = 418, + MisdirectedRequest_421 = 421, + UnprocessableContent_422 = 422, + Locked_423 = 423, + FailedDependency_424 = 424, + TooEarly_425 = 425, + UpgradeRequired_426 = 426, + PreconditionRequired_428 = 428, + TooManyRequests_429 = 429, + RequestHeaderFieldsTooLarge_431 = 431, + UnavailableForLegalReasons_451 = 451, + + // Server error responses + InternalServerError_500 = 500, + NotImplemented_501 = 501, + BadGateway_502 = 502, + ServiceUnavailable_503 = 503, + GatewayTimeout_504 = 504, + HttpVersionNotSupported_505 = 505, + VariantAlsoNegotiates_506 = 506, + InsufficientStorage_507 = 507, + LoopDetected_508 = 508, + NotExtended_510 = 510, + NetworkAuthenticationRequired_511 = 511, +}; + +using Headers = + std::unordered_multimap; + +using Params = std::multimap; +using Match = std::smatch; + +using Progress = std::function; + +struct Response; +using ResponseHandler = std::function; + +struct MultipartFormData { + std::string name; + std::string content; + std::string filename; + std::string content_type; +}; +using MultipartFormDataItems = std::vector; +using MultipartFormDataMap = std::multimap; + +class DataSink { +public: + DataSink() : os(&sb_), sb_(*this) {} + + DataSink(const DataSink &) = delete; + DataSink &operator=(const DataSink &) = delete; + DataSink(DataSink &&) = delete; + DataSink &operator=(DataSink &&) = delete; + + std::function write; + std::function is_writable; + std::function done; + std::function done_with_trailer; + std::ostream os; + +private: + class data_sink_streambuf final : public std::streambuf { + public: + explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} + + protected: + std::streamsize xsputn(const char *s, std::streamsize n) override { + sink_.write(s, static_cast(n)); + return n; + } + + private: + DataSink &sink_; + }; + + data_sink_streambuf sb_; +}; + +using ContentProvider = + std::function; + +using ContentProviderWithoutLength = + std::function; + +using ContentProviderResourceReleaser = std::function; + +struct MultipartFormDataProvider { + std::string name; + ContentProviderWithoutLength provider; + std::string filename; + std::string content_type; +}; +using MultipartFormDataProviderItems = std::vector; + +using ContentReceiverWithProgress = + std::function; + +using ContentReceiver = + std::function; + +using MultipartContentHeader = + std::function; + +class ContentReader { +public: + using Reader = std::function; + using MultipartReader = std::function; + + ContentReader(Reader reader, MultipartReader multipart_reader) + : reader_(std::move(reader)), + multipart_reader_(std::move(multipart_reader)) {} + + bool operator()(MultipartContentHeader header, + ContentReceiver receiver) const { + return multipart_reader_(std::move(header), std::move(receiver)); + } + + bool operator()(ContentReceiver receiver) const { + return reader_(std::move(receiver)); + } + + Reader reader_; + MultipartReader multipart_reader_; +}; + +using Range = std::pair; +using Ranges = std::vector; + +struct Request { + std::string method; + std::string path; + Params params; + Headers headers; + std::string body; + + std::string remote_addr; + int remote_port = -1; + std::string local_addr; + int local_port = -1; + + // for server + std::string version; + std::string target; + MultipartFormDataMap files; + Ranges ranges; + Match matches; + std::unordered_map path_params; + std::function is_connection_closed = []() { return true; }; + + // for client + ResponseHandler response_handler; + ContentReceiverWithProgress content_receiver; + Progress progress; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + const SSL *ssl = nullptr; +#endif + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + bool has_param(const std::string &key) const; + std::string get_param_value(const std::string &key, size_t id = 0) const; + size_t get_param_value_count(const std::string &key) const; + + bool is_multipart_form_data() const; + + bool has_file(const std::string &key) const; + MultipartFormData get_file_value(const std::string &key) const; + std::vector get_file_values(const std::string &key) const; + + // private members... + size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; + size_t content_length_ = 0; + ContentProvider content_provider_; + bool is_chunked_content_provider_ = false; + size_t authorization_count_ = 0; + std::chrono::time_point start_time_ = + std::chrono::steady_clock::time_point::min(); +}; + +struct Response { + std::string version; + int status = -1; + std::string reason; + Headers headers; + std::string body; + std::string location; // Redirect location + + bool has_header(const std::string &key) const; + std::string get_header_value(const std::string &key, const char *def = "", + size_t id = 0) const; + uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, + size_t id = 0) const; + size_t get_header_value_count(const std::string &key) const; + void set_header(const std::string &key, const std::string &val); + + void set_redirect(const std::string &url, int status = StatusCode::Found_302); + void set_content(const char *s, size_t n, const std::string &content_type); + void set_content(const std::string &s, const std::string &content_type); + void set_content(std::string &&s, const std::string &content_type); + + void set_content_provider( + size_t length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser = nullptr); + + void set_file_content(const std::string &path, + const std::string &content_type); + void set_file_content(const std::string &path); + + Response() = default; + Response(const Response &) = default; + Response &operator=(const Response &) = default; + Response(Response &&) = default; + Response &operator=(Response &&) = default; + ~Response() { + if (content_provider_resource_releaser_) { + content_provider_resource_releaser_(content_provider_success_); + } + } + + // private members... + size_t content_length_ = 0; + ContentProvider content_provider_; + ContentProviderResourceReleaser content_provider_resource_releaser_; + bool is_chunked_content_provider_ = false; + bool content_provider_success_ = false; + std::string file_content_path_; + std::string file_content_content_type_; +}; + +class Stream { +public: + virtual ~Stream() = default; + + virtual bool is_readable() const = 0; + virtual bool is_writable() const = 0; + + virtual ssize_t read(char *ptr, size_t size) = 0; + virtual ssize_t write(const char *ptr, size_t size) = 0; + virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; + virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; + virtual socket_t socket() const = 0; + + virtual time_t duration() const = 0; + + ssize_t write(const char *ptr); + ssize_t write(const std::string &s); +}; + +class TaskQueue { +public: + TaskQueue() = default; + virtual ~TaskQueue() = default; + + virtual bool enqueue(std::function fn) = 0; + virtual void shutdown() = 0; + + virtual void on_idle() {} +}; + +class ThreadPool final : public TaskQueue { +public: + explicit ThreadPool(size_t n, size_t mqr = 0) + : shutdown_(false), max_queued_requests_(mqr) { + while (n) { + threads_.emplace_back(worker(*this)); + n--; + } + } + + ThreadPool(const ThreadPool &) = delete; + ~ThreadPool() override = default; + + bool enqueue(std::function fn) override { + { + std::unique_lock lock(mutex_); + if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { + return false; + } + jobs_.push_back(std::move(fn)); + } + + cond_.notify_one(); + return true; + } + + void shutdown() override { + // Stop all worker threads... + { + std::unique_lock lock(mutex_); + shutdown_ = true; + } + + cond_.notify_all(); + + // Join... + for (auto &t : threads_) { + t.join(); + } + } + +private: + struct worker { + explicit worker(ThreadPool &pool) : pool_(pool) {} + + void operator()() { + for (;;) { + std::function fn; + { + std::unique_lock lock(pool_.mutex_); + + pool_.cond_.wait( + lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); + + if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } + + fn = pool_.jobs_.front(); + pool_.jobs_.pop_front(); + } + + assert(true == static_cast(fn)); + fn(); + } + +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ + !defined(LIBRESSL_VERSION_NUMBER) + OPENSSL_thread_stop(); +#endif + } + + ThreadPool &pool_; + }; + friend struct worker; + + std::vector threads_; + std::list> jobs_; + + bool shutdown_; + size_t max_queued_requests_ = 0; + + std::condition_variable cond_; + std::mutex mutex_; +}; + +using Logger = std::function; + +using SocketOptions = std::function; + +void default_socket_options(socket_t sock); + +const char *status_message(int status); + +std::string get_bearer_token_auth(const Request &req); + +namespace detail { + +class MatcherBase { +public: + virtual ~MatcherBase() = default; + + // Match request path and populate its matches and + virtual bool match(Request &request) const = 0; +}; + +/** + * Captures parameters in request path and stores them in Request::path_params + * + * Capture name is a substring of a pattern from : to /. + * The rest of the pattern is matched agains the request path directly + * Parameters are captured starting from the next character after + * the end of the last matched static pattern fragment until the next /. + * + * Example pattern: + * "/path/fragments/:capture/more/fragments/:second_capture" + * Static fragments: + * "/path/fragments/", "more/fragments/" + * + * Given the following request path: + * "/path/fragments/:1/more/fragments/:2" + * the resulting capture will be + * {{"capture", "1"}, {"second_capture", "2"}} + */ +class PathParamsMatcher final : public MatcherBase { +public: + PathParamsMatcher(const std::string &pattern); + + bool match(Request &request) const override; + +private: + // Treat segment separators as the end of path parameter capture + // Does not need to handle query parameters as they are parsed before path + // matching + static constexpr char separator = '/'; + + // Contains static path fragments to match against, excluding the '/' after + // path params + // Fragments are separated by path params + std::vector static_fragments_; + // Stores the names of the path parameters to be used as keys in the + // Request::path_params map + std::vector param_names_; +}; + +/** + * Performs std::regex_match on request path + * and stores the result in Request::matches + * + * Note that regex match is performed directly on the whole request. + * This means that wildcard patterns may match multiple path segments with /: + * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". + */ +class RegexMatcher final : public MatcherBase { +public: + RegexMatcher(const std::string &pattern) : regex_(pattern) {} + + bool match(Request &request) const override; + +private: + std::regex regex_; +}; + +ssize_t write_headers(Stream &strm, const Headers &headers); + +} // namespace detail + +class Server { +public: + using Handler = std::function; + + using ExceptionHandler = + std::function; + + enum class HandlerResponse { + Handled, + Unhandled, + }; + using HandlerWithResponse = + std::function; + + using HandlerWithContentReader = std::function; + + using Expect100ContinueHandler = + std::function; + + Server(); + + virtual ~Server(); + + virtual bool is_valid() const; + + Server &Get(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, Handler handler); + Server &Post(const std::string &pattern, HandlerWithContentReader handler); + Server &Put(const std::string &pattern, Handler handler); + Server &Put(const std::string &pattern, HandlerWithContentReader handler); + Server &Patch(const std::string &pattern, Handler handler); + Server &Patch(const std::string &pattern, HandlerWithContentReader handler); + Server &Delete(const std::string &pattern, Handler handler); + Server &Delete(const std::string &pattern, HandlerWithContentReader handler); + Server &Options(const std::string &pattern, Handler handler); + + bool set_base_dir(const std::string &dir, + const std::string &mount_point = std::string()); + bool set_mount_point(const std::string &mount_point, const std::string &dir, + Headers headers = Headers()); + bool remove_mount_point(const std::string &mount_point); + Server &set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime); + Server &set_default_file_mimetype(const std::string &mime); + Server &set_file_request_handler(Handler handler); + + template + Server &set_error_handler(ErrorHandlerFunc &&handler) { + return set_error_handler_core( + std::forward(handler), + std::is_convertible{}); + } + + Server &set_exception_handler(ExceptionHandler handler); + Server &set_pre_routing_handler(HandlerWithResponse handler); + Server &set_post_routing_handler(Handler handler); + + Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); + Server &set_logger(Logger logger); + + Server &set_address_family(int family); + Server &set_tcp_nodelay(bool on); + Server &set_ipv6_v6only(bool on); + Server &set_socket_options(SocketOptions socket_options); + + Server &set_default_headers(Headers headers); + Server & + set_header_writer(std::function const &writer); + + Server &set_keep_alive_max_count(size_t count); + Server &set_keep_alive_timeout(time_t sec); + + Server &set_read_timeout(time_t sec, time_t usec = 0); + template + Server &set_read_timeout(const std::chrono::duration &duration); + + Server &set_write_timeout(time_t sec, time_t usec = 0); + template + Server &set_write_timeout(const std::chrono::duration &duration); + + Server &set_idle_interval(time_t sec, time_t usec = 0); + template + Server &set_idle_interval(const std::chrono::duration &duration); + + Server &set_payload_max_length(size_t length); + + bool bind_to_port(const std::string &host, int port, int socket_flags = 0); + int bind_to_any_port(const std::string &host, int socket_flags = 0); + bool listen_after_bind(); + + bool listen(const std::string &host, int port, int socket_flags = 0); + + bool is_running() const; + void wait_until_ready() const; + void stop(); + void decommission(); + + std::function new_task_queue; + +protected: + bool process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, + bool &connection_closed, + const std::function &setup_request); + + std::atomic svr_sock_{INVALID_SOCKET}; + size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND; + time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; + time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; + size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; + +private: + using Handlers = + std::vector, Handler>>; + using HandlersForContentReader = + std::vector, + HandlerWithContentReader>>; + + static std::unique_ptr + make_matcher(const std::string &pattern); + + Server &set_error_handler_core(HandlerWithResponse handler, std::true_type); + Server &set_error_handler_core(Handler handler, std::false_type); + + socket_t create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const; + int bind_internal(const std::string &host, int port, int socket_flags); + bool listen_internal(); + + bool routing(Request &req, Response &res, Stream &strm); + bool handle_file_request(const Request &req, Response &res, + bool head = false); + bool dispatch_request(Request &req, Response &res, + const Handlers &handlers) const; + bool dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const; + + bool parse_request_line(const char *s, Request &req) const; + void apply_ranges(const Request &req, Response &res, + std::string &content_type, std::string &boundary) const; + bool write_response(Stream &strm, bool close_connection, Request &req, + Response &res); + bool write_response_with_content(Stream &strm, bool close_connection, + const Request &req, Response &res); + bool write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges); + bool write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type); + bool read_content(Stream &strm, Request &req, Response &res); + bool + read_content_with_content_receiver(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver); + bool read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) const; + + virtual bool process_and_close_socket(socket_t sock); + + std::atomic is_running_{false}; + std::atomic is_decommisioned{false}; + + struct MountPointEntry { + std::string mount_point; + std::string base_dir; + Headers headers; + }; + std::vector base_dirs_; + std::map file_extension_and_mimetype_map_; + std::string default_file_mimetype_ = "application/octet-stream"; + Handler file_request_handler_; + + Handlers get_handlers_; + Handlers post_handlers_; + HandlersForContentReader post_handlers_for_content_reader_; + Handlers put_handlers_; + HandlersForContentReader put_handlers_for_content_reader_; + Handlers patch_handlers_; + HandlersForContentReader patch_handlers_for_content_reader_; + Handlers delete_handlers_; + HandlersForContentReader delete_handlers_for_content_reader_; + Handlers options_handlers_; + + HandlerWithResponse error_handler_; + ExceptionHandler exception_handler_; + HandlerWithResponse pre_routing_handler_; + Handler post_routing_handler_; + Expect100ContinueHandler expect_100_continue_handler_; + + Logger logger_; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; + SocketOptions socket_options_ = default_socket_options; + + Headers default_headers_; + std::function header_writer_ = + detail::write_headers; +}; + +enum class Error { + Success = 0, + Unknown, + Connection, + BindIPAddress, + Read, + Write, + ExceedRedirectCount, + Canceled, + SSLConnection, + SSLLoadingCerts, + SSLServerVerification, + SSLServerHostnameVerification, + UnsupportedMultipartBoundaryChars, + Compression, + ConnectionTimeout, + ProxyConnection, + + // For internal use only + SSLPeerCouldBeClosed_, +}; + +std::string to_string(Error error); + +std::ostream &operator<<(std::ostream &os, const Error &obj); + +class Result { +public: + Result() = default; + Result(std::unique_ptr &&res, Error err, + Headers &&request_headers = Headers{}) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)) {} + // Response + operator bool() const { return res_ != nullptr; } + bool operator==(std::nullptr_t) const { return res_ == nullptr; } + bool operator!=(std::nullptr_t) const { return res_ != nullptr; } + const Response &value() const { return *res_; } + Response &value() { return *res_; } + const Response &operator*() const { return *res_; } + Response &operator*() { return *res_; } + const Response *operator->() const { return res_.get(); } + Response *operator->() { return res_.get(); } + + // Error + Error error() const { return err_; } + + // Request Headers + bool has_request_header(const std::string &key) const; + std::string get_request_header_value(const std::string &key, + const char *def = "", + size_t id = 0) const; + uint64_t get_request_header_value_u64(const std::string &key, + uint64_t def = 0, size_t id = 0) const; + size_t get_request_header_value_count(const std::string &key) const; + +private: + std::unique_ptr res_; + Error err_ = Error::Unknown; + Headers request_headers_; +}; + +class ClientImpl { +public: + explicit ClientImpl(const std::string &host); + + explicit ClientImpl(const std::string &host, int port); + + explicit ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + virtual ~ClientImpl(); + + virtual bool is_valid() const; + + Result Get(const std::string &path); + Result Get(const std::string &path, const Headers &headers); + Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, const Headers &headers, + Progress progress); + Result Get(const std::string &path, ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const std::string &path, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ContentReceiver content_receiver, + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const Headers &headers); + Result Post(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, size_t content_length, + ContentProvider content_provider, const std::string &content_type); + Result Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Put(const std::string &path, const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + + Result Delete(const std::string &path); + Result Delete(const std::string &path, const Headers &headers); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + void stop(); + + std::string host() const; + int port() const; + + size_t is_socket_open() const; + socket_t socket() const; + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void + set_header_writer(std::function const &writer); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_ipv6_v6only(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_max_timeout(time_t msec); + template + void set_max_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + void set_ca_cert_store(X509_STORE *ca_cert_store); + X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier(std::function verifier); +#endif + + void set_logger(Logger logger); + +protected: + struct Socket { + socket_t sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSL *ssl = nullptr; +#endif + + bool is_open() const { return sock != INVALID_SOCKET; } + }; + + virtual bool create_and_connect_socket(Socket &socket, Error &error); + + // All of: + // shutdown_ssl + // shutdown_socket + // close_socket + // should ONLY be called when socket_mutex_ is locked. + // Also, shutdown_ssl and close_socket should also NOT be called concurrently + // with a DIFFERENT thread sending requests using that socket. + virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); + void shutdown_socket(Socket &socket) const; + void close_socket(Socket &socket); + + bool process_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + + bool write_content_with_provider(Stream &strm, const Request &req, + Error &error) const; + + void copy_settings(const ClientImpl &rhs); + + // Socket endpoint information + const std::string host_; + const int port_; + const std::string host_and_port_; + + // Current open socket + Socket socket_; + mutable std::mutex socket_mutex_; + std::recursive_mutex request_mutex_; + + // These are all protected under socket_mutex + size_t socket_requests_in_flight_ = 0; + std::thread::id socket_requests_are_from_thread_ = std::thread::id(); + bool socket_should_be_closed_when_request_is_done_ = false; + + // Hostname-IP map + std::map addr_map_; + + // Default headers + Headers default_headers_; + + // Header writer + std::function header_writer_ = + detail::write_headers; + + // Settings + std::string client_cert_path_; + std::string client_key_path_; + + time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; + time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; + time_t max_timeout_msec_ = CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND; + + std::string basic_auth_username_; + std::string basic_auth_password_; + std::string bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string digest_auth_username_; + std::string digest_auth_password_; +#endif + + bool keep_alive_ = false; + bool follow_location_ = false; + + bool url_encode_ = true; + + int address_family_ = AF_UNSPEC; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; + SocketOptions socket_options_ = nullptr; + + bool compress_ = false; + bool decompress_ = true; + + std::string interface_; + + std::string proxy_host_; + int proxy_port_ = -1; + + std::string proxy_basic_auth_username_; + std::string proxy_basic_auth_password_; + std::string proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string proxy_digest_auth_username_; + std::string proxy_digest_auth_password_; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + std::string ca_cert_file_path_; + std::string ca_cert_dir_path_; + + X509_STORE *ca_cert_store_ = nullptr; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool server_certificate_verification_ = true; + bool server_hostname_verification_ = true; + std::function server_certificate_verifier_; +#endif + + Logger logger_; + +private: + bool send_(Request &req, Response &res, Error &error); + Result send_(Request &&req); + + socket_t create_client_socket(Error &error) const; + bool read_response_line(Stream &strm, const Request &req, + Response &res) const; + bool write_request(Stream &strm, Request &req, bool close_connection, + Error &error); + bool redirect(Request &req, Response &res, Error &error); + bool handle_request(Stream &strm, Request &req, Response &res, + bool close_connection, Error &error); + std::unique_ptr send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error); + Result send_with_content_provider( + const std::string &method, const std::string &path, + const Headers &headers, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Progress progress); + ContentProviderWithoutLength get_multipart_content_provider( + const std::string &boundary, const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) const; + + std::string adjust_host_string(const std::string &host) const; + + virtual bool + process_socket(const Socket &socket, + std::chrono::time_point start_time, + std::function callback); + virtual bool is_ssl() const; +}; + +class Client { +public: + // Universal interface + explicit Client(const std::string &scheme_host_port); + + explicit Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path); + + // HTTP only interface + explicit Client(const std::string &host, int port); + + explicit Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + Client(Client &&) = default; + Client &operator=(Client &&) = default; + + ~Client(); + + bool is_valid() const; + + Result Get(const std::string &path); + Result Get(const std::string &path, const Headers &headers); + Result Get(const std::string &path, Progress progress); + Result Get(const std::string &path, const Headers &headers, + Progress progress); + Result Get(const std::string &path, ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const std::string &path, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + Result Get(const std::string &path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ContentReceiver content_receiver, + Progress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, + const Headers &headers, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress = nullptr); + + Result Head(const std::string &path); + Result Head(const std::string &path, const Headers &headers); + + Result Post(const std::string &path); + Result Post(const std::string &path, const Headers &headers); + Result Post(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type); + Result Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Post(const std::string &path, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Put(const std::string &path); + Result Put(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type); + Result Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Put(const std::string &path, size_t content_length, + ContentProvider content_provider, const std::string &content_type); + Result Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Put(const std::string &path, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); + Result Put(const std::string &path, const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, const std::string &boundary); + Result Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items); + + Result Patch(const std::string &path); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type); + Result Patch(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + Result Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + size_t content_length, ContentProvider content_provider, + const std::string &content_type); + Result Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type); + + Result Delete(const std::string &path); + Result Delete(const std::string &path, const Headers &headers); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type); + Result Delete(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, + Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type); + Result Delete(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type); + Result Delete(const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + Progress progress); + + Result Options(const std::string &path); + Result Options(const std::string &path, const Headers &headers); + + bool send(Request &req, Response &res, Error &error); + Result send(const Request &req); + + void stop(); + + std::string host() const; + int port() const; + + size_t is_socket_open() const; + socket_t socket() const; + + void set_hostname_addr_map(std::map addr_map); + + void set_default_headers(Headers headers); + + void + set_header_writer(std::function const &writer); + + void set_address_family(int family); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + template + void + set_connection_timeout(const std::chrono::duration &duration); + + void set_read_timeout(time_t sec, time_t usec = 0); + template + void set_read_timeout(const std::chrono::duration &duration); + + void set_write_timeout(time_t sec, time_t usec = 0); + template + void set_write_timeout(const std::chrono::duration &duration); + + void set_max_timeout(time_t msec); + template + void set_max_timeout(const std::chrono::duration &duration); + + void set_basic_auth(const std::string &username, const std::string &password); + void set_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const std::string &username, + const std::string &password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_url_encode(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const std::string &intf); + + void set_proxy(const std::string &host, int port); + void set_proxy_basic_auth(const std::string &username, + const std::string &password); + void set_proxy_bearer_token_auth(const std::string &token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const std::string &username, + const std::string &password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + void set_server_certificate_verifier(std::function verifier); +#endif + + void set_logger(Logger logger); + + // SSL +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + + void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; +#endif + +private: + std::unique_ptr cli_; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool is_ssl_ = false; +#endif +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLServer : public Server { +public: + SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path = nullptr, + const char *client_ca_cert_dir_path = nullptr, + const char *private_key_password = nullptr); + + SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + + SSLServer( + const std::function &setup_ssl_ctx_callback); + + ~SSLServer() override; + + bool is_valid() const override; + + SSL_CTX *ssl_context() const; + + void update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + +private: + bool process_and_close_socket(socket_t sock) override; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; +}; + +class SSLClient final : public ClientImpl { +public: + explicit SSLClient(const std::string &host); + + explicit SSLClient(const std::string &host, int port); + + explicit SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password = std::string()); + + explicit SSLClient(const std::string &host, int port, X509 *client_cert, + EVP_PKEY *client_key, + const std::string &private_key_password = std::string()); + + ~SSLClient() override; + + bool is_valid() const override; + + void set_ca_cert_store(X509_STORE *ca_cert_store); + void load_ca_cert_store(const char *ca_cert, std::size_t size); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; + +private: + bool create_and_connect_socket(Socket &socket, Error &error) override; + void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; + void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); + + bool + process_socket(const Socket &socket, + std::chrono::time_point start_time, + std::function callback) override; + bool is_ssl() const override; + + bool connect_with_proxy( + Socket &sock, + std::chrono::time_point start_time, + Response &res, bool &success, Error &error); + bool initialize_ssl(Socket &socket, Error &error); + + bool load_certs(); + + bool verify_host(X509 *server_cert) const; + bool verify_host_with_subject_alt_name(X509 *server_cert) const; + bool verify_host_with_common_name(X509 *server_cert) const; + bool check_host_name(const char *pattern, size_t pattern_len) const; + + SSL_CTX *ctx_; + std::mutex ctx_mutex_; + std::once_flag initialize_cert_; + + std::vector host_components_; + + long verify_result_ = 0; + + friend class ClientImpl; +}; +#endif + +/* + * Implementation of template methods. + */ + +namespace detail { + +template +inline void duration_to_sec_and_usec(const T &duration, U callback) { + auto sec = std::chrono::duration_cast(duration).count(); + auto usec = std::chrono::duration_cast( + duration - std::chrono::seconds(sec)) + .count(); + callback(static_cast(sec), static_cast(usec)); +} + +inline bool is_numeric(const std::string &str) { + return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); +} + +inline uint64_t get_header_value_u64(const Headers &headers, + const std::string &key, uint64_t def, + size_t id, bool &is_invalid_value) { + is_invalid_value = false; + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { + if (is_numeric(it->second)) { + return std::strtoull(it->second.data(), nullptr, 10); + } else { + is_invalid_value = true; + } + } + return def; +} + +inline uint64_t get_header_value_u64(const Headers &headers, + const std::string &key, uint64_t def, + size_t id) { + bool dummy = false; + return get_header_value_u64(headers, key, def, id, dummy); +} + +} // namespace detail + +inline uint64_t Request::get_header_value_u64(const std::string &key, + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + +inline uint64_t Response::get_header_value_u64(const std::string &key, + uint64_t def, size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + +inline void default_socket_options(socket_t sock) { + int opt = 1; +#ifdef _WIN32 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&opt), sizeof(opt)); +#else +#ifdef SO_REUSEPORT + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, + reinterpret_cast(&opt), sizeof(opt)); +#else + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + reinterpret_cast(&opt), sizeof(opt)); +#endif +#endif +} + +inline const char *status_message(int status) { + switch (status) { + case StatusCode::Continue_100: return "Continue"; + case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; + case StatusCode::Processing_102: return "Processing"; + case StatusCode::EarlyHints_103: return "Early Hints"; + case StatusCode::OK_200: return "OK"; + case StatusCode::Created_201: return "Created"; + case StatusCode::Accepted_202: return "Accepted"; + case StatusCode::NonAuthoritativeInformation_203: + return "Non-Authoritative Information"; + case StatusCode::NoContent_204: return "No Content"; + case StatusCode::ResetContent_205: return "Reset Content"; + case StatusCode::PartialContent_206: return "Partial Content"; + case StatusCode::MultiStatus_207: return "Multi-Status"; + case StatusCode::AlreadyReported_208: return "Already Reported"; + case StatusCode::IMUsed_226: return "IM Used"; + case StatusCode::MultipleChoices_300: return "Multiple Choices"; + case StatusCode::MovedPermanently_301: return "Moved Permanently"; + case StatusCode::Found_302: return "Found"; + case StatusCode::SeeOther_303: return "See Other"; + case StatusCode::NotModified_304: return "Not Modified"; + case StatusCode::UseProxy_305: return "Use Proxy"; + case StatusCode::unused_306: return "unused"; + case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; + case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; + case StatusCode::BadRequest_400: return "Bad Request"; + case StatusCode::Unauthorized_401: return "Unauthorized"; + case StatusCode::PaymentRequired_402: return "Payment Required"; + case StatusCode::Forbidden_403: return "Forbidden"; + case StatusCode::NotFound_404: return "Not Found"; + case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; + case StatusCode::NotAcceptable_406: return "Not Acceptable"; + case StatusCode::ProxyAuthenticationRequired_407: + return "Proxy Authentication Required"; + case StatusCode::RequestTimeout_408: return "Request Timeout"; + case StatusCode::Conflict_409: return "Conflict"; + case StatusCode::Gone_410: return "Gone"; + case StatusCode::LengthRequired_411: return "Length Required"; + case StatusCode::PreconditionFailed_412: return "Precondition Failed"; + case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; + case StatusCode::UriTooLong_414: return "URI Too Long"; + case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; + case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; + case StatusCode::ExpectationFailed_417: return "Expectation Failed"; + case StatusCode::ImATeapot_418: return "I'm a teapot"; + case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; + case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; + case StatusCode::Locked_423: return "Locked"; + case StatusCode::FailedDependency_424: return "Failed Dependency"; + case StatusCode::TooEarly_425: return "Too Early"; + case StatusCode::UpgradeRequired_426: return "Upgrade Required"; + case StatusCode::PreconditionRequired_428: return "Precondition Required"; + case StatusCode::TooManyRequests_429: return "Too Many Requests"; + case StatusCode::RequestHeaderFieldsTooLarge_431: + return "Request Header Fields Too Large"; + case StatusCode::UnavailableForLegalReasons_451: + return "Unavailable For Legal Reasons"; + case StatusCode::NotImplemented_501: return "Not Implemented"; + case StatusCode::BadGateway_502: return "Bad Gateway"; + case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; + case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; + case StatusCode::HttpVersionNotSupported_505: + return "HTTP Version Not Supported"; + case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; + case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; + case StatusCode::LoopDetected_508: return "Loop Detected"; + case StatusCode::NotExtended_510: return "Not Extended"; + case StatusCode::NetworkAuthenticationRequired_511: + return "Network Authentication Required"; + + default: + case StatusCode::InternalServerError_500: return "Internal Server Error"; + } +} + +inline std::string get_bearer_token_auth(const Request &req) { + if (req.has_header("Authorization")) { + static std::string BearerHeaderPrefix = "Bearer "; + return req.get_header_value("Authorization") + .substr(BearerHeaderPrefix.length()); + } + return ""; +} + +template +inline Server & +Server::set_read_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_write_timeout(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); + return *this; +} + +template +inline Server & +Server::set_idle_interval(const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); + return *this; +} + +inline std::string to_string(const Error error) { + switch (error) { + case Error::Success: return "Success (no error)"; + case Error::Connection: return "Could not establish connection"; + case Error::BindIPAddress: return "Failed to bind IP address"; + case Error::Read: return "Failed to read connection"; + case Error::Write: return "Failed to write connection"; + case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; + case Error::Canceled: return "Connection handling canceled"; + case Error::SSLConnection: return "SSL connection failed"; + case Error::SSLLoadingCerts: return "SSL certificate loading failed"; + case Error::SSLServerVerification: return "SSL server verification failed"; + case Error::SSLServerHostnameVerification: + return "SSL server hostname verification failed"; + case Error::UnsupportedMultipartBoundaryChars: + return "Unsupported HTTP multipart boundary characters"; + case Error::Compression: return "Compression failed"; + case Error::ConnectionTimeout: return "Connection timed out"; + case Error::ProxyConnection: return "Proxy connection failed"; + case Error::Unknown: return "Unknown"; + default: break; + } + + return "Invalid"; +} + +inline std::ostream &operator<<(std::ostream &os, const Error &obj) { + os << to_string(obj); + os << " (" << static_cast::type>(obj) << ')'; + return os; +} + +inline uint64_t Result::get_request_header_value_u64(const std::string &key, + uint64_t def, + size_t id) const { + return detail::get_header_value_u64(request_headers_, key, def, id); +} + +template +inline void ClientImpl::set_connection_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { + set_connection_timeout(sec, usec); + }); +} + +template +inline void ClientImpl::set_read_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); +} + +template +inline void ClientImpl::set_write_timeout( + const std::chrono::duration &duration) { + detail::duration_to_sec_and_usec( + duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); +} + +template +inline void ClientImpl::set_max_timeout( + const std::chrono::duration &duration) { + auto msec = + std::chrono::duration_cast(duration).count(); + set_max_timeout(msec); +} + +template +inline void Client::set_connection_timeout( + const std::chrono::duration &duration) { + cli_->set_connection_timeout(duration); +} + +template +inline void +Client::set_read_timeout(const std::chrono::duration &duration) { + cli_->set_read_timeout(duration); +} + +template +inline void +Client::set_write_timeout(const std::chrono::duration &duration) { + cli_->set_write_timeout(duration); +} + +template +inline void +Client::set_max_timeout(const std::chrono::duration &duration) { + cli_->set_max_timeout(duration); +} + +/* + * Forward declarations and types that will be part of the .h file if split into + * .h + .cc. + */ + +std::string hosted_at(const std::string &hostname); + +void hosted_at(const std::string &hostname, std::vector &addrs); + +std::string append_query_params(const std::string &path, const Params ¶ms); + +std::pair make_range_header(const Ranges &ranges); + +std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, + bool is_proxy = false); + +namespace detail { + +#if defined(_WIN32) +inline std::wstring u8string_to_wstring(const char *s) { + std::wstring ws; + auto len = static_cast(strlen(s)); + auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); + if (wlen > 0) { + ws.resize(wlen); + wlen = ::MultiByteToWideChar( + CP_UTF8, 0, s, len, + const_cast(reinterpret_cast(ws.data())), wlen); + if (wlen != static_cast(ws.size())) { ws.clear(); } + } + return ws; +} +#endif + +struct FileStat { + FileStat(const std::string &path); + bool is_file() const; + bool is_dir() const; + +private: +#if defined(_WIN32) + struct _stat st_; +#else + struct stat st_; +#endif + int ret_ = -1; +}; + +std::string encode_query_param(const std::string &value); + +std::string decode_url(const std::string &s, bool convert_plus_to_space); + +void read_file(const std::string &path, std::string &out); + +std::string trim_copy(const std::string &s); + +void divide( + const char *data, std::size_t size, char d, + std::function + fn); + +void divide( + const std::string &str, char d, + std::function + fn); + +void split(const char *b, const char *e, char d, + std::function fn); + +void split(const char *b, const char *e, char d, size_t m, + std::function fn); + +bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, + std::function callback); + +socket_t create_client_socket(const std::string &host, const std::string &ip, + int port, int address_family, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + time_t connection_timeout_sec, + time_t connection_timeout_usec, + time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, + const std::string &intf, Error &error); + +const char *get_header_value(const Headers &headers, const std::string &key, + const char *def, size_t id); + +std::string params_to_query_str(const Params ¶ms); + +void parse_query_text(const char *data, std::size_t size, Params ¶ms); + +void parse_query_text(const std::string &s, Params ¶ms); + +bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary); + +bool parse_range_header(const std::string &s, Ranges &ranges); + +int close_socket(socket_t sock); + +ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); + +ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); + +enum class EncodingType { None = 0, Gzip, Brotli }; + +EncodingType encoding_type(const Request &req, const Response &res); + +class BufferStream final : public Stream { +public: + BufferStream() = default; + ~BufferStream() override = default; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + + const std::string &get_buffer() const; + +private: + std::string buffer; + size_t position = 0; +}; + +class compressor { +public: + virtual ~compressor() = default; + + typedef std::function Callback; + virtual bool compress(const char *data, size_t data_length, bool last, + Callback callback) = 0; +}; + +class decompressor { +public: + virtual ~decompressor() = default; + + virtual bool is_valid() const = 0; + + typedef std::function Callback; + virtual bool decompress(const char *data, size_t data_length, + Callback callback) = 0; +}; + +class nocompressor final : public compressor { +public: + ~nocompressor() override = default; + + bool compress(const char *data, size_t data_length, bool /*last*/, + Callback callback) override; +}; + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +class gzip_compressor final : public compressor { +public: + gzip_compressor(); + ~gzip_compressor() override; + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; + +class gzip_decompressor final : public decompressor { +public: + gzip_decompressor(); + ~gzip_decompressor() override; + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + bool is_valid_ = false; + z_stream strm_; +}; +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +class brotli_compressor final : public compressor { +public: + brotli_compressor(); + ~brotli_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + BrotliEncoderState *state_ = nullptr; +}; + +class brotli_decompressor final : public decompressor { +public: + brotli_decompressor(); + ~brotli_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + BrotliDecoderResult decoder_r; + BrotliDecoderState *decoder_s = nullptr; +}; +#endif + +// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` +// to store data. The call can set memory on stack for performance. +class stream_line_reader { +public: + stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size); + const char *ptr() const; + size_t size() const; + bool end_with_crlf() const; + bool getline(); + +private: + void append(char c); + + Stream &strm_; + char *fixed_buffer_; + const size_t fixed_buffer_size_; + size_t fixed_buffer_used_size_ = 0; + std::string glowable_buffer_; +}; + +class mmap { +public: + mmap(const char *path); + ~mmap(); + + bool open(const char *path); + void close(); + + bool is_open() const; + size_t size() const; + const char *data() const; + +private: +#if defined(_WIN32) + HANDLE hFile_ = NULL; + HANDLE hMapping_ = NULL; +#else + int fd_ = -1; +#endif + size_t size_ = 0; + void *addr_ = nullptr; + bool is_open_empty_file = false; +}; + +// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 +namespace fields { + +inline bool is_token_char(char c) { + return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || + c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || + c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; +} + +inline bool is_token(const std::string &s) { + if (s.empty()) { return false; } + for (auto c : s) { + if (!is_token_char(c)) { return false; } + } + return true; +} + +inline bool is_field_name(const std::string &s) { return is_token(s); } + +inline bool is_vchar(char c) { return c >= 33 && c <= 126; } + +inline bool is_obs_text(char c) { return 128 <= static_cast(c); } + +inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } + +inline bool is_field_content(const std::string &s) { + if (s.empty()) { return true; } + + if (s.size() == 1) { + return is_field_vchar(s[0]); + } else if (s.size() == 2) { + return is_field_vchar(s[0]) && is_field_vchar(s[1]); + } else { + size_t i = 0; + + if (!is_field_vchar(s[i])) { return false; } + i++; + + while (i < s.size() - 1) { + auto c = s[i++]; + if (c == ' ' || c == '\t' || is_field_vchar(c)) { + } else { + return false; + } + } + + return is_field_vchar(s[i]); + } +} + +inline bool is_field_value(const std::string &s) { return is_field_content(s); } + +} // namespace fields + +} // namespace detail + +// ---------------------------------------------------------------------------- + +/* + * Implementation that will be part of the .cc file if split into .h + .cc. + */ + +namespace detail { + +inline bool is_hex(char c, int &v) { + if (0x20 <= c && isdigit(c)) { + v = c - '0'; + return true; + } else if ('A' <= c && c <= 'F') { + v = c - 'A' + 10; + return true; + } else if ('a' <= c && c <= 'f') { + v = c - 'a' + 10; + return true; + } + return false; +} + +inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, + int &val) { + if (i >= s.size()) { return false; } + + val = 0; + for (; cnt; i++, cnt--) { + if (!s[i]) { return false; } + auto v = 0; + if (is_hex(s[i], v)) { + val = val * 16 + v; + } else { + return false; + } + } + return true; +} + +inline std::string from_i_to_hex(size_t n) { + static const auto charset = "0123456789abcdef"; + std::string ret; + do { + ret = charset[n & 15] + ret; + n >>= 4; + } while (n > 0); + return ret; +} + +inline size_t to_utf8(int code, char *buff) { + if (code < 0x0080) { + buff[0] = static_cast(code & 0x7F); + return 1; + } else if (code < 0x0800) { + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); + return 2; + } else if (code < 0xD800) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0xE000) { // D800 - DFFF is invalid... + return 0; + } else if (code < 0x10000) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0x110000) { + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); + return 4; + } + + // NOTREACHED + return 0; +} + +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c +inline std::string base64_encode(const std::string &in) { + static const auto lookup = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + std::string out; + out.reserve(in.size()); + + auto val = 0; + auto valb = -6; + + for (auto c : in) { + val = (val << 8) + static_cast(c); + valb += 8; + while (valb >= 0) { + out.push_back(lookup[(val >> valb) & 0x3F]); + valb -= 6; + } + } + + if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } + + while (out.size() % 4) { + out.push_back('='); + } + + return out; +} + +inline bool is_valid_path(const std::string &path) { + size_t level = 0; + size_t i = 0; + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + + while (i < path.size()) { + // Read component + auto beg = i; + while (i < path.size() && path[i] != '/') { + if (path[i] == '\0') { + return false; + } else if (path[i] == '\\') { + return false; + } + i++; + } + + auto len = i - beg; + assert(len > 0); + + if (!path.compare(beg, len, ".")) { + ; + } else if (!path.compare(beg, len, "..")) { + if (level == 0) { return false; } + level--; + } else { + level++; + } + + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + } + + return true; +} + +inline FileStat::FileStat(const std::string &path) { +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path.c_str()); + ret_ = _wstat(wpath.c_str(), &st_); +#else + ret_ = stat(path.c_str(), &st_); +#endif +} +inline bool FileStat::is_file() const { + return ret_ >= 0 && S_ISREG(st_.st_mode); +} +inline bool FileStat::is_dir() const { + return ret_ >= 0 && S_ISDIR(st_.st_mode); +} + +inline std::string encode_query_param(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string encode_url(const std::string &s) { + std::string result; + result.reserve(s.size()); + + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; + case '\r': result += "%0D"; break; + case '\n': result += "%0A"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + // case ':': result += "%3A"; break; // ok? probably... + case ';': result += "%3B"; break; + default: + auto c = static_cast(s[i]); + if (c >= 0x80) { + result += '%'; + char hex[4]; + auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); + assert(len == 2); + result.append(hex, static_cast(len)); + } else { + result += s[i]; + } + break; + } + } + + return result; +} + +inline std::string decode_url(const std::string &s, + bool convert_plus_to_space) { + std::string result; + + for (size_t i = 0; i < s.size(); i++) { + if (s[i] == '%' && i + 1 < s.size()) { + if (s[i + 1] == 'u') { + auto val = 0; + if (from_hex_to_i(s, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = to_utf8(val, buff); + if (len > 0) { result.append(buff, len); } + i += 5; // 'u0000' + } else { + result += s[i]; + } + } else { + auto val = 0; + if (from_hex_to_i(s, i + 1, 2, val)) { + // 2 digits hex codes + result += static_cast(val); + i += 2; // '00' + } else { + result += s[i]; + } + } + } else if (convert_plus_to_space && s[i] == '+') { + result += ' '; + } else { + result += s[i]; + } + } + + return result; +} + +inline void read_file(const std::string &path, std::string &out) { + std::ifstream fs(path, std::ios_base::binary); + fs.seekg(0, std::ios_base::end); + auto size = fs.tellg(); + fs.seekg(0); + out.resize(static_cast(size)); + fs.read(&out[0], static_cast(size)); +} + +inline std::string file_extension(const std::string &path) { + std::smatch m; + static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); + if (std::regex_search(path, m, re)) { return m[1].str(); } + return std::string(); +} + +inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } + +inline std::pair trim(const char *b, const char *e, size_t left, + size_t right) { + while (b + left < e && is_space_or_tab(b[left])) { + left++; + } + while (right > 0 && is_space_or_tab(b[right - 1])) { + right--; + } + return std::make_pair(left, right); +} + +inline std::string trim_copy(const std::string &s) { + auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); + return s.substr(r.first, r.second - r.first); +} + +inline std::string trim_double_quotes_copy(const std::string &s) { + if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { + return s.substr(1, s.size() - 2); + } + return s; +} + +inline void +divide(const char *data, std::size_t size, char d, + std::function + fn) { + const auto it = std::find(data, data + size, d); + const auto found = static_cast(it != data + size); + const auto lhs_data = data; + const auto lhs_size = static_cast(it - data); + const auto rhs_data = it + found; + const auto rhs_size = size - lhs_size - found; + + fn(lhs_data, lhs_size, rhs_data, rhs_size); +} + +inline void +divide(const std::string &str, char d, + std::function + fn) { + divide(str.data(), str.size(), d, std::move(fn)); +} + +inline void split(const char *b, const char *e, char d, + std::function fn) { + return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); +} + +inline void split(const char *b, const char *e, char d, size_t m, + std::function fn) { + size_t i = 0; + size_t beg = 0; + size_t count = 1; + + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d && count < m) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + beg = i + 1; + count++; + } + i++; + } + + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + } +} + +inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size) + : strm_(strm), fixed_buffer_(fixed_buffer), + fixed_buffer_size_(fixed_buffer_size) {} + +inline const char *stream_line_reader::ptr() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_; + } else { + return glowable_buffer_.data(); + } +} + +inline size_t stream_line_reader::size() const { + if (glowable_buffer_.empty()) { + return fixed_buffer_used_size_; + } else { + return glowable_buffer_.size(); + } +} + +inline bool stream_line_reader::end_with_crlf() const { + auto end = ptr() + size(); + return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; +} + +inline bool stream_line_reader::getline() { + fixed_buffer_used_size_ = 0; + glowable_buffer_.clear(); + +#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + char prev_byte = 0; +#endif + + for (size_t i = 0;; i++) { + char byte; + auto n = strm_.read(&byte, 1); + + if (n < 0) { + return false; + } else if (n == 0) { + if (i == 0) { + return false; + } else { + break; + } + } + + append(byte); + +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + if (byte == '\n') { break; } +#else + if (prev_byte == '\r' && byte == '\n') { break; } + prev_byte = byte; +#endif + } + + return true; +} + +inline void stream_line_reader::append(char c) { + if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { + fixed_buffer_[fixed_buffer_used_size_++] = c; + fixed_buffer_[fixed_buffer_used_size_] = '\0'; + } else { + if (glowable_buffer_.empty()) { + assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); + glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); + } + glowable_buffer_ += c; + } +} + +inline mmap::mmap(const char *path) { open(path); } + +inline mmap::~mmap() { close(); } + +inline bool mmap::open(const char *path) { + close(); + +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path); + if (wpath.empty()) { return false; } + +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); +#else + hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); +#endif + + if (hFile_ == INVALID_HANDLE_VALUE) { return false; } + + LARGE_INTEGER size{}; + if (!::GetFileSizeEx(hFile_, &size)) { return false; } + // If the following line doesn't compile due to QuadPart, update Windows SDK. + // See: + // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 + if (static_cast(size.QuadPart) > + (std::numeric_limits::max)()) { + // `size_t` might be 32-bits, on 32-bits Windows. + return false; + } + size_ = static_cast(size.QuadPart); + +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 + hMapping_ = + ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); +#else + hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); +#endif + + // Special treatment for an empty file... + if (hMapping_ == NULL && size_ == 0) { + close(); + is_open_empty_file = true; + return true; + } + + if (hMapping_ == NULL) { + close(); + return false; + } + +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 + addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); +#else + addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); +#endif + + if (addr_ == nullptr) { + close(); + return false; + } +#else + fd_ = ::open(path, O_RDONLY); + if (fd_ == -1) { return false; } + + struct stat sb; + if (fstat(fd_, &sb) == -1) { + close(); + return false; + } + size_ = static_cast(sb.st_size); + + addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); + + // Special treatment for an empty file... + if (addr_ == MAP_FAILED && size_ == 0) { + close(); + is_open_empty_file = true; + return false; + } +#endif + + return true; +} + +inline bool mmap::is_open() const { + return is_open_empty_file ? true : addr_ != nullptr; +} + +inline size_t mmap::size() const { return size_; } + +inline const char *mmap::data() const { + return is_open_empty_file ? "" : static_cast(addr_); +} + +inline void mmap::close() { +#if defined(_WIN32) + if (addr_) { + ::UnmapViewOfFile(addr_); + addr_ = nullptr; + } + + if (hMapping_) { + ::CloseHandle(hMapping_); + hMapping_ = NULL; + } + + if (hFile_ != INVALID_HANDLE_VALUE) { + ::CloseHandle(hFile_); + hFile_ = INVALID_HANDLE_VALUE; + } + + is_open_empty_file = false; +#else + if (addr_ != nullptr) { + munmap(addr_, size_); + addr_ = nullptr; + } + + if (fd_ != -1) { + ::close(fd_); + fd_ = -1; + } +#endif + size_ = 0; +} +inline int close_socket(socket_t sock) { +#ifdef _WIN32 + return closesocket(sock); +#else + return close(sock); +#endif +} + +template inline ssize_t handle_EINTR(T fn) { + ssize_t res = 0; + while (true) { + res = fn(); + if (res < 0 && errno == EINTR) { + std::this_thread::sleep_for(std::chrono::microseconds{1}); + continue; + } + break; + } + return res; +} + +inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { + return handle_EINTR([&]() { + return recv(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, + int flags) { + return handle_EINTR([&]() { + return send(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); + }); +} + +template +inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd; + pfd.fd = sock; + pfd.events = (Read ? POLLIN : POLLOUT); + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + return handle_EINTR([&]() { return poll(&pfd, 1, timeout); }); +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return -1; } +#endif + + fd_set fds, *rfds, *wfds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + rfds = (Read ? &fds : nullptr); + wfds = (Read ? nullptr : &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); + }); +#endif +} + +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} + +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} + +inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, + time_t usec) { +#ifdef CPPHTTPLIB_USE_POLL + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN | POLLOUT; + + auto timeout = static_cast(sec * 1000 + usec / 1000); + + auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); + + if (poll_res == 0) { return Error::ConnectionTimeout; } + + if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + + return Error::Connection; +#else +#ifndef _WIN32 + if (sock >= FD_SETSIZE) { return Error::Connection; } +#endif + + fd_set fdsr; + FD_ZERO(&fdsr); + FD_SET(sock, &fdsr); + + auto fdsw = fdsr; + auto fdse = fdsr; + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); + }); + + if (ret == 0) { return Error::ConnectionTimeout; } + + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + return Error::Connection; +#endif +} + +inline bool is_socket_alive(socket_t sock) { + const auto val = detail::select_read(sock, 0, 0); + if (val == 0) { + return true; + } else if (val < 0 && errno == EBADF) { + return false; + } + char buf[1]; + return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; +} + +class SocketStream final : public Stream { +public: + SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + std::chrono::steady_clock::time_point::min()); + ~SocketStream() override; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time; + + std::vector read_buff_; + size_t read_buff_off_ = 0; + size_t read_buff_content_size_ = 0; + + static const size_t read_buff_size_ = 1024l * 4; +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLSocketStream final : public Stream { +public: + SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + std::chrono::steady_clock::time_point::min()); + ~SSLSocketStream() override; + + bool is_readable() const override; + bool is_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + SSL *ssl_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time; +}; +#endif + +inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, + time_t keep_alive_timeout_sec) { + using namespace std::chrono; + + const auto interval_usec = + CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND; + + // Avoid expensive `steady_clock::now()` call for the first time + if (select_read(sock, 0, interval_usec) > 0) { return true; } + + const auto start = steady_clock::now() - microseconds{interval_usec}; + const auto timeout = seconds{keep_alive_timeout_sec}; + + while (true) { + if (svr_sock == INVALID_SOCKET) { + break; // Server socket is closed + } + + auto val = select_read(sock, 0, interval_usec); + if (val < 0) { + break; // Ssocket error + } else if (val == 0) { + if (steady_clock::now() - start > timeout) { + break; // Timeout + } + } else { + return true; // Ready for read + } + } + + return false; +} + +template +inline bool +process_server_socket_core(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, T callback) { + assert(keep_alive_max_count > 0); + auto ret = false; + auto count = keep_alive_max_count; + while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { + auto close_connection = count == 1; + auto connection_closed = false; + ret = callback(close_connection, connection_closed); + if (!ret || connection_closed) { break; } + count--; + } + return ret; +} + +template +inline bool +process_server_socket(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +inline bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, + std::function callback) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec, max_timeout_msec, + start_time); + return callback(strm); +} + +inline int shutdown_socket(socket_t sock) { +#ifdef _WIN32 + return shutdown(sock, SD_BOTH); +#else + return shutdown(sock, SHUT_RDWR); +#endif +} + +inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '\0') { + auto ret = s; + ret[0] = '@'; + return ret; + } + return s; +} + +inline std::string +unescape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '@') { + auto ret = s; + ret[0] = '\0'; + return ret; + } + return s; +} + +template +socket_t create_socket(const std::string &host, const std::string &ip, int port, + int address_family, int socket_flags, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + BindOrConnect bind_or_connect) { + // Get address info + const char *node = nullptr; + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_IP; + + if (!ip.empty()) { + node = ip.c_str(); + // Ask getaddrinfo to convert IP in c-string to address + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + } else { + if (!host.empty()) { node = host.c_str(); } + hints.ai_family = address_family; + hints.ai_flags = socket_flags; + } + +#ifndef _WIN32 + if (hints.ai_family == AF_UNIX) { + const auto addrlen = host.length(); + if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } + +#ifdef SOCK_CLOEXEC + auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, + hints.ai_protocol); +#else + auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); +#endif + + if (sock != INVALID_SOCKET) { + sockaddr_un addr{}; + addr.sun_family = AF_UNIX; + + auto unescaped_host = unescape_abstract_namespace_unix_domain(host); + std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); + + hints.ai_addr = reinterpret_cast(&addr); + hints.ai_addrlen = static_cast( + sizeof(addr) - sizeof(addr.sun_path) + addrlen); + +#ifndef SOCK_CLOEXEC + fcntl(sock, F_SETFD, FD_CLOEXEC); +#endif + + if (socket_options) { socket_options(sock); } + + bool dummy; + if (!bind_or_connect(sock, hints, dummy)) { + close_socket(sock); + sock = INVALID_SOCKET; + } + } + return sock; + } +#endif + + auto service = std::to_string(port); + + if (getaddrinfo(node, service.c_str(), &hints, &result)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return INVALID_SOCKET; + } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + for (auto rp = result; rp; rp = rp->ai_next) { + // Create a socket +#ifdef _WIN32 + auto sock = + WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, + WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); + /** + * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 + * and above the socket creation fails on older Windows Systems. + * + * Let's try to create a socket the old way in this case. + * + * Reference: + * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa + * + * WSA_FLAG_NO_HANDLE_INHERIT: + * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with + * SP1, and later + * + */ + if (sock == INVALID_SOCKET) { + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + } +#else + +#ifdef SOCK_CLOEXEC + auto sock = + socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); +#else + auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); +#endif + +#endif + if (sock == INVALID_SOCKET) { continue; } + +#if !defined _WIN32 && !defined SOCK_CLOEXEC + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { + close_socket(sock); + continue; + } +#endif + + if (tcp_nodelay) { + auto opt = 1; +#ifdef _WIN32 + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, + reinterpret_cast(&opt), sizeof(opt)); +#else + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, + reinterpret_cast(&opt), sizeof(opt)); +#endif + } + + if (rp->ai_family == AF_INET6) { + auto opt = ipv6_v6only ? 1 : 0; +#ifdef _WIN32 + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&opt), sizeof(opt)); +#else + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&opt), sizeof(opt)); +#endif + } + + if (socket_options) { socket_options(sock); } + + // bind or connect + auto quit = false; + if (bind_or_connect(sock, *rp, quit)) { return sock; } + + close_socket(sock); + + if (quit) { break; } + } + + return INVALID_SOCKET; +} + +inline void set_nonblocking(socket_t sock, bool nonblocking) { +#ifdef _WIN32 + auto flags = nonblocking ? 1UL : 0UL; + ioctlsocket(sock, FIONBIO, &flags); +#else + auto flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, + nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); +#endif +} + +inline bool is_connection_error() { +#ifdef _WIN32 + return WSAGetLastError() != WSAEWOULDBLOCK; +#else + return errno != EINPROGRESS; +#endif +} + +inline bool bind_ip_address(socket_t sock, const std::string &host) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + auto ret = false; + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &ai = *rp; + if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + ret = true; + break; + } + } + + return ret; +} + +#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ +#define USE_IF2IP +#endif + +#ifdef USE_IF2IP +inline std::string if2ip(int address_family, const std::string &ifn) { + struct ifaddrs *ifap; + getifaddrs(&ifap); + auto se = detail::scope_exit([&] { freeifaddrs(ifap); }); + + std::string addr_candidate; + for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && ifn == ifa->ifa_name && + (AF_UNSPEC == address_family || + ifa->ifa_addr->sa_family == address_family)) { + if (ifa->ifa_addr->sa_family == AF_INET) { + auto sa = reinterpret_cast(ifa->ifa_addr); + char buf[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { + return std::string(buf, INET_ADDRSTRLEN); + } + } else if (ifa->ifa_addr->sa_family == AF_INET6) { + auto sa = reinterpret_cast(ifa->ifa_addr); + if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { + char buf[INET6_ADDRSTRLEN] = {}; + if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { + // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL + auto s6_addr_head = sa->sin6_addr.s6_addr[0]; + if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { + addr_candidate = std::string(buf, INET6_ADDRSTRLEN); + } else { + return std::string(buf, INET6_ADDRSTRLEN); + } + } + } + } + } + } + return addr_candidate; +} +#endif + +inline socket_t create_client_socket( + const std::string &host, const std::string &ip, int port, + int address_family, bool tcp_nodelay, bool ipv6_v6only, + SocketOptions socket_options, time_t connection_timeout_sec, + time_t connection_timeout_usec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, const std::string &intf, Error &error) { + auto sock = create_socket( + host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only, + std::move(socket_options), + [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { + if (!intf.empty()) { +#ifdef USE_IF2IP + auto ip_from_if = if2ip(address_family, intf); + if (ip_from_if.empty()) { ip_from_if = intf; } + if (!bind_ip_address(sock2, ip_from_if)) { + error = Error::BindIPAddress; + return false; + } +#endif + } + + set_nonblocking(sock2, true); + + auto ret = + ::connect(sock2, ai.ai_addr, static_cast(ai.ai_addrlen)); + + if (ret < 0) { + if (is_connection_error()) { + error = Error::Connection; + return false; + } + error = wait_until_socket_is_ready(sock2, connection_timeout_sec, + connection_timeout_usec); + if (error != Error::Success) { + if (error == Error::ConnectionTimeout) { quit = true; } + return false; + } + } + + set_nonblocking(sock2, false); + + { +#ifdef _WIN32 + auto timeout = static_cast(read_timeout_sec * 1000 + + read_timeout_usec / 1000); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec); + tv.tv_usec = static_cast(read_timeout_usec); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + { + +#ifdef _WIN32 + auto timeout = static_cast(write_timeout_sec * 1000 + + write_timeout_usec / 1000); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec); + tv.tv_usec = static_cast(write_timeout_usec); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + + error = Error::Success; + return true; + }); + + if (sock != INVALID_SOCKET) { + error = Error::Success; + } else { + if (error == Error::Success) { error = Error::Connection; } + } + + return sock; +} + +inline bool get_ip_and_port(const struct sockaddr_storage &addr, + socklen_t addr_len, std::string &ip, int &port) { + if (addr.ss_family == AF_INET) { + port = ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + port = + ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return false; + } + + std::array ipstr{}; + if (getnameinfo(reinterpret_cast(&addr), addr_len, + ipstr.data(), static_cast(ipstr.size()), nullptr, + 0, NI_NUMERICHOST)) { + return false; + } + + ip = ipstr.data(); + return true; +} + +inline void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (!getsockname(sock, reinterpret_cast(&addr), + &addr_len)) { + get_ip_and_port(addr, addr_len, ip, port); + } +} + +inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + + if (!getpeername(sock, reinterpret_cast(&addr), + &addr_len)) { +#ifndef _WIN32 + if (addr.ss_family == AF_UNIX) { +#if defined(__linux__) + struct ucred ucred; + socklen_t len = sizeof(ucred); + if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { + port = ucred.pid; + } +#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ + pid_t pid; + socklen_t len = sizeof(pid); + if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { + port = pid; + } +#endif + return; + } +#endif + get_ip_and_port(addr, addr_len, ip, port); + } +} + +inline constexpr unsigned int str2tag_core(const char *s, size_t l, + unsigned int h) { + return (l == 0) + ? h + : str2tag_core( + s + 1, l - 1, + // Unsets the 6 high bits of h, therefore no overflow happens + (((std::numeric_limits::max)() >> 6) & + h * 33) ^ + static_cast(*s)); +} + +inline unsigned int str2tag(const std::string &s) { + return str2tag_core(s.data(), s.size(), 0); +} + +namespace udl { + +inline constexpr unsigned int operator""_t(const char *s, size_t l) { + return str2tag_core(s, l, 0); +} + +} // namespace udl + +inline std::string +find_content_type(const std::string &path, + const std::map &user_data, + const std::string &default_content_type) { + auto ext = file_extension(path); + + auto it = user_data.find(ext); + if (it != user_data.end()) { return it->second; } + + using udl::operator""_t; + + switch (str2tag(ext)) { + default: return default_content_type; + + case "css"_t: return "text/css"; + case "csv"_t: return "text/csv"; + case "htm"_t: + case "html"_t: return "text/html"; + case "js"_t: + case "mjs"_t: return "text/javascript"; + case "txt"_t: return "text/plain"; + case "vtt"_t: return "text/vtt"; + + case "apng"_t: return "image/apng"; + case "avif"_t: return "image/avif"; + case "bmp"_t: return "image/bmp"; + case "gif"_t: return "image/gif"; + case "png"_t: return "image/png"; + case "svg"_t: return "image/svg+xml"; + case "webp"_t: return "image/webp"; + case "ico"_t: return "image/x-icon"; + case "tif"_t: return "image/tiff"; + case "tiff"_t: return "image/tiff"; + case "jpg"_t: + case "jpeg"_t: return "image/jpeg"; + + case "mp4"_t: return "video/mp4"; + case "mpeg"_t: return "video/mpeg"; + case "webm"_t: return "video/webm"; + + case "mp3"_t: return "audio/mp3"; + case "mpga"_t: return "audio/mpeg"; + case "weba"_t: return "audio/webm"; + case "wav"_t: return "audio/wave"; + + case "otf"_t: return "font/otf"; + case "ttf"_t: return "font/ttf"; + case "woff"_t: return "font/woff"; + case "woff2"_t: return "font/woff2"; + + case "7z"_t: return "application/x-7z-compressed"; + case "atom"_t: return "application/atom+xml"; + case "pdf"_t: return "application/pdf"; + case "json"_t: return "application/json"; + case "rss"_t: return "application/rss+xml"; + case "tar"_t: return "application/x-tar"; + case "xht"_t: + case "xhtml"_t: return "application/xhtml+xml"; + case "xslt"_t: return "application/xslt+xml"; + case "xml"_t: return "application/xml"; + case "gz"_t: return "application/gzip"; + case "zip"_t: return "application/zip"; + case "wasm"_t: return "application/wasm"; + } +} + +inline bool can_compress_content_type(const std::string &content_type) { + using udl::operator""_t; + + auto tag = str2tag(content_type); + + switch (tag) { + case "image/svg+xml"_t: + case "application/javascript"_t: + case "application/json"_t: + case "application/xml"_t: + case "application/protobuf"_t: + case "application/xhtml+xml"_t: return true; + + case "text/event-stream"_t: return false; + + default: return !content_type.rfind("text/", 0); + } +} + +inline EncodingType encoding_type(const Request &req, const Response &res) { + auto ret = + detail::can_compress_content_type(res.get_header_value("Content-Type")); + if (!ret) { return EncodingType::None; } + + const auto &s = req.get_header_value("Accept-Encoding"); + (void)(s); + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + // TODO: 'Accept-Encoding' has br, not br;q=0 + ret = s.find("br") != std::string::npos; + if (ret) { return EncodingType::Brotli; } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 + ret = s.find("gzip") != std::string::npos; + if (ret) { return EncodingType::Gzip; } +#endif + + return EncodingType::None; +} + +inline bool nocompressor::compress(const char *data, size_t data_length, + bool /*last*/, Callback callback) { + if (!data_length) { return true; } + return callback(data, data_length); +} + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +inline gzip_compressor::gzip_compressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, + Z_DEFAULT_STRATEGY) == Z_OK; +} + +inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } + +inline bool gzip_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + assert(is_valid_); + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; + auto ret = Z_OK; + + std::array buff{}; + do { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = deflate(&strm_, flush); + if (ret == Z_STREAM_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } while (strm_.avail_out == 0); + + assert((flush == Z_FINISH && ret == Z_STREAM_END) || + (flush == Z_NO_FLUSH && ret == Z_OK)); + assert(strm_.avail_in == 0); + } while (data_length > 0); + + return true; +} + +inline gzip_decompressor::gzip_decompressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + // 15 is the value of wbits, which should be at the maximum possible value + // to ensure that any gzip stream can be decoded. The offset of 32 specifies + // that the stream type should be automatically detected either gzip or + // deflate. + is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; +} + +inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } + +inline bool gzip_decompressor::is_valid() const { return is_valid_; } + +inline bool gzip_decompressor::decompress(const char *data, size_t data_length, + Callback callback) { + assert(is_valid_); + + auto ret = Z_OK; + + do { + constexpr size_t max_avail_in = + (std::numeric_limits::max)(); + + strm_.avail_in = static_cast( + (std::min)(data_length, max_avail_in)); + strm_.next_in = const_cast(reinterpret_cast(data)); + + data_length -= strm_.avail_in; + data += strm_.avail_in; + + std::array buff{}; + while (strm_.avail_in > 0 && ret == Z_OK) { + strm_.avail_out = static_cast(buff.size()); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = inflate(&strm_, Z_NO_FLUSH); + + assert(ret != Z_STREAM_ERROR); + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: inflateEnd(&strm_); return false; + } + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } + + if (ret != Z_OK && ret != Z_STREAM_END) { return false; } + + } while (data_length > 0); + + return true; +} +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +inline brotli_compressor::brotli_compressor() { + state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); +} + +inline brotli_compressor::~brotli_compressor() { + BrotliEncoderDestroyInstance(state_); +} + +inline bool brotli_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + std::array buff{}; + + auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; + auto available_in = data_length; + auto next_in = reinterpret_cast(data); + + for (;;) { + if (last) { + if (BrotliEncoderIsFinished(state_)) { break; } + } else { + if (!available_in) { break; } + } + + auto available_out = buff.size(); + auto next_out = buff.data(); + + if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in, + &available_out, &next_out, nullptr)) { + return false; + } + + auto output_bytes = buff.size() - available_out; + if (output_bytes) { + callback(reinterpret_cast(buff.data()), output_bytes); + } + } + + return true; +} + +inline brotli_decompressor::brotli_decompressor() { + decoder_s = BrotliDecoderCreateInstance(0, 0, 0); + decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT + : BROTLI_DECODER_RESULT_ERROR; +} + +inline brotli_decompressor::~brotli_decompressor() { + if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } +} + +inline bool brotli_decompressor::is_valid() const { return decoder_s; } + +inline bool brotli_decompressor::decompress(const char *data, + size_t data_length, + Callback callback) { + if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_ERROR) { + return 0; + } + + auto next_in = reinterpret_cast(data); + size_t avail_in = data_length; + size_t total_out; + + decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; + + std::array buff{}; + while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { + char *next_out = buff.data(); + size_t avail_out = buff.size(); + + decoder_r = BrotliDecoderDecompressStream( + decoder_s, &avail_in, &next_in, &avail_out, + reinterpret_cast(&next_out), &total_out); + + if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - avail_out)) { return false; } + } + + return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; +} +#endif + +inline bool has_header(const Headers &headers, const std::string &key) { + return headers.find(key) != headers.end(); +} + +inline const char *get_header_value(const Headers &headers, + const std::string &key, const char *def, + size_t id) { + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.c_str(); } + return def; +} + +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; + } + + auto p = beg; + while (p < end && *p != ':') { + p++; + } + + if (p == end) { return false; } + + auto key_end = p; + + if (*p++ != ':') { return false; } + + while (p < end && is_space_or_tab(*p)) { + p++; + } + + if (p <= end) { + auto key_len = key_end - beg; + if (!key_len) { return false; } + + auto key = std::string(beg, key_end); + // auto val = (case_ignore::equal(key, "Location") || + // case_ignore::equal(key, "Referer")) + // ? std::string(p, end) + // : decode_url(std::string(p, end), false); + auto val = std::string(p, end); + + if (!detail::fields::is_field_value(val)) { return false; } + + if (case_ignore::equal(key, "Location") || + case_ignore::equal(key, "Referer")) { + fn(key, val); + } else { + fn(key, decode_url(val, false)); + } + + return true; + } + + return false; +} + +inline bool read_headers(Stream &strm, Headers &headers) { + const auto bufsiz = 2048; + char buf[bufsiz]; + stream_line_reader line_reader(strm, buf, bufsiz); + + for (;;) { + if (!line_reader.getline()) { return false; } + + // Check if the line ends with CRLF. + auto line_terminator_len = 2; + if (line_reader.end_with_crlf()) { + // Blank line indicates end of headers. + if (line_reader.size() == 2) { break; } + } else { +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + // Blank line indicates end of headers. + if (line_reader.size() == 1) { break; } + line_terminator_len = 1; +#else + continue; // Skip invalid line. +#endif + } + + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Exclude line terminator + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + if (!parse_header(line_reader.ptr(), end, + [&](const std::string &key, const std::string &val) { + headers.emplace(key, val); + })) { + return false; + } + } + + return true; +} + +inline bool read_content_with_length(Stream &strm, uint64_t len, + Progress progress, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + + uint64_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return false; } + + if (!out(buf, static_cast(n), r, len)) { return false; } + r += static_cast(n); + + if (progress) { + if (!progress(r, len)) { return false; } + } + } + + return true; +} + +inline void skip_content_with_length(Stream &strm, uint64_t len) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + uint64_t r = 0; + while (r < len) { + auto read_len = static_cast(len - r); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + if (n <= 0) { return; } + r += static_cast(n); + } +} + +inline bool read_content_without_length(Stream &strm, + ContentReceiverWithProgress out) { + char buf[CPPHTTPLIB_RECV_BUFSIZ]; + uint64_t r = 0; + for (;;) { + auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); + if (n <= 0) { return false; } + + if (!out(buf, static_cast(n), r, 0)) { return false; } + r += static_cast(n); + } + + return true; +} + +template +inline bool read_content_chunked(Stream &strm, T &x, + ContentReceiverWithProgress out) { + const auto bufsiz = 16; + char buf[bufsiz]; + + stream_line_reader line_reader(strm, buf, bufsiz); + + if (!line_reader.getline()) { return false; } + + unsigned long chunk_len; + while (true) { + char *end_ptr; + + chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); + + if (end_ptr == line_reader.ptr()) { return false; } + if (chunk_len == ULONG_MAX) { return false; } + + if (chunk_len == 0) { break; } + + if (!read_content_with_length(strm, chunk_len, nullptr, out)) { + return false; + } + + if (!line_reader.getline()) { return false; } + + if (strcmp(line_reader.ptr(), "\r\n") != 0) { return false; } + + if (!line_reader.getline()) { return false; } + } + + assert(chunk_len == 0); + + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked + // transfer coding is complete when a chunk with a chunk-size of zero is + // received, possibly followed by a trailer section, and finally terminated by + // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section + // does't care for the existence of the final CRLF. In other words, it seems + // to be ok whether the final CRLF exists or not in the chunked data. + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // + // According to the reference code in RFC 9112, cpp-htpplib now allows + // chuncked transfer coding data without the final CRLF. + if (!line_reader.getline()) { return true; } + + while (strcmp(line_reader.ptr(), "\r\n") != 0) { + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + + // Exclude line terminator + constexpr auto line_terminator_len = 2; + auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; + + parse_header(line_reader.ptr(), end, + [&](const std::string &key, const std::string &val) { + x.headers.emplace(key, val); + }); + + if (!line_reader.getline()) { return false; } + } + + return true; +} + +inline bool is_chunked_transfer_encoding(const Headers &headers) { + return case_ignore::equal( + get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); +} + +template +bool prepare_content_receiver(T &x, int &status, + ContentReceiverWithProgress receiver, + bool decompress, U callback) { + if (decompress) { + std::string encoding = x.get_header_value("Content-Encoding"); + std::unique_ptr decompressor; + + if (encoding == "gzip" || encoding == "deflate") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } else if (encoding.find("br") != std::string::npos) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; +#endif + } + + if (decompressor) { + if (decompressor->is_valid()) { + ContentReceiverWithProgress out = [&](const char *buf, size_t n, + uint64_t off, uint64_t len) { + return decompressor->decompress(buf, n, + [&](const char *buf2, size_t n2) { + return receiver(buf2, n2, off, len); + }); + }; + return callback(std::move(out)); + } else { + status = StatusCode::InternalServerError_500; + return false; + } + } + } + + ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, + uint64_t len) { + return receiver(buf, n, off, len); + }; + return callback(std::move(out)); +} + +template +bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, + Progress progress, ContentReceiverWithProgress receiver, + bool decompress) { + return prepare_content_receiver( + x, status, std::move(receiver), decompress, + [&](const ContentReceiverWithProgress &out) { + auto ret = true; + auto exceed_payload_max_length = false; + + if (is_chunked_transfer_encoding(x.headers)) { + ret = read_content_chunked(strm, x, out); + } else if (!has_header(x.headers, "Content-Length")) { + ret = read_content_without_length(strm, out); + } else { + auto is_invalid_value = false; + auto len = get_header_value_u64( + x.headers, "Content-Length", + (std::numeric_limits::max)(), 0, is_invalid_value); + + if (is_invalid_value) { + ret = false; + } else if (len > payload_max_length) { + exceed_payload_max_length = true; + skip_content_with_length(strm, len); + ret = false; + } else if (len > 0) { + ret = read_content_with_length(strm, len, std::move(progress), out); + } + } + + if (!ret) { + status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413 + : StatusCode::BadRequest_400; + } + return ret; + }); +} + +inline ssize_t write_request_line(Stream &strm, const std::string &method, + const std::string &path) { + std::string s = method; + s += " "; + s += path; + s += " HTTP/1.1\r\n"; + return strm.write(s.data(), s.size()); +} + +inline ssize_t write_response_line(Stream &strm, int status) { + std::string s = "HTTP/1.1 "; + s += std::to_string(status); + s += " "; + s += httplib::status_message(status); + s += "\r\n"; + return strm.write(s.data(), s.size()); +} + +inline ssize_t write_headers(Stream &strm, const Headers &headers) { + ssize_t write_len = 0; + for (const auto &x : headers) { + std::string s; + s = x.first; + s += ": "; + s += x.second; + s += "\r\n"; + + auto len = strm.write(s.data(), s.size()); + if (len < 0) { return len; } + write_len += len; + } + auto len = strm.write("\r\n"); + if (len < 0) { return len; } + write_len += len; + return write_len; +} + +inline bool write_data(Stream &strm, const char *d, size_t l) { + size_t offset = 0; + while (offset < l) { + auto length = strm.write(d + offset, l - offset); + if (length < 0) { return false; } + offset += static_cast(length); + } + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, T is_shutting_down, + Error &error) { + size_t end_offset = offset + length; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + if (strm.is_writable() && write_data(strm, d, l)) { + offset += l; + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + + while (offset < end_offset && !is_shutting_down()) { + if (!strm.is_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, end_offset - offset, data_sink)) { + error = Error::Canceled; + return false; + } else if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, + const T &is_shutting_down) { + auto error = Error::Success; + return write_content(strm, content_provider, offset, length, is_shutting_down, + error); +} + +template +inline bool +write_content_without_length(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + offset += l; + if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + + data_sink.done = [&](void) { data_available = false; }; + + while (data_available && !is_shutting_down()) { + if (!strm.is_writable()) { + return false; + } else if (!content_provider(offset, 0, data_sink)) { + return false; + } else if (!ok) { + return false; + } + } + return true; +} + +template +inline bool +write_content_chunked(Stream &strm, const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor, Error &error) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) -> bool { + if (ok) { + data_available = l > 0; + offset += l; + + std::string payload; + if (compressor.compress(d, l, false, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = + from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!strm.is_writable() || + !write_data(strm, chunk.data(), chunk.size())) { + ok = false; + } + } + } else { + ok = false; + } + } + return ok; + }; + + data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; + + auto done_with_trailer = [&](const Headers *trailer) { + if (!ok) { return; } + + data_available = false; + + std::string payload; + if (!compressor.compress(nullptr, 0, true, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + ok = false; + return; + } + + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (!strm.is_writable() || + !write_data(strm, chunk.data(), chunk.size())) { + ok = false; + return; + } + } + + static const std::string done_marker("0\r\n"); + if (!write_data(strm, done_marker.data(), done_marker.size())) { + ok = false; + } + + // Trailer + if (trailer) { + for (const auto &kv : *trailer) { + std::string field_line = kv.first + ": " + kv.second + "\r\n"; + if (!write_data(strm, field_line.data(), field_line.size())) { + ok = false; + } + } + } + + static const std::string crlf("\r\n"); + if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; } + }; + + data_sink.done = [&](void) { done_with_trailer(nullptr); }; + + data_sink.done_with_trailer = [&](const Headers &trailer) { + done_with_trailer(&trailer); + }; + + while (data_available && !is_shutting_down()) { + if (!strm.is_writable()) { + error = Error::Write; + return false; + } else if (!content_provider(offset, 0, data_sink)) { + error = Error::Canceled; + return false; + } else if (!ok) { + error = Error::Write; + return false; + } + } + + error = Error::Success; + return true; +} + +template +inline bool write_content_chunked(Stream &strm, + const ContentProvider &content_provider, + const T &is_shutting_down, U &compressor) { + auto error = Error::Success; + return write_content_chunked(strm, content_provider, is_shutting_down, + compressor, error); +} + +template +inline bool redirect(T &cli, Request &req, Response &res, + const std::string &path, const std::string &location, + Error &error) { + Request new_req = req; + new_req.path = path; + new_req.redirect_count_ -= 1; + + if (res.status == StatusCode::SeeOther_303 && + (req.method != "GET" && req.method != "HEAD")) { + new_req.method = "GET"; + new_req.body.clear(); + new_req.headers.clear(); + } + + Response new_res; + + auto ret = cli.send(new_req, new_res, error); + if (ret) { + req = new_req; + res = new_res; + + if (res.location.empty()) { res.location = location; } + } + return ret; +} + +inline std::string params_to_query_str(const Params ¶ms) { + std::string query; + + for (auto it = params.begin(); it != params.end(); ++it) { + if (it != params.begin()) { query += "&"; } + query += it->first; + query += "="; + query += encode_query_param(it->second); + } + return query; +} + +inline void parse_query_text(const char *data, std::size_t size, + Params ¶ms) { + std::set cache; + split(data, data + size, '&', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(std::move(kv)); + + std::string key; + std::string val; + divide(b, static_cast(e - b), '=', + [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, + std::size_t rhs_size) { + key.assign(lhs_data, lhs_size); + val.assign(rhs_data, rhs_size); + }); + + if (!key.empty()) { + params.emplace(decode_url(key, true), decode_url(val, true)); + } + }); +} + +inline void parse_query_text(const std::string &s, Params ¶ms) { + parse_query_text(s.data(), s.size(), params); +} + +inline bool parse_multipart_boundary(const std::string &content_type, + std::string &boundary) { + auto boundary_keyword = "boundary="; + auto pos = content_type.find(boundary_keyword); + if (pos == std::string::npos) { return false; } + auto end = content_type.find(';', pos); + auto beg = pos + strlen(boundary_keyword); + boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg)); + return !boundary.empty(); +} + +inline void parse_disposition_params(const std::string &s, Params ¶ms) { + std::set cache; + split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) { + std::string kv(b, e); + if (cache.find(kv) != cache.end()) { return; } + cache.insert(kv); + + std::string key; + std::string val; + split(b, e, '=', [&](const char *b2, const char *e2) { + if (key.empty()) { + key.assign(b2, e2); + } else { + val.assign(b2, e2); + } + }); + + if (!key.empty()) { + params.emplace(trim_double_quotes_copy((key)), + trim_double_quotes_copy((val))); + } + }); +} + +#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 + auto is_valid = [](const std::string &str) { + return std::all_of(str.cbegin(), str.cend(), + [](unsigned char c) { return std::isdigit(c); }); + }; + + if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) { + const auto pos = static_cast(6); + const auto len = static_cast(s.size() - 6); + auto all_valid_ranges = true; + split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { + if (!all_valid_ranges) { return; } + + const auto it = std::find(b, e, '-'); + if (it == e) { + all_valid_ranges = false; + return; + } + + const auto lhs = std::string(b, it); + const auto rhs = std::string(it + 1, e); + if (!is_valid(lhs) || !is_valid(rhs)) { + all_valid_ranges = false; + return; + } + + const auto first = + static_cast(lhs.empty() ? -1 : std::stoll(lhs)); + const auto last = + static_cast(rhs.empty() ? -1 : std::stoll(rhs)); + if ((first == -1 && last == -1) || + (first != -1 && last != -1 && first > last)) { + all_valid_ranges = false; + return; + } + + ranges.emplace_back(first, last); + }); + return all_valid_ranges && !ranges.empty(); + } + return false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +} +#else +} catch (...) { return false; } +#endif + +class MultipartFormDataParser { +public: + MultipartFormDataParser() = default; + + void set_boundary(std::string &&boundary) { + boundary_ = boundary; + dash_boundary_crlf_ = dash_ + boundary_ + crlf_; + crlf_dash_boundary_ = crlf_ + dash_ + boundary_; + } + + bool is_valid() const { return is_valid_; } + + bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, + const MultipartContentHeader &header_callback) { + + buf_append(buf, n); + + while (buf_size() > 0) { + switch (state_) { + case 0: { // Initial boundary + buf_erase(buf_find(dash_boundary_crlf_)); + if (dash_boundary_crlf_.size() > buf_size()) { return true; } + if (!buf_start_with(dash_boundary_crlf_)) { return false; } + buf_erase(dash_boundary_crlf_.size()); + state_ = 1; + break; + } + case 1: { // New entry + clear_file_info(); + state_ = 2; + break; + } + case 2: { // Headers + auto pos = buf_find(crlf_); + if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + while (pos < buf_size()) { + // Empty line + if (pos == 0) { + if (!header_callback(file_)) { + is_valid_ = false; + return false; + } + buf_erase(crlf_.size()); + state_ = 3; + break; + } + + const auto header = buf_head(pos); + + if (!parse_header(header.data(), header.data() + header.size(), + [&](const std::string &, const std::string &) {})) { + is_valid_ = false; + return false; + } + + static const std::string header_content_type = "Content-Type:"; + + if (start_with_case_ignore(header, header_content_type)) { + file_.content_type = + trim_copy(header.substr(header_content_type.size())); + } else { + static const std::regex re_content_disposition( + R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", + std::regex_constants::icase); + + std::smatch m; + if (std::regex_match(header, m, re_content_disposition)) { + Params params; + parse_disposition_params(m[1], params); + + auto it = params.find("name"); + if (it != params.end()) { + file_.name = it->second; + } else { + is_valid_ = false; + return false; + } + + it = params.find("filename"); + if (it != params.end()) { file_.filename = it->second; } + + it = params.find("filename*"); + if (it != params.end()) { + // Only allow UTF-8 enconnding... + static const std::regex re_rfc5987_encoding( + R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); + + std::smatch m2; + if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { + file_.filename = decode_url(m2[1], false); // override... + } else { + is_valid_ = false; + return false; + } + } + } + } + buf_erase(pos + crlf_.size()); + pos = buf_find(crlf_); + } + if (state_ != 3) { return true; } + break; + } + case 3: { // Body + if (crlf_dash_boundary_.size() > buf_size()) { return true; } + auto pos = buf_find(crlf_dash_boundary_); + if (pos < buf_size()) { + if (!content_callback(buf_data(), pos)) { + is_valid_ = false; + return false; + } + buf_erase(pos + crlf_dash_boundary_.size()); + state_ = 4; + } else { + auto len = buf_size() - crlf_dash_boundary_.size(); + if (len > 0) { + if (!content_callback(buf_data(), len)) { + is_valid_ = false; + return false; + } + buf_erase(len); + } + return true; + } + break; + } + case 4: { // Boundary + if (crlf_.size() > buf_size()) { return true; } + if (buf_start_with(crlf_)) { + buf_erase(crlf_.size()); + state_ = 1; + } else { + if (dash_.size() > buf_size()) { return true; } + if (buf_start_with(dash_)) { + buf_erase(dash_.size()); + is_valid_ = true; + buf_erase(buf_size()); // Remove epilogue + } else { + return true; + } + } + break; + } + } + } + + return true; + } + +private: + void clear_file_info() { + file_.name.clear(); + file_.filename.clear(); + file_.content_type.clear(); + } + + bool start_with_case_ignore(const std::string &a, + const std::string &b) const { + if (a.size() < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { + return false; + } + } + return true; + } + + const std::string dash_ = "--"; + const std::string crlf_ = "\r\n"; + std::string boundary_; + std::string dash_boundary_crlf_; + std::string crlf_dash_boundary_; + + size_t state_ = 0; + bool is_valid_ = false; + MultipartFormData file_; + + // Buffer + bool start_with(const std::string &a, size_t spos, size_t epos, + const std::string &b) const { + if (epos - spos < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (a[i + spos] != b[i]) { return false; } + } + return true; + } + + size_t buf_size() const { return buf_epos_ - buf_spos_; } + + const char *buf_data() const { return &buf_[buf_spos_]; } + + std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } + + bool buf_start_with(const std::string &s) const { + return start_with(buf_, buf_spos_, buf_epos_, s); + } + + size_t buf_find(const std::string &s) const { + auto c = s.front(); + + size_t off = buf_spos_; + while (off < buf_epos_) { + auto pos = off; + while (true) { + if (pos == buf_epos_) { return buf_size(); } + if (buf_[pos] == c) { break; } + pos++; + } + + auto remaining_size = buf_epos_ - pos; + if (s.size() > remaining_size) { return buf_size(); } + + if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } + + off = pos + 1; + } + + return buf_size(); + } + + void buf_append(const char *data, size_t n) { + auto remaining_size = buf_size(); + if (remaining_size > 0 && buf_spos_ > 0) { + for (size_t i = 0; i < remaining_size; i++) { + buf_[i] = buf_[buf_spos_ + i]; + } + } + buf_spos_ = 0; + buf_epos_ = remaining_size; + + if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } + + for (size_t i = 0; i < n; i++) { + buf_[buf_epos_ + i] = data[i]; + } + buf_epos_ += n; + } + + void buf_erase(size_t size) { buf_spos_ += size; } + + std::string buf_; + size_t buf_spos_ = 0; + size_t buf_epos_ = 0; +}; + +inline std::string random_string(size_t length) { + static const char data[] = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + // std::random_device might actually be deterministic on some + // platforms, but due to lack of support in the c++ standard library, + // doing better requires either some ugly hacks or breaking portability. + static std::random_device seed_gen; + + // Request 128 bits of entropy for initialization + static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), + seed_gen()}; + + static std::mt19937 engine(seed_sequence); + + std::string result; + for (size_t i = 0; i < length; i++) { + result += data[engine() % (sizeof(data) - 1)]; + } + return result; +} + +inline std::string make_multipart_data_boundary() { + return "--cpp-httplib-multipart-data-" + detail::random_string(16); +} + +inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { + auto valid = true; + for (size_t i = 0; i < boundary.size(); i++) { + auto c = boundary[i]; + if (!std::isalnum(c) && c != '-' && c != '_') { + valid = false; + break; + } + } + return valid; +} + +template +inline std::string +serialize_multipart_formdata_item_begin(const T &item, + const std::string &boundary) { + std::string body = "--" + boundary + "\r\n"; + body += "Content-Disposition: form-data; name=\"" + item.name + "\""; + if (!item.filename.empty()) { + body += "; filename=\"" + item.filename + "\""; + } + body += "\r\n"; + if (!item.content_type.empty()) { + body += "Content-Type: " + item.content_type + "\r\n"; + } + body += "\r\n"; + + return body; +} + +inline std::string serialize_multipart_formdata_item_end() { return "\r\n"; } + +inline std::string +serialize_multipart_formdata_finish(const std::string &boundary) { + return "--" + boundary + "--\r\n"; +} + +inline std::string +serialize_multipart_formdata_get_content_type(const std::string &boundary) { + return "multipart/form-data; boundary=" + boundary; +} + +inline std::string +serialize_multipart_formdata(const MultipartFormDataItems &items, + const std::string &boundary, bool finish = true) { + std::string body; + + for (const auto &item : items) { + body += serialize_multipart_formdata_item_begin(item, boundary); + body += item.content + serialize_multipart_formdata_item_end(); + } + + if (finish) { body += serialize_multipart_formdata_finish(boundary); } + + return body; +} + +inline bool range_error(Request &req, Response &res) { + if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { + ssize_t contant_len = static_cast( + res.content_length_ ? res.content_length_ : res.body.size()); + + ssize_t prev_first_pos = -1; + ssize_t prev_last_pos = -1; + size_t overwrapping_count = 0; + + // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 + // 'HTTP Semantics' to avoid potential denial-of-service attacks. + // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 + + // Too many ranges + if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; } + + for (auto &r : req.ranges) { + auto &first_pos = r.first; + auto &last_pos = r.second; + + if (first_pos == -1 && last_pos == -1) { + first_pos = 0; + last_pos = contant_len; + } + + if (first_pos == -1) { + first_pos = contant_len - last_pos; + last_pos = contant_len - 1; + } + + // NOTE: RFC-9110 '14.1.2. Byte Ranges': + // A client can limit the number of bytes requested without knowing the + // size of the selected representation. If the last-pos value is absent, + // or if the value is greater than or equal to the current length of the + // representation data, the byte range is interpreted as the remainder of + // the representation (i.e., the server replaces the value of last-pos + // with a value that is one less than the current length of the selected + // representation). + // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 + if (last_pos == -1 || last_pos >= contant_len) { + last_pos = contant_len - 1; + } + + // Range must be within content length + if (!(0 <= first_pos && first_pos <= last_pos && + last_pos <= contant_len - 1)) { + return true; + } + + // Ranges must be in ascending order + if (first_pos <= prev_first_pos) { return true; } + + // Request must not have more than two overlapping ranges + if (first_pos <= prev_last_pos) { + overwrapping_count++; + if (overwrapping_count > 2) { return true; } + } + + prev_first_pos = (std::max)(prev_first_pos, first_pos); + prev_last_pos = (std::max)(prev_last_pos, last_pos); + } + } + + return false; +} + +inline std::pair +get_range_offset_and_length(Range r, size_t content_length) { + assert(r.first != -1 && r.second != -1); + assert(0 <= r.first && r.first < static_cast(content_length)); + assert(r.first <= r.second && + r.second < static_cast(content_length)); + (void)(content_length); + return std::make_pair(r.first, static_cast(r.second - r.first) + 1); +} + +inline std::string make_content_range_header_field( + const std::pair &offset_and_length, size_t content_length) { + auto st = offset_and_length.first; + auto ed = st + offset_and_length.second - 1; + + std::string field = "bytes "; + field += std::to_string(st); + field += "-"; + field += std::to_string(ed); + field += "/"; + field += std::to_string(content_length); + return field; +} + +template +bool process_multipart_ranges_data(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length, SToken stoken, + CToken ctoken, Content content) { + for (size_t i = 0; i < req.ranges.size(); i++) { + ctoken("--"); + stoken(boundary); + ctoken("\r\n"); + if (!content_type.empty()) { + ctoken("Content-Type: "); + stoken(content_type); + ctoken("\r\n"); + } + + auto offset_and_length = + get_range_offset_and_length(req.ranges[i], content_length); + + ctoken("Content-Range: "); + stoken(make_content_range_header_field(offset_and_length, content_length)); + ctoken("\r\n"); + ctoken("\r\n"); + + if (!content(offset_and_length.first, offset_and_length.second)) { + return false; + } + ctoken("\r\n"); + } + + ctoken("--"); + stoken(boundary); + ctoken("--"); + + return true; +} + +inline void make_multipart_ranges_data(const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, + std::string &data) { + process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { data += token; }, + [&](const std::string &token) { data += token; }, + [&](size_t offset, size_t length) { + assert(offset + length <= content_length); + data += res.body.substr(offset, length); + return true; + }); +} + +inline size_t get_multipart_ranges_data_length(const Request &req, + const std::string &boundary, + const std::string &content_type, + size_t content_length) { + size_t data_length = 0; + + process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { data_length += token.size(); }, + [&](const std::string &token) { data_length += token.size(); }, + [&](size_t /*offset*/, size_t length) { + data_length += length; + return true; + }); + + return data_length; +} + +template +inline bool +write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, + const std::string &boundary, + const std::string &content_type, + size_t content_length, const T &is_shutting_down) { + return process_multipart_ranges_data( + req, boundary, content_type, content_length, + [&](const std::string &token) { strm.write(token); }, + [&](const std::string &token) { strm.write(token); }, + [&](size_t offset, size_t length) { + return write_content(strm, res.content_provider_, offset, length, + is_shutting_down); + }); +} + +inline bool expect_content(const Request &req) { + if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || + req.method == "DELETE") { + return true; + } + if (req.has_header("Content-Length") && + req.get_header_value_u64("Content-Length") > 0) { + return true; + } + if (is_chunked_transfer_encoding(req.headers)) { return true; } + return false; +} + +inline bool has_crlf(const std::string &s) { + auto p = s.c_str(); + while (*p) { + if (*p == '\r' || *p == '\n') { return true; } + p++; + } + return false; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::string message_digest(const std::string &s, const EVP_MD *algo) { + auto context = std::unique_ptr( + EVP_MD_CTX_new(), EVP_MD_CTX_free); + + unsigned int hash_length = 0; + unsigned char hash[EVP_MAX_MD_SIZE]; + + EVP_DigestInit_ex(context.get(), algo, nullptr); + EVP_DigestUpdate(context.get(), s.c_str(), s.size()); + EVP_DigestFinal_ex(context.get(), hash, &hash_length); + + std::stringstream ss; + for (auto i = 0u; i < hash_length; ++i) { + ss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(hash[i]); + } + + return ss.str(); +} + +inline std::string MD5(const std::string &s) { + return message_digest(s, EVP_md5()); +} + +inline std::string SHA_256(const std::string &s) { + return message_digest(s, EVP_sha256()); +} + +inline std::string SHA_512(const std::string &s) { + return message_digest(s, EVP_sha512()); +} + +inline std::pair make_digest_authentication_header( + const Request &req, const std::map &auth, + size_t cnonce_count, const std::string &cnonce, const std::string &username, + const std::string &password, bool is_proxy = false) { + std::string nc; + { + std::stringstream ss; + ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; + nc = ss.str(); + } + + std::string qop; + if (auth.find("qop") != auth.end()) { + qop = auth.at("qop"); + if (qop.find("auth-int") != std::string::npos) { + qop = "auth-int"; + } else if (qop.find("auth") != std::string::npos) { + qop = "auth"; + } else { + qop.clear(); + } + } + + std::string algo = "MD5"; + if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } + + std::string response; + { + auto H = algo == "SHA-256" ? detail::SHA_256 + : algo == "SHA-512" ? detail::SHA_512 + : detail::MD5; + + auto A1 = username + ":" + auth.at("realm") + ":" + password; + + auto A2 = req.method + ":" + req.path; + if (qop == "auth-int") { A2 += ":" + H(req.body); } + + if (qop.empty()) { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); + } else { + response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + + ":" + qop + ":" + H(A2)); + } + } + + auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; + + auto field = "Digest username=\"" + username + "\", realm=\"" + + auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + + "\", uri=\"" + req.path + "\", algorithm=" + algo + + (qop.empty() ? ", response=\"" + : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + + cnonce + "\", response=\"") + + response + "\"" + + (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); + + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} + +inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { + detail::set_nonblocking(sock, true); + auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); + + char buf[1]; + return !SSL_peek(ssl, buf, 1) && + SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; +} + +#ifdef _WIN32 +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store +inline bool load_system_certs_on_windows(X509_STORE *store) { + auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); + if (!hStore) { return false; } + + auto result = false; + PCCERT_CONTEXT pContext = NULL; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + auto encoded_cert = + static_cast(pContext->pbCertEncoded); + + auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + CertFreeCertificateContext(pContext); + CertCloseStore(hStore, 0); + + return result; +} +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#if TARGET_OS_OSX +template +using CFObjectPtr = + std::unique_ptr::type, void (*)(CFTypeRef)>; + +inline void cf_object_ptr_deleter(CFTypeRef obj) { + if (obj) { CFRelease(obj); } +} + +inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { + CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; + CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, + kCFBooleanTrue}; + + CFObjectPtr query( + CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, + sizeof(keys) / sizeof(keys[0]), + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks), + cf_object_ptr_deleter); + + if (!query) { return false; } + + CFTypeRef security_items = nullptr; + if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || + CFArrayGetTypeID() != CFGetTypeID(security_items)) { + return false; + } + + certs.reset(reinterpret_cast(security_items)); + return true; +} + +inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { + CFArrayRef root_security_items = nullptr; + if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { + return false; + } + + certs.reset(root_security_items); + return true; +} + +inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { + auto result = false; + for (auto i = 0; i < CFArrayGetCount(certs); ++i) { + const auto cert = reinterpret_cast( + CFArrayGetValueAtIndex(certs, i)); + + if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } + + CFDataRef cert_data = nullptr; + if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != + errSecSuccess) { + continue; + } + + CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); + + auto encoded_cert = static_cast( + CFDataGetBytePtr(cert_data_ptr.get())); + + auto x509 = + d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); + + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + result = true; + } + } + + return result; +} + +inline bool load_system_certs_on_macos(X509_STORE *store) { + auto result = false; + CFObjectPtr certs(nullptr, cf_object_ptr_deleter); + if (retrieve_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store); + } + + if (retrieve_root_certs_from_keychain(certs) && certs) { + result = add_certs_to_x509_store(certs.get(), store) || result; + } + + return result; +} +#endif // TARGET_OS_OSX +#endif // _WIN32 +#endif // CPPHTTPLIB_OPENSSL_SUPPORT + +#ifdef _WIN32 +class WSInit { +public: + WSInit() { + WSADATA wsaData; + if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; + } + + ~WSInit() { + if (is_valid_) WSACleanup(); + } + + bool is_valid_ = false; +}; + +static WSInit wsinit_; +#endif + +inline bool parse_www_authenticate(const Response &res, + std::map &auth, + bool is_proxy) { + auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; + if (res.has_header(auth_key)) { + static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + auto s = res.get_header_value(auth_key); + auto pos = s.find(' '); + if (pos != std::string::npos) { + auto type = s.substr(0, pos); + if (type == "Basic") { + return false; + } else if (type == "Digest") { + s = s.substr(pos + 1); + auto beg = std::sregex_iterator(s.begin(), s.end(), re); + for (auto i = beg; i != std::sregex_iterator(); ++i) { + const auto &m = *i; + auto key = s.substr(static_cast(m.position(1)), + static_cast(m.length(1))); + auto val = m.length(2) > 0 + ? s.substr(static_cast(m.position(2)), + static_cast(m.length(2))) + : s.substr(static_cast(m.position(3)), + static_cast(m.length(3))); + auth[key] = val; + } + return true; + } + } + } + return false; +} + +class ContentProviderAdapter { +public: + explicit ContentProviderAdapter( + ContentProviderWithoutLength &&content_provider) + : content_provider_(content_provider) {} + + bool operator()(size_t offset, size_t, DataSink &sink) { + return content_provider_(offset, sink); + } + +private: + ContentProviderWithoutLength content_provider_; +}; + +} // namespace detail + +inline std::string hosted_at(const std::string &hostname) { + std::vector addrs; + hosted_at(hostname, addrs); + if (addrs.empty()) { return std::string(); } + return addrs[0]; +} + +inline void hosted_at(const std::string &hostname, + std::vector &addrs) { + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + + if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return; + } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + for (auto rp = result; rp; rp = rp->ai_next) { + const auto &addr = + *reinterpret_cast(rp->ai_addr); + std::string ip; + auto dummy = -1; + if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, + dummy)) { + addrs.push_back(ip); + } + } +} + +inline std::string append_query_params(const std::string &path, + const Params ¶ms) { + std::string path_with_query = path; + const static std::regex re("[^?]+\\?.*"); + auto delm = std::regex_match(path, re) ? '&' : '?'; + path_with_query += delm + detail::params_to_query_str(params); + return path_with_query; +} + +// Header utilities +inline std::pair +make_range_header(const Ranges &ranges) { + std::string field = "bytes="; + auto i = 0; + for (const auto &r : ranges) { + if (i != 0) { field += ", "; } + if (r.first != -1) { field += std::to_string(r.first); } + field += '-'; + if (r.second != -1) { field += std::to_string(r.second); } + i++; + } + return std::make_pair("Range", std::move(field)); +} + +inline std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, bool is_proxy) { + auto field = "Basic " + detail::base64_encode(username + ":" + password); + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +inline std::pair +make_bearer_token_authentication_header(const std::string &token, + bool is_proxy = false) { + auto field = "Bearer " + token; + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, std::move(field)); +} + +// Request implementation +inline bool Request::has_header(const std::string &key) const { + return detail::has_header(headers, key); +} + +inline std::string Request::get_header_value(const std::string &key, + const char *def, size_t id) const { + return detail::get_header_value(headers, key, def, id); +} + +inline size_t Request::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Request::set_header(const std::string &key, + const std::string &val) { + if (detail::fields::is_field_name(key) && + detail::fields::is_field_value(val)) { + headers.emplace(key, val); + } +} + +inline bool Request::has_param(const std::string &key) const { + return params.find(key) != params.end(); +} + +inline std::string Request::get_param_value(const std::string &key, + size_t id) const { + auto rng = params.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Request::get_param_value_count(const std::string &key) const { + auto r = params.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline bool Request::is_multipart_form_data() const { + const auto &content_type = get_header_value("Content-Type"); + return !content_type.rfind("multipart/form-data", 0); +} + +inline bool Request::has_file(const std::string &key) const { + return files.find(key) != files.end(); +} + +inline MultipartFormData Request::get_file_value(const std::string &key) const { + auto it = files.find(key); + if (it != files.end()) { return it->second; } + return MultipartFormData(); +} + +inline std::vector +Request::get_file_values(const std::string &key) const { + std::vector values; + auto rng = files.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second); + } + return values; +} + +// Response implementation +inline bool Response::has_header(const std::string &key) const { + return headers.find(key) != headers.end(); +} + +inline std::string Response::get_header_value(const std::string &key, + const char *def, + size_t id) const { + return detail::get_header_value(headers, key, def, id); +} + +inline size_t Response::get_header_value_count(const std::string &key) const { + auto r = headers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline void Response::set_header(const std::string &key, + const std::string &val) { + if (detail::fields::is_field_name(key) && + detail::fields::is_field_value(val)) { + headers.emplace(key, val); + } +} + +inline void Response::set_redirect(const std::string &url, int stat) { + if (detail::fields::is_field_value(url)) { + set_header("Location", url); + if (300 <= stat && stat < 400) { + this->status = stat; + } else { + this->status = StatusCode::Found_302; + } + } +} + +inline void Response::set_content(const char *s, size_t n, + const std::string &content_type) { + body.assign(s, n); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content(const std::string &s, + const std::string &content_type) { + set_content(s.data(), s.size(), content_type); +} + +inline void Response::set_content(std::string &&s, + const std::string &content_type) { + body = std::move(s); + + auto rng = headers.equal_range("Content-Type"); + headers.erase(rng.first, rng.second); + set_header("Content-Type", content_type); +} + +inline void Response::set_content_provider( + size_t in_length, const std::string &content_type, ContentProvider provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = in_length; + if (in_length > 0) { content_provider_ = std::move(provider); } + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = false; +} + +inline void Response::set_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = false; +} + +inline void Response::set_chunked_content_provider( + const std::string &content_type, ContentProviderWithoutLength provider, + ContentProviderResourceReleaser resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = std::move(resource_releaser); + is_chunked_content_provider_ = true; +} + +inline void Response::set_file_content(const std::string &path, + const std::string &content_type) { + file_content_path_ = path; + file_content_content_type_ = content_type; +} + +inline void Response::set_file_content(const std::string &path) { + file_content_path_ = path; +} + +// Result implementation +inline bool Result::has_request_header(const std::string &key) const { + return request_headers_.find(key) != request_headers_.end(); +} + +inline std::string Result::get_request_header_value(const std::string &key, + const char *def, + size_t id) const { + return detail::get_header_value(request_headers_, key, def, id); +} + +inline size_t +Result::get_request_header_value_count(const std::string &key) const { + auto r = request_headers_.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +// Stream implementation +inline ssize_t Stream::write(const char *ptr) { + return write(ptr, strlen(ptr)); +} + +inline ssize_t Stream::write(const std::string &s) { + return write(s.data(), s.size()); +} + +namespace detail { + +inline void calc_actual_timeout(time_t max_timeout_msec, + time_t duration_msec, time_t timeout_sec, + time_t timeout_usec, time_t &actual_timeout_sec, + time_t &actual_timeout_usec) { + auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); + + auto actual_timeout_msec = + std::min(max_timeout_msec - duration_msec, timeout_msec); + + actual_timeout_sec = actual_timeout_msec / 1000; + actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; +} + +// Socket stream implementation +inline SocketStream::SocketStream( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time) + : sock_(sock), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), + max_timeout_msec_(max_timeout_msec), start_time(start_time), + read_buff_(read_buff_size_, 0) {} + +inline SocketStream::~SocketStream() = default; + +inline bool SocketStream::is_readable() const { + if (max_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } + + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); + + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; +} + +inline bool SocketStream::is_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_); +} + +inline ssize_t SocketStream::read(char *ptr, size_t size) { +#ifdef _WIN32 + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#else + size = (std::min)(size, + static_cast((std::numeric_limits::max)())); +#endif + + if (read_buff_off_ < read_buff_content_size_) { + auto remaining_size = read_buff_content_size_ - read_buff_off_; + if (size <= remaining_size) { + memcpy(ptr, read_buff_.data() + read_buff_off_, size); + read_buff_off_ += size; + return static_cast(size); + } else { + memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); + read_buff_off_ += remaining_size; + return static_cast(remaining_size); + } + } + + if (!is_readable()) { return -1; } + + read_buff_off_ = 0; + read_buff_content_size_ = 0; + + if (size < read_buff_size_) { + auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, + CPPHTTPLIB_RECV_FLAGS); + if (n <= 0) { + return n; + } else if (n <= static_cast(size)) { + memcpy(ptr, read_buff_.data(), static_cast(n)); + return n; + } else { + memcpy(ptr, read_buff_.data(), size); + read_buff_off_ = size; + read_buff_content_size_ = static_cast(n); + return static_cast(size); + } + } else { + return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); + } +} + +inline ssize_t SocketStream::write(const char *ptr, size_t size) { + if (!is_writable()) { return -1; } + +#if defined(_WIN32) && !defined(_WIN64) + size = + (std::min)(size, static_cast((std::numeric_limits::max)())); +#endif + + return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); +} + +inline void SocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + return detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + return detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SocketStream::socket() const { return sock_; } + +inline time_t SocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time) + .count(); +} + +// Buffer stream implementation +inline bool BufferStream::is_readable() const { return true; } + +inline bool BufferStream::is_writable() const { return true; } + +inline ssize_t BufferStream::read(char *ptr, size_t size) { +#if defined(_MSC_VER) && _MSC_VER < 1910 + auto len_read = buffer._Copy_s(ptr, size, size, position); +#else + auto len_read = buffer.copy(ptr, size, position); +#endif + position += static_cast(len_read); + return static_cast(len_read); +} + +inline ssize_t BufferStream::write(const char *ptr, size_t size) { + buffer.append(ptr, size); + return static_cast(size); +} + +inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} + +inline socket_t BufferStream::socket() const { return 0; } + +inline time_t BufferStream::duration() const { return 0; } + +inline const std::string &BufferStream::get_buffer() const { return buffer; } + +inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { + static constexpr char marker[] = "/:"; + + // One past the last ending position of a path param substring + std::size_t last_param_end = 0; + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + // Needed to ensure that parameter names are unique during matcher + // construction + // If exceptions are disabled, only last duplicate path + // parameter will be set + std::unordered_set param_name_set; +#endif + + while (true) { + const auto marker_pos = pattern.find( + marker, last_param_end == 0 ? last_param_end : last_param_end - 1); + if (marker_pos == std::string::npos) { break; } + + static_fragments_.push_back( + pattern.substr(last_param_end, marker_pos - last_param_end + 1)); + + const auto param_name_start = marker_pos + 2; + + auto sep_pos = pattern.find(separator, param_name_start); + if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } + + auto param_name = + pattern.substr(param_name_start, sep_pos - param_name_start); + +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + if (param_name_set.find(param_name) != param_name_set.cend()) { + std::string msg = "Encountered path parameter '" + param_name + + "' multiple times in route pattern '" + pattern + "'."; + throw std::invalid_argument(msg); + } +#endif + + param_names_.push_back(std::move(param_name)); + + last_param_end = sep_pos + 1; + } + + if (last_param_end < pattern.length()) { + static_fragments_.push_back(pattern.substr(last_param_end)); + } +} + +inline bool PathParamsMatcher::match(Request &request) const { + request.matches = std::smatch(); + request.path_params.clear(); + request.path_params.reserve(param_names_.size()); + + // One past the position at which the path matched the pattern last time + std::size_t starting_pos = 0; + for (size_t i = 0; i < static_fragments_.size(); ++i) { + const auto &fragment = static_fragments_[i]; + + if (starting_pos + fragment.length() > request.path.length()) { + return false; + } + + // Avoid unnecessary allocation by using strncmp instead of substr + + // comparison + if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(), + fragment.length()) != 0) { + return false; + } + + starting_pos += fragment.length(); + + // Should only happen when we have a static fragment after a param + // Example: '/users/:id/subscriptions' + // The 'subscriptions' fragment here does not have a corresponding param + if (i >= param_names_.size()) { continue; } + + auto sep_pos = request.path.find(separator, starting_pos); + if (sep_pos == std::string::npos) { sep_pos = request.path.length(); } + + const auto ¶m_name = param_names_[i]; + + request.path_params.emplace( + param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); + + // Mark everything up to '/' as matched + starting_pos = sep_pos + 1; + } + // Returns false if the path is longer than the pattern + return starting_pos >= request.path.length(); +} + +inline bool RegexMatcher::match(Request &request) const { + request.path_params.clear(); + return std::regex_match(request.path, request.matches, regex_); +} + +} // namespace detail + +// HTTP server implementation +inline Server::Server() + : new_task_queue( + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif +} + +inline Server::~Server() = default; + +inline std::unique_ptr +Server::make_matcher(const std::string &pattern) { + if (pattern.find("/:") != std::string::npos) { + return detail::make_unique(pattern); + } else { + return detail::make_unique(pattern); + } +} + +inline Server &Server::Get(const std::string &pattern, Handler handler) { + get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, Handler handler) { + post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, + HandlerWithContentReader handler) { + post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, Handler handler) { + put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, + HandlerWithContentReader handler) { + put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, Handler handler) { + patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Patch(const std::string &pattern, + HandlerWithContentReader handler) { + patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, Handler handler) { + delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Delete(const std::string &pattern, + HandlerWithContentReader handler) { + delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Options(const std::string &pattern, Handler handler) { + options_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline bool Server::set_base_dir(const std::string &dir, + const std::string &mount_point) { + return set_mount_point(mount_point, dir); +} + +inline bool Server::set_mount_point(const std::string &mount_point, + const std::string &dir, Headers headers) { + detail::FileStat stat(dir); + if (stat.is_dir()) { + std::string mnt = !mount_point.empty() ? mount_point : "/"; + if (!mnt.empty() && mnt[0] == '/') { + base_dirs_.push_back({mnt, dir, std::move(headers)}); + return true; + } + } + return false; +} + +inline bool Server::remove_mount_point(const std::string &mount_point) { + for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { + if (it->mount_point == mount_point) { + base_dirs_.erase(it); + return true; + } + } + return false; +} + +inline Server & +Server::set_file_extension_and_mimetype_mapping(const std::string &ext, + const std::string &mime) { + file_extension_and_mimetype_map_[ext] = mime; + return *this; +} + +inline Server &Server::set_default_file_mimetype(const std::string &mime) { + default_file_mimetype_ = mime; + return *this; +} + +inline Server &Server::set_file_request_handler(Handler handler) { + file_request_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler_core(HandlerWithResponse handler, + std::true_type) { + error_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_error_handler_core(Handler handler, + std::false_type) { + error_handler_ = [handler](const Request &req, Response &res) { + handler(req, res); + return HandlerResponse::Handled; + }; + return *this; +} + +inline Server &Server::set_exception_handler(ExceptionHandler handler) { + exception_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { + pre_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_post_routing_handler(Handler handler) { + post_routing_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_logger(Logger logger) { + logger_ = std::move(logger); + return *this; +} + +inline Server & +Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { + expect_100_continue_handler_ = std::move(handler); + return *this; +} + +inline Server &Server::set_address_family(int family) { + address_family_ = family; + return *this; +} + +inline Server &Server::set_tcp_nodelay(bool on) { + tcp_nodelay_ = on; + return *this; +} + +inline Server &Server::set_ipv6_v6only(bool on) { + ipv6_v6only_ = on; + return *this; +} + +inline Server &Server::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); + return *this; +} + +inline Server &Server::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); + return *this; +} + +inline Server &Server::set_header_writer( + std::function const &writer) { + header_writer_ = writer; + return *this; +} + +inline Server &Server::set_keep_alive_max_count(size_t count) { + keep_alive_max_count_ = count; + return *this; +} + +inline Server &Server::set_keep_alive_timeout(time_t sec) { + keep_alive_timeout_sec_ = sec; + return *this; +} + +inline Server &Server::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; + return *this; +} + +inline Server &Server::set_idle_interval(time_t sec, time_t usec) { + idle_interval_sec_ = sec; + idle_interval_usec_ = usec; + return *this; +} + +inline Server &Server::set_payload_max_length(size_t length) { + payload_max_length_ = length; + return *this; +} + +inline bool Server::bind_to_port(const std::string &host, int port, + int socket_flags) { + auto ret = bind_internal(host, port, socket_flags); + if (ret == -1) { is_decommisioned = true; } + return ret >= 0; +} +inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { + auto ret = bind_internal(host, 0, socket_flags); + if (ret == -1) { is_decommisioned = true; } + return ret; +} + +inline bool Server::listen_after_bind() { return listen_internal(); } + +inline bool Server::listen(const std::string &host, int port, + int socket_flags) { + return bind_to_port(host, port, socket_flags) && listen_internal(); +} + +inline bool Server::is_running() const { return is_running_; } + +inline void Server::wait_until_ready() const { + while (!is_running_ && !is_decommisioned) { + std::this_thread::sleep_for(std::chrono::milliseconds{1}); + } +} + +inline void Server::stop() { + if (is_running_) { + assert(svr_sock_ != INVALID_SOCKET); + std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); + detail::shutdown_socket(sock); + detail::close_socket(sock); + } + is_decommisioned = false; +} + +inline void Server::decommission() { is_decommisioned = true; } + +inline bool Server::parse_request_line(const char *s, Request &req) const { + auto len = strlen(s); + if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } + len -= 2; + + { + size_t count = 0; + + detail::split(s, s + len, ' ', [&](const char *b, const char *e) { + switch (count) { + case 0: req.method = std::string(b, e); break; + case 1: req.target = std::string(b, e); break; + case 2: req.version = std::string(b, e); break; + default: break; + } + count++; + }); + + if (count != 3) { return false; } + } + + static const std::set methods{ + "GET", "HEAD", "POST", "PUT", "DELETE", + "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; + + if (methods.find(req.method) == methods.end()) { return false; } + + if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } + + { + // Skip URL fragment + for (size_t i = 0; i < req.target.size(); i++) { + if (req.target[i] == '#') { + req.target.erase(i); + break; + } + } + + detail::divide(req.target, '?', + [&](const char *lhs_data, std::size_t lhs_size, + const char *rhs_data, std::size_t rhs_size) { + req.path = detail::decode_url( + std::string(lhs_data, lhs_size), false); + detail::parse_query_text(rhs_data, rhs_size, req.params); + }); + } + + return true; +} + +inline bool Server::write_response(Stream &strm, bool close_connection, + Request &req, Response &res) { + // NOTE: `req.ranges` should be empty, otherwise it will be applied + // incorrectly to the error content. + req.ranges.clear(); + return write_response_core(strm, close_connection, req, res, false); +} + +inline bool Server::write_response_with_content(Stream &strm, + bool close_connection, + const Request &req, + Response &res) { + return write_response_core(strm, close_connection, req, res, true); +} + +inline bool Server::write_response_core(Stream &strm, bool close_connection, + const Request &req, Response &res, + bool need_apply_ranges) { + assert(res.status != -1); + + if (400 <= res.status && error_handler_ && + error_handler_(req, res) == HandlerResponse::Handled) { + need_apply_ranges = true; + } + + std::string content_type; + std::string boundary; + if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } + + // Prepare additional headers + if (close_connection || req.get_header_value("Connection") == "close") { + res.set_header("Connection", "close"); + } else { + std::string s = "timeout="; + s += std::to_string(keep_alive_timeout_sec_); + s += ", max="; + s += std::to_string(keep_alive_max_count_); + res.set_header("Keep-Alive", s); + } + + if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) && + !res.has_header("Content-Type")) { + res.set_header("Content-Type", "text/plain"); + } + + if (res.body.empty() && !res.content_length_ && !res.content_provider_ && + !res.has_header("Content-Length")) { + res.set_header("Content-Length", "0"); + } + + if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) { + res.set_header("Accept-Ranges", "bytes"); + } + + if (post_routing_handler_) { post_routing_handler_(req, res); } + + // Response line and headers + { + detail::BufferStream bstrm; + if (!detail::write_response_line(bstrm, res.status)) { return false; } + if (!header_writer_(bstrm, res.headers)) { return false; } + + // Flush buffer + auto &data = bstrm.get_buffer(); + detail::write_data(strm, data.data(), data.size()); + } + + // Body + auto ret = true; + if (req.method != "HEAD") { + if (!res.body.empty()) { + if (!detail::write_data(strm, res.body.data(), res.body.size())) { + ret = false; + } + } else if (res.content_provider_) { + if (write_content_with_provider(strm, req, res, boundary, content_type)) { + res.content_provider_success_ = true; + } else { + ret = false; + } + } + } + + // Log + if (logger_) { logger_(req, res); } + + return ret; +} + +inline bool +Server::write_content_with_provider(Stream &strm, const Request &req, + Response &res, const std::string &boundary, + const std::string &content_type) { + auto is_shutting_down = [this]() { + return this->svr_sock_ == INVALID_SOCKET; + }; + + if (res.content_length_ > 0) { + if (req.ranges.empty()) { + return detail::write_content(strm, res.content_provider_, 0, + res.content_length_, is_shutting_down); + } else if (req.ranges.size() == 1) { + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + return detail::write_content(strm, res.content_provider_, + offset_and_length.first, + offset_and_length.second, is_shutting_down); + } else { + return detail::write_multipart_ranges_data( + strm, req, res, boundary, content_type, res.content_length_, + is_shutting_down); + } + } else { + if (res.is_chunked_content_provider_) { + auto type = detail::encoding_type(req, res); + + std::unique_ptr compressor; + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); +#endif + } else { + compressor = detail::make_unique(); + } + assert(compressor != nullptr); + + return detail::write_content_chunked(strm, res.content_provider_, + is_shutting_down, *compressor); + } else { + return detail::write_content_without_length(strm, res.content_provider_, + is_shutting_down); + } + } +} + +inline bool Server::read_content(Stream &strm, Request &req, Response &res) { + MultipartFormDataMap::iterator cur; + auto file_count = 0; + if (read_content_core( + strm, req, res, + // Regular + [&](const char *buf, size_t n) { + if (req.body.size() + n > req.body.max_size()) { return false; } + req.body.append(buf, n); + return true; + }, + // Multipart + [&](const MultipartFormData &file) { + if (file_count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { + return false; + } + cur = req.files.emplace(file.name, file); + return true; + }, + [&](const char *buf, size_t n) { + auto &content = cur->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + return true; + })) { + const auto &content_type = req.get_header_value("Content-Type"); + if (!content_type.find("application/x-www-form-urlencoded")) { + if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { + res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? + return false; + } + detail::parse_query_text(req.body, req.params); + } + return true; + } + return false; +} + +inline bool Server::read_content_with_content_receiver( + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) { + return read_content_core(strm, req, res, std::move(receiver), + std::move(multipart_header), + std::move(multipart_receiver)); +} + +inline bool +Server::read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver) const { + detail::MultipartFormDataParser multipart_form_data_parser; + ContentReceiverWithProgress out; + + if (req.is_multipart_form_data()) { + const auto &content_type = req.get_header_value("Content-Type"); + std::string boundary; + if (!detail::parse_multipart_boundary(content_type, boundary)) { + res.status = StatusCode::BadRequest_400; + return false; + } + + multipart_form_data_parser.set_boundary(std::move(boundary)); + out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { + /* For debug + size_t pos = 0; + while (pos < n) { + auto read_size = (std::min)(1, n - pos); + auto ret = multipart_form_data_parser.parse( + buf + pos, read_size, multipart_receiver, multipart_header); + if (!ret) { return false; } + pos += read_size; + } + return true; + */ + return multipart_form_data_parser.parse(buf, n, multipart_receiver, + multipart_header); + }; + } else { + out = [receiver](const char *buf, size_t n, uint64_t /*off*/, + uint64_t /*len*/) { return receiver(buf, n); }; + } + + if (req.method == "DELETE" && !req.has_header("Content-Length")) { + return true; + } + + if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, + out, true)) { + return false; + } + + if (req.is_multipart_form_data()) { + if (!multipart_form_data_parser.is_valid()) { + res.status = StatusCode::BadRequest_400; + return false; + } + } + + return true; +} + +inline bool Server::handle_file_request(const Request &req, Response &res, + bool head) { + for (const auto &entry : base_dirs_) { + // Prefix match + if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { + std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); + if (detail::is_valid_path(sub_path)) { + auto path = entry.base_dir + sub_path; + if (path.back() == '/') { path += "index.html"; } + + detail::FileStat stat(path); + + if (stat.is_dir()) { + res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); + return true; + } + + if (stat.is_file()) { + for (const auto &kv : entry.headers) { + res.set_header(kv.first, kv.second); + } + + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { return false; } + + res.set_content_provider( + mm->size(), + detail::find_content_type(path, file_extension_and_mimetype_map_, + default_file_mimetype_), + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + + if (!head && file_request_handler_) { + file_request_handler_(req, res); + } + + return true; + } + } + } + } + return false; +} + +inline socket_t +Server::create_server_socket(const std::string &host, int port, + int socket_flags, + SocketOptions socket_options) const { + return detail::create_socket( + host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, + ipv6_v6only_, std::move(socket_options), + [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { + if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + return false; + } + if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } + return true; + }); +} + +inline int Server::bind_internal(const std::string &host, int port, + int socket_flags) { + if (is_decommisioned) { return -1; } + + if (!is_valid()) { return -1; } + + svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); + if (svr_sock_ == INVALID_SOCKET) { return -1; } + + if (port == 0) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (getsockname(svr_sock_, reinterpret_cast(&addr), + &addr_len) == -1) { + return -1; + } + if (addr.ss_family == AF_INET) { + return ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + return ntohs(reinterpret_cast(&addr)->sin6_port); + } else { + return -1; + } + } else { + return port; + } +} + +inline bool Server::listen_internal() { + if (is_decommisioned) { return false; } + + auto ret = true; + is_running_ = true; + auto se = detail::scope_exit([&]() { is_running_ = false; }); + + { + std::unique_ptr task_queue(new_task_queue()); + + while (svr_sock_ != INVALID_SOCKET) { +#ifndef _WIN32 + if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { +#endif + auto val = detail::select_read(svr_sock_, idle_interval_sec_, + idle_interval_usec_); + if (val == 0) { // Timeout + task_queue->on_idle(); + continue; + } +#ifndef _WIN32 + } +#endif + +#if defined _WIN32 + // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, + // OVERLAPPED + socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); +#elif defined SOCK_CLOEXEC + socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); +#else + socket_t sock = accept(svr_sock_, nullptr, nullptr); +#endif + + if (sock == INVALID_SOCKET) { + if (errno == EMFILE) { + // The per-process limit of open file descriptors has been reached. + // Try to accept new connections after a short sleep. + std::this_thread::sleep_for(std::chrono::microseconds{1}); + continue; + } else if (errno == EINTR || errno == EAGAIN) { + continue; + } + if (svr_sock_ != INVALID_SOCKET) { + detail::close_socket(svr_sock_); + ret = false; + } else { + ; // The server socket was closed by user. + } + break; + } + + { +#ifdef _WIN32 + auto timeout = static_cast(read_timeout_sec_ * 1000 + + read_timeout_usec_ / 1000); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec_); + tv.tv_usec = static_cast(read_timeout_usec_); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + { + +#ifdef _WIN32 + auto timeout = static_cast(write_timeout_sec_ * 1000 + + write_timeout_usec_ / 1000); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&timeout), sizeof(timeout)); +#else + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec_); + tv.tv_usec = static_cast(write_timeout_usec_); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, + reinterpret_cast(&tv), sizeof(tv)); +#endif + } + + if (!task_queue->enqueue( + [this, sock]() { process_and_close_socket(sock); })) { + detail::shutdown_socket(sock); + detail::close_socket(sock); + } + } + + task_queue->shutdown(); + } + + is_decommisioned = !ret; + return ret; +} + +inline bool Server::routing(Request &req, Response &res, Stream &strm) { + if (pre_routing_handler_ && + pre_routing_handler_(req, res) == HandlerResponse::Handled) { + return true; + } + + // File handler + auto is_head_request = req.method == "HEAD"; + if ((req.method == "GET" || is_head_request) && + handle_file_request(req, res, is_head_request)) { + return true; + } + + if (detail::expect_content(req)) { + // Content reader handler + { + ContentReader reader( + [&](ContentReceiver receiver) { + return read_content_with_content_receiver( + strm, req, res, std::move(receiver), nullptr, nullptr); + }, + [&](MultipartContentHeader header, ContentReceiver receiver) { + return read_content_with_content_receiver(strm, req, res, nullptr, + std::move(header), + std::move(receiver)); + }); + + if (req.method == "POST") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + post_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PUT") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + put_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "PATCH") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + patch_handlers_for_content_reader_)) { + return true; + } + } else if (req.method == "DELETE") { + if (dispatch_request_for_content_reader( + req, res, std::move(reader), + delete_handlers_for_content_reader_)) { + return true; + } + } + } + + // Read content into `req.body` + if (!read_content(strm, req, res)) { return false; } + } + + // Regular handler + if (req.method == "GET" || req.method == "HEAD") { + return dispatch_request(req, res, get_handlers_); + } else if (req.method == "POST") { + return dispatch_request(req, res, post_handlers_); + } else if (req.method == "PUT") { + return dispatch_request(req, res, put_handlers_); + } else if (req.method == "DELETE") { + return dispatch_request(req, res, delete_handlers_); + } else if (req.method == "OPTIONS") { + return dispatch_request(req, res, options_handlers_); + } else if (req.method == "PATCH") { + return dispatch_request(req, res, patch_handlers_); + } + + res.status = StatusCode::BadRequest_400; + return false; +} + +inline bool Server::dispatch_request(Request &req, Response &res, + const Handlers &handlers) const { + for (const auto &x : handlers) { + const auto &matcher = x.first; + const auto &handler = x.second; + + if (matcher->match(req)) { + handler(req, res); + return true; + } + } + return false; +} + +inline void Server::apply_ranges(const Request &req, Response &res, + std::string &content_type, + std::string &boundary) const { + if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) { + auto it = res.headers.find("Content-Type"); + if (it != res.headers.end()) { + content_type = it->second; + res.headers.erase(it); + } + + boundary = detail::make_multipart_data_boundary(); + + res.set_header("Content-Type", + "multipart/byteranges; boundary=" + boundary); + } + + auto type = detail::encoding_type(req, res); + + if (res.body.empty()) { + if (res.content_length_ > 0) { + size_t length = 0; + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { + length = res.content_length_; + } else if (req.ranges.size() == 1) { + auto offset_and_length = detail::get_range_offset_and_length( + req.ranges[0], res.content_length_); + + length = offset_and_length.second; + + auto content_range = detail::make_content_range_header_field( + offset_and_length, res.content_length_); + res.set_header("Content-Range", content_range); + } else { + length = detail::get_multipart_ranges_data_length( + req, boundary, content_type, res.content_length_); + } + res.set_header("Content-Length", std::to_string(length)); + } else { + if (res.content_provider_) { + if (res.is_chunked_content_provider_) { + res.set_header("Transfer-Encoding", "chunked"); + if (type == detail::EncodingType::Gzip) { + res.set_header("Content-Encoding", "gzip"); + } else if (type == detail::EncodingType::Brotli) { + res.set_header("Content-Encoding", "br"); + } + } + } + } + } else { + if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { + ; + } else if (req.ranges.size() == 1) { + auto offset_and_length = + detail::get_range_offset_and_length(req.ranges[0], res.body.size()); + auto offset = offset_and_length.first; + auto length = offset_and_length.second; + + auto content_range = detail::make_content_range_header_field( + offset_and_length, res.body.size()); + res.set_header("Content-Range", content_range); + + assert(offset + length <= res.body.size()); + res.body = res.body.substr(offset, length); + } else { + std::string data; + detail::make_multipart_ranges_data(req, res, boundary, content_type, + res.body.size(), data); + res.body.swap(data); + } + + if (type != detail::EncodingType::None) { + std::unique_ptr compressor; + std::string content_encoding; + + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = detail::make_unique(); + content_encoding = "gzip"; +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = detail::make_unique(); + content_encoding = "br"; +#endif + } + + if (compressor) { + std::string compressed; + if (compressor->compress(res.body.data(), res.body.size(), true, + [&](const char *data, size_t data_len) { + compressed.append(data, data_len); + return true; + })) { + res.body.swap(compressed); + res.set_header("Content-Encoding", content_encoding); + } + } + } + + auto length = std::to_string(res.body.size()); + res.set_header("Content-Length", length); + } +} + +inline bool Server::dispatch_request_for_content_reader( + Request &req, Response &res, ContentReader content_reader, + const HandlersForContentReader &handlers) const { + for (const auto &x : handlers) { + const auto &matcher = x.first; + const auto &handler = x.second; + + if (matcher->match(req)) { + handler(req, res, content_reader); + return true; + } + } + return false; +} + +inline bool +Server::process_request(Stream &strm, const std::string &remote_addr, + int remote_port, const std::string &local_addr, + int local_port, bool close_connection, + bool &connection_closed, + const std::function &setup_request) { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + // Connection has been closed on client + if (!line_reader.getline()) { return false; } + + Request req; + + Response res; + res.version = "HTTP/1.1"; + res.headers = default_headers_; + +#ifdef _WIN32 + // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). +#else +#ifndef CPPHTTPLIB_USE_POLL + // Socket file descriptor exceeded FD_SETSIZE... + if (strm.socket() >= FD_SETSIZE) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::InternalServerError_500; + return write_response(strm, close_connection, req, res); + } +#endif +#endif + + // Request line and headers + if (!parse_request_line(line_reader.ptr(), req) || + !detail::read_headers(strm, req.headers)) { + res.status = StatusCode::BadRequest_400; + return write_response(strm, close_connection, req, res); + } + + // Check if the request URI doesn't exceed the limit + if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::UriTooLong_414; + return write_response(strm, close_connection, req, res); + } + + if (req.get_header_value("Connection") == "close") { + connection_closed = true; + } + + if (req.version == "HTTP/1.0" && + req.get_header_value("Connection") != "Keep-Alive") { + connection_closed = true; + } + + req.remote_addr = remote_addr; + req.remote_port = remote_port; + req.set_header("REMOTE_ADDR", req.remote_addr); + req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); + + req.local_addr = local_addr; + req.local_port = local_port; + req.set_header("LOCAL_ADDR", req.local_addr); + req.set_header("LOCAL_PORT", std::to_string(req.local_port)); + + if (req.has_header("Range")) { + const auto &range_header_value = req.get_header_value("Range"); + if (!detail::parse_range_header(range_header_value, req.ranges)) { + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); + } + } + + if (setup_request) { setup_request(req); } + + if (req.get_header_value("Expect") == "100-continue") { + int status = StatusCode::Continue_100; + if (expect_100_continue_handler_) { + status = expect_100_continue_handler_(req, res); + } + switch (status) { + case StatusCode::Continue_100: + case StatusCode::ExpectationFailed_417: + detail::write_response_line(strm, status); + strm.write("\r\n"); + break; + default: + connection_closed = true; + return write_response(strm, true, req, res); + } + } + + // Setup `is_connection_closed` method + req.is_connection_closed = [&]() { + return !detail::is_socket_alive(strm.socket()); + }; + + // Routing + auto routed = false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + routed = routing(req, res, strm); +#else + try { + routed = routing(req, res, strm); + } catch (std::exception &e) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = StatusCode::InternalServerError_500; + std::string val; + auto s = e.what(); + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case '\r': val += "\\r"; break; + case '\n': val += "\\n"; break; + default: val += s[i]; break; + } + } + res.set_header("EXCEPTION_WHAT", val); + } + } catch (...) { + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = StatusCode::InternalServerError_500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); + } + } +#endif + if (routed) { + if (res.status == -1) { + res.status = req.ranges.empty() ? StatusCode::OK_200 + : StatusCode::PartialContent_206; + } + + // Serve file content by using a content provider + if (!res.file_content_path_.empty()) { + const auto &path = res.file_content_path_; + auto mm = std::make_shared(path.c_str()); + if (!mm->is_open()) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::NotFound_404; + return write_response(strm, close_connection, req, res); + } + + auto content_type = res.file_content_content_type_; + if (content_type.empty()) { + content_type = detail::find_content_type( + path, file_extension_and_mimetype_map_, default_file_mimetype_); + } + + res.set_content_provider( + mm->size(), content_type, + [mm](size_t offset, size_t length, DataSink &sink) -> bool { + sink.write(mm->data() + offset, length); + return true; + }); + } + + if (detail::range_error(req, res)) { + res.body.clear(); + res.content_length_ = 0; + res.content_provider_ = nullptr; + res.status = StatusCode::RangeNotSatisfiable_416; + return write_response(strm, close_connection, req, res); + } + + return write_response_with_content(strm, close_connection, req, res); + } else { + if (res.status == -1) { res.status = StatusCode::NotFound_404; } + + return write_response(strm, close_connection, req, res); + } +} + +inline bool Server::is_valid() const { return true; } + +inline bool Server::process_and_close_socket(socket_t sock) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + + auto ret = detail::process_server_socket( + svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, connection_closed, + nullptr); + }); + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +// HTTP client implementation +inline ClientImpl::ClientImpl(const std::string &host) + : ClientImpl(host, 80, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port) + : ClientImpl(host, port, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), + host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), + client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} + +inline ClientImpl::~ClientImpl() { + std::lock_guard guard(socket_mutex_); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline bool ClientImpl::is_valid() const { return true; } + +inline void ClientImpl::copy_settings(const ClientImpl &rhs) { + client_cert_path_ = rhs.client_cert_path_; + client_key_path_ = rhs.client_key_path_; + connection_timeout_sec_ = rhs.connection_timeout_sec_; + read_timeout_sec_ = rhs.read_timeout_sec_; + read_timeout_usec_ = rhs.read_timeout_usec_; + write_timeout_sec_ = rhs.write_timeout_sec_; + write_timeout_usec_ = rhs.write_timeout_usec_; + max_timeout_msec_ = rhs.max_timeout_msec_; + basic_auth_username_ = rhs.basic_auth_username_; + basic_auth_password_ = rhs.basic_auth_password_; + bearer_token_auth_token_ = rhs.bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + digest_auth_username_ = rhs.digest_auth_username_; + digest_auth_password_ = rhs.digest_auth_password_; +#endif + keep_alive_ = rhs.keep_alive_; + follow_location_ = rhs.follow_location_; + url_encode_ = rhs.url_encode_; + address_family_ = rhs.address_family_; + tcp_nodelay_ = rhs.tcp_nodelay_; + ipv6_v6only_ = rhs.ipv6_v6only_; + socket_options_ = rhs.socket_options_; + compress_ = rhs.compress_; + decompress_ = rhs.decompress_; + interface_ = rhs.interface_; + proxy_host_ = rhs.proxy_host_; + proxy_port_ = rhs.proxy_port_; + proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; + proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; + proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; + proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + ca_cert_file_path_ = rhs.ca_cert_file_path_; + ca_cert_dir_path_ = rhs.ca_cert_dir_path_; + ca_cert_store_ = rhs.ca_cert_store_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + server_certificate_verification_ = rhs.server_certificate_verification_; + server_hostname_verification_ = rhs.server_hostname_verification_; + server_certificate_verifier_ = rhs.server_certificate_verifier_; +#endif + logger_ = rhs.logger_; +} + +inline socket_t ClientImpl::create_client_socket(Error &error) const { + if (!proxy_host_.empty() && proxy_port_ != -1) { + return detail::create_client_socket( + proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, + ipv6_v6only_, socket_options_, connection_timeout_sec_, + connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, interface_, error); + } + + // Check is custom IP specified for host_ + std::string ip; + auto it = addr_map_.find(host_); + if (it != addr_map_.end()) { ip = it->second; } + + return detail::create_client_socket( + host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_, + socket_options_, connection_timeout_sec_, connection_timeout_usec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, interface_, error); +} + +inline bool ClientImpl::create_and_connect_socket(Socket &socket, + Error &error) { + auto sock = create_client_socket(error); + if (sock == INVALID_SOCKET) { return false; } + socket.sock = sock; + return true; +} + +inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, + bool /*shutdown_gracefully*/) { + // If there are any requests in flight from threads other than us, then it's + // a thread-unsafe race because individual ssl* objects are not thread-safe. + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); +} + +inline void ClientImpl::shutdown_socket(Socket &socket) const { + if (socket.sock == INVALID_SOCKET) { return; } + detail::shutdown_socket(socket.sock); +} + +inline void ClientImpl::close_socket(Socket &socket) { + // If there are requests in flight in another thread, usually closing + // the socket will be fine and they will simply receive an error when + // using the closed socket, but it is still a bug since rarely the OS + // may reassign the socket id to be used for a new socket, and then + // suddenly they will be operating on a live socket that is different + // than the one they intended! + assert(socket_requests_in_flight_ == 0 || + socket_requests_are_from_thread_ == std::this_thread::get_id()); + + // It is also a bug if this happens while SSL is still active +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + assert(socket.ssl == nullptr); +#endif + if (socket.sock == INVALID_SOCKET) { return; } + detail::close_socket(socket.sock); + socket.sock = INVALID_SOCKET; +} + +inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, + Response &res) const { + std::array buf{}; + + detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); + + if (!line_reader.getline()) { return false; } + +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); +#else + const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); +#endif + + std::cmatch m; + if (!std::regex_match(line_reader.ptr(), m, re)) { + return req.method == "CONNECT"; + } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + + // Ignore '100 Continue' + while (res.status == StatusCode::Continue_100) { + if (!line_reader.getline()) { return false; } // CRLF + if (!line_reader.getline()) { return false; } // next response line + + if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + } + + return true; +} + +inline bool ClientImpl::send(Request &req, Response &res, Error &error) { + std::lock_guard request_mutex_guard(request_mutex_); + auto ret = send_(req, res, error); + if (error == Error::SSLPeerCouldBeClosed_) { + assert(!ret); + ret = send_(req, res, error); + } + return ret; +} + +inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { + { + std::lock_guard guard(socket_mutex_); + + // Set this to false immediately - if it ever gets set to true by the end of + // the request, we know another thread instructed us to close the socket. + socket_should_be_closed_when_request_is_done_ = false; + + auto is_alive = false; + if (socket_.is_open()) { + is_alive = detail::is_socket_alive(socket_.sock); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_alive && is_ssl()) { + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + is_alive = false; + } + } +#endif + + if (!is_alive) { + // Attempt to avoid sigpipe by shutting down nongracefully if it seems + // like the other side has already closed the connection Also, there + // cannot be any requests in flight from other threads since we locked + // request_mutex_, so safe to close everything immediately + const bool shutdown_gracefully = false; + shutdown_ssl(socket_, shutdown_gracefully); + shutdown_socket(socket_); + close_socket(socket_); + } + } + + if (!is_alive) { + if (!create_and_connect_socket(socket_, error)) { return false; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // TODO: refactoring + if (is_ssl()) { + auto &scli = static_cast(*this); + if (!proxy_host_.empty() && proxy_port_ != -1) { + auto success = false; + if (!scli.connect_with_proxy(socket_, req.start_time_, res, success, + error)) { + return success; + } + } + + if (!scli.initialize_ssl(socket_, error)) { return false; } + } +#endif + } + + // Mark the current socket as being in use so that it cannot be closed by + // anyone else while this request is ongoing, even though we will be + // releasing the mutex. + if (socket_requests_in_flight_ > 1) { + assert(socket_requests_are_from_thread_ == std::this_thread::get_id()); + } + socket_requests_in_flight_ += 1; + socket_requests_are_from_thread_ = std::this_thread::get_id(); + } + + for (const auto &header : default_headers_) { + if (req.headers.find(header.first) == req.headers.end()) { + req.headers.insert(header); + } + } + + auto ret = false; + auto close_connection = !keep_alive_; + + auto se = detail::scope_exit([&]() { + // Briefly lock mutex in order to mark that a request is no longer ongoing + std::lock_guard guard(socket_mutex_); + socket_requests_in_flight_ -= 1; + if (socket_requests_in_flight_ <= 0) { + assert(socket_requests_in_flight_ == 0); + socket_requests_are_from_thread_ = std::thread::id(); + } + + if (socket_should_be_closed_when_request_is_done_ || close_connection || + !ret) { + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + }); + + ret = process_socket(socket_, req.start_time_, [&](Stream &strm) { + return handle_request(strm, req, res, close_connection, error); + }); + + if (!ret) { + if (error == Error::Success) { error = Error::Unknown; } + } + + return ret; +} + +inline Result ClientImpl::send(const Request &req) { + auto req2 = req; + return send_(std::move(req2)); +} + +inline Result ClientImpl::send_(Request &&req) { + auto res = detail::make_unique(); + auto error = Error::Success; + auto ret = send(req, *res, error); + return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; +} + +inline bool ClientImpl::handle_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + if (req.path.empty()) { + error = Error::Connection; + return false; + } + + auto req_save = req; + + bool ret; + + if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { + auto req2 = req; + req2.path = "http://" + host_and_port_ + req.path; + ret = process_request(strm, req2, res, close_connection, error); + req = req2; + req.path = req_save.path; + } else { + ret = process_request(strm, req, res, close_connection, error); + } + + if (!ret) { return false; } + + if (res.get_header_value("Connection") == "close" || + (res.version == "HTTP/1.0" && res.reason != "Connection established")) { + // TODO this requires a not-entirely-obvious chain of calls to be correct + // for this to be safe. + + // This is safe to call because handle_request is only called by send_ + // which locks the request mutex during the process. It would be a bug + // to call it from a different thread since it's a thread-safety issue + // to do these things to the socket if another thread is using the socket. + std::lock_guard guard(socket_mutex_); + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); + } + + if (300 < res.status && res.status < 400 && follow_location_) { + req = req_save; + ret = redirect(req, res, error); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if ((res.status == StatusCode::Unauthorized_401 || + res.status == StatusCode::ProxyAuthenticationRequired_407) && + req.authorization_count_ < 5) { + auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407; + const auto &username = + is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; + const auto &password = + is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; + + if (!username.empty() && !password.empty()) { + std::map auth; + if (detail::parse_www_authenticate(res, auth, is_proxy)) { + Request new_req = req; + new_req.authorization_count_ += 1; + new_req.headers.erase(is_proxy ? "Proxy-Authorization" + : "Authorization"); + new_req.headers.insert(detail::make_digest_authentication_header( + req, auth, new_req.authorization_count_, detail::random_string(10), + username, password, is_proxy)); + + Response new_res; + + ret = send(new_req, new_res, error); + if (ret) { res = new_res; } + } + } + } +#endif + + return ret; +} + +inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { + if (req.redirect_count_ == 0) { + error = Error::ExceedRedirectCount; + return false; + } + + auto location = res.get_header_value("location"); + if (location.empty()) { return false; } + + const static std::regex re( + R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); + + std::smatch m; + if (!std::regex_match(location, m, re)) { return false; } + + auto scheme = is_ssl() ? "https" : "http"; + + auto next_scheme = m[1].str(); + auto next_host = m[2].str(); + if (next_host.empty()) { next_host = m[3].str(); } + auto port_str = m[4].str(); + auto next_path = m[5].str(); + auto next_query = m[6].str(); + + auto next_port = port_; + if (!port_str.empty()) { + next_port = std::stoi(port_str); + } else if (!next_scheme.empty()) { + next_port = next_scheme == "https" ? 443 : 80; + } + + if (next_scheme.empty()) { next_scheme = scheme; } + if (next_host.empty()) { next_host = host_; } + if (next_path.empty()) { next_path = "/"; } + + auto path = detail::decode_url(next_path, true) + next_query; + + if (next_scheme == scheme && next_host == host_ && next_port == port_) { + return detail::redirect(*this, req, res, path, location, error); + } else { + if (next_scheme == "https") { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli(next_host, next_port); + cli.copy_settings(*this); + if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } + return detail::redirect(cli, req, res, path, location, error); +#else + return false; +#endif + } else { + ClientImpl cli(next_host, next_port); + cli.copy_settings(*this); + return detail::redirect(cli, req, res, path, location, error); + } + } +} + +inline bool ClientImpl::write_content_with_provider(Stream &strm, + const Request &req, + Error &error) const { + auto is_shutting_down = []() { return false; }; + + if (req.is_chunked_content_provider_) { + // TODO: Brotli support + std::unique_ptr compressor; +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { + compressor = detail::make_unique(); + } else +#endif + { + compressor = detail::make_unique(); + } + + return detail::write_content_chunked(strm, req.content_provider_, + is_shutting_down, *compressor, error); + } else { + return detail::write_content(strm, req.content_provider_, 0, + req.content_length_, is_shutting_down, error); + } +} + +inline bool ClientImpl::write_request(Stream &strm, Request &req, + bool close_connection, Error &error) { + // Prepare additional headers + if (close_connection) { + if (!req.has_header("Connection")) { + req.set_header("Connection", "close"); + } + } + + if (!req.has_header("Host")) { + if (is_ssl()) { + if (port_ == 443) { + req.set_header("Host", host_); + } else { + req.set_header("Host", host_and_port_); + } + } else { + if (port_ == 80) { + req.set_header("Host", host_); + } else { + req.set_header("Host", host_and_port_); + } + } + } + + if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } + + if (!req.content_receiver) { + if (!req.has_header("Accept-Encoding")) { + std::string accept_encoding; +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + accept_encoding = "br"; +#endif +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "gzip, deflate"; +#endif + req.set_header("Accept-Encoding", accept_encoding); + } + +#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT + if (!req.has_header("User-Agent")) { + auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; + req.set_header("User-Agent", agent); + } +#endif + }; + + if (req.body.empty()) { + if (req.content_provider_) { + if (!req.is_chunked_content_provider_) { + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.content_length_); + req.set_header("Content-Length", length); + } + } + } else { + if (req.method == "POST" || req.method == "PUT" || + req.method == "PATCH") { + req.set_header("Content-Length", "0"); + } + } + } else { + if (!req.has_header("Content-Type")) { + req.set_header("Content-Type", "text/plain"); + } + + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.body.size()); + req.set_header("Content-Length", length); + } + } + + if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_basic_authentication_header( + basic_auth_username_, basic_auth_password_, false)); + } + } + + if (!proxy_basic_auth_username_.empty() && + !proxy_basic_auth_password_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_basic_authentication_header( + proxy_basic_auth_username_, proxy_basic_auth_password_, true)); + } + } + + if (!bearer_token_auth_token_.empty()) { + if (!req.has_header("Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + bearer_token_auth_token_, false)); + } + } + + if (!proxy_bearer_token_auth_token_.empty()) { + if (!req.has_header("Proxy-Authorization")) { + req.headers.insert(make_bearer_token_authentication_header( + proxy_bearer_token_auth_token_, true)); + } + } + + // Request line and headers + { + detail::BufferStream bstrm; + + const auto &path_with_query = + req.params.empty() ? req.path + : append_query_params(req.path, req.params); + + const auto &path = + url_encode_ ? detail::encode_url(path_with_query) : path_with_query; + + detail::write_request_line(bstrm, req.method, path); + + header_writer_(bstrm, req.headers); + + // Flush buffer + auto &data = bstrm.get_buffer(); + if (!detail::write_data(strm, data.data(), data.size())) { + error = Error::Write; + return false; + } + } + + // Body + if (req.body.empty()) { + return write_content_with_provider(strm, req, error); + } + + if (!detail::write_data(strm, req.body.data(), req.body.size())) { + error = Error::Write; + return false; + } + + return true; +} + +inline std::unique_ptr ClientImpl::send_with_content_provider( + Request &req, const char *body, size_t content_length, + ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Error &error) { + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_) { req.set_header("Content-Encoding", "gzip"); } +#endif + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (compress_ && !content_provider_without_length) { + // TODO: Brotli support + detail::gzip_compressor compressor; + + if (content_provider) { + auto ok = true; + size_t offset = 0; + DataSink data_sink; + + data_sink.write = [&](const char *data, size_t data_len) -> bool { + if (ok) { + auto last = offset + data_len == content_length; + + auto ret = compressor.compress( + data, data_len, last, + [&](const char *compressed_data, size_t compressed_data_len) { + req.body.append(compressed_data, compressed_data_len); + return true; + }); + + if (ret) { + offset += data_len; + } else { + ok = false; + } + } + return ok; + }; + + while (ok && offset < content_length) { + if (!content_provider(offset, content_length - offset, data_sink)) { + error = Error::Canceled; + return nullptr; + } + } + } else { + if (!compressor.compress(body, content_length, true, + [&](const char *data, size_t data_len) { + req.body.append(data, data_len); + return true; + })) { + error = Error::Compression; + return nullptr; + } + } + } else +#endif + { + if (content_provider) { + req.content_length_ = content_length; + req.content_provider_ = std::move(content_provider); + req.is_chunked_content_provider_ = false; + } else if (content_provider_without_length) { + req.content_length_ = 0; + req.content_provider_ = detail::ContentProviderAdapter( + std::move(content_provider_without_length)); + req.is_chunked_content_provider_ = true; + req.set_header("Transfer-Encoding", "chunked"); + } else { + req.body.assign(body, content_length); + } + } + + auto res = detail::make_unique(); + return send(req, *res, error) ? std::move(res) : nullptr; +} + +inline Result ClientImpl::send_with_content_provider( + const std::string &method, const std::string &path, const Headers &headers, + const char *body, size_t content_length, ContentProvider content_provider, + ContentProviderWithoutLength content_provider_without_length, + const std::string &content_type, Progress progress) { + Request req; + req.method = method; + req.headers = headers; + req.path = path; + req.progress = progress; + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + auto error = Error::Success; + + auto res = send_with_content_provider( + req, body, content_length, std::move(content_provider), + std::move(content_provider_without_length), content_type, error); + + return Result{std::move(res), error, std::move(req.headers)}; +} + +inline std::string +ClientImpl::adjust_host_string(const std::string &host) const { + if (host.find(':') != std::string::npos) { return "[" + host + "]"; } + return host; +} + +inline bool ClientImpl::process_request(Stream &strm, Request &req, + Response &res, bool close_connection, + Error &error) { + // Send request + if (!write_request(strm, req, close_connection, error)) { return false; } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl()) { + auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; + if (!is_proxy_enabled) { + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + error = Error::SSLPeerCouldBeClosed_; + return false; + } + } + } +#endif + + // Receive response and headers + if (!read_response_line(strm, req, res) || + !detail::read_headers(strm, res.headers)) { + error = Error::Read; + return false; + } + + // Body + if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && + req.method != "CONNECT") { + auto redirect = 300 < res.status && res.status < 400 && + res.status != StatusCode::NotModified_304 && + follow_location_; + + if (req.response_handler && !redirect) { + if (!req.response_handler(res)) { + error = Error::Canceled; + return false; + } + } + + auto out = + req.content_receiver + ? static_cast( + [&](const char *buf, size_t n, uint64_t off, uint64_t len) { + if (redirect) { return true; } + auto ret = req.content_receiver(buf, n, off, len); + if (!ret) { error = Error::Canceled; } + return ret; + }) + : static_cast( + [&](const char *buf, size_t n, uint64_t /*off*/, + uint64_t /*len*/) { + assert(res.body.size() + n <= res.body.max_size()); + res.body.append(buf, n); + return true; + }); + + auto progress = [&](uint64_t current, uint64_t total) { + if (!req.progress || redirect) { return true; } + auto ret = req.progress(current, total); + if (!ret) { error = Error::Canceled; } + return ret; + }; + + if (res.has_header("Content-Length")) { + if (!req.content_receiver) { + auto len = res.get_header_value_u64("Content-Length"); + if (len > res.body.max_size()) { + error = Error::Read; + return false; + } + res.body.reserve(static_cast(len)); + } + } + + if (res.status != StatusCode::NotModified_304) { + int dummy_status; + if (!detail::read_content(strm, res, (std::numeric_limits::max)(), + dummy_status, std::move(progress), + std::move(out), decompress_)) { + if (error != Error::Canceled) { error = Error::Read; } + return false; + } + } + } + + // Log + if (logger_) { logger_(req, res); } + + return true; +} + +inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( + const std::string &boundary, const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) const { + size_t cur_item = 0; + size_t cur_start = 0; + // cur_item and cur_start are copied to within the std::function and maintain + // state between successive calls + return [&, cur_item, cur_start](size_t offset, + DataSink &sink) mutable -> bool { + if (!offset && !items.empty()) { + sink.os << detail::serialize_multipart_formdata(items, boundary, false); + return true; + } else if (cur_item < provider_items.size()) { + if (!cur_start) { + const auto &begin = detail::serialize_multipart_formdata_item_begin( + provider_items[cur_item], boundary); + offset += begin.size(); + cur_start = offset; + sink.os << begin; + } + + DataSink cur_sink; + auto has_data = true; + cur_sink.write = sink.write; + cur_sink.done = [&]() { has_data = false; }; + + if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) { + return false; + } + + if (!has_data) { + sink.os << detail::serialize_multipart_formdata_item_end(); + cur_item++; + cur_start = 0; + } + return true; + } else { + sink.os << detail::serialize_multipart_formdata_finish(boundary); + sink.done(); + return true; + } + }; +} + +inline bool ClientImpl::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { + return detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, max_timeout_msec_, start_time, + std::move(callback)); +} + +inline bool ClientImpl::is_ssl() const { return false; } + +inline Result ClientImpl::Get(const std::string &path) { + return Get(path, Headers(), Progress()); +} + +inline Result ClientImpl::Get(const std::string &path, Progress progress) { + return Get(path, Headers(), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { + return Get(path, headers, Progress()); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + Progress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver) { + return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver) { + return Get(path, headers, nullptr, std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), nullptr); +} + +inline Result ClientImpl::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + Request req; + req.method = "GET"; + req.path = path; + req.headers = headers; + req.response_handler = std::move(response_handler); + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress) { + if (params.empty()) { return Get(path, headers); } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, params, headers, nullptr, std::move(content_receiver), + std::move(progress)); +} + +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + if (params.empty()) { + return Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); + } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result ClientImpl::Head(const std::string &path) { + return Head(path, Headers()); +} + +inline Result ClientImpl::Head(const std::string &path, + const Headers &headers) { + Request req; + req.method = "HEAD"; + req.headers = headers; + req.path = path; + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline Result ClientImpl::Post(const std::string &path) { + return Post(path, std::string(), std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, + const Headers &headers) { + return Post(path, headers, nullptr, 0, std::string()); +} + +inline Result ClientImpl::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Post(path, Headers(), body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type) { + return Post(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return Post(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { + return Post(path, Headers(), params); +} + +inline Result ClientImpl::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Post(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Post(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + nullptr); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Post(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + +inline Result ClientImpl::Post(const std::string &path, + const MultipartFormDataItems &items) { + return Post(path, Headers(), items); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Post(path, headers, body, content_type); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Post(path, headers, body, content_type); +} + +inline Result +ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "POST", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path) { + return Put(path, std::string(), std::string()); +} + +inline Result ClientImpl::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Put(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type) { + return Put(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return Put(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Put(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Put(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + nullptr); +} + +inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { + return Put(path, Headers(), params); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Put(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + +inline Result ClientImpl::Put(const std::string &path, + const MultipartFormDataItems &items) { + return Put(path, Headers(), items); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Put(path, headers, body, content_type); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Put(path, headers, body, content_type); +} + +inline Result +ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "PUT", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, nullptr); +} +inline Result ClientImpl::Patch(const std::string &path) { + return Patch(path, std::string(), std::string()); +} + +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Patch(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return Patch(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return Patch(path, headers, body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PATCH", path, headers, body, + content_length, nullptr, nullptr, + content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type) { + return Patch(path, Headers(), body, content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Patch(path, Headers(), body, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return Patch(path, headers, body, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return send_with_content_provider("PATCH", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return Patch(path, Headers(), content_length, std::move(content_provider), + content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return Patch(path, Headers(), std::move(content_provider), content_type); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, nullptr); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + nullptr); +} + +inline Result ClientImpl::Delete(const std::string &path) { + return Delete(path, Headers(), std::string(), std::string()); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers) { + return Delete(path, headers, std::string(), std::string()); +} + +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return Delete(path, Headers(), body, content_length, content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return Delete(path, Headers(), body, content_length, content_type, progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type) { + return Delete(path, headers, body, content_length, content_type, nullptr); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + Request req; + req.method = "DELETE"; + req.headers = headers; + req.path = path; + req.progress = progress; + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + req.body.assign(body, content_length); + + return send_(std::move(req)); +} + +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type) { + return Delete(path, Headers(), body.data(), body.size(), content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Delete(path, Headers(), body.data(), body.size(), content_type, + progress); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type) { + return Delete(path, headers, body.data(), body.size(), content_type); +} + +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return Delete(path, headers, body.data(), body.size(), content_type, + progress); +} + +inline Result ClientImpl::Options(const std::string &path) { + return Options(path, Headers()); +} + +inline Result ClientImpl::Options(const std::string &path, + const Headers &headers) { + Request req; + req.method = "OPTIONS"; + req.headers = headers; + req.path = path; + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + return send_(std::move(req)); +} + +inline void ClientImpl::stop() { + std::lock_guard guard(socket_mutex_); + + // If there is anything ongoing right now, the ONLY thread-safe thing we can + // do is to shutdown_socket, so that threads using this socket suddenly + // discover they can't read/write any more and error out. Everything else + // (closing the socket, shutting ssl down) is unsafe because these actions are + // not thread-safe. + if (socket_requests_in_flight_ > 0) { + shutdown_socket(socket_); + + // Aside from that, we set a flag for the socket to be closed when we're + // done. + socket_should_be_closed_when_request_is_done_ = true; + return; + } + + // Otherwise, still holding the mutex, we can shut everything down ourselves + shutdown_ssl(socket_, true); + shutdown_socket(socket_); + close_socket(socket_); +} + +inline std::string ClientImpl::host() const { return host_; } + +inline int ClientImpl::port() const { return port_; } + +inline size_t ClientImpl::is_socket_open() const { + std::lock_guard guard(socket_mutex_); + return socket_.is_open(); +} + +inline socket_t ClientImpl::socket() const { return socket_.sock; } + +inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { + connection_timeout_sec_ = sec; + connection_timeout_usec_ = usec; +} + +inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { + read_timeout_sec_ = sec; + read_timeout_usec_ = usec; +} + +inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; +} + +inline void ClientImpl::set_max_timeout(time_t msec) { + max_timeout_msec_ = msec; +} + +inline void ClientImpl::set_basic_auth(const std::string &username, + const std::string &password) { + basic_auth_username_ = username; + basic_auth_password_ = password; +} + +inline void ClientImpl::set_bearer_token_auth(const std::string &token) { + bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_digest_auth(const std::string &username, + const std::string &password) { + digest_auth_username_ = username; + digest_auth_password_ = password; +} +#endif + +inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } + +inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } + +inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } + +inline void +ClientImpl::set_hostname_addr_map(std::map addr_map) { + addr_map_ = std::move(addr_map); +} + +inline void ClientImpl::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); +} + +inline void ClientImpl::set_header_writer( + std::function const &writer) { + header_writer_ = writer; +} + +inline void ClientImpl::set_address_family(int family) { + address_family_ = family; +} + +inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } + +inline void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; } + +inline void ClientImpl::set_socket_options(SocketOptions socket_options) { + socket_options_ = std::move(socket_options); +} + +inline void ClientImpl::set_compress(bool on) { compress_ = on; } + +inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } + +inline void ClientImpl::set_interface(const std::string &intf) { + interface_ = intf; +} + +inline void ClientImpl::set_proxy(const std::string &host, int port) { + proxy_host_ = host; + proxy_port_ = port; +} + +inline void ClientImpl::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + proxy_basic_auth_username_ = username; + proxy_basic_auth_password_ = password; +} + +inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { + proxy_bearer_token_auth_token_ = token; +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + proxy_digest_auth_username_ = username; + proxy_digest_auth_password_ = password; +} + +inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + ca_cert_file_path_ = ca_cert_file_path; + ca_cert_dir_path_ = ca_cert_dir_path; +} + +inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store && ca_cert_store != ca_cert_store_) { + ca_cert_store_ = ca_cert_store; + } +} + +inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, + std::size_t size) const { + auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); + auto se = detail::scope_exit([&] { BIO_free_all(mem); }); + if (!mem) { return nullptr; } + + auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); + if (!inf) { return nullptr; } + + auto cts = X509_STORE_new(); + if (cts) { + for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { + auto itmp = sk_X509_INFO_value(inf, i); + if (!itmp) { continue; } + + if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } + if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); } + } + } + + sk_X509_INFO_pop_free(inf, X509_INFO_free); + return cts; +} + +inline void ClientImpl::enable_server_certificate_verification(bool enabled) { + server_certificate_verification_ = enabled; +} + +inline void ClientImpl::enable_server_hostname_verification(bool enabled) { + server_hostname_verification_ = enabled; +} + +inline void ClientImpl::set_server_certificate_verifier( + std::function verifier) { + server_certificate_verifier_ = verifier; +} +#endif + +inline void ClientImpl::set_logger(Logger logger) { + logger_ = std::move(logger); +} + +/* + * SSL Implementation + */ +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +namespace detail { + +template +inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, + U SSL_connect_or_accept, V setup) { + SSL *ssl = nullptr; + { + std::lock_guard guard(ctx_mutex); + ssl = SSL_new(ctx); + } + + if (ssl) { + set_nonblocking(sock, true); + auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); + BIO_set_nbio(bio, 1); + SSL_set_bio(ssl, bio, bio); + + if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { + SSL_shutdown(ssl); + { + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); + } + set_nonblocking(sock, false); + return nullptr; + } + BIO_set_nbio(bio, 0); + set_nonblocking(sock, false); + } + + return ssl; +} + +inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, + bool shutdown_gracefully) { + // sometimes we may want to skip this to try to avoid SIGPIPE if we know + // the remote has closed the network connection + // Note that it is not always possible to avoid SIGPIPE, this is merely a + // best-efforts. + if (shutdown_gracefully) { +#ifdef _WIN32 + (void)(sock); + SSL_shutdown(ssl); +#else + timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, + reinterpret_cast(&tv), sizeof(tv)); + + auto ret = SSL_shutdown(ssl); + while (ret == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds{100}); + ret = SSL_shutdown(ssl); + } +#endif + } + + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); +} + +template +bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, + U ssl_connect_or_accept, + time_t timeout_sec, + time_t timeout_usec) { + auto res = 0; + while ((res = ssl_connect_or_accept(ssl)) != 1) { + auto err = SSL_get_error(ssl, res); + switch (err) { + case SSL_ERROR_WANT_READ: + if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + case SSL_ERROR_WANT_WRITE: + if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } + break; + default: break; + } + return false; + } + return true; +} + +template +inline bool process_server_socket_ssl( + const std::atomic &svr_sock, SSL *ssl, socket_t sock, + size_t keep_alive_max_count, time_t keep_alive_timeout_sec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +template +inline bool process_client_socket_ssl( + SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, T callback) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec, + max_timeout_msec, start_time); + return callback(strm); +} + +class SSLInit { +public: + SSLInit() { + OPENSSL_init_ssl( + OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); + } +}; + +// SSL socket stream implementation +inline SSLSocketStream::SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time) + : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), + max_timeout_msec_(max_timeout_msec), start_time(start_time) { + SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); +} + +inline SSLSocketStream::~SSLSocketStream() = default; + +inline bool SSLSocketStream::is_readable() const { + if (max_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } + + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); + + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; +} + +inline bool SSLSocketStream::is_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_); +} + +inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (is_readable()) { + auto ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_READ || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_READ) { +#endif + if (SSL_pending(ssl_) > 0) { + return SSL_read(ssl_, ptr, static_cast(size)); + } else if (is_readable()) { + std::this_thread::sleep_for(std::chrono::microseconds{10}); + ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + return -1; + } + } + } + return ret; + } else { + return -1; + } +} + +inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { + if (is_writable()) { + auto handle_size = static_cast( + std::min(size, (std::numeric_limits::max)())); + + auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret < 0) { + auto err = SSL_get_error(ssl_, ret); + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || + (err == SSL_ERROR_SYSCALL && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { +#endif + if (is_writable()) { + std::this_thread::sleep_for(std::chrono::microseconds{10}); + ret = SSL_write(ssl_, ptr, static_cast(handle_size)); + if (ret >= 0) { return ret; } + err = SSL_get_error(ssl_, ret); + } else { + return -1; + } + } + } + return ret; + } + return -1; +} + +inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SSLSocketStream::socket() const { return sock_; } + +inline time_t SSLSocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time) + .count(); +} + +static SSLInit sslinit_; + +} // namespace detail + +// SSL HTTP server implementation +inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path, + const char *client_ca_cert_dir_path, + const char *private_key_password) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + if (private_key_password != nullptr && (private_key_password[0] != '\0')) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, + reinterpret_cast(const_cast(private_key_password))); + } + + if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != + 1 || + SSL_CTX_check_private_key(ctx_) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { + SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, + client_ca_cert_dir_path); + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + ctx_ = SSL_CTX_new(TLS_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + if (SSL_CTX_use_certificate(ctx_, cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_store) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + + SSL_CTX_set_verify( + ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); + } + } +} + +inline SSLServer::SSLServer( + const std::function &setup_ssl_ctx_callback) { + ctx_ = SSL_CTX_new(TLS_method()); + if (ctx_) { + if (!setup_ssl_ctx_callback(*ctx_)) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLServer::~SSLServer() { + if (ctx_) { SSL_CTX_free(ctx_); } +} + +inline bool SSLServer::is_valid() const { return ctx_; } + +inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } + +inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + + std::lock_guard guard(ctx_mutex_); + + SSL_CTX_use_certificate(ctx_, cert); + SSL_CTX_use_PrivateKey(ctx_, private_key); + + if (client_ca_cert_store != nullptr) { + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + } +} + +inline bool SSLServer::process_and_close_socket(socket_t sock) { + auto ssl = detail::ssl_new( + sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + return detail::ssl_connect_or_accept_nonblocking( + sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); + }, + [](SSL * /*ssl2*/) { return true; }); + + auto ret = false; + if (ssl) { + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); + + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); + + ret = detail::process_server_socket_ssl( + svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, + connection_closed, + [&](Request &req) { req.ssl = ssl; }); + }); + + // Shutdown gracefully if the result seemed successful, non-gracefully if + // the connection appeared to be closed. + const bool shutdown_gracefully = ret; + detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); + } + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; +} + +// SSL HTTP client implementation +inline SSLClient::SSLClient(const std::string &host) + : SSLClient(host, 443, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port) + : SSLClient(host, port, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password) + : ClientImpl(host, port, client_cert_path, client_key_path) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(b, e); + }); + + if (!client_cert_path.empty() && !client_key_path.empty()) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + + if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), + SSL_FILETYPE_PEM) != 1 || + SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), + SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::SSLClient(const std::string &host, int port, + X509 *client_cert, EVP_PKEY *client_key, + const std::string &private_key_password) + : ClientImpl(host, port) { + ctx_ = SSL_CTX_new(TLS_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(b, e); + }); + + if (client_cert != nullptr && client_key != nullptr) { + if (!private_key_password.empty()) { + SSL_CTX_set_default_passwd_cb_userdata( + ctx_, reinterpret_cast( + const_cast(private_key_password.c_str()))); + } + + if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::~SSLClient() { + if (ctx_) { SSL_CTX_free(ctx_); } + // Make sure to shut down SSL since shutdown_ssl will resolve to the + // base function rather than the derived function once we get to the + // base class destructor, and won't free the SSL (causing a leak). + shutdown_ssl_impl(socket_, true); +} + +inline bool SSLClient::is_valid() const { return ctx_; } + +inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store) { + if (ctx_) { + if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { + // Free memory allocated for old cert and use new store `ca_cert_store` + SSL_CTX_set_cert_store(ctx_, ca_cert_store); + } + } else { + X509_STORE_free(ca_cert_store); + } + } +} + +inline void SSLClient::load_ca_cert_store(const char *ca_cert, + std::size_t size) { + set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); +} + +inline long SSLClient::get_openssl_verify_result() const { + return verify_result_; +} + +inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } + +inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { + return is_valid() && ClientImpl::create_and_connect_socket(socket, error); +} + +// Assumes that socket_mutex_ is locked and that there are no requests in flight +inline bool SSLClient::connect_with_proxy( + Socket &socket, + std::chrono::time_point start_time, + Response &res, bool &success, Error &error) { + success = true; + Response proxy_res; + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, + start_time, [&](Stream &strm) { + Request req2; + req2.method = "CONNECT"; + req2.path = host_and_port_; + if (max_timeout_msec_ > 0) { + req2.start_time_ = std::chrono::steady_clock::now(); + } + return process_request(strm, req2, proxy_res, false, error); + })) { + // Thread-safe to close everything because we are assuming there are no + // requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + + if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) { + if (!proxy_digest_auth_username_.empty() && + !proxy_digest_auth_password_.empty()) { + std::map auth; + if (detail::parse_www_authenticate(proxy_res, auth, true)) { + proxy_res = Response(); + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, + start_time, [&](Stream &strm) { + Request req3; + req3.method = "CONNECT"; + req3.path = host_and_port_; + req3.headers.insert(detail::make_digest_authentication_header( + req3, auth, 1, detail::random_string(10), + proxy_digest_auth_username_, proxy_digest_auth_password_, + true)); + if (max_timeout_msec_ > 0) { + req3.start_time_ = std::chrono::steady_clock::now(); + } + return process_request(strm, req3, proxy_res, false, error); + })) { + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + success = false; + return false; + } + } + } + } + + // If status code is not 200, proxy request is failed. + // Set error to ProxyConnection and return proxy response + // as the response of the request + if (proxy_res.status != StatusCode::OK_200) { + error = Error::ProxyConnection; + res = std::move(proxy_res); + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + return false; + } + + return true; +} + +inline bool SSLClient::load_certs() { + auto ret = true; + + std::call_once(initialize_cert_, [&]() { + std::lock_guard guard(ctx_mutex_); + if (!ca_cert_file_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), + nullptr)) { + ret = false; + } + } else if (!ca_cert_dir_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, nullptr, + ca_cert_dir_path_.c_str())) { + ret = false; + } + } else { + auto loaded = false; +#ifdef _WIN32 + loaded = + detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#if TARGET_OS_OSX + loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); +#endif // TARGET_OS_OSX +#endif // _WIN32 + if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } + } + }); + + return ret; +} + +inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { + auto ssl = detail::ssl_new( + socket.sock, ctx_, ctx_mutex_, + [&](SSL *ssl2) { + if (server_certificate_verification_) { + if (!load_certs()) { + error = Error::SSLLoadingCerts; + return false; + } + SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); + } + + if (!detail::ssl_connect_or_accept_nonblocking( + socket.sock, ssl2, SSL_connect, connection_timeout_sec_, + connection_timeout_usec_)) { + error = Error::SSLConnection; + return false; + } + + if (server_certificate_verification_) { + if (server_certificate_verifier_) { + if (!server_certificate_verifier_(ssl2)) { + error = Error::SSLServerVerification; + return false; + } + } else { + verify_result_ = SSL_get_verify_result(ssl2); + + if (verify_result_ != X509_V_OK) { + error = Error::SSLServerVerification; + return false; + } + + auto server_cert = SSL_get1_peer_certificate(ssl2); + auto se = detail::scope_exit([&] { X509_free(server_cert); }); + + if (server_cert == nullptr) { + error = Error::SSLServerVerification; + return false; + } + + if (server_hostname_verification_) { + if (!verify_host(server_cert)) { + error = Error::SSLServerHostnameVerification; + return false; + } + } + } + } + + return true; + }, + [&](SSL *ssl2) { +#if defined(OPENSSL_IS_BORINGSSL) + SSL_set_tlsext_host_name(ssl2, host_.c_str()); +#else + // NOTE: Direct call instead of using the OpenSSL macro to suppress + // -Wold-style-cast warning + SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, + static_cast(const_cast(host_.c_str()))); +#endif + return true; + }); + + if (ssl) { + socket.ssl = ssl; + return true; + } + + shutdown_socket(socket); + close_socket(socket); + return false; +} + +inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { + shutdown_ssl_impl(socket, shutdown_gracefully); +} + +inline void SSLClient::shutdown_ssl_impl(Socket &socket, + bool shutdown_gracefully) { + if (socket.sock == INVALID_SOCKET) { + assert(socket.ssl == nullptr); + return; + } + if (socket.ssl) { + detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, + shutdown_gracefully); + socket.ssl = nullptr; + } + assert(socket.ssl == nullptr); +} + +inline bool SSLClient::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { + assert(socket.ssl); + return detail::process_client_socket_ssl( + socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, + std::move(callback)); +} + +inline bool SSLClient::is_ssl() const { return true; } + +inline bool SSLClient::verify_host(X509 *server_cert) const { + /* Quote from RFC2818 section 3.1 "Server Identity" + + If a subjectAltName extension of type dNSName is present, that MUST + be used as the identity. Otherwise, the (most specific) Common Name + field in the Subject field of the certificate MUST be used. Although + the use of the Common Name is existing practice, it is deprecated and + Certification Authorities are encouraged to use the dNSName instead. + + Matching is performed using the matching rules specified by + [RFC2459]. If more than one identity of a given type is present in + the certificate (e.g., more than one dNSName name, a match in any one + of the set is considered acceptable.) Names may contain the wildcard + character * which is considered to match any single domain name + component or component fragment. E.g., *.a.com matches foo.a.com but + not bar.foo.a.com. f*.com matches foo.com but not bar.com. + + In some cases, the URI is specified as an IP address rather than a + hostname. In this case, the iPAddress subjectAltName must be present + in the certificate and must exactly match the IP in the URI. + + */ + return verify_host_with_subject_alt_name(server_cert) || + verify_host_with_common_name(server_cert); +} + +inline bool +SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { + auto ret = false; + + auto type = GEN_DNS; + + struct in6_addr addr6{}; + struct in_addr addr{}; + size_t addr_len = 0; + +#ifndef __MINGW32__ + if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { + type = GEN_IPADD; + addr_len = sizeof(struct in6_addr); + } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { + type = GEN_IPADD; + addr_len = sizeof(struct in_addr); + } +#endif + + auto alt_names = static_cast( + X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); + + if (alt_names) { + auto dsn_matched = false; + auto ip_matched = false; + + auto count = sk_GENERAL_NAME_num(alt_names); + + for (decltype(count) i = 0; i < count && !dsn_matched; i++) { + auto val = sk_GENERAL_NAME_value(alt_names, i); + if (val->type == type) { + auto name = + reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); + auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); + + switch (type) { + case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + + case GEN_IPADD: + if (!memcmp(&addr6, name, addr_len) || + !memcmp(&addr, name, addr_len)) { + ip_matched = true; + } + break; + } + } + } + + if (dsn_matched || ip_matched) { ret = true; } + } + + GENERAL_NAMES_free(const_cast( + reinterpret_cast(alt_names))); + return ret; +} + +inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { + const auto subject_name = X509_get_subject_name(server_cert); + + if (subject_name != nullptr) { + char name[BUFSIZ]; + auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, + name, sizeof(name)); + + if (name_len != -1) { + return check_host_name(name, static_cast(name_len)); + } + } + + return false; +} + +inline bool SSLClient::check_host_name(const char *pattern, + size_t pattern_len) const { + if (host_.size() == pattern_len && host_ == pattern) { return true; } + + // Wildcard match + // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 + std::vector pattern_components; + detail::split(&pattern[0], &pattern[pattern_len], '.', + [&](const char *b, const char *e) { + pattern_components.emplace_back(b, e); + }); + + if (host_components_.size() != pattern_components.size()) { return false; } + + auto itr = pattern_components.begin(); + for (const auto &h : host_components_) { + auto &p = *itr; + if (p != h && p != "*") { + auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && + !p.compare(0, p.size() - 1, h)); + if (!partial_match) { return false; } + } + ++itr; + } + + return true; +} +#endif + +// Universal client implementation +inline Client::Client(const std::string &scheme_host_port) + : Client(scheme_host_port, std::string(), std::string()) {} + +inline Client::Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path) { + const static std::regex re( + R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); + + std::smatch m; + if (std::regex_match(scheme_host_port, m, re)) { + auto scheme = m[1].str(); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!scheme.empty() && (scheme != "http" && scheme != "https")) { +#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; + } + + auto is_ssl = scheme == "https"; + + auto host = m[2].str(); + if (host.empty()) { host = m[3].str(); } + + auto port_str = m[4].str(); + auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); + + if (is_ssl) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + is_ssl_ = is_ssl; +#endif + } else { + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + } + } else { + // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) + // if port param below changes. + cli_ = detail::make_unique(scheme_host_port, 80, + client_cert_path, client_key_path); + } +} // namespace detail + +inline Client::Client(const std::string &host, int port) + : cli_(detail::make_unique(host, port)) {} + +inline Client::Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : cli_(detail::make_unique(host, port, client_cert_path, + client_key_path)) {} + +inline Client::~Client() = default; + +inline bool Client::is_valid() const { + return cli_ != nullptr && cli_->is_valid(); +} + +inline Result Client::Get(const std::string &path) { return cli_->Get(path); } +inline Result Client::Get(const std::string &path, const Headers &headers) { + return cli_->Get(path, headers); +} +inline Result Client::Get(const std::string &path, Progress progress) { + return cli_->Get(path, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + Progress progress) { + return cli_->Get(path, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, Progress progress) { + return cli_->Get(path, params, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, params, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} + +inline Result Client::Head(const std::string &path) { return cli_->Head(path); } +inline Result Client::Head(const std::string &path, const Headers &headers) { + return cli_->Head(path, headers); +} + +inline Result Client::Post(const std::string &path) { return cli_->Post(path); } +inline Result Client::Post(const std::string &path, const Headers &headers) { + return cli_->Post(path, headers); +} +inline Result Client::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Post(path, body, content_length, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Post(path, headers, body, content_length, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Post(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Post(path, body, content_type); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Post(path, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Post(path, headers, body, content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Post(path, headers, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Post(path, std::move(content_provider), content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Post(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Post(const std::string &path, const Params ¶ms) { + return cli_->Post(path, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Post(path, headers, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Post(path, headers, params, progress); +} +inline Result Client::Post(const std::string &path, + const MultipartFormDataItems &items) { + return cli_->Post(path, items); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + return cli_->Post(path, headers, items); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + return cli_->Post(path, headers, items, boundary); +} +inline Result +Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + return cli_->Post(path, headers, items, provider_items); +} +inline Result Client::Put(const std::string &path) { return cli_->Put(path); } +inline Result Client::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Put(path, body, content_length, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Put(path, headers, body, content_length, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, Progress progress) { + return cli_->Put(path, headers, body, content_length, content_type, progress); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Put(path, body, content_type); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Put(path, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Put(path, headers, body, content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, Progress progress) { + return cli_->Put(path, headers, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Put(path, std::move(content_provider), content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Put(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Put(const std::string &path, const Params ¶ms) { + return cli_->Put(path, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Put(path, headers, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Put(path, headers, params, progress); +} +inline Result Client::Put(const std::string &path, + const MultipartFormDataItems &items) { + return cli_->Put(path, items); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items) { + return cli_->Put(path, headers, items); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const std::string &boundary) { + return cli_->Put(path, headers, items, boundary); +} +inline Result +Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItems &items, + const MultipartFormDataProviderItems &provider_items) { + return cli_->Put(path, headers, items, provider_items); +} +inline Result Client::Patch(const std::string &path) { + return cli_->Patch(path); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Patch(path, body, content_length, content_type); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, body, content_length, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Patch(path, headers, body, content_length, content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Patch(path, body, content_type); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Patch(path, headers, body, content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Patch(path, headers, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Patch(path, std::move(content_provider), content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type) { + return cli_->Patch(path, headers, std::move(content_provider), content_type); +} +inline Result Client::Delete(const std::string &path) { + return cli_->Delete(path); +} +inline Result Client::Delete(const std::string &path, const Headers &headers) { + return cli_->Delete(path, headers); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type) { + return cli_->Delete(path, body, content_length, content_type); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, body, content_length, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type) { + return cli_->Delete(path, headers, body, content_length, content_type); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type) { + return cli_->Delete(path, body, content_type); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, body, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type) { + return cli_->Delete(path, headers, body, content_type); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + Progress progress) { + return cli_->Delete(path, headers, body, content_type, progress); +} +inline Result Client::Options(const std::string &path) { + return cli_->Options(path); +} +inline Result Client::Options(const std::string &path, const Headers &headers) { + return cli_->Options(path, headers); +} + +inline bool Client::send(Request &req, Response &res, Error &error) { + return cli_->send(req, res, error); +} + +inline Result Client::send(const Request &req) { return cli_->send(req); } + +inline void Client::stop() { cli_->stop(); } + +inline std::string Client::host() const { return cli_->host(); } + +inline int Client::port() const { return cli_->port(); } + +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + +inline socket_t Client::socket() const { return cli_->socket(); } + +inline void +Client::set_hostname_addr_map(std::map addr_map) { + cli_->set_hostname_addr_map(std::move(addr_map)); +} + +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); +} + +inline void Client::set_header_writer( + std::function const &writer) { + cli_->set_header_writer(writer); +} + +inline void Client::set_address_family(int family) { + cli_->set_address_family(family); +} + +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } + +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(std::move(socket_options)); +} + +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); +} + +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); +} + +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); +} + +inline void Client::set_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_basic_auth(username, password); +} +inline void Client::set_bearer_token_auth(const std::string &token) { + cli_->set_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_digest_auth(username, password); +} +#endif + +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); +} + +inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); } + +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const std::string &intf) { + cli_->set_interface(intf); +} + +inline void Client::set_proxy(const std::string &host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const std::string &token) { + cli_->set_proxy_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_digest_auth(username, password); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); +} + +inline void Client::enable_server_hostname_verification(bool enabled) { + cli_->enable_server_hostname_verification(enabled); +} + +inline void Client::set_server_certificate_verifier( + std::function verifier) { + cli_->set_server_certificate_verifier(verifier); +} +#endif + +inline void Client::set_logger(Logger logger) { + cli_->set_logger(std::move(logger)); +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +} + +inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } else { + cli_->set_ca_cert_store(ca_cert_store); + } +} + +inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { + set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); +} + +inline long Client::get_openssl_verify_result() const { + if (is_ssl_) { + return static_cast(*cli_).get_openssl_verify_result(); + } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +} + +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; +} +#endif + +// ---------------------------------------------------------------------------- + +} // namespace httplib + +#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL) +#undef poll +#endif + +#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/benchmark/cpp-httplib-v19/main.cpp b/benchmark/cpp-httplib-v19/main.cpp new file mode 100644 index 0000000000..86070a100c --- /dev/null +++ b/benchmark/cpp-httplib-v19/main.cpp @@ -0,0 +1,12 @@ +#include "./httplib.h" +using namespace httplib; + +int main() { + Server svr; + + svr.Get("/", [](const Request &, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + svr.listen("0.0.0.0", 8080); +} From c765584e6b1055fe0dfe3e9e6d1b4b09aa305070 Mon Sep 17 00:00:00 2001 From: davidalo Date: Sun, 16 Mar 2025 20:51:53 +0100 Subject: [PATCH 0409/1222] Add zstd support (#2088) * Add zstd support * Add zstd to CI tests * Use use zstd cmake target instead of ZSTD. Use cmake variable for found packages * Add missing comment for HTTPLIB_REQUIRE_ZSTD * Fix test.yaml rebase error * Use zstd::libzstd target * Add include and library paths to ZSTD args * Run clang-format * Add zstd to httplibConfig.cmake.in --- .github/workflows/test.yaml | 6 +- CMakeLists.txt | 15 +++ cmake/httplibConfig.cmake.in | 5 + httplib.h | 119 ++++++++++++++++- test/Makefile | 5 +- test/test.cc | 251 ++++++++++++++++++++++++++++++++++- 6 files changed, 393 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7763d379f5..296ad29b38 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -66,7 +66,8 @@ jobs: sudo apt-get update sudo apt-get install -y libc6-dev${{ matrix.config.arch_suffix }} libstdc++-13-dev${{ matrix.config.arch_suffix }} \ libssl-dev${{ matrix.config.arch_suffix }} libcurl4-openssl-dev${{ matrix.config.arch_suffix }} \ - zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} + zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} \ + libzstd-dev${{ matrix.config.arch_suffix }} - name: build and run tests run: cd test && make EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}" - name: run fuzz test target @@ -126,7 +127,7 @@ jobs: - name: Setup msbuild on windows uses: microsoft/setup-msbuild@v2 - name: Install vcpkg dependencies - run: vcpkg install gtest curl zlib brotli + run: vcpkg install gtest curl zlib brotli zstd - name: Install OpenSSL if: ${{ matrix.config.with_ssl }} run: choco install openssl @@ -139,6 +140,7 @@ jobs: -DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }} -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON + -DHTTPLIB_REQUIRE_ZSTD=ON -DHTTPLIB_REQUIRE_OPENSSL=${{ matrix.config.with_ssl && 'ON' || 'OFF' }} - name: Build ${{ matrix.config.name }} run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine diff --git a/CMakeLists.txt b/CMakeLists.txt index 61419c63a8..0353b0ca38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,11 @@ * HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on) * HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on) * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) + * HTTPLIB_USE_ZSTD_IF_AVAILABLE (default on) * HTTPLIB_REQUIRE_OPENSSL (default off) * HTTPLIB_REQUIRE_ZLIB (default off) * HTTPLIB_REQUIRE_BROTLI (default off) + * HTTPLIB_REQUIRE_ZSTD (default off) * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) * HTTPLIB_COMPILE (default off) * HTTPLIB_INSTALL (default on) @@ -45,6 +47,7 @@ * HTTPLIB_IS_USING_OPENSSL - a bool for if OpenSSL support is enabled. * HTTPLIB_IS_USING_ZLIB - a bool for if ZLIB support is enabled. * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. + * HTTPLIB_IS_USING_ZSTD - a bool for if ZSTD support is enabled. * HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled. * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only. * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include). @@ -101,6 +104,8 @@ option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON) +option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF) +option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON) # Defaults to static library option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) @@ -153,6 +158,14 @@ elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE) set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND}) endif() +if(HTTPLIB_REQUIRE_ZSTD) + find_package(zstd REQUIRED) + set(HTTPLIB_IS_USING_ZSTD TRUE) +elseif(HTTPLIB_USE_ZSTD_IF_AVAILABLE) + find_package(zstd QUIET) + set(HTTPLIB_IS_USING_ZSTD ${zstd_FOUND}) +endif() + # Used for default, common dirs that the end-user can change (if needed) # like CMAKE_INSTALL_INCLUDEDIR or CMAKE_INSTALL_DATADIR include(GNUInstallDirs) @@ -227,6 +240,7 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:Brotli::encoder> $<$:Brotli::decoder> $<$:ZLIB::ZLIB> + $<$:zstd::libzstd> $<$:OpenSSL::SSL> $<$:OpenSSL::Crypto> ) @@ -236,6 +250,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_NO_EXCEPTIONS> $<$:CPPHTTPLIB_BROTLI_SUPPORT> $<$:CPPHTTPLIB_ZLIB_SUPPORT> + $<$:CPPHTTPLIB_ZSTD_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> $<$,$,$>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> ) diff --git a/cmake/httplibConfig.cmake.in b/cmake/httplibConfig.cmake.in index 93dff323de..918bea3d7e 100644 --- a/cmake/httplibConfig.cmake.in +++ b/cmake/httplibConfig.cmake.in @@ -35,6 +35,10 @@ if(@HTTPLIB_IS_USING_BROTLI@) find_dependency(Brotli COMPONENTS common encoder decoder) endif() +if(@HTTPLIB_IS_USING_ZSTD@) + find_dependency(zstd) +endif() + # Mildly useful for end-users # Not really recommended to be used though set_and_check(HTTPLIB_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@") @@ -46,6 +50,7 @@ set_and_check(HTTPLIB_HEADER_PATH "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@/httpl set(httplib_OpenSSL_FOUND @HTTPLIB_IS_USING_OPENSSL@) set(httplib_ZLIB_FOUND @HTTPLIB_IS_USING_ZLIB@) set(httplib_Brotli_FOUND @HTTPLIB_IS_USING_BROTLI@) +set(httplib_zstd_FOUND @HTTPLIB_IS_USING_ZSTD@) check_required_components(httplib) diff --git a/httplib.h b/httplib.h index 3f2bdaccfd..60686e3032 100644 --- a/httplib.h +++ b/httplib.h @@ -312,6 +312,10 @@ using socket_t = int; #include #endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +#include +#endif + /* * Declaration */ @@ -2445,7 +2449,7 @@ ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); -enum class EncodingType { None = 0, Gzip, Brotli }; +enum class EncodingType { None = 0, Gzip, Brotli, Zstd }; EncodingType encoding_type(const Request &req, const Response &res); @@ -2558,6 +2562,34 @@ class brotli_decompressor final : public decompressor { }; #endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +class zstd_compressor : public compressor { +public: + zstd_compressor(); + ~zstd_compressor(); + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override; + +private: + ZSTD_CCtx *ctx_ = nullptr; +}; + +class zstd_decompressor : public decompressor { +public: + zstd_decompressor(); + ~zstd_decompressor(); + + bool is_valid() const override; + + bool decompress(const char *data, size_t data_length, + Callback callback) override; + +private: + ZSTD_DCtx *ctx_ = nullptr; +}; +#endif + // NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` // to store data. The call can set memory on stack for performance. class stream_line_reader { @@ -3949,6 +3981,12 @@ inline EncodingType encoding_type(const Request &req, const Response &res) { if (ret) { return EncodingType::Gzip; } #endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + // TODO: 'Accept-Encoding' has zstd, not zstd;q=0 + ret = s.find("zstd") != std::string::npos; + if (ret) { return EncodingType::Zstd; } +#endif + return EncodingType::None; } @@ -4157,6 +4195,61 @@ inline bool brotli_decompressor::decompress(const char *data, } #endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +inline zstd_compressor::zstd_compressor() { + ctx_ = ZSTD_createCCtx(); + ZSTD_CCtx_setParameter(ctx_, ZSTD_c_compressionLevel, ZSTD_fast); +} + +inline zstd_compressor::~zstd_compressor() { ZSTD_freeCCtx(ctx_); } + +inline bool zstd_compressor::compress(const char *data, size_t data_length, + bool last, Callback callback) { + std::array buff{}; + + ZSTD_EndDirective mode = last ? ZSTD_e_end : ZSTD_e_continue; + ZSTD_inBuffer input = {data, data_length, 0}; + + bool finished; + do { + ZSTD_outBuffer output = {buff.data(), CPPHTTPLIB_COMPRESSION_BUFSIZ, 0}; + size_t const remaining = ZSTD_compressStream2(ctx_, &output, &input, mode); + + if (ZSTD_isError(remaining)) { return false; } + + if (!callback(buff.data(), output.pos)) { return false; } + + finished = last ? (remaining == 0) : (input.pos == input.size); + + } while (!finished); + + return true; +} + +inline zstd_decompressor::zstd_decompressor() { ctx_ = ZSTD_createDCtx(); } + +inline zstd_decompressor::~zstd_decompressor() { ZSTD_freeDCtx(ctx_); } + +inline bool zstd_decompressor::is_valid() const { return ctx_ != nullptr; } + +inline bool zstd_decompressor::decompress(const char *data, size_t data_length, + Callback callback) { + std::array buff{}; + ZSTD_inBuffer input = {data, data_length, 0}; + + while (input.pos < input.size) { + ZSTD_outBuffer output = {buff.data(), CPPHTTPLIB_COMPRESSION_BUFSIZ, 0}; + size_t const remaining = ZSTD_decompressStream(ctx_, &output, &input); + + if (ZSTD_isError(remaining)) { return false; } + + if (!callback(buff.data(), output.pos)) { return false; } + } + + return true; +} +#endif + inline bool has_header(const Headers &headers, const std::string &key) { return headers.find(key) != headers.end(); } @@ -4397,6 +4490,13 @@ bool prepare_content_receiver(T &x, int &status, #else status = StatusCode::UnsupportedMediaType_415; return false; +#endif + } else if (encoding == "zstd") { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + decompressor = detail::make_unique(); +#else + status = StatusCode::UnsupportedMediaType_415; + return false; #endif } @@ -6634,6 +6734,10 @@ Server::write_content_with_provider(Stream &strm, const Request &req, } else if (type == detail::EncodingType::Brotli) { #ifdef CPPHTTPLIB_BROTLI_SUPPORT compressor = detail::make_unique(); +#endif + } else if (type == detail::EncodingType::Zstd) { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + compressor = detail::make_unique(); #endif } else { compressor = detail::make_unique(); @@ -7049,6 +7153,8 @@ inline void Server::apply_ranges(const Request &req, Response &res, res.set_header("Content-Encoding", "gzip"); } else if (type == detail::EncodingType::Brotli) { res.set_header("Content-Encoding", "br"); + } else if (type == detail::EncodingType::Zstd) { + res.set_header("Content-Encoding", "zstd"); } } } @@ -7088,6 +7194,11 @@ inline void Server::apply_ranges(const Request &req, Response &res, #ifdef CPPHTTPLIB_BROTLI_SUPPORT compressor = detail::make_unique(); content_encoding = "br"; +#endif + } else if (type == detail::EncodingType::Zstd) { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + compressor = detail::make_unique(); + content_encoding = "zstd"; #endif } @@ -7812,6 +7923,10 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, #ifdef CPPHTTPLIB_ZLIB_SUPPORT if (!accept_encoding.empty()) { accept_encoding += ", "; } accept_encoding += "gzip, deflate"; +#endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "zstd"; #endif req.set_header("Accept-Encoding", accept_encoding); } @@ -10377,4 +10492,4 @@ inline SSL_CTX *Client::ssl_context() const { } // namespace httplib -#endif // CPPHTTPLIB_HTTPLIB_H +#endif // CPPHTTPLIB_HTTPLIB_H \ No newline at end of file diff --git a/test/Makefile b/test/Makefile index 48cd3abb2e..3107702beb 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,7 +18,10 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -pthread -lcurl +ZSTD_DIR = $(PREFIX)/opt/zstd +ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -I$(ZSTD_DIR)/include -L$(ZSTD_DIR)/lib -lzstd + +TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) -pthread -lcurl # By default, use standalone_fuzz_target_runner. # This runner does no fuzzing, but simply executes the inputs diff --git a/test/test.cc b/test/test.cc index 81a5e33a6c..725af16551 100644 --- a/test/test.cc +++ b/test/test.cc @@ -668,7 +668,7 @@ TEST(ParseAcceptEncoding1, AcceptEncoding) { TEST(ParseAcceptEncoding2, AcceptEncoding) { Request req; - req.set_header("Accept-Encoding", "gzip, deflate, br"); + req.set_header("Accept-Encoding", "gzip, deflate, br, zstd"); Response res; res.set_header("Content-Type", "text/plain"); @@ -679,6 +679,8 @@ TEST(ParseAcceptEncoding2, AcceptEncoding) { EXPECT_TRUE(ret == detail::EncodingType::Brotli); #elif CPPHTTPLIB_ZLIB_SUPPORT EXPECT_TRUE(ret == detail::EncodingType::Gzip); +#elif CPPHTTPLIB_ZSTD_SUPPORT + EXPECT_TRUE(ret == detail::EncodingType::Zstd); #else EXPECT_TRUE(ret == detail::EncodingType::None); #endif @@ -686,7 +688,8 @@ TEST(ParseAcceptEncoding2, AcceptEncoding) { TEST(ParseAcceptEncoding3, AcceptEncoding) { Request req; - req.set_header("Accept-Encoding", "br;q=1.0, gzip;q=0.8, *;q=0.1"); + req.set_header("Accept-Encoding", + "br;q=1.0, gzip;q=0.8, zstd;q=0.8, *;q=0.1"); Response res; res.set_header("Content-Type", "text/plain"); @@ -697,6 +700,8 @@ TEST(ParseAcceptEncoding3, AcceptEncoding) { EXPECT_TRUE(ret == detail::EncodingType::Brotli); #elif CPPHTTPLIB_ZLIB_SUPPORT EXPECT_TRUE(ret == detail::EncodingType::Gzip); +#elif CPPHTTPLIB_ZSTD_SUPPORT + EXPECT_TRUE(ret == detail::EncodingType::Zstd); #else EXPECT_TRUE(ret == detail::EncodingType::None); #endif @@ -3007,7 +3012,8 @@ class ServerTest : public ::testing::Test { const httplib::ContentReader &) { res.set_content("ok", "text/plain"); }) -#if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT) +#if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT) || \ + defined(CPPHTTPLIB_ZSTD_SUPPORT) .Get("/compress", [&](const Request & /*req*/, Response &res) { res.set_content( @@ -4928,6 +4934,245 @@ TEST_F(ServerTest, Brotli) { } #endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +TEST_F(ServerTest, Zstd) { + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + auto res = cli_.Get("/compress", headers); + + ASSERT_TRUE(res); + EXPECT_EQ("zstd", res->get_header_value("Content-Encoding")); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("26", res->get_header_value("Content-Length")); + EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" + "7890123456789012345678901234567890", + res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(ServerTest, ZstdWithoutAcceptEncoding) { + Headers headers; + headers.emplace("Accept-Encoding", ""); + auto res = cli_.Get("/compress", headers); + + ASSERT_TRUE(res); + EXPECT_TRUE(res->get_header_value("Content-Encoding").empty()); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("100", res->get_header_value("Content-Length")); + EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" + "7890123456789012345678901234567890", + res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(ServerTest, ZstdWithContentReceiver) { + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + std::string body; + auto res = cli_.Get("/compress", headers, + [&](const char *data, uint64_t data_length) { + EXPECT_EQ(100U, data_length); + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ("zstd", res->get_header_value("Content-Encoding")); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("26", res->get_header_value("Content-Length")); + EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" + "7890123456789012345678901234567890", + body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(ServerTest, ZstdWithoutDecompressing) { + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + + cli_.set_decompress(false); + auto res = cli_.Get("/compress", headers); + + unsigned char compressed[26] = {0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x64, 0x8d, + 0x00, 0x00, 0x50, 0x31, 0x32, 0x33, 0x34, + 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x01, + 0x00, 0xd7, 0xa9, 0x20, 0x01}; + + ASSERT_TRUE(res); + EXPECT_EQ("zstd", res->get_header_value("Content-Encoding")); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("26", res->get_header_value("Content-Length")); + EXPECT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ(26U, res->body.size()); + EXPECT_TRUE(std::memcmp(compressed, res->body.data(), sizeof(compressed)) == + 0); +} + +TEST_F(ServerTest, ZstdWithContentReceiverWithoutAcceptEncoding) { + Headers headers; + headers.emplace("Accept-Encoding", ""); + + std::string body; + auto res = cli_.Get("/compress", headers, + [&](const char *data, uint64_t data_length) { + EXPECT_EQ(100U, data_length); + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_TRUE(res->get_header_value("Content-Encoding").empty()); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("100", res->get_header_value("Content-Length")); + EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" + "7890123456789012345678901234567890", + body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(ServerTest, NoZstd) { + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + auto res = cli_.Get("/nocompress", headers); + + ASSERT_TRUE(res); + EXPECT_EQ(false, res->has_header("Content-Encoding")); + EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type")); + EXPECT_EQ("100", res->get_header_value("Content-Length")); + EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" + "7890123456789012345678901234567890", + res->body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(ServerTest, NoZstdWithContentReceiver) { + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + std::string body; + auto res = cli_.Get("/nocompress", headers, + [&](const char *data, uint64_t data_length) { + EXPECT_EQ(100U, data_length); + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(false, res->has_header("Content-Encoding")); + EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type")); + EXPECT_EQ("100", res->get_header_value("Content-Length")); + EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456" + "7890123456789012345678901234567890", + body); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +// TODO: How to enable zstd ?? +TEST_F(ServerTest, MultipartFormDataZstd) { + MultipartFormDataItems items = { + {"key1", "test", "", ""}, + {"key2", "--abcdefg123", "", ""}, + }; + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + + cli_.set_compress(true); + auto res = cli_.Post("/compress-multipart", headers, items); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(ServerTest, PutWithContentProviderWithZstd) { + Headers headers; + headers.emplace("Accept-Encoding", "zstd"); + + cli_.set_compress(true); + auto res = cli_.Put( + "/put", headers, 3, + [](size_t /*offset*/, size_t /*length*/, DataSink &sink) { + sink.os << "PUT"; + return true; + }, + "text/plain"); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("PUT", res->body); +} + +TEST(ZstdDecompressor, ChunkedDecompression) { + std::string data; + for (size_t i = 0; i < 32 * 1024; ++i) { + data.push_back(static_cast('a' + i % 26)); + } + + std::string compressed_data; + { + httplib::detail::zstd_compressor compressor; + bool result = compressor.compress( + data.data(), data.size(), + /*last=*/true, + [&](const char *compressed_data_chunk, size_t compressed_data_size) { + compressed_data.insert(compressed_data.size(), compressed_data_chunk, + compressed_data_size); + return true; + }); + ASSERT_TRUE(result); + } + + std::string decompressed_data; + { + httplib::detail::zstd_decompressor decompressor; + + // Chunk size is chosen specifically to have a decompressed chunk size equal + // to 16384 bytes 16384 bytes is the size of decompressor output buffer + size_t chunk_size = 130; + for (size_t chunk_begin = 0; chunk_begin < compressed_data.size(); + chunk_begin += chunk_size) { + size_t current_chunk_size = + std::min(compressed_data.size() - chunk_begin, chunk_size); + bool result = decompressor.decompress( + compressed_data.data() + chunk_begin, current_chunk_size, + [&](const char *decompressed_data_chunk, + size_t decompressed_data_chunk_size) { + decompressed_data.insert(decompressed_data.size(), + decompressed_data_chunk, + decompressed_data_chunk_size); + return true; + }); + ASSERT_TRUE(result); + } + } + ASSERT_EQ(data, decompressed_data); +} + +TEST(ZstdDecompressor, Decompress) { + std::string original_text = "Compressed with ZSTD"; + unsigned char data[29] = {0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x14, 0xa1, 0x00, + 0x00, 0x43, 0x6f, 0x6d, 0x70, 0x72, 0x65, 0x73, + 0x73, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, + 0x20, 0x5a, 0x53, 0x54, 0x44}; + std::string compressed_data(data, data + sizeof(data) / sizeof(data[0])); + + std::string decompressed_data; + { + httplib::detail::zstd_decompressor decompressor; + + bool result = decompressor.decompress( + compressed_data.data(), compressed_data.size(), + [&](const char *decompressed_data_chunk, + size_t decompressed_data_chunk_size) { + decompressed_data.insert(decompressed_data.size(), + decompressed_data_chunk, + decompressed_data_chunk_size); + return true; + }); + ASSERT_TRUE(result); + } + ASSERT_EQ(original_text, decompressed_data); +} +#endif + // Sends a raw request to a server listening at HOST:PORT. static bool send_request(time_t read_timeout_sec, const std::string &req, std::string *resp = nullptr) { From 33acccb346ec336b3b3b10d3b0f22884615a35e9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 16 Mar 2025 20:29:54 -0400 Subject: [PATCH 0410/1222] Fix #2109 --- httplib.h | 5 +++-- test/test.cc | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 60686e3032..9c2f3ec00b 100644 --- a/httplib.h +++ b/httplib.h @@ -4389,7 +4389,8 @@ inline bool read_content_without_length(Stream &strm, uint64_t r = 0; for (;;) { auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); - if (n <= 0) { return false; } + if (n == 0) { return true; } + if (n < 0) { return false; } if (!out(buf, static_cast(n), r, 0)) { return false; } r += static_cast(n); @@ -10492,4 +10493,4 @@ inline SSL_CTX *Client::ssl_context() const { } // namespace httplib -#endif // CPPHTTPLIB_HTTPLIB_H \ No newline at end of file +#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/test/test.cc b/test/test.cc index 725af16551..acbd11ad4f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5571,6 +5571,9 @@ TEST(StreamingTest, NoContentLengthStreaming) { s += std::string(data, len); return true; }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("aaabbb", s); }); auto get_se = detail::scope_exit([&] { get_thread.join(); }); From 87a5ae64a416d03021d990c962805d3f0549a5a8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 16 Mar 2025 20:57:17 -0400 Subject: [PATCH 0411/1222] Fix #2097 --- httplib.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index 9c2f3ec00b..f8b052a505 100644 --- a/httplib.h +++ b/httplib.h @@ -2951,7 +2951,7 @@ inline std::string decode_url(const std::string &s, inline std::string file_extension(const std::string &path) { std::smatch m; - static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); + thread_local auto re = std::regex("\\.([a-zA-Z0-9]+)$"); if (std::regex_search(path, m, re)) { return m[1].str(); } return std::string(); } @@ -5013,7 +5013,7 @@ class MultipartFormDataParser { file_.content_type = trim_copy(header.substr(str_len(header_content_type))); } else { - static const std::regex re_content_disposition( + thread_local const std::regex re_content_disposition( R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", std::regex_constants::icase); @@ -5036,7 +5036,7 @@ class MultipartFormDataParser { it = params.find("filename*"); if (it != params.end()) { // Only allow UTF-8 encoding... - static const std::regex re_rfc5987_encoding( + thread_local const std::regex re_rfc5987_encoding( R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); std::smatch m2; @@ -5201,7 +5201,7 @@ inline std::string random_string(size_t length) { constexpr const char data[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - static thread_local std::mt19937 engine([]() { + thread_local auto engine([]() { // std::random_device might actually be deterministic on some // platforms, but due to lack of support in the c++ standard library, // doing better requires either some ugly hacks or breaking portability. @@ -5723,7 +5723,7 @@ inline bool parse_www_authenticate(const Response &res, bool is_proxy) { auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; if (res.has_header(auth_key)) { - static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + thread_local auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); auto s = res.get_header_value(auth_key); auto pos = s.find(' '); if (pos != std::string::npos) { @@ -5807,7 +5807,7 @@ inline void hosted_at(const std::string &hostname, inline std::string append_query_params(const std::string &path, const Params ¶ms) { std::string path_with_query = path; - const static std::regex re("[^?]+\\?.*"); + thread_local const std::regex re("[^?]+\\?.*"); auto delm = std::regex_match(path, re) ? '&' : '?'; path_with_query += delm + detail::params_to_query_str(params); return path_with_query; @@ -6581,7 +6581,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { if (count != 3) { return false; } } - static const std::set methods{ + thread_local const std::set methods{ "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; @@ -7581,9 +7581,9 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, if (!line_reader.getline()) { return false; } #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); + thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); #else - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); + thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); #endif std::cmatch m; @@ -7815,7 +7815,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { auto location = res.get_header_value("location"); if (location.empty()) { return false; } - const static std::regex re( + thread_local const std::regex re( R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); std::smatch m; From 0be0526085416200d3e119915f737ee0db42153e Mon Sep 17 00:00:00 2001 From: Alexandre Bouvier Date: Mon, 17 Mar 2025 01:05:55 +0000 Subject: [PATCH 0412/1222] cmake: only validate component when the required library is found (#2112) --- cmake/httplibConfig.cmake.in | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cmake/httplibConfig.cmake.in b/cmake/httplibConfig.cmake.in index 918bea3d7e..bf57364799 100644 --- a/cmake/httplibConfig.cmake.in +++ b/cmake/httplibConfig.cmake.in @@ -22,9 +22,11 @@ if(@HTTPLIB_IS_USING_OPENSSL@) # Since we use both, we need to search for both. find_dependency(OpenSSL @_HTTPLIB_OPENSSL_MIN_VER@ COMPONENTS Crypto SSL) endif() + set(httplib_OpenSSL_FOUND ${OpenSSL_FOUND}) endif() if(@HTTPLIB_IS_USING_ZLIB@) find_dependency(ZLIB) + set(httplib_ZLIB_FOUND ${ZLIB_FOUND}) endif() if(@HTTPLIB_IS_USING_BROTLI@) @@ -33,10 +35,12 @@ if(@HTTPLIB_IS_USING_BROTLI@) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") set(BROTLI_USE_STATIC_LIBS @BROTLI_USE_STATIC_LIBS@) find_dependency(Brotli COMPONENTS common encoder decoder) + set(httplib_Brotli_FOUND ${Brotli_FOUND}) endif() if(@HTTPLIB_IS_USING_ZSTD@) find_dependency(zstd) + set(httplib_zstd_FOUND ${zstd_FOUND}) endif() # Mildly useful for end-users @@ -46,12 +50,6 @@ set_and_check(HTTPLIB_INCLUDE_DIR "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@") # This is helpful if you're using Cmake's pre-compiled header feature set_and_check(HTTPLIB_HEADER_PATH "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@/httplib.h") -# Consider each library support as a "component" -set(httplib_OpenSSL_FOUND @HTTPLIB_IS_USING_OPENSSL@) -set(httplib_ZLIB_FOUND @HTTPLIB_IS_USING_ZLIB@) -set(httplib_Brotli_FOUND @HTTPLIB_IS_USING_BROTLI@) -set(httplib_zstd_FOUND @HTTPLIB_IS_USING_ZSTD@) - check_required_components(httplib) # Brings in the target library, but only if all required components are found From 7a212cfe40ccadf5f790802b95650d2b9f0f1ec2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 16 Mar 2025 21:22:22 -0400 Subject: [PATCH 0413/1222] clang-format --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index f8b052a505..c76b4fbb36 100644 --- a/httplib.h +++ b/httplib.h @@ -5723,7 +5723,8 @@ inline bool parse_www_authenticate(const Response &res, bool is_proxy) { auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; if (res.has_header(auth_key)) { - thread_local auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + thread_local auto re = + std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); auto s = res.get_header_value(auth_key); auto pos = s.find(' '); if (pos != std::string::npos) { @@ -7583,7 +7584,7 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); #else - thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); + thread_local const std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); #endif std::cmatch m; From 787a34ad7f01f20922a237d5142aae469828be72 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 16 Mar 2025 21:24:53 -0400 Subject: [PATCH 0414/1222] Release v0.20.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index c76b4fbb36..33d7f97343 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.19.0" +#define CPPHTTPLIB_VERSION "0.20.0" /* * Configuration From 65ce51aed7f15e40e8fb6d2c0a8efb10bcb40126 Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Tue, 18 Mar 2025 19:17:47 -0400 Subject: [PATCH 0415/1222] Fix start_time shadow variable (#2114) --- httplib.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 33d7f97343..836618f45f 100644 --- a/httplib.h +++ b/httplib.h @@ -3365,7 +3365,7 @@ class SocketStream final : public Stream { time_t write_timeout_sec_; time_t write_timeout_usec_; time_t max_timeout_msec_; - const std::chrono::time_point start_time; + const std::chrono::time_point start_time_; std::vector read_buff_; size_t read_buff_off_ = 0; @@ -3403,7 +3403,7 @@ class SSLSocketStream final : public Stream { time_t write_timeout_sec_; time_t write_timeout_usec_; time_t max_timeout_msec_; - const std::chrono::time_point start_time; + const std::chrono::time_point start_time_; }; #endif @@ -6060,7 +6060,7 @@ inline SocketStream::SocketStream( read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), write_timeout_usec_(write_timeout_usec), - max_timeout_msec_(max_timeout_msec), start_time(start_time), + max_timeout_msec_(max_timeout_msec), start_time_(start_time), read_buff_(read_buff_size_, 0) {} inline SocketStream::~SocketStream() = default; @@ -6158,7 +6158,7 @@ inline socket_t SocketStream::socket() const { return sock_; } inline time_t SocketStream::duration() const { return std::chrono::duration_cast( - std::chrono::steady_clock::now() - start_time) + std::chrono::steady_clock::now() - start_time_) .count(); } @@ -9200,7 +9200,7 @@ inline SSLSocketStream::SSLSocketStream( read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), write_timeout_usec_(write_timeout_usec), - max_timeout_msec_(max_timeout_msec), start_time(start_time) { + max_timeout_msec_(max_timeout_msec), start_time_(start_time) { SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); } @@ -9306,7 +9306,7 @@ inline socket_t SSLSocketStream::socket() const { return sock_; } inline time_t SSLSocketStream::duration() const { return std::chrono::duration_cast( - std::chrono::steady_clock::now() - start_time) + std::chrono::steady_clock::now() - start_time_) .count(); } From 72b35befb2421e69f0ba7de8859ad7036b4245a2 Mon Sep 17 00:00:00 2001 From: Piotr Date: Tue, 25 Mar 2025 00:14:24 +0100 Subject: [PATCH 0416/1222] Add AF_UNIX support on windows (#2115) Signed-off-by: Piotr Stankiewicz --- httplib.h | 11 +++++++++-- test/test.cc | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 836618f45f..1f06f77269 100644 --- a/httplib.h +++ b/httplib.h @@ -187,6 +187,7 @@ using ssize_t = long; #include #include #include +#include #ifndef WSA_FLAG_NO_HANDLE_INHERIT #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 @@ -3538,7 +3539,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_flags = socket_flags; } -#ifndef _WIN32 if (hints.ai_family == AF_UNIX) { const auto addrlen = host.length(); if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } @@ -3562,11 +3562,19 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, sizeof(addr) - sizeof(addr.sun_path) + addrlen); #ifndef SOCK_CLOEXEC +#ifndef _WIN32 fcntl(sock, F_SETFD, FD_CLOEXEC); +#endif #endif if (socket_options) { socket_options(sock); } +#ifdef _WIN32 + // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so avoid + // setting default_socket_options. + detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); +#endif + bool dummy; if (!bind_or_connect(sock, hints, dummy)) { close_socket(sock); @@ -3575,7 +3583,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, } return sock; } -#endif auto service = std::to_string(port); diff --git a/test/test.cc b/test/test.cc index acbd11ad4f..7100ebc0aa 100644 --- a/test/test.cc +++ b/test/test.cc @@ -70,7 +70,6 @@ static void read_file(const std::string &path, std::string &out) { fs.read(&out[0], static_cast(size)); } -#ifndef _WIN32 class UnixSocketTest : public ::testing::Test { protected: void TearDown() override { std::remove(pathname_.c_str()); } @@ -167,6 +166,7 @@ TEST_F(UnixSocketTest, abstract) { } #endif +#ifndef _WIN32 TEST(SocketStream, wait_writable_UNIX) { int fds[2]; ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); From 7dbf5471ce451364786f745da7154e2f487df716 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 24 Mar 2025 19:16:48 -0400 Subject: [PATCH 0417/1222] Fix the style error and comment --- httplib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 1f06f77269..5d2d0db27b 100644 --- a/httplib.h +++ b/httplib.h @@ -184,10 +184,10 @@ using ssize_t = long; #define NOMINMAX #endif // NOMINMAX +#include #include #include #include -#include #ifndef WSA_FLAG_NO_HANDLE_INHERIT #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 @@ -3570,8 +3570,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, if (socket_options) { socket_options(sock); } #ifdef _WIN32 - // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so avoid - // setting default_socket_options. + // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so + // remove the option. detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); #endif From dbc4af819a2cd462f0b844784be7b52c46584845 Mon Sep 17 00:00:00 2001 From: Piotr Date: Tue, 25 Mar 2025 13:36:20 +0100 Subject: [PATCH 0418/1222] Fix compilation error on windows (#2118) afunix.h uses types declared in winsock2.h, and so has to be included after it. Including afunix.h first will result in a somewhat unhelpful compilation error: error C3646: 'sun_family': unknown override specifier Signed-off-by: Piotr Stankiewicz --- httplib.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5d2d0db27b..0f981dc895 100644 --- a/httplib.h +++ b/httplib.h @@ -184,11 +184,13 @@ using ssize_t = long; #define NOMINMAX #endif // NOMINMAX -#include #include #include #include +// afunix.h uses types declared in winsock2.h, so has to be included after it. +#include + #ifndef WSA_FLAG_NO_HANDLE_INHERIT #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 #endif From 0dbe8ba1446d714f94fcc3202db765754f6ca987 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Sat, 29 Mar 2025 15:46:22 +0000 Subject: [PATCH 0419/1222] Support zstd also via pkg-config (#2121) * Support zstd also via pkg-config It doesn't always provide cmake config * Find zstd with pkg-config also in non-required case Code by @sum01, slightly modified --- CMakeLists.txt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0353b0ca38..d540151e37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -159,10 +159,26 @@ elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE) endif() if(HTTPLIB_REQUIRE_ZSTD) - find_package(zstd REQUIRED) + find_package(zstd) + if(NOT zstd_FOUND) + find_package(PkgConfig REQUIRED) + pkg_check_modules(zstd REQUIRED IMPORTED_TARGET libzstd) + add_library(zstd::libzstd ALIAS PkgConfig::zstd) + endif() set(HTTPLIB_IS_USING_ZSTD TRUE) elseif(HTTPLIB_USE_ZSTD_IF_AVAILABLE) find_package(zstd QUIET) + if(NOT zstd_FOUND) + find_package(PkgConfig QUIET) + if(PKG_CONFIG_FOUND) + pkg_check_modules(zstd QUIET IMPORTED_TARGET libzstd) + + if(TARGET PkgConfig::zstd) + add_library(zstd::libzstd ALIAS PkgConfig::zstd) + endif() + endif() + endif() + # Both find_package and PkgConf set a XXX_FOUND var set(HTTPLIB_IS_USING_ZSTD ${zstd_FOUND}) endif() From b7e33b08f17ae096de3f01a45e7e0a9ebe3c157b Mon Sep 17 00:00:00 2001 From: KTGH Date: Mon, 31 Mar 2025 20:34:28 -0400 Subject: [PATCH 0420/1222] Add missing component comment (#2124) Fix #2123 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d540151e37..b0e0fdaca8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ ------------------------------------------------------------------------------- - After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL ZLIB Brotli) is available. + After installation with Cmake, a find_package(httplib COMPONENTS OpenSSL ZLIB Brotli zstd) is available. This creates a httplib::httplib target (if found and if listed components are supported). It can be linked like so: From 3e3a8cc02f2bbd92e47b2f8a4fc8fcada112202b Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Apr 2025 22:38:50 -0400 Subject: [PATCH 0421/1222] Made the max timeout threshold for SSL longer. --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 7100ebc0aa..4fd9983bd8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8623,7 +8623,7 @@ TEST(MaxTimeoutTest, ContentStream) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(MaxTimeoutTest, ContentStreamSSL) { time_t timeout = 2000; - time_t threshold = 500; // SSL_shutdown is slow on some operating systems. + time_t threshold = 1200; // SSL_shutdown is slow on some operating systems. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); From 65d6316d65aecdc1ca02e46902782e31e57ab9f3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Apr 2025 22:40:08 -0400 Subject: [PATCH 0422/1222] Fix #2113 --- httplib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/httplib.h b/httplib.h index 0f981dc895..9af65a3cbf 100644 --- a/httplib.h +++ b/httplib.h @@ -6055,6 +6055,10 @@ inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, auto actual_timeout_msec = (std::min)(max_timeout_msec - duration_msec, timeout_msec); + if (actual_timeout_msec < 0) { + actual_timeout_msec = 0; + } + actual_timeout_sec = actual_timeout_msec / 1000; actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; } From 9e4aed482e707ffbdcee197aa455de53c6f54379 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 6 Apr 2025 09:02:25 -0400 Subject: [PATCH 0423/1222] Fix style error --- httplib.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 9af65a3cbf..0e1d5223cf 100644 --- a/httplib.h +++ b/httplib.h @@ -6055,9 +6055,7 @@ inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, auto actual_timeout_msec = (std::min)(max_timeout_msec - duration_msec, timeout_msec); - if (actual_timeout_msec < 0) { - actual_timeout_msec = 0; - } + if (actual_timeout_msec < 0) { actual_timeout_msec = 0; } actual_timeout_sec = actual_timeout_msec / 1000; actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; From caf7c55785ed0d259486c6bb80843bbc3dcc9cba Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Tue, 8 Apr 2025 21:08:41 +0100 Subject: [PATCH 0424/1222] Fix regression of #2121 (#2126) --- cmake/httplibConfig.cmake.in | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmake/httplibConfig.cmake.in b/cmake/httplibConfig.cmake.in index bf57364799..19dbe694ce 100644 --- a/cmake/httplibConfig.cmake.in +++ b/cmake/httplibConfig.cmake.in @@ -39,7 +39,25 @@ if(@HTTPLIB_IS_USING_BROTLI@) endif() if(@HTTPLIB_IS_USING_ZSTD@) - find_dependency(zstd) + set(httplib_fd_zstd_quiet_arg) + if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY) + set(httplib_fd_zstd_quiet_arg QUIET) + endif() + set(httplib_fd_zstd_required_arg) + if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED) + set(httplib_fd_zstd_required_arg REQUIRED) + endif() + find_package(zstd QUIET) + if(NOT zstd_FOUND) + find_package(PkgConfig ${httplib_fd_zstd_quiet_arg} ${httplib_fd_zstd_required_arg}) + if(PKG_CONFIG_FOUND) + pkg_check_modules(zstd ${httplib_fd_zstd_quiet_arg} ${httplib_fd_zstd_required_arg} IMPORTED_TARGET libzstd) + + if(TARGET PkgConfig::zstd) + add_library(zstd::libzstd ALIAS PkgConfig::zstd) + endif() + endif() + endif() set(httplib_zstd_FOUND ${zstd_FOUND}) endif() From 9589519d5823366d176773725751129dcb1b15fd Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 17 Apr 2025 11:52:22 -0400 Subject: [PATCH 0425/1222] Fix #2130 --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 0e1d5223cf..cb182c4129 100644 --- a/httplib.h +++ b/httplib.h @@ -7329,8 +7329,9 @@ Server::process_request(Stream &strm, const std::string &remote_addr, } // Setup `is_connection_closed` method - req.is_connection_closed = [&]() { - return !detail::is_socket_alive(strm.socket()); + auto sock = strm.socket(); + req.is_connection_closed = [sock]() { + return !detail::is_socket_alive(sock); }; // Routing From 7b752106ac42bd5b907793950d9125a0972c8e8e Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Sat, 3 May 2025 11:39:01 +0300 Subject: [PATCH 0426/1222] Merge commit from fork * fix(parser): Limit line length in getline Prevents potential infinite loop and memory exhaustion in stream_line_reader::getline by enforcing max line length. Signed-off-by: Ville Vesilehto * fix: increase default max line length to 32k LONG_QUERY_VALUE test is set at 25k. Signed-off-by: Ville Vesilehto * test(client): expect read error with too long query Adds a test case (`TooLongQueryValue`) to verify client behavior when the request URI is excessively long, exceeding `CPPHTTPLIB_MAX_LINE_LENGTH`. In this scenario, the server is expected to reset the connection. Signed-off-by: Ville Vesilehto --------- Signed-off-by: Ville Vesilehto --- httplib.h | 9 +++++++++ test/test.cc | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/httplib.h b/httplib.h index cb182c4129..a2aa24f96b 100644 --- a/httplib.h +++ b/httplib.h @@ -145,6 +145,10 @@ #define CPPHTTPLIB_LISTEN_BACKLOG 5 #endif +#ifndef CPPHTTPLIB_MAX_LINE_LENGTH +#define CPPHTTPLIB_MAX_LINE_LENGTH 32768 +#endif + /* * Headers */ @@ -3067,6 +3071,11 @@ inline bool stream_line_reader::getline() { #endif for (size_t i = 0;; i++) { + if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) { + // Treat exceptionally long lines as an error to + // prevent infinite loops/memory exhaustion + return false; + } char byte; auto n = strm_.read(&byte, 1); diff --git a/test/test.cc b/test/test.cc index 4fd9983bd8..7f5cc8a9d0 100644 --- a/test/test.cc +++ b/test/test.cc @@ -43,6 +43,9 @@ const int PORT = 1234; const string LONG_QUERY_VALUE = string(25000, '@'); const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE; +const string TOO_LONG_QUERY_VALUE = string(35000, '@'); +const string TOO_LONG_QUERY_URL = "/too-long-query-value?key=" + TOO_LONG_QUERY_VALUE; + const std::string JSON_DATA = "{\"hello\":\"world\"}"; const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB @@ -2867,6 +2870,11 @@ class ServerTest : public ::testing::Test { EXPECT_EQ(LONG_QUERY_URL, req.target); EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key")); }) + .Get("/too-long-query-value", + [&](const Request &req, Response & /*res*/) { + EXPECT_EQ(TOO_LONG_QUERY_URL, req.target); + EXPECT_EQ(TOO_LONG_QUERY_VALUE, req.get_param_value("key")); + }) .Get("/array-param", [&](const Request &req, Response & /*res*/) { EXPECT_EQ(3u, req.get_param_value_count("array")); @@ -3655,6 +3663,13 @@ TEST_F(ServerTest, LongQueryValue) { EXPECT_EQ(StatusCode::UriTooLong_414, res->status); } +TEST_F(ServerTest, TooLongQueryValue) { + auto res = cli_.Get(TOO_LONG_QUERY_URL.c_str()); + + ASSERT_FALSE(res); + EXPECT_EQ(Error::Read, res.error()); +} + TEST_F(ServerTest, TooLongHeader) { Request req; req.method = "GET"; From a0de42ebc41d5a7b24175f5766b6a811b056bf09 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 3 May 2025 17:40:34 +0900 Subject: [PATCH 0427/1222] clang-format --- test/test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 7f5cc8a9d0..a57b7c95c0 100644 --- a/test/test.cc +++ b/test/test.cc @@ -44,7 +44,8 @@ const string LONG_QUERY_VALUE = string(25000, '@'); const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE; const string TOO_LONG_QUERY_VALUE = string(35000, '@'); -const string TOO_LONG_QUERY_URL = "/too-long-query-value?key=" + TOO_LONG_QUERY_VALUE; +const string TOO_LONG_QUERY_URL = + "/too-long-query-value?key=" + TOO_LONG_QUERY_VALUE; const std::string JSON_DATA = "{\"hello\":\"world\"}"; From 3af7f2c16147f3fbc6e4d717032daf505dc1652c Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 3 May 2025 21:24:22 +0900 Subject: [PATCH 0428/1222] Release v0.20.1 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index a2aa24f96b..0aa4e62746 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.20.0" +#define CPPHTTPLIB_VERSION "0.20.1" /* * Configuration From 61893a00a42d0f5ac00c9422d63bd82ccdc56531 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 3 May 2025 22:50:47 +0900 Subject: [PATCH 0429/1222] Fix #2135 --- httplib.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 0aa4e62746..766819d89f 100644 --- a/httplib.h +++ b/httplib.h @@ -2066,7 +2066,9 @@ template inline constexpr size_t str_len(const char (&)[N]) { } inline bool is_numeric(const std::string &str) { - return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); + return !str.empty() && + std::all_of(str.cbegin(), str.cend(), + [](unsigned char c) { return std::isdigit(c); }); } inline uint64_t get_header_value_u64(const Headers &headers, From c216dc94d20e617de237860f3cbbc5b92e820e06 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 9 May 2025 18:45:31 +0900 Subject: [PATCH 0430/1222] Code cleanup --- httplib.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index 766819d89f..ae4507d722 100644 --- a/httplib.h +++ b/httplib.h @@ -1087,8 +1087,7 @@ class Server { bool listen_internal(); bool routing(Request &req, Response &res, Stream &strm); - bool handle_file_request(const Request &req, Response &res, - bool head = false); + bool handle_file_request(const Request &req, Response &res); bool dispatch_request(Request &req, Response &res, const Handlers &handlers) const; bool dispatch_request_for_content_reader( @@ -6880,8 +6879,7 @@ Server::read_content_core(Stream &strm, Request &req, Response &res, return true; } -inline bool Server::handle_file_request(const Request &req, Response &res, - bool head) { +inline bool Server::handle_file_request(const Request &req, Response &res) { for (const auto &entry : base_dirs_) { // Prefix match if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { @@ -6914,7 +6912,7 @@ inline bool Server::handle_file_request(const Request &req, Response &res, return true; }); - if (!head && file_request_handler_) { + if (req.method != "HEAD" && file_request_handler_) { file_request_handler_(req, res); } @@ -7048,9 +7046,8 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { } // File handler - auto is_head_request = req.method == "HEAD"; - if ((req.method == "GET" || is_head_request) && - handle_file_request(req, res, is_head_request)) { + if ((req.method == "GET" || req.method == "HEAD") && + handle_file_request(req, res)) { return true; } From 366eda72dce55b8092fba14264d85735b6058be5 Mon Sep 17 00:00:00 2001 From: QWERTIOX Date: Fri, 16 May 2025 16:02:32 +0200 Subject: [PATCH 0431/1222] Specify version in meson.build (#2139) I prefer to specify version when I'm calling dependency() and lack of makes me unable to do it --- meson.build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 9fa1919c8b..ae8ca314ca 100644 --- a/meson.build +++ b/meson.build @@ -87,7 +87,7 @@ if get_option('cpp-httplib_compile') soversion: version.split('.')[0] + '.' + version.split('.')[1], install: true ) - cpp_httplib_dep = declare_dependency(compile_args: args, dependencies: deps, link_with: lib, sources: httplib_ch[1]) + cpp_httplib_dep = declare_dependency(compile_args: args, dependencies: deps, link_with: lib, sources: httplib_ch[1], version: version) import('pkgconfig').generate( lib, @@ -98,7 +98,7 @@ if get_option('cpp-httplib_compile') ) else install_headers('httplib.h') - cpp_httplib_dep = declare_dependency(compile_args: args, dependencies: deps, include_directories: '.') + cpp_httplib_dep = declare_dependency(compile_args: args, dependencies: deps, include_directories: '.', version: version) import('pkgconfig').generate( name: 'cpp-httplib', From fd324e141289f86939493fe00fc102808c683efa Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 20 May 2025 21:41:50 +0900 Subject: [PATCH 0432/1222] Add KeepAliveTest.MaxCount --- test/test.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/test.cc b/test/test.cc index a57b7c95c0..0f012bd8cf 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5732,6 +5732,41 @@ TEST(KeepAliveTest, ReadTimeout) { EXPECT_EQ("b", resb->body); } +TEST(KeepAliveTest, MaxCount) { + size_t keep_alive_max_count = 3; + + Server svr; + svr.set_keep_alive_max_count(keep_alive_max_count); + + svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_keep_alive(true); + + for (size_t i = 0; i < 5; i++) { + auto result = cli.Get("/hi"); + ASSERT_TRUE(result); + EXPECT_EQ(StatusCode::OK_200, result->status); + + if (i == keep_alive_max_count - 1) { + EXPECT_EQ("close", result->get_header_value("Connection")); + } else { + EXPECT_FALSE(result->has_header("Connection")); + } + } +} + TEST(KeepAliveTest, Issue1041) { Server svr; svr.set_keep_alive_timeout(3); From 4a7aae54690c80e7d49398964ba9e1bae1dfca79 Mon Sep 17 00:00:00 2001 From: Piotr Date: Sat, 24 May 2025 05:14:48 +0200 Subject: [PATCH 0433/1222] Detect if afunix.h exists (#2145) Signed-off-by: Piotr Stankiewicz --- httplib.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/httplib.h b/httplib.h index ae4507d722..e1eb470d97 100644 --- a/httplib.h +++ b/httplib.h @@ -192,8 +192,13 @@ using ssize_t = long; #include #include +#if defined(__has_include) +#if __has_include() // afunix.h uses types declared in winsock2.h, so has to be included after it. #include +#define CPPHTTPLIB_HAVE_AFUNIX_H 1 +#endif +#endif #ifndef WSA_FLAG_NO_HANDLE_INHERIT #define WSA_FLAG_NO_HANDLE_INHERIT 0x80 @@ -3551,6 +3556,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_flags = socket_flags; } +#if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) if (hints.ai_family == AF_UNIX) { const auto addrlen = host.length(); if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } @@ -3595,6 +3601,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, } return sock; } +#endif auto service = std::to_string(port); From 365cbe37fac46726c1a9a89b1f7e5a06431d0d1d Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 25 May 2025 21:56:28 -0400 Subject: [PATCH 0434/1222] Fix #2101 --- README.md | 16 ++++++++++++++ httplib.h | 34 ++++++++++++++++++++++++++---- test/test.cc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 104 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index aed19ca6c4..e85f9bc833 100644 --- a/README.md +++ b/README.md @@ -285,6 +285,22 @@ svr.set_post_routing_handler([](const auto& req, auto& res) { }); ``` +### Pre request handler + +```cpp +svr.set_pre_request_handler([](const auto& req, auto& res) { + if (req.matched_route == "/user/:user") { + auto user = req.path_params.at("user"); + if (user != "john") { + res.status = StatusCode::Forbidden_403; + res.set_content("error", "text/html"); + return Server::HandlerResponse::Handled; + } + } + return Server::HandlerResponse::Unhandled; +}); +``` + ### 'multipart/form-data' POST data ```cpp diff --git a/httplib.h b/httplib.h index e1eb470d97..54b80d702b 100644 --- a/httplib.h +++ b/httplib.h @@ -636,6 +636,7 @@ using Ranges = std::vector; struct Request { std::string method; std::string path; + std::string matched_route; Params params; Headers headers; std::string body; @@ -887,10 +888,16 @@ namespace detail { class MatcherBase { public: + MatcherBase(std::string pattern) : pattern_(pattern) {} virtual ~MatcherBase() = default; + const std::string &pattern() const { return pattern_; } + // Match request path and populate its matches and virtual bool match(Request &request) const = 0; + +private: + std::string pattern_; }; /** @@ -942,7 +949,8 @@ class PathParamsMatcher final : public MatcherBase { */ class RegexMatcher final : public MatcherBase { public: - RegexMatcher(const std::string &pattern) : regex_(pattern) {} + RegexMatcher(const std::string &pattern) + : MatcherBase(pattern), regex_(pattern) {} bool match(Request &request) const override; @@ -1009,9 +1017,12 @@ class Server { } Server &set_exception_handler(ExceptionHandler handler); + Server &set_pre_routing_handler(HandlerWithResponse handler); Server &set_post_routing_handler(Handler handler); + Server &set_pre_request_handler(HandlerWithResponse handler); + Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); Server &set_logger(Logger logger); @@ -1153,6 +1164,7 @@ class Server { ExceptionHandler exception_handler_; HandlerWithResponse pre_routing_handler_; Handler post_routing_handler_; + HandlerWithResponse pre_request_handler_; Expect100ContinueHandler expect_100_continue_handler_; Logger logger_; @@ -6224,7 +6236,8 @@ inline time_t BufferStream::duration() const { return 0; } inline const std::string &BufferStream::get_buffer() const { return buffer; } -inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { +inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) + : MatcherBase(pattern) { constexpr const char marker[] = "/:"; // One past the last ending position of a path param substring @@ -6475,6 +6488,11 @@ inline Server &Server::set_post_routing_handler(Handler handler) { return *this; } +inline Server &Server::set_pre_request_handler(HandlerWithResponse handler) { + pre_request_handler_ = std::move(handler); + return *this; +} + inline Server &Server::set_logger(Logger logger) { logger_ = std::move(logger); return *this; @@ -7129,7 +7147,11 @@ inline bool Server::dispatch_request(Request &req, Response &res, const auto &handler = x.second; if (matcher->match(req)) { - handler(req, res); + req.matched_route = matcher->pattern(); + if (!pre_request_handler_ || + pre_request_handler_(req, res) != HandlerResponse::Handled) { + handler(req, res); + } return true; } } @@ -7256,7 +7278,11 @@ inline bool Server::dispatch_request_for_content_reader( const auto &handler = x.second; if (matcher->match(req)) { - handler(req, res, content_reader); + req.matched_route = matcher->pattern(); + if (!pre_request_handler_ || + pre_request_handler_(req, res) != HandlerResponse::Handled) { + handler(req, res, content_reader); + } return true; } } diff --git a/test/test.cc b/test/test.cc index 0f012bd8cf..de3aae5ecd 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2263,7 +2263,7 @@ TEST(NoContentTest, ContentLength) { } } -TEST(RoutingHandlerTest, PreRoutingHandler) { +TEST(RoutingHandlerTest, PreAndPostRoutingHandlers) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); @@ -2354,6 +2354,63 @@ TEST(RoutingHandlerTest, PreRoutingHandler) { } } +TEST(RequestHandlerTest, PreRequestHandler) { + auto route_path = "/user/:user"; + + Server svr; + + svr.Get("/hi", [](const Request &, Response &res) { + res.set_content("hi", "text/plain"); + }); + + svr.Get(route_path, [](const Request &req, Response &res) { + res.set_content(req.path_params.at("user"), "text/plain"); + }); + + svr.set_pre_request_handler([&](const Request &req, Response &res) { + if (req.matched_route == route_path) { + auto user = req.path_params.at("user"); + if (user != "john") { + res.status = StatusCode::Forbidden_403; + res.set_content("error", "text/html"); + return Server::HandlerResponse::Handled; + } + } + return Server::HandlerResponse::Unhandled; + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + { + auto res = cli.Get("/hi"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("hi", res->body); + } + + { + auto res = cli.Get("/user/john"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("john", res->body); + } + + { + auto res = cli.Get("/user/invalid-user"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::Forbidden_403, res->status); + EXPECT_EQ("error", res->body); + } +} + TEST(InvalidFormatTest, StatusCode) { Server svr; From fd8da4d8e4ec84e70a0d0cfc0b623cdd5d8a776c Mon Sep 17 00:00:00 2001 From: Marek Kwasecki Date: Mon, 9 Jun 2025 21:59:25 +0200 Subject: [PATCH 0435/1222] Feature/multipart headers (#2152) * Adds headers to multipart form data Adds a `headers` field to the `MultipartFormData` struct. Populates this field by parsing headers from the multipart form data. This allows access to specific headers associated with each form data part. * Adds multipart header access test Verifies the correct retrieval of headers from multipart form data file parts. Ensures that custom and content-related headers are accessible and parsed as expected. * Enables automatic test discovery with GoogleTest Uses `gtest_discover_tests` to automatically find and run tests, simplifying test maintenance and improving discoverability. * Removes explicit GoogleTest include * Refactors header parsing logic Improves header parsing by using a dedicated parsing function, resulting in cleaner and more robust code. This change also adds error handling during header parsing, returning an error and marking the request as invalid if parsing fails. * clang-format corrected * Renames variable for better readability. Renames the `customHeader` variable to `custom_header` for improved code readability and consistency. * typo --- httplib.h | 12 ++++++++++ test/test.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/httplib.h b/httplib.h index 54b80d702b..022b015054 100644 --- a/httplib.h +++ b/httplib.h @@ -544,6 +544,7 @@ struct MultipartFormData { std::string content; std::string filename; std::string content_type; + Headers headers; }; using MultipartFormDataItems = std::vector; using MultipartFormDataMap = std::multimap; @@ -5045,6 +5046,16 @@ class MultipartFormDataParser { return false; } + // parse and emplace space trimmed headers into a map + if (!parse_header( + header.data(), header.data() + header.size(), + [&](const std::string &key, const std::string &val) { + file_.headers.emplace(key, val); + })) { + is_valid_ = false; + return false; + } + constexpr const char header_content_type[] = "Content-Type:"; if (start_with_case_ignore(header, header_content_type)) { @@ -5144,6 +5155,7 @@ class MultipartFormDataParser { file_.name.clear(); file_.filename.clear(); file_.content_type.clear(); + file_.headers.clear(); } bool start_with_case_ignore(const std::string &a, const char *b) const { diff --git a/test/test.cc b/test/test.cc index de3aae5ecd..11f622a1f5 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8010,6 +8010,68 @@ TEST(MultipartFormDataTest, ContentLength) { ASSERT_TRUE(send_request(1, req, &response)); ASSERT_EQ("200", response.substr(9, 3)); } + +TEST(MultipartFormDataTest, AccessPartHeaders) { + auto handled = false; + + Server svr; + svr.Post("/test", [&](const Request &req, Response &) { + ASSERT_EQ(2u, req.files.size()); + + auto it = req.files.begin(); + ASSERT_EQ("text1", it->second.name); + ASSERT_EQ("text1", it->second.content); + ASSERT_EQ(1, it->second.headers.count("Content-Length")); + auto content_length = it->second.headers.find("CONTENT-length"); + ASSERT_EQ("5", content_length->second); + ASSERT_EQ(3, it->second.headers.size()); + + ++it; + ASSERT_EQ("text2", it->second.name); + ASSERT_EQ("text2", it->second.content); + auto &headers = it->second.headers; + ASSERT_EQ(3, headers.size()); + auto custom_header = headers.find("x-whatever"); + ASSERT_TRUE(custom_header != headers.end()); + ASSERT_NE("customvalue", custom_header->second); + ASSERT_EQ("CustomValue", custom_header->second); + ASSERT_TRUE(headers.find("X-Test") == headers.end()); // text1 header + + handled = true; + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + ASSERT_TRUE(handled); + }); + + svr.wait_until_ready(); + + auto req = "POST /test HTTP/1.1\r\n" + "Content-Type: multipart/form-data;boundary=--------\r\n" + "Content-Length: 232\r\n" + "\r\n----------\r\n" + "Content-Disposition: form-data; name=\"text1\"\r\n" + "Content-Length: 5\r\n" + "X-Test: 1\r\n" + "\r\n" + "text1" + "\r\n----------\r\n" + "Content-Disposition: form-data; name=\"text2\"\r\n" + "Content-Type: text/plain\r\n" + "X-Whatever: CustomValue\r\n" + "\r\n" + "text2" + "\r\n------------\r\n" + "That should be disregarded. Not even read"; + + std::string response; + ASSERT_TRUE(send_request(1, req, &response)); + ASSERT_EQ("200", response.substr(9, 3)); +} #endif TEST(TaskQueueTest, IncreaseAtomicInteger) { From 3a1f379e751ef6555f06c5c1ef367a6fce26722c Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Jun 2025 22:17:04 -0400 Subject: [PATCH 0436/1222] Release v0.21.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 022b015054..d9aa8f1b71 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.20.1" +#define CPPHTTPLIB_VERSION "0.21.0" /* * Configuration From 08a0452fb2fe30344d068e695df415498f087bc8 Mon Sep 17 00:00:00 2001 From: DSiekmeier Date: Wed, 18 Jun 2025 13:05:23 +0200 Subject: [PATCH 0437/1222] Update README.md (#2156) According to the curl manpage, the option is called "--max-time". --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e85f9bc833..0aa7312c54 100644 --- a/README.md +++ b/README.md @@ -671,7 +671,7 @@ cli.set_connection_timeout(0, 300000); // 300 milliseconds cli.set_read_timeout(5, 0); // 5 seconds cli.set_write_timeout(5, 0); // 5 seconds -// This method works the same as curl's `--max-timeout` option +// This method works the same as curl's `--max-time` option svr.set_max_timeout(5000); // 5 seconds ``` From 27879c4874aabfc254edd25cf58385c25ac247b7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 23 Jun 2025 08:35:56 -0400 Subject: [PATCH 0438/1222] Fix #2157 (#2158) * Fix #2157 * Fix Windows build error: wrap std::max in parentheses to avoid macro conflict - On Windows, max/min are often defined as macros by windows.h - This causes compilation errors with std::max/std::min - Solution: use (std::max) to prevent macro expansion - Fixes CI build error: error C2589: '(': illegal token on right side of '::' Fixes: error in coalesce_ranges function on line 5376 --- httplib.h | 78 +++++++++++++++++++++++++++++++++++++++++++------- test/test.cc | 80 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 142 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index d9aa8f1b71..217cb88467 100644 --- a/httplib.h +++ b/httplib.h @@ -5329,13 +5329,68 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, return body; } +inline void coalesce_ranges(Ranges &ranges, size_t content_length) { + if (ranges.size() <= 1) return; + + // Sort ranges by start position + std::sort(ranges.begin(), ranges.end(), + [](const Range &a, const Range &b) { return a.first < b.first; }); + + Ranges coalesced; + coalesced.reserve(ranges.size()); + + for (auto &r : ranges) { + auto first_pos = r.first; + auto last_pos = r.second; + + // Handle special cases like in range_error + if (first_pos == -1 && last_pos == -1) { + first_pos = 0; + last_pos = static_cast(content_length); + } + + if (first_pos == -1) { + first_pos = static_cast(content_length) - last_pos; + last_pos = static_cast(content_length) - 1; + } + + if (last_pos == -1 || last_pos >= static_cast(content_length)) { + last_pos = static_cast(content_length) - 1; + } + + // Skip invalid ranges + if (!(0 <= first_pos && first_pos <= last_pos && + last_pos < static_cast(content_length))) { + continue; + } + + // Coalesce with previous range if overlapping or adjacent (but not + // identical) + if (!coalesced.empty()) { + auto &prev = coalesced.back(); + // Check if current range overlaps or is adjacent to previous range + // but don't coalesce identical ranges (allow duplicates) + if (first_pos <= prev.second + 1 && + !(first_pos == prev.first && last_pos == prev.second)) { + // Extend the previous range + prev.second = (std::max)(prev.second, last_pos); + continue; + } + } + + // Add new range + coalesced.emplace_back(first_pos, last_pos); + } + + ranges = std::move(coalesced); +} + inline bool range_error(Request &req, Response &res) { if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { ssize_t content_len = static_cast( res.content_length_ ? res.content_length_ : res.body.size()); - ssize_t prev_first_pos = -1; - ssize_t prev_last_pos = -1; + std::vector> processed_ranges; size_t overwrapping_count = 0; // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 @@ -5378,18 +5433,21 @@ inline bool range_error(Request &req, Response &res) { return true; } - // Ranges must be in ascending order - if (first_pos <= prev_first_pos) { return true; } - // Request must not have more than two overlapping ranges - if (first_pos <= prev_last_pos) { - overwrapping_count++; - if (overwrapping_count > 2) { return true; } + for (const auto &processed_range : processed_ranges) { + if (!(last_pos < processed_range.first || + first_pos > processed_range.second)) { + overwrapping_count++; + if (overwrapping_count > 2) { return true; } + break; // Only count once per range + } } - prev_first_pos = (std::max)(prev_first_pos, first_pos); - prev_last_pos = (std::max)(prev_last_pos, last_pos); + processed_ranges.emplace_back(first_pos, last_pos); } + + // After validation, coalesce overlapping ranges as per RFC 9110 + coalesce_ranges(req.ranges, static_cast(content_len)); } return false; diff --git a/test/test.cc b/test/test.cc index 11f622a1f5..b7007555a6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3992,6 +3992,16 @@ TEST_F(ServerTest, GetStreamedWithRangeMultipart) { EXPECT_EQ("267", res->get_header_value("Content-Length")); EXPECT_EQ(false, res->has_header("Content-Range")); EXPECT_EQ(267U, res->body.size()); + + // Check that both range contents are present + EXPECT_TRUE(res->body.find("bc\r\n") != std::string::npos); + EXPECT_TRUE(res->body.find("ef\r\n") != std::string::npos); + + // Check that Content-Range headers are present for both ranges + EXPECT_TRUE(res->body.find("Content-Range: bytes 1-2/7") != + std::string::npos); + EXPECT_TRUE(res->body.find("Content-Range: bytes 4-5/7") != + std::string::npos); } TEST_F(ServerTest, GetStreamedWithTooManyRanges) { @@ -4009,14 +4019,59 @@ TEST_F(ServerTest, GetStreamedWithTooManyRanges) { EXPECT_EQ(0U, res->body.size()); } +TEST_F(ServerTest, GetStreamedWithOverwrapping) { + auto res = + cli_.Get("/streamed-with-range", {{make_range_header({{1, 4}, {2, 5}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ(5U, res->body.size()); + + // Check that overlapping ranges are coalesced into a single range + EXPECT_EQ("bcdef", res->body); + EXPECT_EQ("bytes 1-5/7", res->get_header_value("Content-Range")); + + // Should be single range, not multipart + EXPECT_TRUE(res->has_header("Content-Range")); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); +} + TEST_F(ServerTest, GetStreamedWithNonAscendingRanges) { - auto res = cli_.Get("/streamed-with-range?error", - {{make_range_header({{0, -1}, {0, -1}})}}); + auto res = + cli_.Get("/streamed-with-range", {{make_range_header({{4, 5}, {0, 2}})}}); ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::RangeNotSatisfiable_416, res->status); - EXPECT_EQ("0", res->get_header_value("Content-Length")); - EXPECT_EQ(false, res->has_header("Content-Range")); - EXPECT_EQ(0U, res->body.size()); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ(268U, res->body.size()); + + // Check that both range contents are present + EXPECT_TRUE(res->body.find("ef\r\n") != std::string::npos); + EXPECT_TRUE(res->body.find("abc\r\n") != std::string::npos); + + // Check that Content-Range headers are present for both ranges + EXPECT_TRUE(res->body.find("Content-Range: bytes 4-5/7") != + std::string::npos); + EXPECT_TRUE(res->body.find("Content-Range: bytes 0-2/7") != + std::string::npos); +} + +TEST_F(ServerTest, GetStreamedWithDuplicateRanges) { + auto res = + cli_.Get("/streamed-with-range", {{make_range_header({{0, 2}, {0, 2}})}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ(269U, res->body.size()); + + // Check that both duplicate range contents are present + size_t first_abc = res->body.find("abc\r\n"); + EXPECT_TRUE(first_abc != std::string::npos); + size_t second_abc = res->body.find("abc\r\n", first_abc + 1); + EXPECT_TRUE(second_abc != std::string::npos); + + // Check that Content-Range headers are present for both ranges + size_t first_range = res->body.find("Content-Range: bytes 0-2/7"); + EXPECT_TRUE(first_range != std::string::npos); + size_t second_range = + res->body.find("Content-Range: bytes 0-2/7", first_range + 1); + EXPECT_TRUE(second_range != std::string::npos); } TEST_F(ServerTest, GetStreamedWithRangesMoreThanTwoOverwrapping) { @@ -4122,6 +4177,19 @@ TEST_F(ServerTest, GetWithRange4) { EXPECT_EQ(std::string("fg"), res->body); } +TEST_F(ServerTest, GetWithRange5) { + auto res = cli_.Get("/with-range", { + make_range_header({{0, 5}}), + {"Accept-Encoding", ""}, + }); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PartialContent_206, res->status); + EXPECT_EQ("6", res->get_header_value("Content-Length")); + EXPECT_EQ(true, res->has_header("Content-Range")); + EXPECT_EQ("bytes 0-5/7", res->get_header_value("Content-Range")); + EXPECT_EQ(std::string("abcdef"), res->body); +} + TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) { auto res = cli_.Get("/with-range", {{make_range_header({{10000, 20000}})}}); ASSERT_TRUE(res); From 91e79e9a6345f85939a0cca5e65802f92a1df66a Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jun 2025 07:44:10 -0400 Subject: [PATCH 0439/1222] Fix #1777 (#2160) * Add benchmark unit test * Update * Update * Update * Change the default value of CPPHTTPLIB_IDLE_INTERVAL_USECOND to 1ms --- httplib.h | 2 +- test/test.cc | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 217cb88467..a5518aea1c 100644 --- a/httplib.h +++ b/httplib.h @@ -76,7 +76,7 @@ #ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND #ifdef _WIN32 -#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000 #else #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 #endif diff --git a/test/test.cc b/test/test.cc index b7007555a6..3185906b08 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3155,6 +3155,47 @@ TEST_F(ServerTest, GetMethod200) { EXPECT_EQ("Hello World!", res->body); } +TEST(BenchmarkTest, SimpleGetPerformance) { + Server svr; + + svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) { + res.set_content("Benchmark Response", "text/plain"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + const int NUM_REQUESTS = 50; + const int MAX_AVERAGE_MS = 5; + + auto warmup = cli.Get("/benchmark"); + ASSERT_TRUE(warmup); + + auto start = std::chrono::high_resolution_clock::now(); + for (int i = 0; i < NUM_REQUESTS; ++i) { + auto res = cli.Get("/benchmark"); + ASSERT_TRUE(res) << "Request " << i << " failed"; + EXPECT_EQ(StatusCode::OK_200, res->status); + } + auto end = std::chrono::high_resolution_clock::now(); + + auto total_ms = std::chrono::duration_cast(end - start).count(); + double avg_ms = static_cast(total_ms) / NUM_REQUESTS; + + std::cout << "Standalone: " << NUM_REQUESTS << " requests in " << total_ms + << "ms (avg: " << avg_ms << "ms)" << std::endl; + + EXPECT_LE(avg_ms, MAX_AVERAGE_MS) << "Standalone test too slow: " << avg_ms << "ms (Issue #1777)"; +} + TEST_F(ServerTest, GetEmptyFile) { auto res = cli_.Get("/empty_file"); ASSERT_TRUE(res); From 28dcf379e82a2cdb544d812696a7fd46067eb7f9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jun 2025 07:56:00 -0400 Subject: [PATCH 0440/1222] Merge commit from fork --- httplib.h | 17 ++++++++++++ test/test.cc | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/httplib.h b/httplib.h index a5518aea1c..b1694cf722 100644 --- a/httplib.h +++ b/httplib.h @@ -90,6 +90,10 @@ #define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 #endif +#ifndef CPPHTTPLIB_HEADER_MAX_COUNT +#define CPPHTTPLIB_HEADER_MAX_COUNT 100 +#endif + #ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT #define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 #endif @@ -4355,6 +4359,8 @@ inline bool read_headers(Stream &strm, Headers &headers) { char buf[bufsiz]; stream_line_reader line_reader(strm, buf, bufsiz); + size_t header_count = 0; + for (;;) { if (!line_reader.getline()) { return false; } @@ -4375,6 +4381,9 @@ inline bool read_headers(Stream &strm, Headers &headers) { if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + // Check header count limit + if (header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } + // Exclude line terminator auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; @@ -4384,6 +4393,8 @@ inline bool read_headers(Stream &strm, Headers &headers) { })) { return false; } + + header_count++; } return true; @@ -4486,9 +4497,13 @@ inline bool read_content_chunked(Stream &strm, T &x, // chunked transfer coding data without the final CRLF. if (!line_reader.getline()) { return true; } + size_t trailer_header_count = 0; while (strcmp(line_reader.ptr(), "\r\n") != 0) { if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + // Check trailer header count limit + if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } + // Exclude line terminator constexpr auto line_terminator_len = 2; auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; @@ -4498,6 +4513,8 @@ inline bool read_content_chunked(Stream &strm, T &x, x.headers.emplace(key, val); }); + trailer_header_count++; + if (!line_reader.getline()) { return false; } } diff --git a/test/test.cc b/test/test.cc index 3185906b08..df52529536 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3,7 +3,11 @@ #include #ifndef _WIN32 +#include #include +#include +#include +#include #endif #include @@ -3823,6 +3827,50 @@ TEST_F(ServerTest, TooLongHeader) { EXPECT_EQ(StatusCode::OK_200, res->status); } +TEST_F(ServerTest, HeaderCountAtLimit) { + // Test with headers just under the 100 limit + httplib::Headers headers; + + // Add 95 custom headers (the client will add Host, User-Agent, Accept, etc.) + // This should keep us just under the 100 header limit + for (int i = 0; i < 95; i++) { + std::string name = "X-Test-Header-" + std::to_string(i); + std::string value = "value" + std::to_string(i); + headers.emplace(name, value); + } + + // This should work fine as we're under the limit + auto res = cli_.Get("/hi", headers); + EXPECT_TRUE(res); + if (res) { + EXPECT_EQ(StatusCode::OK_200, res->status); + } +} + +TEST_F(ServerTest, HeaderCountExceedsLimit) { + // Test with many headers to exceed the 100 limit + httplib::Headers headers; + + // Add 150 headers to definitely exceed the 100 limit + for (int i = 0; i < 150; i++) { + std::string name = "X-Test-Header-" + std::to_string(i); + std::string value = "value" + std::to_string(i); + headers.emplace(name, value); + } + + // This should fail due to exceeding header count limit + auto res = cli_.Get("/hi", headers); + + // The request should either fail or return 400 Bad Request + if (res) { + // If we get a response, it should be 400 Bad Request + EXPECT_EQ(StatusCode::BadRequest_400, res->status); + } else { + // Or the request should fail entirely + EXPECT_FALSE(res); + } +} + TEST_F(ServerTest, PercentEncoding) { auto res = cli_.Get("/e%6edwith%"); ASSERT_TRUE(res); @@ -3860,6 +3908,32 @@ TEST_F(ServerTest, PlusSignEncoding) { EXPECT_EQ("a +b", res->body); } +TEST_F(ServerTest, HeaderCountSecurityTest) { + // This test simulates a potential DoS attack using many headers + // to verify our security fix prevents memory exhaustion + + httplib::Headers attack_headers; + + // Attempt to add many headers like an attacker would (200 headers to far exceed limit) + for (int i = 0; i < 200; i++) { + std::string name = "X-Attack-Header-" + std::to_string(i); + std::string value = "attack_payload_" + std::to_string(i); + attack_headers.emplace(name, value); + } + + // Try to POST with excessive headers + auto res = cli_.Post("/", attack_headers, "test_data", "text/plain"); + + // Should either fail or return 400 Bad Request due to security limit + if (res) { + // If we get a response, it should be 400 Bad Request + EXPECT_EQ(StatusCode::BadRequest_400, res->status); + } else { + // Request failed, which is the expected behavior for DoS protection + EXPECT_FALSE(res); + } +} + TEST_F(ServerTest, MultipartFormData) { MultipartFormDataItems items = { {"text1", "text default", "", ""}, From b6c55c6030f5160d1a360a5a5180ba3205e2ce2f Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jun 2025 08:01:53 -0400 Subject: [PATCH 0441/1222] Release v0.22.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index b1694cf722..778c1d4255 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.21.0" +#define CPPHTTPLIB_VERSION "0.22.0" /* * Configuration From de5a255ac650ba7915320ab610e2618d94d26f1d Mon Sep 17 00:00:00 2001 From: herbrechtsmeier Date: Tue, 24 Jun 2025 18:57:32 +0200 Subject: [PATCH 0442/1222] Fix bad request for multipart form data with boundary split (#2159) * Add test for multipart form data with boundary split Add a test for multipart form data requests with a large header which leads to a split inside the boundary because of the read buffer size inside the SocketStream class. * Fix bad request for multipart form data with boundary split Fix a bad request (400) response for multipart form data requests with a large header which leads to a split inside the boundary because of the read buffer size inside the SocketStream class. The parse function inside the MultipartFormDataParser wrongly erase the receive buffer if it doesn't find the boundary inside the buffer during first call. --- httplib.h | 7 +++---- test/test.cc | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 778c1d4255..c875fb3938 100644 --- a/httplib.h +++ b/httplib.h @@ -5028,10 +5028,9 @@ class MultipartFormDataParser { while (buf_size() > 0) { switch (state_) { case 0: { // Initial boundary - buf_erase(buf_find(dash_boundary_crlf_)); - if (dash_boundary_crlf_.size() > buf_size()) { return true; } - if (!buf_start_with(dash_boundary_crlf_)) { return false; } - buf_erase(dash_boundary_crlf_.size()); + auto pos = buf_find(dash_boundary_crlf_); + if (pos == buf_size()) { return true; } + buf_erase(pos + dash_boundary_crlf_.size()); state_ = 1; break; } diff --git a/test/test.cc b/test/test.cc index df52529536..b67c87fc2e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8257,6 +8257,60 @@ TEST(MultipartFormDataTest, AccessPartHeaders) { } #endif +TEST(MultipartFormDataTest, LargeHeader) { + auto handled = false; + + Server svr; + svr.Post("/test", [&](const Request &req, Response &) { + ASSERT_EQ(1u, req.files.size()); + + auto it = req.files.begin(); + ASSERT_EQ("name1", it->second.name); + ASSERT_EQ("text1", it->second.content); + + handled = true; + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + ASSERT_TRUE(handled); + }); + + svr.wait_until_ready(); + + auto boundary = std::string("cpp-httplib-multipart-data"); + std::string content = "--" + boundary + + "\r\n" + "Content-Disposition: form-data; name=\"name1\"\r\n" + "\r\n" + "text1\r\n" + "--" + + boundary + "--\r\n"; + std::string header_prefix = "POST /test HTTP/1.1\r\n" + "Content-Type: multipart/form-data;boundary=" + + boundary + + "\r\n" + "Content-Length: " + + std::to_string(content.size()) + + "\r\n" + "Dummy-Header: "; + std::string header_suffix = "\r\n" + "\r\n"; + size_t read_buff_size = 1024u * 4; // SocketStream::read_buff_size_ + size_t header_dummy_size = + read_buff_size - + (header_prefix.size() + header_suffix.size() + boundary.size() / 2); + auto header_dummy = std::string(header_dummy_size, '@'); + auto req = header_prefix + header_dummy + header_suffix + content; + + std::string response; + ASSERT_TRUE(send_request(1, req, &response)); + ASSERT_EQ("200", response.substr(9, 3)); +} + TEST(TaskQueueTest, IncreaseAtomicInteger) { static constexpr unsigned int number_of_tasks{1000000}; std::atomic_uint count{0}; From aabd0634aeac096a422e5c21a0b62b5e5f7590ef Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jun 2025 17:12:12 -0400 Subject: [PATCH 0443/1222] Fix warnings created by #2152 --- httplib.h | 106 +++++++++++++++++++++++++++++++-------------------- test/test.cc | 74 ++++++++++++++++++----------------- 2 files changed, 103 insertions(+), 77 deletions(-) diff --git a/httplib.h b/httplib.h index c875fb3938..ec6faf0186 100644 --- a/httplib.h +++ b/httplib.h @@ -553,6 +553,15 @@ struct MultipartFormData { using MultipartFormDataItems = std::vector; using MultipartFormDataMap = std::multimap; +struct MultipartFormDataForClientInput { + std::string name; + std::string content; + std::string filename; + std::string content_type; +}; +using MultipartFormDataItemsForClientInput = + std::vector; + class DataSink { public: DataSink() : os(&sb_), sb_(*this) {} @@ -1330,13 +1339,15 @@ class ClientImpl { const Params ¶ms); Result Post(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress); - Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, + const MultipartFormDataItemsForClientInput &items); Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); + const MultipartFormDataItemsForClientInput &items); Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary); Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items); Result Put(const std::string &path); @@ -1372,13 +1383,15 @@ class ClientImpl { const Params ¶ms); Result Put(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress); - Result Put(const std::string &path, const MultipartFormDataItems &items); + Result Put(const std::string &path, + const MultipartFormDataItemsForClientInput &items); Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); + const MultipartFormDataItemsForClientInput &items); Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary); Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items); Result Patch(const std::string &path); @@ -1664,7 +1677,8 @@ class ClientImpl { ContentProviderWithoutLength content_provider_without_length, const std::string &content_type, Progress progress); ContentProviderWithoutLength get_multipart_content_provider( - const std::string &boundary, const MultipartFormDataItems &items, + const std::string &boundary, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items) const; std::string adjust_host_string(const std::string &host) const; @@ -1769,13 +1783,15 @@ class Client { const Params ¶ms); Result Post(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress); - Result Post(const std::string &path, const MultipartFormDataItems &items); + Result Post(const std::string &path, + const MultipartFormDataItemsForClientInput &items); Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); + const MultipartFormDataItemsForClientInput &items); Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary); Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items); Result Put(const std::string &path); @@ -1811,13 +1827,15 @@ class Client { const Params ¶ms); Result Put(const std::string &path, const Headers &headers, const Params ¶ms, Progress progress); - Result Put(const std::string &path, const MultipartFormDataItems &items); + Result Put(const std::string &path, + const MultipartFormDataItemsForClientInput &items); Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); + const MultipartFormDataItemsForClientInput &items); Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary); Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items); Result Patch(const std::string &path); @@ -5331,7 +5349,7 @@ serialize_multipart_formdata_get_content_type(const std::string &boundary) { } inline std::string -serialize_multipart_formdata(const MultipartFormDataItems &items, +serialize_multipart_formdata(const MultipartFormDataItemsForClientInput &items, const std::string &boundary, bool finish = true) { std::string body; @@ -8370,7 +8388,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( - const std::string &boundary, const MultipartFormDataItems &items, + const std::string &boundary, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items) const { size_t cur_item = 0; size_t cur_start = 0; @@ -8671,13 +8690,15 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, progress); } -inline Result ClientImpl::Post(const std::string &path, - const MultipartFormDataItems &items) { +inline Result +ClientImpl::Post(const std::string &path, + const MultipartFormDataItemsForClientInput &items) { return Post(path, Headers(), items); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result +ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -8685,9 +8706,10 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, return Post(path, headers, body, content_type); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { +inline Result +ClientImpl::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } @@ -8700,7 +8722,7 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = @@ -8811,13 +8833,15 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, progress); } -inline Result ClientImpl::Put(const std::string &path, - const MultipartFormDataItems &items) { +inline Result +ClientImpl::Put(const std::string &path, + const MultipartFormDataItemsForClientInput &items) { return Put(path, Headers(), items); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result +ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -8826,7 +8850,7 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const std::string &boundary) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; @@ -8840,7 +8864,7 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = @@ -10251,21 +10275,21 @@ inline Result Client::Post(const std::string &path, const Headers &headers, return cli_->Post(path, headers, params, progress); } inline Result Client::Post(const std::string &path, - const MultipartFormDataItems &items) { + const MultipartFormDataItemsForClientInput &items) { return cli_->Post(path, items); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { + const MultipartFormDataItemsForClientInput &items) { return cli_->Post(path, headers, items); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const std::string &boundary) { return cli_->Post(path, headers, items, boundary); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items) { return cli_->Post(path, headers, items, provider_items); } @@ -10338,21 +10362,21 @@ inline Result Client::Put(const std::string &path, const Headers &headers, return cli_->Put(path, headers, params, progress); } inline Result Client::Put(const std::string &path, - const MultipartFormDataItems &items) { + const MultipartFormDataItemsForClientInput &items) { return cli_->Put(path, items); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { + const MultipartFormDataItemsForClientInput &items) { return cli_->Put(path, headers, items); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const std::string &boundary) { return cli_->Put(path, headers, items, boundary); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, + const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items) { return cli_->Put(path, headers, items, provider_items); } diff --git a/test/test.cc b/test/test.cc index b67c87fc2e..297a6946c7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3161,7 +3161,7 @@ TEST_F(ServerTest, GetMethod200) { TEST(BenchmarkTest, SimpleGetPerformance) { Server svr; - + svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) { res.set_content("Benchmark Response", "text/plain"); }); @@ -3176,13 +3176,13 @@ TEST(BenchmarkTest, SimpleGetPerformance) { svr.wait_until_ready(); Client cli("localhost", PORT); - + const int NUM_REQUESTS = 50; const int MAX_AVERAGE_MS = 5; - + auto warmup = cli.Get("/benchmark"); ASSERT_TRUE(warmup); - + auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < NUM_REQUESTS; ++i) { auto res = cli.Get("/benchmark"); @@ -3190,14 +3190,17 @@ TEST(BenchmarkTest, SimpleGetPerformance) { EXPECT_EQ(StatusCode::OK_200, res->status); } auto end = std::chrono::high_resolution_clock::now(); - - auto total_ms = std::chrono::duration_cast(end - start).count(); + + auto total_ms = + std::chrono::duration_cast(end - start) + .count(); double avg_ms = static_cast(total_ms) / NUM_REQUESTS; - - std::cout << "Standalone: " << NUM_REQUESTS << " requests in " << total_ms + + std::cout << "Standalone: " << NUM_REQUESTS << " requests in " << total_ms << "ms (avg: " << avg_ms << "ms)" << std::endl; - - EXPECT_LE(avg_ms, MAX_AVERAGE_MS) << "Standalone test too slow: " << avg_ms << "ms (Issue #1777)"; + + EXPECT_LE(avg_ms, MAX_AVERAGE_MS) + << "Standalone test too slow: " << avg_ms << "ms (Issue #1777)"; } TEST_F(ServerTest, GetEmptyFile) { @@ -3830,7 +3833,7 @@ TEST_F(ServerTest, TooLongHeader) { TEST_F(ServerTest, HeaderCountAtLimit) { // Test with headers just under the 100 limit httplib::Headers headers; - + // Add 95 custom headers (the client will add Host, User-Agent, Accept, etc.) // This should keep us just under the 100 header limit for (int i = 0; i < 95; i++) { @@ -3838,29 +3841,27 @@ TEST_F(ServerTest, HeaderCountAtLimit) { std::string value = "value" + std::to_string(i); headers.emplace(name, value); } - + // This should work fine as we're under the limit auto res = cli_.Get("/hi", headers); EXPECT_TRUE(res); - if (res) { - EXPECT_EQ(StatusCode::OK_200, res->status); - } + if (res) { EXPECT_EQ(StatusCode::OK_200, res->status); } } TEST_F(ServerTest, HeaderCountExceedsLimit) { // Test with many headers to exceed the 100 limit httplib::Headers headers; - + // Add 150 headers to definitely exceed the 100 limit for (int i = 0; i < 150; i++) { std::string name = "X-Test-Header-" + std::to_string(i); std::string value = "value" + std::to_string(i); headers.emplace(name, value); } - + // This should fail due to exceeding header count limit auto res = cli_.Get("/hi", headers); - + // The request should either fail or return 400 Bad Request if (res) { // If we get a response, it should be 400 Bad Request @@ -3911,19 +3912,20 @@ TEST_F(ServerTest, PlusSignEncoding) { TEST_F(ServerTest, HeaderCountSecurityTest) { // This test simulates a potential DoS attack using many headers // to verify our security fix prevents memory exhaustion - + httplib::Headers attack_headers; - - // Attempt to add many headers like an attacker would (200 headers to far exceed limit) + + // Attempt to add many headers like an attacker would (200 headers to far + // exceed limit) for (int i = 0; i < 200; i++) { std::string name = "X-Attack-Header-" + std::to_string(i); std::string value = "attack_payload_" + std::to_string(i); attack_headers.emplace(name, value); } - + // Try to POST with excessive headers auto res = cli_.Post("/", attack_headers, "test_data", "text/plain"); - + // Should either fail or return 400 Bad Request due to security limit if (res) { // If we get a response, it should be 400 Bad Request @@ -3935,7 +3937,7 @@ TEST_F(ServerTest, HeaderCountSecurityTest) { } TEST_F(ServerTest, MultipartFormData) { - MultipartFormDataItems items = { + MultipartFormDataItemsForClientInput items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, @@ -3950,7 +3952,7 @@ TEST_F(ServerTest, MultipartFormData) { } TEST_F(ServerTest, MultipartFormDataMultiFileValues) { - MultipartFormDataItems items = { + MultipartFormDataItemsForClientInput items = { {"text", "default text", "", ""}, {"multi_text1", "aaaaa", "", ""}, @@ -4890,7 +4892,7 @@ TEST_F(ServerTest, PostContentReceiver) { } TEST_F(ServerTest, PostMultipartFileContentReceiver) { - MultipartFormDataItems items = { + MultipartFormDataItemsForClientInput items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, @@ -4905,7 +4907,7 @@ TEST_F(ServerTest, PostMultipartFileContentReceiver) { } TEST_F(ServerTest, PostMultipartPlusBoundary) { - MultipartFormDataItems items = { + MultipartFormDataItemsForClientInput items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, @@ -5160,7 +5162,7 @@ TEST_F(ServerTest, NoGzipWithContentReceiver) { } TEST_F(ServerTest, MultipartFormDataGzip) { - MultipartFormDataItems items = { + MultipartFormDataItemsForClientInput items = { {"key1", "test", "", ""}, {"key2", "--abcdefg123", "", ""}, }; @@ -5324,7 +5326,7 @@ TEST_F(ServerTest, NoZstdWithContentReceiver) { // TODO: How to enable zstd ?? TEST_F(ServerTest, MultipartFormDataZstd) { - MultipartFormDataItems items = { + MultipartFormDataItemsForClientInput items = { {"key1", "test", "", ""}, {"key2", "--abcdefg123", "", ""}, }; @@ -7507,7 +7509,7 @@ TEST(MultipartFormDataTest, LargeData) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -7650,7 +7652,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"name1", "Testing123", "filename1", "application/octet-stream"}, {"name2", "Testing456", "", ""}, // not a file }; @@ -7855,7 +7857,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -7873,7 +7875,7 @@ TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { Client cli("https://localhost:8080"); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -7938,7 +7940,7 @@ TEST(MultipartFormDataTest, PutFormData) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -8002,7 +8004,7 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -8021,7 +8023,7 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItems items{ + MultipartFormDataItemsForClientInput items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; From 1729aa8c1f4501bed1a2506cbb54a7c18af53313 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jun 2025 17:37:30 -0400 Subject: [PATCH 0444/1222] Issue 2162 (#2163) * Resolve #2162 * Update --- example/Makefile | 7 +- example/accept_header.cc | 134 +++++++++++++++++++++++++++ httplib.h | 129 ++++++++++++++++++++++++++ test/test.cc | 195 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 463 insertions(+), 2 deletions(-) create mode 100644 example/accept_header.cc diff --git a/example/Makefile b/example/Makefile index ba68359313..3082b88014 100644 --- a/example/Makefile +++ b/example/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -56,9 +56,12 @@ one_time_request : one_time_request.cc ../httplib.h Makefile server_and_client : server_and_client.cc ../httplib.h Makefile $(CXX) -o server_and_client $(CXXFLAGS) server_and_client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) +accept_header : accept_header.cc ../httplib.h Makefile + $(CXX) -o accept_header $(CXXFLAGS) accept_header.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) + pem: openssl genrsa 2048 > key.pem openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header *.pem diff --git a/example/accept_header.cc b/example/accept_header.cc new file mode 100644 index 0000000000..33798d9b8a --- /dev/null +++ b/example/accept_header.cc @@ -0,0 +1,134 @@ +#include "httplib.h" +#include + +int main() { + using namespace httplib; + + // Example usage of parse_accept_header function + std::cout << "=== Accept Header Parser Example ===" << std::endl; + + // Example 1: Simple Accept header + std::string accept1 = "text/html,application/json,text/plain"; + std::vector result1; + if (detail::parse_accept_header(accept1, result1)) { + std::cout << "\nExample 1: " << accept1 << std::endl; + std::cout << "Parsed order:" << std::endl; + for (size_t i = 0; i < result1.size(); ++i) { + std::cout << " " << (i + 1) << ". " << result1[i] << std::endl; + } + } else { + std::cout << "\nExample 1: Failed to parse Accept header" << std::endl; + } + + // Example 2: Accept header with quality values + std::string accept2 = "text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8"; + std::vector result2; + if (detail::parse_accept_header(accept2, result2)) { + std::cout << "\nExample 2: " << accept2 << std::endl; + std::cout << "Parsed order (sorted by priority):" << std::endl; + for (size_t i = 0; i < result2.size(); ++i) { + std::cout << " " << (i + 1) << ". " << result2[i] << std::endl; + } + } else { + std::cout << "\nExample 2: Failed to parse Accept header" << std::endl; + } + + // Example 3: Browser-like Accept header + std::string accept3 = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; + std::vector result3; + if (detail::parse_accept_header(accept3, result3)) { + std::cout << "\nExample 3: " << accept3 << std::endl; + std::cout << "Parsed order:" << std::endl; + for (size_t i = 0; i < result3.size(); ++i) { + std::cout << " " << (i + 1) << ". " << result3[i] << std::endl; + } + } else { + std::cout << "\nExample 3: Failed to parse Accept header" << std::endl; + } + + // Example 4: Invalid Accept header examples + std::cout << "\n=== Invalid Accept Header Examples ===" << std::endl; + + std::vector invalid_examples = { + "text/html;q=1.5,application/json", // q > 1.0 + "text/html;q=-0.1,application/json", // q < 0.0 + "text/html;q=invalid,application/json", // invalid q value + "invalidtype,application/json", // invalid media type + ",application/json" // empty entry + }; + + for (const auto& invalid_accept : invalid_examples) { + std::vector temp_result; + std::cout << "\nTesting invalid: " << invalid_accept << std::endl; + if (detail::parse_accept_header(invalid_accept, temp_result)) { + std::cout << " Unexpectedly succeeded!" << std::endl; + } else { + std::cout << " Correctly rejected as invalid" << std::endl; + } + } + + // Example 4: Server usage example + std::cout << "\n=== Server Usage Example ===" << std::endl; + Server svr; + + svr.Get("/api/data", [](const Request& req, Response& res) { + // Get Accept header + auto accept_header = req.get_header_value("Accept"); + if (accept_header.empty()) { + accept_header = "*/*"; // Default if no Accept header + } + + // Parse accept header to get preferred content types + std::vector preferred_types; + if (!detail::parse_accept_header(accept_header, preferred_types)) { + // Invalid Accept header + res.status = 400; // Bad Request + res.set_content("Invalid Accept header", "text/plain"); + return; + } + + std::cout << "Client Accept header: " << accept_header << std::endl; + std::cout << "Preferred types in order:" << std::endl; + for (size_t i = 0; i < preferred_types.size(); ++i) { + std::cout << " " << (i + 1) << ". " << preferred_types[i] << std::endl; + } + + // Choose response format based on client preference + std::string response_content; + std::string content_type; + + for (const auto& type : preferred_types) { + if (type == "application/json" || type == "application/*" || type == "*/*") { + response_content = "{\"message\": \"Hello, World!\", \"data\": [1, 2, 3]}"; + content_type = "application/json"; + break; + } else if (type == "text/html" || type == "text/*") { + response_content = "

Hello, World!

Data: 1, 2, 3

"; + content_type = "text/html"; + break; + } else if (type == "text/plain") { + response_content = "Hello, World!\nData: 1, 2, 3"; + content_type = "text/plain"; + break; + } + } + + if (response_content.empty()) { + // No supported content type found + res.status = 406; // Not Acceptable + res.set_content("No acceptable content type found", "text/plain"); + return; + } + + res.set_content(response_content, content_type); + std::cout << "Responding with: " << content_type << std::endl; + }); + + std::cout << "Server configured. You can test it with:" << std::endl; + std::cout << " curl -H \"Accept: application/json\" http://localhost:8080/api/data" << std::endl; + std::cout << " curl -H \"Accept: text/html\" http://localhost:8080/api/data" << std::endl; + std::cout << " curl -H \"Accept: text/plain\" http://localhost:8080/api/data" << std::endl; + std::cout << " curl -H \"Accept: text/html;q=0.9,application/json;q=1.0\" http://localhost:8080/api/data" << std::endl; + + return 0; +} diff --git a/httplib.h b/httplib.h index ec6faf0186..63718f541a 100644 --- a/httplib.h +++ b/httplib.h @@ -670,6 +670,7 @@ struct Request { std::function is_connection_closed = []() { return true; }; // for client + std::vector accept_content_types; ResponseHandler response_handler; ContentReceiverWithProgress content_receiver; Progress progress; @@ -2491,6 +2492,9 @@ bool parse_multipart_boundary(const std::string &content_type, bool parse_range_header(const std::string &s, Ranges &ranges); +bool parse_accept_header(const std::string &s, + std::vector &content_types); + int close_socket(socket_t sock); ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); @@ -5026,6 +5030,123 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { } catch (...) { return false; } #endif +inline bool parse_accept_header(const std::string &s, + std::vector &content_types) { + content_types.clear(); + + // Empty string is considered valid (no preference) + if (s.empty()) { return true; } + + // Check for invalid patterns: leading/trailing commas or consecutive commas + if (s.front() == ',' || s.back() == ',' || + s.find(",,") != std::string::npos) { + return false; + } + + struct AcceptEntry { + std::string media_type; + double quality; + int order; // Original order in header + }; + + std::vector entries; + int order = 0; + bool has_invalid_entry = false; + + // Split by comma and parse each entry + split(s.data(), s.data() + s.size(), ',', [&](const char *b, const char *e) { + std::string entry(b, e); + entry = trim_copy(entry); + + if (entry.empty()) { + has_invalid_entry = true; + return; + } + + AcceptEntry accept_entry; + accept_entry.quality = 1.0; // Default quality + accept_entry.order = order++; + + // Find q= parameter + auto q_pos = entry.find(";q="); + if (q_pos == std::string::npos) { q_pos = entry.find("; q="); } + + if (q_pos != std::string::npos) { + // Extract media type (before q parameter) + accept_entry.media_type = trim_copy(entry.substr(0, q_pos)); + + // Extract quality value + auto q_start = entry.find('=', q_pos) + 1; + auto q_end = entry.find(';', q_start); + if (q_end == std::string::npos) { q_end = entry.length(); } + + std::string quality_str = + trim_copy(entry.substr(q_start, q_end - q_start)); + if (quality_str.empty()) { + has_invalid_entry = true; + return; + } + + try { + accept_entry.quality = std::stod(quality_str); + // Check if quality is in valid range [0.0, 1.0] + if (accept_entry.quality < 0.0 || accept_entry.quality > 1.0) { + has_invalid_entry = true; + return; + } + } catch (...) { + has_invalid_entry = true; + return; + } + } else { + // No quality parameter, use entire entry as media type + accept_entry.media_type = entry; + } + + // Remove additional parameters from media type + auto param_pos = accept_entry.media_type.find(';'); + if (param_pos != std::string::npos) { + accept_entry.media_type = + trim_copy(accept_entry.media_type.substr(0, param_pos)); + } + + // Basic validation of media type format + if (accept_entry.media_type.empty()) { + has_invalid_entry = true; + return; + } + + // Check for basic media type format (should contain '/' or be '*') + if (accept_entry.media_type != "*" && + accept_entry.media_type.find('/') == std::string::npos) { + has_invalid_entry = true; + return; + } + + entries.push_back(accept_entry); + }); + + // Return false if any invalid entry was found + if (has_invalid_entry) { return false; } + + // Sort by quality (descending), then by original order (ascending) + std::sort(entries.begin(), entries.end(), + [](const AcceptEntry &a, const AcceptEntry &b) { + if (a.quality != b.quality) { + return a.quality > b.quality; // Higher quality first + } + return a.order < b.order; // Earlier order first for same quality + }); + + // Extract sorted media types + content_types.reserve(entries.size()); + for (const auto &entry : entries) { + content_types.push_back(entry.media_type); + } + + return true; +} + class MultipartFormDataParser { public: MultipartFormDataParser() = default; @@ -7446,6 +7567,14 @@ Server::process_request(Stream &strm, const std::string &remote_addr, req.set_header("LOCAL_ADDR", req.local_addr); req.set_header("LOCAL_PORT", std::to_string(req.local_port)); + if (req.has_header("Accept")) { + const auto &accept_header = req.get_header_value("Accept"); + if (!detail::parse_accept_header(accept_header, req.accept_content_types)) { + res.status = StatusCode::BadRequest_400; + return write_response(strm, close_connection, req, res); + } + } + if (req.has_header("Range")) { const auto &range_header_value = req.get_header_value("Range"); if (!detail::parse_range_header(range_header_value, req.ranges)) { diff --git a/test/test.cc b/test/test.cc index 297a6946c7..347b90e757 100644 --- a/test/test.cc +++ b/test/test.cc @@ -308,6 +308,201 @@ TEST(TrimTests, TrimStringTests) { EXPECT_TRUE(detail::trim_copy("").empty()); } +TEST(ParseAcceptHeaderTest, BasicAcceptParsing) { + // Simple case without quality values + std::vector result1; + EXPECT_TRUE(detail::parse_accept_header( + "text/html,application/json,text/plain", result1)); + EXPECT_EQ(result1.size(), 3); + EXPECT_EQ(result1[0], "text/html"); + EXPECT_EQ(result1[1], "application/json"); + EXPECT_EQ(result1[2], "text/plain"); + + // With quality values + std::vector result2; + EXPECT_TRUE(detail::parse_accept_header( + "text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8", result2)); + EXPECT_EQ(result2.size(), 3); + EXPECT_EQ(result2[0], "application/json"); // highest q value + EXPECT_EQ(result2[1], "text/html"); + EXPECT_EQ(result2[2], "text/plain"); // lowest q value +} + +TEST(ParseAcceptHeaderTest, MixedQualityValues) { + // Mixed with and without quality values + std::vector result; + EXPECT_TRUE(detail::parse_accept_header( + "text/html,application/json;q=0.5,text/plain;q=0.8", result)); + EXPECT_EQ(result.size(), 3); + EXPECT_EQ(result[0], "text/html"); // no q value means 1.0 + EXPECT_EQ(result[1], "text/plain"); // q=0.8 + EXPECT_EQ(result[2], "application/json"); // q=0.5 +} + +TEST(ParseAcceptHeaderTest, EdgeCases) { + // Empty header + std::vector empty_result; + EXPECT_TRUE(detail::parse_accept_header("", empty_result)); + EXPECT_TRUE(empty_result.empty()); + + // Single type + std::vector single_result; + EXPECT_TRUE(detail::parse_accept_header("application/json", single_result)); + EXPECT_EQ(single_result.size(), 1); + EXPECT_EQ(single_result[0], "application/json"); + + // Wildcard types + std::vector wildcard_result; + EXPECT_TRUE(detail::parse_accept_header( + "text/*;q=0.5,*/*;q=0.1,application/json", wildcard_result)); + EXPECT_EQ(wildcard_result.size(), 3); + EXPECT_EQ(wildcard_result[0], "application/json"); + EXPECT_EQ(wildcard_result[1], "text/*"); + EXPECT_EQ(wildcard_result[2], "*/*"); +} + +TEST(ParseAcceptHeaderTest, RealWorldExamples) { + // Common browser Accept header + std::vector browser_result; + EXPECT_TRUE( + detail::parse_accept_header("text/html,application/xhtml+xml,application/" + "xml;q=0.9,image/webp,image/apng,*/*;q=0.8", + browser_result)); + EXPECT_EQ(browser_result.size(), 6); + EXPECT_EQ(browser_result[0], "text/html"); // q=1.0 (default) + EXPECT_EQ(browser_result[1], "application/xhtml+xml"); // q=1.0 (default) + EXPECT_EQ(browser_result[2], "image/webp"); // q=1.0 (default) + EXPECT_EQ(browser_result[3], "image/apng"); // q=1.0 (default) + EXPECT_EQ(browser_result[4], "application/xml"); // q=0.9 + EXPECT_EQ(browser_result[5], "*/*"); // q=0.8 + + // API client header + std::vector api_result; + EXPECT_TRUE(detail::parse_accept_header( + "application/json;q=0.9,application/xml;q=0.8,text/plain;q=0.1", + api_result)); + EXPECT_EQ(api_result.size(), 3); + EXPECT_EQ(api_result[0], "application/json"); + EXPECT_EQ(api_result[1], "application/xml"); + EXPECT_EQ(api_result[2], "text/plain"); +} + +TEST(ParseAcceptHeaderTest, SpecialCases) { + // Quality value with 3 decimal places + std::vector decimal_result; + EXPECT_TRUE(detail::parse_accept_header( + "text/html;q=0.123,application/json;q=0.456", decimal_result)); + EXPECT_EQ(decimal_result.size(), 2); + EXPECT_EQ(decimal_result[0], "application/json"); // Higher q value + EXPECT_EQ(decimal_result[1], "text/html"); + + // Zero quality (should still be included but with lowest priority) + std::vector zero_q_result; + EXPECT_TRUE(detail::parse_accept_header("text/html;q=0,application/json;q=1", + zero_q_result)); + EXPECT_EQ(zero_q_result.size(), 2); + EXPECT_EQ(zero_q_result[0], "application/json"); // q=1 + EXPECT_EQ(zero_q_result[1], "text/html"); // q=0 + + // No spaces around commas + std::vector no_space_result; + EXPECT_TRUE(detail::parse_accept_header( + "text/html;q=0.9,application/json;q=0.8,text/plain;q=0.7", + no_space_result)); + EXPECT_EQ(no_space_result.size(), 3); + EXPECT_EQ(no_space_result[0], "text/html"); + EXPECT_EQ(no_space_result[1], "application/json"); + EXPECT_EQ(no_space_result[2], "text/plain"); +} + +TEST(ParseAcceptHeaderTest, InvalidCases) { + std::vector result; + + // Invalid quality value (> 1.0) + EXPECT_FALSE( + detail::parse_accept_header("text/html;q=1.5,application/json", result)); + + // Invalid quality value (< 0.0) + EXPECT_FALSE( + detail::parse_accept_header("text/html;q=-0.1,application/json", result)); + + // Invalid quality value (not a number) + EXPECT_FALSE(detail::parse_accept_header( + "text/html;q=invalid,application/json", result)); + + // Empty quality value + EXPECT_FALSE( + detail::parse_accept_header("text/html;q=,application/json", result)); + + // Invalid media type format (no slash and not wildcard) + EXPECT_FALSE( + detail::parse_accept_header("invalidtype,application/json", result)); + + // Empty media type + result.clear(); + EXPECT_FALSE(detail::parse_accept_header(",application/json", result)); + + // Only commas + result.clear(); + EXPECT_FALSE(detail::parse_accept_header(",,,", result)); + + // Valid cases should still work + EXPECT_TRUE(detail::parse_accept_header("*/*", result)); + EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result[0], "*/*"); + + EXPECT_TRUE(detail::parse_accept_header("*", result)); + EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result[0], "*"); + + EXPECT_TRUE(detail::parse_accept_header("text/*", result)); + EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result[0], "text/*"); +} + +TEST(ParseAcceptHeaderTest, ContentTypesPopulatedAndInvalidHeaderHandling) { + Server svr; + + svr.Get("/accept_ok", [&](const Request &req, Response &res) { + EXPECT_EQ(req.accept_content_types.size(), 3); + EXPECT_EQ(req.accept_content_types[0], "application/json"); + EXPECT_EQ(req.accept_content_types[1], "text/html"); + EXPECT_EQ(req.accept_content_types[2], "*/*"); + res.set_content("ok", "text/plain"); + }); + + svr.Get("/accept_bad_request", [&](const Request & /*req*/, Response &res) { + EXPECT_TRUE(false); + res.set_content("bad request", "text/plain"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli("localhost", PORT); + + { + auto res = + cli.Get("/accept_ok", + {{"Accept", "application/json, text/html;q=0.8, */*;q=0.1"}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + } + + { + auto res = cli.Get("/accept_bad_request", + {{"Accept", "text/html;q=abc,application/json"}}); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); + } +} + TEST(DivideTest, DivideStringTests) { auto divide = [](const std::string &str, char d) { std::string lhs; From b2bf172393634998037ed7289564f44bce7fd823 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 24 Jun 2025 19:39:57 -0400 Subject: [PATCH 0445/1222] Fix #1551 --- httplib.h | 38 +++++++++++++++++ test/test.cc | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) diff --git a/httplib.h b/httplib.h index 63718f541a..ae815c2408 100644 --- a/httplib.h +++ b/httplib.h @@ -1450,6 +1450,11 @@ class ClientImpl { Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const Params ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); @@ -1894,6 +1899,11 @@ class Client { Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, Progress progress); + Result Delete(const std::string &path, const Params ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); @@ -9172,6 +9182,23 @@ inline Result ClientImpl::Delete(const std::string &path, progress); } +inline Result ClientImpl::Delete(const std::string &path, const Params ¶ms) { + return Delete(path, Headers(), params); +} + +inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Delete(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + auto query = detail::params_to_query_str(params); + return Delete(path, headers, query, "application/x-www-form-urlencoded", + progress); +} + inline Result ClientImpl::Options(const std::string &path) { return Options(path, Headers()); } @@ -10627,6 +10654,17 @@ inline Result Client::Delete(const std::string &path, const Headers &headers, Progress progress) { return cli_->Delete(path, headers, body, content_type, progress); } +inline Result Client::Delete(const std::string &path, const Params ¶ms) { + return cli_->Delete(path, params); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Delete(path, headers, params); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Delete(path, headers, params, progress); +} inline Result Client::Options(const std::string &path) { return cli_->Options(path); } diff --git a/test/test.cc b/test/test.cc index 347b90e757..08b786d1e5 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2850,6 +2850,20 @@ class ServerTest : public ::testing::Test { res.status = StatusCode::NotFound_404; } }) + .Delete("/person", + [&](const Request &req, Response &res) { + if (req.has_param("name")) { + string name = req.get_param_value("name"); + if (persons_.find(name) != persons_.end()) { + persons_.erase(name); + res.set_content("DELETED", "text/plain"); + } else { + res.status = StatusCode::NotFound_404; + } + } else { + res.status = StatusCode::BadRequest_400; + } + }) .Post("/x-www-form-urlencoded-json", [&](const Request &req, Response &res) { auto json = req.get_param_value("json"); @@ -3562,6 +3576,108 @@ TEST_F(ServerTest, PutMethod3) { ASSERT_EQ("coder", res->body); } +TEST_F(ServerTest, DeleteMethod1) { + auto res = cli_.Get("/person/john4"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::NotFound_404, res->status); + + Params params; + params.emplace("name", "john4"); + params.emplace("note", "coder"); + + res = cli_.Post("/person", params); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + + res = cli_.Get("/person/john4"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); + ASSERT_EQ("coder", res->body); + + Params delete_params; + delete_params.emplace("name", "john4"); + + res = cli_.Delete("/person", delete_params); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ("DELETED", res->body); + + res = cli_.Get("/person/john4"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::NotFound_404, res->status); +} + +TEST_F(ServerTest, DeleteMethod2) { + auto res = cli_.Get("/person/john5"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::NotFound_404, res->status); + + Params params; + params.emplace("name", "john5"); + params.emplace("note", "developer"); + + res = cli_.Post("/person", params); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + + res = cli_.Get("/person/john5"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); + ASSERT_EQ("developer", res->body); + + Params delete_params; + delete_params.emplace("name", "john5"); + + Headers headers; + headers.emplace("Custom-Header", "test-value"); + + res = cli_.Delete("/person", headers, delete_params); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ("DELETED", res->body); + + res = cli_.Get("/person/john5"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::NotFound_404, res->status); +} + +TEST_F(ServerTest, DeleteMethod3) { + auto res = cli_.Get("/person/john6"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::NotFound_404, res->status); + + Params params; + params.emplace("name", "john6"); + params.emplace("note", "tester"); + + res = cli_.Post("/person", params); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + + res = cli_.Get("/person/john6"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); + ASSERT_EQ("tester", res->body); + + Params delete_params; + delete_params.emplace("name", "john6"); + + Headers headers; + headers.emplace("Custom-Header", "test-value"); + + res = cli_.Delete("/person", headers, delete_params, nullptr); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_EQ("DELETED", res->body); + + res = cli_.Get("/person/john6"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::NotFound_404, res->status); +} + TEST_F(ServerTest, PostWwwFormUrlEncodedJson) { Params params; params.emplace("json", JSON_DATA); From d37373a983f973d62368b68cea44a108b815d881 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 25 Jun 2025 16:46:49 -0400 Subject: [PATCH 0446/1222] Performance investigation --- test/test.cc | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/test.cc b/test/test.cc index 08b786d1e5..4797795fea 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3368,14 +3368,16 @@ TEST_F(ServerTest, GetMethod200) { EXPECT_EQ("Hello World!", res->body); } -TEST(BenchmarkTest, SimpleGetPerformance) { +void performance_test(const char *host) { + auto port = 1234; + Server svr; svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) { res.set_content("Benchmark Response", "text/plain"); }); - auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto listen_thread = std::thread([&]() { svr.listen(host, port); }); auto se = detail::scope_exit([&] { svr.stop(); listen_thread.join(); @@ -3384,7 +3386,7 @@ TEST(BenchmarkTest, SimpleGetPerformance) { svr.wait_until_ready(); - Client cli("localhost", PORT); + Client cli(host, port); const int NUM_REQUESTS = 50; const int MAX_AVERAGE_MS = 5; @@ -3405,13 +3407,18 @@ TEST(BenchmarkTest, SimpleGetPerformance) { .count(); double avg_ms = static_cast(total_ms) / NUM_REQUESTS; - std::cout << "Standalone: " << NUM_REQUESTS << " requests in " << total_ms - << "ms (avg: " << avg_ms << "ms)" << std::endl; + std::cout << "Peformance test at \"" << host << "\": " << NUM_REQUESTS + << " requests in " << total_ms << "ms (avg: " << avg_ms << "ms)" + << std::endl; EXPECT_LE(avg_ms, MAX_AVERAGE_MS) - << "Standalone test too slow: " << avg_ms << "ms (Issue #1777)"; + << "Performance is too slow: " << avg_ms << "ms (Issue #1777)"; } +TEST(BenchmarkTest, localhost) { performance_test("localhost"); } + +TEST(BenchmarkTest, v6) { performance_test("::1"); } + TEST_F(ServerTest, GetEmptyFile) { auto res = cli_.Get("/empty_file"); ASSERT_TRUE(res); From a183dba9fc93e0fe2559a7da35a7be4df53a5b08 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 25 Jun 2025 17:08:00 -0400 Subject: [PATCH 0447/1222] clang-format --- httplib.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index ae815c2408..e7fe9efe6e 100644 --- a/httplib.h +++ b/httplib.h @@ -9182,18 +9182,20 @@ inline Result ClientImpl::Delete(const std::string &path, progress); } -inline Result ClientImpl::Delete(const std::string &path, const Params ¶ms) { +inline Result ClientImpl::Delete(const std::string &path, + const Params ¶ms) { return Delete(path, Headers(), params); } -inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, - const Params ¶ms) { +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const Params ¶ms) { auto query = detail::params_to_query_str(params); return Delete(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const Params ¶ms, + Progress progress) { auto query = detail::params_to_query_str(params); return Delete(path, headers, query, "application/x-www-form-urlencoded", progress); From 696c02f2bcc494011239e666ae46f5fde7d3260d Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 26 Jun 2025 23:52:03 -0400 Subject: [PATCH 0448/1222] Update README --- README.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0aa7312c54..71bc170ca9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ cpp-httplib A C++11 single-file header-only cross platform HTTP/HTTPS library. -It's extremely easy to setup. Just include the **httplib.h** file in your code! +It's extremely easy to set up. Just include the **httplib.h** file in your code! > [!IMPORTANT] > This library uses 'blocking' socket I/O. If you are looking for a library with 'non-blocking' socket I/O, this is not the one that you want. @@ -187,7 +187,7 @@ svr.set_file_extension_and_mimetype_mapping("cpp", "text/x-c"); svr.set_file_extension_and_mimetype_mapping("hh", "text/x-h"); ``` -The followings are built-in mappings: +The following are built-in mappings: | Extension | MIME Type | Extension | MIME Type | | :--------- | :-------------------------- | :--------- | :-------------------------- | @@ -201,7 +201,7 @@ The followings are built-in mappings: | bmp | image/bmp | 7z | application/x-7z-compressed | | gif | image/gif | atom | application/atom+xml | | png | image/png | pdf | application/pdf | -| svg | image/svg+xml | mjs, js | application/javascript | +| svg | image/svg+xml | mjs, js | text/javascript | | webp | image/webp | json | application/json | | ico | image/x-icon | rss | application/rss+xml | | tif | image/tiff | tar | application/x-tar | @@ -451,7 +451,7 @@ svr.set_expect_100_continue_handler([](const Request &req, Response &res) { ### Keep-Alive connection ```cpp -svr.set_keep_alive_max_count(2); // Default is 5 +svr.set_keep_alive_max_count(2); // Default is 100 svr.set_keep_alive_timeout(10); // Default is 5 ``` @@ -578,9 +578,11 @@ enum Error { SSLConnection, SSLLoadingCerts, SSLServerVerification, + SSLServerHostnameVerification, UnsupportedMultipartBoundaryChars, Compression, ConnectionTimeout, + ProxyConnection, }; ``` @@ -672,7 +674,7 @@ cli.set_read_timeout(5, 0); // 5 seconds cli.set_write_timeout(5, 0); // 5 seconds // This method works the same as curl's `--max-time` option -svr.set_max_timeout(5000); // 5 seconds +cli.set_max_timeout(5000); // 5 seconds ``` ### Receive content with a content receiver @@ -846,6 +848,7 @@ The server can apply compression to the following MIME type contents: * application/javascript * application/json * application/xml + * application/protobuf * application/xhtml+xml ### Zlib Support @@ -857,13 +860,18 @@ The server can apply compression to the following MIME type contents: Brotli compression is available with `CPPHTTPLIB_BROTLI_SUPPORT`. Necessary libraries should be linked. Please see https://github.com/google/brotli for more detail. +### Zstd Support + +Zstd compression is available with `CPPHTTPLIB_ZSTD_SUPPORT`. Necessary libraries should be linked. +Please see https://github.com/facebook/zstd for more detail. + ### Default `Accept-Encoding` value The default `Accept-Encoding` value contains all possible compression types. So, the following two examples are same. ```c++ res = cli.Get("/resource/foo"); -res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate, br"}}); +res = cli.Get("/resource/foo", {{"Accept-Encoding", "br, gzip, deflate, zstd"}}); ``` If we don't want a response without compression, we have to set `Accept-Encoding` to an empty string. This behavior is similar to curl. @@ -903,7 +911,7 @@ httplib::Client cli("./my-socket.sock"); cli.set_address_family(AF_UNIX); ``` -"my-socket.sock" can be a relative path or an absolute path. You application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. +"my-socket.sock" can be a relative path or an absolute path. Your application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. Split httplib.h into .h and .cc From e1ab5a604befe6ae6fff7a96d2d7a3c2822a2987 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 28 Jun 2025 00:14:01 -0400 Subject: [PATCH 0449/1222] Proxy problems (#2165) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix proxy problems * Auto redirect problem (http → https → https) --- httplib.h | 165 +++++++++++++++++++++++++++++++--- test/proxy/Dockerfile | 4 +- test/proxy/basic_squid.conf | 2 +- test/proxy/digest_squid.conf | 2 +- test/proxy/docker-compose.yml | 2 - test/test_proxy.cc | 28 ++++-- 6 files changed, 177 insertions(+), 26 deletions(-) diff --git a/httplib.h b/httplib.h index e7fe9efe6e..f45fe5fd6c 100644 --- a/httplib.h +++ b/httplib.h @@ -1669,6 +1669,11 @@ class ClientImpl { bool write_request(Stream &strm, Request &req, bool close_connection, Error &error); bool redirect(Request &req, Response &res, Error &error); + bool create_redirect_client(const std::string &scheme, + const std::string &host, int port, Request &req, + Response &res, const std::string &path, + const std::string &location, Error &error); + template void setup_redirect_client(ClientType &client); bool handle_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error); std::unique_ptr send_with_content_provider( @@ -8140,24 +8145,150 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { auto path = detail::decode_url(next_path, true) + next_query; + // Same host redirect - use current client if (next_scheme == scheme && next_host == host_ && next_port == port_) { return detail::redirect(*this, req, res, path, location, error); - } else { - if (next_scheme == "https") { + } + + // Cross-host/scheme redirect - create new client with robust setup + return create_redirect_client(next_scheme, next_host, next_port, req, res, + path, location, error); +} + +// New method for robust redirect client creation +inline bool ClientImpl::create_redirect_client( + const std::string &scheme, const std::string &host, int port, Request &req, + Response &res, const std::string &path, const std::string &location, + Error &error) { + // Determine if we need SSL + auto need_ssl = (scheme == "https"); + + // Clean up request headers that are host/client specific + // Remove headers that should not be carried over to new host + auto headers_to_remove = + std::vector{"Host", "Proxy-Authorization", "Authorization"}; + + for (const auto &header_name : headers_to_remove) { + auto it = req.headers.find(header_name); + while (it != req.headers.end()) { + it = req.headers.erase(it); + it = req.headers.find(header_name); + } + } + + // Create appropriate client type and handle redirect + if (need_ssl) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLClient cli(next_host, next_port); - cli.copy_settings(*this); - if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } - return detail::redirect(cli, req, res, path, location, error); + // Create SSL client for HTTPS redirect + SSLClient redirect_client(host, port); + + // Setup basic client configuration first + setup_redirect_client(redirect_client); + + // SSL-specific configuration for proxy environments + if (!proxy_host_.empty() && proxy_port_ != -1) { + // Critical: Disable SSL verification for proxy environments + redirect_client.enable_server_certificate_verification(false); + redirect_client.enable_server_hostname_verification(false); + } else { + // For direct SSL connections, copy SSL verification settings + redirect_client.enable_server_certificate_verification( + server_certificate_verification_); + redirect_client.enable_server_hostname_verification( + server_hostname_verification_); + } + + // Handle CA certificate store and paths if available + if (ca_cert_store_) { redirect_client.set_ca_cert_store(ca_cert_store_); } + if (!ca_cert_file_path_.empty()) { + redirect_client.set_ca_cert_path(ca_cert_file_path_, ca_cert_dir_path_); + } + + // Client certificates are set through constructor for SSLClient + // NOTE: SSLClient constructor already takes client_cert_path and + // client_key_path so we need to create it properly if client certs are + // needed + + // Execute the redirect + return detail::redirect(redirect_client, req, res, path, location, error); #else - return false; + // SSL not supported - set appropriate error + error = Error::SSLConnection; + return false; #endif - } else { - ClientImpl cli(next_host, next_port); - cli.copy_settings(*this); - return detail::redirect(cli, req, res, path, location, error); + } else { + // HTTP redirect + ClientImpl redirect_client(host, port); + + // Setup client with robust configuration + setup_redirect_client(redirect_client); + + // Execute the redirect + return detail::redirect(redirect_client, req, res, path, location, error); + } +} + +// New method for robust client setup (based on basic_manual_redirect.cpp logic) +template +inline void ClientImpl::setup_redirect_client(ClientType &client) { + // Copy basic settings first + client.set_connection_timeout(connection_timeout_sec_); + client.set_read_timeout(read_timeout_sec_, read_timeout_usec_); + client.set_write_timeout(write_timeout_sec_, write_timeout_usec_); + client.set_keep_alive(keep_alive_); + client.set_follow_location( + true); // Enable redirects to handle multi-step redirects + client.set_url_encode(url_encode_); + client.set_compress(compress_); + client.set_decompress(decompress_); + + // Copy authentication settings BEFORE proxy setup + if (!basic_auth_username_.empty()) { + client.set_basic_auth(basic_auth_username_, basic_auth_password_); + } + if (!bearer_token_auth_token_.empty()) { + client.set_bearer_token_auth(bearer_token_auth_token_); + } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!digest_auth_username_.empty()) { + client.set_digest_auth(digest_auth_username_, digest_auth_password_); + } +#endif + + // Setup proxy configuration (CRITICAL ORDER - proxy must be set + // before proxy auth) + if (!proxy_host_.empty() && proxy_port_ != -1) { + // First set proxy host and port + client.set_proxy(proxy_host_, proxy_port_); + + // Then set proxy authentication (order matters!) + if (!proxy_basic_auth_username_.empty()) { + client.set_proxy_basic_auth(proxy_basic_auth_username_, + proxy_basic_auth_password_); + } + if (!proxy_bearer_token_auth_token_.empty()) { + client.set_proxy_bearer_token_auth(proxy_bearer_token_auth_token_); + } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!proxy_digest_auth_username_.empty()) { + client.set_proxy_digest_auth(proxy_digest_auth_username_, + proxy_digest_auth_password_); } +#endif } + + // Copy network and socket settings + client.set_address_family(address_family_); + client.set_tcp_nodelay(tcp_nodelay_); + client.set_ipv6_v6only(ipv6_v6only_); + if (socket_options_) { client.set_socket_options(socket_options_); } + if (!interface_.empty()) { client.set_interface(interface_); } + + // Copy logging and headers + if (logger_) { client.set_logger(logger_); } + + // NOTE: DO NOT copy default_headers_ as they may contain stale Host headers + // Each new client should generate its own headers based on its target host } inline bool ClientImpl::write_content_with_provider(Stream &strm, @@ -9901,6 +10032,18 @@ inline bool SSLClient::connect_with_proxy( !proxy_digest_auth_password_.empty()) { std::map auth; if (detail::parse_www_authenticate(proxy_res, auth, true)) { + // Close the current socket and create a new one for the authenticated + // request + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + + // Create a new socket for the authenticated CONNECT request + if (!create_and_connect_socket(socket, error)) { + success = false; + return false; + } + proxy_res = Response(); if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, diff --git a/test/proxy/Dockerfile b/test/proxy/Dockerfile index cd20938b6a..2f39159654 100644 --- a/test/proxy/Dockerfile +++ b/test/proxy/Dockerfile @@ -1,9 +1,9 @@ -FROM centos:7 +FROM alpine:latest ARG auth="basic" ARG port="3128" -RUN yum install -y squid +RUN apk update && apk add --no-cache squid COPY ./${auth}_squid.conf /etc/squid/squid.conf COPY ./${auth}_passwd /etc/squid/passwd diff --git a/test/proxy/basic_squid.conf b/test/proxy/basic_squid.conf index f97f09a4fe..e9d1aebfef 100644 --- a/test/proxy/basic_squid.conf +++ b/test/proxy/basic_squid.conf @@ -27,7 +27,7 @@ acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT -auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd +auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/passwd auth_param basic realm proxy acl authenticated proxy_auth REQUIRED http_access allow authenticated diff --git a/test/proxy/digest_squid.conf b/test/proxy/digest_squid.conf index 050c5da1c9..f38135f08a 100644 --- a/test/proxy/digest_squid.conf +++ b/test/proxy/digest_squid.conf @@ -27,7 +27,7 @@ acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT -auth_param digest program /usr/lib64/squid/digest_file_auth /etc/squid/passwd +auth_param digest program /usr/lib/squid/digest_file_auth /etc/squid/passwd auth_param digest realm proxy acl authenticated proxy_auth REQUIRED http_access allow authenticated diff --git a/test/proxy/docker-compose.yml b/test/proxy/docker-compose.yml index 67a9e9b617..8ffe81eabf 100644 --- a/test/proxy/docker-compose.yml +++ b/test/proxy/docker-compose.yml @@ -1,5 +1,3 @@ -version: '2' - services: squid_basic: image: squid_basic diff --git a/test/test_proxy.cc b/test/test_proxy.cc index 672bcce247..745d5f9882 100644 --- a/test/test_proxy.cc +++ b/test/test_proxy.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,14 @@ using namespace std; using namespace httplib; +std::string normalizeJson(const std::string &json) { + std::string result; + for (char c : json) { + if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { result += c; } + } + return result; +} + template void ProxyTest(T &cli, bool basic) { cli.set_proxy("localhost", basic ? 3128 : 3129); auto res = cli.Get("/httpbin/get"); @@ -81,7 +90,7 @@ TEST(RedirectTest, YouTubeNoSSLBasic) { RedirectProxyText(cli, "/", true); } -TEST(RedirectTest, DISABLED_YouTubeNoSSLDigest) { +TEST(RedirectTest, YouTubeNoSSLDigest) { Client cli("youtube.com"); RedirectProxyText(cli, "/", false); } @@ -92,6 +101,7 @@ TEST(RedirectTest, YouTubeSSLBasic) { } TEST(RedirectTest, YouTubeSSLDigest) { + std::this_thread::sleep_for(std::chrono::seconds(3)); SSLClient cli("youtube.com"); RedirectProxyText(cli, "/", false); } @@ -113,8 +123,8 @@ template void BaseAuthTestFromHTTPWatch(T &cli) { auto res = cli.Get("/basic-auth/hello/world", {make_basic_authentication_header("hello", "world")}); ASSERT_TRUE(res != nullptr); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ(normalizeJson("{\"authenticated\":true,\"user\":\"hello\"}\n"), + normalizeJson(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -122,8 +132,8 @@ template void BaseAuthTestFromHTTPWatch(T &cli) { cli.set_basic_auth("hello", "world"); auto res = cli.Get("/basic-auth/hello/world"); ASSERT_TRUE(res != nullptr); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ(normalizeJson("{\"authenticated\":true,\"user\":\"hello\"}\n"), + normalizeJson(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -179,8 +189,8 @@ template void DigestAuthTestFromHTTPWatch(T &cli) { for (auto path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res != nullptr); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ(normalizeJson("{\"authenticated\":true,\"user\":\"hello\"}\n"), + normalizeJson(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -249,8 +259,8 @@ template void KeepAliveTest(T &cli, bool basic) { for (auto path : paths) { auto res = cli.Get(path.c_str()); - EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n", - res->body); + EXPECT_EQ(normalizeJson("{\"authenticated\":true,\"user\":\"hello\"}\n"), + normalizeJson(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } } From e6ff3d7ac28cb7c1659a8ae3148ea745ccd170c5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 28 Jun 2025 00:37:01 -0400 Subject: [PATCH 0450/1222] Cleaner API (#2166) * Cleaner API * Fix Windows build error --- httplib.h | 1473 +++++++++++++++++++++----------------------------- test/test.cc | 267 +++++++++ 2 files changed, 881 insertions(+), 859 deletions(-) diff --git a/httplib.h b/httplib.h index f45fe5fd6c..cfeb051541 100644 --- a/httplib.h +++ b/httplib.h @@ -126,6 +126,10 @@ #define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) #endif +#ifndef CPPHTTPLIB_SEND_BUFSIZ +#define CPPHTTPLIB_SEND_BUFSIZ size_t(16384u) +#endif + #ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ #define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) #endif @@ -538,7 +542,8 @@ using Headers = using Params = std::multimap; using Match = std::smatch; -using Progress = std::function; +using DownloadProgress = std::function; +using UploadProgress = std::function; struct Response; using ResponseHandler = std::function; @@ -673,7 +678,8 @@ struct Request { std::vector accept_content_types; ResponseHandler response_handler; ContentReceiverWithProgress content_receiver; - Progress progress; + DownloadProgress download_progress; + UploadProgress upload_progress; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT const SSL *ssl = nullptr; #endif @@ -1270,194 +1276,83 @@ class ClientImpl { virtual bool is_valid() const; - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, - Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress = nullptr); + // clang-format off + Result Get(const std::string &path, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Head(const std::string &path); Result Head(const std::string &path, const Headers &headers); Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); + Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Post(const std::string &path, - const MultipartFormDataItemsForClientInput &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const std::string &boundary); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items); + Result Post(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); + Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Put(const std::string &path, - const MultipartFormDataItemsForClientInput &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const std::string &boundary); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items); + Result Put(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Params ¶ms); - Result Delete(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Delete(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); + Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Params ¶ms); + Result Patch(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); + Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + + Result Delete(const std::string &path, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Params ¶ms, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const Params ¶ms, DownloadProgress progress = nullptr); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); + // clang-format on bool send(Request &req, Response &res, Error &error); Result send(const Request &req); @@ -1686,7 +1581,7 @@ class ClientImpl { const Headers &headers, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Progress progress); + const std::string &content_type, UploadProgress progress); ContentProviderWithoutLength get_multipart_content_provider( const std::string &boundary, const MultipartFormDataItemsForClientInput &items, @@ -1724,194 +1619,83 @@ class Client { bool is_valid() const; - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, - Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress = nullptr); + // clang-format off + Result Get(const std::string &path, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, DownloadProgress progress = nullptr); + Result Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Head(const std::string &path); Result Head(const std::string &path, const Headers &headers); Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); + Result Post(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Post(const std::string &path, - const MultipartFormDataItemsForClientInput &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const std::string &boundary); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items); + Result Post(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const Params ¶ms); + Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); + Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Put(const std::string &path, - const MultipartFormDataItemsForClientInput &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const std::string &boundary); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items); + Result Put(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const Params ¶ms); + Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Params ¶ms); - Result Delete(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Delete(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); + Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Params ¶ms); + Result Patch(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); + Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + + Result Delete(const std::string &path, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Params ¶ms, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, DownloadProgress progress = nullptr); + Result Delete(const std::string &path, const Headers &headers, const Params ¶ms, DownloadProgress progress = nullptr); Result Options(const std::string &path); Result Options(const std::string &path, const Headers &headers); + // clang-format on bool send(Request &req, Response &res, Error &error); Result send(const Request &req); @@ -4438,7 +4222,7 @@ inline bool read_headers(Stream &strm, Headers &headers) { } inline bool read_content_with_length(Stream &strm, uint64_t len, - Progress progress, + DownloadProgress progress, ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; @@ -4620,8 +4404,8 @@ bool prepare_content_receiver(T &x, int &status, template bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, - Progress progress, ContentReceiverWithProgress receiver, - bool decompress) { + DownloadProgress progress, + ContentReceiverWithProgress receiver, bool decompress) { return prepare_content_receiver( x, status, std::move(receiver), decompress, [&](const ContentReceiverWithProgress &out) { @@ -4705,10 +4489,14 @@ inline bool write_data(Stream &strm, const char *d, size_t l) { } template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, - size_t offset, size_t length, T is_shutting_down, - Error &error) { +inline bool write_content_with_progress(Stream &strm, + const ContentProvider &content_provider, + size_t offset, size_t length, + T is_shutting_down, + const UploadProgress &upload_progress, + Error &error) { size_t end_offset = offset + length; + size_t start_offset = offset; auto ok = true; DataSink data_sink; @@ -4716,6 +4504,14 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, if (ok) { if (write_data(strm, d, l)) { offset += l; + + if (upload_progress && length > 0) { + size_t current_written = offset - start_offset; + if (!upload_progress(current_written, length)) { + ok = false; + return false; + } + } } else { ok = false; } @@ -4742,6 +4538,14 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, return true; } +template +inline bool write_content(Stream &strm, const ContentProvider &content_provider, + size_t offset, size_t length, T is_shutting_down, + Error &error) { + return write_content_with_progress(strm, content_provider, offset, length, + is_shutting_down, nullptr, error); +} + template inline bool write_content(Stream &strm, const ContentProvider &content_provider, size_t offset, size_t length, @@ -8311,8 +8115,9 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm, return detail::write_content_chunked(strm, req.content_provider_, is_shutting_down, *compressor, error); } else { - return detail::write_content(strm, req.content_provider_, 0, - req.content_length_, is_shutting_down, error); + return detail::write_content_with_progress( + strm, req.content_provider_, 0, req.content_length_, is_shutting_down, + req.upload_progress, error); } } @@ -8450,9 +8255,29 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, return write_content_with_provider(strm, req, error); } - if (!detail::write_data(strm, req.body.data(), req.body.size())) { - error = Error::Write; - return false; + if (req.upload_progress) { + auto body_size = req.body.size(); + size_t written = 0; + auto data = req.body.data(); + + while (written < body_size) { + size_t to_write = (std::min)(CPPHTTPLIB_SEND_BUFSIZ, body_size - written); + if (!detail::write_data(strm, data + written, to_write)) { + error = Error::Write; + return false; + } + written += to_write; + + if (!req.upload_progress(written, body_size)) { + error = Error::Canceled; + return false; + } + } + } else { + if (!detail::write_data(strm, req.body.data(), req.body.size())) { + error = Error::Write; + return false; + } } return true; @@ -8541,12 +8366,12 @@ inline Result ClientImpl::send_with_content_provider( const std::string &method, const std::string &path, const Headers &headers, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Progress progress) { + const std::string &content_type, UploadProgress progress) { Request req; req.method = method; req.headers = headers; req.path = path; - req.progress = progress; + req.upload_progress = std::move(progress); if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8623,8 +8448,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, }); auto progress = [&](uint64_t current, uint64_t total) { - if (!req.progress || redirect) { return true; } - auto ret = req.progress(current, total); + if (!req.download_progress || redirect) { return true; } + auto ret = req.download_progress(current, total); if (!ret) { error = Error::Canceled; } return ret; }; @@ -8713,25 +8538,27 @@ inline bool ClientImpl::process_socket( inline bool ClientImpl::is_ssl() const { return false; } -inline Result ClientImpl::Get(const std::string &path) { - return Get(path, Headers(), Progress()); -} - -inline Result ClientImpl::Get(const std::string &path, Progress progress) { +inline Result ClientImpl::Get(const std::string &path, + DownloadProgress progress) { return Get(path, Headers(), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { - return Get(path, headers, Progress()); +inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + DownloadProgress progress) { + if (params.empty()) { return Get(path, headers); } + + std::string path_with_query = append_query_params(path, params); + return Get(path_with_query, headers, std::move(progress)); } inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - Progress progress) { + DownloadProgress progress) { Request req; req.method = "GET"; req.path = path; req.headers = headers; - req.progress = std::move(progress); + req.download_progress = std::move(progress); if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8739,48 +8566,24 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return send_(std::move(req)); } -inline Result ClientImpl::Get(const std::string &path, - ContentReceiver content_receiver) { - return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); -} - inline Result ClientImpl::Get(const std::string &path, ContentReceiver content_receiver, - Progress progress) { + DownloadProgress progress) { return Get(path, Headers(), nullptr, std::move(content_receiver), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return Get(path, headers, nullptr, std::move(content_receiver), nullptr); -} - inline Result ClientImpl::Get(const std::string &path, const Headers &headers, ContentReceiver content_receiver, - Progress progress) { + DownloadProgress progress) { return Get(path, headers, nullptr, std::move(content_receiver), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, Headers(), std::move(response_handler), - std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, headers, std::move(response_handler), - std::move(content_receiver), nullptr); -} - inline Result ClientImpl::Get(const std::string &path, ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress) { + DownloadProgress progress) { return Get(path, Headers(), std::move(response_handler), std::move(content_receiver), std::move(progress)); } @@ -8788,7 +8591,7 @@ inline Result ClientImpl::Get(const std::string &path, inline Result ClientImpl::Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress) { + DownloadProgress progress) { Request req; req.method = "GET"; req.path = path; @@ -8799,7 +8602,7 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, uint64_t /*offset*/, uint64_t /*total_length*/) { return content_receiver(data, data_length); }; - req.progress = std::move(progress); + req.download_progress = std::move(progress); if (max_timeout_msec_ > 0) { req.start_time_ = std::chrono::steady_clock::now(); } @@ -8807,18 +8610,10 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return send_(std::move(req)); } -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { - if (params.empty()) { return Get(path, headers); } - - std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, std::move(progress)); -} - inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, const Headers &headers, ContentReceiver content_receiver, - Progress progress) { + DownloadProgress progress) { return Get(path, params, headers, nullptr, std::move(content_receiver), std::move(progress)); } @@ -8827,7 +8622,7 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress) { + DownloadProgress progress) { if (params.empty()) { return Get(path, headers, std::move(response_handler), std::move(content_receiver), std::move(progress)); @@ -8859,92 +8654,42 @@ inline Result ClientImpl::Post(const std::string &path) { return Post(path, std::string(), std::string()); } -inline Result ClientImpl::Post(const std::string &path, - const Headers &headers) { - return Post(path, headers, nullptr, 0, std::string()); +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + UploadProgress progress) { + return Post(path, headers, nullptr, 0, std::string(), progress); } inline Result ClientImpl::Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type) { - return Post(path, Headers(), body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, const std::string &content_type, - Progress progress) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); -} - -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return Post(path, Headers(), body, content_type); + UploadProgress progress) { + return Post(path, Headers(), body, content_length, content_type, progress); } inline Result ClientImpl::Post(const std::string &path, const std::string &body, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return Post(path, Headers(), body, content_type, progress); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { return Post(path, Headers(), params); } inline Result ClientImpl::Post(const std::string &path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return Post(path, Headers(), content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result ClientImpl::Post(const std::string &path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Post(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); + const std::string &content_type, + UploadProgress progress) { + return Post(path, Headers(), std::move(content_provider), content_type, + progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, @@ -8953,33 +8698,28 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, return Post(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - auto query = detail::params_to_query_str(params); - return Post(path, headers, query, "application/x-www-form-urlencoded", - progress); -} - inline Result ClientImpl::Post(const std::string &path, - const MultipartFormDataItemsForClientInput &items) { - return Post(path, Headers(), items); + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return Post(path, Headers(), items, progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items) { + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type); + return Post(path, headers, body, content_type, progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, - const std::string &boundary) { + const std::string &boundary, UploadProgress progress) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } @@ -8987,107 +8727,99 @@ ClientImpl::Post(const std::string &path, const Headers &headers, const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type); + return Post(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items) { + const MultipartFormDataProviderItems &provider_items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); return send_with_content_provider( "POST", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type, nullptr); + content_type, progress); } inline Result ClientImpl::Put(const std::string &path) { return Put(path, std::string(), std::string()); } -inline Result ClientImpl::Put(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Put(path, Headers(), body, content_length, content_type); -} - inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); + UploadProgress progress) { + return Put(path, headers, nullptr, 0, std::string(), progress); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, +inline Result ClientImpl::Put(const std::string &path, const char *body, + size_t content_length, const std::string &content_type, - Progress progress) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); -} - -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return Put(path, Headers(), body, content_type); + UploadProgress progress) { + return Put(path, Headers(), body, content_length, content_type, progress); } inline Result ClientImpl::Put(const std::string &path, const std::string &body, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return Put(path, Headers(), body, content_type, progress); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); +inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { + return Put(path, Headers(), params); } inline Result ClientImpl::Put(const std::string &path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return Put(path, Headers(), content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result ClientImpl::Put(const std::string &path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Put(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { - return Put(path, Headers(), params); + const std::string &content_type, + UploadProgress progress) { + return Put(path, Headers(), std::move(content_provider), content_type, + progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, @@ -9096,32 +8828,26 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, return Put(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - auto query = detail::params_to_query_str(params); - return Put(path, headers, query, "application/x-www-form-urlencoded", - progress); -} - -inline Result -ClientImpl::Put(const std::string &path, - const MultipartFormDataItemsForClientInput &items) { - return Put(path, Headers(), items); +inline Result ClientImpl::Put(const std::string &path, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return Put(path, Headers(), items, progress); } -inline Result -ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items) { +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Put(path, headers, body, content_type); + return Put(path, headers, body, content_type, progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, - const std::string &boundary) { + const std::string &boundary, + UploadProgress progress) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } @@ -9129,209 +8855,259 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Put(path, headers, body, content_type); + return Put(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, body, content_length, + nullptr, nullptr, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, body.data(), + body.size(), nullptr, nullptr, content_type, + progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, nullptr, + content_length, std::move(content_provider), + nullptr, content_type, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, + std::move(content_provider), content_type, + progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items) { + const MultipartFormDataProviderItems &provider_items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); return send_with_content_provider( "PUT", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type, nullptr); + content_type, progress); } + inline Result ClientImpl::Patch(const std::string &path) { return Patch(path, std::string(), std::string()); } -inline Result ClientImpl::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Patch(path, Headers(), body, content_length, content_type); +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + UploadProgress progress) { + return Patch(path, headers, nullptr, 0, std::string(), progress); } inline Result ClientImpl::Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return Patch(path, Headers(), body, content_length, content_type, progress); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return Patch(path, headers, body, content_length, content_type, nullptr); +inline Result ClientImpl::Patch(const std::string &path, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return Patch(path, Headers(), body, content_type, progress); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PATCH", path, headers, body, - content_length, nullptr, nullptr, - content_type, progress); +inline Result ClientImpl::Patch(const std::string &path, const Params ¶ms) { + return Patch(path, Headers(), params); } -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Patch(path, Headers(), body, content_type); +inline Result ClientImpl::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return Patch(path, Headers(), content_length, std::move(content_provider), + content_type, progress); } inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, + ContentProviderWithoutLength content_provider, const std::string &content_type, - Progress progress) { - return Patch(path, Headers(), body, content_type, progress); + UploadProgress progress) { + return Patch(path, Headers(), std::move(content_provider), content_type, + progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Patch(path, headers, body, content_type, nullptr); + const Params ¶ms) { + auto query = detail::params_to_query_str(params); + return Patch(path, headers, query, "application/x-www-form-urlencoded"); +} + +inline Result +ClientImpl::Patch(const std::string &path, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return Patch(path, Headers(), items, progress); +} + +inline Result +ClientImpl::Patch(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Patch(path, headers, body, content_type, progress); +} + +inline Result +ClientImpl::Patch(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary, UploadProgress progress) { + if (!detail::is_multipart_boundary_chars_valid(boundary)) { + return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; + } + + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + const auto &body = detail::serialize_multipart_formdata(items, boundary); + return Patch(path, headers, body, content_type, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider("PATCH", path, headers, body, + content_length, nullptr, nullptr, + content_type, progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return send_with_content_provider("PATCH", path, headers, body.data(), body.size(), nullptr, nullptr, content_type, progress); } -inline Result ClientImpl::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Patch(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Patch(path, Headers(), std::move(content_provider), content_type); -} - inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return send_with_content_provider("PATCH", path, headers, nullptr, content_length, std::move(content_provider), - nullptr, content_type, nullptr); + nullptr, content_type, progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, std::move(content_provider), content_type, - nullptr); + progress); } -inline Result ClientImpl::Delete(const std::string &path) { - return Delete(path, Headers(), std::string(), std::string()); +inline Result +ClientImpl::Patch(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const MultipartFormDataProviderItems &provider_items, + UploadProgress progress) { + const auto &boundary = detail::make_multipart_data_boundary(); + const auto &content_type = + detail::serialize_multipart_formdata_get_content_type(boundary); + return send_with_content_provider( + "PATCH", path, headers, nullptr, 0, nullptr, + get_multipart_content_provider(boundary, items, provider_items), + content_type, progress); } inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers) { - return Delete(path, headers, std::string(), std::string()); + DownloadProgress progress) { + return Delete(path, Headers(), std::string(), std::string(), progress); } -inline Result ClientImpl::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, Headers(), body, content_length, content_type); +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, + DownloadProgress progress) { + return Delete(path, headers, std::string(), std::string(), progress); } inline Result ClientImpl::Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return Delete(path, Headers(), body, content_length, content_type, progress); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, headers, body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - Request req; - req.method = "DELETE"; - req.headers = headers; - req.path = path; - req.progress = progress; - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - if (!content_type.empty()) { req.set_header("Content-Type", content_type); } - req.body.assign(body, content_length); - - return send_(std::move(req)); -} - -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Delete(path, Headers(), body.data(), body.size(), content_type); -} - inline Result ClientImpl::Delete(const std::string &path, const std::string &body, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return Delete(path, Headers(), body.data(), body.size(), content_type, progress); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Delete(path, headers, body.data(), body.size(), content_type); -} - inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return Delete(path, headers, body.data(), body.size(), content_type, progress); } -inline Result ClientImpl::Delete(const std::string &path, - const Params ¶ms) { - return Delete(path, Headers(), params); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Delete(path, headers, query, "application/x-www-form-urlencoded"); +inline Result ClientImpl::Delete(const std::string &path, const Params ¶ms, + DownloadProgress progress) { + return Delete(path, Headers(), params, progress); } inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, const Params ¶ms, - Progress progress) { + DownloadProgress progress) { auto query = detail::params_to_query_str(params); return Delete(path, headers, query, "application/x-www-form-urlencoded", progress); } +inline Result ClientImpl::Delete(const std::string &path, + const Headers &headers, const char *body, + size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + Request req; + req.method = "DELETE"; + req.headers = headers; + req.path = path; + req.download_progress = std::move(progress); + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + req.body.assign(body, content_length); + + return send_(std::move(req)); +} + inline Result ClientImpl::Options(const std::string &path) { return Options(path, Headers()); } @@ -10428,72 +10204,54 @@ inline bool Client::is_valid() const { return cli_ != nullptr && cli_->is_valid(); } -inline Result Client::Get(const std::string &path) { return cli_->Get(path); } -inline Result Client::Get(const std::string &path, const Headers &headers) { - return cli_->Get(path, headers); -} -inline Result Client::Get(const std::string &path, Progress progress) { +inline Result Client::Get(const std::string &path, DownloadProgress progress) { return cli_->Get(path, std::move(progress)); } inline Result Client::Get(const std::string &path, const Headers &headers, - Progress progress) { + DownloadProgress progress) { return cli_->Get(path, headers, std::move(progress)); } inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver, Progress progress) { + ContentReceiver content_receiver, + DownloadProgress progress) { return cli_->Get(path, std::move(content_receiver), std::move(progress)); } inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress) { + ContentReceiver content_receiver, + DownloadProgress progress) { return cli_->Get(path, headers, std::move(content_receiver), std::move(progress)); } inline Result Client::Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { + ContentReceiver content_receiver, + DownloadProgress progress) { return cli_->Get(path, std::move(response_handler), std::move(content_receiver), std::move(progress)); } inline Result Client::Get(const std::string &path, const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { + ContentReceiver content_receiver, + DownloadProgress progress) { return cli_->Get(path, headers, std::move(response_handler), std::move(content_receiver), std::move(progress)); } inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { + const Headers &headers, DownloadProgress progress) { return cli_->Get(path, params, headers, std::move(progress)); } inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, - ContentReceiver content_receiver, Progress progress) { + ContentReceiver content_receiver, + DownloadProgress progress) { return cli_->Get(path, params, headers, std::move(content_receiver), std::move(progress)); } inline Result Client::Get(const std::string &path, const Params ¶ms, const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { + ContentReceiver content_receiver, + DownloadProgress progress) { return cli_->Get(path, params, headers, std::move(response_handler), std::move(content_receiver), std::move(progress)); } @@ -10504,65 +10262,61 @@ inline Result Client::Head(const std::string &path, const Headers &headers) { } inline Result Client::Post(const std::string &path) { return cli_->Post(path); } -inline Result Client::Post(const std::string &path, const Headers &headers) { - return cli_->Post(path, headers); +inline Result Client::Post(const std::string &path, const Headers &headers, + UploadProgress progress) { + return cli_->Post(path, headers, progress); } inline Result Client::Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Post(path, body, content_length, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_length, content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, body, content_length, content_type, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress) { + const std::string &content_type, + UploadProgress progress) { return cli_->Post(path, headers, body, content_length, content_type, progress); } inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Post(path, body, content_type); -} -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + UploadProgress progress) { return cli_->Post(path, body, content_type, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, const std::string &body, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + UploadProgress progress) { return cli_->Post(path, headers, body, content_type, progress); } inline Result Client::Post(const std::string &path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return cli_->Post(path, content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result Client::Post(const std::string &path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Post(path, std::move(content_provider), content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, std::move(content_provider), content_type, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return cli_->Post(path, headers, content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Post(path, headers, std::move(content_provider), content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, std::move(content_provider), content_type, + progress); } inline Result Client::Post(const std::string &path, const Params ¶ms) { return cli_->Post(path, params); @@ -10571,85 +10325,84 @@ inline Result Client::Post(const std::string &path, const Headers &headers, const Params ¶ms) { return cli_->Post(path, headers, params); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - return cli_->Post(path, headers, params, progress); -} inline Result Client::Post(const std::string &path, - const MultipartFormDataItemsForClientInput &items) { - return cli_->Post(path, items); + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return cli_->Post(path, items, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items) { - return cli_->Post(path, headers, items); + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return cli_->Post(path, headers, items, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, - const std::string &boundary) { - return cli_->Post(path, headers, items, boundary); + const std::string &boundary, + UploadProgress progress) { + return cli_->Post(path, headers, items, boundary, progress); } -inline Result -Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items) { - return cli_->Post(path, headers, items, provider_items); +inline Result Client::Post(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const MultipartFormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Post(path, headers, items, provider_items, progress); } + inline Result Client::Put(const std::string &path) { return cli_->Put(path); } +inline Result Client::Put(const std::string &path, const Headers &headers, + UploadProgress progress) { + return cli_->Put(path, headers, progress); +} inline Result Client::Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Put(path, body, content_length, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_length, content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, body, content_length, content_type, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, - const std::string &content_type, Progress progress) { + const std::string &content_type, + UploadProgress progress) { return cli_->Put(path, headers, body, content_length, content_type, progress); } inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Put(path, body, content_type); -} -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + UploadProgress progress) { return cli_->Put(path, body, content_type, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, const std::string &body, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, Progress progress) { + const std::string &content_type, + UploadProgress progress) { return cli_->Put(path, headers, body, content_type, progress); } inline Result Client::Put(const std::string &path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return cli_->Put(path, content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result Client::Put(const std::string &path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Put(path, std::move(content_provider), content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, std::move(content_provider), content_type, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return cli_->Put(path, headers, content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Put(path, headers, std::move(content_provider), content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, std::move(content_provider), content_type, + progress); } inline Result Client::Put(const std::string &path, const Params ¶ms) { return cli_->Put(path, params); @@ -10658,158 +10411,160 @@ inline Result Client::Put(const std::string &path, const Headers &headers, const Params ¶ms) { return cli_->Put(path, headers, params); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - return cli_->Put(path, headers, params, progress); -} inline Result Client::Put(const std::string &path, - const MultipartFormDataItemsForClientInput &items) { - return cli_->Put(path, items); + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return cli_->Put(path, items, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items) { - return cli_->Put(path, headers, items); + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return cli_->Put(path, headers, items, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, - const std::string &boundary) { - return cli_->Put(path, headers, items, boundary); + const std::string &boundary, + UploadProgress progress) { + return cli_->Put(path, headers, items, boundary, progress); } -inline Result -Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items) { - return cli_->Put(path, headers, items, provider_items); +inline Result Client::Put(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const MultipartFormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Put(path, headers, items, provider_items, progress); } + inline Result Client::Patch(const std::string &path) { return cli_->Patch(path); } -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, body, content_length, content_type); +inline Result Client::Patch(const std::string &path, const Headers &headers, + UploadProgress progress) { + return cli_->Patch(path, headers, progress); } inline Result Client::Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return cli_->Patch(path, body, content_length, content_type, progress); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_length, content_type); -} inline Result Client::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return cli_->Patch(path, headers, body, content_length, content_type, progress); } -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, body, content_type); -} inline Result Client::Patch(const std::string &path, const std::string &body, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return cli_->Patch(path, body, content_type, progress); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_type); -} inline Result Client::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress) { + UploadProgress progress) { return cli_->Patch(path, headers, body, content_type, progress); } inline Result Client::Patch(const std::string &path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return cli_->Patch(path, content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result Client::Patch(const std::string &path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Patch(path, std::move(content_provider), content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, std::move(content_provider), content_type, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string &content_type, + UploadProgress progress) { return cli_->Patch(path, headers, content_length, std::move(content_provider), - content_type); + content_type, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Patch(path, headers, std::move(content_provider), content_type); + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, std::move(content_provider), content_type, + progress); } -inline Result Client::Delete(const std::string &path) { - return cli_->Delete(path); +inline Result Client::Patch(const std::string &path, const Params ¶ms) { + return cli_->Patch(path, params); } -inline Result Client::Delete(const std::string &path, const Headers &headers) { - return cli_->Delete(path, headers); +inline Result Client::Patch(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Patch(path, headers, params); } -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, body, content_length, content_type); +inline Result Client::Patch(const std::string &path, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return cli_->Patch(path, items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Patch(path, headers, items, boundary, progress); +} +inline Result +Client::Patch(const std::string &path, const Headers &headers, + const MultipartFormDataItemsForClientInput &items, + const MultipartFormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, provider_items, progress); +} + +inline Result Client::Delete(const std::string &path, + DownloadProgress progress) { + return cli_->Delete(path, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + DownloadProgress progress) { + return cli_->Delete(path, headers, progress); } inline Result Client::Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return cli_->Delete(path, body, content_length, content_type, progress); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_length, content_type); -} inline Result Client::Delete(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return cli_->Delete(path, headers, body, content_length, content_type, progress); } -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, body, content_type); -} inline Result Client::Delete(const std::string &path, const std::string &body, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return cli_->Delete(path, body, content_type, progress); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_type); -} inline Result Client::Delete(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, - Progress progress) { + DownloadProgress progress) { return cli_->Delete(path, headers, body, content_type, progress); } -inline Result Client::Delete(const std::string &path, const Params ¶ms) { - return cli_->Delete(path, params); +inline Result Client::Delete(const std::string &path, const Params ¶ms, + DownloadProgress progress) { + return cli_->Delete(path, params, progress); } inline Result Client::Delete(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Delete(path, headers, params); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { + const Params ¶ms, DownloadProgress progress) { return cli_->Delete(path, headers, params, progress); } + inline Result Client::Options(const std::string &path) { return cli_->Options(path); } diff --git a/test/test.cc b/test/test.cc index 4797795fea..204c2d2805 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6115,6 +6115,273 @@ TEST(ServerStopTest, Decommision) { } } +// Helper function for string body upload progress tests +template +void TestStringBodyUploadProgress(SetupHandler &&setup_handler, + ClientCall &&client_call, + const string &body) { + Server svr; + setup_handler(svr); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + vector progress_values; + bool progress_called = false; + + auto res = + client_call(cli, body, [&](uint64_t current, uint64_t /*total*/) -> bool { + progress_values.push_back(current); + progress_called = true; + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_TRUE(progress_called); +} + +TEST(UploadProgressTest, PostStringBodyBasic) { + TestStringBodyUploadProgress( + [](Server &svr) { + svr.Post("/test", [](const Request & /*req*/, Response &res) { + res.set_content("received", "text/plain"); + }); + }, + [](Client &cli, const string &body, UploadProgress progress_callback) { + return cli.Post("/test", body, "text/plain", progress_callback); + }, + "test data for upload progress"); +} + +TEST(UploadProgressTest, PutStringBodyBasic) { + TestStringBodyUploadProgress( + [](Server &svr) { + svr.Put("/test", [](const Request & /*req*/, Response &res) { + res.set_content("put received", "text/plain"); + }); + }, + [](Client &cli, const string &body, UploadProgress progress_callback) { + return cli.Put("/test", body, "text/plain", progress_callback); + }, + "put test data for upload progress"); +} + +TEST(UploadProgressTest, PatchStringBodyBasic) { + TestStringBodyUploadProgress( + [](Server &svr) { + svr.Patch("/test", [](const Request & /*req*/, Response &res) { + res.set_content("patch received", "text/plain"); + }); + }, + [](Client &cli, const string &body, UploadProgress progress_callback) { + return cli.Patch("/test", body, "text/plain", progress_callback); + }, + "patch test data for upload progress"); +} + +// Helper function for content provider upload progress tests +template +void TestContentProviderUploadProgress(SetupHandler &&setup_handler, + ClientCall &&client_call) { + Server svr; + setup_handler(svr); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + svr.wait_until_ready(); + + Client cli(HOST, PORT); + vector progress_values; + + auto res = + client_call(cli, [&](uint64_t current, uint64_t /*total*/) -> bool { + progress_values.push_back(current); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_FALSE(progress_values.empty()); +} + +TEST(UploadProgressTest, PostContentProviderProgress) { + TestContentProviderUploadProgress( + [](Server &svr) { + svr.Post("/test", [](const Request & /*req*/, Response &res) { + res.set_content("provider received", "text/plain"); + }); + }, + [](Client &cli, UploadProgress progress_callback) { + return cli.Post( + "/test", 10, + [](size_t /*offset*/, size_t /*length*/, DataSink &sink) -> bool { + sink.os << "test data"; + return true; + }, + "text/plain", progress_callback); + }); +} + +// Helper function for multipart upload progress tests +template +void TestMultipartUploadProgress(SetupHandler &&setup_handler, + ClientCall &&client_call, + const string &endpoint) { + Server svr; + setup_handler(svr); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + svr.wait_until_ready(); + + Client cli(HOST, PORT); + vector progress_values; + + MultipartFormDataItemsForClientInput items = { + {"field1", "value1", "", ""}, + {"field2", "longer value for progress tracking test", "", ""}, + {"file1", "file content data for upload progress", "test.txt", + "text/plain"}}; + + auto res = client_call(cli, endpoint, items, + [&](uint64_t current, uint64_t /*total*/) -> bool { + progress_values.push_back(current); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_FALSE(progress_values.empty()); +} + +TEST(UploadProgressTest, PostMultipartProgress) { + TestMultipartUploadProgress( + [](Server &svr) { + svr.Post("/multipart", [](const Request &req, Response &res) { + EXPECT_FALSE(req.files.empty()); + res.set_content("multipart received", "text/plain"); + }); + }, + [](Client &cli, const string &endpoint, + const MultipartFormDataItemsForClientInput &items, + UploadProgress progress_callback) { + return cli.Post(endpoint, items, progress_callback); + }, + "/multipart"); +} + +// Helper function for basic download progress tests +template +void TestBasicDownloadProgress(SetupHandler &&setup_handler, + ClientCall &&client_call, const string &endpoint, + size_t expected_content_size) { + Server svr; + setup_handler(svr); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + svr.wait_until_ready(); + + Client cli(HOST, PORT); + vector progress_values; + + auto res = client_call(cli, endpoint, + [&](uint64_t current, uint64_t /*total*/) -> bool { + progress_values.push_back(current); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_FALSE(progress_values.empty()); + EXPECT_EQ(expected_content_size, res->body.size()); +} + +TEST(DownloadProgressTest, GetBasic) { + TestBasicDownloadProgress( + [](Server &svr) { + svr.Get("/download", [](const Request & /*req*/, Response &res) { + string content(1000, 'D'); + res.set_content(content, "text/plain"); + }); + }, + [](Client &cli, const string &endpoint, + DownloadProgress progress_callback) { + return cli.Get(endpoint, progress_callback); + }, + "/download", 1000u); +} + +// Helper function for content receiver download progress tests +template +void TestContentReceiverDownloadProgress(SetupHandler &&setup_handler, + ClientCall &&client_call, + const string &endpoint, + size_t expected_content_size) { + Server svr; + setup_handler(svr); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + svr.wait_until_ready(); + + Client cli(HOST, PORT); + vector progress_values; + string received_body; + + auto res = client_call( + cli, endpoint, + [&](const char *data, size_t data_length) -> bool { + received_body.append(data, data_length); + return true; + }, + [&](uint64_t current, uint64_t /*total*/) -> bool { + progress_values.push_back(current); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_FALSE(progress_values.empty()); + EXPECT_EQ(expected_content_size, received_body.size()); + EXPECT_TRUE(res->body.empty()); +} + +TEST(DownloadProgressTest, GetWithContentReceiver) { + TestContentReceiverDownloadProgress( + [](Server &svr) { + svr.Get("/download-receiver", + [](const Request & /*req*/, Response &res) { + string content(2000, 'R'); + res.set_content(content, "text/plain"); + }); + }, + [](Client &cli, const string &endpoint, ContentReceiver content_receiver, + DownloadProgress progress_callback) { + return cli.Get(endpoint, content_receiver, progress_callback); + }, + "/download-receiver", 2000u); +} + TEST(StreamingTest, NoContentLengthStreaming) { Server svr; From ea850cbfa74e2dff228c49bf94542ce5331d73b5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 29 Jun 2025 00:13:09 -0400 Subject: [PATCH 0451/1222] Fix #1601 (#2167) * Fix #1601 * clang-format * Fix Windows problem * Use GetAddrInfoEx on Windows * Fix Windows problem * Add getaddrinfo_a * clang-format * Adjust Benchmark Test * Test * Fix Bench test * Fix build error * Fix build error * Fix Makefile * Fix build error * Fix buid error --- httplib.h | 346 ++++++++++++++++++++++++++++++++++++++++-- test/Makefile | 14 +- test/fuzzing/Makefile | 2 +- test/test.cc | 25 +-- 4 files changed, 356 insertions(+), 31 deletions(-) diff --git a/httplib.h b/httplib.h index cfeb051541..ad03015339 100644 --- a/httplib.h +++ b/httplib.h @@ -278,6 +278,14 @@ using socket_t = int; #include #include +#if defined(__APPLE__) +#include +#if TARGET_OS_OSX || TARGET_OS_IPHONE +#include +#include +#endif +#endif + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef _WIN32 #include @@ -292,13 +300,16 @@ using socket_t = int; #ifdef _MSC_VER #pragma comment(lib, "crypt32.lib") #endif -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) +#endif // _WIN32 + +#if defined(__APPLE__) +#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #include #if TARGET_OS_OSX -#include #include #endif // TARGET_OS_OSX -#endif // _WIN32 +#endif // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN +#endif // ___APPLE__ #include #include @@ -321,7 +332,7 @@ using socket_t = int; #error Sorry, OpenSSL versions prior to 3.0.0 are not supported #endif -#endif +#endif // CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_ZLIB_SUPPORT #include @@ -3369,11 +3380,323 @@ unescape_abstract_namespace_unix_domain(const std::string &s) { return s; } +inline int getaddrinfo_with_timeout(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res, time_t timeout_sec) { + if (timeout_sec <= 0) { + // No timeout specified, use standard getaddrinfo + return getaddrinfo(node, service, hints, res); + } + +#ifdef _WIN32 + // Windows-specific implementation using GetAddrInfoEx with overlapped I/O + OVERLAPPED overlapped = {0}; + HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); + if (!event) { return EAI_FAIL; } + + overlapped.hEvent = event; + + PADDRINFOEXW result_addrinfo = nullptr; + HANDLE cancel_handle = nullptr; + + ADDRINFOEXW hints_ex = {0}; + if (hints) { + hints_ex.ai_flags = hints->ai_flags; + hints_ex.ai_family = hints->ai_family; + hints_ex.ai_socktype = hints->ai_socktype; + hints_ex.ai_protocol = hints->ai_protocol; + } + + auto wnode = u8string_to_wstring(node); + auto wservice = u8string_to_wstring(service); + + auto ret = ::GetAddrInfoExW(wnode.data(), wservice.data(), NS_DNS, nullptr, + hints ? &hints_ex : nullptr, &result_addrinfo, + nullptr, &overlapped, nullptr, &cancel_handle); + + if (ret == WSA_IO_PENDING) { + auto wait_result = + ::WaitForSingleObject(event, static_cast(timeout_sec * 1000)); + if (wait_result == WAIT_TIMEOUT) { + if (cancel_handle) { ::GetAddrInfoExCancel(&cancel_handle); } + ::CloseHandle(event); + return EAI_AGAIN; + } + + DWORD bytes_returned; + if (!::GetOverlappedResult((HANDLE)INVALID_SOCKET, &overlapped, + &bytes_returned, FALSE)) { + ::CloseHandle(event); + return ::WSAGetLastError(); + } + } + + ::CloseHandle(event); + + if (ret == NO_ERROR || ret == WSA_IO_PENDING) { + *res = reinterpret_cast(result_addrinfo); + return 0; + } + + return ret; +#elif defined(__APPLE__) + // macOS implementation using CFHost API for asynchronous DNS resolution + CFStringRef hostname_ref = CFStringCreateWithCString( + kCFAllocatorDefault, node, kCFStringEncodingUTF8); + if (!hostname_ref) { return EAI_MEMORY; } + + CFHostRef host_ref = CFHostCreateWithName(kCFAllocatorDefault, hostname_ref); + CFRelease(hostname_ref); + if (!host_ref) { return EAI_MEMORY; } + + // Set up context for callback + struct CFHostContext { + bool completed = false; + bool success = false; + CFArrayRef addresses = nullptr; + std::mutex mutex; + std::condition_variable cv; + } context; + + CFHostClientContext client_context; + memset(&client_context, 0, sizeof(client_context)); + client_context.info = &context; + + // Set callback + auto callback = [](CFHostRef theHost, CFHostInfoType /*typeInfo*/, + const CFStreamError *error, void *info) { + auto ctx = static_cast(info); + std::lock_guard lock(ctx->mutex); + + if (error && error->error != 0) { + ctx->success = false; + } else { + Boolean hasBeenResolved; + ctx->addresses = CFHostGetAddressing(theHost, &hasBeenResolved); + if (ctx->addresses && hasBeenResolved) { + CFRetain(ctx->addresses); + ctx->success = true; + } else { + ctx->success = false; + } + } + ctx->completed = true; + ctx->cv.notify_one(); + }; + + if (!CFHostSetClient(host_ref, callback, &client_context)) { + CFRelease(host_ref); + return EAI_SYSTEM; + } + + // Schedule on run loop + CFRunLoopRef run_loop = CFRunLoopGetCurrent(); + CFHostScheduleWithRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + + // Start resolution + CFStreamError stream_error; + if (!CFHostStartInfoResolution(host_ref, kCFHostAddresses, &stream_error)) { + CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + CFRelease(host_ref); + return EAI_FAIL; + } + + // Wait for completion with timeout + auto timeout_time = + std::chrono::steady_clock::now() + std::chrono::seconds(timeout_sec); + bool timed_out = false; + + { + std::unique_lock lock(context.mutex); + + while (!context.completed) { + auto now = std::chrono::steady_clock::now(); + if (now >= timeout_time) { + timed_out = true; + break; + } + + // Run the runloop for a short time + lock.unlock(); + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true); + lock.lock(); + } + } + + // Clean up + CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + CFHostSetClient(host_ref, nullptr, nullptr); + + if (timed_out || !context.completed) { + CFHostCancelInfoResolution(host_ref, kCFHostAddresses); + CFRelease(host_ref); + return EAI_AGAIN; + } + + if (!context.success || !context.addresses) { + CFRelease(host_ref); + return EAI_NODATA; + } + + // Convert CFArray to addrinfo + CFIndex count = CFArrayGetCount(context.addresses); + if (count == 0) { + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_NODATA; + } + + struct addrinfo *result_addrinfo = nullptr; + struct addrinfo **current = &result_addrinfo; + + for (CFIndex i = 0; i < count; i++) { + CFDataRef addr_data = + static_cast(CFArrayGetValueAtIndex(context.addresses, i)); + if (!addr_data) continue; + + const struct sockaddr *sockaddr_ptr = + reinterpret_cast(CFDataGetBytePtr(addr_data)); + socklen_t sockaddr_len = static_cast(CFDataGetLength(addr_data)); + + // Allocate addrinfo structure + *current = static_cast(malloc(sizeof(struct addrinfo))); + if (!*current) { + freeaddrinfo(result_addrinfo); + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_MEMORY; + } + + memset(*current, 0, sizeof(struct addrinfo)); + + // Set up addrinfo fields + (*current)->ai_family = sockaddr_ptr->sa_family; + (*current)->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM; + (*current)->ai_protocol = hints ? hints->ai_protocol : IPPROTO_TCP; + (*current)->ai_addrlen = sockaddr_len; + + // Copy sockaddr + (*current)->ai_addr = static_cast(malloc(sockaddr_len)); + if (!(*current)->ai_addr) { + freeaddrinfo(result_addrinfo); + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_MEMORY; + } + memcpy((*current)->ai_addr, sockaddr_ptr, sockaddr_len); + + // Set port if service is specified + if (service && strlen(service) > 0) { + int port = atoi(service); + if (port > 0) { + if (sockaddr_ptr->sa_family == AF_INET) { + reinterpret_cast((*current)->ai_addr) + ->sin_port = htons(static_cast(port)); + } else if (sockaddr_ptr->sa_family == AF_INET6) { + reinterpret_cast((*current)->ai_addr) + ->sin6_port = htons(static_cast(port)); + } + } + } + + current = &((*current)->ai_next); + } + + CFRelease(context.addresses); + CFRelease(host_ref); + + *res = result_addrinfo; + return 0; +#elif defined(_GNU_SOURCE) && defined(__GLIBC__) && \ + (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) + // Linux implementation using getaddrinfo_a for asynchronous DNS resolution + struct gaicb request; + struct gaicb *requests[1] = {&request}; + struct sigevent sevp; + struct timespec timeout; + + // Initialize the request structure + memset(&request, 0, sizeof(request)); + request.ar_name = node; + request.ar_service = service; + request.ar_request = hints; + + // Set up timeout + timeout.tv_sec = timeout_sec; + timeout.tv_nsec = 0; + + // Initialize sigevent structure (not used, but required) + memset(&sevp, 0, sizeof(sevp)); + sevp.sigev_notify = SIGEV_NONE; + + // Start asynchronous resolution + int start_result = getaddrinfo_a(GAI_NOWAIT, requests, 1, &sevp); + if (start_result != 0) { return start_result; } + + // Wait for completion with timeout + int wait_result = + gai_suspend((const struct gaicb *const *)requests, 1, &timeout); + + if (wait_result == 0) { + // Completed successfully, get the result + int gai_result = gai_error(&request); + if (gai_result == 0) { + *res = request.ar_result; + return 0; + } else { + // Clean up on error + if (request.ar_result) { freeaddrinfo(request.ar_result); } + return gai_result; + } + } else if (wait_result == EAI_AGAIN) { + // Timeout occurred, cancel the request + gai_cancel(&request); + return EAI_AGAIN; + } else { + // Other error occurred + gai_cancel(&request); + return wait_result; + } +#else + // Fallback implementation using thread-based timeout for other Unix systems + std::mutex result_mutex; + std::condition_variable result_cv; + auto completed = false; + auto result = EAI_SYSTEM; + struct addrinfo *result_addrinfo = nullptr; + + std::thread resolve_thread([&]() { + auto thread_result = getaddrinfo(node, service, hints, &result_addrinfo); + + std::lock_guard lock(result_mutex); + result = thread_result; + completed = true; + result_cv.notify_one(); + }); + + // Wait for completion or timeout + std::unique_lock lock(result_mutex); + auto finished = result_cv.wait_for(lock, std::chrono::seconds(timeout_sec), + [&] { return completed; }); + + if (finished) { + // Operation completed within timeout + resolve_thread.join(); + *res = result_addrinfo; + return result; + } else { + // Timeout occurred + resolve_thread.detach(); // Let the thread finish in background + return EAI_AGAIN; // Return timeout error + } +#endif +} + template socket_t create_socket(const std::string &host, const std::string &ip, int port, int address_family, int socket_flags, bool tcp_nodelay, bool ipv6_v6only, SocketOptions socket_options, - BindOrConnect bind_or_connect) { + BindOrConnect bind_or_connect, time_t timeout_sec = 0) { // Get address info const char *node = nullptr; struct addrinfo hints; @@ -3443,7 +3766,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, auto service = std::to_string(port); - if (getaddrinfo(node, service.c_str(), &hints, &result)) { + if (getaddrinfo_with_timeout(node, service.c_str(), &hints, &result, + timeout_sec)) { #if defined __linux__ && !defined __ANDROID__ res_init(); #endif @@ -3541,7 +3865,9 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; - if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + if (getaddrinfo_with_timeout(host.c_str(), "0", &hints, &result, 0)) { + return false; + } auto se = detail::scope_exit([&] { freeaddrinfo(result); }); auto ret = false; @@ -3646,7 +3972,8 @@ inline socket_t create_client_socket( error = Error::Success; return true; - }); + }, + connection_timeout_sec); // Pass DNS timeout if (sock != INVALID_SOCKET) { error = Error::Success; @@ -5867,7 +6194,8 @@ inline void hosted_at(const std::string &hostname, hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; - if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result)) { + if (detail::getaddrinfo_with_timeout(hostname.c_str(), nullptr, &hints, + &result, 0)) { #if defined __linux__ && !defined __ANDROID__ res_init(); #endif diff --git a/test/Makefile b/test/Makefile index 3107702beb..8b369917d2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,7 +9,7 @@ OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPEN ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Darwin) - OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework CoreFoundation -framework Security + OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework CoreFoundation -framework Security -framework CFNetwork endif endif @@ -21,7 +21,15 @@ BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_ ZSTD_DIR = $(PREFIX)/opt/zstd ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -I$(ZSTD_DIR)/include -L$(ZSTD_DIR)/lib -lzstd -TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) -pthread -lcurl +LIBS = -lpthread -lcurl +ifneq ($(OS), Windows_NT) + UNAME_S := $(shell uname -s) + ifneq ($(UNAME_S), Darwin) + LIBS += -lanl + endif +endif + +TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS) # By default, use standalone_fuzz_target_runner. # This runner does no fuzzing, but simply executes the inputs @@ -86,7 +94,7 @@ fuzz_test: server_fuzzer # Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use. server_fuzzer : fuzzing/server_fuzzer.cc ../httplib.h standalone_fuzz_target_runner.o - $(CXX) -o $@ -I.. $(CXXFLAGS) $< $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread + $(CXX) -o $@ -I.. $(CXXFLAGS) $< $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(LIB_FUZZING_ENGINE) $(ZSTD_SUPPORT) $(LIBS) @file $@ # Standalone fuzz runner, which just reads inputs from fuzzing/corpus/ dir and diff --git a/test/fuzzing/Makefile b/test/fuzzing/Makefile index d6a3e21bc1..b08ecd0848 100644 --- a/test/fuzzing/Makefile +++ b/test/fuzzing/Makefile @@ -20,7 +20,7 @@ all : server_fuzzer # Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use. server_fuzzer : server_fuzzer.cc ../../httplib.h # $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread - $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread + $(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl zip -q -r server_fuzzer_seed_corpus.zip corpus clean: diff --git a/test/test.cc b/test/test.cc index 204c2d2805..d50f292f3a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3388,31 +3388,20 @@ void performance_test(const char *host) { Client cli(host, port); - const int NUM_REQUESTS = 50; - const int MAX_AVERAGE_MS = 5; + auto start = std::chrono::high_resolution_clock::now(); - auto warmup = cli.Get("/benchmark"); - ASSERT_TRUE(warmup); + auto res = cli.Get("/benchmark"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); - auto start = std::chrono::high_resolution_clock::now(); - for (int i = 0; i < NUM_REQUESTS; ++i) { - auto res = cli.Get("/benchmark"); - ASSERT_TRUE(res) << "Request " << i << " failed"; - EXPECT_EQ(StatusCode::OK_200, res->status); - } auto end = std::chrono::high_resolution_clock::now(); - auto total_ms = + auto elapsed = std::chrono::duration_cast(end - start) .count(); - double avg_ms = static_cast(total_ms) / NUM_REQUESTS; - - std::cout << "Peformance test at \"" << host << "\": " << NUM_REQUESTS - << " requests in " << total_ms << "ms (avg: " << avg_ms << "ms)" - << std::endl; - EXPECT_LE(avg_ms, MAX_AVERAGE_MS) - << "Performance is too slow: " << avg_ms << "ms (Issue #1777)"; + EXPECT_LE(elapsed, 5) << "Performance is too slow: " << elapsed + << "ms (Issue #1777)"; } TEST(BenchmarkTest, localhost) { performance_test("localhost"); } From 7c303bb871289ed8cfb471548b07af3418137929 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 29 Jun 2025 00:17:19 -0400 Subject: [PATCH 0452/1222] Launch proxy server before proxy test --- test/Makefile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index 8b369917d2..f04a138a73 100644 --- a/test/Makefile +++ b/test/Makefile @@ -48,7 +48,15 @@ all : test test_split ./test proxy : test_proxy - ./test_proxy + @echo "Starting proxy server..." + cd proxy && \ + docker-compose up -d + @echo "Running proxy tests..." + ./test_proxy; \ + exit_code=$$?; \ + echo "Stopping proxy server..."; \ + docker-compose down; \ + exit $$exit_code test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS) From 292f9a6c556392f3e01fd947c6b7a8b3fcb700da Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 30 Jun 2025 21:14:58 -0400 Subject: [PATCH 0453/1222] Add CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO --- httplib.h | 20 +++++++++++++++----- test/Makefile | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index ad03015339..e91eb5692f 100644 --- a/httplib.h +++ b/httplib.h @@ -278,13 +278,15 @@ using socket_t = int; #include #include +#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) #if defined(__APPLE__) #include -#if TARGET_OS_OSX || TARGET_OS_IPHONE +#if TARGET_OS_OSX #include #include #endif #endif +#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef _WIN32 @@ -302,14 +304,16 @@ using socket_t = int; #endif #endif // _WIN32 +#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) #if defined(__APPLE__) -#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #include #if TARGET_OS_OSX +#include +#include #include -#endif // TARGET_OS_OSX -#endif // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -#endif // ___APPLE__ +#endif +#endif +#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO #include #include @@ -3383,6 +3387,7 @@ unescape_abstract_namespace_unix_domain(const std::string &s) { inline int getaddrinfo_with_timeout(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res, time_t timeout_sec) { +#ifdef CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO if (timeout_sec <= 0) { // No timeout specified, use standard getaddrinfo return getaddrinfo(node, service, hints, res); @@ -3690,6 +3695,10 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, return EAI_AGAIN; // Return timeout error } #endif +#else + (void)(timeout_sec); // Unused parameter for non-blocking getaddrinfo + return getaddrinfo(node, service, hints, res); +#endif } template @@ -3868,6 +3877,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { if (getaddrinfo_with_timeout(host.c_str(), "0", &hints, &result, 0)) { return false; } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); auto ret = false; diff --git a/test/Makefile b/test/Makefile index f04a138a73..d9e7f31e49 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ CXX = clang++ -CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address +CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) -DCPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address PREFIX ?= $(shell brew --prefix) From 0b875e07471fa43f4612fd3355e218099ccb4a79 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 30 Jun 2025 21:32:29 -0400 Subject: [PATCH 0454/1222] Remove unnecessary parameters --- httplib.h | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/httplib.h b/httplib.h index e91eb5692f..5e2bbd0309 100644 --- a/httplib.h +++ b/httplib.h @@ -1312,7 +1312,7 @@ class ClientImpl { Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Post(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); @@ -1329,7 +1329,7 @@ class ClientImpl { Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Put(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); @@ -1649,13 +1649,13 @@ class Client { Result Head(const std::string &path, const Headers &headers); Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); @@ -1672,7 +1672,7 @@ class Client { Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Put(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); @@ -1689,7 +1689,7 @@ class Client { Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Params ¶ms); Result Patch(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Patch(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); @@ -8992,9 +8992,9 @@ inline Result ClientImpl::Post(const std::string &path) { return Post(path, std::string(), std::string()); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - UploadProgress progress) { - return Post(path, headers, nullptr, 0, std::string(), progress); +inline Result ClientImpl::Post(const std::string &path, + const Headers &headers) { + return Post(path, headers, nullptr, 0, std::string()); } inline Result ClientImpl::Post(const std::string &path, const char *body, @@ -9122,9 +9122,8 @@ inline Result ClientImpl::Put(const std::string &path) { return Put(path, std::string(), std::string()); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - UploadProgress progress) { - return Put(path, headers, nullptr, 0, std::string(), progress); +inline Result ClientImpl::Put(const std::string &path, const Headers &headers) { + return Put(path, headers, nullptr, 0, std::string()); } inline Result ClientImpl::Put(const std::string &path, const char *body, @@ -10600,9 +10599,8 @@ inline Result Client::Head(const std::string &path, const Headers &headers) { } inline Result Client::Post(const std::string &path) { return cli_->Post(path); } -inline Result Client::Post(const std::string &path, const Headers &headers, - UploadProgress progress) { - return cli_->Post(path, headers, progress); +inline Result Client::Post(const std::string &path, const Headers &headers) { + return cli_->Post(path, headers); } inline Result Client::Post(const std::string &path, const char *body, size_t content_length, @@ -10687,9 +10685,8 @@ inline Result Client::Post(const std::string &path, const Headers &headers, } inline Result Client::Put(const std::string &path) { return cli_->Put(path); } -inline Result Client::Put(const std::string &path, const Headers &headers, - UploadProgress progress) { - return cli_->Put(path, headers, progress); +inline Result Client::Put(const std::string &path, const Headers &headers) { + return cli_->Put(path, headers); } inline Result Client::Put(const std::string &path, const char *body, size_t content_length, @@ -10775,9 +10772,8 @@ inline Result Client::Put(const std::string &path, const Headers &headers, inline Result Client::Patch(const std::string &path) { return cli_->Patch(path); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - UploadProgress progress) { - return cli_->Patch(path, headers, progress); +inline Result Client::Patch(const std::string &path, const Headers &headers) { + return cli_->Patch(path, headers); } inline Result Client::Patch(const std::string &path, const char *body, size_t content_length, From d5409ab541ecc6f4f3df7558a829c5dc522ecf7e Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 3 Jul 2025 19:58:28 -0400 Subject: [PATCH 0455/1222] Fix warnings --- test/test.cc | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/test.cc b/test/test.cc index d50f292f3a..9f4916b78f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -313,7 +313,7 @@ TEST(ParseAcceptHeaderTest, BasicAcceptParsing) { std::vector result1; EXPECT_TRUE(detail::parse_accept_header( "text/html,application/json,text/plain", result1)); - EXPECT_EQ(result1.size(), 3); + EXPECT_EQ(result1.size(), 3U); EXPECT_EQ(result1[0], "text/html"); EXPECT_EQ(result1[1], "application/json"); EXPECT_EQ(result1[2], "text/plain"); @@ -322,7 +322,7 @@ TEST(ParseAcceptHeaderTest, BasicAcceptParsing) { std::vector result2; EXPECT_TRUE(detail::parse_accept_header( "text/html;q=0.9,application/json;q=1.0,text/plain;q=0.8", result2)); - EXPECT_EQ(result2.size(), 3); + EXPECT_EQ(result2.size(), 3U); EXPECT_EQ(result2[0], "application/json"); // highest q value EXPECT_EQ(result2[1], "text/html"); EXPECT_EQ(result2[2], "text/plain"); // lowest q value @@ -333,7 +333,7 @@ TEST(ParseAcceptHeaderTest, MixedQualityValues) { std::vector result; EXPECT_TRUE(detail::parse_accept_header( "text/html,application/json;q=0.5,text/plain;q=0.8", result)); - EXPECT_EQ(result.size(), 3); + EXPECT_EQ(result.size(), 3U); EXPECT_EQ(result[0], "text/html"); // no q value means 1.0 EXPECT_EQ(result[1], "text/plain"); // q=0.8 EXPECT_EQ(result[2], "application/json"); // q=0.5 @@ -348,14 +348,14 @@ TEST(ParseAcceptHeaderTest, EdgeCases) { // Single type std::vector single_result; EXPECT_TRUE(detail::parse_accept_header("application/json", single_result)); - EXPECT_EQ(single_result.size(), 1); + EXPECT_EQ(single_result.size(), 1U); EXPECT_EQ(single_result[0], "application/json"); // Wildcard types std::vector wildcard_result; EXPECT_TRUE(detail::parse_accept_header( "text/*;q=0.5,*/*;q=0.1,application/json", wildcard_result)); - EXPECT_EQ(wildcard_result.size(), 3); + EXPECT_EQ(wildcard_result.size(), 3U); EXPECT_EQ(wildcard_result[0], "application/json"); EXPECT_EQ(wildcard_result[1], "text/*"); EXPECT_EQ(wildcard_result[2], "*/*"); @@ -368,7 +368,7 @@ TEST(ParseAcceptHeaderTest, RealWorldExamples) { detail::parse_accept_header("text/html,application/xhtml+xml,application/" "xml;q=0.9,image/webp,image/apng,*/*;q=0.8", browser_result)); - EXPECT_EQ(browser_result.size(), 6); + EXPECT_EQ(browser_result.size(), 6U); EXPECT_EQ(browser_result[0], "text/html"); // q=1.0 (default) EXPECT_EQ(browser_result[1], "application/xhtml+xml"); // q=1.0 (default) EXPECT_EQ(browser_result[2], "image/webp"); // q=1.0 (default) @@ -381,7 +381,7 @@ TEST(ParseAcceptHeaderTest, RealWorldExamples) { EXPECT_TRUE(detail::parse_accept_header( "application/json;q=0.9,application/xml;q=0.8,text/plain;q=0.1", api_result)); - EXPECT_EQ(api_result.size(), 3); + EXPECT_EQ(api_result.size(), 3U); EXPECT_EQ(api_result[0], "application/json"); EXPECT_EQ(api_result[1], "application/xml"); EXPECT_EQ(api_result[2], "text/plain"); @@ -392,7 +392,7 @@ TEST(ParseAcceptHeaderTest, SpecialCases) { std::vector decimal_result; EXPECT_TRUE(detail::parse_accept_header( "text/html;q=0.123,application/json;q=0.456", decimal_result)); - EXPECT_EQ(decimal_result.size(), 2); + EXPECT_EQ(decimal_result.size(), 2U); EXPECT_EQ(decimal_result[0], "application/json"); // Higher q value EXPECT_EQ(decimal_result[1], "text/html"); @@ -400,7 +400,7 @@ TEST(ParseAcceptHeaderTest, SpecialCases) { std::vector zero_q_result; EXPECT_TRUE(detail::parse_accept_header("text/html;q=0,application/json;q=1", zero_q_result)); - EXPECT_EQ(zero_q_result.size(), 2); + EXPECT_EQ(zero_q_result.size(), 2U); EXPECT_EQ(zero_q_result[0], "application/json"); // q=1 EXPECT_EQ(zero_q_result[1], "text/html"); // q=0 @@ -409,7 +409,7 @@ TEST(ParseAcceptHeaderTest, SpecialCases) { EXPECT_TRUE(detail::parse_accept_header( "text/html;q=0.9,application/json;q=0.8,text/plain;q=0.7", no_space_result)); - EXPECT_EQ(no_space_result.size(), 3); + EXPECT_EQ(no_space_result.size(), 3U); EXPECT_EQ(no_space_result[0], "text/html"); EXPECT_EQ(no_space_result[1], "application/json"); EXPECT_EQ(no_space_result[2], "text/plain"); @@ -448,15 +448,15 @@ TEST(ParseAcceptHeaderTest, InvalidCases) { // Valid cases should still work EXPECT_TRUE(detail::parse_accept_header("*/*", result)); - EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result.size(), 1U); EXPECT_EQ(result[0], "*/*"); EXPECT_TRUE(detail::parse_accept_header("*", result)); - EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result.size(), 1U); EXPECT_EQ(result[0], "*"); EXPECT_TRUE(detail::parse_accept_header("text/*", result)); - EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result.size(), 1U); EXPECT_EQ(result[0], "text/*"); } @@ -464,7 +464,7 @@ TEST(ParseAcceptHeaderTest, ContentTypesPopulatedAndInvalidHeaderHandling) { Server svr; svr.Get("/accept_ok", [&](const Request &req, Response &res) { - EXPECT_EQ(req.accept_content_types.size(), 3); + EXPECT_EQ(req.accept_content_types.size(), 3U); EXPECT_EQ(req.accept_content_types[0], "application/json"); EXPECT_EQ(req.accept_content_types[1], "text/html"); EXPECT_EQ(req.accept_content_types[2], "*/*"); @@ -8780,16 +8780,16 @@ TEST(MultipartFormDataTest, AccessPartHeaders) { auto it = req.files.begin(); ASSERT_EQ("text1", it->second.name); ASSERT_EQ("text1", it->second.content); - ASSERT_EQ(1, it->second.headers.count("Content-Length")); + ASSERT_EQ(1U, it->second.headers.count("Content-Length")); auto content_length = it->second.headers.find("CONTENT-length"); ASSERT_EQ("5", content_length->second); - ASSERT_EQ(3, it->second.headers.size()); + ASSERT_EQ(3U, it->second.headers.size()); ++it; ASSERT_EQ("text2", it->second.name); ASSERT_EQ("text2", it->second.content); auto &headers = it->second.headers; - ASSERT_EQ(3, headers.size()); + ASSERT_EQ(3U, headers.size()); auto custom_header = headers.find("x-whatever"); ASSERT_TRUE(custom_header != headers.end()); ASSERT_NE("customvalue", custom_header->second); From fd034832379e4805abe7780cdf378246bd745179 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 4 Jul 2025 18:15:39 -0400 Subject: [PATCH 0456/1222] Fix warnings --- httplib.h | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/httplib.h b/httplib.h index 5e2bbd0309..76bbebf52e 100644 --- a/httplib.h +++ b/httplib.h @@ -249,6 +249,10 @@ using socket_t = int; #endif #endif //_WIN32 +#if defined(__APPLE__) +#include +#endif + #include #include #include @@ -278,15 +282,12 @@ using socket_t = int; #include #include -#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) -#if defined(__APPLE__) -#include +#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) || defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #if TARGET_OS_OSX #include #include #endif -#endif -#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO +#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO or CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef _WIN32 @@ -304,15 +305,10 @@ using socket_t = int; #endif #endif // _WIN32 -#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) -#if defined(__APPLE__) -#include +#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #if TARGET_OS_OSX -#include -#include #include #endif -#endif #endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO #include @@ -3444,7 +3440,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, } return ret; -#elif defined(__APPLE__) +#elif defined(TARGET_OS_OSX) // macOS implementation using CFHost API for asynchronous DNS resolution CFStringRef hostname_ref = CFStringCreateWithCString( kCFAllocatorDefault, node, kCFStringEncodingUTF8); @@ -6026,8 +6022,7 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { return result; } -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#if TARGET_OS_OSX +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(TARGET_OS_OSX) template using CFObjectPtr = std::unique_ptr::type, void (*)(CFTypeRef)>; @@ -6115,7 +6110,6 @@ inline bool load_system_certs_on_macos(X509_STORE *store) { return result; } -#endif // TARGET_OS_OSX #endif // _WIN32 #endif // CPPHTTPLIB_OPENSSL_SUPPORT @@ -10223,10 +10217,8 @@ inline bool SSLClient::load_certs() { #ifdef _WIN32 loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#if TARGET_OS_OSX +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(TARGET_OS_OSX) loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); -#endif // TARGET_OS_OSX #endif // _WIN32 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } } From 9e36247665dff50b9fd174f6ccbe5cc51d717257 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 4 Jul 2025 20:31:31 -0400 Subject: [PATCH 0457/1222] clang-format --- httplib.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 76bbebf52e..33eb57318f 100644 --- a/httplib.h +++ b/httplib.h @@ -282,12 +282,14 @@ using socket_t = int; #include #include -#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) || defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) +#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) || \ + defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #if TARGET_OS_OSX #include #include #endif -#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO or CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN +#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO or + // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN #ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef _WIN32 @@ -6022,7 +6024,8 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { return result; } -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(TARGET_OS_OSX) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ + defined(TARGET_OS_OSX) template using CFObjectPtr = std::unique_ptr::type, void (*)(CFTypeRef)>; @@ -10217,7 +10220,8 @@ inline bool SSLClient::load_certs() { #ifdef _WIN32 loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(TARGET_OS_OSX) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ + defined(TARGET_OS_OSX) loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); #endif // _WIN32 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } From 083fe43ad34d9dc0b7532170f0cdb64ababe1801 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 4 Jul 2025 20:32:48 -0400 Subject: [PATCH 0458/1222] Remove httpwatch.com dependency --- test/test.cc | 132 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 50 deletions(-) diff --git a/test/test.cc b/test/test.cc index 9f4916b78f..7f10afbaee 100644 --- a/test/test.cc +++ b/test/test.cc @@ -931,29 +931,6 @@ TEST(BufferStreamTest, read) { EXPECT_EQ(0, strm.read(buf, 1)); } -TEST(ChunkedEncodingTest, FromHTTPWatch_Online) { - auto host = "www.httpwatch.com"; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - auto port = 443; - SSLClient cli(host, port); -#else - auto port = 80; - Client cli(host, port); -#endif - cli.set_connection_timeout(2); - - auto res = - cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); - ASSERT_TRUE(res); - - std::string out; - read_file("./image.jpg", out); - - EXPECT_EQ(StatusCode::OK_200, res->status); - EXPECT_EQ(out, res->body); -} - TEST(HostnameToIPConversionTest, HTTPWatch_Online) { auto host = "www.httpwatch.com"; @@ -979,49 +956,104 @@ TEST(HostnameToIPConversionTest, YouTube_Online) { } #endif -TEST(ChunkedEncodingTest, WithContentReceiver_Online) { - auto host = "www.httpwatch.com"; - +class ChunkedEncodingTest : public ::testing::Test { +protected: + ChunkedEncodingTest() + : cli_(HOST, PORT) #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - auto port = 443; - SSLClient cli(host, port); -#else - auto port = 80; - Client cli(host, port); + , + svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif - cli.set_connection_timeout(2); + { + cli_.set_connection_timeout(2); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_.enable_server_certificate_verification(false); +#endif + } - std::string body; - auto res = - cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", - [&](const char *data, size_t data_length) { - body.append(data, data_length); + virtual void SetUp() { + read_file("./image.jpg", image_data_); + + svr_.Get("/hi", [&](const Request & /*req*/, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + + svr_.Get( + "/chunked", [this](const httplib::Request &, httplib::Response &res) { + res.set_chunked_content_provider( + "image/jpeg", [this](size_t offset, httplib::DataSink &sink) { + size_t remaining = image_data_.size() - offset; + if (remaining == 0) { + sink.done(); + } else { + constexpr size_t CHUNK_SIZE = 1024; + size_t send_size = std::min(CHUNK_SIZE, remaining); + sink.write(&image_data_[offset], send_size); + + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } return true; }); + }); + + t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); }); + + svr_.wait_until_ready(); + } + + virtual void TearDown() { + svr_.stop(); + if (!request_threads_.empty()) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + for (auto &t : request_threads_) { + t.join(); + } + } + t_.join(); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli_; + SSLServer svr_; +#else + Client cli_; + Server svr_; +#endif + thread t_; + std::vector request_threads_; + std::string image_data_; +}; + +TEST_F(ChunkedEncodingTest, NormalGet) { + auto res = cli_.Get("/chunked"); ASSERT_TRUE(res); std::string out; read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); - EXPECT_EQ(out, body); + EXPECT_EQ(out, res->body); } -TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { - auto host = "www.httpwatch.com"; +TEST_F(ChunkedEncodingTest, WithContentReceiver) { + std::string body; + auto res = cli_.Get("/chunked", [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + ASSERT_TRUE(res); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - auto port = 443; - SSLClient cli(host, port); -#else - auto port = 80; - Client cli(host, port); -#endif - cli.set_connection_timeout(2); + std::string out; + read_file("./image.jpg", out); + + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(out, body); +} +TEST_F(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) { std::string body; - auto res = cli.Get( - "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", + auto res = cli_.Get( + "/chunked", [&](const Response &response) { EXPECT_EQ(StatusCode::OK_200, response.status); return true; From cfb56c0b787b91ec73fa8a1f37813bd55db2ff34 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 4 Jul 2025 21:09:05 -0400 Subject: [PATCH 0459/1222] Fix #1818 --- httplib.h | 108 +++++++++++++++++++++++++++++ test/test.cc | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 297 insertions(+) diff --git a/httplib.h b/httplib.h index 33eb57318f..bf02a802e1 100644 --- a/httplib.h +++ b/httplib.h @@ -1319,6 +1319,7 @@ class ClientImpl { Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Put(const std::string &path); Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); @@ -1336,6 +1337,7 @@ class ClientImpl { Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); @@ -1353,6 +1355,7 @@ class ClientImpl { Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Delete(const std::string &path, DownloadProgress progress = nullptr); Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); @@ -1662,6 +1665,7 @@ class Client { Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Put(const std::string &path); Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); @@ -1679,6 +1683,7 @@ class Client { Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Patch(const std::string &path); Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); @@ -1696,6 +1701,7 @@ class Client { Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Delete(const std::string &path, DownloadProgress progress = nullptr); Result Delete(const std::string &path, const char *body, size_t content_length, const std::string &content_type, DownloadProgress progress = nullptr); @@ -9115,6 +9121,32 @@ ClientImpl::Post(const std::string &path, const Headers &headers, content_type, progress); } +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "POST"; + req.path = path; + req.headers = headers; + req.body = body; + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + + return send_(std::move(req)); +} + inline Result ClientImpl::Put(const std::string &path) { return Put(path, std::string(), std::string()); } @@ -9242,6 +9274,32 @@ ClientImpl::Put(const std::string &path, const Headers &headers, content_type, progress); } +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "PUT"; + req.path = path; + req.headers = headers; + req.body = body; + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + + return send_(std::move(req)); +} + inline Result ClientImpl::Patch(const std::string &path) { return Patch(path, std::string(), std::string()); } @@ -9374,6 +9432,32 @@ ClientImpl::Patch(const std::string &path, const Headers &headers, content_type, progress); } +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + Request req; + req.method = "PATCH"; + req.path = path; + req.headers = headers; + req.body = body; + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; + req.download_progress = std::move(progress); + + if (max_timeout_msec_ > 0) { + req.start_time_ = std::chrono::steady_clock::now(); + } + + if (!content_type.empty()) { req.set_header("Content-Type", content_type); } + + return send_(std::move(req)); +} + inline Result ClientImpl::Delete(const std::string &path, DownloadProgress progress) { return Delete(path, Headers(), std::string(), std::string(), progress); @@ -10679,6 +10763,14 @@ inline Result Client::Post(const std::string &path, const Headers &headers, UploadProgress progress) { return cli_->Post(path, headers, items, provider_items, progress); } +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, body, content_type, content_receiver, + progress); +} inline Result Client::Put(const std::string &path) { return cli_->Put(path); } inline Result Client::Put(const std::string &path, const Headers &headers) { @@ -10764,6 +10856,14 @@ inline Result Client::Put(const std::string &path, const Headers &headers, UploadProgress progress) { return cli_->Put(path, headers, items, provider_items, progress); } +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Put(path, headers, body, content_type, content_receiver, + progress); +} inline Result Client::Patch(const std::string &path) { return cli_->Patch(path); @@ -10853,6 +10953,14 @@ Client::Patch(const std::string &path, const Headers &headers, UploadProgress progress) { return cli_->Patch(path, headers, items, provider_items, progress); } +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Patch(path, headers, body, content_type, content_receiver, + progress); +} inline Result Client::Delete(const std::string &path, DownloadProgress progress) { diff --git a/test/test.cc b/test/test.cc index 7f10afbaee..3ebc859b71 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5302,6 +5302,195 @@ TEST_F(ServerTest, PatchContentReceiver) { ASSERT_EQ("content", res->body); } +template +void TestWithHeadersAndContentReceiver( + ClientType& cli, + std::function request_func) { + Headers headers; + headers.emplace("X-Custom-Header", "test-value"); + + std::string received_body; + auto res = request_func( + cli, "/content_receiver", headers, "content", "application/json", + [&](const char *data, size_t data_length) { + received_body.append(data, data_length); + return true; + }, nullptr); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("content", received_body); +} + +TEST_F(ServerTest, PostWithHeadersAndContentReceiver) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiver(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { + return cli.Post(path, headers, body, content_type, receiver, progress); + }); +} + +TEST_F(ServerTest, PutWithHeadersAndContentReceiver) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiver(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { + return cli.Put(path, headers, body, content_type, receiver, progress); + }); +} + +TEST_F(ServerTest, PatchWithHeadersAndContentReceiver) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiver(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { + return cli.Patch(path, headers, body, content_type, receiver, progress); + }); +} + +template +void TestWithHeadersAndContentReceiverWithProgress( + ClientType& cli, + std::function request_func) { + Headers headers; + headers.emplace("X-Test-Header", "progress-test"); + + std::string received_body; + auto progress_called = false; + + auto res = request_func( + cli, "/content_receiver", headers, "content", "text/plain", + [&](const char *data, size_t data_length) { + received_body.append(data, data_length); + return true; + }, + [&](uint64_t /*current*/, uint64_t /*total*/) { + progress_called = true; + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("content", received_body); + EXPECT_TRUE(progress_called); +} + +TEST_F(ServerTest, PostWithHeadersAndContentReceiverWithProgress) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiverWithProgress(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { + return cli.Post(path, headers, body, content_type, receiver, progress); + }); +} + +TEST_F(ServerTest, PutWithHeadersAndContentReceiverWithProgress) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiverWithProgress(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { + return cli.Put(path, headers, body, content_type, receiver, progress); + }); +} + +TEST_F(ServerTest, PatchWithHeadersAndContentReceiverWithProgress) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiverWithProgress(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { + return cli.Patch(path, headers, body, content_type, receiver, progress); + }); +} + +template +void TestWithHeadersAndContentReceiverError( + ClientType& cli, + std::function request_func) { + Headers headers; + headers.emplace("X-Error-Test", "true"); + + std::string received_body; + auto receiver_failed = false; + + auto res = request_func( + cli, "/content_receiver", headers, "content", "text/plain", + [&](const char *data, size_t data_length) { + received_body.append(data, data_length); + receiver_failed = true; + return false; + }); + + ASSERT_FALSE(res); + EXPECT_TRUE(receiver_failed); +} + +TEST_F(ServerTest, PostWithHeadersAndContentReceiverError) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiverError(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver) { + return cli.Post(path, headers, body, content_type, receiver); + }); +} + +TEST_F(ServerTest, PuttWithHeadersAndContentReceiverError) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiverError(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver) { + return cli.Put(path, headers, body, content_type, receiver); + }); +} + +TEST_F(ServerTest, PatchWithHeadersAndContentReceiverError) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + using ClientT = SSLClient; +#else + using ClientT = Client; +#endif + TestWithHeadersAndContentReceiverError(cli_, + [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, + const std::string& content_type, ContentReceiver receiver) { + return cli.Patch(path, headers, body, content_type, receiver); + }); +} + TEST_F(ServerTest, PostQueryStringAndBody) { auto res = cli_.Post("/query-string-and-body?key=value", "content", "text/plain"); From 9a0571513e0f7b81ee609d87ba16a9f893b208fc Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 4 Jul 2025 22:30:33 -0400 Subject: [PATCH 0460/1222] Fix Makefile --- test/Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/Makefile b/test/Makefile index d9e7f31e49..56cfbc1083 100644 --- a/test/Makefile +++ b/test/Makefile @@ -9,7 +9,7 @@ OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPEN ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Darwin) - OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework CoreFoundation -framework Security -framework CFNetwork + OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security endif endif @@ -24,6 +24,9 @@ ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -I$(ZSTD_DIR)/include -L$(ZSTD_DIR)/lib LIBS = -lpthread -lcurl ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Darwin) + LIBS += -framework CoreFoundation -framework CFNetwork + endif ifneq ($(UNAME_S), Darwin) LIBS += -lanl endif From 0c08c378d7d93b5dfd19074057b5fbe09c508d96 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Jul 2025 00:11:59 -0400 Subject: [PATCH 0461/1222] Simplify benchmark --- benchmark/Makefile | 34 +- benchmark/cpp-httplib-v18/httplib.h | 10154 ------------------------- benchmark/cpp-httplib-v18/main.cpp | 12 - benchmark/cpp-httplib-v19/httplib.h | 10475 -------------------------- benchmark/cpp-httplib-v19/main.cpp | 12 - benchmark/download.sh | 2 - 6 files changed, 2 insertions(+), 20687 deletions(-) delete mode 100644 benchmark/cpp-httplib-v18/httplib.h delete mode 100644 benchmark/cpp-httplib-v18/main.cpp delete mode 100644 benchmark/cpp-httplib-v19/httplib.h delete mode 100644 benchmark/cpp-httplib-v19/main.cpp delete mode 100755 benchmark/download.sh diff --git a/benchmark/Makefile b/benchmark/Makefile index fa4f76c90b..6dc93f05dd 100644 --- a/benchmark/Makefile +++ b/benchmark/Makefile @@ -20,36 +20,6 @@ run : server server : cpp-httplib/main.cpp ../httplib.h @g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp -# cpp-httplib v0.19.0 -bench-v19: server-v19 - @echo "---------------------\n cpp-httplib v0.19.0\n---------------------\n" - @./server-v19 & export PID=$$!; $(BENCH); kill $${PID} - @echo "" - -monitor-v19: server-v19 - @./server-v19 & export PID=$$!; $(MONITOR); kill $${PID} - -run-v19 : server-v19 - @./server-v19 - -server-v19 : cpp-httplib-v19/main.cpp cpp-httplib-v19/httplib.h - @g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib-v19/main.cpp - -# cpp-httplib v0.18.0 -bench-v18: server-v18 - @echo "---------------------\n cpp-httplib v0.18.0\n---------------------\n" - @./server-v18 & export PID=$$!; $(BENCH); kill $${PID} - @echo "" - -monitor-v18: server-v18 - @./server-v18 & export PID=$$!; $(MONITOR); kill $${PID} - -run-v18 : server-v18 - @./server-v18 - -server-v18 : cpp-httplib-v18/main.cpp cpp-httplib-v18/httplib.h - @g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib-v18/main.cpp - # crow bench-crow: server-crow @echo "-------------\n Crow v1.2.0\n-------------\n" @@ -66,9 +36,9 @@ server-crow : crow/main.cpp @g++ -o $@ $(CXXFLAGS) crow/main.cpp # misc -build: server server-v18 server-v19 server-crow +build: server server-crow -bench-all: bench-crow bench bench-v19 bench-v18 +bench-all: bench-crow bench issue: bombardier -c 10 -d 30s localhost:8080 diff --git a/benchmark/cpp-httplib-v18/httplib.h b/benchmark/cpp-httplib-v18/httplib.h deleted file mode 100644 index 3e28fcff91..0000000000 --- a/benchmark/cpp-httplib-v18/httplib.h +++ /dev/null @@ -1,10154 +0,0 @@ -// -// httplib.h -// -// Copyright (c) 2025 Yuji Hirose. All rights reserved. -// MIT License -// - -#ifndef CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_HTTPLIB_H - -#define CPPHTTPLIB_VERSION "0.18.0" - -/* - * Configuration - */ - -#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND -#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT -#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 -#endif - -#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND -#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 -#endif - -#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND -#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND -#define CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND -#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND -#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND 300 -#endif - -#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND -#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND -#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND -#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND -#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 -#endif - -#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND -#ifdef _WIN32 -#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 -#else -#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 -#endif -#endif - -#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH -#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 -#endif - -#ifndef CPPHTTPLIB_HEADER_MAX_LENGTH -#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 -#endif - -#ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT -#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 -#endif - -#ifndef CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT -#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT 1024 -#endif - -#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH -#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) -#endif - -#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH -#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 -#endif - -#ifndef CPPHTTPLIB_RANGE_MAX_COUNT -#define CPPHTTPLIB_RANGE_MAX_COUNT 1024 -#endif - -#ifndef CPPHTTPLIB_TCP_NODELAY -#define CPPHTTPLIB_TCP_NODELAY false -#endif - -#ifndef CPPHTTPLIB_IPV6_V6ONLY -#define CPPHTTPLIB_IPV6_V6ONLY false -#endif - -#ifndef CPPHTTPLIB_RECV_BUFSIZ -#define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) -#endif - -#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ -#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) -#endif - -#ifndef CPPHTTPLIB_THREAD_POOL_COUNT -#define CPPHTTPLIB_THREAD_POOL_COUNT \ - ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ - ? std::thread::hardware_concurrency() - 1 \ - : 0)) -#endif - -#ifndef CPPHTTPLIB_RECV_FLAGS -#define CPPHTTPLIB_RECV_FLAGS 0 -#endif - -#ifndef CPPHTTPLIB_SEND_FLAGS -#define CPPHTTPLIB_SEND_FLAGS 0 -#endif - -#ifndef CPPHTTPLIB_LISTEN_BACKLOG -#define CPPHTTPLIB_LISTEN_BACKLOG 5 -#endif - -/* - * Headers - */ - -#ifdef _WIN32 -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif //_CRT_SECURE_NO_WARNINGS - -#ifndef _CRT_NONSTDC_NO_DEPRECATE -#define _CRT_NONSTDC_NO_DEPRECATE -#endif //_CRT_NONSTDC_NO_DEPRECATE - -#if defined(_MSC_VER) -#if _MSC_VER < 1900 -#error Sorry, Visual Studio versions prior to 2015 are not supported -#endif - -#pragma comment(lib, "ws2_32.lib") - -#ifdef _WIN64 -using ssize_t = __int64; -#else -using ssize_t = long; -#endif -#endif // _MSC_VER - -#ifndef S_ISREG -#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) -#endif // S_ISREG - -#ifndef S_ISDIR -#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) -#endif // S_ISDIR - -#ifndef NOMINMAX -#define NOMINMAX -#endif // NOMINMAX - -#include -#include -#include - -#ifndef WSA_FLAG_NO_HANDLE_INHERIT -#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 -#endif - -using socket_t = SOCKET; -#ifdef CPPHTTPLIB_USE_POLL -#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) -#endif - -#else // not _WIN32 - -#include -#if !defined(_AIX) && !defined(__MVS__) -#include -#endif -#ifdef __MVS__ -#include -#ifndef NI_MAXHOST -#define NI_MAXHOST 1025 -#endif -#endif -#include -#include -#include -#ifdef __linux__ -#include -#endif -#include -#ifdef CPPHTTPLIB_USE_POLL -#include -#endif -#include -#include -#include -#include -#include -#include -#include - -using socket_t = int; -#ifndef INVALID_SOCKET -#define INVALID_SOCKET (-1) -#endif -#endif //_WIN32 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN32 -#include - -// these are defined in wincrypt.h and it breaks compilation if BoringSSL is -// used -#undef X509_NAME -#undef X509_CERT_PAIR -#undef X509_EXTENSIONS -#undef PKCS7_SIGNER_INFO - -#ifdef _MSC_VER -#pragma comment(lib, "crypt32.lib") -#endif -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#include -#if TARGET_OS_OSX -#include -#include -#endif // TARGET_OS_OSX -#endif // _WIN32 - -#include -#include -#include -#include - -#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) -#include -#endif - -#include -#include - -#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) -#if OPENSSL_VERSION_NUMBER < 0x1010107f -#error Please use OpenSSL or a current version of BoringSSL -#endif -#define SSL_get1_peer_certificate SSL_get_peer_certificate -#elif OPENSSL_VERSION_NUMBER < 0x30000000L -#error Sorry, OpenSSL versions prior to 3.0.0 are not supported -#endif - -#endif - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -#include -#endif - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT -#include -#include -#endif - -/* - * Declaration - */ -namespace httplib { - -namespace detail { - -/* - * Backport std::make_unique from C++14. - * - * NOTE: This code came up with the following stackoverflow post: - * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique - * - */ - -template -typename std::enable_if::value, std::unique_ptr>::type -make_unique(Args &&...args) { - return std::unique_ptr(new T(std::forward(args)...)); -} - -template -typename std::enable_if::value, std::unique_ptr>::type -make_unique(std::size_t n) { - typedef typename std::remove_extent::type RT; - return std::unique_ptr(new RT[n]); -} - -namespace case_ignore { - -inline unsigned char to_lower(int c) { - const static unsigned char table[256] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, - 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, - 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, - 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, - 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, - 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, - 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, - 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, - 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226, - 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, - 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224, - 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, - 255, - }; - return table[(unsigned char)(char)c]; -} - -inline bool equal(const std::string &a, const std::string &b) { - return a.size() == b.size() && - std::equal(a.begin(), a.end(), b.begin(), - [](char ca, char cb) { return to_lower(ca) == to_lower(cb); }); -} - -struct equal_to { - bool operator()(const std::string &a, const std::string &b) const { - return equal(a, b); - } -}; - -struct hash { - size_t operator()(const std::string &key) const { - return hash_core(key.data(), key.size(), 0); - } - - size_t hash_core(const char *s, size_t l, size_t h) const { - return (l == 0) ? h - : hash_core(s + 1, l - 1, - // Unsets the 6 high bits of h, therefore no - // overflow happens - (((std::numeric_limits::max)() >> 6) & - h * 33) ^ - static_cast(to_lower(*s))); - } -}; - -}; // namespace case_ignore - -// This is based on -// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". - -struct scope_exit { - explicit scope_exit(std::function &&f) - : exit_function(std::move(f)), execute_on_destruction{true} {} - - scope_exit(scope_exit &&rhs) noexcept - : exit_function(std::move(rhs.exit_function)), - execute_on_destruction{rhs.execute_on_destruction} { - rhs.release(); - } - - ~scope_exit() { - if (execute_on_destruction) { this->exit_function(); } - } - - void release() { this->execute_on_destruction = false; } - -private: - scope_exit(const scope_exit &) = delete; - void operator=(const scope_exit &) = delete; - scope_exit &operator=(scope_exit &&) = delete; - - std::function exit_function; - bool execute_on_destruction; -}; - -} // namespace detail - -enum StatusCode { - // Information responses - Continue_100 = 100, - SwitchingProtocol_101 = 101, - Processing_102 = 102, - EarlyHints_103 = 103, - - // Successful responses - OK_200 = 200, - Created_201 = 201, - Accepted_202 = 202, - NonAuthoritativeInformation_203 = 203, - NoContent_204 = 204, - ResetContent_205 = 205, - PartialContent_206 = 206, - MultiStatus_207 = 207, - AlreadyReported_208 = 208, - IMUsed_226 = 226, - - // Redirection messages - MultipleChoices_300 = 300, - MovedPermanently_301 = 301, - Found_302 = 302, - SeeOther_303 = 303, - NotModified_304 = 304, - UseProxy_305 = 305, - unused_306 = 306, - TemporaryRedirect_307 = 307, - PermanentRedirect_308 = 308, - - // Client error responses - BadRequest_400 = 400, - Unauthorized_401 = 401, - PaymentRequired_402 = 402, - Forbidden_403 = 403, - NotFound_404 = 404, - MethodNotAllowed_405 = 405, - NotAcceptable_406 = 406, - ProxyAuthenticationRequired_407 = 407, - RequestTimeout_408 = 408, - Conflict_409 = 409, - Gone_410 = 410, - LengthRequired_411 = 411, - PreconditionFailed_412 = 412, - PayloadTooLarge_413 = 413, - UriTooLong_414 = 414, - UnsupportedMediaType_415 = 415, - RangeNotSatisfiable_416 = 416, - ExpectationFailed_417 = 417, - ImATeapot_418 = 418, - MisdirectedRequest_421 = 421, - UnprocessableContent_422 = 422, - Locked_423 = 423, - FailedDependency_424 = 424, - TooEarly_425 = 425, - UpgradeRequired_426 = 426, - PreconditionRequired_428 = 428, - TooManyRequests_429 = 429, - RequestHeaderFieldsTooLarge_431 = 431, - UnavailableForLegalReasons_451 = 451, - - // Server error responses - InternalServerError_500 = 500, - NotImplemented_501 = 501, - BadGateway_502 = 502, - ServiceUnavailable_503 = 503, - GatewayTimeout_504 = 504, - HttpVersionNotSupported_505 = 505, - VariantAlsoNegotiates_506 = 506, - InsufficientStorage_507 = 507, - LoopDetected_508 = 508, - NotExtended_510 = 510, - NetworkAuthenticationRequired_511 = 511, -}; - -using Headers = - std::unordered_multimap; - -using Params = std::multimap; -using Match = std::smatch; - -using Progress = std::function; - -struct Response; -using ResponseHandler = std::function; - -struct MultipartFormData { - std::string name; - std::string content; - std::string filename; - std::string content_type; -}; -using MultipartFormDataItems = std::vector; -using MultipartFormDataMap = std::multimap; - -class DataSink { -public: - DataSink() : os(&sb_), sb_(*this) {} - - DataSink(const DataSink &) = delete; - DataSink &operator=(const DataSink &) = delete; - DataSink(DataSink &&) = delete; - DataSink &operator=(DataSink &&) = delete; - - std::function write; - std::function is_writable; - std::function done; - std::function done_with_trailer; - std::ostream os; - -private: - class data_sink_streambuf final : public std::streambuf { - public: - explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} - - protected: - std::streamsize xsputn(const char *s, std::streamsize n) override { - sink_.write(s, static_cast(n)); - return n; - } - - private: - DataSink &sink_; - }; - - data_sink_streambuf sb_; -}; - -using ContentProvider = - std::function; - -using ContentProviderWithoutLength = - std::function; - -using ContentProviderResourceReleaser = std::function; - -struct MultipartFormDataProvider { - std::string name; - ContentProviderWithoutLength provider; - std::string filename; - std::string content_type; -}; -using MultipartFormDataProviderItems = std::vector; - -using ContentReceiverWithProgress = - std::function; - -using ContentReceiver = - std::function; - -using MultipartContentHeader = - std::function; - -class ContentReader { -public: - using Reader = std::function; - using MultipartReader = std::function; - - ContentReader(Reader reader, MultipartReader multipart_reader) - : reader_(std::move(reader)), - multipart_reader_(std::move(multipart_reader)) {} - - bool operator()(MultipartContentHeader header, - ContentReceiver receiver) const { - return multipart_reader_(std::move(header), std::move(receiver)); - } - - bool operator()(ContentReceiver receiver) const { - return reader_(std::move(receiver)); - } - - Reader reader_; - MultipartReader multipart_reader_; -}; - -using Range = std::pair; -using Ranges = std::vector; - -struct Request { - std::string method; - std::string path; - Headers headers; - std::string body; - - std::string remote_addr; - int remote_port = -1; - std::string local_addr; - int local_port = -1; - - // for server - std::string version; - std::string target; - Params params; - MultipartFormDataMap files; - Ranges ranges; - Match matches; - std::unordered_map path_params; - - // for client - ResponseHandler response_handler; - ContentReceiverWithProgress content_receiver; - Progress progress; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - const SSL *ssl = nullptr; -#endif - - bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, const char *def = "", - size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, - size_t id = 0) const; - size_t get_header_value_count(const std::string &key) const; - void set_header(const std::string &key, const std::string &val); - - bool has_param(const std::string &key) const; - std::string get_param_value(const std::string &key, size_t id = 0) const; - size_t get_param_value_count(const std::string &key) const; - - bool is_multipart_form_data() const; - - bool has_file(const std::string &key) const; - MultipartFormData get_file_value(const std::string &key) const; - std::vector get_file_values(const std::string &key) const; - - // private members... - size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; - size_t content_length_ = 0; - ContentProvider content_provider_; - bool is_chunked_content_provider_ = false; - size_t authorization_count_ = 0; -}; - -struct Response { - std::string version; - int status = -1; - std::string reason; - Headers headers; - std::string body; - std::string location; // Redirect location - - bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, const char *def = "", - size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, - size_t id = 0) const; - size_t get_header_value_count(const std::string &key) const; - void set_header(const std::string &key, const std::string &val); - - void set_redirect(const std::string &url, int status = StatusCode::Found_302); - void set_content(const char *s, size_t n, const std::string &content_type); - void set_content(const std::string &s, const std::string &content_type); - void set_content(std::string &&s, const std::string &content_type); - - void set_content_provider( - size_t length, const std::string &content_type, ContentProvider provider, - ContentProviderResourceReleaser resource_releaser = nullptr); - - void set_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser = nullptr); - - void set_chunked_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser = nullptr); - - void set_file_content(const std::string &path, - const std::string &content_type); - void set_file_content(const std::string &path); - - Response() = default; - Response(const Response &) = default; - Response &operator=(const Response &) = default; - Response(Response &&) = default; - Response &operator=(Response &&) = default; - ~Response() { - if (content_provider_resource_releaser_) { - content_provider_resource_releaser_(content_provider_success_); - } - } - - // private members... - size_t content_length_ = 0; - ContentProvider content_provider_; - ContentProviderResourceReleaser content_provider_resource_releaser_; - bool is_chunked_content_provider_ = false; - bool content_provider_success_ = false; - std::string file_content_path_; - std::string file_content_content_type_; -}; - -class Stream { -public: - virtual ~Stream() = default; - - virtual bool is_readable() const = 0; - virtual bool is_writable() const = 0; - - virtual ssize_t read(char *ptr, size_t size) = 0; - virtual ssize_t write(const char *ptr, size_t size) = 0; - virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; - virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; - virtual socket_t socket() const = 0; - - ssize_t write(const char *ptr); - ssize_t write(const std::string &s); -}; - -class TaskQueue { -public: - TaskQueue() = default; - virtual ~TaskQueue() = default; - - virtual bool enqueue(std::function fn) = 0; - virtual void shutdown() = 0; - - virtual void on_idle() {} -}; - -class ThreadPool final : public TaskQueue { -public: - explicit ThreadPool(size_t n, size_t mqr = 0) - : shutdown_(false), max_queued_requests_(mqr) { - while (n) { - threads_.emplace_back(worker(*this)); - n--; - } - } - - ThreadPool(const ThreadPool &) = delete; - ~ThreadPool() override = default; - - bool enqueue(std::function fn) override { - { - std::unique_lock lock(mutex_); - if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { - return false; - } - jobs_.push_back(std::move(fn)); - } - - cond_.notify_one(); - return true; - } - - void shutdown() override { - // Stop all worker threads... - { - std::unique_lock lock(mutex_); - shutdown_ = true; - } - - cond_.notify_all(); - - // Join... - for (auto &t : threads_) { - t.join(); - } - } - -private: - struct worker { - explicit worker(ThreadPool &pool) : pool_(pool) {} - - void operator()() { - for (;;) { - std::function fn; - { - std::unique_lock lock(pool_.mutex_); - - pool_.cond_.wait( - lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); - - if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } - - fn = pool_.jobs_.front(); - pool_.jobs_.pop_front(); - } - - assert(true == static_cast(fn)); - fn(); - } - -#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ - !defined(LIBRESSL_VERSION_NUMBER) - OPENSSL_thread_stop(); -#endif - } - - ThreadPool &pool_; - }; - friend struct worker; - - std::vector threads_; - std::list> jobs_; - - bool shutdown_; - size_t max_queued_requests_ = 0; - - std::condition_variable cond_; - std::mutex mutex_; -}; - -using Logger = std::function; - -using SocketOptions = std::function; - -void default_socket_options(socket_t sock); - -const char *status_message(int status); - -std::string get_bearer_token_auth(const Request &req); - -namespace detail { - -class MatcherBase { -public: - virtual ~MatcherBase() = default; - - // Match request path and populate its matches and - virtual bool match(Request &request) const = 0; -}; - -/** - * Captures parameters in request path and stores them in Request::path_params - * - * Capture name is a substring of a pattern from : to /. - * The rest of the pattern is matched agains the request path directly - * Parameters are captured starting from the next character after - * the end of the last matched static pattern fragment until the next /. - * - * Example pattern: - * "/path/fragments/:capture/more/fragments/:second_capture" - * Static fragments: - * "/path/fragments/", "more/fragments/" - * - * Given the following request path: - * "/path/fragments/:1/more/fragments/:2" - * the resulting capture will be - * {{"capture", "1"}, {"second_capture", "2"}} - */ -class PathParamsMatcher final : public MatcherBase { -public: - PathParamsMatcher(const std::string &pattern); - - bool match(Request &request) const override; - -private: - // Treat segment separators as the end of path parameter capture - // Does not need to handle query parameters as they are parsed before path - // matching - static constexpr char separator = '/'; - - // Contains static path fragments to match against, excluding the '/' after - // path params - // Fragments are separated by path params - std::vector static_fragments_; - // Stores the names of the path parameters to be used as keys in the - // Request::path_params map - std::vector param_names_; -}; - -/** - * Performs std::regex_match on request path - * and stores the result in Request::matches - * - * Note that regex match is performed directly on the whole request. - * This means that wildcard patterns may match multiple path segments with /: - * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". - */ -class RegexMatcher final : public MatcherBase { -public: - RegexMatcher(const std::string &pattern) : regex_(pattern) {} - - bool match(Request &request) const override; - -private: - std::regex regex_; -}; - -ssize_t write_headers(Stream &strm, const Headers &headers); - -} // namespace detail - -class Server { -public: - using Handler = std::function; - - using ExceptionHandler = - std::function; - - enum class HandlerResponse { - Handled, - Unhandled, - }; - using HandlerWithResponse = - std::function; - - using HandlerWithContentReader = std::function; - - using Expect100ContinueHandler = - std::function; - - Server(); - - virtual ~Server(); - - virtual bool is_valid() const; - - Server &Get(const std::string &pattern, Handler handler); - Server &Post(const std::string &pattern, Handler handler); - Server &Post(const std::string &pattern, HandlerWithContentReader handler); - Server &Put(const std::string &pattern, Handler handler); - Server &Put(const std::string &pattern, HandlerWithContentReader handler); - Server &Patch(const std::string &pattern, Handler handler); - Server &Patch(const std::string &pattern, HandlerWithContentReader handler); - Server &Delete(const std::string &pattern, Handler handler); - Server &Delete(const std::string &pattern, HandlerWithContentReader handler); - Server &Options(const std::string &pattern, Handler handler); - - bool set_base_dir(const std::string &dir, - const std::string &mount_point = std::string()); - bool set_mount_point(const std::string &mount_point, const std::string &dir, - Headers headers = Headers()); - bool remove_mount_point(const std::string &mount_point); - Server &set_file_extension_and_mimetype_mapping(const std::string &ext, - const std::string &mime); - Server &set_default_file_mimetype(const std::string &mime); - Server &set_file_request_handler(Handler handler); - - template - Server &set_error_handler(ErrorHandlerFunc &&handler) { - return set_error_handler_core( - std::forward(handler), - std::is_convertible{}); - } - - Server &set_exception_handler(ExceptionHandler handler); - Server &set_pre_routing_handler(HandlerWithResponse handler); - Server &set_post_routing_handler(Handler handler); - - Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); - Server &set_logger(Logger logger); - - Server &set_address_family(int family); - Server &set_tcp_nodelay(bool on); - Server &set_ipv6_v6only(bool on); - Server &set_socket_options(SocketOptions socket_options); - - Server &set_default_headers(Headers headers); - Server & - set_header_writer(std::function const &writer); - - Server &set_keep_alive_max_count(size_t count); - Server &set_keep_alive_timeout(time_t sec); - - Server &set_read_timeout(time_t sec, time_t usec = 0); - template - Server &set_read_timeout(const std::chrono::duration &duration); - - Server &set_write_timeout(time_t sec, time_t usec = 0); - template - Server &set_write_timeout(const std::chrono::duration &duration); - - Server &set_idle_interval(time_t sec, time_t usec = 0); - template - Server &set_idle_interval(const std::chrono::duration &duration); - - Server &set_payload_max_length(size_t length); - - bool bind_to_port(const std::string &host, int port, int socket_flags = 0); - int bind_to_any_port(const std::string &host, int socket_flags = 0); - bool listen_after_bind(); - - bool listen(const std::string &host, int port, int socket_flags = 0); - - bool is_running() const; - void wait_until_ready() const; - void stop(); - void decommission(); - - std::function new_task_queue; - -protected: - bool process_request(Stream &strm, const std::string &remote_addr, - int remote_port, const std::string &local_addr, - int local_port, bool close_connection, - bool &connection_closed, - const std::function &setup_request); - - std::atomic svr_sock_{INVALID_SOCKET}; - size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; - time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND; - time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; - time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; - size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; - -private: - using Handlers = - std::vector, Handler>>; - using HandlersForContentReader = - std::vector, - HandlerWithContentReader>>; - - static std::unique_ptr - make_matcher(const std::string &pattern); - - Server &set_error_handler_core(HandlerWithResponse handler, std::true_type); - Server &set_error_handler_core(Handler handler, std::false_type); - - socket_t create_server_socket(const std::string &host, int port, - int socket_flags, - SocketOptions socket_options) const; - int bind_internal(const std::string &host, int port, int socket_flags); - bool listen_internal(); - - bool routing(Request &req, Response &res, Stream &strm); - bool handle_file_request(const Request &req, Response &res, - bool head = false); - bool dispatch_request(Request &req, Response &res, - const Handlers &handlers) const; - bool dispatch_request_for_content_reader( - Request &req, Response &res, ContentReader content_reader, - const HandlersForContentReader &handlers) const; - - bool parse_request_line(const char *s, Request &req) const; - void apply_ranges(const Request &req, Response &res, - std::string &content_type, std::string &boundary) const; - bool write_response(Stream &strm, bool close_connection, Request &req, - Response &res); - bool write_response_with_content(Stream &strm, bool close_connection, - const Request &req, Response &res); - bool write_response_core(Stream &strm, bool close_connection, - const Request &req, Response &res, - bool need_apply_ranges); - bool write_content_with_provider(Stream &strm, const Request &req, - Response &res, const std::string &boundary, - const std::string &content_type); - bool read_content(Stream &strm, Request &req, Response &res); - bool - read_content_with_content_receiver(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver); - bool read_content_core(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) const; - - virtual bool process_and_close_socket(socket_t sock); - - std::atomic is_running_{false}; - std::atomic is_decommisioned{false}; - - struct MountPointEntry { - std::string mount_point; - std::string base_dir; - Headers headers; - }; - std::vector base_dirs_; - std::map file_extension_and_mimetype_map_; - std::string default_file_mimetype_ = "application/octet-stream"; - Handler file_request_handler_; - - Handlers get_handlers_; - Handlers post_handlers_; - HandlersForContentReader post_handlers_for_content_reader_; - Handlers put_handlers_; - HandlersForContentReader put_handlers_for_content_reader_; - Handlers patch_handlers_; - HandlersForContentReader patch_handlers_for_content_reader_; - Handlers delete_handlers_; - HandlersForContentReader delete_handlers_for_content_reader_; - Handlers options_handlers_; - - HandlerWithResponse error_handler_; - ExceptionHandler exception_handler_; - HandlerWithResponse pre_routing_handler_; - Handler post_routing_handler_; - Expect100ContinueHandler expect_100_continue_handler_; - - Logger logger_; - - int address_family_ = AF_UNSPEC; - bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; - bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; - SocketOptions socket_options_ = default_socket_options; - - Headers default_headers_; - std::function header_writer_ = - detail::write_headers; -}; - -enum class Error { - Success = 0, - Unknown, - Connection, - BindIPAddress, - Read, - Write, - ExceedRedirectCount, - Canceled, - SSLConnection, - SSLLoadingCerts, - SSLServerVerification, - SSLServerHostnameVerification, - UnsupportedMultipartBoundaryChars, - Compression, - ConnectionTimeout, - ProxyConnection, - - // For internal use only - SSLPeerCouldBeClosed_, -}; - -std::string to_string(Error error); - -std::ostream &operator<<(std::ostream &os, const Error &obj); - -class Result { -public: - Result() = default; - Result(std::unique_ptr &&res, Error err, - Headers &&request_headers = Headers{}) - : res_(std::move(res)), err_(err), - request_headers_(std::move(request_headers)) {} - // Response - operator bool() const { return res_ != nullptr; } - bool operator==(std::nullptr_t) const { return res_ == nullptr; } - bool operator!=(std::nullptr_t) const { return res_ != nullptr; } - const Response &value() const { return *res_; } - Response &value() { return *res_; } - const Response &operator*() const { return *res_; } - Response &operator*() { return *res_; } - const Response *operator->() const { return res_.get(); } - Response *operator->() { return res_.get(); } - - // Error - Error error() const { return err_; } - - // Request Headers - bool has_request_header(const std::string &key) const; - std::string get_request_header_value(const std::string &key, - const char *def = "", - size_t id = 0) const; - uint64_t get_request_header_value_u64(const std::string &key, - uint64_t def = 0, size_t id = 0) const; - size_t get_request_header_value_count(const std::string &key) const; - -private: - std::unique_ptr res_; - Error err_ = Error::Unknown; - Headers request_headers_; -}; - -class ClientImpl { -public: - explicit ClientImpl(const std::string &host); - - explicit ClientImpl(const std::string &host, int port); - - explicit ClientImpl(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); - - virtual ~ClientImpl(); - - virtual bool is_valid() const; - - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, - Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress = nullptr); - - Result Head(const std::string &path); - Result Head(const std::string &path, const Headers &headers); - - Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Post(const std::string &path, const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Put(const std::string &path, const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - - Result Options(const std::string &path); - Result Options(const std::string &path, const Headers &headers); - - bool send(Request &req, Response &res, Error &error); - Result send(const Request &req); - - void stop(); - - std::string host() const; - int port() const; - - size_t is_socket_open() const; - socket_t socket() const; - - void set_hostname_addr_map(std::map addr_map); - - void set_default_headers(Headers headers); - - void - set_header_writer(std::function const &writer); - - void set_address_family(int family); - void set_tcp_nodelay(bool on); - void set_ipv6_v6only(bool on); - void set_socket_options(SocketOptions socket_options); - - void set_connection_timeout(time_t sec, time_t usec = 0); - template - void - set_connection_timeout(const std::chrono::duration &duration); - - void set_read_timeout(time_t sec, time_t usec = 0); - template - void set_read_timeout(const std::chrono::duration &duration); - - void set_write_timeout(time_t sec, time_t usec = 0); - template - void set_write_timeout(const std::chrono::duration &duration); - - void set_basic_auth(const std::string &username, const std::string &password); - void set_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); -#endif - - void set_keep_alive(bool on); - void set_follow_location(bool on); - - void set_url_encode(bool on); - - void set_compress(bool on); - - void set_decompress(bool on); - - void set_interface(const std::string &intf); - - void set_proxy(const std::string &host, int port); - void set_proxy_basic_auth(const std::string &username, - const std::string &password); - void set_proxy_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); - void set_ca_cert_store(X509_STORE *ca_cert_store); - X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void enable_server_certificate_verification(bool enabled); - void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier(std::function verifier); -#endif - - void set_logger(Logger logger); - -protected: - struct Socket { - socket_t sock = INVALID_SOCKET; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSL *ssl = nullptr; -#endif - - bool is_open() const { return sock != INVALID_SOCKET; } - }; - - virtual bool create_and_connect_socket(Socket &socket, Error &error); - - // All of: - // shutdown_ssl - // shutdown_socket - // close_socket - // should ONLY be called when socket_mutex_ is locked. - // Also, shutdown_ssl and close_socket should also NOT be called concurrently - // with a DIFFERENT thread sending requests using that socket. - virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); - void shutdown_socket(Socket &socket) const; - void close_socket(Socket &socket); - - bool process_request(Stream &strm, Request &req, Response &res, - bool close_connection, Error &error); - - bool write_content_with_provider(Stream &strm, const Request &req, - Error &error) const; - - void copy_settings(const ClientImpl &rhs); - - // Socket endpoint information - const std::string host_; - const int port_; - const std::string host_and_port_; - - // Current open socket - Socket socket_; - mutable std::mutex socket_mutex_; - std::recursive_mutex request_mutex_; - - // These are all protected under socket_mutex - size_t socket_requests_in_flight_ = 0; - std::thread::id socket_requests_are_from_thread_ = std::thread::id(); - bool socket_should_be_closed_when_request_is_done_ = false; - - // Hostname-IP map - std::map addr_map_; - - // Default headers - Headers default_headers_; - - // Header writer - std::function header_writer_ = - detail::write_headers; - - // Settings - std::string client_cert_path_; - std::string client_key_path_; - - time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; - time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; - - std::string basic_auth_username_; - std::string basic_auth_password_; - std::string bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string digest_auth_username_; - std::string digest_auth_password_; -#endif - - bool keep_alive_ = false; - bool follow_location_ = false; - - bool url_encode_ = true; - - int address_family_ = AF_UNSPEC; - bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; - bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; - SocketOptions socket_options_ = nullptr; - - bool compress_ = false; - bool decompress_ = true; - - std::string interface_; - - std::string proxy_host_; - int proxy_port_ = -1; - - std::string proxy_basic_auth_username_; - std::string proxy_basic_auth_password_; - std::string proxy_bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string proxy_digest_auth_username_; - std::string proxy_digest_auth_password_; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string ca_cert_file_path_; - std::string ca_cert_dir_path_; - - X509_STORE *ca_cert_store_ = nullptr; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool server_certificate_verification_ = true; - bool server_hostname_verification_ = true; - std::function server_certificate_verifier_; -#endif - - Logger logger_; - -private: - bool send_(Request &req, Response &res, Error &error); - Result send_(Request &&req); - - socket_t create_client_socket(Error &error) const; - bool read_response_line(Stream &strm, const Request &req, - Response &res) const; - bool write_request(Stream &strm, Request &req, bool close_connection, - Error &error); - bool redirect(Request &req, Response &res, Error &error); - bool handle_request(Stream &strm, Request &req, Response &res, - bool close_connection, Error &error); - std::unique_ptr send_with_content_provider( - Request &req, const char *body, size_t content_length, - ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error); - Result send_with_content_provider( - const std::string &method, const std::string &path, - const Headers &headers, const char *body, size_t content_length, - ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Progress progress); - ContentProviderWithoutLength get_multipart_content_provider( - const std::string &boundary, const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) const; - - std::string adjust_host_string(const std::string &host) const; - - virtual bool process_socket(const Socket &socket, - std::function callback); - virtual bool is_ssl() const; -}; - -class Client { -public: - // Universal interface - explicit Client(const std::string &scheme_host_port); - - explicit Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path); - - // HTTP only interface - explicit Client(const std::string &host, int port); - - explicit Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); - - Client(Client &&) = default; - Client &operator=(Client &&) = default; - - ~Client(); - - bool is_valid() const; - - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, - Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress = nullptr); - - Result Head(const std::string &path); - Result Head(const std::string &path, const Headers &headers); - - Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Post(const std::string &path, const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Put(const std::string &path, const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - - Result Options(const std::string &path); - Result Options(const std::string &path, const Headers &headers); - - bool send(Request &req, Response &res, Error &error); - Result send(const Request &req); - - void stop(); - - std::string host() const; - int port() const; - - size_t is_socket_open() const; - socket_t socket() const; - - void set_hostname_addr_map(std::map addr_map); - - void set_default_headers(Headers headers); - - void - set_header_writer(std::function const &writer); - - void set_address_family(int family); - void set_tcp_nodelay(bool on); - void set_socket_options(SocketOptions socket_options); - - void set_connection_timeout(time_t sec, time_t usec = 0); - template - void - set_connection_timeout(const std::chrono::duration &duration); - - void set_read_timeout(time_t sec, time_t usec = 0); - template - void set_read_timeout(const std::chrono::duration &duration); - - void set_write_timeout(time_t sec, time_t usec = 0); - template - void set_write_timeout(const std::chrono::duration &duration); - - void set_basic_auth(const std::string &username, const std::string &password); - void set_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); -#endif - - void set_keep_alive(bool on); - void set_follow_location(bool on); - - void set_url_encode(bool on); - - void set_compress(bool on); - - void set_decompress(bool on); - - void set_interface(const std::string &intf); - - void set_proxy(const std::string &host, int port); - void set_proxy_basic_auth(const std::string &username, - const std::string &password); - void set_proxy_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void enable_server_certificate_verification(bool enabled); - void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier(std::function verifier); -#endif - - void set_logger(Logger logger); - - // SSL -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); - - void set_ca_cert_store(X509_STORE *ca_cert_store); - void load_ca_cert_store(const char *ca_cert, std::size_t size); - - long get_openssl_verify_result() const; - - SSL_CTX *ssl_context() const; -#endif - -private: - std::unique_ptr cli_; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool is_ssl_ = false; -#endif -}; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLServer : public Server { -public: - SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path = nullptr, - const char *client_ca_cert_dir_path = nullptr, - const char *private_key_password = nullptr); - - SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); - - SSLServer( - const std::function &setup_ssl_ctx_callback); - - ~SSLServer() override; - - bool is_valid() const override; - - SSL_CTX *ssl_context() const; - - void update_certs(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); - -private: - bool process_and_close_socket(socket_t sock) override; - - SSL_CTX *ctx_; - std::mutex ctx_mutex_; -}; - -class SSLClient final : public ClientImpl { -public: - explicit SSLClient(const std::string &host); - - explicit SSLClient(const std::string &host, int port); - - explicit SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path, - const std::string &private_key_password = std::string()); - - explicit SSLClient(const std::string &host, int port, X509 *client_cert, - EVP_PKEY *client_key, - const std::string &private_key_password = std::string()); - - ~SSLClient() override; - - bool is_valid() const override; - - void set_ca_cert_store(X509_STORE *ca_cert_store); - void load_ca_cert_store(const char *ca_cert, std::size_t size); - - long get_openssl_verify_result() const; - - SSL_CTX *ssl_context() const; - -private: - bool create_and_connect_socket(Socket &socket, Error &error) override; - void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; - void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); - - bool process_socket(const Socket &socket, - std::function callback) override; - bool is_ssl() const override; - - bool connect_with_proxy(Socket &sock, Response &res, bool &success, - Error &error); - bool initialize_ssl(Socket &socket, Error &error); - - bool load_certs(); - - bool verify_host(X509 *server_cert) const; - bool verify_host_with_subject_alt_name(X509 *server_cert) const; - bool verify_host_with_common_name(X509 *server_cert) const; - bool check_host_name(const char *pattern, size_t pattern_len) const; - - SSL_CTX *ctx_; - std::mutex ctx_mutex_; - std::once_flag initialize_cert_; - - std::vector host_components_; - - long verify_result_ = 0; - - friend class ClientImpl; -}; -#endif - -/* - * Implementation of template methods. - */ - -namespace detail { - -template -inline void duration_to_sec_and_usec(const T &duration, U callback) { - auto sec = std::chrono::duration_cast(duration).count(); - auto usec = std::chrono::duration_cast( - duration - std::chrono::seconds(sec)) - .count(); - callback(static_cast(sec), static_cast(usec)); -} - -inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, uint64_t def, - size_t id) { - auto rng = headers.equal_range(key); - auto it = rng.first; - std::advance(it, static_cast(id)); - if (it != rng.second) { - return std::strtoull(it->second.data(), nullptr, 10); - } - return def; -} - -} // namespace detail - -inline uint64_t Request::get_header_value_u64(const std::string &key, - uint64_t def, size_t id) const { - return detail::get_header_value_u64(headers, key, def, id); -} - -inline uint64_t Response::get_header_value_u64(const std::string &key, - uint64_t def, size_t id) const { - return detail::get_header_value_u64(headers, key, def, id); -} - -inline void default_socket_options(socket_t sock) { - int opt = 1; -#ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); - setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - reinterpret_cast(&opt), sizeof(opt)); -#else -#ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); -#endif -#endif -} - -inline const char *status_message(int status) { - switch (status) { - case StatusCode::Continue_100: return "Continue"; - case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; - case StatusCode::Processing_102: return "Processing"; - case StatusCode::EarlyHints_103: return "Early Hints"; - case StatusCode::OK_200: return "OK"; - case StatusCode::Created_201: return "Created"; - case StatusCode::Accepted_202: return "Accepted"; - case StatusCode::NonAuthoritativeInformation_203: - return "Non-Authoritative Information"; - case StatusCode::NoContent_204: return "No Content"; - case StatusCode::ResetContent_205: return "Reset Content"; - case StatusCode::PartialContent_206: return "Partial Content"; - case StatusCode::MultiStatus_207: return "Multi-Status"; - case StatusCode::AlreadyReported_208: return "Already Reported"; - case StatusCode::IMUsed_226: return "IM Used"; - case StatusCode::MultipleChoices_300: return "Multiple Choices"; - case StatusCode::MovedPermanently_301: return "Moved Permanently"; - case StatusCode::Found_302: return "Found"; - case StatusCode::SeeOther_303: return "See Other"; - case StatusCode::NotModified_304: return "Not Modified"; - case StatusCode::UseProxy_305: return "Use Proxy"; - case StatusCode::unused_306: return "unused"; - case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; - case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; - case StatusCode::BadRequest_400: return "Bad Request"; - case StatusCode::Unauthorized_401: return "Unauthorized"; - case StatusCode::PaymentRequired_402: return "Payment Required"; - case StatusCode::Forbidden_403: return "Forbidden"; - case StatusCode::NotFound_404: return "Not Found"; - case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; - case StatusCode::NotAcceptable_406: return "Not Acceptable"; - case StatusCode::ProxyAuthenticationRequired_407: - return "Proxy Authentication Required"; - case StatusCode::RequestTimeout_408: return "Request Timeout"; - case StatusCode::Conflict_409: return "Conflict"; - case StatusCode::Gone_410: return "Gone"; - case StatusCode::LengthRequired_411: return "Length Required"; - case StatusCode::PreconditionFailed_412: return "Precondition Failed"; - case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; - case StatusCode::UriTooLong_414: return "URI Too Long"; - case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; - case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; - case StatusCode::ExpectationFailed_417: return "Expectation Failed"; - case StatusCode::ImATeapot_418: return "I'm a teapot"; - case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; - case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; - case StatusCode::Locked_423: return "Locked"; - case StatusCode::FailedDependency_424: return "Failed Dependency"; - case StatusCode::TooEarly_425: return "Too Early"; - case StatusCode::UpgradeRequired_426: return "Upgrade Required"; - case StatusCode::PreconditionRequired_428: return "Precondition Required"; - case StatusCode::TooManyRequests_429: return "Too Many Requests"; - case StatusCode::RequestHeaderFieldsTooLarge_431: - return "Request Header Fields Too Large"; - case StatusCode::UnavailableForLegalReasons_451: - return "Unavailable For Legal Reasons"; - case StatusCode::NotImplemented_501: return "Not Implemented"; - case StatusCode::BadGateway_502: return "Bad Gateway"; - case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; - case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; - case StatusCode::HttpVersionNotSupported_505: - return "HTTP Version Not Supported"; - case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; - case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; - case StatusCode::LoopDetected_508: return "Loop Detected"; - case StatusCode::NotExtended_510: return "Not Extended"; - case StatusCode::NetworkAuthenticationRequired_511: - return "Network Authentication Required"; - - default: - case StatusCode::InternalServerError_500: return "Internal Server Error"; - } -} - -inline std::string get_bearer_token_auth(const Request &req) { - if (req.has_header("Authorization")) { - static std::string BearerHeaderPrefix = "Bearer "; - return req.get_header_value("Authorization") - .substr(BearerHeaderPrefix.length()); - } - return ""; -} - -template -inline Server & -Server::set_read_timeout(const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); - return *this; -} - -template -inline Server & -Server::set_write_timeout(const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); - return *this; -} - -template -inline Server & -Server::set_idle_interval(const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); - return *this; -} - -inline std::string to_string(const Error error) { - switch (error) { - case Error::Success: return "Success (no error)"; - case Error::Connection: return "Could not establish connection"; - case Error::BindIPAddress: return "Failed to bind IP address"; - case Error::Read: return "Failed to read connection"; - case Error::Write: return "Failed to write connection"; - case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; - case Error::Canceled: return "Connection handling canceled"; - case Error::SSLConnection: return "SSL connection failed"; - case Error::SSLLoadingCerts: return "SSL certificate loading failed"; - case Error::SSLServerVerification: return "SSL server verification failed"; - case Error::SSLServerHostnameVerification: - return "SSL server hostname verification failed"; - case Error::UnsupportedMultipartBoundaryChars: - return "Unsupported HTTP multipart boundary characters"; - case Error::Compression: return "Compression failed"; - case Error::ConnectionTimeout: return "Connection timed out"; - case Error::ProxyConnection: return "Proxy connection failed"; - case Error::Unknown: return "Unknown"; - default: break; - } - - return "Invalid"; -} - -inline std::ostream &operator<<(std::ostream &os, const Error &obj) { - os << to_string(obj); - os << " (" << static_cast::type>(obj) << ')'; - return os; -} - -inline uint64_t Result::get_request_header_value_u64(const std::string &key, - uint64_t def, - size_t id) const { - return detail::get_header_value_u64(request_headers_, key, def, id); -} - -template -inline void ClientImpl::set_connection_timeout( - const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { - set_connection_timeout(sec, usec); - }); -} - -template -inline void ClientImpl::set_read_timeout( - const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); -} - -template -inline void ClientImpl::set_write_timeout( - const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); -} - -template -inline void Client::set_connection_timeout( - const std::chrono::duration &duration) { - cli_->set_connection_timeout(duration); -} - -template -inline void -Client::set_read_timeout(const std::chrono::duration &duration) { - cli_->set_read_timeout(duration); -} - -template -inline void -Client::set_write_timeout(const std::chrono::duration &duration) { - cli_->set_write_timeout(duration); -} - -/* - * Forward declarations and types that will be part of the .h file if split into - * .h + .cc. - */ - -std::string hosted_at(const std::string &hostname); - -void hosted_at(const std::string &hostname, std::vector &addrs); - -std::string append_query_params(const std::string &path, const Params ¶ms); - -std::pair make_range_header(const Ranges &ranges); - -std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, - bool is_proxy = false); - -namespace detail { - -struct FileStat { - FileStat(const std::string &path); - bool is_file() const; - bool is_dir() const; - -private: - struct stat st_; - int ret_ = -1; -}; - -std::string encode_query_param(const std::string &value); - -std::string decode_url(const std::string &s, bool convert_plus_to_space); - -void read_file(const std::string &path, std::string &out); - -std::string trim_copy(const std::string &s); - -void divide( - const char *data, std::size_t size, char d, - std::function - fn); - -void divide( - const std::string &str, char d, - std::function - fn); - -void split(const char *b, const char *e, char d, - std::function fn); - -void split(const char *b, const char *e, char d, size_t m, - std::function fn); - -bool process_client_socket(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, - std::function callback); - -socket_t create_client_socket(const std::string &host, const std::string &ip, - int port, int address_family, bool tcp_nodelay, - bool ipv6_v6only, SocketOptions socket_options, - time_t connection_timeout_sec, - time_t connection_timeout_usec, - time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec, - const std::string &intf, Error &error); - -const char *get_header_value(const Headers &headers, const std::string &key, - const char *def, size_t id); - -std::string params_to_query_str(const Params ¶ms); - -void parse_query_text(const char *data, std::size_t size, Params ¶ms); - -void parse_query_text(const std::string &s, Params ¶ms); - -bool parse_multipart_boundary(const std::string &content_type, - std::string &boundary); - -bool parse_range_header(const std::string &s, Ranges &ranges); - -int close_socket(socket_t sock); - -ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); - -ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); - -enum class EncodingType { None = 0, Gzip, Brotli }; - -EncodingType encoding_type(const Request &req, const Response &res); - -class BufferStream final : public Stream { -public: - BufferStream() = default; - ~BufferStream() override = default; - - bool is_readable() const override; - bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - - const std::string &get_buffer() const; - -private: - std::string buffer; - size_t position = 0; -}; - -class compressor { -public: - virtual ~compressor() = default; - - typedef std::function Callback; - virtual bool compress(const char *data, size_t data_length, bool last, - Callback callback) = 0; -}; - -class decompressor { -public: - virtual ~decompressor() = default; - - virtual bool is_valid() const = 0; - - typedef std::function Callback; - virtual bool decompress(const char *data, size_t data_length, - Callback callback) = 0; -}; - -class nocompressor final : public compressor { -public: - ~nocompressor() override = default; - - bool compress(const char *data, size_t data_length, bool /*last*/, - Callback callback) override; -}; - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -class gzip_compressor final : public compressor { -public: - gzip_compressor(); - ~gzip_compressor() override; - - bool compress(const char *data, size_t data_length, bool last, - Callback callback) override; - -private: - bool is_valid_ = false; - z_stream strm_; -}; - -class gzip_decompressor final : public decompressor { -public: - gzip_decompressor(); - ~gzip_decompressor() override; - - bool is_valid() const override; - - bool decompress(const char *data, size_t data_length, - Callback callback) override; - -private: - bool is_valid_ = false; - z_stream strm_; -}; -#endif - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT -class brotli_compressor final : public compressor { -public: - brotli_compressor(); - ~brotli_compressor(); - - bool compress(const char *data, size_t data_length, bool last, - Callback callback) override; - -private: - BrotliEncoderState *state_ = nullptr; -}; - -class brotli_decompressor final : public decompressor { -public: - brotli_decompressor(); - ~brotli_decompressor(); - - bool is_valid() const override; - - bool decompress(const char *data, size_t data_length, - Callback callback) override; - -private: - BrotliDecoderResult decoder_r; - BrotliDecoderState *decoder_s = nullptr; -}; -#endif - -// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` -// to store data. The call can set memory on stack for performance. -class stream_line_reader { -public: - stream_line_reader(Stream &strm, char *fixed_buffer, - size_t fixed_buffer_size); - const char *ptr() const; - size_t size() const; - bool end_with_crlf() const; - bool getline(); - -private: - void append(char c); - - Stream &strm_; - char *fixed_buffer_; - const size_t fixed_buffer_size_; - size_t fixed_buffer_used_size_ = 0; - std::string glowable_buffer_; -}; - -class mmap { -public: - mmap(const char *path); - ~mmap(); - - bool open(const char *path); - void close(); - - bool is_open() const; - size_t size() const; - const char *data() const; - -private: -#if defined(_WIN32) - HANDLE hFile_ = NULL; - HANDLE hMapping_ = NULL; -#else - int fd_ = -1; -#endif - size_t size_ = 0; - void *addr_ = nullptr; - bool is_open_empty_file = false; -}; - -} // namespace detail - -// ---------------------------------------------------------------------------- - -/* - * Implementation that will be part of the .cc file if split into .h + .cc. - */ - -namespace detail { - -inline bool is_hex(char c, int &v) { - if (0x20 <= c && isdigit(c)) { - v = c - '0'; - return true; - } else if ('A' <= c && c <= 'F') { - v = c - 'A' + 10; - return true; - } else if ('a' <= c && c <= 'f') { - v = c - 'a' + 10; - return true; - } - return false; -} - -inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, - int &val) { - if (i >= s.size()) { return false; } - - val = 0; - for (; cnt; i++, cnt--) { - if (!s[i]) { return false; } - auto v = 0; - if (is_hex(s[i], v)) { - val = val * 16 + v; - } else { - return false; - } - } - return true; -} - -inline std::string from_i_to_hex(size_t n) { - static const auto charset = "0123456789abcdef"; - std::string ret; - do { - ret = charset[n & 15] + ret; - n >>= 4; - } while (n > 0); - return ret; -} - -inline size_t to_utf8(int code, char *buff) { - if (code < 0x0080) { - buff[0] = static_cast(code & 0x7F); - return 1; - } else if (code < 0x0800) { - buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); - buff[1] = static_cast(0x80 | (code & 0x3F)); - return 2; - } else if (code < 0xD800) { - buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); - buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[2] = static_cast(0x80 | (code & 0x3F)); - return 3; - } else if (code < 0xE000) { // D800 - DFFF is invalid... - return 0; - } else if (code < 0x10000) { - buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); - buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[2] = static_cast(0x80 | (code & 0x3F)); - return 3; - } else if (code < 0x110000) { - buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); - buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); - buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[3] = static_cast(0x80 | (code & 0x3F)); - return 4; - } - - // NOTREACHED - return 0; -} - -// NOTE: This code came up with the following stackoverflow post: -// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c -inline std::string base64_encode(const std::string &in) { - static const auto lookup = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - std::string out; - out.reserve(in.size()); - - auto val = 0; - auto valb = -6; - - for (auto c : in) { - val = (val << 8) + static_cast(c); - valb += 8; - while (valb >= 0) { - out.push_back(lookup[(val >> valb) & 0x3F]); - valb -= 6; - } - } - - if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } - - while (out.size() % 4) { - out.push_back('='); - } - - return out; -} - -inline bool is_valid_path(const std::string &path) { - size_t level = 0; - size_t i = 0; - - // Skip slash - while (i < path.size() && path[i] == '/') { - i++; - } - - while (i < path.size()) { - // Read component - auto beg = i; - while (i < path.size() && path[i] != '/') { - if (path[i] == '\0') { - return false; - } else if (path[i] == '\\') { - return false; - } - i++; - } - - auto len = i - beg; - assert(len > 0); - - if (!path.compare(beg, len, ".")) { - ; - } else if (!path.compare(beg, len, "..")) { - if (level == 0) { return false; } - level--; - } else { - level++; - } - - // Skip slash - while (i < path.size() && path[i] == '/') { - i++; - } - } - - return true; -} - -inline FileStat::FileStat(const std::string &path) { - ret_ = stat(path.c_str(), &st_); -} -inline bool FileStat::is_file() const { - return ret_ >= 0 && S_ISREG(st_.st_mode); -} -inline bool FileStat::is_dir() const { - return ret_ >= 0 && S_ISDIR(st_.st_mode); -} - -inline std::string encode_query_param(const std::string &value) { - std::ostringstream escaped; - escaped.fill('0'); - escaped << std::hex; - - for (auto c : value) { - if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || - c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || - c == ')') { - escaped << c; - } else { - escaped << std::uppercase; - escaped << '%' << std::setw(2) - << static_cast(static_cast(c)); - escaped << std::nouppercase; - } - } - - return escaped.str(); -} - -inline std::string encode_url(const std::string &s) { - std::string result; - result.reserve(s.size()); - - for (size_t i = 0; s[i]; i++) { - switch (s[i]) { - case ' ': result += "%20"; break; - case '+': result += "%2B"; break; - case '\r': result += "%0D"; break; - case '\n': result += "%0A"; break; - case '\'': result += "%27"; break; - case ',': result += "%2C"; break; - // case ':': result += "%3A"; break; // ok? probably... - case ';': result += "%3B"; break; - default: - auto c = static_cast(s[i]); - if (c >= 0x80) { - result += '%'; - char hex[4]; - auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); - assert(len == 2); - result.append(hex, static_cast(len)); - } else { - result += s[i]; - } - break; - } - } - - return result; -} - -inline std::string decode_url(const std::string &s, - bool convert_plus_to_space) { - std::string result; - - for (size_t i = 0; i < s.size(); i++) { - if (s[i] == '%' && i + 1 < s.size()) { - if (s[i + 1] == 'u') { - auto val = 0; - if (from_hex_to_i(s, i + 2, 4, val)) { - // 4 digits Unicode codes - char buff[4]; - size_t len = to_utf8(val, buff); - if (len > 0) { result.append(buff, len); } - i += 5; // 'u0000' - } else { - result += s[i]; - } - } else { - auto val = 0; - if (from_hex_to_i(s, i + 1, 2, val)) { - // 2 digits hex codes - result += static_cast(val); - i += 2; // '00' - } else { - result += s[i]; - } - } - } else if (convert_plus_to_space && s[i] == '+') { - result += ' '; - } else { - result += s[i]; - } - } - - return result; -} - -inline void read_file(const std::string &path, std::string &out) { - std::ifstream fs(path, std::ios_base::binary); - fs.seekg(0, std::ios_base::end); - auto size = fs.tellg(); - fs.seekg(0); - out.resize(static_cast(size)); - fs.read(&out[0], static_cast(size)); -} - -inline std::string file_extension(const std::string &path) { - std::smatch m; - static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); - if (std::regex_search(path, m, re)) { return m[1].str(); } - return std::string(); -} - -inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } - -inline std::pair trim(const char *b, const char *e, size_t left, - size_t right) { - while (b + left < e && is_space_or_tab(b[left])) { - left++; - } - while (right > 0 && is_space_or_tab(b[right - 1])) { - right--; - } - return std::make_pair(left, right); -} - -inline std::string trim_copy(const std::string &s) { - auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); - return s.substr(r.first, r.second - r.first); -} - -inline std::string trim_double_quotes_copy(const std::string &s) { - if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { - return s.substr(1, s.size() - 2); - } - return s; -} - -inline void -divide(const char *data, std::size_t size, char d, - std::function - fn) { - const auto it = std::find(data, data + size, d); - const auto found = static_cast(it != data + size); - const auto lhs_data = data; - const auto lhs_size = static_cast(it - data); - const auto rhs_data = it + found; - const auto rhs_size = size - lhs_size - found; - - fn(lhs_data, lhs_size, rhs_data, rhs_size); -} - -inline void -divide(const std::string &str, char d, - std::function - fn) { - divide(str.data(), str.size(), d, std::move(fn)); -} - -inline void split(const char *b, const char *e, char d, - std::function fn) { - return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); -} - -inline void split(const char *b, const char *e, char d, size_t m, - std::function fn) { - size_t i = 0; - size_t beg = 0; - size_t count = 1; - - while (e ? (b + i < e) : (b[i] != '\0')) { - if (b[i] == d && count < m) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } - beg = i + 1; - count++; - } - i++; - } - - if (i) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } - } -} - -inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, - size_t fixed_buffer_size) - : strm_(strm), fixed_buffer_(fixed_buffer), - fixed_buffer_size_(fixed_buffer_size) {} - -inline const char *stream_line_reader::ptr() const { - if (glowable_buffer_.empty()) { - return fixed_buffer_; - } else { - return glowable_buffer_.data(); - } -} - -inline size_t stream_line_reader::size() const { - if (glowable_buffer_.empty()) { - return fixed_buffer_used_size_; - } else { - return glowable_buffer_.size(); - } -} - -inline bool stream_line_reader::end_with_crlf() const { - auto end = ptr() + size(); - return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; -} - -inline bool stream_line_reader::getline() { - fixed_buffer_used_size_ = 0; - glowable_buffer_.clear(); - -#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - char prev_byte = 0; -#endif - - for (size_t i = 0;; i++) { - char byte; - auto n = strm_.read(&byte, 1); - - if (n < 0) { - return false; - } else if (n == 0) { - if (i == 0) { - return false; - } else { - break; - } - } - - append(byte); - -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - if (byte == '\n') { break; } -#else - if (prev_byte == '\r' && byte == '\n') { break; } - prev_byte = byte; -#endif - } - - return true; -} - -inline void stream_line_reader::append(char c) { - if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { - fixed_buffer_[fixed_buffer_used_size_++] = c; - fixed_buffer_[fixed_buffer_used_size_] = '\0'; - } else { - if (glowable_buffer_.empty()) { - assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); - glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); - } - glowable_buffer_ += c; - } -} - -inline mmap::mmap(const char *path) { - open(path); -} - -inline mmap::~mmap() { close(); } - -inline bool mmap::open(const char *path) { - close(); - -#if defined(_WIN32) - std::wstring wpath; - for (size_t i = 0; i < strlen(path); i++) { - wpath += path[i]; - } - -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, - OPEN_EXISTING, NULL); -#else - hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); -#endif - - if (hFile_ == INVALID_HANDLE_VALUE) { return false; } - - LARGE_INTEGER size{}; - if (!::GetFileSizeEx(hFile_, &size)) { return false; } - // If the following line doesn't compile due to QuadPart, update Windows SDK. - // See: - // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 - if (static_cast(size.QuadPart) > - (std::numeric_limits::max)()) { - // `size_t` might be 32-bits, on 32-bits Windows. - return false; - } - size_ = static_cast(size.QuadPart); - -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - hMapping_ = - ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); -#else - hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); -#endif - - // Special treatment for an empty file... - if (hMapping_ == NULL && size_ == 0) { - close(); - is_open_empty_file = true; - return true; - } - - if (hMapping_ == NULL) { - close(); - return false; - } - -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); -#else - addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); -#endif - - if (addr_ == nullptr) { - close(); - return false; - } -#else - fd_ = ::open(path, O_RDONLY); - if (fd_ == -1) { return false; } - - struct stat sb; - if (fstat(fd_, &sb) == -1) { - close(); - return false; - } - size_ = static_cast(sb.st_size); - - addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); - - // Special treatment for an empty file... - if (addr_ == MAP_FAILED && size_ == 0) { - close(); - is_open_empty_file = true; - return false; - } -#endif - - return true; -} - -inline bool mmap::is_open() const { - return is_open_empty_file ? true : addr_ != nullptr; -} - -inline size_t mmap::size() const { return size_; } - -inline const char *mmap::data() const { - return is_open_empty_file ? "" : static_cast(addr_); -} - -inline void mmap::close() { -#if defined(_WIN32) - if (addr_) { - ::UnmapViewOfFile(addr_); - addr_ = nullptr; - } - - if (hMapping_) { - ::CloseHandle(hMapping_); - hMapping_ = NULL; - } - - if (hFile_ != INVALID_HANDLE_VALUE) { - ::CloseHandle(hFile_); - hFile_ = INVALID_HANDLE_VALUE; - } - - is_open_empty_file = false; -#else - if (addr_ != nullptr) { - munmap(addr_, size_); - addr_ = nullptr; - } - - if (fd_ != -1) { - ::close(fd_); - fd_ = -1; - } -#endif - size_ = 0; -} -inline int close_socket(socket_t sock) { -#ifdef _WIN32 - return closesocket(sock); -#else - return close(sock); -#endif -} - -template inline ssize_t handle_EINTR(T fn) { - ssize_t res = 0; - while (true) { - res = fn(); - if (res < 0 && errno == EINTR) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); - continue; - } - break; - } - return res; -} - -inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { - return handle_EINTR([&]() { - return recv(sock, -#ifdef _WIN32 - static_cast(ptr), static_cast(size), -#else - ptr, size, -#endif - flags); - }); -} - -inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, - int flags) { - return handle_EINTR([&]() { - return send(sock, -#ifdef _WIN32 - static_cast(ptr), static_cast(size), -#else - ptr, size, -#endif - flags); - }); -} - -inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN; - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return -1; } -#endif - - fd_set fds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - return handle_EINTR([&]() { - return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); - }); -#endif -} - -inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLOUT; - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return -1; } -#endif - - fd_set fds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - return handle_EINTR([&]() { - return select(static_cast(sock + 1), nullptr, &fds, nullptr, &tv); - }); -#endif -} - -inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, - time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN | POLLOUT; - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); - - if (poll_res == 0) { return Error::ConnectionTimeout; } - - if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } - - return Error::Connection; -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return Error::Connection; } -#endif - - fd_set fdsr; - FD_ZERO(&fdsr); - FD_SET(sock, &fdsr); - - auto fdsw = fdsr; - auto fdse = fdsr; - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - auto ret = handle_EINTR([&]() { - return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); - }); - - if (ret == 0) { return Error::ConnectionTimeout; } - - if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } - return Error::Connection; -#endif -} - -inline bool is_socket_alive(socket_t sock) { - const auto val = detail::select_read(sock, 0, 0); - if (val == 0) { - return true; - } else if (val < 0 && errno == EBADF) { - return false; - } - char buf[1]; - return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; -} - -class SocketStream final : public Stream { -public: - SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec); - ~SocketStream() override; - - bool is_readable() const override; - bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - -private: - socket_t sock_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; - - std::vector read_buff_; - size_t read_buff_off_ = 0; - size_t read_buff_content_size_ = 0; - - static const size_t read_buff_size_ = 1024l * 4; -}; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLSocketStream final : public Stream { -public: - SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec); - ~SSLSocketStream() override; - - bool is_readable() const override; - bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - -private: - socket_t sock_; - SSL *ssl_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; -}; -#endif - -template -inline bool -process_server_socket_core(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, T callback) { - assert(keep_alive_max_count > 0); - auto ret = false; - auto count = keep_alive_max_count; - while (svr_sock != INVALID_SOCKET && count > 0 && - select_read(sock, keep_alive_timeout_sec, 0) > 0) { - auto close_connection = count == 1; - auto connection_closed = false; - ret = callback(close_connection, connection_closed); - if (!ret || connection_closed) { break; } - count--; - } - return ret; -} - -template -inline bool -process_server_socket(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - return process_server_socket_core( - svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm, close_connection, connection_closed); - }); -} - -inline bool process_client_socket(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec, - std::function callback) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm); -} - -inline int shutdown_socket(socket_t sock) { -#ifdef _WIN32 - return shutdown(sock, SD_BOTH); -#else - return shutdown(sock, SHUT_RDWR); -#endif -} - -inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { - if (s.size() > 1 && s[0] == '\0') { - auto ret = s; - ret[0] = '@'; - return ret; - } - return s; -} - -inline std::string -unescape_abstract_namespace_unix_domain(const std::string &s) { - if (s.size() > 1 && s[0] == '@') { - auto ret = s; - ret[0] = '\0'; - return ret; - } - return s; -} - -template -socket_t create_socket(const std::string &host, const std::string &ip, int port, - int address_family, int socket_flags, bool tcp_nodelay, - bool ipv6_v6only, SocketOptions socket_options, - BindOrConnect bind_or_connect) { - // Get address info - const char *node = nullptr; - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_IP; - - if (!ip.empty()) { - node = ip.c_str(); - // Ask getaddrinfo to convert IP in c-string to address - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_NUMERICHOST; - } else { - if (!host.empty()) { node = host.c_str(); } - hints.ai_family = address_family; - hints.ai_flags = socket_flags; - } - -#ifndef _WIN32 - if (hints.ai_family == AF_UNIX) { - const auto addrlen = host.length(); - if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } - -#ifdef SOCK_CLOEXEC - auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, - hints.ai_protocol); -#else - auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); -#endif - - if (sock != INVALID_SOCKET) { - sockaddr_un addr{}; - addr.sun_family = AF_UNIX; - - auto unescaped_host = unescape_abstract_namespace_unix_domain(host); - std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); - - hints.ai_addr = reinterpret_cast(&addr); - hints.ai_addrlen = static_cast( - sizeof(addr) - sizeof(addr.sun_path) + addrlen); - -#ifndef SOCK_CLOEXEC - fcntl(sock, F_SETFD, FD_CLOEXEC); -#endif - - if (socket_options) { socket_options(sock); } - - bool dummy; - if (!bind_or_connect(sock, hints, dummy)) { - close_socket(sock); - sock = INVALID_SOCKET; - } - } - return sock; - } -#endif - - auto service = std::to_string(port); - - if (getaddrinfo(node, service.c_str(), &hints, &result)) { -#if defined __linux__ && !defined __ANDROID__ - res_init(); -#endif - return INVALID_SOCKET; - } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); - - for (auto rp = result; rp; rp = rp->ai_next) { - // Create a socket -#ifdef _WIN32 - auto sock = - WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, - WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); - /** - * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 - * and above the socket creation fails on older Windows Systems. - * - * Let's try to create a socket the old way in this case. - * - * Reference: - * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa - * - * WSA_FLAG_NO_HANDLE_INHERIT: - * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with - * SP1, and later - * - */ - if (sock == INVALID_SOCKET) { - sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - } -#else - -#ifdef SOCK_CLOEXEC - auto sock = - socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); -#else - auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); -#endif - -#endif - if (sock == INVALID_SOCKET) { continue; } - -#if !defined _WIN32 && !defined SOCK_CLOEXEC - if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { - close_socket(sock); - continue; - } -#endif - - if (tcp_nodelay) { - auto opt = 1; -#ifdef _WIN32 - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&opt), sizeof(opt)); -#endif - } - - if (rp->ai_family == AF_INET6) { - auto opt = ipv6_v6only ? 1 : 0; -#ifdef _WIN32 - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&opt), sizeof(opt)); -#endif - } - - if (socket_options) { socket_options(sock); } - - // bind or connect - auto quit = false; - if (bind_or_connect(sock, *rp, quit)) { return sock; } - - close_socket(sock); - - if (quit) { break; } - } - - return INVALID_SOCKET; -} - -inline void set_nonblocking(socket_t sock, bool nonblocking) { -#ifdef _WIN32 - auto flags = nonblocking ? 1UL : 0UL; - ioctlsocket(sock, FIONBIO, &flags); -#else - auto flags = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, - nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); -#endif -} - -inline bool is_connection_error() { -#ifdef _WIN32 - return WSAGetLastError() != WSAEWOULDBLOCK; -#else - return errno != EINPROGRESS; -#endif -} - -inline bool bind_ip_address(socket_t sock, const std::string &host) { - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; - - if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); - - auto ret = false; - for (auto rp = result; rp; rp = rp->ai_next) { - const auto &ai = *rp; - if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { - ret = true; - break; - } - } - - return ret; -} - -#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ -#define USE_IF2IP -#endif - -#ifdef USE_IF2IP -inline std::string if2ip(int address_family, const std::string &ifn) { - struct ifaddrs *ifap; - getifaddrs(&ifap); - auto se = detail::scope_exit([&] { freeifaddrs(ifap); }); - - std::string addr_candidate; - for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr && ifn == ifa->ifa_name && - (AF_UNSPEC == address_family || - ifa->ifa_addr->sa_family == address_family)) { - if (ifa->ifa_addr->sa_family == AF_INET) { - auto sa = reinterpret_cast(ifa->ifa_addr); - char buf[INET_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { - return std::string(buf, INET_ADDRSTRLEN); - } - } else if (ifa->ifa_addr->sa_family == AF_INET6) { - auto sa = reinterpret_cast(ifa->ifa_addr); - if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { - char buf[INET6_ADDRSTRLEN] = {}; - if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { - // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL - auto s6_addr_head = sa->sin6_addr.s6_addr[0]; - if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { - addr_candidate = std::string(buf, INET6_ADDRSTRLEN); - } else { - return std::string(buf, INET6_ADDRSTRLEN); - } - } - } - } - } - } - return addr_candidate; -} -#endif - -inline socket_t create_client_socket( - const std::string &host, const std::string &ip, int port, - int address_family, bool tcp_nodelay, bool ipv6_v6only, - SocketOptions socket_options, time_t connection_timeout_sec, - time_t connection_timeout_usec, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, const std::string &intf, Error &error) { - auto sock = create_socket( - host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only, - std::move(socket_options), - [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { - if (!intf.empty()) { -#ifdef USE_IF2IP - auto ip_from_if = if2ip(address_family, intf); - if (ip_from_if.empty()) { ip_from_if = intf; } - if (!bind_ip_address(sock2, ip_from_if)) { - error = Error::BindIPAddress; - return false; - } -#endif - } - - set_nonblocking(sock2, true); - - auto ret = - ::connect(sock2, ai.ai_addr, static_cast(ai.ai_addrlen)); - - if (ret < 0) { - if (is_connection_error()) { - error = Error::Connection; - return false; - } - error = wait_until_socket_is_ready(sock2, connection_timeout_sec, - connection_timeout_usec); - if (error != Error::Success) { - if (error == Error::ConnectionTimeout) { quit = true; } - return false; - } - } - - set_nonblocking(sock2, false); - - { -#ifdef _WIN32 - auto timeout = static_cast(read_timeout_sec * 1000 + - read_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(read_timeout_sec); - tv.tv_usec = static_cast(read_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - { - -#ifdef _WIN32 - auto timeout = static_cast(write_timeout_sec * 1000 + - write_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(write_timeout_sec); - tv.tv_usec = static_cast(write_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - - error = Error::Success; - return true; - }); - - if (sock != INVALID_SOCKET) { - error = Error::Success; - } else { - if (error == Error::Success) { error = Error::Connection; } - } - - return sock; -} - -inline bool get_ip_and_port(const struct sockaddr_storage &addr, - socklen_t addr_len, std::string &ip, int &port) { - if (addr.ss_family == AF_INET) { - port = ntohs(reinterpret_cast(&addr)->sin_port); - } else if (addr.ss_family == AF_INET6) { - port = - ntohs(reinterpret_cast(&addr)->sin6_port); - } else { - return false; - } - - std::array ipstr{}; - if (getnameinfo(reinterpret_cast(&addr), addr_len, - ipstr.data(), static_cast(ipstr.size()), nullptr, - 0, NI_NUMERICHOST)) { - return false; - } - - ip = ipstr.data(); - return true; -} - -inline void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) { - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); - if (!getsockname(sock, reinterpret_cast(&addr), - &addr_len)) { - get_ip_and_port(addr, addr_len, ip, port); - } -} - -inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); - - if (!getpeername(sock, reinterpret_cast(&addr), - &addr_len)) { -#ifndef _WIN32 - if (addr.ss_family == AF_UNIX) { -#if defined(__linux__) - struct ucred ucred; - socklen_t len = sizeof(ucred); - if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { - port = ucred.pid; - } -#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ - pid_t pid; - socklen_t len = sizeof(pid); - if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { - port = pid; - } -#endif - return; - } -#endif - get_ip_and_port(addr, addr_len, ip, port); - } -} - -inline constexpr unsigned int str2tag_core(const char *s, size_t l, - unsigned int h) { - return (l == 0) - ? h - : str2tag_core( - s + 1, l - 1, - // Unsets the 6 high bits of h, therefore no overflow happens - (((std::numeric_limits::max)() >> 6) & - h * 33) ^ - static_cast(*s)); -} - -inline unsigned int str2tag(const std::string &s) { - return str2tag_core(s.data(), s.size(), 0); -} - -namespace udl { - -inline constexpr unsigned int operator"" _t(const char *s, size_t l) { - return str2tag_core(s, l, 0); -} - -} // namespace udl - -inline std::string -find_content_type(const std::string &path, - const std::map &user_data, - const std::string &default_content_type) { - auto ext = file_extension(path); - - auto it = user_data.find(ext); - if (it != user_data.end()) { return it->second; } - - using udl::operator""_t; - - switch (str2tag(ext)) { - default: return default_content_type; - - case "css"_t: return "text/css"; - case "csv"_t: return "text/csv"; - case "htm"_t: - case "html"_t: return "text/html"; - case "js"_t: - case "mjs"_t: return "text/javascript"; - case "txt"_t: return "text/plain"; - case "vtt"_t: return "text/vtt"; - - case "apng"_t: return "image/apng"; - case "avif"_t: return "image/avif"; - case "bmp"_t: return "image/bmp"; - case "gif"_t: return "image/gif"; - case "png"_t: return "image/png"; - case "svg"_t: return "image/svg+xml"; - case "webp"_t: return "image/webp"; - case "ico"_t: return "image/x-icon"; - case "tif"_t: return "image/tiff"; - case "tiff"_t: return "image/tiff"; - case "jpg"_t: - case "jpeg"_t: return "image/jpeg"; - - case "mp4"_t: return "video/mp4"; - case "mpeg"_t: return "video/mpeg"; - case "webm"_t: return "video/webm"; - - case "mp3"_t: return "audio/mp3"; - case "mpga"_t: return "audio/mpeg"; - case "weba"_t: return "audio/webm"; - case "wav"_t: return "audio/wave"; - - case "otf"_t: return "font/otf"; - case "ttf"_t: return "font/ttf"; - case "woff"_t: return "font/woff"; - case "woff2"_t: return "font/woff2"; - - case "7z"_t: return "application/x-7z-compressed"; - case "atom"_t: return "application/atom+xml"; - case "pdf"_t: return "application/pdf"; - case "json"_t: return "application/json"; - case "rss"_t: return "application/rss+xml"; - case "tar"_t: return "application/x-tar"; - case "xht"_t: - case "xhtml"_t: return "application/xhtml+xml"; - case "xslt"_t: return "application/xslt+xml"; - case "xml"_t: return "application/xml"; - case "gz"_t: return "application/gzip"; - case "zip"_t: return "application/zip"; - case "wasm"_t: return "application/wasm"; - } -} - -inline bool can_compress_content_type(const std::string &content_type) { - using udl::operator""_t; - - auto tag = str2tag(content_type); - - switch (tag) { - case "image/svg+xml"_t: - case "application/javascript"_t: - case "application/json"_t: - case "application/xml"_t: - case "application/protobuf"_t: - case "application/xhtml+xml"_t: return true; - - case "text/event-stream"_t: return false; - - default: return !content_type.rfind("text/", 0); - } -} - -inline EncodingType encoding_type(const Request &req, const Response &res) { - auto ret = - detail::can_compress_content_type(res.get_header_value("Content-Type")); - if (!ret) { return EncodingType::None; } - - const auto &s = req.get_header_value("Accept-Encoding"); - (void)(s); - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - // TODO: 'Accept-Encoding' has br, not br;q=0 - ret = s.find("br") != std::string::npos; - if (ret) { return EncodingType::Brotli; } -#endif - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 - ret = s.find("gzip") != std::string::npos; - if (ret) { return EncodingType::Gzip; } -#endif - - return EncodingType::None; -} - -inline bool nocompressor::compress(const char *data, size_t data_length, - bool /*last*/, Callback callback) { - if (!data_length) { return true; } - return callback(data, data_length); -} - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -inline gzip_compressor::gzip_compressor() { - std::memset(&strm_, 0, sizeof(strm_)); - strm_.zalloc = Z_NULL; - strm_.zfree = Z_NULL; - strm_.opaque = Z_NULL; - - is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, - Z_DEFAULT_STRATEGY) == Z_OK; -} - -inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } - -inline bool gzip_compressor::compress(const char *data, size_t data_length, - bool last, Callback callback) { - assert(is_valid_); - - do { - constexpr size_t max_avail_in = - (std::numeric_limits::max)(); - - strm_.avail_in = static_cast( - (std::min)(data_length, max_avail_in)); - strm_.next_in = const_cast(reinterpret_cast(data)); - - data_length -= strm_.avail_in; - data += strm_.avail_in; - - auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; - auto ret = Z_OK; - - std::array buff{}; - do { - strm_.avail_out = static_cast(buff.size()); - strm_.next_out = reinterpret_cast(buff.data()); - - ret = deflate(&strm_, flush); - if (ret == Z_STREAM_ERROR) { return false; } - - if (!callback(buff.data(), buff.size() - strm_.avail_out)) { - return false; - } - } while (strm_.avail_out == 0); - - assert((flush == Z_FINISH && ret == Z_STREAM_END) || - (flush == Z_NO_FLUSH && ret == Z_OK)); - assert(strm_.avail_in == 0); - } while (data_length > 0); - - return true; -} - -inline gzip_decompressor::gzip_decompressor() { - std::memset(&strm_, 0, sizeof(strm_)); - strm_.zalloc = Z_NULL; - strm_.zfree = Z_NULL; - strm_.opaque = Z_NULL; - - // 15 is the value of wbits, which should be at the maximum possible value - // to ensure that any gzip stream can be decoded. The offset of 32 specifies - // that the stream type should be automatically detected either gzip or - // deflate. - is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; -} - -inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } - -inline bool gzip_decompressor::is_valid() const { return is_valid_; } - -inline bool gzip_decompressor::decompress(const char *data, size_t data_length, - Callback callback) { - assert(is_valid_); - - auto ret = Z_OK; - - do { - constexpr size_t max_avail_in = - (std::numeric_limits::max)(); - - strm_.avail_in = static_cast( - (std::min)(data_length, max_avail_in)); - strm_.next_in = const_cast(reinterpret_cast(data)); - - data_length -= strm_.avail_in; - data += strm_.avail_in; - - std::array buff{}; - while (strm_.avail_in > 0 && ret == Z_OK) { - strm_.avail_out = static_cast(buff.size()); - strm_.next_out = reinterpret_cast(buff.data()); - - ret = inflate(&strm_, Z_NO_FLUSH); - - assert(ret != Z_STREAM_ERROR); - switch (ret) { - case Z_NEED_DICT: - case Z_DATA_ERROR: - case Z_MEM_ERROR: inflateEnd(&strm_); return false; - } - - if (!callback(buff.data(), buff.size() - strm_.avail_out)) { - return false; - } - } - - if (ret != Z_OK && ret != Z_STREAM_END) { return false; } - - } while (data_length > 0); - - return true; -} -#endif - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT -inline brotli_compressor::brotli_compressor() { - state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); -} - -inline brotli_compressor::~brotli_compressor() { - BrotliEncoderDestroyInstance(state_); -} - -inline bool brotli_compressor::compress(const char *data, size_t data_length, - bool last, Callback callback) { - std::array buff{}; - - auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; - auto available_in = data_length; - auto next_in = reinterpret_cast(data); - - for (;;) { - if (last) { - if (BrotliEncoderIsFinished(state_)) { break; } - } else { - if (!available_in) { break; } - } - - auto available_out = buff.size(); - auto next_out = buff.data(); - - if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in, - &available_out, &next_out, nullptr)) { - return false; - } - - auto output_bytes = buff.size() - available_out; - if (output_bytes) { - callback(reinterpret_cast(buff.data()), output_bytes); - } - } - - return true; -} - -inline brotli_decompressor::brotli_decompressor() { - decoder_s = BrotliDecoderCreateInstance(0, 0, 0); - decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT - : BROTLI_DECODER_RESULT_ERROR; -} - -inline brotli_decompressor::~brotli_decompressor() { - if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } -} - -inline bool brotli_decompressor::is_valid() const { return decoder_s; } - -inline bool brotli_decompressor::decompress(const char *data, - size_t data_length, - Callback callback) { - if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || - decoder_r == BROTLI_DECODER_RESULT_ERROR) { - return 0; - } - - auto next_in = reinterpret_cast(data); - size_t avail_in = data_length; - size_t total_out; - - decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; - - std::array buff{}; - while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { - char *next_out = buff.data(); - size_t avail_out = buff.size(); - - decoder_r = BrotliDecoderDecompressStream( - decoder_s, &avail_in, &next_in, &avail_out, - reinterpret_cast(&next_out), &total_out); - - if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } - - if (!callback(buff.data(), buff.size() - avail_out)) { return false; } - } - - return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || - decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; -} -#endif - -inline bool has_header(const Headers &headers, const std::string &key) { - return headers.find(key) != headers.end(); -} - -inline const char *get_header_value(const Headers &headers, - const std::string &key, const char *def, - size_t id) { - auto rng = headers.equal_range(key); - auto it = rng.first; - std::advance(it, static_cast(id)); - if (it != rng.second) { return it->second.c_str(); } - return def; -} - -template -inline bool parse_header(const char *beg, const char *end, T fn) { - // Skip trailing spaces and tabs. - while (beg < end && is_space_or_tab(end[-1])) { - end--; - } - - auto p = beg; - while (p < end && *p != ':') { - p++; - } - - if (p == end) { return false; } - - auto key_end = p; - - if (*p++ != ':') { return false; } - - while (p < end && is_space_or_tab(*p)) { - p++; - } - - if (p < end) { - auto key_len = key_end - beg; - if (!key_len) { return false; } - - auto key = std::string(beg, key_end); - auto val = case_ignore::equal(key, "Location") - ? std::string(p, end) - : decode_url(std::string(p, end), false); - - // NOTE: From RFC 9110: - // Field values containing CR, LF, or NUL characters are - // invalid and dangerous, due to the varying ways that - // implementations might parse and interpret those - // characters; a recipient of CR, LF, or NUL within a field - // value MUST either reject the message or replace each of - // those characters with SP before further processing or - // forwarding of that message. - static const std::string CR_LF_NUL("\r\n\0", 3); - if (val.find_first_of(CR_LF_NUL) != std::string::npos) { return false; } - - fn(key, val); - return true; - } - - return false; -} - -inline bool read_headers(Stream &strm, Headers &headers) { - const auto bufsiz = 2048; - char buf[bufsiz]; - stream_line_reader line_reader(strm, buf, bufsiz); - - for (;;) { - if (!line_reader.getline()) { return false; } - - // Check if the line ends with CRLF. - auto line_terminator_len = 2; - if (line_reader.end_with_crlf()) { - // Blank line indicates end of headers. - if (line_reader.size() == 2) { break; } - } else { -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - // Blank line indicates end of headers. - if (line_reader.size() == 1) { break; } - line_terminator_len = 1; -#else - continue; // Skip invalid line. -#endif - } - - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - - // Exclude line terminator - auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - - if (!parse_header(line_reader.ptr(), end, - [&](const std::string &key, std::string &val) { - headers.emplace(key, val); - })) { - return false; - } - } - - return true; -} - -inline bool read_content_with_length(Stream &strm, uint64_t len, - Progress progress, - ContentReceiverWithProgress out) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - - uint64_t r = 0; - while (r < len) { - auto read_len = static_cast(len - r); - auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return false; } - - if (!out(buf, static_cast(n), r, len)) { return false; } - r += static_cast(n); - - if (progress) { - if (!progress(r, len)) { return false; } - } - } - - return true; -} - -inline void skip_content_with_length(Stream &strm, uint64_t len) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; - while (r < len) { - auto read_len = static_cast(len - r); - auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return; } - r += static_cast(n); - } -} - -inline bool read_content_without_length(Stream &strm, - ContentReceiverWithProgress out) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; - for (;;) { - auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); - if (n <= 0) { return true; } - - if (!out(buf, static_cast(n), r, 0)) { return false; } - r += static_cast(n); - } - - return true; -} - -template -inline bool read_content_chunked(Stream &strm, T &x, - ContentReceiverWithProgress out) { - const auto bufsiz = 16; - char buf[bufsiz]; - - stream_line_reader line_reader(strm, buf, bufsiz); - - if (!line_reader.getline()) { return false; } - - unsigned long chunk_len; - while (true) { - char *end_ptr; - - chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); - - if (end_ptr == line_reader.ptr()) { return false; } - if (chunk_len == ULONG_MAX) { return false; } - - if (chunk_len == 0) { break; } - - if (!read_content_with_length(strm, chunk_len, nullptr, out)) { - return false; - } - - if (!line_reader.getline()) { return false; } - - if (strcmp(line_reader.ptr(), "\r\n") != 0) { return false; } - - if (!line_reader.getline()) { return false; } - } - - assert(chunk_len == 0); - - // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked - // transfer coding is complete when a chunk with a chunk-size of zero is - // received, possibly followed by a trailer section, and finally terminated by - // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 - // - // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section - // does't care for the existence of the final CRLF. In other words, it seems - // to be ok whether the final CRLF exists or not in the chunked data. - // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 - // - // According to the reference code in RFC 9112, cpp-htpplib now allows - // chuncked transfer coding data without the final CRLF. - if (!line_reader.getline()) { return true; } - - while (strcmp(line_reader.ptr(), "\r\n") != 0) { - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - - // Exclude line terminator - constexpr auto line_terminator_len = 2; - auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - - parse_header(line_reader.ptr(), end, - [&](const std::string &key, const std::string &val) { - x.headers.emplace(key, val); - }); - - if (!line_reader.getline()) { return false; } - } - - return true; -} - -inline bool is_chunked_transfer_encoding(const Headers &headers) { - return case_ignore::equal( - get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); -} - -template -bool prepare_content_receiver(T &x, int &status, - ContentReceiverWithProgress receiver, - bool decompress, U callback) { - if (decompress) { - std::string encoding = x.get_header_value("Content-Encoding"); - std::unique_ptr decompressor; - - if (encoding == "gzip" || encoding == "deflate") { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif - } else if (encoding.find("br") != std::string::npos) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif - } - - if (decompressor) { - if (decompressor->is_valid()) { - ContentReceiverWithProgress out = [&](const char *buf, size_t n, - uint64_t off, uint64_t len) { - return decompressor->decompress(buf, n, - [&](const char *buf2, size_t n2) { - return receiver(buf2, n2, off, len); - }); - }; - return callback(std::move(out)); - } else { - status = StatusCode::InternalServerError_500; - return false; - } - } - } - - ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, - uint64_t len) { - return receiver(buf, n, off, len); - }; - return callback(std::move(out)); -} - -template -bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, - Progress progress, ContentReceiverWithProgress receiver, - bool decompress) { - return prepare_content_receiver( - x, status, std::move(receiver), decompress, - [&](const ContentReceiverWithProgress &out) { - auto ret = true; - auto exceed_payload_max_length = false; - - if (is_chunked_transfer_encoding(x.headers)) { - ret = read_content_chunked(strm, x, out); - } else if (!has_header(x.headers, "Content-Length")) { - ret = read_content_without_length(strm, out); - } else { - auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0); - if (len > payload_max_length) { - exceed_payload_max_length = true; - skip_content_with_length(strm, len); - ret = false; - } else if (len > 0) { - ret = read_content_with_length(strm, len, std::move(progress), out); - } - } - - if (!ret) { - status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413 - : StatusCode::BadRequest_400; - } - return ret; - }); -} - -inline ssize_t write_request_line(Stream &strm, const std::string &method, - const std::string &path) { - std::string s = method; - s += " "; - s += path; - s += " HTTP/1.1\r\n"; - return strm.write(s.data(), s.size()); -} - -inline ssize_t write_response_line(Stream &strm, int status) { - std::string s = "HTTP/1.1 "; - s += std::to_string(status); - s += " "; - s += httplib::status_message(status); - s += "\r\n"; - return strm.write(s.data(), s.size()); -} - -inline ssize_t write_headers(Stream &strm, const Headers &headers) { - ssize_t write_len = 0; - for (const auto &x : headers) { - std::string s; - s = x.first; - s += ": "; - s += x.second; - s += "\r\n"; - - auto len = strm.write(s.data(), s.size()); - if (len < 0) { return len; } - write_len += len; - } - auto len = strm.write("\r\n"); - if (len < 0) { return len; } - write_len += len; - return write_len; -} - -inline bool write_data(Stream &strm, const char *d, size_t l) { - size_t offset = 0; - while (offset < l) { - auto length = strm.write(d + offset, l - offset); - if (length < 0) { return false; } - offset += static_cast(length); - } - return true; -} - -template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, - size_t offset, size_t length, T is_shutting_down, - Error &error) { - size_t end_offset = offset + length; - auto ok = true; - DataSink data_sink; - - data_sink.write = [&](const char *d, size_t l) -> bool { - if (ok) { - if (strm.is_writable() && write_data(strm, d, l)) { - offset += l; - } else { - ok = false; - } - } - return ok; - }; - - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; - - while (offset < end_offset && !is_shutting_down()) { - if (!strm.is_writable()) { - error = Error::Write; - return false; - } else if (!content_provider(offset, end_offset - offset, data_sink)) { - error = Error::Canceled; - return false; - } else if (!ok) { - error = Error::Write; - return false; - } - } - - error = Error::Success; - return true; -} - -template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, - size_t offset, size_t length, - const T &is_shutting_down) { - auto error = Error::Success; - return write_content(strm, content_provider, offset, length, is_shutting_down, - error); -} - -template -inline bool -write_content_without_length(Stream &strm, - const ContentProvider &content_provider, - const T &is_shutting_down) { - size_t offset = 0; - auto data_available = true; - auto ok = true; - DataSink data_sink; - - data_sink.write = [&](const char *d, size_t l) -> bool { - if (ok) { - offset += l; - if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; } - } - return ok; - }; - - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; - - data_sink.done = [&](void) { data_available = false; }; - - while (data_available && !is_shutting_down()) { - if (!strm.is_writable()) { - return false; - } else if (!content_provider(offset, 0, data_sink)) { - return false; - } else if (!ok) { - return false; - } - } - return true; -} - -template -inline bool -write_content_chunked(Stream &strm, const ContentProvider &content_provider, - const T &is_shutting_down, U &compressor, Error &error) { - size_t offset = 0; - auto data_available = true; - auto ok = true; - DataSink data_sink; - - data_sink.write = [&](const char *d, size_t l) -> bool { - if (ok) { - data_available = l > 0; - offset += l; - - std::string payload; - if (compressor.compress(d, l, false, - [&](const char *data, size_t data_len) { - payload.append(data, data_len); - return true; - })) { - if (!payload.empty()) { - // Emit chunked response header and footer for each chunk - auto chunk = - from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!strm.is_writable() || - !write_data(strm, chunk.data(), chunk.size())) { - ok = false; - } - } - } else { - ok = false; - } - } - return ok; - }; - - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; - - auto done_with_trailer = [&](const Headers *trailer) { - if (!ok) { return; } - - data_available = false; - - std::string payload; - if (!compressor.compress(nullptr, 0, true, - [&](const char *data, size_t data_len) { - payload.append(data, data_len); - return true; - })) { - ok = false; - return; - } - - if (!payload.empty()) { - // Emit chunked response header and footer for each chunk - auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!strm.is_writable() || - !write_data(strm, chunk.data(), chunk.size())) { - ok = false; - return; - } - } - - static const std::string done_marker("0\r\n"); - if (!write_data(strm, done_marker.data(), done_marker.size())) { - ok = false; - } - - // Trailer - if (trailer) { - for (const auto &kv : *trailer) { - std::string field_line = kv.first + ": " + kv.second + "\r\n"; - if (!write_data(strm, field_line.data(), field_line.size())) { - ok = false; - } - } - } - - static const std::string crlf("\r\n"); - if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; } - }; - - data_sink.done = [&](void) { done_with_trailer(nullptr); }; - - data_sink.done_with_trailer = [&](const Headers &trailer) { - done_with_trailer(&trailer); - }; - - while (data_available && !is_shutting_down()) { - if (!strm.is_writable()) { - error = Error::Write; - return false; - } else if (!content_provider(offset, 0, data_sink)) { - error = Error::Canceled; - return false; - } else if (!ok) { - error = Error::Write; - return false; - } - } - - error = Error::Success; - return true; -} - -template -inline bool write_content_chunked(Stream &strm, - const ContentProvider &content_provider, - const T &is_shutting_down, U &compressor) { - auto error = Error::Success; - return write_content_chunked(strm, content_provider, is_shutting_down, - compressor, error); -} - -template -inline bool redirect(T &cli, Request &req, Response &res, - const std::string &path, const std::string &location, - Error &error) { - Request new_req = req; - new_req.path = path; - new_req.redirect_count_ -= 1; - - if (res.status == StatusCode::SeeOther_303 && - (req.method != "GET" && req.method != "HEAD")) { - new_req.method = "GET"; - new_req.body.clear(); - new_req.headers.clear(); - } - - Response new_res; - - auto ret = cli.send(new_req, new_res, error); - if (ret) { - req = new_req; - res = new_res; - - if (res.location.empty()) { res.location = location; } - } - return ret; -} - -inline std::string params_to_query_str(const Params ¶ms) { - std::string query; - - for (auto it = params.begin(); it != params.end(); ++it) { - if (it != params.begin()) { query += "&"; } - query += it->first; - query += "="; - query += encode_query_param(it->second); - } - return query; -} - -inline void parse_query_text(const char *data, std::size_t size, - Params ¶ms) { - std::set cache; - split(data, data + size, '&', [&](const char *b, const char *e) { - std::string kv(b, e); - if (cache.find(kv) != cache.end()) { return; } - cache.insert(std::move(kv)); - - std::string key; - std::string val; - divide(b, static_cast(e - b), '=', - [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, - std::size_t rhs_size) { - key.assign(lhs_data, lhs_size); - val.assign(rhs_data, rhs_size); - }); - - if (!key.empty()) { - params.emplace(decode_url(key, true), decode_url(val, true)); - } - }); -} - -inline void parse_query_text(const std::string &s, Params ¶ms) { - parse_query_text(s.data(), s.size(), params); -} - -inline bool parse_multipart_boundary(const std::string &content_type, - std::string &boundary) { - auto boundary_keyword = "boundary="; - auto pos = content_type.find(boundary_keyword); - if (pos == std::string::npos) { return false; } - auto end = content_type.find(';', pos); - auto beg = pos + strlen(boundary_keyword); - boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg)); - return !boundary.empty(); -} - -inline void parse_disposition_params(const std::string &s, Params ¶ms) { - std::set cache; - split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) { - std::string kv(b, e); - if (cache.find(kv) != cache.end()) { return; } - cache.insert(kv); - - std::string key; - std::string val; - split(b, e, '=', [&](const char *b2, const char *e2) { - if (key.empty()) { - key.assign(b2, e2); - } else { - val.assign(b2, e2); - } - }); - - if (!key.empty()) { - params.emplace(trim_double_quotes_copy((key)), - trim_double_quotes_copy((val))); - } - }); -} - -#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 - auto is_valid = [](const std::string &str) { - return std::all_of(str.cbegin(), str.cend(), - [](unsigned char c) { return std::isdigit(c); }); - }; - - if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) { - const auto pos = static_cast(6); - const auto len = static_cast(s.size() - 6); - auto all_valid_ranges = true; - split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { - if (!all_valid_ranges) { return; } - - const auto it = std::find(b, e, '-'); - if (it == e) { - all_valid_ranges = false; - return; - } - - const auto lhs = std::string(b, it); - const auto rhs = std::string(it + 1, e); - if (!is_valid(lhs) || !is_valid(rhs)) { - all_valid_ranges = false; - return; - } - - const auto first = - static_cast(lhs.empty() ? -1 : std::stoll(lhs)); - const auto last = - static_cast(rhs.empty() ? -1 : std::stoll(rhs)); - if ((first == -1 && last == -1) || - (first != -1 && last != -1 && first > last)) { - all_valid_ranges = false; - return; - } - - ranges.emplace_back(first, last); - }); - return all_valid_ranges && !ranges.empty(); - } - return false; -#ifdef CPPHTTPLIB_NO_EXCEPTIONS -} -#else -} catch (...) { return false; } -#endif - -class MultipartFormDataParser { -public: - MultipartFormDataParser() = default; - - void set_boundary(std::string &&boundary) { - boundary_ = boundary; - dash_boundary_crlf_ = dash_ + boundary_ + crlf_; - crlf_dash_boundary_ = crlf_ + dash_ + boundary_; - } - - bool is_valid() const { return is_valid_; } - - bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, - const MultipartContentHeader &header_callback) { - - buf_append(buf, n); - - while (buf_size() > 0) { - switch (state_) { - case 0: { // Initial boundary - buf_erase(buf_find(dash_boundary_crlf_)); - if (dash_boundary_crlf_.size() > buf_size()) { return true; } - if (!buf_start_with(dash_boundary_crlf_)) { return false; } - buf_erase(dash_boundary_crlf_.size()); - state_ = 1; - break; - } - case 1: { // New entry - clear_file_info(); - state_ = 2; - break; - } - case 2: { // Headers - auto pos = buf_find(crlf_); - if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - while (pos < buf_size()) { - // Empty line - if (pos == 0) { - if (!header_callback(file_)) { - is_valid_ = false; - return false; - } - buf_erase(crlf_.size()); - state_ = 3; - break; - } - - const auto header = buf_head(pos); - - if (!parse_header(header.data(), header.data() + header.size(), - [&](const std::string &, const std::string &) {})) { - is_valid_ = false; - return false; - } - - static const std::string header_content_type = "Content-Type:"; - - if (start_with_case_ignore(header, header_content_type)) { - file_.content_type = - trim_copy(header.substr(header_content_type.size())); - } else { - static const std::regex re_content_disposition( - R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", - std::regex_constants::icase); - - std::smatch m; - if (std::regex_match(header, m, re_content_disposition)) { - Params params; - parse_disposition_params(m[1], params); - - auto it = params.find("name"); - if (it != params.end()) { - file_.name = it->second; - } else { - is_valid_ = false; - return false; - } - - it = params.find("filename"); - if (it != params.end()) { file_.filename = it->second; } - - it = params.find("filename*"); - if (it != params.end()) { - // Only allow UTF-8 enconnding... - static const std::regex re_rfc5987_encoding( - R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); - - std::smatch m2; - if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { - file_.filename = decode_url(m2[1], false); // override... - } else { - is_valid_ = false; - return false; - } - } - } - } - buf_erase(pos + crlf_.size()); - pos = buf_find(crlf_); - } - if (state_ != 3) { return true; } - break; - } - case 3: { // Body - if (crlf_dash_boundary_.size() > buf_size()) { return true; } - auto pos = buf_find(crlf_dash_boundary_); - if (pos < buf_size()) { - if (!content_callback(buf_data(), pos)) { - is_valid_ = false; - return false; - } - buf_erase(pos + crlf_dash_boundary_.size()); - state_ = 4; - } else { - auto len = buf_size() - crlf_dash_boundary_.size(); - if (len > 0) { - if (!content_callback(buf_data(), len)) { - is_valid_ = false; - return false; - } - buf_erase(len); - } - return true; - } - break; - } - case 4: { // Boundary - if (crlf_.size() > buf_size()) { return true; } - if (buf_start_with(crlf_)) { - buf_erase(crlf_.size()); - state_ = 1; - } else { - if (dash_.size() > buf_size()) { return true; } - if (buf_start_with(dash_)) { - buf_erase(dash_.size()); - is_valid_ = true; - buf_erase(buf_size()); // Remove epilogue - } else { - return true; - } - } - break; - } - } - } - - return true; - } - -private: - void clear_file_info() { - file_.name.clear(); - file_.filename.clear(); - file_.content_type.clear(); - } - - bool start_with_case_ignore(const std::string &a, - const std::string &b) const { - if (a.size() < b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { - if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { - return false; - } - } - return true; - } - - const std::string dash_ = "--"; - const std::string crlf_ = "\r\n"; - std::string boundary_; - std::string dash_boundary_crlf_; - std::string crlf_dash_boundary_; - - size_t state_ = 0; - bool is_valid_ = false; - MultipartFormData file_; - - // Buffer - bool start_with(const std::string &a, size_t spos, size_t epos, - const std::string &b) const { - if (epos - spos < b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { - if (a[i + spos] != b[i]) { return false; } - } - return true; - } - - size_t buf_size() const { return buf_epos_ - buf_spos_; } - - const char *buf_data() const { return &buf_[buf_spos_]; } - - std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } - - bool buf_start_with(const std::string &s) const { - return start_with(buf_, buf_spos_, buf_epos_, s); - } - - size_t buf_find(const std::string &s) const { - auto c = s.front(); - - size_t off = buf_spos_; - while (off < buf_epos_) { - auto pos = off; - while (true) { - if (pos == buf_epos_) { return buf_size(); } - if (buf_[pos] == c) { break; } - pos++; - } - - auto remaining_size = buf_epos_ - pos; - if (s.size() > remaining_size) { return buf_size(); } - - if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } - - off = pos + 1; - } - - return buf_size(); - } - - void buf_append(const char *data, size_t n) { - auto remaining_size = buf_size(); - if (remaining_size > 0 && buf_spos_ > 0) { - for (size_t i = 0; i < remaining_size; i++) { - buf_[i] = buf_[buf_spos_ + i]; - } - } - buf_spos_ = 0; - buf_epos_ = remaining_size; - - if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } - - for (size_t i = 0; i < n; i++) { - buf_[buf_epos_ + i] = data[i]; - } - buf_epos_ += n; - } - - void buf_erase(size_t size) { buf_spos_ += size; } - - std::string buf_; - size_t buf_spos_ = 0; - size_t buf_epos_ = 0; -}; - -inline std::string random_string(size_t length) { - static const char data[] = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - - // std::random_device might actually be deterministic on some - // platforms, but due to lack of support in the c++ standard library, - // doing better requires either some ugly hacks or breaking portability. - static std::random_device seed_gen; - - // Request 128 bits of entropy for initialization - static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), - seed_gen()}; - - static std::mt19937 engine(seed_sequence); - - std::string result; - for (size_t i = 0; i < length; i++) { - result += data[engine() % (sizeof(data) - 1)]; - } - return result; -} - -inline std::string make_multipart_data_boundary() { - return "--cpp-httplib-multipart-data-" + detail::random_string(16); -} - -inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { - auto valid = true; - for (size_t i = 0; i < boundary.size(); i++) { - auto c = boundary[i]; - if (!std::isalnum(c) && c != '-' && c != '_') { - valid = false; - break; - } - } - return valid; -} - -template -inline std::string -serialize_multipart_formdata_item_begin(const T &item, - const std::string &boundary) { - std::string body = "--" + boundary + "\r\n"; - body += "Content-Disposition: form-data; name=\"" + item.name + "\""; - if (!item.filename.empty()) { - body += "; filename=\"" + item.filename + "\""; - } - body += "\r\n"; - if (!item.content_type.empty()) { - body += "Content-Type: " + item.content_type + "\r\n"; - } - body += "\r\n"; - - return body; -} - -inline std::string serialize_multipart_formdata_item_end() { return "\r\n"; } - -inline std::string -serialize_multipart_formdata_finish(const std::string &boundary) { - return "--" + boundary + "--\r\n"; -} - -inline std::string -serialize_multipart_formdata_get_content_type(const std::string &boundary) { - return "multipart/form-data; boundary=" + boundary; -} - -inline std::string -serialize_multipart_formdata(const MultipartFormDataItems &items, - const std::string &boundary, bool finish = true) { - std::string body; - - for (const auto &item : items) { - body += serialize_multipart_formdata_item_begin(item, boundary); - body += item.content + serialize_multipart_formdata_item_end(); - } - - if (finish) { body += serialize_multipart_formdata_finish(boundary); } - - return body; -} - -inline bool range_error(Request &req, Response &res) { - if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { - ssize_t contant_len = static_cast( - res.content_length_ ? res.content_length_ : res.body.size()); - - ssize_t prev_first_pos = -1; - ssize_t prev_last_pos = -1; - size_t overwrapping_count = 0; - - // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 - // 'HTTP Semantics' to avoid potential denial-of-service attacks. - // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 - - // Too many ranges - if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; } - - for (auto &r : req.ranges) { - auto &first_pos = r.first; - auto &last_pos = r.second; - - if (first_pos == -1 && last_pos == -1) { - first_pos = 0; - last_pos = contant_len; - } - - if (first_pos == -1) { - first_pos = contant_len - last_pos; - last_pos = contant_len - 1; - } - - if (last_pos == -1) { last_pos = contant_len - 1; } - - // Range must be within content length - if (!(0 <= first_pos && first_pos <= last_pos && - last_pos <= contant_len - 1)) { - return true; - } - - // Ranges must be in ascending order - if (first_pos <= prev_first_pos) { return true; } - - // Request must not have more than two overlapping ranges - if (first_pos <= prev_last_pos) { - overwrapping_count++; - if (overwrapping_count > 2) { return true; } - } - - prev_first_pos = (std::max)(prev_first_pos, first_pos); - prev_last_pos = (std::max)(prev_last_pos, last_pos); - } - } - - return false; -} - -inline std::pair -get_range_offset_and_length(Range r, size_t content_length) { - assert(r.first != -1 && r.second != -1); - assert(0 <= r.first && r.first < static_cast(content_length)); - assert(r.first <= r.second && - r.second < static_cast(content_length)); - (void)(content_length); - return std::make_pair(r.first, static_cast(r.second - r.first) + 1); -} - -inline std::string make_content_range_header_field( - const std::pair &offset_and_length, size_t content_length) { - auto st = offset_and_length.first; - auto ed = st + offset_and_length.second - 1; - - std::string field = "bytes "; - field += std::to_string(st); - field += "-"; - field += std::to_string(ed); - field += "/"; - field += std::to_string(content_length); - return field; -} - -template -bool process_multipart_ranges_data(const Request &req, - const std::string &boundary, - const std::string &content_type, - size_t content_length, SToken stoken, - CToken ctoken, Content content) { - for (size_t i = 0; i < req.ranges.size(); i++) { - ctoken("--"); - stoken(boundary); - ctoken("\r\n"); - if (!content_type.empty()) { - ctoken("Content-Type: "); - stoken(content_type); - ctoken("\r\n"); - } - - auto offset_and_length = - get_range_offset_and_length(req.ranges[i], content_length); - - ctoken("Content-Range: "); - stoken(make_content_range_header_field(offset_and_length, content_length)); - ctoken("\r\n"); - ctoken("\r\n"); - - if (!content(offset_and_length.first, offset_and_length.second)) { - return false; - } - ctoken("\r\n"); - } - - ctoken("--"); - stoken(boundary); - ctoken("--"); - - return true; -} - -inline void make_multipart_ranges_data(const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type, - size_t content_length, - std::string &data) { - process_multipart_ranges_data( - req, boundary, content_type, content_length, - [&](const std::string &token) { data += token; }, - [&](const std::string &token) { data += token; }, - [&](size_t offset, size_t length) { - assert(offset + length <= content_length); - data += res.body.substr(offset, length); - return true; - }); -} - -inline size_t get_multipart_ranges_data_length(const Request &req, - const std::string &boundary, - const std::string &content_type, - size_t content_length) { - size_t data_length = 0; - - process_multipart_ranges_data( - req, boundary, content_type, content_length, - [&](const std::string &token) { data_length += token.size(); }, - [&](const std::string &token) { data_length += token.size(); }, - [&](size_t /*offset*/, size_t length) { - data_length += length; - return true; - }); - - return data_length; -} - -template -inline bool -write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type, - size_t content_length, const T &is_shutting_down) { - return process_multipart_ranges_data( - req, boundary, content_type, content_length, - [&](const std::string &token) { strm.write(token); }, - [&](const std::string &token) { strm.write(token); }, - [&](size_t offset, size_t length) { - return write_content(strm, res.content_provider_, offset, length, - is_shutting_down); - }); -} - -inline bool expect_content(const Request &req) { - if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || - req.method == "PRI" || req.method == "DELETE") { - return true; - } - // TODO: check if Content-Length is set - return false; -} - -inline bool has_crlf(const std::string &s) { - auto p = s.c_str(); - while (*p) { - if (*p == '\r' || *p == '\n') { return true; } - p++; - } - return false; -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline std::string message_digest(const std::string &s, const EVP_MD *algo) { - auto context = std::unique_ptr( - EVP_MD_CTX_new(), EVP_MD_CTX_free); - - unsigned int hash_length = 0; - unsigned char hash[EVP_MAX_MD_SIZE]; - - EVP_DigestInit_ex(context.get(), algo, nullptr); - EVP_DigestUpdate(context.get(), s.c_str(), s.size()); - EVP_DigestFinal_ex(context.get(), hash, &hash_length); - - std::stringstream ss; - for (auto i = 0u; i < hash_length; ++i) { - ss << std::hex << std::setw(2) << std::setfill('0') - << static_cast(hash[i]); - } - - return ss.str(); -} - -inline std::string MD5(const std::string &s) { - return message_digest(s, EVP_md5()); -} - -inline std::string SHA_256(const std::string &s) { - return message_digest(s, EVP_sha256()); -} - -inline std::string SHA_512(const std::string &s) { - return message_digest(s, EVP_sha512()); -} -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN32 -// NOTE: This code came up with the following stackoverflow post: -// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store -inline bool load_system_certs_on_windows(X509_STORE *store) { - auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); - if (!hStore) { return false; } - - auto result = false; - PCCERT_CONTEXT pContext = NULL; - while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != - nullptr) { - auto encoded_cert = - static_cast(pContext->pbCertEncoded); - - auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); - if (x509) { - X509_STORE_add_cert(store, x509); - X509_free(x509); - result = true; - } - } - - CertFreeCertificateContext(pContext); - CertCloseStore(hStore, 0); - - return result; -} -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#if TARGET_OS_OSX -template -using CFObjectPtr = - std::unique_ptr::type, void (*)(CFTypeRef)>; - -inline void cf_object_ptr_deleter(CFTypeRef obj) { - if (obj) { CFRelease(obj); } -} - -inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { - CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; - CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, - kCFBooleanTrue}; - - CFObjectPtr query( - CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, - sizeof(keys) / sizeof(keys[0]), - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks), - cf_object_ptr_deleter); - - if (!query) { return false; } - - CFTypeRef security_items = nullptr; - if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || - CFArrayGetTypeID() != CFGetTypeID(security_items)) { - return false; - } - - certs.reset(reinterpret_cast(security_items)); - return true; -} - -inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { - CFArrayRef root_security_items = nullptr; - if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { - return false; - } - - certs.reset(root_security_items); - return true; -} - -inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { - auto result = false; - for (auto i = 0; i < CFArrayGetCount(certs); ++i) { - const auto cert = reinterpret_cast( - CFArrayGetValueAtIndex(certs, i)); - - if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } - - CFDataRef cert_data = nullptr; - if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != - errSecSuccess) { - continue; - } - - CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); - - auto encoded_cert = static_cast( - CFDataGetBytePtr(cert_data_ptr.get())); - - auto x509 = - d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); - - if (x509) { - X509_STORE_add_cert(store, x509); - X509_free(x509); - result = true; - } - } - - return result; -} - -inline bool load_system_certs_on_macos(X509_STORE *store) { - auto result = false; - CFObjectPtr certs(nullptr, cf_object_ptr_deleter); - if (retrieve_certs_from_keychain(certs) && certs) { - result = add_certs_to_x509_store(certs.get(), store); - } - - if (retrieve_root_certs_from_keychain(certs) && certs) { - result = add_certs_to_x509_store(certs.get(), store) || result; - } - - return result; -} -#endif // TARGET_OS_OSX -#endif // _WIN32 -#endif // CPPHTTPLIB_OPENSSL_SUPPORT - -#ifdef _WIN32 -class WSInit { -public: - WSInit() { - WSADATA wsaData; - if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; - } - - ~WSInit() { - if (is_valid_) WSACleanup(); - } - - bool is_valid_ = false; -}; - -static WSInit wsinit_; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline std::pair make_digest_authentication_header( - const Request &req, const std::map &auth, - size_t cnonce_count, const std::string &cnonce, const std::string &username, - const std::string &password, bool is_proxy = false) { - std::string nc; - { - std::stringstream ss; - ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; - nc = ss.str(); - } - - std::string qop; - if (auth.find("qop") != auth.end()) { - qop = auth.at("qop"); - if (qop.find("auth-int") != std::string::npos) { - qop = "auth-int"; - } else if (qop.find("auth") != std::string::npos) { - qop = "auth"; - } else { - qop.clear(); - } - } - - std::string algo = "MD5"; - if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } - - std::string response; - { - auto H = algo == "SHA-256" ? detail::SHA_256 - : algo == "SHA-512" ? detail::SHA_512 - : detail::MD5; - - auto A1 = username + ":" + auth.at("realm") + ":" + password; - - auto A2 = req.method + ":" + req.path; - if (qop == "auth-int") { A2 += ":" + H(req.body); } - - if (qop.empty()) { - response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); - } else { - response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + - ":" + qop + ":" + H(A2)); - } - } - - auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; - - auto field = "Digest username=\"" + username + "\", realm=\"" + - auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + - "\", uri=\"" + req.path + "\", algorithm=" + algo + - (qop.empty() ? ", response=\"" - : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + - cnonce + "\", response=\"") + - response + "\"" + - (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); - - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, field); -} -#endif - -inline bool parse_www_authenticate(const Response &res, - std::map &auth, - bool is_proxy) { - auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; - if (res.has_header(auth_key)) { - static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); - auto s = res.get_header_value(auth_key); - auto pos = s.find(' '); - if (pos != std::string::npos) { - auto type = s.substr(0, pos); - if (type == "Basic") { - return false; - } else if (type == "Digest") { - s = s.substr(pos + 1); - auto beg = std::sregex_iterator(s.begin(), s.end(), re); - for (auto i = beg; i != std::sregex_iterator(); ++i) { - const auto &m = *i; - auto key = s.substr(static_cast(m.position(1)), - static_cast(m.length(1))); - auto val = m.length(2) > 0 - ? s.substr(static_cast(m.position(2)), - static_cast(m.length(2))) - : s.substr(static_cast(m.position(3)), - static_cast(m.length(3))); - auth[key] = val; - } - return true; - } - } - } - return false; -} - -class ContentProviderAdapter { -public: - explicit ContentProviderAdapter( - ContentProviderWithoutLength &&content_provider) - : content_provider_(content_provider) {} - - bool operator()(size_t offset, size_t, DataSink &sink) { - return content_provider_(offset, sink); - } - -private: - ContentProviderWithoutLength content_provider_; -}; - -} // namespace detail - -inline std::string hosted_at(const std::string &hostname) { - std::vector addrs; - hosted_at(hostname, addrs); - if (addrs.empty()) { return std::string(); } - return addrs[0]; -} - -inline void hosted_at(const std::string &hostname, - std::vector &addrs) { - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; - - if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result)) { -#if defined __linux__ && !defined __ANDROID__ - res_init(); -#endif - return; - } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); - - for (auto rp = result; rp; rp = rp->ai_next) { - const auto &addr = - *reinterpret_cast(rp->ai_addr); - std::string ip; - auto dummy = -1; - if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, - dummy)) { - addrs.push_back(ip); - } - } -} - -inline std::string append_query_params(const std::string &path, - const Params ¶ms) { - std::string path_with_query = path; - const static std::regex re("[^?]+\\?.*"); - auto delm = std::regex_match(path, re) ? '&' : '?'; - path_with_query += delm + detail::params_to_query_str(params); - return path_with_query; -} - -// Header utilities -inline std::pair -make_range_header(const Ranges &ranges) { - std::string field = "bytes="; - auto i = 0; - for (const auto &r : ranges) { - if (i != 0) { field += ", "; } - if (r.first != -1) { field += std::to_string(r.first); } - field += '-'; - if (r.second != -1) { field += std::to_string(r.second); } - i++; - } - return std::make_pair("Range", std::move(field)); -} - -inline std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, bool is_proxy) { - auto field = "Basic " + detail::base64_encode(username + ":" + password); - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, std::move(field)); -} - -inline std::pair -make_bearer_token_authentication_header(const std::string &token, - bool is_proxy = false) { - auto field = "Bearer " + token; - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, std::move(field)); -} - -// Request implementation -inline bool Request::has_header(const std::string &key) const { - return detail::has_header(headers, key); -} - -inline std::string Request::get_header_value(const std::string &key, - const char *def, size_t id) const { - return detail::get_header_value(headers, key, def, id); -} - -inline size_t Request::get_header_value_count(const std::string &key) const { - auto r = headers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -inline void Request::set_header(const std::string &key, - const std::string &val) { - if (!detail::has_crlf(key) && !detail::has_crlf(val)) { - headers.emplace(key, val); - } -} - -inline bool Request::has_param(const std::string &key) const { - return params.find(key) != params.end(); -} - -inline std::string Request::get_param_value(const std::string &key, - size_t id) const { - auto rng = params.equal_range(key); - auto it = rng.first; - std::advance(it, static_cast(id)); - if (it != rng.second) { return it->second; } - return std::string(); -} - -inline size_t Request::get_param_value_count(const std::string &key) const { - auto r = params.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -inline bool Request::is_multipart_form_data() const { - const auto &content_type = get_header_value("Content-Type"); - return !content_type.rfind("multipart/form-data", 0); -} - -inline bool Request::has_file(const std::string &key) const { - return files.find(key) != files.end(); -} - -inline MultipartFormData Request::get_file_value(const std::string &key) const { - auto it = files.find(key); - if (it != files.end()) { return it->second; } - return MultipartFormData(); -} - -inline std::vector -Request::get_file_values(const std::string &key) const { - std::vector values; - auto rng = files.equal_range(key); - for (auto it = rng.first; it != rng.second; it++) { - values.push_back(it->second); - } - return values; -} - -// Response implementation -inline bool Response::has_header(const std::string &key) const { - return headers.find(key) != headers.end(); -} - -inline std::string Response::get_header_value(const std::string &key, - const char *def, - size_t id) const { - return detail::get_header_value(headers, key, def, id); -} - -inline size_t Response::get_header_value_count(const std::string &key) const { - auto r = headers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -inline void Response::set_header(const std::string &key, - const std::string &val) { - if (!detail::has_crlf(key) && !detail::has_crlf(val)) { - headers.emplace(key, val); - } -} - -inline void Response::set_redirect(const std::string &url, int stat) { - if (!detail::has_crlf(url)) { - set_header("Location", url); - if (300 <= stat && stat < 400) { - this->status = stat; - } else { - this->status = StatusCode::Found_302; - } - } -} - -inline void Response::set_content(const char *s, size_t n, - const std::string &content_type) { - body.assign(s, n); - - auto rng = headers.equal_range("Content-Type"); - headers.erase(rng.first, rng.second); - set_header("Content-Type", content_type); -} - -inline void Response::set_content(const std::string &s, - const std::string &content_type) { - set_content(s.data(), s.size(), content_type); -} - -inline void Response::set_content(std::string &&s, - const std::string &content_type) { - body = std::move(s); - - auto rng = headers.equal_range("Content-Type"); - headers.erase(rng.first, rng.second); - set_header("Content-Type", content_type); -} - -inline void Response::set_content_provider( - size_t in_length, const std::string &content_type, ContentProvider provider, - ContentProviderResourceReleaser resource_releaser) { - set_header("Content-Type", content_type); - content_length_ = in_length; - if (in_length > 0) { content_provider_ = std::move(provider); } - content_provider_resource_releaser_ = std::move(resource_releaser); - is_chunked_content_provider_ = false; -} - -inline void Response::set_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser) { - set_header("Content-Type", content_type); - content_length_ = 0; - content_provider_ = detail::ContentProviderAdapter(std::move(provider)); - content_provider_resource_releaser_ = std::move(resource_releaser); - is_chunked_content_provider_ = false; -} - -inline void Response::set_chunked_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser) { - set_header("Content-Type", content_type); - content_length_ = 0; - content_provider_ = detail::ContentProviderAdapter(std::move(provider)); - content_provider_resource_releaser_ = std::move(resource_releaser); - is_chunked_content_provider_ = true; -} - -inline void Response::set_file_content(const std::string &path, - const std::string &content_type) { - file_content_path_ = path; - file_content_content_type_ = content_type; -} - -inline void Response::set_file_content(const std::string &path) { - file_content_path_ = path; -} - -// Result implementation -inline bool Result::has_request_header(const std::string &key) const { - return request_headers_.find(key) != request_headers_.end(); -} - -inline std::string Result::get_request_header_value(const std::string &key, - const char *def, - size_t id) const { - return detail::get_header_value(request_headers_, key, def, id); -} - -inline size_t -Result::get_request_header_value_count(const std::string &key) const { - auto r = request_headers_.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -// Stream implementation -inline ssize_t Stream::write(const char *ptr) { - return write(ptr, strlen(ptr)); -} - -inline ssize_t Stream::write(const std::string &s) { - return write(s.data(), s.size()); -} - -namespace detail { - -// Socket stream implementation -inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec) - : sock_(sock), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec), - write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec), read_buff_(read_buff_size_, 0) {} - -inline SocketStream::~SocketStream() = default; - -inline bool SocketStream::is_readable() const { - return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; -} - -inline bool SocketStream::is_writable() const { - return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); -} - -inline ssize_t SocketStream::read(char *ptr, size_t size) { -#ifdef _WIN32 - size = - (std::min)(size, static_cast((std::numeric_limits::max)())); -#else - size = (std::min)(size, - static_cast((std::numeric_limits::max)())); -#endif - - if (read_buff_off_ < read_buff_content_size_) { - auto remaining_size = read_buff_content_size_ - read_buff_off_; - if (size <= remaining_size) { - memcpy(ptr, read_buff_.data() + read_buff_off_, size); - read_buff_off_ += size; - return static_cast(size); - } else { - memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); - read_buff_off_ += remaining_size; - return static_cast(remaining_size); - } - } - - if (!is_readable()) { return -1; } - - read_buff_off_ = 0; - read_buff_content_size_ = 0; - - if (size < read_buff_size_) { - auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, - CPPHTTPLIB_RECV_FLAGS); - if (n <= 0) { - return n; - } else if (n <= static_cast(size)) { - memcpy(ptr, read_buff_.data(), static_cast(n)); - return n; - } else { - memcpy(ptr, read_buff_.data(), size); - read_buff_off_ = size; - read_buff_content_size_ = static_cast(n); - return static_cast(size); - } - } else { - return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); - } -} - -inline ssize_t SocketStream::write(const char *ptr, size_t size) { - if (!is_writable()) { return -1; } - -#if defined(_WIN32) && !defined(_WIN64) - size = - (std::min)(size, static_cast((std::numeric_limits::max)())); -#endif - - return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); -} - -inline void SocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { - return detail::get_remote_ip_and_port(sock_, ip, port); -} - -inline void SocketStream::get_local_ip_and_port(std::string &ip, - int &port) const { - return detail::get_local_ip_and_port(sock_, ip, port); -} - -inline socket_t SocketStream::socket() const { return sock_; } - -// Buffer stream implementation -inline bool BufferStream::is_readable() const { return true; } - -inline bool BufferStream::is_writable() const { return true; } - -inline ssize_t BufferStream::read(char *ptr, size_t size) { -#if defined(_MSC_VER) && _MSC_VER < 1910 - auto len_read = buffer._Copy_s(ptr, size, size, position); -#else - auto len_read = buffer.copy(ptr, size, position); -#endif - position += static_cast(len_read); - return static_cast(len_read); -} - -inline ssize_t BufferStream::write(const char *ptr, size_t size) { - buffer.append(ptr, size); - return static_cast(size); -} - -inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, - int & /*port*/) const {} - -inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, - int & /*port*/) const {} - -inline socket_t BufferStream::socket() const { return 0; } - -inline const std::string &BufferStream::get_buffer() const { return buffer; } - -inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { - static constexpr char marker[] = "/:"; - - // One past the last ending position of a path param substring - std::size_t last_param_end = 0; - -#ifndef CPPHTTPLIB_NO_EXCEPTIONS - // Needed to ensure that parameter names are unique during matcher - // construction - // If exceptions are disabled, only last duplicate path - // parameter will be set - std::unordered_set param_name_set; -#endif - - while (true) { - const auto marker_pos = pattern.find( - marker, last_param_end == 0 ? last_param_end : last_param_end - 1); - if (marker_pos == std::string::npos) { break; } - - static_fragments_.push_back( - pattern.substr(last_param_end, marker_pos - last_param_end + 1)); - - const auto param_name_start = marker_pos + 2; - - auto sep_pos = pattern.find(separator, param_name_start); - if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } - - auto param_name = - pattern.substr(param_name_start, sep_pos - param_name_start); - -#ifndef CPPHTTPLIB_NO_EXCEPTIONS - if (param_name_set.find(param_name) != param_name_set.cend()) { - std::string msg = "Encountered path parameter '" + param_name + - "' multiple times in route pattern '" + pattern + "'."; - throw std::invalid_argument(msg); - } -#endif - - param_names_.push_back(std::move(param_name)); - - last_param_end = sep_pos + 1; - } - - if (last_param_end < pattern.length()) { - static_fragments_.push_back(pattern.substr(last_param_end)); - } -} - -inline bool PathParamsMatcher::match(Request &request) const { - request.matches = std::smatch(); - request.path_params.clear(); - request.path_params.reserve(param_names_.size()); - - // One past the position at which the path matched the pattern last time - std::size_t starting_pos = 0; - for (size_t i = 0; i < static_fragments_.size(); ++i) { - const auto &fragment = static_fragments_[i]; - - if (starting_pos + fragment.length() > request.path.length()) { - return false; - } - - // Avoid unnecessary allocation by using strncmp instead of substr + - // comparison - if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(), - fragment.length()) != 0) { - return false; - } - - starting_pos += fragment.length(); - - // Should only happen when we have a static fragment after a param - // Example: '/users/:id/subscriptions' - // The 'subscriptions' fragment here does not have a corresponding param - if (i >= param_names_.size()) { continue; } - - auto sep_pos = request.path.find(separator, starting_pos); - if (sep_pos == std::string::npos) { sep_pos = request.path.length(); } - - const auto ¶m_name = param_names_[i]; - - request.path_params.emplace( - param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); - - // Mark everything up to '/' as matched - starting_pos = sep_pos + 1; - } - // Returns false if the path is longer than the pattern - return starting_pos >= request.path.length(); -} - -inline bool RegexMatcher::match(Request &request) const { - request.path_params.clear(); - return std::regex_match(request.path, request.matches, regex_); -} - -} // namespace detail - -// HTTP server implementation -inline Server::Server() - : new_task_queue( - [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { -#ifndef _WIN32 - signal(SIGPIPE, SIG_IGN); -#endif -} - -inline Server::~Server() = default; - -inline std::unique_ptr -Server::make_matcher(const std::string &pattern) { - if (pattern.find("/:") != std::string::npos) { - return detail::make_unique(pattern); - } else { - return detail::make_unique(pattern); - } -} - -inline Server &Server::Get(const std::string &pattern, Handler handler) { - get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Post(const std::string &pattern, Handler handler) { - post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Post(const std::string &pattern, - HandlerWithContentReader handler) { - post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Put(const std::string &pattern, Handler handler) { - put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Put(const std::string &pattern, - HandlerWithContentReader handler) { - put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Patch(const std::string &pattern, Handler handler) { - patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Patch(const std::string &pattern, - HandlerWithContentReader handler) { - patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Delete(const std::string &pattern, Handler handler) { - delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Delete(const std::string &pattern, - HandlerWithContentReader handler) { - delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Options(const std::string &pattern, Handler handler) { - options_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline bool Server::set_base_dir(const std::string &dir, - const std::string &mount_point) { - return set_mount_point(mount_point, dir); -} - -inline bool Server::set_mount_point(const std::string &mount_point, - const std::string &dir, Headers headers) { - detail::FileStat stat(dir); - if (stat.is_dir()) { - std::string mnt = !mount_point.empty() ? mount_point : "/"; - if (!mnt.empty() && mnt[0] == '/') { - base_dirs_.push_back({mnt, dir, std::move(headers)}); - return true; - } - } - return false; -} - -inline bool Server::remove_mount_point(const std::string &mount_point) { - for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { - if (it->mount_point == mount_point) { - base_dirs_.erase(it); - return true; - } - } - return false; -} - -inline Server & -Server::set_file_extension_and_mimetype_mapping(const std::string &ext, - const std::string &mime) { - file_extension_and_mimetype_map_[ext] = mime; - return *this; -} - -inline Server &Server::set_default_file_mimetype(const std::string &mime) { - default_file_mimetype_ = mime; - return *this; -} - -inline Server &Server::set_file_request_handler(Handler handler) { - file_request_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_error_handler_core(HandlerWithResponse handler, - std::true_type) { - error_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_error_handler_core(Handler handler, - std::false_type) { - error_handler_ = [handler](const Request &req, Response &res) { - handler(req, res); - return HandlerResponse::Handled; - }; - return *this; -} - -inline Server &Server::set_exception_handler(ExceptionHandler handler) { - exception_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { - pre_routing_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_post_routing_handler(Handler handler) { - post_routing_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_logger(Logger logger) { - logger_ = std::move(logger); - return *this; -} - -inline Server & -Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { - expect_100_continue_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_address_family(int family) { - address_family_ = family; - return *this; -} - -inline Server &Server::set_tcp_nodelay(bool on) { - tcp_nodelay_ = on; - return *this; -} - -inline Server &Server::set_ipv6_v6only(bool on) { - ipv6_v6only_ = on; - return *this; -} - -inline Server &Server::set_socket_options(SocketOptions socket_options) { - socket_options_ = std::move(socket_options); - return *this; -} - -inline Server &Server::set_default_headers(Headers headers) { - default_headers_ = std::move(headers); - return *this; -} - -inline Server &Server::set_header_writer( - std::function const &writer) { - header_writer_ = writer; - return *this; -} - -inline Server &Server::set_keep_alive_max_count(size_t count) { - keep_alive_max_count_ = count; - return *this; -} - -inline Server &Server::set_keep_alive_timeout(time_t sec) { - keep_alive_timeout_sec_ = sec; - return *this; -} - -inline Server &Server::set_read_timeout(time_t sec, time_t usec) { - read_timeout_sec_ = sec; - read_timeout_usec_ = usec; - return *this; -} - -inline Server &Server::set_write_timeout(time_t sec, time_t usec) { - write_timeout_sec_ = sec; - write_timeout_usec_ = usec; - return *this; -} - -inline Server &Server::set_idle_interval(time_t sec, time_t usec) { - idle_interval_sec_ = sec; - idle_interval_usec_ = usec; - return *this; -} - -inline Server &Server::set_payload_max_length(size_t length) { - payload_max_length_ = length; - return *this; -} - -inline bool Server::bind_to_port(const std::string &host, int port, - int socket_flags) { - auto ret = bind_internal(host, port, socket_flags); - if (ret == -1) { is_decommisioned = true; } - return ret >= 0; -} -inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { - auto ret = bind_internal(host, 0, socket_flags); - if (ret == -1) { is_decommisioned = true; } - return ret; -} - -inline bool Server::listen_after_bind() { return listen_internal(); } - -inline bool Server::listen(const std::string &host, int port, - int socket_flags) { - return bind_to_port(host, port, socket_flags) && listen_internal(); -} - -inline bool Server::is_running() const { return is_running_; } - -inline void Server::wait_until_ready() const { - while (!is_running_ && !is_decommisioned) { - std::this_thread::sleep_for(std::chrono::milliseconds{1}); - } -} - -inline void Server::stop() { - if (is_running_) { - assert(svr_sock_ != INVALID_SOCKET); - std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); - detail::shutdown_socket(sock); - detail::close_socket(sock); - } - is_decommisioned = false; -} - -inline void Server::decommission() { is_decommisioned = true; } - -inline bool Server::parse_request_line(const char *s, Request &req) const { - auto len = strlen(s); - if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } - len -= 2; - - { - size_t count = 0; - - detail::split(s, s + len, ' ', [&](const char *b, const char *e) { - switch (count) { - case 0: req.method = std::string(b, e); break; - case 1: req.target = std::string(b, e); break; - case 2: req.version = std::string(b, e); break; - default: break; - } - count++; - }); - - if (count != 3) { return false; } - } - - static const std::set methods{ - "GET", "HEAD", "POST", "PUT", "DELETE", - "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; - - if (methods.find(req.method) == methods.end()) { return false; } - - if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } - - { - // Skip URL fragment - for (size_t i = 0; i < req.target.size(); i++) { - if (req.target[i] == '#') { - req.target.erase(i); - break; - } - } - - detail::divide(req.target, '?', - [&](const char *lhs_data, std::size_t lhs_size, - const char *rhs_data, std::size_t rhs_size) { - req.path = detail::decode_url( - std::string(lhs_data, lhs_size), false); - detail::parse_query_text(rhs_data, rhs_size, req.params); - }); - } - - return true; -} - -inline bool Server::write_response(Stream &strm, bool close_connection, - Request &req, Response &res) { - // NOTE: `req.ranges` should be empty, otherwise it will be applied - // incorrectly to the error content. - req.ranges.clear(); - return write_response_core(strm, close_connection, req, res, false); -} - -inline bool Server::write_response_with_content(Stream &strm, - bool close_connection, - const Request &req, - Response &res) { - return write_response_core(strm, close_connection, req, res, true); -} - -inline bool Server::write_response_core(Stream &strm, bool close_connection, - const Request &req, Response &res, - bool need_apply_ranges) { - assert(res.status != -1); - - if (400 <= res.status && error_handler_ && - error_handler_(req, res) == HandlerResponse::Handled) { - need_apply_ranges = true; - } - - std::string content_type; - std::string boundary; - if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } - - // Prepare additional headers - if (close_connection || req.get_header_value("Connection") == "close") { - res.set_header("Connection", "close"); - } else { - std::string s = "timeout="; - s += std::to_string(keep_alive_timeout_sec_); - s += ", max="; - s += std::to_string(keep_alive_max_count_); - res.set_header("Keep-Alive", s); - } - - if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) && - !res.has_header("Content-Type")) { - res.set_header("Content-Type", "text/plain"); - } - - if (res.body.empty() && !res.content_length_ && !res.content_provider_ && - !res.has_header("Content-Length")) { - res.set_header("Content-Length", "0"); - } - - if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) { - res.set_header("Accept-Ranges", "bytes"); - } - - if (post_routing_handler_) { post_routing_handler_(req, res); } - - // Response line and headers - { - detail::BufferStream bstrm; - if (!detail::write_response_line(bstrm, res.status)) { return false; } - if (!header_writer_(bstrm, res.headers)) { return false; } - - // Flush buffer - auto &data = bstrm.get_buffer(); - detail::write_data(strm, data.data(), data.size()); - } - - // Body - auto ret = true; - if (req.method != "HEAD") { - if (!res.body.empty()) { - if (!detail::write_data(strm, res.body.data(), res.body.size())) { - ret = false; - } - } else if (res.content_provider_) { - if (write_content_with_provider(strm, req, res, boundary, content_type)) { - res.content_provider_success_ = true; - } else { - ret = false; - } - } - } - - // Log - if (logger_) { logger_(req, res); } - - return ret; -} - -inline bool -Server::write_content_with_provider(Stream &strm, const Request &req, - Response &res, const std::string &boundary, - const std::string &content_type) { - auto is_shutting_down = [this]() { - return this->svr_sock_ == INVALID_SOCKET; - }; - - if (res.content_length_ > 0) { - if (req.ranges.empty()) { - return detail::write_content(strm, res.content_provider_, 0, - res.content_length_, is_shutting_down); - } else if (req.ranges.size() == 1) { - auto offset_and_length = detail::get_range_offset_and_length( - req.ranges[0], res.content_length_); - - return detail::write_content(strm, res.content_provider_, - offset_and_length.first, - offset_and_length.second, is_shutting_down); - } else { - return detail::write_multipart_ranges_data( - strm, req, res, boundary, content_type, res.content_length_, - is_shutting_down); - } - } else { - if (res.is_chunked_content_provider_) { - auto type = detail::encoding_type(req, res); - - std::unique_ptr compressor; - if (type == detail::EncodingType::Gzip) { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - compressor = detail::make_unique(); -#endif - } else if (type == detail::EncodingType::Brotli) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - compressor = detail::make_unique(); -#endif - } else { - compressor = detail::make_unique(); - } - assert(compressor != nullptr); - - return detail::write_content_chunked(strm, res.content_provider_, - is_shutting_down, *compressor); - } else { - return detail::write_content_without_length(strm, res.content_provider_, - is_shutting_down); - } - } -} - -inline bool Server::read_content(Stream &strm, Request &req, Response &res) { - MultipartFormDataMap::iterator cur; - auto file_count = 0; - if (read_content_core( - strm, req, res, - // Regular - [&](const char *buf, size_t n) { - if (req.body.size() + n > req.body.max_size()) { return false; } - req.body.append(buf, n); - return true; - }, - // Multipart - [&](const MultipartFormData &file) { - if (file_count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { - return false; - } - cur = req.files.emplace(file.name, file); - return true; - }, - [&](const char *buf, size_t n) { - auto &content = cur->second.content; - if (content.size() + n > content.max_size()) { return false; } - content.append(buf, n); - return true; - })) { - const auto &content_type = req.get_header_value("Content-Type"); - if (!content_type.find("application/x-www-form-urlencoded")) { - if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { - res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? - return false; - } - detail::parse_query_text(req.body, req.params); - } - return true; - } - return false; -} - -inline bool Server::read_content_with_content_receiver( - Stream &strm, Request &req, Response &res, ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) { - return read_content_core(strm, req, res, std::move(receiver), - std::move(multipart_header), - std::move(multipart_receiver)); -} - -inline bool -Server::read_content_core(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) const { - detail::MultipartFormDataParser multipart_form_data_parser; - ContentReceiverWithProgress out; - - if (req.is_multipart_form_data()) { - const auto &content_type = req.get_header_value("Content-Type"); - std::string boundary; - if (!detail::parse_multipart_boundary(content_type, boundary)) { - res.status = StatusCode::BadRequest_400; - return false; - } - - multipart_form_data_parser.set_boundary(std::move(boundary)); - out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { - /* For debug - size_t pos = 0; - while (pos < n) { - auto read_size = (std::min)(1, n - pos); - auto ret = multipart_form_data_parser.parse( - buf + pos, read_size, multipart_receiver, multipart_header); - if (!ret) { return false; } - pos += read_size; - } - return true; - */ - return multipart_form_data_parser.parse(buf, n, multipart_receiver, - multipart_header); - }; - } else { - out = [receiver](const char *buf, size_t n, uint64_t /*off*/, - uint64_t /*len*/) { return receiver(buf, n); }; - } - - if (req.method == "DELETE" && !req.has_header("Content-Length")) { - return true; - } - - if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, - out, true)) { - return false; - } - - if (req.is_multipart_form_data()) { - if (!multipart_form_data_parser.is_valid()) { - res.status = StatusCode::BadRequest_400; - return false; - } - } - - return true; -} - -inline bool Server::handle_file_request(const Request &req, Response &res, - bool head) { - for (const auto &entry : base_dirs_) { - // Prefix match - if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { - std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); - if (detail::is_valid_path(sub_path)) { - auto path = entry.base_dir + sub_path; - if (path.back() == '/') { path += "index.html"; } - - detail::FileStat stat(path); - - if (stat.is_dir()) { - res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); - return true; - } - - if (stat.is_file()) { - for (const auto &kv : entry.headers) { - res.set_header(kv.first, kv.second); - } - - auto mm = std::make_shared(path.c_str()); - if (!mm->is_open()) { return false; } - - res.set_content_provider( - mm->size(), - detail::find_content_type(path, file_extension_and_mimetype_map_, - default_file_mimetype_), - [mm](size_t offset, size_t length, DataSink &sink) -> bool { - sink.write(mm->data() + offset, length); - return true; - }); - - if (!head && file_request_handler_) { - file_request_handler_(req, res); - } - - return true; - } - } - } - } - return false; -} - -inline socket_t -Server::create_server_socket(const std::string &host, int port, - int socket_flags, - SocketOptions socket_options) const { - return detail::create_socket( - host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, - ipv6_v6only_, std::move(socket_options), - [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { - if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { - return false; - } - if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } - return true; - }); -} - -inline int Server::bind_internal(const std::string &host, int port, - int socket_flags) { - if (is_decommisioned) { return -1; } - - if (!is_valid()) { return -1; } - - svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); - if (svr_sock_ == INVALID_SOCKET) { return -1; } - - if (port == 0) { - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); - if (getsockname(svr_sock_, reinterpret_cast(&addr), - &addr_len) == -1) { - return -1; - } - if (addr.ss_family == AF_INET) { - return ntohs(reinterpret_cast(&addr)->sin_port); - } else if (addr.ss_family == AF_INET6) { - return ntohs(reinterpret_cast(&addr)->sin6_port); - } else { - return -1; - } - } else { - return port; - } -} - -inline bool Server::listen_internal() { - if (is_decommisioned) { return false; } - - auto ret = true; - is_running_ = true; - auto se = detail::scope_exit([&]() { is_running_ = false; }); - - { - std::unique_ptr task_queue(new_task_queue()); - - while (svr_sock_ != INVALID_SOCKET) { -#ifndef _WIN32 - if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { -#endif - auto val = detail::select_read(svr_sock_, idle_interval_sec_, - idle_interval_usec_); - if (val == 0) { // Timeout - task_queue->on_idle(); - continue; - } -#ifndef _WIN32 - } -#endif - -#if defined _WIN32 - // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, - // OVERLAPPED - socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); -#elif defined SOCK_CLOEXEC - socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); -#else - socket_t sock = accept(svr_sock_, nullptr, nullptr); -#endif - - if (sock == INVALID_SOCKET) { - if (errno == EMFILE) { - // The per-process limit of open file descriptors has been reached. - // Try to accept new connections after a short sleep. - std::this_thread::sleep_for(std::chrono::microseconds{1}); - continue; - } else if (errno == EINTR || errno == EAGAIN) { - continue; - } - if (svr_sock_ != INVALID_SOCKET) { - detail::close_socket(svr_sock_); - ret = false; - } else { - ; // The server socket was closed by user. - } - break; - } - - { -#ifdef _WIN32 - auto timeout = static_cast(read_timeout_sec_ * 1000 + - read_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(read_timeout_sec_); - tv.tv_usec = static_cast(read_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - { - -#ifdef _WIN32 - auto timeout = static_cast(write_timeout_sec_ * 1000 + - write_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(write_timeout_sec_); - tv.tv_usec = static_cast(write_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - - if (!task_queue->enqueue( - [this, sock]() { process_and_close_socket(sock); })) { - detail::shutdown_socket(sock); - detail::close_socket(sock); - } - } - - task_queue->shutdown(); - } - - is_decommisioned = !ret; - return ret; -} - -inline bool Server::routing(Request &req, Response &res, Stream &strm) { - if (pre_routing_handler_ && - pre_routing_handler_(req, res) == HandlerResponse::Handled) { - return true; - } - - // File handler - auto is_head_request = req.method == "HEAD"; - if ((req.method == "GET" || is_head_request) && - handle_file_request(req, res, is_head_request)) { - return true; - } - - if (detail::expect_content(req)) { - // Content reader handler - { - ContentReader reader( - [&](ContentReceiver receiver) { - return read_content_with_content_receiver( - strm, req, res, std::move(receiver), nullptr, nullptr); - }, - [&](MultipartContentHeader header, ContentReceiver receiver) { - return read_content_with_content_receiver(strm, req, res, nullptr, - std::move(header), - std::move(receiver)); - }); - - if (req.method == "POST") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - post_handlers_for_content_reader_)) { - return true; - } - } else if (req.method == "PUT") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - put_handlers_for_content_reader_)) { - return true; - } - } else if (req.method == "PATCH") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - patch_handlers_for_content_reader_)) { - return true; - } - } else if (req.method == "DELETE") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - delete_handlers_for_content_reader_)) { - return true; - } - } - } - - // Read content into `req.body` - if (!read_content(strm, req, res)) { return false; } - } - - // Regular handler - if (req.method == "GET" || req.method == "HEAD") { - return dispatch_request(req, res, get_handlers_); - } else if (req.method == "POST") { - return dispatch_request(req, res, post_handlers_); - } else if (req.method == "PUT") { - return dispatch_request(req, res, put_handlers_); - } else if (req.method == "DELETE") { - return dispatch_request(req, res, delete_handlers_); - } else if (req.method == "OPTIONS") { - return dispatch_request(req, res, options_handlers_); - } else if (req.method == "PATCH") { - return dispatch_request(req, res, patch_handlers_); - } - - res.status = StatusCode::BadRequest_400; - return false; -} - -inline bool Server::dispatch_request(Request &req, Response &res, - const Handlers &handlers) const { - for (const auto &x : handlers) { - const auto &matcher = x.first; - const auto &handler = x.second; - - if (matcher->match(req)) { - handler(req, res); - return true; - } - } - return false; -} - -inline void Server::apply_ranges(const Request &req, Response &res, - std::string &content_type, - std::string &boundary) const { - if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) { - auto it = res.headers.find("Content-Type"); - if (it != res.headers.end()) { - content_type = it->second; - res.headers.erase(it); - } - - boundary = detail::make_multipart_data_boundary(); - - res.set_header("Content-Type", - "multipart/byteranges; boundary=" + boundary); - } - - auto type = detail::encoding_type(req, res); - - if (res.body.empty()) { - if (res.content_length_ > 0) { - size_t length = 0; - if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { - length = res.content_length_; - } else if (req.ranges.size() == 1) { - auto offset_and_length = detail::get_range_offset_and_length( - req.ranges[0], res.content_length_); - - length = offset_and_length.second; - - auto content_range = detail::make_content_range_header_field( - offset_and_length, res.content_length_); - res.set_header("Content-Range", content_range); - } else { - length = detail::get_multipart_ranges_data_length( - req, boundary, content_type, res.content_length_); - } - res.set_header("Content-Length", std::to_string(length)); - } else { - if (res.content_provider_) { - if (res.is_chunked_content_provider_) { - res.set_header("Transfer-Encoding", "chunked"); - if (type == detail::EncodingType::Gzip) { - res.set_header("Content-Encoding", "gzip"); - } else if (type == detail::EncodingType::Brotli) { - res.set_header("Content-Encoding", "br"); - } - } - } - } - } else { - if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { - ; - } else if (req.ranges.size() == 1) { - auto offset_and_length = - detail::get_range_offset_and_length(req.ranges[0], res.body.size()); - auto offset = offset_and_length.first; - auto length = offset_and_length.second; - - auto content_range = detail::make_content_range_header_field( - offset_and_length, res.body.size()); - res.set_header("Content-Range", content_range); - - assert(offset + length <= res.body.size()); - res.body = res.body.substr(offset, length); - } else { - std::string data; - detail::make_multipart_ranges_data(req, res, boundary, content_type, - res.body.size(), data); - res.body.swap(data); - } - - if (type != detail::EncodingType::None) { - std::unique_ptr compressor; - std::string content_encoding; - - if (type == detail::EncodingType::Gzip) { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - compressor = detail::make_unique(); - content_encoding = "gzip"; -#endif - } else if (type == detail::EncodingType::Brotli) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - compressor = detail::make_unique(); - content_encoding = "br"; -#endif - } - - if (compressor) { - std::string compressed; - if (compressor->compress(res.body.data(), res.body.size(), true, - [&](const char *data, size_t data_len) { - compressed.append(data, data_len); - return true; - })) { - res.body.swap(compressed); - res.set_header("Content-Encoding", content_encoding); - } - } - } - - auto length = std::to_string(res.body.size()); - res.set_header("Content-Length", length); - } -} - -inline bool Server::dispatch_request_for_content_reader( - Request &req, Response &res, ContentReader content_reader, - const HandlersForContentReader &handlers) const { - for (const auto &x : handlers) { - const auto &matcher = x.first; - const auto &handler = x.second; - - if (matcher->match(req)) { - handler(req, res, content_reader); - return true; - } - } - return false; -} - -inline bool -Server::process_request(Stream &strm, const std::string &remote_addr, - int remote_port, const std::string &local_addr, - int local_port, bool close_connection, - bool &connection_closed, - const std::function &setup_request) { - std::array buf{}; - - detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); - - // Connection has been closed on client - if (!line_reader.getline()) { return false; } - - Request req; - - Response res; - res.version = "HTTP/1.1"; - res.headers = default_headers_; - -#ifdef _WIN32 - // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). -#else -#ifndef CPPHTTPLIB_USE_POLL - // Socket file descriptor exceeded FD_SETSIZE... - if (strm.socket() >= FD_SETSIZE) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::InternalServerError_500; - return write_response(strm, close_connection, req, res); - } -#endif -#endif - - // Check if the request URI doesn't exceed the limit - if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::UriTooLong_414; - return write_response(strm, close_connection, req, res); - } - - // Request line and headers - if (!parse_request_line(line_reader.ptr(), req) || - !detail::read_headers(strm, req.headers)) { - res.status = StatusCode::BadRequest_400; - return write_response(strm, close_connection, req, res); - } - - if (req.get_header_value("Connection") == "close") { - connection_closed = true; - } - - if (req.version == "HTTP/1.0" && - req.get_header_value("Connection") != "Keep-Alive") { - connection_closed = true; - } - - req.remote_addr = remote_addr; - req.remote_port = remote_port; - req.set_header("REMOTE_ADDR", req.remote_addr); - req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); - - req.local_addr = local_addr; - req.local_port = local_port; - req.set_header("LOCAL_ADDR", req.local_addr); - req.set_header("LOCAL_PORT", std::to_string(req.local_port)); - - if (req.has_header("Range")) { - const auto &range_header_value = req.get_header_value("Range"); - if (!detail::parse_range_header(range_header_value, req.ranges)) { - res.status = StatusCode::RangeNotSatisfiable_416; - return write_response(strm, close_connection, req, res); - } - } - - if (setup_request) { setup_request(req); } - - if (req.get_header_value("Expect") == "100-continue") { - int status = StatusCode::Continue_100; - if (expect_100_continue_handler_) { - status = expect_100_continue_handler_(req, res); - } - switch (status) { - case StatusCode::Continue_100: - case StatusCode::ExpectationFailed_417: - detail::write_response_line(strm, status); - strm.write("\r\n"); - break; - default: - connection_closed = true; - return write_response(strm, true, req, res); - } - } - - // Routing - auto routed = false; -#ifdef CPPHTTPLIB_NO_EXCEPTIONS - routed = routing(req, res, strm); -#else - try { - routed = routing(req, res, strm); - } catch (std::exception &e) { - if (exception_handler_) { - auto ep = std::current_exception(); - exception_handler_(req, res, ep); - routed = true; - } else { - res.status = StatusCode::InternalServerError_500; - std::string val; - auto s = e.what(); - for (size_t i = 0; s[i]; i++) { - switch (s[i]) { - case '\r': val += "\\r"; break; - case '\n': val += "\\n"; break; - default: val += s[i]; break; - } - } - res.set_header("EXCEPTION_WHAT", val); - } - } catch (...) { - if (exception_handler_) { - auto ep = std::current_exception(); - exception_handler_(req, res, ep); - routed = true; - } else { - res.status = StatusCode::InternalServerError_500; - res.set_header("EXCEPTION_WHAT", "UNKNOWN"); - } - } -#endif - if (routed) { - if (res.status == -1) { - res.status = req.ranges.empty() ? StatusCode::OK_200 - : StatusCode::PartialContent_206; - } - - if (detail::range_error(req, res)) { - res.body.clear(); - res.content_length_ = 0; - res.content_provider_ = nullptr; - res.status = StatusCode::RangeNotSatisfiable_416; - return write_response(strm, close_connection, req, res); - } - - // Serve file content by using a content provider - if (!res.file_content_path_.empty()) { - const auto &path = res.file_content_path_; - auto mm = std::make_shared(path.c_str()); - if (!mm->is_open()) { - res.body.clear(); - res.content_length_ = 0; - res.content_provider_ = nullptr; - res.status = StatusCode::NotFound_404; - return write_response(strm, close_connection, req, res); - } - - auto content_type = res.file_content_content_type_; - if (content_type.empty()) { - content_type = detail::find_content_type( - path, file_extension_and_mimetype_map_, default_file_mimetype_); - } - - res.set_content_provider( - mm->size(), content_type, - [mm](size_t offset, size_t length, DataSink &sink) -> bool { - sink.write(mm->data() + offset, length); - return true; - }); - } - - return write_response_with_content(strm, close_connection, req, res); - } else { - if (res.status == -1) { res.status = StatusCode::NotFound_404; } - - return write_response(strm, close_connection, req, res); - } -} - -inline bool Server::is_valid() const { return true; } - -inline bool Server::process_and_close_socket(socket_t sock) { - std::string remote_addr; - int remote_port = 0; - detail::get_remote_ip_and_port(sock, remote_addr, remote_port); - - std::string local_addr; - int local_port = 0; - detail::get_local_ip_and_port(sock, local_addr, local_port); - - auto ret = detail::process_server_socket( - svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, - [&](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, remote_addr, remote_port, local_addr, - local_port, close_connection, connection_closed, - nullptr); - }); - - detail::shutdown_socket(sock); - detail::close_socket(sock); - return ret; -} - -// HTTP client implementation -inline ClientImpl::ClientImpl(const std::string &host) - : ClientImpl(host, 80, std::string(), std::string()) {} - -inline ClientImpl::ClientImpl(const std::string &host, int port) - : ClientImpl(host, port, std::string(), std::string()) {} - -inline ClientImpl::ClientImpl(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) - : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), - host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), - client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} - -inline ClientImpl::~ClientImpl() { - std::lock_guard guard(socket_mutex_); - shutdown_socket(socket_); - close_socket(socket_); -} - -inline bool ClientImpl::is_valid() const { return true; } - -inline void ClientImpl::copy_settings(const ClientImpl &rhs) { - client_cert_path_ = rhs.client_cert_path_; - client_key_path_ = rhs.client_key_path_; - connection_timeout_sec_ = rhs.connection_timeout_sec_; - read_timeout_sec_ = rhs.read_timeout_sec_; - read_timeout_usec_ = rhs.read_timeout_usec_; - write_timeout_sec_ = rhs.write_timeout_sec_; - write_timeout_usec_ = rhs.write_timeout_usec_; - basic_auth_username_ = rhs.basic_auth_username_; - basic_auth_password_ = rhs.basic_auth_password_; - bearer_token_auth_token_ = rhs.bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - digest_auth_username_ = rhs.digest_auth_username_; - digest_auth_password_ = rhs.digest_auth_password_; -#endif - keep_alive_ = rhs.keep_alive_; - follow_location_ = rhs.follow_location_; - url_encode_ = rhs.url_encode_; - address_family_ = rhs.address_family_; - tcp_nodelay_ = rhs.tcp_nodelay_; - ipv6_v6only_ = rhs.ipv6_v6only_; - socket_options_ = rhs.socket_options_; - compress_ = rhs.compress_; - decompress_ = rhs.decompress_; - interface_ = rhs.interface_; - proxy_host_ = rhs.proxy_host_; - proxy_port_ = rhs.proxy_port_; - proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; - proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; - proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; - proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - ca_cert_file_path_ = rhs.ca_cert_file_path_; - ca_cert_dir_path_ = rhs.ca_cert_dir_path_; - ca_cert_store_ = rhs.ca_cert_store_; -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - server_certificate_verification_ = rhs.server_certificate_verification_; - server_hostname_verification_ = rhs.server_hostname_verification_; - server_certificate_verifier_ = rhs.server_certificate_verifier_; -#endif - logger_ = rhs.logger_; -} - -inline socket_t ClientImpl::create_client_socket(Error &error) const { - if (!proxy_host_.empty() && proxy_port_ != -1) { - return detail::create_client_socket( - proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, - ipv6_v6only_, socket_options_, connection_timeout_sec_, - connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, interface_, error); - } - - // Check is custom IP specified for host_ - std::string ip; - auto it = addr_map_.find(host_); - if (it != addr_map_.end()) { ip = it->second; } - - return detail::create_client_socket( - host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_, - socket_options_, connection_timeout_sec_, connection_timeout_usec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, interface_, error); -} - -inline bool ClientImpl::create_and_connect_socket(Socket &socket, - Error &error) { - auto sock = create_client_socket(error); - if (sock == INVALID_SOCKET) { return false; } - socket.sock = sock; - return true; -} - -inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, - bool /*shutdown_gracefully*/) { - // If there are any requests in flight from threads other than us, then it's - // a thread-unsafe race because individual ssl* objects are not thread-safe. - assert(socket_requests_in_flight_ == 0 || - socket_requests_are_from_thread_ == std::this_thread::get_id()); -} - -inline void ClientImpl::shutdown_socket(Socket &socket) const { - if (socket.sock == INVALID_SOCKET) { return; } - detail::shutdown_socket(socket.sock); -} - -inline void ClientImpl::close_socket(Socket &socket) { - // If there are requests in flight in another thread, usually closing - // the socket will be fine and they will simply receive an error when - // using the closed socket, but it is still a bug since rarely the OS - // may reassign the socket id to be used for a new socket, and then - // suddenly they will be operating on a live socket that is different - // than the one they intended! - assert(socket_requests_in_flight_ == 0 || - socket_requests_are_from_thread_ == std::this_thread::get_id()); - - // It is also a bug if this happens while SSL is still active -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - assert(socket.ssl == nullptr); -#endif - if (socket.sock == INVALID_SOCKET) { return; } - detail::close_socket(socket.sock); - socket.sock = INVALID_SOCKET; -} - -inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, - Response &res) const { - std::array buf{}; - - detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); - - if (!line_reader.getline()) { return false; } - -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); -#else - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); -#endif - - std::cmatch m; - if (!std::regex_match(line_reader.ptr(), m, re)) { - return req.method == "CONNECT"; - } - res.version = std::string(m[1]); - res.status = std::stoi(std::string(m[2])); - res.reason = std::string(m[3]); - - // Ignore '100 Continue' - while (res.status == StatusCode::Continue_100) { - if (!line_reader.getline()) { return false; } // CRLF - if (!line_reader.getline()) { return false; } // next response line - - if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } - res.version = std::string(m[1]); - res.status = std::stoi(std::string(m[2])); - res.reason = std::string(m[3]); - } - - return true; -} - -inline bool ClientImpl::send(Request &req, Response &res, Error &error) { - std::lock_guard request_mutex_guard(request_mutex_); - auto ret = send_(req, res, error); - if (error == Error::SSLPeerCouldBeClosed_) { - assert(!ret); - ret = send_(req, res, error); - } - return ret; -} - -inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { - { - std::lock_guard guard(socket_mutex_); - - // Set this to false immediately - if it ever gets set to true by the end of - // the request, we know another thread instructed us to close the socket. - socket_should_be_closed_when_request_is_done_ = false; - - auto is_alive = false; - if (socket_.is_open()) { - is_alive = detail::is_socket_alive(socket_.sock); - if (!is_alive) { - // Attempt to avoid sigpipe by shutting down nongracefully if it seems - // like the other side has already closed the connection Also, there - // cannot be any requests in flight from other threads since we locked - // request_mutex_, so safe to close everything immediately - const bool shutdown_gracefully = false; - shutdown_ssl(socket_, shutdown_gracefully); - shutdown_socket(socket_); - close_socket(socket_); - } - } - - if (!is_alive) { - if (!create_and_connect_socket(socket_, error)) { return false; } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - // TODO: refactoring - if (is_ssl()) { - auto &scli = static_cast(*this); - if (!proxy_host_.empty() && proxy_port_ != -1) { - auto success = false; - if (!scli.connect_with_proxy(socket_, res, success, error)) { - return success; - } - } - - if (!scli.initialize_ssl(socket_, error)) { return false; } - } -#endif - } - - // Mark the current socket as being in use so that it cannot be closed by - // anyone else while this request is ongoing, even though we will be - // releasing the mutex. - if (socket_requests_in_flight_ > 1) { - assert(socket_requests_are_from_thread_ == std::this_thread::get_id()); - } - socket_requests_in_flight_ += 1; - socket_requests_are_from_thread_ = std::this_thread::get_id(); - } - - for (const auto &header : default_headers_) { - if (req.headers.find(header.first) == req.headers.end()) { - req.headers.insert(header); - } - } - - auto ret = false; - auto close_connection = !keep_alive_; - - auto se = detail::scope_exit([&]() { - // Briefly lock mutex in order to mark that a request is no longer ongoing - std::lock_guard guard(socket_mutex_); - socket_requests_in_flight_ -= 1; - if (socket_requests_in_flight_ <= 0) { - assert(socket_requests_in_flight_ == 0); - socket_requests_are_from_thread_ = std::thread::id(); - } - - if (socket_should_be_closed_when_request_is_done_ || close_connection || - !ret) { - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); - } - }); - - ret = process_socket(socket_, [&](Stream &strm) { - return handle_request(strm, req, res, close_connection, error); - }); - - if (!ret) { - if (error == Error::Success) { error = Error::Unknown; } - } - - return ret; -} - -inline Result ClientImpl::send(const Request &req) { - auto req2 = req; - return send_(std::move(req2)); -} - -inline Result ClientImpl::send_(Request &&req) { - auto res = detail::make_unique(); - auto error = Error::Success; - auto ret = send(req, *res, error); - return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; -} - -inline bool ClientImpl::handle_request(Stream &strm, Request &req, - Response &res, bool close_connection, - Error &error) { - if (req.path.empty()) { - error = Error::Connection; - return false; - } - - auto req_save = req; - - bool ret; - - if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { - auto req2 = req; - req2.path = "http://" + host_and_port_ + req.path; - ret = process_request(strm, req2, res, close_connection, error); - req = req2; - req.path = req_save.path; - } else { - ret = process_request(strm, req, res, close_connection, error); - } - - if (!ret) { return false; } - - if (res.get_header_value("Connection") == "close" || - (res.version == "HTTP/1.0" && res.reason != "Connection established")) { - // TODO this requires a not-entirely-obvious chain of calls to be correct - // for this to be safe. - - // This is safe to call because handle_request is only called by send_ - // which locks the request mutex during the process. It would be a bug - // to call it from a different thread since it's a thread-safety issue - // to do these things to the socket if another thread is using the socket. - std::lock_guard guard(socket_mutex_); - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); - } - - if (300 < res.status && res.status < 400 && follow_location_) { - req = req_save; - ret = redirect(req, res, error); - } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if ((res.status == StatusCode::Unauthorized_401 || - res.status == StatusCode::ProxyAuthenticationRequired_407) && - req.authorization_count_ < 5) { - auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407; - const auto &username = - is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; - const auto &password = - is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; - - if (!username.empty() && !password.empty()) { - std::map auth; - if (detail::parse_www_authenticate(res, auth, is_proxy)) { - Request new_req = req; - new_req.authorization_count_ += 1; - new_req.headers.erase(is_proxy ? "Proxy-Authorization" - : "Authorization"); - new_req.headers.insert(detail::make_digest_authentication_header( - req, auth, new_req.authorization_count_, detail::random_string(10), - username, password, is_proxy)); - - Response new_res; - - ret = send(new_req, new_res, error); - if (ret) { res = new_res; } - } - } - } -#endif - - return ret; -} - -inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { - if (req.redirect_count_ == 0) { - error = Error::ExceedRedirectCount; - return false; - } - - auto location = res.get_header_value("location"); - if (location.empty()) { return false; } - - const static std::regex re( - R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); - - std::smatch m; - if (!std::regex_match(location, m, re)) { return false; } - - auto scheme = is_ssl() ? "https" : "http"; - - auto next_scheme = m[1].str(); - auto next_host = m[2].str(); - if (next_host.empty()) { next_host = m[3].str(); } - auto port_str = m[4].str(); - auto next_path = m[5].str(); - auto next_query = m[6].str(); - - auto next_port = port_; - if (!port_str.empty()) { - next_port = std::stoi(port_str); - } else if (!next_scheme.empty()) { - next_port = next_scheme == "https" ? 443 : 80; - } - - if (next_scheme.empty()) { next_scheme = scheme; } - if (next_host.empty()) { next_host = host_; } - if (next_path.empty()) { next_path = "/"; } - - auto path = detail::decode_url(next_path, true) + next_query; - - if (next_scheme == scheme && next_host == host_ && next_port == port_) { - return detail::redirect(*this, req, res, path, location, error); - } else { - if (next_scheme == "https") { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLClient cli(next_host, next_port); - cli.copy_settings(*this); - if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } - return detail::redirect(cli, req, res, path, location, error); -#else - return false; -#endif - } else { - ClientImpl cli(next_host, next_port); - cli.copy_settings(*this); - return detail::redirect(cli, req, res, path, location, error); - } - } -} - -inline bool ClientImpl::write_content_with_provider(Stream &strm, - const Request &req, - Error &error) const { - auto is_shutting_down = []() { return false; }; - - if (req.is_chunked_content_provider_) { - // TODO: Brotli support - std::unique_ptr compressor; -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_) { - compressor = detail::make_unique(); - } else -#endif - { - compressor = detail::make_unique(); - } - - return detail::write_content_chunked(strm, req.content_provider_, - is_shutting_down, *compressor, error); - } else { - return detail::write_content(strm, req.content_provider_, 0, - req.content_length_, is_shutting_down, error); - } -} - -inline bool ClientImpl::write_request(Stream &strm, Request &req, - bool close_connection, Error &error) { - // Prepare additional headers - if (close_connection) { - if (!req.has_header("Connection")) { - req.set_header("Connection", "close"); - } - } - - if (!req.has_header("Host")) { - if (is_ssl()) { - if (port_ == 443) { - req.set_header("Host", host_); - } else { - req.set_header("Host", host_and_port_); - } - } else { - if (port_ == 80) { - req.set_header("Host", host_); - } else { - req.set_header("Host", host_and_port_); - } - } - } - - if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } - -#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT - if (!req.has_header("User-Agent")) { - auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; - req.set_header("User-Agent", agent); - } -#endif - - if (req.body.empty()) { - if (req.content_provider_) { - if (!req.is_chunked_content_provider_) { - if (!req.has_header("Content-Length")) { - auto length = std::to_string(req.content_length_); - req.set_header("Content-Length", length); - } - } - } else { - if (req.method == "POST" || req.method == "PUT" || - req.method == "PATCH") { - req.set_header("Content-Length", "0"); - } - } - } else { - if (!req.has_header("Content-Type")) { - req.set_header("Content-Type", "text/plain"); - } - - if (!req.has_header("Content-Length")) { - auto length = std::to_string(req.body.size()); - req.set_header("Content-Length", length); - } - } - - if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { - if (!req.has_header("Authorization")) { - req.headers.insert(make_basic_authentication_header( - basic_auth_username_, basic_auth_password_, false)); - } - } - - if (!proxy_basic_auth_username_.empty() && - !proxy_basic_auth_password_.empty()) { - if (!req.has_header("Proxy-Authorization")) { - req.headers.insert(make_basic_authentication_header( - proxy_basic_auth_username_, proxy_basic_auth_password_, true)); - } - } - - if (!bearer_token_auth_token_.empty()) { - if (!req.has_header("Authorization")) { - req.headers.insert(make_bearer_token_authentication_header( - bearer_token_auth_token_, false)); - } - } - - if (!proxy_bearer_token_auth_token_.empty()) { - if (!req.has_header("Proxy-Authorization")) { - req.headers.insert(make_bearer_token_authentication_header( - proxy_bearer_token_auth_token_, true)); - } - } - - // Request line and headers - { - detail::BufferStream bstrm; - - const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; - detail::write_request_line(bstrm, req.method, path); - - header_writer_(bstrm, req.headers); - - // Flush buffer - auto &data = bstrm.get_buffer(); - if (!detail::write_data(strm, data.data(), data.size())) { - error = Error::Write; - return false; - } - } - - // Body - if (req.body.empty()) { - return write_content_with_provider(strm, req, error); - } - - if (!detail::write_data(strm, req.body.data(), req.body.size())) { - error = Error::Write; - return false; - } - - return true; -} - -inline std::unique_ptr ClientImpl::send_with_content_provider( - Request &req, const char *body, size_t content_length, - ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error) { - if (!content_type.empty()) { req.set_header("Content-Type", content_type); } - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_) { req.set_header("Content-Encoding", "gzip"); } -#endif - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_ && !content_provider_without_length) { - // TODO: Brotli support - detail::gzip_compressor compressor; - - if (content_provider) { - auto ok = true; - size_t offset = 0; - DataSink data_sink; - - data_sink.write = [&](const char *data, size_t data_len) -> bool { - if (ok) { - auto last = offset + data_len == content_length; - - auto ret = compressor.compress( - data, data_len, last, - [&](const char *compressed_data, size_t compressed_data_len) { - req.body.append(compressed_data, compressed_data_len); - return true; - }); - - if (ret) { - offset += data_len; - } else { - ok = false; - } - } - return ok; - }; - - while (ok && offset < content_length) { - if (!content_provider(offset, content_length - offset, data_sink)) { - error = Error::Canceled; - return nullptr; - } - } - } else { - if (!compressor.compress(body, content_length, true, - [&](const char *data, size_t data_len) { - req.body.append(data, data_len); - return true; - })) { - error = Error::Compression; - return nullptr; - } - } - } else -#endif - { - if (content_provider) { - req.content_length_ = content_length; - req.content_provider_ = std::move(content_provider); - req.is_chunked_content_provider_ = false; - } else if (content_provider_without_length) { - req.content_length_ = 0; - req.content_provider_ = detail::ContentProviderAdapter( - std::move(content_provider_without_length)); - req.is_chunked_content_provider_ = true; - req.set_header("Transfer-Encoding", "chunked"); - } else { - req.body.assign(body, content_length); - } - } - - auto res = detail::make_unique(); - return send(req, *res, error) ? std::move(res) : nullptr; -} - -inline Result ClientImpl::send_with_content_provider( - const std::string &method, const std::string &path, const Headers &headers, - const char *body, size_t content_length, ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Progress progress) { - Request req; - req.method = method; - req.headers = headers; - req.path = path; - req.progress = progress; - - auto error = Error::Success; - - auto res = send_with_content_provider( - req, body, content_length, std::move(content_provider), - std::move(content_provider_without_length), content_type, error); - - return Result{std::move(res), error, std::move(req.headers)}; -} - -inline std::string -ClientImpl::adjust_host_string(const std::string &host) const { - if (host.find(':') != std::string::npos) { return "[" + host + "]"; } - return host; -} - -inline bool ClientImpl::process_request(Stream &strm, Request &req, - Response &res, bool close_connection, - Error &error) { - // Send request - if (!write_request(strm, req, close_connection, error)) { return false; } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl()) { - auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; - if (!is_proxy_enabled) { - char buf[1]; - if (SSL_peek(socket_.ssl, buf, 1) == 0 && - SSL_get_error(socket_.ssl, 0) == SSL_ERROR_ZERO_RETURN) { - error = Error::SSLPeerCouldBeClosed_; - return false; - } - } - } -#endif - - // Receive response and headers - if (!read_response_line(strm, req, res) || - !detail::read_headers(strm, res.headers)) { - error = Error::Read; - return false; - } - - // Body - if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && - req.method != "CONNECT") { - auto redirect = 300 < res.status && res.status < 400 && follow_location_; - - if (req.response_handler && !redirect) { - if (!req.response_handler(res)) { - error = Error::Canceled; - return false; - } - } - - auto out = - req.content_receiver - ? static_cast( - [&](const char *buf, size_t n, uint64_t off, uint64_t len) { - if (redirect) { return true; } - auto ret = req.content_receiver(buf, n, off, len); - if (!ret) { error = Error::Canceled; } - return ret; - }) - : static_cast( - [&](const char *buf, size_t n, uint64_t /*off*/, - uint64_t /*len*/) { - if (res.body.size() + n > res.body.max_size()) { - return false; - } - res.body.append(buf, n); - return true; - }); - - auto progress = [&](uint64_t current, uint64_t total) { - if (!req.progress || redirect) { return true; } - auto ret = req.progress(current, total); - if (!ret) { error = Error::Canceled; } - return ret; - }; - - if (res.has_header("Content-Length")) { - if (!req.content_receiver) { - auto len = std::min(res.get_header_value_u64("Content-Length"), - res.body.max_size()); - if (len > 0) { res.body.reserve(len); } - } - } - - int dummy_status; - if (!detail::read_content(strm, res, (std::numeric_limits::max)(), - dummy_status, std::move(progress), std::move(out), - decompress_)) { - if (error != Error::Canceled) { error = Error::Read; } - return false; - } - } - - // Log - if (logger_) { logger_(req, res); } - - return true; -} - -inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( - const std::string &boundary, const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) const { - size_t cur_item = 0; - size_t cur_start = 0; - // cur_item and cur_start are copied to within the std::function and maintain - // state between successive calls - return [&, cur_item, cur_start](size_t offset, - DataSink &sink) mutable -> bool { - if (!offset && !items.empty()) { - sink.os << detail::serialize_multipart_formdata(items, boundary, false); - return true; - } else if (cur_item < provider_items.size()) { - if (!cur_start) { - const auto &begin = detail::serialize_multipart_formdata_item_begin( - provider_items[cur_item], boundary); - offset += begin.size(); - cur_start = offset; - sink.os << begin; - } - - DataSink cur_sink; - auto has_data = true; - cur_sink.write = sink.write; - cur_sink.done = [&]() { has_data = false; }; - - if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) { - return false; - } - - if (!has_data) { - sink.os << detail::serialize_multipart_formdata_item_end(); - cur_item++; - cur_start = 0; - } - return true; - } else { - sink.os << detail::serialize_multipart_formdata_finish(boundary); - sink.done(); - return true; - } - }; -} - -inline bool -ClientImpl::process_socket(const Socket &socket, - std::function callback) { - return detail::process_client_socket( - socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, std::move(callback)); -} - -inline bool ClientImpl::is_ssl() const { return false; } - -inline Result ClientImpl::Get(const std::string &path) { - return Get(path, Headers(), Progress()); -} - -inline Result ClientImpl::Get(const std::string &path, Progress progress) { - return Get(path, Headers(), std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { - return Get(path, headers, Progress()); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - Progress progress) { - Request req; - req.method = "GET"; - req.path = path; - req.headers = headers; - req.progress = std::move(progress); - - return send_(std::move(req)); -} - -inline Result ClientImpl::Get(const std::string &path, - ContentReceiver content_receiver) { - return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, Headers(), nullptr, std::move(content_receiver), - std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return Get(path, headers, nullptr, std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, headers, nullptr, std::move(content_receiver), - std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, Headers(), std::move(response_handler), - std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, headers, std::move(response_handler), - std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, Headers(), std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { - Request req; - req.method = "GET"; - req.path = path; - req.headers = headers; - req.response_handler = std::move(response_handler); - req.content_receiver = - [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { - return content_receiver(data, data_length); - }; - req.progress = std::move(progress); - - return send_(std::move(req)); -} - -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { - if (params.empty()) { return Get(path, headers); } - - std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, params, headers, nullptr, std::move(content_receiver), - std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { - if (params.empty()) { - return Get(path, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); - } - - std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} - -inline Result ClientImpl::Head(const std::string &path) { - return Head(path, Headers()); -} - -inline Result ClientImpl::Head(const std::string &path, - const Headers &headers) { - Request req; - req.method = "HEAD"; - req.headers = headers; - req.path = path; - - return send_(std::move(req)); -} - -inline Result ClientImpl::Post(const std::string &path) { - return Post(path, std::string(), std::string()); -} - -inline Result ClientImpl::Post(const std::string &path, - const Headers &headers) { - return Post(path, headers, nullptr, 0, std::string()); -} - -inline Result ClientImpl::Post(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Post(path, Headers(), body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); -} - -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return Post(path, Headers(), body, content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return Post(path, Headers(), body, content_type, progress); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - -inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { - return Post(path, Headers(), params); -} - -inline Result ClientImpl::Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Post(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Post(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Post(path, headers, query, "application/x-www-form-urlencoded"); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - auto query = detail::params_to_query_str(params); - return Post(path, headers, query, "application/x-www-form-urlencoded", - progress); -} - -inline Result ClientImpl::Post(const std::string &path, - const MultipartFormDataItems &items) { - return Post(path, Headers(), items); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - if (!detail::is_multipart_boundary_chars_valid(boundary)) { - return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; - } - - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type); -} - -inline Result -ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( - "POST", path, headers, nullptr, 0, nullptr, - get_multipart_content_provider(boundary, items, provider_items), - content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path) { - return Put(path, std::string(), std::string()); -} - -inline Result ClientImpl::Put(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Put(path, Headers(), body, content_length, content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); -} - -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return Put(path, Headers(), body, content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return Put(path, Headers(), body, content_type, progress); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - -inline Result ClientImpl::Put(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Put(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Put(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { - return Put(path, Headers(), params); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Put(path, headers, query, "application/x-www-form-urlencoded"); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - auto query = detail::params_to_query_str(params); - return Put(path, headers, query, "application/x-www-form-urlencoded", - progress); -} - -inline Result ClientImpl::Put(const std::string &path, - const MultipartFormDataItems &items) { - return Put(path, Headers(), items); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Put(path, headers, body, content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - if (!detail::is_multipart_boundary_chars_valid(boundary)) { - return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; - } - - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Put(path, headers, body, content_type); -} - -inline Result -ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( - "PUT", path, headers, nullptr, 0, nullptr, - get_multipart_content_provider(boundary, items, provider_items), - content_type, nullptr); -} -inline Result ClientImpl::Patch(const std::string &path) { - return Patch(path, std::string(), std::string()); -} - -inline Result ClientImpl::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Patch(path, Headers(), body, content_length, content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return Patch(path, Headers(), body, content_length, content_type, progress); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return Patch(path, headers, body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PATCH", path, headers, body, - content_length, nullptr, nullptr, - content_type, progress); -} - -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Patch(path, Headers(), body, content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type, - Progress progress) { - return Patch(path, Headers(), body, content_type, progress); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Patch(path, headers, body, content_type, nullptr); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PATCH", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - -inline Result ClientImpl::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Patch(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Patch(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("PATCH", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Delete(const std::string &path) { - return Delete(path, Headers(), std::string(), std::string()); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers) { - return Delete(path, headers, std::string(), std::string()); -} - -inline Result ClientImpl::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, Headers(), body, content_length, content_type); -} - -inline Result ClientImpl::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return Delete(path, Headers(), body, content_length, content_type, progress); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, headers, body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - Request req; - req.method = "DELETE"; - req.headers = headers; - req.path = path; - req.progress = progress; - - if (!content_type.empty()) { req.set_header("Content-Type", content_type); } - req.body.assign(body, content_length); - - return send_(std::move(req)); -} - -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Delete(path, Headers(), body.data(), body.size(), content_type); -} - -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type, - Progress progress) { - return Delete(path, Headers(), body.data(), body.size(), content_type, - progress); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Delete(path, headers, body.data(), body.size(), content_type); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return Delete(path, headers, body.data(), body.size(), content_type, - progress); -} - -inline Result ClientImpl::Options(const std::string &path) { - return Options(path, Headers()); -} - -inline Result ClientImpl::Options(const std::string &path, - const Headers &headers) { - Request req; - req.method = "OPTIONS"; - req.headers = headers; - req.path = path; - - return send_(std::move(req)); -} - -inline void ClientImpl::stop() { - std::lock_guard guard(socket_mutex_); - - // If there is anything ongoing right now, the ONLY thread-safe thing we can - // do is to shutdown_socket, so that threads using this socket suddenly - // discover they can't read/write any more and error out. Everything else - // (closing the socket, shutting ssl down) is unsafe because these actions are - // not thread-safe. - if (socket_requests_in_flight_ > 0) { - shutdown_socket(socket_); - - // Aside from that, we set a flag for the socket to be closed when we're - // done. - socket_should_be_closed_when_request_is_done_ = true; - return; - } - - // Otherwise, still holding the mutex, we can shut everything down ourselves - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); -} - -inline std::string ClientImpl::host() const { return host_; } - -inline int ClientImpl::port() const { return port_; } - -inline size_t ClientImpl::is_socket_open() const { - std::lock_guard guard(socket_mutex_); - return socket_.is_open(); -} - -inline socket_t ClientImpl::socket() const { return socket_.sock; } - -inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { - connection_timeout_sec_ = sec; - connection_timeout_usec_ = usec; -} - -inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { - read_timeout_sec_ = sec; - read_timeout_usec_ = usec; -} - -inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { - write_timeout_sec_ = sec; - write_timeout_usec_ = usec; -} - -inline void ClientImpl::set_basic_auth(const std::string &username, - const std::string &password) { - basic_auth_username_ = username; - basic_auth_password_ = password; -} - -inline void ClientImpl::set_bearer_token_auth(const std::string &token) { - bearer_token_auth_token_ = token; -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_digest_auth(const std::string &username, - const std::string &password) { - digest_auth_username_ = username; - digest_auth_password_ = password; -} -#endif - -inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } - -inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } - -inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } - -inline void -ClientImpl::set_hostname_addr_map(std::map addr_map) { - addr_map_ = std::move(addr_map); -} - -inline void ClientImpl::set_default_headers(Headers headers) { - default_headers_ = std::move(headers); -} - -inline void ClientImpl::set_header_writer( - std::function const &writer) { - header_writer_ = writer; -} - -inline void ClientImpl::set_address_family(int family) { - address_family_ = family; -} - -inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } - -inline void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; } - -inline void ClientImpl::set_socket_options(SocketOptions socket_options) { - socket_options_ = std::move(socket_options); -} - -inline void ClientImpl::set_compress(bool on) { compress_ = on; } - -inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } - -inline void ClientImpl::set_interface(const std::string &intf) { - interface_ = intf; -} - -inline void ClientImpl::set_proxy(const std::string &host, int port) { - proxy_host_ = host; - proxy_port_ = port; -} - -inline void ClientImpl::set_proxy_basic_auth(const std::string &username, - const std::string &password) { - proxy_basic_auth_username_ = username; - proxy_basic_auth_password_ = password; -} - -inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { - proxy_bearer_token_auth_token_ = token; -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - proxy_digest_auth_username_ = username; - proxy_digest_auth_password_ = password; -} - -inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { - ca_cert_file_path_ = ca_cert_file_path; - ca_cert_dir_path_ = ca_cert_dir_path; -} - -inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (ca_cert_store && ca_cert_store != ca_cert_store_) { - ca_cert_store_ = ca_cert_store; - } -} - -inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, - std::size_t size) const { - auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); - auto se = detail::scope_exit([&] { BIO_free_all(mem); }); - if (!mem) { return nullptr; } - - auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); - if (!inf) { return nullptr; } - - auto cts = X509_STORE_new(); - if (cts) { - for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { - auto itmp = sk_X509_INFO_value(inf, i); - if (!itmp) { continue; } - - if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } - if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); } - } - } - - sk_X509_INFO_pop_free(inf, X509_INFO_free); - return cts; -} - -inline void ClientImpl::enable_server_certificate_verification(bool enabled) { - server_certificate_verification_ = enabled; -} - -inline void ClientImpl::enable_server_hostname_verification(bool enabled) { - server_hostname_verification_ = enabled; -} - -inline void ClientImpl::set_server_certificate_verifier( - std::function verifier) { - server_certificate_verifier_ = verifier; -} -#endif - -inline void ClientImpl::set_logger(Logger logger) { - logger_ = std::move(logger); -} - -/* - * SSL Implementation - */ -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -namespace detail { - -template -inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, - U SSL_connect_or_accept, V setup) { - SSL *ssl = nullptr; - { - std::lock_guard guard(ctx_mutex); - ssl = SSL_new(ctx); - } - - if (ssl) { - set_nonblocking(sock, true); - auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); - BIO_set_nbio(bio, 1); - SSL_set_bio(ssl, bio, bio); - - if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { - SSL_shutdown(ssl); - { - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); - } - set_nonblocking(sock, false); - return nullptr; - } - BIO_set_nbio(bio, 0); - set_nonblocking(sock, false); - } - - return ssl; -} - -inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, - bool shutdown_gracefully) { - // sometimes we may want to skip this to try to avoid SIGPIPE if we know - // the remote has closed the network connection - // Note that it is not always possible to avoid SIGPIPE, this is merely a - // best-efforts. - if (shutdown_gracefully) { -#ifdef _WIN32 - SSL_shutdown(ssl); -#else - timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); - - auto ret = SSL_shutdown(ssl); - while (ret == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds{100}); - ret = SSL_shutdown(ssl); - } -#endif - } - - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); -} - -template -bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, - U ssl_connect_or_accept, - time_t timeout_sec, - time_t timeout_usec) { - auto res = 0; - while ((res = ssl_connect_or_accept(ssl)) != 1) { - auto err = SSL_get_error(ssl, res); - switch (err) { - case SSL_ERROR_WANT_READ: - if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } - break; - case SSL_ERROR_WANT_WRITE: - if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } - break; - default: break; - } - return false; - } - return true; -} - -template -inline bool process_server_socket_ssl( - const std::atomic &svr_sock, SSL *ssl, socket_t sock, - size_t keep_alive_max_count, time_t keep_alive_timeout_sec, - time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - return process_server_socket_core( - svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm, close_connection, connection_closed); - }); -} - -template -inline bool -process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm); -} - -class SSLInit { -public: - SSLInit() { - OPENSSL_init_ssl( - OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); - } -}; - -// SSL socket stream implementation -inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, - time_t read_timeout_sec, - time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec) - : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec), - write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec) { - SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); -} - -inline SSLSocketStream::~SSLSocketStream() = default; - -inline bool SSLSocketStream::is_readable() const { - return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; -} - -inline bool SSLSocketStream::is_writable() const { - return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); -} - -inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { - if (SSL_pending(ssl_) > 0) { - return SSL_read(ssl_, ptr, static_cast(size)); - } else if (is_readable()) { - auto ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret < 0) { - auto err = SSL_get_error(ssl_, ret); - auto n = 1000; -#ifdef _WIN32 - while (--n >= 0 && (err == SSL_ERROR_WANT_READ || - (err == SSL_ERROR_SYSCALL && - WSAGetLastError() == WSAETIMEDOUT))) { -#else - while (--n >= 0 && err == SSL_ERROR_WANT_READ) { -#endif - if (SSL_pending(ssl_) > 0) { - return SSL_read(ssl_, ptr, static_cast(size)); - } else if (is_readable()) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); - ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret >= 0) { return ret; } - err = SSL_get_error(ssl_, ret); - } else { - return -1; - } - } - } - return ret; - } - return -1; -} - -inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { - if (is_writable()) { - auto handle_size = static_cast( - std::min(size, (std::numeric_limits::max)())); - - auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret < 0) { - auto err = SSL_get_error(ssl_, ret); - auto n = 1000; -#ifdef _WIN32 - while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || - (err == SSL_ERROR_SYSCALL && - WSAGetLastError() == WSAETIMEDOUT))) { -#else - while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { -#endif - if (is_writable()) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); - ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret >= 0) { return ret; } - err = SSL_get_error(ssl_, ret); - } else { - return -1; - } - } - } - return ret; - } - return -1; -} - -inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { - detail::get_remote_ip_and_port(sock_, ip, port); -} - -inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, - int &port) const { - detail::get_local_ip_and_port(sock_, ip, port); -} - -inline socket_t SSLSocketStream::socket() const { return sock_; } - -static SSLInit sslinit_; - -} // namespace detail - -// SSL HTTP server implementation -inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path, - const char *client_ca_cert_dir_path, - const char *private_key_password) { - ctx_ = SSL_CTX_new(TLS_server_method()); - - if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - if (private_key_password != nullptr && (private_key_password[0] != '\0')) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, - reinterpret_cast(const_cast(private_key_password))); - } - - if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || - SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != - 1 || - SSL_CTX_check_private_key(ctx_) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { - SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, - client_ca_cert_dir_path); - - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); - } - } -} - -inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { - ctx_ = SSL_CTX_new(TLS_server_method()); - - if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - if (SSL_CTX_use_certificate(ctx_, cert) != 1 || - SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } else if (client_ca_cert_store) { - SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); - - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); - } - } -} - -inline SSLServer::SSLServer( - const std::function &setup_ssl_ctx_callback) { - ctx_ = SSL_CTX_new(TLS_method()); - if (ctx_) { - if (!setup_ssl_ctx_callback(*ctx_)) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } -} - -inline SSLServer::~SSLServer() { - if (ctx_) { SSL_CTX_free(ctx_); } -} - -inline bool SSLServer::is_valid() const { return ctx_; } - -inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } - -inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { - - std::lock_guard guard(ctx_mutex_); - - SSL_CTX_use_certificate(ctx_, cert); - SSL_CTX_use_PrivateKey(ctx_, private_key); - - if (client_ca_cert_store != nullptr) { - SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); - } -} - -inline bool SSLServer::process_and_close_socket(socket_t sock) { - auto ssl = detail::ssl_new( - sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { - return detail::ssl_connect_or_accept_nonblocking( - sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); - }, - [](SSL * /*ssl2*/) { return true; }); - - auto ret = false; - if (ssl) { - std::string remote_addr; - int remote_port = 0; - detail::get_remote_ip_and_port(sock, remote_addr, remote_port); - - std::string local_addr; - int local_port = 0; - detail::get_local_ip_and_port(sock, local_addr, local_port); - - ret = detail::process_server_socket_ssl( - svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, - [&](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, remote_addr, remote_port, local_addr, - local_port, close_connection, - connection_closed, - [&](Request &req) { req.ssl = ssl; }); - }); - - // Shutdown gracefully if the result seemed successful, non-gracefully if - // the connection appeared to be closed. - const bool shutdown_gracefully = ret; - detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); - } - - detail::shutdown_socket(sock); - detail::close_socket(sock); - return ret; -} - -// SSL HTTP client implementation -inline SSLClient::SSLClient(const std::string &host) - : SSLClient(host, 443, std::string(), std::string()) {} - -inline SSLClient::SSLClient(const std::string &host, int port) - : SSLClient(host, port, std::string(), std::string()) {} - -inline SSLClient::SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path, - const std::string &private_key_password) - : ClientImpl(host, port, client_cert_path, client_key_path) { - ctx_ = SSL_CTX_new(TLS_client_method()); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { - host_components_.emplace_back(b, e); - }); - - if (!client_cert_path.empty() && !client_key_path.empty()) { - if (!private_key_password.empty()) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, reinterpret_cast( - const_cast(private_key_password.c_str()))); - } - - if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), - SSL_FILETYPE_PEM) != 1 || - SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), - SSL_FILETYPE_PEM) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } -} - -inline SSLClient::SSLClient(const std::string &host, int port, - X509 *client_cert, EVP_PKEY *client_key, - const std::string &private_key_password) - : ClientImpl(host, port) { - ctx_ = SSL_CTX_new(TLS_client_method()); - - detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { - host_components_.emplace_back(b, e); - }); - - if (client_cert != nullptr && client_key != nullptr) { - if (!private_key_password.empty()) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, reinterpret_cast( - const_cast(private_key_password.c_str()))); - } - - if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || - SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } -} - -inline SSLClient::~SSLClient() { - if (ctx_) { SSL_CTX_free(ctx_); } - // Make sure to shut down SSL since shutdown_ssl will resolve to the - // base function rather than the derived function once we get to the - // base class destructor, and won't free the SSL (causing a leak). - shutdown_ssl_impl(socket_, true); -} - -inline bool SSLClient::is_valid() const { return ctx_; } - -inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (ca_cert_store) { - if (ctx_) { - if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { - // Free memory allocated for old cert and use new store `ca_cert_store` - SSL_CTX_set_cert_store(ctx_, ca_cert_store); - } - } else { - X509_STORE_free(ca_cert_store); - } - } -} - -inline void SSLClient::load_ca_cert_store(const char *ca_cert, - std::size_t size) { - set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); -} - -inline long SSLClient::get_openssl_verify_result() const { - return verify_result_; -} - -inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } - -inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { - return is_valid() && ClientImpl::create_and_connect_socket(socket, error); -} - -// Assumes that socket_mutex_ is locked and that there are no requests in flight -inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, - bool &success, Error &error) { - success = true; - Response proxy_res; - if (!detail::process_client_socket( - socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { - Request req2; - req2.method = "CONNECT"; - req2.path = host_and_port_; - return process_request(strm, req2, proxy_res, false, error); - })) { - // Thread-safe to close everything because we are assuming there are no - // requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - success = false; - return false; - } - - if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) { - if (!proxy_digest_auth_username_.empty() && - !proxy_digest_auth_password_.empty()) { - std::map auth; - if (detail::parse_www_authenticate(proxy_res, auth, true)) { - proxy_res = Response(); - if (!detail::process_client_socket( - socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { - Request req3; - req3.method = "CONNECT"; - req3.path = host_and_port_; - req3.headers.insert(detail::make_digest_authentication_header( - req3, auth, 1, detail::random_string(10), - proxy_digest_auth_username_, proxy_digest_auth_password_, - true)); - return process_request(strm, req3, proxy_res, false, error); - })) { - // Thread-safe to close everything because we are assuming there are - // no requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - success = false; - return false; - } - } - } - } - - // If status code is not 200, proxy request is failed. - // Set error to ProxyConnection and return proxy response - // as the response of the request - if (proxy_res.status != StatusCode::OK_200) { - error = Error::ProxyConnection; - res = std::move(proxy_res); - // Thread-safe to close everything because we are assuming there are - // no requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - return false; - } - - return true; -} - -inline bool SSLClient::load_certs() { - auto ret = true; - - std::call_once(initialize_cert_, [&]() { - std::lock_guard guard(ctx_mutex_); - if (!ca_cert_file_path_.empty()) { - if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), - nullptr)) { - ret = false; - } - } else if (!ca_cert_dir_path_.empty()) { - if (!SSL_CTX_load_verify_locations(ctx_, nullptr, - ca_cert_dir_path_.c_str())) { - ret = false; - } - } else { - auto loaded = false; -#ifdef _WIN32 - loaded = - detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#if TARGET_OS_OSX - loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); -#endif // TARGET_OS_OSX -#endif // _WIN32 - if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } - } - }); - - return ret; -} - -inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { - auto ssl = detail::ssl_new( - socket.sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { - if (server_certificate_verification_) { - if (!load_certs()) { - error = Error::SSLLoadingCerts; - return false; - } - SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); - } - - if (!detail::ssl_connect_or_accept_nonblocking( - socket.sock, ssl2, SSL_connect, connection_timeout_sec_, - connection_timeout_usec_)) { - error = Error::SSLConnection; - return false; - } - - if (server_certificate_verification_) { - if (server_certificate_verifier_) { - if (!server_certificate_verifier_(ssl2)) { - error = Error::SSLServerVerification; - return false; - } - } else { - verify_result_ = SSL_get_verify_result(ssl2); - - if (verify_result_ != X509_V_OK) { - error = Error::SSLServerVerification; - return false; - } - - auto server_cert = SSL_get1_peer_certificate(ssl2); - auto se = detail::scope_exit([&] { X509_free(server_cert); }); - - if (server_cert == nullptr) { - error = Error::SSLServerVerification; - return false; - } - - if (server_hostname_verification_) { - if (!verify_host(server_cert)) { - error = Error::SSLServerHostnameVerification; - return false; - } - } - } - } - - return true; - }, - [&](SSL *ssl2) { -#if defined(OPENSSL_IS_BORINGSSL) - SSL_set_tlsext_host_name(ssl2, host_.c_str()); -#else - // NOTE: Direct call instead of using the OpenSSL macro to suppress - // -Wold-style-cast warning - SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, - static_cast(const_cast(host_.c_str()))); -#endif - return true; - }); - - if (ssl) { - socket.ssl = ssl; - return true; - } - - shutdown_socket(socket); - close_socket(socket); - return false; -} - -inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { - shutdown_ssl_impl(socket, shutdown_gracefully); -} - -inline void SSLClient::shutdown_ssl_impl(Socket &socket, - bool shutdown_gracefully) { - if (socket.sock == INVALID_SOCKET) { - assert(socket.ssl == nullptr); - return; - } - if (socket.ssl) { - detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, - shutdown_gracefully); - socket.ssl = nullptr; - } - assert(socket.ssl == nullptr); -} - -inline bool -SSLClient::process_socket(const Socket &socket, - std::function callback) { - assert(socket.ssl); - return detail::process_client_socket_ssl( - socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, std::move(callback)); -} - -inline bool SSLClient::is_ssl() const { return true; } - -inline bool SSLClient::verify_host(X509 *server_cert) const { - /* Quote from RFC2818 section 3.1 "Server Identity" - - If a subjectAltName extension of type dNSName is present, that MUST - be used as the identity. Otherwise, the (most specific) Common Name - field in the Subject field of the certificate MUST be used. Although - the use of the Common Name is existing practice, it is deprecated and - Certification Authorities are encouraged to use the dNSName instead. - - Matching is performed using the matching rules specified by - [RFC2459]. If more than one identity of a given type is present in - the certificate (e.g., more than one dNSName name, a match in any one - of the set is considered acceptable.) Names may contain the wildcard - character * which is considered to match any single domain name - component or component fragment. E.g., *.a.com matches foo.a.com but - not bar.foo.a.com. f*.com matches foo.com but not bar.com. - - In some cases, the URI is specified as an IP address rather than a - hostname. In this case, the iPAddress subjectAltName must be present - in the certificate and must exactly match the IP in the URI. - - */ - return verify_host_with_subject_alt_name(server_cert) || - verify_host_with_common_name(server_cert); -} - -inline bool -SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { - auto ret = false; - - auto type = GEN_DNS; - - struct in6_addr addr6 {}; - struct in_addr addr {}; - size_t addr_len = 0; - -#ifndef __MINGW32__ - if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { - type = GEN_IPADD; - addr_len = sizeof(struct in6_addr); - } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { - type = GEN_IPADD; - addr_len = sizeof(struct in_addr); - } -#endif - - auto alt_names = static_cast( - X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); - - if (alt_names) { - auto dsn_matched = false; - auto ip_matched = false; - - auto count = sk_GENERAL_NAME_num(alt_names); - - for (decltype(count) i = 0; i < count && !dsn_matched; i++) { - auto val = sk_GENERAL_NAME_value(alt_names, i); - if (val->type == type) { - auto name = - reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); - auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); - - switch (type) { - case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; - - case GEN_IPADD: - if (!memcmp(&addr6, name, addr_len) || - !memcmp(&addr, name, addr_len)) { - ip_matched = true; - } - break; - } - } - } - - if (dsn_matched || ip_matched) { ret = true; } - } - - GENERAL_NAMES_free(const_cast( - reinterpret_cast(alt_names))); - return ret; -} - -inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { - const auto subject_name = X509_get_subject_name(server_cert); - - if (subject_name != nullptr) { - char name[BUFSIZ]; - auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, - name, sizeof(name)); - - if (name_len != -1) { - return check_host_name(name, static_cast(name_len)); - } - } - - return false; -} - -inline bool SSLClient::check_host_name(const char *pattern, - size_t pattern_len) const { - if (host_.size() == pattern_len && host_ == pattern) { return true; } - - // Wildcard match - // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 - std::vector pattern_components; - detail::split(&pattern[0], &pattern[pattern_len], '.', - [&](const char *b, const char *e) { - pattern_components.emplace_back(b, e); - }); - - if (host_components_.size() != pattern_components.size()) { return false; } - - auto itr = pattern_components.begin(); - for (const auto &h : host_components_) { - auto &p = *itr; - if (p != h && p != "*") { - auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && - !p.compare(0, p.size() - 1, h)); - if (!partial_match) { return false; } - } - ++itr; - } - - return true; -} -#endif - -// Universal client implementation -inline Client::Client(const std::string &scheme_host_port) - : Client(scheme_host_port, std::string(), std::string()) {} - -inline Client::Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path) { - const static std::regex re( - R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); - - std::smatch m; - if (std::regex_match(scheme_host_port, m, re)) { - auto scheme = m[1].str(); - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (!scheme.empty() && (scheme != "http" && scheme != "https")) { -#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; - } - - auto is_ssl = scheme == "https"; - - auto host = m[2].str(); - if (host.empty()) { host = m[3].str(); } - - auto port_str = m[4].str(); - auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); - - if (is_ssl) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - cli_ = detail::make_unique(host, port, client_cert_path, - client_key_path); - is_ssl_ = is_ssl; -#endif - } else { - cli_ = detail::make_unique(host, port, client_cert_path, - client_key_path); - } - } else { - // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) - // if port param below changes. - cli_ = detail::make_unique(scheme_host_port, 80, - client_cert_path, client_key_path); - } -} // namespace detail - -inline Client::Client(const std::string &host, int port) - : cli_(detail::make_unique(host, port)) {} - -inline Client::Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) - : cli_(detail::make_unique(host, port, client_cert_path, - client_key_path)) {} - -inline Client::~Client() = default; - -inline bool Client::is_valid() const { - return cli_ != nullptr && cli_->is_valid(); -} - -inline Result Client::Get(const std::string &path) { return cli_->Get(path); } -inline Result Client::Get(const std::string &path, const Headers &headers) { - return cli_->Get(path, headers); -} -inline Result Client::Get(const std::string &path, Progress progress) { - return cli_->Get(path, std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - Progress progress) { - return cli_->Get(path, headers, std::move(progress)); -} -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, headers, std::move(content_receiver), - std::move(progress)); -} -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { - return cli_->Get(path, params, headers, std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, params, headers, std::move(content_receiver), - std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, params, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} - -inline Result Client::Head(const std::string &path) { return cli_->Head(path); } -inline Result Client::Head(const std::string &path, const Headers &headers) { - return cli_->Head(path, headers); -} - -inline Result Client::Post(const std::string &path) { return cli_->Post(path); } -inline Result Client::Post(const std::string &path, const Headers &headers) { - return cli_->Post(path, headers); -} -inline Result Client::Post(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Post(path, body, content_length, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_length, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress) { - return cli_->Post(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Post(path, body, content_type); -} -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Post(path, body, content_type, progress); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Post(path, headers, body, content_type, progress); -} -inline Result Client::Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Post(path, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Post(path, std::move(content_provider), content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Post(path, headers, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Post(path, headers, std::move(content_provider), content_type); -} -inline Result Client::Post(const std::string &path, const Params ¶ms) { - return cli_->Post(path, params); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Post(path, headers, params); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - return cli_->Post(path, headers, params, progress); -} -inline Result Client::Post(const std::string &path, - const MultipartFormDataItems &items) { - return cli_->Post(path, items); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - return cli_->Post(path, headers, items); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - return cli_->Post(path, headers, items, boundary); -} -inline Result -Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - return cli_->Post(path, headers, items, provider_items); -} -inline Result Client::Put(const std::string &path) { return cli_->Put(path); } -inline Result Client::Put(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Put(path, body, content_length, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_length, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress) { - return cli_->Put(path, headers, body, content_length, content_type, progress); -} -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Put(path, body, content_type); -} -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Put(path, body, content_type, progress); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Put(path, headers, body, content_type, progress); -} -inline Result Client::Put(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Put(path, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Put(path, std::move(content_provider), content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Put(path, headers, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Put(path, headers, std::move(content_provider), content_type); -} -inline Result Client::Put(const std::string &path, const Params ¶ms) { - return cli_->Put(path, params); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Put(path, headers, params); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - return cli_->Put(path, headers, params, progress); -} -inline Result Client::Put(const std::string &path, - const MultipartFormDataItems &items) { - return cli_->Put(path, items); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - return cli_->Put(path, headers, items); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - return cli_->Put(path, headers, items, boundary); -} -inline Result -Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - return cli_->Put(path, headers, items, provider_items); -} -inline Result Client::Patch(const std::string &path) { - return cli_->Patch(path); -} -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, body, content_length, content_type); -} -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, body, content_length, content_type, progress); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_length, content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, body, content_type); -} -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, body, content_type, progress); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, headers, body, content_type, progress); -} -inline Result Client::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Patch(path, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Patch(path, std::move(content_provider), content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Patch(path, headers, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Patch(path, headers, std::move(content_provider), content_type); -} -inline Result Client::Delete(const std::string &path) { - return cli_->Delete(path); -} -inline Result Client::Delete(const std::string &path, const Headers &headers) { - return cli_->Delete(path, headers); -} -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, body, content_length, content_type); -} -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, body, content_length, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_length, content_type); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, body, content_type); -} -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, body, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_type); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, headers, body, content_type, progress); -} -inline Result Client::Options(const std::string &path) { - return cli_->Options(path); -} -inline Result Client::Options(const std::string &path, const Headers &headers) { - return cli_->Options(path, headers); -} - -inline bool Client::send(Request &req, Response &res, Error &error) { - return cli_->send(req, res, error); -} - -inline Result Client::send(const Request &req) { return cli_->send(req); } - -inline void Client::stop() { cli_->stop(); } - -inline std::string Client::host() const { return cli_->host(); } - -inline int Client::port() const { return cli_->port(); } - -inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } - -inline socket_t Client::socket() const { return cli_->socket(); } - -inline void -Client::set_hostname_addr_map(std::map addr_map) { - cli_->set_hostname_addr_map(std::move(addr_map)); -} - -inline void Client::set_default_headers(Headers headers) { - cli_->set_default_headers(std::move(headers)); -} - -inline void Client::set_header_writer( - std::function const &writer) { - cli_->set_header_writer(writer); -} - -inline void Client::set_address_family(int family) { - cli_->set_address_family(family); -} - -inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } - -inline void Client::set_socket_options(SocketOptions socket_options) { - cli_->set_socket_options(std::move(socket_options)); -} - -inline void Client::set_connection_timeout(time_t sec, time_t usec) { - cli_->set_connection_timeout(sec, usec); -} - -inline void Client::set_read_timeout(time_t sec, time_t usec) { - cli_->set_read_timeout(sec, usec); -} - -inline void Client::set_write_timeout(time_t sec, time_t usec) { - cli_->set_write_timeout(sec, usec); -} - -inline void Client::set_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_basic_auth(username, password); -} -inline void Client::set_bearer_token_auth(const std::string &token) { - cli_->set_bearer_token_auth(token); -} -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_digest_auth(username, password); -} -#endif - -inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } -inline void Client::set_follow_location(bool on) { - cli_->set_follow_location(on); -} - -inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); } - -inline void Client::set_compress(bool on) { cli_->set_compress(on); } - -inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } - -inline void Client::set_interface(const std::string &intf) { - cli_->set_interface(intf); -} - -inline void Client::set_proxy(const std::string &host, int port) { - cli_->set_proxy(host, port); -} -inline void Client::set_proxy_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_basic_auth(username, password); -} -inline void Client::set_proxy_bearer_token_auth(const std::string &token) { - cli_->set_proxy_bearer_token_auth(token); -} -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_digest_auth(username, password); -} -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::enable_server_certificate_verification(bool enabled) { - cli_->enable_server_certificate_verification(enabled); -} - -inline void Client::enable_server_hostname_verification(bool enabled) { - cli_->enable_server_hostname_verification(enabled); -} - -inline void Client::set_server_certificate_verifier( - std::function verifier) { - cli_->set_server_certificate_verifier(verifier); -} -#endif - -inline void Client::set_logger(Logger logger) { - cli_->set_logger(std::move(logger)); -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { - cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); -} - -inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (is_ssl_) { - static_cast(*cli_).set_ca_cert_store(ca_cert_store); - } else { - cli_->set_ca_cert_store(ca_cert_store); - } -} - -inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { - set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); -} - -inline long Client::get_openssl_verify_result() const { - if (is_ssl_) { - return static_cast(*cli_).get_openssl_verify_result(); - } - return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? -} - -inline SSL_CTX *Client::ssl_context() const { - if (is_ssl_) { return static_cast(*cli_).ssl_context(); } - return nullptr; -} -#endif - -// ---------------------------------------------------------------------------- - -} // namespace httplib - -#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL) -#undef poll -#endif - -#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/benchmark/cpp-httplib-v18/main.cpp b/benchmark/cpp-httplib-v18/main.cpp deleted file mode 100644 index 86070a100c..0000000000 --- a/benchmark/cpp-httplib-v18/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "./httplib.h" -using namespace httplib; - -int main() { - Server svr; - - svr.Get("/", [](const Request &, Response &res) { - res.set_content("Hello World!", "text/plain"); - }); - - svr.listen("0.0.0.0", 8080); -} diff --git a/benchmark/cpp-httplib-v19/httplib.h b/benchmark/cpp-httplib-v19/httplib.h deleted file mode 100644 index e4799dab79..0000000000 --- a/benchmark/cpp-httplib-v19/httplib.h +++ /dev/null @@ -1,10475 +0,0 @@ -// -// httplib.h -// -// Copyright (c) 2025 Yuji Hirose. All rights reserved. -// MIT License -// - -#ifndef CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_HTTPLIB_H - -#define CPPHTTPLIB_VERSION "0.19.0" - -/* - * Configuration - */ - -#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND -#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND -#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND 10000 -#endif - -#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT -#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 100 -#endif - -#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND -#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 -#endif - -#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND -#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND -#define CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND -#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND -#define CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND -#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND 300 -#endif - -#ifndef CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND -#define CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND -#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND 5 -#endif - -#ifndef CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND -#define CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND 0 -#endif - -#ifndef CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND -#define CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND 0 -#endif - -#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND -#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 -#endif - -#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND -#ifdef _WIN32 -#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 -#else -#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 -#endif -#endif - -#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH -#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 -#endif - -#ifndef CPPHTTPLIB_HEADER_MAX_LENGTH -#define CPPHTTPLIB_HEADER_MAX_LENGTH 8192 -#endif - -#ifndef CPPHTTPLIB_REDIRECT_MAX_COUNT -#define CPPHTTPLIB_REDIRECT_MAX_COUNT 20 -#endif - -#ifndef CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT -#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT 1024 -#endif - -#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH -#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) -#endif - -#ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH -#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 8192 -#endif - -#ifndef CPPHTTPLIB_RANGE_MAX_COUNT -#define CPPHTTPLIB_RANGE_MAX_COUNT 1024 -#endif - -#ifndef CPPHTTPLIB_TCP_NODELAY -#define CPPHTTPLIB_TCP_NODELAY false -#endif - -#ifndef CPPHTTPLIB_IPV6_V6ONLY -#define CPPHTTPLIB_IPV6_V6ONLY false -#endif - -#ifndef CPPHTTPLIB_RECV_BUFSIZ -#define CPPHTTPLIB_RECV_BUFSIZ size_t(16384u) -#endif - -#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ -#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) -#endif - -#ifndef CPPHTTPLIB_THREAD_POOL_COUNT -#define CPPHTTPLIB_THREAD_POOL_COUNT \ - ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ - ? std::thread::hardware_concurrency() - 1 \ - : 0)) -#endif - -#ifndef CPPHTTPLIB_RECV_FLAGS -#define CPPHTTPLIB_RECV_FLAGS 0 -#endif - -#ifndef CPPHTTPLIB_SEND_FLAGS -#define CPPHTTPLIB_SEND_FLAGS 0 -#endif - -#ifndef CPPHTTPLIB_LISTEN_BACKLOG -#define CPPHTTPLIB_LISTEN_BACKLOG 5 -#endif - -/* - * Headers - */ - -#ifdef _WIN32 -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif //_CRT_SECURE_NO_WARNINGS - -#ifndef _CRT_NONSTDC_NO_DEPRECATE -#define _CRT_NONSTDC_NO_DEPRECATE -#endif //_CRT_NONSTDC_NO_DEPRECATE - -#if defined(_MSC_VER) -#if _MSC_VER < 1900 -#error Sorry, Visual Studio versions prior to 2015 are not supported -#endif - -#pragma comment(lib, "ws2_32.lib") - -#ifdef _WIN64 -using ssize_t = __int64; -#else -using ssize_t = long; -#endif -#endif // _MSC_VER - -#ifndef S_ISREG -#define S_ISREG(m) (((m) & S_IFREG) == S_IFREG) -#endif // S_ISREG - -#ifndef S_ISDIR -#define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR) -#endif // S_ISDIR - -#ifndef NOMINMAX -#define NOMINMAX -#endif // NOMINMAX - -#include -#include -#include - -#ifndef WSA_FLAG_NO_HANDLE_INHERIT -#define WSA_FLAG_NO_HANDLE_INHERIT 0x80 -#endif - -using socket_t = SOCKET; -#ifdef CPPHTTPLIB_USE_POLL -#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) -#endif - -#else // not _WIN32 - -#include -#if !defined(_AIX) && !defined(__MVS__) -#include -#endif -#ifdef __MVS__ -#include -#ifndef NI_MAXHOST -#define NI_MAXHOST 1025 -#endif -#endif -#include -#include -#include -#ifdef __linux__ -#include -#endif -#include -#ifdef CPPHTTPLIB_USE_POLL -#include -#endif -#include -#include -#include -#ifndef __VMS -#include -#endif -#include -#include -#include - -using socket_t = int; -#ifndef INVALID_SOCKET -#define INVALID_SOCKET (-1) -#endif -#endif //_WIN32 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN32 -#include - -// these are defined in wincrypt.h and it breaks compilation if BoringSSL is -// used -#undef X509_NAME -#undef X509_CERT_PAIR -#undef X509_EXTENSIONS -#undef PKCS7_SIGNER_INFO - -#ifdef _MSC_VER -#pragma comment(lib, "crypt32.lib") -#endif -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#include -#if TARGET_OS_OSX -#include -#include -#endif // TARGET_OS_OSX -#endif // _WIN32 - -#include -#include -#include -#include - -#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) -#include -#endif - -#include -#include - -#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) -#if OPENSSL_VERSION_NUMBER < 0x1010107f -#error Please use OpenSSL or a current version of BoringSSL -#endif -#define SSL_get1_peer_certificate SSL_get_peer_certificate -#elif OPENSSL_VERSION_NUMBER < 0x30000000L -#error Sorry, OpenSSL versions prior to 3.0.0 are not supported -#endif - -#endif - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -#include -#endif - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT -#include -#include -#endif - -/* - * Declaration - */ -namespace httplib { - -namespace detail { - -/* - * Backport std::make_unique from C++14. - * - * NOTE: This code came up with the following stackoverflow post: - * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique - * - */ - -template -typename std::enable_if::value, std::unique_ptr>::type -make_unique(Args &&...args) { - return std::unique_ptr(new T(std::forward(args)...)); -} - -template -typename std::enable_if::value, std::unique_ptr>::type -make_unique(std::size_t n) { - typedef typename std::remove_extent::type RT; - return std::unique_ptr(new RT[n]); -} - -namespace case_ignore { - -inline unsigned char to_lower(int c) { - const static unsigned char table[256] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, - 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, - 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, - 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, - 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, - 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, - 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, - 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, - 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 224, 225, 226, - 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, - 242, 243, 244, 245, 246, 215, 248, 249, 250, 251, 252, 253, 254, 223, 224, - 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, - 255, - }; - return table[(unsigned char)(char)c]; -} - -inline bool equal(const std::string &a, const std::string &b) { - return a.size() == b.size() && - std::equal(a.begin(), a.end(), b.begin(), [](char ca, char cb) { - return to_lower(ca) == to_lower(cb); - }); -} - -struct equal_to { - bool operator()(const std::string &a, const std::string &b) const { - return equal(a, b); - } -}; - -struct hash { - size_t operator()(const std::string &key) const { - return hash_core(key.data(), key.size(), 0); - } - - size_t hash_core(const char *s, size_t l, size_t h) const { - return (l == 0) ? h - : hash_core(s + 1, l - 1, - // Unsets the 6 high bits of h, therefore no - // overflow happens - (((std::numeric_limits::max)() >> 6) & - h * 33) ^ - static_cast(to_lower(*s))); - } -}; - -} // namespace case_ignore - -// This is based on -// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189". - -struct scope_exit { - explicit scope_exit(std::function &&f) - : exit_function(std::move(f)), execute_on_destruction{true} {} - - scope_exit(scope_exit &&rhs) noexcept - : exit_function(std::move(rhs.exit_function)), - execute_on_destruction{rhs.execute_on_destruction} { - rhs.release(); - } - - ~scope_exit() { - if (execute_on_destruction) { this->exit_function(); } - } - - void release() { this->execute_on_destruction = false; } - -private: - scope_exit(const scope_exit &) = delete; - void operator=(const scope_exit &) = delete; - scope_exit &operator=(scope_exit &&) = delete; - - std::function exit_function; - bool execute_on_destruction; -}; - -} // namespace detail - -enum StatusCode { - // Information responses - Continue_100 = 100, - SwitchingProtocol_101 = 101, - Processing_102 = 102, - EarlyHints_103 = 103, - - // Successful responses - OK_200 = 200, - Created_201 = 201, - Accepted_202 = 202, - NonAuthoritativeInformation_203 = 203, - NoContent_204 = 204, - ResetContent_205 = 205, - PartialContent_206 = 206, - MultiStatus_207 = 207, - AlreadyReported_208 = 208, - IMUsed_226 = 226, - - // Redirection messages - MultipleChoices_300 = 300, - MovedPermanently_301 = 301, - Found_302 = 302, - SeeOther_303 = 303, - NotModified_304 = 304, - UseProxy_305 = 305, - unused_306 = 306, - TemporaryRedirect_307 = 307, - PermanentRedirect_308 = 308, - - // Client error responses - BadRequest_400 = 400, - Unauthorized_401 = 401, - PaymentRequired_402 = 402, - Forbidden_403 = 403, - NotFound_404 = 404, - MethodNotAllowed_405 = 405, - NotAcceptable_406 = 406, - ProxyAuthenticationRequired_407 = 407, - RequestTimeout_408 = 408, - Conflict_409 = 409, - Gone_410 = 410, - LengthRequired_411 = 411, - PreconditionFailed_412 = 412, - PayloadTooLarge_413 = 413, - UriTooLong_414 = 414, - UnsupportedMediaType_415 = 415, - RangeNotSatisfiable_416 = 416, - ExpectationFailed_417 = 417, - ImATeapot_418 = 418, - MisdirectedRequest_421 = 421, - UnprocessableContent_422 = 422, - Locked_423 = 423, - FailedDependency_424 = 424, - TooEarly_425 = 425, - UpgradeRequired_426 = 426, - PreconditionRequired_428 = 428, - TooManyRequests_429 = 429, - RequestHeaderFieldsTooLarge_431 = 431, - UnavailableForLegalReasons_451 = 451, - - // Server error responses - InternalServerError_500 = 500, - NotImplemented_501 = 501, - BadGateway_502 = 502, - ServiceUnavailable_503 = 503, - GatewayTimeout_504 = 504, - HttpVersionNotSupported_505 = 505, - VariantAlsoNegotiates_506 = 506, - InsufficientStorage_507 = 507, - LoopDetected_508 = 508, - NotExtended_510 = 510, - NetworkAuthenticationRequired_511 = 511, -}; - -using Headers = - std::unordered_multimap; - -using Params = std::multimap; -using Match = std::smatch; - -using Progress = std::function; - -struct Response; -using ResponseHandler = std::function; - -struct MultipartFormData { - std::string name; - std::string content; - std::string filename; - std::string content_type; -}; -using MultipartFormDataItems = std::vector; -using MultipartFormDataMap = std::multimap; - -class DataSink { -public: - DataSink() : os(&sb_), sb_(*this) {} - - DataSink(const DataSink &) = delete; - DataSink &operator=(const DataSink &) = delete; - DataSink(DataSink &&) = delete; - DataSink &operator=(DataSink &&) = delete; - - std::function write; - std::function is_writable; - std::function done; - std::function done_with_trailer; - std::ostream os; - -private: - class data_sink_streambuf final : public std::streambuf { - public: - explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} - - protected: - std::streamsize xsputn(const char *s, std::streamsize n) override { - sink_.write(s, static_cast(n)); - return n; - } - - private: - DataSink &sink_; - }; - - data_sink_streambuf sb_; -}; - -using ContentProvider = - std::function; - -using ContentProviderWithoutLength = - std::function; - -using ContentProviderResourceReleaser = std::function; - -struct MultipartFormDataProvider { - std::string name; - ContentProviderWithoutLength provider; - std::string filename; - std::string content_type; -}; -using MultipartFormDataProviderItems = std::vector; - -using ContentReceiverWithProgress = - std::function; - -using ContentReceiver = - std::function; - -using MultipartContentHeader = - std::function; - -class ContentReader { -public: - using Reader = std::function; - using MultipartReader = std::function; - - ContentReader(Reader reader, MultipartReader multipart_reader) - : reader_(std::move(reader)), - multipart_reader_(std::move(multipart_reader)) {} - - bool operator()(MultipartContentHeader header, - ContentReceiver receiver) const { - return multipart_reader_(std::move(header), std::move(receiver)); - } - - bool operator()(ContentReceiver receiver) const { - return reader_(std::move(receiver)); - } - - Reader reader_; - MultipartReader multipart_reader_; -}; - -using Range = std::pair; -using Ranges = std::vector; - -struct Request { - std::string method; - std::string path; - Params params; - Headers headers; - std::string body; - - std::string remote_addr; - int remote_port = -1; - std::string local_addr; - int local_port = -1; - - // for server - std::string version; - std::string target; - MultipartFormDataMap files; - Ranges ranges; - Match matches; - std::unordered_map path_params; - std::function is_connection_closed = []() { return true; }; - - // for client - ResponseHandler response_handler; - ContentReceiverWithProgress content_receiver; - Progress progress; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - const SSL *ssl = nullptr; -#endif - - bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, const char *def = "", - size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, - size_t id = 0) const; - size_t get_header_value_count(const std::string &key) const; - void set_header(const std::string &key, const std::string &val); - - bool has_param(const std::string &key) const; - std::string get_param_value(const std::string &key, size_t id = 0) const; - size_t get_param_value_count(const std::string &key) const; - - bool is_multipart_form_data() const; - - bool has_file(const std::string &key) const; - MultipartFormData get_file_value(const std::string &key) const; - std::vector get_file_values(const std::string &key) const; - - // private members... - size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; - size_t content_length_ = 0; - ContentProvider content_provider_; - bool is_chunked_content_provider_ = false; - size_t authorization_count_ = 0; - std::chrono::time_point start_time_ = - std::chrono::steady_clock::time_point::min(); -}; - -struct Response { - std::string version; - int status = -1; - std::string reason; - Headers headers; - std::string body; - std::string location; // Redirect location - - bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, const char *def = "", - size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, - size_t id = 0) const; - size_t get_header_value_count(const std::string &key) const; - void set_header(const std::string &key, const std::string &val); - - void set_redirect(const std::string &url, int status = StatusCode::Found_302); - void set_content(const char *s, size_t n, const std::string &content_type); - void set_content(const std::string &s, const std::string &content_type); - void set_content(std::string &&s, const std::string &content_type); - - void set_content_provider( - size_t length, const std::string &content_type, ContentProvider provider, - ContentProviderResourceReleaser resource_releaser = nullptr); - - void set_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser = nullptr); - - void set_chunked_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser = nullptr); - - void set_file_content(const std::string &path, - const std::string &content_type); - void set_file_content(const std::string &path); - - Response() = default; - Response(const Response &) = default; - Response &operator=(const Response &) = default; - Response(Response &&) = default; - Response &operator=(Response &&) = default; - ~Response() { - if (content_provider_resource_releaser_) { - content_provider_resource_releaser_(content_provider_success_); - } - } - - // private members... - size_t content_length_ = 0; - ContentProvider content_provider_; - ContentProviderResourceReleaser content_provider_resource_releaser_; - bool is_chunked_content_provider_ = false; - bool content_provider_success_ = false; - std::string file_content_path_; - std::string file_content_content_type_; -}; - -class Stream { -public: - virtual ~Stream() = default; - - virtual bool is_readable() const = 0; - virtual bool is_writable() const = 0; - - virtual ssize_t read(char *ptr, size_t size) = 0; - virtual ssize_t write(const char *ptr, size_t size) = 0; - virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; - virtual void get_local_ip_and_port(std::string &ip, int &port) const = 0; - virtual socket_t socket() const = 0; - - virtual time_t duration() const = 0; - - ssize_t write(const char *ptr); - ssize_t write(const std::string &s); -}; - -class TaskQueue { -public: - TaskQueue() = default; - virtual ~TaskQueue() = default; - - virtual bool enqueue(std::function fn) = 0; - virtual void shutdown() = 0; - - virtual void on_idle() {} -}; - -class ThreadPool final : public TaskQueue { -public: - explicit ThreadPool(size_t n, size_t mqr = 0) - : shutdown_(false), max_queued_requests_(mqr) { - while (n) { - threads_.emplace_back(worker(*this)); - n--; - } - } - - ThreadPool(const ThreadPool &) = delete; - ~ThreadPool() override = default; - - bool enqueue(std::function fn) override { - { - std::unique_lock lock(mutex_); - if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { - return false; - } - jobs_.push_back(std::move(fn)); - } - - cond_.notify_one(); - return true; - } - - void shutdown() override { - // Stop all worker threads... - { - std::unique_lock lock(mutex_); - shutdown_ = true; - } - - cond_.notify_all(); - - // Join... - for (auto &t : threads_) { - t.join(); - } - } - -private: - struct worker { - explicit worker(ThreadPool &pool) : pool_(pool) {} - - void operator()() { - for (;;) { - std::function fn; - { - std::unique_lock lock(pool_.mutex_); - - pool_.cond_.wait( - lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); - - if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } - - fn = pool_.jobs_.front(); - pool_.jobs_.pop_front(); - } - - assert(true == static_cast(fn)); - fn(); - } - -#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ - !defined(LIBRESSL_VERSION_NUMBER) - OPENSSL_thread_stop(); -#endif - } - - ThreadPool &pool_; - }; - friend struct worker; - - std::vector threads_; - std::list> jobs_; - - bool shutdown_; - size_t max_queued_requests_ = 0; - - std::condition_variable cond_; - std::mutex mutex_; -}; - -using Logger = std::function; - -using SocketOptions = std::function; - -void default_socket_options(socket_t sock); - -const char *status_message(int status); - -std::string get_bearer_token_auth(const Request &req); - -namespace detail { - -class MatcherBase { -public: - virtual ~MatcherBase() = default; - - // Match request path and populate its matches and - virtual bool match(Request &request) const = 0; -}; - -/** - * Captures parameters in request path and stores them in Request::path_params - * - * Capture name is a substring of a pattern from : to /. - * The rest of the pattern is matched agains the request path directly - * Parameters are captured starting from the next character after - * the end of the last matched static pattern fragment until the next /. - * - * Example pattern: - * "/path/fragments/:capture/more/fragments/:second_capture" - * Static fragments: - * "/path/fragments/", "more/fragments/" - * - * Given the following request path: - * "/path/fragments/:1/more/fragments/:2" - * the resulting capture will be - * {{"capture", "1"}, {"second_capture", "2"}} - */ -class PathParamsMatcher final : public MatcherBase { -public: - PathParamsMatcher(const std::string &pattern); - - bool match(Request &request) const override; - -private: - // Treat segment separators as the end of path parameter capture - // Does not need to handle query parameters as they are parsed before path - // matching - static constexpr char separator = '/'; - - // Contains static path fragments to match against, excluding the '/' after - // path params - // Fragments are separated by path params - std::vector static_fragments_; - // Stores the names of the path parameters to be used as keys in the - // Request::path_params map - std::vector param_names_; -}; - -/** - * Performs std::regex_match on request path - * and stores the result in Request::matches - * - * Note that regex match is performed directly on the whole request. - * This means that wildcard patterns may match multiple path segments with /: - * "/begin/(.*)/end" will match both "/begin/middle/end" and "/begin/1/2/end". - */ -class RegexMatcher final : public MatcherBase { -public: - RegexMatcher(const std::string &pattern) : regex_(pattern) {} - - bool match(Request &request) const override; - -private: - std::regex regex_; -}; - -ssize_t write_headers(Stream &strm, const Headers &headers); - -} // namespace detail - -class Server { -public: - using Handler = std::function; - - using ExceptionHandler = - std::function; - - enum class HandlerResponse { - Handled, - Unhandled, - }; - using HandlerWithResponse = - std::function; - - using HandlerWithContentReader = std::function; - - using Expect100ContinueHandler = - std::function; - - Server(); - - virtual ~Server(); - - virtual bool is_valid() const; - - Server &Get(const std::string &pattern, Handler handler); - Server &Post(const std::string &pattern, Handler handler); - Server &Post(const std::string &pattern, HandlerWithContentReader handler); - Server &Put(const std::string &pattern, Handler handler); - Server &Put(const std::string &pattern, HandlerWithContentReader handler); - Server &Patch(const std::string &pattern, Handler handler); - Server &Patch(const std::string &pattern, HandlerWithContentReader handler); - Server &Delete(const std::string &pattern, Handler handler); - Server &Delete(const std::string &pattern, HandlerWithContentReader handler); - Server &Options(const std::string &pattern, Handler handler); - - bool set_base_dir(const std::string &dir, - const std::string &mount_point = std::string()); - bool set_mount_point(const std::string &mount_point, const std::string &dir, - Headers headers = Headers()); - bool remove_mount_point(const std::string &mount_point); - Server &set_file_extension_and_mimetype_mapping(const std::string &ext, - const std::string &mime); - Server &set_default_file_mimetype(const std::string &mime); - Server &set_file_request_handler(Handler handler); - - template - Server &set_error_handler(ErrorHandlerFunc &&handler) { - return set_error_handler_core( - std::forward(handler), - std::is_convertible{}); - } - - Server &set_exception_handler(ExceptionHandler handler); - Server &set_pre_routing_handler(HandlerWithResponse handler); - Server &set_post_routing_handler(Handler handler); - - Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); - Server &set_logger(Logger logger); - - Server &set_address_family(int family); - Server &set_tcp_nodelay(bool on); - Server &set_ipv6_v6only(bool on); - Server &set_socket_options(SocketOptions socket_options); - - Server &set_default_headers(Headers headers); - Server & - set_header_writer(std::function const &writer); - - Server &set_keep_alive_max_count(size_t count); - Server &set_keep_alive_timeout(time_t sec); - - Server &set_read_timeout(time_t sec, time_t usec = 0); - template - Server &set_read_timeout(const std::chrono::duration &duration); - - Server &set_write_timeout(time_t sec, time_t usec = 0); - template - Server &set_write_timeout(const std::chrono::duration &duration); - - Server &set_idle_interval(time_t sec, time_t usec = 0); - template - Server &set_idle_interval(const std::chrono::duration &duration); - - Server &set_payload_max_length(size_t length); - - bool bind_to_port(const std::string &host, int port, int socket_flags = 0); - int bind_to_any_port(const std::string &host, int socket_flags = 0); - bool listen_after_bind(); - - bool listen(const std::string &host, int port, int socket_flags = 0); - - bool is_running() const; - void wait_until_ready() const; - void stop(); - void decommission(); - - std::function new_task_queue; - -protected: - bool process_request(Stream &strm, const std::string &remote_addr, - int remote_port, const std::string &local_addr, - int local_port, bool close_connection, - bool &connection_closed, - const std::function &setup_request); - - std::atomic svr_sock_{INVALID_SOCKET}; - size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; - time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND; - time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; - time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; - size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; - -private: - using Handlers = - std::vector, Handler>>; - using HandlersForContentReader = - std::vector, - HandlerWithContentReader>>; - - static std::unique_ptr - make_matcher(const std::string &pattern); - - Server &set_error_handler_core(HandlerWithResponse handler, std::true_type); - Server &set_error_handler_core(Handler handler, std::false_type); - - socket_t create_server_socket(const std::string &host, int port, - int socket_flags, - SocketOptions socket_options) const; - int bind_internal(const std::string &host, int port, int socket_flags); - bool listen_internal(); - - bool routing(Request &req, Response &res, Stream &strm); - bool handle_file_request(const Request &req, Response &res, - bool head = false); - bool dispatch_request(Request &req, Response &res, - const Handlers &handlers) const; - bool dispatch_request_for_content_reader( - Request &req, Response &res, ContentReader content_reader, - const HandlersForContentReader &handlers) const; - - bool parse_request_line(const char *s, Request &req) const; - void apply_ranges(const Request &req, Response &res, - std::string &content_type, std::string &boundary) const; - bool write_response(Stream &strm, bool close_connection, Request &req, - Response &res); - bool write_response_with_content(Stream &strm, bool close_connection, - const Request &req, Response &res); - bool write_response_core(Stream &strm, bool close_connection, - const Request &req, Response &res, - bool need_apply_ranges); - bool write_content_with_provider(Stream &strm, const Request &req, - Response &res, const std::string &boundary, - const std::string &content_type); - bool read_content(Stream &strm, Request &req, Response &res); - bool - read_content_with_content_receiver(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver); - bool read_content_core(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) const; - - virtual bool process_and_close_socket(socket_t sock); - - std::atomic is_running_{false}; - std::atomic is_decommisioned{false}; - - struct MountPointEntry { - std::string mount_point; - std::string base_dir; - Headers headers; - }; - std::vector base_dirs_; - std::map file_extension_and_mimetype_map_; - std::string default_file_mimetype_ = "application/octet-stream"; - Handler file_request_handler_; - - Handlers get_handlers_; - Handlers post_handlers_; - HandlersForContentReader post_handlers_for_content_reader_; - Handlers put_handlers_; - HandlersForContentReader put_handlers_for_content_reader_; - Handlers patch_handlers_; - HandlersForContentReader patch_handlers_for_content_reader_; - Handlers delete_handlers_; - HandlersForContentReader delete_handlers_for_content_reader_; - Handlers options_handlers_; - - HandlerWithResponse error_handler_; - ExceptionHandler exception_handler_; - HandlerWithResponse pre_routing_handler_; - Handler post_routing_handler_; - Expect100ContinueHandler expect_100_continue_handler_; - - Logger logger_; - - int address_family_ = AF_UNSPEC; - bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; - bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; - SocketOptions socket_options_ = default_socket_options; - - Headers default_headers_; - std::function header_writer_ = - detail::write_headers; -}; - -enum class Error { - Success = 0, - Unknown, - Connection, - BindIPAddress, - Read, - Write, - ExceedRedirectCount, - Canceled, - SSLConnection, - SSLLoadingCerts, - SSLServerVerification, - SSLServerHostnameVerification, - UnsupportedMultipartBoundaryChars, - Compression, - ConnectionTimeout, - ProxyConnection, - - // For internal use only - SSLPeerCouldBeClosed_, -}; - -std::string to_string(Error error); - -std::ostream &operator<<(std::ostream &os, const Error &obj); - -class Result { -public: - Result() = default; - Result(std::unique_ptr &&res, Error err, - Headers &&request_headers = Headers{}) - : res_(std::move(res)), err_(err), - request_headers_(std::move(request_headers)) {} - // Response - operator bool() const { return res_ != nullptr; } - bool operator==(std::nullptr_t) const { return res_ == nullptr; } - bool operator!=(std::nullptr_t) const { return res_ != nullptr; } - const Response &value() const { return *res_; } - Response &value() { return *res_; } - const Response &operator*() const { return *res_; } - Response &operator*() { return *res_; } - const Response *operator->() const { return res_.get(); } - Response *operator->() { return res_.get(); } - - // Error - Error error() const { return err_; } - - // Request Headers - bool has_request_header(const std::string &key) const; - std::string get_request_header_value(const std::string &key, - const char *def = "", - size_t id = 0) const; - uint64_t get_request_header_value_u64(const std::string &key, - uint64_t def = 0, size_t id = 0) const; - size_t get_request_header_value_count(const std::string &key) const; - -private: - std::unique_ptr res_; - Error err_ = Error::Unknown; - Headers request_headers_; -}; - -class ClientImpl { -public: - explicit ClientImpl(const std::string &host); - - explicit ClientImpl(const std::string &host, int port); - - explicit ClientImpl(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); - - virtual ~ClientImpl(); - - virtual bool is_valid() const; - - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, - Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress = nullptr); - - Result Head(const std::string &path); - Result Head(const std::string &path, const Headers &headers); - - Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Post(const std::string &path, const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Put(const std::string &path, const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - - Result Options(const std::string &path); - Result Options(const std::string &path, const Headers &headers); - - bool send(Request &req, Response &res, Error &error); - Result send(const Request &req); - - void stop(); - - std::string host() const; - int port() const; - - size_t is_socket_open() const; - socket_t socket() const; - - void set_hostname_addr_map(std::map addr_map); - - void set_default_headers(Headers headers); - - void - set_header_writer(std::function const &writer); - - void set_address_family(int family); - void set_tcp_nodelay(bool on); - void set_ipv6_v6only(bool on); - void set_socket_options(SocketOptions socket_options); - - void set_connection_timeout(time_t sec, time_t usec = 0); - template - void - set_connection_timeout(const std::chrono::duration &duration); - - void set_read_timeout(time_t sec, time_t usec = 0); - template - void set_read_timeout(const std::chrono::duration &duration); - - void set_write_timeout(time_t sec, time_t usec = 0); - template - void set_write_timeout(const std::chrono::duration &duration); - - void set_max_timeout(time_t msec); - template - void set_max_timeout(const std::chrono::duration &duration); - - void set_basic_auth(const std::string &username, const std::string &password); - void set_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); -#endif - - void set_keep_alive(bool on); - void set_follow_location(bool on); - - void set_url_encode(bool on); - - void set_compress(bool on); - - void set_decompress(bool on); - - void set_interface(const std::string &intf); - - void set_proxy(const std::string &host, int port); - void set_proxy_basic_auth(const std::string &username, - const std::string &password); - void set_proxy_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); - void set_ca_cert_store(X509_STORE *ca_cert_store); - X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void enable_server_certificate_verification(bool enabled); - void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier(std::function verifier); -#endif - - void set_logger(Logger logger); - -protected: - struct Socket { - socket_t sock = INVALID_SOCKET; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSL *ssl = nullptr; -#endif - - bool is_open() const { return sock != INVALID_SOCKET; } - }; - - virtual bool create_and_connect_socket(Socket &socket, Error &error); - - // All of: - // shutdown_ssl - // shutdown_socket - // close_socket - // should ONLY be called when socket_mutex_ is locked. - // Also, shutdown_ssl and close_socket should also NOT be called concurrently - // with a DIFFERENT thread sending requests using that socket. - virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); - void shutdown_socket(Socket &socket) const; - void close_socket(Socket &socket); - - bool process_request(Stream &strm, Request &req, Response &res, - bool close_connection, Error &error); - - bool write_content_with_provider(Stream &strm, const Request &req, - Error &error) const; - - void copy_settings(const ClientImpl &rhs); - - // Socket endpoint information - const std::string host_; - const int port_; - const std::string host_and_port_; - - // Current open socket - Socket socket_; - mutable std::mutex socket_mutex_; - std::recursive_mutex request_mutex_; - - // These are all protected under socket_mutex - size_t socket_requests_in_flight_ = 0; - std::thread::id socket_requests_are_from_thread_ = std::thread::id(); - bool socket_should_be_closed_when_request_is_done_ = false; - - // Hostname-IP map - std::map addr_map_; - - // Default headers - Headers default_headers_; - - // Header writer - std::function header_writer_ = - detail::write_headers; - - // Settings - std::string client_cert_path_; - std::string client_key_path_; - - time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; - time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; - time_t read_timeout_sec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_SECOND; - time_t read_timeout_usec_ = CPPHTTPLIB_CLIENT_READ_TIMEOUT_USECOND; - time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND; - time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND; - time_t max_timeout_msec_ = CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND; - - std::string basic_auth_username_; - std::string basic_auth_password_; - std::string bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string digest_auth_username_; - std::string digest_auth_password_; -#endif - - bool keep_alive_ = false; - bool follow_location_ = false; - - bool url_encode_ = true; - - int address_family_ = AF_UNSPEC; - bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; - bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY; - SocketOptions socket_options_ = nullptr; - - bool compress_ = false; - bool decompress_ = true; - - std::string interface_; - - std::string proxy_host_; - int proxy_port_ = -1; - - std::string proxy_basic_auth_username_; - std::string proxy_basic_auth_password_; - std::string proxy_bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string proxy_digest_auth_username_; - std::string proxy_digest_auth_password_; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string ca_cert_file_path_; - std::string ca_cert_dir_path_; - - X509_STORE *ca_cert_store_ = nullptr; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool server_certificate_verification_ = true; - bool server_hostname_verification_ = true; - std::function server_certificate_verifier_; -#endif - - Logger logger_; - -private: - bool send_(Request &req, Response &res, Error &error); - Result send_(Request &&req); - - socket_t create_client_socket(Error &error) const; - bool read_response_line(Stream &strm, const Request &req, - Response &res) const; - bool write_request(Stream &strm, Request &req, bool close_connection, - Error &error); - bool redirect(Request &req, Response &res, Error &error); - bool handle_request(Stream &strm, Request &req, Response &res, - bool close_connection, Error &error); - std::unique_ptr send_with_content_provider( - Request &req, const char *body, size_t content_length, - ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error); - Result send_with_content_provider( - const std::string &method, const std::string &path, - const Headers &headers, const char *body, size_t content_length, - ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Progress progress); - ContentProviderWithoutLength get_multipart_content_provider( - const std::string &boundary, const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) const; - - std::string adjust_host_string(const std::string &host) const; - - virtual bool - process_socket(const Socket &socket, - std::chrono::time_point start_time, - std::function callback); - virtual bool is_ssl() const; -}; - -class Client { -public: - // Universal interface - explicit Client(const std::string &scheme_host_port); - - explicit Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path); - - // HTTP only interface - explicit Client(const std::string &host, int port); - - explicit Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); - - Client(Client &&) = default; - Client &operator=(Client &&) = default; - - ~Client(); - - bool is_valid() const; - - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, - Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, ContentReceiver content_receiver, - Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress); - - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, - Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress = nullptr); - - Result Head(const std::string &path); - Result Head(const std::string &path, const Headers &headers); - - Result Post(const std::string &path); - Result Post(const std::string &path, const Headers &headers); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Post(const std::string &path, const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress); - Result Put(const std::string &path, const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items); - - Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - Result Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type, - Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type, - Progress progress); - - Result Options(const std::string &path); - Result Options(const std::string &path, const Headers &headers); - - bool send(Request &req, Response &res, Error &error); - Result send(const Request &req); - - void stop(); - - std::string host() const; - int port() const; - - size_t is_socket_open() const; - socket_t socket() const; - - void set_hostname_addr_map(std::map addr_map); - - void set_default_headers(Headers headers); - - void - set_header_writer(std::function const &writer); - - void set_address_family(int family); - void set_tcp_nodelay(bool on); - void set_socket_options(SocketOptions socket_options); - - void set_connection_timeout(time_t sec, time_t usec = 0); - template - void - set_connection_timeout(const std::chrono::duration &duration); - - void set_read_timeout(time_t sec, time_t usec = 0); - template - void set_read_timeout(const std::chrono::duration &duration); - - void set_write_timeout(time_t sec, time_t usec = 0); - template - void set_write_timeout(const std::chrono::duration &duration); - - void set_max_timeout(time_t msec); - template - void set_max_timeout(const std::chrono::duration &duration); - - void set_basic_auth(const std::string &username, const std::string &password); - void set_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); -#endif - - void set_keep_alive(bool on); - void set_follow_location(bool on); - - void set_url_encode(bool on); - - void set_compress(bool on); - - void set_decompress(bool on); - - void set_interface(const std::string &intf); - - void set_proxy(const std::string &host, int port); - void set_proxy_basic_auth(const std::string &username, - const std::string &password); - void set_proxy_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void enable_server_certificate_verification(bool enabled); - void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier(std::function verifier); -#endif - - void set_logger(Logger logger); - - // SSL -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); - - void set_ca_cert_store(X509_STORE *ca_cert_store); - void load_ca_cert_store(const char *ca_cert, std::size_t size); - - long get_openssl_verify_result() const; - - SSL_CTX *ssl_context() const; -#endif - -private: - std::unique_ptr cli_; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool is_ssl_ = false; -#endif -}; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLServer : public Server { -public: - SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path = nullptr, - const char *client_ca_cert_dir_path = nullptr, - const char *private_key_password = nullptr); - - SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); - - SSLServer( - const std::function &setup_ssl_ctx_callback); - - ~SSLServer() override; - - bool is_valid() const override; - - SSL_CTX *ssl_context() const; - - void update_certs(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); - -private: - bool process_and_close_socket(socket_t sock) override; - - SSL_CTX *ctx_; - std::mutex ctx_mutex_; -}; - -class SSLClient final : public ClientImpl { -public: - explicit SSLClient(const std::string &host); - - explicit SSLClient(const std::string &host, int port); - - explicit SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path, - const std::string &private_key_password = std::string()); - - explicit SSLClient(const std::string &host, int port, X509 *client_cert, - EVP_PKEY *client_key, - const std::string &private_key_password = std::string()); - - ~SSLClient() override; - - bool is_valid() const override; - - void set_ca_cert_store(X509_STORE *ca_cert_store); - void load_ca_cert_store(const char *ca_cert, std::size_t size); - - long get_openssl_verify_result() const; - - SSL_CTX *ssl_context() const; - -private: - bool create_and_connect_socket(Socket &socket, Error &error) override; - void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; - void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); - - bool - process_socket(const Socket &socket, - std::chrono::time_point start_time, - std::function callback) override; - bool is_ssl() const override; - - bool connect_with_proxy( - Socket &sock, - std::chrono::time_point start_time, - Response &res, bool &success, Error &error); - bool initialize_ssl(Socket &socket, Error &error); - - bool load_certs(); - - bool verify_host(X509 *server_cert) const; - bool verify_host_with_subject_alt_name(X509 *server_cert) const; - bool verify_host_with_common_name(X509 *server_cert) const; - bool check_host_name(const char *pattern, size_t pattern_len) const; - - SSL_CTX *ctx_; - std::mutex ctx_mutex_; - std::once_flag initialize_cert_; - - std::vector host_components_; - - long verify_result_ = 0; - - friend class ClientImpl; -}; -#endif - -/* - * Implementation of template methods. - */ - -namespace detail { - -template -inline void duration_to_sec_and_usec(const T &duration, U callback) { - auto sec = std::chrono::duration_cast(duration).count(); - auto usec = std::chrono::duration_cast( - duration - std::chrono::seconds(sec)) - .count(); - callback(static_cast(sec), static_cast(usec)); -} - -inline bool is_numeric(const std::string &str) { - return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit); -} - -inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, uint64_t def, - size_t id, bool &is_invalid_value) { - is_invalid_value = false; - auto rng = headers.equal_range(key); - auto it = rng.first; - std::advance(it, static_cast(id)); - if (it != rng.second) { - if (is_numeric(it->second)) { - return std::strtoull(it->second.data(), nullptr, 10); - } else { - is_invalid_value = true; - } - } - return def; -} - -inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, uint64_t def, - size_t id) { - bool dummy = false; - return get_header_value_u64(headers, key, def, id, dummy); -} - -} // namespace detail - -inline uint64_t Request::get_header_value_u64(const std::string &key, - uint64_t def, size_t id) const { - return detail::get_header_value_u64(headers, key, def, id); -} - -inline uint64_t Response::get_header_value_u64(const std::string &key, - uint64_t def, size_t id) const { - return detail::get_header_value_u64(headers, key, def, id); -} - -inline void default_socket_options(socket_t sock) { - int opt = 1; -#ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); -#else -#ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - reinterpret_cast(&opt), sizeof(opt)); -#endif -#endif -} - -inline const char *status_message(int status) { - switch (status) { - case StatusCode::Continue_100: return "Continue"; - case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; - case StatusCode::Processing_102: return "Processing"; - case StatusCode::EarlyHints_103: return "Early Hints"; - case StatusCode::OK_200: return "OK"; - case StatusCode::Created_201: return "Created"; - case StatusCode::Accepted_202: return "Accepted"; - case StatusCode::NonAuthoritativeInformation_203: - return "Non-Authoritative Information"; - case StatusCode::NoContent_204: return "No Content"; - case StatusCode::ResetContent_205: return "Reset Content"; - case StatusCode::PartialContent_206: return "Partial Content"; - case StatusCode::MultiStatus_207: return "Multi-Status"; - case StatusCode::AlreadyReported_208: return "Already Reported"; - case StatusCode::IMUsed_226: return "IM Used"; - case StatusCode::MultipleChoices_300: return "Multiple Choices"; - case StatusCode::MovedPermanently_301: return "Moved Permanently"; - case StatusCode::Found_302: return "Found"; - case StatusCode::SeeOther_303: return "See Other"; - case StatusCode::NotModified_304: return "Not Modified"; - case StatusCode::UseProxy_305: return "Use Proxy"; - case StatusCode::unused_306: return "unused"; - case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; - case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; - case StatusCode::BadRequest_400: return "Bad Request"; - case StatusCode::Unauthorized_401: return "Unauthorized"; - case StatusCode::PaymentRequired_402: return "Payment Required"; - case StatusCode::Forbidden_403: return "Forbidden"; - case StatusCode::NotFound_404: return "Not Found"; - case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; - case StatusCode::NotAcceptable_406: return "Not Acceptable"; - case StatusCode::ProxyAuthenticationRequired_407: - return "Proxy Authentication Required"; - case StatusCode::RequestTimeout_408: return "Request Timeout"; - case StatusCode::Conflict_409: return "Conflict"; - case StatusCode::Gone_410: return "Gone"; - case StatusCode::LengthRequired_411: return "Length Required"; - case StatusCode::PreconditionFailed_412: return "Precondition Failed"; - case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; - case StatusCode::UriTooLong_414: return "URI Too Long"; - case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; - case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; - case StatusCode::ExpectationFailed_417: return "Expectation Failed"; - case StatusCode::ImATeapot_418: return "I'm a teapot"; - case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; - case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; - case StatusCode::Locked_423: return "Locked"; - case StatusCode::FailedDependency_424: return "Failed Dependency"; - case StatusCode::TooEarly_425: return "Too Early"; - case StatusCode::UpgradeRequired_426: return "Upgrade Required"; - case StatusCode::PreconditionRequired_428: return "Precondition Required"; - case StatusCode::TooManyRequests_429: return "Too Many Requests"; - case StatusCode::RequestHeaderFieldsTooLarge_431: - return "Request Header Fields Too Large"; - case StatusCode::UnavailableForLegalReasons_451: - return "Unavailable For Legal Reasons"; - case StatusCode::NotImplemented_501: return "Not Implemented"; - case StatusCode::BadGateway_502: return "Bad Gateway"; - case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; - case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; - case StatusCode::HttpVersionNotSupported_505: - return "HTTP Version Not Supported"; - case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; - case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; - case StatusCode::LoopDetected_508: return "Loop Detected"; - case StatusCode::NotExtended_510: return "Not Extended"; - case StatusCode::NetworkAuthenticationRequired_511: - return "Network Authentication Required"; - - default: - case StatusCode::InternalServerError_500: return "Internal Server Error"; - } -} - -inline std::string get_bearer_token_auth(const Request &req) { - if (req.has_header("Authorization")) { - static std::string BearerHeaderPrefix = "Bearer "; - return req.get_header_value("Authorization") - .substr(BearerHeaderPrefix.length()); - } - return ""; -} - -template -inline Server & -Server::set_read_timeout(const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); - return *this; -} - -template -inline Server & -Server::set_write_timeout(const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); - return *this; -} - -template -inline Server & -Server::set_idle_interval(const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); - return *this; -} - -inline std::string to_string(const Error error) { - switch (error) { - case Error::Success: return "Success (no error)"; - case Error::Connection: return "Could not establish connection"; - case Error::BindIPAddress: return "Failed to bind IP address"; - case Error::Read: return "Failed to read connection"; - case Error::Write: return "Failed to write connection"; - case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; - case Error::Canceled: return "Connection handling canceled"; - case Error::SSLConnection: return "SSL connection failed"; - case Error::SSLLoadingCerts: return "SSL certificate loading failed"; - case Error::SSLServerVerification: return "SSL server verification failed"; - case Error::SSLServerHostnameVerification: - return "SSL server hostname verification failed"; - case Error::UnsupportedMultipartBoundaryChars: - return "Unsupported HTTP multipart boundary characters"; - case Error::Compression: return "Compression failed"; - case Error::ConnectionTimeout: return "Connection timed out"; - case Error::ProxyConnection: return "Proxy connection failed"; - case Error::Unknown: return "Unknown"; - default: break; - } - - return "Invalid"; -} - -inline std::ostream &operator<<(std::ostream &os, const Error &obj) { - os << to_string(obj); - os << " (" << static_cast::type>(obj) << ')'; - return os; -} - -inline uint64_t Result::get_request_header_value_u64(const std::string &key, - uint64_t def, - size_t id) const { - return detail::get_header_value_u64(request_headers_, key, def, id); -} - -template -inline void ClientImpl::set_connection_timeout( - const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { - set_connection_timeout(sec, usec); - }); -} - -template -inline void ClientImpl::set_read_timeout( - const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); -} - -template -inline void ClientImpl::set_write_timeout( - const std::chrono::duration &duration) { - detail::duration_to_sec_and_usec( - duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); -} - -template -inline void ClientImpl::set_max_timeout( - const std::chrono::duration &duration) { - auto msec = - std::chrono::duration_cast(duration).count(); - set_max_timeout(msec); -} - -template -inline void Client::set_connection_timeout( - const std::chrono::duration &duration) { - cli_->set_connection_timeout(duration); -} - -template -inline void -Client::set_read_timeout(const std::chrono::duration &duration) { - cli_->set_read_timeout(duration); -} - -template -inline void -Client::set_write_timeout(const std::chrono::duration &duration) { - cli_->set_write_timeout(duration); -} - -template -inline void -Client::set_max_timeout(const std::chrono::duration &duration) { - cli_->set_max_timeout(duration); -} - -/* - * Forward declarations and types that will be part of the .h file if split into - * .h + .cc. - */ - -std::string hosted_at(const std::string &hostname); - -void hosted_at(const std::string &hostname, std::vector &addrs); - -std::string append_query_params(const std::string &path, const Params ¶ms); - -std::pair make_range_header(const Ranges &ranges); - -std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, - bool is_proxy = false); - -namespace detail { - -#if defined(_WIN32) -inline std::wstring u8string_to_wstring(const char *s) { - std::wstring ws; - auto len = static_cast(strlen(s)); - auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); - if (wlen > 0) { - ws.resize(wlen); - wlen = ::MultiByteToWideChar( - CP_UTF8, 0, s, len, - const_cast(reinterpret_cast(ws.data())), wlen); - if (wlen != static_cast(ws.size())) { ws.clear(); } - } - return ws; -} -#endif - -struct FileStat { - FileStat(const std::string &path); - bool is_file() const; - bool is_dir() const; - -private: -#if defined(_WIN32) - struct _stat st_; -#else - struct stat st_; -#endif - int ret_ = -1; -}; - -std::string encode_query_param(const std::string &value); - -std::string decode_url(const std::string &s, bool convert_plus_to_space); - -void read_file(const std::string &path, std::string &out); - -std::string trim_copy(const std::string &s); - -void divide( - const char *data, std::size_t size, char d, - std::function - fn); - -void divide( - const std::string &str, char d, - std::function - fn); - -void split(const char *b, const char *e, char d, - std::function fn); - -void split(const char *b, const char *e, char d, size_t m, - std::function fn); - -bool process_client_socket( - socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time, - std::function callback); - -socket_t create_client_socket(const std::string &host, const std::string &ip, - int port, int address_family, bool tcp_nodelay, - bool ipv6_v6only, SocketOptions socket_options, - time_t connection_timeout_sec, - time_t connection_timeout_usec, - time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, - time_t write_timeout_usec, - const std::string &intf, Error &error); - -const char *get_header_value(const Headers &headers, const std::string &key, - const char *def, size_t id); - -std::string params_to_query_str(const Params ¶ms); - -void parse_query_text(const char *data, std::size_t size, Params ¶ms); - -void parse_query_text(const std::string &s, Params ¶ms); - -bool parse_multipart_boundary(const std::string &content_type, - std::string &boundary); - -bool parse_range_header(const std::string &s, Ranges &ranges); - -int close_socket(socket_t sock); - -ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); - -ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); - -enum class EncodingType { None = 0, Gzip, Brotli }; - -EncodingType encoding_type(const Request &req, const Response &res); - -class BufferStream final : public Stream { -public: - BufferStream() = default; - ~BufferStream() override = default; - - bool is_readable() const override; - bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - time_t duration() const override; - - const std::string &get_buffer() const; - -private: - std::string buffer; - size_t position = 0; -}; - -class compressor { -public: - virtual ~compressor() = default; - - typedef std::function Callback; - virtual bool compress(const char *data, size_t data_length, bool last, - Callback callback) = 0; -}; - -class decompressor { -public: - virtual ~decompressor() = default; - - virtual bool is_valid() const = 0; - - typedef std::function Callback; - virtual bool decompress(const char *data, size_t data_length, - Callback callback) = 0; -}; - -class nocompressor final : public compressor { -public: - ~nocompressor() override = default; - - bool compress(const char *data, size_t data_length, bool /*last*/, - Callback callback) override; -}; - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -class gzip_compressor final : public compressor { -public: - gzip_compressor(); - ~gzip_compressor() override; - - bool compress(const char *data, size_t data_length, bool last, - Callback callback) override; - -private: - bool is_valid_ = false; - z_stream strm_; -}; - -class gzip_decompressor final : public decompressor { -public: - gzip_decompressor(); - ~gzip_decompressor() override; - - bool is_valid() const override; - - bool decompress(const char *data, size_t data_length, - Callback callback) override; - -private: - bool is_valid_ = false; - z_stream strm_; -}; -#endif - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT -class brotli_compressor final : public compressor { -public: - brotli_compressor(); - ~brotli_compressor(); - - bool compress(const char *data, size_t data_length, bool last, - Callback callback) override; - -private: - BrotliEncoderState *state_ = nullptr; -}; - -class brotli_decompressor final : public decompressor { -public: - brotli_decompressor(); - ~brotli_decompressor(); - - bool is_valid() const override; - - bool decompress(const char *data, size_t data_length, - Callback callback) override; - -private: - BrotliDecoderResult decoder_r; - BrotliDecoderState *decoder_s = nullptr; -}; -#endif - -// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` -// to store data. The call can set memory on stack for performance. -class stream_line_reader { -public: - stream_line_reader(Stream &strm, char *fixed_buffer, - size_t fixed_buffer_size); - const char *ptr() const; - size_t size() const; - bool end_with_crlf() const; - bool getline(); - -private: - void append(char c); - - Stream &strm_; - char *fixed_buffer_; - const size_t fixed_buffer_size_; - size_t fixed_buffer_used_size_ = 0; - std::string glowable_buffer_; -}; - -class mmap { -public: - mmap(const char *path); - ~mmap(); - - bool open(const char *path); - void close(); - - bool is_open() const; - size_t size() const; - const char *data() const; - -private: -#if defined(_WIN32) - HANDLE hFile_ = NULL; - HANDLE hMapping_ = NULL; -#else - int fd_ = -1; -#endif - size_t size_ = 0; - void *addr_ = nullptr; - bool is_open_empty_file = false; -}; - -// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 -namespace fields { - -inline bool is_token_char(char c) { - return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || - c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || - c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; -} - -inline bool is_token(const std::string &s) { - if (s.empty()) { return false; } - for (auto c : s) { - if (!is_token_char(c)) { return false; } - } - return true; -} - -inline bool is_field_name(const std::string &s) { return is_token(s); } - -inline bool is_vchar(char c) { return c >= 33 && c <= 126; } - -inline bool is_obs_text(char c) { return 128 <= static_cast(c); } - -inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } - -inline bool is_field_content(const std::string &s) { - if (s.empty()) { return true; } - - if (s.size() == 1) { - return is_field_vchar(s[0]); - } else if (s.size() == 2) { - return is_field_vchar(s[0]) && is_field_vchar(s[1]); - } else { - size_t i = 0; - - if (!is_field_vchar(s[i])) { return false; } - i++; - - while (i < s.size() - 1) { - auto c = s[i++]; - if (c == ' ' || c == '\t' || is_field_vchar(c)) { - } else { - return false; - } - } - - return is_field_vchar(s[i]); - } -} - -inline bool is_field_value(const std::string &s) { return is_field_content(s); } - -} // namespace fields - -} // namespace detail - -// ---------------------------------------------------------------------------- - -/* - * Implementation that will be part of the .cc file if split into .h + .cc. - */ - -namespace detail { - -inline bool is_hex(char c, int &v) { - if (0x20 <= c && isdigit(c)) { - v = c - '0'; - return true; - } else if ('A' <= c && c <= 'F') { - v = c - 'A' + 10; - return true; - } else if ('a' <= c && c <= 'f') { - v = c - 'a' + 10; - return true; - } - return false; -} - -inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, - int &val) { - if (i >= s.size()) { return false; } - - val = 0; - for (; cnt; i++, cnt--) { - if (!s[i]) { return false; } - auto v = 0; - if (is_hex(s[i], v)) { - val = val * 16 + v; - } else { - return false; - } - } - return true; -} - -inline std::string from_i_to_hex(size_t n) { - static const auto charset = "0123456789abcdef"; - std::string ret; - do { - ret = charset[n & 15] + ret; - n >>= 4; - } while (n > 0); - return ret; -} - -inline size_t to_utf8(int code, char *buff) { - if (code < 0x0080) { - buff[0] = static_cast(code & 0x7F); - return 1; - } else if (code < 0x0800) { - buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); - buff[1] = static_cast(0x80 | (code & 0x3F)); - return 2; - } else if (code < 0xD800) { - buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); - buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[2] = static_cast(0x80 | (code & 0x3F)); - return 3; - } else if (code < 0xE000) { // D800 - DFFF is invalid... - return 0; - } else if (code < 0x10000) { - buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); - buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[2] = static_cast(0x80 | (code & 0x3F)); - return 3; - } else if (code < 0x110000) { - buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); - buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); - buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[3] = static_cast(0x80 | (code & 0x3F)); - return 4; - } - - // NOTREACHED - return 0; -} - -// NOTE: This code came up with the following stackoverflow post: -// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c -inline std::string base64_encode(const std::string &in) { - static const auto lookup = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - std::string out; - out.reserve(in.size()); - - auto val = 0; - auto valb = -6; - - for (auto c : in) { - val = (val << 8) + static_cast(c); - valb += 8; - while (valb >= 0) { - out.push_back(lookup[(val >> valb) & 0x3F]); - valb -= 6; - } - } - - if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } - - while (out.size() % 4) { - out.push_back('='); - } - - return out; -} - -inline bool is_valid_path(const std::string &path) { - size_t level = 0; - size_t i = 0; - - // Skip slash - while (i < path.size() && path[i] == '/') { - i++; - } - - while (i < path.size()) { - // Read component - auto beg = i; - while (i < path.size() && path[i] != '/') { - if (path[i] == '\0') { - return false; - } else if (path[i] == '\\') { - return false; - } - i++; - } - - auto len = i - beg; - assert(len > 0); - - if (!path.compare(beg, len, ".")) { - ; - } else if (!path.compare(beg, len, "..")) { - if (level == 0) { return false; } - level--; - } else { - level++; - } - - // Skip slash - while (i < path.size() && path[i] == '/') { - i++; - } - } - - return true; -} - -inline FileStat::FileStat(const std::string &path) { -#if defined(_WIN32) - auto wpath = u8string_to_wstring(path.c_str()); - ret_ = _wstat(wpath.c_str(), &st_); -#else - ret_ = stat(path.c_str(), &st_); -#endif -} -inline bool FileStat::is_file() const { - return ret_ >= 0 && S_ISREG(st_.st_mode); -} -inline bool FileStat::is_dir() const { - return ret_ >= 0 && S_ISDIR(st_.st_mode); -} - -inline std::string encode_query_param(const std::string &value) { - std::ostringstream escaped; - escaped.fill('0'); - escaped << std::hex; - - for (auto c : value) { - if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || - c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || - c == ')') { - escaped << c; - } else { - escaped << std::uppercase; - escaped << '%' << std::setw(2) - << static_cast(static_cast(c)); - escaped << std::nouppercase; - } - } - - return escaped.str(); -} - -inline std::string encode_url(const std::string &s) { - std::string result; - result.reserve(s.size()); - - for (size_t i = 0; s[i]; i++) { - switch (s[i]) { - case ' ': result += "%20"; break; - case '+': result += "%2B"; break; - case '\r': result += "%0D"; break; - case '\n': result += "%0A"; break; - case '\'': result += "%27"; break; - case ',': result += "%2C"; break; - // case ':': result += "%3A"; break; // ok? probably... - case ';': result += "%3B"; break; - default: - auto c = static_cast(s[i]); - if (c >= 0x80) { - result += '%'; - char hex[4]; - auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); - assert(len == 2); - result.append(hex, static_cast(len)); - } else { - result += s[i]; - } - break; - } - } - - return result; -} - -inline std::string decode_url(const std::string &s, - bool convert_plus_to_space) { - std::string result; - - for (size_t i = 0; i < s.size(); i++) { - if (s[i] == '%' && i + 1 < s.size()) { - if (s[i + 1] == 'u') { - auto val = 0; - if (from_hex_to_i(s, i + 2, 4, val)) { - // 4 digits Unicode codes - char buff[4]; - size_t len = to_utf8(val, buff); - if (len > 0) { result.append(buff, len); } - i += 5; // 'u0000' - } else { - result += s[i]; - } - } else { - auto val = 0; - if (from_hex_to_i(s, i + 1, 2, val)) { - // 2 digits hex codes - result += static_cast(val); - i += 2; // '00' - } else { - result += s[i]; - } - } - } else if (convert_plus_to_space && s[i] == '+') { - result += ' '; - } else { - result += s[i]; - } - } - - return result; -} - -inline void read_file(const std::string &path, std::string &out) { - std::ifstream fs(path, std::ios_base::binary); - fs.seekg(0, std::ios_base::end); - auto size = fs.tellg(); - fs.seekg(0); - out.resize(static_cast(size)); - fs.read(&out[0], static_cast(size)); -} - -inline std::string file_extension(const std::string &path) { - std::smatch m; - static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); - if (std::regex_search(path, m, re)) { return m[1].str(); } - return std::string(); -} - -inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } - -inline std::pair trim(const char *b, const char *e, size_t left, - size_t right) { - while (b + left < e && is_space_or_tab(b[left])) { - left++; - } - while (right > 0 && is_space_or_tab(b[right - 1])) { - right--; - } - return std::make_pair(left, right); -} - -inline std::string trim_copy(const std::string &s) { - auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); - return s.substr(r.first, r.second - r.first); -} - -inline std::string trim_double_quotes_copy(const std::string &s) { - if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { - return s.substr(1, s.size() - 2); - } - return s; -} - -inline void -divide(const char *data, std::size_t size, char d, - std::function - fn) { - const auto it = std::find(data, data + size, d); - const auto found = static_cast(it != data + size); - const auto lhs_data = data; - const auto lhs_size = static_cast(it - data); - const auto rhs_data = it + found; - const auto rhs_size = size - lhs_size - found; - - fn(lhs_data, lhs_size, rhs_data, rhs_size); -} - -inline void -divide(const std::string &str, char d, - std::function - fn) { - divide(str.data(), str.size(), d, std::move(fn)); -} - -inline void split(const char *b, const char *e, char d, - std::function fn) { - return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); -} - -inline void split(const char *b, const char *e, char d, size_t m, - std::function fn) { - size_t i = 0; - size_t beg = 0; - size_t count = 1; - - while (e ? (b + i < e) : (b[i] != '\0')) { - if (b[i] == d && count < m) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } - beg = i + 1; - count++; - } - i++; - } - - if (i) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } - } -} - -inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, - size_t fixed_buffer_size) - : strm_(strm), fixed_buffer_(fixed_buffer), - fixed_buffer_size_(fixed_buffer_size) {} - -inline const char *stream_line_reader::ptr() const { - if (glowable_buffer_.empty()) { - return fixed_buffer_; - } else { - return glowable_buffer_.data(); - } -} - -inline size_t stream_line_reader::size() const { - if (glowable_buffer_.empty()) { - return fixed_buffer_used_size_; - } else { - return glowable_buffer_.size(); - } -} - -inline bool stream_line_reader::end_with_crlf() const { - auto end = ptr() + size(); - return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; -} - -inline bool stream_line_reader::getline() { - fixed_buffer_used_size_ = 0; - glowable_buffer_.clear(); - -#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - char prev_byte = 0; -#endif - - for (size_t i = 0;; i++) { - char byte; - auto n = strm_.read(&byte, 1); - - if (n < 0) { - return false; - } else if (n == 0) { - if (i == 0) { - return false; - } else { - break; - } - } - - append(byte); - -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - if (byte == '\n') { break; } -#else - if (prev_byte == '\r' && byte == '\n') { break; } - prev_byte = byte; -#endif - } - - return true; -} - -inline void stream_line_reader::append(char c) { - if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { - fixed_buffer_[fixed_buffer_used_size_++] = c; - fixed_buffer_[fixed_buffer_used_size_] = '\0'; - } else { - if (glowable_buffer_.empty()) { - assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); - glowable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); - } - glowable_buffer_ += c; - } -} - -inline mmap::mmap(const char *path) { open(path); } - -inline mmap::~mmap() { close(); } - -inline bool mmap::open(const char *path) { - close(); - -#if defined(_WIN32) - auto wpath = u8string_to_wstring(path); - if (wpath.empty()) { return false; } - -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, - OPEN_EXISTING, NULL); -#else - hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); -#endif - - if (hFile_ == INVALID_HANDLE_VALUE) { return false; } - - LARGE_INTEGER size{}; - if (!::GetFileSizeEx(hFile_, &size)) { return false; } - // If the following line doesn't compile due to QuadPart, update Windows SDK. - // See: - // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 - if (static_cast(size.QuadPart) > - (std::numeric_limits::max)()) { - // `size_t` might be 32-bits, on 32-bits Windows. - return false; - } - size_ = static_cast(size.QuadPart); - -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - hMapping_ = - ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); -#else - hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); -#endif - - // Special treatment for an empty file... - if (hMapping_ == NULL && size_ == 0) { - close(); - is_open_empty_file = true; - return true; - } - - if (hMapping_ == NULL) { - close(); - return false; - } - -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 - addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); -#else - addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); -#endif - - if (addr_ == nullptr) { - close(); - return false; - } -#else - fd_ = ::open(path, O_RDONLY); - if (fd_ == -1) { return false; } - - struct stat sb; - if (fstat(fd_, &sb) == -1) { - close(); - return false; - } - size_ = static_cast(sb.st_size); - - addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); - - // Special treatment for an empty file... - if (addr_ == MAP_FAILED && size_ == 0) { - close(); - is_open_empty_file = true; - return false; - } -#endif - - return true; -} - -inline bool mmap::is_open() const { - return is_open_empty_file ? true : addr_ != nullptr; -} - -inline size_t mmap::size() const { return size_; } - -inline const char *mmap::data() const { - return is_open_empty_file ? "" : static_cast(addr_); -} - -inline void mmap::close() { -#if defined(_WIN32) - if (addr_) { - ::UnmapViewOfFile(addr_); - addr_ = nullptr; - } - - if (hMapping_) { - ::CloseHandle(hMapping_); - hMapping_ = NULL; - } - - if (hFile_ != INVALID_HANDLE_VALUE) { - ::CloseHandle(hFile_); - hFile_ = INVALID_HANDLE_VALUE; - } - - is_open_empty_file = false; -#else - if (addr_ != nullptr) { - munmap(addr_, size_); - addr_ = nullptr; - } - - if (fd_ != -1) { - ::close(fd_); - fd_ = -1; - } -#endif - size_ = 0; -} -inline int close_socket(socket_t sock) { -#ifdef _WIN32 - return closesocket(sock); -#else - return close(sock); -#endif -} - -template inline ssize_t handle_EINTR(T fn) { - ssize_t res = 0; - while (true) { - res = fn(); - if (res < 0 && errno == EINTR) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); - continue; - } - break; - } - return res; -} - -inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { - return handle_EINTR([&]() { - return recv(sock, -#ifdef _WIN32 - static_cast(ptr), static_cast(size), -#else - ptr, size, -#endif - flags); - }); -} - -inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, - int flags) { - return handle_EINTR([&]() { - return send(sock, -#ifdef _WIN32 - static_cast(ptr), static_cast(size), -#else - ptr, size, -#endif - flags); - }); -} - -template -inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd; - pfd.fd = sock; - pfd.events = (Read ? POLLIN : POLLOUT); - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - return handle_EINTR([&]() { return poll(&pfd, 1, timeout); }); -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return -1; } -#endif - - fd_set fds, *rfds, *wfds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - rfds = (Read ? &fds : nullptr); - wfds = (Read ? nullptr : &fds); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - return handle_EINTR([&]() { - return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); - }); -#endif -} - -inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { - return select_impl(sock, sec, usec); -} - -inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { - return select_impl(sock, sec, usec); -} - -inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, - time_t usec) { -#ifdef CPPHTTPLIB_USE_POLL - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN | POLLOUT; - - auto timeout = static_cast(sec * 1000 + usec / 1000); - - auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); - - if (poll_res == 0) { return Error::ConnectionTimeout; } - - if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } - - return Error::Connection; -#else -#ifndef _WIN32 - if (sock >= FD_SETSIZE) { return Error::Connection; } -#endif - - fd_set fdsr; - FD_ZERO(&fdsr); - FD_SET(sock, &fdsr); - - auto fdsw = fdsr; - auto fdse = fdsr; - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - auto ret = handle_EINTR([&]() { - return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); - }); - - if (ret == 0) { return Error::ConnectionTimeout; } - - if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } - return Error::Connection; -#endif -} - -inline bool is_socket_alive(socket_t sock) { - const auto val = detail::select_read(sock, 0, 0); - if (val == 0) { - return true; - } else if (val < 0 && errno == EBADF) { - return false; - } - char buf[1]; - return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; -} - -class SocketStream final : public Stream { -public: - SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec = 0, - std::chrono::time_point start_time = - std::chrono::steady_clock::time_point::min()); - ~SocketStream() override; - - bool is_readable() const override; - bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - time_t duration() const override; - -private: - socket_t sock_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; - time_t max_timeout_msec_; - const std::chrono::time_point start_time; - - std::vector read_buff_; - size_t read_buff_off_ = 0; - size_t read_buff_content_size_ = 0; - - static const size_t read_buff_size_ = 1024l * 4; -}; - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLSocketStream final : public Stream { -public: - SSLSocketStream( - socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, time_t max_timeout_msec = 0, - std::chrono::time_point start_time = - std::chrono::steady_clock::time_point::min()); - ~SSLSocketStream() override; - - bool is_readable() const override; - bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - time_t duration() const override; - -private: - socket_t sock_; - SSL *ssl_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; - time_t max_timeout_msec_; - const std::chrono::time_point start_time; -}; -#endif - -inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, - time_t keep_alive_timeout_sec) { - using namespace std::chrono; - - const auto interval_usec = - CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND; - - // Avoid expensive `steady_clock::now()` call for the first time - if (select_read(sock, 0, interval_usec) > 0) { return true; } - - const auto start = steady_clock::now() - microseconds{interval_usec}; - const auto timeout = seconds{keep_alive_timeout_sec}; - - while (true) { - if (svr_sock == INVALID_SOCKET) { - break; // Server socket is closed - } - - auto val = select_read(sock, 0, interval_usec); - if (val < 0) { - break; // Ssocket error - } else if (val == 0) { - if (steady_clock::now() - start > timeout) { - break; // Timeout - } - } else { - return true; // Ready for read - } - } - - return false; -} - -template -inline bool -process_server_socket_core(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, T callback) { - assert(keep_alive_max_count > 0); - auto ret = false; - auto count = keep_alive_max_count; - while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { - auto close_connection = count == 1; - auto connection_closed = false; - ret = callback(close_connection, connection_closed); - if (!ret || connection_closed) { break; } - count--; - } - return ret; -} - -template -inline bool -process_server_socket(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - return process_server_socket_core( - svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm, close_connection, connection_closed); - }); -} - -inline bool process_client_socket( - socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time, - std::function callback) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec, max_timeout_msec, - start_time); - return callback(strm); -} - -inline int shutdown_socket(socket_t sock) { -#ifdef _WIN32 - return shutdown(sock, SD_BOTH); -#else - return shutdown(sock, SHUT_RDWR); -#endif -} - -inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { - if (s.size() > 1 && s[0] == '\0') { - auto ret = s; - ret[0] = '@'; - return ret; - } - return s; -} - -inline std::string -unescape_abstract_namespace_unix_domain(const std::string &s) { - if (s.size() > 1 && s[0] == '@') { - auto ret = s; - ret[0] = '\0'; - return ret; - } - return s; -} - -template -socket_t create_socket(const std::string &host, const std::string &ip, int port, - int address_family, int socket_flags, bool tcp_nodelay, - bool ipv6_v6only, SocketOptions socket_options, - BindOrConnect bind_or_connect) { - // Get address info - const char *node = nullptr; - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_IP; - - if (!ip.empty()) { - node = ip.c_str(); - // Ask getaddrinfo to convert IP in c-string to address - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_NUMERICHOST; - } else { - if (!host.empty()) { node = host.c_str(); } - hints.ai_family = address_family; - hints.ai_flags = socket_flags; - } - -#ifndef _WIN32 - if (hints.ai_family == AF_UNIX) { - const auto addrlen = host.length(); - if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } - -#ifdef SOCK_CLOEXEC - auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, - hints.ai_protocol); -#else - auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); -#endif - - if (sock != INVALID_SOCKET) { - sockaddr_un addr{}; - addr.sun_family = AF_UNIX; - - auto unescaped_host = unescape_abstract_namespace_unix_domain(host); - std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); - - hints.ai_addr = reinterpret_cast(&addr); - hints.ai_addrlen = static_cast( - sizeof(addr) - sizeof(addr.sun_path) + addrlen); - -#ifndef SOCK_CLOEXEC - fcntl(sock, F_SETFD, FD_CLOEXEC); -#endif - - if (socket_options) { socket_options(sock); } - - bool dummy; - if (!bind_or_connect(sock, hints, dummy)) { - close_socket(sock); - sock = INVALID_SOCKET; - } - } - return sock; - } -#endif - - auto service = std::to_string(port); - - if (getaddrinfo(node, service.c_str(), &hints, &result)) { -#if defined __linux__ && !defined __ANDROID__ - res_init(); -#endif - return INVALID_SOCKET; - } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); - - for (auto rp = result; rp; rp = rp->ai_next) { - // Create a socket -#ifdef _WIN32 - auto sock = - WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, - WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); - /** - * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 - * and above the socket creation fails on older Windows Systems. - * - * Let's try to create a socket the old way in this case. - * - * Reference: - * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa - * - * WSA_FLAG_NO_HANDLE_INHERIT: - * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with - * SP1, and later - * - */ - if (sock == INVALID_SOCKET) { - sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - } -#else - -#ifdef SOCK_CLOEXEC - auto sock = - socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); -#else - auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); -#endif - -#endif - if (sock == INVALID_SOCKET) { continue; } - -#if !defined _WIN32 && !defined SOCK_CLOEXEC - if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { - close_socket(sock); - continue; - } -#endif - - if (tcp_nodelay) { - auto opt = 1; -#ifdef _WIN32 - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, - reinterpret_cast(&opt), sizeof(opt)); -#endif - } - - if (rp->ai_family == AF_INET6) { - auto opt = ipv6_v6only ? 1 : 0; -#ifdef _WIN32 - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&opt), sizeof(opt)); -#else - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, - reinterpret_cast(&opt), sizeof(opt)); -#endif - } - - if (socket_options) { socket_options(sock); } - - // bind or connect - auto quit = false; - if (bind_or_connect(sock, *rp, quit)) { return sock; } - - close_socket(sock); - - if (quit) { break; } - } - - return INVALID_SOCKET; -} - -inline void set_nonblocking(socket_t sock, bool nonblocking) { -#ifdef _WIN32 - auto flags = nonblocking ? 1UL : 0UL; - ioctlsocket(sock, FIONBIO, &flags); -#else - auto flags = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, - nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); -#endif -} - -inline bool is_connection_error() { -#ifdef _WIN32 - return WSAGetLastError() != WSAEWOULDBLOCK; -#else - return errno != EINPROGRESS; -#endif -} - -inline bool bind_ip_address(socket_t sock, const std::string &host) { - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; - - if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); - - auto ret = false; - for (auto rp = result; rp; rp = rp->ai_next) { - const auto &ai = *rp; - if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { - ret = true; - break; - } - } - - return ret; -} - -#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ -#define USE_IF2IP -#endif - -#ifdef USE_IF2IP -inline std::string if2ip(int address_family, const std::string &ifn) { - struct ifaddrs *ifap; - getifaddrs(&ifap); - auto se = detail::scope_exit([&] { freeifaddrs(ifap); }); - - std::string addr_candidate; - for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr && ifn == ifa->ifa_name && - (AF_UNSPEC == address_family || - ifa->ifa_addr->sa_family == address_family)) { - if (ifa->ifa_addr->sa_family == AF_INET) { - auto sa = reinterpret_cast(ifa->ifa_addr); - char buf[INET_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { - return std::string(buf, INET_ADDRSTRLEN); - } - } else if (ifa->ifa_addr->sa_family == AF_INET6) { - auto sa = reinterpret_cast(ifa->ifa_addr); - if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { - char buf[INET6_ADDRSTRLEN] = {}; - if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { - // equivalent to mac's IN6_IS_ADDR_UNIQUE_LOCAL - auto s6_addr_head = sa->sin6_addr.s6_addr[0]; - if (s6_addr_head == 0xfc || s6_addr_head == 0xfd) { - addr_candidate = std::string(buf, INET6_ADDRSTRLEN); - } else { - return std::string(buf, INET6_ADDRSTRLEN); - } - } - } - } - } - } - return addr_candidate; -} -#endif - -inline socket_t create_client_socket( - const std::string &host, const std::string &ip, int port, - int address_family, bool tcp_nodelay, bool ipv6_v6only, - SocketOptions socket_options, time_t connection_timeout_sec, - time_t connection_timeout_usec, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, const std::string &intf, Error &error) { - auto sock = create_socket( - host, ip, port, address_family, 0, tcp_nodelay, ipv6_v6only, - std::move(socket_options), - [&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool { - if (!intf.empty()) { -#ifdef USE_IF2IP - auto ip_from_if = if2ip(address_family, intf); - if (ip_from_if.empty()) { ip_from_if = intf; } - if (!bind_ip_address(sock2, ip_from_if)) { - error = Error::BindIPAddress; - return false; - } -#endif - } - - set_nonblocking(sock2, true); - - auto ret = - ::connect(sock2, ai.ai_addr, static_cast(ai.ai_addrlen)); - - if (ret < 0) { - if (is_connection_error()) { - error = Error::Connection; - return false; - } - error = wait_until_socket_is_ready(sock2, connection_timeout_sec, - connection_timeout_usec); - if (error != Error::Success) { - if (error == Error::ConnectionTimeout) { quit = true; } - return false; - } - } - - set_nonblocking(sock2, false); - - { -#ifdef _WIN32 - auto timeout = static_cast(read_timeout_sec * 1000 + - read_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(read_timeout_sec); - tv.tv_usec = static_cast(read_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - { - -#ifdef _WIN32 - auto timeout = static_cast(write_timeout_sec * 1000 + - write_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(write_timeout_sec); - tv.tv_usec = static_cast(write_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - - error = Error::Success; - return true; - }); - - if (sock != INVALID_SOCKET) { - error = Error::Success; - } else { - if (error == Error::Success) { error = Error::Connection; } - } - - return sock; -} - -inline bool get_ip_and_port(const struct sockaddr_storage &addr, - socklen_t addr_len, std::string &ip, int &port) { - if (addr.ss_family == AF_INET) { - port = ntohs(reinterpret_cast(&addr)->sin_port); - } else if (addr.ss_family == AF_INET6) { - port = - ntohs(reinterpret_cast(&addr)->sin6_port); - } else { - return false; - } - - std::array ipstr{}; - if (getnameinfo(reinterpret_cast(&addr), addr_len, - ipstr.data(), static_cast(ipstr.size()), nullptr, - 0, NI_NUMERICHOST)) { - return false; - } - - ip = ipstr.data(); - return true; -} - -inline void get_local_ip_and_port(socket_t sock, std::string &ip, int &port) { - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); - if (!getsockname(sock, reinterpret_cast(&addr), - &addr_len)) { - get_ip_and_port(addr, addr_len, ip, port); - } -} - -inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); - - if (!getpeername(sock, reinterpret_cast(&addr), - &addr_len)) { -#ifndef _WIN32 - if (addr.ss_family == AF_UNIX) { -#if defined(__linux__) - struct ucred ucred; - socklen_t len = sizeof(ucred); - if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { - port = ucred.pid; - } -#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ - pid_t pid; - socklen_t len = sizeof(pid); - if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { - port = pid; - } -#endif - return; - } -#endif - get_ip_and_port(addr, addr_len, ip, port); - } -} - -inline constexpr unsigned int str2tag_core(const char *s, size_t l, - unsigned int h) { - return (l == 0) - ? h - : str2tag_core( - s + 1, l - 1, - // Unsets the 6 high bits of h, therefore no overflow happens - (((std::numeric_limits::max)() >> 6) & - h * 33) ^ - static_cast(*s)); -} - -inline unsigned int str2tag(const std::string &s) { - return str2tag_core(s.data(), s.size(), 0); -} - -namespace udl { - -inline constexpr unsigned int operator""_t(const char *s, size_t l) { - return str2tag_core(s, l, 0); -} - -} // namespace udl - -inline std::string -find_content_type(const std::string &path, - const std::map &user_data, - const std::string &default_content_type) { - auto ext = file_extension(path); - - auto it = user_data.find(ext); - if (it != user_data.end()) { return it->second; } - - using udl::operator""_t; - - switch (str2tag(ext)) { - default: return default_content_type; - - case "css"_t: return "text/css"; - case "csv"_t: return "text/csv"; - case "htm"_t: - case "html"_t: return "text/html"; - case "js"_t: - case "mjs"_t: return "text/javascript"; - case "txt"_t: return "text/plain"; - case "vtt"_t: return "text/vtt"; - - case "apng"_t: return "image/apng"; - case "avif"_t: return "image/avif"; - case "bmp"_t: return "image/bmp"; - case "gif"_t: return "image/gif"; - case "png"_t: return "image/png"; - case "svg"_t: return "image/svg+xml"; - case "webp"_t: return "image/webp"; - case "ico"_t: return "image/x-icon"; - case "tif"_t: return "image/tiff"; - case "tiff"_t: return "image/tiff"; - case "jpg"_t: - case "jpeg"_t: return "image/jpeg"; - - case "mp4"_t: return "video/mp4"; - case "mpeg"_t: return "video/mpeg"; - case "webm"_t: return "video/webm"; - - case "mp3"_t: return "audio/mp3"; - case "mpga"_t: return "audio/mpeg"; - case "weba"_t: return "audio/webm"; - case "wav"_t: return "audio/wave"; - - case "otf"_t: return "font/otf"; - case "ttf"_t: return "font/ttf"; - case "woff"_t: return "font/woff"; - case "woff2"_t: return "font/woff2"; - - case "7z"_t: return "application/x-7z-compressed"; - case "atom"_t: return "application/atom+xml"; - case "pdf"_t: return "application/pdf"; - case "json"_t: return "application/json"; - case "rss"_t: return "application/rss+xml"; - case "tar"_t: return "application/x-tar"; - case "xht"_t: - case "xhtml"_t: return "application/xhtml+xml"; - case "xslt"_t: return "application/xslt+xml"; - case "xml"_t: return "application/xml"; - case "gz"_t: return "application/gzip"; - case "zip"_t: return "application/zip"; - case "wasm"_t: return "application/wasm"; - } -} - -inline bool can_compress_content_type(const std::string &content_type) { - using udl::operator""_t; - - auto tag = str2tag(content_type); - - switch (tag) { - case "image/svg+xml"_t: - case "application/javascript"_t: - case "application/json"_t: - case "application/xml"_t: - case "application/protobuf"_t: - case "application/xhtml+xml"_t: return true; - - case "text/event-stream"_t: return false; - - default: return !content_type.rfind("text/", 0); - } -} - -inline EncodingType encoding_type(const Request &req, const Response &res) { - auto ret = - detail::can_compress_content_type(res.get_header_value("Content-Type")); - if (!ret) { return EncodingType::None; } - - const auto &s = req.get_header_value("Accept-Encoding"); - (void)(s); - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - // TODO: 'Accept-Encoding' has br, not br;q=0 - ret = s.find("br") != std::string::npos; - if (ret) { return EncodingType::Brotli; } -#endif - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 - ret = s.find("gzip") != std::string::npos; - if (ret) { return EncodingType::Gzip; } -#endif - - return EncodingType::None; -} - -inline bool nocompressor::compress(const char *data, size_t data_length, - bool /*last*/, Callback callback) { - if (!data_length) { return true; } - return callback(data, data_length); -} - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -inline gzip_compressor::gzip_compressor() { - std::memset(&strm_, 0, sizeof(strm_)); - strm_.zalloc = Z_NULL; - strm_.zfree = Z_NULL; - strm_.opaque = Z_NULL; - - is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, - Z_DEFAULT_STRATEGY) == Z_OK; -} - -inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } - -inline bool gzip_compressor::compress(const char *data, size_t data_length, - bool last, Callback callback) { - assert(is_valid_); - - do { - constexpr size_t max_avail_in = - (std::numeric_limits::max)(); - - strm_.avail_in = static_cast( - (std::min)(data_length, max_avail_in)); - strm_.next_in = const_cast(reinterpret_cast(data)); - - data_length -= strm_.avail_in; - data += strm_.avail_in; - - auto flush = (last && data_length == 0) ? Z_FINISH : Z_NO_FLUSH; - auto ret = Z_OK; - - std::array buff{}; - do { - strm_.avail_out = static_cast(buff.size()); - strm_.next_out = reinterpret_cast(buff.data()); - - ret = deflate(&strm_, flush); - if (ret == Z_STREAM_ERROR) { return false; } - - if (!callback(buff.data(), buff.size() - strm_.avail_out)) { - return false; - } - } while (strm_.avail_out == 0); - - assert((flush == Z_FINISH && ret == Z_STREAM_END) || - (flush == Z_NO_FLUSH && ret == Z_OK)); - assert(strm_.avail_in == 0); - } while (data_length > 0); - - return true; -} - -inline gzip_decompressor::gzip_decompressor() { - std::memset(&strm_, 0, sizeof(strm_)); - strm_.zalloc = Z_NULL; - strm_.zfree = Z_NULL; - strm_.opaque = Z_NULL; - - // 15 is the value of wbits, which should be at the maximum possible value - // to ensure that any gzip stream can be decoded. The offset of 32 specifies - // that the stream type should be automatically detected either gzip or - // deflate. - is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; -} - -inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } - -inline bool gzip_decompressor::is_valid() const { return is_valid_; } - -inline bool gzip_decompressor::decompress(const char *data, size_t data_length, - Callback callback) { - assert(is_valid_); - - auto ret = Z_OK; - - do { - constexpr size_t max_avail_in = - (std::numeric_limits::max)(); - - strm_.avail_in = static_cast( - (std::min)(data_length, max_avail_in)); - strm_.next_in = const_cast(reinterpret_cast(data)); - - data_length -= strm_.avail_in; - data += strm_.avail_in; - - std::array buff{}; - while (strm_.avail_in > 0 && ret == Z_OK) { - strm_.avail_out = static_cast(buff.size()); - strm_.next_out = reinterpret_cast(buff.data()); - - ret = inflate(&strm_, Z_NO_FLUSH); - - assert(ret != Z_STREAM_ERROR); - switch (ret) { - case Z_NEED_DICT: - case Z_DATA_ERROR: - case Z_MEM_ERROR: inflateEnd(&strm_); return false; - } - - if (!callback(buff.data(), buff.size() - strm_.avail_out)) { - return false; - } - } - - if (ret != Z_OK && ret != Z_STREAM_END) { return false; } - - } while (data_length > 0); - - return true; -} -#endif - -#ifdef CPPHTTPLIB_BROTLI_SUPPORT -inline brotli_compressor::brotli_compressor() { - state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); -} - -inline brotli_compressor::~brotli_compressor() { - BrotliEncoderDestroyInstance(state_); -} - -inline bool brotli_compressor::compress(const char *data, size_t data_length, - bool last, Callback callback) { - std::array buff{}; - - auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; - auto available_in = data_length; - auto next_in = reinterpret_cast(data); - - for (;;) { - if (last) { - if (BrotliEncoderIsFinished(state_)) { break; } - } else { - if (!available_in) { break; } - } - - auto available_out = buff.size(); - auto next_out = buff.data(); - - if (!BrotliEncoderCompressStream(state_, operation, &available_in, &next_in, - &available_out, &next_out, nullptr)) { - return false; - } - - auto output_bytes = buff.size() - available_out; - if (output_bytes) { - callback(reinterpret_cast(buff.data()), output_bytes); - } - } - - return true; -} - -inline brotli_decompressor::brotli_decompressor() { - decoder_s = BrotliDecoderCreateInstance(0, 0, 0); - decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT - : BROTLI_DECODER_RESULT_ERROR; -} - -inline brotli_decompressor::~brotli_decompressor() { - if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } -} - -inline bool brotli_decompressor::is_valid() const { return decoder_s; } - -inline bool brotli_decompressor::decompress(const char *data, - size_t data_length, - Callback callback) { - if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || - decoder_r == BROTLI_DECODER_RESULT_ERROR) { - return 0; - } - - auto next_in = reinterpret_cast(data); - size_t avail_in = data_length; - size_t total_out; - - decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; - - std::array buff{}; - while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { - char *next_out = buff.data(); - size_t avail_out = buff.size(); - - decoder_r = BrotliDecoderDecompressStream( - decoder_s, &avail_in, &next_in, &avail_out, - reinterpret_cast(&next_out), &total_out); - - if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } - - if (!callback(buff.data(), buff.size() - avail_out)) { return false; } - } - - return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || - decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; -} -#endif - -inline bool has_header(const Headers &headers, const std::string &key) { - return headers.find(key) != headers.end(); -} - -inline const char *get_header_value(const Headers &headers, - const std::string &key, const char *def, - size_t id) { - auto rng = headers.equal_range(key); - auto it = rng.first; - std::advance(it, static_cast(id)); - if (it != rng.second) { return it->second.c_str(); } - return def; -} - -template -inline bool parse_header(const char *beg, const char *end, T fn) { - // Skip trailing spaces and tabs. - while (beg < end && is_space_or_tab(end[-1])) { - end--; - } - - auto p = beg; - while (p < end && *p != ':') { - p++; - } - - if (p == end) { return false; } - - auto key_end = p; - - if (*p++ != ':') { return false; } - - while (p < end && is_space_or_tab(*p)) { - p++; - } - - if (p <= end) { - auto key_len = key_end - beg; - if (!key_len) { return false; } - - auto key = std::string(beg, key_end); - // auto val = (case_ignore::equal(key, "Location") || - // case_ignore::equal(key, "Referer")) - // ? std::string(p, end) - // : decode_url(std::string(p, end), false); - auto val = std::string(p, end); - - if (!detail::fields::is_field_value(val)) { return false; } - - if (case_ignore::equal(key, "Location") || - case_ignore::equal(key, "Referer")) { - fn(key, val); - } else { - fn(key, decode_url(val, false)); - } - - return true; - } - - return false; -} - -inline bool read_headers(Stream &strm, Headers &headers) { - const auto bufsiz = 2048; - char buf[bufsiz]; - stream_line_reader line_reader(strm, buf, bufsiz); - - for (;;) { - if (!line_reader.getline()) { return false; } - - // Check if the line ends with CRLF. - auto line_terminator_len = 2; - if (line_reader.end_with_crlf()) { - // Blank line indicates end of headers. - if (line_reader.size() == 2) { break; } - } else { -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - // Blank line indicates end of headers. - if (line_reader.size() == 1) { break; } - line_terminator_len = 1; -#else - continue; // Skip invalid line. -#endif - } - - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - - // Exclude line terminator - auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - - if (!parse_header(line_reader.ptr(), end, - [&](const std::string &key, const std::string &val) { - headers.emplace(key, val); - })) { - return false; - } - } - - return true; -} - -inline bool read_content_with_length(Stream &strm, uint64_t len, - Progress progress, - ContentReceiverWithProgress out) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - - uint64_t r = 0; - while (r < len) { - auto read_len = static_cast(len - r); - auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return false; } - - if (!out(buf, static_cast(n), r, len)) { return false; } - r += static_cast(n); - - if (progress) { - if (!progress(r, len)) { return false; } - } - } - - return true; -} - -inline void skip_content_with_length(Stream &strm, uint64_t len) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; - while (r < len) { - auto read_len = static_cast(len - r); - auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return; } - r += static_cast(n); - } -} - -inline bool read_content_without_length(Stream &strm, - ContentReceiverWithProgress out) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; - for (;;) { - auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); - if (n <= 0) { return false; } - - if (!out(buf, static_cast(n), r, 0)) { return false; } - r += static_cast(n); - } - - return true; -} - -template -inline bool read_content_chunked(Stream &strm, T &x, - ContentReceiverWithProgress out) { - const auto bufsiz = 16; - char buf[bufsiz]; - - stream_line_reader line_reader(strm, buf, bufsiz); - - if (!line_reader.getline()) { return false; } - - unsigned long chunk_len; - while (true) { - char *end_ptr; - - chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); - - if (end_ptr == line_reader.ptr()) { return false; } - if (chunk_len == ULONG_MAX) { return false; } - - if (chunk_len == 0) { break; } - - if (!read_content_with_length(strm, chunk_len, nullptr, out)) { - return false; - } - - if (!line_reader.getline()) { return false; } - - if (strcmp(line_reader.ptr(), "\r\n") != 0) { return false; } - - if (!line_reader.getline()) { return false; } - } - - assert(chunk_len == 0); - - // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentiones "The chunked - // transfer coding is complete when a chunk with a chunk-size of zero is - // received, possibly followed by a trailer section, and finally terminated by - // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 - // - // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section - // does't care for the existence of the final CRLF. In other words, it seems - // to be ok whether the final CRLF exists or not in the chunked data. - // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 - // - // According to the reference code in RFC 9112, cpp-htpplib now allows - // chuncked transfer coding data without the final CRLF. - if (!line_reader.getline()) { return true; } - - while (strcmp(line_reader.ptr(), "\r\n") != 0) { - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - - // Exclude line terminator - constexpr auto line_terminator_len = 2; - auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - - parse_header(line_reader.ptr(), end, - [&](const std::string &key, const std::string &val) { - x.headers.emplace(key, val); - }); - - if (!line_reader.getline()) { return false; } - } - - return true; -} - -inline bool is_chunked_transfer_encoding(const Headers &headers) { - return case_ignore::equal( - get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); -} - -template -bool prepare_content_receiver(T &x, int &status, - ContentReceiverWithProgress receiver, - bool decompress, U callback) { - if (decompress) { - std::string encoding = x.get_header_value("Content-Encoding"); - std::unique_ptr decompressor; - - if (encoding == "gzip" || encoding == "deflate") { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif - } else if (encoding.find("br") != std::string::npos) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif - } - - if (decompressor) { - if (decompressor->is_valid()) { - ContentReceiverWithProgress out = [&](const char *buf, size_t n, - uint64_t off, uint64_t len) { - return decompressor->decompress(buf, n, - [&](const char *buf2, size_t n2) { - return receiver(buf2, n2, off, len); - }); - }; - return callback(std::move(out)); - } else { - status = StatusCode::InternalServerError_500; - return false; - } - } - } - - ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, - uint64_t len) { - return receiver(buf, n, off, len); - }; - return callback(std::move(out)); -} - -template -bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, - Progress progress, ContentReceiverWithProgress receiver, - bool decompress) { - return prepare_content_receiver( - x, status, std::move(receiver), decompress, - [&](const ContentReceiverWithProgress &out) { - auto ret = true; - auto exceed_payload_max_length = false; - - if (is_chunked_transfer_encoding(x.headers)) { - ret = read_content_chunked(strm, x, out); - } else if (!has_header(x.headers, "Content-Length")) { - ret = read_content_without_length(strm, out); - } else { - auto is_invalid_value = false; - auto len = get_header_value_u64( - x.headers, "Content-Length", - (std::numeric_limits::max)(), 0, is_invalid_value); - - if (is_invalid_value) { - ret = false; - } else if (len > payload_max_length) { - exceed_payload_max_length = true; - skip_content_with_length(strm, len); - ret = false; - } else if (len > 0) { - ret = read_content_with_length(strm, len, std::move(progress), out); - } - } - - if (!ret) { - status = exceed_payload_max_length ? StatusCode::PayloadTooLarge_413 - : StatusCode::BadRequest_400; - } - return ret; - }); -} - -inline ssize_t write_request_line(Stream &strm, const std::string &method, - const std::string &path) { - std::string s = method; - s += " "; - s += path; - s += " HTTP/1.1\r\n"; - return strm.write(s.data(), s.size()); -} - -inline ssize_t write_response_line(Stream &strm, int status) { - std::string s = "HTTP/1.1 "; - s += std::to_string(status); - s += " "; - s += httplib::status_message(status); - s += "\r\n"; - return strm.write(s.data(), s.size()); -} - -inline ssize_t write_headers(Stream &strm, const Headers &headers) { - ssize_t write_len = 0; - for (const auto &x : headers) { - std::string s; - s = x.first; - s += ": "; - s += x.second; - s += "\r\n"; - - auto len = strm.write(s.data(), s.size()); - if (len < 0) { return len; } - write_len += len; - } - auto len = strm.write("\r\n"); - if (len < 0) { return len; } - write_len += len; - return write_len; -} - -inline bool write_data(Stream &strm, const char *d, size_t l) { - size_t offset = 0; - while (offset < l) { - auto length = strm.write(d + offset, l - offset); - if (length < 0) { return false; } - offset += static_cast(length); - } - return true; -} - -template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, - size_t offset, size_t length, T is_shutting_down, - Error &error) { - size_t end_offset = offset + length; - auto ok = true; - DataSink data_sink; - - data_sink.write = [&](const char *d, size_t l) -> bool { - if (ok) { - if (strm.is_writable() && write_data(strm, d, l)) { - offset += l; - } else { - ok = false; - } - } - return ok; - }; - - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; - - while (offset < end_offset && !is_shutting_down()) { - if (!strm.is_writable()) { - error = Error::Write; - return false; - } else if (!content_provider(offset, end_offset - offset, data_sink)) { - error = Error::Canceled; - return false; - } else if (!ok) { - error = Error::Write; - return false; - } - } - - error = Error::Success; - return true; -} - -template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, - size_t offset, size_t length, - const T &is_shutting_down) { - auto error = Error::Success; - return write_content(strm, content_provider, offset, length, is_shutting_down, - error); -} - -template -inline bool -write_content_without_length(Stream &strm, - const ContentProvider &content_provider, - const T &is_shutting_down) { - size_t offset = 0; - auto data_available = true; - auto ok = true; - DataSink data_sink; - - data_sink.write = [&](const char *d, size_t l) -> bool { - if (ok) { - offset += l; - if (!strm.is_writable() || !write_data(strm, d, l)) { ok = false; } - } - return ok; - }; - - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; - - data_sink.done = [&](void) { data_available = false; }; - - while (data_available && !is_shutting_down()) { - if (!strm.is_writable()) { - return false; - } else if (!content_provider(offset, 0, data_sink)) { - return false; - } else if (!ok) { - return false; - } - } - return true; -} - -template -inline bool -write_content_chunked(Stream &strm, const ContentProvider &content_provider, - const T &is_shutting_down, U &compressor, Error &error) { - size_t offset = 0; - auto data_available = true; - auto ok = true; - DataSink data_sink; - - data_sink.write = [&](const char *d, size_t l) -> bool { - if (ok) { - data_available = l > 0; - offset += l; - - std::string payload; - if (compressor.compress(d, l, false, - [&](const char *data, size_t data_len) { - payload.append(data, data_len); - return true; - })) { - if (!payload.empty()) { - // Emit chunked response header and footer for each chunk - auto chunk = - from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!strm.is_writable() || - !write_data(strm, chunk.data(), chunk.size())) { - ok = false; - } - } - } else { - ok = false; - } - } - return ok; - }; - - data_sink.is_writable = [&]() -> bool { return strm.is_writable(); }; - - auto done_with_trailer = [&](const Headers *trailer) { - if (!ok) { return; } - - data_available = false; - - std::string payload; - if (!compressor.compress(nullptr, 0, true, - [&](const char *data, size_t data_len) { - payload.append(data, data_len); - return true; - })) { - ok = false; - return; - } - - if (!payload.empty()) { - // Emit chunked response header and footer for each chunk - auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!strm.is_writable() || - !write_data(strm, chunk.data(), chunk.size())) { - ok = false; - return; - } - } - - static const std::string done_marker("0\r\n"); - if (!write_data(strm, done_marker.data(), done_marker.size())) { - ok = false; - } - - // Trailer - if (trailer) { - for (const auto &kv : *trailer) { - std::string field_line = kv.first + ": " + kv.second + "\r\n"; - if (!write_data(strm, field_line.data(), field_line.size())) { - ok = false; - } - } - } - - static const std::string crlf("\r\n"); - if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; } - }; - - data_sink.done = [&](void) { done_with_trailer(nullptr); }; - - data_sink.done_with_trailer = [&](const Headers &trailer) { - done_with_trailer(&trailer); - }; - - while (data_available && !is_shutting_down()) { - if (!strm.is_writable()) { - error = Error::Write; - return false; - } else if (!content_provider(offset, 0, data_sink)) { - error = Error::Canceled; - return false; - } else if (!ok) { - error = Error::Write; - return false; - } - } - - error = Error::Success; - return true; -} - -template -inline bool write_content_chunked(Stream &strm, - const ContentProvider &content_provider, - const T &is_shutting_down, U &compressor) { - auto error = Error::Success; - return write_content_chunked(strm, content_provider, is_shutting_down, - compressor, error); -} - -template -inline bool redirect(T &cli, Request &req, Response &res, - const std::string &path, const std::string &location, - Error &error) { - Request new_req = req; - new_req.path = path; - new_req.redirect_count_ -= 1; - - if (res.status == StatusCode::SeeOther_303 && - (req.method != "GET" && req.method != "HEAD")) { - new_req.method = "GET"; - new_req.body.clear(); - new_req.headers.clear(); - } - - Response new_res; - - auto ret = cli.send(new_req, new_res, error); - if (ret) { - req = new_req; - res = new_res; - - if (res.location.empty()) { res.location = location; } - } - return ret; -} - -inline std::string params_to_query_str(const Params ¶ms) { - std::string query; - - for (auto it = params.begin(); it != params.end(); ++it) { - if (it != params.begin()) { query += "&"; } - query += it->first; - query += "="; - query += encode_query_param(it->second); - } - return query; -} - -inline void parse_query_text(const char *data, std::size_t size, - Params ¶ms) { - std::set cache; - split(data, data + size, '&', [&](const char *b, const char *e) { - std::string kv(b, e); - if (cache.find(kv) != cache.end()) { return; } - cache.insert(std::move(kv)); - - std::string key; - std::string val; - divide(b, static_cast(e - b), '=', - [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, - std::size_t rhs_size) { - key.assign(lhs_data, lhs_size); - val.assign(rhs_data, rhs_size); - }); - - if (!key.empty()) { - params.emplace(decode_url(key, true), decode_url(val, true)); - } - }); -} - -inline void parse_query_text(const std::string &s, Params ¶ms) { - parse_query_text(s.data(), s.size(), params); -} - -inline bool parse_multipart_boundary(const std::string &content_type, - std::string &boundary) { - auto boundary_keyword = "boundary="; - auto pos = content_type.find(boundary_keyword); - if (pos == std::string::npos) { return false; } - auto end = content_type.find(';', pos); - auto beg = pos + strlen(boundary_keyword); - boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg)); - return !boundary.empty(); -} - -inline void parse_disposition_params(const std::string &s, Params ¶ms) { - std::set cache; - split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) { - std::string kv(b, e); - if (cache.find(kv) != cache.end()) { return; } - cache.insert(kv); - - std::string key; - std::string val; - split(b, e, '=', [&](const char *b2, const char *e2) { - if (key.empty()) { - key.assign(b2, e2); - } else { - val.assign(b2, e2); - } - }); - - if (!key.empty()) { - params.emplace(trim_double_quotes_copy((key)), - trim_double_quotes_copy((val))); - } - }); -} - -#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 - auto is_valid = [](const std::string &str) { - return std::all_of(str.cbegin(), str.cend(), - [](unsigned char c) { return std::isdigit(c); }); - }; - - if (s.size() > 7 && s.compare(0, 6, "bytes=") == 0) { - const auto pos = static_cast(6); - const auto len = static_cast(s.size() - 6); - auto all_valid_ranges = true; - split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { - if (!all_valid_ranges) { return; } - - const auto it = std::find(b, e, '-'); - if (it == e) { - all_valid_ranges = false; - return; - } - - const auto lhs = std::string(b, it); - const auto rhs = std::string(it + 1, e); - if (!is_valid(lhs) || !is_valid(rhs)) { - all_valid_ranges = false; - return; - } - - const auto first = - static_cast(lhs.empty() ? -1 : std::stoll(lhs)); - const auto last = - static_cast(rhs.empty() ? -1 : std::stoll(rhs)); - if ((first == -1 && last == -1) || - (first != -1 && last != -1 && first > last)) { - all_valid_ranges = false; - return; - } - - ranges.emplace_back(first, last); - }); - return all_valid_ranges && !ranges.empty(); - } - return false; -#ifdef CPPHTTPLIB_NO_EXCEPTIONS -} -#else -} catch (...) { return false; } -#endif - -class MultipartFormDataParser { -public: - MultipartFormDataParser() = default; - - void set_boundary(std::string &&boundary) { - boundary_ = boundary; - dash_boundary_crlf_ = dash_ + boundary_ + crlf_; - crlf_dash_boundary_ = crlf_ + dash_ + boundary_; - } - - bool is_valid() const { return is_valid_; } - - bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, - const MultipartContentHeader &header_callback) { - - buf_append(buf, n); - - while (buf_size() > 0) { - switch (state_) { - case 0: { // Initial boundary - buf_erase(buf_find(dash_boundary_crlf_)); - if (dash_boundary_crlf_.size() > buf_size()) { return true; } - if (!buf_start_with(dash_boundary_crlf_)) { return false; } - buf_erase(dash_boundary_crlf_.size()); - state_ = 1; - break; - } - case 1: { // New entry - clear_file_info(); - state_ = 2; - break; - } - case 2: { // Headers - auto pos = buf_find(crlf_); - if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - while (pos < buf_size()) { - // Empty line - if (pos == 0) { - if (!header_callback(file_)) { - is_valid_ = false; - return false; - } - buf_erase(crlf_.size()); - state_ = 3; - break; - } - - const auto header = buf_head(pos); - - if (!parse_header(header.data(), header.data() + header.size(), - [&](const std::string &, const std::string &) {})) { - is_valid_ = false; - return false; - } - - static const std::string header_content_type = "Content-Type:"; - - if (start_with_case_ignore(header, header_content_type)) { - file_.content_type = - trim_copy(header.substr(header_content_type.size())); - } else { - static const std::regex re_content_disposition( - R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~", - std::regex_constants::icase); - - std::smatch m; - if (std::regex_match(header, m, re_content_disposition)) { - Params params; - parse_disposition_params(m[1], params); - - auto it = params.find("name"); - if (it != params.end()) { - file_.name = it->second; - } else { - is_valid_ = false; - return false; - } - - it = params.find("filename"); - if (it != params.end()) { file_.filename = it->second; } - - it = params.find("filename*"); - if (it != params.end()) { - // Only allow UTF-8 enconnding... - static const std::regex re_rfc5987_encoding( - R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase); - - std::smatch m2; - if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { - file_.filename = decode_url(m2[1], false); // override... - } else { - is_valid_ = false; - return false; - } - } - } - } - buf_erase(pos + crlf_.size()); - pos = buf_find(crlf_); - } - if (state_ != 3) { return true; } - break; - } - case 3: { // Body - if (crlf_dash_boundary_.size() > buf_size()) { return true; } - auto pos = buf_find(crlf_dash_boundary_); - if (pos < buf_size()) { - if (!content_callback(buf_data(), pos)) { - is_valid_ = false; - return false; - } - buf_erase(pos + crlf_dash_boundary_.size()); - state_ = 4; - } else { - auto len = buf_size() - crlf_dash_boundary_.size(); - if (len > 0) { - if (!content_callback(buf_data(), len)) { - is_valid_ = false; - return false; - } - buf_erase(len); - } - return true; - } - break; - } - case 4: { // Boundary - if (crlf_.size() > buf_size()) { return true; } - if (buf_start_with(crlf_)) { - buf_erase(crlf_.size()); - state_ = 1; - } else { - if (dash_.size() > buf_size()) { return true; } - if (buf_start_with(dash_)) { - buf_erase(dash_.size()); - is_valid_ = true; - buf_erase(buf_size()); // Remove epilogue - } else { - return true; - } - } - break; - } - } - } - - return true; - } - -private: - void clear_file_info() { - file_.name.clear(); - file_.filename.clear(); - file_.content_type.clear(); - } - - bool start_with_case_ignore(const std::string &a, - const std::string &b) const { - if (a.size() < b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { - if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) { - return false; - } - } - return true; - } - - const std::string dash_ = "--"; - const std::string crlf_ = "\r\n"; - std::string boundary_; - std::string dash_boundary_crlf_; - std::string crlf_dash_boundary_; - - size_t state_ = 0; - bool is_valid_ = false; - MultipartFormData file_; - - // Buffer - bool start_with(const std::string &a, size_t spos, size_t epos, - const std::string &b) const { - if (epos - spos < b.size()) { return false; } - for (size_t i = 0; i < b.size(); i++) { - if (a[i + spos] != b[i]) { return false; } - } - return true; - } - - size_t buf_size() const { return buf_epos_ - buf_spos_; } - - const char *buf_data() const { return &buf_[buf_spos_]; } - - std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } - - bool buf_start_with(const std::string &s) const { - return start_with(buf_, buf_spos_, buf_epos_, s); - } - - size_t buf_find(const std::string &s) const { - auto c = s.front(); - - size_t off = buf_spos_; - while (off < buf_epos_) { - auto pos = off; - while (true) { - if (pos == buf_epos_) { return buf_size(); } - if (buf_[pos] == c) { break; } - pos++; - } - - auto remaining_size = buf_epos_ - pos; - if (s.size() > remaining_size) { return buf_size(); } - - if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } - - off = pos + 1; - } - - return buf_size(); - } - - void buf_append(const char *data, size_t n) { - auto remaining_size = buf_size(); - if (remaining_size > 0 && buf_spos_ > 0) { - for (size_t i = 0; i < remaining_size; i++) { - buf_[i] = buf_[buf_spos_ + i]; - } - } - buf_spos_ = 0; - buf_epos_ = remaining_size; - - if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } - - for (size_t i = 0; i < n; i++) { - buf_[buf_epos_ + i] = data[i]; - } - buf_epos_ += n; - } - - void buf_erase(size_t size) { buf_spos_ += size; } - - std::string buf_; - size_t buf_spos_ = 0; - size_t buf_epos_ = 0; -}; - -inline std::string random_string(size_t length) { - static const char data[] = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - - // std::random_device might actually be deterministic on some - // platforms, but due to lack of support in the c++ standard library, - // doing better requires either some ugly hacks or breaking portability. - static std::random_device seed_gen; - - // Request 128 bits of entropy for initialization - static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), - seed_gen()}; - - static std::mt19937 engine(seed_sequence); - - std::string result; - for (size_t i = 0; i < length; i++) { - result += data[engine() % (sizeof(data) - 1)]; - } - return result; -} - -inline std::string make_multipart_data_boundary() { - return "--cpp-httplib-multipart-data-" + detail::random_string(16); -} - -inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { - auto valid = true; - for (size_t i = 0; i < boundary.size(); i++) { - auto c = boundary[i]; - if (!std::isalnum(c) && c != '-' && c != '_') { - valid = false; - break; - } - } - return valid; -} - -template -inline std::string -serialize_multipart_formdata_item_begin(const T &item, - const std::string &boundary) { - std::string body = "--" + boundary + "\r\n"; - body += "Content-Disposition: form-data; name=\"" + item.name + "\""; - if (!item.filename.empty()) { - body += "; filename=\"" + item.filename + "\""; - } - body += "\r\n"; - if (!item.content_type.empty()) { - body += "Content-Type: " + item.content_type + "\r\n"; - } - body += "\r\n"; - - return body; -} - -inline std::string serialize_multipart_formdata_item_end() { return "\r\n"; } - -inline std::string -serialize_multipart_formdata_finish(const std::string &boundary) { - return "--" + boundary + "--\r\n"; -} - -inline std::string -serialize_multipart_formdata_get_content_type(const std::string &boundary) { - return "multipart/form-data; boundary=" + boundary; -} - -inline std::string -serialize_multipart_formdata(const MultipartFormDataItems &items, - const std::string &boundary, bool finish = true) { - std::string body; - - for (const auto &item : items) { - body += serialize_multipart_formdata_item_begin(item, boundary); - body += item.content + serialize_multipart_formdata_item_end(); - } - - if (finish) { body += serialize_multipart_formdata_finish(boundary); } - - return body; -} - -inline bool range_error(Request &req, Response &res) { - if (!req.ranges.empty() && 200 <= res.status && res.status < 300) { - ssize_t contant_len = static_cast( - res.content_length_ ? res.content_length_ : res.body.size()); - - ssize_t prev_first_pos = -1; - ssize_t prev_last_pos = -1; - size_t overwrapping_count = 0; - - // NOTE: The following Range check is based on '14.2. Range' in RFC 9110 - // 'HTTP Semantics' to avoid potential denial-of-service attacks. - // https://www.rfc-editor.org/rfc/rfc9110#section-14.2 - - // Too many ranges - if (req.ranges.size() > CPPHTTPLIB_RANGE_MAX_COUNT) { return true; } - - for (auto &r : req.ranges) { - auto &first_pos = r.first; - auto &last_pos = r.second; - - if (first_pos == -1 && last_pos == -1) { - first_pos = 0; - last_pos = contant_len; - } - - if (first_pos == -1) { - first_pos = contant_len - last_pos; - last_pos = contant_len - 1; - } - - // NOTE: RFC-9110 '14.1.2. Byte Ranges': - // A client can limit the number of bytes requested without knowing the - // size of the selected representation. If the last-pos value is absent, - // or if the value is greater than or equal to the current length of the - // representation data, the byte range is interpreted as the remainder of - // the representation (i.e., the server replaces the value of last-pos - // with a value that is one less than the current length of the selected - // representation). - // https://www.rfc-editor.org/rfc/rfc9110.html#section-14.1.2-6 - if (last_pos == -1 || last_pos >= contant_len) { - last_pos = contant_len - 1; - } - - // Range must be within content length - if (!(0 <= first_pos && first_pos <= last_pos && - last_pos <= contant_len - 1)) { - return true; - } - - // Ranges must be in ascending order - if (first_pos <= prev_first_pos) { return true; } - - // Request must not have more than two overlapping ranges - if (first_pos <= prev_last_pos) { - overwrapping_count++; - if (overwrapping_count > 2) { return true; } - } - - prev_first_pos = (std::max)(prev_first_pos, first_pos); - prev_last_pos = (std::max)(prev_last_pos, last_pos); - } - } - - return false; -} - -inline std::pair -get_range_offset_and_length(Range r, size_t content_length) { - assert(r.first != -1 && r.second != -1); - assert(0 <= r.first && r.first < static_cast(content_length)); - assert(r.first <= r.second && - r.second < static_cast(content_length)); - (void)(content_length); - return std::make_pair(r.first, static_cast(r.second - r.first) + 1); -} - -inline std::string make_content_range_header_field( - const std::pair &offset_and_length, size_t content_length) { - auto st = offset_and_length.first; - auto ed = st + offset_and_length.second - 1; - - std::string field = "bytes "; - field += std::to_string(st); - field += "-"; - field += std::to_string(ed); - field += "/"; - field += std::to_string(content_length); - return field; -} - -template -bool process_multipart_ranges_data(const Request &req, - const std::string &boundary, - const std::string &content_type, - size_t content_length, SToken stoken, - CToken ctoken, Content content) { - for (size_t i = 0; i < req.ranges.size(); i++) { - ctoken("--"); - stoken(boundary); - ctoken("\r\n"); - if (!content_type.empty()) { - ctoken("Content-Type: "); - stoken(content_type); - ctoken("\r\n"); - } - - auto offset_and_length = - get_range_offset_and_length(req.ranges[i], content_length); - - ctoken("Content-Range: "); - stoken(make_content_range_header_field(offset_and_length, content_length)); - ctoken("\r\n"); - ctoken("\r\n"); - - if (!content(offset_and_length.first, offset_and_length.second)) { - return false; - } - ctoken("\r\n"); - } - - ctoken("--"); - stoken(boundary); - ctoken("--"); - - return true; -} - -inline void make_multipart_ranges_data(const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type, - size_t content_length, - std::string &data) { - process_multipart_ranges_data( - req, boundary, content_type, content_length, - [&](const std::string &token) { data += token; }, - [&](const std::string &token) { data += token; }, - [&](size_t offset, size_t length) { - assert(offset + length <= content_length); - data += res.body.substr(offset, length); - return true; - }); -} - -inline size_t get_multipart_ranges_data_length(const Request &req, - const std::string &boundary, - const std::string &content_type, - size_t content_length) { - size_t data_length = 0; - - process_multipart_ranges_data( - req, boundary, content_type, content_length, - [&](const std::string &token) { data_length += token.size(); }, - [&](const std::string &token) { data_length += token.size(); }, - [&](size_t /*offset*/, size_t length) { - data_length += length; - return true; - }); - - return data_length; -} - -template -inline bool -write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type, - size_t content_length, const T &is_shutting_down) { - return process_multipart_ranges_data( - req, boundary, content_type, content_length, - [&](const std::string &token) { strm.write(token); }, - [&](const std::string &token) { strm.write(token); }, - [&](size_t offset, size_t length) { - return write_content(strm, res.content_provider_, offset, length, - is_shutting_down); - }); -} - -inline bool expect_content(const Request &req) { - if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || - req.method == "DELETE") { - return true; - } - if (req.has_header("Content-Length") && - req.get_header_value_u64("Content-Length") > 0) { - return true; - } - if (is_chunked_transfer_encoding(req.headers)) { return true; } - return false; -} - -inline bool has_crlf(const std::string &s) { - auto p = s.c_str(); - while (*p) { - if (*p == '\r' || *p == '\n') { return true; } - p++; - } - return false; -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline std::string message_digest(const std::string &s, const EVP_MD *algo) { - auto context = std::unique_ptr( - EVP_MD_CTX_new(), EVP_MD_CTX_free); - - unsigned int hash_length = 0; - unsigned char hash[EVP_MAX_MD_SIZE]; - - EVP_DigestInit_ex(context.get(), algo, nullptr); - EVP_DigestUpdate(context.get(), s.c_str(), s.size()); - EVP_DigestFinal_ex(context.get(), hash, &hash_length); - - std::stringstream ss; - for (auto i = 0u; i < hash_length; ++i) { - ss << std::hex << std::setw(2) << std::setfill('0') - << static_cast(hash[i]); - } - - return ss.str(); -} - -inline std::string MD5(const std::string &s) { - return message_digest(s, EVP_md5()); -} - -inline std::string SHA_256(const std::string &s) { - return message_digest(s, EVP_sha256()); -} - -inline std::string SHA_512(const std::string &s) { - return message_digest(s, EVP_sha512()); -} - -inline std::pair make_digest_authentication_header( - const Request &req, const std::map &auth, - size_t cnonce_count, const std::string &cnonce, const std::string &username, - const std::string &password, bool is_proxy = false) { - std::string nc; - { - std::stringstream ss; - ss << std::setfill('0') << std::setw(8) << std::hex << cnonce_count; - nc = ss.str(); - } - - std::string qop; - if (auth.find("qop") != auth.end()) { - qop = auth.at("qop"); - if (qop.find("auth-int") != std::string::npos) { - qop = "auth-int"; - } else if (qop.find("auth") != std::string::npos) { - qop = "auth"; - } else { - qop.clear(); - } - } - - std::string algo = "MD5"; - if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } - - std::string response; - { - auto H = algo == "SHA-256" ? detail::SHA_256 - : algo == "SHA-512" ? detail::SHA_512 - : detail::MD5; - - auto A1 = username + ":" + auth.at("realm") + ":" + password; - - auto A2 = req.method + ":" + req.path; - if (qop == "auth-int") { A2 += ":" + H(req.body); } - - if (qop.empty()) { - response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); - } else { - response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce + - ":" + qop + ":" + H(A2)); - } - } - - auto opaque = (auth.find("opaque") != auth.end()) ? auth.at("opaque") : ""; - - auto field = "Digest username=\"" + username + "\", realm=\"" + - auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + - "\", uri=\"" + req.path + "\", algorithm=" + algo + - (qop.empty() ? ", response=\"" - : ", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + - cnonce + "\", response=\"") + - response + "\"" + - (opaque.empty() ? "" : ", opaque=\"" + opaque + "\""); - - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, field); -} - -inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { - detail::set_nonblocking(sock, true); - auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); - - char buf[1]; - return !SSL_peek(ssl, buf, 1) && - SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; -} - -#ifdef _WIN32 -// NOTE: This code came up with the following stackoverflow post: -// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store -inline bool load_system_certs_on_windows(X509_STORE *store) { - auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); - if (!hStore) { return false; } - - auto result = false; - PCCERT_CONTEXT pContext = NULL; - while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != - nullptr) { - auto encoded_cert = - static_cast(pContext->pbCertEncoded); - - auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); - if (x509) { - X509_STORE_add_cert(store, x509); - X509_free(x509); - result = true; - } - } - - CertFreeCertificateContext(pContext); - CertCloseStore(hStore, 0); - - return result; -} -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#if TARGET_OS_OSX -template -using CFObjectPtr = - std::unique_ptr::type, void (*)(CFTypeRef)>; - -inline void cf_object_ptr_deleter(CFTypeRef obj) { - if (obj) { CFRelease(obj); } -} - -inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { - CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; - CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, - kCFBooleanTrue}; - - CFObjectPtr query( - CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, - sizeof(keys) / sizeof(keys[0]), - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks), - cf_object_ptr_deleter); - - if (!query) { return false; } - - CFTypeRef security_items = nullptr; - if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || - CFArrayGetTypeID() != CFGetTypeID(security_items)) { - return false; - } - - certs.reset(reinterpret_cast(security_items)); - return true; -} - -inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { - CFArrayRef root_security_items = nullptr; - if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { - return false; - } - - certs.reset(root_security_items); - return true; -} - -inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { - auto result = false; - for (auto i = 0; i < CFArrayGetCount(certs); ++i) { - const auto cert = reinterpret_cast( - CFArrayGetValueAtIndex(certs, i)); - - if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } - - CFDataRef cert_data = nullptr; - if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != - errSecSuccess) { - continue; - } - - CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); - - auto encoded_cert = static_cast( - CFDataGetBytePtr(cert_data_ptr.get())); - - auto x509 = - d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); - - if (x509) { - X509_STORE_add_cert(store, x509); - X509_free(x509); - result = true; - } - } - - return result; -} - -inline bool load_system_certs_on_macos(X509_STORE *store) { - auto result = false; - CFObjectPtr certs(nullptr, cf_object_ptr_deleter); - if (retrieve_certs_from_keychain(certs) && certs) { - result = add_certs_to_x509_store(certs.get(), store); - } - - if (retrieve_root_certs_from_keychain(certs) && certs) { - result = add_certs_to_x509_store(certs.get(), store) || result; - } - - return result; -} -#endif // TARGET_OS_OSX -#endif // _WIN32 -#endif // CPPHTTPLIB_OPENSSL_SUPPORT - -#ifdef _WIN32 -class WSInit { -public: - WSInit() { - WSADATA wsaData; - if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; - } - - ~WSInit() { - if (is_valid_) WSACleanup(); - } - - bool is_valid_ = false; -}; - -static WSInit wsinit_; -#endif - -inline bool parse_www_authenticate(const Response &res, - std::map &auth, - bool is_proxy) { - auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; - if (res.has_header(auth_key)) { - static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); - auto s = res.get_header_value(auth_key); - auto pos = s.find(' '); - if (pos != std::string::npos) { - auto type = s.substr(0, pos); - if (type == "Basic") { - return false; - } else if (type == "Digest") { - s = s.substr(pos + 1); - auto beg = std::sregex_iterator(s.begin(), s.end(), re); - for (auto i = beg; i != std::sregex_iterator(); ++i) { - const auto &m = *i; - auto key = s.substr(static_cast(m.position(1)), - static_cast(m.length(1))); - auto val = m.length(2) > 0 - ? s.substr(static_cast(m.position(2)), - static_cast(m.length(2))) - : s.substr(static_cast(m.position(3)), - static_cast(m.length(3))); - auth[key] = val; - } - return true; - } - } - } - return false; -} - -class ContentProviderAdapter { -public: - explicit ContentProviderAdapter( - ContentProviderWithoutLength &&content_provider) - : content_provider_(content_provider) {} - - bool operator()(size_t offset, size_t, DataSink &sink) { - return content_provider_(offset, sink); - } - -private: - ContentProviderWithoutLength content_provider_; -}; - -} // namespace detail - -inline std::string hosted_at(const std::string &hostname) { - std::vector addrs; - hosted_at(hostname, addrs); - if (addrs.empty()) { return std::string(); } - return addrs[0]; -} - -inline void hosted_at(const std::string &hostname, - std::vector &addrs) { - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; - - if (getaddrinfo(hostname.c_str(), nullptr, &hints, &result)) { -#if defined __linux__ && !defined __ANDROID__ - res_init(); -#endif - return; - } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); - - for (auto rp = result; rp; rp = rp->ai_next) { - const auto &addr = - *reinterpret_cast(rp->ai_addr); - std::string ip; - auto dummy = -1; - if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, - dummy)) { - addrs.push_back(ip); - } - } -} - -inline std::string append_query_params(const std::string &path, - const Params ¶ms) { - std::string path_with_query = path; - const static std::regex re("[^?]+\\?.*"); - auto delm = std::regex_match(path, re) ? '&' : '?'; - path_with_query += delm + detail::params_to_query_str(params); - return path_with_query; -} - -// Header utilities -inline std::pair -make_range_header(const Ranges &ranges) { - std::string field = "bytes="; - auto i = 0; - for (const auto &r : ranges) { - if (i != 0) { field += ", "; } - if (r.first != -1) { field += std::to_string(r.first); } - field += '-'; - if (r.second != -1) { field += std::to_string(r.second); } - i++; - } - return std::make_pair("Range", std::move(field)); -} - -inline std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, bool is_proxy) { - auto field = "Basic " + detail::base64_encode(username + ":" + password); - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, std::move(field)); -} - -inline std::pair -make_bearer_token_authentication_header(const std::string &token, - bool is_proxy = false) { - auto field = "Bearer " + token; - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, std::move(field)); -} - -// Request implementation -inline bool Request::has_header(const std::string &key) const { - return detail::has_header(headers, key); -} - -inline std::string Request::get_header_value(const std::string &key, - const char *def, size_t id) const { - return detail::get_header_value(headers, key, def, id); -} - -inline size_t Request::get_header_value_count(const std::string &key) const { - auto r = headers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -inline void Request::set_header(const std::string &key, - const std::string &val) { - if (detail::fields::is_field_name(key) && - detail::fields::is_field_value(val)) { - headers.emplace(key, val); - } -} - -inline bool Request::has_param(const std::string &key) const { - return params.find(key) != params.end(); -} - -inline std::string Request::get_param_value(const std::string &key, - size_t id) const { - auto rng = params.equal_range(key); - auto it = rng.first; - std::advance(it, static_cast(id)); - if (it != rng.second) { return it->second; } - return std::string(); -} - -inline size_t Request::get_param_value_count(const std::string &key) const { - auto r = params.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -inline bool Request::is_multipart_form_data() const { - const auto &content_type = get_header_value("Content-Type"); - return !content_type.rfind("multipart/form-data", 0); -} - -inline bool Request::has_file(const std::string &key) const { - return files.find(key) != files.end(); -} - -inline MultipartFormData Request::get_file_value(const std::string &key) const { - auto it = files.find(key); - if (it != files.end()) { return it->second; } - return MultipartFormData(); -} - -inline std::vector -Request::get_file_values(const std::string &key) const { - std::vector values; - auto rng = files.equal_range(key); - for (auto it = rng.first; it != rng.second; it++) { - values.push_back(it->second); - } - return values; -} - -// Response implementation -inline bool Response::has_header(const std::string &key) const { - return headers.find(key) != headers.end(); -} - -inline std::string Response::get_header_value(const std::string &key, - const char *def, - size_t id) const { - return detail::get_header_value(headers, key, def, id); -} - -inline size_t Response::get_header_value_count(const std::string &key) const { - auto r = headers.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -inline void Response::set_header(const std::string &key, - const std::string &val) { - if (detail::fields::is_field_name(key) && - detail::fields::is_field_value(val)) { - headers.emplace(key, val); - } -} - -inline void Response::set_redirect(const std::string &url, int stat) { - if (detail::fields::is_field_value(url)) { - set_header("Location", url); - if (300 <= stat && stat < 400) { - this->status = stat; - } else { - this->status = StatusCode::Found_302; - } - } -} - -inline void Response::set_content(const char *s, size_t n, - const std::string &content_type) { - body.assign(s, n); - - auto rng = headers.equal_range("Content-Type"); - headers.erase(rng.first, rng.second); - set_header("Content-Type", content_type); -} - -inline void Response::set_content(const std::string &s, - const std::string &content_type) { - set_content(s.data(), s.size(), content_type); -} - -inline void Response::set_content(std::string &&s, - const std::string &content_type) { - body = std::move(s); - - auto rng = headers.equal_range("Content-Type"); - headers.erase(rng.first, rng.second); - set_header("Content-Type", content_type); -} - -inline void Response::set_content_provider( - size_t in_length, const std::string &content_type, ContentProvider provider, - ContentProviderResourceReleaser resource_releaser) { - set_header("Content-Type", content_type); - content_length_ = in_length; - if (in_length > 0) { content_provider_ = std::move(provider); } - content_provider_resource_releaser_ = std::move(resource_releaser); - is_chunked_content_provider_ = false; -} - -inline void Response::set_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser) { - set_header("Content-Type", content_type); - content_length_ = 0; - content_provider_ = detail::ContentProviderAdapter(std::move(provider)); - content_provider_resource_releaser_ = std::move(resource_releaser); - is_chunked_content_provider_ = false; -} - -inline void Response::set_chunked_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, - ContentProviderResourceReleaser resource_releaser) { - set_header("Content-Type", content_type); - content_length_ = 0; - content_provider_ = detail::ContentProviderAdapter(std::move(provider)); - content_provider_resource_releaser_ = std::move(resource_releaser); - is_chunked_content_provider_ = true; -} - -inline void Response::set_file_content(const std::string &path, - const std::string &content_type) { - file_content_path_ = path; - file_content_content_type_ = content_type; -} - -inline void Response::set_file_content(const std::string &path) { - file_content_path_ = path; -} - -// Result implementation -inline bool Result::has_request_header(const std::string &key) const { - return request_headers_.find(key) != request_headers_.end(); -} - -inline std::string Result::get_request_header_value(const std::string &key, - const char *def, - size_t id) const { - return detail::get_header_value(request_headers_, key, def, id); -} - -inline size_t -Result::get_request_header_value_count(const std::string &key) const { - auto r = request_headers_.equal_range(key); - return static_cast(std::distance(r.first, r.second)); -} - -// Stream implementation -inline ssize_t Stream::write(const char *ptr) { - return write(ptr, strlen(ptr)); -} - -inline ssize_t Stream::write(const std::string &s) { - return write(s.data(), s.size()); -} - -namespace detail { - -inline void calc_actual_timeout(time_t max_timeout_msec, - time_t duration_msec, time_t timeout_sec, - time_t timeout_usec, time_t &actual_timeout_sec, - time_t &actual_timeout_usec) { - auto timeout_msec = (timeout_sec * 1000) + (timeout_usec / 1000); - - auto actual_timeout_msec = - std::min(max_timeout_msec - duration_msec, timeout_msec); - - actual_timeout_sec = actual_timeout_msec / 1000; - actual_timeout_usec = (actual_timeout_msec % 1000) * 1000; -} - -// Socket stream implementation -inline SocketStream::SocketStream( - socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time) - : sock_(sock), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec), - write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec), - max_timeout_msec_(max_timeout_msec), start_time(start_time), - read_buff_(read_buff_size_, 0) {} - -inline SocketStream::~SocketStream() = default; - -inline bool SocketStream::is_readable() const { - if (max_timeout_msec_ <= 0) { - return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; - } - - time_t read_timeout_sec; - time_t read_timeout_usec; - calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, - read_timeout_usec_, read_timeout_sec, read_timeout_usec); - - return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; -} - -inline bool SocketStream::is_writable() const { - return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); -} - -inline ssize_t SocketStream::read(char *ptr, size_t size) { -#ifdef _WIN32 - size = - (std::min)(size, static_cast((std::numeric_limits::max)())); -#else - size = (std::min)(size, - static_cast((std::numeric_limits::max)())); -#endif - - if (read_buff_off_ < read_buff_content_size_) { - auto remaining_size = read_buff_content_size_ - read_buff_off_; - if (size <= remaining_size) { - memcpy(ptr, read_buff_.data() + read_buff_off_, size); - read_buff_off_ += size; - return static_cast(size); - } else { - memcpy(ptr, read_buff_.data() + read_buff_off_, remaining_size); - read_buff_off_ += remaining_size; - return static_cast(remaining_size); - } - } - - if (!is_readable()) { return -1; } - - read_buff_off_ = 0; - read_buff_content_size_ = 0; - - if (size < read_buff_size_) { - auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, - CPPHTTPLIB_RECV_FLAGS); - if (n <= 0) { - return n; - } else if (n <= static_cast(size)) { - memcpy(ptr, read_buff_.data(), static_cast(n)); - return n; - } else { - memcpy(ptr, read_buff_.data(), size); - read_buff_off_ = size; - read_buff_content_size_ = static_cast(n); - return static_cast(size); - } - } else { - return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); - } -} - -inline ssize_t SocketStream::write(const char *ptr, size_t size) { - if (!is_writable()) { return -1; } - -#if defined(_WIN32) && !defined(_WIN64) - size = - (std::min)(size, static_cast((std::numeric_limits::max)())); -#endif - - return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); -} - -inline void SocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { - return detail::get_remote_ip_and_port(sock_, ip, port); -} - -inline void SocketStream::get_local_ip_and_port(std::string &ip, - int &port) const { - return detail::get_local_ip_and_port(sock_, ip, port); -} - -inline socket_t SocketStream::socket() const { return sock_; } - -inline time_t SocketStream::duration() const { - return std::chrono::duration_cast( - std::chrono::steady_clock::now() - start_time) - .count(); -} - -// Buffer stream implementation -inline bool BufferStream::is_readable() const { return true; } - -inline bool BufferStream::is_writable() const { return true; } - -inline ssize_t BufferStream::read(char *ptr, size_t size) { -#if defined(_MSC_VER) && _MSC_VER < 1910 - auto len_read = buffer._Copy_s(ptr, size, size, position); -#else - auto len_read = buffer.copy(ptr, size, position); -#endif - position += static_cast(len_read); - return static_cast(len_read); -} - -inline ssize_t BufferStream::write(const char *ptr, size_t size) { - buffer.append(ptr, size); - return static_cast(size); -} - -inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, - int & /*port*/) const {} - -inline void BufferStream::get_local_ip_and_port(std::string & /*ip*/, - int & /*port*/) const {} - -inline socket_t BufferStream::socket() const { return 0; } - -inline time_t BufferStream::duration() const { return 0; } - -inline const std::string &BufferStream::get_buffer() const { return buffer; } - -inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) { - static constexpr char marker[] = "/:"; - - // One past the last ending position of a path param substring - std::size_t last_param_end = 0; - -#ifndef CPPHTTPLIB_NO_EXCEPTIONS - // Needed to ensure that parameter names are unique during matcher - // construction - // If exceptions are disabled, only last duplicate path - // parameter will be set - std::unordered_set param_name_set; -#endif - - while (true) { - const auto marker_pos = pattern.find( - marker, last_param_end == 0 ? last_param_end : last_param_end - 1); - if (marker_pos == std::string::npos) { break; } - - static_fragments_.push_back( - pattern.substr(last_param_end, marker_pos - last_param_end + 1)); - - const auto param_name_start = marker_pos + 2; - - auto sep_pos = pattern.find(separator, param_name_start); - if (sep_pos == std::string::npos) { sep_pos = pattern.length(); } - - auto param_name = - pattern.substr(param_name_start, sep_pos - param_name_start); - -#ifndef CPPHTTPLIB_NO_EXCEPTIONS - if (param_name_set.find(param_name) != param_name_set.cend()) { - std::string msg = "Encountered path parameter '" + param_name + - "' multiple times in route pattern '" + pattern + "'."; - throw std::invalid_argument(msg); - } -#endif - - param_names_.push_back(std::move(param_name)); - - last_param_end = sep_pos + 1; - } - - if (last_param_end < pattern.length()) { - static_fragments_.push_back(pattern.substr(last_param_end)); - } -} - -inline bool PathParamsMatcher::match(Request &request) const { - request.matches = std::smatch(); - request.path_params.clear(); - request.path_params.reserve(param_names_.size()); - - // One past the position at which the path matched the pattern last time - std::size_t starting_pos = 0; - for (size_t i = 0; i < static_fragments_.size(); ++i) { - const auto &fragment = static_fragments_[i]; - - if (starting_pos + fragment.length() > request.path.length()) { - return false; - } - - // Avoid unnecessary allocation by using strncmp instead of substr + - // comparison - if (std::strncmp(request.path.c_str() + starting_pos, fragment.c_str(), - fragment.length()) != 0) { - return false; - } - - starting_pos += fragment.length(); - - // Should only happen when we have a static fragment after a param - // Example: '/users/:id/subscriptions' - // The 'subscriptions' fragment here does not have a corresponding param - if (i >= param_names_.size()) { continue; } - - auto sep_pos = request.path.find(separator, starting_pos); - if (sep_pos == std::string::npos) { sep_pos = request.path.length(); } - - const auto ¶m_name = param_names_[i]; - - request.path_params.emplace( - param_name, request.path.substr(starting_pos, sep_pos - starting_pos)); - - // Mark everything up to '/' as matched - starting_pos = sep_pos + 1; - } - // Returns false if the path is longer than the pattern - return starting_pos >= request.path.length(); -} - -inline bool RegexMatcher::match(Request &request) const { - request.path_params.clear(); - return std::regex_match(request.path, request.matches, regex_); -} - -} // namespace detail - -// HTTP server implementation -inline Server::Server() - : new_task_queue( - [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { -#ifndef _WIN32 - signal(SIGPIPE, SIG_IGN); -#endif -} - -inline Server::~Server() = default; - -inline std::unique_ptr -Server::make_matcher(const std::string &pattern) { - if (pattern.find("/:") != std::string::npos) { - return detail::make_unique(pattern); - } else { - return detail::make_unique(pattern); - } -} - -inline Server &Server::Get(const std::string &pattern, Handler handler) { - get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Post(const std::string &pattern, Handler handler) { - post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Post(const std::string &pattern, - HandlerWithContentReader handler) { - post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Put(const std::string &pattern, Handler handler) { - put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Put(const std::string &pattern, - HandlerWithContentReader handler) { - put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Patch(const std::string &pattern, Handler handler) { - patch_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Patch(const std::string &pattern, - HandlerWithContentReader handler) { - patch_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Delete(const std::string &pattern, Handler handler) { - delete_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline Server &Server::Delete(const std::string &pattern, - HandlerWithContentReader handler) { - delete_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; -} - -inline Server &Server::Options(const std::string &pattern, Handler handler) { - options_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} - -inline bool Server::set_base_dir(const std::string &dir, - const std::string &mount_point) { - return set_mount_point(mount_point, dir); -} - -inline bool Server::set_mount_point(const std::string &mount_point, - const std::string &dir, Headers headers) { - detail::FileStat stat(dir); - if (stat.is_dir()) { - std::string mnt = !mount_point.empty() ? mount_point : "/"; - if (!mnt.empty() && mnt[0] == '/') { - base_dirs_.push_back({mnt, dir, std::move(headers)}); - return true; - } - } - return false; -} - -inline bool Server::remove_mount_point(const std::string &mount_point) { - for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { - if (it->mount_point == mount_point) { - base_dirs_.erase(it); - return true; - } - } - return false; -} - -inline Server & -Server::set_file_extension_and_mimetype_mapping(const std::string &ext, - const std::string &mime) { - file_extension_and_mimetype_map_[ext] = mime; - return *this; -} - -inline Server &Server::set_default_file_mimetype(const std::string &mime) { - default_file_mimetype_ = mime; - return *this; -} - -inline Server &Server::set_file_request_handler(Handler handler) { - file_request_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_error_handler_core(HandlerWithResponse handler, - std::true_type) { - error_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_error_handler_core(Handler handler, - std::false_type) { - error_handler_ = [handler](const Request &req, Response &res) { - handler(req, res); - return HandlerResponse::Handled; - }; - return *this; -} - -inline Server &Server::set_exception_handler(ExceptionHandler handler) { - exception_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { - pre_routing_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_post_routing_handler(Handler handler) { - post_routing_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_logger(Logger logger) { - logger_ = std::move(logger); - return *this; -} - -inline Server & -Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { - expect_100_continue_handler_ = std::move(handler); - return *this; -} - -inline Server &Server::set_address_family(int family) { - address_family_ = family; - return *this; -} - -inline Server &Server::set_tcp_nodelay(bool on) { - tcp_nodelay_ = on; - return *this; -} - -inline Server &Server::set_ipv6_v6only(bool on) { - ipv6_v6only_ = on; - return *this; -} - -inline Server &Server::set_socket_options(SocketOptions socket_options) { - socket_options_ = std::move(socket_options); - return *this; -} - -inline Server &Server::set_default_headers(Headers headers) { - default_headers_ = std::move(headers); - return *this; -} - -inline Server &Server::set_header_writer( - std::function const &writer) { - header_writer_ = writer; - return *this; -} - -inline Server &Server::set_keep_alive_max_count(size_t count) { - keep_alive_max_count_ = count; - return *this; -} - -inline Server &Server::set_keep_alive_timeout(time_t sec) { - keep_alive_timeout_sec_ = sec; - return *this; -} - -inline Server &Server::set_read_timeout(time_t sec, time_t usec) { - read_timeout_sec_ = sec; - read_timeout_usec_ = usec; - return *this; -} - -inline Server &Server::set_write_timeout(time_t sec, time_t usec) { - write_timeout_sec_ = sec; - write_timeout_usec_ = usec; - return *this; -} - -inline Server &Server::set_idle_interval(time_t sec, time_t usec) { - idle_interval_sec_ = sec; - idle_interval_usec_ = usec; - return *this; -} - -inline Server &Server::set_payload_max_length(size_t length) { - payload_max_length_ = length; - return *this; -} - -inline bool Server::bind_to_port(const std::string &host, int port, - int socket_flags) { - auto ret = bind_internal(host, port, socket_flags); - if (ret == -1) { is_decommisioned = true; } - return ret >= 0; -} -inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { - auto ret = bind_internal(host, 0, socket_flags); - if (ret == -1) { is_decommisioned = true; } - return ret; -} - -inline bool Server::listen_after_bind() { return listen_internal(); } - -inline bool Server::listen(const std::string &host, int port, - int socket_flags) { - return bind_to_port(host, port, socket_flags) && listen_internal(); -} - -inline bool Server::is_running() const { return is_running_; } - -inline void Server::wait_until_ready() const { - while (!is_running_ && !is_decommisioned) { - std::this_thread::sleep_for(std::chrono::milliseconds{1}); - } -} - -inline void Server::stop() { - if (is_running_) { - assert(svr_sock_ != INVALID_SOCKET); - std::atomic sock(svr_sock_.exchange(INVALID_SOCKET)); - detail::shutdown_socket(sock); - detail::close_socket(sock); - } - is_decommisioned = false; -} - -inline void Server::decommission() { is_decommisioned = true; } - -inline bool Server::parse_request_line(const char *s, Request &req) const { - auto len = strlen(s); - if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } - len -= 2; - - { - size_t count = 0; - - detail::split(s, s + len, ' ', [&](const char *b, const char *e) { - switch (count) { - case 0: req.method = std::string(b, e); break; - case 1: req.target = std::string(b, e); break; - case 2: req.version = std::string(b, e); break; - default: break; - } - count++; - }); - - if (count != 3) { return false; } - } - - static const std::set methods{ - "GET", "HEAD", "POST", "PUT", "DELETE", - "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; - - if (methods.find(req.method) == methods.end()) { return false; } - - if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } - - { - // Skip URL fragment - for (size_t i = 0; i < req.target.size(); i++) { - if (req.target[i] == '#') { - req.target.erase(i); - break; - } - } - - detail::divide(req.target, '?', - [&](const char *lhs_data, std::size_t lhs_size, - const char *rhs_data, std::size_t rhs_size) { - req.path = detail::decode_url( - std::string(lhs_data, lhs_size), false); - detail::parse_query_text(rhs_data, rhs_size, req.params); - }); - } - - return true; -} - -inline bool Server::write_response(Stream &strm, bool close_connection, - Request &req, Response &res) { - // NOTE: `req.ranges` should be empty, otherwise it will be applied - // incorrectly to the error content. - req.ranges.clear(); - return write_response_core(strm, close_connection, req, res, false); -} - -inline bool Server::write_response_with_content(Stream &strm, - bool close_connection, - const Request &req, - Response &res) { - return write_response_core(strm, close_connection, req, res, true); -} - -inline bool Server::write_response_core(Stream &strm, bool close_connection, - const Request &req, Response &res, - bool need_apply_ranges) { - assert(res.status != -1); - - if (400 <= res.status && error_handler_ && - error_handler_(req, res) == HandlerResponse::Handled) { - need_apply_ranges = true; - } - - std::string content_type; - std::string boundary; - if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } - - // Prepare additional headers - if (close_connection || req.get_header_value("Connection") == "close") { - res.set_header("Connection", "close"); - } else { - std::string s = "timeout="; - s += std::to_string(keep_alive_timeout_sec_); - s += ", max="; - s += std::to_string(keep_alive_max_count_); - res.set_header("Keep-Alive", s); - } - - if ((!res.body.empty() || res.content_length_ > 0 || res.content_provider_) && - !res.has_header("Content-Type")) { - res.set_header("Content-Type", "text/plain"); - } - - if (res.body.empty() && !res.content_length_ && !res.content_provider_ && - !res.has_header("Content-Length")) { - res.set_header("Content-Length", "0"); - } - - if (req.method == "HEAD" && !res.has_header("Accept-Ranges")) { - res.set_header("Accept-Ranges", "bytes"); - } - - if (post_routing_handler_) { post_routing_handler_(req, res); } - - // Response line and headers - { - detail::BufferStream bstrm; - if (!detail::write_response_line(bstrm, res.status)) { return false; } - if (!header_writer_(bstrm, res.headers)) { return false; } - - // Flush buffer - auto &data = bstrm.get_buffer(); - detail::write_data(strm, data.data(), data.size()); - } - - // Body - auto ret = true; - if (req.method != "HEAD") { - if (!res.body.empty()) { - if (!detail::write_data(strm, res.body.data(), res.body.size())) { - ret = false; - } - } else if (res.content_provider_) { - if (write_content_with_provider(strm, req, res, boundary, content_type)) { - res.content_provider_success_ = true; - } else { - ret = false; - } - } - } - - // Log - if (logger_) { logger_(req, res); } - - return ret; -} - -inline bool -Server::write_content_with_provider(Stream &strm, const Request &req, - Response &res, const std::string &boundary, - const std::string &content_type) { - auto is_shutting_down = [this]() { - return this->svr_sock_ == INVALID_SOCKET; - }; - - if (res.content_length_ > 0) { - if (req.ranges.empty()) { - return detail::write_content(strm, res.content_provider_, 0, - res.content_length_, is_shutting_down); - } else if (req.ranges.size() == 1) { - auto offset_and_length = detail::get_range_offset_and_length( - req.ranges[0], res.content_length_); - - return detail::write_content(strm, res.content_provider_, - offset_and_length.first, - offset_and_length.second, is_shutting_down); - } else { - return detail::write_multipart_ranges_data( - strm, req, res, boundary, content_type, res.content_length_, - is_shutting_down); - } - } else { - if (res.is_chunked_content_provider_) { - auto type = detail::encoding_type(req, res); - - std::unique_ptr compressor; - if (type == detail::EncodingType::Gzip) { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - compressor = detail::make_unique(); -#endif - } else if (type == detail::EncodingType::Brotli) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - compressor = detail::make_unique(); -#endif - } else { - compressor = detail::make_unique(); - } - assert(compressor != nullptr); - - return detail::write_content_chunked(strm, res.content_provider_, - is_shutting_down, *compressor); - } else { - return detail::write_content_without_length(strm, res.content_provider_, - is_shutting_down); - } - } -} - -inline bool Server::read_content(Stream &strm, Request &req, Response &res) { - MultipartFormDataMap::iterator cur; - auto file_count = 0; - if (read_content_core( - strm, req, res, - // Regular - [&](const char *buf, size_t n) { - if (req.body.size() + n > req.body.max_size()) { return false; } - req.body.append(buf, n); - return true; - }, - // Multipart - [&](const MultipartFormData &file) { - if (file_count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { - return false; - } - cur = req.files.emplace(file.name, file); - return true; - }, - [&](const char *buf, size_t n) { - auto &content = cur->second.content; - if (content.size() + n > content.max_size()) { return false; } - content.append(buf, n); - return true; - })) { - const auto &content_type = req.get_header_value("Content-Type"); - if (!content_type.find("application/x-www-form-urlencoded")) { - if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { - res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? - return false; - } - detail::parse_query_text(req.body, req.params); - } - return true; - } - return false; -} - -inline bool Server::read_content_with_content_receiver( - Stream &strm, Request &req, Response &res, ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) { - return read_content_core(strm, req, res, std::move(receiver), - std::move(multipart_header), - std::move(multipart_receiver)); -} - -inline bool -Server::read_content_core(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) const { - detail::MultipartFormDataParser multipart_form_data_parser; - ContentReceiverWithProgress out; - - if (req.is_multipart_form_data()) { - const auto &content_type = req.get_header_value("Content-Type"); - std::string boundary; - if (!detail::parse_multipart_boundary(content_type, boundary)) { - res.status = StatusCode::BadRequest_400; - return false; - } - - multipart_form_data_parser.set_boundary(std::move(boundary)); - out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { - /* For debug - size_t pos = 0; - while (pos < n) { - auto read_size = (std::min)(1, n - pos); - auto ret = multipart_form_data_parser.parse( - buf + pos, read_size, multipart_receiver, multipart_header); - if (!ret) { return false; } - pos += read_size; - } - return true; - */ - return multipart_form_data_parser.parse(buf, n, multipart_receiver, - multipart_header); - }; - } else { - out = [receiver](const char *buf, size_t n, uint64_t /*off*/, - uint64_t /*len*/) { return receiver(buf, n); }; - } - - if (req.method == "DELETE" && !req.has_header("Content-Length")) { - return true; - } - - if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, - out, true)) { - return false; - } - - if (req.is_multipart_form_data()) { - if (!multipart_form_data_parser.is_valid()) { - res.status = StatusCode::BadRequest_400; - return false; - } - } - - return true; -} - -inline bool Server::handle_file_request(const Request &req, Response &res, - bool head) { - for (const auto &entry : base_dirs_) { - // Prefix match - if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { - std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); - if (detail::is_valid_path(sub_path)) { - auto path = entry.base_dir + sub_path; - if (path.back() == '/') { path += "index.html"; } - - detail::FileStat stat(path); - - if (stat.is_dir()) { - res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301); - return true; - } - - if (stat.is_file()) { - for (const auto &kv : entry.headers) { - res.set_header(kv.first, kv.second); - } - - auto mm = std::make_shared(path.c_str()); - if (!mm->is_open()) { return false; } - - res.set_content_provider( - mm->size(), - detail::find_content_type(path, file_extension_and_mimetype_map_, - default_file_mimetype_), - [mm](size_t offset, size_t length, DataSink &sink) -> bool { - sink.write(mm->data() + offset, length); - return true; - }); - - if (!head && file_request_handler_) { - file_request_handler_(req, res); - } - - return true; - } - } - } - } - return false; -} - -inline socket_t -Server::create_server_socket(const std::string &host, int port, - int socket_flags, - SocketOptions socket_options) const { - return detail::create_socket( - host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, - ipv6_v6only_, std::move(socket_options), - [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { - if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { - return false; - } - if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } - return true; - }); -} - -inline int Server::bind_internal(const std::string &host, int port, - int socket_flags) { - if (is_decommisioned) { return -1; } - - if (!is_valid()) { return -1; } - - svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); - if (svr_sock_ == INVALID_SOCKET) { return -1; } - - if (port == 0) { - struct sockaddr_storage addr; - socklen_t addr_len = sizeof(addr); - if (getsockname(svr_sock_, reinterpret_cast(&addr), - &addr_len) == -1) { - return -1; - } - if (addr.ss_family == AF_INET) { - return ntohs(reinterpret_cast(&addr)->sin_port); - } else if (addr.ss_family == AF_INET6) { - return ntohs(reinterpret_cast(&addr)->sin6_port); - } else { - return -1; - } - } else { - return port; - } -} - -inline bool Server::listen_internal() { - if (is_decommisioned) { return false; } - - auto ret = true; - is_running_ = true; - auto se = detail::scope_exit([&]() { is_running_ = false; }); - - { - std::unique_ptr task_queue(new_task_queue()); - - while (svr_sock_ != INVALID_SOCKET) { -#ifndef _WIN32 - if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { -#endif - auto val = detail::select_read(svr_sock_, idle_interval_sec_, - idle_interval_usec_); - if (val == 0) { // Timeout - task_queue->on_idle(); - continue; - } -#ifndef _WIN32 - } -#endif - -#if defined _WIN32 - // sockets conneced via WASAccept inherit flags NO_HANDLE_INHERIT, - // OVERLAPPED - socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); -#elif defined SOCK_CLOEXEC - socket_t sock = accept4(svr_sock_, nullptr, nullptr, SOCK_CLOEXEC); -#else - socket_t sock = accept(svr_sock_, nullptr, nullptr); -#endif - - if (sock == INVALID_SOCKET) { - if (errno == EMFILE) { - // The per-process limit of open file descriptors has been reached. - // Try to accept new connections after a short sleep. - std::this_thread::sleep_for(std::chrono::microseconds{1}); - continue; - } else if (errno == EINTR || errno == EAGAIN) { - continue; - } - if (svr_sock_ != INVALID_SOCKET) { - detail::close_socket(svr_sock_); - ret = false; - } else { - ; // The server socket was closed by user. - } - break; - } - - { -#ifdef _WIN32 - auto timeout = static_cast(read_timeout_sec_ * 1000 + - read_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(read_timeout_sec_); - tv.tv_usec = static_cast(read_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - { - -#ifdef _WIN32 - auto timeout = static_cast(write_timeout_sec_ * 1000 + - write_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&timeout), sizeof(timeout)); -#else - timeval tv; - tv.tv_sec = static_cast(write_timeout_sec_); - tv.tv_usec = static_cast(write_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, - reinterpret_cast(&tv), sizeof(tv)); -#endif - } - - if (!task_queue->enqueue( - [this, sock]() { process_and_close_socket(sock); })) { - detail::shutdown_socket(sock); - detail::close_socket(sock); - } - } - - task_queue->shutdown(); - } - - is_decommisioned = !ret; - return ret; -} - -inline bool Server::routing(Request &req, Response &res, Stream &strm) { - if (pre_routing_handler_ && - pre_routing_handler_(req, res) == HandlerResponse::Handled) { - return true; - } - - // File handler - auto is_head_request = req.method == "HEAD"; - if ((req.method == "GET" || is_head_request) && - handle_file_request(req, res, is_head_request)) { - return true; - } - - if (detail::expect_content(req)) { - // Content reader handler - { - ContentReader reader( - [&](ContentReceiver receiver) { - return read_content_with_content_receiver( - strm, req, res, std::move(receiver), nullptr, nullptr); - }, - [&](MultipartContentHeader header, ContentReceiver receiver) { - return read_content_with_content_receiver(strm, req, res, nullptr, - std::move(header), - std::move(receiver)); - }); - - if (req.method == "POST") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - post_handlers_for_content_reader_)) { - return true; - } - } else if (req.method == "PUT") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - put_handlers_for_content_reader_)) { - return true; - } - } else if (req.method == "PATCH") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - patch_handlers_for_content_reader_)) { - return true; - } - } else if (req.method == "DELETE") { - if (dispatch_request_for_content_reader( - req, res, std::move(reader), - delete_handlers_for_content_reader_)) { - return true; - } - } - } - - // Read content into `req.body` - if (!read_content(strm, req, res)) { return false; } - } - - // Regular handler - if (req.method == "GET" || req.method == "HEAD") { - return dispatch_request(req, res, get_handlers_); - } else if (req.method == "POST") { - return dispatch_request(req, res, post_handlers_); - } else if (req.method == "PUT") { - return dispatch_request(req, res, put_handlers_); - } else if (req.method == "DELETE") { - return dispatch_request(req, res, delete_handlers_); - } else if (req.method == "OPTIONS") { - return dispatch_request(req, res, options_handlers_); - } else if (req.method == "PATCH") { - return dispatch_request(req, res, patch_handlers_); - } - - res.status = StatusCode::BadRequest_400; - return false; -} - -inline bool Server::dispatch_request(Request &req, Response &res, - const Handlers &handlers) const { - for (const auto &x : handlers) { - const auto &matcher = x.first; - const auto &handler = x.second; - - if (matcher->match(req)) { - handler(req, res); - return true; - } - } - return false; -} - -inline void Server::apply_ranges(const Request &req, Response &res, - std::string &content_type, - std::string &boundary) const { - if (req.ranges.size() > 1 && res.status == StatusCode::PartialContent_206) { - auto it = res.headers.find("Content-Type"); - if (it != res.headers.end()) { - content_type = it->second; - res.headers.erase(it); - } - - boundary = detail::make_multipart_data_boundary(); - - res.set_header("Content-Type", - "multipart/byteranges; boundary=" + boundary); - } - - auto type = detail::encoding_type(req, res); - - if (res.body.empty()) { - if (res.content_length_ > 0) { - size_t length = 0; - if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { - length = res.content_length_; - } else if (req.ranges.size() == 1) { - auto offset_and_length = detail::get_range_offset_and_length( - req.ranges[0], res.content_length_); - - length = offset_and_length.second; - - auto content_range = detail::make_content_range_header_field( - offset_and_length, res.content_length_); - res.set_header("Content-Range", content_range); - } else { - length = detail::get_multipart_ranges_data_length( - req, boundary, content_type, res.content_length_); - } - res.set_header("Content-Length", std::to_string(length)); - } else { - if (res.content_provider_) { - if (res.is_chunked_content_provider_) { - res.set_header("Transfer-Encoding", "chunked"); - if (type == detail::EncodingType::Gzip) { - res.set_header("Content-Encoding", "gzip"); - } else if (type == detail::EncodingType::Brotli) { - res.set_header("Content-Encoding", "br"); - } - } - } - } - } else { - if (req.ranges.empty() || res.status != StatusCode::PartialContent_206) { - ; - } else if (req.ranges.size() == 1) { - auto offset_and_length = - detail::get_range_offset_and_length(req.ranges[0], res.body.size()); - auto offset = offset_and_length.first; - auto length = offset_and_length.second; - - auto content_range = detail::make_content_range_header_field( - offset_and_length, res.body.size()); - res.set_header("Content-Range", content_range); - - assert(offset + length <= res.body.size()); - res.body = res.body.substr(offset, length); - } else { - std::string data; - detail::make_multipart_ranges_data(req, res, boundary, content_type, - res.body.size(), data); - res.body.swap(data); - } - - if (type != detail::EncodingType::None) { - std::unique_ptr compressor; - std::string content_encoding; - - if (type == detail::EncodingType::Gzip) { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - compressor = detail::make_unique(); - content_encoding = "gzip"; -#endif - } else if (type == detail::EncodingType::Brotli) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - compressor = detail::make_unique(); - content_encoding = "br"; -#endif - } - - if (compressor) { - std::string compressed; - if (compressor->compress(res.body.data(), res.body.size(), true, - [&](const char *data, size_t data_len) { - compressed.append(data, data_len); - return true; - })) { - res.body.swap(compressed); - res.set_header("Content-Encoding", content_encoding); - } - } - } - - auto length = std::to_string(res.body.size()); - res.set_header("Content-Length", length); - } -} - -inline bool Server::dispatch_request_for_content_reader( - Request &req, Response &res, ContentReader content_reader, - const HandlersForContentReader &handlers) const { - for (const auto &x : handlers) { - const auto &matcher = x.first; - const auto &handler = x.second; - - if (matcher->match(req)) { - handler(req, res, content_reader); - return true; - } - } - return false; -} - -inline bool -Server::process_request(Stream &strm, const std::string &remote_addr, - int remote_port, const std::string &local_addr, - int local_port, bool close_connection, - bool &connection_closed, - const std::function &setup_request) { - std::array buf{}; - - detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); - - // Connection has been closed on client - if (!line_reader.getline()) { return false; } - - Request req; - - Response res; - res.version = "HTTP/1.1"; - res.headers = default_headers_; - -#ifdef _WIN32 - // TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL). -#else -#ifndef CPPHTTPLIB_USE_POLL - // Socket file descriptor exceeded FD_SETSIZE... - if (strm.socket() >= FD_SETSIZE) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::InternalServerError_500; - return write_response(strm, close_connection, req, res); - } -#endif -#endif - - // Request line and headers - if (!parse_request_line(line_reader.ptr(), req) || - !detail::read_headers(strm, req.headers)) { - res.status = StatusCode::BadRequest_400; - return write_response(strm, close_connection, req, res); - } - - // Check if the request URI doesn't exceed the limit - if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::UriTooLong_414; - return write_response(strm, close_connection, req, res); - } - - if (req.get_header_value("Connection") == "close") { - connection_closed = true; - } - - if (req.version == "HTTP/1.0" && - req.get_header_value("Connection") != "Keep-Alive") { - connection_closed = true; - } - - req.remote_addr = remote_addr; - req.remote_port = remote_port; - req.set_header("REMOTE_ADDR", req.remote_addr); - req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); - - req.local_addr = local_addr; - req.local_port = local_port; - req.set_header("LOCAL_ADDR", req.local_addr); - req.set_header("LOCAL_PORT", std::to_string(req.local_port)); - - if (req.has_header("Range")) { - const auto &range_header_value = req.get_header_value("Range"); - if (!detail::parse_range_header(range_header_value, req.ranges)) { - res.status = StatusCode::RangeNotSatisfiable_416; - return write_response(strm, close_connection, req, res); - } - } - - if (setup_request) { setup_request(req); } - - if (req.get_header_value("Expect") == "100-continue") { - int status = StatusCode::Continue_100; - if (expect_100_continue_handler_) { - status = expect_100_continue_handler_(req, res); - } - switch (status) { - case StatusCode::Continue_100: - case StatusCode::ExpectationFailed_417: - detail::write_response_line(strm, status); - strm.write("\r\n"); - break; - default: - connection_closed = true; - return write_response(strm, true, req, res); - } - } - - // Setup `is_connection_closed` method - req.is_connection_closed = [&]() { - return !detail::is_socket_alive(strm.socket()); - }; - - // Routing - auto routed = false; -#ifdef CPPHTTPLIB_NO_EXCEPTIONS - routed = routing(req, res, strm); -#else - try { - routed = routing(req, res, strm); - } catch (std::exception &e) { - if (exception_handler_) { - auto ep = std::current_exception(); - exception_handler_(req, res, ep); - routed = true; - } else { - res.status = StatusCode::InternalServerError_500; - std::string val; - auto s = e.what(); - for (size_t i = 0; s[i]; i++) { - switch (s[i]) { - case '\r': val += "\\r"; break; - case '\n': val += "\\n"; break; - default: val += s[i]; break; - } - } - res.set_header("EXCEPTION_WHAT", val); - } - } catch (...) { - if (exception_handler_) { - auto ep = std::current_exception(); - exception_handler_(req, res, ep); - routed = true; - } else { - res.status = StatusCode::InternalServerError_500; - res.set_header("EXCEPTION_WHAT", "UNKNOWN"); - } - } -#endif - if (routed) { - if (res.status == -1) { - res.status = req.ranges.empty() ? StatusCode::OK_200 - : StatusCode::PartialContent_206; - } - - // Serve file content by using a content provider - if (!res.file_content_path_.empty()) { - const auto &path = res.file_content_path_; - auto mm = std::make_shared(path.c_str()); - if (!mm->is_open()) { - res.body.clear(); - res.content_length_ = 0; - res.content_provider_ = nullptr; - res.status = StatusCode::NotFound_404; - return write_response(strm, close_connection, req, res); - } - - auto content_type = res.file_content_content_type_; - if (content_type.empty()) { - content_type = detail::find_content_type( - path, file_extension_and_mimetype_map_, default_file_mimetype_); - } - - res.set_content_provider( - mm->size(), content_type, - [mm](size_t offset, size_t length, DataSink &sink) -> bool { - sink.write(mm->data() + offset, length); - return true; - }); - } - - if (detail::range_error(req, res)) { - res.body.clear(); - res.content_length_ = 0; - res.content_provider_ = nullptr; - res.status = StatusCode::RangeNotSatisfiable_416; - return write_response(strm, close_connection, req, res); - } - - return write_response_with_content(strm, close_connection, req, res); - } else { - if (res.status == -1) { res.status = StatusCode::NotFound_404; } - - return write_response(strm, close_connection, req, res); - } -} - -inline bool Server::is_valid() const { return true; } - -inline bool Server::process_and_close_socket(socket_t sock) { - std::string remote_addr; - int remote_port = 0; - detail::get_remote_ip_and_port(sock, remote_addr, remote_port); - - std::string local_addr; - int local_port = 0; - detail::get_local_ip_and_port(sock, local_addr, local_port); - - auto ret = detail::process_server_socket( - svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, - [&](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, remote_addr, remote_port, local_addr, - local_port, close_connection, connection_closed, - nullptr); - }); - - detail::shutdown_socket(sock); - detail::close_socket(sock); - return ret; -} - -// HTTP client implementation -inline ClientImpl::ClientImpl(const std::string &host) - : ClientImpl(host, 80, std::string(), std::string()) {} - -inline ClientImpl::ClientImpl(const std::string &host, int port) - : ClientImpl(host, port, std::string(), std::string()) {} - -inline ClientImpl::ClientImpl(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) - : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), - host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), - client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} - -inline ClientImpl::~ClientImpl() { - std::lock_guard guard(socket_mutex_); - shutdown_socket(socket_); - close_socket(socket_); -} - -inline bool ClientImpl::is_valid() const { return true; } - -inline void ClientImpl::copy_settings(const ClientImpl &rhs) { - client_cert_path_ = rhs.client_cert_path_; - client_key_path_ = rhs.client_key_path_; - connection_timeout_sec_ = rhs.connection_timeout_sec_; - read_timeout_sec_ = rhs.read_timeout_sec_; - read_timeout_usec_ = rhs.read_timeout_usec_; - write_timeout_sec_ = rhs.write_timeout_sec_; - write_timeout_usec_ = rhs.write_timeout_usec_; - max_timeout_msec_ = rhs.max_timeout_msec_; - basic_auth_username_ = rhs.basic_auth_username_; - basic_auth_password_ = rhs.basic_auth_password_; - bearer_token_auth_token_ = rhs.bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - digest_auth_username_ = rhs.digest_auth_username_; - digest_auth_password_ = rhs.digest_auth_password_; -#endif - keep_alive_ = rhs.keep_alive_; - follow_location_ = rhs.follow_location_; - url_encode_ = rhs.url_encode_; - address_family_ = rhs.address_family_; - tcp_nodelay_ = rhs.tcp_nodelay_; - ipv6_v6only_ = rhs.ipv6_v6only_; - socket_options_ = rhs.socket_options_; - compress_ = rhs.compress_; - decompress_ = rhs.decompress_; - interface_ = rhs.interface_; - proxy_host_ = rhs.proxy_host_; - proxy_port_ = rhs.proxy_port_; - proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; - proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; - proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; - proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - ca_cert_file_path_ = rhs.ca_cert_file_path_; - ca_cert_dir_path_ = rhs.ca_cert_dir_path_; - ca_cert_store_ = rhs.ca_cert_store_; -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - server_certificate_verification_ = rhs.server_certificate_verification_; - server_hostname_verification_ = rhs.server_hostname_verification_; - server_certificate_verifier_ = rhs.server_certificate_verifier_; -#endif - logger_ = rhs.logger_; -} - -inline socket_t ClientImpl::create_client_socket(Error &error) const { - if (!proxy_host_.empty() && proxy_port_ != -1) { - return detail::create_client_socket( - proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, - ipv6_v6only_, socket_options_, connection_timeout_sec_, - connection_timeout_usec_, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, interface_, error); - } - - // Check is custom IP specified for host_ - std::string ip; - auto it = addr_map_.find(host_); - if (it != addr_map_.end()) { ip = it->second; } - - return detail::create_client_socket( - host_, ip, port_, address_family_, tcp_nodelay_, ipv6_v6only_, - socket_options_, connection_timeout_sec_, connection_timeout_usec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, interface_, error); -} - -inline bool ClientImpl::create_and_connect_socket(Socket &socket, - Error &error) { - auto sock = create_client_socket(error); - if (sock == INVALID_SOCKET) { return false; } - socket.sock = sock; - return true; -} - -inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, - bool /*shutdown_gracefully*/) { - // If there are any requests in flight from threads other than us, then it's - // a thread-unsafe race because individual ssl* objects are not thread-safe. - assert(socket_requests_in_flight_ == 0 || - socket_requests_are_from_thread_ == std::this_thread::get_id()); -} - -inline void ClientImpl::shutdown_socket(Socket &socket) const { - if (socket.sock == INVALID_SOCKET) { return; } - detail::shutdown_socket(socket.sock); -} - -inline void ClientImpl::close_socket(Socket &socket) { - // If there are requests in flight in another thread, usually closing - // the socket will be fine and they will simply receive an error when - // using the closed socket, but it is still a bug since rarely the OS - // may reassign the socket id to be used for a new socket, and then - // suddenly they will be operating on a live socket that is different - // than the one they intended! - assert(socket_requests_in_flight_ == 0 || - socket_requests_are_from_thread_ == std::this_thread::get_id()); - - // It is also a bug if this happens while SSL is still active -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - assert(socket.ssl == nullptr); -#endif - if (socket.sock == INVALID_SOCKET) { return; } - detail::close_socket(socket.sock); - socket.sock = INVALID_SOCKET; -} - -inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, - Response &res) const { - std::array buf{}; - - detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); - - if (!line_reader.getline()) { return false; } - -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n"); -#else - const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); -#endif - - std::cmatch m; - if (!std::regex_match(line_reader.ptr(), m, re)) { - return req.method == "CONNECT"; - } - res.version = std::string(m[1]); - res.status = std::stoi(std::string(m[2])); - res.reason = std::string(m[3]); - - // Ignore '100 Continue' - while (res.status == StatusCode::Continue_100) { - if (!line_reader.getline()) { return false; } // CRLF - if (!line_reader.getline()) { return false; } // next response line - - if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } - res.version = std::string(m[1]); - res.status = std::stoi(std::string(m[2])); - res.reason = std::string(m[3]); - } - - return true; -} - -inline bool ClientImpl::send(Request &req, Response &res, Error &error) { - std::lock_guard request_mutex_guard(request_mutex_); - auto ret = send_(req, res, error); - if (error == Error::SSLPeerCouldBeClosed_) { - assert(!ret); - ret = send_(req, res, error); - } - return ret; -} - -inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { - { - std::lock_guard guard(socket_mutex_); - - // Set this to false immediately - if it ever gets set to true by the end of - // the request, we know another thread instructed us to close the socket. - socket_should_be_closed_when_request_is_done_ = false; - - auto is_alive = false; - if (socket_.is_open()) { - is_alive = detail::is_socket_alive(socket_.sock); - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_alive && is_ssl()) { - if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { - is_alive = false; - } - } -#endif - - if (!is_alive) { - // Attempt to avoid sigpipe by shutting down nongracefully if it seems - // like the other side has already closed the connection Also, there - // cannot be any requests in flight from other threads since we locked - // request_mutex_, so safe to close everything immediately - const bool shutdown_gracefully = false; - shutdown_ssl(socket_, shutdown_gracefully); - shutdown_socket(socket_); - close_socket(socket_); - } - } - - if (!is_alive) { - if (!create_and_connect_socket(socket_, error)) { return false; } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - // TODO: refactoring - if (is_ssl()) { - auto &scli = static_cast(*this); - if (!proxy_host_.empty() && proxy_port_ != -1) { - auto success = false; - if (!scli.connect_with_proxy(socket_, req.start_time_, res, success, - error)) { - return success; - } - } - - if (!scli.initialize_ssl(socket_, error)) { return false; } - } -#endif - } - - // Mark the current socket as being in use so that it cannot be closed by - // anyone else while this request is ongoing, even though we will be - // releasing the mutex. - if (socket_requests_in_flight_ > 1) { - assert(socket_requests_are_from_thread_ == std::this_thread::get_id()); - } - socket_requests_in_flight_ += 1; - socket_requests_are_from_thread_ = std::this_thread::get_id(); - } - - for (const auto &header : default_headers_) { - if (req.headers.find(header.first) == req.headers.end()) { - req.headers.insert(header); - } - } - - auto ret = false; - auto close_connection = !keep_alive_; - - auto se = detail::scope_exit([&]() { - // Briefly lock mutex in order to mark that a request is no longer ongoing - std::lock_guard guard(socket_mutex_); - socket_requests_in_flight_ -= 1; - if (socket_requests_in_flight_ <= 0) { - assert(socket_requests_in_flight_ == 0); - socket_requests_are_from_thread_ = std::thread::id(); - } - - if (socket_should_be_closed_when_request_is_done_ || close_connection || - !ret) { - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); - } - }); - - ret = process_socket(socket_, req.start_time_, [&](Stream &strm) { - return handle_request(strm, req, res, close_connection, error); - }); - - if (!ret) { - if (error == Error::Success) { error = Error::Unknown; } - } - - return ret; -} - -inline Result ClientImpl::send(const Request &req) { - auto req2 = req; - return send_(std::move(req2)); -} - -inline Result ClientImpl::send_(Request &&req) { - auto res = detail::make_unique(); - auto error = Error::Success; - auto ret = send(req, *res, error); - return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; -} - -inline bool ClientImpl::handle_request(Stream &strm, Request &req, - Response &res, bool close_connection, - Error &error) { - if (req.path.empty()) { - error = Error::Connection; - return false; - } - - auto req_save = req; - - bool ret; - - if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { - auto req2 = req; - req2.path = "http://" + host_and_port_ + req.path; - ret = process_request(strm, req2, res, close_connection, error); - req = req2; - req.path = req_save.path; - } else { - ret = process_request(strm, req, res, close_connection, error); - } - - if (!ret) { return false; } - - if (res.get_header_value("Connection") == "close" || - (res.version == "HTTP/1.0" && res.reason != "Connection established")) { - // TODO this requires a not-entirely-obvious chain of calls to be correct - // for this to be safe. - - // This is safe to call because handle_request is only called by send_ - // which locks the request mutex during the process. It would be a bug - // to call it from a different thread since it's a thread-safety issue - // to do these things to the socket if another thread is using the socket. - std::lock_guard guard(socket_mutex_); - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); - } - - if (300 < res.status && res.status < 400 && follow_location_) { - req = req_save; - ret = redirect(req, res, error); - } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if ((res.status == StatusCode::Unauthorized_401 || - res.status == StatusCode::ProxyAuthenticationRequired_407) && - req.authorization_count_ < 5) { - auto is_proxy = res.status == StatusCode::ProxyAuthenticationRequired_407; - const auto &username = - is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; - const auto &password = - is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; - - if (!username.empty() && !password.empty()) { - std::map auth; - if (detail::parse_www_authenticate(res, auth, is_proxy)) { - Request new_req = req; - new_req.authorization_count_ += 1; - new_req.headers.erase(is_proxy ? "Proxy-Authorization" - : "Authorization"); - new_req.headers.insert(detail::make_digest_authentication_header( - req, auth, new_req.authorization_count_, detail::random_string(10), - username, password, is_proxy)); - - Response new_res; - - ret = send(new_req, new_res, error); - if (ret) { res = new_res; } - } - } - } -#endif - - return ret; -} - -inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { - if (req.redirect_count_ == 0) { - error = Error::ExceedRedirectCount; - return false; - } - - auto location = res.get_header_value("location"); - if (location.empty()) { return false; } - - const static std::regex re( - R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)"); - - std::smatch m; - if (!std::regex_match(location, m, re)) { return false; } - - auto scheme = is_ssl() ? "https" : "http"; - - auto next_scheme = m[1].str(); - auto next_host = m[2].str(); - if (next_host.empty()) { next_host = m[3].str(); } - auto port_str = m[4].str(); - auto next_path = m[5].str(); - auto next_query = m[6].str(); - - auto next_port = port_; - if (!port_str.empty()) { - next_port = std::stoi(port_str); - } else if (!next_scheme.empty()) { - next_port = next_scheme == "https" ? 443 : 80; - } - - if (next_scheme.empty()) { next_scheme = scheme; } - if (next_host.empty()) { next_host = host_; } - if (next_path.empty()) { next_path = "/"; } - - auto path = detail::decode_url(next_path, true) + next_query; - - if (next_scheme == scheme && next_host == host_ && next_port == port_) { - return detail::redirect(*this, req, res, path, location, error); - } else { - if (next_scheme == "https") { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLClient cli(next_host, next_port); - cli.copy_settings(*this); - if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } - return detail::redirect(cli, req, res, path, location, error); -#else - return false; -#endif - } else { - ClientImpl cli(next_host, next_port); - cli.copy_settings(*this); - return detail::redirect(cli, req, res, path, location, error); - } - } -} - -inline bool ClientImpl::write_content_with_provider(Stream &strm, - const Request &req, - Error &error) const { - auto is_shutting_down = []() { return false; }; - - if (req.is_chunked_content_provider_) { - // TODO: Brotli support - std::unique_ptr compressor; -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_) { - compressor = detail::make_unique(); - } else -#endif - { - compressor = detail::make_unique(); - } - - return detail::write_content_chunked(strm, req.content_provider_, - is_shutting_down, *compressor, error); - } else { - return detail::write_content(strm, req.content_provider_, 0, - req.content_length_, is_shutting_down, error); - } -} - -inline bool ClientImpl::write_request(Stream &strm, Request &req, - bool close_connection, Error &error) { - // Prepare additional headers - if (close_connection) { - if (!req.has_header("Connection")) { - req.set_header("Connection", "close"); - } - } - - if (!req.has_header("Host")) { - if (is_ssl()) { - if (port_ == 443) { - req.set_header("Host", host_); - } else { - req.set_header("Host", host_and_port_); - } - } else { - if (port_ == 80) { - req.set_header("Host", host_); - } else { - req.set_header("Host", host_and_port_); - } - } - } - - if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } - - if (!req.content_receiver) { - if (!req.has_header("Accept-Encoding")) { - std::string accept_encoding; -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - accept_encoding = "br"; -#endif -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (!accept_encoding.empty()) { accept_encoding += ", "; } - accept_encoding += "gzip, deflate"; -#endif - req.set_header("Accept-Encoding", accept_encoding); - } - -#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT - if (!req.has_header("User-Agent")) { - auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; - req.set_header("User-Agent", agent); - } -#endif - }; - - if (req.body.empty()) { - if (req.content_provider_) { - if (!req.is_chunked_content_provider_) { - if (!req.has_header("Content-Length")) { - auto length = std::to_string(req.content_length_); - req.set_header("Content-Length", length); - } - } - } else { - if (req.method == "POST" || req.method == "PUT" || - req.method == "PATCH") { - req.set_header("Content-Length", "0"); - } - } - } else { - if (!req.has_header("Content-Type")) { - req.set_header("Content-Type", "text/plain"); - } - - if (!req.has_header("Content-Length")) { - auto length = std::to_string(req.body.size()); - req.set_header("Content-Length", length); - } - } - - if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { - if (!req.has_header("Authorization")) { - req.headers.insert(make_basic_authentication_header( - basic_auth_username_, basic_auth_password_, false)); - } - } - - if (!proxy_basic_auth_username_.empty() && - !proxy_basic_auth_password_.empty()) { - if (!req.has_header("Proxy-Authorization")) { - req.headers.insert(make_basic_authentication_header( - proxy_basic_auth_username_, proxy_basic_auth_password_, true)); - } - } - - if (!bearer_token_auth_token_.empty()) { - if (!req.has_header("Authorization")) { - req.headers.insert(make_bearer_token_authentication_header( - bearer_token_auth_token_, false)); - } - } - - if (!proxy_bearer_token_auth_token_.empty()) { - if (!req.has_header("Proxy-Authorization")) { - req.headers.insert(make_bearer_token_authentication_header( - proxy_bearer_token_auth_token_, true)); - } - } - - // Request line and headers - { - detail::BufferStream bstrm; - - const auto &path_with_query = - req.params.empty() ? req.path - : append_query_params(req.path, req.params); - - const auto &path = - url_encode_ ? detail::encode_url(path_with_query) : path_with_query; - - detail::write_request_line(bstrm, req.method, path); - - header_writer_(bstrm, req.headers); - - // Flush buffer - auto &data = bstrm.get_buffer(); - if (!detail::write_data(strm, data.data(), data.size())) { - error = Error::Write; - return false; - } - } - - // Body - if (req.body.empty()) { - return write_content_with_provider(strm, req, error); - } - - if (!detail::write_data(strm, req.body.data(), req.body.size())) { - error = Error::Write; - return false; - } - - return true; -} - -inline std::unique_ptr ClientImpl::send_with_content_provider( - Request &req, const char *body, size_t content_length, - ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error) { - if (!content_type.empty()) { req.set_header("Content-Type", content_type); } - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_) { req.set_header("Content-Encoding", "gzip"); } -#endif - -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_ && !content_provider_without_length) { - // TODO: Brotli support - detail::gzip_compressor compressor; - - if (content_provider) { - auto ok = true; - size_t offset = 0; - DataSink data_sink; - - data_sink.write = [&](const char *data, size_t data_len) -> bool { - if (ok) { - auto last = offset + data_len == content_length; - - auto ret = compressor.compress( - data, data_len, last, - [&](const char *compressed_data, size_t compressed_data_len) { - req.body.append(compressed_data, compressed_data_len); - return true; - }); - - if (ret) { - offset += data_len; - } else { - ok = false; - } - } - return ok; - }; - - while (ok && offset < content_length) { - if (!content_provider(offset, content_length - offset, data_sink)) { - error = Error::Canceled; - return nullptr; - } - } - } else { - if (!compressor.compress(body, content_length, true, - [&](const char *data, size_t data_len) { - req.body.append(data, data_len); - return true; - })) { - error = Error::Compression; - return nullptr; - } - } - } else -#endif - { - if (content_provider) { - req.content_length_ = content_length; - req.content_provider_ = std::move(content_provider); - req.is_chunked_content_provider_ = false; - } else if (content_provider_without_length) { - req.content_length_ = 0; - req.content_provider_ = detail::ContentProviderAdapter( - std::move(content_provider_without_length)); - req.is_chunked_content_provider_ = true; - req.set_header("Transfer-Encoding", "chunked"); - } else { - req.body.assign(body, content_length); - } - } - - auto res = detail::make_unique(); - return send(req, *res, error) ? std::move(res) : nullptr; -} - -inline Result ClientImpl::send_with_content_provider( - const std::string &method, const std::string &path, const Headers &headers, - const char *body, size_t content_length, ContentProvider content_provider, - ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Progress progress) { - Request req; - req.method = method; - req.headers = headers; - req.path = path; - req.progress = progress; - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - auto error = Error::Success; - - auto res = send_with_content_provider( - req, body, content_length, std::move(content_provider), - std::move(content_provider_without_length), content_type, error); - - return Result{std::move(res), error, std::move(req.headers)}; -} - -inline std::string -ClientImpl::adjust_host_string(const std::string &host) const { - if (host.find(':') != std::string::npos) { return "[" + host + "]"; } - return host; -} - -inline bool ClientImpl::process_request(Stream &strm, Request &req, - Response &res, bool close_connection, - Error &error) { - // Send request - if (!write_request(strm, req, close_connection, error)) { return false; } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl()) { - auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; - if (!is_proxy_enabled) { - if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { - error = Error::SSLPeerCouldBeClosed_; - return false; - } - } - } -#endif - - // Receive response and headers - if (!read_response_line(strm, req, res) || - !detail::read_headers(strm, res.headers)) { - error = Error::Read; - return false; - } - - // Body - if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && - req.method != "CONNECT") { - auto redirect = 300 < res.status && res.status < 400 && - res.status != StatusCode::NotModified_304 && - follow_location_; - - if (req.response_handler && !redirect) { - if (!req.response_handler(res)) { - error = Error::Canceled; - return false; - } - } - - auto out = - req.content_receiver - ? static_cast( - [&](const char *buf, size_t n, uint64_t off, uint64_t len) { - if (redirect) { return true; } - auto ret = req.content_receiver(buf, n, off, len); - if (!ret) { error = Error::Canceled; } - return ret; - }) - : static_cast( - [&](const char *buf, size_t n, uint64_t /*off*/, - uint64_t /*len*/) { - assert(res.body.size() + n <= res.body.max_size()); - res.body.append(buf, n); - return true; - }); - - auto progress = [&](uint64_t current, uint64_t total) { - if (!req.progress || redirect) { return true; } - auto ret = req.progress(current, total); - if (!ret) { error = Error::Canceled; } - return ret; - }; - - if (res.has_header("Content-Length")) { - if (!req.content_receiver) { - auto len = res.get_header_value_u64("Content-Length"); - if (len > res.body.max_size()) { - error = Error::Read; - return false; - } - res.body.reserve(static_cast(len)); - } - } - - if (res.status != StatusCode::NotModified_304) { - int dummy_status; - if (!detail::read_content(strm, res, (std::numeric_limits::max)(), - dummy_status, std::move(progress), - std::move(out), decompress_)) { - if (error != Error::Canceled) { error = Error::Read; } - return false; - } - } - } - - // Log - if (logger_) { logger_(req, res); } - - return true; -} - -inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( - const std::string &boundary, const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) const { - size_t cur_item = 0; - size_t cur_start = 0; - // cur_item and cur_start are copied to within the std::function and maintain - // state between successive calls - return [&, cur_item, cur_start](size_t offset, - DataSink &sink) mutable -> bool { - if (!offset && !items.empty()) { - sink.os << detail::serialize_multipart_formdata(items, boundary, false); - return true; - } else if (cur_item < provider_items.size()) { - if (!cur_start) { - const auto &begin = detail::serialize_multipart_formdata_item_begin( - provider_items[cur_item], boundary); - offset += begin.size(); - cur_start = offset; - sink.os << begin; - } - - DataSink cur_sink; - auto has_data = true; - cur_sink.write = sink.write; - cur_sink.done = [&]() { has_data = false; }; - - if (!provider_items[cur_item].provider(offset - cur_start, cur_sink)) { - return false; - } - - if (!has_data) { - sink.os << detail::serialize_multipart_formdata_item_end(); - cur_item++; - cur_start = 0; - } - return true; - } else { - sink.os << detail::serialize_multipart_formdata_finish(boundary); - sink.done(); - return true; - } - }; -} - -inline bool ClientImpl::process_socket( - const Socket &socket, - std::chrono::time_point start_time, - std::function callback) { - return detail::process_client_socket( - socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, max_timeout_msec_, start_time, - std::move(callback)); -} - -inline bool ClientImpl::is_ssl() const { return false; } - -inline Result ClientImpl::Get(const std::string &path) { - return Get(path, Headers(), Progress()); -} - -inline Result ClientImpl::Get(const std::string &path, Progress progress) { - return Get(path, Headers(), std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { - return Get(path, headers, Progress()); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - Progress progress) { - Request req; - req.method = "GET"; - req.path = path; - req.headers = headers; - req.progress = std::move(progress); - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - return send_(std::move(req)); -} - -inline Result ClientImpl::Get(const std::string &path, - ContentReceiver content_receiver) { - return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, Headers(), nullptr, std::move(content_receiver), - std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return Get(path, headers, nullptr, std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, headers, nullptr, std::move(content_receiver), - std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, Headers(), std::move(response_handler), - std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return Get(path, headers, std::move(response_handler), - std::move(content_receiver), nullptr); -} - -inline Result ClientImpl::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, Headers(), std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { - Request req; - req.method = "GET"; - req.path = path; - req.headers = headers; - req.response_handler = std::move(response_handler); - req.content_receiver = - [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { - return content_receiver(data, data_length); - }; - req.progress = std::move(progress); - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - return send_(std::move(req)); -} - -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { - if (params.empty()) { return Get(path, headers); } - - std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ContentReceiver content_receiver, - Progress progress) { - return Get(path, params, headers, nullptr, std::move(content_receiver), - std::move(progress)); -} - -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { - if (params.empty()) { - return Get(path, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); - } - - std::string path_with_query = append_query_params(path, params); - return Get(path_with_query, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} - -inline Result ClientImpl::Head(const std::string &path) { - return Head(path, Headers()); -} - -inline Result ClientImpl::Head(const std::string &path, - const Headers &headers) { - Request req; - req.method = "HEAD"; - req.headers = headers; - req.path = path; - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - return send_(std::move(req)); -} - -inline Result ClientImpl::Post(const std::string &path) { - return Post(path, std::string(), std::string()); -} - -inline Result ClientImpl::Post(const std::string &path, - const Headers &headers) { - return Post(path, headers, nullptr, 0, std::string()); -} - -inline Result ClientImpl::Post(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Post(path, Headers(), body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); -} - -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return Post(path, Headers(), body, content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return Post(path, Headers(), body, content_type, progress); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - -inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { - return Post(path, Headers(), params); -} - -inline Result ClientImpl::Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Post(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Post(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Post(path, headers, query, "application/x-www-form-urlencoded"); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - auto query = detail::params_to_query_str(params); - return Post(path, headers, query, "application/x-www-form-urlencoded", - progress); -} - -inline Result ClientImpl::Post(const std::string &path, - const MultipartFormDataItems &items) { - return Post(path, Headers(), items); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type); -} - -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - if (!detail::is_multipart_boundary_chars_valid(boundary)) { - return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; - } - - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Post(path, headers, body, content_type); -} - -inline Result -ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( - "POST", path, headers, nullptr, 0, nullptr, - get_multipart_content_provider(boundary, items, provider_items), - content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path) { - return Put(path, std::string(), std::string()); -} - -inline Result ClientImpl::Put(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Put(path, Headers(), body, content_length, content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); -} - -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return Put(path, Headers(), body, content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return Put(path, Headers(), body, content_type, progress); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - -inline Result ClientImpl::Put(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Put(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Put(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { - return Put(path, Headers(), params); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - auto query = detail::params_to_query_str(params); - return Put(path, headers, query, "application/x-www-form-urlencoded"); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - auto query = detail::params_to_query_str(params); - return Put(path, headers, query, "application/x-www-form-urlencoded", - progress); -} - -inline Result ClientImpl::Put(const std::string &path, - const MultipartFormDataItems &items) { - return Put(path, Headers(), items); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Put(path, headers, body, content_type); -} - -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - if (!detail::is_multipart_boundary_chars_valid(boundary)) { - return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; - } - - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - const auto &body = detail::serialize_multipart_formdata(items, boundary); - return Put(path, headers, body, content_type); -} - -inline Result -ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - const auto &boundary = detail::make_multipart_data_boundary(); - const auto &content_type = - detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( - "PUT", path, headers, nullptr, 0, nullptr, - get_multipart_content_provider(boundary, items, provider_items), - content_type, nullptr); -} -inline Result ClientImpl::Patch(const std::string &path) { - return Patch(path, std::string(), std::string()); -} - -inline Result ClientImpl::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Patch(path, Headers(), body, content_length, content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return Patch(path, Headers(), body, content_length, content_type, progress); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return Patch(path, headers, body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PATCH", path, headers, body, - content_length, nullptr, nullptr, - content_type, progress); -} - -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Patch(path, Headers(), body, content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type, - Progress progress) { - return Patch(path, Headers(), body, content_type, progress); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Patch(path, headers, body, content_type, nullptr); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return send_with_content_provider("PATCH", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); -} - -inline Result ClientImpl::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return Patch(path, Headers(), content_length, std::move(content_provider), - content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return Patch(path, Headers(), std::move(content_provider), content_type); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return send_with_content_provider("PATCH", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, nullptr); -} - -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - nullptr); -} - -inline Result ClientImpl::Delete(const std::string &path) { - return Delete(path, Headers(), std::string(), std::string()); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers) { - return Delete(path, headers, std::string(), std::string()); -} - -inline Result ClientImpl::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, Headers(), body, content_length, content_type); -} - -inline Result ClientImpl::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return Delete(path, Headers(), body, content_length, content_type, progress); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type) { - return Delete(path, headers, body, content_length, content_type, nullptr); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - Request req; - req.method = "DELETE"; - req.headers = headers; - req.path = path; - req.progress = progress; - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - if (!content_type.empty()) { req.set_header("Content-Type", content_type); } - req.body.assign(body, content_length); - - return send_(std::move(req)); -} - -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type) { - return Delete(path, Headers(), body.data(), body.size(), content_type); -} - -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type, - Progress progress) { - return Delete(path, Headers(), body.data(), body.size(), content_type, - progress); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type) { - return Delete(path, headers, body.data(), body.size(), content_type); -} - -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return Delete(path, headers, body.data(), body.size(), content_type, - progress); -} - -inline Result ClientImpl::Options(const std::string &path) { - return Options(path, Headers()); -} - -inline Result ClientImpl::Options(const std::string &path, - const Headers &headers) { - Request req; - req.method = "OPTIONS"; - req.headers = headers; - req.path = path; - if (max_timeout_msec_ > 0) { - req.start_time_ = std::chrono::steady_clock::now(); - } - - return send_(std::move(req)); -} - -inline void ClientImpl::stop() { - std::lock_guard guard(socket_mutex_); - - // If there is anything ongoing right now, the ONLY thread-safe thing we can - // do is to shutdown_socket, so that threads using this socket suddenly - // discover they can't read/write any more and error out. Everything else - // (closing the socket, shutting ssl down) is unsafe because these actions are - // not thread-safe. - if (socket_requests_in_flight_ > 0) { - shutdown_socket(socket_); - - // Aside from that, we set a flag for the socket to be closed when we're - // done. - socket_should_be_closed_when_request_is_done_ = true; - return; - } - - // Otherwise, still holding the mutex, we can shut everything down ourselves - shutdown_ssl(socket_, true); - shutdown_socket(socket_); - close_socket(socket_); -} - -inline std::string ClientImpl::host() const { return host_; } - -inline int ClientImpl::port() const { return port_; } - -inline size_t ClientImpl::is_socket_open() const { - std::lock_guard guard(socket_mutex_); - return socket_.is_open(); -} - -inline socket_t ClientImpl::socket() const { return socket_.sock; } - -inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { - connection_timeout_sec_ = sec; - connection_timeout_usec_ = usec; -} - -inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { - read_timeout_sec_ = sec; - read_timeout_usec_ = usec; -} - -inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { - write_timeout_sec_ = sec; - write_timeout_usec_ = usec; -} - -inline void ClientImpl::set_max_timeout(time_t msec) { - max_timeout_msec_ = msec; -} - -inline void ClientImpl::set_basic_auth(const std::string &username, - const std::string &password) { - basic_auth_username_ = username; - basic_auth_password_ = password; -} - -inline void ClientImpl::set_bearer_token_auth(const std::string &token) { - bearer_token_auth_token_ = token; -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_digest_auth(const std::string &username, - const std::string &password) { - digest_auth_username_ = username; - digest_auth_password_ = password; -} -#endif - -inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } - -inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } - -inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } - -inline void -ClientImpl::set_hostname_addr_map(std::map addr_map) { - addr_map_ = std::move(addr_map); -} - -inline void ClientImpl::set_default_headers(Headers headers) { - default_headers_ = std::move(headers); -} - -inline void ClientImpl::set_header_writer( - std::function const &writer) { - header_writer_ = writer; -} - -inline void ClientImpl::set_address_family(int family) { - address_family_ = family; -} - -inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } - -inline void ClientImpl::set_ipv6_v6only(bool on) { ipv6_v6only_ = on; } - -inline void ClientImpl::set_socket_options(SocketOptions socket_options) { - socket_options_ = std::move(socket_options); -} - -inline void ClientImpl::set_compress(bool on) { compress_ = on; } - -inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } - -inline void ClientImpl::set_interface(const std::string &intf) { - interface_ = intf; -} - -inline void ClientImpl::set_proxy(const std::string &host, int port) { - proxy_host_ = host; - proxy_port_ = port; -} - -inline void ClientImpl::set_proxy_basic_auth(const std::string &username, - const std::string &password) { - proxy_basic_auth_username_ = username; - proxy_basic_auth_password_ = password; -} - -inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { - proxy_bearer_token_auth_token_ = token; -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - proxy_digest_auth_username_ = username; - proxy_digest_auth_password_ = password; -} - -inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { - ca_cert_file_path_ = ca_cert_file_path; - ca_cert_dir_path_ = ca_cert_dir_path; -} - -inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (ca_cert_store && ca_cert_store != ca_cert_store_) { - ca_cert_store_ = ca_cert_store; - } -} - -inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, - std::size_t size) const { - auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); - auto se = detail::scope_exit([&] { BIO_free_all(mem); }); - if (!mem) { return nullptr; } - - auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); - if (!inf) { return nullptr; } - - auto cts = X509_STORE_new(); - if (cts) { - for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { - auto itmp = sk_X509_INFO_value(inf, i); - if (!itmp) { continue; } - - if (itmp->x509) { X509_STORE_add_cert(cts, itmp->x509); } - if (itmp->crl) { X509_STORE_add_crl(cts, itmp->crl); } - } - } - - sk_X509_INFO_pop_free(inf, X509_INFO_free); - return cts; -} - -inline void ClientImpl::enable_server_certificate_verification(bool enabled) { - server_certificate_verification_ = enabled; -} - -inline void ClientImpl::enable_server_hostname_verification(bool enabled) { - server_hostname_verification_ = enabled; -} - -inline void ClientImpl::set_server_certificate_verifier( - std::function verifier) { - server_certificate_verifier_ = verifier; -} -#endif - -inline void ClientImpl::set_logger(Logger logger) { - logger_ = std::move(logger); -} - -/* - * SSL Implementation - */ -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -namespace detail { - -template -inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, - U SSL_connect_or_accept, V setup) { - SSL *ssl = nullptr; - { - std::lock_guard guard(ctx_mutex); - ssl = SSL_new(ctx); - } - - if (ssl) { - set_nonblocking(sock, true); - auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); - BIO_set_nbio(bio, 1); - SSL_set_bio(ssl, bio, bio); - - if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { - SSL_shutdown(ssl); - { - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); - } - set_nonblocking(sock, false); - return nullptr; - } - BIO_set_nbio(bio, 0); - set_nonblocking(sock, false); - } - - return ssl; -} - -inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, - bool shutdown_gracefully) { - // sometimes we may want to skip this to try to avoid SIGPIPE if we know - // the remote has closed the network connection - // Note that it is not always possible to avoid SIGPIPE, this is merely a - // best-efforts. - if (shutdown_gracefully) { -#ifdef _WIN32 - (void)(sock); - SSL_shutdown(ssl); -#else - timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, - reinterpret_cast(&tv), sizeof(tv)); - - auto ret = SSL_shutdown(ssl); - while (ret == 0) { - std::this_thread::sleep_for(std::chrono::milliseconds{100}); - ret = SSL_shutdown(ssl); - } -#endif - } - - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); -} - -template -bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, - U ssl_connect_or_accept, - time_t timeout_sec, - time_t timeout_usec) { - auto res = 0; - while ((res = ssl_connect_or_accept(ssl)) != 1) { - auto err = SSL_get_error(ssl, res); - switch (err) { - case SSL_ERROR_WANT_READ: - if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } - break; - case SSL_ERROR_WANT_WRITE: - if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } - break; - default: break; - } - return false; - } - return true; -} - -template -inline bool process_server_socket_ssl( - const std::atomic &svr_sock, SSL *ssl, socket_t sock, - size_t keep_alive_max_count, time_t keep_alive_timeout_sec, - time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - return process_server_socket_core( - svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm, close_connection, connection_closed); - }); -} - -template -inline bool process_client_socket_ssl( - SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time, T callback) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec, - max_timeout_msec, start_time); - return callback(strm); -} - -class SSLInit { -public: - SSLInit() { - OPENSSL_init_ssl( - OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); - } -}; - -// SSL socket stream implementation -inline SSLSocketStream::SSLSocketStream( - socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time) - : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec), - write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec), - max_timeout_msec_(max_timeout_msec), start_time(start_time) { - SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); -} - -inline SSLSocketStream::~SSLSocketStream() = default; - -inline bool SSLSocketStream::is_readable() const { - if (max_timeout_msec_ <= 0) { - return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; - } - - time_t read_timeout_sec; - time_t read_timeout_usec; - calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, - read_timeout_usec_, read_timeout_sec, read_timeout_usec); - - return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; -} - -inline bool SSLSocketStream::is_writable() const { - return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_); -} - -inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { - if (SSL_pending(ssl_) > 0) { - return SSL_read(ssl_, ptr, static_cast(size)); - } else if (is_readable()) { - auto ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret < 0) { - auto err = SSL_get_error(ssl_, ret); - auto n = 1000; -#ifdef _WIN32 - while (--n >= 0 && (err == SSL_ERROR_WANT_READ || - (err == SSL_ERROR_SYSCALL && - WSAGetLastError() == WSAETIMEDOUT))) { -#else - while (--n >= 0 && err == SSL_ERROR_WANT_READ) { -#endif - if (SSL_pending(ssl_) > 0) { - return SSL_read(ssl_, ptr, static_cast(size)); - } else if (is_readable()) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); - ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret >= 0) { return ret; } - err = SSL_get_error(ssl_, ret); - } else { - return -1; - } - } - } - return ret; - } else { - return -1; - } -} - -inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { - if (is_writable()) { - auto handle_size = static_cast( - std::min(size, (std::numeric_limits::max)())); - - auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret < 0) { - auto err = SSL_get_error(ssl_, ret); - auto n = 1000; -#ifdef _WIN32 - while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || - (err == SSL_ERROR_SYSCALL && - WSAGetLastError() == WSAETIMEDOUT))) { -#else - while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { -#endif - if (is_writable()) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); - ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret >= 0) { return ret; } - err = SSL_get_error(ssl_, ret); - } else { - return -1; - } - } - } - return ret; - } - return -1; -} - -inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { - detail::get_remote_ip_and_port(sock_, ip, port); -} - -inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, - int &port) const { - detail::get_local_ip_and_port(sock_, ip, port); -} - -inline socket_t SSLSocketStream::socket() const { return sock_; } - -inline time_t SSLSocketStream::duration() const { - return std::chrono::duration_cast( - std::chrono::steady_clock::now() - start_time) - .count(); -} - -static SSLInit sslinit_; - -} // namespace detail - -// SSL HTTP server implementation -inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path, - const char *client_ca_cert_dir_path, - const char *private_key_password) { - ctx_ = SSL_CTX_new(TLS_server_method()); - - if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - if (private_key_password != nullptr && (private_key_password[0] != '\0')) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, - reinterpret_cast(const_cast(private_key_password))); - } - - if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || - SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != - 1 || - SSL_CTX_check_private_key(ctx_) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { - SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, - client_ca_cert_dir_path); - - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); - } - } -} - -inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { - ctx_ = SSL_CTX_new(TLS_server_method()); - - if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - if (SSL_CTX_use_certificate(ctx_, cert) != 1 || - SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } else if (client_ca_cert_store) { - SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); - - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); - } - } -} - -inline SSLServer::SSLServer( - const std::function &setup_ssl_ctx_callback) { - ctx_ = SSL_CTX_new(TLS_method()); - if (ctx_) { - if (!setup_ssl_ctx_callback(*ctx_)) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } -} - -inline SSLServer::~SSLServer() { - if (ctx_) { SSL_CTX_free(ctx_); } -} - -inline bool SSLServer::is_valid() const { return ctx_; } - -inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } - -inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { - - std::lock_guard guard(ctx_mutex_); - - SSL_CTX_use_certificate(ctx_, cert); - SSL_CTX_use_PrivateKey(ctx_, private_key); - - if (client_ca_cert_store != nullptr) { - SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); - } -} - -inline bool SSLServer::process_and_close_socket(socket_t sock) { - auto ssl = detail::ssl_new( - sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { - return detail::ssl_connect_or_accept_nonblocking( - sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); - }, - [](SSL * /*ssl2*/) { return true; }); - - auto ret = false; - if (ssl) { - std::string remote_addr; - int remote_port = 0; - detail::get_remote_ip_and_port(sock, remote_addr, remote_port); - - std::string local_addr; - int local_port = 0; - detail::get_local_ip_and_port(sock, local_addr, local_port); - - ret = detail::process_server_socket_ssl( - svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, - [&](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, remote_addr, remote_port, local_addr, - local_port, close_connection, - connection_closed, - [&](Request &req) { req.ssl = ssl; }); - }); - - // Shutdown gracefully if the result seemed successful, non-gracefully if - // the connection appeared to be closed. - const bool shutdown_gracefully = ret; - detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); - } - - detail::shutdown_socket(sock); - detail::close_socket(sock); - return ret; -} - -// SSL HTTP client implementation -inline SSLClient::SSLClient(const std::string &host) - : SSLClient(host, 443, std::string(), std::string()) {} - -inline SSLClient::SSLClient(const std::string &host, int port) - : SSLClient(host, port, std::string(), std::string()) {} - -inline SSLClient::SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path, - const std::string &private_key_password) - : ClientImpl(host, port, client_cert_path, client_key_path) { - ctx_ = SSL_CTX_new(TLS_client_method()); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { - host_components_.emplace_back(b, e); - }); - - if (!client_cert_path.empty() && !client_key_path.empty()) { - if (!private_key_password.empty()) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, reinterpret_cast( - const_cast(private_key_password.c_str()))); - } - - if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), - SSL_FILETYPE_PEM) != 1 || - SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), - SSL_FILETYPE_PEM) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } -} - -inline SSLClient::SSLClient(const std::string &host, int port, - X509 *client_cert, EVP_PKEY *client_key, - const std::string &private_key_password) - : ClientImpl(host, port) { - ctx_ = SSL_CTX_new(TLS_client_method()); - - detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { - host_components_.emplace_back(b, e); - }); - - if (client_cert != nullptr && client_key != nullptr) { - if (!private_key_password.empty()) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, reinterpret_cast( - const_cast(private_key_password.c_str()))); - } - - if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || - SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } -} - -inline SSLClient::~SSLClient() { - if (ctx_) { SSL_CTX_free(ctx_); } - // Make sure to shut down SSL since shutdown_ssl will resolve to the - // base function rather than the derived function once we get to the - // base class destructor, and won't free the SSL (causing a leak). - shutdown_ssl_impl(socket_, true); -} - -inline bool SSLClient::is_valid() const { return ctx_; } - -inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (ca_cert_store) { - if (ctx_) { - if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { - // Free memory allocated for old cert and use new store `ca_cert_store` - SSL_CTX_set_cert_store(ctx_, ca_cert_store); - } - } else { - X509_STORE_free(ca_cert_store); - } - } -} - -inline void SSLClient::load_ca_cert_store(const char *ca_cert, - std::size_t size) { - set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); -} - -inline long SSLClient::get_openssl_verify_result() const { - return verify_result_; -} - -inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } - -inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { - return is_valid() && ClientImpl::create_and_connect_socket(socket, error); -} - -// Assumes that socket_mutex_ is locked and that there are no requests in flight -inline bool SSLClient::connect_with_proxy( - Socket &socket, - std::chrono::time_point start_time, - Response &res, bool &success, Error &error) { - success = true; - Response proxy_res; - if (!detail::process_client_socket( - socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, - start_time, [&](Stream &strm) { - Request req2; - req2.method = "CONNECT"; - req2.path = host_and_port_; - if (max_timeout_msec_ > 0) { - req2.start_time_ = std::chrono::steady_clock::now(); - } - return process_request(strm, req2, proxy_res, false, error); - })) { - // Thread-safe to close everything because we are assuming there are no - // requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - success = false; - return false; - } - - if (proxy_res.status == StatusCode::ProxyAuthenticationRequired_407) { - if (!proxy_digest_auth_username_.empty() && - !proxy_digest_auth_password_.empty()) { - std::map auth; - if (detail::parse_www_authenticate(proxy_res, auth, true)) { - proxy_res = Response(); - if (!detail::process_client_socket( - socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, - start_time, [&](Stream &strm) { - Request req3; - req3.method = "CONNECT"; - req3.path = host_and_port_; - req3.headers.insert(detail::make_digest_authentication_header( - req3, auth, 1, detail::random_string(10), - proxy_digest_auth_username_, proxy_digest_auth_password_, - true)); - if (max_timeout_msec_ > 0) { - req3.start_time_ = std::chrono::steady_clock::now(); - } - return process_request(strm, req3, proxy_res, false, error); - })) { - // Thread-safe to close everything because we are assuming there are - // no requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - success = false; - return false; - } - } - } - } - - // If status code is not 200, proxy request is failed. - // Set error to ProxyConnection and return proxy response - // as the response of the request - if (proxy_res.status != StatusCode::OK_200) { - error = Error::ProxyConnection; - res = std::move(proxy_res); - // Thread-safe to close everything because we are assuming there are - // no requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - return false; - } - - return true; -} - -inline bool SSLClient::load_certs() { - auto ret = true; - - std::call_once(initialize_cert_, [&]() { - std::lock_guard guard(ctx_mutex_); - if (!ca_cert_file_path_.empty()) { - if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), - nullptr)) { - ret = false; - } - } else if (!ca_cert_dir_path_.empty()) { - if (!SSL_CTX_load_verify_locations(ctx_, nullptr, - ca_cert_dir_path_.c_str())) { - ret = false; - } - } else { - auto loaded = false; -#ifdef _WIN32 - loaded = - detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && defined(__APPLE__) -#if TARGET_OS_OSX - loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); -#endif // TARGET_OS_OSX -#endif // _WIN32 - if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } - } - }); - - return ret; -} - -inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { - auto ssl = detail::ssl_new( - socket.sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { - if (server_certificate_verification_) { - if (!load_certs()) { - error = Error::SSLLoadingCerts; - return false; - } - SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); - } - - if (!detail::ssl_connect_or_accept_nonblocking( - socket.sock, ssl2, SSL_connect, connection_timeout_sec_, - connection_timeout_usec_)) { - error = Error::SSLConnection; - return false; - } - - if (server_certificate_verification_) { - if (server_certificate_verifier_) { - if (!server_certificate_verifier_(ssl2)) { - error = Error::SSLServerVerification; - return false; - } - } else { - verify_result_ = SSL_get_verify_result(ssl2); - - if (verify_result_ != X509_V_OK) { - error = Error::SSLServerVerification; - return false; - } - - auto server_cert = SSL_get1_peer_certificate(ssl2); - auto se = detail::scope_exit([&] { X509_free(server_cert); }); - - if (server_cert == nullptr) { - error = Error::SSLServerVerification; - return false; - } - - if (server_hostname_verification_) { - if (!verify_host(server_cert)) { - error = Error::SSLServerHostnameVerification; - return false; - } - } - } - } - - return true; - }, - [&](SSL *ssl2) { -#if defined(OPENSSL_IS_BORINGSSL) - SSL_set_tlsext_host_name(ssl2, host_.c_str()); -#else - // NOTE: Direct call instead of using the OpenSSL macro to suppress - // -Wold-style-cast warning - SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, - static_cast(const_cast(host_.c_str()))); -#endif - return true; - }); - - if (ssl) { - socket.ssl = ssl; - return true; - } - - shutdown_socket(socket); - close_socket(socket); - return false; -} - -inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { - shutdown_ssl_impl(socket, shutdown_gracefully); -} - -inline void SSLClient::shutdown_ssl_impl(Socket &socket, - bool shutdown_gracefully) { - if (socket.sock == INVALID_SOCKET) { - assert(socket.ssl == nullptr); - return; - } - if (socket.ssl) { - detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, - shutdown_gracefully); - socket.ssl = nullptr; - } - assert(socket.ssl == nullptr); -} - -inline bool SSLClient::process_socket( - const Socket &socket, - std::chrono::time_point start_time, - std::function callback) { - assert(socket.ssl); - return detail::process_client_socket_ssl( - socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, - std::move(callback)); -} - -inline bool SSLClient::is_ssl() const { return true; } - -inline bool SSLClient::verify_host(X509 *server_cert) const { - /* Quote from RFC2818 section 3.1 "Server Identity" - - If a subjectAltName extension of type dNSName is present, that MUST - be used as the identity. Otherwise, the (most specific) Common Name - field in the Subject field of the certificate MUST be used. Although - the use of the Common Name is existing practice, it is deprecated and - Certification Authorities are encouraged to use the dNSName instead. - - Matching is performed using the matching rules specified by - [RFC2459]. If more than one identity of a given type is present in - the certificate (e.g., more than one dNSName name, a match in any one - of the set is considered acceptable.) Names may contain the wildcard - character * which is considered to match any single domain name - component or component fragment. E.g., *.a.com matches foo.a.com but - not bar.foo.a.com. f*.com matches foo.com but not bar.com. - - In some cases, the URI is specified as an IP address rather than a - hostname. In this case, the iPAddress subjectAltName must be present - in the certificate and must exactly match the IP in the URI. - - */ - return verify_host_with_subject_alt_name(server_cert) || - verify_host_with_common_name(server_cert); -} - -inline bool -SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { - auto ret = false; - - auto type = GEN_DNS; - - struct in6_addr addr6{}; - struct in_addr addr{}; - size_t addr_len = 0; - -#ifndef __MINGW32__ - if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { - type = GEN_IPADD; - addr_len = sizeof(struct in6_addr); - } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { - type = GEN_IPADD; - addr_len = sizeof(struct in_addr); - } -#endif - - auto alt_names = static_cast( - X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); - - if (alt_names) { - auto dsn_matched = false; - auto ip_matched = false; - - auto count = sk_GENERAL_NAME_num(alt_names); - - for (decltype(count) i = 0; i < count && !dsn_matched; i++) { - auto val = sk_GENERAL_NAME_value(alt_names, i); - if (val->type == type) { - auto name = - reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); - auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); - - switch (type) { - case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; - - case GEN_IPADD: - if (!memcmp(&addr6, name, addr_len) || - !memcmp(&addr, name, addr_len)) { - ip_matched = true; - } - break; - } - } - } - - if (dsn_matched || ip_matched) { ret = true; } - } - - GENERAL_NAMES_free(const_cast( - reinterpret_cast(alt_names))); - return ret; -} - -inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { - const auto subject_name = X509_get_subject_name(server_cert); - - if (subject_name != nullptr) { - char name[BUFSIZ]; - auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, - name, sizeof(name)); - - if (name_len != -1) { - return check_host_name(name, static_cast(name_len)); - } - } - - return false; -} - -inline bool SSLClient::check_host_name(const char *pattern, - size_t pattern_len) const { - if (host_.size() == pattern_len && host_ == pattern) { return true; } - - // Wildcard match - // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 - std::vector pattern_components; - detail::split(&pattern[0], &pattern[pattern_len], '.', - [&](const char *b, const char *e) { - pattern_components.emplace_back(b, e); - }); - - if (host_components_.size() != pattern_components.size()) { return false; } - - auto itr = pattern_components.begin(); - for (const auto &h : host_components_) { - auto &p = *itr; - if (p != h && p != "*") { - auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && - !p.compare(0, p.size() - 1, h)); - if (!partial_match) { return false; } - } - ++itr; - } - - return true; -} -#endif - -// Universal client implementation -inline Client::Client(const std::string &scheme_host_port) - : Client(scheme_host_port, std::string(), std::string()) {} - -inline Client::Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path) { - const static std::regex re( - R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); - - std::smatch m; - if (std::regex_match(scheme_host_port, m, re)) { - auto scheme = m[1].str(); - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (!scheme.empty() && (scheme != "http" && scheme != "https")) { -#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; - } - - auto is_ssl = scheme == "https"; - - auto host = m[2].str(); - if (host.empty()) { host = m[3].str(); } - - auto port_str = m[4].str(); - auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); - - if (is_ssl) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - cli_ = detail::make_unique(host, port, client_cert_path, - client_key_path); - is_ssl_ = is_ssl; -#endif - } else { - cli_ = detail::make_unique(host, port, client_cert_path, - client_key_path); - } - } else { - // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) - // if port param below changes. - cli_ = detail::make_unique(scheme_host_port, 80, - client_cert_path, client_key_path); - } -} // namespace detail - -inline Client::Client(const std::string &host, int port) - : cli_(detail::make_unique(host, port)) {} - -inline Client::Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) - : cli_(detail::make_unique(host, port, client_cert_path, - client_key_path)) {} - -inline Client::~Client() = default; - -inline bool Client::is_valid() const { - return cli_ != nullptr && cli_->is_valid(); -} - -inline Result Client::Get(const std::string &path) { return cli_->Get(path); } -inline Result Client::Get(const std::string &path, const Headers &headers) { - return cli_->Get(path, headers); -} -inline Result Client::Get(const std::string &path, Progress progress) { - return cli_->Get(path, std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - Progress progress) { - return cli_->Get(path, headers, std::move(progress)); -} -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, headers, std::move(content_receiver), - std::move(progress)); -} -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver)); -} -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { - return cli_->Get(path, params, headers, std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, params, headers, std::move(content_receiver), - std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, Progress progress) { - return cli_->Get(path, params, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} - -inline Result Client::Head(const std::string &path) { return cli_->Head(path); } -inline Result Client::Head(const std::string &path, const Headers &headers) { - return cli_->Head(path, headers); -} - -inline Result Client::Post(const std::string &path) { return cli_->Post(path); } -inline Result Client::Post(const std::string &path, const Headers &headers) { - return cli_->Post(path, headers); -} -inline Result Client::Post(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Post(path, body, content_length, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_length, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress) { - return cli_->Post(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Post(path, body, content_type); -} -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Post(path, body, content_type, progress); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Post(path, headers, body, content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Post(path, headers, body, content_type, progress); -} -inline Result Client::Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Post(path, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Post(path, std::move(content_provider), content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Post(path, headers, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Post(path, headers, std::move(content_provider), content_type); -} -inline Result Client::Post(const std::string &path, const Params ¶ms) { - return cli_->Post(path, params); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Post(path, headers, params); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - return cli_->Post(path, headers, params, progress); -} -inline Result Client::Post(const std::string &path, - const MultipartFormDataItems &items) { - return cli_->Post(path, items); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - return cli_->Post(path, headers, items); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - return cli_->Post(path, headers, items, boundary); -} -inline Result -Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - return cli_->Post(path, headers, items, provider_items); -} -inline Result Client::Put(const std::string &path) { return cli_->Put(path); } -inline Result Client::Put(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Put(path, body, content_length, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_length, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, Progress progress) { - return cli_->Put(path, headers, body, content_length, content_type, progress); -} -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Put(path, body, content_type); -} -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Put(path, body, content_type, progress); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Put(path, headers, body, content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, Progress progress) { - return cli_->Put(path, headers, body, content_type, progress); -} -inline Result Client::Put(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Put(path, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Put(path, std::move(content_provider), content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Put(path, headers, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Put(path, headers, std::move(content_provider), content_type); -} -inline Result Client::Put(const std::string &path, const Params ¶ms) { - return cli_->Put(path, params); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Put(path, headers, params); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms, Progress progress) { - return cli_->Put(path, headers, params, progress); -} -inline Result Client::Put(const std::string &path, - const MultipartFormDataItems &items) { - return cli_->Put(path, items); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { - return cli_->Put(path, headers, items); -} -inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { - return cli_->Put(path, headers, items, boundary); -} -inline Result -Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const MultipartFormDataProviderItems &provider_items) { - return cli_->Put(path, headers, items, provider_items); -} -inline Result Client::Patch(const std::string &path) { - return cli_->Patch(path); -} -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, body, content_length, content_type); -} -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, body, content_length, content_type, progress); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_length, content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, body, content_type); -} -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, body, content_type, progress); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Patch(path, headers, body, content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Patch(path, headers, body, content_type, progress); -} -inline Result Client::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Patch(path, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Patch(path, std::move(content_provider), content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type) { - return cli_->Patch(path, headers, content_length, std::move(content_provider), - content_type); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type) { - return cli_->Patch(path, headers, std::move(content_provider), content_type); -} -inline Result Client::Delete(const std::string &path) { - return cli_->Delete(path); -} -inline Result Client::Delete(const std::string &path, const Headers &headers) { - return cli_->Delete(path, headers); -} -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, body, content_length, content_type); -} -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, body, content_length, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_length, content_type); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, body, content_type); -} -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, body, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { - return cli_->Delete(path, headers, body, content_type); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - Progress progress) { - return cli_->Delete(path, headers, body, content_type, progress); -} -inline Result Client::Options(const std::string &path) { - return cli_->Options(path); -} -inline Result Client::Options(const std::string &path, const Headers &headers) { - return cli_->Options(path, headers); -} - -inline bool Client::send(Request &req, Response &res, Error &error) { - return cli_->send(req, res, error); -} - -inline Result Client::send(const Request &req) { return cli_->send(req); } - -inline void Client::stop() { cli_->stop(); } - -inline std::string Client::host() const { return cli_->host(); } - -inline int Client::port() const { return cli_->port(); } - -inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } - -inline socket_t Client::socket() const { return cli_->socket(); } - -inline void -Client::set_hostname_addr_map(std::map addr_map) { - cli_->set_hostname_addr_map(std::move(addr_map)); -} - -inline void Client::set_default_headers(Headers headers) { - cli_->set_default_headers(std::move(headers)); -} - -inline void Client::set_header_writer( - std::function const &writer) { - cli_->set_header_writer(writer); -} - -inline void Client::set_address_family(int family) { - cli_->set_address_family(family); -} - -inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } - -inline void Client::set_socket_options(SocketOptions socket_options) { - cli_->set_socket_options(std::move(socket_options)); -} - -inline void Client::set_connection_timeout(time_t sec, time_t usec) { - cli_->set_connection_timeout(sec, usec); -} - -inline void Client::set_read_timeout(time_t sec, time_t usec) { - cli_->set_read_timeout(sec, usec); -} - -inline void Client::set_write_timeout(time_t sec, time_t usec) { - cli_->set_write_timeout(sec, usec); -} - -inline void Client::set_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_basic_auth(username, password); -} -inline void Client::set_bearer_token_auth(const std::string &token) { - cli_->set_bearer_token_auth(token); -} -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_digest_auth(username, password); -} -#endif - -inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } -inline void Client::set_follow_location(bool on) { - cli_->set_follow_location(on); -} - -inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); } - -inline void Client::set_compress(bool on) { cli_->set_compress(on); } - -inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } - -inline void Client::set_interface(const std::string &intf) { - cli_->set_interface(intf); -} - -inline void Client::set_proxy(const std::string &host, int port) { - cli_->set_proxy(host, port); -} -inline void Client::set_proxy_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_basic_auth(username, password); -} -inline void Client::set_proxy_bearer_token_auth(const std::string &token) { - cli_->set_proxy_bearer_token_auth(token); -} -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_digest_auth(username, password); -} -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::enable_server_certificate_verification(bool enabled) { - cli_->enable_server_certificate_verification(enabled); -} - -inline void Client::enable_server_hostname_verification(bool enabled) { - cli_->enable_server_hostname_verification(enabled); -} - -inline void Client::set_server_certificate_verifier( - std::function verifier) { - cli_->set_server_certificate_verifier(verifier); -} -#endif - -inline void Client::set_logger(Logger logger) { - cli_->set_logger(std::move(logger)); -} - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { - cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); -} - -inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (is_ssl_) { - static_cast(*cli_).set_ca_cert_store(ca_cert_store); - } else { - cli_->set_ca_cert_store(ca_cert_store); - } -} - -inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { - set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); -} - -inline long Client::get_openssl_verify_result() const { - if (is_ssl_) { - return static_cast(*cli_).get_openssl_verify_result(); - } - return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? -} - -inline SSL_CTX *Client::ssl_context() const { - if (is_ssl_) { return static_cast(*cli_).ssl_context(); } - return nullptr; -} -#endif - -// ---------------------------------------------------------------------------- - -} // namespace httplib - -#if defined(_WIN32) && defined(CPPHTTPLIB_USE_POLL) -#undef poll -#endif - -#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/benchmark/cpp-httplib-v19/main.cpp b/benchmark/cpp-httplib-v19/main.cpp deleted file mode 100644 index 86070a100c..0000000000 --- a/benchmark/cpp-httplib-v19/main.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "./httplib.h" -using namespace httplib; - -int main() { - Server svr; - - svr.Get("/", [](const Request &, Response &res) { - res.set_content("Hello World!", "text/plain"); - }); - - svr.listen("0.0.0.0", 8080); -} diff --git a/benchmark/download.sh b/benchmark/download.sh deleted file mode 100755 index 90c959ed85..0000000000 --- a/benchmark/download.sh +++ /dev/null @@ -1,2 +0,0 @@ -rm -f httplib.h -wget https://raw.githubusercontent.com/yhirose/cpp-httplib/v$1/httplib.h From ceff2c115405a29cb77982bf2dc60e49b2e7faf6 Mon Sep 17 00:00:00 2001 From: KTGH <19376155+sum01@users.noreply.github.com> Date: Sat, 5 Jul 2025 07:07:59 -0400 Subject: [PATCH 0462/1222] Add non-blocking getaddrinfo option to Cmake (#2168) Adds Cmake option HTTPLIB_USE_NON_BLOCKING_GETADDRINFO default on. Also adds the HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO Ref #1601, #2167, and https://github.com/yhirose/cpp-httplib/issues/1601#issuecomment-3021357070 --- CMakeLists.txt | 5 +++++ cmake/httplibConfig.cmake.in | 1 + 2 files changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0e0fdaca8..99d9bc4738 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ * HTTPLIB_REQUIRE_BROTLI (default off) * HTTPLIB_REQUIRE_ZSTD (default off) * HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN (default on) + * HTTPLIB_USE_NON_BLOCKING_GETADDRINFO (default on) * HTTPLIB_COMPILE (default off) * HTTPLIB_INSTALL (default on) * HTTPLIB_TEST (default off) @@ -49,6 +50,7 @@ * HTTPLIB_IS_USING_BROTLI - a bool for if Brotli support is enabled. * HTTPLIB_IS_USING_ZSTD - a bool for if ZSTD support is enabled. * HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN - a bool for if support of loading system certs from the Apple Keychain is enabled. + * HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO - a bool for if nonblocking getaddrinfo is enabled. * HTTPLIB_IS_COMPILED - a bool for if the library is compiled, or otherwise header-only. * HTTPLIB_INCLUDE_DIR - the root path to httplib's header (e.g. /usr/include). * HTTPLIB_LIBRARY - the full path to the library if compiled (e.g. /usr/lib/libhttplib.so). @@ -104,6 +106,7 @@ option(HTTPLIB_TEST "Enables testing and builds tests" OFF) option(HTTPLIB_REQUIRE_BROTLI "Requires Brotli to be found & linked, or fails build." OFF) option(HTTPLIB_USE_BROTLI_IF_AVAILABLE "Uses Brotli (if available) to enable Brotli decompression support." ON) option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system certs from the Apple Keychain." ON) +option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON) option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF) option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON) # Defaults to static library @@ -117,6 +120,7 @@ endif() # Set some variables that are used in-tree and while building based on our options set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}) +set(HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO ${HTTPLIB_USE_NON_BLOCKING_GETADDRINFO}) # Threads needed for on some systems, and for on Linux set(THREADS_PREFER_PTHREAD_FLAG TRUE) @@ -269,6 +273,7 @@ target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:CPPHTTPLIB_ZSTD_SUPPORT> $<$:CPPHTTPLIB_OPENSSL_SUPPORT> $<$,$,$>:CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN> + $<$:CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO> ) # CMake configuration files installation directory diff --git a/cmake/httplibConfig.cmake.in b/cmake/httplibConfig.cmake.in index 19dbe694ce..8ca8b991a2 100644 --- a/cmake/httplibConfig.cmake.in +++ b/cmake/httplibConfig.cmake.in @@ -7,6 +7,7 @@ set(HTTPLIB_IS_USING_OPENSSL @HTTPLIB_IS_USING_OPENSSL@) set(HTTPLIB_IS_USING_ZLIB @HTTPLIB_IS_USING_ZLIB@) set(HTTPLIB_IS_COMPILED @HTTPLIB_COMPILE@) set(HTTPLIB_IS_USING_BROTLI @HTTPLIB_IS_USING_BROTLI@) +set(HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO @HTTPLIB_IS_USING_NON_BLOCKING_GETADDRINFO@) set(HTTPLIB_VERSION @PROJECT_VERSION@) include(CMakeFindDependencyMacro) From 120405beac2d6c238c01600bcebfd62365068305 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Jul 2025 07:13:13 -0400 Subject: [PATCH 0463/1222] clang-format --- test/test.cc | 145 ++++++++++++++++++++++++++++----------------------- 1 file changed, 80 insertions(+), 65 deletions(-) diff --git a/test/test.cc b/test/test.cc index 3ebc859b71..878624ee7a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5302,11 +5302,13 @@ TEST_F(ServerTest, PatchContentReceiver) { ASSERT_EQ("content", res->body); } -template +template void TestWithHeadersAndContentReceiver( - ClientType& cli, - std::function request_func) { + ClientType &cli, + std::function + request_func) { Headers headers; headers.emplace("X-Custom-Header", "test-value"); @@ -5316,7 +5318,8 @@ void TestWithHeadersAndContentReceiver( [&](const char *data, size_t data_length) { received_body.append(data, data_length); return true; - }, nullptr); + }, + nullptr); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -5329,11 +5332,12 @@ TEST_F(ServerTest, PostWithHeadersAndContentReceiver) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiver(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { - return cli.Post(path, headers, body, content_type, receiver, progress); - }); + TestWithHeadersAndContentReceiver( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver, DownloadProgress progress) { + return cli.Post(path, headers, body, content_type, receiver, progress); + }); } TEST_F(ServerTest, PutWithHeadersAndContentReceiver) { @@ -5342,11 +5346,12 @@ TEST_F(ServerTest, PutWithHeadersAndContentReceiver) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiver(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { - return cli.Put(path, headers, body, content_type, receiver, progress); - }); + TestWithHeadersAndContentReceiver( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver, DownloadProgress progress) { + return cli.Put(path, headers, body, content_type, receiver, progress); + }); } TEST_F(ServerTest, PatchWithHeadersAndContentReceiver) { @@ -5355,18 +5360,21 @@ TEST_F(ServerTest, PatchWithHeadersAndContentReceiver) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiver(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { - return cli.Patch(path, headers, body, content_type, receiver, progress); - }); + TestWithHeadersAndContentReceiver( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver, DownloadProgress progress) { + return cli.Patch(path, headers, body, content_type, receiver, progress); + }); } -template +template void TestWithHeadersAndContentReceiverWithProgress( - ClientType& cli, - std::function request_func) { + ClientType &cli, + std::function + request_func) { Headers headers; headers.emplace("X-Test-Header", "progress-test"); @@ -5396,11 +5404,12 @@ TEST_F(ServerTest, PostWithHeadersAndContentReceiverWithProgress) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiverWithProgress(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { - return cli.Post(path, headers, body, content_type, receiver, progress); - }); + TestWithHeadersAndContentReceiverWithProgress( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver, DownloadProgress progress) { + return cli.Post(path, headers, body, content_type, receiver, progress); + }); } TEST_F(ServerTest, PutWithHeadersAndContentReceiverWithProgress) { @@ -5409,11 +5418,12 @@ TEST_F(ServerTest, PutWithHeadersAndContentReceiverWithProgress) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiverWithProgress(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { - return cli.Put(path, headers, body, content_type, receiver, progress); - }); + TestWithHeadersAndContentReceiverWithProgress( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver, DownloadProgress progress) { + return cli.Put(path, headers, body, content_type, receiver, progress); + }); } TEST_F(ServerTest, PatchWithHeadersAndContentReceiverWithProgress) { @@ -5422,31 +5432,33 @@ TEST_F(ServerTest, PatchWithHeadersAndContentReceiverWithProgress) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiverWithProgress(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver, DownloadProgress progress) { - return cli.Patch(path, headers, body, content_type, receiver, progress); - }); + TestWithHeadersAndContentReceiverWithProgress( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver, DownloadProgress progress) { + return cli.Patch(path, headers, body, content_type, receiver, progress); + }); } -template +template void TestWithHeadersAndContentReceiverError( - ClientType& cli, - std::function request_func) { + ClientType &cli, std::function + request_func) { Headers headers; headers.emplace("X-Error-Test", "true"); std::string received_body; auto receiver_failed = false; - auto res = request_func( - cli, "/content_receiver", headers, "content", "text/plain", - [&](const char *data, size_t data_length) { - received_body.append(data, data_length); - receiver_failed = true; - return false; - }); + auto res = + request_func(cli, "/content_receiver", headers, "content", "text/plain", + [&](const char *data, size_t data_length) { + received_body.append(data, data_length); + receiver_failed = true; + return false; + }); ASSERT_FALSE(res); EXPECT_TRUE(receiver_failed); @@ -5458,11 +5470,12 @@ TEST_F(ServerTest, PostWithHeadersAndContentReceiverError) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiverError(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver) { - return cli.Post(path, headers, body, content_type, receiver); - }); + TestWithHeadersAndContentReceiverError( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver) { + return cli.Post(path, headers, body, content_type, receiver); + }); } TEST_F(ServerTest, PuttWithHeadersAndContentReceiverError) { @@ -5471,11 +5484,12 @@ TEST_F(ServerTest, PuttWithHeadersAndContentReceiverError) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiverError(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver) { - return cli.Put(path, headers, body, content_type, receiver); - }); + TestWithHeadersAndContentReceiverError( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver) { + return cli.Put(path, headers, body, content_type, receiver); + }); } TEST_F(ServerTest, PatchWithHeadersAndContentReceiverError) { @@ -5484,11 +5498,12 @@ TEST_F(ServerTest, PatchWithHeadersAndContentReceiverError) { #else using ClientT = Client; #endif - TestWithHeadersAndContentReceiverError(cli_, - [](ClientT& cli, const std::string& path, const Headers& headers, const std::string& body, - const std::string& content_type, ContentReceiver receiver) { - return cli.Patch(path, headers, body, content_type, receiver); - }); + TestWithHeadersAndContentReceiverError( + cli_, [](ClientT &cli, const std::string &path, const Headers &headers, + const std::string &body, const std::string &content_type, + ContentReceiver receiver) { + return cli.Patch(path, headers, body, content_type, receiver); + }); } TEST_F(ServerTest, PostQueryStringAndBody) { From cb85e573dea26b936aa07bfa3aa4454fd37b8db3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Jul 2025 15:17:53 -0400 Subject: [PATCH 0464/1222] Fix #1416 (#2169) * Fix #1416 * Update * Update --- README.md | 43 +++++++++++++++++++++++++++ httplib.h | 65 +++++++++++++++++++++++++++++++++++++---- test/test.cc | 82 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 176 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 71bc170ca9..cd933beb73 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,49 @@ cli.enable_server_hostname_verification(false); > [!NOTE] > When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. +### SSL Error Handling + +When SSL operations fail, cpp-httplib provides detailed error information through two separate error fields: + +```c++ +#define CPPHTTPLIB_OPENSSL_SUPPORT +#include "path/to/httplib.h" + +httplib::Client cli("https://example.com"); + +auto res = cli.Get("/"); +if (!res) { + // Check the error type + auto err = res.error(); + + switch (err) { + case httplib::Error::SSLConnection: + std::cout << "SSL connection failed, SSL error: " + << res->ssl_error() << std::endl; + break; + + case httplib::Error::SSLLoadingCerts: + std::cout << "SSL cert loading failed, OpenSSL error: " + << std::hex << res->ssl_openssl_error() << std::endl; + break; + + case httplib::Error::SSLServerVerification: + std::cout << "SSL verification failed, X509 error: " + << res->ssl_openssl_error() << std::endl; + break; + + case httplib::Error::SSLServerHostnameVerification: + std::cout << "SSL hostname verification failed, X509 error: " + << res->ssl_openssl_error() << std::endl; + break; + + default: + std::cout << "HTTP error: " << httplib::to_string(err) << std::endl; + } + } +} +``` + Server ------ diff --git a/httplib.h b/httplib.h index bf02a802e1..c2d986082c 100644 --- a/httplib.h +++ b/httplib.h @@ -1246,6 +1246,17 @@ class Result { Headers &&request_headers = Headers{}) : res_(std::move(res)), err_(err), request_headers_(std::move(request_headers)) {} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + Result(std::unique_ptr &&res, Error err, Headers &&request_headers, + int ssl_error) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)), ssl_error_(ssl_error) {} + Result(std::unique_ptr &&res, Error err, Headers &&request_headers, + int ssl_error, unsigned long ssl_openssl_error) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)), ssl_error_(ssl_error), + ssl_openssl_error_(ssl_openssl_error) {} +#endif // Response operator bool() const { return res_ != nullptr; } bool operator==(std::nullptr_t) const { return res_ == nullptr; } @@ -1260,6 +1271,13 @@ class Result { // Error Error error() const { return err_; } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // SSL Error + int ssl_error() const { return ssl_error_; } + // OpenSSL Error + unsigned long ssl_openssl_error() const { return ssl_openssl_error_; } +#endif + // Request Headers bool has_request_header(const std::string &key) const; std::string get_request_header_value(const std::string &key, @@ -1273,6 +1291,10 @@ class Result { std::unique_ptr res_; Error err_ = Error::Unknown; Headers request_headers_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + int ssl_error_ = 0; + unsigned long ssl_openssl_error_ = 0; +#endif }; class ClientImpl { @@ -1570,6 +1592,11 @@ class ClientImpl { Logger logger_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + int last_ssl_error_ = 0; + unsigned long last_openssl_error_ = 0; +#endif + private: bool send_(Request &req, Response &res, Error &error); Result send_(Request &&req); @@ -1840,6 +1867,9 @@ class SSLServer : public Server { SSL_CTX *ctx_; std::mutex ctx_mutex_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + int last_ssl_error_ = 0; +#endif }; class SSLClient final : public ClientImpl { @@ -8173,7 +8203,12 @@ inline Result ClientImpl::send_(Request &&req) { auto res = detail::make_unique(); auto error = Error::Success; auto ret = send(req, *res, error); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers), + last_ssl_error_, last_openssl_error_}; +#else return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; +#endif } inline bool ClientImpl::handle_request(Stream &strm, Request &req, @@ -8723,7 +8758,12 @@ inline Result ClientImpl::send_with_content_provider( req, body, content_length, std::move(content_provider), std::move(content_provider_without_length), content_type, error); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + return Result{std::move(res), error, std::move(req.headers), last_ssl_error_, + last_openssl_error_}; +#else return Result{std::move(res), error, std::move(req.headers)}; +#endif } inline std::string @@ -9790,8 +9830,8 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, template bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, U ssl_connect_or_accept, - time_t timeout_sec, - time_t timeout_usec) { + time_t timeout_sec, time_t timeout_usec, + int *ssl_error) { auto res = 0; while ((res = ssl_connect_or_accept(ssl)) != 1) { auto err = SSL_get_error(ssl, res); @@ -9804,6 +9844,7 @@ bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, break; default: break; } + if (ssl_error) { *ssl_error = err; } return false; } return true; @@ -9897,9 +9938,10 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); } else { - return -1; + break; } } + assert(ret < 0); } return ret; } else { @@ -9929,9 +9971,10 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { if (ret >= 0) { return ret; } err = SSL_get_error(ssl_, ret); } else { - return -1; + break; } } + assert(ret < 0); } return ret; } @@ -9982,6 +10025,7 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != 1 || SSL_CTX_check_private_key(ctx_) != 1) { + last_ssl_error_ = static_cast(ERR_get_error()); SSL_CTX_free(ctx_); ctx_ = nullptr; } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { @@ -10055,7 +10099,8 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { sock, ctx_, ctx_mutex_, [&](SSL *ssl2) { return detail::ssl_connect_or_accept_nonblocking( - sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); + sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_, + &last_ssl_error_); }, [](SSL * /*ssl2*/) { return true; }); @@ -10123,6 +10168,7 @@ inline SSLClient::SSLClient(const std::string &host, int port, SSL_FILETYPE_PEM) != 1 || SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), SSL_FILETYPE_PEM) != 1) { + last_openssl_error_ = ERR_get_error(); SSL_CTX_free(ctx_); ctx_ = nullptr; } @@ -10149,6 +10195,7 @@ inline SSLClient::SSLClient(const std::string &host, int port, if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { + last_openssl_error_ = ERR_get_error(); SSL_CTX_free(ctx_); ctx_ = nullptr; } @@ -10292,11 +10339,13 @@ inline bool SSLClient::load_certs() { if (!ca_cert_file_path_.empty()) { if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), nullptr)) { + last_openssl_error_ = ERR_get_error(); ret = false; } } else if (!ca_cert_dir_path_.empty()) { if (!SSL_CTX_load_verify_locations(ctx_, nullptr, ca_cert_dir_path_.c_str())) { + last_openssl_error_ = ERR_get_error(); ret = false; } } else { @@ -10329,7 +10378,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { if (!detail::ssl_connect_or_accept_nonblocking( socket.sock, ssl2, SSL_connect, connection_timeout_sec_, - connection_timeout_usec_)) { + connection_timeout_usec_, &last_ssl_error_)) { error = Error::SSLConnection; return false; } @@ -10342,6 +10391,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { } if (verification_status == SSLVerifierResponse::CertificateRejected) { + last_openssl_error_ = ERR_get_error(); error = Error::SSLServerVerification; return false; } @@ -10350,6 +10400,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { verify_result_ = SSL_get_verify_result(ssl2); if (verify_result_ != X509_V_OK) { + last_openssl_error_ = static_cast(verify_result_); error = Error::SSLServerVerification; return false; } @@ -10358,12 +10409,14 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { auto se = detail::scope_exit([&] { X509_free(server_cert); }); if (server_cert == nullptr) { + last_openssl_error_ = ERR_get_error(); error = Error::SSLServerVerification; return false; } if (server_hostname_verification_) { if (!verify_host(server_cert)) { + last_openssl_error_ = X509_V_ERR_HOSTNAME_MISMATCH; error = Error::SSLServerHostnameVerification; return false; } diff --git a/test/test.cc b/test/test.cc index 878624ee7a..34a875d390 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7492,6 +7492,47 @@ TEST(SSLClientTest, ServerNameIndication_Online) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(SSLClientTest, ServerCertificateVerificationError_Online) { + // Use a site that will cause SSL verification failure due to self-signed cert + SSLClient cli("self-signed.badssl.com", 443); + cli.enable_server_certificate_verification(true); + auto res = cli.Get("/"); + + ASSERT_TRUE(!res); + EXPECT_EQ(Error::SSLServerVerification, res.error()); + + // For SSL server verification errors, ssl_error should be 0, only + // ssl_openssl_error should be set + EXPECT_EQ(0, res.ssl_error()); + + // Verify OpenSSL error is captured for SSLServerVerification + // This occurs when SSL_get_verify_result() returns a verification failure + EXPECT_EQ(static_cast(X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT), + res.ssl_openssl_error()); +} + +TEST(SSLClientTest, ServerHostnameVerificationError_Online) { + // Use a site where hostname doesn't match the certificate + // badssl.com provides wrong.host.badssl.com which has cert for *.badssl.com + SSLClient cli("wrong.host.badssl.com", 443); + cli.enable_server_certificate_verification(true); + cli.enable_server_hostname_verification(true); + + auto res = cli.Get("/"); + ASSERT_TRUE(!res); + + EXPECT_EQ(Error::SSLServerHostnameVerification, res.error()); + + // For SSL hostname verification errors, ssl_error should be 0, only + // ssl_openssl_error should be set + EXPECT_EQ(0, res.ssl_error()); + + // Verify OpenSSL error is captured for SSLServerHostnameVerification + // This occurs when verify_host() fails due to hostname mismatch + EXPECT_EQ(static_cast(X509_V_ERR_HOSTNAME_MISMATCH), + res.ssl_openssl_error()); +} + TEST(SSLClientTest, ServerCertificateVerification1_Online) { Client cli("https://google.com"); auto res = cli.Get("/"); @@ -7501,19 +7542,33 @@ TEST(SSLClientTest, ServerCertificateVerification1_Online) { TEST(SSLClientTest, ServerCertificateVerification2_Online) { SSLClient cli("google.com"); - cli.enable_server_certificate_verification(true); - cli.set_ca_cert_path("hello"); + cli.set_ca_cert_path(CA_CERT_FILE); auto res = cli.Get("/"); - ASSERT_TRUE(!res); - EXPECT_EQ(Error::SSLLoadingCerts, res.error()); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); } TEST(SSLClientTest, ServerCertificateVerification3_Online) { SSLClient cli("google.com"); - cli.set_ca_cert_path(CA_CERT_FILE); + cli.enable_server_certificate_verification(true); + cli.set_ca_cert_path("hello"); + auto res = cli.Get("/"); - ASSERT_TRUE(res); - ASSERT_EQ(StatusCode::MovedPermanently_301, res->status); + ASSERT_TRUE(!res); + EXPECT_EQ(Error::SSLLoadingCerts, res.error()); + + // For SSL_CTX operations, ssl_error should be 0, only ssl_openssl_error + // should be set + EXPECT_EQ(0, res.ssl_error()); + + // Verify OpenSSL error is captured for SSLLoadingCerts + // This error occurs when SSL_CTX_load_verify_locations() fails + // > openssl errstr 0x80000002 + // error:80000002:system library::No such file or directory + // > openssl errstr 0xA000126 + // error:0A000126:SSL routines::unexpected eof while reading + EXPECT_TRUE(res.ssl_openssl_error() == 0x80000002 || + res.ssl_openssl_error() == 0xA000126); } TEST(SSLClientTest, ServerCertificateVerification4) { @@ -7790,10 +7845,20 @@ TEST(SSLClientServerTest, ClientCertMissing) { svr.wait_until_ready(); SSLClient cli(HOST, PORT); - auto res = cli.Get("/test"); cli.set_connection_timeout(30); + + auto res = cli.Get("/test"); ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLServerVerification, res.error()); + + // For SSL server verification errors, ssl_error should be 0, only + // ssl_openssl_error should be set + EXPECT_EQ(0, res.ssl_error()); + + // Verify OpenSSL error is captured for SSLServerVerification + // Note: This test may have different error codes depending on the exact + // verification failure + EXPECT_NE(0UL, res.ssl_openssl_error()); } TEST(SSLClientServerTest, TrustDirOptional) { @@ -7868,6 +7933,7 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { auto res = cli.Get("/test"); ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLConnection, res.error()); + EXPECT_EQ(SSL_ERROR_WANT_READ, res.ssl_error()); } TEST(SSLClientServerTest, CustomizeServerSSLCtx) { From a636a094bf2110333b4ce177b79faf6844746bdc Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Jul 2025 20:22:57 -0400 Subject: [PATCH 0465/1222] Fix #1656 --- README.md | 12 +++++ httplib.h | 9 ++++ test/test.cc | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) diff --git a/README.md b/README.md index cd933beb73..4cf22de63a 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,18 @@ svr.set_logger([](const auto& req, const auto& res) { }); ``` +You can also set a pre-compression logger to capture request/response data before compression is applied. This is useful for debugging and monitoring purposes when you need to see the original, uncompressed response content: + +```cpp +svr.set_pre_compression_logger([](const auto& req, const auto& res) { + // Log before compression - res.body contains uncompressed content + // Content-Encoding header is not yet set + your_pre_compression_logger(req, res); +}); +``` + +The pre-compression logger is only called when compression would be applied. For responses without compression, only the regular logger is called. + ### Error handler ```cpp diff --git a/httplib.h b/httplib.h index c2d986082c..244e0555e9 100644 --- a/httplib.h +++ b/httplib.h @@ -1059,6 +1059,7 @@ class Server { Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); Server &set_logger(Logger logger); + Server &set_pre_compression_logger(Logger logger); Server &set_address_family(int family); Server &set_tcp_nodelay(bool on); @@ -1202,6 +1203,7 @@ class Server { Expect100ContinueHandler expect_100_continue_handler_; Logger logger_; + Logger pre_compression_logger_; int address_family_ = AF_UNSPEC; bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; @@ -6913,6 +6915,11 @@ inline Server &Server::set_logger(Logger logger) { return *this; } +inline Server &Server::set_pre_compression_logger(Logger logger) { + pre_compression_logger_ = std::move(logger); + return *this; +} + inline Server & Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { expect_100_continue_handler_ = std::move(handler); @@ -7647,6 +7654,8 @@ inline void Server::apply_ranges(const Request &req, Response &res, } if (type != detail::EncodingType::None) { + if (pre_compression_logger_) { pre_compression_logger_(req, res); } + std::unique_ptr compressor; std::string content_encoding; diff --git a/test/test.cc b/test/test.cc index 34a875d390..86a47b7c8f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -5901,6 +5901,143 @@ TEST_F(ServerTest, PutWithContentProviderWithZstd) { EXPECT_EQ("PUT", res->body); } +// Pre-compression logging tests +TEST_F(ServerTest, PreCompressionLogging) { + // Test data for compression (matches the actual /compress endpoint content) + const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; + + // Variables to capture logging data + std::string pre_compression_body; + std::string pre_compression_content_type; + std::string pre_compression_content_encoding; + + std::string post_compression_body; + std::string post_compression_content_type; + std::string post_compression_content_encoding; + + // Set up pre-compression logger + svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { + pre_compression_body = res.body; + pre_compression_content_type = res.get_header_value("Content-Type"); + pre_compression_content_encoding = res.get_header_value("Content-Encoding"); + }); + + // Set up post-compression logger + svr_.set_logger([&](const Request &req, const Response &res) { + post_compression_body = res.body; + post_compression_content_type = res.get_header_value("Content-Type"); + post_compression_content_encoding = res.get_header_value("Content-Encoding"); + }); + + // Test with gzip compression + Headers headers; + headers.emplace("Accept-Encoding", "gzip"); + + auto res = cli_.Get("/compress", headers); + + // Verify response was compressed + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); + + // Verify pre-compression logger captured uncompressed content + EXPECT_EQ(test_content, pre_compression_body); + EXPECT_EQ("text/plain", pre_compression_content_type); + EXPECT_TRUE(pre_compression_content_encoding.empty()); // No encoding header before compression + + // Verify post-compression logger captured compressed content + EXPECT_NE(test_content, post_compression_body); // Should be different after compression + EXPECT_EQ("text/plain", post_compression_content_type); + EXPECT_EQ("gzip", post_compression_content_encoding); + + // Verify compressed content is smaller + EXPECT_LT(post_compression_body.size(), pre_compression_body.size()); +} + +TEST_F(ServerTest, PreCompressionLoggingWithBrotli) { + const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; + + std::string pre_compression_body; + std::string post_compression_body; + + svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { + pre_compression_body = res.body; + }); + + svr_.set_logger([&](const Request &req, const Response &res) { + post_compression_body = res.body; + }); + + Headers headers; + headers.emplace("Accept-Encoding", "br"); + + auto res = cli_.Get("/compress", headers); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("br", res->get_header_value("Content-Encoding")); + + // Verify pre-compression content is uncompressed + EXPECT_EQ(test_content, pre_compression_body); + + // Verify post-compression content is compressed + EXPECT_NE(test_content, post_compression_body); + EXPECT_LT(post_compression_body.size(), pre_compression_body.size()); +} + +TEST_F(ServerTest, PreCompressionLoggingWithoutCompression) { + const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; + + std::string pre_compression_body; + std::string post_compression_body; + + svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { + pre_compression_body = res.body; + }); + + svr_.set_logger([&](const Request &req, const Response &res) { + post_compression_body = res.body; + }); + + // Request without compression (use /nocompress endpoint) + Headers headers; + auto res = cli_.Get("/nocompress", headers); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_TRUE(res->get_header_value("Content-Encoding").empty()); + + // Pre-compression logger should not be called when no compression is applied + EXPECT_TRUE(pre_compression_body.empty()); // Pre-compression logger not called + EXPECT_EQ(test_content, post_compression_body); // Post-compression logger captures final content +} + +TEST_F(ServerTest, PreCompressionLoggingOnlyPreLogger) { + const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; + + std::string pre_compression_body; + bool pre_logger_called = false; + + // Set only pre-compression logger + svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { + pre_compression_body = res.body; + pre_logger_called = true; + }); + + Headers headers; + headers.emplace("Accept-Encoding", "gzip"); + + auto res = cli_.Get("/compress", headers); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); + + // Verify pre-compression logger was called + EXPECT_TRUE(pre_logger_called); + EXPECT_EQ(test_content, pre_compression_body); +} + TEST(ZstdDecompressor, ChunkedDecompression) { std::string data; for (size_t i = 0; i < 32 * 1024; ++i) { From a3f55691962987aae00570a7663b8dadecce74b7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 5 Jul 2025 20:30:31 -0400 Subject: [PATCH 0466/1222] Fix #2082 (#2170) --- README.md | 47 ++++++++++++++ httplib.h | 161 ++++++++++++++++++++++++++++++++++------------ test/test.cc | 177 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 334 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 4cf22de63a..4f3c720578 100644 --- a/README.md +++ b/README.md @@ -893,6 +893,26 @@ res->status; // 200 cli.set_interface("eth0"); // Interface name, IP address or host name ``` +### Automatic Path Encoding + +The client automatically encodes special characters in URL paths by default: + +```cpp +httplib::Client cli("https://example.com"); + +// Automatic path encoding (default behavior) +cli.set_path_encode(true); +auto res = cli.Get("/path with spaces/file.txt"); // Automatically encodes spaces + +// Disable automatic path encoding +cli.set_path_encode(false); +auto res = cli.Get("/already%20encoded/path"); // Use pre-encoded paths +``` + +- `set_path_encode(bool on)` - Controls automatic encoding of special characters in URL paths + - `true` (default): Automatically encodes spaces, plus signs, newlines, and other special characters + - `false`: Sends paths as-is without encoding (useful for pre-encoded URLs) + Compression ----------- @@ -969,6 +989,33 @@ cli.set_address_family(AF_UNIX); "my-socket.sock" can be a relative path or an absolute path. Your application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. +URI Encoding/Decoding Utilities +------------------------------- + +cpp-httplib provides utility functions for URI encoding and decoding: + +```cpp +#include + +std::string url = "https://example.com/search?q=hello world"; +std::string encoded = httplib::encode_uri(url); +std::string decoded = httplib::decode_uri(encoded); + +std::string param = "hello world"; +std::string encoded_component = httplib::encode_uri_component(param); +std::string decoded_component = httplib::decode_uri_component(encoded_component); +``` + +### Functions + +- `encode_uri(const std::string &value)` - Encodes a full URI, preserving reserved characters like `://`, `?`, `&`, `=` +- `decode_uri(const std::string &value)` - Decodes a URI-encoded string +- `encode_uri_component(const std::string &value)` - Encodes a URI component (query parameter, path segment), encoding all reserved characters +- `decode_uri_component(const std::string &value)` - Decodes a URI component + +Use `encode_uri()` for full URLs and `encode_uri_component()` for individual query parameters or path segments. + + Split httplib.h into .h and .cc ------------------------------- diff --git a/httplib.h b/httplib.h index 244e0555e9..4648ce7bec 100644 --- a/httplib.h +++ b/httplib.h @@ -1444,7 +1444,7 @@ class ClientImpl { void set_keep_alive(bool on); void set_follow_location(bool on); - void set_url_encode(bool on); + void set_path_encode(bool on); void set_compress(bool on); @@ -1556,7 +1556,7 @@ class ClientImpl { bool keep_alive_ = false; bool follow_location_ = false; - bool url_encode_ = true; + bool path_encode_ = true; int address_family_ = AF_UNSPEC; bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; @@ -1794,6 +1794,7 @@ class Client { void set_keep_alive(bool on); void set_follow_location(bool on); + void set_path_encode(bool on); void set_url_encode(bool on); void set_compress(bool on); @@ -2248,6 +2249,16 @@ std::string hosted_at(const std::string &hostname); void hosted_at(const std::string &hostname, std::vector &addrs); +std::string encode_uri_component(const std::string &value); + +std::string encode_uri(const std::string &value); + +std::string decode_uri_component(const std::string &value); + +std::string decode_uri(const std::string &value); + +std::string encode_query_param(const std::string &value); + std::string append_query_params(const std::string &path, const Params ¶ms); std::pair make_range_header(const Ranges &ranges); @@ -2289,9 +2300,7 @@ struct FileStat { int ret_ = -1; }; -std::string encode_query_param(const std::string &value); - -std::string decode_url(const std::string &s, bool convert_plus_to_space); +std::string decode_path(const std::string &s, bool convert_plus_to_space); std::string trim_copy(const std::string &s); @@ -2761,28 +2770,7 @@ inline bool FileStat::is_dir() const { return ret_ >= 0 && S_ISDIR(st_.st_mode); } -inline std::string encode_query_param(const std::string &value) { - std::ostringstream escaped; - escaped.fill('0'); - escaped << std::hex; - - for (auto c : value) { - if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || - c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || - c == ')') { - escaped << c; - } else { - escaped << std::uppercase; - escaped << '%' << std::setw(2) - << static_cast(static_cast(c)); - escaped << std::nouppercase; - } - } - - return escaped.str(); -} - -inline std::string encode_url(const std::string &s) { +inline std::string encode_path(const std::string &s) { std::string result; result.reserve(s.size()); @@ -2814,8 +2802,8 @@ inline std::string encode_url(const std::string &s) { return result; } -inline std::string decode_url(const std::string &s, - bool convert_plus_to_space) { +inline std::string decode_path(const std::string &s, + bool convert_plus_to_space) { std::string result; for (size_t i = 0; i < s.size(); i++) { @@ -4539,7 +4527,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) { case_ignore::equal(key, "Referer")) { fn(key, val); } else { - fn(key, decode_url(val, false)); + fn(key, decode_path(val, false)); } return true; @@ -5104,7 +5092,7 @@ inline std::string params_to_query_str(const Params ¶ms) { if (it != params.begin()) { query += "&"; } query += it->first; query += "="; - query += encode_query_param(it->second); + query += httplib::encode_uri_component(it->second); } return query; } @@ -5127,7 +5115,7 @@ inline void parse_query_text(const char *data, std::size_t size, }); if (!key.empty()) { - params.emplace(decode_url(key, true), decode_url(val, true)); + params.emplace(decode_path(key, true), decode_path(val, true)); } }); } @@ -5437,7 +5425,7 @@ class MultipartFormDataParser { std::smatch m2; if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { - file_.filename = decode_url(m2[1], false); // override... + file_.filename = decode_path(m2[1], false); // override... } else { is_valid_ = false; return false; @@ -6260,6 +6248,94 @@ inline void hosted_at(const std::string &hostname, } } +inline std::string encode_uri_component(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string encode_uri(const std::string &value) { + std::ostringstream escaped; + escaped.fill('0'); + escaped << std::hex; + + for (auto c : value) { + if (std::isalnum(static_cast(c)) || c == '-' || c == '_' || + c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || + c == ')' || c == ';' || c == '/' || c == '?' || c == ':' || c == '@' || + c == '&' || c == '=' || c == '+' || c == '$' || c == ',' || c == '#') { + escaped << c; + } else { + escaped << std::uppercase; + escaped << '%' << std::setw(2) + << static_cast(static_cast(c)); + escaped << std::nouppercase; + } + } + + return escaped.str(); +} + +inline std::string decode_uri_component(const std::string &value) { + std::string result; + + for (size_t i = 0; i < value.size(); i++) { + if (value[i] == '%' && i + 2 < value.size()) { + auto val = 0; + if (detail::from_hex_to_i(value, i + 1, 2, val)) { + result += static_cast(val); + i += 2; + } else { + result += value[i]; + } + } else { + result += value[i]; + } + } + + return result; +} + +inline std::string decode_uri(const std::string &value) { + std::string result; + + for (size_t i = 0; i < value.size(); i++) { + if (value[i] == '%' && i + 2 < value.size()) { + auto val = 0; + if (detail::from_hex_to_i(value, i + 1, 2, val)) { + result += static_cast(val); + i += 2; + } else { + result += value[i]; + } + } else { + result += value[i]; + } + } + + return result; +} + +[[deprecated("Use encode_uri_component instead")]] +inline std::string encode_query_param(const std::string &value) { + return encode_uri_component(value); +} + inline std::string append_query_params(const std::string &path, const Params ¶ms) { std::string path_with_query = path; @@ -7070,7 +7146,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { detail::divide(req.target, '?', [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, std::size_t rhs_size) { - req.path = detail::decode_url( + req.path = detail::decode_path( std::string(lhs_data, lhs_size), false); detail::parse_query_text(rhs_data, rhs_size, req.params); }); @@ -7967,7 +8043,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { #endif keep_alive_ = rhs.keep_alive_; follow_location_ = rhs.follow_location_; - url_encode_ = rhs.url_encode_; + path_encode_ = rhs.path_encode_; address_family_ = rhs.address_family_; tcp_nodelay_ = rhs.tcp_nodelay_; ipv6_v6only_ = rhs.ipv6_v6only_; @@ -8332,7 +8408,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { if (next_host.empty()) { next_host = host_; } if (next_path.empty()) { next_path = "/"; } - auto path = detail::decode_url(next_path, true) + next_query; + auto path = detail::decode_path(next_path, true) + next_query; // Same host redirect - use current client if (next_scheme == scheme && next_host == host_ && next_port == port_) { @@ -8427,7 +8503,7 @@ inline void ClientImpl::setup_redirect_client(ClientType &client) { client.set_keep_alive(keep_alive_); client.set_follow_location( true); // Enable redirects to handle multi-step redirects - client.set_url_encode(url_encode_); + client.set_path_encode(path_encode_); client.set_compress(compress_); client.set_decompress(decompress_); @@ -8621,7 +8697,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, : append_query_params(req.path, req.params); const auto &path = - url_encode_ ? detail::encode_url(path_with_query) : path_with_query; + path_encode_ ? detail::encode_path(path_with_query) : path_with_query; detail::write_request_line(bstrm, req.method, path); @@ -9667,7 +9743,7 @@ inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } -inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } +inline void ClientImpl::set_path_encode(bool on) { path_encode_ = on; } inline void ClientImpl::set_hostname_addr_map(std::map addr_map) { @@ -11143,7 +11219,12 @@ inline void Client::set_follow_location(bool on) { cli_->set_follow_location(on); } -inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); } +inline void Client::set_path_encode(bool on) { cli_->set_path_encode(on); } + +[[deprecated("Use set_path_encode instead")]] +inline void Client::set_url_encode(bool on) { + cli_->set_path_encode(on); +} inline void Client::set_compress(bool on) { cli_->set_compress(on); } diff --git a/test/test.cc b/test/test.cc index 86a47b7c8f..8d07eb8990 100644 --- a/test/test.cc +++ b/test/test.cc @@ -258,33 +258,33 @@ TEST(StartupTest, WSAStartup) { } #endif -TEST(DecodeURLTest, PercentCharacter) { +TEST(DecodePathTest, PercentCharacter) { EXPECT_EQ( - detail::decode_url( + detail::decode_path( R"(descrip=Gastos%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1%C3%91%206)", false), u8"descrip=Gastos áéíóúñÑ 6"); } -TEST(DecodeURLTest, PercentCharacterNUL) { +TEST(DecodePathTest, PercentCharacterNUL) { string expected; expected.push_back('x'); expected.push_back('\0'); expected.push_back('x'); - EXPECT_EQ(detail::decode_url("x%00x", false), expected); + EXPECT_EQ(detail::decode_path("x%00x", false), expected); } TEST(EncodeQueryParamTest, ParseUnescapedChararactersTest) { string unescapedCharacters = "-_.!~*'()"; - EXPECT_EQ(detail::encode_query_param(unescapedCharacters), "-_.!~*'()"); + EXPECT_EQ(httplib::encode_uri_component(unescapedCharacters), "-_.!~*'()"); } TEST(EncodeQueryParamTest, ParseReservedCharactersTest) { string reservedCharacters = ";,/?:@&=+$"; - EXPECT_EQ(detail::encode_query_param(reservedCharacters), + EXPECT_EQ(httplib::encode_uri_component(reservedCharacters), "%3B%2C%2F%3F%3A%40%26%3D%2B%24"); } @@ -293,13 +293,168 @@ TEST(EncodeQueryParamTest, TestUTF8Characters) { string russianCharacters = u8"дом"; string brazilianCharacters = u8"óculos"; - EXPECT_EQ(detail::encode_query_param(chineseCharacters), + EXPECT_EQ(httplib::encode_uri_component(chineseCharacters), "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); - EXPECT_EQ(detail::encode_query_param(russianCharacters), + EXPECT_EQ(httplib::encode_uri_component(russianCharacters), "%D0%B4%D0%BE%D0%BC"); - EXPECT_EQ(detail::encode_query_param(brazilianCharacters), "%C3%B3culos"); + EXPECT_EQ(httplib::encode_uri_component(brazilianCharacters), "%C3%B3culos"); +} + +TEST(EncodeUriComponentTest, ParseUnescapedChararactersTest) { + string unescapedCharacters = "-_.!~*'()"; + + EXPECT_EQ(httplib::encode_uri_component(unescapedCharacters), "-_.!~*'()"); +} + +TEST(EncodeUriComponentTest, ParseReservedCharactersTest) { + string reservedCharacters = ";,/?:@&=+$"; + + EXPECT_EQ(httplib::encode_uri_component(reservedCharacters), + "%3B%2C%2F%3F%3A%40%26%3D%2B%24"); +} + +TEST(EncodeUriComponentTest, TestUTF8Characters) { + string chineseCharacters = u8"中国語"; + string russianCharacters = u8"дом"; + string brazilianCharacters = u8"óculos"; + + EXPECT_EQ(httplib::encode_uri_component(chineseCharacters), + "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); + + EXPECT_EQ(httplib::encode_uri_component(russianCharacters), + "%D0%B4%D0%BE%D0%BC"); + + EXPECT_EQ(httplib::encode_uri_component(brazilianCharacters), "%C3%B3culos"); +} + +TEST(EncodeUriComponentTest, TestPathComponentEncoding) { + // Issue #2082 use case: encoding path component with ampersand + string pathWithAmpersand = "Piri Tommy Villiers - on & on"; + + EXPECT_EQ(httplib::encode_uri_component(pathWithAmpersand), + "Piri%20Tommy%20Villiers%20-%20on%20%26%20on"); +} + +TEST(EncodeUriTest, ParseUnescapedChararactersTest) { + string unescapedCharacters = "-_.!~*'()"; + + EXPECT_EQ(httplib::encode_uri(unescapedCharacters), "-_.!~*'()"); +} + +TEST(EncodeUriTest, ParseReservedCharactersTest) { + string reservedCharacters = ";,/?:@&=+$#"; + + EXPECT_EQ(httplib::encode_uri(reservedCharacters), ";,/?:@&=+$#"); +} + +TEST(EncodeUriTest, TestUTF8Characters) { + string chineseCharacters = u8"中国語"; + string russianCharacters = u8"дом"; + string brazilianCharacters = u8"óculos"; + + EXPECT_EQ(httplib::encode_uri(chineseCharacters), + "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); + + EXPECT_EQ(httplib::encode_uri(russianCharacters), "%D0%B4%D0%BE%D0%BC"); + + EXPECT_EQ(httplib::encode_uri(brazilianCharacters), "%C3%B3culos"); +} + +TEST(EncodeUriTest, TestCompleteUri) { + string uri = + "https://example.com/path/to/resource?query=value¶m=test#fragment"; + + EXPECT_EQ( + httplib::encode_uri(uri), + "https://example.com/path/to/resource?query=value¶m=test#fragment"); +} + +TEST(EncodeUriTest, TestUriWithSpacesAndSpecialChars) { + string uri = + "https://example.com/path with spaces/file name.html?q=hello world"; + + EXPECT_EQ(httplib::encode_uri(uri), + "https://example.com/path%20with%20spaces/" + "file%20name.html?q=hello%20world"); +} + +TEST(DecodeUriComponentTest, ParseEncodedChararactersTest) { + string encodedString = "%3B%2C%2F%3F%3A%40%26%3D%2B%24"; + + EXPECT_EQ(httplib::decode_uri_component(encodedString), ";,/?:@&=+$"); +} + +TEST(DecodeUriComponentTest, ParseUnescapedChararactersTest) { + string unescapedCharacters = "-_.!~*'()"; + + EXPECT_EQ(httplib::decode_uri_component(unescapedCharacters), "-_.!~*'()"); +} + +TEST(DecodeUriComponentTest, TestUTF8Characters) { + string encodedChinese = "%E4%B8%AD%E5%9B%BD%E8%AA%9E"; + string encodedRussian = "%D0%B4%D0%BE%D0%BC"; + string encodedBrazilian = "%C3%B3culos"; + + EXPECT_EQ(httplib::decode_uri_component(encodedChinese), u8"中国語"); + EXPECT_EQ(httplib::decode_uri_component(encodedRussian), u8"дом"); + EXPECT_EQ(httplib::decode_uri_component(encodedBrazilian), u8"óculos"); +} + +TEST(DecodeUriComponentTest, TestPathComponentDecoding) { + string encodedPath = "Piri%20Tommy%20Villiers%20-%20on%20%26%20on"; + + EXPECT_EQ(httplib::decode_uri_component(encodedPath), + "Piri Tommy Villiers - on & on"); +} + +TEST(DecodeUriTest, ParseEncodedChararactersTest) { + string encodedString = "%20%22%3C%3E%5C%5E%60%7B%7D%7C"; + + EXPECT_EQ(httplib::decode_uri(encodedString), " \"<>\\^`{}|"); +} + +TEST(DecodeUriTest, ParseUnescapedChararactersTest) { + string unescapedCharacters = "-_.!~*'();,/?:@&=+$#"; + + EXPECT_EQ(httplib::decode_uri(unescapedCharacters), "-_.!~*'();,/?:@&=+$#"); +} + +TEST(DecodeUriTest, TestUTF8Characters) { + string encodedChinese = "%E4%B8%AD%E5%9B%BD%E8%AA%9E"; + string encodedRussian = "%D0%B4%D0%BE%D0%BC"; + string encodedBrazilian = "%C3%B3culos"; + + EXPECT_EQ(httplib::decode_uri(encodedChinese), u8"中国語"); + EXPECT_EQ(httplib::decode_uri(encodedRussian), u8"дом"); + EXPECT_EQ(httplib::decode_uri(encodedBrazilian), u8"óculos"); +} + +TEST(DecodeUriTest, TestCompleteUri) { + string encodedUri = "https://example.com/path%20with%20spaces/" + "file%20name.html?q=hello%20world"; + + EXPECT_EQ( + httplib::decode_uri(encodedUri), + "https://example.com/path with spaces/file name.html?q=hello world"); +} + +TEST(DecodeUriTest, TestRoundTripWithEncodeUri) { + string original = + "https://example.com/path with spaces/file name.html?q=hello world"; + string encoded = httplib::encode_uri(original); + string decoded = httplib::decode_uri(encoded); + + EXPECT_EQ(decoded, original); +} + +TEST(DecodeUriComponentTest, TestRoundTripWithEncodeUriComponent) { + string original = "Piri Tommy Villiers - on & on"; + string encoded = httplib::encode_uri_component(original); + string decoded = httplib::decode_uri_component(encoded); + + EXPECT_EQ(decoded, original); } TEST(TrimTests, TrimStringTests) { @@ -2116,7 +2271,7 @@ TEST(PathUrlEncodeTest, PathUrlEncode) { { Client cli(HOST, PORT); - cli.set_url_encode(false); + cli.set_path_encode(false); auto res = cli.Get("/foo?a=explicitly+encoded"); ASSERT_TRUE(res); @@ -2146,7 +2301,7 @@ TEST(PathUrlEncodeTest, IncludePercentEncodingLF) { { Client cli(HOST, PORT); - cli.set_url_encode(false); + cli.set_path_encode(false); auto res = cli.Get("/?something=%0A"); ASSERT_TRUE(res); From af733776118151b3a5c871fb7adfcfda352c1579 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 6 Jul 2025 21:27:24 -0400 Subject: [PATCH 0467/1222] Fix #1578 (#2171) * Fix #1578 * Update README * Update * Update * Update * Update * Update * Update --- README.md | 119 +++++++-- example/simplesvr.cc | 21 +- example/upload.cc | 4 +- httplib.h | 380 ++++++++++++++++------------- test/test.cc | 569 ++++++++++++++++++++++--------------------- 5 files changed, 624 insertions(+), 469 deletions(-) diff --git a/README.md b/README.md index 4f3c720578..66348cc52b 100644 --- a/README.md +++ b/README.md @@ -99,28 +99,28 @@ auto res = cli.Get("/"); if (!res) { // Check the error type auto err = res.error(); - + switch (err) { case httplib::Error::SSLConnection: - std::cout << "SSL connection failed, SSL error: " + std::cout << "SSL connection failed, SSL error: " << res->ssl_error() << std::endl; break; case httplib::Error::SSLLoadingCerts: - std::cout << "SSL cert loading failed, OpenSSL error: " + std::cout << "SSL cert loading failed, OpenSSL error: " << std::hex << res->ssl_openssl_error() << std::endl; break; - + case httplib::Error::SSLServerVerification: - std::cout << "SSL verification failed, X509 error: " + std::cout << "SSL verification failed, X509 error: " << res->ssl_openssl_error() << std::endl; break; - + case httplib::Error::SSLServerHostnameVerification: - std::cout << "SSL hostname verification failed, X509 error: " + std::cout << "SSL hostname verification failed, X509 error: " << res->ssl_openssl_error() << std::endl; break; - + default: std::cout << "HTTP error: " << httplib::to_string(err) << std::endl; } @@ -356,16 +356,80 @@ svr.set_pre_request_handler([](const auto& req, auto& res) { }); ``` -### 'multipart/form-data' POST data +### Form data handling + +#### URL-encoded form data ('application/x-www-form-urlencoded') + +```cpp +svr.Post("/form", [&](const auto& req, auto& res) { + // URL query parameters and form-encoded data are accessible via req.params + std::string username = req.get_param_value("username"); + std::string password = req.get_param_value("password"); + + // Handle multiple values with same name + auto interests = req.get_param_values("interests"); + + // Check existence + if (req.has_param("newsletter")) { + // Handle newsletter subscription + } +}); +``` + +#### 'multipart/form-data' POST data ```cpp -svr.Post("/multipart", [&](const auto& req, auto& res) { - auto size = req.files.size(); - auto ret = req.has_file("name1"); - const auto& file = req.get_file_value("name1"); - // file.filename; - // file.content_type; - // file.content; +svr.Post("/multipart", [&](const Request& req, Response& res) { + // Access text fields (from form inputs without files) + std::string username = req.form.get_field("username"); + std::string bio = req.form.get_field("bio"); + + // Access uploaded files + if (req.form.has_file("avatar")) { + const auto& file = req.form.get_file("avatar"); + std::cout << "Uploaded file: " << file.filename + << " (" << file.content_type << ") - " + << file.content.size() << " bytes" << std::endl; + + // Access additional headers if needed + for (const auto& header : file.headers) { + std::cout << "Header: " << header.first << " = " << header.second << std::endl; + } + + // Save to disk + std::ofstream ofs(file.filename, std::ios::binary); + ofs << file.content; + } + + // Handle multiple values with same name + auto tags = req.form.get_fields("tags"); // e.g., multiple checkboxes + for (const auto& tag : tags) { + std::cout << "Tag: " << tag << std::endl; + } + + auto documents = req.form.get_files("documents"); // multiple file upload + for (const auto& doc : documents) { + std::cout << "Document: " << doc.filename + << " (" << doc.content.size() << " bytes)" << std::endl; + } + + // Check existence before accessing + if (req.form.has_field("newsletter")) { + std::cout << "Newsletter subscription: " << req.form.get_field("newsletter") << std::endl; + } + + // Get counts for validation + if (req.form.get_field_count("tags") > 5) { + res.status = StatusCode::BadRequest_400; + res.set_content("Too many tags", "text/plain"); + return; + } + + // Summary + std::cout << "Received " << req.form.fields.size() << " text fields and " + << req.form.files.size() << " files" << std::endl; + + res.set_content("Upload successful", "text/plain"); }); ``` @@ -376,16 +440,29 @@ svr.Post("/content_receiver", [&](const Request &req, Response &res, const ContentReader &content_reader) { if (req.is_multipart_form_data()) { // NOTE: `content_reader` is blocking until every form data field is read - MultipartFormDataItems files; + // This approach allows streaming processing of large files + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &item) { + items.push_back(item); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); + + // Process the received items + for (const auto& item : items) { + if (item.filename.empty()) { + // Text field + std::cout << "Field: " << item.name << " = " << item.content << std::endl; + } else { + // File + std::cout << "File: " << item.name << " (" << item.filename << ") - " + << item.content.size() << " bytes" << std::endl; + } + } } else { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -691,7 +768,7 @@ auto res = cli.Post("/post", params); ### POST with Multipart Form Data ```c++ -httplib::MultipartFormDataItems items = { +httplib::UploadFormDataItems items = { { "text1", "text default", "", "" }, { "text2", "aωb", "", "" }, { "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" }, diff --git a/example/simplesvr.cc b/example/simplesvr.cc index 17a9e98597..e45e360646 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -27,13 +27,26 @@ string dump_headers(const Headers &headers) { return s; } -string dump_multipart_files(const MultipartFormDataMap &files) { +string dump_multipart_formdata(const MultipartFormData &form) { string s; char buf[BUFSIZ]; s += "--------------------------------\n"; - for (const auto &x : files) { + for (const auto &x : form.fields) { + const auto &name = x.first; + const auto &field = x.second; + + snprintf(buf, sizeof(buf), "name: %s\n", name.c_str()); + s += buf; + + snprintf(buf, sizeof(buf), "text length: %zu\n", field.content.size()); + s += buf; + + s += "----------------\n"; + } + + for (const auto &x : form.files) { const auto &name = x.first; const auto &file = x.second; @@ -77,7 +90,7 @@ string log(const Request &req, const Response &res) { s += buf; s += dump_headers(req.headers); - s += dump_multipart_files(req.files); + s += dump_multipart_formdata(req.form); s += "--------------------------------\n"; @@ -101,7 +114,7 @@ int main(int argc, const char **argv) { #endif svr.Post("/multipart", [](const Request &req, Response &res) { - auto body = dump_headers(req.headers) + dump_multipart_files(req.files); + auto body = dump_headers(req.headers) + dump_multipart_formdata(req.form); res.set_content(body, "text/plain"); }); diff --git a/example/upload.cc b/example/upload.cc index 1e4f242afa..f7a822bedc 100644 --- a/example/upload.cc +++ b/example/upload.cc @@ -37,8 +37,8 @@ int main(void) { }); svr.Post("/post", [](const Request &req, Response &res) { - auto image_file = req.get_file_value("image_file"); - auto text_file = req.get_file_value("text_file"); + const auto &image_file = req.form.get_file("image_file"); + const auto &text_file = req.form.get_file("text_file"); cout << "image file length: " << image_file.content.length() << endl << "image file name: " << image_file.filename << endl diff --git a/httplib.h b/httplib.h index 4648ce7bec..ec8349e109 100644 --- a/httplib.h +++ b/httplib.h @@ -561,24 +561,47 @@ using UploadProgress = std::function; struct Response; using ResponseHandler = std::function; -struct MultipartFormData { +struct FormData { std::string name; std::string content; std::string filename; std::string content_type; Headers headers; }; -using MultipartFormDataItems = std::vector; -using MultipartFormDataMap = std::multimap; -struct MultipartFormDataForClientInput { +struct FormField { + std::string name; + std::string content; + Headers headers; +}; +using FormFields = std::multimap; + +using FormFiles = std::multimap; + +struct MultipartFormData { + FormFields fields; // Text fields from multipart + FormFiles files; // Files from multipart + + // Text field access + std::string get_field(const std::string &key, size_t id = 0) const; + std::vector get_fields(const std::string &key) const; + bool has_field(const std::string &key) const; + size_t get_field_count(const std::string &key) const; + + // File access + FormData get_file(const std::string &key, size_t id = 0) const; + std::vector get_files(const std::string &key) const; + bool has_file(const std::string &key) const; + size_t get_file_count(const std::string &key) const; +}; + +struct UploadFormData { std::string name; std::string content; std::string filename; std::string content_type; }; -using MultipartFormDataItemsForClientInput = - std::vector; +using UploadFormDataItems = std::vector; class DataSink { public: @@ -621,13 +644,13 @@ using ContentProviderWithoutLength = using ContentProviderResourceReleaser = std::function; -struct MultipartFormDataProvider { +struct FormDataProvider { std::string name; ContentProviderWithoutLength provider; std::string filename; std::string content_type; }; -using MultipartFormDataProviderItems = std::vector; +using FormDataProviderItems = std::vector; using ContentReceiverWithProgress = std::function; -using MultipartContentHeader = - std::function; +using FormDataHeader = std::function; class ContentReader { public: using Reader = std::function; - using MultipartReader = std::function; + using FormDataReader = + std::function; - ContentReader(Reader reader, MultipartReader multipart_reader) + ContentReader(Reader reader, FormDataReader multipart_reader) : reader_(std::move(reader)), - multipart_reader_(std::move(multipart_reader)) {} + formdata_reader_(std::move(multipart_reader)) {} - bool operator()(MultipartContentHeader header, - ContentReceiver receiver) const { - return multipart_reader_(std::move(header), std::move(receiver)); + bool operator()(FormDataHeader header, ContentReceiver receiver) const { + return formdata_reader_(std::move(header), std::move(receiver)); } bool operator()(ContentReceiver receiver) const { @@ -659,7 +680,7 @@ class ContentReader { } Reader reader_; - MultipartReader multipart_reader_; + FormDataReader formdata_reader_; }; using Range = std::pair; @@ -681,7 +702,7 @@ struct Request { // for server std::string version; std::string target; - MultipartFormDataMap files; + MultipartFormData form; Ranges ranges; Match matches; std::unordered_map path_params; @@ -711,10 +732,6 @@ struct Request { bool is_multipart_form_data() const; - bool has_file(const std::string &key) const; - MultipartFormData get_file_value(const std::string &key) const; - std::vector get_file_values(const std::string &key) const; - // private members... size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; size_t content_length_ = 0; @@ -1159,14 +1176,14 @@ class Server { Response &res, const std::string &boundary, const std::string &content_type); bool read_content(Stream &strm, Request &req, Response &res); - bool - read_content_with_content_receiver(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver); + bool read_content_with_content_receiver(Stream &strm, Request &req, + Response &res, + ContentReceiver receiver, + FormDataHeader multipart_header, + ContentReceiver multipart_receiver); bool read_content_core(Stream &strm, Request &req, Response &res, ContentReceiver receiver, - MultipartContentHeader multipart_header, + FormDataHeader multipart_header, ContentReceiver multipart_receiver) const; virtual bool process_and_close_socket(socket_t sock); @@ -1333,16 +1350,16 @@ class ClientImpl { Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); - Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Put(const std::string &path); @@ -1351,16 +1368,16 @@ class ClientImpl { Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); - Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Patch(const std::string &path); @@ -1369,16 +1386,16 @@ class ClientImpl { Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Params ¶ms); - Result Patch(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); - Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); - Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Delete(const std::string &path, DownloadProgress progress = nullptr); @@ -1628,9 +1645,8 @@ class ClientImpl { ContentProviderWithoutLength content_provider_without_length, const std::string &content_type, UploadProgress progress); ContentProviderWithoutLength get_multipart_content_provider( - const std::string &boundary, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items) const; + const std::string &boundary, const UploadFormDataItems &items, + const FormDataProviderItems &provider_items) const; std::string adjust_host_string(const std::string &host) const; @@ -1684,16 +1700,16 @@ class Client { Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); - Result Post(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Put(const std::string &path); @@ -1702,16 +1718,16 @@ class Client { Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); - Result Put(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Patch(const std::string &path); @@ -1720,16 +1736,16 @@ class Client { Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Params ¶ms); - Result Patch(const std::string &path, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); - Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, UploadProgress progress = nullptr); - Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const std::string &boundary, UploadProgress progress = nullptr); - Result Patch(const std::string &path, const Headers &headers, const MultipartFormDataItemsForClientInput &items, const MultipartFormDataProviderItems &provider_items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const FormDataProviderItems &provider_items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Delete(const std::string &path, DownloadProgress progress = nullptr); @@ -5327,9 +5343,9 @@ inline bool parse_accept_header(const std::string &s, return true; } -class MultipartFormDataParser { +class FormDataParser { public: - MultipartFormDataParser() = default; + FormDataParser() = default; void set_boundary(std::string &&boundary) { boundary_ = boundary; @@ -5339,8 +5355,8 @@ class MultipartFormDataParser { bool is_valid() const { return is_valid_; } - bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, - const MultipartContentHeader &header_callback) { + bool parse(const char *buf, size_t n, const FormDataHeader &header_callback, + const ContentReceiver &content_callback) { buf_append(buf, n); @@ -5381,7 +5397,7 @@ class MultipartFormDataParser { return false; } - // parse and emplace space trimmed headers into a map + // Parse and emplace space trimmed headers into a map if (!parse_header( header.data(), header.data() + header.size(), [&](const std::string &key, const std::string &val) { @@ -5512,7 +5528,7 @@ class MultipartFormDataParser { size_t state_ = 0; bool is_valid_ = false; - MultipartFormData file_; + FormData file_; // Buffer bool start_with(const std::string &a, size_t spos, size_t epos, @@ -5650,7 +5666,7 @@ serialize_multipart_formdata_get_content_type(const std::string &boundary) { } inline std::string -serialize_multipart_formdata(const MultipartFormDataItemsForClientInput &items, +serialize_multipart_formdata(const UploadFormDataItems &items, const std::string &boundary, bool finish = true) { std::string body; @@ -6422,19 +6438,47 @@ inline bool Request::is_multipart_form_data() const { return !content_type.rfind("multipart/form-data", 0); } -inline bool Request::has_file(const std::string &key) const { - return files.find(key) != files.end(); +// Multipart FormData implementation +inline std::string MultipartFormData::get_field(const std::string &key, + size_t id) const { + auto rng = fields.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.content; } + return std::string(); } -inline MultipartFormData Request::get_file_value(const std::string &key) const { - auto it = files.find(key); - if (it != files.end()) { return it->second; } - return MultipartFormData(); +inline std::vector +MultipartFormData::get_fields(const std::string &key) const { + std::vector values; + auto rng = fields.equal_range(key); + for (auto it = rng.first; it != rng.second; it++) { + values.push_back(it->second.content); + } + return values; +} + +inline bool MultipartFormData::has_field(const std::string &key) const { + return fields.find(key) != fields.end(); } -inline std::vector -Request::get_file_values(const std::string &key) const { - std::vector values; +inline size_t MultipartFormData::get_field_count(const std::string &key) const { + auto r = fields.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + +inline FormData MultipartFormData::get_file(const std::string &key, + size_t id) const { + auto rng = files.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return FormData(); +} + +inline std::vector +MultipartFormData::get_files(const std::string &key) const { + std::vector values; auto rng = files.equal_range(key); for (auto it = rng.first; it != rng.second; it++) { values.push_back(it->second); @@ -6442,6 +6486,15 @@ Request::get_file_values(const std::string &key) const { return values; } +inline bool MultipartFormData::has_file(const std::string &key) const { + return files.find(key) != files.end(); +} + +inline size_t MultipartFormData::get_file_count(const std::string &key) const { + auto r = files.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + // Response implementation inline bool Response::has_header(const std::string &key) const { return headers.find(key) != headers.end(); @@ -7300,8 +7353,10 @@ Server::write_content_with_provider(Stream &strm, const Request &req, } inline bool Server::read_content(Stream &strm, Request &req, Response &res) { - MultipartFormDataMap::iterator cur; - auto file_count = 0; + FormFields::iterator cur_field; + FormFiles::iterator cur_file; + auto is_text_field = false; + size_t count = 0; if (read_content_core( strm, req, res, // Regular @@ -7310,18 +7365,32 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { req.body.append(buf, n); return true; }, - // Multipart - [&](const MultipartFormData &file) { - if (file_count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { + // Multipart FormData + [&](const FormData &file) { + if (count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { return false; } - cur = req.files.emplace(file.name, file); + + if (file.filename.empty()) { + cur_field = req.form.fields.emplace( + file.name, FormField{file.name, file.content, file.headers}); + is_text_field = true; + } else { + cur_file = req.form.files.emplace(file.name, file); + is_text_field = false; + } return true; }, [&](const char *buf, size_t n) { - auto &content = cur->second.content; - if (content.size() + n > content.max_size()) { return false; } - content.append(buf, n); + if (is_text_field) { + auto &content = cur_field->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + } else { + auto &content = cur_file->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + } return true; })) { const auto &content_type = req.get_header_value("Content-Type"); @@ -7339,19 +7408,16 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { inline bool Server::read_content_with_content_receiver( Stream &strm, Request &req, Response &res, ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) { + FormDataHeader multipart_header, ContentReceiver multipart_receiver) { return read_content_core(strm, req, res, std::move(receiver), std::move(multipart_header), std::move(multipart_receiver)); } -inline bool -Server::read_content_core(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver) const { - detail::MultipartFormDataParser multipart_form_data_parser; +inline bool Server::read_content_core( + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + FormDataHeader multipart_header, ContentReceiver multipart_receiver) const { + detail::FormDataParser multipart_form_data_parser; ContentReceiverWithProgress out; if (req.is_multipart_form_data()) { @@ -7364,19 +7430,8 @@ Server::read_content_core(Stream &strm, Request &req, Response &res, multipart_form_data_parser.set_boundary(std::move(boundary)); out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { - /* For debug - size_t pos = 0; - while (pos < n) { - auto read_size = (std::min)(1, n - pos); - auto ret = multipart_form_data_parser.parse( - buf + pos, read_size, multipart_receiver, multipart_header); - if (!ret) { return false; } - pos += read_size; - } - return true; - */ - return multipart_form_data_parser.parse(buf, n, multipart_receiver, - multipart_header); + return multipart_form_data_parser.parse(buf, n, multipart_header, + multipart_receiver); }; } else { out = [receiver](const char *buf, size_t n, uint64_t /*off*/, @@ -7582,7 +7637,7 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { return read_content_with_content_receiver( strm, req, res, std::move(receiver), nullptr, nullptr); }, - [&](MultipartContentHeader header, ContentReceiver receiver) { + [&](FormDataHeader header, ContentReceiver receiver) { return read_content_with_content_receiver(strm, req, res, nullptr, std::move(header), std::move(receiver)); @@ -7731,7 +7786,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (type != detail::EncodingType::None) { if (pre_compression_logger_) { pre_compression_logger_(req, res); } - + std::unique_ptr compressor; std::string content_encoding; @@ -8949,9 +9004,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( - const std::string &boundary, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items) const { + const std::string &boundary, const UploadFormDataItems &items, + const FormDataProviderItems &provider_items) const { size_t cur_item = 0; size_t cur_start = 0; // cur_item and cur_start are copied to within the std::function and maintain @@ -9164,17 +9218,15 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, return Post(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result -ClientImpl::Post(const std::string &path, - const MultipartFormDataItemsForClientInput &items, - UploadProgress progress) { +inline Result ClientImpl::Post(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { return Post(path, Headers(), items, progress); } -inline Result -ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - UploadProgress progress) { +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -9182,10 +9234,10 @@ ClientImpl::Post(const std::string &path, const Headers &headers, return Post(path, headers, body, content_type, progress); } -inline Result -ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const std::string &boundary, UploadProgress progress) { +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } @@ -9232,11 +9284,10 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, progress); } -inline Result -ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items, - UploadProgress progress) { +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -9320,13 +9371,13 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, } inline Result ClientImpl::Put(const std::string &path, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return Put(path, Headers(), items, progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = @@ -9336,7 +9387,7 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { @@ -9385,11 +9436,10 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, progress); } -inline Result -ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items, - UploadProgress progress) { +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -9474,17 +9524,15 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, return Patch(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result -ClientImpl::Patch(const std::string &path, - const MultipartFormDataItemsForClientInput &items, - UploadProgress progress) { +inline Result ClientImpl::Patch(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { return Patch(path, Headers(), items, progress); } -inline Result -ClientImpl::Patch(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - UploadProgress progress) { +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -9492,10 +9540,10 @@ ClientImpl::Patch(const std::string &path, const Headers &headers, return Patch(path, headers, body, content_type, progress); } -inline Result -ClientImpl::Patch(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const std::string &boundary, UploadProgress progress) { +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } @@ -9543,11 +9591,10 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, progress); } -inline Result -ClientImpl::Patch(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items, - UploadProgress progress) { +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); @@ -10880,24 +10927,24 @@ inline Result Client::Post(const std::string &path, const Headers &headers, return cli_->Post(path, headers, params); } inline Result Client::Post(const std::string &path, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return cli_->Post(path, items, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return cli_->Post(path, headers, items, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress) { return cli_->Post(path, headers, items, boundary, progress); } inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, UploadProgress progress) { return cli_->Post(path, headers, items, provider_items, progress); } @@ -10973,24 +11020,24 @@ inline Result Client::Put(const std::string &path, const Headers &headers, return cli_->Put(path, headers, params); } inline Result Client::Put(const std::string &path, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return cli_->Put(path, items, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return cli_->Put(path, headers, items, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress) { return cli_->Put(path, headers, items, boundary, progress); } inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, UploadProgress progress) { return cli_->Put(path, headers, items, provider_items, progress); } @@ -11069,26 +11116,25 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, return cli_->Patch(path, headers, params); } inline Result Client::Patch(const std::string &path, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return cli_->Patch(path, items, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, UploadProgress progress) { return cli_->Patch(path, headers, items, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, + const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress) { return cli_->Patch(path, headers, items, boundary, progress); } -inline Result -Client::Patch(const std::string &path, const Headers &headers, - const MultipartFormDataItemsForClientInput &items, - const MultipartFormDataProviderItems &provider_items, - UploadProgress progress) { +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { return cli_->Patch(path, headers, items, provider_items, progress); } inline Result Client::Patch(const std::string &path, const Headers &headers, diff --git a/test/test.cc b/test/test.cc index 8d07eb8990..8e8299b76f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -55,15 +55,14 @@ const std::string JSON_DATA = "{\"hello\":\"world\"}"; const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB -MultipartFormData &get_file_value(MultipartFormDataItems &files, - const char *key) { - auto it = std::find_if( - files.begin(), files.end(), - [&](const MultipartFormData &file) { return file.name == key; }); +FormData &get_file_value(std::vector &items, const char *key) { + auto it = std::find_if(items.begin(), items.end(), [&](const FormData &file) { + return file.name == key; + }); #ifdef CPPHTTPLIB_NO_EXCEPTIONS return *it; #else - if (it != files.end()) { return *it; } + if (it != items.end()) { return *it; } throw std::runtime_error("invalid multipart form data name error"); #endif } @@ -3176,65 +3175,72 @@ class ServerTest : public ::testing::Test { }) .Post("/multipart", [&](const Request &req, Response & /*res*/) { - EXPECT_EQ(6u, req.files.size()); - ASSERT_TRUE(!req.has_file("???")); + EXPECT_EQ(4u, req.form.get_field_count("text1") + + req.form.get_field_count("text2") + + req.form.get_field_count("file3") + + req.form.get_field_count("file4")); + EXPECT_EQ(2u, req.form.get_file_count("file1") + + req.form.get_file_count("file2")); + ASSERT_TRUE(!req.form.has_file("???")); + ASSERT_TRUE(!req.form.has_field("???")); ASSERT_TRUE(req.body.empty()); { - const auto &file = req.get_file_value("text1"); - EXPECT_TRUE(file.filename.empty()); - EXPECT_EQ("text default", file.content); + const auto &text = req.form.get_field("text1"); + EXPECT_EQ("text default", text); } { - const auto &file = req.get_file_value("text2"); - EXPECT_TRUE(file.filename.empty()); - EXPECT_EQ("aωb", file.content); + const auto &text = req.form.get_field("text2"); + EXPECT_EQ("aωb", text); } { - const auto &file = req.get_file_value("file1"); + const auto &file = req.form.get_file("file1"); EXPECT_EQ("hello.txt", file.filename); EXPECT_EQ("text/plain", file.content_type); EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content); } { - const auto &file = req.get_file_value("file3"); - EXPECT_TRUE(file.filename.empty()); - EXPECT_EQ("application/octet-stream", file.content_type); - EXPECT_EQ(0u, file.content.size()); + const auto &file = req.form.get_file("file2"); + EXPECT_EQ("world.json", file.filename); + EXPECT_EQ("application/json", file.content_type); + EXPECT_EQ("{\n \"world\", true\n}\n", file.content); + } + + { + const auto &text = req.form.get_field("file3"); + EXPECT_EQ(0u, text.size()); } { - const auto &file = req.get_file_value("file4"); - EXPECT_TRUE(file.filename.empty()); - EXPECT_EQ(0u, file.content.size()); - EXPECT_EQ("application/json tmp-string", file.content_type); + const auto &text = req.form.get_field("file4"); + EXPECT_EQ(0u, text.size()); } }) .Post("/multipart/multi_file_values", [&](const Request &req, Response & /*res*/) { - EXPECT_EQ(5u, req.files.size()); - ASSERT_TRUE(!req.has_file("???")); + EXPECT_EQ(3u, req.form.get_field_count("text") + + req.form.get_field_count("multi_text1")); + EXPECT_EQ(2u, req.form.get_file_count("multi_file1")); + ASSERT_TRUE(!req.form.has_file("???")); + ASSERT_TRUE(!req.form.has_field("???")); ASSERT_TRUE(req.body.empty()); { - const auto &text_value = req.get_file_values("text"); - EXPECT_EQ(1u, text_value.size()); - auto &text = text_value[0]; - EXPECT_TRUE(text.filename.empty()); - EXPECT_EQ("default text", text.content); + const auto &text = req.form.get_field("text"); + EXPECT_EQ("default text", text); } { - const auto &text1_values = req.get_file_values("multi_text1"); + const auto &text1_values = req.form.get_fields("multi_text1"); EXPECT_EQ(2u, text1_values.size()); - EXPECT_EQ("aaaaa", text1_values[0].content); - EXPECT_EQ("bbbbb", text1_values[1].content); + EXPECT_EQ("aaaaa", text1_values[0]); + EXPECT_EQ("bbbbb", text1_values[1]); } { - const auto &file1_values = req.get_file_values("multi_file1"); + const auto &file1_values = req.form.get_files("multi_file1"); EXPECT_EQ(2u, file1_values.size()); auto file1 = file1_values[0]; EXPECT_EQ(file1.filename, "hello.txt"); @@ -3349,40 +3355,47 @@ class ServerTest : public ::testing::Test { [&](const Request &req, Response &res, const ContentReader &content_reader) { if (req.is_multipart_form_data()) { - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - EXPECT_EQ(5u, files.size()); + EXPECT_EQ(5u, items.size()); { - const auto &file = get_file_value(files, "text1"); + const auto &file = get_file_value(items, "text1"); EXPECT_TRUE(file.filename.empty()); EXPECT_EQ("text default", file.content); } { - const auto &file = get_file_value(files, "text2"); + const auto &file = get_file_value(items, "text2"); EXPECT_TRUE(file.filename.empty()); EXPECT_EQ("aωb", file.content); } { - const auto &file = get_file_value(files, "file1"); + const auto &file = get_file_value(items, "file1"); EXPECT_EQ("hello.txt", file.filename); EXPECT_EQ("text/plain", file.content_type); EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content); } { - const auto &file = get_file_value(files, "file3"); + const auto &file = get_file_value(items, "file2"); + EXPECT_EQ("world.json", file.filename); + EXPECT_EQ("application/json", file.content_type); + EXPECT_EQ(R"({\n "world": true\n}\n)", file.content); + } + + { + const auto &file = get_file_value(items, "file3"); EXPECT_TRUE(file.filename.empty()); EXPECT_EQ("application/octet-stream", file.content_type); EXPECT_EQ(0u, file.content.size()); @@ -3496,19 +3509,17 @@ class ServerTest : public ::testing::Test { }) .Post("/compress-multipart", [&](const Request &req, Response & /*res*/) { - EXPECT_EQ(2u, req.files.size()); - ASSERT_TRUE(!req.has_file("???")); + EXPECT_EQ(2u, req.form.fields.size()); + ASSERT_TRUE(!req.form.has_field("???")); { - const auto &file = req.get_file_value("key1"); - EXPECT_TRUE(file.filename.empty()); - EXPECT_EQ("test", file.content); + const auto &text = req.form.get_field("key1"); + EXPECT_EQ("test", text); } { - const auto &file = req.get_file_value("key2"); - EXPECT_TRUE(file.filename.empty()); - EXPECT_EQ("--abcdefg123", file.content); + const auto &text = req.form.get_field("key2"); + EXPECT_EQ("--abcdefg123", text); } }) #endif @@ -4431,7 +4442,7 @@ TEST_F(ServerTest, HeaderCountSecurityTest) { } TEST_F(ServerTest, MultipartFormData) { - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, @@ -4446,7 +4457,7 @@ TEST_F(ServerTest, MultipartFormData) { } TEST_F(ServerTest, MultipartFormDataMultiFileValues) { - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"text", "default text", "", ""}, {"multi_text1", "aaaaa", "", ""}, @@ -5386,11 +5397,11 @@ TEST_F(ServerTest, PostContentReceiver) { } TEST_F(ServerTest, PostMultipartFileContentReceiver) { - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, - {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"}, + {"file2", R"({\n "world": true\n}\n)", "world.json", "application/json"}, {"file3", "", "", "application/octet-stream"}, }; @@ -5401,11 +5412,11 @@ TEST_F(ServerTest, PostMultipartFileContentReceiver) { } TEST_F(ServerTest, PostMultipartPlusBoundary) { - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"text1", "text default", "", ""}, {"text2", "aωb", "", ""}, {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"}, - {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"}, + {"file2", R"({\n "world": true\n}\n)", "world.json", "application/json"}, {"file3", "", "", "application/octet-stream"}, }; @@ -5860,7 +5871,7 @@ TEST_F(ServerTest, NoGzipWithContentReceiver) { } TEST_F(ServerTest, MultipartFormDataGzip) { - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"key1", "test", "", ""}, {"key2", "--abcdefg123", "", ""}, }; @@ -6024,7 +6035,7 @@ TEST_F(ServerTest, NoZstdWithContentReceiver) { // TODO: How to enable zstd ?? TEST_F(ServerTest, MultipartFormDataZstd) { - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"key1", "test", "", ""}, {"key2", "--abcdefg123", "", ""}, }; @@ -6059,135 +6070,153 @@ TEST_F(ServerTest, PutWithContentProviderWithZstd) { // Pre-compression logging tests TEST_F(ServerTest, PreCompressionLogging) { // Test data for compression (matches the actual /compress endpoint content) - const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - + const std::string test_content = + "123456789012345678901234567890123456789012345678901234567890123456789012" + "3456789012345678901234567890"; + // Variables to capture logging data std::string pre_compression_body; std::string pre_compression_content_type; std::string pre_compression_content_encoding; - + std::string post_compression_body; std::string post_compression_content_type; std::string post_compression_content_encoding; - + // Set up pre-compression logger - svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { + svr_.set_pre_compression_logger([&](const Request & /*req*/, + const Response &res) { pre_compression_body = res.body; pre_compression_content_type = res.get_header_value("Content-Type"); pre_compression_content_encoding = res.get_header_value("Content-Encoding"); }); - + // Set up post-compression logger - svr_.set_logger([&](const Request &req, const Response &res) { + svr_.set_logger([&](const Request & /*req*/, const Response &res) { post_compression_body = res.body; post_compression_content_type = res.get_header_value("Content-Type"); - post_compression_content_encoding = res.get_header_value("Content-Encoding"); + post_compression_content_encoding = + res.get_header_value("Content-Encoding"); }); - + // Test with gzip compression Headers headers; headers.emplace("Accept-Encoding", "gzip"); - + auto res = cli_.Get("/compress", headers); - + // Verify response was compressed ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); - + // Verify pre-compression logger captured uncompressed content EXPECT_EQ(test_content, pre_compression_body); EXPECT_EQ("text/plain", pre_compression_content_type); - EXPECT_TRUE(pre_compression_content_encoding.empty()); // No encoding header before compression - + EXPECT_TRUE(pre_compression_content_encoding + .empty()); // No encoding header before compression + // Verify post-compression logger captured compressed content - EXPECT_NE(test_content, post_compression_body); // Should be different after compression + EXPECT_NE(test_content, + post_compression_body); // Should be different after compression EXPECT_EQ("text/plain", post_compression_content_type); EXPECT_EQ("gzip", post_compression_content_encoding); - + // Verify compressed content is smaller EXPECT_LT(post_compression_body.size(), pre_compression_body.size()); } TEST_F(ServerTest, PreCompressionLoggingWithBrotli) { - const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - + const std::string test_content = + "123456789012345678901234567890123456789012345678901234567890123456789012" + "3456789012345678901234567890"; + std::string pre_compression_body; std::string post_compression_body; - - svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { - pre_compression_body = res.body; - }); - - svr_.set_logger([&](const Request &req, const Response &res) { + + svr_.set_pre_compression_logger( + [&](const Request & /*req*/, const Response &res) { + pre_compression_body = res.body; + }); + + svr_.set_logger([&](const Request & /*req*/, const Response &res) { post_compression_body = res.body; }); - + Headers headers; headers.emplace("Accept-Encoding", "br"); - + auto res = cli_.Get("/compress", headers); - + ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("br", res->get_header_value("Content-Encoding")); - + // Verify pre-compression content is uncompressed EXPECT_EQ(test_content, pre_compression_body); - + // Verify post-compression content is compressed EXPECT_NE(test_content, post_compression_body); EXPECT_LT(post_compression_body.size(), pre_compression_body.size()); } TEST_F(ServerTest, PreCompressionLoggingWithoutCompression) { - const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - + const std::string test_content = + "123456789012345678901234567890123456789012345678901234567890123456789012" + "3456789012345678901234567890"; + std::string pre_compression_body; std::string post_compression_body; - - svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { - pre_compression_body = res.body; - }); - - svr_.set_logger([&](const Request &req, const Response &res) { + + svr_.set_pre_compression_logger( + [&](const Request & /*req*/, const Response &res) { + pre_compression_body = res.body; + }); + + svr_.set_logger([&](const Request & /*req*/, const Response &res) { post_compression_body = res.body; }); - + // Request without compression (use /nocompress endpoint) Headers headers; auto res = cli_.Get("/nocompress", headers); - + ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_TRUE(res->get_header_value("Content-Encoding").empty()); - + // Pre-compression logger should not be called when no compression is applied - EXPECT_TRUE(pre_compression_body.empty()); // Pre-compression logger not called - EXPECT_EQ(test_content, post_compression_body); // Post-compression logger captures final content + EXPECT_TRUE( + pre_compression_body.empty()); // Pre-compression logger not called + EXPECT_EQ( + test_content, + post_compression_body); // Post-compression logger captures final content } TEST_F(ServerTest, PreCompressionLoggingOnlyPreLogger) { - const std::string test_content = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - + const std::string test_content = + "123456789012345678901234567890123456789012345678901234567890123456789012" + "3456789012345678901234567890"; + std::string pre_compression_body; bool pre_logger_called = false; - + // Set only pre-compression logger - svr_.set_pre_compression_logger([&](const Request &req, const Response &res) { - pre_compression_body = res.body; - pre_logger_called = true; - }); - + svr_.set_pre_compression_logger( + [&](const Request & /*req*/, const Response &res) { + pre_compression_body = res.body; + pre_logger_called = true; + }); + Headers headers; headers.emplace("Accept-Encoding", "gzip"); - + auto res = cli_.Get("/compress", headers); - + ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("gzip", res->get_header_value("Content-Encoding")); - + // Verify pre-compression logger was called EXPECT_TRUE(pre_logger_called); EXPECT_EQ(test_content, pre_compression_body); @@ -6767,7 +6796,7 @@ void TestMultipartUploadProgress(SetupHandler &&setup_handler, Client cli(HOST, PORT); vector progress_values; - MultipartFormDataItemsForClientInput items = { + UploadFormDataItems items = { {"field1", "value1", "", ""}, {"field2", "longer value for progress tracking test", "", ""}, {"file1", "file content data for upload progress", "test.txt", @@ -6788,12 +6817,11 @@ TEST(UploadProgressTest, PostMultipartProgress) { TestMultipartUploadProgress( [](Server &svr) { svr.Post("/multipart", [](const Request &req, Response &res) { - EXPECT_FALSE(req.files.empty()); + EXPECT_TRUE(!req.form.files.empty() || !req.form.fields.empty()); res.set_content("multipart received", "text/plain"); }); }, - [](Client &cli, const string &endpoint, - const MultipartFormDataItemsForClientInput &items, + [](Client &cli, const string &endpoint, const UploadFormDataItems &items, UploadProgress progress_callback) { return cli.Post(endpoint, items, progress_callback); }, @@ -8631,26 +8659,26 @@ TEST(MultipartFormDataTest, LargeData) { svr.Post("/post", [&](const Request &req, Response & /*res*/, const ContentReader &content_reader) { if (req.is_multipart_form_data()) { - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - EXPECT_TRUE(std::string(files[0].name) == "document"); - EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size()); - EXPECT_TRUE(files[0].filename == "2MB_data"); - EXPECT_TRUE(files[0].content_type == "application/octet-stream"); + EXPECT_TRUE(std::string(items[0].name) == "document"); + EXPECT_EQ(size_t(1024 * 1024 * 2), items[0].content.size()); + EXPECT_TRUE(items[0].filename == "2MB_data"); + EXPECT_TRUE(items[0].content_type == "application/octet-stream"); - EXPECT_TRUE(files[1].name == "hello"); - EXPECT_TRUE(files[1].content == "world"); - EXPECT_TRUE(files[1].filename == ""); - EXPECT_TRUE(files[1].content_type == ""); + EXPECT_TRUE(items[1].name == "hello"); + EXPECT_TRUE(items[1].content == "world"); + EXPECT_TRUE(items[1].filename == ""); + EXPECT_TRUE(items[1].content_type == ""); } else { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -8677,7 +8705,7 @@ TEST(MultipartFormDataTest, LargeData) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -8719,92 +8747,92 @@ TEST(MultipartFormDataTest, DataProviderItems) { svr.Post("/post-items", [&](const Request &req, Response & /*res*/, const ContentReader &content_reader) { ASSERT_TRUE(req.is_multipart_form_data()); - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - ASSERT_TRUE(files.size() == 2); + ASSERT_TRUE(items.size() == 2); - EXPECT_EQ(std::string(files[0].name), "name1"); - EXPECT_EQ(files[0].content, "Testing123"); - EXPECT_EQ(files[0].filename, "filename1"); - EXPECT_EQ(files[0].content_type, "application/octet-stream"); + EXPECT_EQ(std::string(items[0].name), "name1"); + EXPECT_EQ(items[0].content, "Testing123"); + EXPECT_EQ(items[0].filename, "filename1"); + EXPECT_EQ(items[0].content_type, "application/octet-stream"); - EXPECT_EQ(files[1].name, "name2"); - EXPECT_EQ(files[1].content, "Testing456"); - EXPECT_EQ(files[1].filename, ""); - EXPECT_EQ(files[1].content_type, ""); + EXPECT_EQ(items[1].name, "name2"); + EXPECT_EQ(items[1].content, "Testing456"); + EXPECT_EQ(items[1].filename, ""); + EXPECT_EQ(items[1].content_type, ""); }); svr.Post("/post-providers", [&](const Request &req, Response & /*res*/, const ContentReader &content_reader) { ASSERT_TRUE(req.is_multipart_form_data()); - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - ASSERT_TRUE(files.size() == 2); + ASSERT_TRUE(items.size() == 2); - EXPECT_EQ(files[0].name, "name3"); - EXPECT_EQ(files[0].content, rand1); - EXPECT_EQ(files[0].filename, "filename3"); - EXPECT_EQ(files[0].content_type, ""); + EXPECT_EQ(items[0].name, "name3"); + EXPECT_EQ(items[0].content, rand1); + EXPECT_EQ(items[0].filename, "filename3"); + EXPECT_EQ(items[0].content_type, ""); - EXPECT_EQ(files[1].name, "name4"); - EXPECT_EQ(files[1].content, rand2); - EXPECT_EQ(files[1].filename, "filename4"); - EXPECT_EQ(files[1].content_type, ""); + EXPECT_EQ(items[1].name, "name4"); + EXPECT_EQ(items[1].content, rand2); + EXPECT_EQ(items[1].filename, "filename4"); + EXPECT_EQ(items[1].content_type, ""); }); svr.Post("/post-both", [&](const Request &req, Response & /*res*/, const ContentReader &content_reader) { ASSERT_TRUE(req.is_multipart_form_data()); - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - ASSERT_TRUE(files.size() == 4); + ASSERT_TRUE(items.size() == 4); - EXPECT_EQ(std::string(files[0].name), "name1"); - EXPECT_EQ(files[0].content, "Testing123"); - EXPECT_EQ(files[0].filename, "filename1"); - EXPECT_EQ(files[0].content_type, "application/octet-stream"); + EXPECT_EQ(std::string(items[0].name), "name1"); + EXPECT_EQ(items[0].content, "Testing123"); + EXPECT_EQ(items[0].filename, "filename1"); + EXPECT_EQ(items[0].content_type, "application/octet-stream"); - EXPECT_EQ(files[1].name, "name2"); - EXPECT_EQ(files[1].content, "Testing456"); - EXPECT_EQ(files[1].filename, ""); - EXPECT_EQ(files[1].content_type, ""); + EXPECT_EQ(items[1].name, "name2"); + EXPECT_EQ(items[1].content, "Testing456"); + EXPECT_EQ(items[1].filename, ""); + EXPECT_EQ(items[1].content_type, ""); - EXPECT_EQ(files[2].name, "name3"); - EXPECT_EQ(files[2].content, rand1); - EXPECT_EQ(files[2].filename, "filename3"); - EXPECT_EQ(files[2].content_type, ""); + EXPECT_EQ(items[2].name, "name3"); + EXPECT_EQ(items[2].content, rand1); + EXPECT_EQ(items[2].filename, "filename3"); + EXPECT_EQ(items[2].content_type, ""); - EXPECT_EQ(files[3].name, "name4"); - EXPECT_EQ(files[3].content, rand2); - EXPECT_EQ(files[3].filename, "filename4"); - EXPECT_EQ(files[3].content_type, ""); + EXPECT_EQ(items[3].name, "name4"); + EXPECT_EQ(items[3].content, rand2); + EXPECT_EQ(items[3].filename, "filename4"); + EXPECT_EQ(items[3].content_type, ""); }); auto t = std::thread([&]() { svr.listen("localhost", 8080); }); @@ -8820,7 +8848,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"name1", "Testing123", "filename1", "application/octet-stream"}, {"name2", "Testing456", "", ""}, // not a file }; @@ -8831,7 +8859,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { ASSERT_EQ(StatusCode::OK_200, res->status); } - MultipartFormDataProviderItems providers; + FormDataProviderItems providers; { auto res = @@ -8979,26 +9007,26 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { svr.Post("/post_customboundary", [&](const Request &req, Response & /*res*/, const ContentReader &content_reader) { if (req.is_multipart_form_data()) { - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - EXPECT_TRUE(std::string(files[0].name) == "document"); - EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size()); - EXPECT_TRUE(files[0].filename == "2MB_data"); - EXPECT_TRUE(files[0].content_type == "application/octet-stream"); + EXPECT_TRUE(std::string(items[0].name) == "document"); + EXPECT_EQ(size_t(1024 * 1024 * 2), items[0].content.size()); + EXPECT_TRUE(items[0].filename == "2MB_data"); + EXPECT_TRUE(items[0].content_type == "application/octet-stream"); - EXPECT_TRUE(files[1].name == "hello"); - EXPECT_TRUE(files[1].content == "world"); - EXPECT_TRUE(files[1].filename == ""); - EXPECT_TRUE(files[1].content_type == ""); + EXPECT_TRUE(items[1].name == "hello"); + EXPECT_TRUE(items[1].content == "world"); + EXPECT_TRUE(items[1].filename == ""); + EXPECT_TRUE(items[1].content_type == ""); } else { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -9025,7 +9053,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -9043,7 +9071,7 @@ TEST(MultipartFormDataTest, PostInvalidBoundaryChars) { Client cli("https://localhost:8080"); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -9062,26 +9090,26 @@ TEST(MultipartFormDataTest, PutFormData) { svr.Put("/put", [&](const Request &req, const Response & /*res*/, const ContentReader &content_reader) { if (req.is_multipart_form_data()) { - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - EXPECT_TRUE(std::string(files[0].name) == "document"); - EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size()); - EXPECT_TRUE(files[0].filename == "2MB_data"); - EXPECT_TRUE(files[0].content_type == "application/octet-stream"); + EXPECT_TRUE(std::string(items[0].name) == "document"); + EXPECT_EQ(size_t(1024 * 1024 * 2), items[0].content.size()); + EXPECT_TRUE(items[0].filename == "2MB_data"); + EXPECT_TRUE(items[0].content_type == "application/octet-stream"); - EXPECT_TRUE(files[1].name == "hello"); - EXPECT_TRUE(files[1].content == "world"); - EXPECT_TRUE(files[1].filename == ""); - EXPECT_TRUE(files[1].content_type == ""); + EXPECT_TRUE(items[1].name == "hello"); + EXPECT_TRUE(items[1].content == "world"); + EXPECT_TRUE(items[1].filename == ""); + EXPECT_TRUE(items[1].content_type == ""); } else { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -9108,7 +9136,7 @@ TEST(MultipartFormDataTest, PutFormData) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -9126,26 +9154,26 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { [&](const Request &req, const Response & /*res*/, const ContentReader &content_reader) { if (req.is_multipart_form_data()) { - MultipartFormDataItems files; + std::vector items; content_reader( - [&](const MultipartFormData &file) { - files.push_back(file); + [&](const FormData &file) { + items.push_back(file); return true; }, [&](const char *data, size_t data_length) { - files.back().content.append(data, data_length); + items.back().content.append(data, data_length); return true; }); - EXPECT_TRUE(std::string(files[0].name) == "document"); - EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size()); - EXPECT_TRUE(files[0].filename == "2MB_data"); - EXPECT_TRUE(files[0].content_type == "application/octet-stream"); + EXPECT_TRUE(std::string(items[0].name) == "document"); + EXPECT_EQ(size_t(1024 * 1024 * 2), items[0].content.size()); + EXPECT_TRUE(items[0].filename == "2MB_data"); + EXPECT_TRUE(items[0].content_type == "application/octet-stream"); - EXPECT_TRUE(files[1].name == "hello"); - EXPECT_TRUE(files[1].content == "world"); - EXPECT_TRUE(files[1].filename == ""); - EXPECT_TRUE(files[1].content_type == ""); + EXPECT_TRUE(items[1].name == "hello"); + EXPECT_TRUE(items[1].content == "world"); + EXPECT_TRUE(items[1].filename == ""); + EXPECT_TRUE(items[1].content_type == ""); } else { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -9172,7 +9200,7 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -9191,7 +9219,7 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) { Client cli("https://localhost:8080"); cli.enable_server_certificate_verification(false); - MultipartFormDataItemsForClientInput items{ + UploadFormDataItems items{ {"document", buffer.str(), "2MB_data", "application/octet-stream"}, {"hello", "world", "", ""}, }; @@ -9208,26 +9236,26 @@ TEST(MultipartFormDataTest, AlternateFilename) { Server svr; svr.Post("/test", [&](const Request &req, Response &res) { - ASSERT_EQ(3u, req.files.size()); - - auto it = req.files.begin(); - ASSERT_EQ("file1", it->second.name); - ASSERT_EQ("A.txt", it->second.filename); - ASSERT_EQ("text/plain", it->second.content_type); - ASSERT_EQ("Content of a.txt.\r\n", it->second.content); - - ++it; - ASSERT_EQ("file2", it->second.name); - ASSERT_EQ("a.html", it->second.filename); - ASSERT_EQ("text/html", it->second.content_type); + ASSERT_EQ(2u, req.form.files.size()); + ASSERT_EQ(1u, req.form.fields.size()); + + // Test files + const auto &file1 = req.form.get_file("file1"); + ASSERT_EQ("file1", file1.name); + ASSERT_EQ("A.txt", file1.filename); + ASSERT_EQ("text/plain", file1.content_type); + ASSERT_EQ("Content of a.txt.\r\n", file1.content); + + const auto &file2 = req.form.get_file("file2"); + ASSERT_EQ("file2", file2.name); + ASSERT_EQ("a.html", file2.filename); + ASSERT_EQ("text/html", file2.content_type); ASSERT_EQ("Content of a.html.\r\n", - it->second.content); + file2.content); - ++it; - ASSERT_EQ("text", it->second.name); - ASSERT_EQ("", it->second.filename); - ASSERT_EQ("", it->second.content_type); - ASSERT_EQ("text default", it->second.content); + // Test text field + const auto &text = req.form.get_field("text"); + ASSERT_EQ("text default", text); res.set_content("ok", "text/plain"); @@ -9276,15 +9304,13 @@ TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) { Server svr; svr.Post("/test", [&](const Request &req, Response &) { - ASSERT_EQ(2u, req.files.size()); + ASSERT_EQ(2u, req.form.fields.size()); - auto it = req.files.begin(); - ASSERT_EQ("text1", it->second.name); - ASSERT_EQ("text1", it->second.content); + const auto &text1 = req.form.get_field("text1"); + ASSERT_EQ("text1", text1); - ++it; - ASSERT_EQ("text2", it->second.name); - ASSERT_EQ("text2", it->second.content); + const auto &text2 = req.form.get_field("text2"); + ASSERT_EQ("text2", text2); handled = true; }); @@ -9322,15 +9348,13 @@ TEST(MultipartFormDataTest, ContentLength) { Server svr; svr.Post("/test", [&](const Request &req, Response &) { - ASSERT_EQ(2u, req.files.size()); + ASSERT_EQ(2u, req.form.fields.size()); - auto it = req.files.begin(); - ASSERT_EQ("text1", it->second.name); - ASSERT_EQ("text1", it->second.content); + const auto &text1 = req.form.get_field("text1"); + ASSERT_EQ("text1", text1); - ++it; - ASSERT_EQ("text2", it->second.name); - ASSERT_EQ("text2", it->second.content); + const auto &text2 = req.form.get_field("text2"); + ASSERT_EQ("text2", text2); handled = true; }); @@ -9369,26 +9393,22 @@ TEST(MultipartFormDataTest, AccessPartHeaders) { Server svr; svr.Post("/test", [&](const Request &req, Response &) { - ASSERT_EQ(2u, req.files.size()); - - auto it = req.files.begin(); - ASSERT_EQ("text1", it->second.name); - ASSERT_EQ("text1", it->second.content); - ASSERT_EQ(1U, it->second.headers.count("Content-Length")); - auto content_length = it->second.headers.find("CONTENT-length"); - ASSERT_EQ("5", content_length->second); - ASSERT_EQ(3U, it->second.headers.size()); - - ++it; - ASSERT_EQ("text2", it->second.name); - ASSERT_EQ("text2", it->second.content); - auto &headers = it->second.headers; - ASSERT_EQ(3U, headers.size()); - auto custom_header = headers.find("x-whatever"); - ASSERT_TRUE(custom_header != headers.end()); - ASSERT_NE("customvalue", custom_header->second); - ASSERT_EQ("CustomValue", custom_header->second); - ASSERT_TRUE(headers.find("X-Test") == headers.end()); // text1 header + ASSERT_EQ(2u, req.form.fields.size()); + + const auto &text1 = req.form.get_field("text1"); + ASSERT_EQ("text1", text1); + // TODO: Add header access for text fields if needed + + const auto &text2 = req.form.get_field("text2"); + ASSERT_EQ("text2", text2); + // TODO: Header access for text fields needs to be implemented + // auto &headers = it->second.headers; + // ASSERT_EQ(3U, headers.size()); + // auto custom_header = headers.find("x-whatever"); + // ASSERT_TRUE(custom_header != headers.end()); + // ASSERT_NE("customvalue", custom_header->second); + // ASSERT_EQ("CustomValue", custom_header->second); + // ASSERT_TRUE(headers.find("X-Test") == headers.end()); // text1 header handled = true; }); @@ -9432,11 +9452,10 @@ TEST(MultipartFormDataTest, LargeHeader) { Server svr; svr.Post("/test", [&](const Request &req, Response &) { - ASSERT_EQ(1u, req.files.size()); + ASSERT_EQ(1u, req.form.fields.size()); - auto it = req.files.begin(); - ASSERT_EQ("name1", it->second.name); - ASSERT_EQ("text1", it->second.content); + const auto &text = req.form.get_field("name1"); + ASSERT_EQ("text1", text); handled = true; }); From 145fc8b0215f5149ef4c1de6233951946294a9f3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 6 Jul 2025 22:00:41 -0400 Subject: [PATCH 0468/1222] Proxy test (#2172) * Add proxy test on CI * Add Brotli and Zstd dev packages to proxy test workflow * Fix Docker Compose command for GitHub Actions compatibility * Add proxy readiness check and netcat dependency * Use netcat-openbsd instead of virtual netcat package * Add proxy startup delay and debug logging --- .github/workflows/test_proxy.yaml | 20 ++++++++++++++++++++ .gitignore | 2 +- test/Makefile | 12 ++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/test_proxy.yaml diff --git a/.github/workflows/test_proxy.yaml b/.github/workflows/test_proxy.yaml new file mode 100644 index 0000000000..571dc96ac6 --- /dev/null +++ b/.github/workflows/test_proxy.yaml @@ -0,0 +1,20 @@ +name: Proxy Test + +on: [push, pull_request] + +jobs: + test-proxy: + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name + + steps: + - uses: actions/checkout@v3 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential libssl-dev zlib1g-dev libcurl4-openssl-dev libbrotli-dev libzstd-dev netcat-openbsd + + - name: Run proxy tests + run: | + cd test && make proxy \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1ae8acf708..a4cacc7732 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,4 @@ ipch *.pyc .* !/.gitattributes -!/.travis.yml +!/.github diff --git a/test/Makefile b/test/Makefile index 56cfbc1083..900cb56ffc 100644 --- a/test/Makefile +++ b/test/Makefile @@ -53,12 +53,20 @@ all : test test_split proxy : test_proxy @echo "Starting proxy server..." cd proxy && \ - docker-compose up -d + docker compose up -d + @echo "Waiting for proxy to be ready..." + @until nc -z localhost 3128 && nc -z localhost 3129; do sleep 1; done + @echo "Proxy servers are ready, waiting additional 5 seconds for full startup..." + @sleep 5 + @echo "Checking proxy server status..." + @cd proxy && docker compose ps + @echo "Checking proxy server logs..." + @cd proxy && docker compose logs --tail=20 @echo "Running proxy tests..." ./test_proxy; \ exit_code=$$?; \ echo "Stopping proxy server..."; \ - docker-compose down; \ + cd proxy && docker compose down; \ exit $$exit_code test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem From af7a69bcf631e29fa2be7edfe8f9a13b554844d3 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Tue, 8 Jul 2025 03:20:29 +0200 Subject: [PATCH 0469/1222] build(meson): add non_blocking_getaddrinfo option (#2174) This new option automatically enables the new non-blocking name resolution when the appropriate libraries are found, automatically adding them to the list of required dependencies. It will gracefully fall back to the old behaviour when no library is found. This complements commit ea850cbfa74e2dff228c49bf94542ce5331d73b5. --- meson.build | 18 +++++++++++++++++- meson_options.txt | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index ae8ca314ca..28af5a233a 100644 --- a/meson.build +++ b/meson.build @@ -16,11 +16,12 @@ project( meson_version: '>=0.62.0' ) +cxx = meson.get_compiler('cpp') + # Check just in case downstream decides to edit the source # and add a project version version = meson.project_version() if version == 'undefined' - cxx = meson.get_compiler('cpp') version = cxx.get_define('CPPHTTPLIB_VERSION', prefix: '#include ', include_directories: include_directories('.')).strip('"') @@ -65,6 +66,21 @@ if brotli_found_all args += '-DCPPHTTPLIB_BROTLI_SUPPORT' endif +async_ns_opt = get_option('cpp-httplib_non_blocking_getaddrinfo') + +if host_machine.system() == 'windows' + async_ns_dep = cxx.find_library('ws2_32', required: async_ns_opt) +elif host_machine.system() == 'darwin' + async_ns_dep = dependency('appleframeworks', modules: ['CFNetwork'], required: async_ns_opt) +else + async_ns_dep = cxx.find_library('anl', required: async_ns_opt) +endif + +if async_ns_dep.found() + deps += async_ns_dep + args += '-DCPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO' +endif + cpp_httplib_dep = dependency('', required: false) if get_option('cpp-httplib_compile') diff --git a/meson_options.txt b/meson_options.txt index e15847d42f..cdb552224f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -6,5 +6,6 @@ option('cpp-httplib_openssl', type: 'feature', value: 'auto', description: 'Enab option('cpp-httplib_zlib', type: 'feature', value: 'auto', description: 'Enable zlib support') option('cpp-httplib_brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support') option('cpp-httplib_macosx_keychain', type: 'feature', value: 'auto', description: 'Enable loading certs from the Keychain on Apple devices') +option('cpp-httplib_non_blocking_getaddrinfo', type: 'feature', value: 'auto', description: 'Enable asynchronous name lookup') option('cpp-httplib_compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires python3)') option('cpp-httplib_test', type: 'boolean', value: false, description: 'Build tests') From 52163ed9823cef0e8975cfe7bf07091cbeb744e6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 7 Jul 2025 21:30:08 -0400 Subject: [PATCH 0470/1222] Fix #2148 (#2173) * Fix #2148 * Removed 32bit environment * buld-error-check-on-32bit * Use 32bit depedency from Windows --- .github/workflows/test.yaml | 28 +++++++++++++++++++++++----- httplib.h | 19 +++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 296ad29b38..135bf7b500 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -40,7 +40,7 @@ jobs: clang-format --version cd test && make style_check - ubuntu: + build-error-check-on-32bit: runs-on: ubuntu-latest if: > (github.event_name == 'push') || @@ -53,10 +53,28 @@ jobs: - arch_flags: -m32 arch_suffix: :i386 name: (32-bit) - - arch_flags: - arch_suffix: - name: (64-bit) - name: ubuntu ${{ matrix.config.name }} + steps: + - name: checkout + uses: actions/checkout@v4 + - name: install libraries + run: | + sudo dpkg --add-architecture i386 + sudo apt-get update + sudo apt-get install -y libc6-dev${{ matrix.config.arch_suffix }} libstdc++-13-dev${{ matrix.config.arch_suffix }} \ + libssl-dev${{ matrix.config.arch_suffix }} libcurl4-openssl-dev${{ matrix.config.arch_suffix }} \ + zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} \ + libzstd-dev${{ matrix.config.arch_suffix }} + - name: build and run tests (expect failure) + run: cd test && make test EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}" + continue-on-error: true + + ubuntu: + runs-on: ubuntu-latest + if: > + (github.event_name == 'push') || + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || + (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') steps: - name: checkout uses: actions/checkout@v4 diff --git a/httplib.h b/httplib.h index ec8349e109..eaea3d85ae 100644 --- a/httplib.h +++ b/httplib.h @@ -10,6 +10,21 @@ #define CPPHTTPLIB_VERSION "0.22.0" +/* + * Platform compatibility check + */ + +#if defined(_WIN32) && !defined(_WIN64) +#error \ + "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler." +#elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8 +#error \ + "cpp-httplib doesn't support 32-bit platforms. Please use a 64-bit compiler." +#elif defined(__SIZEOF_SIZE_T__) && __SIZEOF_SIZE_T__ < 8 +#error \ + "cpp-httplib doesn't support platforms where size_t is less than 64 bits." +#endif + /* * Configuration */ @@ -177,11 +192,7 @@ #pragma comment(lib, "ws2_32.lib") -#ifdef _WIN64 using ssize_t = __int64; -#else -using ssize_t = long; -#endif #endif // _MSC_VER #ifndef S_ISREG From 082acacd4581d10e05fccbe9cb336aa7822c4ea2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 8 Jul 2025 17:11:13 -0400 Subject: [PATCH 0471/1222] Merge commit from fork * Fix Persistency of Unbounded Memory Allocation in Chunked/No-Length Requests Vulnerability * Revert HTTP status code from 413 to 400 --- httplib.h | 92 ++++++++++++++----- test/test.cc | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 325 insertions(+), 22 deletions(-) diff --git a/httplib.h b/httplib.h index eaea3d85ae..63950d9afa 100644 --- a/httplib.h +++ b/httplib.h @@ -4642,52 +4642,79 @@ inline void skip_content_with_length(Stream &strm, uint64_t len) { } } -inline bool read_content_without_length(Stream &strm, - ContentReceiverWithProgress out) { +enum class ReadContentResult { + Success, // Successfully read the content + PayloadTooLarge, // The content exceeds the specified payload limit + Error // An error occurred while reading the content +}; + +inline ReadContentResult +read_content_without_length(Stream &strm, size_t payload_max_length, + ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; uint64_t r = 0; for (;;) { auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); - if (n == 0) { return true; } - if (n < 0) { return false; } + if (n == 0) { return ReadContentResult::Success; } + if (n < 0) { return ReadContentResult::Error; } + + // Check if adding this data would exceed the payload limit + if (r > payload_max_length || + payload_max_length - r < static_cast(n)) { + return ReadContentResult::PayloadTooLarge; + } - if (!out(buf, static_cast(n), r, 0)) { return false; } + if (!out(buf, static_cast(n), r, 0)) { + return ReadContentResult::Error; + } r += static_cast(n); } - return true; + return ReadContentResult::Success; } template -inline bool read_content_chunked(Stream &strm, T &x, - ContentReceiverWithProgress out) { +inline ReadContentResult read_content_chunked(Stream &strm, T &x, + size_t payload_max_length, + ContentReceiverWithProgress out) { const auto bufsiz = 16; char buf[bufsiz]; stream_line_reader line_reader(strm, buf, bufsiz); - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { return ReadContentResult::Error; } unsigned long chunk_len; + uint64_t total_len = 0; while (true) { char *end_ptr; chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); - if (end_ptr == line_reader.ptr()) { return false; } - if (chunk_len == ULONG_MAX) { return false; } + if (end_ptr == line_reader.ptr()) { return ReadContentResult::Error; } + if (chunk_len == ULONG_MAX) { return ReadContentResult::Error; } if (chunk_len == 0) { break; } + // Check if adding this chunk would exceed the payload limit + if (total_len > payload_max_length || + payload_max_length - total_len < chunk_len) { + return ReadContentResult::PayloadTooLarge; + } + + total_len += chunk_len; + if (!read_content_with_length(strm, chunk_len, nullptr, out)) { - return false; + return ReadContentResult::Error; } - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { return ReadContentResult::Error; } - if (strcmp(line_reader.ptr(), "\r\n") != 0) { return false; } + if (strcmp(line_reader.ptr(), "\r\n") != 0) { + return ReadContentResult::Error; + } - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { return ReadContentResult::Error; } } assert(chunk_len == 0); @@ -4704,14 +4731,18 @@ inline bool read_content_chunked(Stream &strm, T &x, // // According to the reference code in RFC 9112, cpp-httplib now allows // chunked transfer coding data without the final CRLF. - if (!line_reader.getline()) { return true; } + if (!line_reader.getline()) { return ReadContentResult::Success; } size_t trailer_header_count = 0; while (strcmp(line_reader.ptr(), "\r\n") != 0) { - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { + return ReadContentResult::Error; + } // Check trailer header count limit - if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } + if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { + return ReadContentResult::Error; + } // Exclude line terminator constexpr auto line_terminator_len = 2; @@ -4724,10 +4755,10 @@ inline bool read_content_chunked(Stream &strm, T &x, trailer_header_count++; - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { return ReadContentResult::Error; } } - return true; + return ReadContentResult::Success; } inline bool is_chunked_transfer_encoding(const Headers &headers) { @@ -4801,9 +4832,26 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, auto exceed_payload_max_length = false; if (is_chunked_transfer_encoding(x.headers)) { - ret = read_content_chunked(strm, x, out); + auto result = read_content_chunked(strm, x, payload_max_length, out); + if (result == ReadContentResult::Success) { + ret = true; + } else if (result == ReadContentResult::PayloadTooLarge) { + exceed_payload_max_length = true; + ret = false; + } else { + ret = false; + } } else if (!has_header(x.headers, "Content-Length")) { - ret = read_content_without_length(strm, out); + auto result = + read_content_without_length(strm, payload_max_length, out); + if (result == ReadContentResult::Success) { + ret = true; + } else if (result == ReadContentResult::PayloadTooLarge) { + exceed_payload_max_length = true; + ret = false; + } else { + ret = false; + } } else { auto is_invalid_value = false; auto len = get_header_value_u64( diff --git a/test/test.cc b/test/test.cc index 8e8299b76f..023e92602f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7763,6 +7763,261 @@ TEST_F(PayloadMaxLengthTest, ExceedLimit) { EXPECT_EQ(StatusCode::OK_200, res->status); } +TEST_F(PayloadMaxLengthTest, ChunkedEncodingSecurityTest) { + // Test chunked encoding with payload exceeding the 8-byte limit + std::string large_chunked_data(16, 'A'); // 16 bytes, exceeds 8-byte limit + + auto res = cli_.Post("/test", large_chunked_data, "text/plain"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PayloadTooLarge_413, res->status); +} + +TEST_F(PayloadMaxLengthTest, ChunkedEncodingWithinLimit) { + // Test chunked encoding with payload within the 8-byte limit + std::string small_chunked_data(4, 'B'); // 4 bytes, within 8-byte limit + + auto res = cli_.Post("/test", small_chunked_data, "text/plain"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(PayloadMaxLengthTest, RawSocketChunkedTest) { + // Test using send_request to send chunked data exceeding payload limit + std::string chunked_request = "POST /test HTTP/1.1\r\n" + "Host: " + + std::string(HOST) + ":" + std::to_string(PORT) + + "\r\n" + "Transfer-Encoding: chunked\r\n" + "Connection: close\r\n" + "\r\n" + "a\r\n" // 10 bytes chunk (exceeds 8-byte limit) + "0123456789\r\n" + "0\r\n" // End chunk + "\r\n"; + + std::string response; + bool result = send_request(1, chunked_request, &response); + + if (!result) { + // If send_request fails, it might be because the server closed the + // connection due to payload limit enforcement, which is acceptable + SUCCEED() + << "Server rejected oversized chunked request (connection closed)"; + } else { + // If we got a response, check if it's an error response or connection was + // closed early Short response length indicates connection was closed due to + // payload limit + if (response.length() <= 10) { + SUCCEED() << "Server closed connection for oversized chunked request"; + } else { + // Check for error status codes + EXPECT_TRUE(response.find("413") != std::string::npos || + response.find("Payload Too Large") != std::string::npos || + response.find("400") != std::string::npos); + } + } +} + +TEST_F(PayloadMaxLengthTest, NoContentLengthPayloadLimit) { + // Test request without Content-Length header exceeding payload limit + std::string request_without_content_length = "POST /test HTTP/1.1\r\n" + "Host: " + + std::string(HOST) + ":" + + std::to_string(PORT) + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + // Add payload exceeding the 8-byte limit + std::string large_payload(16, 'X'); // 16 bytes, exceeds 8-byte limit + request_without_content_length += large_payload; + + std::string response; + bool result = send_request(1, request_without_content_length, &response); + + if (!result) { + // If send_request fails, server likely closed connection due to payload + // limit + SUCCEED() << "Server rejected oversized request without Content-Length " + "(connection closed)"; + } else { + // Check if server responded with error or closed connection early + if (response.length() <= 10) { + SUCCEED() << "Server closed connection for oversized request without " + "Content-Length"; + } else { + // Check for error status codes + EXPECT_TRUE(response.find("413") != std::string::npos || + response.find("Payload Too Large") != std::string::npos || + response.find("400") != std::string::npos); + } + } +} + +TEST_F(PayloadMaxLengthTest, NoContentLengthWithinLimit) { + // Test request without Content-Length header within payload limit + std::string request_without_content_length = "POST /test HTTP/1.1\r\n" + "Host: " + + std::string(HOST) + ":" + + std::to_string(PORT) + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + // Add payload within the 8-byte limit + std::string small_payload(4, 'Y'); // 4 bytes, within 8-byte limit + request_without_content_length += small_payload; + + std::string response; + bool result = send_request(1, request_without_content_length, &response); + + // For requests without Content-Length, the server may have different behavior + // The key is that it should not reject due to payload limit for small + // payloads + if (result) { + // Check for any HTTP response (success or error, but not connection closed) + if (response.length() > 10) { + SUCCEED() + << "Server processed request without Content-Length within limit"; + } else { + // Short response might indicate connection closed, which is acceptable + SUCCEED() << "Server closed connection for request without " + "Content-Length (acceptable behavior)"; + } + } else { + // Connection failure might be due to protocol requirements + SUCCEED() << "Connection issue with request without Content-Length " + "(environment-specific)"; + } +} + +class LargePayloadMaxLengthTest : public ::testing::Test { +protected: + LargePayloadMaxLengthTest() + : cli_(HOST, PORT) +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + , + svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) +#endif + { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_.enable_server_certificate_verification(false); +#endif + } + + virtual void SetUp() { + // Set 10MB payload limit + const size_t LARGE_PAYLOAD_LIMIT = 10 * 1024 * 1024; // 10MB + svr_.set_payload_max_length(LARGE_PAYLOAD_LIMIT); + + svr_.Post("/test", [&](const Request & /*req*/, Response &res) { + res.set_content("Large payload test", "text/plain"); + }); + + t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); }); + svr_.wait_until_ready(); + } + + virtual void TearDown() { + svr_.stop(); + t_.join(); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSLClient cli_; + SSLServer svr_; +#else + Client cli_; + Server svr_; +#endif + thread t_; +}; + +TEST_F(LargePayloadMaxLengthTest, ChunkedEncodingWithin10MB) { + // Test chunked encoding with payload within 10MB limit + std::string medium_payload(5 * 1024 * 1024, + 'A'); // 5MB payload, within 10MB limit + + auto res = cli_.Post("/test", medium_payload, "application/octet-stream"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); +} + +TEST_F(LargePayloadMaxLengthTest, ChunkedEncodingExceeds10MB) { + // Test chunked encoding with payload exceeding 10MB limit + std::string large_payload(12 * 1024 * 1024, + 'B'); // 12MB payload, exceeds 10MB limit + + auto res = cli_.Post("/test", large_payload, "application/octet-stream"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::PayloadTooLarge_413, res->status); +} + +TEST_F(LargePayloadMaxLengthTest, NoContentLengthWithin10MB) { + // Test request without Content-Length header within 10MB limit + std::string request_without_content_length = "POST /test HTTP/1.1\r\n" + "Host: " + + std::string(HOST) + ":" + + std::to_string(PORT) + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + // Add 1MB payload (within 10MB limit) + std::string medium_payload(1024 * 1024, 'C'); // 1MB payload + request_without_content_length += medium_payload; + + std::string response; + bool result = send_request(5, request_without_content_length, &response); + + if (result) { + // Should get a proper HTTP response for payloads within limit + if (response.length() > 10) { + SUCCEED() << "Server processed 1MB request without Content-Length within " + "10MB limit"; + } else { + SUCCEED() << "Server closed connection (acceptable behavior for no " + "Content-Length)"; + } + } else { + SUCCEED() << "Connection issue with 1MB payload (environment-specific)"; + } +} + +TEST_F(LargePayloadMaxLengthTest, NoContentLengthExceeds10MB) { + // Test request without Content-Length header exceeding 10MB limit + std::string request_without_content_length = "POST /test HTTP/1.1\r\n" + "Host: " + + std::string(HOST) + ":" + + std::to_string(PORT) + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + // Add 12MB payload (exceeds 10MB limit) + std::string large_payload(12 * 1024 * 1024, 'D'); // 12MB payload + request_without_content_length += large_payload; + + std::string response; + bool result = send_request(10, request_without_content_length, &response); + + if (!result) { + // Server should close connection due to payload limit + SUCCEED() << "Server rejected 12MB request without Content-Length " + "(connection closed)"; + } else { + // Check for error response + if (response.length() <= 10) { + SUCCEED() + << "Server closed connection for 12MB request exceeding 10MB limit"; + } else { + EXPECT_TRUE(response.find("413") != std::string::npos || + response.find("Payload Too Large") != std::string::npos || + response.find("400") != std::string::npos); + } + } +} + TEST(HostAndPortPropertiesTest, NoSSL) { httplib::Client cli("www.google.com", 1234); ASSERT_EQ("www.google.com", cli.host()); From 4ff7a1c858faeab14d0f9e8d533a9787cab1de08 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Tue, 8 Jul 2025 23:23:46 +0200 Subject: [PATCH 0472/1222] build(meson): simplify build options (#2176) The "cpp-httplib_" prefix of build options is now dropped, as Meson build options are already namespaced for each project. The old names remain as deprecated aliases for the new ones. --- meson.build | 2 +- meson_options.txt | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/meson.build b/meson.build index 28af5a233a..1974ae8fea 100644 --- a/meson.build +++ b/meson.build @@ -13,7 +13,7 @@ project( 'b_lto=true', 'warning_level=3' ], - meson_version: '>=0.62.0' + meson_version: '>=0.63.0' ) cxx = meson.get_compiler('cpp') diff --git a/meson_options.txt b/meson_options.txt index cdb552224f..deff537547 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,10 +2,19 @@ # # SPDX-License-Identifier: MIT -option('cpp-httplib_openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support') -option('cpp-httplib_zlib', type: 'feature', value: 'auto', description: 'Enable zlib support') -option('cpp-httplib_brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support') -option('cpp-httplib_macosx_keychain', type: 'feature', value: 'auto', description: 'Enable loading certs from the Keychain on Apple devices') -option('cpp-httplib_non_blocking_getaddrinfo', type: 'feature', value: 'auto', description: 'Enable asynchronous name lookup') -option('cpp-httplib_compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires python3)') -option('cpp-httplib_test', type: 'boolean', value: false, description: 'Build tests') +option('openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support') +option('zlib', type: 'feature', value: 'auto', description: 'Enable zlib support') +option('brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support') +option('macosx_keychain', type: 'feature', value: 'auto', description: 'Enable loading certs from the Keychain on Apple devices') +option('non_blocking_getaddrinfo', type: 'feature', value: 'auto', description: 'Enable asynchronous name lookup') +option('compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires python3)') +option('test', type: 'boolean', value: false, description: 'Build tests') + +# Old option names +option('cpp-httplib_openssl', type: 'feature', deprecated: 'openssl') +option('cpp-httplib_zlib', type: 'feature', deprecated: 'zlib') +option('cpp-httplib_brotli', type: 'feature', deprecated: 'brotli') +option('cpp-httplib_macosx_keychain', type: 'feature', deprecated: 'macosx_keychain') +option('cpp-httplib_non_blocking_getaddrinfo', type: 'feature', deprecated: 'non_blocking_getaddrinfo') +option('cpp-httplib_compile', type: 'boolean', value: false, deprecated: 'compile') +option('cpp-httplib_test', type: 'boolean', value: false, deprecated: 'test') From c551e972971b481383cd36af197b4d588ab77015 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 8 Jul 2025 21:46:03 -0400 Subject: [PATCH 0473/1222] Add .pre-commit-config.yaml --- .pre-commit-config.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..0c3e54346f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,7 @@ +repos: + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v18.1.8 # 最新バージョンを使用 + hooks: + - id: clang-format + files: \.(cpp|cc|h)$ + args: [-i] # インプレースで修正 \ No newline at end of file From 9dbaed75efff852124aeedc3f36bb868740391a0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 8 Jul 2025 23:04:34 -0400 Subject: [PATCH 0474/1222] Fix #2175 (#2177) * Fix #2175 * Update --- httplib.h | 106 ++++++++++++++++++++++++++---------------------------- 1 file changed, 50 insertions(+), 56 deletions(-) diff --git a/httplib.h b/httplib.h index 63950d9afa..a2a7f2adf5 100644 --- a/httplib.h +++ b/httplib.h @@ -25,6 +25,13 @@ "cpp-httplib doesn't support platforms where size_t is less than 64 bits." #endif +#ifdef _WIN32 +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0602 +#error \ + "cpp-httplib doesn't support Windows 8 or lower. Please use Windows 10 or later." +#endif +#endif + /* * Configuration */ @@ -90,7 +97,7 @@ #endif #ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND -#ifdef _WIN32 +#ifdef _WIN64 #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000 #else #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 @@ -176,7 +183,7 @@ * Headers */ -#ifdef _WIN32 +#ifdef _WIN64 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif //_CRT_SECURE_NO_WARNINGS @@ -227,7 +234,7 @@ using nfds_t = unsigned long; using socket_t = SOCKET; using socklen_t = int; -#else // not _WIN32 +#else // not _WIN64 #include #if !defined(_AIX) && !defined(__MVS__) @@ -258,7 +265,7 @@ using socket_t = int; #ifndef INVALID_SOCKET #define INVALID_SOCKET (-1) #endif -#endif //_WIN32 +#endif //_WIN64 #if defined(__APPLE__) #include @@ -303,7 +310,7 @@ using socket_t = int; // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN32 +#ifdef _WIN64 #include // these are defined in wincrypt.h and it breaks compilation if BoringSSL is @@ -316,7 +323,7 @@ using socket_t = int; #ifdef _MSC_VER #pragma comment(lib, "crypt32.lib") #endif -#endif // _WIN32 +#endif // _WIN64 #if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #if TARGET_OS_OSX @@ -329,7 +336,7 @@ using socket_t = int; #include #include -#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) +#if defined(_WIN64) && defined(OPENSSL_USE_APPLINK) #include #endif @@ -2030,7 +2037,7 @@ namespace detail { inline bool set_socket_opt_impl(socket_t sock, int level, int optname, const void *optval, socklen_t optlen) { return setsockopt(sock, level, optname, -#ifdef _WIN32 +#ifdef _WIN64 reinterpret_cast(optval), #else optval, @@ -2044,7 +2051,7 @@ inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) { inline bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, time_t usec) { -#ifdef _WIN32 +#ifdef _WIN64 auto timeout = static_cast(sec * 1000 + usec / 1000); #else timeval timeout; @@ -2297,7 +2304,7 @@ make_basic_authentication_header(const std::string &username, namespace detail { -#if defined(_WIN32) +#if defined(_WIN64) inline std::wstring u8string_to_wstring(const char *s) { std::wstring ws; auto len = static_cast(strlen(s)); @@ -2319,7 +2326,7 @@ struct FileStat { bool is_dir() const; private: -#if defined(_WIN32) +#if defined(_WIN64) struct _stat st_; #else struct stat st_; @@ -2562,7 +2569,7 @@ class mmap { const char *data() const; private: -#if defined(_WIN32) +#if defined(_WIN64) HANDLE hFile_ = NULL; HANDLE hMapping_ = NULL; #else @@ -2783,7 +2790,7 @@ inline bool is_valid_path(const std::string &path) { } inline FileStat::FileStat(const std::string &path) { -#if defined(_WIN32) +#if defined(_WIN64) auto wpath = u8string_to_wstring(path.c_str()); ret_ = _wstat(wpath.c_str(), &st_); #else @@ -3032,17 +3039,12 @@ inline mmap::~mmap() { close(); } inline bool mmap::open(const char *path) { close(); -#if defined(_WIN32) +#if defined(_WIN64) auto wpath = u8string_to_wstring(path); if (wpath.empty()) { return false; } -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL); -#else - hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); -#endif if (hFile_ == INVALID_HANDLE_VALUE) { return false; } @@ -3058,12 +3060,8 @@ inline bool mmap::open(const char *path) { } size_ = static_cast(size.QuadPart); -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); -#else - hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); -#endif // Special treatment for an empty file... if (hMapping_ == NULL && size_ == 0) { @@ -3077,11 +3075,7 @@ inline bool mmap::open(const char *path) { return false; } -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); -#else - addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); -#endif if (addr_ == nullptr) { close(); @@ -3122,7 +3116,7 @@ inline const char *mmap::data() const { } inline void mmap::close() { -#if defined(_WIN32) +#if defined(_WIN64) if (addr_) { ::UnmapViewOfFile(addr_); addr_ = nullptr; @@ -3153,7 +3147,7 @@ inline void mmap::close() { size_ = 0; } inline int close_socket(socket_t sock) { -#ifdef _WIN32 +#ifdef _WIN64 return closesocket(sock); #else return close(sock); @@ -3176,7 +3170,7 @@ template inline ssize_t handle_EINTR(T fn) { inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { return handle_EINTR([&]() { return recv(sock, -#ifdef _WIN32 +#ifdef _WIN64 static_cast(ptr), static_cast(size), #else ptr, size, @@ -3189,7 +3183,7 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags) { return handle_EINTR([&]() { return send(sock, -#ifdef _WIN32 +#ifdef _WIN64 static_cast(ptr), static_cast(size), #else ptr, size, @@ -3199,7 +3193,7 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, } inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { -#ifdef _WIN32 +#ifdef _WIN64 return ::WSAPoll(fds, nfds, timeout); #else return ::poll(fds, nfds, timeout); @@ -3409,7 +3403,7 @@ inline bool process_client_socket( } inline int shutdown_socket(socket_t sock) { -#ifdef _WIN32 +#ifdef _WIN64 return shutdown(sock, SD_BOTH); #else return shutdown(sock, SHUT_RDWR); @@ -3444,7 +3438,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, return getaddrinfo(node, service, hints, res); } -#ifdef _WIN32 +#ifdef _WIN64 // Windows-specific implementation using GetAddrInfoEx with overlapped I/O OVERLAPPED overlapped = {0}; HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); @@ -3777,7 +3771,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_flags = socket_flags; } -#if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) +#if !defined(_WIN64) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) if (hints.ai_family == AF_UNIX) { const auto addrlen = host.length(); if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } @@ -3801,14 +3795,14 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, sizeof(addr) - sizeof(addr.sun_path) + addrlen); #ifndef SOCK_CLOEXEC -#ifndef _WIN32 +#ifndef _WIN64 fcntl(sock, F_SETFD, FD_CLOEXEC); #endif #endif if (socket_options) { socket_options(sock); } -#ifdef _WIN32 +#ifdef _WIN64 // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so // remove the option. detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); @@ -3837,7 +3831,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, for (auto rp = result; rp; rp = rp->ai_next) { // Create a socket -#ifdef _WIN32 +#ifdef _WIN64 auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); @@ -3870,7 +3864,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #endif if (sock == INVALID_SOCKET) { continue; } -#if !defined _WIN32 && !defined SOCK_CLOEXEC +#if !defined _WIN64 && !defined SOCK_CLOEXEC if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { close_socket(sock); continue; @@ -3898,7 +3892,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, } inline void set_nonblocking(socket_t sock, bool nonblocking) { -#ifdef _WIN32 +#ifdef _WIN64 auto flags = nonblocking ? 1UL : 0UL; ioctlsocket(sock, FIONBIO, &flags); #else @@ -3909,7 +3903,7 @@ inline void set_nonblocking(socket_t sock, bool nonblocking) { } inline bool is_connection_error() { -#ifdef _WIN32 +#ifdef _WIN64 return WSAGetLastError() != WSAEWOULDBLOCK; #else return errno != EINPROGRESS; @@ -3943,7 +3937,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { return ret; } -#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ +#if !defined _WIN64 && !defined ANDROID && !defined _AIX && !defined __MVS__ #define USE_IF2IP #endif @@ -4082,7 +4076,7 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { if (!getpeername(sock, reinterpret_cast(&addr), &addr_len)) { -#ifndef _WIN32 +#ifndef _WIN64 if (addr.ss_family == AF_UNIX) { #if defined(__linux__) struct ucred ucred; @@ -6098,7 +6092,7 @@ inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; } -#ifdef _WIN32 +#ifdef _WIN64 // NOTE: This code came up with the following stackoverflow post: // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store inline bool load_system_certs_on_windows(X509_STORE *store) { @@ -6214,10 +6208,10 @@ inline bool load_system_certs_on_macos(X509_STORE *store) { return result; } -#endif // _WIN32 +#endif // _WIN64 #endif // CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN32 +#ifdef _WIN64 class WSInit { public: WSInit() { @@ -6733,7 +6727,7 @@ inline bool SocketStream::wait_writable() const { } inline ssize_t SocketStream::read(char *ptr, size_t size) { -#ifdef _WIN32 +#ifdef _WIN64 size = (std::min)(size, static_cast((std::numeric_limits::max)())); #else @@ -6781,7 +6775,7 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { inline ssize_t SocketStream::write(const char *ptr, size_t size) { if (!wait_writable()) { return -1; } -#if defined(_WIN32) && !defined(_WIN64) +#if defined(_WIN64) && !defined(_WIN64) size = (std::min)(size, static_cast((std::numeric_limits::max)())); #endif @@ -6944,7 +6938,7 @@ inline bool RegexMatcher::match(Request &request) const { inline Server::Server() : new_task_queue( [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { -#ifndef _WIN32 +#ifndef _WIN64 signal(SIGPIPE, SIG_IGN); #endif } @@ -7616,7 +7610,7 @@ inline bool Server::listen_internal() { std::unique_ptr task_queue(new_task_queue()); while (svr_sock_ != INVALID_SOCKET) { -#ifndef _WIN32 +#ifndef _WIN64 if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { #endif auto val = detail::select_read(svr_sock_, idle_interval_sec_, @@ -7625,11 +7619,11 @@ inline bool Server::listen_internal() { task_queue->on_idle(); continue; } -#ifndef _WIN32 +#ifndef _WIN64 } #endif -#if defined _WIN32 +#if defined _WIN64 // sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT, // OVERLAPPED socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); @@ -10114,7 +10108,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (ret < 0) { auto err = SSL_get_error(ssl_, ret); auto n = 1000; -#ifdef _WIN32 +#ifdef _WIN64 while (--n >= 0 && (err == SSL_ERROR_WANT_READ || (err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT))) { @@ -10149,7 +10143,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { if (ret < 0) { auto err = SSL_get_error(ssl_, ret); auto n = 1000; -#ifdef _WIN32 +#ifdef _WIN64 while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || (err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT))) { @@ -10541,13 +10535,13 @@ inline bool SSLClient::load_certs() { } } else { auto loaded = false; -#ifdef _WIN32 +#ifdef _WIN64 loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); #elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ defined(TARGET_OS_OSX) loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); -#endif // _WIN32 +#endif // _WIN64 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } } }); From 802743264cecd24291b703dea9d7caa42490ef5c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 8 Jul 2025 23:53:52 -0400 Subject: [PATCH 0475/1222] Remove incorrect comment --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index a2a7f2adf5..34955002df 100644 --- a/httplib.h +++ b/httplib.h @@ -4084,7 +4084,7 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == 0) { port = ucred.pid; } -#elif defined(SOL_LOCAL) && defined(SO_PEERPID) // __APPLE__ +#elif defined(SOL_LOCAL) && defined(SO_PEERPID) pid_t pid; socklen_t len = sizeof(pid); if (getsockopt(sock, SOL_LOCAL, SO_PEERPID, &pid, &len) == 0) { From 17ba303889b8d4d719be3879a70639ab653efb99 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 9 Jul 2025 07:10:09 -0400 Subject: [PATCH 0476/1222] Merge commit from fork * Fix HTTP Header Smuggling due to insecure trailers merge * Improve performance --- httplib.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++-- test/test.cc | 69 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 156 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 34955002df..e6c55344af 100644 --- a/httplib.h +++ b/httplib.h @@ -450,6 +450,10 @@ struct hash { } }; +template +using unordered_set = std::unordered_set; + } // namespace case_ignore // This is based on @@ -710,6 +714,7 @@ struct Request { std::string matched_route; Params params; Headers headers; + Headers trailers; std::string body; std::string remote_addr; @@ -744,6 +749,10 @@ struct Request { size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); + bool has_trailer(const std::string &key) const; + std::string get_trailer_value(const std::string &key, size_t id = 0) const; + size_t get_trailer_value_count(const std::string &key) const; + bool has_param(const std::string &key) const; std::string get_param_value(const std::string &key, size_t id = 0) const; size_t get_param_value_count(const std::string &key) const; @@ -765,6 +774,7 @@ struct Response { int status = -1; std::string reason; Headers headers; + Headers trailers; std::string body; std::string location; // Redirect location @@ -776,6 +786,10 @@ struct Response { size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); + bool has_trailer(const std::string &key) const; + std::string get_trailer_value(const std::string &key, size_t id = 0) const; + size_t get_trailer_value_count(const std::string &key) const; + void set_redirect(const std::string &url, int status = StatusCode::Found_302); void set_content(const char *s, size_t n, const std::string &content_type); void set_content(const std::string &s, const std::string &content_type); @@ -4727,6 +4741,42 @@ inline ReadContentResult read_content_chunked(Stream &strm, T &x, // chunked transfer coding data without the final CRLF. if (!line_reader.getline()) { return ReadContentResult::Success; } + // RFC 7230 Section 4.1.2 - Headers prohibited in trailers + thread_local case_ignore::unordered_set prohibited_trailers = { + // Message framing + "transfer-encoding", "content-length", + + // Routing + "host", + + // Authentication + "authorization", "www-authenticate", "proxy-authenticate", + "proxy-authorization", "cookie", "set-cookie", + + // Request modifiers + "cache-control", "expect", "max-forwards", "pragma", "range", "te", + + // Response control + "age", "expires", "date", "location", "retry-after", "vary", "warning", + + // Payload processing + "content-encoding", "content-type", "content-range", "trailer"}; + + // Parse declared trailer headers once for performance + case_ignore::unordered_set declared_trailers; + if (has_header(x.headers, "Trailer")) { + auto trailer_header = get_header_value(x.headers, "Trailer", "", 0); + auto len = std::strlen(trailer_header); + + split(trailer_header, trailer_header + len, ',', + [&](const char *b, const char *e) { + std::string key(b, e); + if (prohibited_trailers.find(key) == prohibited_trailers.end()) { + declared_trailers.insert(key); + } + }); + } + size_t trailer_header_count = 0; while (strcmp(line_reader.ptr(), "\r\n") != 0) { if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { @@ -4744,11 +4794,12 @@ inline ReadContentResult read_content_chunked(Stream &strm, T &x, parse_header(line_reader.ptr(), end, [&](const std::string &key, const std::string &val) { - x.headers.emplace(key, val); + if (declared_trailers.find(key) != declared_trailers.end()) { + x.trailers.emplace(key, val); + trailer_header_count++; + } }); - trailer_header_count++; - if (!line_reader.getline()) { return ReadContentResult::Error; } } @@ -6468,6 +6519,24 @@ inline void Request::set_header(const std::string &key, } } +inline bool Request::has_trailer(const std::string &key) const { + return trailers.find(key) != trailers.end(); +} + +inline std::string Request::get_trailer_value(const std::string &key, + size_t id) const { + auto rng = trailers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Request::get_trailer_value_count(const std::string &key) const { + auto r = trailers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} + inline bool Request::has_param(const std::string &key) const { return params.find(key) != params.end(); } @@ -6571,6 +6640,23 @@ inline void Response::set_header(const std::string &key, headers.emplace(key, val); } } +inline bool Response::has_trailer(const std::string &key) const { + return trailers.find(key) != trailers.end(); +} + +inline std::string Response::get_trailer_value(const std::string &key, + size_t id) const { + auto rng = trailers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } + return std::string(); +} + +inline size_t Response::get_trailer_value_count(const std::string &key) const { + auto r = trailers.equal_range(key); + return static_cast(std::distance(r.first, r.second)); +} inline void Response::set_redirect(const std::string &url, int stat) { if (detail::fields::is_field_value(url)) { diff --git a/test/test.cc b/test/test.cc index 023e92602f..7e397de322 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4886,8 +4886,22 @@ TEST_F(ServerTest, GetStreamedChunkedWithTrailer) { ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(std::string("123456789"), res->body); - EXPECT_EQ(std::string("DummyVal1"), res->get_header_value("Dummy1")); - EXPECT_EQ(std::string("DummyVal2"), res->get_header_value("Dummy2")); + + EXPECT_TRUE(res->has_header("Trailer")); + EXPECT_EQ(1U, res->get_header_value_count("Trailer")); + EXPECT_EQ(std::string("Dummy1, Dummy2"), res->get_header_value("Trailer")); + + // Trailers are now stored separately from headers (security fix) + EXPECT_EQ(2U, res->trailers.size()); + EXPECT_TRUE(res->has_trailer("Dummy1")); + EXPECT_TRUE(res->has_trailer("Dummy2")); + EXPECT_FALSE(res->has_trailer("Dummy3")); + EXPECT_EQ(std::string("DummyVal1"), res->get_trailer_value("Dummy1")); + EXPECT_EQ(std::string("DummyVal2"), res->get_trailer_value("Dummy2")); + + // Verify trailers are NOT in headers (security verification) + EXPECT_EQ(std::string(""), res->get_header_value("Dummy1")); + EXPECT_EQ(std::string(""), res->get_header_value("Dummy2")); } TEST_F(ServerTest, LargeChunkedPost) { @@ -10567,3 +10581,54 @@ TEST(ClientInThreadTest, Issue2068) { t.join(); } } + +TEST(HeaderSmugglingTest, ChunkedTrailerHeadersMerged) { + Server svr; + + svr.Get("/", [](const Request &req, Response &res) { + EXPECT_EQ(2U, req.trailers.size()); + + EXPECT_FALSE(req.has_trailer("[invalid key...]")); + + // Denied + EXPECT_FALSE(req.has_trailer("Content-Length")); + EXPECT_FALSE(req.has_trailer("X-Forwarded-For")); + + // Accepted + EXPECT_TRUE(req.has_trailer("X-Hello")); + EXPECT_EQ(req.get_trailer_value("X-Hello"), "hello"); + + EXPECT_TRUE(req.has_trailer("X-World")); + EXPECT_EQ(req.get_trailer_value("X-World"), "world"); + + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + const std::string req = "GET / HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "Trailer: X-Hello, X-World, X-AAA, X-BBB\r\n" + "\r\n" + "0\r\n" + "Content-Length: 10\r\n" + "Host: internal.local\r\n" + "Content-Type: malicious/content\r\n" + "Cookie: any\r\n" + "Set-Cookie: any\r\n" + "X-Forwarded-For: attacker.com\r\n" + "X-Real-Ip: 1.1.1.1\r\n" + "X-Hello: hello\r\n" + "X-World: world\r\n" + "\r\n"; + + std::string res; + ASSERT_TRUE(send_request(1, req, &res)); +} From b5b2a1d1c86265881bb44b85ff109837728bc5e4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 9 Jul 2025 18:10:57 -0400 Subject: [PATCH 0477/1222] Change uint64_t to size_t --- httplib.h | 101 +++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/httplib.h b/httplib.h index e6c55344af..07310e41eb 100644 --- a/httplib.h +++ b/httplib.h @@ -577,8 +577,8 @@ using Headers = using Params = std::multimap; using Match = std::smatch; -using DownloadProgress = std::function; -using UploadProgress = std::function; +using DownloadProgress = std::function; +using UploadProgress = std::function; struct Response; using ResponseHandler = std::function; @@ -674,9 +674,8 @@ struct FormDataProvider { }; using FormDataProviderItems = std::vector; -using ContentReceiverWithProgress = - std::function; +using ContentReceiverWithProgress = std::function; using ContentReceiver = std::function; @@ -744,8 +743,8 @@ struct Request { bool has_header(const std::string &key) const; std::string get_header_value(const std::string &key, const char *def = "", size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, - size_t id = 0) const; + size_t get_header_value_u64(const std::string &key, size_t def = 0, + size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); @@ -781,8 +780,8 @@ struct Response { bool has_header(const std::string &key) const; std::string get_header_value(const std::string &key, const char *def = "", size_t id = 0) const; - uint64_t get_header_value_u64(const std::string &key, uint64_t def = 0, - size_t id = 0) const; + size_t get_header_value_u64(const std::string &key, size_t def = 0, + size_t id = 0) const; size_t get_header_value_count(const std::string &key) const; void set_header(const std::string &key, const std::string &val); @@ -1334,8 +1333,8 @@ class Result { std::string get_request_header_value(const std::string &key, const char *def = "", size_t id = 0) const; - uint64_t get_request_header_value_u64(const std::string &key, - uint64_t def = 0, size_t id = 0) const; + size_t get_request_header_value_u64(const std::string &key, size_t def = 0, + size_t id = 0) const; size_t get_request_header_value_count(const std::string &key) const; private: @@ -2010,9 +2009,9 @@ inline bool is_numeric(const std::string &str) { [](unsigned char c) { return std::isdigit(c); }); } -inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, uint64_t def, - size_t id, bool &is_invalid_value) { +inline size_t get_header_value_u64(const Headers &headers, + const std::string &key, size_t def, + size_t id, bool &is_invalid_value) { is_invalid_value = false; auto rng = headers.equal_range(key); auto it = rng.first; @@ -2027,22 +2026,22 @@ inline uint64_t get_header_value_u64(const Headers &headers, return def; } -inline uint64_t get_header_value_u64(const Headers &headers, - const std::string &key, uint64_t def, - size_t id) { +inline size_t get_header_value_u64(const Headers &headers, + const std::string &key, size_t def, + size_t id) { bool dummy = false; return get_header_value_u64(headers, key, def, id, dummy); } } // namespace detail -inline uint64_t Request::get_header_value_u64(const std::string &key, - uint64_t def, size_t id) const { +inline size_t Request::get_header_value_u64(const std::string &key, size_t def, + size_t id) const { return detail::get_header_value_u64(headers, key, def, id); } -inline uint64_t Response::get_header_value_u64(const std::string &key, - uint64_t def, size_t id) const { +inline size_t Response::get_header_value_u64(const std::string &key, size_t def, + size_t id) const { return detail::get_header_value_u64(headers, key, def, id); } @@ -2228,9 +2227,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) { return os; } -inline uint64_t Result::get_request_header_value_u64(const std::string &key, - uint64_t def, - size_t id) const { +inline size_t Result::get_request_header_value_u64(const std::string &key, + size_t def, + size_t id) const { return detail::get_header_value_u64(request_headers_, key, def, id); } @@ -4617,19 +4616,19 @@ inline bool read_headers(Stream &strm, Headers &headers) { return true; } -inline bool read_content_with_length(Stream &strm, uint64_t len, +inline bool read_content_with_length(Stream &strm, size_t len, DownloadProgress progress, ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; + size_t r = 0; while (r < len) { auto read_len = static_cast(len - r); auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); if (n <= 0) { return false; } if (!out(buf, static_cast(n), r, len)) { return false; } - r += static_cast(n); + r += static_cast(n); if (progress) { if (!progress(r, len)) { return false; } @@ -4639,14 +4638,14 @@ inline bool read_content_with_length(Stream &strm, uint64_t len, return true; } -inline void skip_content_with_length(Stream &strm, uint64_t len) { +inline void skip_content_with_length(Stream &strm, size_t len) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; + size_t r = 0; while (r < len) { auto read_len = static_cast(len - r); auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); if (n <= 0) { return; } - r += static_cast(n); + r += static_cast(n); } } @@ -4660,7 +4659,7 @@ inline ReadContentResult read_content_without_length(Stream &strm, size_t payload_max_length, ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; - uint64_t r = 0; + size_t r = 0; for (;;) { auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); if (n == 0) { return ReadContentResult::Success; } @@ -4668,14 +4667,14 @@ read_content_without_length(Stream &strm, size_t payload_max_length, // Check if adding this data would exceed the payload limit if (r > payload_max_length || - payload_max_length - r < static_cast(n)) { + payload_max_length - r < static_cast(n)) { return ReadContentResult::PayloadTooLarge; } if (!out(buf, static_cast(n), r, 0)) { return ReadContentResult::Error; } - r += static_cast(n); + r += static_cast(n); } return ReadContentResult::Success; @@ -4693,7 +4692,7 @@ inline ReadContentResult read_content_chunked(Stream &strm, T &x, if (!line_reader.getline()) { return ReadContentResult::Error; } unsigned long chunk_len; - uint64_t total_len = 0; + size_t total_len = 0; while (true) { char *end_ptr; @@ -4845,7 +4844,7 @@ bool prepare_content_receiver(T &x, int &status, if (decompressor) { if (decompressor->is_valid()) { ContentReceiverWithProgress out = [&](const char *buf, size_t n, - uint64_t off, uint64_t len) { + size_t off, size_t len) { return decompressor->decompress(buf, n, [&](const char *buf2, size_t n2) { return receiver(buf2, n2, off, len); @@ -4859,8 +4858,8 @@ bool prepare_content_receiver(T &x, int &status, } } - ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, - uint64_t len) { + ContentReceiverWithProgress out = [&](const char *buf, size_t n, size_t off, + size_t len) { return receiver(buf, n, off, len); }; return callback(std::move(out)); @@ -4899,9 +4898,9 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } } else { auto is_invalid_value = false; - auto len = get_header_value_u64( - x.headers, "Content-Length", - (std::numeric_limits::max)(), 0, is_invalid_value); + auto len = get_header_value_u64(x.headers, "Content-Length", + (std::numeric_limits::max)(), + 0, is_invalid_value); if (is_invalid_value) { ret = false; @@ -7568,13 +7567,13 @@ inline bool Server::read_content_core( } multipart_form_data_parser.set_boundary(std::move(boundary)); - out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { + out = [&](const char *buf, size_t n, size_t /*off*/, size_t /*len*/) { return multipart_form_data_parser.parse(buf, n, multipart_header, multipart_receiver); }; } else { - out = [receiver](const char *buf, size_t n, uint64_t /*off*/, - uint64_t /*len*/) { return receiver(buf, n); }; + out = [receiver](const char *buf, size_t n, size_t /*off*/, + size_t /*len*/) { return receiver(buf, n); }; } if (req.method == "DELETE" && !req.has_header("Content-Length")) { @@ -9093,21 +9092,21 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, auto out = req.content_receiver ? static_cast( - [&](const char *buf, size_t n, uint64_t off, uint64_t len) { + [&](const char *buf, size_t n, size_t off, size_t len) { if (redirect) { return true; } auto ret = req.content_receiver(buf, n, off, len); if (!ret) { error = Error::Canceled; } return ret; }) : static_cast( - [&](const char *buf, size_t n, uint64_t /*off*/, - uint64_t /*len*/) { + [&](const char *buf, size_t n, size_t /*off*/, + size_t /*len*/) { assert(res.body.size() + n <= res.body.max_size()); res.body.append(buf, n); return true; }); - auto progress = [&](uint64_t current, uint64_t total) { + auto progress = [&](size_t current, size_t total) { if (!req.download_progress || redirect) { return true; } auto ret = req.download_progress(current, total); if (!ret) { error = Error::Canceled; } @@ -9258,7 +9257,7 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, req.response_handler = std::move(response_handler); req.content_receiver = [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { + size_t /*offset*/, size_t /*total_length*/) { return content_receiver(data, data_length); }; req.download_progress = std::move(progress); @@ -9448,7 +9447,7 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, req.body = body; req.content_receiver = [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { + size_t /*offset*/, size_t /*total_length*/) { return content_receiver(data, data_length); }; req.download_progress = std::move(progress); @@ -9600,7 +9599,7 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, req.body = body; req.content_receiver = [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { + size_t /*offset*/, size_t /*total_length*/) { return content_receiver(data, data_length); }; req.download_progress = std::move(progress); @@ -9755,7 +9754,7 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, req.body = body; req.content_receiver = [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { + size_t /*offset*/, size_t /*total_length*/) { return content_receiver(data, data_length); }; req.download_progress = std::move(progress); From ecfd84c1712b1bfe92107e37843f97ecec537da9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 9 Jul 2025 23:57:47 -0400 Subject: [PATCH 0478/1222] Release v0.23.0 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 07310e41eb..512f83c4a8 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,7 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.22.0" +#define CPPHTTPLIB_VERSION "0.23.0" /* * Platform compatibility check From 53ea9e8bb4750294ef820a213c4b2758b68117f2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 10 Jul 2025 00:47:45 -0400 Subject: [PATCH 0479/1222] Fix #2111 (#2179) --- httplib.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/httplib.h b/httplib.h index 512f83c4a8..830c81e42a 100644 --- a/httplib.h +++ b/httplib.h @@ -3215,6 +3215,23 @@ inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { template inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { +#ifdef __APPLE__ + if (sock >= FD_SETSIZE) { return -1; } + + fd_set fds, *rfds, *wfds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + rfds = (Read ? &fds : nullptr); + wfds = (Read ? nullptr : &fds); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + return handle_EINTR([&]() { + return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); + }); +#else struct pollfd pfd; pfd.fd = sock; pfd.events = (Read ? POLLIN : POLLOUT); @@ -3222,6 +3239,7 @@ inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { auto timeout = static_cast(sec * 1000 + usec / 1000); return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); }); +#endif } inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { @@ -3234,6 +3252,36 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { +#ifdef __APPLE__ + if (sock >= FD_SETSIZE) { return Error::Connection; } + + fd_set fdsr, fdsw; + FD_ZERO(&fdsr); + FD_ZERO(&fdsw); + FD_SET(sock, &fdsr); + FD_SET(sock, &fdsw); + + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); + + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, nullptr, &tv); + }); + + if (ret == 0) { return Error::ConnectionTimeout; } + + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } + + return Error::Connection; +#else struct pollfd pfd_read; pfd_read.fd = sock; pfd_read.events = POLLIN | POLLOUT; @@ -3255,6 +3303,7 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, } return Error::Connection; +#endif } inline bool is_socket_alive(socket_t sock) { @@ -8001,6 +8050,16 @@ Server::process_request(Stream &strm, const std::string &remote_addr, res.version = "HTTP/1.1"; res.headers = default_headers_; +#ifdef __APPLE__ + // Socket file descriptor exceeded FD_SETSIZE... + if (strm.socket() >= FD_SETSIZE) { + Headers dummy; + detail::read_headers(strm, dummy); + res.status = StatusCode::InternalServerError_500; + return write_response(strm, close_connection, req, res); + } +#endif + // Request line and headers if (!parse_request_line(line_reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { From 55b38907dcdf3d21d16e0dc21d5ac8336fcb21b7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 10 Jul 2025 00:58:34 -0400 Subject: [PATCH 0480/1222] Resolve #1264 --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 66348cc52b..c079488c7f 100644 --- a/README.md +++ b/README.md @@ -1141,6 +1141,24 @@ Serving HTTP on 0.0.0.0 port 80 ... NOTE ---- +### Regular Expression Stack Overflow + +> [!CAUTION] +> When using complex regex patterns in route handlers, be aware that certain patterns may cause stack overflow during pattern matching. This is a known issue with `std::regex` implementations and affects the `dispatch_request()` method. +> +> ```cpp +> // This pattern can cause stack overflow with large input +> svr.Get(".*", handler); +> ``` +> +> Consider using simpler patterns or path parameters to avoid this issue: +> +> ```cpp +> // Safer alternatives +> svr.Get("/users/:id", handler); // Path parameters +> svr.Get(R"(/api/v\d+/.*)", handler); // More specific patterns +> ``` + ### g++ g++ 4.8 and below cannot build this library since `` in the versions are [broken](https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions). From 8b28577ec6c389988ecebdd4dc1fd66c0a47b8bd Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 10 Jul 2025 01:07:44 -0400 Subject: [PATCH 0481/1222] Resolve #366 --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index c079488c7f..75330a7477 100644 --- a/README.md +++ b/README.md @@ -990,6 +990,25 @@ auto res = cli.Get("/already%20encoded/path"); // Use pre-encoded paths - `true` (default): Automatically encodes spaces, plus signs, newlines, and other special characters - `false`: Sends paths as-is without encoding (useful for pre-encoded URLs) +### Performance Note for Local Connections + +> [!WARNING] +> On Windows systems with improperly configured IPv6 settings, using "localhost" as the hostname may cause significant connection delays (up to 2 seconds per request) due to DNS resolution issues. This affects both client and server operations. For better performance when connecting to local services, use "127.0.0.1" instead of "localhost". +> +> See: https://github.com/yhirose/cpp-httplib/issues/366#issuecomment-593004264 + +```cpp +// May be slower on Windows due to DNS resolution delays +httplib::Client cli("localhost", 8080); +httplib::Server svr; +svr.listen("localhost", 8080); + +// Faster alternative for local connections +httplib::Client cli("127.0.0.1", 8080); +httplib::Server svr; +svr.listen("127.0.0.1", 8080); +``` + Compression ----------- From 7b6867bcdf727f0a12b5e85a91007e8011eb41e5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 10 Jul 2025 22:01:41 -0400 Subject: [PATCH 0482/1222] Fix #2021 (#2180) --- README.md | 2 ++ httplib.h | 6 +++++- test/test.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 75330a7477..b607c205a6 100644 --- a/README.md +++ b/README.md @@ -1084,6 +1084,8 @@ cli.set_address_family(AF_UNIX); "my-socket.sock" can be a relative path or an absolute path. Your application must have the appropriate permissions for the path. You can also use an abstract socket address on Linux. To use an abstract socket address, prepend a null byte ('\x00') to the path. +This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior: + URI Encoding/Decoding Utilities ------------------------------- diff --git a/httplib.h b/httplib.h index 830c81e42a..b1c778a058 100644 --- a/httplib.h +++ b/httplib.h @@ -8844,7 +8844,11 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, } if (!req.has_header("Host")) { - if (is_ssl()) { + // For Unix socket connections, use "localhost" as Host header (similar to + // curl behavior) + if (address_family_ == AF_UNIX) { + req.set_header("Host", "localhost"); + } else if (is_ssl()) { if (port_ == 443) { req.set_header("Host", host_); } else { diff --git a/test/test.cc b/test/test.cc index 7e397de322..f7624bf365 100644 --- a/test/test.cc +++ b/test/test.cc @@ -173,6 +173,48 @@ TEST_F(UnixSocketTest, abstract) { } #endif +TEST_F(UnixSocketTest, HostHeaderAutoSet) { + httplib::Server svr; + std::string received_host_header; + + svr.Get(pattern_, [&](const httplib::Request &req, httplib::Response &res) { + // Capture the Host header sent by the client + auto host_iter = req.headers.find("Host"); + if (host_iter != req.headers.end()) { + received_host_header = host_iter->second; + } + res.set_content(content_, "text/plain"); + }); + + std::thread t{[&] { + ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80)); + }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + ASSERT_TRUE(svr.is_running()); + + // Test that Host header is automatically set to "localhost" for Unix socket + // connections + httplib::Client cli{pathname_}; + cli.set_address_family(AF_UNIX); + ASSERT_TRUE(cli.is_valid()); + + const auto &result = cli.Get(pattern_); + ASSERT_TRUE(result) << "error: " << result.error(); + + const auto &resp = result.value(); + EXPECT_EQ(resp.status, StatusCode::OK_200); + EXPECT_EQ(resp.body, content_); + + // Verify that Host header was automatically set to "localhost" + EXPECT_EQ(received_host_header, "localhost"); +} + #ifndef _WIN32 TEST(SocketStream, wait_writable_UNIX) { int fds[2]; From 1f110b54d8b31c0a6a14d57cbe8630dbc2ff476b Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 11 Jul 2025 22:42:24 -0400 Subject: [PATCH 0483/1222] Chang #error to #warning for the 32-bit environment check except 32-bit Windows --- README.md | 2 +- httplib.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b607c205a6..8553114ff3 100644 --- a/README.md +++ b/README.md @@ -871,7 +871,7 @@ auto res = cli.Post( httplib::Client cli(url, port); // prints: 0 / 000 bytes => 50% complete -auto res = cli.Get("/", [](uint64_t len, uint64_t total) { +auto res = cli.Get("/", [](size_t len, size_t total) { printf("%lld / %lld bytes => %d%% complete\n", len, total, (int)(len*100/total)); diff --git a/httplib.h b/httplib.h index b1c778a058..16aced15d1 100644 --- a/httplib.h +++ b/httplib.h @@ -18,10 +18,10 @@ #error \ "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler." #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8 -#error \ +#warning \ "cpp-httplib doesn't support 32-bit platforms. Please use a 64-bit compiler." #elif defined(__SIZEOF_SIZE_T__) && __SIZEOF_SIZE_T__ < 8 -#error \ +#warning \ "cpp-httplib doesn't support platforms where size_t is less than 64 bits." #endif From dd98d2a24d9dfa1c4ae4e54886bd37b9556c9513 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Wed, 16 Jul 2025 09:47:51 -0700 Subject: [PATCH 0484/1222] build(meson): warn/fail on 32-bit machines (#2181) On 32-bit Windows, meson setup fails with an unclear error: meson.build:25:16: ERROR: Could not get define 'CPPHTTPLIB_VERSION' The actual problem is that httplib.h #errors out. Have the Meson logic explicitly check for a 32-bit host and warn or error, matching the check in httplib.h. Phrase the Windows error in a way that triggers WrapDB CI's unsupported architecture check. --- meson.build | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/meson.build b/meson.build index 1974ae8fea..8f667f8060 100644 --- a/meson.build +++ b/meson.build @@ -18,6 +18,14 @@ project( cxx = meson.get_compiler('cpp') +if cxx.sizeof('void *') != 8 + if host_machine.system() == 'windows' + error('unsupported architecture: cpp-httplib doesn\'t support 32-bit Windows. Please use a 64-bit compiler.') + else + warning('cpp-httplib doesn\'t support 32-bit platforms. Please use a 64-bit compiler.') + endif +endif + # Check just in case downstream decides to edit the source # and add a project version version = meson.project_version() From ca5fe354fb83194bc72a676c4cc4136fca5316d0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 16 Jul 2025 17:49:50 -0400 Subject: [PATCH 0485/1222] Release v0.23.1 --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 16aced15d1..749fa61768 100644 --- a/httplib.h +++ b/httplib.h @@ -8,7 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.23.0" +#define CPPHTTPLIB_VERSION "0.23.1" +#define CPPHTTPLIB_VERSION_NUM "0x001701" /* * Platform compatibility check From 890a2dd85d9f79cdcbf34b9808ec92bec42ad03b Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 24 Jul 2025 17:04:59 -0400 Subject: [PATCH 0486/1222] Fix #2189 --- httplib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/httplib.h b/httplib.h index 749fa61768..74ae478a20 100644 --- a/httplib.h +++ b/httplib.h @@ -2282,6 +2282,10 @@ Client::set_write_timeout(const std::chrono::duration &duration) { cli_->set_write_timeout(duration); } +inline void Client::set_max_timeout(time_t msec) { + cli_->set_max_timeout(msec); +} + template inline void Client::set_max_timeout(const std::chrono::duration &duration) { From 8e8a23e3d24884385f64d03992bbbd1c2ad70256 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 24 Jul 2025 19:35:47 -0400 Subject: [PATCH 0487/1222] Fix #2187 --- httplib.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 74ae478a20..130d200981 100644 --- a/httplib.h +++ b/httplib.h @@ -5440,17 +5440,30 @@ inline bool parse_accept_header(const std::string &s, return; } - try { - accept_entry.quality = std::stod(quality_str); - // Check if quality is in valid range [0.0, 1.0] - if (accept_entry.quality < 0.0 || accept_entry.quality > 1.0) { +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + { + std::istringstream iss(quality_str); + iss >> accept_entry.quality; + + // Check if conversion was successful and entire string was consumed + if (iss.fail() || !iss.eof()) { has_invalid_entry = true; return; } + } +#else + try { + accept_entry.quality = std::stod(quality_str); } catch (...) { has_invalid_entry = true; return; } +#endif + // Check if quality is in valid range [0.0, 1.0] + if (accept_entry.quality < 0.0 || accept_entry.quality > 1.0) { + has_invalid_entry = true; + return; + } } else { // No quality parameter, use entire entry as media type accept_entry.media_type = entry; From c0c36f021defe69033ffbebbf04f54c68c4d0b25 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 29 Jul 2025 19:29:37 -0400 Subject: [PATCH 0488/1222] Fix #2184, #2185 (#2190) * Fix #2184, #2185 * Fix build error * Update * Update --- httplib.h | 245 ++++++++++++++++++++++++++++++++++++++------------- test/test.cc | 52 ++++++++++- 2 files changed, 232 insertions(+), 65 deletions(-) diff --git a/httplib.h b/httplib.h index 130d200981..47928bd646 100644 --- a/httplib.h +++ b/httplib.h @@ -2030,7 +2030,7 @@ inline size_t get_header_value_u64(const Headers &headers, inline size_t get_header_value_u64(const Headers &headers, const std::string &key, size_t def, size_t id) { - bool dummy = false; + auto dummy = false; return get_header_value_u64(headers, key, def, id, dummy); } @@ -2301,15 +2301,19 @@ std::string hosted_at(const std::string &hostname); void hosted_at(const std::string &hostname, std::vector &addrs); +// JavaScript-style URL encoding/decoding functions std::string encode_uri_component(const std::string &value); - std::string encode_uri(const std::string &value); - std::string decode_uri_component(const std::string &value); - std::string decode_uri(const std::string &value); -std::string encode_query_param(const std::string &value); +// RFC 3986 compliant URL component encoding/decoding functions +std::string encode_path_component(const std::string &component); +std::string decode_path_component(const std::string &component); +std::string encode_query_component(const std::string &component, + bool space_as_plus = true); +std::string decode_query_component(const std::string &component, + bool plus_as_space = true); std::string append_query_params(const std::string &path, const Params ¶ms); @@ -2352,8 +2356,6 @@ struct FileStat { int ret_ = -1; }; -std::string decode_path(const std::string &s, bool convert_plus_to_space); - std::string trim_copy(const std::string &s); void divide( @@ -2854,43 +2856,6 @@ inline std::string encode_path(const std::string &s) { return result; } -inline std::string decode_path(const std::string &s, - bool convert_plus_to_space) { - std::string result; - - for (size_t i = 0; i < s.size(); i++) { - if (s[i] == '%' && i + 1 < s.size()) { - if (s[i + 1] == 'u') { - auto val = 0; - if (from_hex_to_i(s, i + 2, 4, val)) { - // 4 digits Unicode codes - char buff[4]; - size_t len = to_utf8(val, buff); - if (len > 0) { result.append(buff, len); } - i += 5; // 'u0000' - } else { - result += s[i]; - } - } else { - auto val = 0; - if (from_hex_to_i(s, i + 1, 2, val)) { - // 2 digits hex codes - result += static_cast(val); - i += 2; // '00' - } else { - result += s[i]; - } - } - } else if (convert_plus_to_space && s[i] == '+') { - result += ' '; - } else { - result += s[i]; - } - } - - return result; -} - inline std::string file_extension(const std::string &path) { std::smatch m; thread_local auto re = std::regex("\\.([a-zA-Z0-9]+)$"); @@ -4615,7 +4580,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) { case_ignore::equal(key, "Referer")) { fn(key, val); } else { - fn(key, decode_path(val, false)); + fn(key, decode_path_component(val)); } return true; @@ -5263,9 +5228,9 @@ inline std::string params_to_query_str(const Params ¶ms) { for (auto it = params.begin(); it != params.end(); ++it) { if (it != params.begin()) { query += "&"; } - query += it->first; + query += encode_query_component(it->first); query += "="; - query += httplib::encode_uri_component(it->second); + query += encode_query_component(it->second); } return query; } @@ -5288,7 +5253,7 @@ inline void parse_query_text(const char *data, std::size_t size, }); if (!key.empty()) { - params.emplace(decode_path(key, true), decode_path(val, true)); + params.emplace(decode_query_component(key), decode_query_component(val)); } }); } @@ -5611,7 +5576,7 @@ class FormDataParser { std::smatch m2; if (std::regex_match(it->second, m2, re_rfc5987_encoding)) { - file_.filename = decode_path(m2[1], false); // override... + file_.filename = decode_path_component(m2[1]); // override... } else { is_valid_ = false; return false; @@ -6517,9 +6482,154 @@ inline std::string decode_uri(const std::string &value) { return result; } -[[deprecated("Use encode_uri_component instead")]] -inline std::string encode_query_param(const std::string &value) { - return encode_uri_component(value); +inline std::string encode_path_component(const std::string &component) { + std::string result; + result.reserve(component.size() * 3); + + for (size_t i = 0; i < component.size(); i++) { + auto c = static_cast(component[i]); + + // Unreserved characters per RFC 3986: ALPHA / DIGIT / "-" / "." / "_" / "~" + if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') { + result += static_cast(c); + } + // Path-safe sub-delimiters: "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / + // "," / ";" / "=" + else if (c == '!' || c == '$' || c == '&' || c == '\'' || c == '(' || + c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || + c == '=') { + result += static_cast(c); + } + // Colon is allowed in path segments except first segment + else if (c == ':') { + result += static_cast(c); + } + // @ is allowed in path + else if (c == '@') { + result += static_cast(c); + } else { + result += '%'; + char hex[3]; + snprintf(hex, sizeof(hex), "%02X", c); + result.append(hex, 2); + } + } + return result; +} + +inline std::string decode_path_component(const std::string &component) { + std::string result; + result.reserve(component.size()); + + for (size_t i = 0; i < component.size(); i++) { + if (component[i] == '%' && i + 1 < component.size()) { + if (component[i + 1] == 'u') { + // Unicode %uXXXX encoding + auto val = 0; + if (detail::from_hex_to_i(component, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = detail::to_utf8(val, buff); + if (len > 0) { result.append(buff, len); } + i += 5; // 'u0000' + } else { + result += component[i]; + } + } else { + // Standard %XX encoding + auto val = 0; + if (detail::from_hex_to_i(component, i + 1, 2, val)) { + // 2 digits hex codes + result += static_cast(val); + i += 2; // 'XX' + } else { + result += component[i]; + } + } + } else { + result += component[i]; + } + } + return result; +} + +inline std::string encode_query_component(const std::string &component, + bool space_as_plus) { + std::string result; + result.reserve(component.size() * 3); + + for (size_t i = 0; i < component.size(); i++) { + auto c = static_cast(component[i]); + + // Unreserved characters per RFC 3986 + if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') { + result += static_cast(c); + } + // Space handling + else if (c == ' ') { + if (space_as_plus) { + result += '+'; + } else { + result += "%20"; + } + } + // Plus sign handling + else if (c == '+') { + if (space_as_plus) { + result += "%2B"; + } else { + result += static_cast(c); + } + } + // Query-safe sub-delimiters (excluding & and = which are query delimiters) + else if (c == '!' || c == '$' || c == '\'' || c == '(' || c == ')' || + c == '*' || c == ',' || c == ';') { + result += static_cast(c); + } + // Colon and @ are allowed in query + else if (c == ':' || c == '@') { + result += static_cast(c); + } + // Forward slash is allowed in query values + else if (c == '/') { + result += static_cast(c); + } + // Question mark is allowed in query values (after first ?) + else if (c == '?') { + result += static_cast(c); + } else { + result += '%'; + char hex[3]; + snprintf(hex, sizeof(hex), "%02X", c); + result.append(hex, 2); + } + } + return result; +} + +inline std::string decode_query_component(const std::string &component, + bool plus_as_space) { + std::string result; + result.reserve(component.size()); + + for (size_t i = 0; i < component.size(); i++) { + if (component[i] == '%' && i + 2 < component.size()) { + std::string hex = component.substr(i + 1, 2); + char *end; + unsigned long value = std::strtoul(hex.c_str(), &end, 16); + if (end == hex.c_str() + 2) { + result += static_cast(value); + i += 2; + } else { + result += component[i]; + } + } else if (component[i] == '+' && plus_as_space) { + result += ' '; // + becomes space in form-urlencoded + } else { + result += component[i]; + } + } + return result; } inline std::string append_query_params(const std::string &path, @@ -7404,8 +7514,8 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { detail::divide(req.target, '?', [&](const char *lhs_data, std::size_t lhs_size, const char *rhs_data, std::size_t rhs_size) { - req.path = detail::decode_path( - std::string(lhs_data, lhs_size), false); + req.path = + decode_path_component(std::string(lhs_data, lhs_size)); detail::parse_query_text(rhs_data, rhs_size, req.params); }); } @@ -8678,7 +8788,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { if (next_host.empty()) { next_host = host_; } if (next_path.empty()) { next_path = "/"; } - auto path = detail::decode_path(next_path, true) + next_query; + auto path = decode_query_component(next_path, true) + next_query; // Same host redirect - use current client if (next_scheme == scheme && next_host == host_ && next_port == port_) { @@ -8966,15 +9076,28 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, { detail::BufferStream bstrm; - const auto &path_with_query = - req.params.empty() ? req.path - : append_query_params(req.path, req.params); + // Extract path and query from req.path + std::string path_part, query_part; + auto query_pos = req.path.find('?'); + if (query_pos != std::string::npos) { + path_part = req.path.substr(0, query_pos); + query_part = req.path.substr(query_pos + 1); + } else { + path_part = req.path; + query_part = ""; + } - const auto &path = - path_encode_ ? detail::encode_path(path_with_query) : path_with_query; + // Encode path and query + auto path_with_query = + path_encode_ ? detail::encode_path(path_part) : path_part; - detail::write_request_line(bstrm, req.method, path); + detail::parse_query_text(query_part, req.params); + if (!req.params.empty()) { + path_with_query = append_query_params(path_with_query, req.params); + } + // Write request line and headers + detail::write_request_line(bstrm, req.method, path_with_query); header_writer_(bstrm, req.headers); // Flush buffer diff --git a/test/test.cc b/test/test.cc index f7624bf365..0e4fd42d58 100644 --- a/test/test.cc +++ b/test/test.cc @@ -301,9 +301,8 @@ TEST(StartupTest, WSAStartup) { TEST(DecodePathTest, PercentCharacter) { EXPECT_EQ( - detail::decode_path( - R"(descrip=Gastos%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1%C3%91%206)", - false), + decode_path_component( + R"(descrip=Gastos%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1%C3%91%206)"), u8"descrip=Gastos áéíóúñÑ 6"); } @@ -313,7 +312,7 @@ TEST(DecodePathTest, PercentCharacterNUL) { expected.push_back('\0'); expected.push_back('x'); - EXPECT_EQ(detail::decode_path("x%00x", false), expected); + EXPECT_EQ(decode_path_component("x%00x"), expected); } TEST(EncodeQueryParamTest, ParseUnescapedChararactersTest) { @@ -9942,6 +9941,51 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { } } +TEST(RedirectTest, RedirectToUrlWithPlusInQueryParameters) { + Server svr; + + svr.Get("/", [](const Request & /*req*/, Response &res) { + res.set_redirect(R"(/hello?key=AByz09+~-._%20%26%3F%C3%BC%2B)"); + }); + + svr.Get("/hello", [](const Request &req, Response &res) { + res.set_content(req.get_param_value("key"), "text/plain"); + }); + + auto thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + { + Client cli(HOST, PORT); + cli.set_follow_location(true); + + auto res = cli.Get("/"); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("AByz09 ~-._ &?ü+", res->body); + } +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(RedirectTest, Issue2185_Online) { + SSLClient client("github.com"); + client.set_follow_location(true); + + auto res = client.Get("/Coollab-Art/Coollab/releases/download/1.1.1_UI-Scale/" + "Coollab-Windows.zip"); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(9920427U, res->body.size()); +} +#endif + TEST(VulnerabilityTest, CRLFInjection) { Server svr; From a5d4c143e52441307387ec65b05a2482083b6cbc Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 29 Jul 2025 19:47:48 -0400 Subject: [PATCH 0489/1222] Release v0.24.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 47928bd646..8e65d7d9e0 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.23.1" -#define CPPHTTPLIB_VERSION_NUM "0x001701" +#define CPPHTTPLIB_VERSION "0.24.0" +#define CPPHTTPLIB_VERSION_NUM "0x001800" /* * Platform compatibility check From 0b3758ec36bea9ec567aedc8fa1cacbdbc3a83d6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 30 Jul 2025 17:39:40 -0400 Subject: [PATCH 0490/1222] Fix problem with Windows version check --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 8e65d7d9e0..fc175484c8 100644 --- a/httplib.h +++ b/httplib.h @@ -27,7 +27,7 @@ #endif #ifdef _WIN32 -#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0602 +#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00 #error \ "cpp-httplib doesn't support Windows 8 or lower. Please use Windows 10 or later." #endif From acf28a362df8b5cc6d1d17d706ec33467fcdbbae Mon Sep 17 00:00:00 2001 From: Thomas Beutlich <115483027+thbeu@users.noreply.github.com> Date: Sat, 2 Aug 2025 02:16:43 +0200 Subject: [PATCH 0491/1222] #2191 Check for minimum required Windows version (#2192) --- CMakeLists.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 99d9bc4738..cf036b4066 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,12 +111,16 @@ option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build. option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON) # Defaults to static library option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) -if (BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) +if(BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) # Necessary for Windows if building shared libs # See https://stackoverflow.com/a/40743080 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() +if( CMAKE_SYSTEM_NAME MATCHES "Windows" AND ${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0") + message(SEND_ERROR "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.") +endif() + # Set some variables that are used in-tree and while building based on our options set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}) @@ -243,8 +247,8 @@ add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11) target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC} - $ - $ + $ + $ ) # Always require threads @@ -340,6 +344,6 @@ if(HTTPLIB_INSTALL) endif() if(HTTPLIB_TEST) - include(CTest) - add_subdirectory(test) + include(CTest) + add_subdirectory(test) endif() From b52d7d8411f92ab82588ce20f14559062788c71b Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 6 Aug 2025 17:38:18 -0400 Subject: [PATCH 0492/1222] ErrorLogger support (#870) (#2195) --- Dockerfile | 4 +- docker/main.cc | 212 +++++++++++++++++++++++++++++++++++---------- httplib.h | 228 ++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 379 insertions(+), 65 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4abae1793e..67aa028b7e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,6 @@ FROM scratch COPY --from=builder /build/server /server COPY docker/html/index.html /html/index.html EXPOSE 80 -CMD ["/server"] + +ENTRYPOINT ["/server"] +CMD ["0.0.0.0", "80", "/", "/html"] diff --git a/docker/main.cc b/docker/main.cc index 62d0c74f5f..784c803b02 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -5,77 +5,203 @@ // MIT License // +#include #include #include #include #include #include +#include #include #include -constexpr auto error_html = R"( -{} {} - -

404 Not Found

-
cpp-httplib/{}
- - -)"; +using namespace httplib; -void sigint_handler(int s) { exit(1); } +auto SERVER_NAME = + std::format("cpp-httplib-nginxish-server/{}", CPPHTTPLIB_VERSION); -std::string time_local() { - auto p = std::chrono::system_clock::now(); - auto t = std::chrono::system_clock::to_time_t(p); +Server svr; + +void signal_handler(int signal) { + if (signal == SIGINT || signal == SIGTERM) { + std::cout << std::format("\nReceived signal, shutting down gracefully...") + << std::endl; + svr.stop(); + } +} + +std::string get_nginx_time_format() { + auto now = std::chrono::system_clock::now(); + auto time_t = std::chrono::system_clock::to_time_t(now); std::stringstream ss; - ss << std::put_time(std::localtime(&t), "%d/%b/%Y:%H:%M:%S %z"); + ss << std::put_time(std::localtime(&time_t), "%d/%b/%Y:%H:%M:%S %z"); return ss.str(); } -std::string log(auto &req, auto &res) { - auto remote_user = "-"; // TODO: - auto request = std::format("{} {} {}", req.method, req.path, req.version); - auto body_bytes_sent = res.get_header_value("Content-Length"); - auto http_referer = "-"; // TODO: - auto http_user_agent = req.get_header_value("User-Agent", "-"); - - // NOTE: From NGINX default access log format - // log_format combined '$remote_addr - $remote_user [$time_local] ' - // '"$request" $status $body_bytes_sent ' - // '"$http_referer" "$http_user_agent"'; - return std::format(R"({} - {} [{}] "{}" {} {} "{}" "{}")", req.remote_addr, - remote_user, time_local(), request, res.status, - body_bytes_sent, http_referer, http_user_agent); +std::string get_nginx_error_time_format() { + auto now = std::chrono::system_clock::now(); + auto time_t = std::chrono::system_clock::to_time_t(now); + + std::stringstream ss; + ss << std::put_time(std::localtime(&time_t), "%Y/%m/%d %H:%M:%S"); + return ss.str(); +} + +std::string get_client_ip(const Request &req) { + // Check for X-Forwarded-For header first (common in reverse proxy setups) + auto forwarded_for = req.get_header_value("X-Forwarded-For"); + if (!forwarded_for.empty()) { + // Get the first IP if there are multiple + auto comma_pos = forwarded_for.find(','); + if (comma_pos != std::string::npos) { + return forwarded_for.substr(0, comma_pos); + } + return forwarded_for; + } + + // Check for X-Real-IP header + auto real_ip = req.get_header_value("X-Real-IP"); + if (!real_ip.empty()) { return real_ip; } + + // Fallback to remote address (though cpp-httplib doesn't provide this + // directly) For demonstration, we'll use a placeholder + return "127.0.0.1"; } -int main(int argc, const char **argv) { - signal(SIGINT, sigint_handler); +// NGINX Combined log format: +// $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent +// "$http_referer" "$http_user_agent" +void nginx_access_logger(const Request &req, const Response &res) { + std::string remote_addr = get_client_ip(req); + std::string remote_user = + "-"; // cpp-httplib doesn't have built-in auth user tracking + std::string time_local = get_nginx_time_format(); + std::string request = + std::format("{} {} {}", req.method, req.path, req.version); + int status = res.status; + size_t body_bytes_sent = res.body.size(); + std::string http_referer = req.get_header_value("Referer"); + if (http_referer.empty()) http_referer = "-"; + std::string http_user_agent = req.get_header_value("User-Agent"); + if (http_user_agent.empty()) http_user_agent = "-"; + + std::cout << std::format("{} - {} [{}] \"{}\" {} {} \"{}\" \"{}\"", + remote_addr, remote_user, time_local, request, + status, body_bytes_sent, http_referer, + http_user_agent) + << std::endl; +} - auto base_dir = "./html"; - auto host = "0.0.0.0"; - auto port = 80; +// NGINX Error log format: +// YYYY/MM/DD HH:MM:SS [level] message, client: client_ip, request: "request", +// host: "host" +void nginx_error_logger(const Error &err, const Request *req) { + std::string time_local = get_nginx_error_time_format(); + std::string level = "error"; + + if (req) { + std::string client_ip = get_client_ip(*req); + std::string request = + std::format("{} {} {}", req->method, req->path, req->version); + std::string host = req->get_header_value("Host"); + if (host.empty()) host = "-"; + + std::cerr << std::format("{} [{}] {}, client: {}, request: " + "\"{}\", host: \"{}\"", + time_local, level, to_string(err), client_ip, + request, host) + << std::endl; + } else { + // If no request context, just log the error + std::cerr << std::format("{} [{}] {}", time_local, level, to_string(err)) + << std::endl; + } +} - httplib::Server svr; +void print_usage(const char *program_name) { + std::cout << std::format("Usage: {} " + "", + program_name) + << std::endl; - svr.set_error_handler([](auto & /*req*/, auto &res) { - auto body = - std::format(error_html, res.status, httplib::status_message(res.status), - CPPHTTPLIB_VERSION); + std::cout << std::format("Example: {} localhost 8080 /var/www/html .", + program_name) + << std::endl; +} - res.set_content(body, "text/html"); +int main(int argc, char *argv[]) { + if (argc != 5) { + print_usage(argv[0]); + return 1; + } + + std::string hostname = argv[1]; + auto port = std::atoi(argv[2]); + std::string mount_point = argv[3]; + std::string document_root = argv[4]; + + svr.set_logger(nginx_access_logger); + svr.set_error_logger(nginx_error_logger); + + auto ret = svr.set_mount_point(mount_point, document_root); + if (!ret) { + std::cerr + << std::format( + "Error: Cannot mount '{}' to '{}'. Directory may not exist.", + mount_point, document_root) + << std::endl; + return 1; + } + + svr.set_file_extension_and_mimetype_mapping("html", "text/html"); + svr.set_file_extension_and_mimetype_mapping("htm", "text/html"); + svr.set_file_extension_and_mimetype_mapping("css", "text/css"); + svr.set_file_extension_and_mimetype_mapping("js", "text/javascript"); + svr.set_file_extension_and_mimetype_mapping("json", "application/json"); + svr.set_file_extension_and_mimetype_mapping("xml", "application/xml"); + svr.set_file_extension_and_mimetype_mapping("png", "image/png"); + svr.set_file_extension_and_mimetype_mapping("jpg", "image/jpeg"); + svr.set_file_extension_and_mimetype_mapping("jpeg", "image/jpeg"); + svr.set_file_extension_and_mimetype_mapping("gif", "image/gif"); + svr.set_file_extension_and_mimetype_mapping("svg", "image/svg+xml"); + svr.set_file_extension_and_mimetype_mapping("ico", "image/x-icon"); + svr.set_file_extension_and_mimetype_mapping("pdf", "application/pdf"); + svr.set_file_extension_and_mimetype_mapping("zip", "application/zip"); + svr.set_file_extension_and_mimetype_mapping("txt", "text/plain"); + + svr.set_error_handler([](const Request & /*req*/, Response &res) { + if (res.status == 404) { + res.set_content( + std::format( + "404 Not Found" + "

404 Not Found

" + "

The requested resource was not found on this server.

" + "

{}

", + SERVER_NAME), + "text/html"); + } }); - svr.set_logger( - [](auto &req, auto &res) { std::cout << log(req, res) << std::endl; }); + svr.set_pre_routing_handler([](const Request & /*req*/, Response &res) { + res.set_header("Server", SERVER_NAME); + return Server::HandlerResponse::Unhandled; + }); - svr.set_mount_point("/", base_dir); + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); - std::cout << std::format("Serving HTTP on {0} port {1} ...", host, port) + std::cout << std::format("Serving HTTP on {}:{}", hostname, port) + << std::endl; + std::cout << std::format("Mount point: {} -> {}", mount_point, document_root) << std::endl; + std::cout << std::format("Press Ctrl+C to shutdown gracefully...") + << std::endl; + + ret = svr.listen(hostname, port); - auto ret = svr.listen(host, port); + std::cout << std::format("Server has been shut down.") << std::endl; return ret ? 0 : 1; } diff --git a/httplib.h b/httplib.h index fc175484c8..c6976917e0 100644 --- a/httplib.h +++ b/httplib.h @@ -949,6 +949,10 @@ class ThreadPool final : public TaskQueue { using Logger = std::function; +// Forward declaration for Error type +enum class Error; +using ErrorLogger = std::function; + using SocketOptions = std::function; namespace detail { @@ -1109,6 +1113,7 @@ class Server { Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); Server &set_logger(Logger logger); Server &set_pre_compression_logger(Logger logger); + Server &set_error_logger(ErrorLogger error_logger); Server &set_address_family(int family); Server &set_tcp_nodelay(bool on); @@ -1220,6 +1225,11 @@ class Server { virtual bool process_and_close_socket(socket_t sock); + void output_log(const Request &req, const Response &res) const; + void output_pre_compression_log(const Request &req, + const Response &res) const; + void output_error_log(const Error &err, const Request *req) const; + std::atomic is_running_{false}; std::atomic is_decommissioned{false}; @@ -1251,8 +1261,10 @@ class Server { HandlerWithResponse pre_request_handler_; Expect100ContinueHandler expect_100_continue_handler_; + mutable std::mutex logger_mutex_; Logger logger_; Logger pre_compression_logger_; + ErrorLogger error_logger_; int address_family_ = AF_UNSPEC; bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; @@ -1281,6 +1293,22 @@ enum class Error { Compression, ConnectionTimeout, ProxyConnection, + ResourceExhaustion, + TooManyFormDataFiles, + ExceedMaxPayloadSize, + ExceedUriMaxLength, + ExceedMaxSocketDescriptorCount, + InvalidRequestLine, + InvalidHTTPMethod, + InvalidHTTPVersion, + InvalidHeaders, + MultipartParsing, + OpenFile, + Listen, + GetSockName, + UnsupportedAddressFamily, + HTTPParsing, + InvalidRangeHeader, // For internal use only SSLPeerCouldBeClosed_, @@ -1525,6 +1553,7 @@ class ClientImpl { #endif void set_logger(Logger logger); + void set_error_logger(ErrorLogger error_logger); protected: struct Socket { @@ -1557,6 +1586,9 @@ class ClientImpl { void copy_settings(const ClientImpl &rhs); + void output_log(const Request &req, const Response &res) const; + void output_error_log(const Error &err, const Request *req) const; + // Socket endpoint information const std::string host_; const int port_; @@ -1641,7 +1673,9 @@ class ClientImpl { std::function server_certificate_verifier_; #endif + mutable std::mutex logger_mutex_; Logger logger_; + ErrorLogger error_logger_; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT int last_ssl_error_ = 0; @@ -1868,6 +1902,7 @@ class Client { #endif void set_logger(Logger logger); + void set_error_logger(ErrorLogger error_logger); // SSL #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -2199,6 +2234,7 @@ Server::set_idle_interval(const std::chrono::duration &duration) { inline std::string to_string(const Error error) { switch (error) { case Error::Success: return "Success (no error)"; + case Error::Unknown: return "Unknown"; case Error::Connection: return "Could not establish connection"; case Error::BindIPAddress: return "Failed to bind IP address"; case Error::Read: return "Failed to read connection"; @@ -2215,7 +2251,23 @@ inline std::string to_string(const Error error) { case Error::Compression: return "Compression failed"; case Error::ConnectionTimeout: return "Connection timed out"; case Error::ProxyConnection: return "Proxy connection failed"; - case Error::Unknown: return "Unknown"; + case Error::ResourceExhaustion: return "Resource exhaustion"; + case Error::TooManyFormDataFiles: return "Too many form data files"; + case Error::ExceedMaxPayloadSize: return "Exceeded maximum payload size"; + case Error::ExceedUriMaxLength: return "Exceeded maximum URI length"; + case Error::ExceedMaxSocketDescriptorCount: + return "Exceeded maximum socket descriptor count"; + case Error::InvalidRequestLine: return "Invalid request line"; + case Error::InvalidHTTPMethod: return "Invalid HTTP method"; + case Error::InvalidHTTPVersion: return "Invalid HTTP version"; + case Error::InvalidHeaders: return "Invalid headers"; + case Error::MultipartParsing: return "Multipart parsing failed"; + case Error::OpenFile: return "Failed to open file"; + case Error::Listen: return "Failed to listen on socket"; + case Error::GetSockName: return "Failed to get socket name"; + case Error::UnsupportedAddressFamily: return "Unsupported address family"; + case Error::HTTPParsing: return "HTTP parsing failed"; + case Error::InvalidRangeHeader: return "Invalid Range header"; default: break; } @@ -7359,6 +7411,11 @@ inline Server &Server::set_logger(Logger logger) { return *this; } +inline Server &Server::set_error_logger(ErrorLogger error_logger) { + error_logger_ = std::move(error_logger); + return *this; +} + inline Server &Server::set_pre_compression_logger(Logger logger) { pre_compression_logger_ = std::move(logger); return *this; @@ -7498,9 +7555,15 @@ inline bool Server::parse_request_line(const char *s, Request &req) const { "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; - if (methods.find(req.method) == methods.end()) { return false; } + if (methods.find(req.method) == methods.end()) { + output_error_log(Error::InvalidHTTPMethod, &req); + return false; + } - if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } + if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { + output_error_log(Error::InvalidHTTPVersion, &req); + return false; + } { // Skip URL fragment @@ -7607,7 +7670,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, } // Log - if (logger_) { logger_(req, res); } + output_log(req, res); return ret; } @@ -7683,6 +7746,7 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { // Multipart FormData [&](const FormData &file) { if (count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { + output_error_log(Error::TooManyFormDataFiles, &req); return false; } @@ -7712,6 +7776,7 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { if (!content_type.find("application/x-www-form-urlencoded")) { if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { res.status = StatusCode::PayloadTooLarge_413; // NOTE: should be 414? + output_error_log(Error::ExceedMaxPayloadSize, &req); return false; } detail::parse_query_text(req.body, req.params); @@ -7740,6 +7805,7 @@ inline bool Server::read_content_core( std::string boundary; if (!detail::parse_multipart_boundary(content_type, boundary)) { res.status = StatusCode::BadRequest_400; + output_error_log(Error::MultipartParsing, &req); return false; } @@ -7765,6 +7831,7 @@ inline bool Server::read_content_core( if (req.is_multipart_form_data()) { if (!multipart_form_data_parser.is_valid()) { res.status = StatusCode::BadRequest_400; + output_error_log(Error::MultipartParsing, &req); return false; } } @@ -7794,7 +7861,10 @@ inline bool Server::handle_file_request(const Request &req, Response &res) { } auto mm = std::make_shared(path.c_str()); - if (!mm->is_open()) { return false; } + if (!mm->is_open()) { + output_error_log(Error::OpenFile, &req); + return false; + } res.set_content_provider( mm->size(), @@ -7810,6 +7880,8 @@ inline bool Server::handle_file_request(const Request &req, Response &res) { } return true; + } else { + output_error_log(Error::OpenFile, &req); } } } @@ -7824,11 +7896,15 @@ Server::create_server_socket(const std::string &host, int port, return detail::create_socket( host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, ipv6_v6only_, std::move(socket_options), - [](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { + [&](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool { if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + output_error_log(Error::BindIPAddress, nullptr); + return false; + } + if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { + output_error_log(Error::Listen, nullptr); return false; } - if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } return true; }); } @@ -7847,6 +7923,7 @@ inline int Server::bind_internal(const std::string &host, int port, socklen_t addr_len = sizeof(addr); if (getsockname(svr_sock_, reinterpret_cast(&addr), &addr_len) == -1) { + output_error_log(Error::GetSockName, nullptr); return -1; } if (addr.ss_family == AF_INET) { @@ -7854,6 +7931,7 @@ inline int Server::bind_internal(const std::string &host, int port, } else if (addr.ss_family == AF_INET6) { return ntohs(reinterpret_cast(&addr)->sin6_port); } else { + output_error_log(Error::UnsupportedAddressFamily, nullptr); return -1; } } else { @@ -7907,6 +7985,7 @@ inline bool Server::listen_internal() { if (svr_sock_ != INVALID_SOCKET) { detail::close_socket(svr_sock_); ret = false; + output_error_log(Error::Connection, nullptr); } else { ; // The server socket was closed by user. } @@ -7920,6 +7999,7 @@ inline bool Server::listen_internal() { if (!task_queue->enqueue( [this, sock]() { process_and_close_socket(sock); })) { + output_error_log(Error::ResourceExhaustion, nullptr); detail::shutdown_socket(sock); detail::close_socket(sock); } @@ -7949,13 +8029,17 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { { ContentReader reader( [&](ContentReceiver receiver) { - return read_content_with_content_receiver( + auto result = read_content_with_content_receiver( strm, req, res, std::move(receiver), nullptr, nullptr); + if (!result) { output_error_log(Error::Read, &req); } + return result; }, [&](FormDataHeader header, ContentReceiver receiver) { - return read_content_with_content_receiver(strm, req, res, nullptr, - std::move(header), - std::move(receiver)); + auto result = read_content_with_content_receiver( + strm, req, res, nullptr, std::move(header), + std::move(receiver)); + if (!result) { output_error_log(Error::Read, &req); } + return result; }); if (req.method == "POST") { @@ -7986,7 +8070,10 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { } // Read content into `req.body` - if (!read_content(strm, req, res)) { return false; } + if (!read_content(strm, req, res)) { + output_error_log(Error::Read, &req); + return false; + } } // Regular handler @@ -8100,7 +8187,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, } if (type != detail::EncodingType::None) { - if (pre_compression_logger_) { pre_compression_logger_(req, res); } + output_pre_compression_log(req, res); std::unique_ptr compressor; std::string content_encoding; @@ -8184,14 +8271,22 @@ Server::process_request(Stream &strm, const std::string &remote_addr, Headers dummy; detail::read_headers(strm, dummy); res.status = StatusCode::InternalServerError_500; + output_error_log(Error::ExceedMaxSocketDescriptorCount, &req); return write_response(strm, close_connection, req, res); } #endif // Request line and headers - if (!parse_request_line(line_reader.ptr(), req) || - !detail::read_headers(strm, req.headers)) { + if (!parse_request_line(line_reader.ptr(), req)) { res.status = StatusCode::BadRequest_400; + output_error_log(Error::InvalidRequestLine, &req); + return write_response(strm, close_connection, req, res); + } + + // Request headers + if (!detail::read_headers(strm, req.headers)) { + res.status = StatusCode::BadRequest_400; + output_error_log(Error::InvalidHeaders, &req); return write_response(strm, close_connection, req, res); } @@ -8200,6 +8295,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr, Headers dummy; detail::read_headers(strm, dummy); res.status = StatusCode::UriTooLong_414; + output_error_log(Error::ExceedUriMaxLength, &req); return write_response(strm, close_connection, req, res); } @@ -8226,6 +8322,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr, const auto &accept_header = req.get_header_value("Accept"); if (!detail::parse_accept_header(accept_header, req.accept_content_types)) { res.status = StatusCode::BadRequest_400; + output_error_log(Error::HTTPParsing, &req); return write_response(strm, close_connection, req, res); } } @@ -8234,6 +8331,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr, const auto &range_header_value = req.get_header_value("Range"); if (!detail::parse_range_header(range_header_value, req.ranges)) { res.status = StatusCode::RangeNotSatisfiable_416; + output_error_log(Error::InvalidRangeHeader, &req); return write_response(strm, close_connection, req, res); } } @@ -8314,6 +8412,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr, res.content_length_ = 0; res.content_provider_ = nullptr; res.status = StatusCode::NotFound_404; + output_error_log(Error::OpenFile, &req); return write_response(strm, close_connection, req, res); } @@ -8373,6 +8472,29 @@ inline bool Server::process_and_close_socket(socket_t sock) { return ret; } +inline void Server::output_log(const Request &req, const Response &res) const { + if (logger_) { + std::lock_guard guard(logger_mutex_); + logger_(req, res); + } +} + +inline void Server::output_pre_compression_log(const Request &req, + const Response &res) const { + if (pre_compression_logger_) { + std::lock_guard guard(logger_mutex_); + pre_compression_logger_(req, res); + } +} + +inline void Server::output_error_log(const Error &err, + const Request *req) const { + if (error_logger_) { + std::lock_guard guard(logger_mutex_); + error_logger_(err, req); + } +} + // HTTP client implementation inline ClientImpl::ClientImpl(const std::string &host) : ClientImpl(host, 80, std::string(), std::string()) {} @@ -8451,6 +8573,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { server_certificate_verifier_ = rhs.server_certificate_verifier_; #endif logger_ = rhs.logger_; + error_logger_ = rhs.error_logger_; } inline socket_t ClientImpl::create_client_socket(Error &error) const { @@ -8593,7 +8716,10 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { } if (!is_alive) { - if (!create_and_connect_socket(socket_, error)) { return false; } + if (!create_and_connect_socket(socket_, error)) { + output_error_log(error, &req); + return false; + } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT // TODO: refactoring @@ -8603,11 +8729,15 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { auto success = false; if (!scli.connect_with_proxy(socket_, req.start_time_, res, success, error)) { + if (!success) { output_error_log(error, &req); } return success; } } - if (!scli.initialize_ssl(socket_, error)) { return false; } + if (!scli.initialize_ssl(socket_, error)) { + output_error_log(error, &req); + return false; + } } #endif } @@ -8653,7 +8783,10 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { }); if (!ret) { - if (error == Error::Success) { error = Error::Unknown; } + if (error == Error::Success) { + error = Error::Unknown; + output_error_log(error, &req); + } } return ret; @@ -8681,6 +8814,7 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, Error &error) { if (req.path.empty()) { error = Error::Connection; + output_error_log(error, &req); return false; } @@ -8756,6 +8890,7 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { if (req.redirect_count_ == 0) { error = Error::ExceedRedirectCount; + output_error_log(error, &req); return false; } @@ -8859,6 +8994,7 @@ inline bool ClientImpl::create_redirect_client( #else // SSL not supported - set appropriate error error = Error::SSLConnection; + output_error_log(error, &req); return false; #endif } else { @@ -8931,6 +9067,7 @@ inline void ClientImpl::setup_redirect_client(ClientType &client) { // Copy logging and headers if (logger_) { client.set_logger(logger_); } + if (error_logger_) { client.set_error_logger(error_logger_); } // NOTE: DO NOT copy default_headers_ as they may contain stale Host headers // Each new client should generate its own headers based on its target host @@ -9104,6 +9241,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, auto &data = bstrm.get_buffer(); if (!detail::write_data(strm, data.data(), data.size())) { error = Error::Write; + output_error_log(error, &req); return false; } } @@ -9122,18 +9260,21 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, size_t to_write = (std::min)(CPPHTTPLIB_SEND_BUFSIZ, body_size - written); if (!detail::write_data(strm, data + written, to_write)) { error = Error::Write; + output_error_log(error, &req); return false; } written += to_write; if (!req.upload_progress(written, body_size)) { error = Error::Canceled; + output_error_log(error, &req); return false; } } } else { if (!detail::write_data(strm, req.body.data(), req.body.size())) { error = Error::Write; + output_error_log(error, &req); return false; } } @@ -9185,6 +9326,7 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( while (ok && offset < content_length) { if (!content_provider(offset, content_length - offset, data_sink)) { error = Error::Canceled; + output_error_log(error, &req); return nullptr; } } @@ -9195,6 +9337,7 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( return true; })) { error = Error::Compression; + output_error_log(error, &req); return nullptr; } } @@ -9254,6 +9397,22 @@ ClientImpl::adjust_host_string(const std::string &host) const { return host; } +inline void ClientImpl::output_log(const Request &req, + const Response &res) const { + if (logger_) { + std::lock_guard guard(logger_mutex_); + logger_(req, res); + } +} + +inline void ClientImpl::output_error_log(const Error &err, + const Request *req) const { + if (error_logger_) { + std::lock_guard guard(logger_mutex_); + error_logger_(err, req); + } +} + inline bool ClientImpl::process_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error) { @@ -9266,6 +9425,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (!is_proxy_enabled) { if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { error = Error::SSLPeerCouldBeClosed_; + output_error_log(error, &req); return false; } } @@ -9276,6 +9436,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (!read_response_line(strm, req, res) || !detail::read_headers(strm, res.headers)) { error = Error::Read; + output_error_log(error, &req); return false; } @@ -9289,6 +9450,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (req.response_handler && !redirect) { if (!req.response_handler(res)) { error = Error::Canceled; + output_error_log(error, &req); return false; } } @@ -9299,7 +9461,10 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, [&](const char *buf, size_t n, size_t off, size_t len) { if (redirect) { return true; } auto ret = req.content_receiver(buf, n, off, len); - if (!ret) { error = Error::Canceled; } + if (!ret) { + error = Error::Canceled; + output_error_log(error, &req); + } return ret; }) : static_cast( @@ -9313,7 +9478,10 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, auto progress = [&](size_t current, size_t total) { if (!req.download_progress || redirect) { return true; } auto ret = req.download_progress(current, total); - if (!ret) { error = Error::Canceled; } + if (!ret) { + error = Error::Canceled; + output_error_log(error, &req); + } return ret; }; @@ -9322,6 +9490,7 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, auto len = res.get_header_value_u64("Content-Length"); if (len > res.body.max_size()) { error = Error::Read; + output_error_log(error, &req); return false; } res.body.reserve(static_cast(len)); @@ -9334,13 +9503,14 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, dummy_status, std::move(progress), std::move(out), decompress_)) { if (error != Error::Canceled) { error = Error::Read; } + output_error_log(error, &req); return false; } } } // Log - if (logger_) { logger_(req, res); } + output_log(req, res); return true; } @@ -10244,6 +10414,10 @@ inline void ClientImpl::set_logger(Logger logger) { logger_ = std::move(logger); } +inline void ClientImpl::set_error_logger(ErrorLogger error_logger) { + error_logger_ = std::move(error_logger); +} + /* * SSL Implementation */ @@ -10756,6 +10930,7 @@ inline bool SSLClient::connect_with_proxy( // Create a new socket for the authenticated CONNECT request if (!create_and_connect_socket(socket, error)) { success = false; + output_error_log(error, nullptr); return false; } @@ -10793,6 +10968,7 @@ inline bool SSLClient::connect_with_proxy( // as the response of the request if (proxy_res.status != StatusCode::OK_200) { error = Error::ProxyConnection; + output_error_log(error, nullptr); res = std::move(proxy_res); // Thread-safe to close everything because we are assuming there are // no requests in flight @@ -10845,6 +11021,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { if (server_certificate_verification_) { if (!load_certs()) { error = Error::SSLLoadingCerts; + output_error_log(error, nullptr); return false; } SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); @@ -10854,6 +11031,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { socket.sock, ssl2, SSL_connect, connection_timeout_sec_, connection_timeout_usec_, &last_ssl_error_)) { error = Error::SSLConnection; + output_error_log(error, nullptr); return false; } @@ -10867,6 +11045,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { if (verification_status == SSLVerifierResponse::CertificateRejected) { last_openssl_error_ = ERR_get_error(); error = Error::SSLServerVerification; + output_error_log(error, nullptr); return false; } @@ -10876,6 +11055,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { if (verify_result_ != X509_V_OK) { last_openssl_error_ = static_cast(verify_result_); error = Error::SSLServerVerification; + output_error_log(error, nullptr); return false; } @@ -10885,6 +11065,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { if (server_cert == nullptr) { last_openssl_error_ = ERR_get_error(); error = Error::SSLServerVerification; + output_error_log(error, nullptr); return false; } @@ -10892,6 +11073,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { if (!verify_host(server_cert)) { last_openssl_error_ = X509_V_ERR_HOSTNAME_MISMATCH; error = Error::SSLServerHostnameVerification; + output_error_log(error, nullptr); return false; } } @@ -11658,6 +11840,10 @@ inline void Client::set_logger(Logger logger) { cli_->set_logger(std::move(logger)); } +inline void Client::set_error_logger(ErrorLogger error_logger) { + cli_->set_error_logger(std::move(error_logger)); +} + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, const std::string &ca_cert_dir_path) { From cdaed14925a4eb843cd30aa7e13655633f51ef44 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 6 Aug 2025 17:41:03 -0400 Subject: [PATCH 0493/1222] Update README --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8553114ff3..ff07694c22 100644 --- a/README.md +++ b/README.md @@ -269,23 +269,47 @@ svr.set_file_request_handler([](const Request &req, Response &res) { ### Logging +cpp-httplib provides separate logging capabilities for access logs and error logs, similar to web servers like Nginx and Apache. + +#### Access Logging + +Access loggers capture successful HTTP requests and responses: + ```cpp -svr.set_logger([](const auto& req, const auto& res) { - your_logger(req, res); +svr.set_logger([](const httplib::Request& req, const httplib::Response& res) { + std::cout << req.method << " " << req.path << " -> " << res.status << std::endl; }); ``` -You can also set a pre-compression logger to capture request/response data before compression is applied. This is useful for debugging and monitoring purposes when you need to see the original, uncompressed response content: +#### Pre-compression Logging + +You can also set a pre-compression logger to capture request/response data before compression is applied: ```cpp -svr.set_pre_compression_logger([](const auto& req, const auto& res) { +svr.set_pre_compression_logger([](const httplib::Request& req, const httplib::Response& res) { // Log before compression - res.body contains uncompressed content // Content-Encoding header is not yet set your_pre_compression_logger(req, res); }); ``` -The pre-compression logger is only called when compression would be applied. For responses without compression, only the regular logger is called. +The pre-compression logger is only called when compression would be applied. For responses without compression, only the access logger is called. + +#### Error Logging + +Error loggers capture failed requests and connection issues. Unlike access loggers, error loggers only receive the Error and Request information, as errors typically occur before a meaningful Response can be generated. + +```cpp +svr.set_error_logger([](const httplib::Error& err, const httplib::Request* req) { + std::cerr << httplib::to_string(err) << " while processing request"; + if (req) { + std::cerr << ", client: " << req->get_header_value("X-Forwarded-For") + << ", request: '" << req->method << " " << req->path << " " << req->version << "'" + << ", host: " << req->get_header_value("Host"); + } + std::cerr << std::endl; +}); +``` ### Error handler @@ -718,6 +742,51 @@ enum Error { }; ``` +### Client Logging + +#### Access Logging + +```cpp +cli.set_logger([](const httplib::Request& req, const httplib::Response& res) { + auto duration = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time).count(); + std::cout << "✓ " << req.method << " " << req.path + << " -> " << res.status << " (" << res.body.size() << " bytes, " + << duration << "ms)" << std::endl; +}); +``` + +#### Error Logging + +```cpp +cli.set_error_logger([](const httplib::Error& err, const httplib::Request* req) { + std::cerr << "✗ "; + if (req) { + std::cerr << req->method << " " << req->path << " "; + } + std::cerr << "failed: " << httplib::to_string(err); + + // Add specific guidance based on error type + switch (err) { + case httplib::Error::Connection: + std::cerr << " (verify server is running and reachable)"; + break; + case httplib::Error::SSLConnection: + std::cerr << " (check SSL certificate and TLS configuration)"; + break; + case httplib::Error::ConnectionTimeout: + std::cerr << " (increase timeout or check network latency)"; + break; + case httplib::Error::Read: + std::cerr << " (server may have closed connection prematurely)"; + break; + default: + break; + } + std::cerr << std::endl; +}); +``` + ### GET with HTTP headers ```c++ From 70cca55cafb4edb8f57dc0c9ee3e5ab227055a40 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 6 Aug 2025 17:54:08 -0400 Subject: [PATCH 0494/1222] Workaround for chocolatey issue with the latest OpenSSL --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 135bf7b500..45dc91c48e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -148,7 +148,7 @@ jobs: run: vcpkg install gtest curl zlib brotli zstd - name: Install OpenSSL if: ${{ matrix.config.with_ssl }} - run: choco install openssl + run: choco install openssl --version 3.5.2 # workaround for chocolatey issue with the latest OpenSSL - name: Configure CMake ${{ matrix.config.name }} run: > cmake -B build -S . From fbee136dca542e1824cffcdf50ddc04738113e51 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 6 Aug 2025 23:05:42 -0400 Subject: [PATCH 0495/1222] Fix #2193. Allow _WIN32 --- httplib.h | 88 +++++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/httplib.h b/httplib.h index c6976917e0..98854aff85 100644 --- a/httplib.h +++ b/httplib.h @@ -16,7 +16,7 @@ */ #if defined(_WIN32) && !defined(_WIN64) -#error \ +#warning \ "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler." #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8 #warning \ @@ -98,7 +98,7 @@ #endif #ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND -#ifdef _WIN64 +#ifdef _WIN32 #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 1000 #else #define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 @@ -184,7 +184,7 @@ * Headers */ -#ifdef _WIN64 +#ifdef _WIN32 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif //_CRT_SECURE_NO_WARNINGS @@ -235,7 +235,7 @@ using nfds_t = unsigned long; using socket_t = SOCKET; using socklen_t = int; -#else // not _WIN64 +#else // not _WIN32 #include #if !defined(_AIX) && !defined(__MVS__) @@ -266,7 +266,7 @@ using socket_t = int; #ifndef INVALID_SOCKET #define INVALID_SOCKET (-1) #endif -#endif //_WIN64 +#endif //_WIN32 #if defined(__APPLE__) #include @@ -311,7 +311,7 @@ using socket_t = int; // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN64 +#ifdef _WIN32 #include // these are defined in wincrypt.h and it breaks compilation if BoringSSL is @@ -324,7 +324,7 @@ using socket_t = int; #ifdef _MSC_VER #pragma comment(lib, "crypt32.lib") #endif -#endif // _WIN64 +#endif // _WIN32 #if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) #if TARGET_OS_OSX @@ -337,7 +337,7 @@ using socket_t = int; #include #include -#if defined(_WIN64) && defined(OPENSSL_USE_APPLINK) +#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) #include #endif @@ -2086,7 +2086,7 @@ namespace detail { inline bool set_socket_opt_impl(socket_t sock, int level, int optname, const void *optval, socklen_t optlen) { return setsockopt(sock, level, optname, -#ifdef _WIN64 +#ifdef _WIN32 reinterpret_cast(optval), #else optval, @@ -2100,7 +2100,7 @@ inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) { inline bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, time_t usec) { -#ifdef _WIN64 +#ifdef _WIN32 auto timeout = static_cast(sec * 1000 + usec / 1000); #else timeval timeout; @@ -2378,7 +2378,7 @@ make_basic_authentication_header(const std::string &username, namespace detail { -#if defined(_WIN64) +#if defined(_WIN32) inline std::wstring u8string_to_wstring(const char *s) { std::wstring ws; auto len = static_cast(strlen(s)); @@ -2400,7 +2400,7 @@ struct FileStat { bool is_dir() const; private: -#if defined(_WIN64) +#if defined(_WIN32) struct _stat st_; #else struct stat st_; @@ -2641,7 +2641,7 @@ class mmap { const char *data() const; private: -#if defined(_WIN64) +#if defined(_WIN32) HANDLE hFile_ = NULL; HANDLE hMapping_ = NULL; #else @@ -2862,7 +2862,7 @@ inline bool is_valid_path(const std::string &path) { } inline FileStat::FileStat(const std::string &path) { -#if defined(_WIN64) +#if defined(_WIN32) auto wpath = u8string_to_wstring(path.c_str()); ret_ = _wstat(wpath.c_str(), &st_); #else @@ -3074,7 +3074,7 @@ inline mmap::~mmap() { close(); } inline bool mmap::open(const char *path) { close(); -#if defined(_WIN64) +#if defined(_WIN32) auto wpath = u8string_to_wstring(path); if (wpath.empty()) { return false; } @@ -3151,7 +3151,7 @@ inline const char *mmap::data() const { } inline void mmap::close() { -#if defined(_WIN64) +#if defined(_WIN32) if (addr_) { ::UnmapViewOfFile(addr_); addr_ = nullptr; @@ -3182,7 +3182,7 @@ inline void mmap::close() { size_ = 0; } inline int close_socket(socket_t sock) { -#ifdef _WIN64 +#ifdef _WIN32 return closesocket(sock); #else return close(sock); @@ -3205,7 +3205,7 @@ template inline ssize_t handle_EINTR(T fn) { inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { return handle_EINTR([&]() { return recv(sock, -#ifdef _WIN64 +#ifdef _WIN32 static_cast(ptr), static_cast(size), #else ptr, size, @@ -3218,7 +3218,7 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags) { return handle_EINTR([&]() { return send(sock, -#ifdef _WIN64 +#ifdef _WIN32 static_cast(ptr), static_cast(size), #else ptr, size, @@ -3228,7 +3228,7 @@ inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, } inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { -#ifdef _WIN64 +#ifdef _WIN32 return ::WSAPoll(fds, nfds, timeout); #else return ::poll(fds, nfds, timeout); @@ -3487,7 +3487,7 @@ inline bool process_client_socket( } inline int shutdown_socket(socket_t sock) { -#ifdef _WIN64 +#ifdef _WIN32 return shutdown(sock, SD_BOTH); #else return shutdown(sock, SHUT_RDWR); @@ -3522,7 +3522,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, return getaddrinfo(node, service, hints, res); } -#ifdef _WIN64 +#ifdef _WIN32 // Windows-specific implementation using GetAddrInfoEx with overlapped I/O OVERLAPPED overlapped = {0}; HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); @@ -3855,7 +3855,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_flags = socket_flags; } -#if !defined(_WIN64) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) +#if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) if (hints.ai_family == AF_UNIX) { const auto addrlen = host.length(); if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } @@ -3879,14 +3879,14 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, sizeof(addr) - sizeof(addr.sun_path) + addrlen); #ifndef SOCK_CLOEXEC -#ifndef _WIN64 +#ifndef _WIN32 fcntl(sock, F_SETFD, FD_CLOEXEC); #endif #endif if (socket_options) { socket_options(sock); } -#ifdef _WIN64 +#ifdef _WIN32 // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so // remove the option. detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); @@ -3915,7 +3915,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, for (auto rp = result; rp; rp = rp->ai_next) { // Create a socket -#ifdef _WIN64 +#ifdef _WIN32 auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); @@ -3948,7 +3948,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #endif if (sock == INVALID_SOCKET) { continue; } -#if !defined _WIN64 && !defined SOCK_CLOEXEC +#if !defined _WIN32 && !defined SOCK_CLOEXEC if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { close_socket(sock); continue; @@ -3976,7 +3976,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, } inline void set_nonblocking(socket_t sock, bool nonblocking) { -#ifdef _WIN64 +#ifdef _WIN32 auto flags = nonblocking ? 1UL : 0UL; ioctlsocket(sock, FIONBIO, &flags); #else @@ -3987,7 +3987,7 @@ inline void set_nonblocking(socket_t sock, bool nonblocking) { } inline bool is_connection_error() { -#ifdef _WIN64 +#ifdef _WIN32 return WSAGetLastError() != WSAEWOULDBLOCK; #else return errno != EINPROGRESS; @@ -4021,7 +4021,7 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { return ret; } -#if !defined _WIN64 && !defined ANDROID && !defined _AIX && !defined __MVS__ +#if !defined _WIN32 && !defined ANDROID && !defined _AIX && !defined __MVS__ #define USE_IF2IP #endif @@ -4160,7 +4160,7 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { if (!getpeername(sock, reinterpret_cast(&addr), &addr_len)) { -#ifndef _WIN64 +#ifndef _WIN32 if (addr.ss_family == AF_UNIX) { #if defined(__linux__) struct ucred ucred; @@ -6226,7 +6226,7 @@ inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; } -#ifdef _WIN64 +#ifdef _WIN32 // NOTE: This code came up with the following stackoverflow post: // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store inline bool load_system_certs_on_windows(X509_STORE *store) { @@ -6342,10 +6342,10 @@ inline bool load_system_certs_on_macos(X509_STORE *store) { return result; } -#endif // _WIN64 +#endif // _WIN32 #endif // CPPHTTPLIB_OPENSSL_SUPPORT -#ifdef _WIN64 +#ifdef _WIN32 class WSInit { public: WSInit() { @@ -7041,7 +7041,7 @@ inline bool SocketStream::wait_writable() const { } inline ssize_t SocketStream::read(char *ptr, size_t size) { -#ifdef _WIN64 +#ifdef _WIN32 size = (std::min)(size, static_cast((std::numeric_limits::max)())); #else @@ -7089,7 +7089,7 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { inline ssize_t SocketStream::write(const char *ptr, size_t size) { if (!wait_writable()) { return -1; } -#if defined(_WIN64) && !defined(_WIN64) +#if defined(_WIN32) && !defined(_WIN64) size = (std::min)(size, static_cast((std::numeric_limits::max)())); #endif @@ -7252,7 +7252,7 @@ inline bool RegexMatcher::match(Request &request) const { inline Server::Server() : new_task_queue( [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { -#ifndef _WIN64 +#ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif } @@ -7950,7 +7950,7 @@ inline bool Server::listen_internal() { std::unique_ptr task_queue(new_task_queue()); while (svr_sock_ != INVALID_SOCKET) { -#ifndef _WIN64 +#ifndef _WIN32 if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { #endif auto val = detail::select_read(svr_sock_, idle_interval_sec_, @@ -7959,11 +7959,11 @@ inline bool Server::listen_internal() { task_queue->on_idle(); continue; } -#ifndef _WIN64 +#ifndef _WIN32 } #endif -#if defined _WIN64 +#if defined _WIN32 // sockets connected via WASAccept inherit flags NO_HANDLE_INHERIT, // OVERLAPPED socket_t sock = WSAAccept(svr_sock_, nullptr, nullptr, nullptr, 0); @@ -10571,7 +10571,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (ret < 0) { auto err = SSL_get_error(ssl_, ret); auto n = 1000; -#ifdef _WIN64 +#ifdef _WIN32 while (--n >= 0 && (err == SSL_ERROR_WANT_READ || (err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT))) { @@ -10606,7 +10606,7 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { if (ret < 0) { auto err = SSL_get_error(ssl_, ret); auto n = 1000; -#ifdef _WIN64 +#ifdef _WIN32 while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || (err == SSL_ERROR_SYSCALL && WSAGetLastError() == WSAETIMEDOUT))) { @@ -11000,13 +11000,13 @@ inline bool SSLClient::load_certs() { } } else { auto loaded = false; -#ifdef _WIN64 +#ifdef _WIN32 loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); #elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ defined(TARGET_OS_OSX) loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); -#endif // _WIN64 +#endif // _WIN32 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } } }); From b1c1fa2dc6355b3d8f5a459a01681d620cc866bc Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Aug 2025 00:09:09 -0400 Subject: [PATCH 0496/1222] Code cleanup --- docker/main.cc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docker/main.cc b/docker/main.cc index 784c803b02..ae59b4e498 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -18,8 +18,7 @@ using namespace httplib; -auto SERVER_NAME = - std::format("cpp-httplib-nginxish-server/{}", CPPHTTPLIB_VERSION); +auto SERVER_NAME = std::format("cpp-httplib-server/{}", CPPHTTPLIB_VERSION); Server svr; @@ -31,7 +30,7 @@ void signal_handler(int signal) { } } -std::string get_nginx_time_format() { +std::string get_time_format() { auto now = std::chrono::system_clock::now(); auto time_t = std::chrono::system_clock::to_time_t(now); @@ -40,7 +39,7 @@ std::string get_nginx_time_format() { return ss.str(); } -std::string get_nginx_error_time_format() { +std::string get_error_time_format() { auto now = std::chrono::system_clock::now(); auto time_t = std::chrono::system_clock::to_time_t(now); @@ -77,7 +76,7 @@ void nginx_access_logger(const Request &req, const Response &res) { std::string remote_addr = get_client_ip(req); std::string remote_user = "-"; // cpp-httplib doesn't have built-in auth user tracking - std::string time_local = get_nginx_time_format(); + std::string time_local = get_time_format(); std::string request = std::format("{} {} {}", req.method, req.path, req.version); int status = res.status; @@ -98,7 +97,7 @@ void nginx_access_logger(const Request &req, const Response &res) { // YYYY/MM/DD HH:MM:SS [level] message, client: client_ip, request: "request", // host: "host" void nginx_error_logger(const Error &err, const Request *req) { - std::string time_local = get_nginx_error_time_format(); + std::string time_local = get_error_time_format(); std::string level = "error"; if (req) { From 7012e765e1315afa43b37d01fd43c7382a1bd1f8 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich <115483027+thbeu@users.noreply.github.com> Date: Thu, 7 Aug 2025 23:14:19 +0200 Subject: [PATCH 0497/1222] CMake: Check pointer size at configure time (#2197) * Check pointer size at configure time * Relax check to warning --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf036b4066..d39958a05c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,9 +117,12 @@ if(BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() -if( CMAKE_SYSTEM_NAME MATCHES "Windows" AND ${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0") +if(CMAKE_SYSTEM_NAME MATCHES "Windows" AND ${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0") message(SEND_ERROR "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.") endif() +if(CMAKE_SIZEOF_VOID_P LESS 8) + message(WARNING "Pointer size ${CMAKE_SIZEOF_VOID_P} is not supported. Please use a 64-bit compiler.") +endif() # Set some variables that are used in-tree and while building based on our options set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) From a2bb6f6c1ea56f351e43bbf740e30503453679cd Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Aug 2025 20:57:37 -0400 Subject: [PATCH 0498/1222] Update docker/main.cc --- docker/main.cc | 169 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 129 insertions(+), 40 deletions(-) diff --git a/docker/main.cc b/docker/main.cc index ae59b4e498..8ffbf2ca15 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -18,14 +18,14 @@ using namespace httplib; -auto SERVER_NAME = std::format("cpp-httplib-server/{}", CPPHTTPLIB_VERSION); +const auto SERVER_NAME = + std::format("cpp-httplib-server/{}", CPPHTTPLIB_VERSION); Server svr; void signal_handler(int signal) { if (signal == SIGINT || signal == SIGTERM) { - std::cout << std::format("\nReceived signal, shutting down gracefully...") - << std::endl; + std::cout << "\nReceived signal, shutting down gracefully...\n"; svr.stop(); } } @@ -73,17 +73,16 @@ std::string get_client_ip(const Request &req) { // $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent // "$http_referer" "$http_user_agent" void nginx_access_logger(const Request &req, const Response &res) { - std::string remote_addr = get_client_ip(req); + auto remote_addr = get_client_ip(req); std::string remote_user = "-"; // cpp-httplib doesn't have built-in auth user tracking - std::string time_local = get_time_format(); - std::string request = - std::format("{} {} {}", req.method, req.path, req.version); - int status = res.status; - size_t body_bytes_sent = res.body.size(); - std::string http_referer = req.get_header_value("Referer"); + auto time_local = get_time_format(); + auto request = std::format("{} {} {}", req.method, req.path, req.version); + auto status = res.status; + auto body_bytes_sent = res.body.size(); + auto http_referer = req.get_header_value("Referer"); if (http_referer.empty()) http_referer = "-"; - std::string http_user_agent = req.get_header_value("User-Agent"); + auto http_user_agent = req.get_header_value("User-Agent"); if (http_user_agent.empty()) http_user_agent = "-"; std::cout << std::format("{} - {} [{}] \"{}\" {} {} \"{}\" \"{}\"", @@ -97,14 +96,14 @@ void nginx_access_logger(const Request &req, const Response &res) { // YYYY/MM/DD HH:MM:SS [level] message, client: client_ip, request: "request", // host: "host" void nginx_error_logger(const Error &err, const Request *req) { - std::string time_local = get_error_time_format(); + auto time_local = get_error_time_format(); std::string level = "error"; if (req) { - std::string client_ip = get_client_ip(*req); - std::string request = + auto client_ip = get_client_ip(*req); + auto request = std::format("{} {} {}", req->method, req->path, req->version); - std::string host = req->get_header_value("Host"); + auto host = req->get_header_value("Host"); if (host.empty()) host = "-"; std::cerr << std::format("{} [{}] {}, client: {}, request: " @@ -120,38 +119,113 @@ void nginx_error_logger(const Error &err, const Request *req) { } void print_usage(const char *program_name) { - std::cout << std::format("Usage: {} " - "", - program_name) + std::cout << "Usage: " << program_name << " [OPTIONS]" << std::endl; + std::cout << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " --host Server hostname (default: localhost)" << std::endl; - - std::cout << std::format("Example: {} localhost 8080 /var/www/html .", - program_name) + std::cout << " --port Server port (default: 8080)" + << std::endl; + std::cout << " --mount Mount point and document root" + << std::endl; + std::cout << " Format: mount_point:document_root" + << std::endl; + std::cout << " (default: /:./html)" << std::endl; + std::cout << " --version Show version information" << std::endl; + std::cout << " --help Show this help message" << std::endl; + std::cout << std::endl; + std::cout << "Examples:" << std::endl; + std::cout << " " << program_name + << " --host localhost --port 8080 --mount /:./html" << std::endl; + std::cout << " " << program_name + << " --host 0.0.0.0 --port 3000 --mount /api:./api" << std::endl; } -int main(int argc, char *argv[]) { - if (argc != 5) { - print_usage(argv[0]); - return 1; +struct ServerConfig { + std::string hostname = "localhost"; + int port = 8080; + std::string mount_point = "/"; + std::string document_root = "./html"; +}; + +enum class ParseResult { SUCCESS, HELP_REQUESTED, VERSION_REQUESTED, ERROR }; + +ParseResult parse_command_line(int argc, char *argv[], ServerConfig &config) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { + print_usage(argv[0]); + return ParseResult::HELP_REQUESTED; + } else if (strcmp(argv[i], "--host") == 0) { + if (i + 1 >= argc) { + std::cerr << "Error: --host requires a hostname argument" << std::endl; + print_usage(argv[0]); + return ParseResult::ERROR; + } + config.hostname = argv[++i]; + } else if (strcmp(argv[i], "--port") == 0) { + if (i + 1 >= argc) { + std::cerr << "Error: --port requires a port number argument" + << std::endl; + print_usage(argv[0]); + return ParseResult::ERROR; + } + config.port = std::atoi(argv[++i]); + if (config.port <= 0 || config.port > 65535) { + std::cerr << "Error: Invalid port number. Must be between 1 and 65535" + << std::endl; + return ParseResult::ERROR; + } + } else if (strcmp(argv[i], "--mount") == 0) { + if (i + 1 >= argc) { + std::cerr + << "Error: --mount requires mount_point:document_root argument" + << std::endl; + print_usage(argv[0]); + return ParseResult::ERROR; + } + std::string mount_arg = argv[++i]; + auto colon_pos = mount_arg.find(':'); + if (colon_pos == std::string::npos) { + std::cerr << "Error: --mount argument must be in format " + "mount_point:document_root" + << std::endl; + print_usage(argv[0]); + return ParseResult::ERROR; + } + config.mount_point = mount_arg.substr(0, colon_pos); + config.document_root = mount_arg.substr(colon_pos + 1); + + if (config.mount_point.empty() || config.document_root.empty()) { + std::cerr + << "Error: Both mount_point and document_root must be non-empty" + << std::endl; + return ParseResult::ERROR; + } + } else if (strcmp(argv[i], "--version") == 0) { + std::cout << CPPHTTPLIB_VERSION << std::endl; + return ParseResult::VERSION_REQUESTED; + } else { + std::cerr << "Error: Unknown option '" << argv[i] << "'" << std::endl; + print_usage(argv[0]); + return ParseResult::ERROR; + } } + return ParseResult::SUCCESS; +} - std::string hostname = argv[1]; - auto port = std::atoi(argv[2]); - std::string mount_point = argv[3]; - std::string document_root = argv[4]; - +bool setup_server(Server &svr, const ServerConfig &config) { svr.set_logger(nginx_access_logger); svr.set_error_logger(nginx_error_logger); - auto ret = svr.set_mount_point(mount_point, document_root); + auto ret = svr.set_mount_point(config.mount_point, config.document_root); if (!ret) { std::cerr << std::format( "Error: Cannot mount '{}' to '{}'. Directory may not exist.", - mount_point, document_root) + config.mount_point, config.document_root) << std::endl; - return 1; + return false; } svr.set_file_extension_and_mimetype_mapping("html", "text/html"); @@ -191,16 +265,31 @@ int main(int argc, char *argv[]) { signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); - std::cout << std::format("Serving HTTP on {}:{}", hostname, port) - << std::endl; - std::cout << std::format("Mount point: {} -> {}", mount_point, document_root) - << std::endl; - std::cout << std::format("Press Ctrl+C to shutdown gracefully...") + return true; +} + +int main(int argc, char *argv[]) { + ServerConfig config; + + auto result = parse_command_line(argc, argv, config); + switch (result) { + case ParseResult::HELP_REQUESTED: + case ParseResult::VERSION_REQUESTED: return 0; + case ParseResult::ERROR: return 1; + case ParseResult::SUCCESS: break; + } + + if (!setup_server(svr, config)) { return 1; } + + std::cout << "Serving HTTP on " << config.hostname << ":" << config.port << std::endl; + std::cout << "Mount point: " << config.mount_point << " -> " + << config.document_root << std::endl; + std::cout << "Press Ctrl+C to shutdown gracefully..." << std::endl; - ret = svr.listen(hostname, port); + auto ret = svr.listen(config.hostname, config.port); - std::cout << std::format("Server has been shut down.") << std::endl; + std::cout << "Server has been shut down." << std::endl; return ret ? 0 : 1; } From 3f44c80fd345e859d6f61b2dda566aa3329ce35e Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Aug 2025 20:58:39 -0400 Subject: [PATCH 0499/1222] Release v0.25.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 98854aff85..52b1b829f7 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.24.0" -#define CPPHTTPLIB_VERSION_NUM "0x001800" +#define CPPHTTPLIB_VERSION "0.25.0" +#define CPPHTTPLIB_VERSION_NUM "0x001900" /* * Platform compatibility check From dffce89514c248f9001c9f772fd15d64c8404a58 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich <115483027+thbeu@users.noreply.github.com> Date: Tue, 12 Aug 2025 23:06:09 +0200 Subject: [PATCH 0500/1222] #2201 Fix 32-bit MSVC compiler error due to unknown command #warning (#2202) --- httplib.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/httplib.h b/httplib.h index 52b1b829f7..25e1b2f1ab 100644 --- a/httplib.h +++ b/httplib.h @@ -16,8 +16,13 @@ */ #if defined(_WIN32) && !defined(_WIN64) +#if defined(_MSC_VER) +#pragma message( \ + "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler.") +#else #warning \ "cpp-httplib doesn't support 32-bit Windows. Please use a 64-bit compiler." +#endif #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ < 8 #warning \ "cpp-httplib doesn't support 32-bit platforms. Please use a 64-bit compiler." From fbd6ce7a3f9878a8a433e37d0c2ac9f7e2dcabd7 Mon Sep 17 00:00:00 2001 From: Thomas Beutlich <115483027+thbeu@users.noreply.github.com> Date: Thu, 14 Aug 2025 12:57:39 +0200 Subject: [PATCH 0501/1222] Make code sample compilable (#2207) --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ff07694c22..a69f7be791 100644 --- a/README.md +++ b/README.md @@ -98,32 +98,31 @@ httplib::Client cli("https://example.com"); auto res = cli.Get("/"); if (!res) { // Check the error type - auto err = res.error(); + const auto err = res.error(); switch (err) { case httplib::Error::SSLConnection: std::cout << "SSL connection failed, SSL error: " - << res->ssl_error() << std::endl; + << res.ssl_error() << std::endl; break; case httplib::Error::SSLLoadingCerts: std::cout << "SSL cert loading failed, OpenSSL error: " - << std::hex << res->ssl_openssl_error() << std::endl; + << std::hex << res.ssl_openssl_error() << std::endl; break; case httplib::Error::SSLServerVerification: std::cout << "SSL verification failed, X509 error: " - << res->ssl_openssl_error() << std::endl; + << res.ssl_openssl_error() << std::endl; break; case httplib::Error::SSLServerHostnameVerification: std::cout << "SSL hostname verification failed, X509 error: " - << res->ssl_openssl_error() << std::endl; + << res.ssl_openssl_error() << std::endl; break; default: std::cout << "HTTP error: " << httplib::to_string(err) << std::endl; - } } } ``` From fe7fe15d2ea9acf290dc27002f386649f750fa15 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Tue, 19 Aug 2025 21:22:08 +0200 Subject: [PATCH 0502/1222] build(meson): fix new build option names (#2208) This is a follow-up to commit 4ff7a1c858faeab14d0f9e8d533a9787cab1de08, which introduced new simplified build options and deprecated the old ones. I forgot to also change the various get_option() calls, effectively rendering the new option names useless, as they would not get honoured. --- meson.build | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/meson.build b/meson.build index 8f667f8060..7e95bdea96 100644 --- a/meson.build +++ b/meson.build @@ -39,12 +39,12 @@ endif deps = [dependency('threads')] args = [] -openssl_dep = dependency('openssl', version: '>=3.0.0', required: get_option('cpp-httplib_openssl')) +openssl_dep = dependency('openssl', version: '>=3.0.0', required: get_option('openssl')) if openssl_dep.found() deps += openssl_dep args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' if host_machine.system() == 'darwin' - macosx_keychain_dep = dependency('appleframeworks', modules: ['CoreFoundation', 'Security'], required: get_option('cpp-httplib_macosx_keychain')) + macosx_keychain_dep = dependency('appleframeworks', modules: ['CoreFoundation', 'Security'], required: get_option('macosx_keychain')) if macosx_keychain_dep.found() deps += macosx_keychain_dep args += '-DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN' @@ -52,15 +52,15 @@ if openssl_dep.found() endif endif -zlib_dep = dependency('zlib', required: get_option('cpp-httplib_zlib')) +zlib_dep = dependency('zlib', required: get_option('zlib')) if zlib_dep.found() deps += zlib_dep args += '-DCPPHTTPLIB_ZLIB_SUPPORT' endif -brotli_deps = [dependency('libbrotlicommon', required: get_option('cpp-httplib_brotli'))] -brotli_deps += dependency('libbrotlidec', required: get_option('cpp-httplib_brotli')) -brotli_deps += dependency('libbrotlienc', required: get_option('cpp-httplib_brotli')) +brotli_deps = [dependency('libbrotlicommon', required: get_option('brotli'))] +brotli_deps += dependency('libbrotlidec', required: get_option('brotli')) +brotli_deps += dependency('libbrotlienc', required: get_option('brotli')) brotli_found_all = true foreach brotli_dep : brotli_deps @@ -74,7 +74,7 @@ if brotli_found_all args += '-DCPPHTTPLIB_BROTLI_SUPPORT' endif -async_ns_opt = get_option('cpp-httplib_non_blocking_getaddrinfo') +async_ns_opt = get_option('non_blocking_getaddrinfo') if host_machine.system() == 'windows' async_ns_dep = cxx.find_library('ws2_32', required: async_ns_opt) @@ -91,7 +91,7 @@ endif cpp_httplib_dep = dependency('', required: false) -if get_option('cpp-httplib_compile') +if get_option('compile') python3 = find_program('python3') httplib_ch = custom_target( @@ -135,6 +135,6 @@ endif meson.override_dependency('cpp-httplib', cpp_httplib_dep) -if get_option('cpp-httplib_test') +if get_option('test') subdir('test') endif From 3fae5f1473fbcde414fba36250f90c44dc78fa42 Mon Sep 17 00:00:00 2001 From: Sergey Date: Tue, 26 Aug 2025 09:46:51 -0700 Subject: [PATCH 0503/1222] osx: fix inconsistent use of the macro TARGET_OS_OSX (#2222) * osx: fix inconsistent use of the macro `TARGET_OS_OSX` Fixed the build error on iOS: ``` httplib.h:3583:3: error: unknown type name 'CFStringRef' 870 | CFStringRef hostname_ref = CFStringCreateWithCString( ``` Note, `TARGET_OS_OSX` is defined but is 0 when `TARGET_OS_IOS` is 1, and vise versa. Hence, `TARGET_OS_MAC` should have been used, that is set to 1 for the both targets. * improve: non-blocking getaddrinfo() for all mac target variants `TARGET_OS_MAC` should have been used, that is set to 1 for all other targets: OSX, IPHONE (IOS, TV, WATCH, VISION, BRIDGE), SIMULATOR, DRIVERKIT. --- httplib.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/httplib.h b/httplib.h index 25e1b2f1ab..a8dd9052ef 100644 --- a/httplib.h +++ b/httplib.h @@ -308,7 +308,7 @@ using socket_t = int; #if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) || \ defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) -#if TARGET_OS_OSX +#if TARGET_OS_MAC #include #include #endif @@ -332,7 +332,7 @@ using socket_t = int; #endif // _WIN32 #if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) -#if TARGET_OS_OSX +#if TARGET_OS_MAC #include #endif #endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO @@ -3578,7 +3578,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, } return ret; -#elif defined(TARGET_OS_OSX) +#elif TARGET_OS_MAC // macOS implementation using CFHost API for asynchronous DNS resolution CFStringRef hostname_ref = CFStringCreateWithCString( kCFAllocatorDefault, node, kCFStringEncodingUTF8); @@ -6258,8 +6258,7 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { return result; } -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ - defined(TARGET_OS_OSX) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC template using CFObjectPtr = std::unique_ptr::type, void (*)(CFTypeRef)>; @@ -11008,8 +11007,7 @@ inline bool SSLClient::load_certs() { #ifdef _WIN32 loaded = detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && \ - defined(TARGET_OS_OSX) +#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); #endif // _WIN32 if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } From b8e21eac89a93234791b96e472de65354bf47cce Mon Sep 17 00:00:00 2001 From: tejas Date: Wed, 27 Aug 2025 01:04:13 +0530 Subject: [PATCH 0504/1222] Initialize start time for server (#2220) * Initialize start time for server By initializing start_time_ for server, I hope to measure the time taken to process a request at the end maybe in the set_logger callback and print it. I only see current usage in client with server retaining the inital min value * Add test to verify start time is initialized * Address review comments * run clang format --- httplib.h | 1 + test/test.cc | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/httplib.h b/httplib.h index a8dd9052ef..703c171a34 100644 --- a/httplib.h +++ b/httplib.h @@ -8264,6 +8264,7 @@ Server::process_request(Stream &strm, const std::string &remote_addr, if (!line_reader.getline()) { return false; } Request req; + req.start_time_ = std::chrono::steady_clock::now(); Response res; res.version = "HTTP/1.1"; diff --git a/test/test.cc b/test/test.cc index 0e4fd42d58..4788ada383 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3200,6 +3200,11 @@ class ServerTest : public ::testing::Test { [&](const Request & /*req*/, Response &res) { res.set_content("abcdefg", "text/plain"); }) + .Get("/test-start-time", + [&](const Request &req, Response &res) { + EXPECT_NE(req.start_time_, + std::chrono::steady_clock::time_point::min()); + }) .Get("/with-range-customized-response", [&](const Request & /*req*/, Response &res) { res.status = StatusCode::BadRequest_400; @@ -5800,6 +5805,8 @@ TEST_F(ServerTest, TooManyRedirect) { EXPECT_EQ(Error::ExceedRedirectCount, res.error()); } +TEST_F(ServerTest, StartTime) { auto res = cli_.Get("/test-start-time"); } + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) { Headers headers; From 92b4f5301239bad096968dcc6e3de64dc0b0a5c4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 26 Aug 2025 20:29:26 -0400 Subject: [PATCH 0505/1222] clang-format --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 4788ada383..867b093e9f 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3201,7 +3201,7 @@ class ServerTest : public ::testing::Test { res.set_content("abcdefg", "text/plain"); }) .Get("/test-start-time", - [&](const Request &req, Response &res) { + [&](const Request &req, Response & /*res*/) { EXPECT_NE(req.start_time_, std::chrono::steady_clock::time_point::min()); }) From 4285d33992bfa05d0313843973341caab552388c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 26 Aug 2025 21:42:13 -0400 Subject: [PATCH 0506/1222] Fix #2223 (#2224) * Fix #2223 * Fix build error --- httplib.h | 21 ++++++++++++++++----- test/test.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 703c171a34..371db9a618 100644 --- a/httplib.h +++ b/httplib.h @@ -10429,6 +10429,13 @@ inline void ClientImpl::set_error_logger(ErrorLogger error_logger) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { +inline bool is_ip_address(const std::string &host) { + struct in_addr addr4; + struct in6_addr addr6; + return inet_pton(AF_INET, host.c_str(), &addr4) == 1 || + inet_pton(AF_INET6, host.c_str(), &addr6) == 1; +} + template inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, U SSL_connect_or_accept, V setup) { @@ -11087,14 +11094,18 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return true; }, [&](SSL *ssl2) { + // Set SNI only if host is not IP address + if (!detail::is_ip_address(host_)) { #if defined(OPENSSL_IS_BORINGSSL) - SSL_set_tlsext_host_name(ssl2, host_.c_str()); + SSL_set_tlsext_host_name(ssl2, host_.c_str()); #else - // NOTE: Direct call instead of using the OpenSSL macro to suppress - // -Wold-style-cast warning - SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, - static_cast(const_cast(host_.c_str()))); + // NOTE: Direct call instead of using the OpenSSL macro to suppress + // -Wold-style-cast warning + SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, + TLSEXT_NAMETYPE_host_name, + static_cast(const_cast(host_.c_str()))); #endif + } return true; }); diff --git a/test/test.cc b/test/test.cc index 867b093e9f..23dd0fb418 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7366,6 +7366,48 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { ASSERT_TRUE(result); EXPECT_EQ(200, result->status); } + +TEST(SNI_AutoDetectionTest, SNI_Logic) { + { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + svr.Get("/sni", [&](const Request &req, Response &res) { + std::string expected; + if (req.ssl) { + if (const char *sni = + SSL_get_servername(req.ssl, TLSEXT_NAMETYPE_host_name)) { + expected = sni; + } + } + EXPECT_EQ(expected, req.get_param_value("expected")); + res.set_content("ok", "text/plain"); + }); + + auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + { + SSLClient cli("localhost", PORT); + cli.enable_server_certificate_verification(false); + auto res = cli.Get("/sni?expected=localhost"); + ASSERT_TRUE(res); + } + + { + SSLClient cli("::1", PORT); + cli.enable_server_certificate_verification(false); + auto res = cli.Get("/sni?expected="); + ASSERT_TRUE(res); + } + } +} #endif TEST(ClientProblemDetectionTest, ContentProvider) { From f4cc542d4b5149ab78bac2e889e564c7550f2244 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 26 Aug 2025 22:17:54 -0400 Subject: [PATCH 0507/1222] Fix Dockerfile problem with CMD --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 67aa028b7e..3495b42f24 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,4 +10,4 @@ COPY docker/html/index.html /html/index.html EXPOSE 80 ENTRYPOINT ["/server"] -CMD ["0.0.0.0", "80", "/", "/html"] +CMD ["--host", "0.0.0.0", "--port", "80", "--mount", "/:./html"] From b20b5fdd1f5355c27a3cac42e2c4ea0e4729616d Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 26 Aug 2025 23:18:59 -0400 Subject: [PATCH 0508/1222] Add 'release-docker' workflow --- .github/workflows/release-docker.yml | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/release-docker.yml diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml new file mode 100644 index 0000000000..1038bf847c --- /dev/null +++ b/.github/workflows/release-docker.yml @@ -0,0 +1,35 @@ +name: Release Docker Image + +on: + release: + types: [published] + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Extract version tag without 'v' + id: set_tag + run: echo "tag=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + push: true + tags: | + yhirose4dockerhub/cpp-httplib-server:latest + yhirose4dockerhub/cpp-httplib-server:${{ steps.set_tag.outputs.tag }} From 54e75dc8ef4dd92cda46c5d83222ea01bfabf8a2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 26 Aug 2025 23:34:18 -0400 Subject: [PATCH 0509/1222] Add manual run --- .github/workflows/release-docker.yml | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml index 1038bf847c..80a5c07cdc 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yml @@ -1,8 +1,12 @@ + + + name: Release Docker Image on: release: types: [published] + workflow_dispatch: jobs: build-and-push: @@ -10,6 +14,23 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history and tags + + - name: Extract tag (manual) + if: github.event_name == 'workflow_dispatch' + id: set_tag_manual + run: | + # Checkout the latest tag and set output + git fetch --tags + LATEST_TAG=$(git describe --tags --abbrev=0) + git checkout $LATEST_TAG + echo "tag=${LATEST_TAG#v}" >> $GITHUB_OUTPUT + + - name: Extract tag (release) + if: github.event_name == 'release' + id: set_tag_release + run: echo "tag=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -20,16 +41,13 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Extract version tag without 'v' - id: set_tag - run: echo "tag=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT - - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . file: ./Dockerfile push: true + # Use extracted tag without leading 'v' tags: | yhirose4dockerhub/cpp-httplib-server:latest - yhirose4dockerhub/cpp-httplib-server:${{ steps.set_tag.outputs.tag }} + yhirose4dockerhub/cpp-httplib-server:${{ steps.set_tag_manual.outputs.tag || steps.set_tag_release.outputs.tag }} From eb11032797f18b21d4a24abacd6c5df655e99dbb Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 27 Aug 2025 00:31:14 -0400 Subject: [PATCH 0510/1222] Fix platform problem --- .github/workflows/release-docker.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml index 80a5c07cdc..179ab82fae 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yml @@ -1,6 +1,3 @@ - - - name: Release Docker Image on: @@ -47,6 +44,7 @@ jobs: context: . file: ./Dockerfile push: true + platforms: linux/amd64,linux/arm64 # Build for both amd64 and arm64 # Use extracted tag without leading 'v' tags: | yhirose4dockerhub/cpp-httplib-server:latest From 7a3b92bbd9937de24793d758b108e59fe31a0d1c Mon Sep 17 00:00:00 2001 From: kgokalp Date: Thu, 28 Aug 2025 18:08:32 +0300 Subject: [PATCH 0511/1222] Fix: handle EAI_ALLDONE from gai_suspend in getaddrinfo_with_timeout (#2228) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 371db9a618..c035984394 100644 --- a/httplib.h +++ b/httplib.h @@ -3776,7 +3776,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, int wait_result = gai_suspend((const struct gaicb *const *)requests, 1, &timeout); - if (wait_result == 0) { + if (wait_result == 0 || wait_result == EAI_ALLDONE) { // Completed successfully, get the result int gai_result = gai_error(&request); if (gai_result == 0) { From eb5a65e0dfbaf52e9e43b253be5aae6fcbcfc20e Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Aug 2025 15:01:59 -0400 Subject: [PATCH 0512/1222] Fix #2217 --- httplib.h | 5 ++++- test/test.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index c035984394..d44766ad3f 100644 --- a/httplib.h +++ b/httplib.h @@ -8984,7 +8984,9 @@ inline bool ClientImpl::create_redirect_client( } // Handle CA certificate store and paths if available - if (ca_cert_store_) { redirect_client.set_ca_cert_store(ca_cert_store_); } + if (ca_cert_store_ && X509_STORE_up_ref(ca_cert_store_)) { + redirect_client.set_ca_cert_store(ca_cert_store_); + } if (!ca_cert_file_path_.empty()) { redirect_client.set_ca_cert_path(ca_cert_file_path_, ca_cert_dir_path_); } @@ -10878,6 +10880,7 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { // Free memory allocated for old cert and use new store `ca_cert_store` SSL_CTX_set_cert_store(ctx_, ca_cert_store); + ca_cert_store_ = ca_cert_store; } } else { X509_STORE_free(ca_cert_store); diff --git a/test/test.cc b/test/test.cc index 23dd0fb418..490260675e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9012,6 +9012,46 @@ TEST(HttpToHttpsRedirectTest, CertFile) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(SSLClientRedirectTest, CertFile) { + SSLServer ssl_svr1(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(ssl_svr1.is_valid()); + ssl_svr1.Get("/index", [&](const Request &, Response &res) { + res.set_redirect("https://127.0.0.1:1235/index"); + ssl_svr1.stop(); + }); + + SSLServer ssl_svr2(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(ssl_svr2.is_valid()); + ssl_svr2.Get("/index", [&](const Request &, Response &res) { + res.set_content("test", "text/plain"); + ssl_svr2.stop(); + }); + + thread t = thread([&]() { ASSERT_TRUE(ssl_svr1.listen("127.0.0.1", PORT)); }); + thread t2 = + thread([&]() { ASSERT_TRUE(ssl_svr2.listen("127.0.0.1", 1235)); }); + auto se = detail::scope_exit([&] { + t2.join(); + t.join(); + ASSERT_FALSE(ssl_svr1.is_running()); + }); + + ssl_svr1.wait_until_ready(); + ssl_svr2.wait_until_ready(); + + SSLClient cli("127.0.0.1", PORT); + std::string cert; + read_file(SERVER_CERT2_FILE, cert); + cli.load_ca_cert_store(cert.c_str(), cert.size()); + cli.enable_server_certificate_verification(true); + cli.set_follow_location(true); + cli.set_connection_timeout(30); + + auto res = cli.Get("/index"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); +} + TEST(MultipartFormDataTest, LargeData) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); From 89c932f313c6437c38f2982869beacc89c2f2246 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 29 Aug 2025 16:05:44 -0400 Subject: [PATCH 0513/1222] Release v0.26.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index d44766ad3f..e15ba44f22 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.25.0" -#define CPPHTTPLIB_VERSION_NUM "0x001900" +#define CPPHTTPLIB_VERSION "0.26.0" +#define CPPHTTPLIB_VERSION_NUM "0x001A00" /* * Platform compatibility check From f72b4582e60051e582543872377dd4223a8e1c33 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Sun, 14 Sep 2025 20:05:51 +0800 Subject: [PATCH 0514/1222] Fix: Fix Windows Cross-Compilation (#2234) --- CMakeLists.txt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d39958a05c..d88fdde449 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,8 +117,15 @@ if(BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) endif() -if(CMAKE_SYSTEM_NAME MATCHES "Windows" AND ${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0") - message(SEND_ERROR "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.") +if(CMAKE_SYSTEM_NAME MATCHES "Windows") + if(CMAKE_SYSTEM_VERSION) + if(${CMAKE_SYSTEM_VERSION} VERSION_LESS "10.0.0") + message(SEND_ERROR "Windows ${CMAKE_SYSTEM_VERSION} or lower is not supported. Please use Windows 10 or later.") + endif() + else() + set(CMAKE_SYSTEM_VERSION "10.0.19041.0") + message(WARNING "The target is Windows but CMAKE_SYSTEM_VERSION is not set, the default system version is set to Windows 10.") + endif() endif() if(CMAKE_SIZEOF_VOID_P LESS 8) message(WARNING "Pointer size ${CMAKE_SIZEOF_VOID_P} is not supported. Please use a 64-bit compiler.") From 6e52d0a0574d02ec0aaabe8610aff89b03da336b Mon Sep 17 00:00:00 2001 From: Jonas van den Berg Date: Mon, 15 Sep 2025 02:05:09 +0200 Subject: [PATCH 0515/1222] Fix UB by use of dangling references in getaddrinfo_with_timeout (#2232) * Fix use of dangling references When the resolve thread is detached, local variables were still used, which could lead to a program crash. * Add test to verify dangling ref fix * Add missing brace initialization * Assert that the remaining fields are really zeroed * Fix use of chrono literals --- httplib.h | 56 +++++++++++++++++++++++++++++++++++++--------------- test/test.cc | 21 ++++++++++++++++++++ 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index e15ba44f22..054e37a7c2 100644 --- a/httplib.h +++ b/httplib.h @@ -3798,31 +3798,55 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, } #else // Fallback implementation using thread-based timeout for other Unix systems - std::mutex result_mutex; - std::condition_variable result_cv; - auto completed = false; - auto result = EAI_SYSTEM; - struct addrinfo *result_addrinfo = nullptr; - std::thread resolve_thread([&]() { - auto thread_result = getaddrinfo(node, service, hints, &result_addrinfo); + struct GetAddrInfoState { + std::mutex mutex{}; + std::condition_variable result_cv{}; + bool completed{false}; + int result{0}; + std::string node{}; + std::string service{}; + struct addrinfo hints {}; + struct addrinfo *info{nullptr}; + }; - std::lock_guard lock(result_mutex); - result = thread_result; - completed = true; - result_cv.notify_one(); + // Allocate on the heap, so the resolver thread can keep using the data. + auto state = std::make_shared(); + + state->result = EAI_SYSTEM; + state->node = node; + state->service = service; + state->hints.ai_flags = hints->ai_flags; + state->hints.ai_family = hints->ai_family; + state->hints.ai_socktype = hints->ai_socktype; + state->hints.ai_protocol = hints->ai_protocol; + // The remaining fields of "hints" must be zeroed, so do not copy them. + assert(hints->ai_addrlen == 0); + assert(hints->ai_addr == nullptr); + assert(hints->ai_canonname == nullptr); + assert(hints->ai_next == nullptr); + + std::thread resolve_thread([=]() { + auto thread_result = getaddrinfo( + state->node.c_str(), state->service.c_str(), hints, &state->info); + + std::lock_guard lock(state->mutex); + state->result = thread_result; + state->completed = true; + state->result_cv.notify_one(); }); // Wait for completion or timeout - std::unique_lock lock(result_mutex); - auto finished = result_cv.wait_for(lock, std::chrono::seconds(timeout_sec), - [&] { return completed; }); + std::unique_lock lock(state->mutex); + auto finished = + state->result_cv.wait_for(lock, std::chrono::seconds(timeout_sec), + [&] { return state->completed; }); if (finished) { // Operation completed within timeout resolve_thread.join(); - *res = result_addrinfo; - return result; + *res = state->info; + return state->result; } else { // Timeout occurred resolve_thread.detach(); // Let the thread finish in background diff --git a/test/test.cc b/test/test.cc index 490260675e..a9ac0d17f1 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1331,6 +1331,27 @@ TEST(RangeTest, FromHTTPBin_Online) { } } +TEST(GetAddrInfoDanglingRefTest, LongTimeout) { + auto host = "unresolvableaddress.local"; + auto path = std::string{"/"}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + auto port = 443; + SSLClient cli(host, port); +#else + auto port = 80; + Client cli(host, port); +#endif + cli.set_connection_timeout(1); + + { + auto res = cli.Get(path); + ASSERT_FALSE(res); + } + + std::this_thread::sleep_for(std::chrono::seconds(8)); +} + TEST(ConnectionErrorTest, InvalidHost) { auto host = "-abcde.com"; From 41be1e24e3a58c17390002aa50a1afc8b1fad284 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 15 Sep 2025 07:59:53 -0400 Subject: [PATCH 0516/1222] Code cleanup --- httplib.h | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/httplib.h b/httplib.h index 054e37a7c2..db55d07e25 100644 --- a/httplib.h +++ b/httplib.h @@ -3800,32 +3800,19 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, // Fallback implementation using thread-based timeout for other Unix systems struct GetAddrInfoState { - std::mutex mutex{}; - std::condition_variable result_cv{}; - bool completed{false}; - int result{0}; - std::string node{}; - std::string service{}; - struct addrinfo hints {}; - struct addrinfo *info{nullptr}; + std::mutex mutex; + std::condition_variable result_cv; + bool completed = false; + int result = EAI_SYSTEM; + std::string node = node; + std::string service = service; + struct addrinfo hints = hints; + struct addrinfo *info = nullptr; }; // Allocate on the heap, so the resolver thread can keep using the data. auto state = std::make_shared(); - state->result = EAI_SYSTEM; - state->node = node; - state->service = service; - state->hints.ai_flags = hints->ai_flags; - state->hints.ai_family = hints->ai_family; - state->hints.ai_socktype = hints->ai_socktype; - state->hints.ai_protocol = hints->ai_protocol; - // The remaining fields of "hints" must be zeroed, so do not copy them. - assert(hints->ai_addrlen == 0); - assert(hints->ai_addr == nullptr); - assert(hints->ai_canonname == nullptr); - assert(hints->ai_next == nullptr); - std::thread resolve_thread([=]() { auto thread_result = getaddrinfo( state->node.c_str(), state->service.c_str(), hints, &state->info); From 23ff9a56055bce4df26f9d421d3e5a1a5a0cb379 Mon Sep 17 00:00:00 2001 From: Alex Kasko Date: Thu, 18 Sep 2025 13:44:39 +0100 Subject: [PATCH 0517/1222] Fix error reporting in SSLClient When the `SSLClient` is used to connect to a plain-HTTP server (which can happen in clients due to some end-user misconfiguration) it can return a failure from the `send()` call without setting the `Error` reference to the corresponding error code. This can cause problems to callers, that may expect that, when the check like this is passed on the response: ```c++ if (res.error() == Error::Success) ``` then they can access the response contents with `res.value()`. When `SSLClient`'s connection fails - the contents `unique_ptr` is not set and an attemt to access it causes UB. This change fixes the `SSLClient::create_and_connect_socket` method making sure that, the `Error` value is set correctly when the `is_valid()` check fails. --- httplib.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index db55d07e25..2327b048f3 100644 --- a/httplib.h +++ b/httplib.h @@ -10911,7 +10911,11 @@ inline long SSLClient::get_openssl_verify_result() const { inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { - return is_valid() && ClientImpl::create_and_connect_socket(socket, error); + if (!is_valid()) { + error = Error::SSLConnection; + return false; + } + return ClientImpl::create_and_connect_socket(socket, error); } // Assumes that socket_mutex_ is locked and that there are no requests in flight From 35c52c1ab9b8ade2e66548fcec5e417092d8e583 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Sat, 20 Sep 2025 21:06:49 +0200 Subject: [PATCH 0518/1222] build(meson): use C++17 for gtest >= 1.17.0 (#2241) --- test/meson.build | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/meson.build b/test/meson.build index 80621d30aa..745236b9e3 100644 --- a/test/meson.build +++ b/test/meson.build @@ -108,9 +108,11 @@ subdir('www') subdir('www2'/'dir') subdir('www3'/'dir') -# GoogleTest 1.13.0 requires C++14 +# New GoogleTest versions require new C++ standards test_options = [] -if gtest_dep.version().version_compare('>=1.13.0') +if gtest_dep.version().version_compare('>=1.17.0') + test_options += 'cpp_std=c++17' +elif gtest_dep.version().version_compare('>=1.13.0') test_options += 'cpp_std=c++14' endif From db561f5552cff751cf70393b6f236e720b12271b Mon Sep 17 00:00:00 2001 From: crueter Date: Thu, 16 Oct 2025 09:59:15 -0400 Subject: [PATCH 0519/1222] [cmake] FindBrotli: do not add `Brotli::` targets if they already exist (#2249) Not checking for this is terrible practice. --- cmake/FindBrotli.cmake | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmake/FindBrotli.cmake b/cmake/FindBrotli.cmake index 2048e55499..de167ed959 100644 --- a/cmake/FindBrotli.cmake +++ b/cmake/FindBrotli.cmake @@ -86,7 +86,9 @@ foreach(_listvar "common;common" "decoder;dec" "encoder;enc") # Check if the target was already found by Pkgconf if(TARGET PkgConfig::Brotli_${_component_name}${_brotli_stat_str}) # ALIAS since we don't want the PkgConfig namespace on the Cmake library (for end-users) - add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str}) + if (NOT TARGET Brotli::${_component_name}) + add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str}) + endif() # Tells HANDLE_COMPONENTS we found the component set(Brotli_${_component_name}_FOUND TRUE) @@ -139,7 +141,10 @@ foreach(_listvar "common;common" "decoder;dec" "encoder;enc") # Tells HANDLE_COMPONENTS we found the component set(Brotli_${_component_name}_FOUND TRUE) - add_library("Brotli::${_component_name}" UNKNOWN IMPORTED) + if (NOT TARGET Brotli::${_component_name}) + add_library("Brotli::${_component_name}" UNKNOWN IMPORTED) + endif() + # Attach the literal library and include dir to the IMPORTED target for the end-user set_target_properties("Brotli::${_component_name}" PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}" From bae40fcdf29fba8210651bb00835431ee52ddd58 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Oct 2025 16:48:45 -0400 Subject: [PATCH 0520/1222] Resolve #2237 --- httplib.h | 79 ++++++++++++++++++++++++------------------ test/test.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 33 deletions(-) diff --git a/httplib.h b/httplib.h index db55d07e25..00db6a8eb9 100644 --- a/httplib.h +++ b/httplib.h @@ -1052,6 +1052,9 @@ class RegexMatcher final : public MatcherBase { ssize_t write_headers(Stream &strm, const Headers &headers); +std::string make_host_and_port_string(const std::string &host, int port, + bool is_ssl); + } // namespace detail class Server { @@ -1719,8 +1722,6 @@ class ClientImpl { const std::string &boundary, const UploadFormDataItems &items, const FormDataProviderItems &provider_items) const; - std::string adjust_host_string(const std::string &host) const; - virtual bool process_socket(const Socket &socket, std::chrono::time_point start_time, @@ -7261,6 +7262,30 @@ inline bool RegexMatcher::match(Request &request) const { return std::regex_match(request.path, request.matches, regex_); } +inline std::string make_host_and_port_string(const std::string &host, int port, + bool is_ssl) { + std::string result; + + // Enclose IPv6 address in brackets (but not if already enclosed) + if (host.find(':') == std::string::npos || + (!host.empty() && host[0] == '[')) { + // IPv4, hostname, or already bracketed IPv6 + result = host; + } else { + // IPv6 address without brackets + result = "[" + host + "]"; + } + + // Append port if not default + if ((!is_ssl && port == 80) || (is_ssl && port == 443)) { + ; // do nothing + } else { + result += ":" + std::to_string(port); + } + + return result; +} + } // namespace detail // HTTP server implementation @@ -8522,7 +8547,7 @@ inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), - host_and_port_(adjust_host_string(host_) + ":" + std::to_string(port)), + host_and_port_(detail::make_host_and_port_string(host_, port, is_ssl())), client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { @@ -8703,8 +8728,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { { std::lock_guard guard(socket_mutex_); - // Set this to false immediately - if it ever gets set to true by the end of - // the request, we know another thread instructed us to close the socket. + // Set this to false immediately - if it ever gets set to true by the end + // of the request, we know another thread instructed us to close the + // socket. socket_should_be_closed_when_request_is_done_ = false; auto is_alive = false; @@ -8720,10 +8746,10 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { #endif if (!is_alive) { - // Attempt to avoid sigpipe by shutting down non-gracefully if it seems - // like the other side has already closed the connection Also, there - // cannot be any requests in flight from other threads since we locked - // request_mutex_, so safe to close everything immediately + // Attempt to avoid sigpipe by shutting down non-gracefully if it + // seems like the other side has already closed the connection Also, + // there cannot be any requests in flight from other threads since we + // locked request_mutex_, so safe to close everything immediately const bool shutdown_gracefully = false; shutdown_ssl(socket_, shutdown_gracefully); shutdown_socket(socket_); @@ -9027,7 +9053,8 @@ inline bool ClientImpl::create_redirect_client( } } -// New method for robust client setup (based on basic_manual_redirect.cpp logic) +// New method for robust client setup (based on basic_manual_redirect.cpp +// logic) template inline void ClientImpl::setup_redirect_client(ClientType &client) { // Copy basic settings first @@ -9131,18 +9158,8 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, // curl behavior) if (address_family_ == AF_UNIX) { req.set_header("Host", "localhost"); - } else if (is_ssl()) { - if (port_ == 443) { - req.set_header("Host", host_); - } else { - req.set_header("Host", host_and_port_); - } } else { - if (port_ == 80) { - req.set_header("Host", host_); - } else { - req.set_header("Host", host_and_port_); - } + req.set_header("Host", host_and_port_); } } @@ -9409,12 +9426,6 @@ inline Result ClientImpl::send_with_content_provider( #endif } -inline std::string -ClientImpl::adjust_host_string(const std::string &host) const { - if (host.find(':') != std::string::npos) { return "[" + host + "]"; } - return host; -} - inline void ClientImpl::output_log(const Request &req, const Response &res) const { if (logger_) { @@ -9538,8 +9549,8 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider( const FormDataProviderItems &provider_items) const { size_t cur_item = 0; size_t cur_start = 0; - // cur_item and cur_start are copied to within the std::function and maintain - // state between successive calls + // cur_item and cur_start are copied to within the std::function and + // maintain state between successive calls return [&, cur_item, cur_start](size_t offset, DataSink &sink) mutable -> bool { if (!offset && !items.empty()) { @@ -10251,8 +10262,8 @@ inline void ClientImpl::stop() { // If there is anything ongoing right now, the ONLY thread-safe thing we can // do is to shutdown_socket, so that threads using this socket suddenly // discover they can't read/write any more and error out. Everything else - // (closing the socket, shutting ssl down) is unsafe because these actions are - // not thread-safe. + // (closing the socket, shutting ssl down) is unsafe because these actions + // are not thread-safe. if (socket_requests_in_flight_ > 0) { shutdown_socket(socket_); @@ -10889,7 +10900,8 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { if (ca_cert_store) { if (ctx_) { if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { - // Free memory allocated for old cert and use new store `ca_cert_store` + // Free memory allocated for old cert and use new store + // `ca_cert_store` SSL_CTX_set_cert_store(ctx_, ca_cert_store); ca_cert_store_ = ca_cert_store; } @@ -10914,7 +10926,8 @@ inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { return is_valid() && ClientImpl::create_and_connect_socket(socket, error); } -// Assumes that socket_mutex_ is locked and that there are no requests in flight +// Assumes that socket_mutex_ is locked and that there are no requests in +// flight inline bool SSLClient::connect_with_proxy( Socket &socket, std::chrono::time_point start_time, diff --git a/test/test.cc b/test/test.cc index a9ac0d17f1..d58b87e391 100644 --- a/test/test.cc +++ b/test/test.cc @@ -10319,6 +10319,103 @@ TEST(FileSystemTest, FileAndDirExistenceCheck) { EXPECT_TRUE(stat_dir.is_dir()); } +TEST(MakeHostAndPortStringTest, VariousPatterns) { + // IPv4 with default HTTP port (80) + EXPECT_EQ("example.com", + detail::make_host_and_port_string("example.com", 80, false)); + + // IPv4 with default HTTPS port (443) + EXPECT_EQ("example.com", + detail::make_host_and_port_string("example.com", 443, true)); + + // IPv4 with non-default HTTP port + EXPECT_EQ("example.com:8080", + detail::make_host_and_port_string("example.com", 8080, false)); + + // IPv4 with non-default HTTPS port + EXPECT_EQ("example.com:8443", + detail::make_host_and_port_string("example.com", 8443, true)); + + // IPv6 with default HTTP port (80) + EXPECT_EQ("[::1]", detail::make_host_and_port_string("::1", 80, false)); + + // IPv6 with default HTTPS port (443) + EXPECT_EQ("[::1]", detail::make_host_and_port_string("::1", 443, true)); + + // IPv6 with non-default HTTP port + EXPECT_EQ("[::1]:8080", + detail::make_host_and_port_string("::1", 8080, false)); + + // IPv6 with non-default HTTPS port + EXPECT_EQ("[::1]:8443", detail::make_host_and_port_string("::1", 8443, true)); + + // IPv6 full address with default port + EXPECT_EQ("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", + detail::make_host_and_port_string( + "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 443, true)); + + // IPv6 full address with non-default port + EXPECT_EQ("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:9000", + detail::make_host_and_port_string( + "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 9000, false)); + + // IPv6 localhost with non-default port + EXPECT_EQ("[::1]:3000", + detail::make_host_and_port_string("::1", 3000, false)); + + // IPv6 with zone ID (link-local address) with default port + EXPECT_EQ("[fe80::1%eth0]", + detail::make_host_and_port_string("fe80::1%eth0", 80, false)); + + // IPv6 with zone ID (link-local address) with non-default port + EXPECT_EQ("[fe80::1%eth0]:8080", + detail::make_host_and_port_string("fe80::1%eth0", 8080, false)); + + // Edge case: Port 443 with is_ssl=false (should add port) + EXPECT_EQ("example.com:443", + detail::make_host_and_port_string("example.com", 443, false)); + + // Edge case: Port 80 with is_ssl=true (should add port) + EXPECT_EQ("example.com:80", + detail::make_host_and_port_string("example.com", 80, true)); + + // IPv6 edge case: Port 443 with is_ssl=false (should add port) + EXPECT_EQ("[::1]:443", detail::make_host_and_port_string("::1", 443, false)); + + // IPv6 edge case: Port 80 with is_ssl=true (should add port) + EXPECT_EQ("[::1]:80", detail::make_host_and_port_string("::1", 80, true)); + + // Security fix: Already bracketed IPv6 should not get double brackets + EXPECT_EQ("[::1]", detail::make_host_and_port_string("[::1]", 80, false)); + EXPECT_EQ("[::1]", detail::make_host_and_port_string("[::1]", 443, true)); + EXPECT_EQ("[::1]:8080", + detail::make_host_and_port_string("[::1]", 8080, false)); + EXPECT_EQ("[2001:db8::1]:8080", + detail::make_host_and_port_string("[2001:db8::1]", 8080, false)); + EXPECT_EQ("[fe80::1%eth0]", + detail::make_host_and_port_string("[fe80::1%eth0]", 80, false)); + EXPECT_EQ("[fe80::1%eth0]:8080", + detail::make_host_and_port_string("[fe80::1%eth0]", 8080, false)); + + // Edge case: Empty host (should return as-is) + EXPECT_EQ("", detail::make_host_and_port_string("", 80, false)); + + // Edge case: Colon in hostname (non-IPv6) - will be treated as IPv6 + // This is a known limitation but shouldn't crash + EXPECT_EQ("[host:name]", + detail::make_host_and_port_string("host:name", 80, false)); + + // Port number edge cases (no validation, but should not crash) + EXPECT_EQ("example.com:0", + detail::make_host_and_port_string("example.com", 0, false)); + EXPECT_EQ("example.com:-1", + detail::make_host_and_port_string("example.com", -1, false)); + EXPECT_EQ("example.com:65535", + detail::make_host_and_port_string("example.com", 65535, false)); + EXPECT_EQ("example.com:65536", + detail::make_host_and_port_string("example.com", 65536, false)); +} + TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { Server svr; From 8aedbf4547b4dc051846536da51695b4f3a2ec88 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Oct 2025 19:31:48 -0400 Subject: [PATCH 0521/1222] Add a unit test --- test/test.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test.cc b/test/test.cc index d58b87e391..3779ca29f6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8366,6 +8366,19 @@ TEST(SSLClientTest, Issue2004_Online) { EXPECT_EQ(body.substr(0, 15), ""); } +TEST(SSLClientTest, ErrorReportingWhenInvalid) { + // Create SSLClient with invalid cert/key to make is_valid() return false + SSLClient cli("localhost", 8080, "nonexistent_cert.pem", + "nonexistent_key.pem"); + + // is_valid() should be false due to cert loading failure + ASSERT_FALSE(cli.is_valid()); + + auto res = cli.Get("/"); + ASSERT_FALSE(res); + EXPECT_EQ(Error::SSLConnection, res.error()); +} + #if 0 TEST(SSLClientTest, SetInterfaceWithINET6) { auto cli = std::make_shared("https://httpbin.org"); From afa88dbe70dadc4e5616b3c53e1e6e0aaeec1a01 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Oct 2025 21:36:53 -0400 Subject: [PATCH 0522/1222] Fix #2250 --- httplib.h | 67 +++++++++++++++++- test/test.cc | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 32bd094387..40b0b4a70b 100644 --- a/httplib.h +++ b/httplib.h @@ -1954,14 +1954,17 @@ class SSLServer : public Server { void update_certs(X509 *cert, EVP_PKEY *private_key, X509_STORE *client_ca_cert_store = nullptr); + int ssl_last_error() const { return last_ssl_error_; } + private: bool process_and_close_socket(socket_t sock) override; + STACK_OF(X509_NAME) * extract_ca_names_from_x509_store(X509_STORE *store); + SSL_CTX *ctx_; std::mutex ctx_mutex_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + int last_ssl_error_ = 0; -#endif }; class SSLClient final : public ClientImpl { @@ -10716,6 +10719,19 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, client_ca_cert_dir_path); + // Set client CA list to be sent to clients during TLS handshake + if (client_ca_cert_file_path) { + auto ca_list = SSL_load_client_CA_file(client_ca_cert_file_path); + if (ca_list != nullptr) { + SSL_CTX_set_client_CA_list(ctx_, ca_list); + } else { + // Failed to load client CA list, but we continue since + // SSL_CTX_load_verify_locations already succeeded and + // certificate verification will still work + last_ssl_error_ = static_cast(ERR_get_error()); + } + } + SSL_CTX_set_verify( ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); } @@ -10740,6 +10756,15 @@ inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, } else if (client_ca_cert_store) { SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + // Extract CA names from the store and set them as the client CA list + auto ca_list = extract_ca_names_from_x509_store(client_ca_cert_store); + if (ca_list) { + SSL_CTX_set_client_CA_list(ctx_, ca_list); + } else { + // Failed to extract CA names, record the error + last_ssl_error_ = static_cast(ERR_get_error()); + } + SSL_CTX_set_verify( ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); } @@ -10820,6 +10845,44 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { return ret; } +inline STACK_OF(X509_NAME) * SSLServer::extract_ca_names_from_x509_store( + X509_STORE *store) { + if (!store) { return nullptr; } + + auto ca_list = sk_X509_NAME_new_null(); + if (!ca_list) { return nullptr; } + + // Get all objects from the store + auto objs = X509_STORE_get0_objects(store); + if (!objs) { + sk_X509_NAME_free(ca_list); + return nullptr; + } + + // Iterate through objects and extract certificate subject names + for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { + auto obj = sk_X509_OBJECT_value(objs, i); + if (X509_OBJECT_get_type(obj) == X509_LU_X509) { + auto cert = X509_OBJECT_get0_X509(obj); + if (cert) { + auto subject = X509_get_subject_name(cert); + if (subject) { + auto name_dup = X509_NAME_dup(subject); + if (name_dup) { sk_X509_NAME_push(ca_list, name_dup); } + } + } + } + } + + // If no names were extracted, free the list and return nullptr + if (sk_X509_NAME_num(ca_list) == 0) { + sk_X509_NAME_free(ca_list); + return nullptr; + } + + return ca_list; +} + // SSL HTTP client implementation inline SSLClient::SSLClient(const std::string &host) : SSLClient(host, 443, std::string(), std::string()) {} diff --git a/test/test.cc b/test/test.cc index 3779ca29f6..beef099df6 100644 --- a/test/test.cc +++ b/test/test.cc @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -8721,6 +8722,197 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(SSLClientServerTest, ClientCAListSentToClient) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); + ASSERT_TRUE(svr.is_valid()); + + // Set up a handler to verify client certificate is present + bool client_cert_verified = false; + svr.Get("/test", [&](const Request & /*req*/, Response &res) { + // Verify that client certificate was provided + client_cert_verified = true; + res.set_content("success", "text/plain"); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + // Client with certificate + SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_TRUE(client_cert_verified); + EXPECT_EQ("success", res->body); +} + +TEST(SSLClientServerTest, ClientCAListSetInContext) { + // Test that when client CA cert file is provided, + // SSL_CTX_set_client_CA_list is called and the CA list is properly set + + // Create a server with client authentication + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); + ASSERT_TRUE(svr.is_valid()); + + // We can't directly access the SSL_CTX from SSLServer to verify, + // but we can test that the server properly requests client certificates + // and accepts valid ones from the specified CA + + bool handler_called = false; + svr.Get("/test", [&](const Request &req, Response &res) { + handler_called = true; + + // Verify that a client certificate was provided + auto peer_cert = SSL_get_peer_certificate(req.ssl); + ASSERT_TRUE(peer_cert != nullptr); + + // Get the issuer name + auto issuer_name = X509_get_issuer_name(peer_cert); + ASSERT_TRUE(issuer_name != nullptr); + + char issuer_buf[256]; + X509_NAME_oneline(issuer_name, issuer_buf, sizeof(issuer_buf)); + + // The client certificate should be issued by our test CA + std::string issuer_str(issuer_buf); + EXPECT_TRUE(issuer_str.find("Root CA Name") != std::string::npos); + + X509_free(peer_cert); + res.set_content("authenticated", "text/plain"); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + // Connect with a client certificate issued by the CA + SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_TRUE(handler_called); + EXPECT_EQ("authenticated", res->body); +} + +TEST(SSLClientServerTest, ClientCAListLoadErrorRecorded) { + // Test 1: Valid CA file - no error should be recorded + { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, + CLIENT_CA_CERT_FILE); + ASSERT_TRUE(svr.is_valid()); + + // With valid setup, last_ssl_error should be 0 + EXPECT_EQ(0, svr.ssl_last_error()); + } + + // Test 2: Invalid CA file content + // When SSL_load_client_CA_file fails, last_ssl_error_ should be set + { + // Create a temporary file with completely invalid content + const char *temp_invalid_ca = "./temp_invalid_ca_for_test.txt"; + { + std::ofstream ofs(temp_invalid_ca); + ofs << "This is not a certificate file at all\n"; + ofs << "Just plain text content\n"; + } + + // Create server with invalid CA file + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, temp_invalid_ca); + + // Clean up temporary file + std::remove(temp_invalid_ca); + + // When there's an SSL error (from either SSL_CTX_load_verify_locations + // or SSL_load_client_CA_file), last_ssl_error_ should be non-zero + // Note: SSL_CTX_load_verify_locations typically fails first, + // but our error handling code path is still exercised + if (!svr.is_valid()) { EXPECT_NE(0, svr.ssl_last_error()); } + } +} + +TEST(SSLClientServerTest, ClientCAListFromX509Store) { + // Test SSL server using X509_STORE constructor with client CA certificates + // This test verifies that Phase 2 implementation correctly extracts CA names + // from an X509_STORE and sets them in the SSL context + + // Load the CA certificate into memory + auto bio = BIO_new_file(CLIENT_CA_CERT_FILE, "r"); + ASSERT_NE(nullptr, bio); + + auto ca_cert = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); + BIO_free(bio); + ASSERT_NE(nullptr, ca_cert); + + // Create an X509_STORE and add the CA certificate + auto store = X509_STORE_new(); + ASSERT_NE(nullptr, store); + ASSERT_EQ(1, X509_STORE_add_cert(store, ca_cert)); + + // Load server certificate and private key + auto cert_bio = BIO_new_file(SERVER_CERT_FILE, "r"); + ASSERT_NE(nullptr, cert_bio); + auto server_cert = PEM_read_bio_X509(cert_bio, nullptr, nullptr, nullptr); + BIO_free(cert_bio); + ASSERT_NE(nullptr, server_cert); + + auto key_bio = BIO_new_file(SERVER_PRIVATE_KEY_FILE, "r"); + ASSERT_NE(nullptr, key_bio); + auto server_key = PEM_read_bio_PrivateKey(key_bio, nullptr, nullptr, nullptr); + BIO_free(key_bio); + ASSERT_NE(nullptr, server_key); + + // Create SSLServer with X509_STORE constructor + // Note: X509_STORE ownership is transferred to SSL_CTX + SSLServer svr(server_cert, server_key, store); + ASSERT_TRUE(svr.is_valid()); + + // No SSL error should be recorded for valid setup + EXPECT_EQ(0, svr.ssl_last_error()); + + // Set up server endpoints + svr.Get("/test-x509store", [&](const Request & /*req*/, Response &res) { + res.set_content("ok", "text/plain"); + }); + + // Start server in a thread + auto server_thread = thread([&]() { svr.listen(HOST, PORT); }); + svr.wait_until_ready(); + + // Connect with client certificate (using constructor with paths) + SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + cli.enable_server_certificate_verification(false); + + auto res = cli.Get("/test-x509store"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ("ok", res->body); + + // Clean up + X509_free(server_cert); + EVP_PKEY_free(server_key); + X509_free(ca_cert); + + svr.stop(); + server_thread.join(); +} + // Disabled due to the out-of-memory problem on GitHub Actions Workflows TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { From 2d8d5241783c877abc804435d87a700de75632f9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Oct 2025 22:06:42 -0400 Subject: [PATCH 0523/1222] Fix #2251 --- httplib.h | 5 +++++ test/test.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/httplib.h b/httplib.h index 40b0b4a70b..d171e56b6b 100644 --- a/httplib.h +++ b/httplib.h @@ -11208,6 +11208,11 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return true; } + if (ctx_ == nullptr) { + error = Error::SSLConnection; + last_openssl_error_ = ERR_get_error(); + } + shutdown_socket(socket); close_socket(socket); return false; diff --git a/test/test.cc b/test/test.cc index beef099df6..d6c06194cb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8380,6 +8380,48 @@ TEST(SSLClientTest, ErrorReportingWhenInvalid) { EXPECT_EQ(Error::SSLConnection, res.error()); } +TEST(SSLClientTest, Issue2251_SwappedClientCertAndKey) { + // Test for Issue #2251: SSL error not properly reported when client cert + // and key paths are swapped or mismatched + // This simulates the scenario where user accidentally swaps the cert and key + // files + + // Using client cert file as private key and vice versa (completely wrong) + SSLClient cli("localhost", 8080, "client.key.pem", "client.cert.pem"); + + // Should fail validation due to cert/key mismatch + ASSERT_FALSE(cli.is_valid()); + + // Attempt to make a request should fail with proper error + auto res = cli.Get("/"); + ASSERT_FALSE(res); + EXPECT_EQ(Error::SSLConnection, res.error()); + + // SSL error should be recorded in the Result object (this is the key fix for + // Issue #2251) + auto openssl_error = res.ssl_openssl_error(); + EXPECT_NE(0u, openssl_error); +} + +TEST(SSLClientTest, Issue2251_ClientCertFileNotMatchingKey) { + // Another variant: using valid file paths but with mismatched cert/key pair + // This tests the case where files exist but contain incompatible key material + + // Using client cert with wrong key (cert2 key) + SSLClient cli("localhost", 8080, "client.cert.pem", "key.pem"); + + // Should fail validation + ASSERT_FALSE(cli.is_valid()); + + auto res = cli.Get("/"); + ASSERT_FALSE(res); + // Must report error properly, not appear as success + EXPECT_EQ(Error::SSLConnection, res.error()); + + // OpenSSL error should be captured in Result + EXPECT_NE(0u, res.ssl_openssl_error()); +} + #if 0 TEST(SSLClientTest, SetInterfaceWithINET6) { auto cli = std::make_shared("https://httpbin.org"); From 318a3fe425c7916c4f273400f9b4bbad1db088f9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 25 Oct 2025 22:35:21 -0400 Subject: [PATCH 0524/1222] Fix problem with installing OpenSSL for Windows (#2254) --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 45dc91c48e..135bf7b500 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -148,7 +148,7 @@ jobs: run: vcpkg install gtest curl zlib brotli zstd - name: Install OpenSSL if: ${{ matrix.config.with_ssl }} - run: choco install openssl --version 3.5.2 # workaround for chocolatey issue with the latest OpenSSL + run: choco install openssl - name: Configure CMake ${{ matrix.config.name }} run: > cmake -B build -S . From 6e0f211cffc45c84c4f406b8e129251b4307d211 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 17:54:27 -0400 Subject: [PATCH 0525/1222] Fix problem with .gitignore for examples --- .gitignore | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a4cacc7732..b9acdca78e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,28 @@ tags +# Ignore executables (no extension) but not source files example/server +!example/server.* example/client +!example/client.* example/hello +!example/hello.* example/simplecli +!example/simplecli.* example/simplesvr +!example/simplesvr.* example/benchmark +!example/benchmark.* example/redirect -example/sse* +!example/redirect.* +example/ssecli +example/ssesvr example/upload +!example/upload.* example/one_time_request +!example/one_time_request.* example/server_and_client +!example/server_and_client.* example/*.pem test/httplib.cc test/httplib.h From 2da189f88cb0d64d7c004e580b0549cab197b56e Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 18:10:52 -0400 Subject: [PATCH 0526/1222] Fix EventDispatcher problem (#2257) --- example/ssesvr.cc | 17 +++++++++++------ test/test.cc | 18 +++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/example/ssesvr.cc b/example/ssesvr.cc index 547b864f3d..6d7390716e 100644 --- a/example/ssesvr.cc +++ b/example/ssesvr.cc @@ -14,11 +14,18 @@ class EventDispatcher { public: EventDispatcher() {} - void wait_event(DataSink *sink) { + bool wait_event(DataSink *sink) { unique_lock lk(m_); int id = id_; - cv_.wait(lk, [&] { return cid_ == id; }); + + // Wait with timeout to prevent hanging if client disconnects + if (!cv_.wait_for(lk, std::chrono::seconds(5), + [&] { return cid_ == id; })) { + return false; // Timeout occurred + } + sink->write(message_.data(), message_.size()); + return true; } void send_event(const string &message) { @@ -71,8 +78,7 @@ int main(void) { cout << "connected to event1..." << endl; res.set_chunked_content_provider("text/event-stream", [&](size_t /*offset*/, DataSink &sink) { - ed.wait_event(&sink); - return true; + return ed.wait_event(&sink); }); }); @@ -80,8 +86,7 @@ int main(void) { cout << "connected to event2..." << endl; res.set_chunked_content_provider("text/event-stream", [&](size_t /*offset*/, DataSink &sink) { - ed.wait_event(&sink); - return true; + return ed.wait_event(&sink); }); }); diff --git a/test/test.cc b/test/test.cc index d6c06194cb..cce375b9d9 100644 --- a/test/test.cc +++ b/test/test.cc @@ -11043,11 +11043,18 @@ class EventDispatcher { public: EventDispatcher() {} - void wait_event(DataSink *sink) { + bool wait_event(DataSink *sink) { unique_lock lk(m_); int id = id_; - cv_.wait(lk, [&] { return cid_ == id; }); + + // Wait with timeout to prevent hanging if client disconnects + if (!cv_.wait_for(lk, std::chrono::seconds(5), + [&] { return cid_ == id; })) { + return false; // Timeout occurred + } + sink->write(message_.data(), message_.size()); + return true; } void send_event(const string &message) { @@ -11072,8 +11079,7 @@ TEST(ClientInThreadTest, Issue2068) { svr.Get("/event1", [&](const Request & /*req*/, Response &res) { res.set_chunked_content_provider("text/event-stream", [&](size_t /*offset*/, DataSink &sink) { - ed.wait_event(&sink); - return true; + return ed.wait_event(&sink); }); }); @@ -11116,9 +11122,11 @@ TEST(ClientInThreadTest, Issue2068) { std::this_thread::sleep_for(std::chrono::seconds(2)); stop = true; client->stop(); - client.reset(); t.join(); + + // Reset client after thread has finished + client.reset(); } } From f3bba0646aadb9ca63f35e3da8f8bb66e1882ce2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 18:27:55 -0400 Subject: [PATCH 0527/1222] Fix benchmark test issue on Windows (#2258) --- test/test.cc | 108 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 40 deletions(-) diff --git a/test/test.cc b/test/test.cc index cce375b9d9..6613ff333e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -11,6 +11,7 @@ #endif #include +#include #include #include #include @@ -78,6 +79,73 @@ static void read_file(const std::string &path, std::string &out) { fs.read(&out[0], static_cast(size)); } +void performance_test(const char *host) { + auto port = 1234; + + Server svr; + + svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) { + res.set_content("Benchmark Response", "text/plain"); + }); + + auto listen_thread = std::thread([&]() { svr.listen(host, port); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(host, port); + + // Warm-up request to establish connection and resolve DNS + auto warmup_res = cli.Get("/benchmark"); + ASSERT_TRUE(warmup_res); // Ensure server is responding correctly + + // Run multiple trials and collect timings + const int num_trials = 20; + std::vector timings; + timings.reserve(num_trials); + + for (int i = 0; i < num_trials; i++) { + auto start = std::chrono::high_resolution_clock::now(); + auto res = cli.Get("/benchmark"); + auto end = std::chrono::high_resolution_clock::now(); + + auto elapsed = + std::chrono::duration_cast(end - start) + .count(); + + // Assertions after timing measurement to avoid overhead + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + timings.push_back(elapsed); + } + + // Calculate 25th percentile (lower quartile) + std::sort(timings.begin(), timings.end()); + auto p25 = timings[num_trials / 4]; + + // Format timings for output + std::ostringstream timings_str; + timings_str << "["; + for (size_t i = 0; i < timings.size(); i++) { + if (i > 0) timings_str << ", "; + timings_str << timings[i]; + } + timings_str << "]"; + + // Localhost HTTP GET should be fast even in CI environments + EXPECT_LE(p25, 5) << "25th percentile performance is too slow: " << p25 + << "ms (Issue #1777). Timings: " << timings_str.str(); +} + +TEST(BenchmarkTest, localhost) { performance_test("localhost"); } + +TEST(BenchmarkTest, v6) { performance_test("::1"); } + class UnixSocketTest : public ::testing::Test { protected: void TearDown() override { std::remove(pathname_.c_str()); } @@ -3634,46 +3702,6 @@ TEST_F(ServerTest, GetMethod200) { EXPECT_EQ("Hello World!", res->body); } -void performance_test(const char *host) { - auto port = 1234; - - Server svr; - - svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) { - res.set_content("Benchmark Response", "text/plain"); - }); - - auto listen_thread = std::thread([&]() { svr.listen(host, port); }); - auto se = detail::scope_exit([&] { - svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); - }); - - svr.wait_until_ready(); - - Client cli(host, port); - - auto start = std::chrono::high_resolution_clock::now(); - - auto res = cli.Get("/benchmark"); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::OK_200, res->status); - - auto end = std::chrono::high_resolution_clock::now(); - - auto elapsed = - std::chrono::duration_cast(end - start) - .count(); - - EXPECT_LE(elapsed, 5) << "Performance is too slow: " << elapsed - << "ms (Issue #1777)"; -} - -TEST(BenchmarkTest, localhost) { performance_test("localhost"); } - -TEST(BenchmarkTest, v6) { performance_test("::1"); } - TEST_F(ServerTest, GetEmptyFile) { auto res = cli_.Get("/empty_file"); ASSERT_TRUE(res); From 11eed05ce71ba5bee0fdf7ddbd05e134ca3176f6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 19:44:51 -0400 Subject: [PATCH 0528/1222] Fix #2255 and #2256 --- httplib.h | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/httplib.h b/httplib.h index d171e56b6b..11acefd972 100644 --- a/httplib.h +++ b/httplib.h @@ -11306,21 +11306,22 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { for (decltype(count) i = 0; i < count && !dsn_matched; i++) { auto val = sk_GENERAL_NAME_value(alt_names, i); - if (val->type == type) { - auto name = - reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); - auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); - - switch (type) { - case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; - - case GEN_IPADD: - if (!memcmp(&addr6, name, addr_len) || - !memcmp(&addr, name, addr_len)) { - ip_matched = true; - } - break; + if (!val || val->type != type) { continue; } + + auto name = + reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); + if (name == nullptr) { continue; } + + auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); + + switch (type) { + case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + + case GEN_IPADD: + if (!memcmp(&addr6, name, addr_len) || !memcmp(&addr, name, addr_len)) { + ip_matched = true; } + break; } } From ac9ebb0ee333ce8bf13523f487bdfad9518a2aff Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 19:54:12 -0400 Subject: [PATCH 0529/1222] Merge commit from fork * Fix "Untrusted HTTP Header Handling (REMOTE*/LOCAL*)" * Fix "Untrusted HTTP Header Handling (X-Forwarded-For)" * Fix security problems in docker/main.cc --- docker/main.cc | 60 +++++++----- httplib.h | 78 ++++++++++++++- test/test.cc | 260 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 355 insertions(+), 43 deletions(-) diff --git a/docker/main.cc b/docker/main.cc index 8ffbf2ca15..edc7093bda 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -48,32 +48,10 @@ std::string get_error_time_format() { return ss.str(); } -std::string get_client_ip(const Request &req) { - // Check for X-Forwarded-For header first (common in reverse proxy setups) - auto forwarded_for = req.get_header_value("X-Forwarded-For"); - if (!forwarded_for.empty()) { - // Get the first IP if there are multiple - auto comma_pos = forwarded_for.find(','); - if (comma_pos != std::string::npos) { - return forwarded_for.substr(0, comma_pos); - } - return forwarded_for; - } - - // Check for X-Real-IP header - auto real_ip = req.get_header_value("X-Real-IP"); - if (!real_ip.empty()) { return real_ip; } - - // Fallback to remote address (though cpp-httplib doesn't provide this - // directly) For demonstration, we'll use a placeholder - return "127.0.0.1"; -} - // NGINX Combined log format: // $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent // "$http_referer" "$http_user_agent" void nginx_access_logger(const Request &req, const Response &res) { - auto remote_addr = get_client_ip(req); std::string remote_user = "-"; // cpp-httplib doesn't have built-in auth user tracking auto time_local = get_time_format(); @@ -86,7 +64,7 @@ void nginx_access_logger(const Request &req, const Response &res) { if (http_user_agent.empty()) http_user_agent = "-"; std::cout << std::format("{} - {} [{}] \"{}\" {} {} \"{}\" \"{}\"", - remote_addr, remote_user, time_local, request, + req.remote_addr, remote_user, time_local, request, status, body_bytes_sent, http_referer, http_user_agent) << std::endl; @@ -100,7 +78,6 @@ void nginx_error_logger(const Error &err, const Request *req) { std::string level = "error"; if (req) { - auto client_ip = get_client_ip(*req); auto request = std::format("{} {} {}", req->method, req->path, req->version); auto host = req->get_header_value("Host"); @@ -108,8 +85,8 @@ void nginx_error_logger(const Error &err, const Request *req) { std::cerr << std::format("{} [{}] {}, client: {}, request: " "\"{}\", host: \"{}\"", - time_local, level, to_string(err), client_ip, - request, host) + time_local, level, to_string(err), + req->remote_addr, request, host) << std::endl; } else { // If no request context, just log the error @@ -131,6 +108,10 @@ void print_usage(const char *program_name) { std::cout << " Format: mount_point:document_root" << std::endl; std::cout << " (default: /:./html)" << std::endl; + std::cout << " --trusted-proxy Add trusted proxy IP address" + << std::endl; + std::cout << " (can be specified multiple times)" + << std::endl; std::cout << " --version Show version information" << std::endl; std::cout << " --help Show this help message" << std::endl; @@ -140,6 +121,9 @@ void print_usage(const char *program_name) { << " --host localhost --port 8080 --mount /:./html" << std::endl; std::cout << " " << program_name << " --host 0.0.0.0 --port 3000 --mount /api:./api" << std::endl; + std::cout << " " << program_name + << " --trusted-proxy 192.168.1.100 --trusted-proxy 10.0.0.1" + << std::endl; } struct ServerConfig { @@ -147,6 +131,7 @@ struct ServerConfig { int port = 8080; std::string mount_point = "/"; std::string document_root = "./html"; + std::vector trusted_proxies; }; enum class ParseResult { SUCCESS, HELP_REQUESTED, VERSION_REQUESTED, ERROR }; @@ -205,6 +190,14 @@ ParseResult parse_command_line(int argc, char *argv[], ServerConfig &config) { } else if (strcmp(argv[i], "--version") == 0) { std::cout << CPPHTTPLIB_VERSION << std::endl; return ParseResult::VERSION_REQUESTED; + } else if (strcmp(argv[i], "--trusted-proxy") == 0) { + if (i + 1 >= argc) { + std::cerr << "Error: --trusted-proxy requires an IP address argument" + << std::endl; + print_usage(argv[0]); + return ParseResult::ERROR; + } + config.trusted_proxies.push_back(argv[++i]); } else { std::cerr << "Error: Unknown option '" << argv[i] << "'" << std::endl; print_usage(argv[0]); @@ -218,6 +211,11 @@ bool setup_server(Server &svr, const ServerConfig &config) { svr.set_logger(nginx_access_logger); svr.set_error_logger(nginx_error_logger); + // Set trusted proxies if specified + if (!config.trusted_proxies.empty()) { + svr.set_trusted_proxies(config.trusted_proxies); + } + auto ret = svr.set_mount_point(config.mount_point, config.document_root); if (!ret) { std::cerr @@ -285,6 +283,16 @@ int main(int argc, char *argv[]) { << std::endl; std::cout << "Mount point: " << config.mount_point << " -> " << config.document_root << std::endl; + + if (!config.trusted_proxies.empty()) { + std::cout << "Trusted proxies: "; + for (size_t i = 0; i < config.trusted_proxies.size(); ++i) { + if (i > 0) std::cout << ", "; + std::cout << config.trusted_proxies[i]; + } + std::cout << std::endl; + } + std::cout << "Press Ctrl+C to shutdown gracefully..." << std::endl; auto ret = svr.listen(config.hostname, config.port); diff --git a/httplib.h b/httplib.h index 11acefd972..1a09b09702 100644 --- a/httplib.h +++ b/httplib.h @@ -1132,6 +1132,8 @@ class Server { Server & set_header_writer(std::function const &writer); + Server &set_trusted_proxies(const std::vector &proxies); + Server &set_keep_alive_max_count(size_t count); Server &set_keep_alive_timeout(time_t sec); @@ -1170,6 +1172,9 @@ class Server { const std::function &setup_request); std::atomic svr_sock_{INVALID_SOCKET}; + + std::vector trusted_proxies_; + size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND; @@ -4600,13 +4605,35 @@ inline bool zstd_decompressor::decompress(const char *data, size_t data_length, } #endif +inline bool is_prohibited_header_name(const std::string &name) { + using udl::operator""_t; + + switch (str2tag(name)) { + case "REMOTE_ADDR"_t: + case "REMOTE_PORT"_t: + case "LOCAL_ADDR"_t: + case "LOCAL_PORT"_t: return true; + default: return false; + } +} + inline bool has_header(const Headers &headers, const std::string &key) { + if (is_prohibited_header_name(key)) { return false; } return headers.find(key) != headers.end(); } inline const char *get_header_value(const Headers &headers, const std::string &key, const char *def, size_t id) { + if (is_prohibited_header_name(key)) { +#ifndef CPPHTTPLIB_NO_EXCEPTIONS + std::string msg = "Prohibited header name '" + key + "' is specified."; + throw std::invalid_argument(msg); +#else + return ""; +#endif + } + auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); @@ -7501,6 +7528,12 @@ inline Server &Server::set_header_writer( return *this; } +inline Server & +Server::set_trusted_proxies(const std::vector &proxies) { + trusted_proxies_ = proxies; + return *this; +} + inline Server &Server::set_keep_alive_max_count(size_t count) { keep_alive_max_count_ = count; return *this; @@ -8289,6 +8322,40 @@ inline bool Server::dispatch_request_for_content_reader( return false; } +inline std::string +get_client_ip(const std::string &x_forwarded_for, + const std::vector &trusted_proxies) { + // X-Forwarded-For is a comma-separated list per RFC 7239 + std::vector ip_list; + detail::split(x_forwarded_for.data(), + x_forwarded_for.data() + x_forwarded_for.size(), ',', + [&](const char *b, const char *e) { + auto r = detail::trim(b, e, 0, static_cast(e - b)); + ip_list.emplace_back(std::string(b + r.first, b + r.second)); + }); + + for (size_t i = 0; i < ip_list.size(); ++i) { + auto ip = ip_list[i]; + + auto is_trusted_proxy = + std::any_of(trusted_proxies.begin(), trusted_proxies.end(), + [&](const std::string &proxy) { return ip == proxy; }); + + if (is_trusted_proxy) { + if (i == 0) { + // If the trusted proxy is the first IP, there's no preceding client IP + return ip; + } else { + // Return the IP immediately before the trusted proxy + return ip_list[i - 1]; + } + } + } + + // If no trusted proxy is found, return the first IP in the list + return ip_list.front(); +} + inline bool Server::process_request(Stream &strm, const std::string &remote_addr, int remote_port, const std::string &local_addr, @@ -8352,15 +8419,16 @@ Server::process_request(Stream &strm, const std::string &remote_addr, connection_closed = true; } - req.remote_addr = remote_addr; + if (!trusted_proxies_.empty() && req.has_header("X-Forwarded-For")) { + auto x_forwarded_for = req.get_header_value("X-Forwarded-For"); + req.remote_addr = get_client_ip(x_forwarded_for, trusted_proxies_); + } else { + req.remote_addr = remote_addr; + } req.remote_port = remote_port; - req.set_header("REMOTE_ADDR", req.remote_addr); - req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); req.local_addr = local_addr; req.local_port = local_port; - req.set_header("LOCAL_ADDR", req.local_addr); - req.set_header("LOCAL_PORT", std::to_string(req.local_port)); if (req.has_header("Accept")) { const auto &accept_header = req.get_header_value("Accept"); diff --git a/test/test.cc b/test/test.cc index 6613ff333e..e5b3639629 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3100,21 +3100,20 @@ class ServerTest : public ::testing::Test { #endif .Get("/remote_addr", [&](const Request &req, Response &res) { - auto remote_addr = req.headers.find("REMOTE_ADDR")->second; - EXPECT_TRUE(req.has_header("REMOTE_PORT")); - EXPECT_EQ(req.remote_addr, req.get_header_value("REMOTE_ADDR")); - EXPECT_EQ(req.remote_port, - std::stoi(req.get_header_value("REMOTE_PORT"))); - res.set_content(remote_addr.c_str(), "text/plain"); + ASSERT_FALSE(req.has_header("REMOTE_ADDR")); + ASSERT_FALSE(req.has_header("REMOTE_PORT")); + ASSERT_ANY_THROW(req.get_header_value("REMOTE_ADDR")); + ASSERT_ANY_THROW(req.get_header_value("REMOTE_PORT")); + res.set_content(req.remote_addr, "text/plain"); }) .Get("/local_addr", [&](const Request &req, Response &res) { - EXPECT_TRUE(req.has_header("LOCAL_PORT")); - EXPECT_TRUE(req.has_header("LOCAL_ADDR")); - auto local_addr = req.get_header_value("LOCAL_ADDR"); - auto local_port = req.get_header_value("LOCAL_PORT"); - EXPECT_EQ(req.local_addr, local_addr); - EXPECT_EQ(req.local_port, std::stoi(local_port)); + ASSERT_FALSE(req.has_header("LOCAL_ADDR")); + ASSERT_FALSE(req.has_header("LOCAL_PORT")); + ASSERT_ANY_THROW(req.get_header_value("LOCAL_ADDR")); + ASSERT_ANY_THROW(req.get_header_value("LOCAL_PORT")); + auto local_addr = req.local_addr; + auto local_port = std::to_string(req.local_port); res.set_content(local_addr.append(":").append(local_port), "text/plain"); }) @@ -11208,3 +11207,240 @@ TEST(HeaderSmugglingTest, ChunkedTrailerHeadersMerged) { std::string res; ASSERT_TRUE(send_request(1, req, &res)); } + +TEST(ForwardedHeadersTest, NoProxiesSetting) { + Server svr; + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get("/ip", {{"X-Forwarded-For", "203.0.113.66"}}); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + EXPECT_EQ(observed_xff, "203.0.113.66"); + EXPECT_TRUE(observed_remote_addr == "::1" || observed_remote_addr == "127.0.0.1"); +} + +TEST(ForwardedHeadersTest, NoForwardedHeaders) { + Server svr; + + svr.set_trusted_proxies({"203.0.113.66"}); + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get("/ip"); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + EXPECT_EQ(observed_xff, ""); + EXPECT_TRUE(observed_remote_addr == "::1" || observed_remote_addr == "127.0.0.1"); +} + +TEST(ForwardedHeadersTest, SingleTrustedProxy_UsesIPBeforeTrusted) { + Server svr; + + svr.set_trusted_proxies({"203.0.113.66"}); + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get("/ip", {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66"}}); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + EXPECT_EQ(observed_xff, "198.51.100.23, 203.0.113.66"); + EXPECT_EQ(observed_remote_addr, "198.51.100.23"); +} + +TEST(ForwardedHeadersTest, MultipleTrustedProxies_UsesClientIP) { + Server svr; + + svr.set_trusted_proxies({"203.0.113.66", "192.0.2.45"}); + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get( + "/ip", + {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66, 192.0.2.45"}}); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + EXPECT_EQ(observed_xff, "198.51.100.23, 203.0.113.66, 192.0.2.45"); + EXPECT_EQ(observed_remote_addr, "198.51.100.23"); +} + +TEST(ForwardedHeadersTest, TrustedProxyNotInHeader_UsesFirstFromXFF) { + Server svr; + + svr.set_trusted_proxies({"192.0.2.45"}); + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get("/ip", + {{"X-Forwarded-For", "198.51.100.23, 198.51.100.24"}}); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + EXPECT_EQ(observed_xff, "198.51.100.23, 198.51.100.24"); + EXPECT_EQ(observed_remote_addr, "198.51.100.23"); +} + +TEST(ForwardedHeadersTest, LastHopTrusted_SelectsImmediateLeftIP) { + Server svr; + + svr.set_trusted_proxies({"192.0.2.45"}); + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get( + "/ip", + {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66, 192.0.2.45"}}); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + EXPECT_EQ(observed_xff, "198.51.100.23, 203.0.113.66, 192.0.2.45"); + EXPECT_EQ(observed_remote_addr, "203.0.113.66"); +} + +TEST(ForwardedHeadersTest, HandlesWhitespaceAroundIPs) { + Server svr; + + svr.set_trusted_proxies({"192.0.2.45"}); + + std::string observed_remote_addr; + std::string observed_xff; + + svr.Get("/ip", [&](const Request &req, Response &res) { + observed_remote_addr = req.remote_addr; + observed_xff = req.get_header_value("X-Forwarded-For"); + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + auto res = cli.Get( + "/ip", + {{"X-Forwarded-For", " 198.51.100.23 , 203.0.113.66 , 192.0.2.45 "}}); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + + // Header parser trims surrounding whitespace of the header value + EXPECT_EQ(observed_xff, "198.51.100.23 , 203.0.113.66 , 192.0.2.45"); + EXPECT_EQ(observed_remote_addr, "203.0.113.66"); +} From eacc1ca98e5fef25184c7d417e8417225e05e65d Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 19:57:53 -0400 Subject: [PATCH 0530/1222] Release v0.27.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 1a09b09702..b76a17d07a 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.26.0" -#define CPPHTTPLIB_VERSION_NUM "0x001A00" +#define CPPHTTPLIB_VERSION "0.27.0" +#define CPPHTTPLIB_VERSION_NUM "0x001B00" /* * Platform compatibility check From 551f96d4a2a07a56d72a13d4837b31eead884d34 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 27 Oct 2025 20:40:12 -0400 Subject: [PATCH 0531/1222] Remove REMOTE_PORT dependency from UnixSocketTest.PeerPid --- .gitignore | 2 ++ test/test.cc | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index b9acdca78e..21839782df 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,8 @@ example/one_time_request !example/one_time_request.* example/server_and_client !example/server_and_client.* +example/accept_header +!example/accept_header.* example/*.pem test/httplib.cc test/httplib.h diff --git a/test/test.cc b/test/test.cc index e5b3639629..c9d6421719 100644 --- a/test/test.cc +++ b/test/test.cc @@ -196,7 +196,7 @@ TEST_F(UnixSocketTest, PeerPid) { std::string remote_port_val; svr.Get(pattern_, [&](const httplib::Request &req, httplib::Response &res) { res.set_content(content_, "text/plain"); - remote_port_val = req.get_header_value("REMOTE_PORT"); + remote_port_val = std::to_string(req.remote_port); }); std::thread t{[&] { @@ -11236,7 +11236,8 @@ TEST(ForwardedHeadersTest, NoProxiesSetting) { EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(observed_xff, "203.0.113.66"); - EXPECT_TRUE(observed_remote_addr == "::1" || observed_remote_addr == "127.0.0.1"); + EXPECT_TRUE(observed_remote_addr == "::1" || + observed_remote_addr == "127.0.0.1"); } TEST(ForwardedHeadersTest, NoForwardedHeaders) { @@ -11269,7 +11270,8 @@ TEST(ForwardedHeadersTest, NoForwardedHeaders) { EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(observed_xff, ""); - EXPECT_TRUE(observed_remote_addr == "::1" || observed_remote_addr == "127.0.0.1"); + EXPECT_TRUE(observed_remote_addr == "::1" || + observed_remote_addr == "127.0.0.1"); } TEST(ForwardedHeadersTest, SingleTrustedProxy_UsesIPBeforeTrusted) { @@ -11296,7 +11298,8 @@ TEST(ForwardedHeadersTest, SingleTrustedProxy_UsesIPBeforeTrusted) { svr.wait_until_ready(); Client cli(HOST, PORT); - auto res = cli.Get("/ip", {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66"}}); + auto res = + cli.Get("/ip", {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -11330,8 +11333,7 @@ TEST(ForwardedHeadersTest, MultipleTrustedProxies_UsesClientIP) { Client cli(HOST, PORT); auto res = cli.Get( - "/ip", - {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66, 192.0.2.45"}}); + "/ip", {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66, 192.0.2.45"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -11364,8 +11366,8 @@ TEST(ForwardedHeadersTest, TrustedProxyNotInHeader_UsesFirstFromXFF) { svr.wait_until_ready(); Client cli(HOST, PORT); - auto res = cli.Get("/ip", - {{"X-Forwarded-For", "198.51.100.23, 198.51.100.24"}}); + auto res = + cli.Get("/ip", {{"X-Forwarded-For", "198.51.100.23, 198.51.100.24"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -11399,8 +11401,7 @@ TEST(ForwardedHeadersTest, LastHopTrusted_SelectsImmediateLeftIP) { Client cli(HOST, PORT); auto res = cli.Get( - "/ip", - {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66, 192.0.2.45"}}); + "/ip", {{"X-Forwarded-For", "198.51.100.23, 203.0.113.66, 192.0.2.45"}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); @@ -11433,9 +11434,8 @@ TEST(ForwardedHeadersTest, HandlesWhitespaceAroundIPs) { svr.wait_until_ready(); Client cli(HOST, PORT); - auto res = cli.Get( - "/ip", - {{"X-Forwarded-For", " 198.51.100.23 , 203.0.113.66 , 192.0.2.45 "}}); + auto res = cli.Get("/ip", {{"X-Forwarded-For", + " 198.51.100.23 , 203.0.113.66 , 192.0.2.45 "}}); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); From 4b2b851dbb91f9d6c2299976b5d03f7c5c1a312d Mon Sep 17 00:00:00 2001 From: chansikpark Date: Sun, 2 Nov 2025 22:23:42 -0500 Subject: [PATCH 0532/1222] Fix HTTP 414 errors hanging until timeout (#2260) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix HTTP 414 errors hanging until timeout * All errors (status code 400+) close the connection * 🧹 --------- Co-authored-by: Wor Ker --- httplib.h | 5 ++--- test/test.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index b76a17d07a..8411c8c93b 100644 --- a/httplib.h +++ b/httplib.h @@ -7692,7 +7692,8 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } // Prepare additional headers - if (close_connection || req.get_header_value("Connection") == "close") { + if (close_connection || req.get_header_value("Connection") == "close" || + 400 <= res.status) { // Don't leave connections open after errors res.set_header("Connection", "close"); } else { std::string s = "timeout="; @@ -8403,8 +8404,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr, // Check if the request URI doesn't exceed the limit if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { - Headers dummy; - detail::read_headers(strm, dummy); res.status = StatusCode::UriTooLong_414; output_error_log(Error::ExceedUriMaxLength, &req); return write_response(strm, close_connection, req, res); diff --git a/test/test.cc b/test/test.cc index c9d6421719..b54c0f0afb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4289,10 +4289,21 @@ TEST_F(ServerTest, TooLongRequest) { } request += "_NG"; + auto start = std::chrono::high_resolution_clock::now(); + + cli_.set_keep_alive(true); auto res = cli_.Get(request.c_str()); + auto end = std::chrono::high_resolution_clock::now(); + auto elapsed = + std::chrono::duration_cast(end - start) + .count(); + ASSERT_TRUE(res); EXPECT_EQ(StatusCode::UriTooLong_414, res->status); + EXPECT_LE(elapsed, 100); + EXPECT_EQ("close", res->get_header_value("Connection")); + EXPECT_FALSE(cli_.is_socket_open()); } TEST_F(ServerTest, AlmostTooLongRequest) { @@ -4363,10 +4374,21 @@ TEST_F(ServerTest, LongHeader) { } TEST_F(ServerTest, LongQueryValue) { + auto start = std::chrono::high_resolution_clock::now(); + + cli_.set_keep_alive(true); auto res = cli_.Get(LONG_QUERY_URL.c_str()); + auto end = std::chrono::high_resolution_clock::now(); + auto elapsed = + std::chrono::duration_cast(end - start) + .count(); + ASSERT_TRUE(res); EXPECT_EQ(StatusCode::UriTooLong_414, res->status); + EXPECT_LE(elapsed, 100); + EXPECT_EQ("close", res->get_header_value("Connection")); + EXPECT_FALSE(cli_.is_socket_open()); } TEST_F(ServerTest, TooLongQueryValue) { @@ -4460,6 +4482,7 @@ TEST_F(ServerTest, HeaderCountExceedsLimit) { } // This should fail due to exceeding header count limit + cli_.set_keep_alive(true); auto res = cli_.Get("/hi", headers); // The request should either fail or return 400 Bad Request @@ -4470,6 +4493,9 @@ TEST_F(ServerTest, HeaderCountExceedsLimit) { // Or the request should fail entirely EXPECT_FALSE(res); } + + EXPECT_EQ("close", res->get_header_value("Connection")); + EXPECT_FALSE(cli_.is_socket_open()); } TEST_F(ServerTest, PercentEncoding) { @@ -4524,6 +4550,7 @@ TEST_F(ServerTest, HeaderCountSecurityTest) { } // Try to POST with excessive headers + cli_.set_keep_alive(true); auto res = cli_.Post("/", attack_headers, "test_data", "text/plain"); // Should either fail or return 400 Bad Request due to security limit @@ -4534,6 +4561,9 @@ TEST_F(ServerTest, HeaderCountSecurityTest) { // Request failed, which is the expected behavior for DoS protection EXPECT_FALSE(res); } + + EXPECT_EQ("close", res->get_header_value("Connection")); + EXPECT_FALSE(cli_.is_socket_open()); } TEST_F(ServerTest, MultipartFormData) { @@ -5854,6 +5884,20 @@ TEST_F(ServerTest, TooManyRedirect) { EXPECT_EQ(Error::ExceedRedirectCount, res.error()); } +TEST_F(ServerTest, BadRequestLineCancelsKeepAlive) { + Request req; + req.method = "FOOBAR"; + req.path = "/hi"; + + cli_.set_keep_alive(true); + auto res = cli_.send(req); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); + EXPECT_EQ("close", res->get_header_value("Connection")); + EXPECT_FALSE(cli_.is_socket_open()); +} + TEST_F(ServerTest, StartTime) { auto res = cli_.Get("/test-start-time"); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT From 1acf18876fdbd1a4f3ff91f43cf21495af357850 Mon Sep 17 00:00:00 2001 From: VZ Date: Tue, 11 Nov 2025 04:17:43 +0100 Subject: [PATCH 0533/1222] CMake: Add HTTPLIB_SHARED option, don't define BUILD_SHARED_LIBS (#2266) To avoid surprises in the projects consuming the library, don't define BUILD_SHARED_LIBS option ourselves but just use its value, if provided, to define HTTPLIB_SHARED option which can be also set directly to specify whether we should build static or shared library. Closes #2263. --- CMakeLists.txt | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d88fdde449..8a8ab0a8d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ #[[ Build options: - * BUILD_SHARED_LIBS (default off) builds as a shared library (if HTTPLIB_COMPILE is ON) + * Standard BUILD_SHARED_LIBS is supported and sets HTTPLIB_SHARED default value. * HTTPLIB_USE_OPENSSL_IF_AVAILABLE (default on) * HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on) * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) @@ -13,6 +13,7 @@ * HTTPLIB_USE_NON_BLOCKING_GETADDRINFO (default on) * HTTPLIB_COMPILE (default off) * HTTPLIB_INSTALL (default on) + * HTTPLIB_SHARED (default off) builds as a shared library (if HTTPLIB_COMPILE is ON) * HTTPLIB_TEST (default off) * BROTLI_USE_STATIC_LIBS - tells Cmake to use the static Brotli libs (only works if you have them installed). * OPENSSL_USE_STATIC_LIBS - tells Cmake to use the static OpenSSL libs (only works if you have them installed). @@ -109,12 +110,20 @@ option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system cer option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON) option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF) option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON) -# Defaults to static library -option(BUILD_SHARED_LIBS "Build the library as a shared library instead of static. Has no effect if using header-only." OFF) -if(BUILD_SHARED_LIBS AND WIN32 AND HTTPLIB_COMPILE) - # Necessary for Windows if building shared libs - # See https://stackoverflow.com/a/40743080 - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) +# Defaults to static library but respects standard BUILD_SHARED_LIBS if set +include(CMakeDependentOption) +cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only." + "${BUILD_SHARED_LIBS}" HTTPLIB_COMPILE OFF +) +if(HTTPLIB_SHARED) + set(HTTPLIB_LIB_TYPE SHARED) + if(WIN32) + # Necessary for Windows if building shared libs + # See https://stackoverflow.com/a/40743080 + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + endif() +else() + set(HTTPLIB_LIB_TYPE STATIC) endif() if(CMAKE_SYSTEM_NAME MATCHES "Windows") @@ -230,8 +239,7 @@ if(HTTPLIB_COMPILE) # split.py puts output in "out" set(_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR}/out") - # This will automatically be either static or shared based on the value of BUILD_SHARED_LIBS - add_library(${PROJECT_NAME} "${_httplib_build_includedir}/httplib.cc") + add_library(${PROJECT_NAME} ${HTTPLIB_LIB_TYPE} "${_httplib_build_includedir}/httplib.cc") target_sources(${PROJECT_NAME} PUBLIC $ From 61e9f7ce8f310d897fda2a49a122a40f66573a39 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 14 Nov 2025 14:17:04 -0500 Subject: [PATCH 0534/1222] Fix #2270 --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a69f7be791..edecbb6a42 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,10 @@ httplib::Client cli("http://yhirose.github.io"); // HTTPS httplib::Client cli("https://yhirose.github.io"); -auto res = cli.Get("/hi"); -res->status; -res->body; +if (auto res = cli.Get("/hi")) { + res->status; + res->body; +} ``` SSL Support @@ -190,7 +191,7 @@ int main(void) } ``` -`Post`, `Put`, `Delete` and `Options` methods are also supported. +`Post`, `Put`, `Patch`, `Delete` and `Options` methods are also supported. ### Bind a socket to multiple interfaces and any available port @@ -853,6 +854,12 @@ auto res = cli.Post("/multipart", items); res = cli.Put("/resource/foo", "text", "text/plain"); ``` +### PATCH + +```c++ +res = cli.Patch("/resource/foo", "text", "text/plain"); +``` + ### DELETE ```c++ From 59882752aaa98e290d1d19ded0fbc4da4fee0072 Mon Sep 17 00:00:00 2001 From: Clarkok Zhang Date: Fri, 14 Nov 2025 11:52:06 -0800 Subject: [PATCH 0535/1222] Add Client::Post with both content provider and receiver (#2268) Co-authored-by: Clarkok Zhang --- httplib.h | 363 +++++++++++++++++++++++++++++++++++++++++++-------- test/test.cc | 121 +++++++++++++++++ 2 files changed, 432 insertions(+), 52 deletions(-) diff --git a/httplib.h b/httplib.h index 8411c8c93b..8ed840efd7 100644 --- a/httplib.h +++ b/httplib.h @@ -1421,14 +1421,18 @@ class ClientImpl { Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const Params ¶ms); Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); @@ -1439,14 +1443,18 @@ class ClientImpl { Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const Params ¶ms); Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); @@ -1457,14 +1465,18 @@ class ClientImpl { Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Params ¶ms); Result Patch(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); @@ -1712,17 +1724,19 @@ class ClientImpl { template void setup_redirect_client(ClientType &client); bool handle_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error); - std::unique_ptr send_with_content_provider( + std::unique_ptr send_with_content_provider_and_receiver( Request &req, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error); - Result send_with_content_provider( + const std::string &content_type, ContentReceiver content_receiver, + Error &error); + Result send_with_content_provider_and_receiver( const std::string &method, const std::string &path, const Headers &headers, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, UploadProgress progress); + const std::string &content_type, ContentReceiver content_receiver, + UploadProgress progress); ContentProviderWithoutLength get_multipart_content_provider( const std::string &boundary, const UploadFormDataItems &items, const FormDataProviderItems &provider_items) const; @@ -1775,14 +1789,18 @@ class Client { Result Post(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Post(const std::string &path, const Params ¶ms); Result Post(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers); Result Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const Params ¶ms); Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Post(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); @@ -1793,14 +1811,18 @@ class Client { Result Put(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, const Params ¶ms); Result Put(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers); Result Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const Params ¶ms); Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Put(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); @@ -1811,14 +1833,18 @@ class Client { Result Patch(const std::string &path, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Params ¶ms); Result Patch(const std::string &path, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers); Result Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress = nullptr); + Result Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, ContentReceiver content_receiver, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const Params ¶ms); Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, UploadProgress progress = nullptr); Result Patch(const std::string &path, const Headers &headers, const UploadFormDataItems &items, const std::string &boundary, UploadProgress progress = nullptr); @@ -9387,11 +9413,13 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, return true; } -inline std::unique_ptr ClientImpl::send_with_content_provider( +inline std::unique_ptr +ClientImpl::send_with_content_provider_and_receiver( Request &req, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error) { + const std::string &content_type, ContentReceiver content_receiver, + Error &error) { if (!content_type.empty()) { req.set_header("Content-Type", content_type); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT @@ -9464,15 +9492,24 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( } } + if (content_receiver) { + req.content_receiver = + [content_receiver](const char *data, size_t data_length, + size_t /*offset*/, size_t /*total_length*/) { + return content_receiver(data, data_length); + }; + } + auto res = detail::make_unique(); return send(req, *res, error) ? std::move(res) : nullptr; } -inline Result ClientImpl::send_with_content_provider( +inline Result ClientImpl::send_with_content_provider_and_receiver( const std::string &method, const std::string &path, const Headers &headers, const char *body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, UploadProgress progress) { + const std::string &content_type, ContentReceiver content_receiver, + UploadProgress progress) { Request req; req.method = method; req.headers = headers; @@ -9484,9 +9521,10 @@ inline Result ClientImpl::send_with_content_provider( auto error = Error::Success; - auto res = send_with_content_provider( + auto res = send_with_content_provider_and_receiver( req, body, content_length, std::move(content_provider), - std::move(content_provider_without_length), content_type, error); + std::move(content_provider_without_length), content_type, + std::move(content_receiver), error); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT return Result{std::move(res), error, std::move(req.headers), last_ssl_error_, @@ -9815,6 +9853,15 @@ inline Result ClientImpl::Post(const std::string &path, size_t content_length, content_type, progress); } +inline Result ClientImpl::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return Post(path, Headers(), content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} + inline Result ClientImpl::Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, @@ -9823,6 +9870,15 @@ inline Result ClientImpl::Post(const std::string &path, progress); } +inline Result ClientImpl::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return Post(path, Headers(), std::move(content_provider), content_type, + std::move(content_receiver), progress); +} + inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const Params ¶ms) { auto query = detail::params_to_query_str(params); @@ -9863,17 +9919,18 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("POST", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); + return send_with_content_provider_and_receiver( + "POST", path, headers, body, content_length, nullptr, nullptr, + content_type, nullptr, progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("POST", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); + return send_with_content_provider_and_receiver( + "POST", path, headers, body.data(), body.size(), nullptr, nullptr, + content_type, nullptr, progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, @@ -9881,18 +9938,40 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, ContentProvider content_provider, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("POST", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, progress); + return send_with_content_provider_and_receiver( + "POST", path, headers, nullptr, content_length, + std::move(content_provider), nullptr, content_type, nullptr, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return send_with_content_provider_and_receiver( + "POST", path, headers, nullptr, content_length, + std::move(content_provider), nullptr, content_type, + std::move(content_receiver), std::move(progress)); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - progress); + return send_with_content_provider_and_receiver( + "POST", path, headers, nullptr, 0, nullptr, std::move(content_provider), + content_type, nullptr, progress); +} + +inline Result ClientImpl::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return send_with_content_provider_and_receiver( + "POST", path, headers, nullptr, 0, nullptr, std::move(content_provider), + content_type, std::move(content_receiver), std::move(progress)); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, @@ -9902,10 +9981,10 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers, const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( + return send_with_content_provider_and_receiver( "POST", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type, progress); + content_type, nullptr, progress); } inline Result ClientImpl::Post(const std::string &path, const Headers &headers, @@ -9967,6 +10046,15 @@ inline Result ClientImpl::Put(const std::string &path, size_t content_length, content_type, progress); } +inline Result ClientImpl::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return Put(path, Headers(), content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} + inline Result ClientImpl::Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, @@ -9975,6 +10063,15 @@ inline Result ClientImpl::Put(const std::string &path, progress); } +inline Result ClientImpl::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return Put(path, Headers(), std::move(content_provider), content_type, + std::move(content_receiver), progress); +} + inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const Params ¶ms) { auto query = detail::params_to_query_str(params); @@ -10015,17 +10112,18 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PUT", path, headers, body, content_length, - nullptr, nullptr, content_type, progress); + return send_with_content_provider_and_receiver( + "PUT", path, headers, body, content_length, nullptr, nullptr, + content_type, nullptr, progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PUT", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); + return send_with_content_provider_and_receiver( + "PUT", path, headers, body.data(), body.size(), nullptr, nullptr, + content_type, nullptr, progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, @@ -10033,18 +10131,40 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, ContentProvider content_provider, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PUT", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, progress); + return send_with_content_provider_and_receiver( + "PUT", path, headers, nullptr, content_length, + std::move(content_provider), nullptr, content_type, nullptr, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return send_with_content_provider_and_receiver( + "PUT", path, headers, nullptr, content_length, + std::move(content_provider), nullptr, content_type, + std::move(content_receiver), progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - progress); + return send_with_content_provider_and_receiver( + "PUT", path, headers, nullptr, 0, nullptr, std::move(content_provider), + content_type, nullptr, progress); +} + +inline Result ClientImpl::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return send_with_content_provider_and_receiver( + "PUT", path, headers, nullptr, 0, nullptr, std::move(content_provider), + content_type, std::move(content_receiver), progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, @@ -10054,10 +10174,10 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers, const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( + return send_with_content_provider_and_receiver( "PUT", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type, progress); + content_type, nullptr, progress); } inline Result ClientImpl::Put(const std::string &path, const Headers &headers, @@ -10121,6 +10241,15 @@ inline Result ClientImpl::Patch(const std::string &path, size_t content_length, content_type, progress); } +inline Result ClientImpl::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return Patch(path, Headers(), content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} + inline Result ClientImpl::Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, @@ -10129,6 +10258,15 @@ inline Result ClientImpl::Patch(const std::string &path, progress); } +inline Result ClientImpl::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return Patch(path, Headers(), std::move(content_provider), content_type, + std::move(content_receiver), progress); +} + inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const Params ¶ms) { auto query = detail::params_to_query_str(params); @@ -10169,18 +10307,18 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const char *body, size_t content_length, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PATCH", path, headers, body, - content_length, nullptr, nullptr, - content_type, progress); + return send_with_content_provider_and_receiver( + "PATCH", path, headers, body, content_length, nullptr, nullptr, + content_type, nullptr, progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const std::string &body, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PATCH", path, headers, body.data(), - body.size(), nullptr, nullptr, content_type, - progress); + return send_with_content_provider_and_receiver( + "PATCH", path, headers, body.data(), body.size(), nullptr, nullptr, + content_type, nullptr, progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, @@ -10188,18 +10326,40 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, ContentProvider content_provider, const std::string &content_type, UploadProgress progress) { - return send_with_content_provider("PATCH", path, headers, nullptr, - content_length, std::move(content_provider), - nullptr, content_type, progress); + return send_with_content_provider_and_receiver( + "PATCH", path, headers, nullptr, content_length, + std::move(content_provider), nullptr, content_type, nullptr, progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return send_with_content_provider_and_receiver( + "PATCH", path, headers, nullptr, content_length, + std::move(content_provider), nullptr, content_type, + std::move(content_receiver), progress); +} + +inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return send_with_content_provider_and_receiver( + "PATCH", path, headers, nullptr, 0, nullptr, std::move(content_provider), + content_type, nullptr, progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, + ContentReceiver content_receiver, UploadProgress progress) { - return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, - std::move(content_provider), content_type, - progress); + return send_with_content_provider_and_receiver( + "PATCH", path, headers, nullptr, 0, nullptr, std::move(content_provider), + content_type, std::move(content_receiver), progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, @@ -10209,10 +10369,10 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, const auto &boundary = detail::make_multipart_data_boundary(); const auto &content_type = detail::serialize_multipart_formdata_get_content_type(boundary); - return send_with_content_provider( + return send_with_content_provider_and_receiver( "PATCH", path, headers, nullptr, 0, nullptr, get_multipart_content_provider(boundary, items, provider_items), - content_type, progress); + content_type, nullptr, progress); } inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, @@ -11604,12 +11764,28 @@ inline Result Client::Post(const std::string &path, size_t content_length, return cli_->Post(path, content_length, std::move(content_provider), content_type, progress); } +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} inline Result Client::Post(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress) { return cli_->Post(path, std::move(content_provider), content_type, progress); } +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Post(path, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} inline Result Client::Post(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, @@ -11618,6 +11794,15 @@ inline Result Client::Post(const std::string &path, const Headers &headers, return cli_->Post(path, headers, content_length, std::move(content_provider), content_type, progress); } +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} inline Result Client::Post(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, @@ -11625,6 +11810,14 @@ inline Result Client::Post(const std::string &path, const Headers &headers, return cli_->Post(path, headers, std::move(content_provider), content_type, progress); } +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} inline Result Client::Post(const std::string &path, const Params ¶ms) { return cli_->Post(path, params); } @@ -11659,8 +11852,8 @@ inline Result Client::Post(const std::string &path, const Headers &headers, const std::string &content_type, ContentReceiver content_receiver, DownloadProgress progress) { - return cli_->Post(path, headers, body, content_type, content_receiver, - progress); + return cli_->Post(path, headers, body, content_type, + std::move(content_receiver), progress); } inline Result Client::Put(const std::string &path) { return cli_->Put(path); } @@ -11697,12 +11890,28 @@ inline Result Client::Put(const std::string &path, size_t content_length, return cli_->Put(path, content_length, std::move(content_provider), content_type, progress); } +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} inline Result Client::Put(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress) { return cli_->Put(path, std::move(content_provider), content_type, progress); } +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} inline Result Client::Put(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, @@ -11711,6 +11920,15 @@ inline Result Client::Put(const std::string &path, const Headers &headers, return cli_->Put(path, headers, content_length, std::move(content_provider), content_type, progress); } +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} inline Result Client::Put(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, @@ -11718,6 +11936,14 @@ inline Result Client::Put(const std::string &path, const Headers &headers, return cli_->Put(path, headers, std::move(content_provider), content_type, progress); } +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, headers, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} inline Result Client::Put(const std::string &path, const Params ¶ms) { return cli_->Put(path, params); } @@ -11793,12 +12019,28 @@ inline Result Client::Patch(const std::string &path, size_t content_length, return cli_->Patch(path, content_length, std::move(content_provider), content_type, progress); } +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} inline Result Client::Patch(const std::string &path, ContentProviderWithoutLength content_provider, const std::string &content_type, UploadProgress progress) { return cli_->Patch(path, std::move(content_provider), content_type, progress); } +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} inline Result Client::Patch(const std::string &path, const Headers &headers, size_t content_length, ContentProvider content_provider, @@ -11807,6 +12049,15 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, return cli_->Patch(path, headers, content_length, std::move(content_provider), content_type, progress); } +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} inline Result Client::Patch(const std::string &path, const Headers &headers, ContentProviderWithoutLength content_provider, const std::string &content_type, @@ -11814,6 +12065,14 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, return cli_->Patch(path, headers, std::move(content_provider), content_type, progress); } +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, headers, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} inline Result Client::Patch(const std::string &path, const Params ¶ms) { return cli_->Patch(path, params); } diff --git a/test/test.cc b/test/test.cc index b54c0f0afb..6944c7be5a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3423,6 +3423,39 @@ class ServerTest : public ::testing::Test { EXPECT_EQ(req.body, LARGE_DATA); res.set_content(req.body, "text/plain"); }) + .Post("/post-loopback", + [&](const Request &req, Response &res, + ContentReader const &content_reader) { + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + res.set_content(body, "text/plain"); + }) + .Put("/put-loopback", + [&](const Request &req, Response &res, + ContentReader const &content_reader) { + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + res.set_content(body, "text/plain"); + }) + .Patch("/patch-loopback", + [&](const Request &req, Response &res, + ContentReader const &content_reader) { + std::string body; + content_reader([&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + res.set_content(body, "text/plain"); + }) .Put("/empty-no-content-type", [&](const Request &req, Response &res) { EXPECT_EQ(req.body, ""); @@ -5183,6 +5216,94 @@ TEST_F(ServerTest, PostWithContentProviderWithoutLengthAbort) { EXPECT_EQ(Error::Canceled, res.error()); } +TEST_F(ServerTest, PostLoopBack) { + std::string body; + auto res = cli_.Post( + "/post-loopback", 9, + [](size_t /*offset*/, size_t length, DataSink &sink) { + EXPECT_EQ(9u, length); + sink.write("123", 3); + sink.write("456", 3); + sink.write("789", 3); + return true; + }, + "text/plain", + [&body](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("123456789", body); +} + +TEST_F(ServerTest, PutLoopBack) { + std::string body; + auto res = cli_.Put( + "/put-loopback", 9, + [](size_t /*offset*/, size_t length, DataSink &sink) { + EXPECT_EQ(9u, length); + sink.write("123", 3); + sink.write("456", 3); + sink.write("789", 3); + return true; + }, + "text/plain", + [&body](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("123456789", body); +} + +TEST_F(ServerTest, PatchLoopBack) { + std::string body; + auto res = cli_.Patch( + "/patch-loopback", 9, + [](size_t /*offset*/, size_t length, DataSink &sink) { + EXPECT_EQ(9u, length); + sink.write("123", 3); + sink.write("456", 3); + sink.write("789", 3); + return true; + }, + "text/plain", + [&body](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("123456789", body); +} + +TEST_F(ServerTest, PostLoopBackWithoutRequestContentLength) { + std::string body; + auto res = cli_.Post( + "/post-loopback", + [](size_t /*offset*/, DataSink &sink) { + sink.write("123", 3); + sink.write("456", 3); + sink.write("789", 3); + sink.done(); + return true; + }, + "text/plain", + [&body](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("123456789", body); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, PutWithContentProviderWithGzip) { cli_.set_compress(true); From 27ee115a60522c16c1e153c4e3de777a8f9b794a Mon Sep 17 00:00:00 2001 From: Clarkok Zhang Date: Fri, 14 Nov 2025 13:48:21 -0800 Subject: [PATCH 0536/1222] Fix android getaddrinfo issue (#2273) Co-authored-by: Clarkok Zhang --- httplib.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 8ed840efd7..7b07522005 100644 --- a/httplib.h +++ b/httplib.h @@ -3835,22 +3835,30 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, // Fallback implementation using thread-based timeout for other Unix systems struct GetAddrInfoState { + ~GetAddrInfoState() { + if (info) { freeaddrinfo(info); } + } + std::mutex mutex; std::condition_variable result_cv; bool completed = false; int result = EAI_SYSTEM; - std::string node = node; - std::string service = service; - struct addrinfo hints = hints; + std::string node; + std::string service; + struct addrinfo hints; struct addrinfo *info = nullptr; }; // Allocate on the heap, so the resolver thread can keep using the data. auto state = std::make_shared(); + state->node = node; + state->service = service; + state->hints = *hints; - std::thread resolve_thread([=]() { - auto thread_result = getaddrinfo( - state->node.c_str(), state->service.c_str(), hints, &state->info); + std::thread resolve_thread([state]() { + auto thread_result = + getaddrinfo(state->node.c_str(), state->service.c_str(), &state->hints, + &state->info); std::lock_guard lock(state->mutex); state->result = thread_result; @@ -3868,6 +3876,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, // Operation completed within timeout resolve_thread.join(); *res = state->info; + state->info = nullptr; // Pass ownership to caller return state->result; } else { // Timeout occurred From 9e7861b0b48299fb570e74f8d43fd5d76e7999fe Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Tue, 25 Nov 2025 20:00:10 -0500 Subject: [PATCH 0537/1222] Add #undef _res after including resolv.h to prevent macro conflicts (#2280) * Initial plan * Add #undef _res after including resolv.h to prevent macro conflicts Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com> * Complete task - added #undef _res after resolv.h include Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com> * Remove accidentally committed codeql build artifacts Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com> * Add inline comment explaining why #undef _res is necessary Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: yhirose <357397+yhirose@users.noreply.github.com> --- httplib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/httplib.h b/httplib.h index 7b07522005..1049cc2927 100644 --- a/httplib.h +++ b/httplib.h @@ -257,6 +257,7 @@ using socklen_t = int; #include #ifdef __linux__ #include +#undef _res // Undefine _res macro to avoid conflicts with user code (#2278) #endif #include #include From 337fbb0793c598046d636d4450a5dcf8d3620a0b Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 25 Nov 2025 20:30:43 -0500 Subject: [PATCH 0538/1222] Fix #2279 Enhance request handling: add support for requests without Content-Length or Transfer-Encoding headers --- httplib.h | 6 ++++- test/test.cc | 76 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 1049cc2927..c9506cea07 100644 --- a/httplib.h +++ b/httplib.h @@ -7932,7 +7932,11 @@ inline bool Server::read_content_core( size_t /*len*/) { return receiver(buf, n); }; } - if (req.method == "DELETE" && !req.has_header("Content-Length")) { + // RFC 7230 Section 3.3.3: If this is a request message and none of the above + // are true (no Transfer-Encoding and no Content-Length), then the message + // body length is zero (no message body is present). + if (!req.has_header("Content-Length") && + !detail::is_chunked_transfer_encoding(req.headers)) { return true; } diff --git a/test/test.cc b/test/test.cc index 6944c7be5a..72e0436073 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3424,7 +3424,7 @@ class ServerTest : public ::testing::Test { res.set_content(req.body, "text/plain"); }) .Post("/post-loopback", - [&](const Request &req, Response &res, + [&](const Request &, Response &res, ContentReader const &content_reader) { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -3435,7 +3435,7 @@ class ServerTest : public ::testing::Test { res.set_content(body, "text/plain"); }) .Put("/put-loopback", - [&](const Request &req, Response &res, + [&](const Request &, Response &res, ContentReader const &content_reader) { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -3446,7 +3446,7 @@ class ServerTest : public ::testing::Test { res.set_content(body, "text/plain"); }) .Patch("/patch-loopback", - [&](const Request &req, Response &res, + [&](const Request &, Response &res, ContentReader const &content_reader) { std::string body; content_reader([&](const char *data, size_t data_length) { @@ -11609,3 +11609,73 @@ TEST(ForwardedHeadersTest, HandlesWhitespaceAroundIPs) { EXPECT_EQ(observed_xff, "198.51.100.23 , 203.0.113.66 , 192.0.2.45"); EXPECT_EQ(observed_remote_addr, "203.0.113.66"); } + +TEST(ServerRequestParsingTest, RequestWithoutContentLengthOrTransferEncoding) { + Server svr; + + svr.Post("/post", [&](const Request &req, Response &res) { + res.set_content(req.body, "text/plain"); + }); + + svr.Put("/put", [&](const Request &req, Response &res) { + res.set_content(req.body, "text/plain"); + }); + + svr.Patch("/patch", [&](const Request &req, Response &res) { + res.set_content(req.body, "text/plain"); + }); + + svr.Delete("/delete", [&](const Request &req, Response &res) { + res.set_content(req.body, "text/plain"); + }); + + thread t = thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + std::string resp; + + // POST without Content-Length + ASSERT_TRUE(send_request(5, + "POST /post HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n", + &resp)); + EXPECT_TRUE(resp.find("HTTP/1.1 200 OK") == 0); + + // PUT without Content-Length + resp.clear(); + ASSERT_TRUE(send_request(5, + "PUT /put HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n", + &resp)); + EXPECT_TRUE(resp.find("HTTP/1.1 200 OK") == 0); + + // PATCH without Content-Length + resp.clear(); + ASSERT_TRUE(send_request(5, + "PATCH /patch HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n", + &resp)); + EXPECT_TRUE(resp.find("HTTP/1.1 200 OK") == 0); + + // DELETE without Content-Length + resp.clear(); + ASSERT_TRUE(send_request(5, + "DELETE /delete HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n", + &resp)); + EXPECT_TRUE(resp.find("HTTP/1.1 200 OK") == 0); +} From adf58bf474fac638160592d6c3f67da4ebc7df20 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 25 Nov 2025 21:06:51 -0500 Subject: [PATCH 0539/1222] Release v0.28.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index c9506cea07..ed30e557a2 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.27.0" -#define CPPHTTPLIB_VERSION_NUM "0x001B00" +#define CPPHTTPLIB_VERSION "0.28.0" +#define CPPHTTPLIB_VERSION_NUM "0x001C00" /* * Platform compatibility check From 84796738fc5d1297b80c1c10795a19147104ff90 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 25 Nov 2025 22:22:07 -0500 Subject: [PATCH 0540/1222] Fix #2248 --- test/test.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 72e0436073..76713ad0cf 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7618,7 +7618,14 @@ TEST(SNI_AutoDetectionTest, SNI_Logic) { SSLClient cli("::1", PORT); cli.enable_server_certificate_verification(false); auto res = cli.Get("/sni?expected="); - ASSERT_TRUE(res); + + // NOTE: This may fail if the server is listening on IPv4 only + // (e.g., when localhost resolves to 127.0.0.1 only) + if (res) { + EXPECT_EQ(StatusCode::OK_200, res->status); + } else { + EXPECT_EQ(Error::Connection, res.error()); + } } } } From c1fa5e1710d09d17d3a72b8b04110d30af93ec56 Mon Sep 17 00:00:00 2001 From: bigmoonbit Date: Fri, 28 Nov 2025 09:34:18 +0800 Subject: [PATCH 0541/1222] chore: fix some typos in comments (#2282) Signed-off-by: bigmoonbit --- test/gtest/include/gtest/gtest-printers.h | 2 +- test/gtest/include/gtest/internal/gtest-param-util.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/gtest/include/gtest/gtest-printers.h b/test/gtest/include/gtest/gtest-printers.h index a91e8b8b10..cec28596f7 100644 --- a/test/gtest/include/gtest/gtest-printers.h +++ b/test/gtest/include/gtest/gtest-printers.h @@ -993,7 +993,7 @@ void UniversalTersePrint(const T& value, ::std::ostream* os) { // NUL-terminated string. template void UniversalPrint(const T& value, ::std::ostream* os) { - // A workarond for the bug in VC++ 7.1 that prevents us from instantiating + // A workaround for the bug in VC++ 7.1 that prevents us from instantiating // UniversalPrinter with T directly. typedef T T1; UniversalPrinter::Print(value, os); diff --git a/test/gtest/include/gtest/internal/gtest-param-util.h b/test/gtest/include/gtest/internal/gtest-param-util.h index e7af2f904a..ffa0d4f51b 100644 --- a/test/gtest/include/gtest/internal/gtest-param-util.h +++ b/test/gtest/include/gtest/internal/gtest-param-util.h @@ -753,7 +753,7 @@ class ParameterizedTestSuiteRegistry { }; // Keep track of what type-parameterized test suite are defined and -// where as well as which are intatiated. This allows susequently +// where as well as which are intatiated. This allows subsequently // identifying suits that are defined but never used. class TypeParameterizedTestSuiteRegistry { public: @@ -764,7 +764,7 @@ class TypeParameterizedTestSuiteRegistry { // Add an instantiation of a suit. void RegisterInstantiation(const char* test_suite_name); - // For each suit repored as defined but not reported as instantiation, + // For each suit reported as defined but not reported as instantiation, // emit a test that reports that fact (configurably, as an error). void CheckForInstantiations(); From 0a9102ff6b44f4b6382999b0141f2be69267a057 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sun, 30 Nov 2025 18:49:23 -0800 Subject: [PATCH 0542/1222] Optimize ThreadPool and MatcherBase constructors (#2283) Add a missing reserve and missing std::move to each ctor respectively. The latter should really be caught by a clang-tidy perf linter. --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ed30e557a2..a235fd8ed4 100644 --- a/httplib.h +++ b/httplib.h @@ -873,6 +873,7 @@ class ThreadPool final : public TaskQueue { public: explicit ThreadPool(size_t n, size_t mqr = 0) : shutdown_(false), max_queued_requests_(mqr) { + threads_.reserve(n); while (n) { threads_.emplace_back(worker(*this)); n--; @@ -981,7 +982,7 @@ namespace detail { class MatcherBase { public: - MatcherBase(std::string pattern) : pattern_(pattern) {} + MatcherBase(std::string pattern) : pattern_(std::move(pattern)) {} virtual ~MatcherBase() = default; const std::string &pattern() const { return pattern_; } From 8bba34eebc384cc17989e0b018345962bfee299f Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 1 Dec 2025 21:40:13 -0500 Subject: [PATCH 0543/1222] Add New Streaming API support (#2281) --- .gitignore | 4 + README-stream.md | 315 +++++++++ README.md | 25 + example/Makefile | 7 +- example/ssecli-stream.cc | 234 +++++++ httplib.h | 1439 +++++++++++++++++++++++++++++++------- test/test.cc | 778 +++++++++++++++++++++ 7 files changed, 2563 insertions(+), 239 deletions(-) create mode 100644 README-stream.md create mode 100644 example/ssecli-stream.cc diff --git a/.gitignore b/.gitignore index 21839782df..9e2bf24566 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,11 @@ example/benchmark example/redirect !example/redirect.* example/ssecli +!example/ssecli.* +example/ssecli-stream +!example/ssecli-stream.* example/ssesvr +!example/ssesvr.* example/upload !example/upload.* example/one_time_request diff --git a/README-stream.md b/README-stream.md new file mode 100644 index 0000000000..ce02e1c705 --- /dev/null +++ b/README-stream.md @@ -0,0 +1,315 @@ +# cpp-httplib Streaming API + +This document describes the streaming extensions for cpp-httplib, providing an iterator-style API for handling HTTP responses incrementally with **true socket-level streaming**. + +> **Important Notes**: +> +> - **No Keep-Alive**: Each `stream::Get()` call uses a dedicated connection that is closed after the response is fully read. For connection reuse, use `Client::Get()`. +> - **Single iteration only**: The `next()` method can only iterate through the body once. +> - **Result is not thread-safe**: While `stream::Get()` can be called from multiple threads simultaneously, the returned `stream::Result` must be used from a single thread only. + +## Overview + +The streaming API allows you to process HTTP response bodies chunk by chunk using an iterator-style pattern. Data is read directly from the network socket, enabling low-memory processing of large responses. This is particularly useful for: + +- **LLM/AI streaming responses** (e.g., ChatGPT, Claude, Ollama) +- **Server-Sent Events (SSE)** +- **Large file downloads** with progress tracking +- **Reverse proxy implementations** + +## Quick Start + +```cpp +#include "httplib.h" + +int main() { + httplib::Client cli("http://localhost:8080"); + + // Get streaming response + auto result = httplib::stream::Get(cli, "/stream"); + + if (result) { + // Process response body in chunks + while (result.next()) { + std::cout.write(result.data(), result.size()); + } + } + + return 0; +} +``` + +## API Layers + +cpp-httplib provides multiple API layers for different use cases: + +```text +┌─────────────────────────────────────────────┐ +│ SSEClient (planned) │ ← SSE-specific, parsed events +│ - on_message(), on_event() │ +│ - Auto-reconnect, Last-Event-ID │ +├─────────────────────────────────────────────┤ +│ stream::Get() / stream::Result │ ← Iterator-based streaming +│ - while (result.next()) { ... } │ +├─────────────────────────────────────────────┤ +│ open_stream() / StreamHandle │ ← General-purpose streaming +│ - handle.read(buf, len) │ +├─────────────────────────────────────────────┤ +│ Client::Get() │ ← Traditional, full buffering +└─────────────────────────────────────────────┘ +``` + +| Use Case | Recommended API | +|----------|----------------| +| SSE with auto-reconnect | SSEClient (planned) or `ssecli-stream.cc` example | +| LLM streaming (JSON Lines) | `stream::Get()` | +| Large file download | `stream::Get()` or `open_stream()` | +| Reverse proxy | `open_stream()` | +| Small responses with Keep-Alive | `Client::Get()` | + +## API Reference + +### Low-Level API: `StreamHandle` + +The `StreamHandle` struct provides direct control over streaming responses. It takes ownership of the socket connection and reads data directly from the network. + +> **Note:** When using `open_stream()`, the connection is dedicated to streaming and **Keep-Alive is not supported**. For Keep-Alive connections, use `client.Get()` instead. + +```cpp +// Open a stream (takes ownership of socket) +httplib::Client cli("http://localhost:8080"); +auto handle = cli.open_stream("/path"); + +// Check validity +if (handle.is_valid()) { + // Access response headers immediately + int status = handle.response->status; + auto content_type = handle.response->get_header_value("Content-Type"); + + // Read body incrementally + char buf[4096]; + ssize_t n; + while ((n = handle.read(buf, sizeof(buf))) > 0) { + process(buf, n); + } +} +``` + +#### StreamHandle Members + +| Member | Type | Description | +|--------|------|-------------| +| `response` | `std::unique_ptr` | HTTP response with headers | +| `error` | `Error` | Error code if request failed | +| `is_valid()` | `bool` | Returns true if response is valid | +| `read(buf, len)` | `ssize_t` | Read up to `len` bytes directly from socket | +| `get_read_error()` | `Error` | Get the last read error | +| `has_read_error()` | `bool` | Check if a read error occurred | + +### High-Level API: `stream::Get()` and `stream::Result` + +The `httplib.h` header provides a more ergonomic iterator-style API. + +```cpp +#include "httplib.h" + +httplib::Client cli("http://localhost:8080"); + +// Simple GET +auto result = httplib::stream::Get(cli, "/path"); + +// GET with custom headers +httplib::Headers headers = {{"Authorization", "Bearer token"}}; +auto result = httplib::stream::Get(cli, "/path", headers); + +// Process the response +if (result) { + while (result.next()) { + process(result.data(), result.size()); + } +} + +// Or read entire body at once +auto result2 = httplib::stream::Get(cli, "/path"); +if (result2) { + std::string body = result2.read_all(); +} +``` + +#### stream::Result Members + +| Member | Type | Description | +|--------|------|-------------| +| `operator bool()` | `bool` | Returns true if response is valid | +| `is_valid()` | `bool` | Same as `operator bool()` | +| `status()` | `int` | HTTP status code | +| `headers()` | `const Headers&` | Response headers | +| `get_header_value(key, def)` | `std::string` | Get header value (with optional default) | +| `has_header(key)` | `bool` | Check if header exists | +| `next()` | `bool` | Read next chunk, returns false when done | +| `data()` | `const char*` | Pointer to current chunk data | +| `size()` | `size_t` | Size of current chunk | +| `read_all()` | `std::string` | Read entire remaining body into string | +| `error()` | `Error` | Get the connection/request error | +| `read_error()` | `Error` | Get the last read error | +| `has_read_error()` | `bool` | Check if a read error occurred | + +## Usage Examples + +### Example 1: SSE (Server-Sent Events) Client + +```cpp +#include "httplib.h" +#include + +int main() { + httplib::Client cli("http://localhost:1234"); + + auto result = httplib::stream::Get(cli, "/events"); + if (!result) { return 1; } + + while (result.next()) { + std::cout.write(result.data(), result.size()); + std::cout.flush(); + } + + return 0; +} +``` + +For a complete SSE client with auto-reconnection and event parsing, see `example/ssecli-stream.cc`. + +### Example 2: LLM Streaming Response + +```cpp +#include "httplib.h" +#include + +int main() { + httplib::Client cli("http://localhost:11434"); // Ollama + + auto result = httplib::stream::Get(cli, "/api/generate"); + + if (result && result.status() == 200) { + while (result.next()) { + std::cout.write(result.data(), result.size()); + std::cout.flush(); + } + } + + // Check for connection errors + if (result.read_error() != httplib::Error::Success) { + std::cerr << "Connection lost\n"; + } + + return 0; +} +``` + +### Example 3: Large File Download with Progress + +```cpp +#include "httplib.h" +#include +#include + +int main() { + httplib::Client cli("http://example.com"); + auto result = httplib::stream::Get(cli, "/large-file.zip"); + + if (!result || result.status() != 200) { + std::cerr << "Download failed\n"; + return 1; + } + + std::ofstream file("download.zip", std::ios::binary); + size_t total = 0; + + while (result.next()) { + file.write(result.data(), result.size()); + total += result.size(); + std::cout << "\rDownloaded: " << (total / 1024) << " KB" << std::flush; + } + + std::cout << "\nComplete!\n"; + return 0; +} +``` + +### Example 4: Reverse Proxy Streaming + +```cpp +#include "httplib.h" + +httplib::Server svr; + +svr.Get("/proxy/(.*)", [](const httplib::Request& req, httplib::Response& res) { + httplib::Client upstream("http://backend:8080"); + auto handle = upstream.open_stream("/" + req.matches[1].str()); + + if (!handle.is_valid()) { + res.status = 502; + return; + } + + res.status = handle.response->status; + res.set_chunked_content_provider( + handle.response->get_header_value("Content-Type"), + [handle = std::move(handle)](size_t, httplib::DataSink& sink) mutable { + char buf[8192]; + auto n = handle.read(buf, sizeof(buf)); + if (n > 0) { + sink.write(buf, static_cast(n)); + return true; + } + sink.done(); + return true; + } + ); +}); + +svr.listen("0.0.0.0", 3000); +``` + +## Comparison with Existing APIs + +| Feature | `Client::Get()` | `open_stream()` | `stream::Get()` | +|---------|----------------|-----------------|----------------| +| Headers available | After complete | Immediately | Immediately | +| Body reading | All at once | Direct from socket | Iterator-based | +| Memory usage | Full body in RAM | Minimal (controlled) | Minimal (controlled) | +| Keep-Alive support | ✅ Yes | ❌ No | ❌ No | +| Compression | Auto-handled | Auto-handled | Auto-handled | +| Best for | Small responses, Keep-Alive | Low-level streaming | Easy streaming | + +## Features + +- **True socket-level streaming**: Data is read directly from the network socket +- **Low memory footprint**: Only the current chunk is held in memory +- **Compression support**: Automatic decompression for gzip, brotli, and zstd +- **Chunked transfer**: Full support for chunked transfer encoding +- **SSL/TLS support**: Works with HTTPS connections + +## Important Notes + +### Keep-Alive Behavior + +The streaming API (`stream::Get()` / `open_stream()`) takes ownership of the socket connection for the duration of the stream. This means: + +- **Keep-Alive is not supported** for streaming connections +- The socket is closed when `StreamHandle` is destroyed +- For Keep-Alive scenarios, use the standard `client.Get()` API instead + +```cpp +// Use for streaming (no Keep-Alive) +auto result = httplib::stream::Get(cli, "/large-stream"); +while (result.next()) { /* ... */ } + +// Use for Keep-Alive connections +auto res = cli.Get("/api/data"); // Connection can be reused +``` + +## Related + +- [Issue #2269](https://github.com/yhirose/cpp-httplib/issues/2269) - Original feature request +- [example/ssecli-stream.cc](./example/ssecli-stream.cc) - SSE client with auto-reconnection diff --git a/README.md b/README.md index edecbb6a42..01eca3ea39 100644 --- a/README.md +++ b/README.md @@ -1188,6 +1188,31 @@ std::string decoded_component = httplib::decode_uri_component(encoded_component) Use `encode_uri()` for full URLs and `encode_uri_component()` for individual query parameters or path segments. +Streaming API +------------- + +Process large responses without loading everything into memory. + +```c++ +httplib::Client cli("localhost", 8080); + +auto result = httplib::stream::Get(cli, "/large-file"); +if (result) { + while (result.next()) { + process(result.data(), result.size()); // Process each chunk as it arrives + } +} + +// Or read the entire body at once +auto result2 = httplib::stream::Get(cli, "/file"); +if (result2) { + std::string body = result2.read_all(); +} +``` + +All HTTP methods are supported: `stream::Get`, `Post`, `Put`, `Patch`, `Delete`, `Head`, `Options`. + +See [README-stream.md](README-stream.md) for more details. Split httplib.h into .h and .cc ------------------------------- diff --git a/example/Makefile b/example/Makefile index 3082b88014..0d3c46d080 100644 --- a/example/Makefile +++ b/example/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli ssecli-stream benchmark one_time_request server_and_client accept_header server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -47,6 +47,9 @@ ssesvr : ssesvr.cc ../httplib.h Makefile ssecli : ssecli.cc ../httplib.h Makefile $(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) +ssecli-stream : ssecli-stream.cc ../httplib.h ../httplib.h Makefile + $(CXX) -o ssecli-stream $(CXXFLAGS) ssecli-stream.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) + benchmark : benchmark.cc ../httplib.h Makefile $(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -64,4 +67,4 @@ pem: openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr ssecli ssecli-stream benchmark one_time_request server_and_client accept_header *.pem diff --git a/example/ssecli-stream.cc b/example/ssecli-stream.cc new file mode 100644 index 0000000000..0dd279ad37 --- /dev/null +++ b/example/ssecli-stream.cc @@ -0,0 +1,234 @@ +// +// ssecli-stream.cc +// +// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// MIT License +// +// SSE (Server-Sent Events) client example using Streaming API +// with automatic reconnection support (similar to JavaScript's EventSource) +// + +#include + +#include +#include +#include +#include + +//------------------------------------------------------------------------------ +// SSE Event Parser +//------------------------------------------------------------------------------ +// Parses SSE events from the stream according to the SSE specification. +// SSE format: +// event: (optional, defaults to "message") +// data: (can have multiple lines) +// id: (optional, used for reconnection) +// retry: (optional, reconnection interval) +// (signals end of event) +// +struct SSEEvent { + std::string event = "message"; // Event type (default: "message") + std::string data; // Event payload + std::string id; // Event ID for Last-Event-ID header + + void clear() { + event = "message"; + data.clear(); + id.clear(); + } +}; + +// Parse a single SSE field line (e.g., "data: hello") +// Returns true if this line ends an event (blank line) +bool parse_sse_line(const std::string &line, SSEEvent &event, int &retry_ms) { + // Blank line signals end of event + if (line.empty() || line == "\r") { return true; } + + // Find the colon separator + auto colon_pos = line.find(':'); + if (colon_pos == std::string::npos) { + // Line with no colon is treated as field name with empty value + return false; + } + + std::string field = line.substr(0, colon_pos); + std::string value; + + // Value starts after colon, skip optional single space + if (colon_pos + 1 < line.size()) { + size_t value_start = colon_pos + 1; + if (line[value_start] == ' ') { value_start++; } + value = line.substr(value_start); + // Remove trailing \r if present + if (!value.empty() && value.back() == '\r') { value.pop_back(); } + } + + // Handle known fields + if (field == "event") { + event.event = value; + } else if (field == "data") { + // Multiple data lines are concatenated with newlines + if (!event.data.empty()) { event.data += "\n"; } + event.data += value; + } else if (field == "id") { + // Empty id is valid (clears the last event ID) + event.id = value; + } else if (field == "retry") { + // Parse retry interval in milliseconds + try { + retry_ms = std::stoi(value); + } catch (...) { + // Invalid retry value, ignore + } + } + // Unknown fields are ignored per SSE spec + + return false; +} + +//------------------------------------------------------------------------------ +// Main - SSE Client with Auto-Reconnection +//------------------------------------------------------------------------------ +int main(void) { + // Configuration + const std::string host = "http://localhost:1234"; + const std::string path = "/event1"; + + httplib::Client cli(host); + + // State for reconnection (persists across connections) + std::string last_event_id; // Sent as Last-Event-ID header on reconnect + int retry_ms = 3000; // Reconnection delay (server can override via retry:) + int connection_count = 0; + + std::cout << "SSE Client starting...\n"; + std::cout << "Target: " << host << path << "\n"; + std::cout << "Press Ctrl+C to exit\n\n"; + + //---------------------------------------------------------------------------- + // Main reconnection loop + // This mimics JavaScript's EventSource behavior: + // - Automatically reconnects on connection failure + // - Sends Last-Event-ID header to resume from last received event + // - Respects server's retry interval + //---------------------------------------------------------------------------- + while (true) { + connection_count++; + std::cout << "[Connection #" << connection_count << "] Connecting...\n"; + + // Build headers, including Last-Event-ID if we have one + httplib::Headers headers; + if (!last_event_id.empty()) { + headers.emplace("Last-Event-ID", last_event_id); + std::cout << "[Connection #" << connection_count + << "] Resuming from event ID: " << last_event_id << "\n"; + } + + // Open streaming connection + auto result = httplib::stream::Get(cli, path, headers); + + //-------------------------------------------------------------------------- + // Connection error handling + //-------------------------------------------------------------------------- + if (!result) { + std::cerr << "[Connection #" << connection_count + << "] Failed: " << httplib::to_string(result.error()) << "\n"; + std::cerr << "Reconnecting in " << retry_ms << "ms...\n\n"; + std::this_thread::sleep_for(std::chrono::milliseconds(retry_ms)); + continue; + } + + if (result.status() != 200) { + std::cerr << "[Connection #" << connection_count + << "] HTTP error: " << result.status() << "\n"; + + // For certain errors, don't reconnect + if (result.status() == 204 || // No Content - server wants us to stop + result.status() == 404 || // Not Found + result.status() == 401 || // Unauthorized + result.status() == 403) { // Forbidden + std::cerr << "Permanent error, not reconnecting.\n"; + return 1; + } + + std::cerr << "Reconnecting in " << retry_ms << "ms...\n\n"; + std::this_thread::sleep_for(std::chrono::milliseconds(retry_ms)); + continue; + } + + // Verify Content-Type (optional but recommended) + auto content_type = result.get_header_value("Content-Type"); + if (content_type.find("text/event-stream") == std::string::npos) { + std::cerr << "[Warning] Content-Type is not text/event-stream: " + << content_type << "\n"; + } + + std::cout << "[Connection #" << connection_count << "] Connected!\n\n"; + + //-------------------------------------------------------------------------- + // Event receiving loop + // Reads chunks from the stream and parses SSE events + //-------------------------------------------------------------------------- + std::string buffer; + SSEEvent current_event; + int event_count = 0; + + // Read data from stream using httplib::stream API + while (result.next()) { + buffer.append(result.data(), result.size()); + + // Process complete lines in the buffer + size_t line_start = 0; + size_t newline_pos; + + while ((newline_pos = buffer.find('\n', line_start)) != + std::string::npos) { + std::string line = buffer.substr(line_start, newline_pos - line_start); + line_start = newline_pos + 1; + + // Parse the line and check if event is complete + bool event_complete = parse_sse_line(line, current_event, retry_ms); + + if (event_complete && !current_event.data.empty()) { + // Event received - process it + event_count++; + + std::cout << "--- Event #" << event_count << " ---\n"; + std::cout << "Type: " << current_event.event << "\n"; + std::cout << "Data: " << current_event.data << "\n"; + if (!current_event.id.empty()) { + std::cout << "ID: " << current_event.id << "\n"; + } + std::cout << "\n"; + + // Update last_event_id for reconnection + // Note: Empty id clears the last event ID per SSE spec + if (!current_event.id.empty()) { last_event_id = current_event.id; } + + current_event.clear(); + } + } + + // Keep unprocessed data in buffer + buffer.erase(0, line_start); + } + + //-------------------------------------------------------------------------- + // Connection ended - check why + //-------------------------------------------------------------------------- + if (result.read_error() != httplib::Error::Success) { + std::cerr << "\n[Connection #" << connection_count + << "] Error: " << httplib::to_string(result.read_error()) + << "\n"; + } else { + std::cout << "\n[Connection #" << connection_count + << "] Stream ended normally\n"; + } + + std::cout << "Received " << event_count << " events in this connection\n"; + std::cout << "Reconnecting in " << retry_ms << "ms...\n\n"; + std::this_thread::sleep_for(std::chrono::milliseconds(retry_ms)); + } + + return 0; +} diff --git a/httplib.h b/httplib.h index a235fd8ed4..f63f76cc0e 100644 --- a/httplib.h +++ b/httplib.h @@ -969,6 +969,7 @@ bool set_socket_opt_impl(socket_t sock, int level, int optname, bool set_socket_opt(socket_t sock, int level, int optname, int opt); bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, time_t usec); +int close_socket(socket_t sock); } // namespace detail @@ -1391,6 +1392,87 @@ class Result { #endif }; +struct ClientConnection { + socket_t sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSL *ssl = nullptr; +#endif + + bool is_open() const { return sock != INVALID_SOCKET; } + + ClientConnection() = default; + + ~ClientConnection() { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (ssl) { + SSL_free(ssl); + ssl = nullptr; + } +#endif + if (sock != INVALID_SOCKET) { + detail::close_socket(sock); + sock = INVALID_SOCKET; + } + } + + ClientConnection(const ClientConnection &) = delete; + ClientConnection &operator=(const ClientConnection &) = delete; + + ClientConnection(ClientConnection &&other) noexcept + : sock(other.sock) +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + , + ssl(other.ssl) +#endif + { + other.sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + other.ssl = nullptr; +#endif + } + + ClientConnection &operator=(ClientConnection &&other) noexcept { + if (this != &other) { + sock = other.sock; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + ssl = other.ssl; +#endif + other.sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + other.ssl = nullptr; +#endif + } + return *this; + } +}; + +namespace detail { + +struct ChunkedDecoder; + +struct BodyReader { + Stream *stream = nullptr; + size_t content_length = 0; + size_t bytes_read = 0; + bool chunked = false; + bool eof = false; + std::unique_ptr chunked_decoder; + Error last_error = Error::Success; + + ssize_t read(char *buf, size_t len); + bool has_error() const { return last_error != Error::Success; } +}; + +inline ssize_t read_body_content(Stream *stream, BodyReader &br, char *buf, + size_t len) { + (void)stream; + return br.read(buf, len); +} + +class decompressor; + +} // namespace detail + class ClientImpl { public: explicit ClientImpl(const std::string &host); @@ -1405,6 +1487,43 @@ class ClientImpl { virtual bool is_valid() const; + struct StreamHandle { + std::unique_ptr response; + Error error = Error::Success; + + StreamHandle() = default; + StreamHandle(const StreamHandle &) = delete; + StreamHandle &operator=(const StreamHandle &) = delete; + StreamHandle(StreamHandle &&) = default; + StreamHandle &operator=(StreamHandle &&) = default; + ~StreamHandle() = default; + + bool is_valid() const { + return response != nullptr && error == Error::Success; + } + + ssize_t read(char *buf, size_t len); + void parse_trailers_if_needed(); + Error get_read_error() const { return body_reader_.last_error; } + bool has_read_error() const { return body_reader_.has_error(); } + + bool trailers_parsed_ = false; + + private: + friend class ClientImpl; + + ssize_t read_with_decompression(char *buf, size_t len); + + std::unique_ptr connection_; + std::unique_ptr socket_stream_; + Stream *stream_ = nullptr; + detail::BodyReader body_reader_; + + std::unique_ptr decompressor_; + std::string decompress_buffer_; + size_t decompress_offset_ = 0; + }; + // clang-format off Result Get(const std::string &path, DownloadProgress progress = nullptr); Result Get(const std::string &path, ContentReceiver content_receiver, DownloadProgress progress = nullptr); @@ -1498,6 +1617,15 @@ class ClientImpl { Result Options(const std::string &path, const Headers &headers); // clang-format on + // Streaming API: Open a stream for reading response body incrementally + // Socket ownership is transferred to StreamHandle for true streaming + // Supports all HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.) + StreamHandle open_stream(const std::string &method, const std::string &path, + const Params ¶ms = {}, + const Headers &headers = {}, + const std::string &body = {}, + const std::string &content_type = {}); + bool send(Request &req, Response &res, Error &error); Result send(const Request &req); @@ -1593,6 +1721,7 @@ class ClientImpl { }; virtual bool create_and_connect_socket(Socket &socket, Error &error); + virtual bool ensure_socket_connection(Socket &socket, Error &error); // All of: // shutdown_ssl @@ -1718,6 +1847,8 @@ class ClientImpl { Response &res) const; bool write_request(Stream &strm, Request &req, bool close_connection, Error &error); + void prepare_default_headers(Request &r, bool for_stream, + const std::string &ct); bool redirect(Request &req, Response &res, Error &error); bool create_redirect_client(const std::string &scheme, const std::string &host, int port, Request &req, @@ -1748,6 +1879,8 @@ class ClientImpl { std::chrono::time_point start_time, std::function callback); virtual bool is_ssl() const; + + void transfer_socket_ownership_to_handle(StreamHandle &handle); }; class Client { @@ -1866,6 +1999,16 @@ class Client { Result Options(const std::string &path, const Headers &headers); // clang-format on + // Streaming API: Open a stream for reading response body incrementally + // Socket ownership is transferred to StreamHandle for true streaming + // Supports all HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.) + ClientImpl::StreamHandle open_stream(const std::string &method, + const std::string &path, + const Params ¶ms = {}, + const Headers &headers = {}, + const std::string &body = {}, + const std::string &content_type = {}); + bool send(Request &req, Response &res, Error &error); Result send(const Request &req); @@ -2028,6 +2171,7 @@ class SSLClient final : public ClientImpl { private: bool create_and_connect_socket(Socket &socket, Error &error) override; + bool ensure_socket_connection(Socket &socket, Error &error) override; void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; void shutdown_ssl_impl(Socket &socket, bool shutdown_gracefully); @@ -2670,6 +2814,25 @@ class stream_line_reader { std::string growable_buffer_; }; +bool parse_trailers(stream_line_reader &line_reader, Headers &dest, + const Headers &src_headers); + +struct ChunkedDecoder { + Stream &strm; + size_t chunk_remaining = 0; + bool finished = false; + char line_buf[64]; + size_t last_chunk_total = 0; + size_t last_chunk_offset = 0; + + explicit ChunkedDecoder(Stream &s); + + ssize_t read_payload(char *buf, size_t len, size_t &out_chunk_offset, + size_t &out_chunk_total); + + bool parse_trailers_into(Headers &dest, const Headers &src_headers); +}; + class mmap { public: mmap(const char *path); @@ -2959,6 +3122,149 @@ inline std::string file_extension(const std::string &path) { inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } +template +inline bool parse_header(const char *beg, const char *end, T fn); + +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; + } + + auto p = beg; + while (p < end && *p != ':') { + p++; + } + + auto name = std::string(beg, p); + if (!detail::fields::is_field_name(name)) { return false; } + + if (p == end) { return false; } + + auto key_end = p; + + if (*p++ != ':') { return false; } + + while (p < end && is_space_or_tab(*p)) { + p++; + } + + if (p <= end) { + auto key_len = key_end - beg; + if (!key_len) { return false; } + + auto key = std::string(beg, key_end); + auto val = std::string(p, end); + + if (!detail::fields::is_field_value(val)) { return false; } + + if (case_ignore::equal(key, "Location") || + case_ignore::equal(key, "Referer")) { + fn(key, val); + } else { + fn(key, decode_path_component(val)); + } + + return true; + } + + return false; +} + +inline bool parse_trailers(stream_line_reader &line_reader, Headers &dest, + const Headers &src_headers) { + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked + // transfer coding is complete when a chunk with a chunk-size of zero is + // received, possibly followed by a trailer section, and finally terminated by + // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section + // doesn't care for the existence of the final CRLF. In other words, it seems + // to be ok whether the final CRLF exists or not in the chunked data. + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // + // According to the reference code in RFC 9112, cpp-httplib now allows + // chunked transfer coding data without the final CRLF. + + // RFC 7230 Section 4.1.2 - Headers prohibited in trailers + thread_local case_ignore::unordered_set prohibited_trailers = { + "transfer-encoding", + "content-length", + "host", + "authorization", + "www-authenticate", + "proxy-authenticate", + "proxy-authorization", + "cookie", + "set-cookie", + "cache-control", + "expect", + "max-forwards", + "pragma", + "range", + "te", + "age", + "expires", + "date", + "location", + "retry-after", + "vary", + "warning", + "content-encoding", + "content-type", + "content-range", + "trailer"}; + + case_ignore::unordered_set declared_trailers; + auto trailer_header = get_header_value(src_headers, "Trailer", "", 0); + if (trailer_header && std::strlen(trailer_header)) { + auto len = std::strlen(trailer_header); + split(trailer_header, trailer_header + len, ',', + [&](const char *b, const char *e) { + const char *kbeg = b; + const char *kend = e; + while (kbeg < kend && (*kbeg == ' ' || *kbeg == '\t')) { + ++kbeg; + } + while (kend > kbeg && (kend[-1] == ' ' || kend[-1] == '\t')) { + --kend; + } + std::string key(kbeg, static_cast(kend - kbeg)); + if (!key.empty() && + prohibited_trailers.find(key) == prohibited_trailers.end()) { + declared_trailers.insert(key); + } + }); + } + + size_t trailer_header_count = 0; + while (strcmp(line_reader.ptr(), "\r\n") != 0) { + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } + + constexpr auto line_terminator_len = 2; + auto line_beg = line_reader.ptr(); + auto line_end = + line_reader.ptr() + line_reader.size() - line_terminator_len; + + if (!parse_header(line_beg, line_end, + [&](const std::string &key, const std::string &val) { + if (declared_trailers.find(key) != + declared_trailers.end()) { + dest.emplace(key, val); + trailer_header_count++; + } + })) { + return false; + } + + if (!line_reader.getline()) { return false; } + } + + return true; +} + inline std::pair trim(const char *b, const char *e, size_t left, size_t right) { while (b + left < e && is_space_or_tab(b[left])) { @@ -4381,6 +4687,7 @@ inline EncodingType encoding_type(const Request &req, const Response &res) { return EncodingType::None; } +#ifdef CPPHTTPLIB_ZLIB_SUPPORT inline bool nocompressor::compress(const char *data, size_t data_length, bool /*last*/, Callback callback) { @@ -4388,7 +4695,6 @@ inline bool nocompressor::compress(const char *data, size_t data_length, return callback(data, data_length); } -#ifdef CPPHTTPLIB_ZLIB_SUPPORT inline gzip_compressor::gzip_compressor() { std::memset(&strm_, 0, sizeof(strm_)); strm_.zalloc = Z_NULL; @@ -4642,6 +4948,27 @@ inline bool zstd_decompressor::decompress(const char *data, size_t data_length, } #endif +inline std::unique_ptr +create_decompressor(const std::string &encoding) { + std::unique_ptr decompressor; + + if (encoding == "gzip" || encoding == "deflate") { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + decompressor = detail::make_unique(); +#endif + } else if (encoding.find("br") != std::string::npos) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + decompressor = detail::make_unique(); +#endif + } else if (encoding == "zstd" || encoding.find("zstd") != std::string::npos) { +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + decompressor = detail::make_unique(); +#endif + } + + return decompressor; +} + inline bool is_prohibited_header_name(const std::string &name) { using udl::operator""_t; @@ -4678,53 +5005,6 @@ inline const char *get_header_value(const Headers &headers, return def; } -template -inline bool parse_header(const char *beg, const char *end, T fn) { - // Skip trailing spaces and tabs. - while (beg < end && is_space_or_tab(end[-1])) { - end--; - } - - auto p = beg; - while (p < end && *p != ':') { - p++; - } - - auto name = std::string(beg, p); - if (!detail::fields::is_field_name(name)) { return false; } - - if (p == end) { return false; } - - auto key_end = p; - - if (*p++ != ':') { return false; } - - while (p < end && is_space_or_tab(*p)) { - p++; - } - - if (p <= end) { - auto key_len = key_end - beg; - if (!key_len) { return false; } - - auto key = std::string(beg, key_end); - auto val = std::string(p, end); - - if (!detail::fields::is_field_value(val)) { return false; } - - if (case_ignore::equal(key, "Location") || - case_ignore::equal(key, "Referer")) { - fn(key, val); - } else { - fn(key, decode_path_component(val)); - } - - return true; - } - - return false; -} - inline bool read_headers(Stream &strm, Headers &headers) { const auto bufsiz = 2048; char buf[bufsiz]; @@ -4776,10 +5056,18 @@ inline bool read_content_with_length(Stream &strm, size_t len, ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; + detail::BodyReader br; + br.stream = &strm; + br.content_length = len; + br.chunked = false; + br.bytes_read = 0; + br.last_error = Error::Success; + size_t r = 0; while (r < len) { auto read_len = static_cast(len - r); - auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + auto to_read = (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ); + auto n = detail::read_body_content(&strm, br, buf, to_read); if (n <= 0) { return false; } if (!out(buf, static_cast(n), r, len)) { return false; } @@ -4839,161 +5127,57 @@ template inline ReadContentResult read_content_chunked(Stream &strm, T &x, size_t payload_max_length, ContentReceiverWithProgress out) { - const auto bufsiz = 16; - char buf[bufsiz]; - - stream_line_reader line_reader(strm, buf, bufsiz); + detail::ChunkedDecoder dec(strm); - if (!line_reader.getline()) { return ReadContentResult::Error; } - - unsigned long chunk_len; + char buf[CPPHTTPLIB_RECV_BUFSIZ]; size_t total_len = 0; - while (true) { - char *end_ptr; - - chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); - if (end_ptr == line_reader.ptr()) { return ReadContentResult::Error; } - if (chunk_len == ULONG_MAX) { return ReadContentResult::Error; } + for (;;) { + size_t chunk_offset = 0; + size_t chunk_total = 0; + auto n = dec.read_payload(buf, sizeof(buf), chunk_offset, chunk_total); + if (n < 0) { return ReadContentResult::Error; } - if (chunk_len == 0) { break; } + if (n == 0) { + if (!dec.parse_trailers_into(x.trailers, x.headers)) { + return ReadContentResult::Error; + } + return ReadContentResult::Success; + } - // Check if adding this chunk would exceed the payload limit if (total_len > payload_max_length || - payload_max_length - total_len < chunk_len) { + payload_max_length - total_len < static_cast(n)) { return ReadContentResult::PayloadTooLarge; } - total_len += chunk_len; - - if (!read_content_with_length(strm, chunk_len, nullptr, out)) { - return ReadContentResult::Error; - } - - if (!line_reader.getline()) { return ReadContentResult::Error; } - - if (strcmp(line_reader.ptr(), "\r\n") != 0) { + if (!out(buf, static_cast(n), chunk_offset, chunk_total)) { return ReadContentResult::Error; } - if (!line_reader.getline()) { return ReadContentResult::Error; } + total_len += static_cast(n); } +} - assert(chunk_len == 0); +inline bool is_chunked_transfer_encoding(const Headers &headers) { + return case_ignore::equal( + get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); +} - // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked - // transfer coding is complete when a chunk with a chunk-size of zero is - // received, possibly followed by a trailer section, and finally terminated by - // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 - // - // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section - // does't care for the existence of the final CRLF. In other words, it seems - // to be ok whether the final CRLF exists or not in the chunked data. - // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 - // - // According to the reference code in RFC 9112, cpp-httplib now allows - // chunked transfer coding data without the final CRLF. - if (!line_reader.getline()) { return ReadContentResult::Success; } +template +bool prepare_content_receiver(T &x, int &status, + ContentReceiverWithProgress receiver, + bool decompress, U callback) { + if (decompress) { + std::string encoding = x.get_header_value("Content-Encoding"); + std::unique_ptr decompressor; - // RFC 7230 Section 4.1.2 - Headers prohibited in trailers - thread_local case_ignore::unordered_set prohibited_trailers = { - // Message framing - "transfer-encoding", "content-length", - - // Routing - "host", - - // Authentication - "authorization", "www-authenticate", "proxy-authenticate", - "proxy-authorization", "cookie", "set-cookie", - - // Request modifiers - "cache-control", "expect", "max-forwards", "pragma", "range", "te", - - // Response control - "age", "expires", "date", "location", "retry-after", "vary", "warning", - - // Payload processing - "content-encoding", "content-type", "content-range", "trailer"}; - - // Parse declared trailer headers once for performance - case_ignore::unordered_set declared_trailers; - if (has_header(x.headers, "Trailer")) { - auto trailer_header = get_header_value(x.headers, "Trailer", "", 0); - auto len = std::strlen(trailer_header); - - split(trailer_header, trailer_header + len, ',', - [&](const char *b, const char *e) { - std::string key(b, e); - if (prohibited_trailers.find(key) == prohibited_trailers.end()) { - declared_trailers.insert(key); - } - }); - } - - size_t trailer_header_count = 0; - while (strcmp(line_reader.ptr(), "\r\n") != 0) { - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { - return ReadContentResult::Error; - } - - // Check trailer header count limit - if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { - return ReadContentResult::Error; - } - - // Exclude line terminator - constexpr auto line_terminator_len = 2; - auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; - - parse_header(line_reader.ptr(), end, - [&](const std::string &key, const std::string &val) { - if (declared_trailers.find(key) != declared_trailers.end()) { - x.trailers.emplace(key, val); - trailer_header_count++; - } - }); - - if (!line_reader.getline()) { return ReadContentResult::Error; } - } - - return ReadContentResult::Success; -} - -inline bool is_chunked_transfer_encoding(const Headers &headers) { - return case_ignore::equal( - get_header_value(headers, "Transfer-Encoding", "", 0), "chunked"); -} - -template -bool prepare_content_receiver(T &x, int &status, - ContentReceiverWithProgress receiver, - bool decompress, U callback) { - if (decompress) { - std::string encoding = x.get_header_value("Content-Encoding"); - std::unique_ptr decompressor; - - if (encoding == "gzip" || encoding == "deflate") { -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif - } else if (encoding.find("br") != std::string::npos) { -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif - } else if (encoding == "zstd") { -#ifdef CPPHTTPLIB_ZSTD_SUPPORT - decompressor = detail::make_unique(); -#else - status = StatusCode::UnsupportedMediaType_415; - return false; -#endif + if (!encoding.empty()) { + decompressor = detail::create_decompressor(encoding); + if (!decompressor) { + // Unsupported encoding or no support compiled in + status = StatusCode::UnsupportedMediaType_415; + return false; + } } if (decompressor) { @@ -7069,6 +7253,64 @@ inline ssize_t Stream::write(const std::string &s) { return write(s.data(), s.size()); } +// BodyReader implementation +inline ssize_t detail::BodyReader::read(char *buf, size_t len) { + if (!stream) { + last_error = Error::Connection; + return -1; + } + if (eof) { return 0; } + + if (!chunked) { + // Content-Length based reading + if (bytes_read >= content_length) { + eof = true; + return 0; + } + + auto remaining = content_length - bytes_read; + auto to_read = (std::min)(len, remaining); + auto n = stream->read(buf, to_read); + + if (n < 0) { + last_error = Error::Read; + eof = true; + return n; + } + if (n == 0) { + // Unexpected EOF before content_length + last_error = Error::Read; + eof = true; + return 0; + } + + bytes_read += static_cast(n); + if (bytes_read >= content_length) { eof = true; } + return n; + } + + // Chunked transfer encoding: delegate to shared decoder instance. + if (!chunked_decoder) { chunked_decoder.reset(new ChunkedDecoder(*stream)); } + + size_t chunk_offset = 0; + size_t chunk_total = 0; + auto n = chunked_decoder->read_payload(buf, len, chunk_offset, chunk_total); + if (n < 0) { + last_error = Error::Read; + eof = true; + return n; + } + + if (n == 0) { + // Final chunk observed. Leave trailer parsing to the caller (StreamHandle). + eof = true; + return 0; + } + + bytes_read += static_cast(n); + return n; +} + namespace detail { inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, @@ -7936,10 +8178,39 @@ inline bool Server::read_content_core( // RFC 7230 Section 3.3.3: If this is a request message and none of the above // are true (no Transfer-Encoding and no Content-Length), then the message // body length is zero (no message body is present). + // + // For non-SSL builds, peek into the socket to detect clients that send a + // body without a Content-Length header (raw HTTP over TCP). If there is + // pending data that exceeds the configured payload limit, treat this as an + // oversized request and fail early (causing connection close). For SSL + // builds we cannot reliably peek the decrypted application bytes, so keep + // the original behaviour. +#if !defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(_WIN32) + if (!req.has_header("Content-Length") && + !detail::is_chunked_transfer_encoding(req.headers)) { + socket_t s = strm.socket(); + if (s != INVALID_SOCKET) { + // Peek up to payload_max_length_ + 1 bytes. If more than + // payload_max_length_ bytes are pending, reject the request. + size_t to_peek = + (payload_max_length_ > 0) + ? (std::min)(payload_max_length_ + 1, static_cast(4096)) + : 1; + std::vector peekbuf(to_peek); + ssize_t n = ::recv(s, peekbuf.data(), to_peek, MSG_PEEK); + if (n > 0 && static_cast(n) > payload_max_length_) { + // Indicate failure so connection will be closed. + return false; + } + } + return true; + } +#else if (!req.has_header("Content-Length") && !detail::is_chunked_transfer_encoding(req.headers)) { return true; } +#endif if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, out, true)) { @@ -8757,6 +9028,26 @@ inline bool ClientImpl::create_and_connect_socket(Socket &socket, return true; } +inline bool ClientImpl::ensure_socket_connection(Socket &socket, Error &error) { + return create_and_connect_socket(socket, error); +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline bool SSLClient::ensure_socket_connection(Socket &socket, Error &error) { + if (!ClientImpl::ensure_socket_connection(socket, error)) { return false; } + + if (!proxy_host_.empty() && proxy_port_ != -1) { return true; } + + if (!initialize_ssl(socket, error)) { + shutdown_socket(socket); + close_socket(socket); + return false; + } + + return true; +} +#endif + inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, bool /*shutdown_gracefully*/) { // If there are any requests in flight from threads other than us, then it's @@ -8869,7 +9160,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { } if (!is_alive) { - if (!create_and_connect_socket(socket_, error)) { + if (!ensure_socket_connection(socket_, error)) { output_error_log(error, &req); return false; } @@ -8887,9 +9178,11 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { } } - if (!scli.initialize_ssl(socket_, error)) { - output_error_log(error, &req); - return false; + if (!proxy_host_.empty() && proxy_port_ != -1) { + if (!scli.initialize_ssl(socket_, error)) { + output_error_log(error, &req); + return false; + } } } #endif @@ -8962,6 +9255,342 @@ inline Result ClientImpl::send_(Request &&req) { #endif } +inline void ClientImpl::prepare_default_headers(Request &r, bool for_stream, + const std::string &ct) { + (void)for_stream; + for (const auto &header : default_headers_) { + if (!r.has_header(header.first)) { r.headers.insert(header); } + } + + if (!r.has_header("Host")) { + if (address_family_ == AF_UNIX) { + r.headers.emplace("Host", "localhost"); + } else { + r.headers.emplace("Host", host_and_port_); + } + } + + if (!r.has_header("Accept")) { r.headers.emplace("Accept", "*/*"); } + + if (!r.content_receiver) { + if (!r.has_header("Accept-Encoding")) { + std::string accept_encoding; +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + accept_encoding = "br"; +#endif +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "gzip, deflate"; +#endif +#ifdef CPPHTTPLIB_ZSTD_SUPPORT + if (!accept_encoding.empty()) { accept_encoding += ", "; } + accept_encoding += "zstd"; +#endif + r.set_header("Accept-Encoding", accept_encoding); + } + +#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT + if (!r.has_header("User-Agent")) { + auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; + r.set_header("User-Agent", agent); + } +#endif + } + + if (!r.body.empty()) { + if (!ct.empty() && !r.has_header("Content-Type")) { + r.headers.emplace("Content-Type", ct); + } + if (!r.has_header("Content-Length")) { + r.headers.emplace("Content-Length", std::to_string(r.body.size())); + } + } +} + +inline ClientImpl::StreamHandle +ClientImpl::open_stream(const std::string &method, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, + const std::string &content_type) { + StreamHandle handle; + handle.response = detail::make_unique(); + handle.error = Error::Success; + + auto query_path = params.empty() ? path : append_query_params(path, params); + handle.connection_ = detail::make_unique(); + + { + std::lock_guard guard(socket_mutex_); + + auto is_alive = false; + if (socket_.is_open()) { + is_alive = detail::is_socket_alive(socket_.sock); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_alive && is_ssl()) { + if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + is_alive = false; + } + } +#endif + if (!is_alive) { + shutdown_ssl(socket_, false); + shutdown_socket(socket_); + close_socket(socket_); + } + } + + if (!is_alive) { + if (!ensure_socket_connection(socket_, handle.error)) { + handle.response.reset(); + return handle; + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl()) { + auto &scli = static_cast(*this); + if (!proxy_host_.empty() && proxy_port_ != -1) { + if (!scli.initialize_ssl(socket_, handle.error)) { + handle.response.reset(); + return handle; + } + } + } +#endif + } + + transfer_socket_ownership_to_handle(handle); + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (is_ssl() && handle.connection_->ssl) { + handle.socket_stream_ = detail::make_unique( + handle.connection_->sock, handle.connection_->ssl, read_timeout_sec_, + read_timeout_usec_, write_timeout_sec_, write_timeout_usec_); + } else { + handle.socket_stream_ = detail::make_unique( + handle.connection_->sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_); + } +#else + handle.socket_stream_ = detail::make_unique( + handle.connection_->sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_); +#endif + handle.stream_ = handle.socket_stream_.get(); + + Request req; + req.method = method; + req.path = query_path; + req.headers = headers; + req.body = body; + + prepare_default_headers(req, true, content_type); + + auto &strm = *handle.stream_; + if (detail::write_request_line(strm, req.method, req.path) < 0) { + handle.error = Error::Write; + handle.response.reset(); + return handle; + } + + if (!detail::write_headers(strm, req.headers)) { + handle.error = Error::Write; + handle.response.reset(); + return handle; + } + + if (!body.empty()) { + if (strm.write(body.data(), body.size()) < 0) { + handle.error = Error::Write; + handle.response.reset(); + return handle; + } + } + + if (!read_response_line(strm, req, *handle.response) || + !detail::read_headers(strm, handle.response->headers)) { + handle.error = Error::Read; + handle.response.reset(); + return handle; + } + + handle.body_reader_.stream = handle.stream_; + + auto content_length_str = handle.response->get_header_value("Content-Length"); + if (!content_length_str.empty()) { + handle.body_reader_.content_length = + static_cast(std::stoull(content_length_str)); + } + + auto transfer_encoding = + handle.response->get_header_value("Transfer-Encoding"); + handle.body_reader_.chunked = (transfer_encoding == "chunked"); + + auto content_encoding = handle.response->get_header_value("Content-Encoding"); + if (!content_encoding.empty()) { + handle.decompressor_ = detail::create_decompressor(content_encoding); + } + + return handle; +} + +inline ssize_t ClientImpl::StreamHandle::read(char *buf, size_t len) { + if (!is_valid() || !response) { return -1; } + + if (decompressor_) { return read_with_decompression(buf, len); } + auto n = detail::read_body_content(stream_, body_reader_, buf, len); + + if (n <= 0 && body_reader_.chunked && !trailers_parsed_ && stream_) { + trailers_parsed_ = true; + if (body_reader_.chunked_decoder) { + if (!body_reader_.chunked_decoder->parse_trailers_into( + response->trailers, response->headers)) { + return n; + } + } else { + detail::ChunkedDecoder dec(*stream_); + if (!dec.parse_trailers_into(response->trailers, response->headers)) { + return n; + } + } + } + + return n; +} + +inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf, + size_t len) { + if (decompress_offset_ < decompress_buffer_.size()) { + auto available = decompress_buffer_.size() - decompress_offset_; + auto to_copy = (std::min)(len, available); + std::memcpy(buf, decompress_buffer_.data() + decompress_offset_, to_copy); + decompress_offset_ += to_copy; + return static_cast(to_copy); + } + + decompress_buffer_.clear(); + decompress_offset_ = 0; + + constexpr size_t kDecompressionBufferSize = 8192; + char compressed_buf[kDecompressionBufferSize]; + + while (true) { + auto n = detail::read_body_content(stream_, body_reader_, compressed_buf, + sizeof(compressed_buf)); + + if (n <= 0) { return n; } + + bool decompress_ok = + decompressor_->decompress(compressed_buf, static_cast(n), + [this](const char *data, size_t data_len) { + decompress_buffer_.append(data, data_len); + return true; + }); + + if (!decompress_ok) { + body_reader_.last_error = Error::Read; + return -1; + } + + if (!decompress_buffer_.empty()) { break; } + } + + auto to_copy = (std::min)(len, decompress_buffer_.size()); + std::memcpy(buf, decompress_buffer_.data(), to_copy); + decompress_offset_ = to_copy; + return static_cast(to_copy); +} + +inline void ClientImpl::StreamHandle::parse_trailers_if_needed() { + if (!response || !stream_ || !body_reader_.chunked || trailers_parsed_) { + return; + } + + trailers_parsed_ = true; + + const auto bufsiz = 128; + char line_buf[bufsiz]; + detail::stream_line_reader line_reader(*stream_, line_buf, bufsiz); + + if (!line_reader.getline()) { return; } + + if (!detail::parse_trailers(line_reader, response->trailers, + response->headers)) { + return; + } +} + +// Inline method implementations for `ChunkedDecoder`. +namespace detail { + +inline ChunkedDecoder::ChunkedDecoder(Stream &s) : strm(s) {} + +inline ssize_t ChunkedDecoder::read_payload(char *buf, size_t len, + size_t &out_chunk_offset, + size_t &out_chunk_total) { + if (finished) { return 0; } + + if (chunk_remaining == 0) { + stream_line_reader lr(strm, line_buf, sizeof(line_buf)); + if (!lr.getline()) { return -1; } + + char *endptr = nullptr; + unsigned long chunk_len = std::strtoul(lr.ptr(), &endptr, 16); + if (endptr == lr.ptr()) { return -1; } + if (chunk_len == ULONG_MAX) { return -1; } + + if (chunk_len == 0) { + chunk_remaining = 0; + finished = true; + out_chunk_offset = 0; + out_chunk_total = 0; + return 0; + } + + chunk_remaining = static_cast(chunk_len); + last_chunk_total = chunk_remaining; + last_chunk_offset = 0; + } + + auto to_read = (std::min)(chunk_remaining, len); + auto n = strm.read(buf, to_read); + if (n <= 0) { return -1; } + + auto offset_before = last_chunk_offset; + last_chunk_offset += static_cast(n); + chunk_remaining -= static_cast(n); + + out_chunk_offset = offset_before; + out_chunk_total = last_chunk_total; + + if (chunk_remaining == 0) { + stream_line_reader lr(strm, line_buf, sizeof(line_buf)); + if (!lr.getline()) { return -1; } + if (std::strcmp(lr.ptr(), "\r\n") != 0) { return -1; } + } + + return n; +} + +inline bool ChunkedDecoder::parse_trailers_into(Headers &dest, + const Headers &src_headers) { + stream_line_reader lr(strm, line_buf, sizeof(line_buf)); + if (!lr.getline()) { return false; } + return parse_trailers(lr, dest, src_headers); +} + +} // namespace detail + +inline void +ClientImpl::transfer_socket_ownership_to_handle(StreamHandle &handle) { + handle.connection_->sock = socket_.sock; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + handle.connection_->ssl = socket_.ssl; + socket_.ssl = nullptr; +#endif + socket_.sock = INVALID_SOCKET; +} + inline bool ClientImpl::handle_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error) { @@ -9264,42 +9893,11 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, } } - if (!req.has_header("Host")) { - // For Unix socket connections, use "localhost" as Host header (similar to - // curl behavior) - if (address_family_ == AF_UNIX) { - req.set_header("Host", "localhost"); - } else { - req.set_header("Host", host_and_port_); - } + std::string ct_for_defaults; + if (!req.has_header("Content-Type") && !req.body.empty()) { + ct_for_defaults = "text/plain"; } - - if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); } - - if (!req.content_receiver) { - if (!req.has_header("Accept-Encoding")) { - std::string accept_encoding; -#ifdef CPPHTTPLIB_BROTLI_SUPPORT - accept_encoding = "br"; -#endif -#ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (!accept_encoding.empty()) { accept_encoding += ", "; } - accept_encoding += "gzip, deflate"; -#endif -#ifdef CPPHTTPLIB_ZSTD_SUPPORT - if (!accept_encoding.empty()) { accept_encoding += ", "; } - accept_encoding += "zstd"; -#endif - req.set_header("Accept-Encoding", accept_encoding); - } - -#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT - if (!req.has_header("User-Agent")) { - auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION; - req.set_header("User-Agent", agent); - } -#endif - }; + prepare_default_headers(req, false, ct_for_defaults); if (req.body.empty()) { if (req.content_provider_) { @@ -9315,15 +9913,6 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, req.set_header("Content-Length", "0"); } } - } else { - if (!req.has_header("Content-Type")) { - req.set_header("Content-Type", "text/plain"); - } - - if (!req.has_header("Content-Length")) { - auto length = std::to_string(req.body.size()); - req.set_header("Content-Length", length); - } } if (!basic_auth_password_.empty() || !basic_auth_username_.empty()) { @@ -11276,7 +11865,7 @@ inline bool SSLClient::connect_with_proxy( close_socket(socket); // Create a new socket for the authenticated CONNECT request - if (!create_and_connect_socket(socket, error)) { + if (!ensure_socket_connection(socket, error)) { success = false; output_error_log(error, nullptr); return false; @@ -12174,6 +12763,13 @@ inline Result Client::Options(const std::string &path, const Headers &headers) { return cli_->Options(path, headers); } +inline ClientImpl::StreamHandle +Client::open_stream(const std::string &method, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type) { + return cli_->open_stream(method, path, params, headers, body, content_type); +} + inline bool Client::send(Request &req, Response &res, Error &error) { return cli_->send(req, res, error); } @@ -12333,6 +12929,375 @@ inline SSL_CTX *Client::ssl_context() const { // ---------------------------------------------------------------------------- +/* + * C++11/14/17 Streaming API + * + * This section provides iterator-style streaming functionality for C++11/14/17. + * For C++20 and later, a coroutine-based API with range-for syntax is + * available. + * + * Usage: + * httplib::Client cli("example.com"); + * auto result = httplib::stream::Get(cli, "/large-file"); + * if (result) { + * while (result.next()) { + * process(result.data(), result.size()); + * } + * } + */ + +namespace stream { + +class Result { +public: + Result() : chunk_size_(8192) {} + + explicit Result(ClientImpl::StreamHandle &&handle, size_t chunk_size = 8192) + : handle_(std::move(handle)), chunk_size_(chunk_size) {} + + Result(Result &&other) noexcept + : handle_(std::move(other.handle_)), buffer_(std::move(other.buffer_)), + current_size_(other.current_size_), chunk_size_(other.chunk_size_), + finished_(other.finished_) { + other.current_size_ = 0; + other.finished_ = true; + } + + Result &operator=(Result &&other) noexcept { + if (this != &other) { + handle_ = std::move(other.handle_); + buffer_ = std::move(other.buffer_); + current_size_ = other.current_size_; + chunk_size_ = other.chunk_size_; + finished_ = other.finished_; + other.current_size_ = 0; + other.finished_ = true; + } + return *this; + } + + Result(const Result &) = delete; + Result &operator=(const Result &) = delete; + + // Check if the result is valid (connection succeeded and response received) + bool is_valid() const { return handle_.is_valid(); } + explicit operator bool() const { return is_valid(); } + + // Response status code + int status() const { + return handle_.response ? handle_.response->status : -1; + } + + // Response headers + const Headers &headers() const { + static const Headers empty_headers; + return handle_.response ? handle_.response->headers : empty_headers; + } + + std::string get_header_value(const std::string &key, + const char *def = "") const { + return handle_.response ? handle_.response->get_header_value(key, def) + : def; + } + + bool has_header(const std::string &key) const { + return handle_.response ? handle_.response->has_header(key) : false; + } + + // Error information + Error error() const { return handle_.error; } + Error read_error() const { return handle_.get_read_error(); } + bool has_read_error() const { return handle_.has_read_error(); } + + // Streaming iteration API + // Call next() to read the next chunk, then access data via data()/size() + // Returns true if data was read, false when stream is exhausted + bool next() { + if (!handle_.is_valid() || finished_) { return false; } + + if (buffer_.size() < chunk_size_) { buffer_.resize(chunk_size_); } + + ssize_t n = handle_.read(&buffer_[0], chunk_size_); + if (n > 0) { + current_size_ = static_cast(n); + return true; + } + + current_size_ = 0; + finished_ = true; + return false; + } + + // Pointer to current chunk data (valid after next() returns true) + const char *data() const { return buffer_.data(); } + + // Size of current chunk (valid after next() returns true) + size_t size() const { return current_size_; } + + // Convenience method: read all remaining data into a string + std::string read_all() { + std::string result; + while (next()) { + result.append(data(), size()); + } + return result; + } + +private: + ClientImpl::StreamHandle handle_; + std::string buffer_; + size_t current_size_ = 0; + size_t chunk_size_; + bool finished_ = false; +}; + +// GET +template +inline Result Get(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path), chunk_size}; +} + +template +inline Result Get(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path, {}, headers), chunk_size}; +} + +template +inline Result Get(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path, params), chunk_size}; +} + +template +inline Result Get(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path, params, headers), chunk_size}; +} + +// POST +template +inline Result Post(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("POST", path, {}, {}, body, content_type), + chunk_size}; +} + +template +inline Result Post(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("POST", path, {}, headers, body, content_type), + chunk_size}; +} + +template +inline Result Post(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("POST", path, params, {}, body, content_type), + chunk_size}; +} + +template +inline Result Post(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("POST", path, params, headers, body, content_type), + chunk_size}; +} + +// PUT +template +inline Result Put(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("PUT", path, {}, {}, body, content_type), + chunk_size}; +} + +template +inline Result Put(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PUT", path, {}, headers, body, content_type), + chunk_size}; +} + +template +inline Result Put(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PUT", path, params, {}, body, content_type), + chunk_size}; +} + +template +inline Result Put(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("PUT", path, params, headers, body, content_type), + chunk_size}; +} + +// PATCH +template +inline Result Patch(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("PATCH", path, {}, {}, body, content_type), + chunk_size}; +} + +template +inline Result Patch(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PATCH", path, {}, headers, body, content_type), + chunk_size}; +} + +template +inline Result Patch(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PATCH", path, params, {}, body, content_type), + chunk_size}; +} + +template +inline Result Patch(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("PATCH", path, params, headers, body, content_type), + chunk_size}; +} + +// DELETE +template +inline Result Delete(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path), chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, {}, headers), chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, {}, {}, body, content_type), + chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("DELETE", path, {}, headers, body, content_type), + chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, params), chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, params, headers), chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, params, {}, body, content_type), + chunk_size}; +} + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("DELETE", path, params, headers, body, content_type), + chunk_size}; +} + +// HEAD +template +inline Result Head(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path), chunk_size}; +} + +template +inline Result Head(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path, {}, headers), chunk_size}; +} + +template +inline Result Head(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path, params), chunk_size}; +} + +template +inline Result Head(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path, params, headers), chunk_size}; +} + +// OPTIONS +template +inline Result Options(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path), chunk_size}; +} + +template +inline Result Options(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path, {}, headers), chunk_size}; +} + +template +inline Result Options(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path, params), chunk_size}; +} + +template +inline Result Options(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path, params, headers), chunk_size}; +} + +} // namespace stream } // namespace httplib #endif // CPPHTTPLIB_HTTPLIB_H diff --git a/test/test.cc b/test/test.cc index 76713ad0cf..3282936a65 100644 --- a/test/test.cc +++ b/test/test.cc @@ -3198,6 +3198,33 @@ class ServerTest : public ::testing::Test { return true; }); }) + .Get("/streamed-chunked-with-prohibited-trailer", + [&](const Request & /*req*/, Response &res) { + auto i = new int(0); + // Declare both a prohibited trailer (Content-Length) and an + // allowed one + res.set_header("Trailer", "Content-Length, X-Allowed"); + + res.set_chunked_content_provider( + "text/plain", + [i](size_t /*offset*/, DataSink &sink) { + switch (*i) { + case 0: sink.os << "123"; break; + case 1: sink.os << "456"; break; + case 2: sink.os << "789"; break; + case 3: { + sink.done_with_trailer( + {{"Content-Length", "5"}, {"X-Allowed", "yes"}}); + } break; + } + (*i)++; + return true; + }, + [i](bool success) { + EXPECT_TRUE(success); + delete i; + }); + }) .Get("/streamed-chunked2", [&](const Request & /*req*/, Response &res) { auto i = new int(0); @@ -11686,3 +11713,754 @@ TEST(ServerRequestParsingTest, RequestWithoutContentLengthOrTransferEncoding) { &resp)); EXPECT_TRUE(resp.find("HTTP/1.1 200 OK") == 0); } + +//============================================================================== +// open_stream() Tests +//============================================================================== + +inline std::string read_all(ClientImpl::StreamHandle &handle) { + std::string result; + char buf[8192]; + ssize_t n; + while ((n = handle.read(buf, sizeof(buf))) > 0) { + result.append(buf, static_cast(n)); + } + return result; +} + +// Mock stream for unit tests +class MockStream : public Stream { +public: + std::string data; + size_t pos = 0; + ssize_t error_after = -1; // -1 = no error + + explicit MockStream(const std::string &d, ssize_t err = -1) + : data(d), error_after(err) {} + bool is_readable() const override { return true; } + bool wait_readable() const override { return true; } + bool wait_writable() const override { return true; } + ssize_t read(char *ptr, size_t size) override { + if (error_after >= 0 && pos >= static_cast(error_after)) return -1; + if (pos >= data.size()) return 0; + size_t limit = + error_after >= 0 ? static_cast(error_after) : data.size(); + size_t to_read = std::min(size, std::min(data.size() - pos, limit - pos)); + std::memcpy(ptr, data.data() + pos, to_read); + pos += to_read; + return static_cast(to_read); + } + ssize_t write(const char *, size_t) override { return -1; } + void get_remote_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 0; + } + void get_local_ip_and_port(std::string &ip, int &port) const override { + ip = "127.0.0.1"; + port = 0; + } + socket_t socket() const override { return INVALID_SOCKET; } + time_t duration() const override { return 0; } +}; + +TEST(StreamHandleTest, Basic) { + ClientImpl::StreamHandle handle; + EXPECT_FALSE(handle.is_valid()); + handle.response = detail::make_unique(); + handle.error = Error::Connection; + EXPECT_FALSE(handle.is_valid()); + handle.error = Error::Success; + EXPECT_TRUE(handle.is_valid()); +} + +TEST(BodyReaderTest, Basic) { + MockStream stream("Hello, World!"); + detail::BodyReader reader; + reader.stream = &stream; + reader.content_length = 13; + char buf[32]; + EXPECT_EQ(13, reader.read(buf, sizeof(buf))); + EXPECT_EQ(0, reader.read(buf, sizeof(buf))); + EXPECT_TRUE(reader.eof); +} + +TEST(BodyReaderTest, NoStream) { + detail::BodyReader reader; + char buf[32]; + EXPECT_EQ(-1, reader.read(buf, sizeof(buf))); + EXPECT_EQ(Error::Connection, reader.last_error); +} + +TEST(BodyReaderTest, Error) { + MockStream stream("Hello, World!", 5); + detail::BodyReader reader; + reader.stream = &stream; + reader.content_length = 13; + char buf[32]; + EXPECT_EQ(5, reader.read(buf, sizeof(buf))); + EXPECT_EQ(-1, reader.read(buf, sizeof(buf))); + EXPECT_EQ(Error::Read, reader.last_error); +} + +// Memory buffer mode removed: StreamHandle reads only from socket streams. +// Mock-based StreamHandle tests relying on private internals are removed. + +class OpenStreamTest : public ::testing::Test { +protected: + void SetUp() override { + svr_.Get("/hello", [](const Request &, Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + svr_.Get("/large", [](const Request &, Response &res) { + res.set_content(std::string(10000, 'X'), "text/plain"); + }); + svr_.Get("/chunked", [](const Request &, Response &res) { + res.set_chunked_content_provider("text/plain", + [](size_t offset, DataSink &sink) { + if (offset < 15) { + sink.write("chunk", 5); + return true; + } + sink.done(); + return true; + }); + }); + svr_.Get("/compressible", [](const Request &, Response &res) { + res.set_chunked_content_provider("text/plain", [](size_t offset, + DataSink &sink) { + if (offset < 100 * 1024) { + std::string chunk(std::min(size_t(8192), 100 * 1024 - offset), 'A'); + sink.write(chunk.data(), chunk.size()); + return true; + } + sink.done(); + return true; + }); + }); + svr_.Get("/streamed-chunked-with-prohibited-trailer", + [](const Request & /*req*/, Response &res) { + auto i = new int(0); + res.set_header("Trailer", "Content-Length, X-Allowed"); + res.set_chunked_content_provider( + "text/plain", + [i](size_t /*offset*/, DataSink &sink) { + switch (*i) { + case 0: sink.os << "123"; break; + case 1: sink.os << "456"; break; + case 2: sink.os << "789"; break; + case 3: { + sink.done_with_trailer( + {{"Content-Length", "5"}, {"X-Allowed", "yes"}}); + } break; + } + (*i)++; + return true; + }, + [i](bool success) { + EXPECT_TRUE(success); + delete i; + }); + }); + // Echo headers endpoint for header-related tests + svr_.Get("/echo-headers", [](const Request &req, Response &res) { + std::string body; + for (const auto &h : req.headers) { + body.append(h.first); + body.push_back(':'); + body.append(h.second); + body.push_back('\n'); + } + res.set_content(body, "text/plain"); + }); + svr_.Post("/echo-headers", [](const Request &req, Response &res) { + std::string body; + for (const auto &h : req.headers) { + body.append(h.first); + body.push_back(':'); + body.append(h.second); + body.push_back('\n'); + } + res.set_content(body, "text/plain"); + }); + thread_ = std::thread([this]() { svr_.listen("127.0.0.1", 8787); }); + svr_.wait_until_ready(); + } + void TearDown() override { + svr_.stop(); + if (thread_.joinable()) thread_.join(); + } + Server svr_; + std::thread thread_; +}; + +TEST_F(OpenStreamTest, Basic) { + Client cli("127.0.0.1", 8787); + auto handle = cli.open_stream("GET", "/hello"); + EXPECT_TRUE(handle.is_valid()); + EXPECT_EQ("Hello World!", read_all(handle)); +} + +TEST_F(OpenStreamTest, SmallBuffer) { + Client cli("127.0.0.1", 8787); + auto handle = cli.open_stream("GET", "/hello"); + std::string result; + char buf[4]; + ssize_t n; + while ((n = handle.read(buf, sizeof(buf))) > 0) + result.append(buf, static_cast(n)); + EXPECT_EQ("Hello World!", result); +} + +TEST_F(OpenStreamTest, DefaultHeaders) { + Client cli("127.0.0.1", 8787); + + // open_stream GET should include Host, User-Agent and Accept-Encoding + { + auto handle = cli.open_stream("GET", "/echo-headers"); + ASSERT_TRUE(handle.is_valid()); + auto body = read_all(handle); + EXPECT_NE(body.find("Host:127.0.0.1:8787"), std::string::npos); + EXPECT_NE(body.find("User-Agent:cpp-httplib/" CPPHTTPLIB_VERSION), + std::string::npos); + EXPECT_NE(body.find("Accept-Encoding:"), std::string::npos); + } + + // open_stream POST with body and no explicit content_type should NOT add + // text/plain Content-Type (behavior differs from non-streaming path), but + // should include Content-Length + { + auto handle = cli.open_stream("POST", "/echo-headers", {}, {}, "hello", ""); + ASSERT_TRUE(handle.is_valid()); + auto body = read_all(handle); + EXPECT_EQ(body.find("Content-Type: text/plain"), std::string::npos); + EXPECT_NE(body.find("Content-Length:5"), std::string::npos); + } + + // open_stream POST with explicit Content-Type should preserve it + { + auto handle = cli.open_stream("POST", "/echo-headers", {}, + {{"Content-Type", "application/custom"}}, + "{}", "application/custom"); + ASSERT_TRUE(handle.is_valid()); + auto body = read_all(handle); + EXPECT_NE(body.find("Content-Type:application/custom"), std::string::npos); + } + + // User-specified User-Agent must not be overwritten for stream API + { + auto handle = cli.open_stream("GET", "/echo-headers", {}, + {{"User-Agent", "MyAgent/1.2"}}); + ASSERT_TRUE(handle.is_valid()); + auto body = read_all(handle); + EXPECT_NE(body.find("User-Agent:MyAgent/1.2"), std::string::npos); + } +} + +TEST_F(OpenStreamTest, Large) { + Client cli("127.0.0.1", 8787); + auto handle = cli.open_stream("GET", "/large"); + EXPECT_EQ(10000u, read_all(handle).size()); +} + +TEST_F(OpenStreamTest, ConnectionError) { + Client cli("127.0.0.1", 9999); + auto handle = cli.open_stream("GET", "/hello"); + EXPECT_FALSE(handle.is_valid()); +} + +TEST_F(OpenStreamTest, Chunked) { + Client cli("127.0.0.1", 8787); + auto handle = cli.open_stream("GET", "/chunked"); + EXPECT_TRUE(handle.response && handle.response->get_header_value( + "Transfer-Encoding") == "chunked"); + EXPECT_EQ("chunkchunkchunk", read_all(handle)); +} + +TEST_F(OpenStreamTest, ProhibitedTrailersAreIgnored_Stream) { + Client cli("127.0.0.1", 8787); + auto handle = + cli.open_stream("GET", "/streamed-chunked-with-prohibited-trailer"); + ASSERT_TRUE(handle.is_valid()); + + // Consume body to allow trailers to be received/parsed + auto body = read_all(handle); + + // Explicitly parse trailers (ensure trailers are available for assertion) + handle.parse_trailers_if_needed(); + EXPECT_EQ(std::string("123456789"), body); + + // The response should include a Trailer header declaring both names + ASSERT_TRUE(handle.response); + EXPECT_TRUE(handle.response->has_header("Trailer")); + EXPECT_EQ(std::string("Content-Length, X-Allowed"), + handle.response->get_header_value("Trailer")); + + // Prohibited trailer must not be present + EXPECT_FALSE(handle.response->has_trailer("Content-Length")); + // Allowed trailer should be present + EXPECT_TRUE(handle.response->has_trailer("X-Allowed")); + EXPECT_EQ(std::string("yes"), + handle.response->get_trailer_value("X-Allowed")); + + // Verify trailers are NOT present as regular headers + EXPECT_EQ(std::string(""), + handle.response->get_header_value("Content-Length")); + EXPECT_EQ(std::string(""), handle.response->get_header_value("X-Allowed")); +} + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +TEST_F(OpenStreamTest, Gzip) { + Client cli("127.0.0.1", 8787); + auto handle = cli.open_stream("GET", "/compressible", {}, + {{"Accept-Encoding", "gzip"}}); + EXPECT_EQ("gzip", handle.response->get_header_value("Content-Encoding")); + EXPECT_EQ(100u * 1024u, read_all(handle).size()); +} +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +TEST_F(OpenStreamTest, Brotli) { + Client cli("127.0.0.1", 8787); + auto handle = + cli.open_stream("GET", "/compressible", {}, {{"Accept-Encoding", "br"}}); + EXPECT_EQ("br", handle.response->get_header_value("Content-Encoding")); + EXPECT_EQ(100u * 1024u, read_all(handle).size()); +} +#endif + +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +TEST_F(OpenStreamTest, Zstd) { + Client cli("127.0.0.1", 8787); + auto handle = cli.open_stream("GET", "/compressible", {}, + {{"Accept-Encoding", "zstd"}}); + EXPECT_EQ("zstd", handle.response->get_header_value("Content-Encoding")); + EXPECT_EQ(100u * 1024u, read_all(handle).size()); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLOpenStreamTest : public ::testing::Test { +protected: + SSLOpenStreamTest() : svr_("cert.pem", "key.pem") {} + void SetUp() override { + svr_.Get("/hello", [](const Request &, Response &res) { + res.set_content("Hello SSL World!", "text/plain"); + }); + svr_.Get("/chunked", [](const Request &, Response &res) { + res.set_chunked_content_provider("text/plain", + [](size_t offset, DataSink &sink) { + if (offset < 15) { + sink.write("chunk", 5); + return true; + } + sink.done(); + return true; + }); + }); + svr_.Post("/echo", [](const Request &req, Response &res) { + res.set_content(req.body, req.get_header_value("Content-Type")); + }); + svr_.Post("/chunked-response", [](const Request &req, Response &res) { + std::string body = req.body; + res.set_chunked_content_provider( + "text/plain", [body](size_t offset, DataSink &sink) { + if (offset < body.size()) { + sink.write(body.data() + offset, body.size() - offset); + } + sink.done(); + return true; + }); + }); + thread_ = std::thread([this]() { svr_.listen("127.0.0.1", 8788); }); + svr_.wait_until_ready(); + } + void TearDown() override { + svr_.stop(); + if (thread_.joinable()) thread_.join(); + } + SSLServer svr_; + std::thread thread_; +}; + +TEST_F(SSLOpenStreamTest, Basic) { + SSLClient cli("127.0.0.1", 8788); + cli.enable_server_certificate_verification(false); + auto handle = cli.open_stream("GET", "/hello"); + ASSERT_TRUE(handle.is_valid()); + EXPECT_EQ("Hello SSL World!", read_all(handle)); +} + +TEST_F(SSLOpenStreamTest, Chunked) { + SSLClient cli("127.0.0.1", 8788); + cli.enable_server_certificate_verification(false); + + auto handle = cli.open_stream("GET", "/chunked"); + + ASSERT_TRUE(handle.is_valid()) << "Error: " << static_cast(handle.error); + EXPECT_TRUE(handle.response && handle.response->get_header_value( + "Transfer-Encoding") == "chunked"); + + auto body = read_all(handle); + EXPECT_EQ("chunkchunkchunk", body); +} + +TEST_F(SSLOpenStreamTest, Post) { + SSLClient cli("127.0.0.1", 8788); + cli.enable_server_certificate_verification(false); + + auto handle = + cli.open_stream("POST", "/echo", {}, {}, "Hello SSL POST", "text/plain"); + + ASSERT_TRUE(handle.is_valid()) << "Error: " << static_cast(handle.error); + EXPECT_EQ(200, handle.response->status); + + auto body = read_all(handle); + EXPECT_EQ("Hello SSL POST", body); +} + +TEST_F(SSLOpenStreamTest, PostChunked) { + SSLClient cli("127.0.0.1", 8788); + cli.enable_server_certificate_verification(false); + + auto handle = cli.open_stream("POST", "/chunked-response", {}, {}, + "Chunked SSL Data", "text/plain"); + + ASSERT_TRUE(handle.is_valid()); + EXPECT_EQ(200, handle.response->status); + + auto body = read_all(handle); + EXPECT_EQ("Chunked SSL Data", body); +} +#endif // CPPHTTPLIB_OPENSSL_SUPPORT + +//============================================================================== +// Parity Tests: ensure streaming and non-streaming APIs produce identical +// results for various scenarios. +//============================================================================== + +TEST(ParityTest, GetVsOpenStream) { + Server svr; + + const std::string path = "/parity"; + const std::string content = "Parity test content: hello world"; + + svr.Get(path, [&](const Request & /*req*/, Response &res) { + res.set_content(content, "text/plain"); + }); + + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + + // Non-stream path + auto r1 = cli.Get(path); + ASSERT_TRUE(r1); + EXPECT_EQ(StatusCode::OK_200, r1->status); + + // Stream path + auto h = cli.open_stream("GET", path); + ASSERT_TRUE(h.is_valid()); + + EXPECT_EQ(r1->body, read_all(h)); +} + +// Helper to compress data with provided compressor type T +template +static std::string compress_payload_for_parity(const std::string &in) { + std::string out; + Compressor compressor; + bool ok = compressor.compress(in.data(), in.size(), /*last=*/true, + [&](const char *data, size_t n) { + out.append(data, n); + return true; + }); + EXPECT_TRUE(ok); + return out; +} + +// Helper function for compression parity tests +template +static void test_compression_parity(const std::string &original, + const std::string &path, + const std::string &encoding) { + const std::string compressed = + compress_payload_for_parity(original); + + Server svr; + + svr.Get(path, [&](const Request & /*req*/, Response &res) { + res.set_content(compressed, "application/octet-stream"); + res.set_header("Content-Encoding", encoding); + }); + + auto t = std::thread([&] { svr.listen("localhost", 1234); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli("localhost", 1234); + + // Non-streaming + { + auto res = cli.Get(path); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(original, res->body); + } + + // Streaming + { + auto h = cli.open_stream("GET", path); + ASSERT_TRUE(h.is_valid()); + EXPECT_EQ(original, read_all(h)); + } +} + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +TEST(ParityTest, Gzip) { + test_compression_parity( + "The quick brown fox jumps over the lazy dog", "/parity-gzip", "gzip"); +} +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +TEST(ParityTest, Brotli) { + test_compression_parity( + "Hello, brotli parity test payload", "/parity-br", "br"); +} +#endif + +#ifdef CPPHTTPLIB_ZSTD_SUPPORT +TEST(ParityTest, Zstd) { + test_compression_parity( + "Zstandard parity test payload", "/parity-zstd", "zstd"); +} +#endif + +//============================================================================== +// New Stream API Tests +//============================================================================== + +inline std::string read_body(httplib::stream::Result &result) { + std::string body; + while (result.next()) { + body.append(result.data(), result.size()); + } + return body; +} + +TEST(ClientConnectionTest, Basic) { + httplib::ClientConnection conn; + EXPECT_FALSE(conn.is_open()); + conn.sock = 1; + EXPECT_TRUE(conn.is_open()); + httplib::ClientConnection conn2(std::move(conn)); + EXPECT_EQ(INVALID_SOCKET, conn.sock); + conn2.sock = INVALID_SOCKET; +} + +// Unified test server for all stream::* tests +class StreamApiTest : public ::testing::Test { +protected: + void SetUp() override { + svr_.Get("/hello", [](const httplib::Request &, httplib::Response &res) { + res.set_content("Hello World!", "text/plain"); + }); + svr_.Get("/echo-params", + [](const httplib::Request &req, httplib::Response &res) { + std::string r; + for (const auto &p : req.params) { + if (!r.empty()) r += "&"; + r += p.first + "=" + p.second; + } + res.set_content(r, "text/plain"); + }); + svr_.Post("/echo", [](const httplib::Request &req, httplib::Response &res) { + res.set_content(req.body, req.get_header_value("Content-Type")); + }); + svr_.Post("/echo-headers", + [](const httplib::Request &req, httplib::Response &res) { + std::string r; + for (const auto &h : req.headers) + r += h.first + ": " + h.second + "\n"; + res.set_content(r, "text/plain"); + }); + svr_.Post("/echo-params", + [](const httplib::Request &req, httplib::Response &res) { + std::string r = "params:"; + for (const auto &p : req.params) + r += p.first + "=" + p.second + ";"; + res.set_content(r + " body:" + req.body, "text/plain"); + }); + svr_.Post("/large", [](const httplib::Request &, httplib::Response &res) { + res.set_content(std::string(100 * 1024, 'X'), "application/octet-stream"); + }); + svr_.Put("/echo", [](const httplib::Request &req, httplib::Response &res) { + res.set_content("PUT:" + req.body, "text/plain"); + }); + svr_.Patch("/echo", + [](const httplib::Request &req, httplib::Response &res) { + res.set_content("PATCH:" + req.body, "text/plain"); + }); + svr_.Delete( + "/resource", [](const httplib::Request &req, httplib::Response &res) { + res.set_content(req.body.empty() ? "Deleted" : "Deleted:" + req.body, + "text/plain"); + }); + svr_.Get("/head-test", + [](const httplib::Request &, httplib::Response &res) { + res.set_content("body for HEAD", "text/plain"); + }); + svr_.Options("/options", + [](const httplib::Request &, httplib::Response &res) { + res.set_header("Allow", "GET, POST, PUT, DELETE, OPTIONS"); + }); + thread_ = std::thread([this]() { svr_.listen("localhost", 8790); }); + svr_.wait_until_ready(); + } + void TearDown() override { + svr_.stop(); + if (thread_.joinable()) thread_.join(); + } + httplib::Server svr_; + std::thread thread_; +}; + +// stream::Get tests +TEST_F(StreamApiTest, GetBasic) { + httplib::Client cli("localhost", 8790); + auto result = httplib::stream::Get(cli, "/hello"); + ASSERT_TRUE(result.is_valid()); + EXPECT_EQ(200, result.status()); + EXPECT_EQ("Hello World!", read_body(result)); +} + +TEST_F(StreamApiTest, GetWithParams) { + httplib::Client cli("localhost", 8790); + httplib::Params params{{"foo", "bar"}}; + auto result = httplib::stream::Get(cli, "/echo-params", params); + ASSERT_TRUE(result.is_valid()); + EXPECT_TRUE(read_body(result).find("foo=bar") != std::string::npos); +} + +TEST_F(StreamApiTest, GetConnectionError) { + httplib::Client cli("localhost", 9999); + EXPECT_FALSE(httplib::stream::Get(cli, "/hello").is_valid()); +} + +TEST_F(StreamApiTest, Get404) { + httplib::Client cli("localhost", 8790); + auto result = httplib::stream::Get(cli, "/nonexistent"); + EXPECT_TRUE(result.is_valid()); + EXPECT_EQ(404, result.status()); +} + +// stream::Post tests +TEST_F(StreamApiTest, PostBasic) { + httplib::Client cli("localhost", 8790); + auto result = httplib::stream::Post(cli, "/echo", R"({"key":"value"})", + "application/json"); + ASSERT_TRUE(result.is_valid()); + EXPECT_EQ("application/json", result.get_header_value("Content-Type")); + EXPECT_EQ(R"({"key":"value"})", read_body(result)); +} + +TEST_F(StreamApiTest, PostWithHeaders) { + httplib::Client cli("localhost", 8790); + httplib::Headers headers{{"X-Custom", "value"}}; + auto result = httplib::stream::Post(cli, "/echo-headers", headers, "body", + "text/plain"); + EXPECT_TRUE(read_body(result).find("X-Custom: value") != std::string::npos); +} + +TEST_F(StreamApiTest, PostWithParams) { + httplib::Client cli("localhost", 8790); + httplib::Params params{{"k", "v"}}; + auto result = + httplib::stream::Post(cli, "/echo-params", params, "data", "text/plain"); + auto body = read_body(result); + EXPECT_TRUE(body.find("k=v") != std::string::npos); + EXPECT_TRUE(body.find("body:data") != std::string::npos); +} + +TEST_F(StreamApiTest, PostLarge) { + httplib::Client cli("localhost", 8790); + auto result = httplib::stream::Post(cli, "/large", "", "text/plain"); + size_t total = 0; + while (result.next()) { + total += result.size(); + } + EXPECT_EQ(100u * 1024u, total); +} + +// stream::Put/Patch tests +TEST_F(StreamApiTest, PutAndPatch) { + httplib::Client cli("localhost", 8790); + auto put = httplib::stream::Put(cli, "/echo", "test", "text/plain"); + EXPECT_EQ("PUT:test", read_body(put)); + auto patch = httplib::stream::Patch(cli, "/echo", "test", "text/plain"); + EXPECT_EQ("PATCH:test", read_body(patch)); +} + +// stream::Delete tests +TEST_F(StreamApiTest, Delete) { + httplib::Client cli("localhost", 8790); + auto del1 = httplib::stream::Delete(cli, "/resource"); + EXPECT_EQ("Deleted", read_body(del1)); + auto del2 = httplib::stream::Delete(cli, "/resource", "data", "text/plain"); + EXPECT_EQ("Deleted:data", read_body(del2)); +} + +// stream::Head/Options tests +TEST_F(StreamApiTest, HeadAndOptions) { + httplib::Client cli("localhost", 8790); + auto head = httplib::stream::Head(cli, "/head-test"); + EXPECT_TRUE(head.is_valid()); + EXPECT_FALSE(head.get_header_value("Content-Length").empty()); + + auto opts = httplib::stream::Options(cli, "/options"); + EXPECT_EQ("GET, POST, PUT, DELETE, OPTIONS", opts.get_header_value("Allow")); +} + +// SSL stream::* tests +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLStreamApiTest : public ::testing::Test { +protected: + void SetUp() override { + svr_.Get("/hello", [](const httplib::Request &, httplib::Response &res) { + res.set_content("Hello SSL!", "text/plain"); + }); + svr_.Post("/echo", [](const httplib::Request &req, httplib::Response &res) { + res.set_content(req.body, "text/plain"); + }); + thread_ = std::thread([this]() { svr_.listen("127.0.0.1", 8803); }); + svr_.wait_until_ready(); + } + void TearDown() override { + svr_.stop(); + if (thread_.joinable()) thread_.join(); + } + httplib::SSLServer svr_{"cert.pem", "key.pem"}; + std::thread thread_; +}; + +TEST_F(SSLStreamApiTest, GetAndPost) { + httplib::SSLClient cli("127.0.0.1", 8803); + cli.enable_server_certificate_verification(false); + auto get = httplib::stream::Get(cli, "/hello"); + EXPECT_EQ("Hello SSL!", read_body(get)); + auto post = httplib::stream::Post(cli, "/echo", "test", "text/plain"); + EXPECT_EQ("test", read_body(post)); +} +#endif \ No newline at end of file From 1d36013fc3aa14f39f5e8071be445e5a75fafff7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 2 Dec 2025 17:08:53 -0500 Subject: [PATCH 0544/1222] Update README --- README-stream.md | 4 +++- README.md | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README-stream.md b/README-stream.md index ce02e1c705..851fd38e79 100644 --- a/README-stream.md +++ b/README-stream.md @@ -78,7 +78,7 @@ The `StreamHandle` struct provides direct control over streaming responses. It t ```cpp // Open a stream (takes ownership of socket) httplib::Client cli("http://localhost:8080"); -auto handle = cli.open_stream("/path"); +auto handle = cli.open_stream("GET", "/path"); // Check validity if (handle.is_valid()) { @@ -114,6 +114,8 @@ The `httplib.h` header provides a more ergonomic iterator-style API. #include "httplib.h" httplib::Client cli("http://localhost:8080"); +cli.set_follow_location(true); +... // Simple GET auto result = httplib::stream::Get(cli, "/path"); diff --git a/README.md b/README.md index 01eca3ea39..40ecda0377 100644 --- a/README.md +++ b/README.md @@ -1195,6 +1195,8 @@ Process large responses without loading everything into memory. ```c++ httplib::Client cli("localhost", 8080); +cli.set_follow_location(true); +... auto result = httplib::stream::Get(cli, "/large-file"); if (result) { From 143019a38cb88661ce78ca3799dc1c0779c6c563 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 2 Dec 2025 17:24:27 -0500 Subject: [PATCH 0545/1222] Fix #2284 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index f63f76cc0e..62be061cda 100644 --- a/httplib.h +++ b/httplib.h @@ -4687,7 +4687,6 @@ inline EncodingType encoding_type(const Request &req, const Response &res) { return EncodingType::None; } -#ifdef CPPHTTPLIB_ZLIB_SUPPORT inline bool nocompressor::compress(const char *data, size_t data_length, bool /*last*/, Callback callback) { @@ -4695,6 +4694,7 @@ inline bool nocompressor::compress(const char *data, size_t data_length, return callback(data, data_length); } +#ifdef CPPHTTPLIB_ZLIB_SUPPORT inline gzip_compressor::gzip_compressor() { std::memset(&strm_, 0, sizeof(strm_)); strm_.zalloc = Z_NULL; From dbd5ca4bf2da438da1c6dcd0ba7c725f7f2cc7b7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 3 Dec 2025 23:56:04 -0500 Subject: [PATCH 0546/1222] Add error handling for stream read timeouts and connection closures --- README.md | 20 ++++- httplib.h | 129 ++++++++++++++++++----------- test/test.cc | 225 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 324 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 40ecda0377..b595b63ab6 100644 --- a/README.md +++ b/README.md @@ -722,7 +722,7 @@ httplib::SSLClient cli("localhost"); Here is the list of errors from `Result::error()`. ```c++ -enum Error { +enum class Error { Success = 0, Unknown, Connection, @@ -739,6 +739,24 @@ enum Error { Compression, ConnectionTimeout, ProxyConnection, + ConnectionClosed, + Timeout, + ResourceExhaustion, + TooManyFormDataFiles, + ExceedMaxPayloadSize, + ExceedUriMaxLength, + ExceedMaxSocketDescriptorCount, + InvalidRequestLine, + InvalidHTTPMethod, + InvalidHTTPVersion, + InvalidHeaders, + MultipartParsing, + OpenFile, + Listen, + GetSockName, + UnsupportedAddressFamily, + HTTPParsing, + InvalidRangeHeader, }; ``` diff --git a/httplib.h b/httplib.h index 62be061cda..a7296b9c47 100644 --- a/httplib.h +++ b/httplib.h @@ -838,6 +838,50 @@ struct Response { std::string file_content_content_type_; }; +enum class Error { + Success = 0, + Unknown, + Connection, + BindIPAddress, + Read, + Write, + ExceedRedirectCount, + Canceled, + SSLConnection, + SSLLoadingCerts, + SSLServerVerification, + SSLServerHostnameVerification, + UnsupportedMultipartBoundaryChars, + Compression, + ConnectionTimeout, + ProxyConnection, + ConnectionClosed, + Timeout, + ResourceExhaustion, + TooManyFormDataFiles, + ExceedMaxPayloadSize, + ExceedUriMaxLength, + ExceedMaxSocketDescriptorCount, + InvalidRequestLine, + InvalidHTTPMethod, + InvalidHTTPVersion, + InvalidHeaders, + MultipartParsing, + OpenFile, + Listen, + GetSockName, + UnsupportedAddressFamily, + HTTPParsing, + InvalidRangeHeader, + + // For internal use only + SSLPeerCouldBeClosed_, +}; + +std::string to_string(Error error); + +std::ostream &operator<<(std::ostream &os, const Error &obj); + class Stream { public: virtual ~Stream() = default; @@ -856,6 +900,11 @@ class Stream { ssize_t write(const char *ptr); ssize_t write(const std::string &s); + + Error get_error() const { return error_; } + +protected: + Error error_ = Error::Success; }; class TaskQueue { @@ -1292,48 +1341,6 @@ class Server { detail::write_headers; }; -enum class Error { - Success = 0, - Unknown, - Connection, - BindIPAddress, - Read, - Write, - ExceedRedirectCount, - Canceled, - SSLConnection, - SSLLoadingCerts, - SSLServerVerification, - SSLServerHostnameVerification, - UnsupportedMultipartBoundaryChars, - Compression, - ConnectionTimeout, - ProxyConnection, - ResourceExhaustion, - TooManyFormDataFiles, - ExceedMaxPayloadSize, - ExceedUriMaxLength, - ExceedMaxSocketDescriptorCount, - InvalidRequestLine, - InvalidHTTPMethod, - InvalidHTTPVersion, - InvalidHeaders, - MultipartParsing, - OpenFile, - Listen, - GetSockName, - UnsupportedAddressFamily, - HTTPParsing, - InvalidRangeHeader, - - // For internal use only - SSLPeerCouldBeClosed_, -}; - -std::string to_string(Error error); - -std::ostream &operator<<(std::ostream &os, const Error &obj); - class Result { public: Result() = default; @@ -2437,6 +2444,8 @@ inline std::string to_string(const Error error) { case Error::Compression: return "Compression failed"; case Error::ConnectionTimeout: return "Connection timed out"; case Error::ProxyConnection: return "Proxy connection failed"; + case Error::ConnectionClosed: return "Connection closed by server"; + case Error::Timeout: return "Read timeout"; case Error::ResourceExhaustion: return "Resource exhaustion"; case Error::TooManyFormDataFiles: return "Too many form data files"; case Error::ExceedMaxPayloadSize: return "Exceeded maximum payload size"; @@ -7273,13 +7282,15 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) { auto n = stream->read(buf, to_read); if (n < 0) { - last_error = Error::Read; + last_error = stream->get_error(); + if (last_error == Error::Success) { last_error = Error::Read; } eof = true; return n; } if (n == 0) { // Unexpected EOF before content_length - last_error = Error::Read; + last_error = stream->get_error(); + if (last_error == Error::Success) { last_error = Error::Read; } eof = true; return 0; } @@ -7296,7 +7307,8 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) { size_t chunk_total = 0; auto n = chunked_decoder->read_payload(buf, len, chunk_offset, chunk_total); if (n < 0) { - last_error = Error::Read; + last_error = stream->get_error(); + if (last_error == Error::Success) { last_error = Error::Read; } eof = true; return n; } @@ -7387,7 +7399,10 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { } } - if (!wait_readable()) { return -1; } + if (!wait_readable()) { + error_ = Error::Timeout; + return -1; + } read_buff_off_ = 0; read_buff_content_size_ = 0; @@ -7396,6 +7411,11 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { auto n = read_socket(sock_, read_buff_.data(), read_buff_size_, CPPHTTPLIB_RECV_FLAGS); if (n <= 0) { + if (n == 0) { + error_ = Error::ConnectionClosed; + } else { + error_ = Error::Read; + } return n; } else if (n <= static_cast(size)) { memcpy(ptr, read_buff_.data(), static_cast(n)); @@ -7407,7 +7427,15 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { return static_cast(size); } } else { - return read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); + auto n = read_socket(sock_, ptr, size, CPPHTTPLIB_RECV_FLAGS); + if (n <= 0) { + if (n == 0) { + error_ = Error::ConnectionClosed; + } else { + error_ = Error::Read; + } + } + return n; } } @@ -11435,7 +11463,9 @@ inline bool SSLSocketStream::wait_writable() const { inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { if (SSL_pending(ssl_) > 0) { - return SSL_read(ssl_, ptr, static_cast(size)); + auto ret = SSL_read(ssl_, ptr, static_cast(size)); + if (ret == 0) { error_ = Error::ConnectionClosed; } + return ret; } else if (wait_readable()) { auto ret = SSL_read(ssl_, ptr, static_cast(size)); if (ret < 0) { @@ -11460,9 +11490,12 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { } } assert(ret < 0); + } else if (ret == 0) { + error_ = Error::ConnectionClosed; } return ret; } else { + error_ = Error::Timeout; return -1; } } diff --git a/test/test.cc b/test/test.cc index 3282936a65..b6e93d9101 100644 --- a/test/test.cc +++ b/test/test.cc @@ -12463,4 +12463,227 @@ TEST_F(SSLStreamApiTest, GetAndPost) { auto post = httplib::stream::Post(cli, "/echo", "test", "text/plain"); EXPECT_EQ("test", read_body(post)); } -#endif \ No newline at end of file +#endif + +// Tests for Error::Timeout and Error::ConnectionClosed error types +// These errors are set in SocketStream/SSLSocketStream and propagated through +// BodyReader + +TEST(ErrorHandlingTest, StreamReadTimeout) { + // Test that read timeout during streaming is detected + // Use a large content-length response where server delays mid-stream + Server svr; + + svr.Get("/slow-stream", [](const Request &, Response &res) { + // Send a large response with delay in the middle + res.set_content_provider( + 1000, // content_length + "text/plain", [](size_t offset, size_t /*length*/, DataSink &sink) { + if (offset < 100) { + // Send first 100 bytes immediately + std::string data(100, 'A'); + sink.write(data.c_str(), data.size()); + return true; + } + // Then delay longer than client timeout + std::this_thread::sleep_for(std::chrono::seconds(3)); + std::string data(900, 'B'); + sink.write(data.c_str(), data.size()); + return true; + }); + }); + + auto port = 8091; + std::thread t([&]() { svr.listen("localhost", port); }); + svr.wait_until_ready(); + + Client cli("localhost", port); + cli.set_read_timeout(1, 0); // 1 second timeout + + auto handle = cli.open_stream("GET", "/slow-stream"); + ASSERT_TRUE(handle.is_valid()); + + char buf[256]; + ssize_t total = 0; + ssize_t n; + bool got_error = false; + + while ((n = handle.read(buf, sizeof(buf))) > 0) { + total += n; + } + + if (n < 0) { + got_error = true; + // Should be timeout or read error + EXPECT_TRUE(handle.get_read_error() == Error::Timeout || + handle.get_read_error() == Error::Read) + << "Actual error: " << to_string(handle.get_read_error()); + } + + // Either we got an error, or we got less data than expected + EXPECT_TRUE(got_error || total < 1000) + << "Expected timeout but got all " << total << " bytes"; + + svr.stop(); + t.join(); +} + +TEST(ErrorHandlingTest, StreamConnectionClosed) { + // Test connection closed detection via BodyReader + Server svr; + std::atomic close_now{false}; + + svr.Get("/will-close", [&](const Request &, Response &res) { + res.set_content_provider( + 10000, // Large content_length that we won't fully send + "text/plain", [&](size_t offset, size_t /*length*/, DataSink &sink) { + if (offset < 100) { + std::string data(100, 'X'); + sink.write(data.c_str(), data.size()); + return true; + } + // Wait for signal then abort + while (!close_now) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + return false; // Abort - server will close connection + }); + }); + + auto port = 8092; + std::thread t([&]() { svr.listen("localhost", port); }); + svr.wait_until_ready(); + + Client cli("localhost", port); + auto handle = cli.open_stream("GET", "/will-close"); + ASSERT_TRUE(handle.is_valid()); + + char buf[256]; + ssize_t n = handle.read(buf, sizeof(buf)); // First read + EXPECT_GT(n, 0) << "First read should succeed"; + + // Signal server to close + close_now = true; + + // Keep reading until error or EOF + while ((n = handle.read(buf, sizeof(buf))) > 0) { + // Keep reading + } + + // Should get an error since content_length wasn't satisfied + if (n < 0) { + EXPECT_TRUE(handle.get_read_error() == Error::ConnectionClosed || + handle.get_read_error() == Error::Read) + << "Actual error: " << to_string(handle.get_read_error()); + } + + svr.stop(); + t.join(); +} + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(ErrorHandlingTest, SSLStreamReadTimeout) { + // Test that read timeout during SSL streaming is detected + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + + svr.Get("/slow-stream", [](const Request &, Response &res) { + res.set_content_provider( + 1000, "text/plain", + [](size_t offset, size_t /*length*/, DataSink &sink) { + if (offset < 100) { + std::string data(100, 'A'); + sink.write(data.c_str(), data.size()); + return true; + } + std::this_thread::sleep_for(std::chrono::seconds(3)); + std::string data(900, 'B'); + sink.write(data.c_str(), data.size()); + return true; + }); + }); + + auto port = 8093; + std::thread t([&]() { svr.listen("localhost", port); }); + svr.wait_until_ready(); + + SSLClient cli("localhost", port); + cli.enable_server_certificate_verification(false); + cli.set_read_timeout(1, 0); // 1 second timeout + + auto handle = cli.open_stream("GET", "/slow-stream"); + ASSERT_TRUE(handle.is_valid()); + + char buf[256]; + ssize_t total = 0; + ssize_t n; + bool got_error = false; + + while ((n = handle.read(buf, sizeof(buf))) > 0) { + total += n; + } + + if (n < 0) { + got_error = true; + EXPECT_TRUE(handle.get_read_error() == Error::Timeout || + handle.get_read_error() == Error::Read) + << "Actual error: " << to_string(handle.get_read_error()); + } + + EXPECT_TRUE(got_error || total < 1000) + << "Expected timeout but got all " << total << " bytes"; + + svr.stop(); + t.join(); +} + +TEST(ErrorHandlingTest, SSLStreamConnectionClosed) { + // Test SSL connection closed detection + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + std::atomic close_now{false}; + + svr.Get("/will-close", [&](const Request &, Response &res) { + res.set_content_provider( + 10000, "text/plain", + [&](size_t offset, size_t /*length*/, DataSink &sink) { + if (offset < 100) { + std::string data(100, 'X'); + sink.write(data.c_str(), data.size()); + return true; + } + while (!close_now) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + return false; + }); + }); + + auto port = 8094; + std::thread t([&]() { svr.listen("localhost", port); }); + svr.wait_until_ready(); + + SSLClient cli("localhost", port); + cli.enable_server_certificate_verification(false); + auto handle = cli.open_stream("GET", "/will-close"); + ASSERT_TRUE(handle.is_valid()); + + char buf[256]; + ssize_t n = handle.read(buf, sizeof(buf)); // First read + EXPECT_GT(n, 0); + + // Signal server to close + close_now = true; + + while ((n = handle.read(buf, sizeof(buf))) > 0) { + // Keep reading + } + + if (n < 0) { + EXPECT_TRUE(handle.get_read_error() == Error::ConnectionClosed || + handle.get_read_error() == Error::Read) + << "Actual error: " << to_string(handle.get_read_error()); + } + + svr.stop(); + t.join(); +} +#endif From 27b73f050ee5d55b7552a3f18a5f345755673de8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 5 Dec 2025 08:50:12 -0500 Subject: [PATCH 0547/1222] Fix #2287 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index a7296b9c47..c0fbbf50b6 100644 --- a/httplib.h +++ b/httplib.h @@ -2931,7 +2931,7 @@ inline bool is_field_value(const std::string &s) { return is_field_content(s); } namespace detail { inline bool is_hex(char c, int &v) { - if (0x20 <= c && isdigit(c)) { + if (isdigit(c)) { v = c - '0'; return true; } else if ('A' <= c && c <= 'F') { From 3e0fa33559f1bd94cd137f61d939bdac3f854165 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 5 Dec 2025 18:58:54 -0500 Subject: [PATCH 0548/1222] Implement ETag and Last-Modified support for static file responses and If-Range requests (#2286) * Fix #2242: Implement ETag and Last-Modified support for static file responses * Add ETag and Last-Modified handling for If-Range requests * Enhance HTTP date parsing with improved error handling and locale support * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/test.cc Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor ETag handling: separate strong and weak ETag checks for If-Range requests * Fix type for mtime in FileStat and improve ETag handling comments * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Resolved code review comments * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor ETag handling: use 'auto' for type inference and improve code readability * Refactor ETag handling: extract check_if_not_modified and check_if_range methods for improved readability and maintainability * Code cleanup * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test/test.cc Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update httplib.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Enhance ETag handling and validation in httplib.h and add comprehensive tests in test.cc * Refactor ETag comparison logic and add test for If-None-Match with non-existent file * Fix #2287 * Code cleanup * Add tests for extreme date values and negative file modification time in ETag handling * Update HTTP-date parsing comments to reference RFC 9110 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- httplib.h | 249 ++++++++++++++++++++++-- test/test.cc | 535 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 768 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index c0fbbf50b6..31f6ba3760 100644 --- a/httplib.h +++ b/httplib.h @@ -1011,17 +1011,6 @@ using ErrorLogger = std::function; using SocketOptions = std::function; -namespace detail { - -bool set_socket_opt_impl(socket_t sock, int level, int optname, - const void *optval, socklen_t optlen); -bool set_socket_opt(socket_t sock, int level, int optname, int opt); -bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, - time_t usec); -int close_socket(socket_t sock); - -} // namespace detail - void default_socket_options(socket_t sock); const char *status_message(int status); @@ -1102,10 +1091,9 @@ class RegexMatcher final : public MatcherBase { std::regex regex_; }; -ssize_t write_headers(Stream &strm, const Headers &headers); +int close_socket(socket_t sock); -std::string make_host_and_port_string(const std::string &host, int port, - bool is_ssl); +ssize_t write_headers(Stream &strm, const Headers &headers); } // namespace detail @@ -1257,7 +1245,11 @@ class Server { bool listen_internal(); bool routing(Request &req, Response &res, Stream &strm); - bool handle_file_request(const Request &req, Response &res); + bool handle_file_request(Request &req, Response &res); + bool check_if_not_modified(const Request &req, Response &res, + const std::string &etag, time_t mtime) const; + bool check_if_range(Request &req, const std::string &etag, + time_t mtime) const; bool dispatch_request(Request &req, Response &res, const Handlers &handlers) const; bool dispatch_request_for_content_reader( @@ -2593,6 +2585,8 @@ struct FileStat { FileStat(const std::string &path); bool is_file() const; bool is_dir() const; + time_t mtime() const; + size_t size() const; private: #if defined(_WIN32) @@ -2603,6 +2597,9 @@ struct FileStat { int ret_ = -1; }; +std::string make_host_and_port_string(const std::string &host, int port, + bool is_ssl); + std::string trim_copy(const std::string &s); void divide( @@ -2971,6 +2968,90 @@ inline std::string from_i_to_hex(size_t n) { return ret; } +inline std::string compute_etag(const FileStat &fs) { + if (!fs.is_file()) { return std::string(); } + + // If mtime cannot be determined (negative value indicates an error + // or sentinel), do not generate an ETag. Returning a neutral / fixed + // value like 0 could collide with a real file that legitimately has + // mtime == 0 (epoch) and lead to misleading validators. + auto mtime_raw = fs.mtime(); + if (mtime_raw < 0) { return std::string(); } + + auto mtime = static_cast(mtime_raw); + auto size = fs.size(); + + return std::string("W/\"") + from_i_to_hex(mtime) + "-" + + from_i_to_hex(size) + "\""; +} + +// Format time_t as HTTP-date (RFC 9110 Section 5.6.7): "Sun, 06 Nov 1994 +// 08:49:37 GMT" This implementation is defensive: it validates `mtime`, checks +// return values from `gmtime_r`/`gmtime_s`, and ensures `strftime` succeeds. +inline std::string file_mtime_to_http_date(time_t mtime) { + if (mtime < 0) { return std::string(); } + + struct tm tm_buf; +#ifdef _WIN32 + if (gmtime_s(&tm_buf, &mtime) != 0) { return std::string(); } +#else + if (gmtime_r(&mtime, &tm_buf) == nullptr) { return std::string(); } +#endif + char buf[64]; + if (strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tm_buf) == 0) { + return std::string(); + } + + return std::string(buf); +} + +// Parse HTTP-date (RFC 9110 Section 5.6.7) to time_t. Returns -1 on failure. +inline time_t parse_http_date(const std::string &date_str) { + struct tm tm_buf; + + // Create a classic locale object once for all parsing attempts + const std::locale classic_locale = std::locale::classic(); + + // Try to parse using std::get_time (C++11, cross-platform) + auto try_parse = [&](const char *fmt) -> bool { + std::istringstream ss(date_str); + ss.imbue(classic_locale); + + memset(&tm_buf, 0, sizeof(tm_buf)); + ss >> std::get_time(&tm_buf, fmt); + + return !ss.fail(); + }; + + // RFC 9110 preferred format (HTTP-date): "Sun, 06 Nov 1994 08:49:37 GMT" + if (!try_parse("%a, %d %b %Y %H:%M:%S")) { + // RFC 850 format: "Sunday, 06-Nov-94 08:49:37 GMT" + if (!try_parse("%A, %d-%b-%y %H:%M:%S")) { + // asctime format: "Sun Nov 6 08:49:37 1994" + if (!try_parse("%a %b %d %H:%M:%S %Y")) { + return static_cast(-1); + } + } + } + +#ifdef _WIN32 + return _mkgmtime(&tm_buf); +#else + return timegm(&tm_buf); +#endif +} + +inline bool is_weak_etag(const std::string &s) { + // Check if the string is a weak ETag (starts with 'W/"') + return s.size() > 3 && s[0] == 'W' && s[1] == '/' && s[2] == '"'; +} + +inline bool is_strong_etag(const std::string &s) { + // Check if the string is a strong ETag (starts and ends with '"', at least 2 + // chars) + return s.size() >= 2 && s[0] == '"' && s.back() == '"'; +} + inline size_t to_utf8(int code, char *buff) { if (code < 0x0080) { buff[0] = static_cast(code & 0x7F); @@ -3090,6 +3171,15 @@ inline bool FileStat::is_dir() const { return ret_ >= 0 && S_ISDIR(st_.st_mode); } +inline time_t FileStat::mtime() const { + return ret_ >= 0 ? static_cast(st_.st_mtime) + : static_cast(-1); +} + +inline size_t FileStat::size() const { + return ret_ >= 0 ? static_cast(st_.st_size) : 0; +} + inline std::string encode_path(const std::string &s) { std::string result; result.reserve(s.size()); @@ -3345,6 +3435,42 @@ inline void split(const char *b, const char *e, char d, size_t m, } } +inline bool split_find(const char *b, const char *e, char d, size_t m, + std::function fn) { + size_t i = 0; + size_t beg = 0; + size_t count = 1; + + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d && count < m) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { + auto found = fn(&b[r.first], &b[r.second]); + if (found) { return true; } + } + beg = i + 1; + count++; + } + i++; + } + + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { + auto found = fn(&b[r.first], &b[r.second]); + if (found) { return true; } + } + } + + return false; +} + +inline bool split_find(const char *b, const char *e, char d, + std::function fn) { + return split_find(b, e, d, (std::numeric_limits::max)(), + std::move(fn)); +} + inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, size_t fixed_buffer_size) : strm_(strm), fixed_buffer_(fixed_buffer), @@ -8256,7 +8382,7 @@ inline bool Server::read_content_core( return true; } -inline bool Server::handle_file_request(const Request &req, Response &res) { +inline bool Server::handle_file_request(Request &req, Response &res) { for (const auto &entry : base_dirs_) { // Prefix match if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { @@ -8277,6 +8403,20 @@ inline bool Server::handle_file_request(const Request &req, Response &res) { res.set_header(kv.first, kv.second); } + auto etag = detail::compute_etag(stat); + if (!etag.empty()) { res.set_header("ETag", etag); } + + auto mtime = stat.mtime(); + + auto last_modified = detail::file_mtime_to_http_date(mtime); + if (!last_modified.empty()) { + res.set_header("Last-Modified", last_modified); + } + + if (check_if_not_modified(req, res, etag, mtime)) { return true; } + + check_if_range(req, etag, mtime); + auto mm = std::make_shared(path.c_str()); if (!mm->is_open()) { output_error_log(Error::OpenFile, &req); @@ -8306,6 +8446,79 @@ inline bool Server::handle_file_request(const Request &req, Response &res) { return false; } +inline bool Server::check_if_not_modified(const Request &req, Response &res, + const std::string &etag, + time_t mtime) const { + // Handle conditional GET: + // 1. If-None-Match takes precedence (RFC 9110 Section 13.1.2) + // 2. If-Modified-Since is checked only when If-None-Match is absent + if (req.has_header("If-None-Match")) { + if (!etag.empty()) { + auto val = req.get_header_value("If-None-Match"); + + // NOTE: We use exact string matching here. This works correctly + // because our server always generates weak ETags (W/"..."), and + // clients typically send back the same ETag they received. + // RFC 9110 Section 8.8.3.2 allows weak comparison for + // If-None-Match, where W/"x" and "x" would match, but this + // simplified implementation requires exact matches. + auto ret = detail::split_find(val.data(), val.data() + val.size(), ',', + [&](const char *b, const char *e) { + return std::equal(b, e, "*") || + std::equal(b, e, etag.begin()); + }); + + if (ret) { + res.status = StatusCode::NotModified_304; + return true; + } + } + } else if (req.has_header("If-Modified-Since")) { + auto val = req.get_header_value("If-Modified-Since"); + auto t = detail::parse_http_date(val); + + if (t != static_cast(-1) && mtime <= t) { + res.status = StatusCode::NotModified_304; + return true; + } + } + return false; +} + +inline bool Server::check_if_range(Request &req, const std::string &etag, + time_t mtime) const { + // Handle If-Range for partial content requests (RFC 9110 + // Section 13.1.5). If-Range is only evaluated when Range header is + // present. If the validator matches, serve partial content; otherwise + // serve full content. + if (!req.ranges.empty() && req.has_header("If-Range")) { + auto val = req.get_header_value("If-Range"); + + auto is_valid_range = [&]() { + if (detail::is_strong_etag(val)) { + // RFC 9110 Section 13.1.5: If-Range requires strong ETag + // comparison. + return (!etag.empty() && val == etag); + } else if (detail::is_weak_etag(val)) { + // Weak ETags are not valid for If-Range (RFC 9110 Section 13.1.5) + return false; + } else { + // HTTP-date comparison + auto t = detail::parse_http_date(val); + return (t != static_cast(-1) && mtime <= t); + } + }; + + if (!is_valid_range()) { + // Validator doesn't match: ignore Range and serve full content + req.ranges.clear(); + return false; + } + } + + return true; +} + inline socket_t Server::create_server_socket(const std::string &host, int port, int socket_flags, @@ -8573,10 +8786,13 @@ inline void Server::apply_ranges(const Request &req, Response &res, res.set_header("Transfer-Encoding", "chunked"); if (type == detail::EncodingType::Gzip) { res.set_header("Content-Encoding", "gzip"); + res.set_header("Vary", "Accept-Encoding"); } else if (type == detail::EncodingType::Brotli) { res.set_header("Content-Encoding", "br"); + res.set_header("Vary", "Accept-Encoding"); } else if (type == detail::EncodingType::Zstd) { res.set_header("Content-Encoding", "zstd"); + res.set_header("Vary", "Accept-Encoding"); } } } @@ -8635,6 +8851,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, })) { res.body.swap(compressed); res.set_header("Content-Encoding", content_encoding); + res.set_header("Vary", "Accept-Encoding"); } } } diff --git a/test/test.cc b/test/test.cc index b6e93d9101..9001aaee5b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4,9 +4,11 @@ #ifndef _WIN32 #include +#include #include #include #include +#include #include #endif #include @@ -12687,3 +12689,536 @@ TEST(ErrorHandlingTest, SSLStreamConnectionClosed) { t.join(); } #endif + +TEST(ETagTest, StaticFileETagAndIfNoneMatch) { + using namespace httplib; + + // Create a test file + const char *fname = "etag_testfile.txt"; + const char *content = "etag-content"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8087); }); + svr.wait_until_ready(); + + Client cli("localhost", 8087); + + // First request: should get 200 with ETag header + auto res1 = cli.Get("/static/etag_testfile.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("ETag")); + std::string etag = res1->get_header_value("ETag"); + EXPECT_FALSE(etag.empty()); + + // Verify ETag format: W/"hex-hex" + ASSERT_GE(etag.length(), 5u); // Minimum: W/"" + EXPECT_EQ('W', etag[0]); + EXPECT_EQ('/', etag[1]); + EXPECT_EQ('"', etag[2]); + EXPECT_EQ('"', etag.back()); + + // Exact match: expect 304 Not Modified + Headers h2 = {{"If-None-Match", etag}}; + auto res2 = cli.Get("/static/etag_testfile.txt", h2); + ASSERT_TRUE(res2); + EXPECT_EQ(304, res2->status); + + // Wildcard match: expect 304 Not Modified + Headers h3 = {{"If-None-Match", "*"}}; + auto res3 = cli.Get("/static/etag_testfile.txt", h3); + ASSERT_TRUE(res3); + EXPECT_EQ(304, res3->status); + + // Non-matching ETag: expect 200 + Headers h4 = {{"If-None-Match", "W/\"deadbeef\""}}; + auto res4 = cli.Get("/static/etag_testfile.txt", h4); + ASSERT_TRUE(res4); + EXPECT_EQ(200, res4->status); + + // Multiple ETags with one matching: expect 304 + Headers h5 = {{"If-None-Match", "W/\"other\", " + etag + ", W/\"another\""}}; + auto res5 = cli.Get("/static/etag_testfile.txt", h5); + ASSERT_TRUE(res5); + EXPECT_EQ(304, res5->status); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, StaticFileETagIfNoneMatchStarNotFound) { + using namespace httplib; + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8090); }); + svr.wait_until_ready(); + + Client cli("localhost", 8090); + + // Send If-None-Match: * to a non-existent file + Headers h = {{"If-None-Match", "*"}}; + auto res = cli.Get("/static/etag_testfile_notfound.txt", h); + ASSERT_TRUE(res); + EXPECT_EQ(404, res->status); + + svr.stop(); + t.join(); +} + +TEST(ETagTest, LastModifiedAndIfModifiedSince) { + using namespace httplib; + + // Create a test file + const char *fname = "ims_testfile.txt"; + const char *content = "if-modified-since-test"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8088); }); + svr.wait_until_ready(); + + Client cli("localhost", 8088); + + // First request: should get 200 with Last-Modified header + auto res1 = cli.Get("/static/ims_testfile.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("Last-Modified")); + std::string last_modified = res1->get_header_value("Last-Modified"); + EXPECT_FALSE(last_modified.empty()); + + // If-Modified-Since with same time: expect 304 + Headers h2 = {{"If-Modified-Since", last_modified}}; + auto res2 = cli.Get("/static/ims_testfile.txt", h2); + ASSERT_TRUE(res2); + EXPECT_EQ(304, res2->status); + + // If-Modified-Since with future time: expect 304 + Headers h3 = {{"If-Modified-Since", "Sun, 01 Jan 2099 00:00:00 GMT"}}; + auto res3 = cli.Get("/static/ims_testfile.txt", h3); + ASSERT_TRUE(res3); + EXPECT_EQ(304, res3->status); + + // If-Modified-Since with past time: expect 200 + Headers h4 = {{"If-Modified-Since", "Sun, 01 Jan 2000 00:00:00 GMT"}}; + auto res4 = cli.Get("/static/ims_testfile.txt", h4); + ASSERT_TRUE(res4); + EXPECT_EQ(200, res4->status); + + // If-None-Match takes precedence over If-Modified-Since + // (send matching ETag with old If-Modified-Since -> should still be 304) + ASSERT_TRUE(res1->has_header("ETag")); + std::string etag = res1->get_header_value("ETag"); + Headers h5 = {{"If-None-Match", etag}, + {"If-Modified-Since", "Sun, 01 Jan 2000 00:00:00 GMT"}}; + auto res5 = cli.Get("/static/ims_testfile.txt", h5); + ASSERT_TRUE(res5); + EXPECT_EQ(304, res5->status); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, VaryAcceptEncodingWithCompression) { + using namespace httplib; + + Server svr; + + // Endpoint that returns compressible content + svr.Get("/compressible", [](const Request &, Response &res) { + // Return a large enough body to trigger compression + std::string body(1000, 'a'); + res.set_content(body, "text/plain"); + }); + + auto t = std::thread([&]() { svr.listen("localhost", 8089); }); + svr.wait_until_ready(); + + Client cli("localhost", 8089); + + // Request with gzip support: should get Vary header when compressed + cli.set_compress(true); + auto res1 = cli.Get("/compressible"); + ASSERT_TRUE(res1); + EXPECT_EQ(200, res1->status); + + // If Content-Encoding is set, Vary should also be set + if (res1->has_header("Content-Encoding")) { + EXPECT_TRUE(res1->has_header("Vary")); + EXPECT_EQ("Accept-Encoding", res1->get_header_value("Vary")); + } + + // Request without Accept-Encoding header: should not have compression + Headers h_no_compress; + auto res2 = cli.Get("/compressible", h_no_compress); + ASSERT_TRUE(res2); + EXPECT_EQ(200, res2->status); + + // Verify Vary header is present when compression is applied + // (the exact behavior depends on server configuration) + + svr.stop(); + t.join(); +} + +TEST(ETagTest, IfRangeWithETag) { + using namespace httplib; + + // Create a test file with known content + const char *fname = "if_range_testfile.txt"; + const std::string content = "0123456789ABCDEFGHIJ"; // 20 bytes + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8090); }); + svr.wait_until_ready(); + + Client cli("localhost", 8090); + + // First request: get ETag + auto res1 = cli.Get("/static/if_range_testfile.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("ETag")); + std::string etag = res1->get_header_value("ETag"); + + // RFC 9110 Section 13.1.5: If-Range requires strong ETag comparison. + // Since our server generates weak ETags (W/"..."), If-Range with our + // ETag should NOT result in partial content - it should return full content. + Headers h2 = {{"Range", "bytes=0-4"}, {"If-Range", etag}}; + auto res2 = cli.Get("/static/if_range_testfile.txt", h2); + ASSERT_TRUE(res2); + // Weak ETag in If-Range -> full content (200), not partial (206) + EXPECT_EQ(200, res2->status); + EXPECT_EQ(content, res2->body); + EXPECT_FALSE(res2->has_header("Content-Range")); + + // Range request with non-matching If-Range (ETag): should get 200 (full + // content) + Headers h3 = {{"Range", "bytes=0-4"}, {"If-Range", "W/\"wrong-etag\""}}; + auto res3 = cli.Get("/static/if_range_testfile.txt", h3); + ASSERT_TRUE(res3); + EXPECT_EQ(200, res3->status); + EXPECT_EQ(content, res3->body); + EXPECT_FALSE(res3->has_header("Content-Range")); + + // Range request with strong ETag (hypothetical - our server doesn't generate + // strong ETags, but if client sends a strong ETag that doesn't match, it + // should return full content) + Headers h4 = {{"Range", "bytes=0-4"}, {"If-Range", "\"strong-etag\""}}; + auto res4 = cli.Get("/static/if_range_testfile.txt", h4); + ASSERT_TRUE(res4); + EXPECT_EQ(200, res4->status); + EXPECT_EQ(content, res4->body); + EXPECT_FALSE(res4->has_header("Content-Range")); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, IfRangeWithDate) { + using namespace httplib; + + // Create a test file + const char *fname = "if_range_date_testfile.txt"; + const std::string content = "ABCDEFGHIJ0123456789"; // 20 bytes + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8091); }); + svr.wait_until_ready(); + + Client cli("localhost", 8091); + + // First request: get Last-Modified + auto res1 = cli.Get("/static/if_range_date_testfile.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("Last-Modified")); + std::string last_modified = res1->get_header_value("Last-Modified"); + + // Range request with matching If-Range (date): should get 206 + Headers h2 = {{"Range", "bytes=5-9"}, {"If-Range", last_modified}}; + auto res2 = cli.Get("/static/if_range_date_testfile.txt", h2); + ASSERT_TRUE(res2); + EXPECT_EQ(206, res2->status); + EXPECT_EQ("FGHIJ", res2->body); + + // Range request with old If-Range date: should get 200 (full content) + Headers h3 = {{"Range", "bytes=5-9"}, + {"If-Range", "Sun, 01 Jan 2000 00:00:00 GMT"}}; + auto res3 = cli.Get("/static/if_range_date_testfile.txt", h3); + ASSERT_TRUE(res3); + EXPECT_EQ(200, res3->status); + EXPECT_EQ(content, res3->body); + + // Range request with future If-Range date: should get 206 + Headers h4 = {{"Range", "bytes=0-4"}, + {"If-Range", "Sun, 01 Jan 2099 00:00:00 GMT"}}; + auto res4 = cli.Get("/static/if_range_date_testfile.txt", h4); + ASSERT_TRUE(res4); + EXPECT_EQ(206, res4->status); + EXPECT_EQ("ABCDE", res4->body); + + svr.stop(); + t.join(); + std::remove(fname); +} +TEST(ETagTest, MalformedIfNoneMatchAndWhitespace) { + using namespace httplib; + + const char *fname = "etag_malformed.txt"; + const char *content = "malformed-etag"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8092); }); + svr.wait_until_ready(); + + Client cli("localhost", 8092); + + // baseline: should get 200 and an ETag + auto res1 = cli.Get("/static/etag_malformed.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("ETag")); + + // Malformed ETag value (missing quotes) should be treated as non-matching + Headers h_bad = {{"If-None-Match", "W/noquotes"}}; + auto res_bad = cli.Get("/static/etag_malformed.txt", h_bad); + ASSERT_TRUE(res_bad); + EXPECT_EQ(200, res_bad->status); + + // Whitespace-only header value should be considered invalid / non-matching + Headers h_space = {{"If-None-Match", " "}}; + auto res_space = cli.Get("/static/etag_malformed.txt", h_space); + ASSERT_TRUE(res_space); + EXPECT_EQ(200, res_space->status); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, InvalidIfModifiedSinceAndIfRangeDate) { + using namespace httplib; + + const char *fname = "ims_invalid_format.txt"; + const char *content = "ims-bad-format"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8093); }); + svr.wait_until_ready(); + + Client cli("localhost", 8093); + + auto res1 = cli.Get("/static/ims_invalid_format.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("Last-Modified")); + + // If-Modified-Since with invalid format should not result in 304 + Headers h_bad_date = {{"If-Modified-Since", "not-a-valid-date"}}; + auto res_bad = cli.Get("/static/ims_invalid_format.txt", h_bad_date); + ASSERT_TRUE(res_bad); + EXPECT_EQ(200, res_bad->status); + + // If-Range with invalid date format should be treated as mismatch -> full + // content (200) + Headers h_ifrange_bad = {{"Range", "bytes=0-3"}, + {"If-Range", "invalid-date"}}; + auto res_ifrange = cli.Get("/static/ims_invalid_format.txt", h_ifrange_bad); + ASSERT_TRUE(res_ifrange); + EXPECT_EQ(200, res_ifrange->status); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, IfRangeWithMalformedETag) { + using namespace httplib; + + const char *fname = "ifrange_malformed.txt"; + const std::string content = "0123456789"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8094); }); + svr.wait_until_ready(); + + Client cli("localhost", 8094); + + // First request: get ETag + auto res1 = cli.Get("/static/ifrange_malformed.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("ETag")); + + // If-Range with malformed ETag (no quotes) should be treated as mismatch -> + // full content (200) + Headers h_malformed = {{"Range", "bytes=0-4"}, {"If-Range", "W/noquotes"}}; + auto res2 = cli.Get("/static/ifrange_malformed.txt", h_malformed); + ASSERT_TRUE(res2); + EXPECT_EQ(200, res2->status); + EXPECT_EQ(content, res2->body); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, ExtremeLargeDateValues) { + using namespace httplib; + + const char *fname = "ims_extreme_date.txt"; + const char *content = "ims-extreme-date"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8095); }); + svr.wait_until_ready(); + + Client cli("localhost", 8095); + + auto res1 = cli.Get(std::string("/static/") + fname); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("Last-Modified")); + + // Extremely large year that may overflow date parsing routines. + Headers h_large_date = { + {"If-Modified-Since", "Sun, 01 Jan 99999 00:00:00 GMT"}}; + auto res_bad = cli.Get(std::string("/static/") + fname, h_large_date); + ASSERT_TRUE(res_bad); + // Expect server to treat this as invalid/mismatch and return full content + EXPECT_EQ(200, res_bad->status); + + // If-Range with extremely large date should be treated as mismatch -> full + // content (200) + Headers h_ifrange_large = {{"Range", "bytes=0-3"}, + {"If-Range", "Sun, 01 Jan 99999 00:00:00 GMT"}}; + auto res_ifrange = cli.Get(std::string("/static/") + fname, h_ifrange_large); + ASSERT_TRUE(res_ifrange); + EXPECT_EQ(200, res_ifrange->status); + + svr.stop(); + t.join(); + std::remove(fname); +} + +TEST(ETagTest, NegativeFileModificationTime) { + using namespace httplib; + + const char *fname = "ims_negative_mtime.txt"; + const std::string content = "negative-mtime"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + // Try to set file mtime to a negative value. This may fail on some + // platforms/filesystems; if it fails, the test will still verify server + // behaves safely by performing a regular conditional request. +#if defined(__APPLE__) || defined(__linux__) + bool set_negative = false; + do { + struct timeval times[2]; + // access time: now + times[0].tv_sec = time(nullptr); + times[0].tv_usec = 0; + // modification time: negative (e.g., -1) + times[1].tv_sec = -1; + times[1].tv_usec = 0; + if (utimes(fname, times) == 0) { set_negative = true; } + } while (0); +#else + bool set_negative = false; +#endif + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", 8096); }); + svr.wait_until_ready(); + + Client cli("localhost", 8096); + + auto res1 = cli.Get(std::string("/static/") + fname); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + bool has_last_modified = res1->has_header("Last-Modified"); + std::string last_modified; + if (has_last_modified) { + last_modified = res1->get_header_value("Last-Modified"); + } + + if (set_negative) { + // If we successfully set a negative mtime, ensure server returns a + // Last-Modified string (may be empty or normalized). Send If-Modified-Since + // with an old date and ensure server handles it without crash. + Headers h_old = {{"If-Modified-Since", "Sun, 01 Jan 1970 00:00:00 GMT"}}; + auto res2 = cli.Get(std::string("/static/") + fname, h_old); + ASSERT_TRUE(res2); + // Behavior may vary; at minimum ensure server responds (200 or 304). + EXPECT_TRUE(res2->status == 200 || res2->status == 304); + } else { + // Could not set negative mtime on this platform; fall back to verifying + // that normal invalid/malformed dates are treated safely (non-304). + Headers h_bad_date = { + {"If-Modified-Since", "Sun, 01 Jan 99999 00:00:00 GMT"}}; + auto res_bad = cli.Get(std::string("/static/") + fname, h_bad_date); + ASSERT_TRUE(res_bad); + EXPECT_EQ(200, res_bad->status); + } + + svr.stop(); + t.join(); + std::remove(fname); +} \ No newline at end of file From c795ad1c326b4b63ef2da40befd5fcaf8b74e1bd Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 5 Dec 2025 21:39:40 -0500 Subject: [PATCH 0549/1222] Fix #2259. Add query string normalization to preserve parameter order in requests --- httplib.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++---- test/test.cc | 34 +++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index 31f6ba3760..49eb719e73 100644 --- a/httplib.h +++ b/httplib.h @@ -5717,6 +5717,38 @@ inline void parse_query_text(const std::string &s, Params ¶ms) { parse_query_text(s.data(), s.size(), params); } +// Normalize a query string by decoding and re-encoding each key/value pair +// while preserving the original parameter order. This avoids double-encoding +// and ensures consistent encoding without reordering (unlike Params which +// uses std::multimap and sorts keys). +inline std::string normalize_query_string(const std::string &query) { + std::string result; + split(query.data(), query.data() + query.size(), '&', + [&](const char *b, const char *e) { + std::string key; + std::string val; + divide(b, static_cast(e - b), '=', + [&](const char *lhs_data, std::size_t lhs_size, + const char *rhs_data, std::size_t rhs_size) { + key.assign(lhs_data, lhs_size); + val.assign(rhs_data, rhs_size); + }); + + if (!key.empty()) { + auto dec_key = decode_query_component(key); + auto dec_val = decode_query_component(val); + + if (!result.empty()) { result += '&'; } + result += encode_query_component(dec_key); + if (!val.empty() || std::find(b, e, '=') != e) { + result += '='; + result += encode_query_component(dec_val); + } + } + }); + return result; +} + inline bool parse_multipart_boundary(const std::string &content_type, std::string &boundary) { auto boundary_keyword = "boundary="; @@ -10204,13 +10236,32 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, query_part = ""; } - // Encode path and query + // Encode path part. If the original `req.path` already contained a + // query component, preserve its raw query string (including parameter + // order) instead of reparsing and reassembling it which may reorder + // parameters due to container ordering (e.g. `Params` uses + // `std::multimap`). When there is no query in `req.path`, fall back to + // building a query from `req.params` so existing callers that pass + // `Params` continue to work. auto path_with_query = path_encode_ ? detail::encode_path(path_part) : path_part; - detail::parse_query_text(query_part, req.params); - if (!req.params.empty()) { - path_with_query = append_query_params(path_with_query, req.params); + if (!query_part.empty()) { + // Normalize the query string (decode then re-encode) while preserving + // the original parameter order. + auto normalized = detail::normalize_query_string(query_part); + if (!normalized.empty()) { path_with_query += '?' + normalized; } + + // Still populate req.params for handlers/users who read them. + detail::parse_query_text(query_part, req.params); + } else { + // No query in path; parse any query_part (empty) and append params + // from `req.params` when present (preserves prior behavior for + // callers who provide Params separately). + detail::parse_query_text(query_part, req.params); + if (!req.params.empty()) { + path_with_query = append_query_params(path_with_query, req.params); + } } // Write request line and headers diff --git a/test/test.cc b/test/test.cc index 9001aaee5b..e8fe6fd596 100644 --- a/test/test.cc +++ b/test/test.cc @@ -399,6 +399,38 @@ TEST(EncodeQueryParamTest, ParseReservedCharactersTest) { "%3B%2C%2F%3F%3A%40%26%3D%2B%24"); } +TEST(ClientQueryOrder, PreserveOrder) { + // This test reproduces Issue #2259: client may reorder query parameters + // when sending a GET request. The expected behavior is that the client + // preserves the original query string order when the caller supplied it + // as part of the path. + Server svr; + svr.Get("/", [&](const Request &req, Response &res) { + // Echo back the raw target so the test can assert ordering + res.set_content(req.target, "text/plain"); + }); + + std::thread t{[&] { svr.listen(HOST, PORT); }}; + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + ASSERT_TRUE(cli.is_valid()); + + const std::string original = "/?z=1&y=2&x=3&c=7&b=8&a=9"; + auto res = cli.Get(original); + ASSERT_TRUE(res); + + // Expect the echoed target to exactly match the original path (order + // preserved) + EXPECT_EQ(res->body, original); +} + TEST(EncodeQueryParamTest, TestUTF8Characters) { string chineseCharacters = u8"中国語"; string russianCharacters = u8"дом"; @@ -13221,4 +13253,4 @@ TEST(ETagTest, NegativeFileModificationTime) { svr.stop(); t.join(); std::remove(fname); -} \ No newline at end of file +} From 87c2b4e584da64d80379e155480eeb954c7f9df5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 8 Dec 2025 19:32:35 -0500 Subject: [PATCH 0550/1222] Fix #2294 --- httplib.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 49eb719e73..d46a2d3b82 100644 --- a/httplib.h +++ b/httplib.h @@ -9206,7 +9206,8 @@ inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), - host_and_port_(detail::make_host_and_port_string(host_, port, is_ssl())), + host_and_port_( + detail::make_host_and_port_string(host_, port, ClientImpl::is_ssl())), client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { From c3613c6977580012aa9acfde8f01c5614a7c2e11 Mon Sep 17 00:00:00 2001 From: Miko <110693261+mikomikotaishi@users.noreply.github.com> Date: Mon, 8 Dec 2025 19:18:01 -0800 Subject: [PATCH 0551/1222] Update the split.py file (#2295) --- split.py | 108 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 51 deletions(-) diff --git a/split.py b/split.py index 4d8b307417..d3682ef42d 100755 --- a/split.py +++ b/split.py @@ -2,66 +2,72 @@ """This script splits httplib.h into .h and .cc parts.""" -import argparse import os import sys +from argparse import ArgumentParser, Namespace +from typing import List -border = '// ----------------------------------------------------------------------------' -args_parser = argparse.ArgumentParser(description=__doc__) -args_parser.add_argument( - "-e", "--extension", help="extension of the implementation file (default: cc)", - default="cc" -) -args_parser.add_argument( - "-o", "--out", help="where to write the files (default: out)", default="out" -) -args = args_parser.parse_args() +def main() -> None: + """Main entry point for the script.""" + BORDER: str = '// ----------------------------------------------------------------------------' -cur_dir = os.path.dirname(sys.argv[0]) -lib_name = 'httplib' -header_name = '/' + lib_name + '.h' -source_name = '/' + lib_name + '.' + args.extension -# get the input file -in_file = cur_dir + header_name -# get the output file -h_out = args.out + header_name -cc_out = args.out + source_name + args_parser: ArgumentParser = ArgumentParser(description=__doc__) + args_parser.add_argument( + "-e", "--extension", help="extension of the implementation file (default: cc)", + default="cc" + ) + args_parser.add_argument( + "-o", "--out", help="where to write the files (default: out)", default="out" + ) + args: Namespace = args_parser.parse_args() -# if the modification time of the out file is after the in file, -# don't split (as it is already finished) -do_split = True + cur_dir: str = os.path.dirname(sys.argv[0]) + if not cur_dir: + cur_dir = '.' + lib_name: str = 'httplib' + header_name: str = f"/{lib_name}.h" + source_name: str = f"/{lib_name}.{args.extension}" + # get the input file + in_file: str = cur_dir + header_name + # get the output file + h_out: str = args.out + header_name + cc_out: str = args.out + source_name -if os.path.exists(h_out): - in_time = os.path.getmtime(in_file) - out_time = os.path.getmtime(h_out) - do_split = in_time > out_time + # if the modification time of the out file is after the in file, + # don't split (as it is already finished) + do_split: bool = True -if do_split: - with open(in_file) as f: - lines = f.readlines() + if os.path.exists(h_out): + in_time: float = os.path.getmtime(in_file) + out_time: float = os.path.getmtime(h_out) + do_split: bool = in_time > out_time + + if do_split: + with open(in_file) as f: + lines: List[str] = f.readlines() - python_version = sys.version_info[0] - if python_version < 3: - os.makedirs(args.out) - else: os.makedirs(args.out, exist_ok=True) - in_implementation = False - cc_out = args.out + source_name - with open(h_out, 'w') as fh, open(cc_out, 'w') as fc: - fc.write('#include "httplib.h"\n') - fc.write('namespace httplib {\n') - for line in lines: - is_border_line = border in line - if is_border_line: - in_implementation = not in_implementation - elif in_implementation: - fc.write(line.replace('inline ', '')) - else: - fh.write(line) - fc.write('} // namespace httplib\n') + in_implementation: bool = False + cc_out: str = args.out + source_name + with open(h_out, 'w') as fh, open(cc_out, 'w') as fc: + fc.write('#include "httplib.h"\n') + fc.write('namespace httplib {\n') + for line in lines: + is_border_line: bool = BORDER in line + if is_border_line: + in_implementation: bool = not in_implementation + elif in_implementation: + fc.write(line.replace('inline ', '')) + else: + fh.write(line) + fc.write('} // namespace httplib\n') + + print(f"Wrote {h_out} and {cc_out}") + else: + print(f"{h_out} and {cc_out} are up to date") + - print("Wrote {} and {}".format(h_out, cc_out)) -else: - print("{} and {} are up to date".format(h_out, cc_out)) +if __name__ == "__main__": + main() From f441cd2a44dd92024100857b5ea5f646bcc50bb2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 9 Dec 2025 22:23:45 -0500 Subject: [PATCH 0552/1222] Use std::move for content_provider in adapter (#2297) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index d46a2d3b82..3c2d406f20 100644 --- a/httplib.h +++ b/httplib.h @@ -6835,7 +6835,7 @@ class ContentProviderAdapter { public: explicit ContentProviderAdapter( ContentProviderWithoutLength &&content_provider) - : content_provider_(content_provider) {} + : content_provider_(std::move(content_provider)) {} bool operator()(size_t offset, size_t, DataSink &sink) { return content_provider_(offset, sink); From c23764269dbcdd5298b477542738438a5c06ba2b Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 9 Dec 2025 22:24:33 -0500 Subject: [PATCH 0553/1222] Use std::move for boundary in set_boundary method (#2298) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3c2d406f20..43cf2b9244 100644 --- a/httplib.h +++ b/httplib.h @@ -5970,7 +5970,7 @@ class FormDataParser { FormDataParser() = default; void set_boundary(std::string &&boundary) { - boundary_ = boundary; + boundary_ = std::move(boundary); dash_boundary_crlf_ = dash_ + boundary_ + crlf_; crlf_dash_boundary_ = crlf_ + dash_ + boundary_; } From f4ecb96e5441fedb6ecf8af67088abcc2122cacc Mon Sep 17 00:00:00 2001 From: Carter Green Date: Thu, 11 Dec 2025 19:12:31 -0600 Subject: [PATCH 0554/1222] Fix linker error on macOS (#2299) --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a8ab0a8d0..7566dcd807 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,14 +269,16 @@ target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC} $ ) -# Always require threads target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} + # Always require threads Threads::Threads # Needed for Windows libs on Mingw, as the pragma comment(lib, "xyz") aren't triggered. $<$:ws2_32> $<$:crypt32> # Needed for API from MacOS Security framework "$<$,$,$>:-framework CoreFoundation -framework Security>" + # Needed for non-blocking getaddrinfo on MacOS + "$<$,$>:-framework CFNetwork>" # Can't put multiple targets in a single generator expression or it bugs out. $<$:Brotli::common> $<$:Brotli::encoder> From bce08e62f96b31cad6f13597c9cdd5219f1a51aa Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 13 Dec 2025 22:49:58 -0500 Subject: [PATCH 0555/1222] Remove unnecessary copies for AcceptEntry (#2303) --- httplib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 43cf2b9244..2ad77fce4c 100644 --- a/httplib.h +++ b/httplib.h @@ -5941,7 +5941,7 @@ inline bool parse_accept_header(const std::string &s, return; } - entries.push_back(accept_entry); + entries.push_back(std::move(accept_entry)); }); // Return false if any invalid entry was found @@ -5958,8 +5958,8 @@ inline bool parse_accept_header(const std::string &s, // Extract sorted media types content_types.reserve(entries.size()); - for (const auto &entry : entries) { - content_types.push_back(entry.media_type); + for (auto &entry : entries) { + content_types.push_back(std::move(entry.media_type)); } return true; From 3401877d3df5eb19ea68d9fc29d15c6847edee03 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 13 Dec 2025 22:52:12 -0500 Subject: [PATCH 0556/1222] Change single char string literals to chars (#2304) --- httplib.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 2ad77fce4c..ccbc3ab897 100644 --- a/httplib.h +++ b/httplib.h @@ -5398,7 +5398,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, inline ssize_t write_request_line(Stream &strm, const std::string &method, const std::string &path) { std::string s = method; - s += " "; + s += ' '; s += path; s += " HTTP/1.1\r\n"; return strm.write(s.data(), s.size()); @@ -5407,7 +5407,7 @@ inline ssize_t write_request_line(Stream &strm, const std::string &method, inline ssize_t write_response_line(Stream &strm, int status) { std::string s = "HTTP/1.1 "; s += std::to_string(status); - s += " "; + s += ' '; s += httplib::status_message(status); s += "\r\n"; return strm.write(s.data(), s.size()); @@ -5682,9 +5682,9 @@ inline std::string params_to_query_str(const Params ¶ms) { std::string query; for (auto it = params.begin(); it != params.end(); ++it) { - if (it != params.begin()) { query += "&"; } + if (it != params.begin()) { query += '&'; } query += encode_query_component(it->first); - query += "="; + query += '='; query += encode_query_component(it->second); } return query; @@ -6443,9 +6443,9 @@ inline std::string make_content_range_header_field( std::string field = "bytes "; field += std::to_string(st); - field += "-"; + field += '-'; field += std::to_string(ed); - field += "/"; + field += '/'; field += std::to_string(content_length); return field; } From ae94d64f670d2a7248f9f9568758cb766105edfe Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 13 Dec 2025 22:52:26 -0500 Subject: [PATCH 0557/1222] Remove another unnecessary string copy (#2305) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ccbc3ab897..3972e384ae 100644 --- a/httplib.h +++ b/httplib.h @@ -6880,7 +6880,7 @@ inline void hosted_at(const std::string &hostname, auto dummy = -1; if (detail::get_ip_and_port(addr, sizeof(struct sockaddr_storage), ip, dummy)) { - addrs.push_back(ip); + addrs.emplace_back(std::move(ip)); } } } From 681d388247bbf91723edaa65823c928f21b3d049 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 13 Dec 2025 22:53:10 -0500 Subject: [PATCH 0558/1222] Use move semantics for auth key and value (#2306) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 3972e384ae..e2d75fee36 100644 --- a/httplib.h +++ b/httplib.h @@ -6822,7 +6822,7 @@ inline bool parse_www_authenticate(const Response &res, static_cast(m.length(2))) : s.substr(static_cast(m.position(3)), static_cast(m.length(3))); - auth[key] = val; + auth[std::move(key)] = std::move(val); } return true; } From b7097f1386f5daf5d59b71b9e61eddc40cdfa305 Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Sat, 13 Dec 2025 23:52:05 -0500 Subject: [PATCH 0559/1222] Replace httpbin.org with httpcan.org (#2300) * Replace httpbin.org with httpcan.org * Fix DigestAuthTest.FromHTTPWatch_Online test --- README.md | 2 +- example/benchmark.cc | 2 +- test/test.cc | 91 ++++++++++++++++++++++++++++++++++---------- test/test_proxy.cc | 30 +++++++-------- 4 files changed, 88 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index b595b63ab6..9c64a17656 100644 --- a/README.md +++ b/README.md @@ -1012,7 +1012,7 @@ cli.set_proxy_bearer_token_auth("pass"); ### Range ```cpp -httplib::Client cli("httpbin.org"); +httplib::Client cli("httpcan.org"); auto res = cli.Get("/range/32", { httplib::make_range_header({{1, 10}}) // 'Range: bytes=1-10' diff --git a/example/benchmark.cc b/example/benchmark.cc index 433cc675c4..93fd7849b4 100644 --- a/example/benchmark.cc +++ b/example/benchmark.cc @@ -21,7 +21,7 @@ struct StopWatch { int main(void) { string body(1024 * 5, 'a'); - httplib::Client cli("httpbin.org", 80); + httplib::Client cli("httpcan.org", 80); for (int i = 0; i < 3; i++) { StopWatch sw(to_string(i).c_str()); diff --git a/test/test.cc b/test/test.cc index e8fe6fd596..f695ab054b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1371,7 +1371,7 @@ TEST_F(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) { TEST(RangeTest, FromHTTPBin_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/range/32"}; #else auto host = "nghttp2.org"; @@ -1473,7 +1473,7 @@ TEST(ConnectionErrorTest, InvalidHost) { } TEST(ConnectionErrorTest, InvalidHost2) { - auto host = "httpbin.org/"; + auto host = "httpcan.org/"; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT SSLClient cli(host); @@ -1488,7 +1488,7 @@ TEST(ConnectionErrorTest, InvalidHost2) { } TEST(ConnectionErrorTest, InvalidHostCheckResultErrorToString) { - auto host = "httpbin.org/"; + auto host = "httpcan.org/"; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT SSLClient cli(host); @@ -1545,7 +1545,7 @@ TEST(ConnectionErrorTest, Timeout_Online) { TEST(CancelTest, NoCancel_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/range/32"}; #else auto host = "nghttp2.org"; @@ -1569,7 +1569,7 @@ TEST(CancelTest, NoCancel_Online) { TEST(CancelTest, WithCancelSmallPayload_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/range/32"}; #else auto host = "nghttp2.org"; @@ -1592,7 +1592,7 @@ TEST(CancelTest, WithCancelSmallPayload_Online) { TEST(CancelTest, WithCancelLargePayload_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/range/65536"}; #else auto host = "nghttp2.org"; @@ -1941,7 +1941,7 @@ static std::string remove_whitespace(const std::string &input) { TEST(BaseAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/basic-auth/hello/world"}; #else auto host = "nghttp2.org"; @@ -1998,13 +1998,12 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(DigestAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto unauth_path = std::string{"/digest-auth/auth/hello/world"}; auto paths = std::vector{ "/digest-auth/auth/hello/world/MD5", "/digest-auth/auth/hello/world/SHA-256", "/digest-auth/auth/hello/world/SHA-512", - "/digest-auth/auth-int/hello/world/MD5", }; #else auto host = "nghttp2.org"; @@ -2032,8 +2031,60 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { for (const auto &path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res); +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN + std::string algo(path.substr(path.rfind('/') + 1)); + EXPECT_EQ( + remove_whitespace("{\"algorithm\":\"" + algo + + "\",\"authenticated\":true,\"user\":\"hello\"}\n"), + remove_whitespace(res->body)); +#else EXPECT_EQ("{\"authenticated\":true,\"user\":\"hello\"}", remove_whitespace(res->body)); +#endif + EXPECT_EQ(StatusCode::OK_200, res->status); + } + +#ifdef CPPHTTPLIB_DEFAULT_HTTPBIN + cli.set_digest_auth("hello", "bad"); + for (const auto &path : paths) { + auto res = cli.Get(path.c_str()); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); + } +#endif + } +} + +#ifndef CPPHTTPLIB_DEFAULT_HTTPBIN +TEST(DigestAuthTest, FromHTTPWatch_Online_HTTPCan) { + auto host = "httpcan.org"; + auto unauth_path = std::string{"/digest-auth/auth/hello/world"}; + auto paths = std::vector{ + "/digest-auth/auth/hello/world/MD5", + "/digest-auth/auth/hello/world/SHA-256", + "/digest-auth/auth/hello/world/SHA-512", + }; + + auto port = 443; + SSLClient cli(host, port); + + { + auto res = cli.Get(unauth_path); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); + } + + { + + cli.set_digest_auth("hello", "world"); + for (const auto &path : paths) { + auto res = cli.Get(path.c_str()); + ASSERT_TRUE(res); + std::string algo(path.substr(path.rfind('/') + 1)); + EXPECT_EQ( + remove_whitespace("{\"algorithm\":\"" + algo + + "\",\"authenticated\":true,\"user\":\"hello\"}\n"), + remove_whitespace(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -2044,18 +2095,18 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } - // NOTE: Until httpbin.org fixes issue #46, the following test is commented - // out. Please see https://httpbin.org/digest-auth/auth/hello/world - // cli.set_digest_auth("bad", "world"); - // for (const auto& path : paths) { - // auto res = cli.Get(path.c_str()); - // ASSERT_TRUE(res); - // EXPECT_EQ(StatusCode::BadRequest_400, res->status); - // } + cli.set_digest_auth("bad", "world"); + for (const auto &path : paths) { + auto res = cli.Get(path.c_str()); + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); + } } } #endif +#endif + TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) { auto host = "google.com"; auto another_host = "example.com"; @@ -7924,7 +7975,7 @@ TEST(GetWithParametersTest, GetWithParameters2) { TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/range/32"}; #else auto host = "nghttp2.org"; @@ -8440,7 +8491,7 @@ TEST(SSLClientTest, UpdateCAStore) { TEST(SSLClientTest, ServerNameIndication_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN - auto host = "httpbin.org"; + auto host = "httpcan.org"; auto path = std::string{"/get"}; #else auto host = "nghttp2.org"; @@ -8684,7 +8735,7 @@ TEST(SSLClientTest, Issue2251_ClientCertFileNotMatchingKey) { #if 0 TEST(SSLClientTest, SetInterfaceWithINET6) { - auto cli = std::make_shared("https://httpbin.org"); + auto cli = std::make_shared("https://httpcan.org"); ASSERT_TRUE(cli != nullptr); cli->set_address_family(AF_INET6); diff --git a/test/test_proxy.cc b/test/test_proxy.cc index 745d5f9882..80239c5f52 100644 --- a/test/test_proxy.cc +++ b/test/test_proxy.cc @@ -153,13 +153,13 @@ template void BaseAuthTestFromHTTPWatch(T &cli) { } TEST(BaseAuthTest, NoSSL) { - Client cli("httpbin.org"); + Client cli("httpcan.org"); BaseAuthTestFromHTTPWatch(cli); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(BaseAuthTest, SSL) { - SSLClient cli("httpbin.org"); + SSLClient cli("httpcan.org"); BaseAuthTestFromHTTPWatch(cli); } #endif @@ -182,15 +182,17 @@ template void DigestAuthTestFromHTTPWatch(T &cli) { "/digest-auth/auth/hello/world/MD5", "/digest-auth/auth/hello/world/SHA-256", "/digest-auth/auth/hello/world/SHA-512", - "/digest-auth/auth-int/hello/world/MD5", }; cli.set_digest_auth("hello", "world"); for (auto path : paths) { auto res = cli.Get(path.c_str()); ASSERT_TRUE(res != nullptr); - EXPECT_EQ(normalizeJson("{\"authenticated\":true,\"user\":\"hello\"}\n"), - normalizeJson(res->body)); + std::string algo(path.substr(path.rfind('/') + 1)); + EXPECT_EQ( + normalizeJson("{\"algorithm\":\"" + algo + + "\",\"authenticated\":true,\"user\":\"hello\"}\n"), + normalizeJson(res->body)); EXPECT_EQ(StatusCode::OK_200, res->status); } @@ -201,24 +203,22 @@ template void DigestAuthTestFromHTTPWatch(T &cli) { EXPECT_EQ(StatusCode::Unauthorized_401, res->status); } - // NOTE: Until httpbin.org fixes issue #46, the following test is commented - // out. Please see https://httpbin.org/digest-auth/auth/hello/world - // cli.set_digest_auth("bad", "world"); - // for (auto path : paths) { - // auto res = cli.Get(path.c_str()); - // ASSERT_TRUE(res != nullptr); - // EXPECT_EQ(StatusCode::Unauthorized_401, res->status); - // } + cli.set_digest_auth("bad", "world"); + for (auto path : paths) { + auto res = cli.Get(path.c_str()); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(StatusCode::Unauthorized_401, res->status); + } } } TEST(DigestAuthTest, SSL) { - SSLClient cli("httpbin.org"); + SSLClient cli("httpcan.org"); DigestAuthTestFromHTTPWatch(cli); } TEST(DigestAuthTest, NoSSL) { - Client cli("httpbin.org"); + Client cli("httpcan.org"); DigestAuthTestFromHTTPWatch(cli); } #endif From 2de4c59bc20d2a81e3fb1fa0c859b259e8772b0d Mon Sep 17 00:00:00 2001 From: Jean-Francois Simoneau Date: Sat, 13 Dec 2025 23:55:58 -0500 Subject: [PATCH 0560/1222] Add Zstd support through meson (#2293) * Add Zstd support through meson * Add libzstd-dev to abitest --- .github/workflows/abidiff.yaml | 1 + meson.build | 6 ++++++ meson_options.txt | 1 + 3 files changed, 8 insertions(+) diff --git a/.github/workflows/abidiff.yaml b/.github/workflows/abidiff.yaml index 186e4fcc8b..84ead93d3c 100644 --- a/.github/workflows/abidiff.yaml +++ b/.github/workflows/abidiff.yaml @@ -29,6 +29,7 @@ jobs: git libbrotli-dev libssl-dev + libzstd-dev meson pkg-config python3 diff --git a/meson.build b/meson.build index 7e95bdea96..14aab1ade0 100644 --- a/meson.build +++ b/meson.build @@ -74,6 +74,12 @@ if brotli_found_all args += '-DCPPHTTPLIB_BROTLI_SUPPORT' endif +zstd_dep = dependency('libzstd', required: get_option('zstd')) +if zstd_dep.found() + deps += zstd_dep + args += '-DCPPHTTPLIB_ZSTD_SUPPORT' +endif + async_ns_opt = get_option('non_blocking_getaddrinfo') if host_machine.system() == 'windows' diff --git a/meson_options.txt b/meson_options.txt index deff537547..7447d8d1c2 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -5,6 +5,7 @@ option('openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support') option('zlib', type: 'feature', value: 'auto', description: 'Enable zlib support') option('brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support') +option('zstd', type: 'feature', value: 'auto', description: 'Enable zstd support') option('macosx_keychain', type: 'feature', value: 'auto', description: 'Enable loading certs from the Keychain on Apple devices') option('non_blocking_getaddrinfo', type: 'feature', value: 'auto', description: 'Enable asynchronous name lookup') option('compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires python3)') From 63b07ada433c3679782d40d48da271df84767aae Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 14 Dec 2025 13:54:29 -0500 Subject: [PATCH 0561/1222] Initial plan (#2307) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> From 6a6d4161d1923e2e77a82b1fa3dffc642b8e3617 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 14 Dec 2025 14:45:48 -0500 Subject: [PATCH 0562/1222] Removed DigestAuthTest.FromHTTPWatch_Online_HTTPCan --- test/test.cc | 50 -------------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/test/test.cc b/test/test.cc index f695ab054b..035f0509a0 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2055,56 +2055,6 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) { } } -#ifndef CPPHTTPLIB_DEFAULT_HTTPBIN -TEST(DigestAuthTest, FromHTTPWatch_Online_HTTPCan) { - auto host = "httpcan.org"; - auto unauth_path = std::string{"/digest-auth/auth/hello/world"}; - auto paths = std::vector{ - "/digest-auth/auth/hello/world/MD5", - "/digest-auth/auth/hello/world/SHA-256", - "/digest-auth/auth/hello/world/SHA-512", - }; - - auto port = 443; - SSLClient cli(host, port); - - { - auto res = cli.Get(unauth_path); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::Unauthorized_401, res->status); - } - - { - - cli.set_digest_auth("hello", "world"); - for (const auto &path : paths) { - auto res = cli.Get(path.c_str()); - ASSERT_TRUE(res); - std::string algo(path.substr(path.rfind('/') + 1)); - EXPECT_EQ( - remove_whitespace("{\"algorithm\":\"" + algo + - "\",\"authenticated\":true,\"user\":\"hello\"}\n"), - remove_whitespace(res->body)); - EXPECT_EQ(StatusCode::OK_200, res->status); - } - - cli.set_digest_auth("hello", "bad"); - for (const auto &path : paths) { - auto res = cli.Get(path.c_str()); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::Unauthorized_401, res->status); - } - - cli.set_digest_auth("bad", "world"); - for (const auto &path : paths) { - auto res = cli.Get(path.c_str()); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::Unauthorized_401, res->status); - } - } -} -#endif - #endif TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) { From 7eb03e81fcca829dabf007f9e6a4d961872f404d Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 14 Dec 2025 17:41:11 -0500 Subject: [PATCH 0563/1222] Refactoring --- httplib.h | 368 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 193 insertions(+), 175 deletions(-) diff --git a/httplib.h b/httplib.h index e2d75fee36..5e34915958 100644 --- a/httplib.h +++ b/httplib.h @@ -1015,6 +1015,10 @@ void default_socket_options(socket_t sock); const char *status_message(int status); +std::string to_string(Error error); + +std::ostream &operator<<(std::ostream &os, const Error &obj); + std::string get_bearer_token_auth(const Request &req); namespace detail { @@ -2307,82 +2311,6 @@ inline void default_socket_options(socket_t sock) { 1); } -inline const char *status_message(int status) { - switch (status) { - case StatusCode::Continue_100: return "Continue"; - case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; - case StatusCode::Processing_102: return "Processing"; - case StatusCode::EarlyHints_103: return "Early Hints"; - case StatusCode::OK_200: return "OK"; - case StatusCode::Created_201: return "Created"; - case StatusCode::Accepted_202: return "Accepted"; - case StatusCode::NonAuthoritativeInformation_203: - return "Non-Authoritative Information"; - case StatusCode::NoContent_204: return "No Content"; - case StatusCode::ResetContent_205: return "Reset Content"; - case StatusCode::PartialContent_206: return "Partial Content"; - case StatusCode::MultiStatus_207: return "Multi-Status"; - case StatusCode::AlreadyReported_208: return "Already Reported"; - case StatusCode::IMUsed_226: return "IM Used"; - case StatusCode::MultipleChoices_300: return "Multiple Choices"; - case StatusCode::MovedPermanently_301: return "Moved Permanently"; - case StatusCode::Found_302: return "Found"; - case StatusCode::SeeOther_303: return "See Other"; - case StatusCode::NotModified_304: return "Not Modified"; - case StatusCode::UseProxy_305: return "Use Proxy"; - case StatusCode::unused_306: return "unused"; - case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; - case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; - case StatusCode::BadRequest_400: return "Bad Request"; - case StatusCode::Unauthorized_401: return "Unauthorized"; - case StatusCode::PaymentRequired_402: return "Payment Required"; - case StatusCode::Forbidden_403: return "Forbidden"; - case StatusCode::NotFound_404: return "Not Found"; - case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; - case StatusCode::NotAcceptable_406: return "Not Acceptable"; - case StatusCode::ProxyAuthenticationRequired_407: - return "Proxy Authentication Required"; - case StatusCode::RequestTimeout_408: return "Request Timeout"; - case StatusCode::Conflict_409: return "Conflict"; - case StatusCode::Gone_410: return "Gone"; - case StatusCode::LengthRequired_411: return "Length Required"; - case StatusCode::PreconditionFailed_412: return "Precondition Failed"; - case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; - case StatusCode::UriTooLong_414: return "URI Too Long"; - case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; - case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; - case StatusCode::ExpectationFailed_417: return "Expectation Failed"; - case StatusCode::ImATeapot_418: return "I'm a teapot"; - case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; - case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; - case StatusCode::Locked_423: return "Locked"; - case StatusCode::FailedDependency_424: return "Failed Dependency"; - case StatusCode::TooEarly_425: return "Too Early"; - case StatusCode::UpgradeRequired_426: return "Upgrade Required"; - case StatusCode::PreconditionRequired_428: return "Precondition Required"; - case StatusCode::TooManyRequests_429: return "Too Many Requests"; - case StatusCode::RequestHeaderFieldsTooLarge_431: - return "Request Header Fields Too Large"; - case StatusCode::UnavailableForLegalReasons_451: - return "Unavailable For Legal Reasons"; - case StatusCode::NotImplemented_501: return "Not Implemented"; - case StatusCode::BadGateway_502: return "Bad Gateway"; - case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; - case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; - case StatusCode::HttpVersionNotSupported_505: - return "HTTP Version Not Supported"; - case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; - case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; - case StatusCode::LoopDetected_508: return "Loop Detected"; - case StatusCode::NotExtended_510: return "Not Extended"; - case StatusCode::NetworkAuthenticationRequired_511: - return "Network Authentication Required"; - - default: - case StatusCode::InternalServerError_500: return "Internal Server Error"; - } -} - inline std::string get_bearer_token_auth(const Request &req) { if (req.has_header("Authorization")) { constexpr auto bearer_header_prefix_len = detail::str_len("Bearer "); @@ -2416,57 +2344,6 @@ Server::set_idle_interval(const std::chrono::duration &duration) { return *this; } -inline std::string to_string(const Error error) { - switch (error) { - case Error::Success: return "Success (no error)"; - case Error::Unknown: return "Unknown"; - case Error::Connection: return "Could not establish connection"; - case Error::BindIPAddress: return "Failed to bind IP address"; - case Error::Read: return "Failed to read connection"; - case Error::Write: return "Failed to write connection"; - case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; - case Error::Canceled: return "Connection handling canceled"; - case Error::SSLConnection: return "SSL connection failed"; - case Error::SSLLoadingCerts: return "SSL certificate loading failed"; - case Error::SSLServerVerification: return "SSL server verification failed"; - case Error::SSLServerHostnameVerification: - return "SSL server hostname verification failed"; - case Error::UnsupportedMultipartBoundaryChars: - return "Unsupported HTTP multipart boundary characters"; - case Error::Compression: return "Compression failed"; - case Error::ConnectionTimeout: return "Connection timed out"; - case Error::ProxyConnection: return "Proxy connection failed"; - case Error::ConnectionClosed: return "Connection closed by server"; - case Error::Timeout: return "Read timeout"; - case Error::ResourceExhaustion: return "Resource exhaustion"; - case Error::TooManyFormDataFiles: return "Too many form data files"; - case Error::ExceedMaxPayloadSize: return "Exceeded maximum payload size"; - case Error::ExceedUriMaxLength: return "Exceeded maximum URI length"; - case Error::ExceedMaxSocketDescriptorCount: - return "Exceeded maximum socket descriptor count"; - case Error::InvalidRequestLine: return "Invalid request line"; - case Error::InvalidHTTPMethod: return "Invalid HTTP method"; - case Error::InvalidHTTPVersion: return "Invalid HTTP version"; - case Error::InvalidHeaders: return "Invalid headers"; - case Error::MultipartParsing: return "Multipart parsing failed"; - case Error::OpenFile: return "Failed to open file"; - case Error::Listen: return "Failed to listen on socket"; - case Error::GetSockName: return "Failed to get socket name"; - case Error::UnsupportedAddressFamily: return "Unsupported address family"; - case Error::HTTPParsing: return "HTTP parsing failed"; - case Error::InvalidRangeHeader: return "Invalid Range header"; - default: break; - } - - return "Invalid"; -} - -inline std::ostream &operator<<(std::ostream &os, const Error &obj) { - os << to_string(obj); - os << " (" << static_cast::type>(obj) << ')'; - return os; -} - inline size_t Result::get_request_header_value_u64(const std::string &key, size_t def, size_t id) const { @@ -2866,54 +2743,14 @@ class mmap { // NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 namespace fields { -inline bool is_token_char(char c) { - return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || - c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || - c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; -} - -inline bool is_token(const std::string &s) { - if (s.empty()) { return false; } - for (auto c : s) { - if (!is_token_char(c)) { return false; } - } - return true; -} - -inline bool is_field_name(const std::string &s) { return is_token(s); } - -inline bool is_vchar(char c) { return c >= 33 && c <= 126; } - -inline bool is_obs_text(char c) { return 128 <= static_cast(c); } - -inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } - -inline bool is_field_content(const std::string &s) { - if (s.empty()) { return true; } - - if (s.size() == 1) { - return is_field_vchar(s[0]); - } else if (s.size() == 2) { - return is_field_vchar(s[0]) && is_field_vchar(s[1]); - } else { - size_t i = 0; - - if (!is_field_vchar(s[i])) { return false; } - i++; - - while (i < s.size() - 1) { - auto c = s[i++]; - if (c == ' ' || c == '\t' || is_field_vchar(c)) { - } else { - return false; - } - } - - return is_field_vchar(s[i]); - } -} - -inline bool is_field_value(const std::string &s) { return is_field_content(s); } +bool is_token_char(char c); +bool is_token(const std::string &s); +bool is_field_name(const std::string &s); +bool is_vchar(char c); +bool is_obs_text(char c); +bool is_field_vchar(char c); +bool is_field_content(const std::string &s); +bool is_field_value(const std::string &s); } // namespace fields @@ -6845,8 +6682,189 @@ class ContentProviderAdapter { ContentProviderWithoutLength content_provider_; }; +// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 +namespace fields { + +inline bool is_token_char(char c) { + return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || + c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || + c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; +} + +inline bool is_token(const std::string &s) { + if (s.empty()) { return false; } + for (auto c : s) { + if (!is_token_char(c)) { return false; } + } + return true; +} + +inline bool is_field_name(const std::string &s) { return is_token(s); } + +inline bool is_vchar(char c) { return c >= 33 && c <= 126; } + +inline bool is_obs_text(char c) { return 128 <= static_cast(c); } + +inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } + +inline bool is_field_content(const std::string &s) { + if (s.empty()) { return true; } + + if (s.size() == 1) { + return is_field_vchar(s[0]); + } else if (s.size() == 2) { + return is_field_vchar(s[0]) && is_field_vchar(s[1]); + } else { + size_t i = 0; + + if (!is_field_vchar(s[i])) { return false; } + i++; + + while (i < s.size() - 1) { + auto c = s[i++]; + if (c == ' ' || c == '\t' || is_field_vchar(c)) { + } else { + return false; + } + } + + return is_field_vchar(s[i]); + } +} + +inline bool is_field_value(const std::string &s) { return is_field_content(s); } + +} // namespace fields + } // namespace detail +inline const char *status_message(int status) { + switch (status) { + case StatusCode::Continue_100: return "Continue"; + case StatusCode::SwitchingProtocol_101: return "Switching Protocol"; + case StatusCode::Processing_102: return "Processing"; + case StatusCode::EarlyHints_103: return "Early Hints"; + case StatusCode::OK_200: return "OK"; + case StatusCode::Created_201: return "Created"; + case StatusCode::Accepted_202: return "Accepted"; + case StatusCode::NonAuthoritativeInformation_203: + return "Non-Authoritative Information"; + case StatusCode::NoContent_204: return "No Content"; + case StatusCode::ResetContent_205: return "Reset Content"; + case StatusCode::PartialContent_206: return "Partial Content"; + case StatusCode::MultiStatus_207: return "Multi-Status"; + case StatusCode::AlreadyReported_208: return "Already Reported"; + case StatusCode::IMUsed_226: return "IM Used"; + case StatusCode::MultipleChoices_300: return "Multiple Choices"; + case StatusCode::MovedPermanently_301: return "Moved Permanently"; + case StatusCode::Found_302: return "Found"; + case StatusCode::SeeOther_303: return "See Other"; + case StatusCode::NotModified_304: return "Not Modified"; + case StatusCode::UseProxy_305: return "Use Proxy"; + case StatusCode::unused_306: return "unused"; + case StatusCode::TemporaryRedirect_307: return "Temporary Redirect"; + case StatusCode::PermanentRedirect_308: return "Permanent Redirect"; + case StatusCode::BadRequest_400: return "Bad Request"; + case StatusCode::Unauthorized_401: return "Unauthorized"; + case StatusCode::PaymentRequired_402: return "Payment Required"; + case StatusCode::Forbidden_403: return "Forbidden"; + case StatusCode::NotFound_404: return "Not Found"; + case StatusCode::MethodNotAllowed_405: return "Method Not Allowed"; + case StatusCode::NotAcceptable_406: return "Not Acceptable"; + case StatusCode::ProxyAuthenticationRequired_407: + return "Proxy Authentication Required"; + case StatusCode::RequestTimeout_408: return "Request Timeout"; + case StatusCode::Conflict_409: return "Conflict"; + case StatusCode::Gone_410: return "Gone"; + case StatusCode::LengthRequired_411: return "Length Required"; + case StatusCode::PreconditionFailed_412: return "Precondition Failed"; + case StatusCode::PayloadTooLarge_413: return "Payload Too Large"; + case StatusCode::UriTooLong_414: return "URI Too Long"; + case StatusCode::UnsupportedMediaType_415: return "Unsupported Media Type"; + case StatusCode::RangeNotSatisfiable_416: return "Range Not Satisfiable"; + case StatusCode::ExpectationFailed_417: return "Expectation Failed"; + case StatusCode::ImATeapot_418: return "I'm a teapot"; + case StatusCode::MisdirectedRequest_421: return "Misdirected Request"; + case StatusCode::UnprocessableContent_422: return "Unprocessable Content"; + case StatusCode::Locked_423: return "Locked"; + case StatusCode::FailedDependency_424: return "Failed Dependency"; + case StatusCode::TooEarly_425: return "Too Early"; + case StatusCode::UpgradeRequired_426: return "Upgrade Required"; + case StatusCode::PreconditionRequired_428: return "Precondition Required"; + case StatusCode::TooManyRequests_429: return "Too Many Requests"; + case StatusCode::RequestHeaderFieldsTooLarge_431: + return "Request Header Fields Too Large"; + case StatusCode::UnavailableForLegalReasons_451: + return "Unavailable For Legal Reasons"; + case StatusCode::NotImplemented_501: return "Not Implemented"; + case StatusCode::BadGateway_502: return "Bad Gateway"; + case StatusCode::ServiceUnavailable_503: return "Service Unavailable"; + case StatusCode::GatewayTimeout_504: return "Gateway Timeout"; + case StatusCode::HttpVersionNotSupported_505: + return "HTTP Version Not Supported"; + case StatusCode::VariantAlsoNegotiates_506: return "Variant Also Negotiates"; + case StatusCode::InsufficientStorage_507: return "Insufficient Storage"; + case StatusCode::LoopDetected_508: return "Loop Detected"; + case StatusCode::NotExtended_510: return "Not Extended"; + case StatusCode::NetworkAuthenticationRequired_511: + return "Network Authentication Required"; + + default: + case StatusCode::InternalServerError_500: return "Internal Server Error"; + } +} + +inline std::string to_string(const Error error) { + switch (error) { + case Error::Success: return "Success (no error)"; + case Error::Unknown: return "Unknown"; + case Error::Connection: return "Could not establish connection"; + case Error::BindIPAddress: return "Failed to bind IP address"; + case Error::Read: return "Failed to read connection"; + case Error::Write: return "Failed to write connection"; + case Error::ExceedRedirectCount: return "Maximum redirect count exceeded"; + case Error::Canceled: return "Connection handling canceled"; + case Error::SSLConnection: return "SSL connection failed"; + case Error::SSLLoadingCerts: return "SSL certificate loading failed"; + case Error::SSLServerVerification: return "SSL server verification failed"; + case Error::SSLServerHostnameVerification: + return "SSL server hostname verification failed"; + case Error::UnsupportedMultipartBoundaryChars: + return "Unsupported HTTP multipart boundary characters"; + case Error::Compression: return "Compression failed"; + case Error::ConnectionTimeout: return "Connection timed out"; + case Error::ProxyConnection: return "Proxy connection failed"; + case Error::ConnectionClosed: return "Connection closed by server"; + case Error::Timeout: return "Read timeout"; + case Error::ResourceExhaustion: return "Resource exhaustion"; + case Error::TooManyFormDataFiles: return "Too many form data files"; + case Error::ExceedMaxPayloadSize: return "Exceeded maximum payload size"; + case Error::ExceedUriMaxLength: return "Exceeded maximum URI length"; + case Error::ExceedMaxSocketDescriptorCount: + return "Exceeded maximum socket descriptor count"; + case Error::InvalidRequestLine: return "Invalid request line"; + case Error::InvalidHTTPMethod: return "Invalid HTTP method"; + case Error::InvalidHTTPVersion: return "Invalid HTTP version"; + case Error::InvalidHeaders: return "Invalid headers"; + case Error::MultipartParsing: return "Multipart parsing failed"; + case Error::OpenFile: return "Failed to open file"; + case Error::Listen: return "Failed to listen on socket"; + case Error::GetSockName: return "Failed to get socket name"; + case Error::UnsupportedAddressFamily: return "Unsupported address family"; + case Error::HTTPParsing: return "HTTP parsing failed"; + case Error::InvalidRangeHeader: return "Invalid Range header"; + default: break; + } + + return "Invalid"; +} + +inline std::ostream &operator<<(std::ostream &os, const Error &obj) { + os << to_string(obj); + os << " (" << static_cast::type>(obj) << ')'; + return os; +} + inline std::string hosted_at(const std::string &hostname) { std::vector addrs; hosted_at(hostname, addrs); From 51b704b9021eacbf581bc8a6bf8d7187b2676e6c Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 15 Dec 2025 00:00:42 -0500 Subject: [PATCH 0564/1222] Implement SSEClient (#2308) * Implement SSEClient * Fix Windows problem --- README-sse.md | 182 +++ README.md | 26 +- example/Makefile | 7 +- example/ssecli.cc | 46 +- httplib.h | 3851 ++++++++++++++++++++++++--------------------- test/test.cc | 726 +++++++++ 6 files changed, 3041 insertions(+), 1797 deletions(-) create mode 100644 README-sse.md diff --git a/README-sse.md b/README-sse.md new file mode 100644 index 0000000000..62dee37888 --- /dev/null +++ b/README-sse.md @@ -0,0 +1,182 @@ +# SSEClient - Server-Sent Events Client + +A simple, EventSource-like SSE client for C++11. + +## Features + +- **Auto-reconnect**: Automatically reconnects on connection loss +- **Last-Event-ID**: Sends last received ID on reconnect for resumption +- **retry field**: Respects server's reconnect interval +- **Event types**: Supports custom event types via `on_event()` +- **Async support**: Run in background thread with `start_async()` +- **C++11 compatible**: No C++14/17/20 features required + +## Quick Start + +```cpp +httplib::Client cli("http://localhost:8080"); +httplib::sse::SSEClient sse(cli, "/events"); + +sse.on_message([](const httplib::sse::SSEMessage &msg) { + std::cout << "Event: " << msg.event << std::endl; + std::cout << "Data: " << msg.data << std::endl; +}); + +sse.start(); // Blocking, with auto-reconnect +``` + +## API Reference + +### SSEMessage + +```cpp +struct SSEMessage { + std::string event; // Event type (default: "message") + std::string data; // Event payload + std::string id; // Event ID +}; +``` + +### SSEClient + +#### Constructor + +```cpp +// Basic +SSEClient(Client &client, const std::string &path); + +// With custom headers +SSEClient(Client &client, const std::string &path, const Headers &headers); +``` + +#### Event Handlers + +```cpp +// Called for all events (or events without a specific handler) +sse.on_message([](const SSEMessage &msg) { }); + +// Called for specific event types +sse.on_event("update", [](const SSEMessage &msg) { }); +sse.on_event("delete", [](const SSEMessage &msg) { }); + +// Called when connection is established +sse.on_open([]() { }); + +// Called on connection errors +sse.on_error([](httplib::Error err) { }); +``` + +#### Configuration + +```cpp +// Set reconnect interval (default: 3000ms) +sse.set_reconnect_interval(5000); + +// Set max reconnect attempts (default: 0 = unlimited) +sse.set_max_reconnect_attempts(10); +``` + +#### Control + +```cpp +// Blocking start with auto-reconnect +sse.start(); + +// Non-blocking start (runs in background thread) +sse.start_async(); + +// Stop the client (thread-safe) +sse.stop(); +``` + +#### State + +```cpp +bool connected = sse.is_connected(); +const std::string &id = sse.last_event_id(); +``` + +## Examples + +### Basic Usage + +```cpp +httplib::Client cli("http://localhost:8080"); +httplib::sse::SSEClient sse(cli, "/events"); + +sse.on_message([](const httplib::sse::SSEMessage &msg) { + std::cout << msg.data << std::endl; +}); + +sse.start(); +``` + +### With Custom Event Types + +```cpp +httplib::sse::SSEClient sse(cli, "/events"); + +sse.on_event("notification", [](const httplib::sse::SSEMessage &msg) { + std::cout << "Notification: " << msg.data << std::endl; +}); + +sse.on_event("update", [](const httplib::sse::SSEMessage &msg) { + std::cout << "Update: " << msg.data << std::endl; +}); + +sse.start(); +``` + +### Async with Stop + +```cpp +httplib::sse::SSEClient sse(cli, "/events"); + +sse.on_message([](const httplib::sse::SSEMessage &msg) { + std::cout << msg.data << std::endl; +}); + +sse.start_async(); // Returns immediately + +// ... do other work ... + +sse.stop(); // Stop when done +``` + +### With Custom Headers (e.g., Authentication) + +```cpp +httplib::Headers headers = { + {"Authorization", "Bearer token123"} +}; + +httplib::sse::SSEClient sse(cli, "/events", headers); +sse.start(); +``` + +### Error Handling + +```cpp +sse.on_error([](httplib::Error err) { + std::cerr << "Error: " << httplib::to_string(err) << std::endl; +}); + +sse.set_reconnect_interval(1000); +sse.set_max_reconnect_attempts(5); + +sse.start(); +``` + +## SSE Protocol + +The client parses SSE format according to the [W3C specification](https://html.spec.whatwg.org/multipage/server-sent-events.html): + +``` +event: custom-type +id: 123 +data: {"message": "hello"} + +data: simple message + +: this is a comment (ignored) +``` diff --git a/README.md b/README.md index 9c64a17656..71309ef262 100644 --- a/README.md +++ b/README.md @@ -1206,8 +1206,8 @@ std::string decoded_component = httplib::decode_uri_component(encoded_component) Use `encode_uri()` for full URLs and `encode_uri_component()` for individual query parameters or path segments. -Streaming API -------------- +Stream API +---------- Process large responses without loading everything into memory. @@ -1234,6 +1234,28 @@ All HTTP methods are supported: `stream::Get`, `Post`, `Put`, `Patch`, `Delete`, See [README-stream.md](README-stream.md) for more details. +SSE Client +---------- + +```cpp +#include + +int main() { + httplib::Client cli("http://localhost:8080"); + httplib::sse::SSEClient sse(cli, "/events"); + + sse.on_message([](const httplib::sse::SSEMessage &msg) { + std::cout << "Event: " << msg.event << std::endl; + std::cout << "Data: " << msg.data << std::endl; + }); + + sse.start(); // Blocking, with auto-reconnect + return 0; +} +``` + +See [README-sse.md](README-sse.md) for more details. + Split httplib.h into .h and .cc ------------------------------- diff --git a/example/Makefile b/example/Makefile index 0d3c46d080..3082b88014 100644 --- a/example/Makefile +++ b/example/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -all: server client hello simplecli simplesvr upload redirect ssesvr ssecli ssecli-stream benchmark one_time_request server_and_client accept_header +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -47,9 +47,6 @@ ssesvr : ssesvr.cc ../httplib.h Makefile ssecli : ssecli.cc ../httplib.h Makefile $(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) -ssecli-stream : ssecli-stream.cc ../httplib.h ../httplib.h Makefile - $(CXX) -o ssecli-stream $(CXXFLAGS) ssecli-stream.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) - benchmark : benchmark.cc ../httplib.h Makefile $(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -67,4 +64,4 @@ pem: openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect ssesvr ssecli ssecli-stream benchmark one_time_request server_and_client accept_header *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header *.pem diff --git a/example/ssecli.cc b/example/ssecli.cc index 2c938229f6..f7b9ed55fb 100644 --- a/example/ssecli.cc +++ b/example/ssecli.cc @@ -6,16 +6,52 @@ // #include + +#include #include using namespace std; +// Global SSEClient pointer for signal handling +httplib::sse::SSEClient *g_sse = nullptr; + +void signal_handler(int) { + if (g_sse) { g_sse->stop(); } +} + int main(void) { - httplib::Client("http://localhost:1234") - .Get("/event1", [&](const char *data, size_t data_length) { - std::cout << string(data, data_length); - return true; - }); + // Configuration + const string host = "http://localhost:1234"; + const string path = "/event1"; + + cout << "SSE Client using httplib::sse::SSEClient\n"; + cout << "Connecting to: " << host << path << "\n"; + cout << "Press Ctrl+C to exit\n\n"; + + httplib::Client cli(host); + httplib::sse::SSEClient sse(cli, path); + + // Set up signal handler for graceful shutdown + g_sse = &sse; + signal(SIGINT, signal_handler); + + // Event handlers + sse.on_open([]() { cout << "[Connected]\n\n"; }); + + sse.on_message([](const httplib::sse::SSEMessage &msg) { + cout << "Event: " << msg.event << "\n"; + cout << "Data: " << msg.data << "\n"; + if (!msg.id.empty()) { cout << "ID: " << msg.id << "\n"; } + cout << "\n"; + }); + + sse.on_error([](httplib::Error err) { + cerr << "[Error] " << httplib::to_string(err) << "\n"; + }); + + // Start with auto-reconnect (blocking) + sse.start(); + cout << "\n[Disconnected]\n"; return 0; } diff --git a/httplib.h b/httplib.h index 5e34915958..114d60b814 100644 --- a/httplib.h +++ b/httplib.h @@ -2756,1571 +2756,2221 @@ bool is_field_value(const std::string &s); } // namespace detail -// ---------------------------------------------------------------------------- +namespace stream { -/* - * Implementation that will be part of the .cc file if split into .h + .cc. - */ +class Result { +public: + Result() : chunk_size_(8192) {} -namespace detail { + explicit Result(ClientImpl::StreamHandle &&handle, size_t chunk_size = 8192) + : handle_(std::move(handle)), chunk_size_(chunk_size) {} -inline bool is_hex(char c, int &v) { - if (isdigit(c)) { - v = c - '0'; - return true; - } else if ('A' <= c && c <= 'F') { - v = c - 'A' + 10; - return true; - } else if ('a' <= c && c <= 'f') { - v = c - 'a' + 10; - return true; + Result(Result &&other) noexcept + : handle_(std::move(other.handle_)), buffer_(std::move(other.buffer_)), + current_size_(other.current_size_), chunk_size_(other.chunk_size_), + finished_(other.finished_) { + other.current_size_ = 0; + other.finished_ = true; } - return false; -} - -inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, - int &val) { - if (i >= s.size()) { return false; } - val = 0; - for (; cnt; i++, cnt--) { - if (!s[i]) { return false; } - auto v = 0; - if (is_hex(s[i], v)) { - val = val * 16 + v; - } else { - return false; + Result &operator=(Result &&other) noexcept { + if (this != &other) { + handle_ = std::move(other.handle_); + buffer_ = std::move(other.buffer_); + current_size_ = other.current_size_; + chunk_size_ = other.chunk_size_; + finished_ = other.finished_; + other.current_size_ = 0; + other.finished_ = true; } + return *this; } - return true; -} - -inline std::string from_i_to_hex(size_t n) { - static const auto charset = "0123456789abcdef"; - std::string ret; - do { - ret = charset[n & 15] + ret; - n >>= 4; - } while (n > 0); - return ret; -} -inline std::string compute_etag(const FileStat &fs) { - if (!fs.is_file()) { return std::string(); } + Result(const Result &) = delete; + Result &operator=(const Result &) = delete; - // If mtime cannot be determined (negative value indicates an error - // or sentinel), do not generate an ETag. Returning a neutral / fixed - // value like 0 could collide with a real file that legitimately has - // mtime == 0 (epoch) and lead to misleading validators. - auto mtime_raw = fs.mtime(); - if (mtime_raw < 0) { return std::string(); } + // Check if the result is valid (connection succeeded and response received) + bool is_valid() const { return handle_.is_valid(); } + explicit operator bool() const { return is_valid(); } - auto mtime = static_cast(mtime_raw); - auto size = fs.size(); + // Response status code + int status() const { + return handle_.response ? handle_.response->status : -1; + } - return std::string("W/\"") + from_i_to_hex(mtime) + "-" + - from_i_to_hex(size) + "\""; -} + // Response headers + const Headers &headers() const { + static const Headers empty_headers; + return handle_.response ? handle_.response->headers : empty_headers; + } -// Format time_t as HTTP-date (RFC 9110 Section 5.6.7): "Sun, 06 Nov 1994 -// 08:49:37 GMT" This implementation is defensive: it validates `mtime`, checks -// return values from `gmtime_r`/`gmtime_s`, and ensures `strftime` succeeds. -inline std::string file_mtime_to_http_date(time_t mtime) { - if (mtime < 0) { return std::string(); } + std::string get_header_value(const std::string &key, + const char *def = "") const { + return handle_.response ? handle_.response->get_header_value(key, def) + : def; + } - struct tm tm_buf; -#ifdef _WIN32 - if (gmtime_s(&tm_buf, &mtime) != 0) { return std::string(); } -#else - if (gmtime_r(&mtime, &tm_buf) == nullptr) { return std::string(); } -#endif - char buf[64]; - if (strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tm_buf) == 0) { - return std::string(); + bool has_header(const std::string &key) const { + return handle_.response ? handle_.response->has_header(key) : false; } - return std::string(buf); -} + // Error information + Error error() const { return handle_.error; } + Error read_error() const { return handle_.get_read_error(); } + bool has_read_error() const { return handle_.has_read_error(); } -// Parse HTTP-date (RFC 9110 Section 5.6.7) to time_t. Returns -1 on failure. -inline time_t parse_http_date(const std::string &date_str) { - struct tm tm_buf; + // Streaming iteration API + // Call next() to read the next chunk, then access data via data()/size() + // Returns true if data was read, false when stream is exhausted + bool next() { + if (!handle_.is_valid() || finished_) { return false; } - // Create a classic locale object once for all parsing attempts - const std::locale classic_locale = std::locale::classic(); + if (buffer_.size() < chunk_size_) { buffer_.resize(chunk_size_); } - // Try to parse using std::get_time (C++11, cross-platform) - auto try_parse = [&](const char *fmt) -> bool { - std::istringstream ss(date_str); - ss.imbue(classic_locale); + ssize_t n = handle_.read(&buffer_[0], chunk_size_); + if (n > 0) { + current_size_ = static_cast(n); + return true; + } - memset(&tm_buf, 0, sizeof(tm_buf)); - ss >> std::get_time(&tm_buf, fmt); + current_size_ = 0; + finished_ = true; + return false; + } - return !ss.fail(); - }; + // Pointer to current chunk data (valid after next() returns true) + const char *data() const { return buffer_.data(); } - // RFC 9110 preferred format (HTTP-date): "Sun, 06 Nov 1994 08:49:37 GMT" - if (!try_parse("%a, %d %b %Y %H:%M:%S")) { - // RFC 850 format: "Sunday, 06-Nov-94 08:49:37 GMT" - if (!try_parse("%A, %d-%b-%y %H:%M:%S")) { - // asctime format: "Sun Nov 6 08:49:37 1994" - if (!try_parse("%a %b %d %H:%M:%S %Y")) { - return static_cast(-1); - } + // Size of current chunk (valid after next() returns true) + size_t size() const { return current_size_; } + + // Convenience method: read all remaining data into a string + std::string read_all() { + std::string result; + while (next()) { + result.append(data(), size()); } + return result; } -#ifdef _WIN32 - return _mkgmtime(&tm_buf); -#else - return timegm(&tm_buf); -#endif -} +private: + ClientImpl::StreamHandle handle_; + std::string buffer_; + size_t current_size_ = 0; + size_t chunk_size_; + bool finished_ = false; +}; -inline bool is_weak_etag(const std::string &s) { - // Check if the string is a weak ETag (starts with 'W/"') - return s.size() > 3 && s[0] == 'W' && s[1] == '/' && s[2] == '"'; +// GET +template +inline Result Get(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path), chunk_size}; } -inline bool is_strong_etag(const std::string &s) { - // Check if the string is a strong ETag (starts and ends with '"', at least 2 - // chars) - return s.size() >= 2 && s[0] == '"' && s.back() == '"'; +template +inline Result Get(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path, {}, headers), chunk_size}; } -inline size_t to_utf8(int code, char *buff) { - if (code < 0x0080) { - buff[0] = static_cast(code & 0x7F); - return 1; - } else if (code < 0x0800) { - buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); - buff[1] = static_cast(0x80 | (code & 0x3F)); - return 2; - } else if (code < 0xD800) { - buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); - buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[2] = static_cast(0x80 | (code & 0x3F)); - return 3; - } else if (code < 0xE000) { // D800 - DFFF is invalid... - return 0; - } else if (code < 0x10000) { - buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); - buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[2] = static_cast(0x80 | (code & 0x3F)); - return 3; - } else if (code < 0x110000) { - buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); - buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); - buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); - buff[3] = static_cast(0x80 | (code & 0x3F)); - return 4; - } +template +inline Result Get(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path, params), chunk_size}; +} - // NOTREACHED - return 0; +template +inline Result Get(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("GET", path, params, headers), chunk_size}; } -// NOTE: This code came up with the following stackoverflow post: -// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c -inline std::string base64_encode(const std::string &in) { - static const auto lookup = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +// POST +template +inline Result Post(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("POST", path, {}, {}, body, content_type), + chunk_size}; +} - std::string out; - out.reserve(in.size()); +template +inline Result Post(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("POST", path, {}, headers, body, content_type), + chunk_size}; +} - auto val = 0; - auto valb = -6; +template +inline Result Post(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("POST", path, params, {}, body, content_type), + chunk_size}; +} - for (auto c : in) { - val = (val << 8) + static_cast(c); - valb += 8; - while (valb >= 0) { - out.push_back(lookup[(val >> valb) & 0x3F]); - valb -= 6; - } - } +template +inline Result Post(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("POST", path, params, headers, body, content_type), + chunk_size}; +} - if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } +// PUT +template +inline Result Put(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("PUT", path, {}, {}, body, content_type), + chunk_size}; +} - while (out.size() % 4) { - out.push_back('='); - } +template +inline Result Put(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PUT", path, {}, headers, body, content_type), + chunk_size}; +} - return out; +template +inline Result Put(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PUT", path, params, {}, body, content_type), + chunk_size}; } -inline bool is_valid_path(const std::string &path) { - size_t level = 0; - size_t i = 0; +template +inline Result Put(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("PUT", path, params, headers, body, content_type), + chunk_size}; +} - // Skip slash - while (i < path.size() && path[i] == '/') { - i++; - } +// PATCH +template +inline Result Patch(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("PATCH", path, {}, {}, body, content_type), + chunk_size}; +} - while (i < path.size()) { - // Read component - auto beg = i; - while (i < path.size() && path[i] != '/') { - if (path[i] == '\0') { - return false; - } else if (path[i] == '\\') { - return false; - } - i++; - } +template +inline Result Patch(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PATCH", path, {}, headers, body, content_type), + chunk_size}; +} - auto len = i - beg; - assert(len > 0); +template +inline Result Patch(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, size_t chunk_size = 8192) { + return Result{cli.open_stream("PATCH", path, params, {}, body, content_type), + chunk_size}; +} - if (!path.compare(beg, len, ".")) { - ; - } else if (!path.compare(beg, len, "..")) { - if (level == 0) { return false; } - level--; - } else { - level++; - } +template +inline Result Patch(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("PATCH", path, params, headers, body, content_type), + chunk_size}; +} - // Skip slash - while (i < path.size() && path[i] == '/') { - i++; - } - } +// DELETE +template +inline Result Delete(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path), chunk_size}; +} - return true; +template +inline Result Delete(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, {}, headers), chunk_size}; } -inline FileStat::FileStat(const std::string &path) { -#if defined(_WIN32) - auto wpath = u8string_to_wstring(path.c_str()); - ret_ = _wstat(wpath.c_str(), &st_); -#else - ret_ = stat(path.c_str(), &st_); -#endif +template +inline Result Delete(ClientType &cli, const std::string &path, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, {}, {}, body, content_type), + chunk_size}; } -inline bool FileStat::is_file() const { - return ret_ >= 0 && S_ISREG(st_.st_mode); + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Headers &headers, const std::string &body, + const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("DELETE", path, {}, headers, body, content_type), + chunk_size}; } -inline bool FileStat::is_dir() const { - return ret_ >= 0 && S_ISDIR(st_.st_mode); + +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, params), chunk_size}; } -inline time_t FileStat::mtime() const { - return ret_ >= 0 ? static_cast(st_.st_mtime) - : static_cast(-1); +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, params, headers), chunk_size}; } -inline size_t FileStat::size() const { - return ret_ >= 0 ? static_cast(st_.st_size) : 0; +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, const std::string &body, + const std::string &content_type, + size_t chunk_size = 8192) { + return Result{cli.open_stream("DELETE", path, params, {}, body, content_type), + chunk_size}; } -inline std::string encode_path(const std::string &s) { - std::string result; - result.reserve(s.size()); +template +inline Result Delete(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type, + size_t chunk_size = 8192) { + return Result{ + cli.open_stream("DELETE", path, params, headers, body, content_type), + chunk_size}; +} - for (size_t i = 0; s[i]; i++) { - switch (s[i]) { - case ' ': result += "%20"; break; - case '+': result += "%2B"; break; - case '\r': result += "%0D"; break; - case '\n': result += "%0A"; break; - case '\'': result += "%27"; break; - case ',': result += "%2C"; break; - // case ':': result += "%3A"; break; // ok? probably... - case ';': result += "%3B"; break; - default: - auto c = static_cast(s[i]); - if (c >= 0x80) { - result += '%'; - char hex[4]; - auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); - assert(len == 2); - result.append(hex, static_cast(len)); - } else { - result += s[i]; - } - break; - } - } +// HEAD +template +inline Result Head(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path), chunk_size}; +} - return result; +template +inline Result Head(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path, {}, headers), chunk_size}; } -inline std::string file_extension(const std::string &path) { - std::smatch m; - thread_local auto re = std::regex("\\.([a-zA-Z0-9]+)$"); - if (std::regex_search(path, m, re)) { return m[1].str(); } - return std::string(); +template +inline Result Head(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path, params), chunk_size}; } -inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } +template +inline Result Head(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("HEAD", path, params, headers), chunk_size}; +} -template -inline bool parse_header(const char *beg, const char *end, T fn); +// OPTIONS +template +inline Result Options(ClientType &cli, const std::string &path, + size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path), chunk_size}; +} -template -inline bool parse_header(const char *beg, const char *end, T fn) { - // Skip trailing spaces and tabs. - while (beg < end && is_space_or_tab(end[-1])) { - end--; - } +template +inline Result Options(ClientType &cli, const std::string &path, + const Headers &headers, size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path, {}, headers), chunk_size}; +} - auto p = beg; - while (p < end && *p != ':') { - p++; - } +template +inline Result Options(ClientType &cli, const std::string &path, + const Params ¶ms, size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path, params), chunk_size}; +} - auto name = std::string(beg, p); - if (!detail::fields::is_field_name(name)) { return false; } +template +inline Result Options(ClientType &cli, const std::string &path, + const Params ¶ms, const Headers &headers, + size_t chunk_size = 8192) { + return Result{cli.open_stream("OPTIONS", path, params, headers), chunk_size}; +} - if (p == end) { return false; } +} // namespace stream - auto key_end = p; +namespace sse { - if (*p++ != ':') { return false; } +struct SSEMessage { + std::string event; // Event type (default: "message") + std::string data; // Event payload + std::string id; // Event ID for Last-Event-ID header - while (p < end && is_space_or_tab(*p)) { - p++; + SSEMessage() : event("message") {} + + void clear() { + event = "message"; + data.clear(); + id.clear(); } +}; - if (p <= end) { - auto key_len = key_end - beg; - if (!key_len) { return false; } +class SSEClient { +public: + using MessageHandler = std::function; + using ErrorHandler = std::function; + using OpenHandler = std::function; - auto key = std::string(beg, key_end); - auto val = std::string(p, end); + SSEClient(Client &client, const std::string &path) + : client_(client), path_(path) {} - if (!detail::fields::is_field_value(val)) { return false; } + SSEClient(Client &client, const std::string &path, const Headers &headers) + : client_(client), path_(path), headers_(headers) {} - if (case_ignore::equal(key, "Location") || - case_ignore::equal(key, "Referer")) { - fn(key, val); - } else { - fn(key, decode_path_component(val)); - } + ~SSEClient() { stop(); } - return true; + SSEClient(const SSEClient &) = delete; + SSEClient &operator=(const SSEClient &) = delete; + + // Event handlers + SSEClient &on_message(MessageHandler handler) { + on_message_ = std::move(handler); + return *this; } - return false; -} + SSEClient &on_event(const std::string &type, MessageHandler handler) { + event_handlers_[type] = std::move(handler); + return *this; + } -inline bool parse_trailers(stream_line_reader &line_reader, Headers &dest, - const Headers &src_headers) { - // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked - // transfer coding is complete when a chunk with a chunk-size of zero is - // received, possibly followed by a trailer section, and finally terminated by - // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 - // - // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section - // doesn't care for the existence of the final CRLF. In other words, it seems - // to be ok whether the final CRLF exists or not in the chunked data. - // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 - // - // According to the reference code in RFC 9112, cpp-httplib now allows - // chunked transfer coding data without the final CRLF. + SSEClient &on_open(OpenHandler handler) { + on_open_ = std::move(handler); + return *this; + } - // RFC 7230 Section 4.1.2 - Headers prohibited in trailers - thread_local case_ignore::unordered_set prohibited_trailers = { - "transfer-encoding", - "content-length", - "host", - "authorization", - "www-authenticate", - "proxy-authenticate", - "proxy-authorization", - "cookie", - "set-cookie", - "cache-control", - "expect", - "max-forwards", - "pragma", - "range", - "te", - "age", - "expires", - "date", - "location", - "retry-after", - "vary", - "warning", - "content-encoding", - "content-type", - "content-range", - "trailer"}; + SSEClient &on_error(ErrorHandler handler) { + on_error_ = std::move(handler); + return *this; + } - case_ignore::unordered_set declared_trailers; - auto trailer_header = get_header_value(src_headers, "Trailer", "", 0); - if (trailer_header && std::strlen(trailer_header)) { - auto len = std::strlen(trailer_header); - split(trailer_header, trailer_header + len, ',', - [&](const char *b, const char *e) { - const char *kbeg = b; - const char *kend = e; - while (kbeg < kend && (*kbeg == ' ' || *kbeg == '\t')) { - ++kbeg; - } - while (kend > kbeg && (kend[-1] == ' ' || kend[-1] == '\t')) { - --kend; - } - std::string key(kbeg, static_cast(kend - kbeg)); - if (!key.empty() && - prohibited_trailers.find(key) == prohibited_trailers.end()) { - declared_trailers.insert(key); - } - }); + SSEClient &set_reconnect_interval(int ms) { + reconnect_interval_ms_ = ms; + return *this; } - size_t trailer_header_count = 0; - while (strcmp(line_reader.ptr(), "\r\n") != 0) { - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } - if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } + SSEClient &set_max_reconnect_attempts(int n) { + max_reconnect_attempts_ = n; + return *this; + } - constexpr auto line_terminator_len = 2; - auto line_beg = line_reader.ptr(); - auto line_end = - line_reader.ptr() + line_reader.size() - line_terminator_len; + // State accessors + bool is_connected() const { return connected_.load(); } + const std::string &last_event_id() const { return last_event_id_; } - if (!parse_header(line_beg, line_end, - [&](const std::string &key, const std::string &val) { - if (declared_trailers.find(key) != - declared_trailers.end()) { - dest.emplace(key, val); - trailer_header_count++; - } - })) { + // Blocking start - runs event loop with auto-reconnect + void start() { + running_.store(true); + run_event_loop(); + } + + // Non-blocking start - runs in background thread + void start_async() { + running_.store(true); + async_thread_ = std::thread([this]() { run_event_loop(); }); + } + + // Stop the client (thread-safe) + void stop() { + running_.store(false); + client_.stop(); // Cancel any pending operations + if (async_thread_.joinable()) { async_thread_.join(); } + } + +private: + // Parse a single SSE field line + // Returns true if this line ends an event (blank line) + bool parse_sse_line(const std::string &line, SSEMessage &msg, int &retry_ms) { + // Blank line signals end of event + if (line.empty() || line == "\r") { return true; } + + // Lines starting with ':' are comments (ignored) + if (!line.empty() && line[0] == ':') { return false; } + + // Find the colon separator + auto colon_pos = line.find(':'); + if (colon_pos == std::string::npos) { + // Line with no colon is treated as field name with empty value return false; } - if (!line_reader.getline()) { return false; } + auto field = line.substr(0, colon_pos); + std::string value; + + // Value starts after colon, skip optional single space + if (colon_pos + 1 < line.size()) { + auto value_start = colon_pos + 1; + if (line[value_start] == ' ') { value_start++; } + value = line.substr(value_start); + // Remove trailing \r if present + if (!value.empty() && value.back() == '\r') { value.pop_back(); } + } + + // Handle known fields + if (field == "event") { + msg.event = value; + } else if (field == "data") { + // Multiple data lines are concatenated with newlines + if (!msg.data.empty()) { msg.data += "\n"; } + msg.data += value; + } else if (field == "id") { + // Empty id is valid (clears the last event ID) + msg.id = value; + } else if (field == "retry") { + // Parse retry interval in milliseconds + try { + retry_ms = std::stoi(value); + } catch (...) { + // Invalid retry value, ignore + } + } + // Unknown fields are ignored per SSE spec + + return false; } - return true; -} + // Main event loop with auto-reconnect + void run_event_loop() { + auto reconnect_count = 0; -inline std::pair trim(const char *b, const char *e, size_t left, - size_t right) { - while (b + left < e && is_space_or_tab(b[left])) { - left++; - } - while (right > 0 && is_space_or_tab(b[right - 1])) { - right--; - } - return std::make_pair(left, right); -} + while (running_.load()) { + // Build headers, including Last-Event-ID if we have one + auto request_headers = headers_; + if (!last_event_id_.empty()) { + request_headers.emplace("Last-Event-ID", last_event_id_); + } -inline std::string trim_copy(const std::string &s) { - auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); - return s.substr(r.first, r.second - r.first); -} + // Open streaming connection + auto result = stream::Get(client_, path_, request_headers); -inline std::string trim_double_quotes_copy(const std::string &s) { - if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { - return s.substr(1, s.size() - 2); - } - return s; -} + // Connection error handling + if (!result) { + connected_.store(false); + if (on_error_) { on_error_(result.error()); } -inline void -divide(const char *data, std::size_t size, char d, - std::function - fn) { - const auto it = std::find(data, data + size, d); - const auto found = static_cast(it != data + size); - const auto lhs_data = data; - const auto lhs_size = static_cast(it - data); - const auto rhs_data = it + found; - const auto rhs_size = size - lhs_size - found; + if (!should_reconnect(reconnect_count)) { break; } + wait_for_reconnect(); + reconnect_count++; + continue; + } - fn(lhs_data, lhs_size, rhs_data, rhs_size); -} + if (result.status() != 200) { + connected_.store(false); + // For certain errors, don't reconnect + if (result.status() == 204 || // No Content - server wants us to stop + result.status() == 404 || // Not Found + result.status() == 401 || // Unauthorized + result.status() == 403) { // Forbidden + if (on_error_) { on_error_(Error::Connection); } + break; + } -inline void -divide(const std::string &str, char d, - std::function - fn) { - divide(str.data(), str.size(), d, std::move(fn)); -} + if (on_error_) { on_error_(Error::Connection); } -inline void split(const char *b, const char *e, char d, - std::function fn) { - return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); -} + if (!should_reconnect(reconnect_count)) { break; } + wait_for_reconnect(); + reconnect_count++; + continue; + } -inline void split(const char *b, const char *e, char d, size_t m, - std::function fn) { - size_t i = 0; - size_t beg = 0; - size_t count = 1; + // Connection successful + connected_.store(true); + reconnect_count = 0; + if (on_open_) { on_open_(); } - while (e ? (b + i < e) : (b[i] != '\0')) { - if (b[i] == d && count < m) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } - beg = i + 1; - count++; - } - i++; - } + // Event receiving loop + std::string buffer; + SSEMessage current_msg; - if (i) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } - } -} + while (running_.load() && result.next()) { + buffer.append(result.data(), result.size()); -inline bool split_find(const char *b, const char *e, char d, size_t m, - std::function fn) { - size_t i = 0; - size_t beg = 0; - size_t count = 1; + // Process complete lines in the buffer + size_t line_start = 0; + size_t newline_pos; - while (e ? (b + i < e) : (b[i] != '\0')) { - if (b[i] == d && count < m) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { - auto found = fn(&b[r.first], &b[r.second]); - if (found) { return true; } + while ((newline_pos = buffer.find('\n', line_start)) != + std::string::npos) { + auto line = buffer.substr(line_start, newline_pos - line_start); + line_start = newline_pos + 1; + + // Parse the line and check if event is complete + auto event_complete = + parse_sse_line(line, current_msg, reconnect_interval_ms_); + + if (event_complete && !current_msg.data.empty()) { + // Update last_event_id for reconnection + if (!current_msg.id.empty()) { last_event_id_ = current_msg.id; } + + // Dispatch event to appropriate handler + dispatch_event(current_msg); + + current_msg.clear(); + } + } + + // Keep unprocessed data in buffer + buffer.erase(0, line_start); } - beg = i + 1; - count++; + + // Connection ended + connected_.store(false); + + if (!running_.load()) { break; } + + // Check for read errors + if (result.has_read_error()) { + if (on_error_) { on_error_(result.read_error()); } + } + + if (!should_reconnect(reconnect_count)) { break; } + wait_for_reconnect(); + reconnect_count++; } - i++; + + connected_.store(false); } - if (i) { - auto r = trim(b, e, beg, i); - if (r.first < r.second) { - auto found = fn(&b[r.first], &b[r.second]); - if (found) { return true; } + // Dispatch event to appropriate handler + void dispatch_event(const SSEMessage &msg) { + // Check for specific event type handler first + auto it = event_handlers_.find(msg.event); + if (it != event_handlers_.end()) { + it->second(msg); + return; } + + // Fall back to generic message handler + if (on_message_) { on_message_(msg); } } - return false; -} + // Check if we should attempt to reconnect + bool should_reconnect(int count) const { + if (!running_.load()) { return false; } + if (max_reconnect_attempts_ == 0) { return true; } // unlimited + return count < max_reconnect_attempts_; + } -inline bool split_find(const char *b, const char *e, char d, - std::function fn) { - return split_find(b, e, d, (std::numeric_limits::max)(), - std::move(fn)); -} + // Wait for reconnect interval + void wait_for_reconnect() { + // Use small increments to check running_ flag frequently + auto waited = 0; + while (running_.load() && waited < reconnect_interval_ms_) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + waited += 100; + } + } -inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, - size_t fixed_buffer_size) - : strm_(strm), fixed_buffer_(fixed_buffer), - fixed_buffer_size_(fixed_buffer_size) {} + // Client and path + Client &client_; + std::string path_; + Headers headers_; -inline const char *stream_line_reader::ptr() const { - if (growable_buffer_.empty()) { - return fixed_buffer_; - } else { - return growable_buffer_.data(); - } -} + // Callbacks + MessageHandler on_message_; + std::map event_handlers_; + OpenHandler on_open_; + ErrorHandler on_error_; -inline size_t stream_line_reader::size() const { - if (growable_buffer_.empty()) { - return fixed_buffer_used_size_; - } else { - return growable_buffer_.size(); - } -} + // Configuration + int reconnect_interval_ms_ = 3000; + int max_reconnect_attempts_ = 0; // 0 = unlimited -inline bool stream_line_reader::end_with_crlf() const { - auto end = ptr() + size(); - return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; -} + // State + std::atomic running_{false}; + std::atomic connected_{false}; + std::string last_event_id_; -inline bool stream_line_reader::getline() { - fixed_buffer_used_size_ = 0; - growable_buffer_.clear(); + // Async support + std::thread async_thread_; +}; -#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - char prev_byte = 0; -#endif +} // namespace sse - for (size_t i = 0;; i++) { - if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) { - // Treat exceptionally long lines as an error to - // prevent infinite loops/memory exhaustion - return false; - } - char byte; - auto n = strm_.read(&byte, 1); +// ---------------------------------------------------------------------------- - if (n < 0) { - return false; - } else if (n == 0) { - if (i == 0) { - return false; - } else { - break; - } - } +/* + * Implementation that will be part of the .cc file if split into .h + .cc. + */ - append(byte); +namespace detail { -#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR - if (byte == '\n') { break; } -#else - if (prev_byte == '\r' && byte == '\n') { break; } - prev_byte = byte; -#endif +inline bool is_hex(char c, int &v) { + if (isdigit(c)) { + v = c - '0'; + return true; + } else if ('A' <= c && c <= 'F') { + v = c - 'A' + 10; + return true; + } else if ('a' <= c && c <= 'f') { + v = c - 'a' + 10; + return true; } - - return true; + return false; } -inline void stream_line_reader::append(char c) { - if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { - fixed_buffer_[fixed_buffer_used_size_++] = c; - fixed_buffer_[fixed_buffer_used_size_] = '\0'; - } else { - if (growable_buffer_.empty()) { - assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); - growable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); +inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, + int &val) { + if (i >= s.size()) { return false; } + + val = 0; + for (; cnt; i++, cnt--) { + if (!s[i]) { return false; } + auto v = 0; + if (is_hex(s[i], v)) { + val = val * 16 + v; + } else { + return false; } - growable_buffer_ += c; } + return true; } -inline mmap::mmap(const char *path) { open(path); } +inline std::string from_i_to_hex(size_t n) { + static const auto charset = "0123456789abcdef"; + std::string ret; + do { + ret = charset[n & 15] + ret; + n >>= 4; + } while (n > 0); + return ret; +} -inline mmap::~mmap() { close(); } +inline std::string compute_etag(const FileStat &fs) { + if (!fs.is_file()) { return std::string(); } -inline bool mmap::open(const char *path) { - close(); + // If mtime cannot be determined (negative value indicates an error + // or sentinel), do not generate an ETag. Returning a neutral / fixed + // value like 0 could collide with a real file that legitimately has + // mtime == 0 (epoch) and lead to misleading validators. + auto mtime_raw = fs.mtime(); + if (mtime_raw < 0) { return std::string(); } -#if defined(_WIN32) - auto wpath = u8string_to_wstring(path); - if (wpath.empty()) { return false; } + auto mtime = static_cast(mtime_raw); + auto size = fs.size(); - hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, - OPEN_EXISTING, NULL); + return std::string("W/\"") + from_i_to_hex(mtime) + "-" + + from_i_to_hex(size) + "\""; +} - if (hFile_ == INVALID_HANDLE_VALUE) { return false; } +// Format time_t as HTTP-date (RFC 9110 Section 5.6.7): "Sun, 06 Nov 1994 +// 08:49:37 GMT" This implementation is defensive: it validates `mtime`, checks +// return values from `gmtime_r`/`gmtime_s`, and ensures `strftime` succeeds. +inline std::string file_mtime_to_http_date(time_t mtime) { + if (mtime < 0) { return std::string(); } - LARGE_INTEGER size{}; - if (!::GetFileSizeEx(hFile_, &size)) { return false; } - // If the following line doesn't compile due to QuadPart, update Windows SDK. - // See: - // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 - if (static_cast(size.QuadPart) > - (std::numeric_limits::max)()) { - // `size_t` might be 32-bits, on 32-bits Windows. - return false; + struct tm tm_buf; +#ifdef _WIN32 + if (gmtime_s(&tm_buf, &mtime) != 0) { return std::string(); } +#else + if (gmtime_r(&mtime, &tm_buf) == nullptr) { return std::string(); } +#endif + char buf[64]; + if (strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", &tm_buf) == 0) { + return std::string(); } - size_ = static_cast(size.QuadPart); - - hMapping_ = - ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); - // Special treatment for an empty file... - if (hMapping_ == NULL && size_ == 0) { - close(); - is_open_empty_file = true; - return true; - } + return std::string(buf); +} - if (hMapping_ == NULL) { - close(); - return false; - } +// Parse HTTP-date (RFC 9110 Section 5.6.7) to time_t. Returns -1 on failure. +inline time_t parse_http_date(const std::string &date_str) { + struct tm tm_buf; - addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); + // Create a classic locale object once for all parsing attempts + const std::locale classic_locale = std::locale::classic(); - if (addr_ == nullptr) { - close(); - return false; - } -#else - fd_ = ::open(path, O_RDONLY); - if (fd_ == -1) { return false; } + // Try to parse using std::get_time (C++11, cross-platform) + auto try_parse = [&](const char *fmt) -> bool { + std::istringstream ss(date_str); + ss.imbue(classic_locale); - struct stat sb; - if (fstat(fd_, &sb) == -1) { - close(); - return false; - } - size_ = static_cast(sb.st_size); + memset(&tm_buf, 0, sizeof(tm_buf)); + ss >> std::get_time(&tm_buf, fmt); - addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); + return !ss.fail(); + }; - // Special treatment for an empty file... - if (addr_ == MAP_FAILED && size_ == 0) { - close(); - is_open_empty_file = true; - return false; + // RFC 9110 preferred format (HTTP-date): "Sun, 06 Nov 1994 08:49:37 GMT" + if (!try_parse("%a, %d %b %Y %H:%M:%S")) { + // RFC 850 format: "Sunday, 06-Nov-94 08:49:37 GMT" + if (!try_parse("%A, %d-%b-%y %H:%M:%S")) { + // asctime format: "Sun Nov 6 08:49:37 1994" + if (!try_parse("%a %b %d %H:%M:%S %Y")) { + return static_cast(-1); + } + } } -#endif - return true; +#ifdef _WIN32 + return _mkgmtime(&tm_buf); +#else + return timegm(&tm_buf); +#endif } -inline bool mmap::is_open() const { - return is_open_empty_file ? true : addr_ != nullptr; +inline bool is_weak_etag(const std::string &s) { + // Check if the string is a weak ETag (starts with 'W/"') + return s.size() > 3 && s[0] == 'W' && s[1] == '/' && s[2] == '"'; } -inline size_t mmap::size() const { return size_; } - -inline const char *mmap::data() const { - return is_open_empty_file ? "" : static_cast(addr_); +inline bool is_strong_etag(const std::string &s) { + // Check if the string is a strong ETag (starts and ends with '"', at least 2 + // chars) + return s.size() >= 2 && s[0] == '"' && s.back() == '"'; } -inline void mmap::close() { -#if defined(_WIN32) - if (addr_) { - ::UnmapViewOfFile(addr_); - addr_ = nullptr; - } - - if (hMapping_) { - ::CloseHandle(hMapping_); - hMapping_ = NULL; - } - - if (hFile_ != INVALID_HANDLE_VALUE) { - ::CloseHandle(hFile_); - hFile_ = INVALID_HANDLE_VALUE; - } - - is_open_empty_file = false; -#else - if (addr_ != nullptr) { - munmap(addr_, size_); - addr_ = nullptr; +inline size_t to_utf8(int code, char *buff) { + if (code < 0x0080) { + buff[0] = static_cast(code & 0x7F); + return 1; + } else if (code < 0x0800) { + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); + return 2; + } else if (code < 0xD800) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0xE000) { // D800 - DFFF is invalid... + return 0; + } else if (code < 0x10000) { + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); + return 3; + } else if (code < 0x110000) { + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); + return 4; } - if (fd_ != -1) { - ::close(fd_); - fd_ = -1; - } -#endif - size_ = 0; -} -inline int close_socket(socket_t sock) { -#ifdef _WIN32 - return closesocket(sock); -#else - return close(sock); -#endif + // NOTREACHED + return 0; } -template inline ssize_t handle_EINTR(T fn) { - ssize_t res = 0; - while (true) { - res = fn(); - if (res < 0 && errno == EINTR) { - std::this_thread::sleep_for(std::chrono::microseconds{1}); - continue; +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c +inline std::string base64_encode(const std::string &in) { + static const auto lookup = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + std::string out; + out.reserve(in.size()); + + auto val = 0; + auto valb = -6; + + for (auto c : in) { + val = (val << 8) + static_cast(c); + valb += 8; + while (valb >= 0) { + out.push_back(lookup[(val >> valb) & 0x3F]); + valb -= 6; } - break; } - return res; -} -inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { - return handle_EINTR([&]() { - return recv(sock, -#ifdef _WIN32 - static_cast(ptr), static_cast(size), -#else - ptr, size, -#endif - flags); - }); -} + if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } -inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, - int flags) { - return handle_EINTR([&]() { - return send(sock, -#ifdef _WIN32 - static_cast(ptr), static_cast(size), -#else - ptr, size, -#endif - flags); - }); -} + while (out.size() % 4) { + out.push_back('='); + } -inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { -#ifdef _WIN32 - return ::WSAPoll(fds, nfds, timeout); -#else - return ::poll(fds, nfds, timeout); -#endif + return out; } -template -inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { -#ifdef __APPLE__ - if (sock >= FD_SETSIZE) { return -1; } +inline bool is_valid_path(const std::string &path) { + size_t level = 0; + size_t i = 0; - fd_set fds, *rfds, *wfds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - rfds = (Read ? &fds : nullptr); - wfds = (Read ? nullptr : &fds); + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); + while (i < path.size()) { + // Read component + auto beg = i; + while (i < path.size() && path[i] != '/') { + if (path[i] == '\0') { + return false; + } else if (path[i] == '\\') { + return false; + } + i++; + } - return handle_EINTR([&]() { - return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); - }); -#else - struct pollfd pfd; - pfd.fd = sock; - pfd.events = (Read ? POLLIN : POLLOUT); + auto len = i - beg; + assert(len > 0); - auto timeout = static_cast(sec * 1000 + usec / 1000); + if (!path.compare(beg, len, ".")) { + ; + } else if (!path.compare(beg, len, "..")) { + if (level == 0) { return false; } + level--; + } else { + level++; + } - return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); }); + // Skip slash + while (i < path.size() && path[i] == '/') { + i++; + } + } + + return true; +} + +inline FileStat::FileStat(const std::string &path) { +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path.c_str()); + ret_ = _wstat(wpath.c_str(), &st_); +#else + ret_ = stat(path.c_str(), &st_); #endif } +inline bool FileStat::is_file() const { + return ret_ >= 0 && S_ISREG(st_.st_mode); +} +inline bool FileStat::is_dir() const { + return ret_ >= 0 && S_ISDIR(st_.st_mode); +} -inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { - return select_impl(sock, sec, usec); +inline time_t FileStat::mtime() const { + return ret_ >= 0 ? static_cast(st_.st_mtime) + : static_cast(-1); } -inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { - return select_impl(sock, sec, usec); +inline size_t FileStat::size() const { + return ret_ >= 0 ? static_cast(st_.st_size) : 0; } -inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, - time_t usec) { -#ifdef __APPLE__ - if (sock >= FD_SETSIZE) { return Error::Connection; } +inline std::string encode_path(const std::string &s) { + std::string result; + result.reserve(s.size()); - fd_set fdsr, fdsw; - FD_ZERO(&fdsr); - FD_ZERO(&fdsw); - FD_SET(sock, &fdsr); - FD_SET(sock, &fdsw); + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; + case '\r': result += "%0D"; break; + case '\n': result += "%0A"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + // case ':': result += "%3A"; break; // ok? probably... + case ';': result += "%3B"; break; + default: + auto c = static_cast(s[i]); + if (c >= 0x80) { + result += '%'; + char hex[4]; + auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); + assert(len == 2); + result.append(hex, static_cast(len)); + } else { + result += s[i]; + } + break; + } + } - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); + return result; +} - auto ret = handle_EINTR([&]() { - return select(static_cast(sock + 1), &fdsr, &fdsw, nullptr, &tv); - }); +inline std::string file_extension(const std::string &path) { + std::smatch m; + thread_local auto re = std::regex("\\.([a-zA-Z0-9]+)$"); + if (std::regex_search(path, m, re)) { return m[1].str(); } + return std::string(); +} - if (ret == 0) { return Error::ConnectionTimeout; } +inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } - if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; +template +inline bool parse_header(const char *beg, const char *end, T fn); + +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; } - return Error::Connection; -#else - struct pollfd pfd_read; - pfd_read.fd = sock; - pfd_read.events = POLLIN | POLLOUT; - - auto timeout = static_cast(sec * 1000 + usec / 1000); + auto p = beg; + while (p < end && *p != ':') { + p++; + } - auto poll_res = - handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); }); + auto name = std::string(beg, p); + if (!detail::fields::is_field_name(name)) { return false; } - if (poll_res == 0) { return Error::ConnectionTimeout; } + if (p == end) { return false; } - if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } + auto key_end = p; - return Error::Connection; -#endif -} + if (*p++ != ':') { return false; } -inline bool is_socket_alive(socket_t sock) { - const auto val = detail::select_read(sock, 0, 0); - if (val == 0) { - return true; - } else if (val < 0 && errno == EBADF) { - return false; + while (p < end && is_space_or_tab(*p)) { + p++; } - char buf[1]; - return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; -} - -class SocketStream final : public Stream { -public: - SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec = 0, - std::chrono::time_point start_time = - (std::chrono::steady_clock::time_point::min)()); - ~SocketStream() override; - bool is_readable() const override; - bool wait_readable() const override; - bool wait_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - time_t duration() const override; + if (p <= end) { + auto key_len = key_end - beg; + if (!key_len) { return false; } -private: - socket_t sock_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; - time_t max_timeout_msec_; - const std::chrono::time_point start_time_; + auto key = std::string(beg, key_end); + auto val = std::string(p, end); - std::vector read_buff_; - size_t read_buff_off_ = 0; - size_t read_buff_content_size_ = 0; + if (!detail::fields::is_field_value(val)) { return false; } - static const size_t read_buff_size_ = 1024l * 4; -}; + if (case_ignore::equal(key, "Location") || + case_ignore::equal(key, "Referer")) { + fn(key, val); + } else { + fn(key, decode_path_component(val)); + } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLSocketStream final : public Stream { -public: - SSLSocketStream( - socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, time_t max_timeout_msec = 0, - std::chrono::time_point start_time = - (std::chrono::steady_clock::time_point::min)()); - ~SSLSocketStream() override; + return true; + } - bool is_readable() const override; - bool wait_readable() const override; - bool wait_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - time_t duration() const override; + return false; +} -private: - socket_t sock_; - SSL *ssl_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; - time_t max_timeout_msec_; - const std::chrono::time_point start_time_; -}; -#endif +inline bool parse_trailers(stream_line_reader &line_reader, Headers &dest, + const Headers &src_headers) { + // NOTE: In RFC 9112, '7.1 Chunked Transfer Coding' mentions "The chunked + // transfer coding is complete when a chunk with a chunk-size of zero is + // received, possibly followed by a trailer section, and finally terminated by + // an empty line". https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1 + // + // In '7.1.3. Decoding Chunked', however, the pseudo-code in the section + // doesn't care for the existence of the final CRLF. In other words, it seems + // to be ok whether the final CRLF exists or not in the chunked data. + // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.1.3 + // + // According to the reference code in RFC 9112, cpp-httplib now allows + // chunked transfer coding data without the final CRLF. -inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, - time_t keep_alive_timeout_sec) { - using namespace std::chrono; + // RFC 7230 Section 4.1.2 - Headers prohibited in trailers + thread_local case_ignore::unordered_set prohibited_trailers = { + "transfer-encoding", + "content-length", + "host", + "authorization", + "www-authenticate", + "proxy-authenticate", + "proxy-authorization", + "cookie", + "set-cookie", + "cache-control", + "expect", + "max-forwards", + "pragma", + "range", + "te", + "age", + "expires", + "date", + "location", + "retry-after", + "vary", + "warning", + "content-encoding", + "content-type", + "content-range", + "trailer"}; - const auto interval_usec = - CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND; + case_ignore::unordered_set declared_trailers; + auto trailer_header = get_header_value(src_headers, "Trailer", "", 0); + if (trailer_header && std::strlen(trailer_header)) { + auto len = std::strlen(trailer_header); + split(trailer_header, trailer_header + len, ',', + [&](const char *b, const char *e) { + const char *kbeg = b; + const char *kend = e; + while (kbeg < kend && (*kbeg == ' ' || *kbeg == '\t')) { + ++kbeg; + } + while (kend > kbeg && (kend[-1] == ' ' || kend[-1] == '\t')) { + --kend; + } + std::string key(kbeg, static_cast(kend - kbeg)); + if (!key.empty() && + prohibited_trailers.find(key) == prohibited_trailers.end()) { + declared_trailers.insert(key); + } + }); + } - // Avoid expensive `steady_clock::now()` call for the first time - if (select_read(sock, 0, interval_usec) > 0) { return true; } + size_t trailer_header_count = 0; + while (strcmp(line_reader.ptr(), "\r\n") != 0) { + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + if (trailer_header_count >= CPPHTTPLIB_HEADER_MAX_COUNT) { return false; } - const auto start = steady_clock::now() - microseconds{interval_usec}; - const auto timeout = seconds{keep_alive_timeout_sec}; + constexpr auto line_terminator_len = 2; + auto line_beg = line_reader.ptr(); + auto line_end = + line_reader.ptr() + line_reader.size() - line_terminator_len; - while (true) { - if (svr_sock == INVALID_SOCKET) { - break; // Server socket is closed + if (!parse_header(line_beg, line_end, + [&](const std::string &key, const std::string &val) { + if (declared_trailers.find(key) != + declared_trailers.end()) { + dest.emplace(key, val); + trailer_header_count++; + } + })) { + return false; } - auto val = select_read(sock, 0, interval_usec); - if (val < 0) { - break; // Ssocket error - } else if (val == 0) { - if (steady_clock::now() - start > timeout) { - break; // Timeout - } - } else { - return true; // Ready for read - } + if (!line_reader.getline()) { return false; } } - return false; + return true; } -template -inline bool -process_server_socket_core(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, T callback) { - assert(keep_alive_max_count > 0); - auto ret = false; - auto count = keep_alive_max_count; - while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { - auto close_connection = count == 1; - auto connection_closed = false; - ret = callback(close_connection, connection_closed); - if (!ret || connection_closed) { break; } - count--; +inline std::pair trim(const char *b, const char *e, size_t left, + size_t right) { + while (b + left < e && is_space_or_tab(b[left])) { + left++; } - return ret; + while (right > 0 && is_space_or_tab(b[right - 1])) { + right--; + } + return std::make_pair(left, right); } -template -inline bool -process_server_socket(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - return process_server_socket_core( - svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm, close_connection, connection_closed); - }); -} - -inline bool process_client_socket( - socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time, - std::function callback) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec, max_timeout_msec, - start_time); - return callback(strm); -} - -inline int shutdown_socket(socket_t sock) { -#ifdef _WIN32 - return shutdown(sock, SD_BOTH); -#else - return shutdown(sock, SHUT_RDWR); -#endif +inline std::string trim_copy(const std::string &s) { + auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); + return s.substr(r.first, r.second - r.first); } -inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { - if (s.size() > 1 && s[0] == '\0') { - auto ret = s; - ret[0] = '@'; - return ret; +inline std::string trim_double_quotes_copy(const std::string &s) { + if (s.length() >= 2 && s.front() == '"' && s.back() == '"') { + return s.substr(1, s.size() - 2); } return s; } -inline std::string -unescape_abstract_namespace_unix_domain(const std::string &s) { - if (s.size() > 1 && s[0] == '@') { - auto ret = s; - ret[0] = '\0'; - return ret; - } - return s; -} +inline void +divide(const char *data, std::size_t size, char d, + std::function + fn) { + const auto it = std::find(data, data + size, d); + const auto found = static_cast(it != data + size); + const auto lhs_data = data; + const auto lhs_size = static_cast(it - data); + const auto rhs_data = it + found; + const auto rhs_size = size - lhs_size - found; -inline int getaddrinfo_with_timeout(const char *node, const char *service, - const struct addrinfo *hints, - struct addrinfo **res, time_t timeout_sec) { -#ifdef CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO - if (timeout_sec <= 0) { - // No timeout specified, use standard getaddrinfo - return getaddrinfo(node, service, hints, res); - } + fn(lhs_data, lhs_size, rhs_data, rhs_size); +} -#ifdef _WIN32 - // Windows-specific implementation using GetAddrInfoEx with overlapped I/O - OVERLAPPED overlapped = {0}; - HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); - if (!event) { return EAI_FAIL; } +inline void +divide(const std::string &str, char d, + std::function + fn) { + divide(str.data(), str.size(), d, std::move(fn)); +} - overlapped.hEvent = event; +inline void split(const char *b, const char *e, char d, + std::function fn) { + return split(b, e, d, (std::numeric_limits::max)(), std::move(fn)); +} - PADDRINFOEXW result_addrinfo = nullptr; - HANDLE cancel_handle = nullptr; +inline void split(const char *b, const char *e, char d, size_t m, + std::function fn) { + size_t i = 0; + size_t beg = 0; + size_t count = 1; - ADDRINFOEXW hints_ex = {0}; - if (hints) { - hints_ex.ai_flags = hints->ai_flags; - hints_ex.ai_family = hints->ai_family; - hints_ex.ai_socktype = hints->ai_socktype; - hints_ex.ai_protocol = hints->ai_protocol; + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d && count < m) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + beg = i + 1; + count++; + } + i++; } - auto wnode = u8string_to_wstring(node); - auto wservice = u8string_to_wstring(service); + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + } +} - auto ret = ::GetAddrInfoExW(wnode.data(), wservice.data(), NS_DNS, nullptr, - hints ? &hints_ex : nullptr, &result_addrinfo, - nullptr, &overlapped, nullptr, &cancel_handle); +inline bool split_find(const char *b, const char *e, char d, size_t m, + std::function fn) { + size_t i = 0; + size_t beg = 0; + size_t count = 1; - if (ret == WSA_IO_PENDING) { - auto wait_result = - ::WaitForSingleObject(event, static_cast(timeout_sec * 1000)); - if (wait_result == WAIT_TIMEOUT) { - if (cancel_handle) { ::GetAddrInfoExCancel(&cancel_handle); } - ::CloseHandle(event); - return EAI_AGAIN; + while (e ? (b + i < e) : (b[i] != '\0')) { + if (b[i] == d && count < m) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { + auto found = fn(&b[r.first], &b[r.second]); + if (found) { return true; } + } + beg = i + 1; + count++; } + i++; + } - DWORD bytes_returned; - if (!::GetOverlappedResult((HANDLE)INVALID_SOCKET, &overlapped, - &bytes_returned, FALSE)) { - ::CloseHandle(event); - return ::WSAGetLastError(); + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { + auto found = fn(&b[r.first], &b[r.second]); + if (found) { return true; } } } - ::CloseHandle(event); + return false; +} - if (ret == NO_ERROR || ret == WSA_IO_PENDING) { - *res = reinterpret_cast(result_addrinfo); - return 0; +inline bool split_find(const char *b, const char *e, char d, + std::function fn) { + return split_find(b, e, d, (std::numeric_limits::max)(), + std::move(fn)); +} + +inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, + size_t fixed_buffer_size) + : strm_(strm), fixed_buffer_(fixed_buffer), + fixed_buffer_size_(fixed_buffer_size) {} + +inline const char *stream_line_reader::ptr() const { + if (growable_buffer_.empty()) { + return fixed_buffer_; + } else { + return growable_buffer_.data(); } +} - return ret; -#elif TARGET_OS_MAC - // macOS implementation using CFHost API for asynchronous DNS resolution - CFStringRef hostname_ref = CFStringCreateWithCString( - kCFAllocatorDefault, node, kCFStringEncodingUTF8); - if (!hostname_ref) { return EAI_MEMORY; } +inline size_t stream_line_reader::size() const { + if (growable_buffer_.empty()) { + return fixed_buffer_used_size_; + } else { + return growable_buffer_.size(); + } +} - CFHostRef host_ref = CFHostCreateWithName(kCFAllocatorDefault, hostname_ref); - CFRelease(hostname_ref); - if (!host_ref) { return EAI_MEMORY; } +inline bool stream_line_reader::end_with_crlf() const { + auto end = ptr() + size(); + return size() >= 2 && end[-2] == '\r' && end[-1] == '\n'; +} - // Set up context for callback - struct CFHostContext { - bool completed = false; - bool success = false; - CFArrayRef addresses = nullptr; - std::mutex mutex; - std::condition_variable cv; - } context; +inline bool stream_line_reader::getline() { + fixed_buffer_used_size_ = 0; + growable_buffer_.clear(); - CFHostClientContext client_context; - memset(&client_context, 0, sizeof(client_context)); - client_context.info = &context; +#ifndef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + char prev_byte = 0; +#endif - // Set callback - auto callback = [](CFHostRef theHost, CFHostInfoType /*typeInfo*/, - const CFStreamError *error, void *info) { - auto ctx = static_cast(info); - std::lock_guard lock(ctx->mutex); + for (size_t i = 0;; i++) { + if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) { + // Treat exceptionally long lines as an error to + // prevent infinite loops/memory exhaustion + return false; + } + char byte; + auto n = strm_.read(&byte, 1); - if (error && error->error != 0) { - ctx->success = false; - } else { - Boolean hasBeenResolved; - ctx->addresses = CFHostGetAddressing(theHost, &hasBeenResolved); - if (ctx->addresses && hasBeenResolved) { - CFRetain(ctx->addresses); - ctx->success = true; + if (n < 0) { + return false; + } else if (n == 0) { + if (i == 0) { + return false; } else { - ctx->success = false; + break; } } - ctx->completed = true; - ctx->cv.notify_one(); - }; - - if (!CFHostSetClient(host_ref, callback, &client_context)) { - CFRelease(host_ref); - return EAI_SYSTEM; - } - // Schedule on run loop - CFRunLoopRef run_loop = CFRunLoopGetCurrent(); - CFHostScheduleWithRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + append(byte); - // Start resolution - CFStreamError stream_error; - if (!CFHostStartInfoResolution(host_ref, kCFHostAddresses, &stream_error)) { - CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); - CFRelease(host_ref); - return EAI_FAIL; +#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR + if (byte == '\n') { break; } +#else + if (prev_byte == '\r' && byte == '\n') { break; } + prev_byte = byte; +#endif } - // Wait for completion with timeout - auto timeout_time = - std::chrono::steady_clock::now() + std::chrono::seconds(timeout_sec); - bool timed_out = false; - - { - std::unique_lock lock(context.mutex); - - while (!context.completed) { - auto now = std::chrono::steady_clock::now(); - if (now >= timeout_time) { - timed_out = true; - break; - } + return true; +} - // Run the runloop for a short time - lock.unlock(); - CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true); - lock.lock(); +inline void stream_line_reader::append(char c) { + if (fixed_buffer_used_size_ < fixed_buffer_size_ - 1) { + fixed_buffer_[fixed_buffer_used_size_++] = c; + fixed_buffer_[fixed_buffer_used_size_] = '\0'; + } else { + if (growable_buffer_.empty()) { + assert(fixed_buffer_[fixed_buffer_used_size_] == '\0'); + growable_buffer_.assign(fixed_buffer_, fixed_buffer_used_size_); } + growable_buffer_ += c; } +} - // Clean up - CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); - CFHostSetClient(host_ref, nullptr, nullptr); +inline mmap::mmap(const char *path) { open(path); } - if (timed_out || !context.completed) { - CFHostCancelInfoResolution(host_ref, kCFHostAddresses); - CFRelease(host_ref); - return EAI_AGAIN; - } +inline mmap::~mmap() { close(); } - if (!context.success || !context.addresses) { - CFRelease(host_ref); - return EAI_NODATA; - } +inline bool mmap::open(const char *path) { + close(); - // Convert CFArray to addrinfo - CFIndex count = CFArrayGetCount(context.addresses); - if (count == 0) { - CFRelease(context.addresses); - CFRelease(host_ref); - return EAI_NODATA; - } +#if defined(_WIN32) + auto wpath = u8string_to_wstring(path); + if (wpath.empty()) { return false; } - struct addrinfo *result_addrinfo = nullptr; - struct addrinfo **current = &result_addrinfo; + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); - for (CFIndex i = 0; i < count; i++) { - CFDataRef addr_data = - static_cast(CFArrayGetValueAtIndex(context.addresses, i)); - if (!addr_data) continue; + if (hFile_ == INVALID_HANDLE_VALUE) { return false; } - const struct sockaddr *sockaddr_ptr = - reinterpret_cast(CFDataGetBytePtr(addr_data)); - socklen_t sockaddr_len = static_cast(CFDataGetLength(addr_data)); + LARGE_INTEGER size{}; + if (!::GetFileSizeEx(hFile_, &size)) { return false; } + // If the following line doesn't compile due to QuadPart, update Windows SDK. + // See: + // https://github.com/yhirose/cpp-httplib/issues/1903#issuecomment-2316520721 + if (static_cast(size.QuadPart) > + (std::numeric_limits::max)()) { + // `size_t` might be 32-bits, on 32-bits Windows. + return false; + } + size_ = static_cast(size.QuadPart); - // Allocate addrinfo structure - *current = static_cast(malloc(sizeof(struct addrinfo))); - if (!*current) { - freeaddrinfo(result_addrinfo); - CFRelease(context.addresses); - CFRelease(host_ref); - return EAI_MEMORY; - } + hMapping_ = + ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL); - memset(*current, 0, sizeof(struct addrinfo)); + // Special treatment for an empty file... + if (hMapping_ == NULL && size_ == 0) { + close(); + is_open_empty_file = true; + return true; + } - // Set up addrinfo fields - (*current)->ai_family = sockaddr_ptr->sa_family; - (*current)->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM; - (*current)->ai_protocol = hints ? hints->ai_protocol : IPPROTO_TCP; - (*current)->ai_addrlen = sockaddr_len; + if (hMapping_ == NULL) { + close(); + return false; + } - // Copy sockaddr - (*current)->ai_addr = static_cast(malloc(sockaddr_len)); - if (!(*current)->ai_addr) { - freeaddrinfo(result_addrinfo); - CFRelease(context.addresses); - CFRelease(host_ref); - return EAI_MEMORY; - } - memcpy((*current)->ai_addr, sockaddr_ptr, sockaddr_len); + addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); - // Set port if service is specified - if (service && strlen(service) > 0) { - int port = atoi(service); - if (port > 0) { - if (sockaddr_ptr->sa_family == AF_INET) { - reinterpret_cast((*current)->ai_addr) - ->sin_port = htons(static_cast(port)); - } else if (sockaddr_ptr->sa_family == AF_INET6) { - reinterpret_cast((*current)->ai_addr) - ->sin6_port = htons(static_cast(port)); - } - } - } + if (addr_ == nullptr) { + close(); + return false; + } +#else + fd_ = ::open(path, O_RDONLY); + if (fd_ == -1) { return false; } - current = &((*current)->ai_next); + struct stat sb; + if (fstat(fd_, &sb) == -1) { + close(); + return false; } + size_ = static_cast(sb.st_size); - CFRelease(context.addresses); - CFRelease(host_ref); + addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); - *res = result_addrinfo; - return 0; -#elif defined(_GNU_SOURCE) && defined(__GLIBC__) && \ - (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) - // Linux implementation using getaddrinfo_a for asynchronous DNS resolution - struct gaicb request; - struct gaicb *requests[1] = {&request}; - struct sigevent sevp; - struct timespec timeout; + // Special treatment for an empty file... + if (addr_ == MAP_FAILED && size_ == 0) { + close(); + is_open_empty_file = true; + return false; + } +#endif - // Initialize the request structure - memset(&request, 0, sizeof(request)); - request.ar_name = node; - request.ar_service = service; - request.ar_request = hints; + return true; +} - // Set up timeout - timeout.tv_sec = timeout_sec; - timeout.tv_nsec = 0; +inline bool mmap::is_open() const { + return is_open_empty_file ? true : addr_ != nullptr; +} - // Initialize sigevent structure (not used, but required) - memset(&sevp, 0, sizeof(sevp)); - sevp.sigev_notify = SIGEV_NONE; +inline size_t mmap::size() const { return size_; } - // Start asynchronous resolution - int start_result = getaddrinfo_a(GAI_NOWAIT, requests, 1, &sevp); - if (start_result != 0) { return start_result; } +inline const char *mmap::data() const { + return is_open_empty_file ? "" : static_cast(addr_); +} - // Wait for completion with timeout - int wait_result = - gai_suspend((const struct gaicb *const *)requests, 1, &timeout); +inline void mmap::close() { +#if defined(_WIN32) + if (addr_) { + ::UnmapViewOfFile(addr_); + addr_ = nullptr; + } - if (wait_result == 0 || wait_result == EAI_ALLDONE) { - // Completed successfully, get the result - int gai_result = gai_error(&request); - if (gai_result == 0) { - *res = request.ar_result; - return 0; - } else { - // Clean up on error - if (request.ar_result) { freeaddrinfo(request.ar_result); } - return gai_result; - } - } else if (wait_result == EAI_AGAIN) { - // Timeout occurred, cancel the request - gai_cancel(&request); - return EAI_AGAIN; - } else { - // Other error occurred - gai_cancel(&request); - return wait_result; + if (hMapping_) { + ::CloseHandle(hMapping_); + hMapping_ = NULL; } -#else - // Fallback implementation using thread-based timeout for other Unix systems - struct GetAddrInfoState { - ~GetAddrInfoState() { - if (info) { freeaddrinfo(info); } - } + if (hFile_ != INVALID_HANDLE_VALUE) { + ::CloseHandle(hFile_); + hFile_ = INVALID_HANDLE_VALUE; + } - std::mutex mutex; - std::condition_variable result_cv; - bool completed = false; - int result = EAI_SYSTEM; - std::string node; - std::string service; - struct addrinfo hints; - struct addrinfo *info = nullptr; - }; + is_open_empty_file = false; +#else + if (addr_ != nullptr) { + munmap(addr_, size_); + addr_ = nullptr; + } - // Allocate on the heap, so the resolver thread can keep using the data. - auto state = std::make_shared(); - state->node = node; - state->service = service; - state->hints = *hints; + if (fd_ != -1) { + ::close(fd_); + fd_ = -1; + } +#endif + size_ = 0; +} +inline int close_socket(socket_t sock) { +#ifdef _WIN32 + return closesocket(sock); +#else + return close(sock); +#endif +} - std::thread resolve_thread([state]() { - auto thread_result = - getaddrinfo(state->node.c_str(), state->service.c_str(), &state->hints, - &state->info); +template inline ssize_t handle_EINTR(T fn) { + ssize_t res = 0; + while (true) { + res = fn(); + if (res < 0 && errno == EINTR) { + std::this_thread::sleep_for(std::chrono::microseconds{1}); + continue; + } + break; + } + return res; +} - std::lock_guard lock(state->mutex); - state->result = thread_result; - state->completed = true; - state->result_cv.notify_one(); +inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { + return handle_EINTR([&]() { + return recv(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), +#else + ptr, size, +#endif + flags); }); +} - // Wait for completion or timeout - std::unique_lock lock(state->mutex); - auto finished = - state->result_cv.wait_for(lock, std::chrono::seconds(timeout_sec), - [&] { return state->completed; }); - - if (finished) { - // Operation completed within timeout - resolve_thread.join(); - *res = state->info; - state->info = nullptr; // Pass ownership to caller - return state->result; - } else { - // Timeout occurred - resolve_thread.detach(); // Let the thread finish in background - return EAI_AGAIN; // Return timeout error - } -#endif +inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, + int flags) { + return handle_EINTR([&]() { + return send(sock, +#ifdef _WIN32 + static_cast(ptr), static_cast(size), #else - (void)(timeout_sec); // Unused parameter for non-blocking getaddrinfo - return getaddrinfo(node, service, hints, res); + ptr, size, #endif + flags); + }); } -template -socket_t create_socket(const std::string &host, const std::string &ip, int port, - int address_family, int socket_flags, bool tcp_nodelay, - bool ipv6_v6only, SocketOptions socket_options, - BindOrConnect bind_or_connect, time_t timeout_sec = 0) { - // Get address info - const char *node = nullptr; - struct addrinfo hints; - struct addrinfo *result; - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_IP; - - if (!ip.empty()) { - node = ip.c_str(); - // Ask getaddrinfo to convert IP in c-string to address - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_NUMERICHOST; - } else { - if (!host.empty()) { node = host.c_str(); } - hints.ai_family = address_family; - hints.ai_flags = socket_flags; - } - -#if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) - if (hints.ai_family == AF_UNIX) { - const auto addrlen = host.length(); - if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } - -#ifdef SOCK_CLOEXEC - auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, - hints.ai_protocol); +inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { +#ifdef _WIN32 + return ::WSAPoll(fds, nfds, timeout); #else - auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); + return ::poll(fds, nfds, timeout); #endif +} - if (sock != INVALID_SOCKET) { - sockaddr_un addr{}; - addr.sun_family = AF_UNIX; +template +inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { +#ifdef __APPLE__ + if (sock >= FD_SETSIZE) { return -1; } - auto unescaped_host = unescape_abstract_namespace_unix_domain(host); - std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); + fd_set fds, *rfds, *wfds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + rfds = (Read ? &fds : nullptr); + wfds = (Read ? nullptr : &fds); - hints.ai_addr = reinterpret_cast(&addr); - hints.ai_addrlen = static_cast( - sizeof(addr) - sizeof(addr.sun_path) + addrlen); + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); -#ifndef SOCK_CLOEXEC -#ifndef _WIN32 - fcntl(sock, F_SETFD, FD_CLOEXEC); -#endif -#endif + return handle_EINTR([&]() { + return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); + }); +#else + struct pollfd pfd; + pfd.fd = sock; + pfd.events = (Read ? POLLIN : POLLOUT); - if (socket_options) { socket_options(sock); } + auto timeout = static_cast(sec * 1000 + usec / 1000); -#ifdef _WIN32 - // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so - // remove the option. - detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); + return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); }); #endif +} - bool dummy; - if (!bind_or_connect(sock, hints, dummy)) { - close_socket(sock); - sock = INVALID_SOCKET; - } - } - return sock; - } -#endif +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} - auto service = std::to_string(port); +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { + return select_impl(sock, sec, usec); +} - if (getaddrinfo_with_timeout(node, service.c_str(), &hints, &result, - timeout_sec)) { -#if defined __linux__ && !defined __ANDROID__ - res_init(); -#endif - return INVALID_SOCKET; - } - auto se = detail::scope_exit([&] { freeaddrinfo(result); }); +inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, + time_t usec) { +#ifdef __APPLE__ + if (sock >= FD_SETSIZE) { return Error::Connection; } - for (auto rp = result; rp; rp = rp->ai_next) { - // Create a socket -#ifdef _WIN32 - auto sock = - WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, - WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); - /** - * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 - * and above the socket creation fails on older Windows Systems. - * - * Let's try to create a socket the old way in this case. - * - * Reference: - * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa - * - * WSA_FLAG_NO_HANDLE_INHERIT: - * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with - * SP1, and later - * - */ - if (sock == INVALID_SOCKET) { - sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - } -#else + fd_set fdsr, fdsw; + FD_ZERO(&fdsr); + FD_ZERO(&fdsw); + FD_SET(sock, &fdsr); + FD_SET(sock, &fdsw); -#ifdef SOCK_CLOEXEC - auto sock = - socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); -#else - auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); -#endif + timeval tv; + tv.tv_sec = static_cast(sec); + tv.tv_usec = static_cast(usec); -#endif - if (sock == INVALID_SOCKET) { continue; } + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, nullptr, &tv); + }); -#if !defined _WIN32 && !defined SOCK_CLOEXEC - if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { - close_socket(sock); - continue; - } -#endif + if (ret == 0) { return Error::ConnectionTimeout; } - if (tcp_nodelay) { set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); } + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; + } - if (rp->ai_family == AF_INET6) { - set_socket_opt(sock, IPPROTO_IPV6, IPV6_V6ONLY, ipv6_v6only ? 1 : 0); - } + return Error::Connection; +#else + struct pollfd pfd_read; + pfd_read.fd = sock; + pfd_read.events = POLLIN | POLLOUT; - if (socket_options) { socket_options(sock); } + auto timeout = static_cast(sec * 1000 + usec / 1000); - // bind or connect - auto quit = false; - if (bind_or_connect(sock, *rp, quit)) { return sock; } + auto poll_res = + handle_EINTR([&]() { return poll_wrapper(&pfd_read, 1, timeout); }); - close_socket(sock); + if (poll_res == 0) { return Error::ConnectionTimeout; } - if (quit) { break; } + if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { + auto error = 0; + socklen_t len = sizeof(error); + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + auto successful = res >= 0 && !error; + return successful ? Error::Success : Error::Connection; } - return INVALID_SOCKET; + return Error::Connection; +#endif } -inline void set_nonblocking(socket_t sock, bool nonblocking) { -#ifdef _WIN32 - auto flags = nonblocking ? 1UL : 0UL; - ioctlsocket(sock, FIONBIO, &flags); -#else - auto flags = fcntl(sock, F_GETFL, 0); +inline bool is_socket_alive(socket_t sock) { + const auto val = detail::select_read(sock, 0, 0); + if (val == 0) { + return true; + } else if (val < 0 && errno == EBADF) { + return false; + } + char buf[1]; + return detail::read_socket(sock, &buf[0], sizeof(buf), MSG_PEEK) > 0; +} + +class SocketStream final : public Stream { +public: + SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + (std::chrono::steady_clock::time_point::min)()); + ~SocketStream() override; + + bool is_readable() const override; + bool wait_readable() const override; + bool wait_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time_; + + std::vector read_buff_; + size_t read_buff_off_ = 0; + size_t read_buff_content_size_ = 0; + + static const size_t read_buff_size_ = 1024l * 4; +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +class SSLSocketStream final : public Stream { +public: + SSLSocketStream( + socket_t sock, SSL *ssl, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + (std::chrono::steady_clock::time_point::min)()); + ~SSLSocketStream() override; + + bool is_readable() const override; + bool wait_readable() const override; + bool wait_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + SSL *ssl_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time_; +}; +#endif + +inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, + time_t keep_alive_timeout_sec) { + using namespace std::chrono; + + const auto interval_usec = + CPPHTTPLIB_KEEPALIVE_TIMEOUT_CHECK_INTERVAL_USECOND; + + // Avoid expensive `steady_clock::now()` call for the first time + if (select_read(sock, 0, interval_usec) > 0) { return true; } + + const auto start = steady_clock::now() - microseconds{interval_usec}; + const auto timeout = seconds{keep_alive_timeout_sec}; + + while (true) { + if (svr_sock == INVALID_SOCKET) { + break; // Server socket is closed + } + + auto val = select_read(sock, 0, interval_usec); + if (val < 0) { + break; // Ssocket error + } else if (val == 0) { + if (steady_clock::now() - start > timeout) { + break; // Timeout + } + } else { + return true; // Ready for read + } + } + + return false; +} + +template +inline bool +process_server_socket_core(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, T callback) { + assert(keep_alive_max_count > 0); + auto ret = false; + auto count = keep_alive_max_count; + while (count > 0 && keep_alive(svr_sock, sock, keep_alive_timeout_sec)) { + auto close_connection = count == 1; + auto connection_closed = false; + ret = callback(close_connection, connection_closed); + if (!ret || connection_closed) { break; } + count--; + } + return ret; +} + +template +inline bool +process_server_socket(const std::atomic &svr_sock, socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +inline bool process_client_socket( + socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + time_t max_timeout_msec, + std::chrono::time_point start_time, + std::function callback) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec, max_timeout_msec, + start_time); + return callback(strm); +} + +inline int shutdown_socket(socket_t sock) { +#ifdef _WIN32 + return shutdown(sock, SD_BOTH); +#else + return shutdown(sock, SHUT_RDWR); +#endif +} + +inline std::string escape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '\0') { + auto ret = s; + ret[0] = '@'; + return ret; + } + return s; +} + +inline std::string +unescape_abstract_namespace_unix_domain(const std::string &s) { + if (s.size() > 1 && s[0] == '@') { + auto ret = s; + ret[0] = '\0'; + return ret; + } + return s; +} + +inline int getaddrinfo_with_timeout(const char *node, const char *service, + const struct addrinfo *hints, + struct addrinfo **res, time_t timeout_sec) { +#ifdef CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO + if (timeout_sec <= 0) { + // No timeout specified, use standard getaddrinfo + return getaddrinfo(node, service, hints, res); + } + +#ifdef _WIN32 + // Windows-specific implementation using GetAddrInfoEx with overlapped I/O + OVERLAPPED overlapped = {0}; + HANDLE event = CreateEventW(nullptr, TRUE, FALSE, nullptr); + if (!event) { return EAI_FAIL; } + + overlapped.hEvent = event; + + PADDRINFOEXW result_addrinfo = nullptr; + HANDLE cancel_handle = nullptr; + + ADDRINFOEXW hints_ex = {0}; + if (hints) { + hints_ex.ai_flags = hints->ai_flags; + hints_ex.ai_family = hints->ai_family; + hints_ex.ai_socktype = hints->ai_socktype; + hints_ex.ai_protocol = hints->ai_protocol; + } + + auto wnode = u8string_to_wstring(node); + auto wservice = u8string_to_wstring(service); + + auto ret = ::GetAddrInfoExW(wnode.data(), wservice.data(), NS_DNS, nullptr, + hints ? &hints_ex : nullptr, &result_addrinfo, + nullptr, &overlapped, nullptr, &cancel_handle); + + if (ret == WSA_IO_PENDING) { + auto wait_result = + ::WaitForSingleObject(event, static_cast(timeout_sec * 1000)); + if (wait_result == WAIT_TIMEOUT) { + if (cancel_handle) { ::GetAddrInfoExCancel(&cancel_handle); } + ::CloseHandle(event); + return EAI_AGAIN; + } + + DWORD bytes_returned; + if (!::GetOverlappedResult((HANDLE)INVALID_SOCKET, &overlapped, + &bytes_returned, FALSE)) { + ::CloseHandle(event); + return ::WSAGetLastError(); + } + } + + ::CloseHandle(event); + + if (ret == NO_ERROR || ret == WSA_IO_PENDING) { + *res = reinterpret_cast(result_addrinfo); + return 0; + } + + return ret; +#elif TARGET_OS_MAC + // macOS implementation using CFHost API for asynchronous DNS resolution + CFStringRef hostname_ref = CFStringCreateWithCString( + kCFAllocatorDefault, node, kCFStringEncodingUTF8); + if (!hostname_ref) { return EAI_MEMORY; } + + CFHostRef host_ref = CFHostCreateWithName(kCFAllocatorDefault, hostname_ref); + CFRelease(hostname_ref); + if (!host_ref) { return EAI_MEMORY; } + + // Set up context for callback + struct CFHostContext { + bool completed = false; + bool success = false; + CFArrayRef addresses = nullptr; + std::mutex mutex; + std::condition_variable cv; + } context; + + CFHostClientContext client_context; + memset(&client_context, 0, sizeof(client_context)); + client_context.info = &context; + + // Set callback + auto callback = [](CFHostRef theHost, CFHostInfoType /*typeInfo*/, + const CFStreamError *error, void *info) { + auto ctx = static_cast(info); + std::lock_guard lock(ctx->mutex); + + if (error && error->error != 0) { + ctx->success = false; + } else { + Boolean hasBeenResolved; + ctx->addresses = CFHostGetAddressing(theHost, &hasBeenResolved); + if (ctx->addresses && hasBeenResolved) { + CFRetain(ctx->addresses); + ctx->success = true; + } else { + ctx->success = false; + } + } + ctx->completed = true; + ctx->cv.notify_one(); + }; + + if (!CFHostSetClient(host_ref, callback, &client_context)) { + CFRelease(host_ref); + return EAI_SYSTEM; + } + + // Schedule on run loop + CFRunLoopRef run_loop = CFRunLoopGetCurrent(); + CFHostScheduleWithRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + + // Start resolution + CFStreamError stream_error; + if (!CFHostStartInfoResolution(host_ref, kCFHostAddresses, &stream_error)) { + CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + CFRelease(host_ref); + return EAI_FAIL; + } + + // Wait for completion with timeout + auto timeout_time = + std::chrono::steady_clock::now() + std::chrono::seconds(timeout_sec); + bool timed_out = false; + + { + std::unique_lock lock(context.mutex); + + while (!context.completed) { + auto now = std::chrono::steady_clock::now(); + if (now >= timeout_time) { + timed_out = true; + break; + } + + // Run the runloop for a short time + lock.unlock(); + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true); + lock.lock(); + } + } + + // Clean up + CFHostUnscheduleFromRunLoop(host_ref, run_loop, kCFRunLoopDefaultMode); + CFHostSetClient(host_ref, nullptr, nullptr); + + if (timed_out || !context.completed) { + CFHostCancelInfoResolution(host_ref, kCFHostAddresses); + CFRelease(host_ref); + return EAI_AGAIN; + } + + if (!context.success || !context.addresses) { + CFRelease(host_ref); + return EAI_NODATA; + } + + // Convert CFArray to addrinfo + CFIndex count = CFArrayGetCount(context.addresses); + if (count == 0) { + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_NODATA; + } + + struct addrinfo *result_addrinfo = nullptr; + struct addrinfo **current = &result_addrinfo; + + for (CFIndex i = 0; i < count; i++) { + CFDataRef addr_data = + static_cast(CFArrayGetValueAtIndex(context.addresses, i)); + if (!addr_data) continue; + + const struct sockaddr *sockaddr_ptr = + reinterpret_cast(CFDataGetBytePtr(addr_data)); + socklen_t sockaddr_len = static_cast(CFDataGetLength(addr_data)); + + // Allocate addrinfo structure + *current = static_cast(malloc(sizeof(struct addrinfo))); + if (!*current) { + freeaddrinfo(result_addrinfo); + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_MEMORY; + } + + memset(*current, 0, sizeof(struct addrinfo)); + + // Set up addrinfo fields + (*current)->ai_family = sockaddr_ptr->sa_family; + (*current)->ai_socktype = hints ? hints->ai_socktype : SOCK_STREAM; + (*current)->ai_protocol = hints ? hints->ai_protocol : IPPROTO_TCP; + (*current)->ai_addrlen = sockaddr_len; + + // Copy sockaddr + (*current)->ai_addr = static_cast(malloc(sockaddr_len)); + if (!(*current)->ai_addr) { + freeaddrinfo(result_addrinfo); + CFRelease(context.addresses); + CFRelease(host_ref); + return EAI_MEMORY; + } + memcpy((*current)->ai_addr, sockaddr_ptr, sockaddr_len); + + // Set port if service is specified + if (service && strlen(service) > 0) { + int port = atoi(service); + if (port > 0) { + if (sockaddr_ptr->sa_family == AF_INET) { + reinterpret_cast((*current)->ai_addr) + ->sin_port = htons(static_cast(port)); + } else if (sockaddr_ptr->sa_family == AF_INET6) { + reinterpret_cast((*current)->ai_addr) + ->sin6_port = htons(static_cast(port)); + } + } + } + + current = &((*current)->ai_next); + } + + CFRelease(context.addresses); + CFRelease(host_ref); + + *res = result_addrinfo; + return 0; +#elif defined(_GNU_SOURCE) && defined(__GLIBC__) && \ + (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) + // Linux implementation using getaddrinfo_a for asynchronous DNS resolution + struct gaicb request; + struct gaicb *requests[1] = {&request}; + struct sigevent sevp; + struct timespec timeout; + + // Initialize the request structure + memset(&request, 0, sizeof(request)); + request.ar_name = node; + request.ar_service = service; + request.ar_request = hints; + + // Set up timeout + timeout.tv_sec = timeout_sec; + timeout.tv_nsec = 0; + + // Initialize sigevent structure (not used, but required) + memset(&sevp, 0, sizeof(sevp)); + sevp.sigev_notify = SIGEV_NONE; + + // Start asynchronous resolution + int start_result = getaddrinfo_a(GAI_NOWAIT, requests, 1, &sevp); + if (start_result != 0) { return start_result; } + + // Wait for completion with timeout + int wait_result = + gai_suspend((const struct gaicb *const *)requests, 1, &timeout); + + if (wait_result == 0 || wait_result == EAI_ALLDONE) { + // Completed successfully, get the result + int gai_result = gai_error(&request); + if (gai_result == 0) { + *res = request.ar_result; + return 0; + } else { + // Clean up on error + if (request.ar_result) { freeaddrinfo(request.ar_result); } + return gai_result; + } + } else if (wait_result == EAI_AGAIN) { + // Timeout occurred, cancel the request + gai_cancel(&request); + return EAI_AGAIN; + } else { + // Other error occurred + gai_cancel(&request); + return wait_result; + } +#else + // Fallback implementation using thread-based timeout for other Unix systems + + struct GetAddrInfoState { + ~GetAddrInfoState() { + if (info) { freeaddrinfo(info); } + } + + std::mutex mutex; + std::condition_variable result_cv; + bool completed = false; + int result = EAI_SYSTEM; + std::string node; + std::string service; + struct addrinfo hints; + struct addrinfo *info = nullptr; + }; + + // Allocate on the heap, so the resolver thread can keep using the data. + auto state = std::make_shared(); + state->node = node; + state->service = service; + state->hints = *hints; + + std::thread resolve_thread([state]() { + auto thread_result = + getaddrinfo(state->node.c_str(), state->service.c_str(), &state->hints, + &state->info); + + std::lock_guard lock(state->mutex); + state->result = thread_result; + state->completed = true; + state->result_cv.notify_one(); + }); + + // Wait for completion or timeout + std::unique_lock lock(state->mutex); + auto finished = + state->result_cv.wait_for(lock, std::chrono::seconds(timeout_sec), + [&] { return state->completed; }); + + if (finished) { + // Operation completed within timeout + resolve_thread.join(); + *res = state->info; + state->info = nullptr; // Pass ownership to caller + return state->result; + } else { + // Timeout occurred + resolve_thread.detach(); // Let the thread finish in background + return EAI_AGAIN; // Return timeout error + } +#endif +#else + (void)(timeout_sec); // Unused parameter for non-blocking getaddrinfo + return getaddrinfo(node, service, hints, res); +#endif +} + +template +socket_t create_socket(const std::string &host, const std::string &ip, int port, + int address_family, int socket_flags, bool tcp_nodelay, + bool ipv6_v6only, SocketOptions socket_options, + BindOrConnect bind_or_connect, time_t timeout_sec = 0) { + // Get address info + const char *node = nullptr; + struct addrinfo hints; + struct addrinfo *result; + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_IP; + + if (!ip.empty()) { + node = ip.c_str(); + // Ask getaddrinfo to convert IP in c-string to address + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_NUMERICHOST; + } else { + if (!host.empty()) { node = host.c_str(); } + hints.ai_family = address_family; + hints.ai_flags = socket_flags; + } + +#if !defined(_WIN32) || defined(CPPHTTPLIB_HAVE_AFUNIX_H) + if (hints.ai_family == AF_UNIX) { + const auto addrlen = host.length(); + if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; } + +#ifdef SOCK_CLOEXEC + auto sock = socket(hints.ai_family, hints.ai_socktype | SOCK_CLOEXEC, + hints.ai_protocol); +#else + auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); +#endif + + if (sock != INVALID_SOCKET) { + sockaddr_un addr{}; + addr.sun_family = AF_UNIX; + + auto unescaped_host = unescape_abstract_namespace_unix_domain(host); + std::copy(unescaped_host.begin(), unescaped_host.end(), addr.sun_path); + + hints.ai_addr = reinterpret_cast(&addr); + hints.ai_addrlen = static_cast( + sizeof(addr) - sizeof(addr.sun_path) + addrlen); + +#ifndef SOCK_CLOEXEC +#ifndef _WIN32 + fcntl(sock, F_SETFD, FD_CLOEXEC); +#endif +#endif + + if (socket_options) { socket_options(sock); } + +#ifdef _WIN32 + // Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so + // remove the option. + detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0); +#endif + + bool dummy; + if (!bind_or_connect(sock, hints, dummy)) { + close_socket(sock); + sock = INVALID_SOCKET; + } + } + return sock; + } +#endif + + auto service = std::to_string(port); + + if (getaddrinfo_with_timeout(node, service.c_str(), &hints, &result, + timeout_sec)) { +#if defined __linux__ && !defined __ANDROID__ + res_init(); +#endif + return INVALID_SOCKET; + } + auto se = detail::scope_exit([&] { freeaddrinfo(result); }); + + for (auto rp = result; rp; rp = rp->ai_next) { + // Create a socket +#ifdef _WIN32 + auto sock = + WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, nullptr, 0, + WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED); + /** + * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1 + * and above the socket creation fails on older Windows Systems. + * + * Let's try to create a socket the old way in this case. + * + * Reference: + * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa + * + * WSA_FLAG_NO_HANDLE_INHERIT: + * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with + * SP1, and later + * + */ + if (sock == INVALID_SOCKET) { + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + } +#else + +#ifdef SOCK_CLOEXEC + auto sock = + socket(rp->ai_family, rp->ai_socktype | SOCK_CLOEXEC, rp->ai_protocol); +#else + auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); +#endif + +#endif + if (sock == INVALID_SOCKET) { continue; } + +#if !defined _WIN32 && !defined SOCK_CLOEXEC + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { + close_socket(sock); + continue; + } +#endif + + if (tcp_nodelay) { set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); } + + if (rp->ai_family == AF_INET6) { + set_socket_opt(sock, IPPROTO_IPV6, IPV6_V6ONLY, ipv6_v6only ? 1 : 0); + } + + if (socket_options) { socket_options(sock); } + + // bind or connect + auto quit = false; + if (bind_or_connect(sock, *rp, quit)) { return sock; } + + close_socket(sock); + + if (quit) { break; } + } + + return INVALID_SOCKET; +} + +inline void set_nonblocking(socket_t sock, bool nonblocking) { +#ifdef _WIN32 + auto flags = nonblocking ? 1UL : 0UL; + ioctlsocket(sock, FIONBIO, &flags); +#else + auto flags = fcntl(sock, F_GETFL, 0); fcntl(sock, F_SETFL, nonblocking ? (flags | O_NONBLOCK) : (flags & (~O_NONBLOCK))); #endif @@ -13020,604 +13670,235 @@ inline Result Client::Patch(const std::string &path, const Headers &headers, UploadProgress progress) { return cli_->Patch(path, headers, items, boundary, progress); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const FormDataProviderItems &provider_items, - UploadProgress progress) { - return cli_->Patch(path, headers, items, provider_items, progress); -} -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Patch(path, headers, body, content_type, content_receiver, - progress); -} - -inline Result Client::Delete(const std::string &path, - DownloadProgress progress) { - return cli_->Delete(path, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - DownloadProgress progress) { - return cli_->Delete(path, headers, progress); -} -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, body, content_length, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, body, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, headers, body, content_type, progress); -} -inline Result Client::Delete(const std::string &path, const Params ¶ms, - DownloadProgress progress) { - return cli_->Delete(path, params, progress); -} -inline Result Client::Delete(const std::string &path, const Headers &headers, - const Params ¶ms, DownloadProgress progress) { - return cli_->Delete(path, headers, params, progress); -} - -inline Result Client::Options(const std::string &path) { - return cli_->Options(path); -} -inline Result Client::Options(const std::string &path, const Headers &headers) { - return cli_->Options(path, headers); -} - -inline ClientImpl::StreamHandle -Client::open_stream(const std::string &method, const std::string &path, - const Params ¶ms, const Headers &headers, - const std::string &body, const std::string &content_type) { - return cli_->open_stream(method, path, params, headers, body, content_type); -} - -inline bool Client::send(Request &req, Response &res, Error &error) { - return cli_->send(req, res, error); -} - -inline Result Client::send(const Request &req) { return cli_->send(req); } - -inline void Client::stop() { cli_->stop(); } - -inline std::string Client::host() const { return cli_->host(); } - -inline int Client::port() const { return cli_->port(); } - -inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } - -inline socket_t Client::socket() const { return cli_->socket(); } - -inline void -Client::set_hostname_addr_map(std::map addr_map) { - cli_->set_hostname_addr_map(std::move(addr_map)); -} - -inline void Client::set_default_headers(Headers headers) { - cli_->set_default_headers(std::move(headers)); -} - -inline void Client::set_header_writer( - std::function const &writer) { - cli_->set_header_writer(writer); -} - -inline void Client::set_address_family(int family) { - cli_->set_address_family(family); -} - -inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } - -inline void Client::set_socket_options(SocketOptions socket_options) { - cli_->set_socket_options(std::move(socket_options)); -} - -inline void Client::set_connection_timeout(time_t sec, time_t usec) { - cli_->set_connection_timeout(sec, usec); -} - -inline void Client::set_read_timeout(time_t sec, time_t usec) { - cli_->set_read_timeout(sec, usec); -} - -inline void Client::set_write_timeout(time_t sec, time_t usec) { - cli_->set_write_timeout(sec, usec); -} - -inline void Client::set_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_basic_auth(username, password); -} -inline void Client::set_bearer_token_auth(const std::string &token) { - cli_->set_bearer_token_auth(token); -} -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_digest_auth(username, password); -} -#endif - -inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } -inline void Client::set_follow_location(bool on) { - cli_->set_follow_location(on); -} - -inline void Client::set_path_encode(bool on) { cli_->set_path_encode(on); } - -[[deprecated("Use set_path_encode instead")]] -inline void Client::set_url_encode(bool on) { - cli_->set_path_encode(on); -} - -inline void Client::set_compress(bool on) { cli_->set_compress(on); } - -inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } - -inline void Client::set_interface(const std::string &intf) { - cli_->set_interface(intf); -} - -inline void Client::set_proxy(const std::string &host, int port) { - cli_->set_proxy(host, port); +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, provider_items, progress); } -inline void Client::set_proxy_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_basic_auth(username, password); +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Patch(path, headers, body, content_type, content_receiver, + progress); } -inline void Client::set_proxy_bearer_token_auth(const std::string &token) { - cli_->set_proxy_bearer_token_auth(token); + +inline Result Client::Delete(const std::string &path, + DownloadProgress progress) { + return cli_->Delete(path, progress); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_digest_auth(username, password); +inline Result Client::Delete(const std::string &path, const Headers &headers, + DownloadProgress progress) { + return cli_->Delete(path, headers, progress); } -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::enable_server_certificate_verification(bool enabled) { - cli_->enable_server_certificate_verification(enabled); +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, body, content_length, content_type, progress); } - -inline void Client::enable_server_hostname_verification(bool enabled) { - cli_->enable_server_hostname_verification(enabled); +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, + progress); } - -inline void Client::set_server_certificate_verifier( - std::function verifier) { - cli_->set_server_certificate_verifier(verifier); +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, body, content_type, progress); } -#endif - -inline void Client::set_logger(Logger logger) { - cli_->set_logger(std::move(logger)); +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, headers, body, content_type, progress); } - -inline void Client::set_error_logger(ErrorLogger error_logger) { - cli_->set_error_logger(std::move(error_logger)); +inline Result Client::Delete(const std::string &path, const Params ¶ms, + DownloadProgress progress) { + return cli_->Delete(path, params, progress); } - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { - cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms, DownloadProgress progress) { + return cli_->Delete(path, headers, params, progress); } -inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (is_ssl_) { - static_cast(*cli_).set_ca_cert_store(ca_cert_store); - } else { - cli_->set_ca_cert_store(ca_cert_store); - } +inline Result Client::Options(const std::string &path) { + return cli_->Options(path); } - -inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { - set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); +inline Result Client::Options(const std::string &path, const Headers &headers) { + return cli_->Options(path, headers); } -inline long Client::get_openssl_verify_result() const { - if (is_ssl_) { - return static_cast(*cli_).get_openssl_verify_result(); - } - return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +inline ClientImpl::StreamHandle +Client::open_stream(const std::string &method, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type) { + return cli_->open_stream(method, path, params, headers, body, content_type); } -inline SSL_CTX *Client::ssl_context() const { - if (is_ssl_) { return static_cast(*cli_).ssl_context(); } - return nullptr; +inline bool Client::send(Request &req, Response &res, Error &error) { + return cli_->send(req, res, error); } -#endif - -// ---------------------------------------------------------------------------- - -/* - * C++11/14/17 Streaming API - * - * This section provides iterator-style streaming functionality for C++11/14/17. - * For C++20 and later, a coroutine-based API with range-for syntax is - * available. - * - * Usage: - * httplib::Client cli("example.com"); - * auto result = httplib::stream::Get(cli, "/large-file"); - * if (result) { - * while (result.next()) { - * process(result.data(), result.size()); - * } - * } - */ - -namespace stream { - -class Result { -public: - Result() : chunk_size_(8192) {} - - explicit Result(ClientImpl::StreamHandle &&handle, size_t chunk_size = 8192) - : handle_(std::move(handle)), chunk_size_(chunk_size) {} - - Result(Result &&other) noexcept - : handle_(std::move(other.handle_)), buffer_(std::move(other.buffer_)), - current_size_(other.current_size_), chunk_size_(other.chunk_size_), - finished_(other.finished_) { - other.current_size_ = 0; - other.finished_ = true; - } - - Result &operator=(Result &&other) noexcept { - if (this != &other) { - handle_ = std::move(other.handle_); - buffer_ = std::move(other.buffer_); - current_size_ = other.current_size_; - chunk_size_ = other.chunk_size_; - finished_ = other.finished_; - other.current_size_ = 0; - other.finished_ = true; - } - return *this; - } - - Result(const Result &) = delete; - Result &operator=(const Result &) = delete; - - // Check if the result is valid (connection succeeded and response received) - bool is_valid() const { return handle_.is_valid(); } - explicit operator bool() const { return is_valid(); } - - // Response status code - int status() const { - return handle_.response ? handle_.response->status : -1; - } - - // Response headers - const Headers &headers() const { - static const Headers empty_headers; - return handle_.response ? handle_.response->headers : empty_headers; - } - - std::string get_header_value(const std::string &key, - const char *def = "") const { - return handle_.response ? handle_.response->get_header_value(key, def) - : def; - } - - bool has_header(const std::string &key) const { - return handle_.response ? handle_.response->has_header(key) : false; - } - - // Error information - Error error() const { return handle_.error; } - Error read_error() const { return handle_.get_read_error(); } - bool has_read_error() const { return handle_.has_read_error(); } - - // Streaming iteration API - // Call next() to read the next chunk, then access data via data()/size() - // Returns true if data was read, false when stream is exhausted - bool next() { - if (!handle_.is_valid() || finished_) { return false; } - - if (buffer_.size() < chunk_size_) { buffer_.resize(chunk_size_); } - - ssize_t n = handle_.read(&buffer_[0], chunk_size_); - if (n > 0) { - current_size_ = static_cast(n); - return true; - } - - current_size_ = 0; - finished_ = true; - return false; - } - - // Pointer to current chunk data (valid after next() returns true) - const char *data() const { return buffer_.data(); } - - // Size of current chunk (valid after next() returns true) - size_t size() const { return current_size_; } - - // Convenience method: read all remaining data into a string - std::string read_all() { - std::string result; - while (next()) { - result.append(data(), size()); - } - return result; - } -private: - ClientImpl::StreamHandle handle_; - std::string buffer_; - size_t current_size_ = 0; - size_t chunk_size_; - bool finished_ = false; -}; +inline Result Client::send(const Request &req) { return cli_->send(req); } -// GET -template -inline Result Get(ClientType &cli, const std::string &path, - size_t chunk_size = 8192) { - return Result{cli.open_stream("GET", path), chunk_size}; -} +inline void Client::stop() { cli_->stop(); } -template -inline Result Get(ClientType &cli, const std::string &path, - const Headers &headers, size_t chunk_size = 8192) { - return Result{cli.open_stream("GET", path, {}, headers), chunk_size}; -} +inline std::string Client::host() const { return cli_->host(); } -template -inline Result Get(ClientType &cli, const std::string &path, - const Params ¶ms, size_t chunk_size = 8192) { - return Result{cli.open_stream("GET", path, params), chunk_size}; -} +inline int Client::port() const { return cli_->port(); } -template -inline Result Get(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - size_t chunk_size = 8192) { - return Result{cli.open_stream("GET", path, params, headers), chunk_size}; -} +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } -// POST -template -inline Result Post(ClientType &cli, const std::string &path, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{cli.open_stream("POST", path, {}, {}, body, content_type), - chunk_size}; -} +inline socket_t Client::socket() const { return cli_->socket(); } -template -inline Result Post(ClientType &cli, const std::string &path, - const Headers &headers, const std::string &body, - const std::string &content_type, size_t chunk_size = 8192) { - return Result{cli.open_stream("POST", path, {}, headers, body, content_type), - chunk_size}; +inline void +Client::set_hostname_addr_map(std::map addr_map) { + cli_->set_hostname_addr_map(std::move(addr_map)); } -template -inline Result Post(ClientType &cli, const std::string &path, - const Params ¶ms, const std::string &body, - const std::string &content_type, size_t chunk_size = 8192) { - return Result{cli.open_stream("POST", path, params, {}, body, content_type), - chunk_size}; +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); } -template -inline Result Post(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{ - cli.open_stream("POST", path, params, headers, body, content_type), - chunk_size}; +inline void Client::set_header_writer( + std::function const &writer) { + cli_->set_header_writer(writer); } -// PUT -template -inline Result Put(ClientType &cli, const std::string &path, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{cli.open_stream("PUT", path, {}, {}, body, content_type), - chunk_size}; +inline void Client::set_address_family(int family) { + cli_->set_address_family(family); } -template -inline Result Put(ClientType &cli, const std::string &path, - const Headers &headers, const std::string &body, - const std::string &content_type, size_t chunk_size = 8192) { - return Result{cli.open_stream("PUT", path, {}, headers, body, content_type), - chunk_size}; -} +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } -template -inline Result Put(ClientType &cli, const std::string &path, - const Params ¶ms, const std::string &body, - const std::string &content_type, size_t chunk_size = 8192) { - return Result{cli.open_stream("PUT", path, params, {}, body, content_type), - chunk_size}; +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(std::move(socket_options)); } -template -inline Result Put(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{ - cli.open_stream("PUT", path, params, headers, body, content_type), - chunk_size}; +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); } -// PATCH -template -inline Result Patch(ClientType &cli, const std::string &path, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{cli.open_stream("PATCH", path, {}, {}, body, content_type), - chunk_size}; +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); } -template -inline Result Patch(ClientType &cli, const std::string &path, - const Headers &headers, const std::string &body, - const std::string &content_type, size_t chunk_size = 8192) { - return Result{cli.open_stream("PATCH", path, {}, headers, body, content_type), - chunk_size}; +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); } -template -inline Result Patch(ClientType &cli, const std::string &path, - const Params ¶ms, const std::string &body, - const std::string &content_type, size_t chunk_size = 8192) { - return Result{cli.open_stream("PATCH", path, params, {}, body, content_type), - chunk_size}; +inline void Client::set_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_basic_auth(username, password); } - -template -inline Result Patch(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{ - cli.open_stream("PATCH", path, params, headers, body, content_type), - chunk_size}; +inline void Client::set_bearer_token_auth(const std::string &token) { + cli_->set_bearer_token_auth(token); } - -// DELETE -template -inline Result Delete(ClientType &cli, const std::string &path, - size_t chunk_size = 8192) { - return Result{cli.open_stream("DELETE", path), chunk_size}; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_digest_auth(username, password); } +#endif -template -inline Result Delete(ClientType &cli, const std::string &path, - const Headers &headers, size_t chunk_size = 8192) { - return Result{cli.open_stream("DELETE", path, {}, headers), chunk_size}; +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); } -template -inline Result Delete(ClientType &cli, const std::string &path, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{cli.open_stream("DELETE", path, {}, {}, body, content_type), - chunk_size}; -} +inline void Client::set_path_encode(bool on) { cli_->set_path_encode(on); } -template -inline Result Delete(ClientType &cli, const std::string &path, - const Headers &headers, const std::string &body, - const std::string &content_type, - size_t chunk_size = 8192) { - return Result{ - cli.open_stream("DELETE", path, {}, headers, body, content_type), - chunk_size}; +[[deprecated("Use set_path_encode instead")]] +inline void Client::set_url_encode(bool on) { + cli_->set_path_encode(on); } -template -inline Result Delete(ClientType &cli, const std::string &path, - const Params ¶ms, size_t chunk_size = 8192) { - return Result{cli.open_stream("DELETE", path, params), chunk_size}; +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const std::string &intf) { + cli_->set_interface(intf); } -template -inline Result Delete(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - size_t chunk_size = 8192) { - return Result{cli.open_stream("DELETE", path, params, headers), chunk_size}; +inline void Client::set_proxy(const std::string &host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const std::string &token) { + cli_->set_proxy_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_digest_auth(username, password); } +#endif -template -inline Result Delete(ClientType &cli, const std::string &path, - const Params ¶ms, const std::string &body, - const std::string &content_type, - size_t chunk_size = 8192) { - return Result{cli.open_stream("DELETE", path, params, {}, body, content_type), - chunk_size}; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); } -template -inline Result Delete(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - const std::string &body, const std::string &content_type, - size_t chunk_size = 8192) { - return Result{ - cli.open_stream("DELETE", path, params, headers, body, content_type), - chunk_size}; +inline void Client::enable_server_hostname_verification(bool enabled) { + cli_->enable_server_hostname_verification(enabled); } -// HEAD -template -inline Result Head(ClientType &cli, const std::string &path, - size_t chunk_size = 8192) { - return Result{cli.open_stream("HEAD", path), chunk_size}; +inline void Client::set_server_certificate_verifier( + std::function verifier) { + cli_->set_server_certificate_verifier(verifier); } +#endif -template -inline Result Head(ClientType &cli, const std::string &path, - const Headers &headers, size_t chunk_size = 8192) { - return Result{cli.open_stream("HEAD", path, {}, headers), chunk_size}; +inline void Client::set_logger(Logger logger) { + cli_->set_logger(std::move(logger)); } -template -inline Result Head(ClientType &cli, const std::string &path, - const Params ¶ms, size_t chunk_size = 8192) { - return Result{cli.open_stream("HEAD", path, params), chunk_size}; +inline void Client::set_error_logger(ErrorLogger error_logger) { + cli_->set_error_logger(std::move(error_logger)); } -template -inline Result Head(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - size_t chunk_size = 8192) { - return Result{cli.open_stream("HEAD", path, params, headers), chunk_size}; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); } -// OPTIONS -template -inline Result Options(ClientType &cli, const std::string &path, - size_t chunk_size = 8192) { - return Result{cli.open_stream("OPTIONS", path), chunk_size}; +inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } else { + cli_->set_ca_cert_store(ca_cert_store); + } } -template -inline Result Options(ClientType &cli, const std::string &path, - const Headers &headers, size_t chunk_size = 8192) { - return Result{cli.open_stream("OPTIONS", path, {}, headers), chunk_size}; +inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { + set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); } -template -inline Result Options(ClientType &cli, const std::string &path, - const Params ¶ms, size_t chunk_size = 8192) { - return Result{cli.open_stream("OPTIONS", path, params), chunk_size}; +inline long Client::get_openssl_verify_result() const { + if (is_ssl_) { + return static_cast(*cli_).get_openssl_verify_result(); + } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? } -template -inline Result Options(ClientType &cli, const std::string &path, - const Params ¶ms, const Headers &headers, - size_t chunk_size = 8192) { - return Result{cli.open_stream("OPTIONS", path, params, headers), chunk_size}; +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; } +#endif + +// ---------------------------------------------------------------------------- -} // namespace stream } // namespace httplib #endif // CPPHTTPLIB_HTTPLIB_H diff --git a/test/test.cc b/test/test.cc index 035f0509a0..d99b8a533a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -13255,3 +13255,729 @@ TEST(ETagTest, NegativeFileModificationTime) { t.join(); std::remove(fname); } + +//============================================================================== +// SSE Parsing Tests +//============================================================================== + +class SSEParsingTest : public ::testing::Test { +protected: + // Test helper that mimics SSE parsing behavior + static bool parse_sse_line(const std::string &line, sse::SSEMessage &msg, + int &retry_ms) { + // Blank line signals end of event + if (line.empty() || line == "\r") { return true; } + + // Lines starting with ':' are comments (ignored) + if (!line.empty() && line[0] == ':') { return false; } + + // Find the colon separator + auto colon_pos = line.find(':'); + if (colon_pos == std::string::npos) { + // Line with no colon is treated as field name with empty value + return false; + } + + std::string field = line.substr(0, colon_pos); + std::string value; + + // Value starts after colon, skip optional single space + if (colon_pos + 1 < line.size()) { + size_t value_start = colon_pos + 1; + if (line[value_start] == ' ') { value_start++; } + value = line.substr(value_start); + // Remove trailing \r if present + if (!value.empty() && value.back() == '\r') { value.pop_back(); } + } + + // Handle known fields + if (field == "event") { + msg.event = value; + } else if (field == "data") { + // Multiple data lines are concatenated with newlines + if (!msg.data.empty()) { msg.data += "\n"; } + msg.data += value; + } else if (field == "id") { + // Empty id is valid (clears the last event ID) + msg.id = value; + } else if (field == "retry") { + // Parse retry interval in milliseconds + try { + retry_ms = std::stoi(value); + } catch (...) { + // Invalid retry value, ignore + } + } + // Unknown fields are ignored per SSE spec + + return false; + } +}; + +// Test: Single-line data +TEST_F(SSEParsingTest, SingleLineData) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("data: hello", msg, retry_ms)); + EXPECT_EQ(msg.data, "hello"); + EXPECT_EQ(msg.event, "message"); + + // Blank line ends event + EXPECT_TRUE(parse_sse_line("", msg, retry_ms)); +} + +// Test: Multi-line data +TEST_F(SSEParsingTest, MultiLineData) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("data: line1", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("data: line2", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("data: line3", msg, retry_ms)); + EXPECT_EQ(msg.data, "line1\nline2\nline3"); +} + +// Test: Custom event types +TEST_F(SSEParsingTest, CustomEventType) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("event: update", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("data: payload", msg, retry_ms)); + EXPECT_EQ(msg.event, "update"); + EXPECT_EQ(msg.data, "payload"); +} + +// Test: Event ID handling +TEST_F(SSEParsingTest, EventIdHandling) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("id: 12345", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("data: test", msg, retry_ms)); + EXPECT_EQ(msg.id, "12345"); +} + +// Test: Empty event ID (clears last event ID) +TEST_F(SSEParsingTest, EmptyEventId) { + sse::SSEMessage msg; + msg.id = "previous"; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("id:", msg, retry_ms)); + EXPECT_EQ(msg.id, ""); +} + +// Test: Retry field parsing +TEST_F(SSEParsingTest, RetryFieldParsing) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("retry: 5000", msg, retry_ms)); + EXPECT_EQ(retry_ms, 5000); +} + +// Test: Invalid retry value +TEST_F(SSEParsingTest, InvalidRetryValue) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("retry: invalid", msg, retry_ms)); + EXPECT_EQ(retry_ms, 3000); // Unchanged +} + +// Test: Comments (lines starting with :) +TEST_F(SSEParsingTest, CommentsIgnored) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line(": this is a comment", msg, retry_ms)); + EXPECT_EQ(msg.data, ""); + EXPECT_EQ(msg.event, "message"); +} + +// Test: Colon in value +TEST_F(SSEParsingTest, ColonInValue) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("data: hello:world:test", msg, retry_ms)); + EXPECT_EQ(msg.data, "hello:world:test"); +} + +// Test: Line with no colon (field name only) +TEST_F(SSEParsingTest, FieldNameOnly) { + sse::SSEMessage msg; + int retry_ms = 3000; + + // According to SSE spec, this is treated as field name with empty value + EXPECT_FALSE(parse_sse_line("data", msg, retry_ms)); + // Since we don't recognize "data" without colon, data should be empty + EXPECT_EQ(msg.data, ""); +} + +// Test: Trailing \r handling +TEST_F(SSEParsingTest, TrailingCarriageReturn) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("data: hello\r", msg, retry_ms)); + EXPECT_EQ(msg.data, "hello"); +} + +// Test: Unknown fields ignored +TEST_F(SSEParsingTest, UnknownFieldsIgnored) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("unknown: value", msg, retry_ms)); + EXPECT_EQ(msg.data, ""); + EXPECT_EQ(msg.event, "message"); +} + +// Test: Space after colon is optional +TEST_F(SSEParsingTest, SpaceAfterColonOptional) { + sse::SSEMessage msg1, msg2; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("data: hello", msg1, retry_ms)); + EXPECT_FALSE(parse_sse_line("data:hello", msg2, retry_ms)); + EXPECT_EQ(msg1.data, "hello"); + EXPECT_EQ(msg2.data, "hello"); +} + +// Test: SSEMessage clear +TEST_F(SSEParsingTest, MessageClear) { + sse::SSEMessage msg; + msg.event = "custom"; + msg.data = "some data"; + msg.id = "123"; + + msg.clear(); + + EXPECT_EQ(msg.event, "message"); + EXPECT_EQ(msg.data, ""); + EXPECT_EQ(msg.id, ""); +} + +// Test: Complete event parsing +TEST_F(SSEParsingTest, CompleteEventParsing) { + sse::SSEMessage msg; + int retry_ms = 3000; + + EXPECT_FALSE(parse_sse_line("event: notification", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("id: evt-42", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("data: {\"type\":\"alert\"}", msg, retry_ms)); + EXPECT_FALSE(parse_sse_line("retry: 1000", msg, retry_ms)); + + // Blank line ends event + EXPECT_TRUE(parse_sse_line("", msg, retry_ms)); + + EXPECT_EQ(msg.event, "notification"); + EXPECT_EQ(msg.id, "evt-42"); + EXPECT_EQ(msg.data, "{\"type\":\"alert\"}"); + EXPECT_EQ(retry_ms, 1000); +} + +//============================================================================== +// Integration Tests with Server +//============================================================================== + +class SSEIntegrationTest : public ::testing::Test { +protected: + void SetUp() override { + stop_server_.store(false); + events_.clear(); + server_ = httplib::detail::make_unique(); + setup_server(); + start_server(); + } + + void TearDown() override { + stop_server_.store(true); + event_cv_.notify_all(); + server_->stop(); + if (server_thread_.joinable()) { server_thread_.join(); } + } + + void setup_server() { + // Simple SSE endpoint + server_->Get("/events", [this](const Request &req, Response &res) { + auto last_id = req.get_header_value("Last-Event-ID"); + if (!last_id.empty()) { last_received_event_id_ = last_id; } + + res.set_chunked_content_provider( + "text/event-stream", [this](size_t /*offset*/, DataSink &sink) { + std::unique_lock lock(event_mutex_); + if (event_cv_.wait_for( + lock, std::chrono::milliseconds(200), [this] { + return !events_.empty() || stop_server_.load(); + })) { + if (stop_server_.load()) { return false; } + if (!events_.empty()) { + std::string event = events_.front(); + events_.erase(events_.begin()); + sink.write(event.data(), event.size()); + return true; + } + } + return !stop_server_.load(); + }); + }); + + // Endpoint that returns error + server_->Get("/error-endpoint", [](const Request &, Response &res) { + res.status = 500; + res.set_content("Internal Server Error", "text/plain"); + }); + + // Endpoint for custom event types + server_->Get("/custom-events", [](const Request &, Response &res) { + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = "event: update\ndata: updated\n\n" + "event: delete\ndata: deleted\n\n"; + sink.write(event.data(), event.size()); + } + return false; // End stream after sending + }); + }); + } + + void start_server() { + port_ = server_->bind_to_any_port("localhost"); + server_thread_ = std::thread([this]() { server_->listen_after_bind(); }); + + // Wait for server to start + while (!server_->is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + } + + int get_port() const { return port_; } + + void send_event(const std::string &event) { + std::lock_guard lock(event_mutex_); + events_.push_back(event); + event_cv_.notify_all(); + } + + std::unique_ptr server_; + std::thread server_thread_; + std::mutex event_mutex_; + std::condition_variable event_cv_; + std::vector events_; + std::atomic stop_server_{false}; + std::string last_received_event_id_; + int port_ = 0; +}; + +// Test: Successful connection and on_open callback +TEST_F(SSEIntegrationTest, SuccessfulConnection) { + // Add a simple endpoint that sends one event and closes + server_->Get("/simple-event", [](const Request &, Response &res) { + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = "data: hello\n\n"; + sink.write(event.data(), event.size()); + } + return false; // Close stream after sending + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/simple-event"); + + std::atomic open_called{false}; + std::atomic message_received{false}; + + sse.on_open([&open_called]() { open_called.store(true); }); + + sse.on_message([&message_received](const sse::SSEMessage &msg) { + if (msg.data == "hello") { message_received.store(true); } + }); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(1); + + // Start async + sse.start_async(); + + // Wait for message to be processed + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + sse.stop(); + + EXPECT_TRUE(open_called.load()); + EXPECT_TRUE(message_received.load()); +} + +// Test: on_message callback +TEST_F(SSEIntegrationTest, OnMessageCallback) { + // Endpoint that sends multiple events then closes + server_->Get("/multi-event", [](const Request &, Response &res) { + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string events = "data: message1\n\ndata: message2\n\n"; + sink.write(events.data(), events.size()); + } + return false; + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/multi-event"); + + std::vector received_messages; + std::mutex messages_mutex; + + sse.on_message([&](const sse::SSEMessage &msg) { + std::lock_guard lock(messages_mutex); + received_messages.push_back(msg.data); + }); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(1); + sse.start_async(); + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + sse.stop(); + + std::lock_guard lock(messages_mutex); + EXPECT_GE(received_messages.size(), 2u); + if (received_messages.size() >= 2) { + EXPECT_EQ(received_messages[0], "message1"); + EXPECT_EQ(received_messages[1], "message2"); + } +} + +// Test: on_event for specific types +TEST_F(SSEIntegrationTest, OnEventForSpecificTypes) { + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/custom-events"); + + std::atomic update_received{false}; + std::atomic delete_received{false}; + + sse.on_event("update", [&update_received](const sse::SSEMessage &msg) { + if (msg.data == "updated") { update_received.store(true); } + }); + + sse.on_event("delete", [&delete_received](const sse::SSEMessage &msg) { + if (msg.data == "deleted") { delete_received.store(true); } + }); + + sse.set_max_reconnect_attempts(1); + sse.start_async(); + + std::this_thread::sleep_for(std::chrono::milliseconds(300)); + sse.stop(); + + EXPECT_TRUE(update_received.load()); + EXPECT_TRUE(delete_received.load()); +} + +// Test: on_error callback on connection failure +TEST_F(SSEIntegrationTest, OnErrorCallback) { + // Connect to a non-existent port + Client client("localhost", 59999); + sse::SSEClient sse(client, "/events"); + + std::atomic error_called{false}; + Error received_error = Error::Success; + + sse.on_error([&](Error err) { + error_called.store(true); + received_error = err; + }); + + sse.set_reconnect_interval(50); + sse.set_max_reconnect_attempts(1); + + sse.start_async(); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + sse.stop(); + + EXPECT_TRUE(error_called.load()); + EXPECT_NE(received_error, Error::Success); +} + +// Test: Last-Event-ID header sent on reconnect +TEST_F(SSEIntegrationTest, LastEventIdHeader) { + // Endpoint that sends event with ID + server_->Get("/event-with-id", [](const Request &, Response &res) { + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = "id: evt-123\ndata: test\n\n"; + sink.write(event.data(), event.size()); + } + return false; + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/event-with-id"); + + std::atomic id_received{false}; + + sse.on_message([&](const sse::SSEMessage &msg) { + if (!msg.id.empty()) { id_received.store(true); } + }); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(1); + sse.start_async(); + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + sse.stop(); + + EXPECT_TRUE(id_received.load()); + EXPECT_EQ(sse.last_event_id(), "evt-123"); +} + +// Test: Manual stop +TEST_F(SSEIntegrationTest, ManualStop) { + // Endpoint that sends one event and stays open briefly + std::atomic handler_running{true}; + + server_->Get("/stay-open", [&handler_running](const Request &, + Response &res) { + res.set_chunked_content_provider( + "text/event-stream", [&handler_running](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = "data: connected\n\n"; + sink.write(event.data(), event.size()); + } + // Keep connection open while handler_running is true + for (int i = 0; i < 10 && handler_running.load(); ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } + return false; + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/stay-open"); + + std::atomic connected{false}; + sse.on_open([&connected]() { connected.store(true); }); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(1); + sse.start_async(); + + // Wait for connection to establish + for (int i = 0; i < 20 && !connected.load(); ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } + EXPECT_TRUE(connected.load()); + EXPECT_TRUE(sse.is_connected()); + + // Signal handler to stop + handler_running.store(false); + + // Stop SSE client + sse.stop(); + EXPECT_FALSE(sse.is_connected()); +} + +// Test: SSEClient with custom headers +TEST_F(SSEIntegrationTest, CustomHeaders) { + // Setup a server endpoint that checks for custom header + std::atomic header_received{false}; + + server_->Get("/header-check", [&](const Request &req, Response &res) { + if (req.get_header_value("X-Custom-Header") == "custom-value") { + header_received.store(true); + } + res.set_chunked_content_provider("text/event-stream", + [](size_t, DataSink &) { return false; }); + }); + + Client client("localhost", get_port()); + Headers headers = {{"X-Custom-Header", "custom-value"}}; + sse::SSEClient sse(client, "/header-check", headers); + + sse.set_max_reconnect_attempts(1); + sse.start_async(); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + sse.stop(); + + EXPECT_TRUE(header_received.load()); +} + +// Test: Reconnect interval configuration +TEST_F(SSEIntegrationTest, ReconnectIntervalConfiguration) { + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/events"); + + auto &result = sse.set_reconnect_interval(500); + // Builder pattern should return reference to self + EXPECT_EQ(&result, &sse); +} + +// Test: Max reconnect attempts +TEST_F(SSEIntegrationTest, MaxReconnectAttempts) { + // Connect to non-existent port to force reconnects + Client client("localhost", 59998); + sse::SSEClient sse(client, "/events"); + + std::atomic error_count{0}; + + sse.on_error([&](Error) { error_count.fetch_add(1); }); + + sse.set_reconnect_interval(50); + sse.set_max_reconnect_attempts(2); + + auto start = std::chrono::steady_clock::now(); + sse.start(); // Blocking call + auto end = std::chrono::steady_clock::now(); + + // Should have stopped after 2 failed attempts + EXPECT_GE(error_count.load(), 2); + + // Should not have taken too long (max 2 attempts * 50ms + overhead) + auto duration = + std::chrono::duration_cast(end - start); +#ifdef _WIN32 + // Windows is much slower for socket connection failures + EXPECT_LT(duration.count(), 7000); +#else + EXPECT_LT(duration.count(), 1000); +#endif +} + +// Test: Multi-line data in integration +TEST_F(SSEIntegrationTest, MultiLineDataIntegration) { + // Endpoint with multi-line data + server_->Get("/multiline-data", [](const Request &, Response &res) { + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = "data: line1\ndata: line2\ndata: line3\n\n"; + sink.write(event.data(), event.size()); + } + return false; + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/multiline-data"); + + std::string received_data; + std::mutex data_mutex; + + sse.on_message([&](const sse::SSEMessage &msg) { + std::lock_guard lock(data_mutex); + received_data = msg.data; + }); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(1); + sse.start_async(); + + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + sse.stop(); + + std::lock_guard lock(data_mutex); + EXPECT_EQ(received_data, "line1\nline2\nline3"); +} + +// Test: Auto-reconnect after server disconnection +TEST_F(SSEIntegrationTest, AutoReconnectAfterDisconnect) { + std::atomic connection_count{0}; + std::atomic message_count{0}; + + // Endpoint that sends one event and closes, forcing reconnect + server_->Get("/reconnect-test", + [&connection_count](const Request &, Response &res) { + connection_count.fetch_add(1); + res.set_chunked_content_provider( + "text/event-stream", [](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = "data: hello\n\n"; + sink.write(event.data(), event.size()); + } + return false; // Close connection after sending + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/reconnect-test"); + + sse.on_message([&message_count](const sse::SSEMessage &) { + message_count.fetch_add(1); + }); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(3); + sse.start_async(); + + // Wait long enough for multiple reconnects + std::this_thread::sleep_for(std::chrono::milliseconds(800)); + sse.stop(); + + // Should have connected multiple times (initial + reconnects) + EXPECT_GE(connection_count.load(), 2); + // Should have received messages from multiple connections + EXPECT_GE(message_count.load(), 2); +} + +// Test: Last-Event-ID sent on reconnect +TEST_F(SSEIntegrationTest, LastEventIdSentOnReconnect) { + std::atomic connection_count{0}; + std::vector received_last_event_ids; + std::mutex id_mutex; + + // Endpoint that checks Last-Event-ID header and sends event with ID + server_->Get("/reconnect-with-id", [&](const Request &req, Response &res) { + int conn = connection_count.fetch_add(1); + + // Capture the Last-Event-ID header from each connection + { + std::lock_guard lock(id_mutex); + received_last_event_ids.push_back(req.get_header_value("Last-Event-ID")); + } + + res.set_chunked_content_provider( + "text/event-stream", [conn](size_t offset, DataSink &sink) { + if (offset == 0) { + std::string event = + "id: event-" + std::to_string(conn) + "\ndata: msg\n\n"; + sink.write(event.data(), event.size()); + } + return false; + }); + }); + + Client client("localhost", get_port()); + sse::SSEClient sse(client, "/reconnect-with-id"); + + sse.set_reconnect_interval(100); + sse.set_max_reconnect_attempts(3); + sse.start_async(); + + // Wait for at least 2 connections + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + sse.stop(); + + // Verify behavior + std::lock_guard lock(id_mutex); + EXPECT_GE(received_last_event_ids.size(), 2u); + + // First connection should have no Last-Event-ID + if (!received_last_event_ids.empty()) { + EXPECT_EQ(received_last_event_ids[0], ""); + } + + // Second connection should have Last-Event-ID from first connection + if (received_last_event_ids.size() >= 2) { + EXPECT_EQ(received_last_event_ids[1], "event-0"); + } +} From 0461cb770c3226e4e69b98e82326647607be3dd2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 17 Dec 2025 12:04:17 -0500 Subject: [PATCH 0565/1222] Avoid unncessary copying of request and response objects (#2310) --- httplib.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index 114d60b814..5a5aa00caa 100644 --- a/httplib.h +++ b/httplib.h @@ -6157,8 +6157,8 @@ inline bool redirect(T &cli, Request &req, Response &res, auto ret = cli.send(new_req, new_res, error); if (ret) { - req = new_req; - res = new_res; + req = std::move(new_req); + res = std::move(new_res); if (res.location.empty()) { res.location = location; } } @@ -10554,7 +10554,7 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, auto req2 = req; req2.path = "http://" + host_and_port_ + req.path; ret = process_request(strm, req2, res, close_connection, error); - req = req2; + req = std::move(req2); req.path = req_save.path; } else { ret = process_request(strm, req, res, close_connection, error); @@ -10606,7 +10606,7 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, Response new_res; ret = send(new_req, new_res, error); - if (ret) { res = new_res; } + if (ret) { res = std::move(new_res); } } } } From f0990ca96d979cdceffa8dee957edd0d0d67e513 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 17 Dec 2025 12:04:37 -0500 Subject: [PATCH 0566/1222] Use std::move for request redirection (#2311) Prevents an additional copy --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5a5aa00caa..a8919662b4 100644 --- a/httplib.h +++ b/httplib.h @@ -10578,7 +10578,7 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, } if (300 < res.status && res.status < 400 && follow_location_) { - req = req_save; + req = std::move(req_save); ret = redirect(req, res, error); } From 25688258ad7a557047371546feec34768ce86e82 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 22 Dec 2025 20:43:46 -0500 Subject: [PATCH 0567/1222] Add another missing std::move for _base_dirs vector (#2314) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index a8919662b4..04a8fe2d77 100644 --- a/httplib.h +++ b/httplib.h @@ -8540,7 +8540,7 @@ inline bool Server::set_mount_point(const std::string &mount_point, if (stat.is_dir()) { std::string mnt = !mount_point.empty() ? mount_point : "/"; if (!mnt.empty() && mnt[0] == '/') { - base_dirs_.push_back({mnt, dir, std::move(headers)}); + base_dirs_.push_back({std::move(mnt), dir, std::move(headers)}); return true; } } From cdf0d332587b9e3b23a760ff502f7b598369bc05 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 22 Dec 2025 21:36:08 -0500 Subject: [PATCH 0568/1222] Fix #2301 --- httplib.h | 9 ++++++--- test/test.cc | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 04a8fe2d77..4bef078911 100644 --- a/httplib.h +++ b/httplib.h @@ -1751,7 +1751,7 @@ class ClientImpl { // Socket endpoint information const std::string host_; const int port_; - const std::string host_and_port_; + std::string host_and_port_; // Current open socket Socket socket_; @@ -9874,8 +9874,7 @@ inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), - host_and_port_( - detail::make_host_and_port_string(host_, port, ClientImpl::is_ssl())), + host_and_port_(detail::make_host_and_port_string(host_, port, false)), client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { @@ -12696,6 +12695,8 @@ inline SSLClient::SSLClient(const std::string &host, int port, const std::string &client_key_path, const std::string &private_key_password) : ClientImpl(host, port, client_cert_path, client_key_path) { + host_and_port_ = detail::make_host_and_port_string(host_, port_, true); + ctx_ = SSL_CTX_new(TLS_client_method()); SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); @@ -12727,6 +12728,8 @@ inline SSLClient::SSLClient(const std::string &host, int port, X509 *client_cert, EVP_PKEY *client_key, const std::string &private_key_password) : ClientImpl(host, port) { + host_and_port_ = detail::make_host_and_port_string(host_, port_, true); + ctx_ = SSL_CTX_new(TLS_client_method()); detail::split(&host_[0], &host_[host_.size()], '.', diff --git a/test/test.cc b/test/test.cc index d99b8a533a..ff168aca6d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -10924,6 +10924,18 @@ TEST(MakeHostAndPortStringTest, VariousPatterns) { detail::make_host_and_port_string("example.com", 65536, false)); } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(SSLClientHostHeaderTest, Issue2301) { + httplib::SSLClient cli("roblox.com", 443); + cli.set_follow_location(true); + + auto res = cli.Get("/"); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); +} +#endif + TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) { Server svr; From db98efee5ae109cff4871cdc54b07e20c6441aa9 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 22 Dec 2025 23:10:24 -0500 Subject: [PATCH 0569/1222] Fix problem with Proxy test --- httplib.h | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 4bef078911..cd2e798dc9 100644 --- a/httplib.h +++ b/httplib.h @@ -1751,7 +1751,6 @@ class ClientImpl { // Socket endpoint information const std::string host_; const int port_; - std::string host_and_port_; // Current open socket Socket socket_; @@ -8425,19 +8424,22 @@ inline bool RegexMatcher::match(Request &request) const { return std::regex_match(request.path, request.matches, regex_); } -inline std::string make_host_and_port_string(const std::string &host, int port, - bool is_ssl) { - std::string result; - +// Enclose IPv6 address in brackets if needed +inline std::string prepare_host_string(const std::string &host) { // Enclose IPv6 address in brackets (but not if already enclosed) if (host.find(':') == std::string::npos || (!host.empty() && host[0] == '[')) { // IPv4, hostname, or already bracketed IPv6 - result = host; + return host; } else { // IPv6 address without brackets - result = "[" + host + "]"; + return "[" + host + "]"; } +} + +inline std::string make_host_and_port_string(const std::string &host, int port, + bool is_ssl) { + auto result = prepare_host_string(host); // Append port if not default if ((!is_ssl && port == 80) || (is_ssl && port == 443)) { @@ -8449,6 +8451,12 @@ inline std::string make_host_and_port_string(const std::string &host, int port, return result; } +// Create "host:port" string always including port number (for CONNECT method) +inline std::string +make_host_and_port_string_always_port(const std::string &host, int port) { + return prepare_host_string(host) + ":" + std::to_string(port); +} + } // namespace detail // HTTP server implementation @@ -9874,7 +9882,6 @@ inline ClientImpl::ClientImpl(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) : host_(detail::escape_abstract_namespace_unix_domain(host)), port_(port), - host_and_port_(detail::make_host_and_port_string(host_, port, false)), client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { @@ -10211,7 +10218,8 @@ inline void ClientImpl::prepare_default_headers(Request &r, bool for_stream, if (address_family_ == AF_UNIX) { r.headers.emplace("Host", "localhost"); } else { - r.headers.emplace("Host", host_and_port_); + r.headers.emplace( + "Host", detail::make_host_and_port_string(host_, port_, is_ssl())); } } @@ -10551,7 +10559,9 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { auto req2 = req; - req2.path = "http://" + host_and_port_ + req.path; + req2.path = "http://" + + detail::make_host_and_port_string(host_, port_, false) + + req.path; ret = process_request(strm, req2, res, close_connection, error); req = std::move(req2); req.path = req_save.path; @@ -12695,8 +12705,6 @@ inline SSLClient::SSLClient(const std::string &host, int port, const std::string &client_key_path, const std::string &private_key_password) : ClientImpl(host, port, client_cert_path, client_key_path) { - host_and_port_ = detail::make_host_and_port_string(host_, port_, true); - ctx_ = SSL_CTX_new(TLS_client_method()); SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); @@ -12728,8 +12736,6 @@ inline SSLClient::SSLClient(const std::string &host, int port, X509 *client_cert, EVP_PKEY *client_key, const std::string &private_key_password) : ClientImpl(host, port) { - host_and_port_ = detail::make_host_and_port_string(host_, port_, true); - ctx_ = SSL_CTX_new(TLS_client_method()); detail::split(&host_[0], &host_[host_.size()], '.', @@ -12811,7 +12817,8 @@ inline bool SSLClient::connect_with_proxy( start_time, [&](Stream &strm) { Request req2; req2.method = "CONNECT"; - req2.path = host_and_port_; + req2.path = + detail::make_host_and_port_string_always_port(host_, port_); if (max_timeout_msec_ > 0) { req2.start_time_ = std::chrono::steady_clock::now(); } @@ -12851,7 +12858,8 @@ inline bool SSLClient::connect_with_proxy( start_time, [&](Stream &strm) { Request req3; req3.method = "CONNECT"; - req3.path = host_and_port_; + req3.path = detail::make_host_and_port_string_always_port( + host_, port_); req3.headers.insert(detail::make_digest_authentication_header( req3, auth, 1, detail::random_string(10), proxy_digest_auth_username_, proxy_digest_auth_password_, From 5304464a53f793f8e3e4777ea5941d60cf8c91e3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 23 Dec 2025 00:03:49 -0500 Subject: [PATCH 0570/1222] Release v0.29.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index cd2e798dc9..92a4369436 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.28.0" -#define CPPHTTPLIB_VERSION_NUM "0x001C00" +#define CPPHTTPLIB_VERSION "0.29.0" +#define CPPHTTPLIB_VERSION_NUM "0x001D00" /* * Platform compatibility check From d23cf77cd088256489769adde4390f367c4d6e77 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 23 Dec 2025 20:10:59 -0500 Subject: [PATCH 0571/1222] Resolve #2313 --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7566dcd807..6baed498d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,6 +185,8 @@ elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE) set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND}) endif() +# NOTE: When using cpp-httplib as a subproject (e.g., via FetchContent), the zstd::libzstd target may not be visible in the parent project scope. +# If you encounter a "target not found" error, see https://github.com/yhirose/cpp-httplib/issues/2313 for a workaround. if(HTTPLIB_REQUIRE_ZSTD) find_package(zstd) if(NOT zstd_FOUND) From b7c2f043188ecb544718a849bb2ab634ee241164 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 25 Dec 2025 22:19:37 -0500 Subject: [PATCH 0572/1222] Fix potential arithmatic overflow problem --- httplib.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 92a4369436..c3e768f249 100644 --- a/httplib.h +++ b/httplib.h @@ -8957,7 +8957,16 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { strm, req, res, // Regular [&](const char *buf, size_t n) { - if (req.body.size() + n > req.body.max_size()) { return false; } + // Prevent arithmetic overflow when checking sizes. + // Avoid computing (req.body.size() + n) directly because + // adding two unsigned `size_t` values can wrap around and + // produce a small result instead of indicating overflow. + // Instead, check using subtraction: ensure `n` does not + // exceed the remaining capacity `max_size() - size()`. + if (req.body.size() >= req.body.max_size() || + n > req.body.max_size() - req.body.size()) { + return false; + } req.body.append(buf, n); return true; }, From 385adefb119298ab781ac31696a6be982ffddbfc Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 26 Dec 2025 00:24:19 -0500 Subject: [PATCH 0573/1222] Use `HOST` and `PORT` in test.cc --- test/test.cc | 88 +++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/test/test.cc b/test/test.cc index ff168aca6d..79e578b7cb 100644 --- a/test/test.cc +++ b/test/test.cc @@ -82,15 +82,13 @@ static void read_file(const std::string &path, std::string &out) { } void performance_test(const char *host) { - auto port = 1234; - Server svr; svr.Get("/benchmark", [&](const Request & /*req*/, Response &res) { res.set_content("Benchmark Response", "text/plain"); }); - auto listen_thread = std::thread([&]() { svr.listen(host, port); }); + auto listen_thread = std::thread([&]() { svr.listen(host, PORT); }); auto se = detail::scope_exit([&] { svr.stop(); listen_thread.join(); @@ -99,7 +97,7 @@ void performance_test(const char *host) { svr.wait_until_ready(); - Client cli(host, port); + Client cli(host, PORT); // Warm-up request to establish connection and resolve DNS auto warmup_res = cli.Get("/benchmark"); @@ -2235,7 +2233,7 @@ TEST(ReceiveSignals, Signal) { int port = 0; auto thread = std::thread([&]() { setupSignalHandlers(); - port = svr.bind_to_any_port("localhost"); + port = svr.bind_to_any_port(HOST); svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { @@ -2261,7 +2259,7 @@ TEST(RedirectToDifferentPort, Redirect) { int svr1_port = 0; auto thread1 = std::thread([&]() { - svr1_port = svr1.bind_to_any_port("localhost"); + svr1_port = svr1.bind_to_any_port(HOST); svr1.listen_after_bind(); }); @@ -2272,7 +2270,7 @@ TEST(RedirectToDifferentPort, Redirect) { int svr2_port = 0; auto thread2 = std::thread([&]() { - svr2_port = svr2.bind_to_any_port("localhost"); + svr2_port = svr2.bind_to_any_port(HOST); svr2.listen_after_bind(); }); auto se = detail::scope_exit([&] { @@ -9616,7 +9614,7 @@ TEST(MultipartFormDataTest, LargeData) { } }); - auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto t = std::thread([&]() { svr.listen(HOST, 8080); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -12247,7 +12245,7 @@ static void test_compression_parity(const std::string &original, res.set_header("Content-Encoding", encoding); }); - auto t = std::thread([&] { svr.listen("localhost", 1234); }); + auto t = std::thread([&] { svr.listen(HOST, PORT); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -12256,7 +12254,7 @@ static void test_compression_parity(const std::string &original, svr.wait_until_ready(); - Client cli("localhost", 1234); + Client cli(HOST, PORT); // Non-streaming { @@ -12373,7 +12371,7 @@ class StreamApiTest : public ::testing::Test { [](const httplib::Request &, httplib::Response &res) { res.set_header("Allow", "GET, POST, PUT, DELETE, OPTIONS"); }); - thread_ = std::thread([this]() { svr_.listen("localhost", 8790); }); + thread_ = std::thread([this]() { svr_.listen(HOST, PORT); }); svr_.wait_until_ready(); } void TearDown() override { @@ -12386,7 +12384,7 @@ class StreamApiTest : public ::testing::Test { // stream::Get tests TEST_F(StreamApiTest, GetBasic) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto result = httplib::stream::Get(cli, "/hello"); ASSERT_TRUE(result.is_valid()); EXPECT_EQ(200, result.status()); @@ -12394,7 +12392,7 @@ TEST_F(StreamApiTest, GetBasic) { } TEST_F(StreamApiTest, GetWithParams) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); httplib::Params params{{"foo", "bar"}}; auto result = httplib::stream::Get(cli, "/echo-params", params); ASSERT_TRUE(result.is_valid()); @@ -12402,12 +12400,12 @@ TEST_F(StreamApiTest, GetWithParams) { } TEST_F(StreamApiTest, GetConnectionError) { - httplib::Client cli("localhost", 9999); + httplib::Client cli(HOST, 9999); EXPECT_FALSE(httplib::stream::Get(cli, "/hello").is_valid()); } TEST_F(StreamApiTest, Get404) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto result = httplib::stream::Get(cli, "/nonexistent"); EXPECT_TRUE(result.is_valid()); EXPECT_EQ(404, result.status()); @@ -12415,7 +12413,7 @@ TEST_F(StreamApiTest, Get404) { // stream::Post tests TEST_F(StreamApiTest, PostBasic) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto result = httplib::stream::Post(cli, "/echo", R"({"key":"value"})", "application/json"); ASSERT_TRUE(result.is_valid()); @@ -12424,7 +12422,7 @@ TEST_F(StreamApiTest, PostBasic) { } TEST_F(StreamApiTest, PostWithHeaders) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); httplib::Headers headers{{"X-Custom", "value"}}; auto result = httplib::stream::Post(cli, "/echo-headers", headers, "body", "text/plain"); @@ -12432,7 +12430,7 @@ TEST_F(StreamApiTest, PostWithHeaders) { } TEST_F(StreamApiTest, PostWithParams) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); httplib::Params params{{"k", "v"}}; auto result = httplib::stream::Post(cli, "/echo-params", params, "data", "text/plain"); @@ -12442,7 +12440,7 @@ TEST_F(StreamApiTest, PostWithParams) { } TEST_F(StreamApiTest, PostLarge) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto result = httplib::stream::Post(cli, "/large", "", "text/plain"); size_t total = 0; while (result.next()) { @@ -12453,7 +12451,7 @@ TEST_F(StreamApiTest, PostLarge) { // stream::Put/Patch tests TEST_F(StreamApiTest, PutAndPatch) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto put = httplib::stream::Put(cli, "/echo", "test", "text/plain"); EXPECT_EQ("PUT:test", read_body(put)); auto patch = httplib::stream::Patch(cli, "/echo", "test", "text/plain"); @@ -12462,7 +12460,7 @@ TEST_F(StreamApiTest, PutAndPatch) { // stream::Delete tests TEST_F(StreamApiTest, Delete) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto del1 = httplib::stream::Delete(cli, "/resource"); EXPECT_EQ("Deleted", read_body(del1)); auto del2 = httplib::stream::Delete(cli, "/resource", "data", "text/plain"); @@ -12471,7 +12469,7 @@ TEST_F(StreamApiTest, Delete) { // stream::Head/Options tests TEST_F(StreamApiTest, HeadAndOptions) { - httplib::Client cli("localhost", 8790); + httplib::Client cli(HOST, PORT); auto head = httplib::stream::Head(cli, "/head-test"); EXPECT_TRUE(head.is_valid()); EXPECT_FALSE(head.get_header_value("Content-Length").empty()); @@ -12749,10 +12747,10 @@ TEST(ETagTest, StaticFileETagAndIfNoneMatch) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8087); }); + auto t = std::thread([&]() { svr.listen("localhost", PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8087); + Client cli(HOST, PORT); // First request: should get 200 with ETag header auto res1 = cli.Get("/static/etag_testfile.txt"); @@ -12803,10 +12801,10 @@ TEST(ETagTest, StaticFileETagIfNoneMatchStarNotFound) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8090); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8090); + Client cli(HOST, PORT); // Send If-None-Match: * to a non-existent file Headers h = {{"If-None-Match", "*"}}; @@ -12832,10 +12830,10 @@ TEST(ETagTest, LastModifiedAndIfModifiedSince) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8088); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8088); + Client cli(HOST, PORT); // First request: should get 200 with Last-Modified header auto res1 = cli.Get("/static/ims_testfile.txt"); @@ -12890,10 +12888,10 @@ TEST(ETagTest, VaryAcceptEncodingWithCompression) { res.set_content(body, "text/plain"); }); - auto t = std::thread([&]() { svr.listen("localhost", 8089); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8089); + Client cli(HOST, PORT); // Request with gzip support: should get Vary header when compressed cli.set_compress(true); @@ -12934,10 +12932,10 @@ TEST(ETagTest, IfRangeWithETag) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8090); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8090); + Client cli(HOST, PORT); // First request: get ETag auto res1 = cli.Get("/static/if_range_testfile.txt"); @@ -12995,10 +12993,10 @@ TEST(ETagTest, IfRangeWithDate) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8091); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8091); + Client cli(HOST, PORT); // First request: get Last-Modified auto res1 = cli.Get("/static/if_range_date_testfile.txt"); @@ -13047,10 +13045,10 @@ TEST(ETagTest, MalformedIfNoneMatchAndWhitespace) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8092); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8092); + Client cli(HOST, PORT); // baseline: should get 200 and an ETag auto res1 = cli.Get("/static/etag_malformed.txt"); @@ -13088,10 +13086,10 @@ TEST(ETagTest, InvalidIfModifiedSinceAndIfRangeDate) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8093); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8093); + Client cli(HOST, PORT); auto res1 = cli.Get("/static/ims_invalid_format.txt"); ASSERT_TRUE(res1); @@ -13130,10 +13128,10 @@ TEST(ETagTest, IfRangeWithMalformedETag) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8094); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8094); + Client cli(HOST, PORT); // First request: get ETag auto res1 = cli.Get("/static/ifrange_malformed.txt"); @@ -13167,10 +13165,10 @@ TEST(ETagTest, ExtremeLargeDateValues) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8095); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8095); + Client cli(HOST, PORT); auto res1 = cli.Get(std::string("/static/") + fname); ASSERT_TRUE(res1); @@ -13230,10 +13228,10 @@ TEST(ETagTest, NegativeFileModificationTime) { Server svr; svr.set_mount_point("/static", "."); - auto t = std::thread([&]() { svr.listen("localhost", 8096); }); + auto t = std::thread([&]() { svr.listen(HOST, PORT); }); svr.wait_until_ready(); - Client cli("localhost", 8096); + Client cli(HOST, PORT); auto res1 = cli.Get(std::string("/static/") + fname); ASSERT_TRUE(res1); @@ -13559,7 +13557,7 @@ class SSEIntegrationTest : public ::testing::Test { } void start_server() { - port_ = server_->bind_to_any_port("localhost"); + port_ = server_->bind_to_any_port(HOST); server_thread_ = std::thread([this]() { server_->listen_after_bind(); }); // Wait for server to start From 7ae794a6bfa51200baa6d852fff377f0ee0566af Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 26 Dec 2025 16:17:43 -0500 Subject: [PATCH 0574/1222] Fix #2315 --- httplib.h | 4 ++++ test/test.cc | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/httplib.h b/httplib.h index c3e768f249..07c154c55c 100644 --- a/httplib.h +++ b/httplib.h @@ -9646,6 +9646,10 @@ Server::process_request(Stream &strm, const std::string &remote_addr, Request req; req.start_time_ = std::chrono::steady_clock::now(); + req.remote_addr = remote_addr; + req.remote_port = remote_port; + req.local_addr = local_addr; + req.local_port = local_port; Response res; res.version = "HTTP/1.1"; diff --git a/test/test.cc b/test/test.cc index 79e578b7cb..a29a843a39 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6843,6 +6843,29 @@ TEST(ServerRequestParsingTest, InvalidSpaceInURL) { EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); } +TEST(ServerRequestParsingTest, RemoteAddrSetOnBadRequest) { + Server svr; + + svr.set_error_handler([&](const Request &req, Response & /*res*/) { + EXPECT_TRUE(!req.remote_addr.empty()); + }); + + thread t = thread([&] { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + // Send an invalid request line to trigger Bad Request + const std::string bad_req = "BADMETHOD / HTTP/1.1\r\nHost: localhost\r\n\r\n"; + std::string out; + ASSERT_TRUE(send_request(5, bad_req, &out)); + EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24)); +} + TEST(ServerRequestParsingTest, InvalidFieldValueContains_CR_LF_NUL) { std::string out; std::string request( From 98048a033a532ff22320ce1d11789f8d5710dfcd Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 30 Dec 2025 17:32:42 -0500 Subject: [PATCH 0575/1222] Merge commit from fork --- httplib.h | 29 +++++++++++++--- test/test.cc | 96 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 112 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 07c154c55c..d7841a1603 100644 --- a/httplib.h +++ b/httplib.h @@ -8457,6 +8457,23 @@ make_host_and_port_string_always_port(const std::string &host, int port) { return prepare_host_string(host) + ":" + std::to_string(port); } +template +inline bool check_and_write_headers(Stream &strm, Headers &headers, + T header_writer, Error &error) { + for (const auto &h : headers) { + if (!detail::fields::is_field_name(h.first) || + !detail::fields::is_field_value(h.second)) { + error = Error::InvalidHeaders; + return false; + } + } + if (header_writer(strm, headers) <= 0) { + error = Error::Write; + return false; + } + return true; +} + } // namespace detail // HTTP server implementation @@ -8864,7 +8881,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, { detail::BufferStream bstrm; if (!detail::write_response_line(bstrm, res.status)) { return false; } - if (!header_writer_(bstrm, res.headers)) { return false; } + if (header_writer_(bstrm, res.headers) <= 0) { return false; } // Flush buffer auto &data = bstrm.get_buffer(); @@ -10359,8 +10376,8 @@ ClientImpl::open_stream(const std::string &method, const std::string &path, return handle; } - if (!detail::write_headers(strm, req.headers)) { - handle.error = Error::Write; + if (!detail::check_and_write_headers(strm, req.headers, header_writer_, + handle.error)) { handle.response.reset(); return handle; } @@ -10957,7 +10974,11 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, // Write request line and headers detail::write_request_line(bstrm, req.method, path_with_query); - header_writer_(bstrm, req.headers); + if (!detail::check_and_write_headers(bstrm, req.headers, header_writer_, + error)) { + output_error_log(error, &req); + return false; + } // Flush buffer auto &data = bstrm.get_buffer(); diff --git a/test/test.cc b/test/test.cc index a29a843a39..f4eededd5a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -10671,6 +10671,74 @@ TEST(VulnerabilityTest, CRLFInjection) { } } +TEST(VulnerabilityTest, CRLFInjectionInHeaders) { + auto server_thread = std::thread([] { + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + int on = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT); + ::inet_pton(AF_INET, HOST, &addr.sin_addr); + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + struct timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + ::setsockopt(cli, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + + std::string buf_all; + char buf[2048]; + ssize_t n; + + while ((n = ::recv(cli, buf, sizeof(buf), 0)) > 0) { + buf_all.append(buf, static_cast(n)); + + size_t pos; + while ((pos = buf_all.find("\r\n\r\n")) != std::string::npos) { + auto request_block = buf_all.substr(0, pos + 4); // include separator + + auto e = request_block.find("\r\n"); + if (e != std::string::npos) { + auto request_line = request_block.substr(0, e); + std::string msg = + "CRLF injection detected in request line: '" + request_line + "'"; + EXPECT_FALSE(true) << msg; + } + + std::string resp = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello"; + ::send(cli, resp.c_str(), resp.size(), 0); + + buf_all.erase(0, pos + 4); + } + } + + ::close(cli); + ::close(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + auto cli = httplib::Client(HOST, PORT); + + auto headers = httplib::Headers{ + {"A", "B\r\n\r\nGET /pwned HTTP/1.1\r\nHost: 127.0.0.1:1234\r\n\r\n"}, + {"Connection", "keep-alive"}}; + + auto res = cli.Get("/hi", headers); + EXPECT_FALSE(res); + + if (res) { EXPECT_EQ(httplib::Error::InvalidHeaders, res.error()); } + + server_thread.join(); +} + TEST(PathParamsTest, StaticMatch) { const auto pattern = "/users/all"; detail::PathParamsMatcher matcher(pattern); @@ -11700,12 +11768,16 @@ TEST(ForwardedHeadersTest, HandlesWhitespaceAroundIPs) { svr.wait_until_ready(); - Client cli(HOST, PORT); - auto res = cli.Get("/ip", {{"X-Forwarded-For", - " 198.51.100.23 , 203.0.113.66 , 192.0.2.45 "}}); + std::string raw_req = + "GET /ip HTTP/1.1\r\n" + "Host: localhost\r\n" + "X-Forwarded-For: 198.51.100.23 , 203.0.113.66 , 192.0.2.45 \r\n" + "Connection: close\r\n" + "\r\n"; - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::OK_200, res->status); + std::string out; + ASSERT_TRUE(send_request(5, raw_req, &out)); + EXPECT_EQ("HTTP/1.1 200 OK", out.substr(0, 15)); // Header parser trims surrounding whitespace of the header value EXPECT_EQ(observed_xff, "198.51.100.23 , 203.0.113.66 , 192.0.2.45"); @@ -13086,10 +13158,16 @@ TEST(ETagTest, MalformedIfNoneMatchAndWhitespace) { EXPECT_EQ(200, res_bad->status); // Whitespace-only header value should be considered invalid / non-matching - Headers h_space = {{"If-None-Match", " "}}; - auto res_space = cli.Get("/static/etag_malformed.txt", h_space); - ASSERT_TRUE(res_space); - EXPECT_EQ(200, res_space->status); + std::string raw_req = + "GET /static/etag_malformed.txt HTTP/1.1\r\n" + "Host: localhost\r\n" + "If-None-Match: \r\n" + "Connection: close\r\n" + "\r\n"; + + std::string out; + ASSERT_TRUE(send_request(5, raw_req, &out)); + EXPECT_EQ("HTTP/1.1 200 OK", out.substr(0, 15)); svr.stop(); t.join(); From ded82448aadde69631c42dbdc4c43dd9f7d0ae08 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 30 Dec 2025 17:34:52 -0500 Subject: [PATCH 0576/1222] clang-format --- test/test.cc | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/test.cc b/test/test.cc index f4eededd5a..2d2685d122 100644 --- a/test/test.cc +++ b/test/test.cc @@ -11769,11 +11769,11 @@ TEST(ForwardedHeadersTest, HandlesWhitespaceAroundIPs) { svr.wait_until_ready(); std::string raw_req = - "GET /ip HTTP/1.1\r\n" - "Host: localhost\r\n" - "X-Forwarded-For: 198.51.100.23 , 203.0.113.66 , 192.0.2.45 \r\n" - "Connection: close\r\n" - "\r\n"; + "GET /ip HTTP/1.1\r\n" + "Host: localhost\r\n" + "X-Forwarded-For: 198.51.100.23 , 203.0.113.66 , 192.0.2.45 \r\n" + "Connection: close\r\n" + "\r\n"; std::string out; ASSERT_TRUE(send_request(5, raw_req, &out)); @@ -13158,12 +13158,11 @@ TEST(ETagTest, MalformedIfNoneMatchAndWhitespace) { EXPECT_EQ(200, res_bad->status); // Whitespace-only header value should be considered invalid / non-matching - std::string raw_req = - "GET /static/etag_malformed.txt HTTP/1.1\r\n" - "Host: localhost\r\n" - "If-None-Match: \r\n" - "Connection: close\r\n" - "\r\n"; + std::string raw_req = "GET /static/etag_malformed.txt HTTP/1.1\r\n" + "Host: localhost\r\n" + "If-None-Match: \r\n" + "Connection: close\r\n" + "\r\n"; std::string out; ASSERT_TRUE(send_request(5, raw_req, &out)); From 2ba0e7a7979bf3cce2eb09123f6c0e6b1b5af0d3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 30 Dec 2025 19:46:21 -0500 Subject: [PATCH 0577/1222] Fix build error --- test/test.cc | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/test/test.cc b/test/test.cc index 2d2685d122..7b41292b45 100644 --- a/test/test.cc +++ b/test/test.cc @@ -10675,12 +10675,18 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) { auto server_thread = std::thread([] { auto srv = ::socket(AF_INET, SOCK_STREAM, 0); int on = 1; - ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&on), +#else + &on, +#endif + sizeof(on)); sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_port = htons(PORT); - ::inet_pton(AF_INET, HOST, &addr.sin_addr); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); ::listen(srv, 1); @@ -10691,7 +10697,13 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) { struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; - ::setsockopt(cli, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + ::setsockopt(cli, SOL_SOCKET, SO_RCVTIMEO, +#ifdef _WIN32 + reinterpret_cast(&tv), +#else + &tv, +#endif + sizeof(tv)); std::string buf_all; char buf[2048]; @@ -10725,7 +10737,7 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); - auto cli = httplib::Client(HOST, PORT); + auto cli = httplib::Client("127.0.0.1", PORT); auto headers = httplib::Headers{ {"A", "B\r\n\r\nGET /pwned HTTP/1.1\r\nHost: 127.0.0.1:1234\r\n\r\n"}, From 6da7f0c61c69df842dd142d773c3fef766aede5c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 30 Dec 2025 20:15:37 -0500 Subject: [PATCH 0578/1222] Fix port reuse problem --- test/test.cc | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/test/test.cc b/test/test.cc index 7b41292b45..e1fa23937a 100644 --- a/test/test.cc +++ b/test/test.cc @@ -10674,18 +10674,11 @@ TEST(VulnerabilityTest, CRLFInjection) { TEST(VulnerabilityTest, CRLFInjectionInHeaders) { auto server_thread = std::thread([] { auto srv = ::socket(AF_INET, SOCK_STREAM, 0); - int on = 1; - ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, -#ifdef _WIN32 - reinterpret_cast(&on), -#else - &on, -#endif - sizeof(on)); + default_socket_options(srv); sockaddr_in addr{}; addr.sin_family = AF_INET; - addr.sin_port = htons(PORT); + addr.sin_port = htons(PORT + 1); ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); ::listen(srv, 1); @@ -10694,16 +10687,7 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) { socklen_t cli_len = sizeof(cli_addr); auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); - struct timeval tv; - tv.tv_sec = 1; - tv.tv_usec = 0; - ::setsockopt(cli, SOL_SOCKET, SO_RCVTIMEO, -#ifdef _WIN32 - reinterpret_cast(&tv), -#else - &tv, -#endif - sizeof(tv)); + detail::set_socket_opt_time(cli, SOL_SOCKET, SO_RCVTIMEO, 1, 0); std::string buf_all; char buf[2048]; @@ -10731,22 +10715,21 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) { } } - ::close(cli); - ::close(srv); + detail::close_socket(cli); + detail::close_socket(srv); }); std::this_thread::sleep_for(std::chrono::milliseconds(200)); - auto cli = httplib::Client("127.0.0.1", PORT); + auto cli = Client("127.0.0.1", PORT + 1); - auto headers = httplib::Headers{ + auto headers = Headers{ {"A", "B\r\n\r\nGET /pwned HTTP/1.1\r\nHost: 127.0.0.1:1234\r\n\r\n"}, {"Connection", "keep-alive"}}; auto res = cli.Get("/hi", headers); EXPECT_FALSE(res); - - if (res) { EXPECT_EQ(httplib::Error::InvalidHeaders, res.error()); } + EXPECT_EQ(Error::InvalidHeaders, res.error()); server_thread.join(); } From f85f30a637c6bfb958ded91595a978d19266a974 Mon Sep 17 00:00:00 2001 From: PerseoGI Date: Wed, 31 Dec 2025 18:49:59 +0100 Subject: [PATCH 0579/1222] Apple frameworks: match CoreFoundation and CFNetwork linkage (#2317) * Apple frameworks: match linkage with CoreFoundation and CFNetwork with actual code * Fix appleframeworks in Meson code --- CMakeLists.txt | 4 ++-- meson.build | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6baed498d8..b0f8f45dce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -278,9 +278,9 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} $<$:ws2_32> $<$:crypt32> # Needed for API from MacOS Security framework - "$<$,$,$>:-framework CoreFoundation -framework Security>" + "$<$,$,$>:-framework CFNetwork -framework CoreFoundation -framework Security>" # Needed for non-blocking getaddrinfo on MacOS - "$<$,$>:-framework CFNetwork>" + "$<$,$>:-framework CFNetwork -framework CoreFoundation>" # Can't put multiple targets in a single generator expression or it bugs out. $<$:Brotli::common> $<$:Brotli::encoder> diff --git a/meson.build b/meson.build index 14aab1ade0..5b97168fef 100644 --- a/meson.build +++ b/meson.build @@ -44,7 +44,7 @@ if openssl_dep.found() deps += openssl_dep args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' if host_machine.system() == 'darwin' - macosx_keychain_dep = dependency('appleframeworks', modules: ['CoreFoundation', 'Security'], required: get_option('macosx_keychain')) + macosx_keychain_dep = dependency('appleframeworks', modules: ['CFNetwork', 'CoreFoundation', 'Security'], required: get_option('macosx_keychain')) if macosx_keychain_dep.found() deps += macosx_keychain_dep args += '-DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN' @@ -85,7 +85,7 @@ async_ns_opt = get_option('non_blocking_getaddrinfo') if host_machine.system() == 'windows' async_ns_dep = cxx.find_library('ws2_32', required: async_ns_opt) elif host_machine.system() == 'darwin' - async_ns_dep = dependency('appleframeworks', modules: ['CFNetwork'], required: async_ns_opt) + async_ns_dep = dependency('appleframeworks', modules: ['CFNetwork', 'CoreFoundation'], required: async_ns_opt) else async_ns_dep = cxx.find_library('anl', required: async_ns_opt) endif From 40f7985e027bca1f0895986fefad5f2dc1071945 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 31 Dec 2025 22:18:11 -0500 Subject: [PATCH 0580/1222] Update copyright year --- README.md | 2 +- docker/main.cc | 2 +- example/client.cc | 2 +- example/hello.cc | 2 +- example/redirect.cc | 2 +- example/server.cc | 2 +- example/server_and_client.cc | 2 +- example/simplecli.cc | 2 +- example/simplesvr.cc | 2 +- example/ssecli-stream.cc | 2 +- example/ssecli.cc | 2 +- example/upload.cc | 2 +- httplib.h | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 71309ef262..aa94afe0d9 100644 --- a/README.md +++ b/README.md @@ -1350,7 +1350,7 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32 License ------- -MIT license (© 2025 Yuji Hirose) +MIT license (© 2026 Yuji Hirose) Special Thanks To ----------------- diff --git a/docker/main.cc b/docker/main.cc index edc7093bda..69279bfb89 100644 --- a/docker/main.cc +++ b/docker/main.cc @@ -1,7 +1,7 @@ // // main.cc // -// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/client.cc b/example/client.cc index a9b0fc0ac3..b89bb4c580 100644 --- a/example/client.cc +++ b/example/client.cc @@ -1,7 +1,7 @@ // // client.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/hello.cc b/example/hello.cc index 38d25a62b8..ca0d379c87 100644 --- a/example/hello.cc +++ b/example/hello.cc @@ -1,7 +1,7 @@ // // hello.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/redirect.cc b/example/redirect.cc index b1f39396a5..e9c04b2bf8 100644 --- a/example/redirect.cc +++ b/example/redirect.cc @@ -1,7 +1,7 @@ // // redirect.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/server.cc b/example/server.cc index 1c347f5f9c..6a80060637 100644 --- a/example/server.cc +++ b/example/server.cc @@ -1,7 +1,7 @@ // // sample.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/server_and_client.cc b/example/server_and_client.cc index 34bf852714..e35a02a515 100644 --- a/example/server_and_client.cc +++ b/example/server_and_client.cc @@ -1,7 +1,7 @@ // // server_and_client.cc // -// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/simplecli.cc b/example/simplecli.cc index b005e40848..a7e41b8290 100644 --- a/example/simplecli.cc +++ b/example/simplecli.cc @@ -1,7 +1,7 @@ // // simplecli.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/simplesvr.cc b/example/simplesvr.cc index e45e360646..bdf2a1c887 100644 --- a/example/simplesvr.cc +++ b/example/simplesvr.cc @@ -1,7 +1,7 @@ // // simplesvr.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/ssecli-stream.cc b/example/ssecli-stream.cc index 0dd279ad37..493a2232b6 100644 --- a/example/ssecli-stream.cc +++ b/example/ssecli-stream.cc @@ -1,7 +1,7 @@ // // ssecli-stream.cc // -// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // // SSE (Server-Sent Events) client example using Streaming API diff --git a/example/ssecli.cc b/example/ssecli.cc index f7b9ed55fb..98e26237fd 100644 --- a/example/ssecli.cc +++ b/example/ssecli.cc @@ -1,7 +1,7 @@ // // ssecli.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/example/upload.cc b/example/upload.cc index f7a822bedc..fd34e8f43a 100644 --- a/example/upload.cc +++ b/example/upload.cc @@ -1,7 +1,7 @@ // // upload.cc // -// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // diff --git a/httplib.h b/httplib.h index d7841a1603..acb5bc395f 100644 --- a/httplib.h +++ b/httplib.h @@ -1,7 +1,7 @@ // // httplib.h // -// Copyright (c) 2025 Yuji Hirose. All rights reserved. +// Copyright (c) 2026 Yuji Hirose. All rights reserved. // MIT License // From 781c55f120f945d254832660ef9487776b8a90fc Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 31 Dec 2025 22:19:05 -0500 Subject: [PATCH 0581/1222] Release v0.30.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index acb5bc395f..5c2c2871da 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.29.0" -#define CPPHTTPLIB_VERSION_NUM "0x001D00" +#define CPPHTTPLIB_VERSION "0.30.0" +#define CPPHTTPLIB_VERSION_NUM "0x001E00" /* * Platform compatibility check From 23a1d79a662a17398a3ea2c12bc818eedff24756 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 2 Jan 2026 20:45:01 -0500 Subject: [PATCH 0582/1222] Fix #2318 --- httplib.h | 20 ++++++++++++-------- test/test.cc | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index 5c2c2871da..e40e7f6191 100644 --- a/httplib.h +++ b/httplib.h @@ -2443,16 +2443,20 @@ namespace detail { #if defined(_WIN32) inline std::wstring u8string_to_wstring(const char *s) { - std::wstring ws; + if (!s) { return std::wstring(); } + auto len = static_cast(strlen(s)); + if (!len) { return std::wstring(); } + auto wlen = ::MultiByteToWideChar(CP_UTF8, 0, s, len, nullptr, 0); - if (wlen > 0) { - ws.resize(wlen); - wlen = ::MultiByteToWideChar( - CP_UTF8, 0, s, len, - const_cast(reinterpret_cast(ws.data())), wlen); - if (wlen != static_cast(ws.size())) { ws.clear(); } - } + if (!wlen) { return std::wstring(); } + + std::wstring ws; + ws.resize(wlen); + wlen = ::MultiByteToWideChar( + CP_UTF8, 0, s, len, + const_cast(reinterpret_cast(ws.data())), wlen); + if (wlen != static_cast(ws.size())) { ws.clear(); } return ws; } #endif diff --git a/test/test.cc b/test/test.cc index e1fa23937a..41f072b0b8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -14086,3 +14086,18 @@ TEST_F(SSEIntegrationTest, LastEventIdSentOnReconnect) { EXPECT_EQ(received_last_event_ids[1], "event-0"); } } + +TEST(Issue2318Test, EmptyHostString) { + { + httplib::Client cli_empty("", PORT); + auto res = cli_empty.Get("/"); + ASSERT_FALSE(res); + EXPECT_EQ(httplib::Error::Connection, res.error()); + } + { + httplib::Client cli(" ", PORT); + auto res = cli.Get("/"); + ASSERT_FALSE(res); + EXPECT_EQ(httplib::Error::Connection, res.error()); + } +} From 8d03ef1615039a4414a99add745cff5103cd617e Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 2 Jan 2026 22:28:15 -0500 Subject: [PATCH 0583/1222] Fix #2318 on macOS --- httplib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/httplib.h b/httplib.h index e40e7f6191..c69d8613d2 100644 --- a/httplib.h +++ b/httplib.h @@ -4547,6 +4547,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, return ret; #elif TARGET_OS_MAC + if (!node) { return EAI_NONAME; } // macOS implementation using CFHost API for asynchronous DNS resolution CFStringRef hostname_ref = CFStringCreateWithCString( kCFAllocatorDefault, node, kCFStringEncodingUTF8); From 59905c7f0d3c796dc2673918d4ed17476c57f335 Mon Sep 17 00:00:00 2001 From: seragh Date: Tue, 6 Jan 2026 02:03:18 +0100 Subject: [PATCH 0584/1222] Prevent redefinition of ssize_t (#2319) On Windows cpp-httplib defines ssize_t, therefore applications needing to define ssize_t for their own needs or are using libraries that do require a means to avoid a possible incompatible redefinition. Signed-off-by: Ralph Sennhauser --- httplib.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/httplib.h b/httplib.h index c69d8613d2..86c38aa820 100644 --- a/httplib.h +++ b/httplib.h @@ -205,7 +205,10 @@ #pragma comment(lib, "ws2_32.lib") +#ifndef _SSIZE_T_DEFINED using ssize_t = __int64; +#define _SSIZE_T_DEFINED +#endif #endif // _MSC_VER #ifndef S_ISREG From 2e2e47bab1ae6a853476eecbc4bf279dd1fef792 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 9 Jan 2026 21:09:07 -0500 Subject: [PATCH 0585/1222] Merge commit from fork * Ensure payload_max_length_ is respected for compressed payloads * Fix Denial of service (DOS) using zip bomb --------- Co-authored-by: Hritik Vijay --- httplib.h | 13 +++++-------- test/test.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/httplib.h b/httplib.h index 86c38aa820..afd71a2d6a 100644 --- a/httplib.h +++ b/httplib.h @@ -8982,14 +8982,11 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { strm, req, res, // Regular [&](const char *buf, size_t n) { - // Prevent arithmetic overflow when checking sizes. - // Avoid computing (req.body.size() + n) directly because - // adding two unsigned `size_t` values can wrap around and - // produce a small result instead of indicating overflow. - // Instead, check using subtraction: ensure `n` does not - // exceed the remaining capacity `max_size() - size()`. - if (req.body.size() >= req.body.max_size() || - n > req.body.max_size() - req.body.size()) { + // Limit decompressed body size to payload_max_length_ to protect + // against "zip bomb" attacks where a small compressed payload + // decompresses to a massive size. + if (req.body.size() + n > payload_max_length_ || + req.body.size() + n > req.body.max_size()) { return false; } req.body.append(buf, n); diff --git a/test/test.cc b/test/test.cc index 41f072b0b8..dbc2955e31 100644 --- a/test/test.cc +++ b/test/test.cc @@ -14101,3 +14101,52 @@ TEST(Issue2318Test, EmptyHostString) { EXPECT_EQ(httplib::Error::Connection, res.error()); } } + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +TEST(ZipBombProtectionTest, DecompressedSizeExceedsLimit) { + Server svr; + + // Set a small payload limit (1KB) + svr.set_payload_max_length(1024); + + svr.Post("/test", [&](const Request &req, Response &res) { + res.set_content("Body size: " + std::to_string(req.body.size()), + "text/plain"); + }); + + auto listen_thread = std::thread([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + }); + + svr.wait_until_ready(); + + // Create data that compresses well but exceeds limit when decompressed + // 8KB of repeated null bytes compresses to a very small size + std::string original_data(8 * 1024, '\0'); + + // Compress the data using gzip + std::string compressed_data; + detail::gzip_compressor compressor; + compressor.compress(original_data.data(), original_data.size(), true, + [&](const char *data, size_t size) { + compressed_data.append(data, size); + return true; + }); + + // Verify compression worked (compressed should be much smaller) + ASSERT_LT(compressed_data.size(), original_data.size()); + ASSERT_LT(compressed_data.size(), 1024u); // Compressed fits in limit + + // Send compressed data with Content-Encoding: gzip + Client cli(HOST, PORT); + Headers headers = {{"Content-Encoding", "gzip"}}; + auto res = + cli.Post("/test", headers, compressed_data, "application/octet-stream"); + + // Server should reject because decompressed size (8KB) exceeds limit (1KB) + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::BadRequest_400, res->status); +} +#endif From bd95e67c234930cd6d6bb11309588c5462c63cec Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 9 Jan 2026 21:35:03 -0500 Subject: [PATCH 0586/1222] Release v0.30.1 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index afd71a2d6a..ec8d2a212f 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.30.0" -#define CPPHTTPLIB_VERSION_NUM "0x001E00" +#define CPPHTTPLIB_VERSION "0.30.1" +#define CPPHTTPLIB_VERSION_NUM "0x001E01" /* * Platform compatibility check From 6eff49e1fb96d7d66d10d6bd2d8dea347da6d7aa Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 10 Jan 2026 19:23:24 -0500 Subject: [PATCH 0587/1222] Problem with CI test on Windows without OpenSSL (#2323) * Fix problem with 'windows without SSL` * Fix payload limit enforcement for requests without Content-Length on Windows - Enable MSG_PEEK on Windows (non-SSL builds) to detect payloads without Content-Length - Only use MSG_PEEK when payload_max_length is set to a finite value to avoid blocking - Use read_content_without_length for actual size checking to support any payload limit - Set 413 Payload Too Large status before rejecting oversized requests This fixes three test cases on Windows: - RequestWithoutContentLengthOrTransferEncoding (no payload limit) - NoContentLengthPayloadLimit (8-byte limit) - NoContentLengthExceeds10MB (10MB limit) * clang-format --- .github/workflows/test.yaml | 1 + httplib.h | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 135bf7b500..59c3a29f67 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -156,6 +156,7 @@ jobs: -DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake -DHTTPLIB_TEST=ON -DHTTPLIB_COMPILE=${{ matrix.config.compiled && 'ON' || 'OFF' }} + -DHTTPLIB_USE_OPENSSL_IF_AVAILABLE=${{ matrix.config.with_ssl && 'ON' || 'OFF' }} -DHTTPLIB_REQUIRE_ZLIB=ON -DHTTPLIB_REQUIRE_BROTLI=ON -DHTTPLIB_REQUIRE_ZSTD=ON diff --git a/httplib.h b/httplib.h index ec8d2a212f..fbb38b226c 100644 --- a/httplib.h +++ b/httplib.h @@ -9078,22 +9078,29 @@ inline bool Server::read_content_core( // oversized request and fail early (causing connection close). For SSL // builds we cannot reliably peek the decrypted application bytes, so keep // the original behaviour. -#if !defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(_WIN32) +#if !defined(CPPHTTPLIB_OPENSSL_SUPPORT) if (!req.has_header("Content-Length") && !detail::is_chunked_transfer_encoding(req.headers)) { - socket_t s = strm.socket(); - if (s != INVALID_SOCKET) { - // Peek up to payload_max_length_ + 1 bytes. If more than - // payload_max_length_ bytes are pending, reject the request. - size_t to_peek = - (payload_max_length_ > 0) - ? (std::min)(payload_max_length_ + 1, static_cast(4096)) - : 1; - std::vector peekbuf(to_peek); - ssize_t n = ::recv(s, peekbuf.data(), to_peek, MSG_PEEK); - if (n > 0 && static_cast(n) > payload_max_length_) { - // Indicate failure so connection will be closed. - return false; + // Only peek if payload_max_length is set to a finite value + if (payload_max_length_ > 0 && + payload_max_length_ < (std::numeric_limits::max)()) { + socket_t s = strm.socket(); + if (s != INVALID_SOCKET) { + // Peek to check if there is any pending data + char peekbuf[1]; + ssize_t n = ::recv(s, peekbuf, 1, MSG_PEEK); + if (n > 0) { + // There is data, so read it with payload limit enforcement + auto result = detail::read_content_without_length( + strm, payload_max_length_, out); + if (result == detail::ReadContentResult::PayloadTooLarge) { + res.status = StatusCode::PayloadTooLarge_413; + return false; + } else if (result != detail::ReadContentResult::Success) { + return false; + } + return true; + } } } return true; From a7e1d14b1538e0900a8d7e607a311f9e216cd987 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 10 Jan 2026 19:18:50 -0500 Subject: [PATCH 0588/1222] Fix warning on Windows --- test/test.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index dbc2955e31..e8471ac902 100644 --- a/test/test.cc +++ b/test/test.cc @@ -10709,7 +10709,15 @@ TEST(VulnerabilityTest, CRLFInjectionInHeaders) { } std::string resp = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello"; - ::send(cli, resp.c_str(), resp.size(), 0); + + ::send(cli, +#ifdef _WIN32 + static_cast(resp.c_str()), + static_cast(resp.size()), +#else + resp.c_str(), resp.size(), +#endif + 0); buf_all.erase(0, pos + 4); } From 1111219f17ab83046e2b88fc37169b77754d3557 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 10 Jan 2026 21:05:30 -0500 Subject: [PATCH 0589/1222] Fix #2324 --- httplib.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index fbb38b226c..526623aa6b 100644 --- a/httplib.h +++ b/httplib.h @@ -8982,13 +8982,26 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { strm, req, res, // Regular [&](const char *buf, size_t n) { + // Prevent arithmetic overflow when checking sizes. + // Avoid computing (req.body.size() + n) directly because + // adding two unsigned `size_t` values can wrap around and + // produce a small result instead of indicating overflow. + // Instead, check using subtraction: ensure `n` does not + // exceed the remaining capacity `max_size() - size()`. + if (req.body.size() >= req.body.max_size() || + n > req.body.max_size() - req.body.size()) { + return false; + } + // Limit decompressed body size to payload_max_length_ to protect // against "zip bomb" attacks where a small compressed payload // decompresses to a massive size. - if (req.body.size() + n > payload_max_length_ || - req.body.size() + n > req.body.max_size()) { + if (payload_max_length_ > 0 && + (req.body.size() >= payload_max_length_ || + n > payload_max_length_ - req.body.size())) { return false; } + req.body.append(buf, n); return true; }, From cea018f2cdf67d3742f617c6884cd1df84fe75ad Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 11 Jan 2026 21:22:42 -0500 Subject: [PATCH 0590/1222] Fix #2324 --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index e8471ac902..d364019824 100644 --- a/test/test.cc +++ b/test/test.cc @@ -11017,7 +11017,7 @@ TEST(MakeHostAndPortStringTest, VariousPatterns) { } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -TEST(SSLClientHostHeaderTest, Issue2301) { +TEST(SSLClientHostHeaderTest, Issue2301_Online) { httplib::SSLClient cli("roblox.com", 443); cli.set_follow_location(true); From b85aa76bd2d93facfeaf184107e811c2dd8da19a Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 16 Jan 2026 11:10:34 -0500 Subject: [PATCH 0591/1222] Fix #2321, #2322, #2326 --- test/test.cc | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/test/test.cc b/test/test.cc index d364019824..5591a12149 100644 --- a/test/test.cc +++ b/test/test.cc @@ -27,6 +27,15 @@ #include #include +#if __cplusplus >= 202002L +inline std::string u8_to_string(const char8_t *s) { + return std::string(reinterpret_cast(s)); +} +#define U8(x) u8_to_string(u8##x) +#else +#define U8(x) u8##x +#endif + #define SERVER_CERT_FILE "./cert.pem" #define SERVER_CERT2_FILE "./cert2.pem" #define SERVER_PRIVATE_KEY_FILE "./key.pem" @@ -372,7 +381,7 @@ TEST(DecodePathTest, PercentCharacter) { EXPECT_EQ( decode_path_component( R"(descrip=Gastos%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1%C3%91%206)"), - u8"descrip=Gastos áéíóúñÑ 6"); + U8("descrip=Gastos áéíóúñÑ 6")); } TEST(DecodePathTest, PercentCharacterNUL) { @@ -430,9 +439,9 @@ TEST(ClientQueryOrder, PreserveOrder) { } TEST(EncodeQueryParamTest, TestUTF8Characters) { - string chineseCharacters = u8"中国語"; - string russianCharacters = u8"дом"; - string brazilianCharacters = u8"óculos"; + string chineseCharacters = U8("中国語"); + string russianCharacters = U8("дом"); + string brazilianCharacters = U8("óculos"); EXPECT_EQ(httplib::encode_uri_component(chineseCharacters), "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); @@ -457,9 +466,9 @@ TEST(EncodeUriComponentTest, ParseReservedCharactersTest) { } TEST(EncodeUriComponentTest, TestUTF8Characters) { - string chineseCharacters = u8"中国語"; - string russianCharacters = u8"дом"; - string brazilianCharacters = u8"óculos"; + string chineseCharacters = U8("中国語"); + string russianCharacters = U8("дом"); + string brazilianCharacters = U8("óculos"); EXPECT_EQ(httplib::encode_uri_component(chineseCharacters), "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); @@ -491,9 +500,9 @@ TEST(EncodeUriTest, ParseReservedCharactersTest) { } TEST(EncodeUriTest, TestUTF8Characters) { - string chineseCharacters = u8"中国語"; - string russianCharacters = u8"дом"; - string brazilianCharacters = u8"óculos"; + string chineseCharacters = U8("中国語"); + string russianCharacters = U8("дом"); + string brazilianCharacters = U8("óculos"); EXPECT_EQ(httplib::encode_uri(chineseCharacters), "%E4%B8%AD%E5%9B%BD%E8%AA%9E"); @@ -538,9 +547,9 @@ TEST(DecodeUriComponentTest, TestUTF8Characters) { string encodedRussian = "%D0%B4%D0%BE%D0%BC"; string encodedBrazilian = "%C3%B3culos"; - EXPECT_EQ(httplib::decode_uri_component(encodedChinese), u8"中国語"); - EXPECT_EQ(httplib::decode_uri_component(encodedRussian), u8"дом"); - EXPECT_EQ(httplib::decode_uri_component(encodedBrazilian), u8"óculos"); + EXPECT_EQ(httplib::decode_uri_component(encodedChinese), U8("中国語")); + EXPECT_EQ(httplib::decode_uri_component(encodedRussian), U8("дом")); + EXPECT_EQ(httplib::decode_uri_component(encodedBrazilian), U8("óculos")); } TEST(DecodeUriComponentTest, TestPathComponentDecoding) { @@ -567,9 +576,9 @@ TEST(DecodeUriTest, TestUTF8Characters) { string encodedRussian = "%D0%B4%D0%BE%D0%BC"; string encodedBrazilian = "%C3%B3culos"; - EXPECT_EQ(httplib::decode_uri(encodedChinese), u8"中国語"); - EXPECT_EQ(httplib::decode_uri(encodedRussian), u8"дом"); - EXPECT_EQ(httplib::decode_uri(encodedBrazilian), u8"óculos"); + EXPECT_EQ(httplib::decode_uri(encodedChinese), U8("中国語")); + EXPECT_EQ(httplib::decode_uri(encodedRussian), U8("дом")); + EXPECT_EQ(httplib::decode_uri(encodedBrazilian), U8("óculos")); } TEST(DecodeUriTest, TestCompleteUri) { @@ -7422,10 +7431,10 @@ TEST(MountTest, MultibytesPathName) { Client cli("localhost", PORT); - auto res = cli.Get(u8"/日本語Dir/日本語File.txt"); + auto res = cli.Get(U8("/日本語Dir/日本語File.txt")); ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); - EXPECT_EQ(u8"日本語コンテンツ", res->body); + EXPECT_EQ(U8("日本語コンテンツ"), res->body); } TEST(KeepAliveTest, ReadTimeout) { From 7dec57d1eb5c84b9112caeaab7ee37e6e075d466 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 16 Jan 2026 16:25:06 -0500 Subject: [PATCH 0592/1222] Fix #2325 --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 526623aa6b..080df627ad 100644 --- a/httplib.h +++ b/httplib.h @@ -4833,7 +4833,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, bool ipv6_v6only, SocketOptions socket_options, BindOrConnect bind_or_connect, time_t timeout_sec = 0) { // Get address info - const char *node = nullptr; + const char *node = ""; struct addrinfo hints; struct addrinfo *result; From c0469eba96e2b44b8fb8b6446a4653ffd86830c2 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 16 Jan 2026 17:28:28 -0500 Subject: [PATCH 0593/1222] Revert "Fix #2325" This reverts commit 7dec57d1eb5c84b9112caeaab7ee37e6e075d466. --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 080df627ad..526623aa6b 100644 --- a/httplib.h +++ b/httplib.h @@ -4833,7 +4833,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, bool ipv6_v6only, SocketOptions socket_options, BindOrConnect bind_or_connect, time_t timeout_sec = 0) { // Get address info - const char *node = ""; + const char *node = nullptr; struct addrinfo hints; struct addrinfo *result; From 0e1b52b23e7d434b359de6807b2455c1ab75d4a7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 16 Jan 2026 18:19:14 -0500 Subject: [PATCH 0594/1222] Fix #2325 (#2331) * Fix #2325 * clang-format --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 526623aa6b..44cc8fb621 100644 --- a/httplib.h +++ b/httplib.h @@ -4788,7 +4788,7 @@ inline int getaddrinfo_with_timeout(const char *node, const char *service, // Allocate on the heap, so the resolver thread can keep using the data. auto state = std::make_shared(); - state->node = node; + if (node) { state->node = node; } state->service = service; state->hints = *hints; From a38a076571817e47f4f260e0e56732ecec1b3981 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 18 Jan 2026 00:38:43 -0500 Subject: [PATCH 0595/1222] Resolve #2262 (#2332) * Resolve #2262 * Enhance request handling on Windows by adding early response check for large request bodies * Enhance early response handling for large requests with long URIs on Windows --- httplib.h | 46 ++++++++++++++++++++++++++++++++++++++++++++-- test/test.cc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 44cc8fb621..5308bb0f35 100644 --- a/httplib.h +++ b/httplib.h @@ -286,6 +286,7 @@ using socket_t = int; #include #include #include +#include #include #include #include @@ -11014,6 +11015,44 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, } } + // After sending request line and headers, wait briefly for an early server + // response (e.g. 4xx) and avoid sending a potentially large request body + // unnecessarily. This workaround is only enabled on Windows because Unix + // platforms surface write errors (EPIPE) earlier; on Windows kernel send + // buffering can accept large writes even when the peer already responded. + // Check the stream first (which covers SSL via `is_readable()`), then + // fall back to select on the socket. Only perform the wait for very large + // request bodies to avoid interfering with normal small requests and + // reduce side-effects. Poll briefly (up to 50ms) for an early response. +#if defined(_WIN32) + if (req.body.size() > (1u << 20) && + req.path.size() > + CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { // > 1MB && long URI + auto start = std::chrono::high_resolution_clock::now(); + const auto max_wait_ms = 50; + for (;;) { + auto sock = strm.socket(); + // Prefer socket-level readiness to avoid SSL_pending() false-positives + // from SSL internals. If the underlying socket is readable, assume an + // early response may be present. + if (sock != INVALID_SOCKET && detail::select_read(sock, 0, 0) > 0) { + return false; + } + // Fallback to stream-level check for non-socket streams or when the + // socket isn't reporting readable. Avoid using `is_readable()` for + // SSL, since `SSL_pending()` may report buffered records that do not + // indicate a complete application-level response yet. + if (!is_ssl() && strm.is_readable()) { return false; } + auto now = std::chrono::high_resolution_clock::now(); + auto elapsed = + std::chrono::duration_cast(now - start) + .count(); + if (elapsed >= max_wait_ms) { break; } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } +#endif + // Body if (req.body.empty()) { return write_content_with_provider(strm, req, error); @@ -11191,7 +11230,8 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error) { // Send request - if (!write_request(strm, req, close_connection, error)) { return false; } + auto write_request_success = + write_request(strm, req, close_connection, error); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_ssl()) { @@ -11209,11 +11249,13 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, // Receive response and headers if (!read_response_line(strm, req, res) || !detail::read_headers(strm, res.headers)) { - error = Error::Read; + if (write_request_success) { error = Error::Read; } output_error_log(error, &req); return false; } + if (!write_request_success) { return false; } + // Body if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && req.method != "CONNECT") { diff --git a/test/test.cc b/test/test.cc index 5591a12149..edda74b4e7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6567,6 +6567,53 @@ TEST_F(ServerTest, PreCompressionLoggingOnlyPreLogger) { EXPECT_EQ(test_content, pre_compression_body); } +TEST_F(ServerTest, SendLargeBodyAfterRequestLineError) { + { + Request req; + req.method = "POST"; + req.path = "/post-large?q=" + LONG_QUERY_VALUE; + req.body = LARGE_DATA; + + Response res; + auto error = Error::Success; + + auto start = std::chrono::high_resolution_clock::now(); + cli_.set_keep_alive(true); + auto ret = cli_.send(req, res, error); + auto end = std::chrono::high_resolution_clock::now(); + + auto elapsed = + std::chrono::duration_cast(end - start) + .count(); + + EXPECT_FALSE(ret); + EXPECT_EQ(StatusCode::UriTooLong_414, res.status); + EXPECT_EQ("close", res.get_header_value("Connection")); + EXPECT_FALSE(cli_.is_socket_open()); + EXPECT_LE(elapsed, 200); + } + + { + // Send an extra GET request to ensure error recovery without hanging + Request req; + req.method = "GET"; + req.path = "/hi"; + + auto start = std::chrono::high_resolution_clock::now(); + auto res = cli_.send(req); + auto end = std::chrono::high_resolution_clock::now(); + + auto elapsed = + std::chrono::duration_cast(end - start) + .count(); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ("Hello World!", res->body); + EXPECT_LE(elapsed, 100); + } +} + TEST(ZstdDecompressor, ChunkedDecompression) { std::string data; for (size_t i = 0; i < 32 * 1024; ++i) { From 02dfb97fd61519b8e5f6dcb71070102a71dd15b1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 18 Jan 2026 20:48:13 -0500 Subject: [PATCH 0596/1222] Add `Expect: 100-continue` support --- httplib.h | 110 +++++++++++++++++++++++++++++++++++++++++++-------- test/test.cc | 26 +++++++++++- 2 files changed, 118 insertions(+), 18 deletions(-) diff --git a/httplib.h b/httplib.h index 5308bb0f35..4193a4f43a 100644 --- a/httplib.h +++ b/httplib.h @@ -98,6 +98,22 @@ #define CPPHTTPLIB_CLIENT_MAX_TIMEOUT_MSECOND 0 #endif +#ifndef CPPHTTPLIB_EXPECT_100_THRESHOLD +#define CPPHTTPLIB_EXPECT_100_THRESHOLD 1024 +#endif + +#ifndef CPPHTTPLIB_EXPECT_100_TIMEOUT_MSECOND +#define CPPHTTPLIB_EXPECT_100_TIMEOUT_MSECOND 1000 +#endif + +#ifndef CPPHTTPLIB_WAIT_EARLY_SERVER_RESPONSE_THRESHOLD +#define CPPHTTPLIB_WAIT_EARLY_SERVER_RESPONSE_THRESHOLD (1024 * 1024) +#endif + +#ifndef CPPHTTPLIB_WAIT_EARLY_SERVER_RESPONSE_TIMEOUT_MSECOND +#define CPPHTTPLIB_WAIT_EARLY_SERVER_RESPONSE_TIMEOUT_MSECOND 50 +#endif + #ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND #define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 #endif @@ -1849,10 +1865,11 @@ class ClientImpl { Result send_(Request &&req); socket_t create_client_socket(Error &error) const; - bool read_response_line(Stream &strm, const Request &req, - Response &res) const; + bool read_response_line(Stream &strm, const Request &req, Response &res, + bool skip_100_continue = true) const; bool write_request(Stream &strm, Request &req, bool close_connection, - Error &error); + Error &error, bool skip_body = false); + bool write_request_body(Stream &strm, Request &req, Error &error); void prepare_default_headers(Request &r, bool for_stream, const std::string &ct); bool redirect(Request &req, Response &res, Error &error); @@ -10089,7 +10106,8 @@ inline void ClientImpl::close_socket(Socket &socket) { } inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, - Response &res) const { + Response &res, + bool skip_100_continue) const { std::array buf{}; detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); @@ -10110,8 +10128,8 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, res.status = std::stoi(std::string(m[2])); res.reason = std::string(m[3]); - // Ignore '100 Continue' - while (res.status == StatusCode::Continue_100) { + // Ignore '100 Continue' (only when not using Expect: 100-continue explicitly) + while (skip_100_continue && res.status == StatusCode::Continue_100) { if (!line_reader.getline()) { return false; } // CRLF if (!line_reader.getline()) { return false; } // next response line @@ -10896,7 +10914,8 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm, } inline bool ClientImpl::write_request(Stream &strm, Request &req, - bool close_connection, Error &error) { + bool close_connection, Error &error, + bool skip_body) { // Prepare additional headers if (close_connection) { if (!req.has_header("Connection")) { @@ -11023,37 +11042,51 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, // Check the stream first (which covers SSL via `is_readable()`), then // fall back to select on the socket. Only perform the wait for very large // request bodies to avoid interfering with normal small requests and - // reduce side-effects. Poll briefly (up to 50ms) for an early response. + // reduce side-effects. Poll briefly (up to 50ms as default) for an early + // response. Skip this check when using Expect: 100-continue, as the protocol + // handles early responses properly. #if defined(_WIN32) - if (req.body.size() > (1u << 20) && - req.path.size() > - CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { // > 1MB && long URI + if (!skip_body && + req.body.size() > CPPHTTPLIB_WAIT_EARLY_SERVER_RESPONSE_THRESHOLD && + req.path.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { auto start = std::chrono::high_resolution_clock::now(); - const auto max_wait_ms = 50; + for (;;) { - auto sock = strm.socket(); // Prefer socket-level readiness to avoid SSL_pending() false-positives // from SSL internals. If the underlying socket is readable, assume an // early response may be present. + auto sock = strm.socket(); if (sock != INVALID_SOCKET && detail::select_read(sock, 0, 0) > 0) { return false; } + // Fallback to stream-level check for non-socket streams or when the // socket isn't reporting readable. Avoid using `is_readable()` for // SSL, since `SSL_pending()` may report buffered records that do not // indicate a complete application-level response yet. if (!is_ssl() && strm.is_readable()) { return false; } + auto now = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast(now - start) .count(); - if (elapsed >= max_wait_ms) { break; } + if (elapsed >= CPPHTTPLIB_WAIT_EARLY_SERVER_RESPONSE_TIMEOUT_MSECOND) { + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } #endif // Body + if (skip_body) { return true; } + + return write_request_body(strm, req, error); +} + +inline bool ClientImpl::write_request_body(Stream &strm, Request &req, + Error &error) { if (req.body.empty()) { return write_content_with_provider(strm, req, error); } @@ -11229,9 +11262,20 @@ inline void ClientImpl::output_error_log(const Error &err, inline bool ClientImpl::process_request(Stream &strm, Request &req, Response &res, bool close_connection, Error &error) { - // Send request + // Auto-add Expect: 100-continue for large bodies + if (CPPHTTPLIB_EXPECT_100_THRESHOLD > 0 && !req.has_header("Expect")) { + auto body_size = req.body.empty() ? req.content_length_ : req.body.size(); + if (body_size >= CPPHTTPLIB_EXPECT_100_THRESHOLD) { + req.set_header("Expect", "100-continue"); + } + } + + // Check for Expect: 100-continue + auto expect_100_continue = req.get_header_value("Expect") == "100-continue"; + + // Send request (skip body if using Expect: 100-continue) auto write_request_success = - write_request(strm, req, close_connection, error); + write_request(strm, req, close_connection, error, expect_100_continue); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT if (is_ssl()) { @@ -11246,8 +11290,21 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } #endif + // Handle Expect: 100-continue with timeout + if (expect_100_continue && CPPHTTPLIB_EXPECT_100_TIMEOUT_MSECOND > 0) { + time_t sec = CPPHTTPLIB_EXPECT_100_TIMEOUT_MSECOND / 1000; + time_t usec = (CPPHTTPLIB_EXPECT_100_TIMEOUT_MSECOND % 1000) * 1000; + auto ret = detail::select_read(strm.socket(), sec, usec); + if (ret <= 0) { + // Timeout or error: send body anyway (server didn't respond in time) + if (!write_request_body(strm, req, error)) { return false; } + expect_100_continue = false; // Switch to normal response handling + } + } + // Receive response and headers - if (!read_response_line(strm, req, res) || + // When using Expect: 100-continue, don't auto-skip `100 Continue` response + if (!read_response_line(strm, req, res, !expect_100_continue) || !detail::read_headers(strm, res.headers)) { if (write_request_success) { error = Error::Read; } output_error_log(error, &req); @@ -11256,6 +11313,25 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (!write_request_success) { return false; } + // Handle Expect: 100-continue response + if (expect_100_continue) { + if (res.status == StatusCode::Continue_100) { + // Server accepted, send the body + if (!write_request_body(strm, req, error)) { return false; } + + // Read the actual response + res.headers.clear(); + res.body.clear(); + if (!read_response_line(strm, req, res) || + !detail::read_headers(strm, res.headers)) { + error = Error::Read; + output_error_log(error, &req); + return false; + } + } + // If not 100 Continue, server returned an error; proceed with that response + } + // Body if ((res.status != StatusCode::NoContent_204) && req.method != "HEAD" && req.method != "CONNECT") { diff --git a/test/test.cc b/test/test.cc index edda74b4e7..b0f64da98d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -6569,9 +6569,32 @@ TEST_F(ServerTest, PreCompressionLoggingOnlyPreLogger) { TEST_F(ServerTest, SendLargeBodyAfterRequestLineError) { { + // Test with Expect: 100-continue header - success case + // Server returns 100 Continue, client sends body, server returns 200 OK + Request req; + req.method = "POST"; + req.path = "/post-large"; + req.set_header("Expect", "100-continue"); + req.body = LARGE_DATA; + + Response res; + auto error = Error::Success; + + cli_.set_keep_alive(true); + auto ret = cli_.send(req, res, error); + + EXPECT_TRUE(ret); + EXPECT_EQ(StatusCode::OK_200, res.status); + EXPECT_EQ(LARGE_DATA, res.body); + } + + { + // Test with Expect: 100-continue header - error case + // Client should not send the body when server returns an error Request req; req.method = "POST"; req.path = "/post-large?q=" + LONG_QUERY_VALUE; + req.set_header("Expect", "100-continue"); req.body = LARGE_DATA; Response res; @@ -6586,7 +6609,8 @@ TEST_F(ServerTest, SendLargeBodyAfterRequestLineError) { std::chrono::duration_cast(end - start) .count(); - EXPECT_FALSE(ret); + // With Expect: 100-continue, request completes successfully but with error + EXPECT_TRUE(ret); EXPECT_EQ(StatusCode::UriTooLong_414, res.status); EXPECT_EQ("close", res.get_header_value("Connection")); EXPECT_FALSE(cli_.is_socket_open()); From ad5839f0d1c579066de75a78f45be0e2aa2ef05c Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 20 Jan 2026 18:43:21 -0500 Subject: [PATCH 0597/1222] Add retry logic to BenchmarkTest test on Windows --- .github/workflows/test.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 59c3a29f67..36e3610520 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -165,7 +165,10 @@ jobs: run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine - name: Run tests ${{ matrix.config.name }} if: ${{ matrix.config.run_tests }} - run: ctest --output-on-failure --test-dir build -C Release + run: ctest --output-on-failure --test-dir build -C Release -E "BenchmarkTest" + - name: Run benchmark tests with retry ${{ matrix.config.name }} + if: ${{ matrix.config.run_tests }} + run: ctest --output-on-failure --test-dir build -C Release -R "BenchmarkTest" --repeat until-pass:5 env: VCPKG_ROOT: "C:/vcpkg" From 191bfb2ea4c2c1cb29063937a5be2f57393515d8 Mon Sep 17 00:00:00 2001 From: TH Date: Fri, 23 Jan 2026 07:56:34 +0800 Subject: [PATCH 0598/1222] Fix build error when zstd < 1.5.6 lacks zstd::libzstd CMake target (#2334) Fix #2313 --- CMakeLists.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0f8f45dce..e51611e078 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,10 +185,11 @@ elseif(HTTPLIB_USE_BROTLI_IF_AVAILABLE) set(HTTPLIB_IS_USING_BROTLI ${Brotli_FOUND}) endif() -# NOTE: When using cpp-httplib as a subproject (e.g., via FetchContent), the zstd::libzstd target may not be visible in the parent project scope. -# If you encounter a "target not found" error, see https://github.com/yhirose/cpp-httplib/issues/2313 for a workaround. +# NOTE: +# zstd < 1.5.6 does not provide the CMake imported target `zstd::libzstd`. +# Older versions must be consumed via their pkg-config file. if(HTTPLIB_REQUIRE_ZSTD) - find_package(zstd) + find_package(zstd 1.5.6 CONFIG) if(NOT zstd_FOUND) find_package(PkgConfig REQUIRED) pkg_check_modules(zstd REQUIRED IMPORTED_TARGET libzstd) @@ -196,7 +197,7 @@ if(HTTPLIB_REQUIRE_ZSTD) endif() set(HTTPLIB_IS_USING_ZSTD TRUE) elseif(HTTPLIB_USE_ZSTD_IF_AVAILABLE) - find_package(zstd QUIET) + find_package(zstd 1.5.6 CONFIG QUIET) if(NOT zstd_FOUND) find_package(PkgConfig QUIET) if(PKG_CONFIG_FOUND) From f73e694f0ccd6a0cefe886a1d04f787cdaa84bf7 Mon Sep 17 00:00:00 2001 From: Prajwal B Mehendarkar Date: Fri, 23 Jan 2026 05:27:23 +0530 Subject: [PATCH 0599/1222] timegm api absent in AIX (#2336) --- httplib.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/httplib.h b/httplib.h index 4193a4f43a..5e3b0f81f4 100644 --- a/httplib.h +++ b/httplib.h @@ -3547,6 +3547,8 @@ inline time_t parse_http_date(const std::string &date_str) { #ifdef _WIN32 return _mkgmtime(&tm_buf); +#elif defined _AIX + return mktime(&tm_buf); #else return timegm(&tm_buf); #endif From c3fa06112bb0be757fbc1b3121d08e1caf7bc5e6 Mon Sep 17 00:00:00 2001 From: Sung Po-Han Date: Fri, 23 Jan 2026 07:58:25 +0800 Subject: [PATCH 0600/1222] Fix set_ca_cert_store() to skip system certs like set_ca_cert_path() (#2335) Both APIs conceptually do the same thing: "use these CA certs for verification." However, set_ca_cert_store() falls through to the else branch in load_certs() where system certs are added to the user's custom store, defeating the purpose of certificate pinning. This change makes set_ca_cert_store() behave consistently with set_ca_cert_path() by checking ca_cert_store_ before loading system certificates. Added test to verify system certs are not loaded when custom store is set. Co-authored-by: Your --- httplib.h | 2 +- test/test.cc | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 5e3b0f81f4..abe73a1c58 100644 --- a/httplib.h +++ b/httplib.h @@ -13095,7 +13095,7 @@ inline bool SSLClient::load_certs() { last_openssl_error_ = ERR_get_error(); ret = false; } - } else { + } else if (!ca_cert_store_) { auto loaded = false; #ifdef _WIN32 loaded = diff --git a/test/test.cc b/test/test.cc index b0f64da98d..5964096f40 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9682,6 +9682,27 @@ TEST(SSLClientRedirectTest, CertFile) { ASSERT_EQ(StatusCode::OK_200, res->status); } +// Test that set_ca_cert_store() skips system certs (consistent with +// set_ca_cert_path behavior). When a custom cert store is set, only those certs +// should be trusted - system certs should NOT be loaded. +TEST(SSLClientTest, SetCaCertStoreSkipsSystemCerts_Online) { + // Load a specific cert that is NOT a system CA cert + std::string cert; + read_file(SERVER_CERT2_FILE, cert); + + SSLClient cli("google.com"); + cli.load_ca_cert_store(cert.c_str(), cert.size()); + cli.enable_server_certificate_verification(true); + + // This should FAIL because: + // 1. We loaded only SERVER_CERT2 (a test cert, not a CA for google.com) + // 2. System certs should NOT be loaded when custom store is set + // If system certs WERE loaded, this would succeed + auto res = cli.Get("/"); + ASSERT_FALSE(res); + EXPECT_EQ(Error::SSLServerVerification, res.error()); +} + TEST(MultipartFormDataTest, LargeData) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); From fbec2a3466efa5fb89527195fd103f2f116d98f5 Mon Sep 17 00:00:00 2001 From: ctabor-itracs <166428682+ctabor-itracs@users.noreply.github.com> Date: Fri, 23 Jan 2026 12:58:47 -0700 Subject: [PATCH 0601/1222] case insensitive hostname validation (fix #2333) (#2337) * Add Tests for case sensitive hostname verfication * Modify check_host_name to perform case insensitive comparisons during ssl hostname validation --------- Co-authored-by: Tabor, Chris --- httplib.h | 24 ++++++++++++++++++++---- test/test.cc | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/httplib.h b/httplib.h index abe73a1c58..b556c83df6 100644 --- a/httplib.h +++ b/httplib.h @@ -13342,7 +13342,11 @@ inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { inline bool SSLClient::check_host_name(const char *pattern, size_t pattern_len) const { - if (host_.size() == pattern_len && host_ == pattern) { return true; } + // Exact match (case-insensitive) + if (host_.size() == pattern_len && + detail::case_ignore::equal(host_, std::string(pattern, pattern_len))) { + return true; + } // Wildcard match // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 @@ -13357,9 +13361,21 @@ inline bool SSLClient::check_host_name(const char *pattern, auto itr = pattern_components.begin(); for (const auto &h : host_components_) { auto &p = *itr; - if (p != h && p != "*") { - auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && - !p.compare(0, p.size() - 1, h)); + if (!httplib::detail::case_ignore::equal(p, h) && p != "*") { + bool partial_match = false; + if (!p.empty() && p[p.size() - 1] == '*') { + const auto prefix_length = p.size() - 1; + if (prefix_length == 0) { + partial_match = true; + } else if (h.size() >= prefix_length) { + partial_match = + std::equal(p.begin(), p.begin() + prefix_length, h.begin(), + [](const char ca, const char cb) { + return httplib::detail::case_ignore::to_lower(ca) == + httplib::detail::case_ignore::to_lower(cb); + }); + } + } if (!partial_match) { return false; } } ++itr; diff --git a/test/test.cc b/test/test.cc index 5964096f40..6f9511ec4e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8717,6 +8717,31 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(SSLClientTest, WildcardHostNameMatchCase_Online) { + SSLClient cli("wWw.YouTube.Com"); + + cli.set_ca_cert_path(CA_CERT_FILE); + cli.enable_server_certificate_verification(true); + cli.enable_server_hostname_verification(true); + cli.set_follow_location(true); + + auto res = cli.Get("/"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); +} + +TEST(SSLClientTest, HostNameMatchCase_Online) { + SSLClient cli("gOoGlE.COm"); + + cli.enable_server_certificate_verification(true); + cli.enable_server_hostname_verification(true); + cli.set_follow_location(true); + + auto res = cli.Get("/"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); +} + TEST(SSLClientTest, Issue2004_Online) { Client client("https://google.com"); client.set_follow_location(true); From 094bf112bbb5b7458a4a4b3dbc8fbed7f3e871bd Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 30 Jan 2026 22:01:43 -0500 Subject: [PATCH 0602/1222] Fix #2340 --- .github/workflows/test_no_exceptions.yaml | 20 ++++++++++++++ httplib.h | 7 +++++ test/test.cc | 33 ++++++++++++++++++++++- 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test_no_exceptions.yaml diff --git a/.github/workflows/test_no_exceptions.yaml b/.github/workflows/test_no_exceptions.yaml new file mode 100644 index 0000000000..7e71635f6c --- /dev/null +++ b/.github/workflows/test_no_exceptions.yaml @@ -0,0 +1,20 @@ +name: No Exceptions Test + +on: [push, pull_request] + +jobs: + test-no-exceptions: + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name + + steps: + - uses: actions/checkout@v3 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential libssl-dev zlib1g-dev libcurl4-openssl-dev libbrotli-dev libzstd-dev + + - name: Run tests with CPPHTTPLIB_NO_EXCEPTIONS + run: | + cd test && make EXTRA_CXXFLAGS="-fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS" diff --git a/httplib.h b/httplib.h index b556c83df6..e98d462958 100644 --- a/httplib.h +++ b/httplib.h @@ -3261,11 +3261,18 @@ class SSEClient { msg.id = value; } else if (field == "retry") { // Parse retry interval in milliseconds +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + { + std::istringstream iss(value); + iss >> retry_ms; + } +#else try { retry_ms = std::stoi(value); } catch (...) { // Invalid retry value, ignore } +#endif } // Unknown fields are ignored per SSE spec diff --git a/test/test.cc b/test/test.cc index 6f9511ec4e..d03df186f4 100644 --- a/test/test.cc +++ b/test/test.cc @@ -82,7 +82,13 @@ FormData &get_file_value(std::vector &items, const char *key) { static void read_file(const std::string &path, std::string &out) { std::ifstream fs(path, std::ios_base::binary); - if (!fs) throw std::runtime_error("File not found: " + path); + if (!fs) { +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + return; +#else + throw std::runtime_error("File not found: " + path); +#endif + } fs.seekg(0, std::ios_base::end); auto size = fs.tellg(); fs.seekg(0); @@ -3144,16 +3150,20 @@ class ServerTest : public ::testing::Test { [&](const Request &req, Response &res) { ASSERT_FALSE(req.has_header("REMOTE_ADDR")); ASSERT_FALSE(req.has_header("REMOTE_PORT")); +#ifndef CPPHTTPLIB_NO_EXCEPTIONS ASSERT_ANY_THROW(req.get_header_value("REMOTE_ADDR")); ASSERT_ANY_THROW(req.get_header_value("REMOTE_PORT")); +#endif res.set_content(req.remote_addr, "text/plain"); }) .Get("/local_addr", [&](const Request &req, Response &res) { ASSERT_FALSE(req.has_header("LOCAL_ADDR")); ASSERT_FALSE(req.has_header("LOCAL_PORT")); +#ifndef CPPHTTPLIB_NO_EXCEPTIONS ASSERT_ANY_THROW(req.get_header_value("LOCAL_ADDR")); ASSERT_ANY_THROW(req.get_header_value("LOCAL_PORT")); +#endif auto local_addr = req.local_addr; auto local_port = std::to_string(req.local_port); res.set_content(local_addr.append(":").append(local_port), @@ -7054,6 +7064,7 @@ TEST(ServerStopTest, ListenFailure) { t.join(); } +#ifndef CPPHTTPLIB_NO_EXCEPTIONS TEST(ServerStopTest, Decommision) { Server svr; @@ -7099,6 +7110,7 @@ TEST(ServerStopTest, Decommision) { } } } +#endif // Helper function for string body upload progress tests template @@ -10587,7 +10599,11 @@ TEST(TaskQueueTest, IncreaseAtomicInteger) { EXPECT_TRUE(queued); } +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + task_queue->shutdown(); +#else EXPECT_NO_THROW(task_queue->shutdown()); +#endif EXPECT_EQ(number_of_tasks, count.load()); } @@ -10606,7 +10622,11 @@ TEST(TaskQueueTest, IncreaseAtomicIntegerWithQueueLimit) { } } +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + task_queue->shutdown(); +#else EXPECT_NO_THROW(task_queue->shutdown()); +#endif EXPECT_EQ(queued_count, count.load()); EXPECT_TRUE(queued_count <= number_of_tasks); EXPECT_TRUE(queued_count >= qlimit); @@ -10672,7 +10692,11 @@ TEST(TaskQueueTest, MaxQueuedRequests) { EXPECT_TRUE(queued); } +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + task_queue->shutdown(); +#else EXPECT_NO_THROW(task_queue->shutdown()); +#endif } TEST(RedirectTest, RedirectToUrlWithQueryParameters) { @@ -13541,11 +13565,18 @@ class SSEParsingTest : public ::testing::Test { msg.id = value; } else if (field == "retry") { // Parse retry interval in milliseconds +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + { + std::istringstream iss(value); + iss >> retry_ms; + } +#else try { retry_ms = std::stoi(value); } catch (...) { // Invalid retry value, ignore } +#endif } // Unknown fields are ignored per SSE spec From 71bb17fb0e7ab7dcb1fb5a4e97ebaf98025c9672 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 31 Jan 2026 16:40:41 -0500 Subject: [PATCH 0603/1222] Fix problems with CPPHTTPLIB_NO_EXCEPTIONS --- httplib.h | 123 ++++++++++++++++++++++++++++++++++++++------------- test/test.cc | 14 ++---- 2 files changed, 97 insertions(+), 40 deletions(-) diff --git a/httplib.h b/httplib.h index e98d462958..1a64e05a5c 100644 --- a/httplib.h +++ b/httplib.h @@ -305,6 +305,7 @@ using socket_t = int; #include #include #include +#include #include #include #include @@ -322,6 +323,7 @@ using socket_t = int; #include #include #include +#include #include #include #include @@ -511,6 +513,69 @@ struct scope_exit { bool execute_on_destruction; }; +// Simple from_chars implementation for integer and double types (C++17 +// substitute) +template struct from_chars_result { + const char *ptr; + std::errc ec; +}; + +template +inline from_chars_result from_chars(const char *first, const char *last, + T &value, int base = 10) { + value = 0; + const char *p = first; + bool negative = false; + + if (p != last && *p == '-') { + negative = true; + ++p; + } + if (p == last) { return {first, std::errc::invalid_argument}; } + + T result = 0; + for (; p != last; ++p) { + char c = *p; + int digit = -1; + if ('0' <= c && c <= '9') { + digit = c - '0'; + } else if ('a' <= c && c <= 'z') { + digit = c - 'a' + 10; + } else if ('A' <= c && c <= 'Z') { + digit = c - 'A' + 10; + } else { + break; + } + + if (digit < 0 || digit >= base) { break; } + if (result > ((std::numeric_limits::max)() - digit) / base) { + return {p, std::errc::result_out_of_range}; + } + result = result * base + digit; + } + + if (p == first || (negative && p == first + 1)) { + return {first, std::errc::invalid_argument}; + } + + value = negative ? -result : result; + return {p, std::errc{}}; +} + +// from_chars for double (simple wrapper for strtod) +inline from_chars_result from_chars(const char *first, const char *last, + double &value) { + std::string s(first, last); + char *endptr = nullptr; + errno = 0; + value = std::strtod(s.c_str(), &endptr); + if (endptr == s.c_str()) { return {first, std::errc::invalid_argument}; } + if (errno == ERANGE) { + return {first + (endptr - s.c_str()), std::errc::result_out_of_range}; + } + return {first + (endptr - s.c_str()), std::errc{}}; +} + } // namespace detail enum SSLVerifierResponse { @@ -3261,18 +3326,12 @@ class SSEClient { msg.id = value; } else if (field == "retry") { // Parse retry interval in milliseconds -#ifdef CPPHTTPLIB_NO_EXCEPTIONS { - std::istringstream iss(value); - iss >> retry_ms; - } -#else - try { - retry_ms = std::stoi(value); - } catch (...) { - // Invalid retry value, ignore + int v = 0; + auto res = + detail::from_chars(value.data(), value.data() + value.size(), v); + if (res.ec == std::errc{}) { retry_ms = v; } } -#endif } // Unknown fields are ignored per SSE spec @@ -6335,10 +6394,20 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { return; } - const auto first = - static_cast(lhs.empty() ? -1 : std::stoll(lhs)); - const auto last = - static_cast(rhs.empty() ? -1 : std::stoll(rhs)); + ssize_t first = -1; + if (!lhs.empty()) { + ssize_t v; + auto res = detail::from_chars(lhs.data(), lhs.data() + lhs.size(), v); + if (res.ec == std::errc{}) { first = v; } + } + + ssize_t last = -1; + if (!rhs.empty()) { + ssize_t v; + auto res = detail::from_chars(rhs.data(), rhs.data() + rhs.size(), v); + if (res.ec == std::errc{}) { last = v; } + } + if ((first == -1 && last == -1) || (first != -1 && last != -1 && first > last)) { all_valid_ranges = false; @@ -6413,25 +6482,17 @@ inline bool parse_accept_header(const std::string &s, return; } -#ifdef CPPHTTPLIB_NO_EXCEPTIONS { - std::istringstream iss(quality_str); - iss >> accept_entry.quality; - - // Check if conversion was successful and entire string was consumed - if (iss.fail() || !iss.eof()) { + double v = 0.0; + auto res = detail::from_chars( + quality_str.data(), quality_str.data() + quality_str.size(), v); + if (res.ec == std::errc{}) { + accept_entry.quality = v; + } else { has_invalid_entry = true; return; } } -#else - try { - accept_entry.quality = std::stod(quality_str); - } catch (...) { - has_invalid_entry = true; - return; - } -#endif // Check if quality is in valid range [0.0, 1.0] if (accept_entry.quality < 0.0 || accept_entry.quality > 1.0) { has_invalid_entry = true; @@ -13376,8 +13437,10 @@ inline bool SSLClient::check_host_name(const char *pattern, partial_match = true; } else if (h.size() >= prefix_length) { partial_match = - std::equal(p.begin(), p.begin() + prefix_length, h.begin(), - [](const char ca, const char cb) { + std::equal(p.begin(), + p.begin() + static_cast( + prefix_length), + h.begin(), [](const char ca, const char cb) { return httplib::detail::case_ignore::to_lower(ca) == httplib::detail::case_ignore::to_lower(cb); }); diff --git a/test/test.cc b/test/test.cc index d03df186f4..21f48698a4 100644 --- a/test/test.cc +++ b/test/test.cc @@ -13565,18 +13565,12 @@ class SSEParsingTest : public ::testing::Test { msg.id = value; } else if (field == "retry") { // Parse retry interval in milliseconds -#ifdef CPPHTTPLIB_NO_EXCEPTIONS { - std::istringstream iss(value); - iss >> retry_ms; + int v = 0; + auto res = + detail::from_chars(value.data(), value.data() + value.size(), v); + if (res.ec == std::errc{}) { retry_ms = v; } } -#else - try { - retry_ms = std::stoi(value); - } catch (...) { - // Invalid retry value, ignore - } -#endif } // Unknown fields are ignored per SSE spec From dc6faf5c17c1e3c30b5d59e31ae2e890a35e64e1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 1 Feb 2026 19:11:44 -0500 Subject: [PATCH 0604/1222] Release v0.30.2 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 1a64e05a5c..4d798e57bd 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.30.1" -#define CPPHTTPLIB_VERSION_NUM "0x001E01" +#define CPPHTTPLIB_VERSION "0.30.2" +#define CPPHTTPLIB_VERSION_NUM "0x001E02" /* * Platform compatibility check From 6be32a540dca60dd3910239e82488e114f9be0a8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 1 Feb 2026 23:48:03 -0500 Subject: [PATCH 0605/1222] Abstract TLS API support (Resolve #2309) (#2342) Abstract TLS API support (OpenSSL and MbedTLS backends) --- .github/workflows/test.yaml | 43 +- .github/workflows/test_proxy.yaml | 31 +- .gitignore | 5 + README.md | 21 +- example/client.cc | 2 +- httplib.h | 6763 ++++++++++++++++++++--------- justfile | 21 + test/Makefile | 78 +- test/gen-certs.sh | 2 +- test/test.cc | 1363 ++++-- test/test_proxy.cc | 18 +- 11 files changed, 5915 insertions(+), 2432 deletions(-) create mode 100644 justfile diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 36e3610520..418cac5144 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -75,21 +75,32 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') + strategy: + matrix: + tls_backend: [openssl, mbedtls] + name: ubuntu (${{ matrix.tls_backend }}) steps: - name: checkout uses: actions/checkout@v4 - - name: install libraries + - name: install common libraries run: | - sudo dpkg --add-architecture i386 sudo apt-get update - sudo apt-get install -y libc6-dev${{ matrix.config.arch_suffix }} libstdc++-13-dev${{ matrix.config.arch_suffix }} \ - libssl-dev${{ matrix.config.arch_suffix }} libcurl4-openssl-dev${{ matrix.config.arch_suffix }} \ - zlib1g-dev${{ matrix.config.arch_suffix }} libbrotli-dev${{ matrix.config.arch_suffix }} \ - libzstd-dev${{ matrix.config.arch_suffix }} - - name: build and run tests - run: cd test && make EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}" + sudo apt-get install -y libcurl4-openssl-dev zlib1g-dev libbrotli-dev libzstd-dev + - name: install OpenSSL + if: matrix.tls_backend == 'openssl' + run: sudo apt-get install -y libssl-dev + - name: install Mbed TLS + if: matrix.tls_backend == 'mbedtls' + run: sudo apt-get install -y libmbedtls-dev + - name: build and run tests (OpenSSL) + if: matrix.tls_backend == 'openssl' + run: cd test && make + - name: build and run tests (Mbed TLS) + if: matrix.tls_backend == 'mbedtls' + run: cd test && make test_split_mbedtls && make test_mbedtls && ./test_mbedtls - name: run fuzz test target - run: cd test && make EXTRA_CXXFLAGS="${{ matrix.config.arch_flags }}" fuzz_test + if: matrix.tls_backend == 'openssl' + run: cd test && make fuzz_test macos: runs-on: macos-latest @@ -98,12 +109,24 @@ jobs: (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || (github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true') + strategy: + matrix: + tls_backend: [openssl, mbedtls] + name: macos (${{ matrix.tls_backend }}) steps: - name: checkout uses: actions/checkout@v4 - - name: build and run tests + - name: install Mbed TLS + if: matrix.tls_backend == 'mbedtls' + run: brew install mbedtls@3 + - name: build and run tests (OpenSSL) + if: matrix.tls_backend == 'openssl' run: cd test && make + - name: build and run tests (Mbed TLS) + if: matrix.tls_backend == 'mbedtls' + run: cd test && make test_split_mbedtls && make test_mbedtls && ./test_mbedtls - name: run fuzz test target + if: matrix.tls_backend == 'openssl' run: cd test && make fuzz_test windows: diff --git a/.github/workflows/test_proxy.yaml b/.github/workflows/test_proxy.yaml index 571dc96ac6..f21b148998 100644 --- a/.github/workflows/test_proxy.yaml +++ b/.github/workflows/test_proxy.yaml @@ -6,15 +6,28 @@ jobs: test-proxy: runs-on: ubuntu-latest if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name - + strategy: + matrix: + tls_backend: [openssl, mbedtls] + name: proxy (${{ matrix.tls_backend }}) + steps: - - uses: actions/checkout@v3 - - - name: Install dependencies + - uses: actions/checkout@v4 + + - name: Install common dependencies run: | sudo apt-get update - sudo apt-get install -y build-essential libssl-dev zlib1g-dev libcurl4-openssl-dev libbrotli-dev libzstd-dev netcat-openbsd - - - name: Run proxy tests - run: | - cd test && make proxy \ No newline at end of file + sudo apt-get install -y build-essential zlib1g-dev libcurl4-openssl-dev libbrotli-dev libzstd-dev netcat-openbsd + - name: Install OpenSSL + if: matrix.tls_backend == 'openssl' + run: sudo apt-get install -y libssl-dev + - name: Install Mbed TLS + if: matrix.tls_backend == 'mbedtls' + run: sudo apt-get install -y libmbedtls-dev + + - name: Run proxy tests (OpenSSL) + if: matrix.tls_backend == 'openssl' + run: cd test && make proxy + - name: Run proxy tests (Mbed TLS) + if: matrix.tls_backend == 'mbedtls' + run: cd test && make proxy_mbedtls \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9e2bf24566..80278dc2d0 100644 --- a/.gitignore +++ b/.gitignore @@ -33,9 +33,14 @@ example/*.pem test/httplib.cc test/httplib.h test/test +test/test_mbedtls +test/test_no_tls test/server_fuzzer test/test_proxy +test/test_proxy_mbedtls test/test_split +test/test_split_mbedtls +test/test_split_no_tls test/test.xcodeproj/xcuser* test/test.xcodeproj/*/xcuser* test/*.o diff --git a/README.md b/README.md index aa94afe0d9..49c97297c0 100644 --- a/README.md +++ b/README.md @@ -50,19 +50,28 @@ if (auto res = cli.Get("/hi")) { } ``` -SSL Support ------------ +SSL/TLS Support +--------------- + +cpp-httplib supports multiple TLS backends through an abstraction layer: -SSL support is available with `CPPHTTPLIB_OPENSSL_SUPPORT`. `libssl` and `libcrypto` should be linked. +| Backend | Define | Libraries | +| :------ | :----- | :-------- | +| OpenSSL | `CPPHTTPLIB_OPENSSL_SUPPORT` | `libssl`, `libcrypto` | +| Mbed TLS | `CPPHTTPLIB_MBEDTLS_SUPPORT` | `libmbedtls`, `libmbedx509`, `libmbedcrypto` | > [!NOTE] -> cpp-httplib currently supports only version 3.0 or later. Please see [this page](https://www.openssl.org/policies/releasestrat.html) to get more information. +> OpenSSL 3.0 or later is required. Please see [this page](https://www.openssl.org/policies/releasestrat.html) for more information. + +> [!NOTE] +> Mbed TLS 2.x and 3.x are supported. The library automatically detects the version and uses the appropriate API. > [!TIP] -> For macOS: cpp-httplib now can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`. +> For macOS: cpp-httplib can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`. ```c++ -#define CPPHTTPLIB_OPENSSL_SUPPORT +// Use either OpenSSL or Mbed TLS +#define CPPHTTPLIB_OPENSSL_SUPPORT // or CPPHTTPLIB_MBEDTLS_SUPPORT #include "path/to/httplib.h" // Server diff --git a/example/client.cc b/example/client.cc index b89bb4c580..8bfbd335b9 100644 --- a/example/client.cc +++ b/example/client.cc @@ -30,7 +30,7 @@ int main(void) { } else { cout << "error code: " << res.error() << std::endl; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - auto result = cli.get_openssl_verify_result(); + auto result = cli.get_verify_result(); if (result) { cout << "verify error: " << X509_verify_cert_error_string(result) << endl; } diff --git a/httplib.h b/httplib.h index 4d798e57bd..571f3d5af8 100644 --- a/httplib.h +++ b/httplib.h @@ -383,6 +383,45 @@ using socket_t = int; #endif // CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#ifdef _MSC_VER +#pragma comment(lib, "crypt32.lib") +#endif +#endif // _WIN32 +#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) +#if TARGET_OS_MAC +#include +#endif +#endif // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN + +// Mbed TLS 3.x API compatibility +#if MBEDTLS_VERSION_MAJOR >= 3 +#define CPPHTTPLIB_MBEDTLS_V3 +#endif + +#endif // CPPHTTPLIB_MBEDTLS_SUPPORT + +// Define CPPHTTPLIB_SSL_ENABLED if any SSL backend is available +// This simplifies conditional compilation when adding new backends (e.g., +// wolfSSL) +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) || defined(CPPHTTPLIB_MBEDTLS_SUPPORT) +#define CPPHTTPLIB_SSL_ENABLED +#endif + #ifdef CPPHTTPLIB_ZLIB_SUPPORT #include #endif @@ -799,6 +838,105 @@ class ContentReader { using Range = std::pair; using Ranges = std::vector; +#ifdef CPPHTTPLIB_SSL_ENABLED +// TLS abstraction layer - public type definitions and API +namespace tls { + +// Opaque handles (defined as void* for abstraction) +using ctx_t = void *; +using session_t = void *; +using const_session_t = const void *; // For read-only session access +using cert_t = void *; +using ca_store_t = void *; + +// TLS versions +enum class Version { + TLS1_2 = 0x0303, + TLS1_3 = 0x0304, +}; + +// Subject Alternative Names (SAN) entry types +enum class SanType { DNS, IP, EMAIL, URI, OTHER }; + +// SAN entry structure +struct SanEntry { + SanType type; + std::string value; +}; + +// Verification context for certificate verification callback +struct VerifyContext { + session_t session; // TLS session handle + cert_t cert; // Current certificate being verified + int depth; // Certificate chain depth (0 = leaf) + bool preverify_ok; // OpenSSL/Mbed TLS pre-verification result + long error_code; // Backend-specific error code (0 = no error) + const char *error_string; // Human-readable error description + + // Certificate introspection methods + std::string subject_cn() const; + std::string issuer_name() const; + bool check_hostname(const char *hostname) const; + std::vector sans() const; + bool validity(time_t ¬_before, time_t ¬_after) const; + std::string serial() const; +}; + +using VerifyCallback = std::function; + +// TlsError codes for TLS operations (backend-independent) +enum class ErrorCode : int { + Success = 0, + WantRead, // Non-blocking: need to wait for read + WantWrite, // Non-blocking: need to wait for write + PeerClosed, // Peer closed the connection + Fatal, // Unrecoverable error + SyscallError, // System call error (check sys_errno) + CertVerifyFailed, // Certificate verification failed + HostnameMismatch, // Hostname verification failed +}; + +// TLS error information +struct TlsError { + ErrorCode code = ErrorCode::Fatal; + uint64_t backend_code = 0; // OpenSSL: ERR_get_error(), mbedTLS: return value + int sys_errno = 0; // errno when SyscallError + + // Convert verification error code to human-readable string + static std::string verify_error_to_string(long error_code); +}; + +// RAII wrapper for peer certificate +class PeerCert { +public: + PeerCert(); + PeerCert(PeerCert &&other) noexcept; + PeerCert &operator=(PeerCert &&other) noexcept; + ~PeerCert(); + + PeerCert(const PeerCert &) = delete; + PeerCert &operator=(const PeerCert &) = delete; + + explicit operator bool() const; + std::string subject_cn() const; + std::string issuer_name() const; + bool check_hostname(const char *hostname) const; + std::vector sans() const; + bool validity(time_t ¬_before, time_t ¬_after) const; + std::string serial() const; + +private: + explicit PeerCert(cert_t cert); + cert_t cert_ = nullptr; + friend PeerCert get_peer_cert_from_session(const_session_t session); +}; + +// Callback for TLS context setup (used by SSLServer constructor) +using ContextSetupCallback = std::function; + +} // namespace tls +#endif + struct Request { std::string method; std::string path; @@ -828,9 +966,6 @@ struct Request { ContentReceiverWithProgress content_receiver; DownloadProgress download_progress; UploadProgress upload_progress; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - const SSL *ssl = nullptr; -#endif bool has_header(const std::string &key) const; std::string get_header_value(const std::string &key, const char *def = "", @@ -858,6 +993,12 @@ struct Request { size_t authorization_count_ = 0; std::chrono::time_point start_time_ = (std::chrono::steady_clock::time_point::min)(); + +#ifdef CPPHTTPLIB_SSL_ENABLED + tls::const_session_t ssl = nullptr; + tls::PeerCert peer_cert() const; + std::string sni() const; +#endif }; struct Response { @@ -1005,74 +1146,18 @@ class TaskQueue { class ThreadPool final : public TaskQueue { public: - explicit ThreadPool(size_t n, size_t mqr = 0) - : shutdown_(false), max_queued_requests_(mqr) { - threads_.reserve(n); - while (n) { - threads_.emplace_back(worker(*this)); - n--; - } - } - + explicit ThreadPool(size_t n, size_t mqr = 0); ThreadPool(const ThreadPool &) = delete; ~ThreadPool() override = default; - bool enqueue(std::function fn) override { - { - std::unique_lock lock(mutex_); - if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { - return false; - } - jobs_.push_back(std::move(fn)); - } - - cond_.notify_one(); - return true; - } - - void shutdown() override { - // Stop all worker threads... - { - std::unique_lock lock(mutex_); - shutdown_ = true; - } - - cond_.notify_all(); - - // Join... - for (auto &t : threads_) { - t.join(); - } - } + bool enqueue(std::function fn) override; + void shutdown() override; private: struct worker { - explicit worker(ThreadPool &pool) : pool_(pool) {} - - void operator()() { - for (;;) { - std::function fn; - { - std::unique_lock lock(pool_.mutex_); - - pool_.cond_.wait( - lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); - - if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } - - fn = pool_.jobs_.front(); - pool_.jobs_.pop_front(); - } + explicit worker(ThreadPool &pool); - assert(true == static_cast(fn)); - fn(); - } - -#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ - !defined(LIBRESSL_VERSION_NUMBER) - OPENSSL_thread_stop(); -#endif - } + void operator()(); ThreadPool &pool_; }; @@ -1184,6 +1269,9 @@ int close_socket(socket_t sock); ssize_t write_headers(Stream &strm, const Headers &headers); +bool set_socket_opt_time(socket_t sock, int level, int optname, time_t sec, + time_t usec); + } // namespace detail class Server { @@ -1429,17 +1517,6 @@ class Result { Headers &&request_headers = Headers{}) : res_(std::move(res)), err_(err), request_headers_(std::move(request_headers)) {} -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - Result(std::unique_ptr &&res, Error err, Headers &&request_headers, - int ssl_error) - : res_(std::move(res)), err_(err), - request_headers_(std::move(request_headers)), ssl_error_(ssl_error) {} - Result(std::unique_ptr &&res, Error err, Headers &&request_headers, - int ssl_error, unsigned long ssl_openssl_error) - : res_(std::move(res)), err_(err), - request_headers_(std::move(request_headers)), ssl_error_(ssl_error), - ssl_openssl_error_(ssl_openssl_error) {} -#endif // Response operator bool() const { return res_ != nullptr; } bool operator==(std::nullptr_t) const { return res_ == nullptr; } @@ -1454,13 +1531,6 @@ class Result { // Error Error error() const { return err_; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - // SSL Error - int ssl_error() const { return ssl_error_; } - // OpenSSL Error - unsigned long ssl_openssl_error() const { return ssl_openssl_error_; } -#endif - // Request Headers bool has_request_header(const std::string &key) const; std::string get_request_header_value(const std::string &key, @@ -1474,64 +1544,76 @@ class Result { std::unique_ptr res_; Error err_ = Error::Unknown; Headers request_headers_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + +#ifdef CPPHTTPLIB_SSL_ENABLED +public: + Result(std::unique_ptr &&res, Error err, Headers &&request_headers, + int ssl_error) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)), ssl_error_(ssl_error) {} + Result(std::unique_ptr &&res, Error err, Headers &&request_headers, + int ssl_error, unsigned long ssl_backend_error) + : res_(std::move(res)), err_(err), + request_headers_(std::move(request_headers)), ssl_error_(ssl_error), + ssl_backend_error_(ssl_backend_error) {} + + int ssl_error() const { return ssl_error_; } + unsigned long ssl_backend_error() const { return ssl_backend_error_; } + +private: int ssl_error_ = 0; - unsigned long ssl_openssl_error_ = 0; + unsigned long ssl_backend_error_ = 0; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +public: + [[deprecated("Use ssl_backend_error() instead")]] + unsigned long ssl_openssl_error() const { + return ssl_backend_error_; + } #endif }; struct ClientConnection { socket_t sock = INVALID_SOCKET; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSL *ssl = nullptr; -#endif bool is_open() const { return sock != INVALID_SOCKET; } ClientConnection() = default; - ~ClientConnection() { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (ssl) { - SSL_free(ssl); - ssl = nullptr; - } -#endif - if (sock != INVALID_SOCKET) { - detail::close_socket(sock); - sock = INVALID_SOCKET; - } - } + ~ClientConnection(); ClientConnection(const ClientConnection &) = delete; ClientConnection &operator=(const ClientConnection &) = delete; ClientConnection(ClientConnection &&other) noexcept : sock(other.sock) -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED , - ssl(other.ssl) + session(other.session) #endif { other.sock = INVALID_SOCKET; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - other.ssl = nullptr; +#ifdef CPPHTTPLIB_SSL_ENABLED + other.session = nullptr; #endif } ClientConnection &operator=(ClientConnection &&other) noexcept { if (this != &other) { sock = other.sock; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - ssl = other.ssl; -#endif other.sock = INVALID_SOCKET; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - other.ssl = nullptr; +#ifdef CPPHTTPLIB_SSL_ENABLED + session = other.session; + other.session = nullptr; #endif } return *this; } + +#ifdef CPPHTTPLIB_SSL_ENABLED + tls::session_t session = nullptr; +#endif }; namespace detail { @@ -1756,10 +1838,6 @@ class ClientImpl { void set_basic_auth(const std::string &username, const std::string &password); void set_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); -#endif void set_keep_alive(bool on); void set_follow_location(bool on); @@ -1776,24 +1854,6 @@ class ClientImpl { void set_proxy_basic_auth(const std::string &username, const std::string &password); void set_proxy_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); - void set_ca_cert_store(X509_STORE *ca_cert_store); - X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void enable_server_certificate_verification(bool enabled); - void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier( - std::function verifier); -#endif void set_logger(Logger logger); void set_error_logger(ErrorLogger error_logger); @@ -1801,11 +1861,15 @@ class ClientImpl { protected: struct Socket { socket_t sock = INVALID_SOCKET; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSL *ssl = nullptr; -#endif + + // For Mbed TLS compatibility: start_time for request timeout tracking + std::chrono::time_point start_time_; bool is_open() const { return sock != INVALID_SOCKET; } + +#ifdef CPPHTTPLIB_SSL_ENABLED + tls::session_t ssl = nullptr; +#endif }; virtual bool create_and_connect_socket(Socket &socket, Error &error); @@ -1872,10 +1936,6 @@ class ClientImpl { std::string basic_auth_username_; std::string basic_auth_password_; std::string bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string digest_auth_username_; - std::string digest_auth_password_; -#endif bool keep_alive_ = false; bool follow_location_ = false; @@ -1898,33 +1958,11 @@ class ClientImpl { std::string proxy_basic_auth_username_; std::string proxy_basic_auth_password_; std::string proxy_bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string proxy_digest_auth_username_; - std::string proxy_digest_auth_password_; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - std::string ca_cert_file_path_; - std::string ca_cert_dir_path_; - - X509_STORE *ca_cert_store_ = nullptr; -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool server_certificate_verification_ = true; - bool server_hostname_verification_ = true; - std::function server_certificate_verifier_; -#endif mutable std::mutex logger_mutex_; Logger logger_; ErrorLogger error_logger_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - int last_ssl_error_ = 0; - unsigned long last_openssl_error_ = 0; -#endif - private: bool send_(Request &req, Response &res, Error &error); Result send_(Request &&req); @@ -1969,6 +2007,44 @@ class ClientImpl { virtual bool is_ssl() const; void transfer_socket_ownership_to_handle(StreamHandle &handle); + +#ifdef CPPHTTPLIB_SSL_ENABLED +public: + void set_digest_auth(const std::string &username, + const std::string &password); + void set_proxy_digest_auth(const std::string &username, + const std::string &password); + void set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path = std::string()); + void enable_server_certificate_verification(bool enabled); + void enable_server_hostname_verification(bool enabled); + +protected: + std::string digest_auth_username_; + std::string digest_auth_password_; + std::string proxy_digest_auth_username_; + std::string proxy_digest_auth_password_; + std::string ca_cert_file_path_; + std::string ca_cert_dir_path_; + bool server_certificate_verification_ = true; + bool server_hostname_verification_ = true; + std::string ca_cert_pem_; // Store CA cert PEM for redirect transfer + int last_ssl_error_ = 0; + unsigned long last_backend_error_ = 0; +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +public: + [[deprecated("Use load_ca_cert_store() instead")]] + void set_ca_cert_store(X509_STORE *ca_cert_store); + + [[deprecated("Use tls::create_ca_store() instead")]] + X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const; + + [[deprecated("Use set_server_certificate_verifier(VerifyCallback) instead")]] + virtual void set_server_certificate_verifier( + std::function verifier); +#endif }; class Client { @@ -2138,10 +2214,6 @@ class Client { void set_basic_auth(const std::string &username, const std::string &password); void set_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); -#endif void set_keep_alive(bool on); void set_follow_location(bool on); @@ -2159,43 +2231,52 @@ class Client { void set_proxy_basic_auth(const std::string &username, const std::string &password); void set_proxy_bearer_token_auth(const std::string &token); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_logger(Logger logger); + void set_error_logger(ErrorLogger error_logger); + +private: + std::unique_ptr cli_; + +#ifdef CPPHTTPLIB_SSL_ENABLED +public: + void set_digest_auth(const std::string &username, + const std::string &password); void set_proxy_digest_auth(const std::string &username, const std::string &password); -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT void enable_server_certificate_verification(bool enabled); void enable_server_hostname_verification(bool enabled); - void set_server_certificate_verifier( - std::function verifier); -#endif - - void set_logger(Logger logger); - void set_error_logger(ErrorLogger error_logger); - - // SSL -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT void set_ca_cert_path(const std::string &ca_cert_file_path, const std::string &ca_cert_dir_path = std::string()); - void set_ca_cert_store(X509_STORE *ca_cert_store); + void set_ca_cert_store(tls::ca_store_t ca_cert_store); void load_ca_cert_store(const char *ca_cert, std::size_t size); - long get_openssl_verify_result() const; + void set_server_certificate_verifier(tls::VerifyCallback verifier); - SSL_CTX *ssl_context() const; -#endif + void set_session_verifier( + std::function verifier); + + tls::ctx_t tls_context() const; private: - std::unique_ptr cli_; + bool is_ssl_ = false; +#endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool is_ssl_ = false; +public: + [[deprecated("Use tls_context() instead")]] + SSL_CTX *ssl_context() const; + + [[deprecated("Use set_session_verifier(session_t) instead")]] + void set_server_certificate_verifier( + std::function verifier); + + [[deprecated("Use Result::ssl_backend_error() instead")]] + long get_verify_result() const; #endif }; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED class SSLServer : public Server { public: SSLServer(const char *cert_path, const char *private_key_path, @@ -2203,59 +2284,96 @@ class SSLServer : public Server { const char *client_ca_cert_dir_path = nullptr, const char *private_key_password = nullptr); - SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); + struct PemMemory { + const char *cert_pem; + size_t cert_pem_len; + const char *key_pem; + size_t key_pem_len; + const char *client_ca_pem; + size_t client_ca_pem_len; + const char *private_key_password; + }; + explicit SSLServer(const PemMemory &pem); - SSLServer( - const std::function &setup_ssl_ctx_callback); + // The callback receives the ctx_t handle which can be cast to the + // appropriate backend type (SSL_CTX* for OpenSSL, + // tls::impl::MbedTlsContext* for Mbed TLS) + explicit SSLServer(const tls::ContextSetupCallback &setup_callback); ~SSLServer() override; bool is_valid() const override; - SSL_CTX *ssl_context() const; + bool update_certs_pem(const char *cert_pem, const char *key_pem, + const char *client_ca_pem = nullptr, + const char *password = nullptr); - void update_certs(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); + tls::ctx_t tls_context() const { return ctx_; } int ssl_last_error() const { return last_ssl_error_; } private: bool process_and_close_socket(socket_t sock) override; - STACK_OF(X509_NAME) * extract_ca_names_from_x509_store(X509_STORE *store); - - SSL_CTX *ctx_; + tls::ctx_t ctx_ = nullptr; std::mutex ctx_mutex_; int last_ssl_error_ = 0; -}; -class SSLClient final : public ClientImpl { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT public: - explicit SSLClient(const std::string &host); - - explicit SSLClient(const std::string &host, int port); + [[deprecated("Use SSLServer(PemMemory) or " + "SSLServer(ContextSetupCallback) instead")]] + SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); + + [[deprecated("Use SSLServer(ContextSetupCallback) instead")]] + SSLServer( + const std::function &setup_ssl_ctx_callback); + + [[deprecated("Use tls_context() instead")]] + SSL_CTX *ssl_context() const; + + [[deprecated("Use update_certs_pem() instead")]] + void update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); +#endif +}; + +class SSLClient final : public ClientImpl { +public: + explicit SSLClient(const std::string &host); + + explicit SSLClient(const std::string &host, int port); explicit SSLClient(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path, const std::string &private_key_password = std::string()); - explicit SSLClient(const std::string &host, int port, X509 *client_cert, - EVP_PKEY *client_key, - const std::string &private_key_password = std::string()); + struct PemMemory { + const char *cert_pem; + size_t cert_pem_len; + const char *key_pem; + size_t key_pem_len; + const char *private_key_password; + }; + explicit SSLClient(const std::string &host, int port, const PemMemory &pem); ~SSLClient() override; bool is_valid() const override; - void set_ca_cert_store(X509_STORE *ca_cert_store); + void set_ca_cert_store(tls::ca_store_t ca_cert_store); void load_ca_cert_store(const char *ca_cert, std::size_t size); - long get_openssl_verify_result() const; + void set_server_certificate_verifier(tls::VerifyCallback verifier); - SSL_CTX *ssl_context() const; + // Post-handshake session verifier (backend-independent) + void set_session_verifier( + std::function verifier); + + tls::ctx_t tls_context() const { return ctx_; } private: bool create_and_connect_socket(Socket &socket, Error &error) override; @@ -2277,26 +2395,40 @@ class SSLClient final : public ClientImpl { bool load_certs(); - bool verify_host(X509 *server_cert) const; - bool verify_host_with_subject_alt_name(X509 *server_cert) const; - bool verify_host_with_common_name(X509 *server_cert) const; - bool check_host_name(const char *pattern, size_t pattern_len) const; - - SSL_CTX *ctx_; + tls::ctx_t ctx_ = nullptr; std::mutex ctx_mutex_; std::once_flag initialize_cert_; - std::vector host_components_; - long verify_result_ = 0; + std::function session_verifier_; + friend class ClientImpl; -}; -#endif -/* - * Implementation of template methods. - */ +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +public: + [[deprecated("Use SSLClient(host, port, PemMemory) instead")]] + explicit SSLClient(const std::string &host, int port, X509 *client_cert, + EVP_PKEY *client_key, + const std::string &private_key_password = std::string()); + + [[deprecated("Use Result::ssl_backend_error() instead")]] + long get_verify_result() const; + + [[deprecated("Use tls_context() instead")]] + SSL_CTX *ssl_context() const; + + [[deprecated("Use set_session_verifier(session_t) instead")]] + void set_server_certificate_verifier( + std::function verifier) override; + +private: + bool verify_host(X509 *server_cert) const; + bool verify_host_with_subject_alt_name(X509 *server_cert) const; + bool verify_host_with_common_name(X509 *server_cert) const; +#endif +}; +#endif // CPPHTTPLIB_SSL_ENABLED namespace detail { @@ -2345,66 +2477,6 @@ inline size_t get_header_value_u64(const Headers &headers, } // namespace detail -inline size_t Request::get_header_value_u64(const std::string &key, size_t def, - size_t id) const { - return detail::get_header_value_u64(headers, key, def, id); -} - -inline size_t Response::get_header_value_u64(const std::string &key, size_t def, - size_t id) const { - return detail::get_header_value_u64(headers, key, def, id); -} - -namespace detail { - -inline bool set_socket_opt_impl(socket_t sock, int level, int optname, - const void *optval, socklen_t optlen) { - return setsockopt(sock, level, optname, -#ifdef _WIN32 - reinterpret_cast(optval), -#else - optval, -#endif - optlen) == 0; -} - -inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) { - return set_socket_opt_impl(sock, level, optname, &optval, sizeof(optval)); -} - -inline bool set_socket_opt_time(socket_t sock, int level, int optname, - time_t sec, time_t usec) { -#ifdef _WIN32 - auto timeout = static_cast(sec * 1000 + usec / 1000); -#else - timeval timeout; - timeout.tv_sec = static_cast(sec); - timeout.tv_usec = static_cast(usec); -#endif - return set_socket_opt_impl(sock, level, optname, &timeout, sizeof(timeout)); -} - -} // namespace detail - -inline void default_socket_options(socket_t sock) { - detail::set_socket_opt(sock, SOL_SOCKET, -#ifdef SO_REUSEPORT - SO_REUSEPORT, -#else - SO_REUSEADDR, -#endif - 1); -} - -inline std::string get_bearer_token_auth(const Request &req) { - if (req.has_header("Authorization")) { - constexpr auto bearer_header_prefix_len = detail::str_len("Bearer "); - return req.get_header_value("Authorization") - .substr(bearer_header_prefix_len); - } - return ""; -} - template inline Server & Server::set_read_timeout(const std::chrono::duration &duration) { @@ -2429,12 +2501,6 @@ Server::set_idle_interval(const std::chrono::duration &duration) { return *this; } -inline size_t Result::get_request_header_value_u64(const std::string &key, - size_t def, - size_t id) const { - return detail::get_header_value_u64(request_headers_, key, def, id); -} - template inline void ClientImpl::set_connection_timeout( const std::chrono::duration &duration) { @@ -2842,9 +2908,45 @@ bool is_field_content(const std::string &s); bool is_field_value(const std::string &s); } // namespace fields - } // namespace detail +/* + * TLS Abstraction Layer Declarations + */ + +#ifdef CPPHTTPLIB_SSL_ENABLED +// TLS abstraction layer - backend-specific type declarations +#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT +namespace tls { +namespace impl { + +// Mbed TLS context wrapper (holds config, entropy, DRBG, CA chain, own +// cert/key). This struct is accessible via tls::impl for use in SSL context +// setup callbacks (cast ctx_t to tls::impl::MbedTlsContext*). +struct MbedTlsContext { + mbedtls_ssl_config conf; + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_x509_crt ca_chain; + mbedtls_x509_crt own_cert; + mbedtls_pk_context own_key; + bool is_server = false; + bool verify_client = false; + bool has_verify_callback = false; + + MbedTlsContext(); + ~MbedTlsContext(); + + MbedTlsContext(const MbedTlsContext &) = delete; + MbedTlsContext &operator=(const MbedTlsContext &) = delete; +}; + +} // namespace impl +} // namespace tls +#endif + +#endif // CPPHTTPLIB_SSL_ENABLED + namespace stream { class Result { @@ -3502,8 +3604,133 @@ class SSEClient { * Implementation that will be part of the .cc file if split into .h + .cc. */ +#ifdef CPPHTTPLIB_SSL_ENABLED +/* + * TLS abstraction layer - internal function declarations + * These are implementation details and not part of the public API. + */ +namespace tls { + +// Client context +ctx_t create_client_context(); +void free_context(ctx_t ctx); +bool set_min_version(ctx_t ctx, Version version); +bool load_ca_pem(ctx_t ctx, const char *pem, size_t len); +bool load_ca_file(ctx_t ctx, const char *file_path); +bool load_ca_dir(ctx_t ctx, const char *dir_path); +bool load_system_certs(ctx_t ctx); +bool set_client_cert_pem(ctx_t ctx, const char *cert, const char *key, + const char *password); +bool set_client_cert_file(ctx_t ctx, const char *cert_path, + const char *key_path, const char *password); + +// Server context +ctx_t create_server_context(); +bool set_server_cert_pem(ctx_t ctx, const char *cert, const char *key, + const char *password); +bool set_server_cert_file(ctx_t ctx, const char *cert_path, + const char *key_path, const char *password); +bool set_client_ca_file(ctx_t ctx, const char *ca_file, const char *ca_dir); +void set_verify_client(ctx_t ctx, bool require); + +// Session management +session_t create_session(ctx_t ctx, socket_t sock); +void free_session(session_t session); +bool set_sni(session_t session, const char *hostname); +bool set_hostname(session_t session, const char *hostname); + +// Handshake (non-blocking capable) +TlsError connect(session_t session); +TlsError accept(session_t session); + +// Handshake with timeout (blocking until timeout) +bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec, + time_t timeout_usec, TlsError *err); +bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec, + time_t timeout_usec, TlsError *err); + +// I/O (non-blocking capable) +ssize_t read(session_t session, void *buf, size_t len, TlsError &err); +ssize_t write(session_t session, const void *buf, size_t len, TlsError &err); +int pending(const_session_t session); +void shutdown(session_t session, bool graceful); + +// Connection state +bool is_peer_closed(session_t session, socket_t sock); + +// Certificate verification +cert_t get_peer_cert(const_session_t session); +void free_cert(cert_t cert); +bool verify_hostname(cert_t cert, const char *hostname); +uint64_t hostname_mismatch_code(); +long get_verify_result(const_session_t session); + +// Certificate introspection +std::string get_cert_subject_cn(cert_t cert); +std::string get_cert_issuer_name(cert_t cert); +bool get_cert_sans(cert_t cert, std::vector &sans); +bool get_cert_validity(cert_t cert, time_t ¬_before, time_t ¬_after); +std::string get_cert_serial(cert_t cert); +const char *get_sni(const_session_t session); + +// CA store management +ca_store_t create_ca_store(const char *pem, size_t len); +void free_ca_store(ca_store_t store); +bool set_ca_store(ctx_t ctx, ca_store_t store); +size_t get_ca_certs(ctx_t ctx, std::vector &certs); +std::vector get_ca_names(ctx_t ctx); + +// Dynamic certificate update (for servers) +bool update_server_cert(ctx_t ctx, const char *cert_pem, const char *key_pem, + const char *password); +bool update_server_client_ca(ctx_t ctx, const char *ca_pem); + +// Certificate verification callback +bool set_verify_callback(ctx_t ctx, VerifyCallback callback); +long get_verify_error(const_session_t session); +std::string verify_error_string(long error_code); + +// TlsError information +uint64_t peek_error(); +uint64_t get_error(); +std::string error_string(uint64_t code); + +} // namespace tls +#endif // CPPHTTPLIB_SSL_ENABLED + +/* + * Group 1: detail namespace - Non-SSL utilities + */ + namespace detail { +inline bool set_socket_opt_impl(socket_t sock, int level, int optname, + const void *optval, socklen_t optlen) { + return setsockopt(sock, level, optname, +#ifdef _WIN32 + reinterpret_cast(optval), +#else + optval, +#endif + optlen) == 0; +} + +inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) { + return set_socket_opt_impl(sock, level, optname, &optval, sizeof(optval)); +} + +inline bool set_socket_opt_time(socket_t sock, int level, int optname, + time_t sec, time_t usec) { +#ifdef _WIN32 + auto timeout = static_cast(sec * 1000 + usec / 1000); +#else + timeval timeout; + timeout.tv_sec = static_cast(sec); + timeout.tv_usec = static_cast(usec); +#endif + return set_socket_opt_impl(sock, level, optname, &timeout, sizeof(timeout)); +} + inline bool is_hex(char c, int &v) { if (isdigit(c)) { v = c - '0'; @@ -4436,39 +4663,6 @@ class SocketStream final : public Stream { static const size_t read_buff_size_ = 1024l * 4; }; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -class SSLSocketStream final : public Stream { -public: - SSLSocketStream( - socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, time_t max_timeout_msec = 0, - std::chrono::time_point start_time = - (std::chrono::steady_clock::time_point::min)()); - ~SSLSocketStream() override; - - bool is_readable() const override; - bool wait_readable() const override; - bool wait_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; - void get_local_ip_and_port(std::string &ip, int &port) const override; - socket_t socket() const override; - time_t duration() const override; - -private: - socket_t sock_; - SSL *ssl_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - time_t write_timeout_sec_; - time_t write_timeout_usec_; - time_t max_timeout_msec_; - const std::chrono::time_point start_time_; -}; -#endif - inline bool keep_alive(const std::atomic &svr_sock, socket_t sock, time_t keep_alive_timeout_sec) { using namespace std::chrono; @@ -7141,38 +7335,280 @@ inline bool has_crlf(const std::string &s) { return false; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline std::string message_digest(const std::string &s, const EVP_MD *algo) { - auto context = std::unique_ptr( - EVP_MD_CTX_new(), EVP_MD_CTX_free); - - unsigned int hash_length = 0; - unsigned char hash[EVP_MAX_MD_SIZE]; - - EVP_DigestInit_ex(context.get(), algo, nullptr); - EVP_DigestUpdate(context.get(), s.c_str(), s.size()); - EVP_DigestFinal_ex(context.get(), hash, &hash_length); +#ifdef _WIN32 +class WSInit { +public: + WSInit() { + WSADATA wsaData; + if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; + } - std::stringstream ss; - for (auto i = 0u; i < hash_length; ++i) { - ss << std::hex << std::setw(2) << std::setfill('0') - << static_cast(hash[i]); + ~WSInit() { + if (is_valid_) WSACleanup(); } - return ss.str(); -} + bool is_valid_ = false; +}; -inline std::string MD5(const std::string &s) { - return message_digest(s, EVP_md5()); -} +static WSInit wsinit_; +#endif -inline std::string SHA_256(const std::string &s) { - return message_digest(s, EVP_sha256()); +inline bool parse_www_authenticate(const Response &res, + std::map &auth, + bool is_proxy) { + auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; + if (res.has_header(auth_key)) { + thread_local auto re = + std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); + auto s = res.get_header_value(auth_key); + auto pos = s.find(' '); + if (pos != std::string::npos) { + auto type = s.substr(0, pos); + if (type == "Basic") { + return false; + } else if (type == "Digest") { + s = s.substr(pos + 1); + auto beg = std::sregex_iterator(s.begin(), s.end(), re); + for (auto i = beg; i != std::sregex_iterator(); ++i) { + const auto &m = *i; + auto key = s.substr(static_cast(m.position(1)), + static_cast(m.length(1))); + auto val = m.length(2) > 0 + ? s.substr(static_cast(m.position(2)), + static_cast(m.length(2))) + : s.substr(static_cast(m.position(3)), + static_cast(m.length(3))); + auth[std::move(key)] = std::move(val); + } + return true; + } + } + } + return false; +} + +class ContentProviderAdapter { +public: + explicit ContentProviderAdapter( + ContentProviderWithoutLength &&content_provider) + : content_provider_(std::move(content_provider)) {} + + bool operator()(size_t offset, size_t, DataSink &sink) { + return content_provider_(offset, sink); + } + +private: + ContentProviderWithoutLength content_provider_; +}; + +// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 +namespace fields { + +inline bool is_token_char(char c) { + return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || + c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || + c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; +} + +inline bool is_token(const std::string &s) { + if (s.empty()) { return false; } + for (auto c : s) { + if (!is_token_char(c)) { return false; } + } + return true; +} + +inline bool is_field_name(const std::string &s) { return is_token(s); } + +inline bool is_vchar(char c) { return c >= 33 && c <= 126; } + +inline bool is_obs_text(char c) { return 128 <= static_cast(c); } + +inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } + +inline bool is_field_content(const std::string &s) { + if (s.empty()) { return true; } + + if (s.size() == 1) { + return is_field_vchar(s[0]); + } else if (s.size() == 2) { + return is_field_vchar(s[0]) && is_field_vchar(s[1]); + } else { + size_t i = 0; + + if (!is_field_vchar(s[i])) { return false; } + i++; + + while (i < s.size() - 1) { + auto c = s[i++]; + if (c == ' ' || c == '\t' || is_field_vchar(c)) { + } else { + return false; + } + } + + return is_field_vchar(s[i]); + } +} + +inline bool is_field_value(const std::string &s) { return is_field_content(s); } + +} // namespace fields +} // namespace detail + +/* + * Group 2: detail namespace - SSL common utilities + */ + +#ifdef CPPHTTPLIB_SSL_ENABLED +namespace detail { + +class SSLSocketStream final : public Stream { +public: + SSLSocketStream( + socket_t sock, tls::session_t session, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t max_timeout_msec = 0, + std::chrono::time_point start_time = + (std::chrono::steady_clock::time_point::min)()); + ~SSLSocketStream() override; + + bool is_readable() const override; + bool wait_readable() const override; + bool wait_writable() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; + void get_local_ip_and_port(std::string &ip, int &port) const override; + socket_t socket() const override; + time_t duration() const override; + +private: + socket_t sock_; + tls::session_t session_; + time_t read_timeout_sec_; + time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; + time_t max_timeout_msec_; + const std::chrono::time_point start_time_; +}; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline std::string message_digest(const std::string &s, const EVP_MD *algo) { + auto context = std::unique_ptr( + EVP_MD_CTX_new(), EVP_MD_CTX_free); + + unsigned int hash_length = 0; + unsigned char hash[EVP_MAX_MD_SIZE]; + + EVP_DigestInit_ex(context.get(), algo, nullptr); + EVP_DigestUpdate(context.get(), s.c_str(), s.size()); + EVP_DigestFinal_ex(context.get(), hash, &hash_length); + + std::stringstream ss; + for (auto i = 0u; i < hash_length; ++i) { + ss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(hash[i]); + } + + return ss.str(); +} + +inline std::string MD5(const std::string &s) { + return message_digest(s, EVP_md5()); +} + +inline std::string SHA_256(const std::string &s) { + return message_digest(s, EVP_sha256()); } inline std::string SHA_512(const std::string &s) { return message_digest(s, EVP_sha512()); } +#elif defined(CPPHTTPLIB_MBEDTLS_SUPPORT) +namespace { +template +inline std::string hash_to_hex(const unsigned char (&hash)[N]) { + std::stringstream ss; + for (size_t i = 0; i < N; ++i) { + ss << std::hex << std::setw(2) << std::setfill('0') + << static_cast(hash[i]); + } + return ss.str(); +} +} // namespace + +inline std::string MD5(const std::string &s) { + unsigned char hash[16]; +#ifdef CPPHTTPLIB_MBEDTLS_V3 + mbedtls_md5(reinterpret_cast(s.c_str()), s.size(), + hash); +#else + mbedtls_md5_ret(reinterpret_cast(s.c_str()), s.size(), + hash); +#endif + return hash_to_hex(hash); +} + +inline std::string SHA_256(const std::string &s) { + unsigned char hash[32]; +#ifdef CPPHTTPLIB_MBEDTLS_V3 + mbedtls_sha256(reinterpret_cast(s.c_str()), s.size(), + hash, 0); +#else + mbedtls_sha256_ret(reinterpret_cast(s.c_str()), + s.size(), hash, 0); +#endif + return hash_to_hex(hash); +} + +inline std::string SHA_512(const std::string &s) { + unsigned char hash[64]; +#ifdef CPPHTTPLIB_MBEDTLS_V3 + mbedtls_sha512(reinterpret_cast(s.c_str()), s.size(), + hash, 0); +#else + mbedtls_sha512_ret(reinterpret_cast(s.c_str()), + s.size(), hash, 0); +#endif + return hash_to_hex(hash); +} +#endif + +inline bool is_ip_address(const std::string &host) { + struct in_addr addr4; + struct in6_addr addr6; + return inet_pton(AF_INET, host.c_str(), &addr4) == 1 || + inet_pton(AF_INET6, host.c_str(), &addr6) == 1; +} + +template +inline bool process_server_socket_ssl( + const std::atomic &svr_sock, tls::session_t session, + socket_t sock, size_t keep_alive_max_count, time_t keep_alive_timeout_sec, + time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SSLSocketStream strm(sock, session, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +template +inline bool process_client_socket_ssl( + tls::session_t session, socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t max_timeout_msec, + std::chrono::time_point start_time, T callback) { + SSLSocketStream strm(sock, session, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec, max_timeout_msec, + start_time); + return callback(strm); +} inline std::pair make_digest_authentication_header( const Request &req, const std::map &auth, @@ -7234,255 +7670,88 @@ inline std::pair make_digest_authentication_header( return std::make_pair(key, field); } -inline bool is_ssl_peer_could_be_closed(SSL *ssl, socket_t sock) { - detail::set_nonblocking(sock, true); - auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); +inline bool match_hostname(const std::string &pattern, + const std::string &hostname) { + // Exact match (case-insensitive) + if (detail::case_ignore::equal(hostname, pattern)) { return true; } - char buf[1]; - return !SSL_peek(ssl, buf, 1) && - SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN; -} + // Split both pattern and hostname into components by '.' + std::vector pattern_components; + if (!pattern.empty()) { + split(pattern.data(), pattern.data() + pattern.size(), '.', + [&](const char *b, const char *e) { + pattern_components.emplace_back(b, e); + }); + } -#ifdef _WIN32 -// NOTE: This code came up with the following stackoverflow post: -// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store -inline bool load_system_certs_on_windows(X509_STORE *store) { - auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); - if (!hStore) { return false; } + std::vector host_components; + if (!hostname.empty()) { + split(hostname.data(), hostname.data() + hostname.size(), '.', + [&](const char *b, const char *e) { + host_components.emplace_back(b, e); + }); + } - auto result = false; - PCCERT_CONTEXT pContext = NULL; - while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != - nullptr) { - auto encoded_cert = - static_cast(pContext->pbCertEncoded); + // Component count must match + if (host_components.size() != pattern_components.size()) { return false; } - auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); - if (x509) { - X509_STORE_add_cert(store, x509); - X509_free(x509); - result = true; + // Compare each component with wildcard support + // Supports: "*" (full wildcard), "prefix*" (partial wildcard) + // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 + auto itr = pattern_components.begin(); + for (const auto &h : host_components) { + auto &p = *itr; + if (!detail::case_ignore::equal(p, h) && p != "*") { + bool partial_match = false; + if (!p.empty() && p[p.size() - 1] == '*') { + const auto prefix_length = p.size() - 1; + if (prefix_length == 0) { + partial_match = true; + } else if (h.size() >= prefix_length) { + partial_match = + std::equal(p.begin(), + p.begin() + static_cast( + prefix_length), + h.begin(), [](const char ca, const char cb) { + return detail::case_ignore::to_lower(ca) == + detail::case_ignore::to_lower(cb); + }); + } + } + if (!partial_match) { return false; } } + ++itr; } - CertFreeCertificateContext(pContext); - CertCloseStore(hStore, 0); - - return result; -} -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC -template -using CFObjectPtr = - std::unique_ptr::type, void (*)(CFTypeRef)>; - -inline void cf_object_ptr_deleter(CFTypeRef obj) { - if (obj) { CFRelease(obj); } + return true; } -inline bool retrieve_certs_from_keychain(CFObjectPtr &certs) { - CFStringRef keys[] = {kSecClass, kSecMatchLimit, kSecReturnRef}; - CFTypeRef values[] = {kSecClassCertificate, kSecMatchLimitAll, - kCFBooleanTrue}; - - CFObjectPtr query( - CFDictionaryCreate(nullptr, reinterpret_cast(keys), values, - sizeof(keys) / sizeof(keys[0]), - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks), - cf_object_ptr_deleter); - - if (!query) { return false; } +} // namespace detail +#endif // CPPHTTPLIB_SSL_ENABLED - CFTypeRef security_items = nullptr; - if (SecItemCopyMatching(query.get(), &security_items) != errSecSuccess || - CFArrayGetTypeID() != CFGetTypeID(security_items)) { - return false; - } +/* + * Group 3: httplib namespace - Non-SSL public API implementations + */ - certs.reset(reinterpret_cast(security_items)); - return true; +inline void default_socket_options(socket_t sock) { + detail::set_socket_opt(sock, SOL_SOCKET, +#ifdef SO_REUSEPORT + SO_REUSEPORT, +#else + SO_REUSEADDR, +#endif + 1); } -inline bool retrieve_root_certs_from_keychain(CFObjectPtr &certs) { - CFArrayRef root_security_items = nullptr; - if (SecTrustCopyAnchorCertificates(&root_security_items) != errSecSuccess) { - return false; +inline std::string get_bearer_token_auth(const Request &req) { + if (req.has_header("Authorization")) { + constexpr auto bearer_header_prefix_len = detail::str_len("Bearer "); + return req.get_header_value("Authorization") + .substr(bearer_header_prefix_len); } - - certs.reset(root_security_items); - return true; + return ""; } -inline bool add_certs_to_x509_store(CFArrayRef certs, X509_STORE *store) { - auto result = false; - for (auto i = 0; i < CFArrayGetCount(certs); ++i) { - const auto cert = reinterpret_cast( - CFArrayGetValueAtIndex(certs, i)); - - if (SecCertificateGetTypeID() != CFGetTypeID(cert)) { continue; } - - CFDataRef cert_data = nullptr; - if (SecItemExport(cert, kSecFormatX509Cert, 0, nullptr, &cert_data) != - errSecSuccess) { - continue; - } - - CFObjectPtr cert_data_ptr(cert_data, cf_object_ptr_deleter); - - auto encoded_cert = static_cast( - CFDataGetBytePtr(cert_data_ptr.get())); - - auto x509 = - d2i_X509(NULL, &encoded_cert, CFDataGetLength(cert_data_ptr.get())); - - if (x509) { - X509_STORE_add_cert(store, x509); - X509_free(x509); - result = true; - } - } - - return result; -} - -inline bool load_system_certs_on_macos(X509_STORE *store) { - auto result = false; - CFObjectPtr certs(nullptr, cf_object_ptr_deleter); - if (retrieve_certs_from_keychain(certs) && certs) { - result = add_certs_to_x509_store(certs.get(), store); - } - - if (retrieve_root_certs_from_keychain(certs) && certs) { - result = add_certs_to_x509_store(certs.get(), store) || result; - } - - return result; -} -#endif // _WIN32 -#endif // CPPHTTPLIB_OPENSSL_SUPPORT - -#ifdef _WIN32 -class WSInit { -public: - WSInit() { - WSADATA wsaData; - if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; - } - - ~WSInit() { - if (is_valid_) WSACleanup(); - } - - bool is_valid_ = false; -}; - -static WSInit wsinit_; -#endif - -inline bool parse_www_authenticate(const Response &res, - std::map &auth, - bool is_proxy) { - auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; - if (res.has_header(auth_key)) { - thread_local auto re = - std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~"); - auto s = res.get_header_value(auth_key); - auto pos = s.find(' '); - if (pos != std::string::npos) { - auto type = s.substr(0, pos); - if (type == "Basic") { - return false; - } else if (type == "Digest") { - s = s.substr(pos + 1); - auto beg = std::sregex_iterator(s.begin(), s.end(), re); - for (auto i = beg; i != std::sregex_iterator(); ++i) { - const auto &m = *i; - auto key = s.substr(static_cast(m.position(1)), - static_cast(m.length(1))); - auto val = m.length(2) > 0 - ? s.substr(static_cast(m.position(2)), - static_cast(m.length(2))) - : s.substr(static_cast(m.position(3)), - static_cast(m.length(3))); - auth[std::move(key)] = std::move(val); - } - return true; - } - } - } - return false; -} - -class ContentProviderAdapter { -public: - explicit ContentProviderAdapter( - ContentProviderWithoutLength &&content_provider) - : content_provider_(std::move(content_provider)) {} - - bool operator()(size_t offset, size_t, DataSink &sink) { - return content_provider_(offset, sink); - } - -private: - ContentProviderWithoutLength content_provider_; -}; - -// NOTE: https://www.rfc-editor.org/rfc/rfc9110#section-5 -namespace fields { - -inline bool is_token_char(char c) { - return std::isalnum(c) || c == '!' || c == '#' || c == '$' || c == '%' || - c == '&' || c == '\'' || c == '*' || c == '+' || c == '-' || - c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~'; -} - -inline bool is_token(const std::string &s) { - if (s.empty()) { return false; } - for (auto c : s) { - if (!is_token_char(c)) { return false; } - } - return true; -} - -inline bool is_field_name(const std::string &s) { return is_token(s); } - -inline bool is_vchar(char c) { return c >= 33 && c <= 126; } - -inline bool is_obs_text(char c) { return 128 <= static_cast(c); } - -inline bool is_field_vchar(char c) { return is_vchar(c) || is_obs_text(c); } - -inline bool is_field_content(const std::string &s) { - if (s.empty()) { return true; } - - if (s.size() == 1) { - return is_field_vchar(s[0]); - } else if (s.size() == 2) { - return is_field_vchar(s[0]) && is_field_vchar(s[1]); - } else { - size_t i = 0; - - if (!is_field_vchar(s[i])) { return false; } - i++; - - while (i < s.size() - 1) { - auto c = s[i++]; - if (c == ' ' || c == '\t' || is_field_vchar(c)) { - } else { - return false; - } - } - - return is_field_vchar(s[i]); - } -} - -inline bool is_field_value(const std::string &s) { return is_field_content(s); } - -} // namespace fields - -} // namespace detail - inline const char *status_message(int status) { switch (status) { case StatusCode::Continue_100: return "Continue"; @@ -7922,6 +8191,11 @@ make_bearer_token_authentication_header(const std::string &token, } // Request implementation +inline size_t Request::get_header_value_u64(const std::string &key, size_t def, + size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + inline bool Request::has_header(const std::string &key) const { return detail::has_header(headers, key); } @@ -8043,6 +8317,11 @@ inline size_t MultipartFormData::get_file_count(const std::string &key) const { } // Response implementation +inline size_t Response::get_header_value_u64(const std::string &key, size_t def, + size_t id) const { + return detail::get_header_value_u64(headers, key, def, id); +} + inline bool Response::has_header(const std::string &key) const { return headers.find(key) != headers.end(); } @@ -8158,6 +8437,12 @@ inline void Response::set_file_content(const std::string &path) { } // Result implementation +inline size_t Result::get_request_header_value_u64(const std::string &key, + size_t def, + size_t id) const { + return detail::get_header_value_u64(request_headers_, key, def, id); +} + inline bool Result::has_request_header(const std::string &key) const { return request_headers_.find(key) != request_headers_.end(); } @@ -8244,6 +8529,75 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) { return n; } +// ThreadPool implementation +inline ThreadPool::ThreadPool(size_t n, size_t mqr) + : shutdown_(false), max_queued_requests_(mqr) { + threads_.reserve(n); + while (n) { + threads_.emplace_back(worker(*this)); + n--; + } +} + +inline bool ThreadPool::enqueue(std::function fn) { + { + std::unique_lock lock(mutex_); + if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) { + return false; + } + jobs_.push_back(std::move(fn)); + } + + cond_.notify_one(); + return true; +} + +inline void ThreadPool::shutdown() { + // Stop all worker threads... + { + std::unique_lock lock(mutex_); + shutdown_ = true; + } + + cond_.notify_all(); + + // Join... + for (auto &t : threads_) { + t.join(); + } +} + +inline ThreadPool::worker::worker(ThreadPool &pool) : pool_(pool) {} + +inline void ThreadPool::worker::operator()() { + for (;;) { + std::function fn; + { + std::unique_lock lock(pool_.mutex_); + + pool_.cond_.wait(lock, + [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); + + if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } + + fn = pool_.jobs_.front(); + pool_.jobs_.pop_front(); + } + + assert(true == static_cast(fn)); + fn(); + } + +#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) && \ + !defined(LIBRESSL_VERSION_NUMBER) + OPENSSL_thread_stop(); +#endif +} + +/* + * Group 1 (continued): detail namespace - Stream implementations + */ + namespace detail { inline void calc_actual_timeout(time_t max_timeout_msec, time_t duration_msec, @@ -8572,53 +8926,202 @@ inline bool check_and_write_headers(Stream &strm, Headers &headers, } // namespace detail -// HTTP server implementation -inline Server::Server() - : new_task_queue( - [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { -#ifndef _WIN32 - signal(SIGPIPE, SIG_IGN); +/* + * Group 2 (continued): detail namespace - SSLSocketStream implementation + */ + +#ifdef CPPHTTPLIB_SSL_ENABLED +namespace detail { + +// SSL socket stream implementation +inline SSLSocketStream::SSLSocketStream( + socket_t sock, tls::session_t session, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, time_t max_timeout_msec, + std::chrono::time_point start_time) + : sock_(sock), session_(session), read_timeout_sec_(read_timeout_sec), + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec), + max_timeout_msec_(max_timeout_msec), start_time_(start_time) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // Clear AUTO_RETRY for proper non-blocking I/O timeout handling + // Note: create_session() also clears this, but SSLClient currently + // uses ssl_new() which does not. Until full TLS API migration is complete, + // we need to ensure AUTO_RETRY is cleared here regardless of how the + // SSL session was created. + SSL_clear_mode(static_cast(session), SSL_MODE_AUTO_RETRY); #endif } -inline Server::~Server() = default; +inline SSLSocketStream::~SSLSocketStream() = default; -inline std::unique_ptr -Server::make_matcher(const std::string &pattern) { - if (pattern.find("/:") != std::string::npos) { - return detail::make_unique(pattern); - } else { - return detail::make_unique(pattern); - } +inline bool SSLSocketStream::is_readable() const { + return tls::pending(session_) > 0; } -inline Server &Server::Get(const std::string &pattern, Handler handler) { - get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} +inline bool SSLSocketStream::wait_readable() const { + if (max_timeout_msec_ <= 0) { + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + } -inline Server &Server::Post(const std::string &pattern, Handler handler) { - post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; -} + time_t read_timeout_sec; + time_t read_timeout_usec; + calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, + read_timeout_usec_, read_timeout_sec, read_timeout_usec); -inline Server &Server::Post(const std::string &pattern, - HandlerWithContentReader handler) { - post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; + return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; } -inline Server &Server::Put(const std::string &pattern, Handler handler) { - put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); - return *this; +inline bool SSLSocketStream::wait_writable() const { + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && + is_socket_alive(sock_) && !tls::is_peer_closed(session_, sock_); } -inline Server &Server::Put(const std::string &pattern, - HandlerWithContentReader handler) { - put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), - std::move(handler)); - return *this; +inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { + if (tls::pending(session_) > 0) { + tls::TlsError err; + auto ret = tls::read(session_, ptr, size, err); + if (ret == 0 || err.code == tls::ErrorCode::PeerClosed) { + error_ = Error::ConnectionClosed; + } + return ret; + } else if (wait_readable()) { + tls::TlsError err; + auto ret = tls::read(session_, ptr, size, err); + if (ret < 0) { + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err.code == tls::ErrorCode::WantRead || + (err.code == tls::ErrorCode::SyscallError && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err.code == tls::ErrorCode::WantRead) { +#endif + if (tls::pending(session_) > 0) { + return tls::read(session_, ptr, size, err); + } else if (wait_readable()) { + std::this_thread::sleep_for(std::chrono::microseconds{10}); + ret = tls::read(session_, ptr, size, err); + if (ret >= 0) { return ret; } + } else { + break; + } + } + assert(ret < 0); + } else if (ret == 0 || err.code == tls::ErrorCode::PeerClosed) { + error_ = Error::ConnectionClosed; + } + return ret; + } else { + error_ = Error::Timeout; + return -1; + } +} + +inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { + if (wait_writable()) { + auto handle_size = + std::min(size, (std::numeric_limits::max)()); + + tls::TlsError err; + auto ret = tls::write(session_, ptr, handle_size, err); + if (ret < 0) { + auto n = 1000; +#ifdef _WIN32 + while (--n >= 0 && (err.code == tls::ErrorCode::WantWrite || + (err.code == tls::ErrorCode::SyscallError && + WSAGetLastError() == WSAETIMEDOUT))) { +#else + while (--n >= 0 && err.code == tls::ErrorCode::WantWrite) { +#endif + if (wait_writable()) { + std::this_thread::sleep_for(std::chrono::microseconds{10}); + ret = tls::write(session_, ptr, handle_size, err); + if (ret >= 0) { return ret; } + } else { + break; + } + } + assert(ret < 0); + } + return ret; + } + return -1; +} + +inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + detail::get_remote_ip_and_port(sock_, ip, port); +} + +inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, + int &port) const { + detail::get_local_ip_and_port(sock_, ip, port); +} + +inline socket_t SSLSocketStream::socket() const { return sock_; } + +inline time_t SSLSocketStream::duration() const { + return std::chrono::duration_cast( + std::chrono::steady_clock::now() - start_time_) + .count(); +} + +} // namespace detail +#endif // CPPHTTPLIB_SSL_ENABLED + +/* + * Group 4: Server implementation + */ + +// HTTP server implementation +inline Server::Server() + : new_task_queue( + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) { +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif +} + +inline Server::~Server() = default; + +inline std::unique_ptr +Server::make_matcher(const std::string &pattern) { + if (pattern.find("/:") != std::string::npos) { + return detail::make_unique(pattern); + } else { + return detail::make_unique(pattern); + } +} + +inline Server &Server::Get(const std::string &pattern, Handler handler) { + get_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, Handler handler) { + post_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Post(const std::string &pattern, + HandlerWithContentReader handler) { + post_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, Handler handler) { + put_handlers_.emplace_back(make_matcher(pattern), std::move(handler)); + return *this; +} + +inline Server &Server::Put(const std::string &pattern, + HandlerWithContentReader handler) { + put_handlers_for_content_reader_.emplace_back(make_matcher(pattern), + std::move(handler)); + return *this; } inline Server &Server::Patch(const std::string &pattern, Handler handler) { @@ -9179,7 +9682,7 @@ inline bool Server::read_content_core( // oversized request and fail early (causing connection close). For SSL // builds we cannot reliably peek the decrypted application bytes, so keep // the original behaviour. -#if !defined(CPPHTTPLIB_OPENSSL_SUPPORT) +#if !defined(CPPHTTPLIB_TLS_ENABLED) if (!req.has_header("Content-Length") && !detail::is_chunked_transfer_encoding(req.headers)) { // Only peek if payload_max_length is set to a finite value @@ -10014,6 +10517,9 @@ inline void Server::output_error_log(const Error &err, } } +/* + * Group 5: ClientImpl and Client (Universal) implementation + */ // HTTP client implementation inline ClientImpl::ClientImpl(const std::string &host) : ClientImpl(host, 80, std::string(), std::string()) {} @@ -10057,10 +10563,6 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { basic_auth_username_ = rhs.basic_auth_username_; basic_auth_password_ = rhs.basic_auth_password_; bearer_token_auth_token_ = rhs.bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - digest_auth_username_ = rhs.digest_auth_username_; - digest_auth_password_ = rhs.digest_auth_password_; -#endif keep_alive_ = rhs.keep_alive_; follow_location_ = rhs.follow_location_; path_encode_ = rhs.path_encode_; @@ -10076,22 +10578,19 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + logger_ = rhs.logger_; + error_logger_ = rhs.error_logger_; + +#ifdef CPPHTTPLIB_SSL_ENABLED + digest_auth_username_ = rhs.digest_auth_username_; + digest_auth_password_ = rhs.digest_auth_password_; proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT ca_cert_file_path_ = rhs.ca_cert_file_path_; ca_cert_dir_path_ = rhs.ca_cert_dir_path_; - ca_cert_store_ = rhs.ca_cert_store_; -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT server_certificate_verification_ = rhs.server_certificate_verification_; server_hostname_verification_ = rhs.server_hostname_verification_; - server_certificate_verifier_ = rhs.server_certificate_verifier_; #endif - logger_ = rhs.logger_; - error_logger_ = rhs.error_logger_; } inline socket_t ClientImpl::create_client_socket(Error &error) const { @@ -10127,22 +10626,6 @@ inline bool ClientImpl::ensure_socket_connection(Socket &socket, Error &error) { return create_and_connect_socket(socket, error); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline bool SSLClient::ensure_socket_connection(Socket &socket, Error &error) { - if (!ClientImpl::ensure_socket_connection(socket, error)) { return false; } - - if (!proxy_host_.empty() && proxy_port_ != -1) { return true; } - - if (!initialize_ssl(socket, error)) { - shutdown_socket(socket); - close_socket(socket); - return false; - } - - return true; -} -#endif - inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, bool /*shutdown_gracefully*/) { // If there are any requests in flight from threads other than us, then it's @@ -10167,9 +10650,10 @@ inline void ClientImpl::close_socket(Socket &socket) { socket_requests_are_from_thread_ == std::this_thread::get_id()); // It is also a bug if this happens while SSL is still active -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED assert(socket.ssl == nullptr); #endif + if (socket.sock == INVALID_SOCKET) { return; } detail::close_socket(socket.sock); socket.sock = INVALID_SOCKET; @@ -10218,6 +10702,8 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { if (error == Error::SSLPeerCouldBeClosed_) { assert(!ret); ret = send_(req, res, error); + // If still failing with SSLPeerCouldBeClosed_, convert to Read error + if (error == Error::SSLPeerCouldBeClosed_) { error = Error::Read; } } return ret; } @@ -10235,9 +10721,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { if (socket_.is_open()) { is_alive = detail::is_socket_alive(socket_.sock); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED if (is_alive && is_ssl()) { - if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + if (tls::is_peer_closed(socket_.ssl, socket_.sock)) { is_alive = false; } } @@ -10261,7 +10747,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) { return false; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED // TODO: refactoring if (is_ssl()) { auto &scli = static_cast(*this); @@ -10343,9 +10829,9 @@ inline Result ClientImpl::send_(Request &&req) { auto res = detail::make_unique(); auto error = Error::Success; auto ret = send(req, *res, error); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers), - last_ssl_error_, last_openssl_error_}; + last_ssl_error_, last_backend_error_}; #else return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; #endif @@ -10422,9 +10908,9 @@ ClientImpl::open_stream(const std::string &method, const std::string &path, auto is_alive = false; if (socket_.is_open()) { is_alive = detail::is_socket_alive(socket_.sock); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED if (is_alive && is_ssl()) { - if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + if (tls::is_peer_closed(socket_.ssl, socket_.sock)) { is_alive = false; } } @@ -10442,7 +10928,7 @@ ClientImpl::open_stream(const std::string &method, const std::string &path, return handle; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED if (is_ssl()) { auto &scli = static_cast(*this); if (!proxy_host_.empty() && proxy_port_ != -1) { @@ -10458,11 +10944,12 @@ ClientImpl::open_stream(const std::string &method, const std::string &path, transfer_socket_ownership_to_handle(handle); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl() && handle.connection_->ssl) { +#ifdef CPPHTTPLIB_SSL_ENABLED + if (is_ssl() && handle.connection_->session) { handle.socket_stream_ = detail::make_unique( - handle.connection_->sock, handle.connection_->ssl, read_timeout_sec_, - read_timeout_usec_, write_timeout_sec_, write_timeout_usec_); + handle.connection_->sock, handle.connection_->session, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_); } else { handle.socket_stream_ = detail::make_unique( handle.connection_->sock, read_timeout_sec_, read_timeout_usec_, @@ -10617,7 +11104,6 @@ inline void ClientImpl::StreamHandle::parse_trailers_if_needed() { } } -// Inline method implementations for `ChunkedDecoder`. namespace detail { inline ChunkedDecoder::ChunkedDecoder(Stream &s) : strm(s) {} @@ -10681,8 +11167,8 @@ inline bool ChunkedDecoder::parse_trailers_into(Headers &dest, inline void ClientImpl::transfer_socket_ownership_to_handle(StreamHandle &handle) { handle.connection_->sock = socket_.sock; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - handle.connection_->ssl = socket_.ssl; +#ifdef CPPHTTPLIB_SSL_ENABLED + handle.connection_->session = socket_.ssl; socket_.ssl = nullptr; #endif socket_.sock = INVALID_SOCKET; @@ -10735,7 +11221,7 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, ret = redirect(req, res, error); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED if ((res.status == StatusCode::Unauthorized_401 || res.status == StatusCode::ProxyAuthenticationRequired_407) && req.authorization_count_ < 5) { @@ -10839,7 +11325,7 @@ inline bool ClientImpl::create_redirect_client( // Create appropriate client type and handle redirect if (need_ssl) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED // Create SSL client for HTTPS redirect SSLClient redirect_client(host, port); @@ -10859,9 +11345,10 @@ inline bool ClientImpl::create_redirect_client( server_hostname_verification_); } - // Handle CA certificate store and paths if available - if (ca_cert_store_ && X509_STORE_up_ref(ca_cert_store_)) { - redirect_client.set_ca_cert_store(ca_cert_store_); + // Transfer CA certificate to redirect client + if (!ca_cert_pem_.empty()) { + redirect_client.load_ca_cert_store(ca_cert_pem_.c_str(), + ca_cert_pem_.size()); } if (!ca_cert_file_path_.empty()) { redirect_client.set_ca_cert_path(ca_cert_file_path_, ca_cert_dir_path_); @@ -10914,7 +11401,7 @@ inline void ClientImpl::setup_redirect_client(ClientType &client) { if (!bearer_token_auth_token_.empty()) { client.set_bearer_token_auth(bearer_token_auth_token_); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED if (!digest_auth_username_.empty()) { client.set_digest_auth(digest_auth_username_, digest_auth_password_); } @@ -10934,7 +11421,7 @@ inline void ClientImpl::setup_redirect_client(ClientType &client) { if (!proxy_bearer_token_auth_token_.empty()) { client.set_proxy_bearer_token_auth(proxy_bearer_token_auth_token_); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED if (!proxy_digest_auth_username_.empty()) { client.set_proxy_digest_auth(proxy_digest_auth_username_, proxy_digest_auth_password_); @@ -11305,9 +11792,9 @@ inline Result ClientImpl::send_with_content_provider_and_receiver( std::move(content_provider_without_length), content_type, std::move(content_receiver), error); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED return Result{std::move(res), error, std::move(req.headers), last_ssl_error_, - last_openssl_error_}; + last_backend_error_}; #else return Result{std::move(res), error, std::move(req.headers)}; #endif @@ -11347,11 +11834,11 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, auto write_request_success = write_request(strm, req, close_connection, error, expect_100_continue); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl()) { +#ifdef CPPHTTPLIB_SSL_ENABLED + if (is_ssl() && !expect_100_continue) { auto is_proxy_enabled = !proxy_host_.empty() && proxy_port_ != -1; if (!is_proxy_enabled) { - if (detail::is_ssl_peer_could_be_closed(socket_.ssl, socket_.sock)) { + if (tls::is_peer_closed(socket_.ssl, socket_.sock)) { error = Error::SSLPeerCouldBeClosed_; output_error_log(error, &req); return false; @@ -12374,14 +12861,6 @@ inline void ClientImpl::set_bearer_token_auth(const std::string &token) { bearer_token_auth_token_ = token; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_digest_auth(const std::string &username, - const std::string &password) { - digest_auth_username_ = username; - digest_auth_password_ = password; -} -#endif - inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } @@ -12437,11 +12916,11 @@ inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { proxy_bearer_token_auth_token_ = token; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - proxy_digest_auth_username_ = username; - proxy_digest_auth_password_ = password; +#ifdef CPPHTTPLIB_SSL_ENABLED +inline void ClientImpl::set_digest_auth(const std::string &username, + const std::string &password) { + digest_auth_username_ = username; + digest_auth_password_ = password; } inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, @@ -12450,12 +12929,23 @@ inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, ca_cert_dir_path_ = ca_cert_dir_path; } -inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (ca_cert_store && ca_cert_store != ca_cert_store_) { - ca_cert_store_ = ca_cert_store; - } +inline void ClientImpl::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + proxy_digest_auth_username_ = username; + proxy_digest_auth_password_ = password; +} + +inline void ClientImpl::enable_server_certificate_verification(bool enabled) { + server_certificate_verification_ = enabled; +} + +inline void ClientImpl::enable_server_hostname_verification(bool enabled) { + server_hostname_verification_ = enabled; } +#endif +// ClientImpl::set_ca_cert_store is defined after TLS namespace (uses helpers) +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, std::size_t size) const { auto mem = BIO_new_mem_buf(ca_cert, static_cast(size)); @@ -12480,17 +12970,9 @@ inline X509_STORE *ClientImpl::create_ca_cert_store(const char *ca_cert, return cts; } -inline void ClientImpl::enable_server_certificate_verification(bool enabled) { - server_certificate_verification_ = enabled; -} - -inline void ClientImpl::enable_server_hostname_verification(bool enabled) { - server_hostname_verification_ = enabled; -} - inline void ClientImpl::set_server_certificate_verifier( - std::function verifier) { - server_certificate_verifier_ = verifier; + std::function /*verifier*/) { + // Base implementation does nothing - SSLClient overrides this } #endif @@ -12503,544 +12985,890 @@ inline void ClientImpl::set_error_logger(ErrorLogger error_logger) { } /* - * SSL Implementation + * SSL/TLS Common Implementation */ -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -namespace detail { - -inline bool is_ip_address(const std::string &host) { - struct in_addr addr4; - struct in6_addr addr6; - return inet_pton(AF_INET, host.c_str(), &addr4) == 1 || - inet_pton(AF_INET6, host.c_str(), &addr6) == 1; -} -template -inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, - U SSL_connect_or_accept, V setup) { - SSL *ssl = nullptr; - { - std::lock_guard guard(ctx_mutex); - ssl = SSL_new(ctx); +inline ClientConnection::~ClientConnection() { +#ifdef CPPHTTPLIB_SSL_ENABLED + if (session) { + tls::shutdown(session, true); + tls::free_session(session); + session = nullptr; } +#endif - if (ssl) { - set_nonblocking(sock, true); - auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); - BIO_set_nbio(bio, 1); - SSL_set_bio(ssl, bio, bio); - - if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { - SSL_shutdown(ssl); - { - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); - } - set_nonblocking(sock, false); - return nullptr; - } - BIO_set_nbio(bio, 0); - set_nonblocking(sock, false); + if (sock != INVALID_SOCKET) { + detail::close_socket(sock); + sock = INVALID_SOCKET; } - - return ssl; } -inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock, - bool shutdown_gracefully) { - // sometimes we may want to skip this to try to avoid SIGPIPE if we know - // the remote has closed the network connection - // Note that it is not always possible to avoid SIGPIPE, this is merely a - // best-efforts. - if (shutdown_gracefully) { - (void)(sock); - // SSL_shutdown() returns 0 on first call (indicating close_notify alert - // sent) and 1 on subsequent call (indicating close_notify alert received) - if (SSL_shutdown(ssl) == 0) { - // Expected to return 1, but even if it doesn't, we free ssl - SSL_shutdown(ssl); - } - } +// Universal client implementation +inline Client::Client(const std::string &scheme_host_port) + : Client(scheme_host_port, std::string(), std::string()) {} - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); -} +inline Client::Client(const std::string &scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path) { + const static std::regex re( + R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); -template -bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, - U ssl_connect_or_accept, - time_t timeout_sec, time_t timeout_usec, - int *ssl_error) { - auto res = 0; - while ((res = ssl_connect_or_accept(ssl)) != 1) { - auto err = SSL_get_error(ssl, res); - switch (err) { - case SSL_ERROR_WANT_READ: - if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } - break; - case SSL_ERROR_WANT_WRITE: - if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } - break; - default: break; - } - if (ssl_error) { *ssl_error = err; } - return false; - } - return true; -} - -template -inline bool process_server_socket_ssl( - const std::atomic &svr_sock, SSL *ssl, socket_t sock, - size_t keep_alive_max_count, time_t keep_alive_timeout_sec, - time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { - return process_server_socket_core( - svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec); - return callback(strm, close_connection, connection_closed); - }); -} - -template -inline bool process_client_socket_ssl( - SSL *ssl, socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time, T callback) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, - write_timeout_sec, write_timeout_usec, max_timeout_msec, - start_time); - return callback(strm); -} - -// SSL socket stream implementation -inline SSLSocketStream::SSLSocketStream( - socket_t sock, SSL *ssl, time_t read_timeout_sec, time_t read_timeout_usec, - time_t write_timeout_sec, time_t write_timeout_usec, - time_t max_timeout_msec, - std::chrono::time_point start_time) - : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec), - write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec), - max_timeout_msec_(max_timeout_msec), start_time_(start_time) { - SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); -} - -inline SSLSocketStream::~SSLSocketStream() = default; - -inline bool SSLSocketStream::is_readable() const { - return SSL_pending(ssl_) > 0; -} + std::smatch m; + if (std::regex_match(scheme_host_port, m, re)) { + auto scheme = m[1].str(); -inline bool SSLSocketStream::wait_readable() const { - if (max_timeout_msec_ <= 0) { - return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; - } +#ifdef CPPHTTPLIB_SSL_ENABLED + if (!scheme.empty() && (scheme != "http" && scheme != "https")) { +#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; + } - time_t read_timeout_sec; - time_t read_timeout_usec; - calc_actual_timeout(max_timeout_msec_, duration(), read_timeout_sec_, - read_timeout_usec_, read_timeout_sec, read_timeout_usec); + auto is_ssl = scheme == "https"; - return select_read(sock_, read_timeout_sec, read_timeout_usec) > 0; -} + auto host = m[2].str(); + if (host.empty()) { host = m[3].str(); } -inline bool SSLSocketStream::wait_writable() const { - return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_) && !is_ssl_peer_could_be_closed(ssl_, sock_); -} + auto port_str = m[4].str(); + auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); -inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { - if (SSL_pending(ssl_) > 0) { - auto ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret == 0) { error_ = Error::ConnectionClosed; } - return ret; - } else if (wait_readable()) { - auto ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret < 0) { - auto err = SSL_get_error(ssl_, ret); - auto n = 1000; -#ifdef _WIN32 - while (--n >= 0 && (err == SSL_ERROR_WANT_READ || - (err == SSL_ERROR_SYSCALL && - WSAGetLastError() == WSAETIMEDOUT))) { -#else - while (--n >= 0 && err == SSL_ERROR_WANT_READ) { + if (is_ssl) { +#ifdef CPPHTTPLIB_SSL_ENABLED + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); + is_ssl_ = is_ssl; #endif - if (SSL_pending(ssl_) > 0) { - return SSL_read(ssl_, ptr, static_cast(size)); - } else if (wait_readable()) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); - ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret >= 0) { return ret; } - err = SSL_get_error(ssl_, ret); - } else { - break; - } - } - assert(ret < 0); - } else if (ret == 0) { - error_ = Error::ConnectionClosed; + } else { + cli_ = detail::make_unique(host, port, client_cert_path, + client_key_path); } - return ret; } else { - error_ = Error::Timeout; - return -1; + // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) + // if port param below changes. + cli_ = detail::make_unique(scheme_host_port, 80, + client_cert_path, client_key_path); } -} +} // namespace detail -inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { - if (wait_writable()) { - auto handle_size = static_cast( - std::min(size, (std::numeric_limits::max)())); +inline Client::Client(const std::string &host, int port) + : cli_(detail::make_unique(host, port)) {} - auto ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret < 0) { - auto err = SSL_get_error(ssl_, ret); - auto n = 1000; -#ifdef _WIN32 - while (--n >= 0 && (err == SSL_ERROR_WANT_WRITE || - (err == SSL_ERROR_SYSCALL && - WSAGetLastError() == WSAETIMEDOUT))) { -#else - while (--n >= 0 && err == SSL_ERROR_WANT_WRITE) { -#endif - if (wait_writable()) { - std::this_thread::sleep_for(std::chrono::microseconds{10}); - ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret >= 0) { return ret; } - err = SSL_get_error(ssl_, ret); - } else { - break; - } - } - assert(ret < 0); - } - return ret; - } - return -1; -} +inline Client::Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : cli_(detail::make_unique(host, port, client_cert_path, + client_key_path)) {} -inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { - detail::get_remote_ip_and_port(sock_, ip, port); -} +inline Client::~Client() = default; -inline void SSLSocketStream::get_local_ip_and_port(std::string &ip, - int &port) const { - detail::get_local_ip_and_port(sock_, ip, port); +inline bool Client::is_valid() const { + return cli_ != nullptr && cli_->is_valid(); } -inline socket_t SSLSocketStream::socket() const { return sock_; } - -inline time_t SSLSocketStream::duration() const { - return std::chrono::duration_cast( - std::chrono::steady_clock::now() - start_time_) - .count(); +inline Result Client::Get(const std::string &path, DownloadProgress progress) { + return cli_->Get(path, std::move(progress)); } - -} // namespace detail - -// SSL HTTP server implementation -inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path, - const char *client_ca_cert_dir_path, - const char *private_key_password) { - ctx_ = SSL_CTX_new(TLS_server_method()); - - if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - if (private_key_password != nullptr && (private_key_password[0] != '\0')) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, - reinterpret_cast(const_cast(private_key_password))); - } - - if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || - SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != - 1 || - SSL_CTX_check_private_key(ctx_) != 1) { - last_ssl_error_ = static_cast(ERR_get_error()); - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } else if (client_ca_cert_file_path || client_ca_cert_dir_path) { - SSL_CTX_load_verify_locations(ctx_, client_ca_cert_file_path, - client_ca_cert_dir_path); - - // Set client CA list to be sent to clients during TLS handshake - if (client_ca_cert_file_path) { - auto ca_list = SSL_load_client_CA_file(client_ca_cert_file_path); - if (ca_list != nullptr) { - SSL_CTX_set_client_CA_list(ctx_, ca_list); - } else { - // Failed to load client CA list, but we continue since - // SSL_CTX_load_verify_locations already succeeded and - // certificate verification will still work - last_ssl_error_ = static_cast(ERR_get_error()); - } - } - - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); - } - } +inline Result Client::Get(const std::string &path, const Headers &headers, + DownloadProgress progress) { + return cli_->Get(path, headers, std::move(progress)); } - -inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { - ctx_ = SSL_CTX_new(TLS_server_method()); - - if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); - - if (SSL_CTX_use_certificate(ctx_, cert) != 1 || - SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } else if (client_ca_cert_store) { - SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); - - // Extract CA names from the store and set them as the client CA list - auto ca_list = extract_ca_names_from_x509_store(client_ca_cert_store); - if (ca_list) { - SSL_CTX_set_client_CA_list(ctx_, ca_list); - } else { - // Failed to extract CA names, record the error - last_ssl_error_ = static_cast(ERR_get_error()); - } - - SSL_CTX_set_verify( - ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr); - } - } +inline Result Client::Get(const std::string &path, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, std::move(content_receiver), std::move(progress)); } - -inline SSLServer::SSLServer( - const std::function &setup_ssl_ctx_callback) { - ctx_ = SSL_CTX_new(TLS_method()); - if (ctx_) { - if (!setup_ssl_ctx_callback(*ctx_)) { - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } - } +inline Result Client::Get(const std::string &path, const Headers &headers, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, headers, std::move(content_receiver), + std::move(progress)); } - -inline SSLServer::~SSLServer() { - if (ctx_) { SSL_CTX_free(ctx_); } +inline Result Client::Get(const std::string &path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver), std::move(progress)); } - -inline bool SSLServer::is_valid() const { return ctx_; } - -inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } - -inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { - - std::lock_guard guard(ctx_mutex_); - - SSL_CTX_use_certificate(ctx_, cert); - SSL_CTX_use_PrivateKey(ctx_, private_key); - - if (client_ca_cert_store != nullptr) { - SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); - } +inline Result Client::Get(const std::string &path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, DownloadProgress progress) { + return cli_->Get(path, params, headers, std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, params, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const std::string &path, const Params ¶ms, + const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Get(path, params, headers, std::move(response_handler), + std::move(content_receiver), std::move(progress)); } -inline bool SSLServer::process_and_close_socket(socket_t sock) { - auto ssl = detail::ssl_new( - sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { - return detail::ssl_connect_or_accept_nonblocking( - sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_, - &last_ssl_error_); - }, - [](SSL * /*ssl2*/) { return true; }); - - auto ret = false; - if (ssl) { - std::string remote_addr; - int remote_port = 0; - detail::get_remote_ip_and_port(sock, remote_addr, remote_port); - - std::string local_addr; - int local_port = 0; - detail::get_local_ip_and_port(sock, local_addr, local_port); +inline Result Client::Head(const std::string &path) { return cli_->Head(path); } +inline Result Client::Head(const std::string &path, const Headers &headers) { + return cli_->Head(path, headers); +} - ret = detail::process_server_socket_ssl( - svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, - read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, - write_timeout_usec_, - [&](Stream &strm, bool close_connection, bool &connection_closed) { - return process_request(strm, remote_addr, remote_port, local_addr, - local_port, close_connection, - connection_closed, - [&](Request &req) { req.ssl = ssl; }); - }); +inline Result Client::Post(const std::string &path) { return cli_->Post(path); } +inline Result Client::Post(const std::string &path, const Headers &headers) { + return cli_->Post(path, headers); +} +inline Result Client::Post(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, body, content_length, content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Post(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, body, content_type, progress); +} +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Post(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Post(path, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, std::move(content_provider), content_type, progress); +} +inline Result Client::Post(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Post(path, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Post(path, headers, std::move(content_provider), content_type, + progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} +inline Result Client::Post(const std::string &path, const Params ¶ms) { + return cli_->Post(path, params); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Post(path, headers, params); +} +inline Result Client::Post(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Post(path, items, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Post(path, headers, items, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Post(path, headers, items, boundary, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Post(path, headers, items, provider_items, progress); +} +inline Result Client::Post(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Post(path, headers, body, content_type, + std::move(content_receiver), progress); +} + +inline Result Client::Put(const std::string &path) { return cli_->Put(path); } +inline Result Client::Put(const std::string &path, const Headers &headers) { + return cli_->Put(path, headers); +} +inline Result Client::Put(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, body, content_length, content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, body, content_length, content_type, progress); +} +inline Result Client::Put(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, body, content_type, progress); +} +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Put(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, std::move(content_provider), content_type, progress); +} +inline Result Client::Put(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, headers, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Put(path, headers, std::move(content_provider), content_type, + progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Put(path, headers, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} +inline Result Client::Put(const std::string &path, const Params ¶ms) { + return cli_->Put(path, params); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Put(path, headers, params); +} +inline Result Client::Put(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Put(path, items, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Put(path, headers, items, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Put(path, headers, items, boundary, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Put(path, headers, items, provider_items, progress); +} +inline Result Client::Put(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Put(path, headers, body, content_type, content_receiver, + progress); +} + +inline Result Client::Patch(const std::string &path) { + return cli_->Patch(path); +} +inline Result Client::Patch(const std::string &path, const Headers &headers) { + return cli_->Patch(path, headers); +} +inline Result Client::Patch(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, body, content_length, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Patch(const std::string &path, const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, body, content_type, progress); +} +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Patch(const std::string &path, size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, std::move(content_provider), content_type, progress); +} +inline Result Client::Patch(const std::string &path, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, headers, content_length, std::move(content_provider), + content_type, std::move(content_receiver), progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + UploadProgress progress) { + return cli_->Patch(path, headers, std::move(content_provider), content_type, + progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + ContentProviderWithoutLength content_provider, + const std::string &content_type, + ContentReceiver content_receiver, + UploadProgress progress) { + return cli_->Patch(path, headers, std::move(content_provider), content_type, + std::move(content_receiver), progress); +} +inline Result Client::Patch(const std::string &path, const Params ¶ms) { + return cli_->Patch(path, params); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Patch(path, headers, params); +} +inline Result Client::Patch(const std::string &path, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Patch(path, items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const std::string &boundary, + UploadProgress progress) { + return cli_->Patch(path, headers, items, boundary, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const UploadFormDataItems &items, + const FormDataProviderItems &provider_items, + UploadProgress progress) { + return cli_->Patch(path, headers, items, provider_items, progress); +} +inline Result Client::Patch(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + ContentReceiver content_receiver, + DownloadProgress progress) { + return cli_->Patch(path, headers, body, content_type, content_receiver, + progress); +} + +inline Result Client::Delete(const std::string &path, + DownloadProgress progress) { + return cli_->Delete(path, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + DownloadProgress progress) { + return cli_->Delete(path, headers, progress); +} +inline Result Client::Delete(const std::string &path, const char *body, + size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, body, content_length, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const char *body, size_t content_length, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, headers, body, content_length, content_type, + progress); +} +inline Result Client::Delete(const std::string &path, const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, body, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const std::string &body, + const std::string &content_type, + DownloadProgress progress) { + return cli_->Delete(path, headers, body, content_type, progress); +} +inline Result Client::Delete(const std::string &path, const Params ¶ms, + DownloadProgress progress) { + return cli_->Delete(path, params, progress); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms, DownloadProgress progress) { + return cli_->Delete(path, headers, params, progress); +} - // Shutdown gracefully if the result seemed successful, non-gracefully if - // the connection appeared to be closed. - const bool shutdown_gracefully = ret; - detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully); - } +inline Result Client::Options(const std::string &path) { + return cli_->Options(path); +} +inline Result Client::Options(const std::string &path, const Headers &headers) { + return cli_->Options(path, headers); +} - detail::shutdown_socket(sock); - detail::close_socket(sock); - return ret; +inline ClientImpl::StreamHandle +Client::open_stream(const std::string &method, const std::string &path, + const Params ¶ms, const Headers &headers, + const std::string &body, const std::string &content_type) { + return cli_->open_stream(method, path, params, headers, body, content_type); } -inline STACK_OF(X509_NAME) * SSLServer::extract_ca_names_from_x509_store( - X509_STORE *store) { - if (!store) { return nullptr; } +inline bool Client::send(Request &req, Response &res, Error &error) { + return cli_->send(req, res, error); +} - auto ca_list = sk_X509_NAME_new_null(); - if (!ca_list) { return nullptr; } +inline Result Client::send(const Request &req) { return cli_->send(req); } - // Get all objects from the store - auto objs = X509_STORE_get0_objects(store); - if (!objs) { - sk_X509_NAME_free(ca_list); - return nullptr; +inline void Client::stop() { cli_->stop(); } + +inline std::string Client::host() const { return cli_->host(); } + +inline int Client::port() const { return cli_->port(); } + +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + +inline socket_t Client::socket() const { return cli_->socket(); } + +inline void +Client::set_hostname_addr_map(std::map addr_map) { + cli_->set_hostname_addr_map(std::move(addr_map)); +} + +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); +} + +inline void Client::set_header_writer( + std::function const &writer) { + cli_->set_header_writer(writer); +} + +inline void Client::set_address_family(int family) { + cli_->set_address_family(family); +} + +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } + +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(std::move(socket_options)); +} + +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); +} + +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); +} + +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); +} + +inline void Client::set_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_basic_auth(username, password); +} +inline void Client::set_bearer_token_auth(const std::string &token) { + cli_->set_bearer_token_auth(token); +} + +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); +} + +inline void Client::set_path_encode(bool on) { cli_->set_path_encode(on); } + +[[deprecated("Use set_path_encode instead")]] +inline void Client::set_url_encode(bool on) { + cli_->set_path_encode(on); +} + +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const std::string &intf) { + cli_->set_interface(intf); +} + +inline void Client::set_proxy(const std::string &host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const std::string &token) { + cli_->set_proxy_bearer_token_auth(token); +} + +inline void Client::set_logger(Logger logger) { + cli_->set_logger(std::move(logger)); +} + +inline void Client::set_error_logger(ErrorLogger error_logger) { + cli_->set_error_logger(std::move(error_logger)); +} + +/* + * Group 6: SSL Server and Client implementation + */ + +#ifdef CPPHTTPLIB_SSL_ENABLED + +// SSL HTTP server implementation +inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, + const char *client_ca_cert_file_path, + const char *client_ca_cert_dir_path, + const char *private_key_password) { + using namespace tls; + + ctx_ = create_server_context(); + if (!ctx_) { return; } + + // Load server certificate and private key + if (!set_server_cert_file(ctx_, cert_path, private_key_path, + private_key_password)) { + last_ssl_error_ = static_cast(get_error()); + free_context(ctx_); + ctx_ = nullptr; + return; } - // Iterate through objects and extract certificate subject names - for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { - auto obj = sk_X509_OBJECT_value(objs, i); - if (X509_OBJECT_get_type(obj) == X509_LU_X509) { - auto cert = X509_OBJECT_get0_X509(obj); - if (cert) { - auto subject = X509_get_subject_name(cert); - if (subject) { - auto name_dup = X509_NAME_dup(subject); - if (name_dup) { sk_X509_NAME_push(ca_list, name_dup); } - } + // Load client CA certificates for client authentication + if (client_ca_cert_file_path || client_ca_cert_dir_path) { + if (!set_client_ca_file(ctx_, client_ca_cert_file_path, + client_ca_cert_dir_path)) { + last_ssl_error_ = static_cast(get_error()); + free_context(ctx_); + ctx_ = nullptr; + return; + } + // Enable client certificate verification + set_verify_client(ctx_, true); + } +} + +inline SSLServer::SSLServer(const PemMemory &pem) { + using namespace tls; + ctx_ = create_server_context(); + if (ctx_) { + if (!set_server_cert_pem(ctx_, pem.cert_pem, pem.key_pem, + pem.private_key_password)) { + last_ssl_error_ = static_cast(get_error()); + free_context(ctx_); + ctx_ = nullptr; + } else if (pem.client_ca_pem && pem.client_ca_pem_len > 0) { + if (!load_ca_pem(ctx_, pem.client_ca_pem, pem.client_ca_pem_len)) { + last_ssl_error_ = static_cast(get_error()); + free_context(ctx_); + ctx_ = nullptr; + } else { + set_verify_client(ctx_, true); } } } +} - // If no names were extracted, free the list and return nullptr - if (sk_X509_NAME_num(ca_list) == 0) { - sk_X509_NAME_free(ca_list); - return nullptr; +inline SSLServer::SSLServer(const tls::ContextSetupCallback &setup_callback) { + using namespace tls; + ctx_ = create_server_context(); + if (ctx_) { + if (!setup_callback(ctx_)) { + free_context(ctx_); + ctx_ = nullptr; + } } +} - return ca_list; +inline SSLServer::~SSLServer() { + if (ctx_) { tls::free_context(ctx_); } } -// SSL HTTP client implementation -inline SSLClient::SSLClient(const std::string &host) - : SSLClient(host, 443, std::string(), std::string()) {} +inline bool SSLServer::is_valid() const { return ctx_ != nullptr; } -inline SSLClient::SSLClient(const std::string &host, int port) - : SSLClient(host, port, std::string(), std::string()) {} +inline bool SSLServer::process_and_close_socket(socket_t sock) { + using namespace tls; -inline SSLClient::SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path, - const std::string &private_key_password) - : ClientImpl(host, port, client_cert_path, client_key_path) { - ctx_ = SSL_CTX_new(TLS_client_method()); + // Create TLS session with mutex protection + session_t session = nullptr; + { + std::lock_guard guard(ctx_mutex_); + session = create_session(static_cast(ctx_), sock); + } - SSL_CTX_set_min_proto_version(ctx_, TLS1_2_VERSION); + if (!session) { + last_ssl_error_ = static_cast(get_error()); + detail::shutdown_socket(sock); + detail::close_socket(sock); + return false; + } - detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { - host_components_.emplace_back(b, e); - }); + // Use scope_exit to ensure cleanup on all paths (including exceptions) + bool handshake_done = false; + bool ret = false; + auto cleanup = detail::scope_exit([&] { + // Shutdown gracefully if handshake succeeded and processing was successful + if (handshake_done) { shutdown(session, ret); } + free_session(session); + detail::shutdown_socket(sock); + detail::close_socket(sock); + }); - if (!client_cert_path.empty() && !client_key_path.empty()) { - if (!private_key_password.empty()) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, reinterpret_cast( - const_cast(private_key_password.c_str()))); - } - - if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(), - SSL_FILETYPE_PEM) != 1 || - SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(), - SSL_FILETYPE_PEM) != 1) { - last_openssl_error_ = ERR_get_error(); - SSL_CTX_free(ctx_); - ctx_ = nullptr; + // Perform TLS accept handshake with timeout + TlsError tls_err; + if (!accept_nonblocking(session, sock, read_timeout_sec_, read_timeout_usec_, + &tls_err)) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // Map TlsError to legacy ssl_error for backward compatibility + if (tls_err.code == ErrorCode::WantRead) { + last_ssl_error_ = SSL_ERROR_WANT_READ; + } else if (tls_err.code == ErrorCode::WantWrite) { + last_ssl_error_ = SSL_ERROR_WANT_WRITE; + } else { + last_ssl_error_ = SSL_ERROR_SSL; } +#else + last_ssl_error_ = static_cast(get_error()); +#endif + return false; } -} -inline SSLClient::SSLClient(const std::string &host, int port, - X509 *client_cert, EVP_PKEY *client_key, - const std::string &private_key_password) - : ClientImpl(host, port) { - ctx_ = SSL_CTX_new(TLS_client_method()); + handshake_done = true; - detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { - host_components_.emplace_back(b, e); - }); + std::string remote_addr; + int remote_port = 0; + detail::get_remote_ip_and_port(sock, remote_addr, remote_port); - if (client_cert != nullptr && client_key != nullptr) { - if (!private_key_password.empty()) { - SSL_CTX_set_default_passwd_cb_userdata( - ctx_, reinterpret_cast( - const_cast(private_key_password.c_str()))); - } + std::string local_addr; + int local_port = 0; + detail::get_local_ip_and_port(sock, local_addr, local_port); - if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || - SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { - last_openssl_error_ = ERR_get_error(); - SSL_CTX_free(ctx_); - ctx_ = nullptr; - } + ret = detail::process_server_socket_ssl( + svr_sock_, session, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [&](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, remote_addr, remote_port, local_addr, + local_port, close_connection, connection_closed, + [&](Request &req) { req.ssl = session; }); + }); + + return ret; +} + +inline bool SSLServer::update_certs_pem(const char *cert_pem, + const char *key_pem, + const char *client_ca_pem, + const char *password) { + if (!ctx_) { return false; } + std::lock_guard guard(ctx_mutex_); + if (!tls::update_server_cert(ctx_, cert_pem, key_pem, password)) { + return false; } + if (client_ca_pem) { + return tls::update_server_client_ca(ctx_, client_ca_pem); + } + return true; } +// SSL HTTP client implementation inline SSLClient::~SSLClient() { - if (ctx_) { SSL_CTX_free(ctx_); } + if (ctx_) { tls::free_context(ctx_); } // Make sure to shut down SSL since shutdown_ssl will resolve to the // base function rather than the derived function once we get to the // base class destructor, and won't free the SSL (causing a leak). shutdown_ssl_impl(socket_, true); } -inline bool SSLClient::is_valid() const { return ctx_; } +inline bool SSLClient::is_valid() const { return ctx_ != nullptr; } -inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (ca_cert_store) { - if (ctx_) { - if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { - // Free memory allocated for old cert and use new store - // `ca_cert_store` - SSL_CTX_set_cert_store(ctx_, ca_cert_store); - ca_cert_store_ = ca_cert_store; - } - } else { - X509_STORE_free(ca_cert_store); +inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { + shutdown_ssl_impl(socket, shutdown_gracefully); +} + +inline void SSLClient::shutdown_ssl_impl(Socket &socket, + bool shutdown_gracefully) { + if (socket.sock == INVALID_SOCKET) { + assert(socket.ssl == nullptr); + return; + } + if (socket.ssl) { + tls::shutdown(socket.ssl, shutdown_gracefully); + { + std::lock_guard guard(ctx_mutex_); + tls::free_session(socket.ssl); } + socket.ssl = nullptr; } + assert(socket.ssl == nullptr); } -inline void SSLClient::load_ca_cert_store(const char *ca_cert, - std::size_t size) { - set_ca_cert_store(ClientImpl::create_ca_cert_store(ca_cert, size)); -} - -inline long SSLClient::get_openssl_verify_result() const { - return verify_result_; +inline bool SSLClient::process_socket( + const Socket &socket, + std::chrono::time_point start_time, + std::function callback) { + assert(socket.ssl); + return detail::process_client_socket_ssl( + socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, + std::move(callback)); } -inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } +inline bool SSLClient::is_ssl() const { return true; } inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { if (!is_valid()) { @@ -13128,1050 +13956,3035 @@ inline bool SSLClient::connect_with_proxy( } } - // If status code is not 200, proxy request is failed. - // Set error to ProxyConnection and return proxy response - // as the response of the request - if (proxy_res.status != StatusCode::OK_200) { - error = Error::ProxyConnection; - output_error_log(error, nullptr); - res = std::move(proxy_res); - // Thread-safe to close everything because we are assuming there are - // no requests in flight - shutdown_ssl(socket, true); - shutdown_socket(socket); - close_socket(socket); - return false; - } + // If status code is not 200, proxy request is failed. + // Set error to ProxyConnection and return proxy response + // as the response of the request + if (proxy_res.status != StatusCode::OK_200) { + error = Error::ProxyConnection; + output_error_log(error, nullptr); + res = std::move(proxy_res); + // Thread-safe to close everything because we are assuming there are + // no requests in flight + shutdown_ssl(socket, true); + shutdown_socket(socket); + close_socket(socket); + return false; + } + + return true; +} + +inline bool SSLClient::ensure_socket_connection(Socket &socket, Error &error) { + if (!ClientImpl::ensure_socket_connection(socket, error)) { return false; } + + if (!proxy_host_.empty() && proxy_port_ != -1) { return true; } + + if (!initialize_ssl(socket, error)) { + shutdown_socket(socket); + close_socket(socket); + return false; + } + + return true; +} + +// SSL HTTP client implementation +inline SSLClient::SSLClient(const std::string &host) + : SSLClient(host, 443, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port) + : SSLClient(host, port, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path, + const std::string &private_key_password) + : ClientImpl(host, port, client_cert_path, client_key_path) { + ctx_ = tls::create_client_context(); + if (!ctx_) { return; } + + tls::set_min_version(ctx_, tls::Version::TLS1_2); + + if (!client_cert_path.empty() && !client_key_path.empty()) { + const char *password = + private_key_password.empty() ? nullptr : private_key_password.c_str(); + if (!tls::set_client_cert_file(ctx_, client_cert_path.c_str(), + client_key_path.c_str(), password)) { + last_backend_error_ = tls::get_error(); + tls::free_context(ctx_); + ctx_ = nullptr; + } + } +} + +inline SSLClient::SSLClient(const std::string &host, int port, + const PemMemory &pem) + : ClientImpl(host, port) { + ctx_ = tls::create_client_context(); + if (!ctx_) { return; } + + tls::set_min_version(ctx_, tls::Version::TLS1_2); + + if (pem.cert_pem && pem.key_pem) { + if (!tls::set_client_cert_pem(ctx_, pem.cert_pem, pem.key_pem, + pem.private_key_password)) { + last_backend_error_ = tls::get_error(); + tls::free_context(ctx_); + ctx_ = nullptr; + } + } +} + +inline void SSLClient::set_ca_cert_store(tls::ca_store_t ca_cert_store) { + if (ca_cert_store && ctx_) { + // set_ca_store takes ownership of ca_cert_store + tls::set_ca_store(ctx_, ca_cert_store); + } else if (ca_cert_store) { + tls::free_ca_store(ca_cert_store); + } +} + +inline void +SSLClient::set_server_certificate_verifier(tls::VerifyCallback verifier) { + if (!ctx_) { return; } + tls::set_verify_callback(ctx_, verifier); +} + +inline void SSLClient::set_session_verifier( + std::function verifier) { + session_verifier_ = std::move(verifier); +} + +inline void SSLClient::load_ca_cert_store(const char *ca_cert, + std::size_t size) { + if (ctx_ && ca_cert && size > 0) { + ca_cert_pem_.assign(ca_cert, size); // Store for redirect transfer + tls::load_ca_pem(ctx_, ca_cert, size); + } +} + +inline bool SSLClient::load_certs() { + auto ret = true; + + std::call_once(initialize_cert_, [&]() { + std::lock_guard guard(ctx_mutex_); + + if (!ca_cert_file_path_.empty()) { + if (!tls::load_ca_file(ctx_, ca_cert_file_path_.c_str())) { + last_backend_error_ = tls::get_error(); + ret = false; + } + } else if (!ca_cert_dir_path_.empty()) { + if (!tls::load_ca_dir(ctx_, ca_cert_dir_path_.c_str())) { + last_backend_error_ = tls::get_error(); + ret = false; + } + } else if (ca_cert_pem_.empty()) { + if (!tls::load_system_certs(ctx_)) { + last_backend_error_ = tls::get_error(); + } + } + }); + + return ret; +} + +inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { + using namespace tls; + + // Load CA certificates if server verification is enabled + if (server_certificate_verification_) { + if (!load_certs()) { + error = Error::SSLLoadingCerts; + output_error_log(error, nullptr); + return false; + } + } + + bool is_ip = detail::is_ip_address(host_); + +#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT + // MbedTLS needs explicit verification mode (OpenSSL uses SSL_VERIFY_NONE + // by default and performs all verification post-handshake). + // For IP addresses with verification enabled, use OPTIONAL mode since + // MbedTLS requires hostname for VERIFY_REQUIRED. + if (is_ip && server_certificate_verification_) { + set_verify_client(ctx_, false); + } else { + set_verify_client(ctx_, server_certificate_verification_); + } +#endif + + // Create TLS session + session_t session = nullptr; + { + std::lock_guard guard(ctx_mutex_); + session = create_session(ctx_, socket.sock); + } + + if (!session) { + error = Error::SSLConnection; + last_backend_error_ = get_error(); + return false; + } + + // Use scope_exit to ensure session is freed on error paths + bool success = false; + auto session_guard = detail::scope_exit([&] { + if (!success) { free_session(session); } + }); + + // Set SNI extension (skip for IP addresses per RFC 6066). + // On MbedTLS, set_sni also enables hostname verification internally. + // On OpenSSL, set_sni only sets SNI; verification is done post-handshake. + if (!is_ip) { + if (!set_sni(session, host_.c_str())) { + error = Error::SSLConnection; + last_backend_error_ = get_error(); + return false; + } + } + + // Perform non-blocking TLS handshake with timeout + TlsError tls_err; + if (!connect_nonblocking(session, socket.sock, connection_timeout_sec_, + connection_timeout_usec_, &tls_err)) { + last_ssl_error_ = static_cast(tls_err.code); + last_backend_error_ = tls_err.backend_code; + if (tls_err.code == ErrorCode::CertVerifyFailed) { + error = Error::SSLServerVerification; + } else if (tls_err.code == ErrorCode::HostnameMismatch) { + error = Error::SSLServerHostnameVerification; + } else { + error = Error::SSLConnection; + } + output_error_log(error, nullptr); + return false; + } + + // Post-handshake session verifier callback + auto verification_status = SSLVerifierResponse::NoDecisionMade; + if (session_verifier_) { verification_status = session_verifier_(session); } + + if (verification_status == SSLVerifierResponse::CertificateRejected) { + last_backend_error_ = get_error(); + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + + // Default server certificate verification + if (verification_status == SSLVerifierResponse::NoDecisionMade && + server_certificate_verification_) { + verify_result_ = tls::get_verify_result(session); + if (verify_result_ != 0) { + last_backend_error_ = static_cast(verify_result_); + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + + auto server_cert = get_peer_cert(session); + if (!server_cert) { + last_backend_error_ = get_error(); + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + auto cert_guard = detail::scope_exit([&] { free_cert(server_cert); }); + + // Hostname verification (post-handshake for all cases). + // On OpenSSL, verification is always post-handshake (SSL_VERIFY_NONE). + // On MbedTLS, set_sni already enabled hostname verification during + // handshake for non-IP hosts, but this check is still needed for IP + // addresses where SNI is not set. + if (server_hostname_verification_) { + if (!verify_hostname(server_cert, host_.c_str())) { + last_backend_error_ = hostname_mismatch_code(); + error = Error::SSLServerHostnameVerification; + output_error_log(error, nullptr); + return false; + } + } + } + + success = true; + socket.ssl = session; + return true; +} + +inline void Client::set_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_digest_auth(username, password); +} + +inline void Client::set_proxy_digest_auth(const std::string &username, + const std::string &password) { + cli_->set_proxy_digest_auth(username, password); +} + +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); +} + +inline void Client::enable_server_hostname_verification(bool enabled) { + cli_->enable_server_hostname_verification(enabled); +} + +inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, + const std::string &ca_cert_dir_path) { + cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +} + +inline void Client::set_ca_cert_store(tls::ca_store_t ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } else if (ca_cert_store) { + tls::free_ca_store(ca_cert_store); + } +} + +inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { + set_ca_cert_store(tls::create_ca_store(ca_cert, size)); +} + +inline void +Client::set_server_certificate_verifier(tls::VerifyCallback verifier) { + if (is_ssl_) { + static_cast(*cli_).set_server_certificate_verifier( + std::move(verifier)); + } +} + +inline void Client::set_session_verifier( + std::function verifier) { + if (is_ssl_) { + static_cast(*cli_).set_session_verifier(std::move(verifier)); + } +} + +inline tls::ctx_t Client::tls_context() const { + if (is_ssl_) { return static_cast(*cli_).tls_context(); } + return nullptr; +} + +#endif // CPPHTTPLIB_SSL_ENABLED + +/* + * Group 7: TLS abstraction layer - Common API + */ + +#ifdef CPPHTTPLIB_SSL_ENABLED + +namespace tls { + +// Helper for PeerCert construction +inline PeerCert get_peer_cert_from_session(const_session_t session) { + return PeerCert(get_peer_cert(session)); +} + +namespace impl { + +inline VerifyCallback &get_verify_callback() { + static thread_local VerifyCallback callback; + return callback; +} + +inline VerifyCallback &get_mbedtls_verify_callback() { + static thread_local VerifyCallback callback; + return callback; +} + +} // namespace impl + +inline bool set_client_ca_file(ctx_t ctx, const char *ca_file, + const char *ca_dir) { + if (!ctx) { return false; } + + bool success = true; + if (ca_file && *ca_file) { + if (!load_ca_file(ctx, ca_file)) { success = false; } + } + if (ca_dir && *ca_dir) { + if (!load_ca_dir(ctx, ca_dir)) { success = false; } + } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // Set CA list for client certificate request (CertificateRequest message) + if (ca_file && *ca_file) { + auto list = SSL_load_client_CA_file(ca_file); + if (list) { SSL_CTX_set_client_CA_list(static_cast(ctx), list); } + } +#endif + + return success; +} + +inline bool set_server_cert_pem(ctx_t ctx, const char *cert, const char *key, + const char *password) { + return set_client_cert_pem(ctx, cert, key, password); +} + +inline bool set_server_cert_file(ctx_t ctx, const char *cert_path, + const char *key_path, const char *password) { + return set_client_cert_file(ctx, cert_path, key_path, password); +} + +// PeerCert implementation +inline PeerCert::PeerCert() = default; + +inline PeerCert::PeerCert(cert_t cert) : cert_(cert) {} + +inline PeerCert::PeerCert(PeerCert &&other) noexcept : cert_(other.cert_) { + other.cert_ = nullptr; +} + +inline PeerCert &PeerCert::operator=(PeerCert &&other) noexcept { + if (this != &other) { + if (cert_) { free_cert(cert_); } + cert_ = other.cert_; + other.cert_ = nullptr; + } + return *this; +} + +inline PeerCert::~PeerCert() { + if (cert_) { free_cert(cert_); } +} + +inline PeerCert::operator bool() const { return cert_ != nullptr; } + +inline std::string PeerCert::subject_cn() const { + return cert_ ? get_cert_subject_cn(cert_) : std::string(); +} + +inline std::string PeerCert::issuer_name() const { + return cert_ ? get_cert_issuer_name(cert_) : std::string(); +} + +inline bool PeerCert::check_hostname(const char *hostname) const { + return cert_ ? verify_hostname(cert_, hostname) : false; +} + +inline std::vector PeerCert::sans() const { + std::vector result; + if (cert_) { get_cert_sans(cert_, result); } + return result; +} + +inline bool PeerCert::validity(time_t ¬_before, time_t ¬_after) const { + return cert_ ? get_cert_validity(cert_, not_before, not_after) : false; +} + +inline std::string PeerCert::serial() const { + return cert_ ? get_cert_serial(cert_) : std::string(); +} + +// VerifyContext method implementations +inline std::string VerifyContext::subject_cn() const { + return cert ? get_cert_subject_cn(cert) : std::string(); +} + +inline std::string VerifyContext::issuer_name() const { + return cert ? get_cert_issuer_name(cert) : std::string(); +} - return true; +inline bool VerifyContext::check_hostname(const char *hostname) const { + return cert ? verify_hostname(cert, hostname) : false; } -inline bool SSLClient::load_certs() { - auto ret = true; +inline std::vector VerifyContext::sans() const { + std::vector result; + if (cert) { get_cert_sans(cert, result); } + return result; +} - std::call_once(initialize_cert_, [&]() { - std::lock_guard guard(ctx_mutex_); - if (!ca_cert_file_path_.empty()) { - if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), - nullptr)) { - last_openssl_error_ = ERR_get_error(); - ret = false; - } - } else if (!ca_cert_dir_path_.empty()) { - if (!SSL_CTX_load_verify_locations(ctx_, nullptr, - ca_cert_dir_path_.c_str())) { - last_openssl_error_ = ERR_get_error(); - ret = false; - } - } else if (!ca_cert_store_) { - auto loaded = false; -#ifdef _WIN32 - loaded = - detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); -#elif defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) && TARGET_OS_MAC - loaded = detail::load_system_certs_on_macos(SSL_CTX_get_cert_store(ctx_)); -#endif // _WIN32 - if (!loaded) { SSL_CTX_set_default_verify_paths(ctx_); } - } - }); +inline bool VerifyContext::validity(time_t ¬_before, + time_t ¬_after) const { + return cert ? get_cert_validity(cert, not_before, not_after) : false; +} - return ret; +inline std::string VerifyContext::serial() const { + return cert ? get_cert_serial(cert) : std::string(); } -inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { - auto ssl = detail::ssl_new( - socket.sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { - if (server_certificate_verification_) { - if (!load_certs()) { - error = Error::SSLLoadingCerts; - output_error_log(error, nullptr); - return false; - } - SSL_set_verify(ssl2, SSL_VERIFY_NONE, nullptr); - } +// TlsError static method implementation +inline std::string TlsError::verify_error_to_string(long error_code) { + return verify_error_string(error_code); +} - if (!detail::ssl_connect_or_accept_nonblocking( - socket.sock, ssl2, SSL_connect, connection_timeout_sec_, - connection_timeout_usec_, &last_ssl_error_)) { - error = Error::SSLConnection; - output_error_log(error, nullptr); - return false; - } +} // namespace tls - if (server_certificate_verification_) { - auto verification_status = SSLVerifierResponse::NoDecisionMade; +// Request::peer_cert() implementation +inline tls::PeerCert Request::peer_cert() const { + return tls::get_peer_cert_from_session(ssl); +} - if (server_certificate_verifier_) { - verification_status = server_certificate_verifier_(ssl2); - } +// Request::sni() implementation +inline std::string Request::sni() const { + if (!ssl) { return std::string(); } + const char *s = tls::get_sni(ssl); + return s ? std::string(s) : std::string(); +} - if (verification_status == SSLVerifierResponse::CertificateRejected) { - last_openssl_error_ = ERR_get_error(); - error = Error::SSLServerVerification; - output_error_log(error, nullptr); - return false; - } +#endif // CPPHTTPLIB_SSL_ENABLED - if (verification_status == SSLVerifierResponse::NoDecisionMade) { - verify_result_ = SSL_get_verify_result(ssl2); +/* + * Group 8: TLS abstraction layer - OpenSSL backend + */ - if (verify_result_ != X509_V_OK) { - last_openssl_error_ = static_cast(verify_result_); - error = Error::SSLServerVerification; - output_error_log(error, nullptr); - return false; - } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; +} - auto server_cert = SSL_get1_peer_certificate(ssl2); - auto se = detail::scope_exit([&] { X509_free(server_cert); }); +inline void Client::set_server_certificate_verifier( + std::function verifier) { + cli_->set_server_certificate_verifier(verifier); +} - if (server_cert == nullptr) { - last_openssl_error_ = ERR_get_error(); - error = Error::SSLServerVerification; - output_error_log(error, nullptr); - return false; - } +inline long Client::get_verify_result() const { + if (is_ssl_) { return static_cast(*cli_).get_verify_result(); } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +} +#endif // CPPHTTPLIB_OPENSSL_SUPPORT - if (server_hostname_verification_) { - if (!verify_host(server_cert)) { - last_openssl_error_ = X509_V_ERR_HOSTNAME_MISMATCH; - error = Error::SSLServerHostnameVerification; - output_error_log(error, nullptr); - return false; - } - } - } - } +/* + * OpenSSL Backend Implementation + */ - return true; - }, - [&](SSL *ssl2) { - // Set SNI only if host is not IP address - if (!detail::is_ip_address(host_)) { -#if defined(OPENSSL_IS_BORINGSSL) - SSL_set_tlsext_host_name(ssl2, host_.c_str()); -#else - // NOTE: Direct call instead of using the OpenSSL macro to suppress - // -Wold-style-cast warning - SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, - TLSEXT_NAMETYPE_host_name, - static_cast(const_cast(host_.c_str()))); -#endif - } - return true; - }); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +namespace tls { + +namespace impl { + +// OpenSSL-specific helpers for converting native types to PEM +inline std::string x509_to_pem(X509 *cert) { + if (!cert) return {}; + BIO *bio = BIO_new(BIO_s_mem()); + if (!bio) return {}; + if (PEM_write_bio_X509(bio, cert) != 1) { + BIO_free(bio); + return {}; + } + char *data = nullptr; + long len = BIO_get_mem_data(bio, &data); + std::string pem(data, static_cast(len)); + BIO_free(bio); + return pem; +} + +inline std::string evp_pkey_to_pem(EVP_PKEY *key) { + if (!key) return {}; + BIO *bio = BIO_new(BIO_s_mem()); + if (!bio) return {}; + if (PEM_write_bio_PrivateKey(bio, key, nullptr, nullptr, 0, nullptr, + nullptr) != 1) { + BIO_free(bio); + return {}; + } + char *data = nullptr; + long len = BIO_get_mem_data(bio, &data); + std::string pem(data, static_cast(len)); + BIO_free(bio); + return pem; +} + +inline std::string x509_store_to_pem(X509_STORE *store) { + if (!store) return {}; + std::string pem; + auto objs = X509_STORE_get0_objects(store); + if (!objs) return {}; + for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { + auto obj = sk_X509_OBJECT_value(objs, i); + if (X509_OBJECT_get_type(obj) == X509_LU_X509) { + auto cert = X509_OBJECT_get0_X509(obj); + if (cert) { pem += x509_to_pem(cert); } + } + } + return pem; +} - if (ssl) { - socket.ssl = ssl; - return true; +// Helper to map OpenSSL SSL_get_error to ErrorCode +inline ErrorCode map_ssl_error(int ssl_error, int &out_errno) { + switch (ssl_error) { + case SSL_ERROR_NONE: return ErrorCode::Success; + case SSL_ERROR_WANT_READ: return ErrorCode::WantRead; + case SSL_ERROR_WANT_WRITE: return ErrorCode::WantWrite; + case SSL_ERROR_ZERO_RETURN: return ErrorCode::PeerClosed; + case SSL_ERROR_SYSCALL: out_errno = errno; return ErrorCode::SyscallError; + case SSL_ERROR_SSL: + default: return ErrorCode::Fatal; } +} - if (ctx_ == nullptr) { - error = Error::SSLConnection; - last_openssl_error_ = ERR_get_error(); +// Helper: Create client CA list from PEM string +// Returns a new STACK_OF(X509_NAME)* or nullptr on failure +// Caller takes ownership of returned list +inline STACK_OF(X509_NAME) * + create_client_ca_list_from_pem(const char *ca_pem) { + if (!ca_pem) { return nullptr; } + + auto ca_list = sk_X509_NAME_new_null(); + if (!ca_list) { return nullptr; } + + BIO *bio = BIO_new_mem_buf(ca_pem, -1); + if (!bio) { + sk_X509_NAME_pop_free(ca_list, X509_NAME_free); + return nullptr; } - shutdown_socket(socket); - close_socket(socket); - return false; -} + X509 *cert = nullptr; + while ((cert = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr)) != + nullptr) { + X509_NAME *name = X509_get_subject_name(cert); + if (name) { sk_X509_NAME_push(ca_list, X509_NAME_dup(name)); } + X509_free(cert); + } + BIO_free(bio); -inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { - shutdown_ssl_impl(socket, shutdown_gracefully); + return ca_list; } -inline void SSLClient::shutdown_ssl_impl(Socket &socket, - bool shutdown_gracefully) { - if (socket.sock == INVALID_SOCKET) { - assert(socket.ssl == nullptr); - return; +// Helper: Extract CA names from X509_STORE +// Returns a new STACK_OF(X509_NAME)* or nullptr on failure +// Caller takes ownership of returned list +inline STACK_OF(X509_NAME) * + extract_client_ca_list_from_store(X509_STORE *store) { + if (!store) { return nullptr; } + + auto ca_list = sk_X509_NAME_new_null(); + if (!ca_list) { return nullptr; } + + auto objs = X509_STORE_get0_objects(store); + if (!objs) { + sk_X509_NAME_free(ca_list); + return nullptr; } - if (socket.ssl) { - detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock, - shutdown_gracefully); - socket.ssl = nullptr; + + for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { + auto obj = sk_X509_OBJECT_value(objs, i); + if (X509_OBJECT_get_type(obj) == X509_LU_X509) { + auto cert = X509_OBJECT_get0_X509(obj); + if (cert) { + auto subject = X509_get_subject_name(cert); + if (subject) { + auto name_dup = X509_NAME_dup(subject); + if (name_dup) { sk_X509_NAME_push(ca_list, name_dup); } + } + } + } } - assert(socket.ssl == nullptr); -} -inline bool SSLClient::process_socket( - const Socket &socket, - std::chrono::time_point start_time, - std::function callback) { - assert(socket.ssl); - return detail::process_client_socket_ssl( - socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, max_timeout_msec_, start_time, - std::move(callback)); + if (sk_X509_NAME_num(ca_list) == 0) { + sk_X509_NAME_free(ca_list); + return nullptr; + } + + return ca_list; } -inline bool SSLClient::is_ssl() const { return true; } +// OpenSSL verify callback wrapper +inline int openssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx) { + auto &callback = get_verify_callback(); + if (!callback) { return preverify_ok; } -inline bool SSLClient::verify_host(X509 *server_cert) const { - /* Quote from RFC2818 section 3.1 "Server Identity" + // Get SSL object from X509_STORE_CTX + auto ssl = static_cast( + X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); + if (!ssl) { return preverify_ok; } - If a subjectAltName extension of type dNSName is present, that MUST - be used as the identity. Otherwise, the (most specific) Common Name - field in the Subject field of the certificate MUST be used. Although - the use of the Common Name is existing practice, it is deprecated and - Certification Authorities are encouraged to use the dNSName instead. + // Get current certificate and depth + auto cert = X509_STORE_CTX_get_current_cert(ctx); + int depth = X509_STORE_CTX_get_error_depth(ctx); + int error = X509_STORE_CTX_get_error(ctx); - Matching is performed using the matching rules specified by - [RFC2459]. If more than one identity of a given type is present in - the certificate (e.g., more than one dNSName name, a match in any one - of the set is considered acceptable.) Names may contain the wildcard - character * which is considered to match any single domain name - component or component fragment. E.g., *.a.com matches foo.a.com but - not bar.foo.a.com. f*.com matches foo.com but not bar.com. + // Build context + VerifyContext verify_ctx; + verify_ctx.session = static_cast(ssl); + verify_ctx.cert = static_cast(cert); + verify_ctx.depth = depth; + verify_ctx.preverify_ok = (preverify_ok != 0); + verify_ctx.error_code = error; + verify_ctx.error_string = + (error != X509_V_OK) ? X509_verify_cert_error_string(error) : nullptr; - In some cases, the URI is specified as an IP address rather than a - hostname. In this case, the iPAddress subjectAltName must be present - in the certificate and must exactly match the IP in the URI. + return callback(verify_ctx) ? 1 : 0; +} - */ - return verify_host_with_subject_alt_name(server_cert) || - verify_host_with_common_name(server_cert); +} // namespace impl + +inline ctx_t create_client_context() { + SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); + if (ctx) { + // Disable auto-retry to properly handle non-blocking I/O + SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY); + // Set minimum TLS version + SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); + } + return static_cast(ctx); } -inline bool -SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { - auto ret = false; +inline void free_context(ctx_t ctx) { + if (ctx) { SSL_CTX_free(static_cast(ctx)); } +} - auto type = GEN_DNS; +inline bool set_min_version(ctx_t ctx, Version version) { + if (!ctx) return false; + return SSL_CTX_set_min_proto_version(static_cast(ctx), + static_cast(version)) == 1; +} - struct in6_addr addr6 = {}; - struct in_addr addr = {}; - size_t addr_len = 0; +inline bool load_ca_pem(ctx_t ctx, const char *pem, size_t len) { + if (!ctx || !pem || len == 0) return false; -#ifndef __MINGW32__ - if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { - type = GEN_IPADD; - addr_len = sizeof(struct in6_addr); - } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { - type = GEN_IPADD; - addr_len = sizeof(struct in_addr); + auto ssl_ctx = static_cast(ctx); + auto store = SSL_CTX_get_cert_store(ssl_ctx); + if (!store) return false; + + auto bio = BIO_new_mem_buf(pem, static_cast(len)); + if (!bio) return false; + + bool ok = true; + X509 *cert = nullptr; + while ((cert = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr)) != + nullptr) { + if (X509_STORE_add_cert(store, cert) != 1) { + // Ignore duplicate errors + auto err = ERR_peek_last_error(); + if (ERR_GET_REASON(err) != X509_R_CERT_ALREADY_IN_HASH_TABLE) { + ok = false; + } + } + X509_free(cert); + if (!ok) break; } -#endif + BIO_free(bio); - auto alt_names = static_cast( - X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); + // Clear any "no more certificates" errors + ERR_clear_error(); + return ok; +} - if (alt_names) { - auto dsn_matched = false; - auto ip_matched = false; +inline bool load_ca_file(ctx_t ctx, const char *file_path) { + if (!ctx || !file_path) return false; + return SSL_CTX_load_verify_locations(static_cast(ctx), file_path, + nullptr) == 1; +} - auto count = sk_GENERAL_NAME_num(alt_names); +inline bool load_ca_dir(ctx_t ctx, const char *dir_path) { + if (!ctx || !dir_path) return false; + return SSL_CTX_load_verify_locations(static_cast(ctx), nullptr, + dir_path) == 1; +} - for (decltype(count) i = 0; i < count && !dsn_matched; i++) { - auto val = sk_GENERAL_NAME_value(alt_names, i); - if (!val || val->type != type) { continue; } +inline bool load_system_certs(ctx_t ctx) { + if (!ctx) return false; + auto ssl_ctx = static_cast(ctx); - auto name = - reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); - if (name == nullptr) { continue; } +#ifdef _WIN32 + // Windows: Load from system certificate store + auto store = SSL_CTX_get_cert_store(ssl_ctx); + if (!store) return false; - auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); + auto hStore = CertOpenSystemStoreW(NULL, L"ROOT"); + if (!hStore) return false; - switch (type) { - case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + bool loaded_any = false; + PCCERT_CONTEXT pContext = nullptr; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + const unsigned char *data = pContext->pbCertEncoded; + auto x509 = d2i_X509(nullptr, &data, pContext->cbCertEncoded); + if (x509) { + if (X509_STORE_add_cert(store, x509) == 1) { loaded_any = true; } + X509_free(x509); + } + } + CertCloseStore(hStore, 0); + return loaded_any; - case GEN_IPADD: - if (!memcmp(&addr6, name, addr_len) || !memcmp(&addr, name, addr_len)) { - ip_matched = true; - } - break; +#elif defined(__APPLE__) +#ifdef CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN + // macOS: Load from Keychain + auto store = SSL_CTX_get_cert_store(ssl_ctx); + if (!store) return false; + + CFArrayRef certs = nullptr; + if (SecTrustCopyAnchorCertificates(&certs) != errSecSuccess || !certs) { + return SSL_CTX_set_default_verify_paths(ssl_ctx) == 1; + } + + bool loaded_any = false; + auto count = CFArrayGetCount(certs); + for (CFIndex i = 0; i < count; i++) { + auto cert = reinterpret_cast( + const_cast(CFArrayGetValueAtIndex(certs, i))); + CFDataRef der = SecCertificateCopyData(cert); + if (der) { + const unsigned char *data = CFDataGetBytePtr(der); + auto x509 = d2i_X509(nullptr, &data, CFDataGetLength(der)); + if (x509) { + if (X509_STORE_add_cert(store, x509) == 1) { loaded_any = true; } + X509_free(x509); } + CFRelease(der); } - - if (dsn_matched || ip_matched) { ret = true; } } + CFRelease(certs); + return loaded_any || SSL_CTX_set_default_verify_paths(ssl_ctx) == 1; +#else + return SSL_CTX_set_default_verify_paths(ssl_ctx) == 1; +#endif - GENERAL_NAMES_free(const_cast( - reinterpret_cast(alt_names))); - return ret; +#else + // Other Unix: use default verify paths + return SSL_CTX_set_default_verify_paths(ssl_ctx) == 1; +#endif } -inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { - const auto subject_name = X509_get_subject_name(server_cert); +inline bool set_client_cert_pem(ctx_t ctx, const char *cert, const char *key, + const char *password) { + if (!ctx || !cert || !key) return false; - if (subject_name != nullptr) { - char name[BUFSIZ]; - auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, - name, sizeof(name)); + auto ssl_ctx = static_cast(ctx); + + // Load certificate + auto cert_bio = BIO_new_mem_buf(cert, -1); + if (!cert_bio) return false; + + auto x509 = PEM_read_bio_X509(cert_bio, nullptr, nullptr, nullptr); + BIO_free(cert_bio); + if (!x509) return false; + + auto cert_ok = SSL_CTX_use_certificate(ssl_ctx, x509) == 1; + X509_free(x509); + if (!cert_ok) return false; + + // Load private key + auto key_bio = BIO_new_mem_buf(key, -1); + if (!key_bio) return false; + + auto pkey = PEM_read_bio_PrivateKey(key_bio, nullptr, nullptr, + password ? const_cast(password) + : nullptr); + BIO_free(key_bio); + if (!pkey) return false; + + auto key_ok = SSL_CTX_use_PrivateKey(ssl_ctx, pkey) == 1; + EVP_PKEY_free(pkey); + + return key_ok && SSL_CTX_check_private_key(ssl_ctx) == 1; +} + +inline bool set_client_cert_file(ctx_t ctx, const char *cert_path, + const char *key_path, const char *password) { + if (!ctx || !cert_path || !key_path) return false; + + auto ssl_ctx = static_cast(ctx); - if (name_len != -1) { - return check_host_name(name, static_cast(name_len)); - } + if (password && password[0] != '\0') { + SSL_CTX_set_default_passwd_cb_userdata( + ssl_ctx, reinterpret_cast(const_cast(password))); } - return false; + return SSL_CTX_use_certificate_chain_file(ssl_ctx, cert_path) == 1 && + SSL_CTX_use_PrivateKey_file(ssl_ctx, key_path, SSL_FILETYPE_PEM) == 1; } -inline bool SSLClient::check_host_name(const char *pattern, - size_t pattern_len) const { - // Exact match (case-insensitive) - if (host_.size() == pattern_len && - detail::case_ignore::equal(host_, std::string(pattern, pattern_len))) { - return true; +inline ctx_t create_server_context() { + SSL_CTX *ctx = SSL_CTX_new(TLS_server_method()); + if (ctx) { + SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); } + return static_cast(ctx); +} - // Wildcard match - // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 - std::vector pattern_components; - detail::split(&pattern[0], &pattern[pattern_len], '.', - [&](const char *b, const char *e) { - pattern_components.emplace_back(b, e); - }); +inline void set_verify_client(ctx_t ctx, bool require) { + if (!ctx) return; + SSL_CTX_set_verify(static_cast(ctx), + require + ? (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT) + : SSL_VERIFY_NONE, + nullptr); +} - if (host_components_.size() != pattern_components.size()) { return false; } +inline session_t create_session(ctx_t ctx, socket_t sock) { + if (!ctx || sock == INVALID_SOCKET) return nullptr; - auto itr = pattern_components.begin(); - for (const auto &h : host_components_) { - auto &p = *itr; - if (!httplib::detail::case_ignore::equal(p, h) && p != "*") { - bool partial_match = false; - if (!p.empty() && p[p.size() - 1] == '*') { - const auto prefix_length = p.size() - 1; - if (prefix_length == 0) { - partial_match = true; - } else if (h.size() >= prefix_length) { - partial_match = - std::equal(p.begin(), - p.begin() + static_cast( - prefix_length), - h.begin(), [](const char ca, const char cb) { - return httplib::detail::case_ignore::to_lower(ca) == - httplib::detail::case_ignore::to_lower(cb); - }); - } - } - if (!partial_match) { return false; } - } - ++itr; + auto ssl_ctx = static_cast(ctx); + SSL *ssl = SSL_new(ssl_ctx); + if (!ssl) return nullptr; + + // Disable auto-retry for proper non-blocking I/O handling + SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY); + + auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); + if (!bio) { + SSL_free(ssl); + return nullptr; } - return true; + SSL_set_bio(ssl, bio, bio); + return static_cast(ssl); } -#endif -// Universal client implementation -inline Client::Client(const std::string &scheme_host_port) - : Client(scheme_host_port, std::string(), std::string()) {} +inline void free_session(session_t session) { + if (session) { SSL_free(static_cast(session)); } +} -inline Client::Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path) { - const static std::regex re( - R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); +inline bool set_sni(session_t session, const char *hostname) { + if (!session || !hostname) return false; - std::smatch m; - if (std::regex_match(scheme_host_port, m, re)) { - auto scheme = m[1].str(); + auto ssl = static_cast(session); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (!scheme.empty() && (scheme != "http" && scheme != "https")) { + // Set SNI (Server Name Indication) only - does not enable verification +#if defined(OPENSSL_IS_BORINGSSL) + return SSL_set_tlsext_host_name(ssl, hostname) == 1; #else - if (!scheme.empty() && scheme != "http") { -#endif -#ifndef CPPHTTPLIB_NO_EXCEPTIONS - std::string msg = "'" + scheme + "' scheme is not supported."; - throw std::invalid_argument(msg); + // Direct call instead of macro to suppress -Wold-style-cast warning + return SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, + static_cast(const_cast(hostname))) == 1; #endif - return; - } +} - auto is_ssl = scheme == "https"; +inline bool set_hostname(session_t session, const char *hostname) { + if (!session || !hostname) return false; - auto host = m[2].str(); - if (host.empty()) { host = m[3].str(); } + auto ssl = static_cast(session); - auto port_str = m[4].str(); - auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); + // Set SNI (Server Name Indication) + if (!set_sni(session, hostname)) { return false; } - if (is_ssl) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - cli_ = detail::make_unique(host, port, client_cert_path, - client_key_path); - is_ssl_ = is_ssl; -#endif - } else { - cli_ = detail::make_unique(host, port, client_cert_path, - client_key_path); - } - } else { - // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) - // if port param below changes. - cli_ = detail::make_unique(scheme_host_port, 80, - client_cert_path, client_key_path); - } -} // namespace detail + // Enable hostname verification + auto param = SSL_get0_param(ssl); + if (!param) return false; -inline Client::Client(const std::string &host, int port) - : cli_(detail::make_unique(host, port)) {} + X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); + if (X509_VERIFY_PARAM_set1_host(param, hostname, 0) != 1) { return false; } -inline Client::Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) - : cli_(detail::make_unique(host, port, client_cert_path, - client_key_path)) {} + SSL_set_verify(ssl, SSL_VERIFY_PEER, nullptr); + return true; +} -inline Client::~Client() = default; +inline TlsError connect(session_t session) { + if (!session) { return TlsError(); } -inline bool Client::is_valid() const { - return cli_ != nullptr && cli_->is_valid(); -} + auto ssl = static_cast(session); + auto ret = SSL_connect(ssl); -inline Result Client::Get(const std::string &path, DownloadProgress progress) { - return cli_->Get(path, std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - DownloadProgress progress) { - return cli_->Get(path, headers, std::move(progress)); -} -inline Result Client::Get(const std::string &path, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Get(path, std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Get(path, headers, std::move(content_receiver), - std::move(progress)); -} -inline Result Client::Get(const std::string &path, - ResponseHandler response_handler, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Get(path, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Get(path, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, DownloadProgress progress) { - return cli_->Get(path, params, headers, std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Get(path, params, headers, std::move(content_receiver), - std::move(progress)); -} -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Get(path, params, headers, std::move(response_handler), - std::move(content_receiver), std::move(progress)); + TlsError err; + if (ret == 1) { + err.code = ErrorCode::Success; + } else { + auto ssl_err = SSL_get_error(ssl, ret); + err.code = impl::map_ssl_error(ssl_err, err.sys_errno); + err.backend_code = ERR_get_error(); + } + return err; } -inline Result Client::Head(const std::string &path) { return cli_->Head(path); } -inline Result Client::Head(const std::string &path, const Headers &headers) { - return cli_->Head(path, headers); -} +inline TlsError accept(session_t session) { + if (!session) { return TlsError(); } -inline Result Client::Post(const std::string &path) { return cli_->Post(path); } -inline Result Client::Post(const std::string &path, const Headers &headers) { - return cli_->Post(path, headers); -} -inline Result Client::Post(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, body, content_length, content_type, progress); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, headers, body, content_length, content_type, - progress); -} -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, body, content_type, progress); -} -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, headers, body, content_type, progress); -} -inline Result Client::Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, content_length, std::move(content_provider), - content_type, progress); -} -inline Result Client::Post(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Post(path, content_length, std::move(content_provider), - content_type, std::move(content_receiver), progress); + auto ssl = static_cast(session); + auto ret = SSL_accept(ssl); + + TlsError err; + if (ret == 1) { + err.code = ErrorCode::Success; + } else { + auto ssl_err = SSL_get_error(ssl, ret); + err.code = impl::map_ssl_error(ssl_err, err.sys_errno); + err.backend_code = ERR_get_error(); + } + return err; } -inline Result Client::Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, std::move(content_provider), content_type, progress); + +inline bool connect_nonblocking(session_t session, socket_t sock, + time_t timeout_sec, time_t timeout_usec, + TlsError *err) { + if (!session) { + if (err) { err->code = ErrorCode::Fatal; } + return false; + } + + auto ssl = static_cast(session); + auto bio = SSL_get_rbio(ssl); + + // Set non-blocking mode for handshake + detail::set_nonblocking(sock, true); + if (bio) { BIO_set_nbio(bio, 1); } + + auto cleanup = detail::scope_exit([&]() { + // Restore blocking mode after handshake + if (bio) { BIO_set_nbio(bio, 0); } + detail::set_nonblocking(sock, false); + }); + + auto res = 0; + while ((res = SSL_connect(ssl)) != 1) { + auto ssl_err = SSL_get_error(ssl, res); + switch (ssl_err) { + case SSL_ERROR_WANT_READ: + if (detail::select_read(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + break; + case SSL_ERROR_WANT_WRITE: + if (detail::select_write(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + break; + default: break; + } + if (err) { + err->code = impl::map_ssl_error(ssl_err, err->sys_errno); + err->backend_code = ERR_get_error(); + } + return false; + } + if (err) { err->code = ErrorCode::Success; } + return true; } -inline Result Client::Post(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Post(path, std::move(content_provider), content_type, - std::move(content_receiver), progress); + +inline bool accept_nonblocking(session_t session, socket_t sock, + time_t timeout_sec, time_t timeout_usec, + TlsError *err) { + if (!session) { + if (err) { err->code = ErrorCode::Fatal; } + return false; + } + + auto ssl = static_cast(session); + auto bio = SSL_get_rbio(ssl); + + // Set non-blocking mode for handshake + detail::set_nonblocking(sock, true); + if (bio) { BIO_set_nbio(bio, 1); } + + auto cleanup = detail::scope_exit([&]() { + // Restore blocking mode after handshake + if (bio) { BIO_set_nbio(bio, 0); } + detail::set_nonblocking(sock, false); + }); + + auto res = 0; + while ((res = SSL_accept(ssl)) != 1) { + auto ssl_err = SSL_get_error(ssl, res); + switch (ssl_err) { + case SSL_ERROR_WANT_READ: + if (detail::select_read(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + break; + case SSL_ERROR_WANT_WRITE: + if (detail::select_write(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + break; + default: break; + } + if (err) { + err->code = impl::map_ssl_error(ssl_err, err->sys_errno); + err->backend_code = ERR_get_error(); + } + return false; + } + if (err) { err->code = ErrorCode::Success; } + return true; } -inline Result Client::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, headers, content_length, std::move(content_provider), - content_type, progress); + +inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) { + if (!session || !buf) { + err.code = ErrorCode::Fatal; + return -1; + } + + auto ssl = static_cast(session); + auto ret = SSL_read(ssl, buf, static_cast(len)); + + if (ret > 0) { + err.code = ErrorCode::Success; + return ret; + } + + auto ssl_err = SSL_get_error(ssl, ret); + err.code = impl::map_ssl_error(ssl_err, err.sys_errno); + if (err.code == ErrorCode::Fatal) { err.backend_code = ERR_get_error(); } + return -1; } -inline Result Client::Post(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Post(path, headers, content_length, std::move(content_provider), - content_type, std::move(content_receiver), progress); + +inline ssize_t write(session_t session, const void *buf, size_t len, + TlsError &err) { + if (!session || !buf) { + err.code = ErrorCode::Fatal; + return -1; + } + + auto ssl = static_cast(session); + auto ret = SSL_write(ssl, buf, static_cast(len)); + + if (ret > 0) { + err.code = ErrorCode::Success; + return ret; + } + + auto ssl_err = SSL_get_error(ssl, ret); + err.code = impl::map_ssl_error(ssl_err, err.sys_errno); + if (err.code == ErrorCode::Fatal) { err.backend_code = ERR_get_error(); } + return -1; } -inline Result Client::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Post(path, headers, std::move(content_provider), content_type, - progress); + +inline int pending(const_session_t session) { + if (!session) return 0; + return SSL_pending(static_cast(const_cast(session))); } -inline Result Client::Post(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Post(path, headers, std::move(content_provider), content_type, - std::move(content_receiver), progress); + +inline void shutdown(session_t session, bool graceful) { + if (!session) return; + + auto ssl = static_cast(session); + if (graceful) { + // First call sends close_notify + if (SSL_shutdown(ssl) == 0) { + // Second call waits for peer's close_notify + SSL_shutdown(ssl); + } + } } -inline Result Client::Post(const std::string &path, const Params ¶ms) { - return cli_->Post(path, params); + +inline bool is_peer_closed(session_t session, socket_t sock) { + if (!session) return true; + + // Temporarily set socket to non-blocking to avoid blocking on SSL_peek + detail::set_nonblocking(sock, true); + auto se = detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); + + auto ssl = static_cast(session); + char buf; + auto ret = SSL_peek(ssl, &buf, 1); + if (ret > 0) return false; + + auto err = SSL_get_error(ssl, ret); + return err == SSL_ERROR_ZERO_RETURN; } -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Post(path, headers, params); + +inline cert_t get_peer_cert(const_session_t session) { + if (!session) return nullptr; + return static_cast(SSL_get1_peer_certificate( + static_cast(const_cast(session)))); } -inline Result Client::Post(const std::string &path, - const UploadFormDataItems &items, - UploadProgress progress) { - return cli_->Post(path, items, progress); + +inline void free_cert(cert_t cert) { + if (cert) { X509_free(static_cast(cert)); } } -inline Result Client::Post(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - UploadProgress progress) { - return cli_->Post(path, headers, items, progress); + +inline bool verify_hostname(cert_t cert, const char *hostname) { + if (!cert || !hostname) return false; + + auto x509 = static_cast(cert); + + // Use X509_check_ip_asc for IP addresses, X509_check_host for DNS names + if (detail::is_ip_address(hostname)) { + return X509_check_ip_asc(x509, hostname, 0) == 1; + } + return X509_check_host(x509, hostname, strlen(hostname), 0, nullptr) == 1; } -inline Result Client::Post(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const std::string &boundary, - UploadProgress progress) { - return cli_->Post(path, headers, items, boundary, progress); + +inline uint64_t hostname_mismatch_code() { + return static_cast(X509_V_ERR_HOSTNAME_MISMATCH); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const FormDataProviderItems &provider_items, - UploadProgress progress) { - return cli_->Post(path, headers, items, provider_items, progress); + +inline long get_verify_result(const_session_t session) { + if (!session) return X509_V_ERR_UNSPECIFIED; + return SSL_get_verify_result(static_cast(const_cast(session))); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Post(path, headers, body, content_type, - std::move(content_receiver), progress); + +inline std::string get_cert_subject_cn(cert_t cert) { + if (!cert) return ""; + auto x509 = static_cast(cert); + auto subject_name = X509_get_subject_name(x509); + if (!subject_name) return ""; + + char buf[256]; + auto len = + X509_NAME_get_text_by_NID(subject_name, NID_commonName, buf, sizeof(buf)); + if (len < 0) return ""; + return std::string(buf, static_cast(len)); } -inline Result Client::Put(const std::string &path) { return cli_->Put(path); } -inline Result Client::Put(const std::string &path, const Headers &headers) { - return cli_->Put(path, headers); +inline std::string get_cert_issuer_name(cert_t cert) { + if (!cert) return ""; + auto x509 = static_cast(cert); + auto issuer_name = X509_get_issuer_name(x509); + if (!issuer_name) return ""; + + char buf[256]; + X509_NAME_oneline(issuer_name, buf, sizeof(buf)); + return std::string(buf); } -inline Result Client::Put(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, body, content_length, content_type, progress); + +inline bool get_cert_sans(cert_t cert, std::vector &sans) { + sans.clear(); + if (!cert) return false; + auto x509 = static_cast(cert); + + auto names = static_cast( + X509_get_ext_d2i(x509, NID_subject_alt_name, nullptr, nullptr)); + if (!names) return true; // No SANs is valid + + auto count = sk_GENERAL_NAME_num(names); + for (int i = 0; i < count; i++) { + auto gen = sk_GENERAL_NAME_value(names, i); + if (!gen) continue; + + SanEntry entry; + switch (gen->type) { + case GEN_DNS: + entry.type = SanType::DNS; + if (gen->d.dNSName) { + entry.value = std::string( + reinterpret_cast( + ASN1_STRING_get0_data(gen->d.dNSName)), + static_cast(ASN1_STRING_length(gen->d.dNSName))); + } + break; + case GEN_IPADD: + entry.type = SanType::IP; + if (gen->d.iPAddress) { + auto data = ASN1_STRING_get0_data(gen->d.iPAddress); + auto len = ASN1_STRING_length(gen->d.iPAddress); + if (len == 4) { + // IPv4 + char buf[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, data, buf, sizeof(buf)); + entry.value = buf; + } else if (len == 16) { + // IPv6 + char buf[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, data, buf, sizeof(buf)); + entry.value = buf; + } + } + break; + case GEN_EMAIL: + entry.type = SanType::EMAIL; + if (gen->d.rfc822Name) { + entry.value = std::string( + reinterpret_cast( + ASN1_STRING_get0_data(gen->d.rfc822Name)), + static_cast(ASN1_STRING_length(gen->d.rfc822Name))); + } + break; + case GEN_URI: + entry.type = SanType::URI; + if (gen->d.uniformResourceIdentifier) { + entry.value = std::string( + reinterpret_cast( + ASN1_STRING_get0_data(gen->d.uniformResourceIdentifier)), + static_cast( + ASN1_STRING_length(gen->d.uniformResourceIdentifier))); + } + break; + default: entry.type = SanType::OTHER; break; + } + + if (!entry.value.empty()) { sans.push_back(std::move(entry)); } + } + + GENERAL_NAMES_free(names); + return true; } -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, headers, body, content_length, content_type, progress); + +inline bool get_cert_validity(cert_t cert, time_t ¬_before, + time_t ¬_after) { + if (!cert) return false; + auto x509 = static_cast(cert); + + auto nb = X509_get0_notBefore(x509); + auto na = X509_get0_notAfter(x509); + if (!nb || !na) return false; + + // Convert ASN1_TIME to time_t + struct tm tm_nb = {}, tm_na = {}; + if (ASN1_TIME_to_tm(nb, &tm_nb) != 1) return false; + if (ASN1_TIME_to_tm(na, &tm_na) != 1) return false; + +#ifdef _WIN32 + not_before = _mkgmtime(&tm_nb); + not_after = _mkgmtime(&tm_na); +#else + not_before = timegm(&tm_nb); + not_after = timegm(&tm_na); +#endif + return true; } -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, body, content_type, progress); + +inline std::string get_cert_serial(cert_t cert) { + if (!cert) return ""; + auto x509 = static_cast(cert); + + auto serial = X509_get_serialNumber(x509); + if (!serial) return ""; + + auto bn = ASN1_INTEGER_to_BN(serial, nullptr); + if (!bn) return ""; + + auto hex = BN_bn2hex(bn); + BN_free(bn); + if (!hex) return ""; + + std::string result(hex); + OPENSSL_free(hex); + return result; } -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, headers, body, content_type, progress); + +inline const char *get_sni(const_session_t session) { + if (!session) return nullptr; + auto ssl = static_cast(const_cast(session)); + return SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); } -inline Result Client::Put(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, content_length, std::move(content_provider), - content_type, progress); + +inline uint64_t peek_error() { return ERR_peek_last_error(); } + +inline uint64_t get_error() { return ERR_get_error(); } + +inline std::string error_string(uint64_t code) { + char buf[256]; + ERR_error_string_n(static_cast(code), buf, sizeof(buf)); + return std::string(buf); } -inline Result Client::Put(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Put(path, content_length, std::move(content_provider), - content_type, std::move(content_receiver), progress); + +inline ca_store_t create_ca_store(const char *pem, size_t len) { + auto mem = BIO_new_mem_buf(pem, static_cast(len)); + if (!mem) { return nullptr; } + auto mem_guard = detail::scope_exit([&] { BIO_free_all(mem); }); + + auto inf = PEM_X509_INFO_read_bio(mem, nullptr, nullptr, nullptr); + if (!inf) { return nullptr; } + + auto store = X509_STORE_new(); + if (store) { + for (auto i = 0; i < static_cast(sk_X509_INFO_num(inf)); i++) { + auto itmp = sk_X509_INFO_value(inf, i); + if (!itmp) { continue; } + if (itmp->x509) { X509_STORE_add_cert(store, itmp->x509); } + if (itmp->crl) { X509_STORE_add_crl(store, itmp->crl); } + } + } + + sk_X509_INFO_pop_free(inf, X509_INFO_free); + return static_cast(store); } -inline Result Client::Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, std::move(content_provider), content_type, progress); + +inline void free_ca_store(ca_store_t store) { + if (store) { X509_STORE_free(static_cast(store)); } } -inline Result Client::Put(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Put(path, std::move(content_provider), content_type, - std::move(content_receiver), progress); + +inline bool set_ca_store(ctx_t ctx, ca_store_t store) { + if (!ctx || !store) { return false; } + auto ssl_ctx = static_cast(ctx); + auto x509_store = static_cast(store); + + // Check if same store is already set + if (SSL_CTX_get_cert_store(ssl_ctx) == x509_store) { return true; } + + // SSL_CTX_set_cert_store takes ownership and frees the old store + SSL_CTX_set_cert_store(ssl_ctx, x509_store); + return true; } -inline Result Client::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, headers, content_length, std::move(content_provider), - content_type, progress); + +inline size_t get_ca_certs(ctx_t ctx, std::vector &certs) { + certs.clear(); + if (!ctx) { return 0; } + auto ssl_ctx = static_cast(ctx); + + auto store = SSL_CTX_get_cert_store(ssl_ctx); + if (!store) { return 0; } + + auto objs = X509_STORE_get0_objects(store); + if (!objs) { return 0; } + + int count = sk_X509_OBJECT_num(objs); + for (int i = 0; i < count; i++) { + auto obj = sk_X509_OBJECT_value(objs, i); + if (!obj) { continue; } + if (X509_OBJECT_get_type(obj) == X509_LU_X509) { + auto x509 = X509_OBJECT_get0_X509(obj); + if (x509) { + // Increment reference count so caller can free it + X509_up_ref(x509); + certs.push_back(static_cast(x509)); + } + } + } + return certs.size(); } -inline Result Client::Put(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Put(path, headers, content_length, std::move(content_provider), - content_type, std::move(content_receiver), progress); + +inline std::vector get_ca_names(ctx_t ctx) { + std::vector names; + if (!ctx) { return names; } + auto ssl_ctx = static_cast(ctx); + + auto store = SSL_CTX_get_cert_store(ssl_ctx); + if (!store) { return names; } + + auto objs = X509_STORE_get0_objects(store); + if (!objs) { return names; } + + int count = sk_X509_OBJECT_num(objs); + for (int i = 0; i < count; i++) { + auto obj = sk_X509_OBJECT_value(objs, i); + if (!obj) { continue; } + if (X509_OBJECT_get_type(obj) == X509_LU_X509) { + auto x509 = X509_OBJECT_get0_X509(obj); + if (x509) { + auto subject = X509_get_subject_name(x509); + if (subject) { + char buf[512]; + X509_NAME_oneline(subject, buf, sizeof(buf)); + names.push_back(buf); + } + } + } + } + return names; } -inline Result Client::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Put(path, headers, std::move(content_provider), content_type, - progress); + +inline bool update_server_cert(ctx_t ctx, const char *cert_pem, + const char *key_pem, const char *password) { + if (!ctx || !cert_pem || !key_pem) { return false; } + auto ssl_ctx = static_cast(ctx); + + // Load certificate from PEM + auto cert_bio = BIO_new_mem_buf(cert_pem, -1); + if (!cert_bio) { return false; } + auto cert = PEM_read_bio_X509(cert_bio, nullptr, nullptr, nullptr); + BIO_free(cert_bio); + if (!cert) { return false; } + + // Load private key from PEM + auto key_bio = BIO_new_mem_buf(key_pem, -1); + if (!key_bio) { + X509_free(cert); + return false; + } + auto key = PEM_read_bio_PrivateKey(key_bio, nullptr, nullptr, + password ? const_cast(password) + : nullptr); + BIO_free(key_bio); + if (!key) { + X509_free(cert); + return false; + } + + // Update certificate and key + auto ret = SSL_CTX_use_certificate(ssl_ctx, cert) == 1 && + SSL_CTX_use_PrivateKey(ssl_ctx, key) == 1; + + X509_free(cert); + EVP_PKEY_free(key); + return ret; +} + +inline bool update_server_client_ca(ctx_t ctx, const char *ca_pem) { + if (!ctx || !ca_pem) { return false; } + auto ssl_ctx = static_cast(ctx); + + // Create new X509_STORE from PEM + auto store = create_ca_store(ca_pem, strlen(ca_pem)); + if (!store) { return false; } + + // SSL_CTX_set_cert_store takes ownership + SSL_CTX_set_cert_store(ssl_ctx, static_cast(store)); + + // Set client CA list for client certificate request + auto ca_list = impl::create_client_ca_list_from_pem(ca_pem); + if (ca_list) { + // SSL_CTX_set_client_CA_list takes ownership of ca_list + SSL_CTX_set_client_CA_list(ssl_ctx, ca_list); + } + + return true; } -inline Result Client::Put(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Put(path, headers, std::move(content_provider), content_type, - std::move(content_receiver), progress); + +inline bool set_verify_callback(ctx_t ctx, VerifyCallback callback) { + if (!ctx) { return false; } + auto ssl_ctx = static_cast(ctx); + + impl::get_verify_callback() = std::move(callback); + + if (impl::get_verify_callback()) { + SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, impl::openssl_verify_callback); + } else { + SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, nullptr); + } + return true; } -inline Result Client::Put(const std::string &path, const Params ¶ms) { - return cli_->Put(path, params); + +inline long get_verify_error(const_session_t session) { + if (!session) { return -1; } + auto ssl = static_cast(const_cast(session)); + return SSL_get_verify_result(ssl); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Put(path, headers, params); + +inline std::string verify_error_string(long error_code) { + if (error_code == X509_V_OK) { return ""; } + const char *str = X509_verify_cert_error_string(static_cast(error_code)); + return str ? str : "unknown error"; } -inline Result Client::Put(const std::string &path, - const UploadFormDataItems &items, - UploadProgress progress) { - return cli_->Put(path, items, progress); + +namespace impl { + +// OpenSSL-specific helpers for public API wrappers +inline ctx_t create_server_context_from_x509(X509 *cert, EVP_PKEY *key, + X509_STORE *client_ca_store, + int &out_error) { + out_error = 0; + auto cert_pem = x509_to_pem(cert); + auto key_pem = evp_pkey_to_pem(key); + if (cert_pem.empty() || key_pem.empty()) { + out_error = static_cast(ERR_get_error()); + return nullptr; + } + + auto ctx = create_server_context(); + if (!ctx) { + out_error = static_cast(get_error()); + return nullptr; + } + + if (!set_server_cert_pem(ctx, cert_pem.c_str(), key_pem.c_str(), nullptr)) { + out_error = static_cast(get_error()); + free_context(ctx); + return nullptr; + } + + if (client_ca_store) { + // Set cert store for verification (SSL_CTX_set_cert_store takes ownership) + SSL_CTX_set_cert_store(static_cast(ctx), client_ca_store); + + // Extract and set client CA list directly from store (more efficient than + // PEM conversion) + auto ca_list = extract_client_ca_list_from_store(client_ca_store); + if (ca_list) { + SSL_CTX_set_client_CA_list(static_cast(ctx), ca_list); + } + + set_verify_client(ctx, true); + } + + return ctx; } -inline Result Client::Put(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - UploadProgress progress) { - return cli_->Put(path, headers, items, progress); + +inline void update_server_certs_from_x509(ctx_t ctx, X509 *cert, EVP_PKEY *key, + X509_STORE *client_ca_store) { + auto cert_pem = x509_to_pem(cert); + auto key_pem = evp_pkey_to_pem(key); + + if (!cert_pem.empty() && !key_pem.empty()) { + update_server_cert(ctx, cert_pem.c_str(), key_pem.c_str(), nullptr); + } + + if (client_ca_store) { + auto ca_pem = x509_store_to_pem(client_ca_store); + if (!ca_pem.empty()) { update_server_client_ca(ctx, ca_pem.c_str()); } + X509_STORE_free(client_ca_store); + } } -inline Result Client::Put(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const std::string &boundary, - UploadProgress progress) { - return cli_->Put(path, headers, items, boundary, progress); + +inline ctx_t create_client_context_from_x509(X509 *cert, EVP_PKEY *key, + const char *password, + unsigned long &out_error) { + out_error = 0; + auto ctx = create_client_context(); + if (!ctx) { + out_error = static_cast(get_error()); + return nullptr; + } + + if (cert && key) { + auto cert_pem = x509_to_pem(cert); + auto key_pem = evp_pkey_to_pem(key); + if (cert_pem.empty() || key_pem.empty()) { + out_error = ERR_get_error(); + free_context(ctx); + return nullptr; + } + if (!set_client_cert_pem(ctx, cert_pem.c_str(), key_pem.c_str(), + password)) { + out_error = static_cast(get_error()); + free_context(ctx); + return nullptr; + } + } + + return ctx; } -inline Result Client::Put(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const FormDataProviderItems &provider_items, - UploadProgress progress) { - return cli_->Put(path, headers, items, provider_items, progress); + +} // namespace impl + +} // namespace tls + +// ClientImpl::set_ca_cert_store - defined here to use +// tls::impl::x509_store_to_pem Deprecated: converts X509_STORE to PEM and +// stores for redirect transfer +inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store) { + ca_cert_pem_ = tls::impl::x509_store_to_pem(ca_cert_store); + } } -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Put(path, headers, body, content_type, content_receiver, - progress); + +inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + ctx_ = tls::impl::create_server_context_from_x509( + cert, private_key, client_ca_cert_store, last_ssl_error_); } -inline Result Client::Patch(const std::string &path) { - return cli_->Patch(path); +inline SSLServer::SSLServer( + const std::function &setup_ssl_ctx_callback) { + // Use abstract API to create context + ctx_ = tls::create_server_context(); + if (ctx_) { + // Pass to OpenSSL-specific callback (ctx_ is SSL_CTX* internally) + auto ssl_ctx = static_cast(ctx_); + if (!setup_ssl_ctx_callback(*ssl_ctx)) { + tls::free_context(ctx_); + ctx_ = nullptr; + } + } } -inline Result Client::Patch(const std::string &path, const Headers &headers) { - return cli_->Patch(path, headers); + +inline SSL_CTX *SSLServer::ssl_context() const { + return static_cast(ctx_); } -inline Result Client::Patch(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, body, content_length, content_type, progress); + +inline void SSLServer::update_certs(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + std::lock_guard guard(ctx_mutex_); + tls::impl::update_server_certs_from_x509(ctx_, cert, private_key, + client_ca_cert_store); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, headers, body, content_length, content_type, - progress); + +inline SSLClient::SSLClient(const std::string &host, int port, + X509 *client_cert, EVP_PKEY *client_key, + const std::string &private_key_password) + : ClientImpl(host, port) { + const char *password = + private_key_password.empty() ? nullptr : private_key_password.c_str(); + ctx_ = tls::impl::create_client_context_from_x509( + client_cert, client_key, password, last_backend_error_); } -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, body, content_type, progress); + +inline long SSLClient::get_verify_result() const { return verify_result_; } + +inline void SSLClient::set_server_certificate_verifier( + std::function verifier) { + // Wrap SSL* callback into backend-independent session_verifier_ + auto v = std::make_shared>( + std::move(verifier)); + session_verifier_ = [v](tls::session_t session) { + return (*v)(static_cast(session)); + }; } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, headers, body, content_type, progress); + +inline SSL_CTX *SSLClient::ssl_context() const { + return static_cast(ctx_); } -inline Result Client::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, content_length, std::move(content_provider), - content_type, progress); + +inline bool SSLClient::verify_host(X509 *server_cert) const { + /* Quote from RFC2818 section 3.1 "Server Identity" + + If a subjectAltName extension of type dNSName is present, that MUST + be used as the identity. Otherwise, the (most specific) Common Name + field in the Subject field of the certificate MUST be used. Although + the use of the Common Name is existing practice, it is deprecated and + Certification Authorities are encouraged to use the dNSName instead. + + Matching is performed using the matching rules specified by + [RFC2459]. If more than one identity of a given type is present in + the certificate (e.g., more than one dNSName name, a match in any one + of the set is considered acceptable.) Names may contain the wildcard + character * which is considered to match any single domain name + component or component fragment. E.g., *.a.com matches foo.a.com but + not bar.foo.a.com. f*.com matches foo.com but not bar.com. + + In some cases, the URI is specified as an IP address rather than a + hostname. In this case, the iPAddress subjectAltName must be present + in the certificate and must exactly match the IP in the URI. + + */ + return verify_host_with_subject_alt_name(server_cert) || + verify_host_with_common_name(server_cert); } -inline Result Client::Patch(const std::string &path, size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Patch(path, content_length, std::move(content_provider), - content_type, std::move(content_receiver), progress); + +inline bool +SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { + auto ret = false; + + auto type = GEN_DNS; + + struct in6_addr addr6 = {}; + struct in_addr addr = {}; + size_t addr_len = 0; + +#ifndef __MINGW32__ + if (inet_pton(AF_INET6, host_.c_str(), &addr6)) { + type = GEN_IPADD; + addr_len = sizeof(struct in6_addr); + } else if (inet_pton(AF_INET, host_.c_str(), &addr)) { + type = GEN_IPADD; + addr_len = sizeof(struct in_addr); + } +#endif + + auto alt_names = static_cast( + X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); + + if (alt_names) { + auto dsn_matched = false; + auto ip_matched = false; + + auto count = sk_GENERAL_NAME_num(alt_names); + + for (decltype(count) i = 0; i < count && !dsn_matched; i++) { + auto val = sk_GENERAL_NAME_value(alt_names, i); + if (!val || val->type != type) { continue; } + + auto name = + reinterpret_cast(ASN1_STRING_get0_data(val->d.ia5)); + if (name == nullptr) { continue; } + + auto name_len = static_cast(ASN1_STRING_length(val->d.ia5)); + + switch (type) { + case GEN_DNS: + dsn_matched = + detail::match_hostname(std::string(name, name_len), host_); + break; + + case GEN_IPADD: + if (!memcmp(&addr6, name, addr_len) || !memcmp(&addr, name, addr_len)) { + ip_matched = true; + } + break; + } + } + + if (dsn_matched || ip_matched) { ret = true; } + } + + GENERAL_NAMES_free(const_cast( + reinterpret_cast(alt_names))); + return ret; } -inline Result Client::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, std::move(content_provider), content_type, progress); + +inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { + const auto subject_name = X509_get_subject_name(server_cert); + + if (subject_name != nullptr) { + char name[BUFSIZ]; + auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, + name, sizeof(name)); + + if (name_len != -1) { + return detail::match_hostname( + std::string(name, static_cast(name_len)), host_); + } + } + + return false; } -inline Result Client::Patch(const std::string &path, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Patch(path, std::move(content_provider), content_type, - std::move(content_receiver), progress); + +#endif // CPPHTTPLIB_OPENSSL_SUPPORT + +/* + * Group 9: TLS abstraction layer - Mbed TLS backend + */ + +/* + * Mbed TLS Backend Implementation + */ + +#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT +namespace tls { + +namespace impl { + +// Mbed TLS session wrapper +struct MbedTlsSession { + mbedtls_ssl_context ssl; + socket_t sock = INVALID_SOCKET; + std::string hostname; // For client: set via set_sni + std::string sni_hostname; // For server: received from client via SNI callback + + MbedTlsSession() { mbedtls_ssl_init(&ssl); } + + ~MbedTlsSession() { mbedtls_ssl_free(&ssl); } + + MbedTlsSession(const MbedTlsSession &) = delete; + MbedTlsSession &operator=(const MbedTlsSession &) = delete; +}; + +// Thread-local error code accessor for Mbed TLS (since it doesn't have an error +// queue) +inline int &mbedtls_last_error() { + static thread_local int err = 0; + return err; } -inline Result Client::Patch(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, headers, content_length, std::move(content_provider), - content_type, progress); + +// Helper to map Mbed TLS error to ErrorCode +inline ErrorCode map_mbedtls_error(int ret, int &out_errno) { + if (ret == 0) { return ErrorCode::Success; } + if (ret == MBEDTLS_ERR_SSL_WANT_READ) { return ErrorCode::WantRead; } + if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) { return ErrorCode::WantWrite; } + if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { + return ErrorCode::PeerClosed; + } + if (ret == MBEDTLS_ERR_NET_CONN_RESET || ret == MBEDTLS_ERR_NET_SEND_FAILED || + ret == MBEDTLS_ERR_NET_RECV_FAILED) { + out_errno = errno; + return ErrorCode::SyscallError; + } + if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) { + return ErrorCode::CertVerifyFailed; + } + return ErrorCode::Fatal; } -inline Result Client::Patch(const std::string &path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Patch(path, headers, content_length, std::move(content_provider), - content_type, std::move(content_receiver), progress); + +// BIO-like send callback for Mbed TLS +inline int mbedtls_net_send_cb(void *ctx, const unsigned char *buf, + size_t len) { + auto sock = *static_cast(ctx); +#ifdef _WIN32 + auto ret = + send(sock, reinterpret_cast(buf), static_cast(len), 0); + if (ret == SOCKET_ERROR) { + int err = WSAGetLastError(); + if (err == WSAEWOULDBLOCK) { return MBEDTLS_ERR_SSL_WANT_WRITE; } + return MBEDTLS_ERR_NET_SEND_FAILED; + } +#else + auto ret = send(sock, buf, len, 0); + if (ret < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return MBEDTLS_ERR_SSL_WANT_WRITE; + } + return MBEDTLS_ERR_NET_SEND_FAILED; + } +#endif + return static_cast(ret); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - UploadProgress progress) { - return cli_->Patch(path, headers, std::move(content_provider), content_type, - progress); + +// BIO-like recv callback for Mbed TLS +inline int mbedtls_net_recv_cb(void *ctx, unsigned char *buf, size_t len) { + auto sock = *static_cast(ctx); +#ifdef _WIN32 + auto ret = + recv(sock, reinterpret_cast(buf), static_cast(len), 0); + if (ret == SOCKET_ERROR) { + int err = WSAGetLastError(); + if (err == WSAEWOULDBLOCK) { return MBEDTLS_ERR_SSL_WANT_READ; } + return MBEDTLS_ERR_NET_RECV_FAILED; + } +#else + auto ret = recv(sock, buf, len, 0); + if (ret < 0) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return MBEDTLS_ERR_SSL_WANT_READ; + } + return MBEDTLS_ERR_NET_RECV_FAILED; + } +#endif + if (ret == 0) { return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; } + return static_cast(ret); +} + +// MbedTlsContext constructor/destructor implementations +inline MbedTlsContext::MbedTlsContext() { + mbedtls_ssl_config_init(&conf); + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&ctr_drbg); + mbedtls_x509_crt_init(&ca_chain); + mbedtls_x509_crt_init(&own_cert); + mbedtls_pk_init(&own_key); +} + +inline MbedTlsContext::~MbedTlsContext() { + mbedtls_pk_free(&own_key); + mbedtls_x509_crt_free(&own_cert); + mbedtls_x509_crt_free(&ca_chain); + mbedtls_ctr_drbg_free(&ctr_drbg); + mbedtls_entropy_free(&entropy); + mbedtls_ssl_config_free(&conf); +} + +// Thread-local storage for SNI captured during handshake +// This is needed because the SNI callback doesn't have a way to pass +// session-specific data before the session is fully set up +inline std::string &mbedpending_sni() { + static thread_local std::string sni; + return sni; +} + +// SNI callback for Mbed TLS server to capture client's SNI hostname +inline int mbedtls_sni_callback(void *p_ctx, mbedtls_ssl_context *ssl, + const unsigned char *name, size_t name_len) { + (void)p_ctx; + (void)ssl; + + // Store SNI name in thread-local storage + // It will be retrieved and stored in the session after handshake + if (name && name_len > 0) { + mbedpending_sni().assign(reinterpret_cast(name), name_len); + } else { + mbedpending_sni().clear(); + } + return 0; // Accept any SNI } -inline Result Client::Patch(const std::string &path, const Headers &headers, - ContentProviderWithoutLength content_provider, - const std::string &content_type, - ContentReceiver content_receiver, - UploadProgress progress) { - return cli_->Patch(path, headers, std::move(content_provider), content_type, - std::move(content_receiver), progress); + +inline int mbedtls_verify_callback(void *data, mbedtls_x509_crt *crt, + int cert_depth, uint32_t *flags); + +// Check if a string is an IPv4 address +inline bool is_ipv4_address(const std::string &str) { + int dots = 0; + for (char c : str) { + if (c == '.') { + dots++; + } else if (!isdigit(static_cast(c))) { + return false; + } + } + return dots == 3; } -inline Result Client::Patch(const std::string &path, const Params ¶ms) { - return cli_->Patch(path, params); + +// Parse IPv4 address string to bytes +inline bool parse_ipv4(const std::string &str, unsigned char *out) { + int parts[4]; + if (sscanf(str.c_str(), "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2], + &parts[3]) != 4) { + return false; + } + for (int i = 0; i < 4; i++) { + if (parts[i] < 0 || parts[i] > 255) return false; + out[i] = static_cast(parts[i]); + } + return true; } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const Params ¶ms) { - return cli_->Patch(path, headers, params); + +// MbedTLS verify callback wrapper +inline int mbedtls_verify_callback(void *data, mbedtls_x509_crt *crt, + int cert_depth, uint32_t *flags) { + auto &callback = get_verify_callback(); + if (!callback) { return 0; } // Continue with default verification + + // data points to the MbedTlsSession + auto *session = static_cast(data); + + // Build context + VerifyContext verify_ctx; + verify_ctx.session = static_cast(session); + verify_ctx.cert = static_cast(crt); + verify_ctx.depth = cert_depth; + verify_ctx.preverify_ok = (*flags == 0); + verify_ctx.error_code = static_cast(*flags); + + // Convert Mbed TLS flags to error string + static thread_local char error_buf[256]; + if (*flags != 0) { + mbedtls_x509_crt_verify_info(error_buf, sizeof(error_buf), "", *flags); + verify_ctx.error_string = error_buf; + } else { + verify_ctx.error_string = nullptr; + } + + bool accepted = callback(verify_ctx); + + if (accepted) { + *flags = 0; // Clear all error flags + return 0; + } + return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED; } -inline Result Client::Patch(const std::string &path, - const UploadFormDataItems &items, - UploadProgress progress) { - return cli_->Patch(path, items, progress); + +} // namespace impl + +inline ctx_t create_client_context() { + auto ctx = new (std::nothrow) impl::MbedTlsContext(); + if (!ctx) { return nullptr; } + + ctx->is_server = false; + + // Seed the random number generator + const char *pers = "httplib_client"; + int ret = mbedtls_ctr_drbg_seed( + &ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy, + reinterpret_cast(pers), strlen(pers)); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + delete ctx; + return nullptr; + } + + // Set up SSL config for client + ret = mbedtls_ssl_config_defaults(&ctx->conf, MBEDTLS_SSL_IS_CLIENT, + MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + delete ctx; + return nullptr; + } + + // Set random number generator + mbedtls_ssl_conf_rng(&ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg); + + // Default: verify peer certificate + mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_REQUIRED); + + // Set minimum TLS version to 1.2 +#ifdef CPPHTTPLIB_MBEDTLS_V3 + mbedtls_ssl_conf_min_tls_version(&ctx->conf, MBEDTLS_SSL_VERSION_TLS1_2); +#else + mbedtls_ssl_conf_min_version(&ctx->conf, MBEDTLS_SSL_MAJOR_VERSION_3, + MBEDTLS_SSL_MINOR_VERSION_3); +#endif + + return static_cast(ctx); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - UploadProgress progress) { - return cli_->Patch(path, headers, items, progress); + +inline ctx_t create_server_context() { + auto ctx = new (std::nothrow) impl::MbedTlsContext(); + if (!ctx) { return nullptr; } + + ctx->is_server = true; + + // Seed the random number generator + const char *pers = "httplib_server"; + int ret = mbedtls_ctr_drbg_seed( + &ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy, + reinterpret_cast(pers), strlen(pers)); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + delete ctx; + return nullptr; + } + + // Set up SSL config for server + ret = mbedtls_ssl_config_defaults(&ctx->conf, MBEDTLS_SSL_IS_SERVER, + MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + delete ctx; + return nullptr; + } + + // Set random number generator + mbedtls_ssl_conf_rng(&ctx->conf, mbedtls_ctr_drbg_random, &ctx->ctr_drbg); + + // Default: don't verify client + mbedtls_ssl_conf_authmode(&ctx->conf, MBEDTLS_SSL_VERIFY_NONE); + + // Set minimum TLS version to 1.2 +#ifdef CPPHTTPLIB_MBEDTLS_V3 + mbedtls_ssl_conf_min_tls_version(&ctx->conf, MBEDTLS_SSL_VERSION_TLS1_2); +#else + mbedtls_ssl_conf_min_version(&ctx->conf, MBEDTLS_SSL_MAJOR_VERSION_3, + MBEDTLS_SSL_MINOR_VERSION_3); +#endif + + // Set SNI callback to capture client's SNI hostname + mbedtls_ssl_conf_sni(&ctx->conf, impl::mbedtls_sni_callback, nullptr); + + return static_cast(ctx); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const std::string &boundary, - UploadProgress progress) { - return cli_->Patch(path, headers, items, boundary, progress); + +inline void free_context(ctx_t ctx) { + if (ctx) { delete static_cast(ctx); } } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const UploadFormDataItems &items, - const FormDataProviderItems &provider_items, - UploadProgress progress) { - return cli_->Patch(path, headers, items, provider_items, progress); + +inline bool set_min_version(ctx_t ctx, Version version) { + if (!ctx) { return false; } + auto mctx = static_cast(ctx); + +#ifdef CPPHTTPLIB_MBEDTLS_V3 + // Mbed TLS 3.x uses mbedtls_ssl_protocol_version enum + mbedtls_ssl_protocol_version min_ver = MBEDTLS_SSL_VERSION_TLS1_2; + if (version >= Version::TLS1_3) { +#if defined(MBEDTLS_SSL_PROTO_TLS1_3) + min_ver = MBEDTLS_SSL_VERSION_TLS1_3; +#endif + } + mbedtls_ssl_conf_min_tls_version(&mctx->conf, min_ver); +#else + // Mbed TLS 2.x uses major/minor version numbers + int major = MBEDTLS_SSL_MAJOR_VERSION_3; + int minor = MBEDTLS_SSL_MINOR_VERSION_3; // TLS 1.2 + if (version >= Version::TLS1_3) { +#if defined(MBEDTLS_SSL_PROTO_TLS1_3) + minor = MBEDTLS_SSL_MINOR_VERSION_4; // TLS 1.3 +#else + minor = MBEDTLS_SSL_MINOR_VERSION_3; // Fall back to TLS 1.2 +#endif + } + mbedtls_ssl_conf_min_version(&mctx->conf, major, minor); +#endif + return true; } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - ContentReceiver content_receiver, - DownloadProgress progress) { - return cli_->Patch(path, headers, body, content_type, content_receiver, - progress); + +inline bool load_ca_pem(ctx_t ctx, const char *pem, size_t len) { + if (!ctx || !pem) { return false; } + auto mctx = static_cast(ctx); + + // mbedtls_x509_crt_parse expects null-terminated string for PEM + // Add null terminator if not present + std::string pem_str(pem, len); + int ret = mbedtls_x509_crt_parse( + &mctx->ca_chain, reinterpret_cast(pem_str.c_str()), + pem_str.size() + 1); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + mbedtls_ssl_conf_ca_chain(&mctx->conf, &mctx->ca_chain, nullptr); + return true; } -inline Result Client::Delete(const std::string &path, - DownloadProgress progress) { - return cli_->Delete(path, progress); +inline bool load_ca_file(ctx_t ctx, const char *file_path) { + if (!ctx || !file_path) { return false; } + auto mctx = static_cast(ctx); + + int ret = mbedtls_x509_crt_parse_file(&mctx->ca_chain, file_path); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + mbedtls_ssl_conf_ca_chain(&mctx->conf, &mctx->ca_chain, nullptr); + return true; } -inline Result Client::Delete(const std::string &path, const Headers &headers, - DownloadProgress progress) { - return cli_->Delete(path, headers, progress); + +inline bool load_ca_dir(ctx_t ctx, const char *dir_path) { + if (!ctx || !dir_path) { return false; } + auto mctx = static_cast(ctx); + + int ret = mbedtls_x509_crt_parse_path(&mctx->ca_chain, dir_path); + if (ret < 0) { // Returns number of certs on success, negative on error + impl::mbedtls_last_error() = ret; + return false; + } + + mbedtls_ssl_conf_ca_chain(&mctx->conf, &mctx->ca_chain, nullptr); + return true; } -inline Result Client::Delete(const std::string &path, const char *body, - size_t content_length, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, body, content_length, content_type, progress); + +inline bool load_system_certs(ctx_t ctx) { + if (!ctx) { return false; } + auto mctx = static_cast(ctx); + bool loaded = false; + +#ifdef _WIN32 + // Load from Windows certificate store + HCERTSTORE hStore = CertOpenSystemStoreW(0, L"ROOT"); + if (hStore) { + PCCERT_CONTEXT pContext = nullptr; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + int ret = mbedtls_x509_crt_parse_der( + &mctx->ca_chain, pContext->pbCertEncoded, pContext->cbCertEncoded); + if (ret == 0) { loaded = true; } + } + CertCloseStore(hStore, 0); + } +#elif defined(__APPLE__) && defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) + // Load from macOS Keychain + CFArrayRef certs = nullptr; + OSStatus status = SecTrustCopyAnchorCertificates(&certs); + if (status == errSecSuccess && certs) { + CFIndex count = CFArrayGetCount(certs); + for (CFIndex i = 0; i < count; i++) { + SecCertificateRef cert = + (SecCertificateRef)CFArrayGetValueAtIndex(certs, i); + CFDataRef data = SecCertificateCopyData(cert); + if (data) { + int ret = mbedtls_x509_crt_parse_der( + &mctx->ca_chain, CFDataGetBytePtr(data), + static_cast(CFDataGetLength(data))); + if (ret == 0) { loaded = true; } + CFRelease(data); + } + } + CFRelease(certs); + } +#else + // Try common CA certificate locations on Linux/Unix + static const char *ca_paths[] = { + "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu + "/etc/pki/tls/certs/ca-bundle.crt", // RHEL/CentOS + "/etc/ssl/ca-bundle.pem", // OpenSUSE + "/etc/pki/tls/cacert.pem", // OpenELEC + "/etc/ssl/cert.pem", // Alpine, FreeBSD + nullptr}; + + for (const char **path = ca_paths; *path; ++path) { + int ret = mbedtls_x509_crt_parse_file(&mctx->ca_chain, *path); + if (ret >= 0) { + loaded = true; + break; + } + } + + // Also try the CA directory + if (!loaded) { + static const char *ca_dirs[] = {"/etc/ssl/certs", // Debian/Ubuntu + "/etc/pki/tls/certs", // RHEL/CentOS + "/usr/share/ca-certificates", nullptr}; + + for (const char **dir = ca_dirs; *dir; ++dir) { + int ret = mbedtls_x509_crt_parse_path(&mctx->ca_chain, *dir); + if (ret >= 0) { + loaded = true; + break; + } + } + } +#endif + + if (loaded) { + mbedtls_ssl_conf_ca_chain(&mctx->conf, &mctx->ca_chain, nullptr); + } + return loaded; } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, headers, body, content_length, content_type, - progress); + +inline bool set_client_cert_pem(ctx_t ctx, const char *cert, const char *key, + const char *password) { + if (!ctx || !cert || !key) { return false; } + auto mctx = static_cast(ctx); + + // Parse certificate + std::string cert_str(cert); + int ret = mbedtls_x509_crt_parse( + &mctx->own_cert, + reinterpret_cast(cert_str.c_str()), + cert_str.size() + 1); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + // Parse private key + std::string key_str(key); + const unsigned char *pwd = + password ? reinterpret_cast(password) : nullptr; + size_t pwd_len = password ? strlen(password) : 0; + +#ifdef CPPHTTPLIB_MBEDTLS_V3 + ret = mbedtls_pk_parse_key( + &mctx->own_key, reinterpret_cast(key_str.c_str()), + key_str.size() + 1, pwd, pwd_len, mbedtls_ctr_drbg_random, + &mctx->ctr_drbg); +#else + ret = mbedtls_pk_parse_key( + &mctx->own_key, reinterpret_cast(key_str.c_str()), + key_str.size() + 1, pwd, pwd_len); +#endif + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + ret = mbedtls_ssl_conf_own_cert(&mctx->conf, &mctx->own_cert, &mctx->own_key); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + return true; } -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, body, content_type, progress); + +inline bool set_client_cert_file(ctx_t ctx, const char *cert_path, + const char *key_path, const char *password) { + if (!ctx || !cert_path || !key_path) { return false; } + auto mctx = static_cast(ctx); + + // Parse certificate file + int ret = mbedtls_x509_crt_parse_file(&mctx->own_cert, cert_path); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + // Parse private key file +#ifdef CPPHTTPLIB_MBEDTLS_V3 + ret = mbedtls_pk_parse_keyfile(&mctx->own_key, key_path, password, + mbedtls_ctr_drbg_random, &mctx->ctr_drbg); +#else + ret = mbedtls_pk_parse_keyfile(&mctx->own_key, key_path, password); +#endif + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + ret = mbedtls_ssl_conf_own_cert(&mctx->conf, &mctx->own_cert, &mctx->own_key); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + return true; } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type, - DownloadProgress progress) { - return cli_->Delete(path, headers, body, content_type, progress); + +inline void set_verify_client(ctx_t ctx, bool require) { + if (!ctx) { return; } + auto mctx = static_cast(ctx); + mctx->verify_client = require; + if (require) { + mbedtls_ssl_conf_authmode(&mctx->conf, MBEDTLS_SSL_VERIFY_REQUIRED); + } else { + // If a verify callback is set, use OPTIONAL mode to ensure the callback + // is called (matching OpenSSL behavior). Otherwise use NONE. + mbedtls_ssl_conf_authmode(&mctx->conf, mctx->has_verify_callback + ? MBEDTLS_SSL_VERIFY_OPTIONAL + : MBEDTLS_SSL_VERIFY_NONE); + } } -inline Result Client::Delete(const std::string &path, const Params ¶ms, - DownloadProgress progress) { - return cli_->Delete(path, params, progress); + +inline session_t create_session(ctx_t ctx, socket_t sock) { + if (!ctx || sock == INVALID_SOCKET) { return nullptr; } + auto mctx = static_cast(ctx); + + auto session = new (std::nothrow) impl::MbedTlsSession(); + if (!session) { return nullptr; } + + session->sock = sock; + + int ret = mbedtls_ssl_setup(&session->ssl, &mctx->conf); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + delete session; + return nullptr; + } + + // Set BIO callbacks + mbedtls_ssl_set_bio(&session->ssl, &session->sock, impl::mbedtls_net_send_cb, + impl::mbedtls_net_recv_cb, nullptr); + + // Set per-session verify callback with session pointer if callback is + // registered + if (mctx->has_verify_callback) { + mbedtls_ssl_set_verify(&session->ssl, impl::mbedtls_verify_callback, + session); + } + + return static_cast(session); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const Params ¶ms, DownloadProgress progress) { - return cli_->Delete(path, headers, params, progress); + +inline void free_session(session_t session) { + if (session) { delete static_cast(session); } } -inline Result Client::Options(const std::string &path) { - return cli_->Options(path); +inline bool set_sni(session_t session, const char *hostname) { + if (!session || !hostname) { return false; } + auto msession = static_cast(session); + + int ret = mbedtls_ssl_set_hostname(&msession->ssl, hostname); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + msession->hostname = hostname; + return true; } -inline Result Client::Options(const std::string &path, const Headers &headers) { - return cli_->Options(path, headers); + +inline bool set_hostname(session_t session, const char *hostname) { + // In Mbed TLS, set_hostname also sets up hostname verification + return set_sni(session, hostname); } -inline ClientImpl::StreamHandle -Client::open_stream(const std::string &method, const std::string &path, - const Params ¶ms, const Headers &headers, - const std::string &body, const std::string &content_type) { - return cli_->open_stream(method, path, params, headers, body, content_type); +inline TlsError connect(session_t session) { + TlsError err; + if (!session) { + err.code = ErrorCode::Fatal; + return err; + } + + auto msession = static_cast(session); + int ret = mbedtls_ssl_handshake(&msession->ssl); + + if (ret == 0) { + err.code = ErrorCode::Success; + } else { + err.code = impl::map_mbedtls_error(ret, err.sys_errno); + err.backend_code = static_cast(-ret); + impl::mbedtls_last_error() = ret; + } + + return err; } -inline bool Client::send(Request &req, Response &res, Error &error) { - return cli_->send(req, res, error); +inline TlsError accept(session_t session) { + // Same as connect for Mbed TLS - handshake works for both client and server + auto result = connect(session); + + // After successful handshake, capture SNI from thread-local storage + if (result.code == ErrorCode::Success && session) { + auto msession = static_cast(session); + msession->sni_hostname = std::move(impl::mbedpending_sni()); + impl::mbedpending_sni().clear(); + } + + return result; } -inline Result Client::send(const Request &req) { return cli_->send(req); } +inline bool connect_nonblocking(session_t session, socket_t sock, + time_t timeout_sec, time_t timeout_usec, + TlsError *err) { + if (!session) { + if (err) { err->code = ErrorCode::Fatal; } + return false; + } -inline void Client::stop() { cli_->stop(); } + auto msession = static_cast(session); -inline std::string Client::host() const { return cli_->host(); } + // Set socket to non-blocking mode + detail::set_nonblocking(sock, true); + auto cleanup = + detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); -inline int Client::port() const { return cli_->port(); } + int ret; + while ((ret = mbedtls_ssl_handshake(&msession->ssl)) != 0) { + if (ret == MBEDTLS_ERR_SSL_WANT_READ) { + if (detail::select_read(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + } else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) { + if (detail::select_write(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + } -inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + // TlsError or timeout + if (err) { + err->code = impl::map_mbedtls_error(ret, err->sys_errno); + err->backend_code = static_cast(-ret); + } + impl::mbedtls_last_error() = ret; + return false; + } -inline socket_t Client::socket() const { return cli_->socket(); } + if (err) { err->code = ErrorCode::Success; } + return true; +} -inline void -Client::set_hostname_addr_map(std::map addr_map) { - cli_->set_hostname_addr_map(std::move(addr_map)); +inline bool accept_nonblocking(session_t session, socket_t sock, + time_t timeout_sec, time_t timeout_usec, + TlsError *err) { + // Same implementation as connect for Mbed TLS + bool result = + connect_nonblocking(session, sock, timeout_sec, timeout_usec, err); + + // After successful handshake, capture SNI from thread-local storage + if (result && session) { + auto msession = static_cast(session); + msession->sni_hostname = std::move(impl::mbedpending_sni()); + impl::mbedpending_sni().clear(); + } + + return result; } -inline void Client::set_default_headers(Headers headers) { - cli_->set_default_headers(std::move(headers)); +inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) { + if (!session || !buf) { + err.code = ErrorCode::Fatal; + return -1; + } + + auto msession = static_cast(session); + int ret = + mbedtls_ssl_read(&msession->ssl, static_cast(buf), len); + + if (ret > 0) { + err.code = ErrorCode::Success; + return static_cast(ret); + } + + if (ret == 0) { + err.code = ErrorCode::PeerClosed; + return 0; + } + + err.code = impl::map_mbedtls_error(ret, err.sys_errno); + err.backend_code = static_cast(-ret); + impl::mbedtls_last_error() = ret; + return -1; } -inline void Client::set_header_writer( - std::function const &writer) { - cli_->set_header_writer(writer); +inline ssize_t write(session_t session, const void *buf, size_t len, + TlsError &err) { + if (!session || !buf) { + err.code = ErrorCode::Fatal; + return -1; + } + + auto msession = static_cast(session); + int ret = mbedtls_ssl_write(&msession->ssl, + static_cast(buf), len); + + if (ret > 0) { + err.code = ErrorCode::Success; + return static_cast(ret); + } + + if (ret == 0) { + err.code = ErrorCode::PeerClosed; + return 0; + } + + err.code = impl::map_mbedtls_error(ret, err.sys_errno); + err.backend_code = static_cast(-ret); + impl::mbedtls_last_error() = ret; + return -1; } -inline void Client::set_address_family(int family) { - cli_->set_address_family(family); +inline int pending(const_session_t session) { + if (!session) { return 0; } + auto msession = + static_cast(const_cast(session)); + return static_cast(mbedtls_ssl_get_bytes_avail(&msession->ssl)); } -inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } +inline void shutdown(session_t session, bool graceful) { + if (!session) { return; } + auto msession = static_cast(session); -inline void Client::set_socket_options(SocketOptions socket_options) { - cli_->set_socket_options(std::move(socket_options)); + if (graceful) { + // Try to send close_notify, but don't block forever + int ret; + int attempts = 0; + while ((ret = mbedtls_ssl_close_notify(&msession->ssl)) != 0 && + attempts < 3) { + if (ret != MBEDTLS_ERR_SSL_WANT_READ && + ret != MBEDTLS_ERR_SSL_WANT_WRITE) { + break; + } + attempts++; + } + } } -inline void Client::set_connection_timeout(time_t sec, time_t usec) { - cli_->set_connection_timeout(sec, usec); +inline bool is_peer_closed(session_t session, socket_t sock) { + if (!session || sock == INVALID_SOCKET) { return true; } + auto msession = static_cast(session); + + // Check if there's already decrypted data available in the TLS buffer + // If so, the connection is definitely alive + if (mbedtls_ssl_get_bytes_avail(&msession->ssl) > 0) { return false; } + + // Set socket to non-blocking to avoid blocking on read + detail::set_nonblocking(sock, true); + auto cleanup = + detail::scope_exit([&]() { detail::set_nonblocking(sock, false); }); + + // Try a 1-byte read to check connection status + // Note: This will consume the byte if data is available, but for the + // purpose of checking if peer is closed, this should be acceptable + // since we're only called when we expect the connection might be closing + unsigned char buf; + int ret = mbedtls_ssl_read(&msession->ssl, &buf, 1); + + // If we got data or WANT_READ (would block), connection is alive + if (ret > 0 || ret == MBEDTLS_ERR_SSL_WANT_READ) { return false; } + + // If we get a peer close notify or a connection reset, the peer is closed + return ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY || + ret == MBEDTLS_ERR_NET_CONN_RESET || ret == 0; +} + +inline cert_t get_peer_cert(const_session_t session) { + if (!session) { return nullptr; } + auto msession = + static_cast(const_cast(session)); + + // Mbed TLS returns a pointer to the internal peer cert chain. + // WARNING: This pointer is only valid while the session is active. + // Do not use the certificate after calling free_session(). + const mbedtls_x509_crt *cert = mbedtls_ssl_get_peer_cert(&msession->ssl); + return const_cast(cert); +} + +inline void free_cert(cert_t cert) { + // Mbed TLS: peer certificate is owned by the SSL context. + // No-op here, but callers should still call this for cross-backend + // portability. + (void)cert; +} + +inline bool verify_hostname(cert_t cert, const char *hostname) { + if (!cert || !hostname) { return false; } + auto mcert = static_cast(cert); + std::string host_str(hostname); + + // Check if hostname is an IP address + bool is_ip = impl::is_ipv4_address(host_str); + unsigned char ip_bytes[4]; + if (is_ip) { impl::parse_ipv4(host_str, ip_bytes); } + + // Check Subject Alternative Names (SAN) + // In Mbed TLS 3.x, subject_alt_names contains raw values without ASN.1 tags + // - DNS names: raw string bytes + // - IP addresses: raw IP bytes (4 for IPv4, 16 for IPv6) + const mbedtls_x509_sequence *san = &mcert->subject_alt_names; + while (san != nullptr && san->buf.p != nullptr && san->buf.len > 0) { + const unsigned char *p = san->buf.p; + size_t len = san->buf.len; + + if (is_ip) { + // Check if this SAN is an IPv4 address (4 bytes) + if (len == 4 && memcmp(p, ip_bytes, 4) == 0) { return true; } + // Check if this SAN is an IPv6 address (16 bytes) - skip for now + } else { + // Check if this SAN is a DNS name (printable ASCII string) + bool is_dns = len > 0; + for (size_t i = 0; i < len && is_dns; i++) { + if (p[i] < 32 || p[i] > 126) { is_dns = false; } + } + if (is_dns) { + std::string san_name(reinterpret_cast(p), len); + if (detail::match_hostname(san_name, host_str)) { return true; } + } + } + san = san->next; + } + + // Fallback: Check Common Name (CN) in subject + char cn[256]; + int ret = mbedtls_x509_dn_gets(cn, sizeof(cn), &mcert->subject); + if (ret > 0) { + std::string cn_str(cn); + + // Look for "CN=" in the DN string + size_t cn_pos = cn_str.find("CN="); + if (cn_pos != std::string::npos) { + size_t start = cn_pos + 3; + size_t end = cn_str.find(',', start); + std::string cn_value = + cn_str.substr(start, end == std::string::npos ? end : end - start); + + if (detail::match_hostname(cn_value, host_str)) { return true; } + } + } + + return false; } -inline void Client::set_read_timeout(time_t sec, time_t usec) { - cli_->set_read_timeout(sec, usec); +inline uint64_t hostname_mismatch_code() { + return static_cast(MBEDTLS_X509_BADCERT_CN_MISMATCH); } -inline void Client::set_write_timeout(time_t sec, time_t usec) { - cli_->set_write_timeout(sec, usec); +inline long get_verify_result(const_session_t session) { + if (!session) { return -1; } + auto msession = + static_cast(const_cast(session)); + uint32_t flags = mbedtls_ssl_get_verify_result(&msession->ssl); + // Return 0 (X509_V_OK equivalent) if verification passed + return flags == 0 ? 0 : static_cast(flags); } -inline void Client::set_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_basic_auth(username, password); +inline std::string get_cert_subject_cn(cert_t cert) { + if (!cert) return ""; + auto x509 = static_cast(cert); + + // Find the CN in the subject + const mbedtls_x509_name *name = &x509->subject; + while (name != nullptr) { + if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0) { + return std::string(reinterpret_cast(name->val.p), + name->val.len); + } + name = name->next; + } + return ""; } -inline void Client::set_bearer_token_auth(const std::string &token) { - cli_->set_bearer_token_auth(token); + +inline std::string get_cert_issuer_name(cert_t cert) { + if (!cert) return ""; + auto x509 = static_cast(cert); + + // Build a human-readable issuer name string + char buf[512]; + int ret = mbedtls_x509_dn_gets(buf, sizeof(buf), &x509->issuer); + if (ret < 0) return ""; + return std::string(buf); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_digest_auth(username, password); + +inline bool get_cert_sans(cert_t cert, std::vector &sans) { + sans.clear(); + if (!cert) return false; + auto x509 = static_cast(cert); + + // Parse the Subject Alternative Name extension + const mbedtls_x509_sequence *cur = &x509->subject_alt_names; + while (cur != nullptr) { + if (cur->buf.len > 0) { + // Mbed TLS stores SAN as ASN.1 sequences + // The tag byte indicates the type + const unsigned char *p = cur->buf.p; + size_t len = cur->buf.len; + + // First byte is the tag + unsigned char tag = *p; + p++; + len--; + + // Parse length (simple single-byte length assumed) + if (len > 0 && *p < 0x80) { + size_t value_len = *p; + p++; + len--; + + if (value_len <= len) { + SanEntry entry; + // ASN.1 context tags for GeneralName + switch (tag & 0x1F) { + case 2: // dNSName + entry.type = SanType::DNS; + entry.value = + std::string(reinterpret_cast(p), value_len); + break; + case 7: // iPAddress + entry.type = SanType::IP; + if (value_len == 4) { + // IPv4 + char buf[16]; + snprintf(buf, sizeof(buf), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + entry.value = buf; + } else if (value_len == 16) { + // IPv6 + char buf[64]; + snprintf(buf, sizeof(buf), + "%02x%02x:%02x%02x:%02x%02x:%02x%02x:" + "%02x%02x:%02x%02x:%02x%02x:%02x%02x", + p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], + p[9], p[10], p[11], p[12], p[13], p[14], p[15]); + entry.value = buf; + } + break; + case 1: // rfc822Name (email) + entry.type = SanType::EMAIL; + entry.value = + std::string(reinterpret_cast(p), value_len); + break; + case 6: // uniformResourceIdentifier + entry.type = SanType::URI; + entry.value = + std::string(reinterpret_cast(p), value_len); + break; + default: entry.type = SanType::OTHER; break; + } + + if (!entry.value.empty()) { sans.push_back(std::move(entry)); } + } + } + } + cur = cur->next; + } + return true; } + +inline bool get_cert_validity(cert_t cert, time_t ¬_before, + time_t ¬_after) { + if (!cert) return false; + auto x509 = static_cast(cert); + + // Convert mbedtls_x509_time to time_t + auto to_time_t = [](const mbedtls_x509_time &t) -> time_t { + struct tm tm_time = {}; + tm_time.tm_year = t.year - 1900; + tm_time.tm_mon = t.mon - 1; + tm_time.tm_mday = t.day; + tm_time.tm_hour = t.hour; + tm_time.tm_min = t.min; + tm_time.tm_sec = t.sec; +#ifdef _WIN32 + return _mkgmtime(&tm_time); +#else + return timegm(&tm_time); #endif + }; -inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } -inline void Client::set_follow_location(bool on) { - cli_->set_follow_location(on); + not_before = to_time_t(x509->valid_from); + not_after = to_time_t(x509->valid_to); + return true; } -inline void Client::set_path_encode(bool on) { cli_->set_path_encode(on); } +inline std::string get_cert_serial(cert_t cert) { + if (!cert) return ""; + auto x509 = static_cast(cert); -[[deprecated("Use set_path_encode instead")]] -inline void Client::set_url_encode(bool on) { - cli_->set_path_encode(on); + // Convert serial number to hex string + std::string result; + result.reserve(x509->serial.len * 2); + for (size_t i = 0; i < x509->serial.len; i++) { + char hex[3]; + snprintf(hex, sizeof(hex), "%02X", x509->serial.p[i]); + result += hex; + } + return result; } -inline void Client::set_compress(bool on) { cli_->set_compress(on); } +inline const char *get_sni(const_session_t session) { + if (!session) return nullptr; + auto msession = static_cast(session); -inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + // For server: return SNI received from client during handshake + if (!msession->sni_hostname.empty()) { + return msession->sni_hostname.c_str(); + } -inline void Client::set_interface(const std::string &intf) { - cli_->set_interface(intf); + // For client: return the hostname set via set_sni + if (!msession->hostname.empty()) { return msession->hostname.c_str(); } + + return nullptr; } -inline void Client::set_proxy(const std::string &host, int port) { - cli_->set_proxy(host, port); +inline uint64_t peek_error() { + // Mbed TLS doesn't have an error queue, return the last error + return static_cast(-impl::mbedtls_last_error()); } -inline void Client::set_proxy_basic_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_basic_auth(username, password); + +inline uint64_t get_error() { + // Mbed TLS doesn't have an error queue, return and clear the last error + uint64_t err = static_cast(-impl::mbedtls_last_error()); + impl::mbedtls_last_error() = 0; + return err; } -inline void Client::set_proxy_bearer_token_auth(const std::string &token) { - cli_->set_proxy_bearer_token_auth(token); + +inline std::string error_string(uint64_t code) { + char buf[256]; + mbedtls_strerror(-static_cast(code), buf, sizeof(buf)); + return std::string(buf); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_proxy_digest_auth(const std::string &username, - const std::string &password) { - cli_->set_proxy_digest_auth(username, password); + +inline ca_store_t create_ca_store(const char *pem, size_t len) { + auto *ca_chain = new (std::nothrow) mbedtls_x509_crt; + if (!ca_chain) { return nullptr; } + + mbedtls_x509_crt_init(ca_chain); + + // mbedtls_x509_crt_parse expects null-terminated PEM + int ret = mbedtls_x509_crt_parse(ca_chain, + reinterpret_cast(pem), + len + 1); // +1 for null terminator + if (ret != 0) { + // Try without +1 in case PEM is already null-terminated + ret = mbedtls_x509_crt_parse( + ca_chain, reinterpret_cast(pem), len); + if (ret != 0) { + mbedtls_x509_crt_free(ca_chain); + delete ca_chain; + return nullptr; + } + } + + return static_cast(ca_chain); } -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::enable_server_certificate_verification(bool enabled) { - cli_->enable_server_certificate_verification(enabled); +inline void free_ca_store(ca_store_t store) { + if (store) { + auto *ca_chain = static_cast(store); + mbedtls_x509_crt_free(ca_chain); + delete ca_chain; + } } -inline void Client::enable_server_hostname_verification(bool enabled) { - cli_->enable_server_hostname_verification(enabled); +inline bool set_ca_store(ctx_t ctx, ca_store_t store) { + if (!ctx || !store) { return false; } + auto *mbed_ctx = static_cast(ctx); + auto *ca_chain = static_cast(store); + + // Free existing CA chain + mbedtls_x509_crt_free(&mbed_ctx->ca_chain); + mbedtls_x509_crt_init(&mbed_ctx->ca_chain); + + // Copy the CA chain (deep copy) + // Parse from the raw data of the source cert + mbedtls_x509_crt *src = ca_chain; + while (src != nullptr) { + int ret = mbedtls_x509_crt_parse_der(&mbed_ctx->ca_chain, src->raw.p, + src->raw.len); + if (ret != 0) { return false; } + src = src->next; + } + + // Update the SSL config to use the new CA chain + mbedtls_ssl_conf_ca_chain(&mbed_ctx->conf, &mbed_ctx->ca_chain, nullptr); + return true; } -inline void Client::set_server_certificate_verifier( - std::function verifier) { - cli_->set_server_certificate_verifier(verifier); +inline size_t get_ca_certs(ctx_t ctx, std::vector &certs) { + certs.clear(); + if (!ctx) { return 0; } + auto *mbed_ctx = static_cast(ctx); + + // Iterate through the CA chain + mbedtls_x509_crt *cert = &mbed_ctx->ca_chain; + while (cert != nullptr && cert->raw.len > 0) { + // Create a copy of the certificate for the caller + auto *copy = new mbedtls_x509_crt; + mbedtls_x509_crt_init(copy); + int ret = mbedtls_x509_crt_parse_der(copy, cert->raw.p, cert->raw.len); + if (ret == 0) { + certs.push_back(static_cast(copy)); + } else { + mbedtls_x509_crt_free(copy); + delete copy; + } + cert = cert->next; + } + return certs.size(); } -#endif -inline void Client::set_logger(Logger logger) { - cli_->set_logger(std::move(logger)); +inline std::vector get_ca_names(ctx_t ctx) { + std::vector names; + if (!ctx) { return names; } + auto *mbed_ctx = static_cast(ctx); + + // Iterate through the CA chain + mbedtls_x509_crt *cert = &mbed_ctx->ca_chain; + while (cert != nullptr && cert->raw.len > 0) { + char buf[512]; + int ret = mbedtls_x509_dn_gets(buf, sizeof(buf), &cert->subject); + if (ret > 0) { names.push_back(buf); } + cert = cert->next; + } + return names; } -inline void Client::set_error_logger(ErrorLogger error_logger) { - cli_->set_error_logger(std::move(error_logger)); +inline bool update_server_cert(ctx_t ctx, const char *cert_pem, + const char *key_pem, const char *password) { + if (!ctx || !cert_pem || !key_pem) { return false; } + auto *mbed_ctx = static_cast(ctx); + + // Free existing certificate and key + mbedtls_x509_crt_free(&mbed_ctx->own_cert); + mbedtls_pk_free(&mbed_ctx->own_key); + mbedtls_x509_crt_init(&mbed_ctx->own_cert); + mbedtls_pk_init(&mbed_ctx->own_key); + + // Parse certificate PEM + int ret = mbedtls_x509_crt_parse( + &mbed_ctx->own_cert, reinterpret_cast(cert_pem), + strlen(cert_pem) + 1); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + // Parse private key PEM +#ifdef CPPHTTPLIB_MBEDTLS_V3 + ret = mbedtls_pk_parse_key( + &mbed_ctx->own_key, reinterpret_cast(key_pem), + strlen(key_pem) + 1, + password ? reinterpret_cast(password) : nullptr, + password ? strlen(password) : 0, mbedtls_ctr_drbg_random, + &mbed_ctx->ctr_drbg); +#else + ret = mbedtls_pk_parse_key( + &mbed_ctx->own_key, reinterpret_cast(key_pem), + strlen(key_pem) + 1, + password ? reinterpret_cast(password) : nullptr, + password ? strlen(password) : 0); +#endif + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + // Configure SSL to use the new certificate and key + ret = mbedtls_ssl_conf_own_cert(&mbed_ctx->conf, &mbed_ctx->own_cert, + &mbed_ctx->own_key); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + return true; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { - cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); +inline bool update_server_client_ca(ctx_t ctx, const char *ca_pem) { + if (!ctx || !ca_pem) { return false; } + auto *mbed_ctx = static_cast(ctx); + + // Free existing CA chain + mbedtls_x509_crt_free(&mbed_ctx->ca_chain); + mbedtls_x509_crt_init(&mbed_ctx->ca_chain); + + // Parse CA PEM + int ret = mbedtls_x509_crt_parse( + &mbed_ctx->ca_chain, reinterpret_cast(ca_pem), + strlen(ca_pem) + 1); + if (ret != 0) { + impl::mbedtls_last_error() = ret; + return false; + } + + // Update SSL config to use new CA chain + mbedtls_ssl_conf_ca_chain(&mbed_ctx->conf, &mbed_ctx->ca_chain, nullptr); + return true; } -inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { - if (is_ssl_) { - static_cast(*cli_).set_ca_cert_store(ca_cert_store); +inline bool set_verify_callback(ctx_t ctx, VerifyCallback callback) { + if (!ctx) { return false; } + auto *mbed_ctx = static_cast(ctx); + + impl::get_verify_callback() = std::move(callback); + mbed_ctx->has_verify_callback = + static_cast(impl::get_verify_callback()); + + if (mbed_ctx->has_verify_callback) { + // Set OPTIONAL mode to ensure callback is called even when verification + // is disabled (matching OpenSSL behavior where SSL_VERIFY_PEER is set) + mbedtls_ssl_conf_authmode(&mbed_ctx->conf, MBEDTLS_SSL_VERIFY_OPTIONAL); + mbedtls_ssl_conf_verify(&mbed_ctx->conf, impl::mbedtls_verify_callback, + nullptr); } else { - cli_->set_ca_cert_store(ca_cert_store); + mbedtls_ssl_conf_verify(&mbed_ctx->conf, nullptr, nullptr); } + return true; } -inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) { - set_ca_cert_store(cli_->create_ca_cert_store(ca_cert, size)); +inline long get_verify_error(const_session_t session) { + if (!session) { return -1; } + auto *msession = + static_cast(const_cast(session)); + return static_cast(mbedtls_ssl_get_verify_result(&msession->ssl)); } -inline long Client::get_openssl_verify_result() const { - if (is_ssl_) { - return static_cast(*cli_).get_openssl_verify_result(); +inline std::string verify_error_string(long error_code) { + if (error_code == 0) { return ""; } + char buf[256]; + mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", + static_cast(error_code)); + // Remove trailing newline if present + std::string result(buf); + while (!result.empty() && (result.back() == '\n' || result.back() == ' ')) { + result.pop_back(); } - return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? + return result; } -inline SSL_CTX *Client::ssl_context() const { - if (is_ssl_) { return static_cast(*cli_).ssl_context(); } - return nullptr; -} -#endif +} // namespace tls + +#endif // CPPHTTPLIB_MBEDTLS_SUPPORT // ---------------------------------------------------------------------------- diff --git a/justfile b/justfile new file mode 100644 index 0000000000..4ff3e420d4 --- /dev/null +++ b/justfile @@ -0,0 +1,21 @@ +set shell := ["bash", "-c"] + +default: list + +list: + @just --list --unsorted + +openssl: + @(cd test && make test && ./test) + @(cd test && make proxy) + +mbedtls: + @(cd test && make test_mbedtls && ./test_mbedtls) + @(cd test && make proxy_mbedtls) + +fuzz: + @(cd test && make fuzz_test) + +build: + @(cd test && make test_split) + @(cd test && make test_split_mbedtls) diff --git a/test/Makefile b/test/Makefile index 900cb56ffc..b75eac9976 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,38 +1,44 @@ CXX = clang++ CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) -DCPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address -PREFIX ?= $(shell brew --prefix) - -OPENSSL_DIR = $(PREFIX)/opt/openssl@3 -OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto - ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Darwin) + PREFIX ?= $(shell brew --prefix) + OPENSSL_DIR = $(PREFIX)/opt/openssl@3 + OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security + MBEDTLS_DIR ?= $(shell brew --prefix mbedtls@3) + MBEDTLS_SUPPORT = -DCPPHTTPLIB_MBEDTLS_SUPPORT -I$(MBEDTLS_DIR)/include -L$(MBEDTLS_DIR)/lib -lmbedtls -lmbedx509 -lmbedcrypto + MBEDTLS_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security + else + OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -lssl -lcrypto + MBEDTLS_SUPPORT = -DCPPHTTPLIB_MBEDTLS_SUPPORT -lmbedtls -lmbedx509 -lmbedcrypto endif endif ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz -BROTLI_DIR = $(PREFIX)/opt/brotli -BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec - -ZSTD_DIR = $(PREFIX)/opt/zstd -ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -I$(ZSTD_DIR)/include -L$(ZSTD_DIR)/lib -lzstd - -LIBS = -lpthread -lcurl ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Darwin) - LIBS += -framework CoreFoundation -framework CFNetwork - endif - ifneq ($(UNAME_S), Darwin) - LIBS += -lanl + # macOS: use Homebrew paths for brotli and zstd + BROTLI_DIR = $(PREFIX)/opt/brotli + BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec + ZSTD_DIR = $(PREFIX)/opt/zstd + ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -I$(ZSTD_DIR)/include -L$(ZSTD_DIR)/lib -lzstd + LIBS = -lpthread -lcurl -framework CoreFoundation -framework CFNetwork + else + # Linux: use system paths + BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -lbrotlicommon -lbrotlienc -lbrotlidec + ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -lzstd + LIBS = -lpthread -lcurl -lanl endif endif TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS) +TEST_ARGS_MBEDTLS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(MBEDTLS_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS) +TEST_ARGS_NO_TLS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS) # By default, use standalone_fuzz_target_runner. # This runner does no fuzzing, but simply executes the inputs @@ -69,6 +75,25 @@ proxy : test_proxy cd proxy && docker compose down; \ exit $$exit_code +proxy_mbedtls : test_proxy_mbedtls + @echo "Starting proxy server..." + cd proxy && \ + docker compose up -d + @echo "Waiting for proxy to be ready..." + @until nc -z localhost 3128 && nc -z localhost 3129; do sleep 1; done + @echo "Proxy servers are ready, waiting additional 5 seconds for full startup..." + @sleep 5 + @echo "Checking proxy server status..." + @cd proxy && docker compose ps + @echo "Checking proxy server logs..." + @cd proxy && docker compose logs --tail=20 + @echo "Running proxy tests (Mbed TLS)..." + ./test_proxy_mbedtls; \ + exit_code=$$?; \ + echo "Stopping proxy server..."; \ + cd proxy && docker compose down; \ + exit $$exit_code + test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS) @file $@ @@ -78,6 +103,22 @@ test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem test_split : test.cc ../httplib.h httplib.cc Makefile cert.pem $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS) +# Mbed TLS backend targets +test_mbedtls : test.cc include_httplib.cc ../httplib.h Makefile cert.pem + $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_MBEDTLS) + @file $@ + +test_split_mbedtls : test.cc ../httplib.h httplib.cc Makefile cert.pem + $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_MBEDTLS) + +# No TLS +test_no_tls : test.cc include_httplib.cc ../httplib.h Makefile + $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_NO_TLS) + @file $@ + +test_split_no_tls : test.cc ../httplib.h httplib.cc Makefile + $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_NO_TLS) + check_abi: @./check-shared-library-abi-compatibility.sh @@ -106,6 +147,9 @@ style_check: $(STYLE_CHECK_FILES) test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem $(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS) +test_proxy_mbedtls : test_proxy.cc ../httplib.h Makefile cert.pem + $(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS_MBEDTLS) + # Runs server_fuzzer.cc based on value of $(LIB_FUZZING_ENGINE). # Usage: make fuzz_test LIB_FUZZING_ENGINE=/path/to/libFuzzer fuzz_test: server_fuzzer @@ -128,5 +172,5 @@ cert.pem: ./gen-certs.sh clean: - rm -rf test test_split test_proxy server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM + rm -rf test test_split test_mbedtls test_split_mbedtls test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM diff --git a/test/gen-certs.sh b/test/gen-certs.sh index ee2a2cf310..bc91f84d0f 100755 --- a/test/gen-certs.sh +++ b/test/gen-certs.sh @@ -14,5 +14,5 @@ openssl genrsa 2048 > client.key.pem openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem openssl req -new -batch -config test.conf -key key_encrypted.pem | openssl x509 -days 3650 -req -signkey key_encrypted.pem > cert_encrypted.pem -openssl genrsa -aes256 -passout pass:test012! 2048 > client_encrypted.key.pem +openssl genrsa 2048 | openssl pkcs8 -topk8 -v1 PBE-SHA1-3DES -passout pass:test012! -out client_encrypted.key.pem openssl req -new -batch -config test.conf -key client_encrypted.key.pem -passin pass:test012! | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client_encrypted.cert.pem diff --git a/test/test.cc b/test/test.cc index 21f48698a4..d5cdf69a95 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1271,13 +1271,13 @@ class ChunkedEncodingTest : public ::testing::Test { protected: ChunkedEncodingTest() : cli_(HOST, PORT) -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif { cli_.set_connection_timeout(2); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli_.enable_server_certificate_verification(false); #endif } @@ -1323,7 +1323,7 @@ class ChunkedEncodingTest : public ::testing::Test { t_.join(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli_; SSLServer svr_; #else @@ -1391,7 +1391,7 @@ TEST(RangeTest, FromHTTPBin_Online) { auto path = std::string{"/httpbin/range/32"}; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -1451,7 +1451,7 @@ TEST(GetAddrInfoDanglingRefTest, LongTimeout) { auto host = "unresolvableaddress.local"; auto path = std::string{"/"}; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -1471,7 +1471,7 @@ TEST(GetAddrInfoDanglingRefTest, LongTimeout) { TEST(ConnectionErrorTest, InvalidHost) { auto host = "-abcde.com"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -1488,7 +1488,7 @@ TEST(ConnectionErrorTest, InvalidHost) { TEST(ConnectionErrorTest, InvalidHost2) { auto host = "httpcan.org/"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -1503,7 +1503,7 @@ TEST(ConnectionErrorTest, InvalidHost2) { TEST(ConnectionErrorTest, InvalidHostCheckResultErrorToString) { auto host = "httpcan.org/"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -1521,7 +1521,7 @@ TEST(ConnectionErrorTest, InvalidPort) { auto host = "localhost"; auto port = 44380; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host, port); #else Client cli(host, port); @@ -1537,7 +1537,7 @@ TEST(ConnectionErrorTest, InvalidPort) { TEST(ConnectionErrorTest, Timeout_Online) { auto host = "google.com"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 44380; SSLClient cli(host, port); #else @@ -1565,7 +1565,7 @@ TEST(CancelTest, NoCancel_Online) { auto path = std::string{"/httpbin/range/32"}; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -1589,7 +1589,7 @@ TEST(CancelTest, WithCancelSmallPayload_Online) { auto path = std::string{"/httpbin/range/32"}; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -1612,7 +1612,7 @@ TEST(CancelTest, WithCancelLargePayload_Online) { auto path = std::string{"/httpbin/range/65536"}; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -1961,7 +1961,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { auto path = std::string{"/httpbin/basic-auth/hello/world"}; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED auto port = 443; SSLClient cli(host, port); #else @@ -2008,7 +2008,7 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) { } } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(DigestAuthTest, FromHTTPWatch_Online) { #ifdef CPPHTTPLIB_DEFAULT_HTTPBIN auto host = "httpcan.org"; @@ -2075,7 +2075,7 @@ TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) { auto another_host = "example.com"; auto wrong_ip = "0.0.0.0"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -2091,7 +2091,7 @@ TEST(SpecifyServerIPAddressTest, RealHostname_Online) { auto host = "google.com"; auto wrong_ip = "0.0.0.0"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -2106,7 +2106,7 @@ TEST(SpecifyServerIPAddressTest, RealHostname_Online) { TEST(AbsoluteRedirectTest, Redirect_Online) { auto host = "nghttp2.org"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -2121,7 +2121,7 @@ TEST(AbsoluteRedirectTest, Redirect_Online) { TEST(RedirectTest, Redirect_Online) { auto host = "nghttp2.org"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -2136,7 +2136,7 @@ TEST(RedirectTest, Redirect_Online) { TEST(RelativeRedirectTest, Redirect_Online) { auto host = "nghttp2.org"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -2151,7 +2151,7 @@ TEST(RelativeRedirectTest, Redirect_Online) { TEST(TooManyRedirectTest, Redirect_Online) { auto host = "nghttp2.org"; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -2163,7 +2163,7 @@ TEST(TooManyRedirectTest, Redirect_Online) { EXPECT_EQ(Error::ExceedRedirectCount, res.error()); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(YahooRedirectTest, Redirect_Online) { Client cli("yahoo.com"); @@ -2225,7 +2225,6 @@ TEST(UrlWithSpace, Redirect_Online) { EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(18527U, res->get_header_value_u64("Content-Length")); } - #endif #if !defined(_WIN32) && !defined(_WIN64) @@ -2529,7 +2528,7 @@ TEST(BindServerTest, BindAndListenSeparately) { svr.stop(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(BindServerTest, BindAndListenSeparatelySSL) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE, CLIENT_CA_CERT_DIR); @@ -2538,9 +2537,7 @@ TEST(BindServerTest, BindAndListenSeparatelySSL) { ASSERT_TRUE(port > 0); svr.stop(); } -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT TEST(BindServerTest, BindAndListenSeparatelySSLEncryptedKey) { SSLServer svr(SERVER_ENCRYPTED_CERT_FILE, SERVER_ENCRYPTED_PRIVATE_KEY_FILE, nullptr, nullptr, SERVER_ENCRYPTED_PRIVATE_KEY_PASS); @@ -2549,65 +2546,70 @@ TEST(BindServerTest, BindAndListenSeparatelySSLEncryptedKey) { ASSERT_TRUE(port > 0); svr.stop(); } -#endif - -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -X509 *readCertificate(const std::string &strFileName) { - std::ifstream inStream(strFileName); - std::string strCertPEM((std::istreambuf_iterator(inStream)), - std::istreambuf_iterator()); - - if (strCertPEM.empty()) return (nullptr); - - BIO *pbCert = BIO_new(BIO_s_mem()); - BIO_write(pbCert, strCertPEM.c_str(), (int)strCertPEM.size()); - X509 *pCert = PEM_read_bio_X509(pbCert, NULL, 0, NULL); - BIO_free(pbCert); - - return (pCert); -} -EVP_PKEY *readPrivateKey(const std::string &strFileName) { - std::ifstream inStream(strFileName); - std::string strPrivateKeyPEM((std::istreambuf_iterator(inStream)), - std::istreambuf_iterator()); +TEST(BindServerTest, UpdateCertsPem) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); + int port = svr.bind_to_any_port("0.0.0.0"); + ASSERT_TRUE(svr.is_valid()); + ASSERT_TRUE(port > 0); - if (strPrivateKeyPEM.empty()) return (nullptr); + // Read PEM files + std::string cert_pem, key_pem, ca_pem; + read_file(SERVER_CERT_FILE, cert_pem); + read_file(SERVER_PRIVATE_KEY_FILE, key_pem); + read_file(CLIENT_CA_CERT_FILE, ca_pem); - BIO *pbPrivKey = BIO_new(BIO_s_mem()); - BIO_write(pbPrivKey, strPrivateKeyPEM.c_str(), (int)strPrivateKeyPEM.size()); - EVP_PKEY *pPrivateKey = PEM_read_bio_PrivateKey(pbPrivKey, NULL, NULL, NULL); - BIO_free(pbPrivKey); + // Update server certificates using PEM API + ASSERT_TRUE( + svr.update_certs_pem(cert_pem.c_str(), key_pem.c_str(), ca_pem.c_str())); - return (pPrivateKey); + ASSERT_TRUE(svr.is_valid()); + svr.stop(); } -TEST(BindServerTest, UpdateCerts) { +TEST(SSLClientServerTest, UpdateCertsPemWithClientAuth) { + // Start server with client CA (enables client auth) SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); - int port = svr.bind_to_any_port("0.0.0.0"); ASSERT_TRUE(svr.is_valid()); - ASSERT_TRUE(port > 0); - X509 *cert = readCertificate(SERVER_CERT_FILE); - X509 *ca_cert = readCertificate(CLIENT_CA_CERT_FILE); - EVP_PKEY *key = readPrivateKey(SERVER_PRIVATE_KEY_FILE); + bool handler_called = false; + svr.Get("/test", [&](const Request &req, Response &res) { + handler_called = true; + // Verify client certificate is present + auto cert = req.peer_cert(); + EXPECT_TRUE(static_cast(cert)); + res.set_content("ok", "text/plain"); + }); - ASSERT_TRUE(cert != nullptr); - ASSERT_TRUE(ca_cert != nullptr); - ASSERT_TRUE(key != nullptr); + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); - X509_STORE *cert_store = X509_STORE_new(); + svr.wait_until_ready(); - X509_STORE_add_cert(cert_store, ca_cert); + // Read PEM files + std::string cert_pem, key_pem, ca_pem; + read_file(SERVER_CERT_FILE, cert_pem); + read_file(SERVER_PRIVATE_KEY_FILE, key_pem); + read_file(CLIENT_CA_CERT_FILE, ca_pem); - svr.update_certs(cert, key, cert_store); + // Update server certificates and client CA using PEM API while server running + ASSERT_TRUE( + svr.update_certs_pem(cert_pem.c_str(), key_pem.c_str(), ca_pem.c_str())); - ASSERT_TRUE(svr.is_valid()); - svr.stop(); + // Connect with client certificate + SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); - X509_free(cert); - X509_free(ca_cert); - EVP_PKEY_free(key); + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); + ASSERT_TRUE(handler_called); + EXPECT_EQ("ok", res->body); } #endif @@ -2828,7 +2830,7 @@ TEST(NoContentTest, ContentLength) { } TEST(RoutingHandlerTest, PreAndPostRoutingHandlers) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); #else @@ -2868,7 +2870,7 @@ TEST(RoutingHandlerTest, PreAndPostRoutingHandlers) { svr.wait_until_ready(); { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(HOST, PORT); cli.enable_server_certificate_verification(false); #else @@ -2886,7 +2888,7 @@ TEST(RoutingHandlerTest, PreAndPostRoutingHandlers) { } { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(HOST, PORT); cli.enable_server_certificate_verification(false); #else @@ -2902,7 +2904,7 @@ TEST(RoutingHandlerTest, PreAndPostRoutingHandlers) { } { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(HOST, PORT); cli.enable_server_certificate_verification(false); #else @@ -3073,12 +3075,12 @@ class ServerTest : public ::testing::Test { protected: ServerTest() : cli_(HOST, PORT) -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli_.enable_server_certificate_verification(false); #endif } @@ -3791,7 +3793,7 @@ class ServerTest : public ::testing::Test { } map persons_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli_; SSLServer svr_; #else @@ -4427,7 +4429,7 @@ TEST_F(ServerTest, AlmostTooLongRequest) { auto res = cli_.Get(request.c_str()); - ASSERT_TRUE(res); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); EXPECT_EQ(StatusCode::NotFound_404, res->status); } @@ -5448,7 +5450,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip) { } TEST_F(ServerTest, PutLargeFileWithGzip2) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED std::string s = std::string("https://") + HOST + ":" + std::to_string(PORT); Client cli(s.c_str()); cli.enable_server_certificate_verification(false); @@ -5832,7 +5834,7 @@ void TestWithHeadersAndContentReceiver( } TEST_F(ServerTest, PostWithHeadersAndContentReceiver) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5846,7 +5848,7 @@ TEST_F(ServerTest, PostWithHeadersAndContentReceiver) { } TEST_F(ServerTest, PutWithHeadersAndContentReceiver) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5860,7 +5862,7 @@ TEST_F(ServerTest, PutWithHeadersAndContentReceiver) { } TEST_F(ServerTest, PatchWithHeadersAndContentReceiver) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5904,7 +5906,7 @@ void TestWithHeadersAndContentReceiverWithProgress( } TEST_F(ServerTest, PostWithHeadersAndContentReceiverWithProgress) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5918,7 +5920,7 @@ TEST_F(ServerTest, PostWithHeadersAndContentReceiverWithProgress) { } TEST_F(ServerTest, PutWithHeadersAndContentReceiverWithProgress) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5932,7 +5934,7 @@ TEST_F(ServerTest, PutWithHeadersAndContentReceiverWithProgress) { } TEST_F(ServerTest, PatchWithHeadersAndContentReceiverWithProgress) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5970,7 +5972,7 @@ void TestWithHeadersAndContentReceiverError( } TEST_F(ServerTest, PostWithHeadersAndContentReceiverError) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5984,7 +5986,7 @@ TEST_F(ServerTest, PostWithHeadersAndContentReceiverError) { } TEST_F(ServerTest, PuttWithHeadersAndContentReceiverError) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -5998,7 +6000,7 @@ TEST_F(ServerTest, PuttWithHeadersAndContentReceiverError) { } TEST_F(ServerTest, PatchWithHeadersAndContentReceiverError) { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED using ClientT = SSLClient; #else using ClientT = Client; @@ -6644,7 +6646,7 @@ TEST_F(ServerTest, SendLargeBodyAfterRequestLineError) { ASSERT_TRUE(res); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ("Hello World!", res->body); - EXPECT_LE(elapsed, 100); + EXPECT_LE(elapsed, 500); } } @@ -7656,7 +7658,7 @@ TEST(KeepAliveTest, Issue1959) { EXPECT_LT(elapsed, 5000); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(KeepAliveTest, SSLClientReconnection) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); @@ -7757,18 +7759,14 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) { } TEST(SNI_AutoDetectionTest, SNI_Logic) { + using namespace httplib::tls; + { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); svr.Get("/sni", [&](const Request &req, Response &res) { - std::string expected; - if (req.ssl) { - if (const char *sni = - SSL_get_servername(req.ssl, TLSEXT_NAMETYPE_host_name)) { - expected = sni; - } - } + std::string expected = req.sni(); EXPECT_EQ(expected, req.get_param_value("expected")); res.set_content("ok", "text/plain"); }); @@ -8045,7 +8043,7 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) { auto path = std::string{"/httpbin/range/32"}; #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli(host); #else Client cli(host); @@ -8096,7 +8094,7 @@ TEST(ServerDefaultHeadersTest, DefaultHeaders) { EXPECT_EQ("World", res->get_header_value("Hello")); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(KeepAliveTest, ReadTimeoutSSL) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); @@ -8139,12 +8137,12 @@ class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE() : cli_(HOST, PORT) -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli_.enable_server_certificate_verification(false); #endif } @@ -8165,7 +8163,7 @@ class ServerTestWithAI_PASSIVE : public ::testing::Test { t_.join(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli_; SSLServer svr_; #else @@ -8216,12 +8214,12 @@ class PayloadMaxLengthTest : public ::testing::Test { protected: PayloadMaxLengthTest() : cli_(HOST, PORT) -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli_.enable_server_certificate_verification(false); #endif } @@ -8243,7 +8241,7 @@ class PayloadMaxLengthTest : public ::testing::Test { t_.join(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli_; SSLServer svr_; #else @@ -8395,12 +8393,12 @@ class LargePayloadMaxLengthTest : public ::testing::Test { protected: LargePayloadMaxLengthTest() : cli_(HOST, PORT) -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE) #endif { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli_.enable_server_certificate_verification(false); #endif } @@ -8423,7 +8421,7 @@ class LargePayloadMaxLengthTest : public ::testing::Test { t_.join(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED SSLClient cli_; SSLServer svr_; #else @@ -8530,26 +8528,33 @@ TEST(HostAndPortPropertiesTest, NoSSLWithSimpleAPI) { ASSERT_EQ(1234, cli.port()); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(HostAndPortPropertiesTest, SSL) { httplib::SSLClient cli("www.google.com"); ASSERT_EQ("www.google.com", cli.host()); ASSERT_EQ(443, cli.port()); } -#endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -TEST(SSLClientTest, UpdateCAStore) { +TEST(SSLClientTest, UpdateCAStoreWithPem) { + // Test updating CA store multiple times using PEM-based load_ca_cert_store + std::string cert; + read_file(CA_CERT_FILE, cert); + httplib::SSLClient httplib_client("www.google.com"); - auto ca_store_1 = X509_STORE_new(); - X509_STORE_load_locations(ca_store_1, "/etc/ssl/certs/ca-certificates.crt", - nullptr); - httplib_client.set_ca_cert_store(ca_store_1); - auto ca_store_2 = X509_STORE_new(); - X509_STORE_load_locations(ca_store_2, "/etc/ssl/certs/ca-certificates.crt", - nullptr); - httplib_client.set_ca_cert_store(ca_store_2); + // Load CA store first time + httplib_client.load_ca_cert_store(cert.data(), cert.size()); + + // Load CA store second time (update) + httplib_client.load_ca_cert_store(cert.data(), cert.size()); + + // Verify client is still valid and can make connections + httplib_client.enable_server_certificate_verification(true); + auto res = httplib_client.Get("/"); + ASSERT_TRUE(res); + // Google may return 200 or 301 depending on various factors + EXPECT_TRUE(res->status == StatusCode::OK_200 || + res->status == StatusCode::MovedPermanently_301); } TEST(SSLClientTest, ServerNameIndication_Online) { @@ -8576,14 +8581,17 @@ TEST(SSLClientTest, ServerCertificateVerificationError_Online) { ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLServerVerification, res.error()); - // For SSL server verification errors, ssl_error should be 0, only - // ssl_openssl_error should be set + // Verify backend error is captured for SSLServerVerification + // This occurs when certificate verification fails + // OpenSSL: X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT (18) + // Mbed TLS: MBEDTLS_X509_BADCERT_NOT_TRUSTED or similar flags + EXPECT_NE(0UL, res.ssl_backend_error()); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // For OpenSSL, ssl_error is 0 for verification errors EXPECT_EQ(0, res.ssl_error()); - - // Verify OpenSSL error is captured for SSLServerVerification - // This occurs when SSL_get_verify_result() returns a verification failure EXPECT_EQ(static_cast(X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT), - res.ssl_openssl_error()); + res.ssl_backend_error()); +#endif } TEST(SSLClientTest, ServerHostnameVerificationError_Online) { @@ -8596,16 +8604,20 @@ TEST(SSLClientTest, ServerHostnameVerificationError_Online) { auto res = cli.Get("/"); ASSERT_TRUE(!res); - EXPECT_EQ(Error::SSLServerHostnameVerification, res.error()); + // The error type depends on when hostname verification occurs: + // - OpenSSL: SSLServerHostnameVerification (post-handshake verification) + // - Mbed TLS: SSLServerVerification (during handshake) + EXPECT_TRUE(res.error() == Error::SSLServerHostnameVerification || + res.error() == Error::SSLServerVerification); - // For SSL hostname verification errors, ssl_error should be 0, only - // ssl_openssl_error should be set + // Verify backend error is captured for hostname verification failure + EXPECT_NE(0UL, res.ssl_backend_error()); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // For OpenSSL, ssl_error is 0 for verification errors EXPECT_EQ(0, res.ssl_error()); - - // Verify OpenSSL error is captured for SSLServerHostnameVerification - // This occurs when verify_host() fails due to hostname mismatch EXPECT_EQ(static_cast(X509_V_ERR_HOSTNAME_MISMATCH), - res.ssl_openssl_error()); + res.ssl_backend_error()); +#endif } TEST(SSLClientTest, ServerCertificateVerification1_Online) { @@ -8632,18 +8644,22 @@ TEST(SSLClientTest, ServerCertificateVerification3_Online) { ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLLoadingCerts, res.error()); - // For SSL_CTX operations, ssl_error should be 0, only ssl_openssl_error + // For SSL_CTX operations, ssl_error should be 0, only ssl_backend_error // should be set EXPECT_EQ(0, res.ssl_error()); - // Verify OpenSSL error is captured for SSLLoadingCerts - // This error occurs when SSL_CTX_load_verify_locations() fails + // Verify backend error is captured for SSLLoadingCerts + // This error occurs when loading CA certificates fails + EXPECT_NE(0UL, res.ssl_backend_error()); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + // OpenSSL specific error codes: // > openssl errstr 0x80000002 // error:80000002:system library::No such file or directory // > openssl errstr 0xA000126 // error:0A000126:SSL routines::unexpected eof while reading - EXPECT_TRUE(res.ssl_openssl_error() == 0x80000002 || - res.ssl_openssl_error() == 0xA000126); + EXPECT_TRUE(res.ssl_backend_error() == 0x80000002 || + res.ssl_backend_error() == 0xA000126); +#endif } TEST(SSLClientTest, ServerCertificateVerification4) { @@ -8798,28 +8814,26 @@ TEST(SSLClientTest, Issue2251_SwappedClientCertAndKey) { // SSL error should be recorded in the Result object (this is the key fix for // Issue #2251) - auto openssl_error = res.ssl_openssl_error(); - EXPECT_NE(0u, openssl_error); + auto backend_error = res.ssl_backend_error(); + EXPECT_NE(0u, backend_error); } -TEST(SSLClientTest, Issue2251_ClientCertFileNotMatchingKey) { - // Another variant: using valid file paths but with mismatched cert/key pair - // This tests the case where files exist but contain incompatible key material - - // Using client cert with wrong key (cert2 key) - SSLClient cli("localhost", 8080, "client.cert.pem", "key.pem"); - - // Should fail validation - ASSERT_FALSE(cli.is_valid()); - - auto res = cli.Get("/"); - ASSERT_FALSE(res); - // Must report error properly, not appear as success - EXPECT_EQ(Error::SSLConnection, res.error()); +// Tests cert/key mismatch detection at the TLS context level +TEST(TlsApiTest, ClientCertKeyMismatch) { + // Test that using mismatched cert/key causes connection failure. + // We verify this at the SSLClient level rather than through internal + // TLS API functions. + SSLClient cli(HOST, PORT, "client.cert.pem", "key.pem"); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(2); - // OpenSSL error should be captured in Result - EXPECT_NE(0u, res.ssl_openssl_error()); + // The mismatch should cause a connection or handshake error + auto res = cli.Get("/test"); + // OpenSSL detects mismatch at context setup, MbedTLS at handshake + // Either way, the request should fail + EXPECT_FALSE(res); } +#endif #if 0 TEST(SSLClientTest, SetInterfaceWithINET6) { @@ -8835,10 +8849,14 @@ TEST(SSLClientTest, SetInterfaceWithINET6) { } #endif +// ClientCertPresent uses get_peer_cert() - works with all TLS backends +#ifdef CPPHTTPLIB_SSL_ENABLED void ClientCertPresent( const std::string &client_cert_file, const std::string &client_private_key_file, const std::string &client_encrypted_private_key_pass = std::string()) { + using namespace httplib::tls; + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE, CLIENT_CA_CERT_DIR); ASSERT_TRUE(svr.is_valid()); @@ -8846,23 +8864,11 @@ void ClientCertPresent( svr.Get("/test", [&](const Request &req, Response &res) { res.set_content("test", "text/plain"); - auto peer_cert = SSL_get_peer_certificate(req.ssl); - ASSERT_TRUE(peer_cert != nullptr); - - auto subject_name = X509_get_subject_name(peer_cert); - ASSERT_TRUE(subject_name != nullptr); - - std::string common_name; - { - char name[BUFSIZ]; - auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, - name, sizeof(name)); - common_name.assign(name, static_cast(name_len)); - } + auto cert = req.peer_cert(); + ASSERT_TRUE(static_cast(cert)); + std::string common_name = cert.subject_cn(); EXPECT_EQ("Common Name", common_name); - - X509_free(peer_cert); }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); @@ -8894,58 +8900,37 @@ TEST(SSLClientServerTest, ClientEncryptedCertPresent) { CLIENT_ENCRYPTED_PRIVATE_KEY_PASS); } -#if !defined(_WIN32) || defined(OPENSSL_USE_APPLINK) -void MemoryClientCertPresent( +// PEM memory-based constructor tests (works with all TLS backends) +void PemMemoryClientCertPresent( const std::string &client_cert_file, const std::string &client_private_key_file, const std::string &client_encrypted_private_key_pass = std::string()) { - auto f = fopen(SERVER_CERT_FILE, "r+"); - auto server_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); - fclose(f); - - f = fopen(SERVER_PRIVATE_KEY_FILE, "r+"); - auto server_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr); - fclose(f); - - f = fopen(CLIENT_CA_CERT_FILE, "r+"); - auto client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); - auto client_ca_cert_store = X509_STORE_new(); - X509_STORE_add_cert(client_ca_cert_store, client_cert); - X509_free(client_cert); - fclose(f); - - f = fopen(client_cert_file.c_str(), "r+"); - client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr); - fclose(f); - - f = fopen(client_private_key_file.c_str(), "r+"); - auto client_private_key = PEM_read_PrivateKey( - f, nullptr, nullptr, (void *)client_encrypted_private_key_pass.c_str()); - fclose(f); - - SSLServer svr(server_cert, server_private_key, client_ca_cert_store); + // Read PEM files into memory + std::string server_cert_pem, server_key_pem; + std::string client_ca_pem; + std::string client_cert_pem, client_key_pem; + + read_file(SERVER_CERT_FILE, server_cert_pem); + read_file(SERVER_PRIVATE_KEY_FILE, server_key_pem); + read_file(CLIENT_CA_CERT_FILE, client_ca_pem); + read_file(client_cert_file, client_cert_pem); + read_file(client_private_key_file, client_key_pem); + + // Create server with PEM memory + SSLServer::PemMemory server_pem = { + server_cert_pem.c_str(), + server_cert_pem.size(), + server_key_pem.c_str(), + server_key_pem.size(), + client_ca_pem.c_str(), + client_ca_pem.size(), + nullptr // no password for server key + }; + SSLServer svr(server_pem); ASSERT_TRUE(svr.is_valid()); - svr.Get("/test", [&](const Request &req, Response &res) { + svr.Get("/test", [&](const Request &, Response &res) { res.set_content("test", "text/plain"); - - auto peer_cert = SSL_get_peer_certificate(req.ssl); - ASSERT_TRUE(peer_cert != nullptr); - - auto subject_name = X509_get_subject_name(peer_cert); - ASSERT_TRUE(subject_name != nullptr); - - std::string common_name; - { - char name[BUFSIZ]; - auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, - name, sizeof(name)); - common_name.assign(name, static_cast(name_len)); - } - - EXPECT_EQ("Common Name", common_name); - - X509_free(peer_cert); }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); @@ -8957,31 +8942,31 @@ void MemoryClientCertPresent( svr.wait_until_ready(); - SSLClient cli(HOST, PORT, client_cert, client_private_key, - client_encrypted_private_key_pass); + // Create client with PEM memory + const char *password = client_encrypted_private_key_pass.empty() + ? nullptr + : client_encrypted_private_key_pass.c_str(); + SSLClient::PemMemory client_pem = { + client_cert_pem.c_str(), client_cert_pem.size(), client_key_pem.c_str(), + client_key_pem.size(), password}; + SSLClient cli(HOST, PORT, client_pem); cli.enable_server_certificate_verification(false); cli.set_connection_timeout(30); auto res = cli.Get("/test"); ASSERT_TRUE(res); ASSERT_EQ(StatusCode::OK_200, res->status); - - X509_free(server_cert); - EVP_PKEY_free(server_private_key); - X509_free(client_cert); - EVP_PKEY_free(client_private_key); } -TEST(SSLClientServerTest, MemoryClientCertPresent) { - MemoryClientCertPresent(CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); +TEST(SSLClientServerTest, PemMemoryClientCertPresent) { + PemMemoryClientCertPresent(CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); } -TEST(SSLClientServerTest, MemoryClientEncryptedCertPresent) { - MemoryClientCertPresent(CLIENT_ENCRYPTED_CERT_FILE, - CLIENT_ENCRYPTED_PRIVATE_KEY_FILE, - CLIENT_ENCRYPTED_PRIVATE_KEY_PASS); +TEST(SSLClientServerTest, PemMemoryClientEncryptedCertPresent) { + PemMemoryClientCertPresent(CLIENT_ENCRYPTED_CERT_FILE, + CLIENT_ENCRYPTED_PRIVATE_KEY_FILE, + CLIENT_ENCRYPTED_PRIVATE_KEY_PASS); } -#endif TEST(SSLClientServerTest, ClientCertMissing) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE, @@ -9004,16 +8989,15 @@ TEST(SSLClientServerTest, ClientCertMissing) { auto res = cli.Get("/test"); ASSERT_TRUE(!res); - EXPECT_EQ(Error::SSLServerVerification, res.error()); - - // For SSL server verification errors, ssl_error should be 0, only - // ssl_openssl_error should be set - EXPECT_EQ(0, res.ssl_error()); + // When client cert is missing and server requires it, connection fails + // Error type depends on backend implementation + EXPECT_TRUE(res.error() == Error::SSLServerVerification || + res.error() == Error::SSLConnection); - // Verify OpenSSL error is captured for SSLServerVerification + // Verify backend error is captured // Note: This test may have different error codes depending on the exact // verification failure - EXPECT_NE(0UL, res.ssl_openssl_error()); + EXPECT_NE(0UL, res.ssl_backend_error()); } TEST(SSLClientServerTest, TrustDirOptional) { @@ -9088,61 +9072,25 @@ TEST(SSLClientServerTest, SSLConnectTimeout) { auto res = cli.Get("/test"); ASSERT_TRUE(!res); EXPECT_EQ(Error::SSLConnection, res.error()); - EXPECT_EQ(SSL_ERROR_WANT_READ, res.ssl_error()); -} - -TEST(SSLClientServerTest, CustomizeServerSSLCtx) { - auto setup_ssl_ctx_callback = [](SSL_CTX &ssl_ctx) { - SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_COMPRESSION); - SSL_CTX_set_options(&ssl_ctx, - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); - SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_SSLv2); - SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_SSLv3); - SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_TLSv1); - SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_TLSv1_1); - auto ciphers = "ECDHE-RSA-AES128-SHA256:" - "ECDHE-DSS-AES128-SHA256:" - "ECDHE-RSA-AES256-SHA256:" - "ECDHE-DSS-AES256-SHA256:"; - SSL_CTX_set_cipher_list(&ssl_ctx, ciphers); - if (SSL_CTX_use_certificate_chain_file(&ssl_ctx, SERVER_CERT_FILE) != 1 || - SSL_CTX_use_PrivateKey_file(&ssl_ctx, SERVER_PRIVATE_KEY_FILE, - SSL_FILETYPE_PEM) != 1) { - return false; - } - SSL_CTX_load_verify_locations(&ssl_ctx, CLIENT_CA_CERT_FILE, - CLIENT_CA_CERT_DIR); - SSL_CTX_set_verify( - &ssl_ctx, - SSL_VERIFY_PEER | - SSL_VERIFY_FAIL_IF_NO_PEER_CERT, // SSL_VERIFY_CLIENT_ONCE, - nullptr); - return true; - }; + // Timeout results in WantRead error code (maps to backend-specific value) + EXPECT_NE(0, res.ssl_error()); +} - SSLServer svr(setup_ssl_ctx_callback); +TEST(SSLClientServerTest, CustomizeServerSSLCtxGeneric) { + // Test SSLServer with client certificate verification using the standard + // constructor (ContextSetupCallback is tested by backend-specific tests) + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE, + CLIENT_CA_CERT_DIR); ASSERT_TRUE(svr.is_valid()); svr.Get("/test", [&](const Request &req, Response &res) { res.set_content("test", "text/plain"); - auto peer_cert = SSL_get_peer_certificate(req.ssl); - ASSERT_TRUE(peer_cert != nullptr); - - auto subject_name = X509_get_subject_name(peer_cert); - ASSERT_TRUE(subject_name != nullptr); - - std::string common_name; - { - char name[BUFSIZ]; - auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, - name, sizeof(name)); - common_name.assign(name, static_cast(name_len)); - } + auto cert = req.peer_cert(); + ASSERT_TRUE(static_cast(cert)); + auto common_name = cert.subject_cn(); EXPECT_EQ("Common Name", common_name); - - X509_free(peer_cert); }); thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); @@ -9163,6 +9111,71 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtx) { ASSERT_EQ(StatusCode::OK_200, res->status); } +// Test verify_hostname for both OpenSSL and MbedTLS backends +// Verifies that wildcard matching and exact matching work consistently +TEST(SSLClientServerTest, TlsVerifyHostname) { + using namespace httplib::tls; + + // We need a running server to test against + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); + + thread t([&]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + svr.wait_until_ready(); + + bool verify_callback_called = false; + bool verify_result_wrong = false; + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(true); + cli.set_ca_cert_path(CA_CERT_FILE); + cli.set_connection_timeout(5); + + // Note: Test certificate has CN="Common Name", not "localhost" + bool verify_result_cn = false; + cli.set_server_certificate_verifier([&](const VerifyContext &ctx) -> bool { + verify_callback_called = true; + + if (!ctx.cert) return false; + + // Test 1: "Common Name" should match (our test server cert CN) + verify_result_cn = ctx.check_hostname("Common Name"); + + // Test 2: wrong hostname should not match + verify_result_wrong = ctx.check_hostname("wronghost.example.com"); + + return true; // Accept for the purpose of this test + }); + + auto res = cli.Get("/test"); + // The request may succeed or fail depending on cert configuration + // but the callback should have been called + + ASSERT_TRUE(verify_callback_called) + << "Verify callback should have been called"; + + // CN="Common Name" should match our test certificate + EXPECT_TRUE(verify_result_cn) + << "verify_hostname should match 'Common Name' (certificate CN)"; + + // Wrong hostname should not match + EXPECT_FALSE(verify_result_wrong) + << "verify_hostname should not match 'wronghost.example.com'"; +} +#endif + +// mbedTLS-specific callback constructor test +// Tests that the void* callback can customize TLS settings via MbedTlsContext + +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(SSLClientServerTest, ClientCAListSentToClient) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); ASSERT_TRUE(svr.is_valid()); @@ -9195,39 +9208,35 @@ TEST(SSLClientServerTest, ClientCAListSentToClient) { ASSERT_TRUE(client_cert_verified); EXPECT_EQ("success", res->body); } +#endif +// ClientCAListSetInContext uses get_peer_cert() - works with all TLS +// backends +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(SSLClientServerTest, ClientCAListSetInContext) { + using namespace httplib::tls; + // Test that when client CA cert file is provided, - // SSL_CTX_set_client_CA_list is called and the CA list is properly set + // the server properly requests and validates client certificates // Create a server with client authentication SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); ASSERT_TRUE(svr.is_valid()); - // We can't directly access the SSL_CTX from SSLServer to verify, - // but we can test that the server properly requests client certificates - // and accepts valid ones from the specified CA - bool handler_called = false; svr.Get("/test", [&](const Request &req, Response &res) { handler_called = true; // Verify that a client certificate was provided - auto peer_cert = SSL_get_peer_certificate(req.ssl); - ASSERT_TRUE(peer_cert != nullptr); + auto cert = req.peer_cert(); + ASSERT_TRUE(static_cast(cert)); // Get the issuer name - auto issuer_name = X509_get_issuer_name(peer_cert); - ASSERT_TRUE(issuer_name != nullptr); - - char issuer_buf[256]; - X509_NAME_oneline(issuer_name, issuer_buf, sizeof(issuer_buf)); + std::string issuer_str = cert.issuer_name(); + ASSERT_FALSE(issuer_str.empty()); // The client certificate should be issued by our test CA - std::string issuer_str(issuer_buf); EXPECT_TRUE(issuer_str.find("Root CA Name") != std::string::npos); - - X509_free(peer_cert); res.set_content("authenticated", "text/plain"); }); @@ -9252,32 +9261,160 @@ TEST(SSLClientServerTest, ClientCAListSetInContext) { EXPECT_EQ("authenticated", res->body); } -TEST(SSLClientServerTest, ClientCAListLoadErrorRecorded) { - // Test 1: Valid CA file - no error should be recorded - { - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, - CLIENT_CA_CERT_FILE); - ASSERT_TRUE(svr.is_valid()); - - // With valid setup, last_ssl_error should be 0 - EXPECT_EQ(0, svr.ssl_last_error()); - } +TEST(TlsCertIntrospectionTest, GetCertSANs) { + using namespace httplib::tls; - // Test 2: Invalid CA file content - // When SSL_load_client_CA_file fails, last_ssl_error_ should be set - { - // Create a temporary file with completely invalid content - const char *temp_invalid_ca = "./temp_invalid_ca_for_test.txt"; - { - std::ofstream ofs(temp_invalid_ca); - ofs << "This is not a certificate file at all\n"; - ofs << "Just plain text content\n"; - } + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); - // Create server with invalid CA file - SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, temp_invalid_ca); + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); - // Clean up temporary file + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + bool cert_checked = false; + cli.set_server_certificate_verifier([&](const VerifyContext &ctx) -> bool { + if (ctx.cert) { + auto sans = ctx.sans(); + // Test certificate may or may not have SANs - just verify the API + // works If SANs exist, verify the types are valid + for (const auto &san : sans) { + EXPECT_TRUE(san.type == SanType::DNS || san.type == SanType::IP || + san.type == SanType::EMAIL || san.type == SanType::URI || + san.type == SanType::OTHER); + EXPECT_FALSE(san.value.empty()); + } + cert_checked = true; + } + return true; + }); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + EXPECT_TRUE(cert_checked); +} + +TEST(TlsCertIntrospectionTest, GetCertValidity) { + using namespace httplib::tls; + + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + bool validity_checked = false; + cli.set_server_certificate_verifier([&](const VerifyContext &ctx) -> bool { + if (ctx.cert) { + time_t not_before = 0, not_after = 0; + bool result = ctx.validity(not_before, not_after); + EXPECT_TRUE(result); + // Verify that not_before < now < not_after for a valid cert + time_t now = time(nullptr); + EXPECT_LT(not_before, now); + EXPECT_GT(not_after, now); + validity_checked = true; + } + return true; + }); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + EXPECT_TRUE(validity_checked); +} + +TEST(TlsCertIntrospectionTest, GetCertSerial) { + using namespace httplib::tls; + + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + bool serial_checked = false; + cli.set_server_certificate_verifier([&](const VerifyContext &ctx) -> bool { + if (ctx.cert) { + std::string serial = ctx.serial(); + EXPECT_FALSE(serial.empty()); + // Serial should be a hex string + for (char c : serial) { + EXPECT_TRUE((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || + (c >= 'a' && c <= 'f')); + } + serial_checked = true; + } + return true; + }); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + EXPECT_TRUE(serial_checked); +} + +TEST(SSLClientServerTest, ClientCAListLoadErrorRecorded) { + // Test 1: Valid CA file - no error should be recorded + { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, + CLIENT_CA_CERT_FILE); + ASSERT_TRUE(svr.is_valid()); + + // With valid setup, last_ssl_error should be 0 + EXPECT_EQ(0, svr.ssl_last_error()); + } + + // Test 2: Invalid CA file content + // When SSL_load_client_CA_file fails, last_ssl_error_ should be set + { + // Create a temporary file with completely invalid content + const char *temp_invalid_ca = "./temp_invalid_ca_for_test.txt"; + { + std::ofstream ofs(temp_invalid_ca); + ofs << "This is not a certificate file at all\n"; + ofs << "Just plain text content\n"; + } + + // Create server with invalid CA file + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, temp_invalid_ca); + + // Clean up temporary file std::remove(temp_invalid_ca); // When there's an SSL error (from either SSL_CTX_load_verify_locations @@ -9288,113 +9425,241 @@ TEST(SSLClientServerTest, ClientCAListLoadErrorRecorded) { } } -TEST(SSLClientServerTest, ClientCAListFromX509Store) { - // Test SSL server using X509_STORE constructor with client CA certificates - // This test verifies that Phase 2 implementation correctly extracts CA names - // from an X509_STORE and sets them in the SSL context +TEST(VerifyCallbackTest, VerifyContextFields) { + using namespace httplib::tls; + + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); - // Load the CA certificate into memory - auto bio = BIO_new_file(CLIENT_CA_CERT_FILE, "r"); - ASSERT_NE(nullptr, bio); + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); - auto ca_cert = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr); - BIO_free(bio); - ASSERT_NE(nullptr, ca_cert); + int callback_count = 0; + bool saw_leaf_cert = false; + + cli.set_server_certificate_verifier([&](const VerifyContext &ctx) -> bool { + if (ctx.cert) { + callback_count++; + // We should see at least one certificate (the leaf) + std::string cn = ctx.subject_cn(); + if (!cn.empty()) { saw_leaf_cert = true; } + // Verify context fields are populated + EXPECT_NE(ctx.session, nullptr); + EXPECT_GE(ctx.depth, 0); + } + return true; + }); - // Create an X509_STORE and add the CA certificate - auto store = X509_STORE_new(); - ASSERT_NE(nullptr, store); - ASSERT_EQ(1, X509_STORE_add_cert(store, ca_cert)); + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + EXPECT_GT(callback_count, 0); + EXPECT_TRUE(saw_leaf_cert); +} - // Load server certificate and private key - auto cert_bio = BIO_new_file(SERVER_CERT_FILE, "r"); - ASSERT_NE(nullptr, cert_bio); - auto server_cert = PEM_read_bio_X509(cert_bio, nullptr, nullptr, nullptr); - BIO_free(cert_bio); - ASSERT_NE(nullptr, server_cert); +TEST(TlsVerifyErrorTest, GetVerifyErrorString) { + using httplib::tls::TlsError; - auto key_bio = BIO_new_file(SERVER_PRIVATE_KEY_FILE, "r"); - ASSERT_NE(nullptr, key_bio); - auto server_key = PEM_read_bio_PrivateKey(key_bio, nullptr, nullptr, nullptr); - BIO_free(key_bio); - ASSERT_NE(nullptr, server_key); + // Test that verify_error_to_string returns empty for success + std::string success_str = TlsError::verify_error_to_string(0); + EXPECT_TRUE(success_str.empty()); - // Create SSLServer with X509_STORE constructor - // Note: X509_STORE ownership is transferred to SSL_CTX - SSLServer svr(server_cert, server_key, store); - ASSERT_TRUE(svr.is_valid()); + // Test that verify_error_to_string returns non-empty for error codes + // Using a common error code (certificate expired) + std::string error_str = + TlsError::verify_error_to_string(10); // X509_V_ERR_CERT_HAS_EXPIRED + EXPECT_FALSE(error_str.empty()); +} - // No SSL error should be recorded for valid setup - EXPECT_EQ(0, svr.ssl_last_error()); +TEST(SessionVerifierTest, CertificateAccepted) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); - // Set up server endpoints - svr.Get("/test-x509store", [&](const Request & /*req*/, Response &res) { + svr.Get("/test", [](const Request &, Response &res) { res.set_content("ok", "text/plain"); }); - // Start server in a thread - auto server_thread = thread([&]() { svr.listen(HOST, PORT); }); + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + svr.wait_until_ready(); - // Connect with client certificate (using constructor with paths) - SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + SSLClient cli(HOST, PORT); cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); - auto res = cli.Get("/test-x509store"); + bool callback_called = false; + cli.set_session_verifier([&](tls::session_t session) -> SSLVerifierResponse { + EXPECT_NE(session, nullptr); + callback_called = true; + return SSLVerifierResponse::CertificateAccepted; + }); + + auto res = cli.Get("/test"); ASSERT_TRUE(res); EXPECT_EQ(200, res->status); - EXPECT_EQ("ok", res->body); + EXPECT_TRUE(callback_called); +} - // Clean up - X509_free(server_cert); - EVP_PKEY_free(server_key); - X509_free(ca_cert); +TEST(SessionVerifierTest, CertificateRejected) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); - svr.stop(); - server_thread.join(); + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); + + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + bool callback_called = false; + cli.set_session_verifier([&](tls::session_t session) -> SSLVerifierResponse { + EXPECT_NE(session, nullptr); + callback_called = true; + return SSLVerifierResponse::CertificateRejected; + }); + + auto res = cli.Get("/test"); + EXPECT_FALSE(res); + EXPECT_TRUE(callback_called); } -// Disabled due to the out-of-memory problem on GitHub Actions Workflows -TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { +TEST(SessionVerifierTest, NoDecisionFallsThrough) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); - // prepare large data - std::random_device seed_gen; - std::mt19937 random(seed_gen()); - constexpr auto large_size_byte = 2147483648UL + 1048576UL; // 2GiB + 1MiB - std::vector binary(large_size_byte / sizeof(std::uint32_t)); - std::generate(binary.begin(), binary.end(), [&random]() { return random(); }); + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); + }); - // server + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + }); + + svr.wait_until_ready(); + + // NoDecisionMade with verification disabled should succeed (no default check) + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + bool callback_called = false; + cli.set_session_verifier([&](tls::session_t session) -> SSLVerifierResponse { + EXPECT_NE(session, nullptr); + callback_called = true; + return SSLVerifierResponse::NoDecisionMade; + }); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_TRUE(callback_called); +} + +TEST(SessionVerifierTest, NoDecisionWithVerificationEnabled) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(svr.is_valid()); - svr.Post("/binary", [&](const Request &req, Response &res) { - EXPECT_EQ(large_size_byte, req.body.size()); - EXPECT_EQ(0, std::memcmp(binary.data(), req.body.data(), large_size_byte)); - res.set_content(req.body, "application/octet-stream"); + svr.Get("/test", [](const Request &, Response &res) { + res.set_content("ok", "text/plain"); }); - auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); auto se = detail::scope_exit([&] { svr.stop(); - listen_thread.join(); - ASSERT_FALSE(svr.is_running()); + t.join(); }); svr.wait_until_ready(); - // client POST - SSLClient cli("localhost", PORT); + // NoDecisionMade with verification enabled should fail (self-signed cert). + // Note: On MbedTLS, the handshake itself fails before reaching the verifier, + // so we only check that the request fails, not whether the callback was + // called. + SSLClient cli(HOST, PORT); + cli.enable_server_certificate_verification(true); + cli.set_connection_timeout(30); + + cli.set_session_verifier([&](tls::session_t session) -> SSLVerifierResponse { + EXPECT_NE(session, nullptr); + return SSLVerifierResponse::NoDecisionMade; + }); + + auto res = cli.Get("/test"); + EXPECT_FALSE(res); +} + +TEST(SSLClientServerTest, ClientCAListFromPem) { + // Test SSL server using PemMemory constructor with client CA certificates + + // Read PEM files + std::string server_cert_pem, server_key_pem, client_ca_pem; + read_file(SERVER_CERT_FILE, server_cert_pem); + read_file(SERVER_PRIVATE_KEY_FILE, server_key_pem); + read_file(CLIENT_CA_CERT_FILE, client_ca_pem); + + // Create SSLServer with PemMemory constructor including client CA + SSLServer::PemMemory server_pem = { + server_cert_pem.c_str(), + server_cert_pem.size(), + server_key_pem.c_str(), + server_key_pem.size(), + client_ca_pem.c_str(), + client_ca_pem.size(), + nullptr // no password for server key + }; + SSLServer svr(server_pem); + ASSERT_TRUE(svr.is_valid()); + + // No SSL error should be recorded for valid setup + EXPECT_EQ(0, svr.ssl_last_error()); + + // Set up server endpoints + svr.Get("/test-pem-ca", [&](const Request & /*req*/, Response &res) { + res.set_content("ok", "text/plain"); + }); + + // Start server in a thread + auto server_thread = thread([&]() { svr.listen(HOST, PORT); }); + svr.wait_until_ready(); + + // Connect with client certificate (using constructor with paths) + SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); cli.enable_server_certificate_verification(false); - cli.set_read_timeout(std::chrono::seconds(100)); - cli.set_write_timeout(std::chrono::seconds(100)); - auto res = cli.Post("/binary", reinterpret_cast(binary.data()), - large_size_byte, "application/octet-stream"); - // compare - EXPECT_EQ(StatusCode::OK_200, res->status); - EXPECT_EQ(large_size_byte, res->body.size()); - EXPECT_EQ(0, std::memcmp(binary.data(), res->body.data(), large_size_byte)); + auto res = cli.Get("/test-pem-ca"); + ASSERT_TRUE(res); + EXPECT_EQ(200, res->status); + EXPECT_EQ("ok", res->body); + + svr.stop(); + server_thread.join(); } #endif @@ -9405,7 +9670,7 @@ TEST(CleanupTest, WSACleanup) { } #endif -#ifndef CPPHTTPLIB_OPENSSL_SUPPORT +#ifndef CPPHTTPLIB_SSL_ENABLED TEST(NoSSLSupport, SimpleInterface) { ASSERT_ANY_THROW(Client cli("https://yahoo.com")); } @@ -9536,7 +9801,7 @@ TEST(ServerLargeContentTest, DISABLED_SendLargeContent) { } #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(YahooRedirectTest2, SimpleInterface_Online) { Client cli("http://yahoo.com"); @@ -9718,7 +9983,9 @@ TEST(SSLClientRedirectTest, CertFile) { ASSERT_TRUE(res); ASSERT_EQ(StatusCode::OK_200, res->status); } +#endif +#ifdef CPPHTTPLIB_SSL_ENABLED // Test that set_ca_cert_store() skips system certs (consistent with // set_ca_cert_path behavior). When a custom cert store is set, only those certs // should be trusted - system certs should NOT be loaded. @@ -10532,7 +10799,6 @@ TEST(MultipartFormDataTest, AccessPartHeaders) { ASSERT_TRUE(send_request(1, req, &response)); ASSERT_EQ("200", response.substr(9, 3)); } -#endif TEST(MultipartFormDataTest, LargeHeader) { auto handled = false; @@ -10729,6 +10995,7 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) { EXPECT_EQ("val&key2=val2", res->body); } } +#endif TEST(RedirectTest, RedirectToUrlWithPlusInQueryParameters) { Server svr; @@ -10761,7 +11028,7 @@ TEST(RedirectTest, RedirectToUrlWithPlusInQueryParameters) { } } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(RedirectTest, Issue2185_Online) { SSLClient client("github.com"); client.set_follow_location(true); @@ -11166,7 +11433,7 @@ TEST(MakeHostAndPortStringTest, VariousPatterns) { detail::make_host_and_port_string("example.com", 65536, false)); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(SSLClientHostHeaderTest, Issue2301_Online) { httplib::SSLClient cli("roblox.com", 443); cli.set_follow_location(true); @@ -11540,7 +11807,7 @@ TEST(MaxTimeoutTest, ContentStream) { max_timeout_test(svr, cli, timeout, threshold); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(MaxTimeoutTest, ContentStreamSSL) { time_t timeout = 2000; time_t threshold = 1200; // SSL_shutdown is slow on some operating systems. @@ -12331,7 +12598,7 @@ TEST_F(OpenStreamTest, Zstd) { } #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED class SSLOpenStreamTest : public ::testing::Test { protected: SSLOpenStreamTest() : svr_("cert.pem", "key.pem") {} @@ -12424,7 +12691,7 @@ TEST_F(SSLOpenStreamTest, PostChunked) { auto body = read_all(handle); EXPECT_EQ("Chunked SSL Data", body); } -#endif // CPPHTTPLIB_OPENSSL_SUPPORT +#endif // CPPHTTPLIB_SSL_ENABLED //============================================================================== // Parity Tests: ensure streaming and non-streaming APIs produce identical @@ -12727,7 +12994,7 @@ TEST_F(StreamApiTest, HeadAndOptions) { } // SSL stream::* tests -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED class SSLStreamApiTest : public ::testing::Test { protected: void SetUp() override { @@ -12874,7 +13141,7 @@ TEST(ErrorHandlingTest, StreamConnectionClosed) { t.join(); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(ErrorHandlingTest, SSLStreamReadTimeout) { // Test that read timeout during SSL streaming is detected SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); @@ -14309,3 +14576,291 @@ TEST(ZipBombProtectionTest, DecompressedSizeExceedsLimit) { EXPECT_EQ(StatusCode::BadRequest_400, res->status); } #endif + +// ============================================================================ +// OpenSSL-Specific Tests +// ============================================================================ + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +X509 *readCertificate(const std::string &strFileName) { + std::ifstream inStream(strFileName); + std::string strCertPEM((std::istreambuf_iterator(inStream)), + std::istreambuf_iterator()); + + if (strCertPEM.empty()) return (nullptr); + + BIO *pbCert = BIO_new(BIO_s_mem()); + BIO_write(pbCert, strCertPEM.c_str(), (int)strCertPEM.size()); + X509 *pCert = PEM_read_bio_X509(pbCert, NULL, 0, NULL); + BIO_free(pbCert); + + return (pCert); +} + +EVP_PKEY *readPrivateKey(const std::string &strFileName) { + std::ifstream inStream(strFileName); + std::string strPrivateKeyPEM((std::istreambuf_iterator(inStream)), + std::istreambuf_iterator()); + + if (strPrivateKeyPEM.empty()) return (nullptr); + + BIO *pbPrivKey = BIO_new(BIO_s_mem()); + BIO_write(pbPrivKey, strPrivateKeyPEM.c_str(), (int)strPrivateKeyPEM.size()); + EVP_PKEY *pPrivateKey = PEM_read_bio_PrivateKey(pbPrivKey, NULL, NULL, NULL); + BIO_free(pbPrivKey); + + return (pPrivateKey); +} + +TEST(BindServerTest, UpdateCerts) { + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); + int port = svr.bind_to_any_port("0.0.0.0"); + ASSERT_TRUE(svr.is_valid()); + ASSERT_TRUE(port > 0); + + X509 *cert = readCertificate(SERVER_CERT_FILE); + X509 *ca_cert = readCertificate(CLIENT_CA_CERT_FILE); + EVP_PKEY *key = readPrivateKey(SERVER_PRIVATE_KEY_FILE); + + ASSERT_TRUE(cert != nullptr); + ASSERT_TRUE(ca_cert != nullptr); + ASSERT_TRUE(key != nullptr); + + X509_STORE *cert_store = X509_STORE_new(); + + X509_STORE_add_cert(cert_store, ca_cert); + + // svr.update_certs(cert, key, cert_store); // deprecated + svr.update_certs_pem(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, + CLIENT_CA_CERT_FILE); + + ASSERT_TRUE(svr.is_valid()); + svr.stop(); + + X509_free(cert); + X509_free(ca_cert); + EVP_PKEY_free(key); +} + +// Test that SSLServer(X509*, EVP_PKEY*, X509_STORE*) constructor sets +// client CA list correctly for TLS handshake +TEST(SSLClientServerTest, X509ConstructorSetsClientCAList) { + X509 *cert = readCertificate(SERVER_CERT_FILE); + X509 *ca_cert = readCertificate(CLIENT_CA_CERT_FILE); + EVP_PKEY *key = readPrivateKey(SERVER_PRIVATE_KEY_FILE); + + ASSERT_TRUE(cert != nullptr); + ASSERT_TRUE(ca_cert != nullptr); + ASSERT_TRUE(key != nullptr); + + X509_STORE *cert_store = X509_STORE_new(); + X509_STORE_add_cert(cert_store, ca_cert); + + // Use X509-based constructor (deprecated but should still work correctly) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + SSLServer svr(cert, key, cert_store); +#pragma GCC diagnostic pop + + ASSERT_TRUE(svr.is_valid()); + + // Verify that client CA list is set in SSL_CTX + auto ssl_ctx = static_cast(svr.tls_context()); + ASSERT_TRUE(ssl_ctx != nullptr); + + STACK_OF(X509_NAME) *ca_list = SSL_CTX_get_client_CA_list(ssl_ctx); + ASSERT_TRUE(ca_list != nullptr); + EXPECT_GT(sk_X509_NAME_num(ca_list), 0); + + X509_free(cert); + X509_free(ca_cert); + EVP_PKEY_free(key); +} + +// Test that update_certs() updates client CA list correctly +TEST(SSLClientServerTest, UpdateCertsSetsClientCAList) { + // Start with file-based constructor + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + // Initially no client CA list should be set + auto ssl_ctx = static_cast(svr.tls_context()); + ASSERT_TRUE(ssl_ctx != nullptr); + + STACK_OF(X509_NAME) *ca_list_before = SSL_CTX_get_client_CA_list(ssl_ctx); + int count_before = ca_list_before ? sk_X509_NAME_num(ca_list_before) : 0; + EXPECT_EQ(0, count_before); + + // Now update with client CA (PEM string) + std::string cert_pem, key_pem, ca_pem; + read_file(SERVER_CERT_FILE, cert_pem); + read_file(SERVER_PRIVATE_KEY_FILE, key_pem); + read_file(CLIENT_CA_CERT_FILE, ca_pem); + + svr.update_certs_pem(cert_pem.c_str(), key_pem.c_str(), ca_pem.c_str()); + + ASSERT_TRUE(svr.is_valid()); + + // Now client CA list should be set + STACK_OF(X509_NAME) *ca_list_after = SSL_CTX_get_client_CA_list(ssl_ctx); + ASSERT_TRUE(ca_list_after != nullptr); + EXPECT_GT(sk_X509_NAME_num(ca_list_after), 0); +} + +TEST(SSLClientServerTest, FilePathConstructorSetsClientCAList) { + // Test that the file-path SSLServer constructor properly sets the client CA + // list that is sent to clients during the TLS handshake (CertificateRequest) + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE); + ASSERT_TRUE(svr.is_valid()); + + auto ssl_ctx = static_cast(svr.tls_context()); + ASSERT_TRUE(ssl_ctx != nullptr); + + STACK_OF(X509_NAME) *ca_list = SSL_CTX_get_client_CA_list(ssl_ctx); + ASSERT_TRUE(ca_list != nullptr); + EXPECT_GT(sk_X509_NAME_num(ca_list), 0); +} + +// Disabled due to the out-of-memory problem on GitHub Actions Workflows +TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) { + + // prepare large data + std::random_device seed_gen; + std::mt19937 random(seed_gen()); + constexpr auto large_size_byte = 2147483648UL + 1048576UL; // 2GiB + 1MiB + std::vector binary(large_size_byte / sizeof(std::uint32_t)); + std::generate(binary.begin(), binary.end(), [&random]() { return random(); }); + + // server + SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(svr.is_valid()); + + svr.Post("/binary", [&](const Request &req, Response &res) { + EXPECT_EQ(large_size_byte, req.body.size()); + EXPECT_EQ(0, std::memcmp(binary.data(), req.body.data(), large_size_byte)); + res.set_content(req.body, "application/octet-stream"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + // client POST + SSLClient cli("localhost", PORT); + cli.enable_server_certificate_verification(false); + cli.set_read_timeout(std::chrono::seconds(100)); + cli.set_write_timeout(std::chrono::seconds(100)); + auto res = cli.Post("/binary", reinterpret_cast(binary.data()), + large_size_byte, "application/octet-stream"); + + // compare + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(large_size_byte, res->body.size()); + EXPECT_EQ(0, std::memcmp(binary.data(), res->body.data(), large_size_byte)); +} +#endif + +// ============================================================================ +// MbedTLS-Specific Tests +// ============================================================================ + +#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT +TEST(SSLClientServerTest, CustomizeServerSSLCtxMbedTLS) { + using namespace httplib::tls; + + // Track if callback was invoked + bool callback_invoked = false; + + // The callback receives void* ctx which is actually MbedTlsContext* + // We can access the mbedtls_ssl_config via the context + auto setup_callback = [&callback_invoked](void *ctx) { + callback_invoked = true; + + // Cast to MbedTlsContext* to access the ssl config + auto *mbedtls_ctx = static_cast(ctx); + mbedtls_ssl_config *conf = &mbedtls_ctx->conf; + + // Use static variables to hold certificate data (simplified for test) + static mbedtls_x509_crt own_cert; + static mbedtls_pk_context own_key; + static mbedtls_x509_crt ca_chain; + static bool initialized = false; + + if (!initialized) { + mbedtls_x509_crt_init(&own_cert); + mbedtls_pk_init(&own_key); + mbedtls_x509_crt_init(&ca_chain); + + // Load server certificate + if (mbedtls_x509_crt_parse_file(&own_cert, SERVER_CERT_FILE) != 0) { + return false; + } + // Load server private key + if (mbedtls_pk_parse_keyfile(&own_key, SERVER_PRIVATE_KEY_FILE, nullptr +#if MBEDTLS_VERSION_MAJOR >= 3 + , + mbedtls_ctr_drbg_random, nullptr +#endif + ) != 0) { + return false; + } + // Load CA chain for client verification + if (mbedtls_x509_crt_parse_file(&ca_chain, CLIENT_CA_CERT_FILE) != 0) { + return false; + } + initialized = true; + } + + // Configure the SSL config + mbedtls_ssl_conf_own_cert(conf, &own_cert, &own_key); + mbedtls_ssl_conf_ca_chain(conf, &ca_chain, nullptr); + mbedtls_ssl_conf_authmode(conf, MBEDTLS_SSL_VERIFY_REQUIRED); + + // Set minimum TLS version using mbedTLS native API +#if MBEDTLS_VERSION_MAJOR >= 3 + mbedtls_ssl_conf_min_tls_version(conf, MBEDTLS_SSL_VERSION_TLS1_2); +#else + mbedtls_ssl_conf_min_version(conf, MBEDTLS_SSL_MAJOR_VERSION_3, + MBEDTLS_SSL_MINOR_VERSION_3); +#endif + + return true; + }; + + SSLServer svr(setup_callback); + ASSERT_TRUE(svr.is_valid()); + ASSERT_TRUE(callback_invoked); + + svr.Get("/test", [&](const Request &req, Response &res) { + res.set_content("test", "text/plain"); + + auto cert = req.peer_cert(); + ASSERT_TRUE(static_cast(cert)); + + auto common_name = cert.subject_cn(); + EXPECT_EQ("Common Name", common_name); + }); + + thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); }); + auto se = detail::scope_exit([&] { + svr.stop(); + t.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE); + cli.enable_server_certificate_verification(false); + cli.set_connection_timeout(30); + + auto res = cli.Get("/test"); + ASSERT_TRUE(res); + ASSERT_EQ(StatusCode::OK_200, res->status); +} +#endif diff --git a/test/test_proxy.cc b/test/test_proxy.cc index 80239c5f52..66c8ef6fdc 100644 --- a/test/test_proxy.cc +++ b/test/test_proxy.cc @@ -26,7 +26,7 @@ TEST(ProxyTest, NoSSLBasic) { ProxyTest(cli, true); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(ProxyTest, SSLBasic) { SSLClient cli("nghttp2.org"); ProxyTest(cli, true); @@ -51,7 +51,7 @@ void RedirectProxyText(T &cli, const char *path, bool basic) { if (basic) { cli.set_proxy_basic_auth("hello", "world"); } else { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli.set_proxy_digest_auth("hello", "world"); #endif } @@ -67,7 +67,7 @@ TEST(RedirectTest, HTTPBinNoSSLBasic) { RedirectProxyText(cli, "/httpbin/redirect/2", true); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(RedirectTest, HTTPBinNoSSLDigest) { Client cli("nghttp2.org"); RedirectProxyText(cli, "/httpbin/redirect/2", false); @@ -84,7 +84,7 @@ TEST(RedirectTest, HTTPBinSSLDigest) { } #endif -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(RedirectTest, YouTubeNoSSLBasic) { Client cli("youtube.com"); RedirectProxyText(cli, "/", true); @@ -157,7 +157,7 @@ TEST(BaseAuthTest, NoSSL) { BaseAuthTestFromHTTPWatch(cli); } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(BaseAuthTest, SSL) { SSLClient cli("httpcan.org"); BaseAuthTestFromHTTPWatch(cli); @@ -166,7 +166,7 @@ TEST(BaseAuthTest, SSL) { // ---------------------------------------------------------------------------- -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED template void DigestAuthTestFromHTTPWatch(T &cli) { cli.set_proxy("localhost", 3129); cli.set_proxy_digest_auth("hello", "world"); @@ -230,13 +230,13 @@ template void KeepAliveTest(T &cli, bool basic) { if (basic) { cli.set_proxy_basic_auth("hello", "world"); } else { -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli.set_proxy_digest_auth("hello", "world"); #endif } cli.set_follow_location(true); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED cli.set_digest_auth("hello", "world"); #endif @@ -274,7 +274,7 @@ template void KeepAliveTest(T &cli, bool basic) { } } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +#ifdef CPPHTTPLIB_SSL_ENABLED TEST(KeepAliveTest, NoSSLWithBasic) { Client cli("nghttp2.org"); KeepAliveTest(cli, true); From 1942e0ef0171711b5a157ea29ec6dddc079df5be Mon Sep 17 00:00:00 2001 From: Miko <110693261+mikomikotaishi@users.noreply.github.com> Date: Mon, 2 Feb 2026 11:27:09 -0500 Subject: [PATCH 0606/1222] Add C++ modules support (#2291) * Add C++ modules support * Add module examples * Missing semicolon * Update GitHub Actions script and create a modules updating script * Name the unused param * Use the guarded/direct export of header approach * Update CMakeLists.txt Co-authored-by: Andrea Pappacoda * Update CMakeLists.txt Co-authored-by: Andrea Pappacoda * Split scripts into split.py and generate_module.py --------- Co-authored-by: Andrea Pappacoda --- CMakeLists.txt | 51 ++++++++++++++++++++++++++++-- cmake/modules.cmake | 16 ++++++++++ generate_module.py | 77 +++++++++++++++++++++++++++++++++++++++++++++ split.py | 26 ++++++++------- 4 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 cmake/modules.cmake create mode 100644 generate_module.py diff --git a/CMakeLists.txt b/CMakeLists.txt index e51611e078..45a0e16b98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ * HTTPLIB_USE_ZLIB_IF_AVAILABLE (default on) * HTTPLIB_USE_BROTLI_IF_AVAILABLE (default on) * HTTPLIB_USE_ZSTD_IF_AVAILABLE (default on) + * HTTPLIB_BUILD_MODULES (default off) * HTTPLIB_REQUIRE_OPENSSL (default off) * HTTPLIB_REQUIRE_ZLIB (default off) * HTTPLIB_REQUIRE_BROTLI (default off) @@ -110,6 +111,15 @@ option(HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN "Enable feature to load system cer option(HTTPLIB_USE_NON_BLOCKING_GETADDRINFO "Enables the non-blocking alternatives for getaddrinfo." ON) option(HTTPLIB_REQUIRE_ZSTD "Requires ZSTD to be found & linked, or fails build." OFF) option(HTTPLIB_USE_ZSTD_IF_AVAILABLE "Uses ZSTD (if available) to enable zstd support." ON) +# C++20 modules support requires CMake 3.28 or later +if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.28") + option(HTTPLIB_BUILD_MODULES "Build httplib modules (requires HTTPLIB_COMPILE to be ON)." OFF) +else() + set(HTTPLIB_BUILD_MODULES OFF CACHE INTERNAL "Build httplib modules disabled (requires CMake 3.28+)" FORCE) + if(DEFINED CACHE{HTTPLIB_BUILD_MODULES} AND HTTPLIB_BUILD_MODULES) + message(WARNING "HTTPLIB_BUILD_MODULES requires CMake 3.28 or later. Current version is ${CMAKE_VERSION}. Modules support has been disabled.") + endif() +endif() # Defaults to static library but respects standard BUILD_SHARED_LIBS if set include(CMakeDependentOption) cmake_dependent_option(HTTPLIB_SHARED "Build the library as a shared library instead of static. Has no effect if using header-only." @@ -240,6 +250,22 @@ if(HTTPLIB_COMPILE) message(FATAL_ERROR "Failed when trying to split cpp-httplib with the Python script.\n${_httplib_split_error}") endif() + # If building modules, also generate the module file + if(HTTPLIB_BUILD_MODULES) + # Put the generate_module script into the build dir + configure_file(generate_module.py "${CMAKE_CURRENT_BINARY_DIR}/generate_module.py" + COPYONLY + ) + # Generate the module file + execute_process(COMMAND ${Python3_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/generate_module.py" + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ERROR_VARIABLE _httplib_module_error + ) + if(_httplib_module_error) + message(FATAL_ERROR "Failed when trying to generate cpp-httplib module with the Python script.\n${_httplib_module_error}") + endif() + endif() + # split.py puts output in "out" set(_httplib_build_includedir "${CMAKE_CURRENT_BINARY_DIR}/out") add_library(${PROJECT_NAME} ${HTTPLIB_LIB_TYPE} "${_httplib_build_includedir}/httplib.cc") @@ -248,6 +274,13 @@ if(HTTPLIB_COMPILE) $ $ ) + + # Add C++20 module support if requested + # Include from separate file to prevent parse errors on older CMake versions + if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.28") + include(cmake/modules.cmake) + endif() + set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} @@ -264,8 +297,12 @@ endif() # Only useful if building in-tree, versus using it from an installation. add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) -# Require C++11 -target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11) +# Require C++11, or C++20 if modules are enabled +if(HTTPLIB_BUILD_MODULES) + target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_20) +else() + target_compile_features(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC} cxx_std_11) +endif() target_include_directories(${PROJECT_NAME} SYSTEM ${_INTERFACE_OR_PUBLIC} $ @@ -337,7 +374,11 @@ if(HTTPLIB_INSTALL) # Creates the export httplibTargets.cmake # This is strictly what holds compilation requirements # and linkage information (doesn't find deps though). - install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets) + if(HTTPLIB_BUILD_MODULES) + install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/httplib/modules CXX_MODULES_BMI DESTINATION ${CMAKE_INSTALL_LIBDIR}/httplib/modules) + else() + install(TARGETS ${PROJECT_NAME} EXPORT httplibTargets) + endif() install(FILES "${_httplib_build_includedir}/httplib.h" TYPE INCLUDE) @@ -366,6 +407,10 @@ if(HTTPLIB_INSTALL) include(CPack) endif() +if(HTTPLIB_BUILD_MODULES AND NOT HTTPLIB_COMPILE) + message(FATAL_ERROR "HTTPLIB_BUILD_MODULES requires HTTPLIB_COMPILE to be ON.") +endif() + if(HTTPLIB_TEST) include(CTest) add_subdirectory(test) diff --git a/cmake/modules.cmake b/cmake/modules.cmake new file mode 100644 index 0000000000..bea44520cd --- /dev/null +++ b/cmake/modules.cmake @@ -0,0 +1,16 @@ +# This file contains C++20 module support requiring CMake 3.28+ +# Included conditionally to prevent parse errors on older CMake versions + +if(HTTPLIB_BUILD_MODULES) + if(POLICY CMP0155) + cmake_policy(SET CMP0155 NEW) + endif() + + set(CMAKE_CXX_SCAN_FOR_MODULES ON) + + target_sources(${PROJECT_NAME} + PUBLIC + FILE_SET CXX_MODULES FILES + "${_httplib_build_includedir}/httplib.cppm" + ) +endif() diff --git a/generate_module.py b/generate_module.py new file mode 100644 index 0000000000..c6e71d931a --- /dev/null +++ b/generate_module.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +"""This script generates httplib.cppm module file from httplib.h.""" + +import os +import sys +from argparse import ArgumentParser, Namespace +from typing import List + +def main() -> None: + """Main entry point for the script.""" + + args_parser: ArgumentParser = ArgumentParser(description=__doc__) + args_parser.add_argument( + "-o", "--out", help="where to write the files (default: out)", default="out" + ) + args: Namespace = args_parser.parse_args() + + cur_dir: str = os.path.dirname(sys.argv[0]) + if not cur_dir: + cur_dir = '.' + lib_name: str = "httplib" + header_name: str = f"/{lib_name}.h" + # get the input file + in_file: str = f"{cur_dir}{header_name}" + # get the output file + cppm_out: str = f"{args.out}/{lib_name}.cppm" + + # if the modification time of the out file is after the in file, + # don't generate (as it is already finished) + do_generate: bool = True + + if os.path.exists(cppm_out): + in_time: float = os.path.getmtime(in_file) + out_time: float = os.path.getmtime(cppm_out) + do_generate: bool = in_time > out_time + + if do_generate: + with open(in_file) as f: + lines: List[str] = f.readlines() + + os.makedirs(args.out, exist_ok=True) + + # Find the Headers and Declaration comment markers + headers_start: int = -1 + declaration_start: int = -1 + for i, line in enumerate(lines): + if ' * Headers' in line: + headers_start = i - 1 # Include the /* line + elif ' * Declaration' in line: + declaration_start = i - 1 # Stop before the /* line + break + + with open(cppm_out, 'w') as fm: + # Write module file + fm.write("module;\n\n") + + # Write global module fragment (from Headers to Declaration comment) + # Filter out 'using' declarations to avoid conflicts + if headers_start >= 0 and declaration_start >= 0: + for i in range(headers_start, declaration_start): + line: str = lines[i] + if 'using' not in line: + fm.write(line) + + fm.write("\nexport module httplib;\n\n") + fm.write("export extern \"C++\" {\n") + fm.write(f"{' ' * 4}#include \"httplib.h\"\n") + fm.write("}\n") + + print(f"Wrote {cppm_out}") + else: + print(f"{cppm_out} is up to date") + + +if __name__ == "__main__": + main() diff --git a/split.py b/split.py index d3682ef42d..660b45a0cf 100755 --- a/split.py +++ b/split.py @@ -7,15 +7,14 @@ from argparse import ArgumentParser, Namespace from typing import List +BORDER: str = '// ----------------------------------------------------------------------------' def main() -> None: """Main entry point for the script.""" - BORDER: str = '// ----------------------------------------------------------------------------' args_parser: ArgumentParser = ArgumentParser(description=__doc__) args_parser.add_argument( - "-e", "--extension", help="extension of the implementation file (default: cc)", - default="cc" + "-e", "--extension", help="extension of the implementation file (default: cc)", default="cc" ) args_parser.add_argument( "-o", "--out", help="where to write the files (default: out)", default="out" @@ -25,14 +24,14 @@ def main() -> None: cur_dir: str = os.path.dirname(sys.argv[0]) if not cur_dir: cur_dir = '.' - lib_name: str = 'httplib' + lib_name: str = "httplib" header_name: str = f"/{lib_name}.h" source_name: str = f"/{lib_name}.{args.extension}" # get the input file - in_file: str = cur_dir + header_name + in_file: str = f"{cur_dir}{header_name}" # get the output file - h_out: str = args.out + header_name - cc_out: str = args.out + source_name + h_out: str = f"{args.out}{header_name}" + cc_out: str = f"{args.out}{source_name}" # if the modification time of the out file is after the in file, # don't split (as it is already finished) @@ -51,18 +50,23 @@ def main() -> None: in_implementation: bool = False cc_out: str = args.out + source_name + with open(h_out, 'w') as fh, open(cc_out, 'w') as fc: - fc.write('#include "httplib.h"\n') - fc.write('namespace httplib {\n') + # Write source file + fc.write("#include \"httplib.h\"\n") + fc.write("namespace httplib {\n") + + # Process lines for header and source split for line in lines: is_border_line: bool = BORDER in line if is_border_line: in_implementation: bool = not in_implementation elif in_implementation: - fc.write(line.replace('inline ', '')) + fc.write(line.replace("inline ", "")) else: fh.write(line) - fc.write('} // namespace httplib\n') + + fc.write("} // namespace httplib\n") print(f"Wrote {h_out} and {cc_out}") else: From 5ead179b8e9c3bd5325e1465bc34d8226e833c47 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 2 Feb 2026 00:11:38 -0500 Subject: [PATCH 0607/1222] Update README --- README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 49c97297c0..c9bd12d84b 100644 --- a/README.md +++ b/README.md @@ -97,10 +97,13 @@ cli.enable_server_hostname_verification(false); ### SSL Error Handling -When SSL operations fail, cpp-httplib provides detailed error information through two separate error fields: +When SSL operations fail, cpp-httplib provides detailed error information through `ssl_error()` and `ssl_backend_error()`: + +- `ssl_error()` - Returns the TLS-level error code (e.g., `SSL_ERROR_SSL` for OpenSSL) +- `ssl_backend_error()` - Returns the backend-specific error code (e.g., `ERR_get_error()` for OpenSSL, return value for Mbed TLS) ```c++ -#define CPPHTTPLIB_OPENSSL_SUPPORT +#define CPPHTTPLIB_OPENSSL_SUPPORT // or CPPHTTPLIB_MBEDTLS_SUPPORT #include "path/to/httplib.h" httplib::Client cli("https://example.com"); @@ -117,18 +120,18 @@ if (!res) { break; case httplib::Error::SSLLoadingCerts: - std::cout << "SSL cert loading failed, OpenSSL error: " - << std::hex << res.ssl_openssl_error() << std::endl; + std::cout << "SSL cert loading failed, backend error: " + << std::hex << res.ssl_backend_error() << std::endl; break; case httplib::Error::SSLServerVerification: - std::cout << "SSL verification failed, X509 error: " - << res.ssl_openssl_error() << std::endl; + std::cout << "SSL verification failed, verify error: " + << res.ssl_backend_error() << std::endl; break; case httplib::Error::SSLServerHostnameVerification: - std::cout << "SSL hostname verification failed, X509 error: " - << res.ssl_openssl_error() << std::endl; + std::cout << "SSL hostname verification failed, verify error: " + << res.ssl_backend_error() << std::endl; break; default: @@ -137,6 +140,50 @@ if (!res) { } ``` +### Custom Certificate Verification + +You can set a custom verification callback using `tls::VerifyCallback`: + +```c++ +httplib::Client cli("https://example.com"); + +cli.set_server_certificate_verifier( + [](const httplib::tls::VerifyContext &ctx) -> bool { + std::cout << "Subject CN: " << ctx.subject_cn() << std::endl; + std::cout << "Issuer: " << ctx.issuer_name() << std::endl; + std::cout << "Depth: " << ctx.depth << std::endl; + std::cout << "Pre-verified: " << ctx.preverify_ok << std::endl; + + // Inspect SANs (Subject Alternative Names) + for (const auto &san : ctx.sans()) { + std::cout << "SAN: " << san.value << std::endl; + } + + // Return true to accept, false to reject + return ctx.preverify_ok; + }); +``` + +### Peer Certificate Inspection + +On the server side, you can inspect the client's peer certificate from a request handler: + +```c++ +httplib::SSLServer svr("./cert.pem", "./key.pem", + "./client-ca-cert.pem"); + +svr.Get("/", [](const httplib::Request &req, httplib::Response &res) { + auto cert = req.peer_cert(); + if (cert) { + std::cout << "Client CN: " << cert.subject_cn() << std::endl; + std::cout << "Serial: " << cert.serial() << std::endl; + } + + auto sni = req.sni(); + std::cout << "SNI: " << sni << std::endl; +}); +``` + Server ------ From 4639b696ab8160c60857d20df8ae775b2cfb8991 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Feb 2026 19:30:33 -1000 Subject: [PATCH 0608/1222] Fix #2339 (#2344) * Fix #2339 * Fix CI errors * Fix Windows build error * Fix CI errors on Windows * Fix payload_max_length initialization in BodyReader * Initialize payload_max_length with CPPHTTPLIB_PAYLOAD_MAX_LENGTH in BodyReader * Update README and tests to clarify payload_max_length behavior and add no limit case * Fix server thread lambda capture in ClientVulnerabilityTest --- README.md | 11 ++ httplib.h | 133 ++++++++++++------- test/test.cc | 365 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 463 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index c9bd12d84b..33cc4e9358 100644 --- a/README.md +++ b/README.md @@ -958,6 +958,12 @@ cli.set_write_timeout(5, 0); // 5 seconds cli.set_max_timeout(5000); // 5 seconds ``` +### Set maximum payload length for reading a response body + +```c++ +cli.set_payload_max_length(1024 * 1024 * 512); // 512MB +``` + ### Receive content with a content receiver ```c++ @@ -1158,6 +1164,11 @@ httplib::Server svr; svr.listen("127.0.0.1", 8080); ``` +Payload Limit +------------- + +The maximum payload body size is limited to 100MB by default for both server and client. You can change it with `set_payload_max_length()` or by defining `CPPHTTPLIB_PAYLOAD_MAX_LENGTH` at compile time. Setting it to `0` disables the limit entirely. + Compression ----------- diff --git a/httplib.h b/httplib.h index 571f3d5af8..45818ef6ac 100644 --- a/httplib.h +++ b/httplib.h @@ -147,7 +147,7 @@ #endif #ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH -#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) +#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (100 * 1024 * 1024) // 100MB #endif #ifndef CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH @@ -1622,7 +1622,9 @@ struct ChunkedDecoder; struct BodyReader { Stream *stream = nullptr; + bool has_content_length = false; size_t content_length = 0; + size_t payload_max_length = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; size_t bytes_read = 0; bool chunked = false; bool eof = false; @@ -1692,6 +1694,7 @@ class ClientImpl { std::unique_ptr decompressor_; std::string decompress_buffer_; size_t decompress_offset_ = 0; + size_t decompressed_bytes_read_ = 0; }; // clang-format off @@ -1848,6 +1851,8 @@ class ClientImpl { void set_decompress(bool on); + void set_payload_max_length(size_t length); + void set_interface(const std::string &intf); void set_proxy(const std::string &host, int port); @@ -1950,6 +1955,8 @@ class ClientImpl { bool compress_ = false; bool decompress_ = true; + size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; + std::string interface_; std::string proxy_host_; @@ -2225,6 +2232,8 @@ class Client { void set_decompress(bool on); + void set_payload_max_length(size_t length); + void set_interface(const std::string &intf); void set_proxy(const std::string &host, int port); @@ -5960,14 +5969,23 @@ inline bool read_headers(Stream &strm, Headers &headers) { return true; } -inline bool read_content_with_length(Stream &strm, size_t len, - DownloadProgress progress, - ContentReceiverWithProgress out) { +enum class ReadContentResult { + Success, // Successfully read the content + PayloadTooLarge, // The content exceeds the specified payload limit + Error // An error occurred while reading the content +}; + +inline ReadContentResult read_content_with_length( + Stream &strm, size_t len, DownloadProgress progress, + ContentReceiverWithProgress out, + size_t payload_max_length = (std::numeric_limits::max)()) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; detail::BodyReader br; br.stream = &strm; + br.has_content_length = true; br.content_length = len; + br.payload_max_length = payload_max_length; br.chunked = false; br.bytes_read = 0; br.last_error = Error::Success; @@ -5977,36 +5995,27 @@ inline bool read_content_with_length(Stream &strm, size_t len, auto read_len = static_cast(len - r); auto to_read = (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ); auto n = detail::read_body_content(&strm, br, buf, to_read); - if (n <= 0) { return false; } + if (n <= 0) { + // Check if it was a payload size error + if (br.last_error == Error::ExceedMaxPayloadSize) { + return ReadContentResult::PayloadTooLarge; + } + return ReadContentResult::Error; + } - if (!out(buf, static_cast(n), r, len)) { return false; } + if (!out(buf, static_cast(n), r, len)) { + return ReadContentResult::Error; + } r += static_cast(n); if (progress) { - if (!progress(r, len)) { return false; } + if (!progress(r, len)) { return ReadContentResult::Error; } } } - return true; -} - -inline void skip_content_with_length(Stream &strm, size_t len) { - char buf[CPPHTTPLIB_RECV_BUFSIZ]; - size_t r = 0; - while (r < len) { - auto read_len = static_cast(len - r); - auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return; } - r += static_cast(n); - } + return ReadContentResult::Success; } -enum class ReadContentResult { - Success, // Successfully read the content - PayloadTooLarge, // The content exceeds the specified payload limit - Error // An error occurred while reading the content -}; - inline ReadContentResult read_content_without_length(Stream &strm, size_t payload_max_length, ContentReceiverWithProgress out) { @@ -6152,12 +6161,13 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, if (is_invalid_value) { ret = false; - } else if (len > payload_max_length) { - exceed_payload_max_length = true; - skip_content_with_length(strm, len); - ret = false; } else if (len > 0) { - ret = read_content_with_length(strm, len, std::move(progress), out); + auto result = read_content_with_length( + strm, len, std::move(progress), out, payload_max_length); + ret = (result == ReadContentResult::Success); + if (result == ReadContentResult::PayloadTooLarge) { + exceed_payload_max_length = true; + } } } @@ -8478,13 +8488,16 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) { if (!chunked) { // Content-Length based reading - if (bytes_read >= content_length) { + if (has_content_length && bytes_read >= content_length) { eof = true; return 0; } - auto remaining = content_length - bytes_read; - auto to_read = (std::min)(len, remaining); + auto to_read = len; + if (has_content_length) { + auto remaining = content_length - bytes_read; + to_read = (std::min)(len, remaining); + } auto n = stream->read(buf, to_read); if (n < 0) { @@ -8502,7 +8515,12 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) { } bytes_read += static_cast(n); - if (bytes_read >= content_length) { eof = true; } + if (has_content_length && bytes_read >= content_length) { eof = true; } + if (payload_max_length > 0 && bytes_read > payload_max_length) { + last_error = Error::ExceedMaxPayloadSize; + eof = true; + return -1; + } return n; } @@ -8526,6 +8544,11 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) { } bytes_read += static_cast(n); + if (payload_max_length > 0 && bytes_read > payload_max_length) { + last_error = Error::ExceedMaxPayloadSize; + eof = true; + return -1; + } return n; } @@ -9682,7 +9705,7 @@ inline bool Server::read_content_core( // oversized request and fail early (causing connection close). For SSL // builds we cannot reliably peek the decrypted application bytes, so keep // the original behaviour. -#if !defined(CPPHTTPLIB_TLS_ENABLED) +#if !defined(CPPHTTPLIB_SSL_ENABLED) if (!req.has_header("Content-Length") && !detail::is_chunked_transfer_encoding(req.headers)) { // Only peek if payload_max_length is set to a finite value @@ -10572,6 +10595,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { socket_options_ = rhs.socket_options_; compress_ = rhs.compress_; decompress_ = rhs.decompress_; + payload_max_length_ = rhs.payload_max_length_; interface_ = rhs.interface_; proxy_host_ = rhs.proxy_host_; proxy_port_ = rhs.proxy_port_; @@ -10999,9 +11023,11 @@ ClientImpl::open_stream(const std::string &method, const std::string &path, } handle.body_reader_.stream = handle.stream_; + handle.body_reader_.payload_max_length = payload_max_length_; auto content_length_str = handle.response->get_header_value("Content-Length"); if (!content_length_str.empty()) { + handle.body_reader_.has_content_length = true; handle.body_reader_.content_length = static_cast(std::stoull(content_length_str)); } @@ -11049,6 +11075,7 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf, auto to_copy = (std::min)(len, available); std::memcpy(buf, decompress_buffer_.data() + decompress_offset_, to_copy); decompress_offset_ += to_copy; + decompressed_bytes_read_ += to_copy; return static_cast(to_copy); } @@ -11064,12 +11091,16 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf, if (n <= 0) { return n; } - bool decompress_ok = - decompressor_->decompress(compressed_buf, static_cast(n), - [this](const char *data, size_t data_len) { - decompress_buffer_.append(data, data_len); - return true; - }); + bool decompress_ok = decompressor_->decompress( + compressed_buf, static_cast(n), + [this](const char *data, size_t data_len) { + decompress_buffer_.append(data, data_len); + auto limit = body_reader_.payload_max_length; + if (decompressed_bytes_read_ + decompress_buffer_.size() > limit) { + return false; + } + return true; + }); if (!decompress_ok) { body_reader_.last_error = Error::Read; @@ -11082,6 +11113,7 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf, auto to_copy = (std::min)(len, decompress_buffer_.size()); std::memcpy(buf, decompress_buffer_.data(), to_copy); decompress_offset_ = to_copy; + decompressed_bytes_read_ += to_copy; return static_cast(to_copy); } @@ -11920,6 +11952,11 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, [&](const char *buf, size_t n, size_t /*off*/, size_t /*len*/) { assert(res.body.size() + n <= res.body.max_size()); + if (payload_max_length_ > 0 && + (res.body.size() >= payload_max_length_ || + n > payload_max_length_ - res.body.size())) { + return false; + } res.body.append(buf, n); return true; }); @@ -11948,9 +11985,9 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (res.status != StatusCode::NotModified_304) { int dummy_status; - if (!detail::read_content(strm, res, (std::numeric_limits::max)(), - dummy_status, std::move(progress), - std::move(out), decompress_)) { + if (!detail::read_content(strm, res, payload_max_length_, dummy_status, + std::move(progress), std::move(out), + decompress_)) { if (error != Error::Canceled) { error = Error::Read; } output_error_log(error, &req); return false; @@ -12897,6 +12934,10 @@ inline void ClientImpl::set_compress(bool on) { compress_ = on; } inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } +inline void ClientImpl::set_payload_max_length(size_t length) { + payload_max_length_ = length; +} + inline void ClientImpl::set_interface(const std::string &intf) { interface_ = intf; } @@ -13640,6 +13681,10 @@ inline void Client::set_compress(bool on) { cli_->set_compress(on); } inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } +inline void Client::set_payload_max_length(size_t length) { + cli_->set_payload_max_length(length); +} + inline void Client::set_interface(const std::string &intf) { cli_->set_interface(intf); } diff --git a/test/test.cc b/test/test.cc index d5cdf69a95..c485f3a7b2 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1683,6 +1683,7 @@ TEST(CancelTest, WithCancelSmallPayloadPost) { TEST(CancelTest, WithCancelLargePayloadPost) { Server svr; + svr.set_payload_max_length(200 * 1024 * 1024); svr.Post("/", [&](const Request & /*req*/, Response &res) { res.set_content(LARGE_DATA, "text/plain"); @@ -1698,6 +1699,7 @@ TEST(CancelTest, WithCancelLargePayloadPost) { svr.wait_until_ready(); Client cli(HOST, PORT); + cli.set_payload_max_length(200 * 1024 * 1024); cli.set_connection_timeout(std::chrono::seconds(5)); auto res = @@ -1762,6 +1764,7 @@ TEST(CancelTest, WithCancelSmallPayloadPut) { TEST(CancelTest, WithCancelLargePayloadPut) { Server svr; + svr.set_payload_max_length(200 * 1024 * 1024); svr.Put("/", [&](const Request & /*req*/, Response &res) { res.set_content(LARGE_DATA, "text/plain"); @@ -1777,6 +1780,7 @@ TEST(CancelTest, WithCancelLargePayloadPut) { svr.wait_until_ready(); Client cli(HOST, PORT); + cli.set_payload_max_length(200 * 1024 * 1024); cli.set_connection_timeout(std::chrono::seconds(5)); auto res = @@ -1841,6 +1845,7 @@ TEST(CancelTest, WithCancelSmallPayloadPatch) { TEST(CancelTest, WithCancelLargePayloadPatch) { Server svr; + svr.set_payload_max_length(200 * 1024 * 1024); svr.Patch("/", [&](const Request & /*req*/, Response &res) { res.set_content(LARGE_DATA, "text/plain"); @@ -1856,6 +1861,7 @@ TEST(CancelTest, WithCancelLargePayloadPatch) { svr.wait_until_ready(); Client cli(HOST, PORT); + cli.set_payload_max_length(200 * 1024 * 1024); cli.set_connection_timeout(std::chrono::seconds(5)); auto res = @@ -1920,6 +1926,7 @@ TEST(CancelTest, WithCancelSmallPayloadDelete) { TEST(CancelTest, WithCancelLargePayloadDelete) { Server svr; + svr.set_payload_max_length(200 * 1024 * 1024); svr.Delete("/", [&](const Request & /*req*/, Response &res) { res.set_content(LARGE_DATA, "text/plain"); @@ -1935,6 +1942,7 @@ TEST(CancelTest, WithCancelLargePayloadDelete) { svr.wait_until_ready(); Client cli(HOST, PORT); + cli.set_payload_max_length(200 * 1024 * 1024); cli.set_connection_timeout(std::chrono::seconds(5)); auto res = @@ -3083,9 +3091,14 @@ class ServerTest : public ::testing::Test { #ifdef CPPHTTPLIB_SSL_ENABLED cli_.enable_server_certificate_verification(false); #endif + // Allow LARGE_DATA (100MB) responses + cli_.set_payload_max_length(200 * 1024 * 1024); } virtual void SetUp() { + // Allow LARGE_DATA (100MB) tests to pass with new 100MB default limit + svr_.set_payload_max_length(200 * 1024 * 1024); + svr_.set_mount_point("/", "./www"); svr_.set_mount_point("/mount", "./www2"); svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde"); @@ -8447,8 +8460,12 @@ TEST_F(LargePayloadMaxLengthTest, ChunkedEncodingExceeds10MB) { 'B'); // 12MB payload, exceeds 10MB limit auto res = cli_.Post("/test", large_payload, "application/octet-stream"); - ASSERT_TRUE(res); - EXPECT_EQ(StatusCode::PayloadTooLarge_413, res->status); + // Server may either return 413 or close the connection + if (res) { + EXPECT_EQ(StatusCode::PayloadTooLarge_413, res->status); + } else { + SUCCEED() << "Server closed connection for payload exceeding 10MB limit"; + } } TEST_F(LargePayloadMaxLengthTest, NoContentLengthWithin10MB) { @@ -8516,6 +8533,348 @@ TEST_F(LargePayloadMaxLengthTest, NoContentLengthExceeds10MB) { } } +// Regression test for DoS vulnerability: a malicious server sending a response +// without Content-Length header must not cause unbounded memory consumption on +// the client side. The client should stop reading after a reasonable limit, +// similar to the server-side set_payload_max_length protection. +TEST(ClientVulnerabilityTest, UnboundedReadWithoutContentLength) { + constexpr size_t CLIENT_READ_LIMIT = 2 * 1024 * 1024; // 2MB safety limit + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + auto server_thread = std::thread([] { + constexpr size_t MALICIOUS_DATA_SIZE = 10 * 1024 * 1024; // 10MB from server + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 2); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&opt), +#else + &opt, +#endif + sizeof(opt)); + + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + if (cli != INVALID_SOCKET) { + char buf[4096]; + ::recv(cli, buf, sizeof(buf), 0); + + // Malicious response: no Content-Length, no chunked encoding + std::string response_header = "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n" + "\r\n"; + + ::send(cli, +#ifdef _WIN32 + static_cast(response_header.c_str()), + static_cast(response_header.size()), +#else + response_header.c_str(), response_header.size(), +#endif + 0); + + // Send 10MB of data + std::string chunk(64 * 1024, 'A'); + size_t total_sent = 0; + + while (total_sent < MALICIOUS_DATA_SIZE) { + auto to_send = std::min(chunk.size(), MALICIOUS_DATA_SIZE - total_sent); + auto sent = ::send(cli, +#ifdef _WIN32 + static_cast(chunk.c_str()), + static_cast(to_send), +#else + chunk.c_str(), to_send, +#endif + 0); + if (sent <= 0) break; + total_sent += static_cast(sent); + } + + detail::close_socket(cli); + } + detail::close_socket(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + size_t total_read = 0; + + { + Client cli("127.0.0.1", PORT + 2); + cli.set_read_timeout(5, 0); + cli.set_payload_max_length(CLIENT_READ_LIMIT); + + auto stream = cli.open_stream("GET", "/malicious"); + ASSERT_TRUE(stream.is_valid()); + + char buffer[64 * 1024]; + ssize_t n; + + while ((n = stream.read(buffer, sizeof(buffer))) > 0) { + total_read += static_cast(n); + } + } // StreamHandle and Client destroyed here, closing the socket + + server_thread.join(); + + // With set_payload_max_length, the client must stop reading before consuming + // all 10MB. The read loop should be cut off at or near the configured limit. + EXPECT_LE(total_read, CLIENT_READ_LIMIT) + << "Client read " << total_read << " bytes, exceeding the configured " + << "payload_max_length of " << CLIENT_READ_LIMIT << " bytes."; +} + +// Verify that set_payload_max_length(0) means "no limit" and allows reading +// the entire response body without truncation. +TEST(ClientVulnerabilityTest, PayloadMaxLengthZeroMeansNoLimit) { + constexpr size_t DATA_SIZE = 4 * 1024 * 1024; // 4MB from server + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + auto server_thread = std::thread([DATA_SIZE] { + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 2); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&opt), +#else + &opt, +#endif + sizeof(opt)); + + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + if (cli != INVALID_SOCKET) { + char buf[4096]; + ::recv(cli, buf, sizeof(buf), 0); + + std::string response_header = "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n" + "\r\n"; + + ::send(cli, +#ifdef _WIN32 + static_cast(response_header.c_str()), + static_cast(response_header.size()), +#else + response_header.c_str(), response_header.size(), +#endif + 0); + + std::string chunk(64 * 1024, 'A'); + size_t total_sent = 0; + + while (total_sent < DATA_SIZE) { + auto to_send = std::min(chunk.size(), DATA_SIZE - total_sent); + auto sent = ::send(cli, +#ifdef _WIN32 + static_cast(chunk.c_str()), + static_cast(to_send), +#else + chunk.c_str(), to_send, +#endif + 0); + if (sent <= 0) break; + total_sent += static_cast(sent); + } + + detail::close_socket(cli); + } + detail::close_socket(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + size_t total_read = 0; + + { + Client cli("127.0.0.1", PORT + 2); + cli.set_read_timeout(5, 0); + cli.set_payload_max_length(0); // 0 means no limit + + auto stream = cli.open_stream("GET", "/data"); + ASSERT_TRUE(stream.is_valid()); + + char buffer[64 * 1024]; + ssize_t n; + + while ((n = stream.read(buffer, sizeof(buffer))) > 0) { + total_read += static_cast(n); + } + } + + server_thread.join(); + + EXPECT_EQ(total_read, DATA_SIZE) + << "With payload_max_length(0), the client should read all " << DATA_SIZE + << " bytes without truncation, but only read " << total_read << " bytes."; +} + +#if defined(CPPHTTPLIB_ZLIB_SUPPORT) && !defined(_WIN32) +// Regression test for "zip bomb" attack on the client side: a malicious server +// sends a small gzip-compressed response that decompresses to a huge payload. +// The client must enforce payload_max_length on the decompressed size. +TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) { + constexpr size_t DECOMPRESSED_SIZE = + 10 * 1024 * 1024; // 10MB after decompression + constexpr size_t CLIENT_READ_LIMIT = 2 * 1024 * 1024; // 2MB safety limit + + // Prepare gzip-compressed data: 10MB of zeros compresses to a few KB + std::string uncompressed(DECOMPRESSED_SIZE, '\0'); + std::string compressed; + { + httplib::detail::gzip_compressor compressor; + bool ok = + compressor.compress(uncompressed.data(), uncompressed.size(), + /*last=*/true, [&](const char *buf, size_t len) { + compressed.append(buf, len); + return true; + }); + ASSERT_TRUE(ok); + } + // Sanity: compressed data should be much smaller than the decompressed size + ASSERT_LT(compressed.size(), DECOMPRESSED_SIZE / 10); + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + auto server_thread = std::thread([&compressed] { + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 3); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&opt), +#else + &opt, +#endif + sizeof(opt)); + + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + if (cli != INVALID_SOCKET) { + char buf[4096]; + ::recv(cli, buf, sizeof(buf), 0); + + // Malicious response: gzip-compressed body, no Content-Length + std::string response_header = "HTTP/1.1 200 OK\r\n" + "Content-Encoding: gzip\r\n" + "Connection: close\r\n" + "\r\n"; + + ::send(cli, +#ifdef _WIN32 + static_cast(response_header.c_str()), + static_cast(response_header.size()), +#else + response_header.c_str(), response_header.size(), +#endif + 0); + + // Send the compressed payload (small on the wire, huge when decompressed) + size_t total_sent = 0; + while (total_sent < compressed.size()) { + auto to_send = std::min(compressed.size() - total_sent, + static_cast(64 * 1024)); + auto sent = + ::send(cli, +#ifdef _WIN32 + static_cast(compressed.c_str() + total_sent), + static_cast(to_send), +#else + compressed.c_str() + total_sent, to_send, +#endif + 0); + if (sent <= 0) break; + total_sent += static_cast(sent); + } + + detail::close_socket(cli); + } + detail::close_socket(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + size_t total_decompressed = 0; + + { + Client cli("127.0.0.1", PORT + 3); + cli.set_read_timeout(5, 0); + cli.set_decompress(true); + cli.set_payload_max_length(CLIENT_READ_LIMIT); + + auto stream = cli.open_stream("GET", "/zipbomb"); + ASSERT_TRUE(stream.is_valid()); + + char buffer[64 * 1024]; + ssize_t n; + + while ((n = stream.read(buffer, sizeof(buffer))) > 0) { + total_decompressed += static_cast(n); + } + } + + server_thread.join(); + + // The decompressed size must be capped by payload_max_length. Without + // protection, the client would decompress the full 10MB from a tiny + // compressed payload, enabling a zip bomb DoS attack. + EXPECT_LE(total_decompressed, CLIENT_READ_LIMIT) + << "Client decompressed " << total_decompressed + << " bytes from a gzip response. The decompressed size should be " + << "limited by set_payload_max_length to prevent zip bomb attacks."; +} +#endif + TEST(HostAndPortPropertiesTest, NoSSL) { httplib::Client cli("www.google.com", 1234); ASSERT_EQ("www.google.com", cli.host()); @@ -12204,6 +12563,7 @@ TEST(ForwardedHeadersTest, HandlesWhitespaceAroundIPs) { EXPECT_EQ(observed_remote_addr, "203.0.113.66"); } +#ifndef _WIN32 TEST(ServerRequestParsingTest, RequestWithoutContentLengthOrTransferEncoding) { Server svr; @@ -12273,6 +12633,7 @@ TEST(ServerRequestParsingTest, RequestWithoutContentLengthOrTransferEncoding) { &resp)); EXPECT_TRUE(resp.find("HTTP/1.1 200 OK") == 0); } +#endif //============================================================================== // open_stream() Tests From 9248ce3bfee5ca6bccc612661d8cdc7bca961ce1 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Feb 2026 22:02:22 -1000 Subject: [PATCH 0609/1222] Fix problem with PayloadMaxLengthZeroMeansNoLimit --- test/test.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test.cc b/test/test.cc index c485f3a7b2..fd3b145317 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8712,6 +8712,10 @@ TEST(ClientVulnerabilityTest, PayloadMaxLengthZeroMeansNoLimit) { total_sent += static_cast(sent); } + ::shutdown(cli, SHUT_WR); + // Drain until the client closes its end, ensuring all data is delivered + char drain[1024]; + while (::recv(cli, drain, sizeof(drain), 0) > 0) {} detail::close_socket(cli); } detail::close_socket(srv); From 94b5038eb34e20457eb13c1c0101179258ea3e1d Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 6 Feb 2026 22:08:28 -1000 Subject: [PATCH 0610/1222] Fix build error on Windows --- test/test.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test.cc b/test/test.cc index fd3b145317..28889337ac 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8712,7 +8712,11 @@ TEST(ClientVulnerabilityTest, PayloadMaxLengthZeroMeansNoLimit) { total_sent += static_cast(sent); } +#ifdef _WIN32 + ::shutdown(cli, SD_SEND); +#else ::shutdown(cli, SHUT_WR); +#endif // Drain until the client closes its end, ensuring all data is delivered char drain[1024]; while (::recv(cli, drain, sizeof(drain), 0) > 0) {} From 8b4146324f36e42eeefbcb866931bccc73cccc69 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 7 Feb 2026 19:26:11 -1000 Subject: [PATCH 0611/1222] Fix #2116 (#2346) * Fix #2116 * Fix problem --- README.md | 21 +++++ httplib.h | 217 +++++++++++++++++++++++++++++++++++++++++++++------ test/test.cc | 34 +++++++- 3 files changed, 247 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 33cc4e9358..52047c1be1 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,27 @@ cli.enable_server_certificate_verification(false); cli.enable_server_hostname_verification(false); ``` +### Windows Certificate Verification + +On Windows, cpp-httplib automatically performs additional certificate verification using the Windows certificate store via CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`). This works with both OpenSSL and Mbed TLS backends, providing: + +- Real-time certificate validation integrated with Windows Update +- Certificate revocation checking +- SSL/TLS policy verification using the system certificate store (ROOT and CA) + +This feature is enabled by default and can be controlled at runtime: + +```c++ +// Disable Windows certificate verification (use only OpenSSL/Mbed TLS verification) +cli.enable_windows_certificate_verification(false); +``` + +To disable this feature at compile time, define: + +```c++ +#define CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE +``` + > [!NOTE] > When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself. diff --git a/httplib.h b/httplib.h index 45818ef6ac..a2b655b635 100644 --- a/httplib.h +++ b/httplib.h @@ -2267,6 +2267,11 @@ class Client { tls::ctx_t tls_context() const; +#if defined(_WIN32) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) + void enable_windows_certificate_verification(bool enabled); +#endif + private: bool is_ssl_ = false; #endif @@ -2384,6 +2389,11 @@ class SSLClient final : public ClientImpl { tls::ctx_t tls_context() const { return ctx_; } +#if defined(_WIN32) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) + void enable_windows_certificate_verification(bool enabled); +#endif + private: bool create_and_connect_socket(Socket &socket, Error &error) override; bool ensure_socket_connection(Socket &socket, Error &error) override; @@ -2412,6 +2422,11 @@ class SSLClient final : public ClientImpl { std::function session_verifier_; +#if defined(_WIN32) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) + bool enable_windows_cert_verification_ = true; +#endif + friend class ClientImpl; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -3680,6 +3695,7 @@ std::string get_cert_issuer_name(cert_t cert); bool get_cert_sans(cert_t cert, std::vector &sans); bool get_cert_validity(cert_t cert, time_t ¬_before, time_t ¬_after); std::string get_cert_serial(cert_t cert); +bool get_cert_der(cert_t cert, std::vector &der); const char *get_sni(const_session_t session); // CA store management @@ -7736,6 +7752,96 @@ inline bool match_hostname(const std::string &pattern, return true; } +#ifdef _WIN32 +// Verify certificate using Windows CertGetCertificateChain API. +// This provides real-time certificate validation with Windows Update +// integration, independent of the TLS backend (OpenSSL or MbedTLS). +inline bool verify_cert_with_windows_schannel( + const std::vector &der_cert, const std::string &hostname, + bool verify_hostname, unsigned long &out_error) { + if (der_cert.empty()) { return false; } + + out_error = 0; + + // Create Windows certificate context from DER data + auto cert_context = CertCreateCertificateContext( + X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, der_cert.data(), + static_cast(der_cert.size())); + + if (!cert_context) { + out_error = GetLastError(); + return false; + } + + auto cert_guard = + scope_exit([&] { CertFreeCertificateContext(cert_context); }); + + // Setup chain parameters + CERT_CHAIN_PARA chain_para = {}; + chain_para.cbSize = sizeof(chain_para); + + // Build certificate chain with revocation checking + PCCERT_CHAIN_CONTEXT chain_context = nullptr; + auto chain_result = CertGetCertificateChain( + nullptr, cert_context, nullptr, cert_context->hCertStore, &chain_para, + CERT_CHAIN_CACHE_END_CERT | CERT_CHAIN_REVOCATION_CHECK_END_CERT | + CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT, + nullptr, &chain_context); + + if (!chain_result || !chain_context) { + out_error = GetLastError(); + return false; + } + + auto chain_guard = + scope_exit([&] { CertFreeCertificateChain(chain_context); }); + + // Check if chain has errors + if (chain_context->TrustStatus.dwErrorStatus != CERT_TRUST_NO_ERROR) { + out_error = chain_context->TrustStatus.dwErrorStatus; + return false; + } + + // Verify SSL policy + SSL_EXTRA_CERT_CHAIN_POLICY_PARA extra_policy_para = {}; + extra_policy_para.cbSize = sizeof(extra_policy_para); +#ifdef AUTHTYPE_SERVER + extra_policy_para.dwAuthType = AUTHTYPE_SERVER; +#endif + + std::wstring whost; + if (verify_hostname) { + whost = u8string_to_wstring(hostname.c_str()); + extra_policy_para.pwszServerName = const_cast(whost.c_str()); + } + + CERT_CHAIN_POLICY_PARA policy_para = {}; + policy_para.cbSize = sizeof(policy_para); +#ifdef CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS + policy_para.dwFlags = CERT_CHAIN_POLICY_IGNORE_ALL_REV_UNKNOWN_FLAGS; +#else + policy_para.dwFlags = 0; +#endif + policy_para.pvExtraPolicyPara = &extra_policy_para; + + CERT_CHAIN_POLICY_STATUS policy_status = {}; + policy_status.cbSize = sizeof(policy_status); + + if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, chain_context, + &policy_para, &policy_status)) { + out_error = GetLastError(); + return false; + } + + if (policy_status.dwError != 0) { + out_error = policy_status.dwError; + return false; + } + + return true; +} +#endif // _WIN32 + } // namespace detail #endif // CPPHTTPLIB_SSL_ENABLED @@ -14100,6 +14206,13 @@ inline void SSLClient::set_session_verifier( session_verifier_ = std::move(verifier); } +#if defined(_WIN32) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) +inline void SSLClient::enable_windows_certificate_verification(bool enabled) { + enable_windows_cert_verification_ = enabled; +} +#endif + inline void SSLClient::load_ca_cert_store(const char *ca_cert, std::size_t size) { if (ctx_ && ca_cert && size > 0) { @@ -14251,6 +14364,29 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return false; } } + +#if defined(_WIN32) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) + // Additional Windows Schannel verification. + // This provides real-time certificate validation with Windows Update + // integration, working with both OpenSSL and MbedTLS backends. + // Skip when a custom CA cert is specified, as the Windows certificate + // store would not know about user-provided CA certificates. + if (enable_windows_cert_verification_ && ca_cert_file_path_.empty() && + ca_cert_dir_path_.empty() && ca_cert_pem_.empty()) { + std::vector der; + if (get_cert_der(server_cert, der)) { + unsigned long wincrypt_error = 0; + if (!detail::verify_cert_with_windows_schannel( + der, host_, server_hostname_verification_, wincrypt_error)) { + last_backend_error_ = wincrypt_error; + error = Error::SSLServerVerification; + output_error_log(error, nullptr); + return false; + } + } + } +#endif } success = true; @@ -14276,6 +14412,16 @@ inline void Client::enable_server_hostname_verification(bool enabled) { cli_->enable_server_hostname_verification(enabled); } +#if defined(_WIN32) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) +inline void Client::enable_windows_certificate_verification(bool enabled) { + if (is_ssl_) { + static_cast(*cli_).enable_windows_certificate_verification( + enabled); + } +} +#endif + inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, const std::string &ca_cert_dir_path) { cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); @@ -14729,25 +14875,28 @@ inline bool load_system_certs(ctx_t ctx) { auto ssl_ctx = static_cast(ctx); #ifdef _WIN32 - // Windows: Load from system certificate store + // Windows: Load from system certificate store (ROOT and CA) auto store = SSL_CTX_get_cert_store(ssl_ctx); if (!store) return false; - auto hStore = CertOpenSystemStoreW(NULL, L"ROOT"); - if (!hStore) return false; - bool loaded_any = false; - PCCERT_CONTEXT pContext = nullptr; - while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != - nullptr) { - const unsigned char *data = pContext->pbCertEncoded; - auto x509 = d2i_X509(nullptr, &data, pContext->cbCertEncoded); - if (x509) { - if (X509_STORE_add_cert(store, x509) == 1) { loaded_any = true; } - X509_free(x509); + static const wchar_t *store_names[] = {L"ROOT", L"CA"}; + for (auto store_name : store_names) { + auto hStore = CertOpenSystemStoreW(NULL, store_name); + if (!hStore) continue; + + PCCERT_CONTEXT pContext = nullptr; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + const unsigned char *data = pContext->pbCertEncoded; + auto x509 = d2i_X509(nullptr, &data, pContext->cbCertEncoded); + if (x509) { + if (X509_STORE_add_cert(store, x509) == 1) { loaded_any = true; } + X509_free(x509); + } } + CertCloseStore(hStore, 0); } - CertCloseStore(hStore, 0); return loaded_any; #elif defined(__APPLE__) @@ -15288,6 +15437,17 @@ inline std::string get_cert_serial(cert_t cert) { return result; } +inline bool get_cert_der(cert_t cert, std::vector &der) { + if (!cert) return false; + auto x509 = static_cast(cert); + auto len = i2d_X509(x509, nullptr); + if (len < 0) return false; + der.resize(static_cast(len)); + auto p = der.data(); + i2d_X509(x509, &p); + return true; +} + inline const char *get_sni(const_session_t session) { if (!session) return nullptr; auto ssl = static_cast(const_cast(session)); @@ -16136,17 +16296,20 @@ inline bool load_system_certs(ctx_t ctx) { bool loaded = false; #ifdef _WIN32 - // Load from Windows certificate store - HCERTSTORE hStore = CertOpenSystemStoreW(0, L"ROOT"); - if (hStore) { - PCCERT_CONTEXT pContext = nullptr; - while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != - nullptr) { - int ret = mbedtls_x509_crt_parse_der( - &mctx->ca_chain, pContext->pbCertEncoded, pContext->cbCertEncoded); - if (ret == 0) { loaded = true; } + // Load from Windows certificate store (ROOT and CA) + static const wchar_t *store_names[] = {L"ROOT", L"CA"}; + for (auto store_name : store_names) { + HCERTSTORE hStore = CertOpenSystemStoreW(0, store_name); + if (hStore) { + PCCERT_CONTEXT pContext = nullptr; + while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != + nullptr) { + int ret = mbedtls_x509_crt_parse_der( + &mctx->ca_chain, pContext->pbCertEncoded, pContext->cbCertEncoded); + if (ret == 0) { loaded = true; } + } + CertCloseStore(hStore, 0); } - CertCloseStore(hStore, 0); } #elif defined(__APPLE__) && defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN) // Load from macOS Keychain @@ -16786,6 +16949,14 @@ inline std::string get_cert_serial(cert_t cert) { return result; } +inline bool get_cert_der(cert_t cert, std::vector &der) { + if (!cert) return false; + auto crt = static_cast(cert); + if (!crt->raw.p || crt->raw.len == 0) return false; + der.assign(crt->raw.p, crt->raw.p + crt->raw.len); + return true; +} + inline const char *get_sni(const_session_t session) { if (!session) return nullptr; auto msession = static_cast(session); diff --git a/test/test.cc b/test/test.cc index 28889337ac..4636627374 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8645,13 +8645,13 @@ TEST(ClientVulnerabilityTest, UnboundedReadWithoutContentLength) { // Verify that set_payload_max_length(0) means "no limit" and allows reading // the entire response body without truncation. TEST(ClientVulnerabilityTest, PayloadMaxLengthZeroMeansNoLimit) { - constexpr size_t DATA_SIZE = 4 * 1024 * 1024; // 4MB from server + static constexpr size_t DATA_SIZE = 4 * 1024 * 1024; // 4MB from server #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif - auto server_thread = std::thread([DATA_SIZE] { + auto server_thread = std::thread([] { auto srv = ::socket(AF_INET, SOCK_STREAM, 0); default_socket_options(srv); detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); @@ -8956,9 +8956,14 @@ TEST(SSLClientTest, ServerCertificateVerificationError_Online) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT // For OpenSSL, ssl_error is 0 for verification errors EXPECT_EQ(0, res.ssl_error()); +#if !defined(_WIN32) || \ + defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) + // On non-Windows or when Windows Schannel is disabled, the error comes + // from OpenSSL's verification EXPECT_EQ(static_cast(X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT), res.ssl_backend_error()); #endif +#endif } TEST(SSLClientTest, ServerHostnameVerificationError_Online) { @@ -8982,11 +8987,36 @@ TEST(SSLClientTest, ServerHostnameVerificationError_Online) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT // For OpenSSL, ssl_error is 0 for verification errors EXPECT_EQ(0, res.ssl_error()); +#if !defined(_WIN32) || \ + defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) + // On non-Windows or when Windows Schannel is disabled, the error comes + // from OpenSSL's hostname verification EXPECT_EQ(static_cast(X509_V_ERR_HOSTNAME_MISMATCH), res.ssl_backend_error()); #endif +#endif +} + +#if defined(_WIN32) && defined(CPPHTTPLIB_SSL_ENABLED) && \ + !defined(CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE) +TEST(SSLClientTest, WindowsCertificateVerification_DefaultEnabled) { + SSLClient cli("www.google.com", 443); + cli.enable_server_certificate_verification(true); + + auto res = cli.Get("/"); + if (res) { EXPECT_NE(StatusCode::InternalServerError_500, res->status); } } +TEST(SSLClientTest, WindowsCertificateVerification_Disabled) { + SSLClient cli("www.google.com", 443); + cli.enable_server_certificate_verification(true); + cli.enable_windows_certificate_verification(false); + + auto res = cli.Get("/"); + if (res) { EXPECT_NE(StatusCode::InternalServerError_500, res->status); } +} +#endif + TEST(SSLClientTest, ServerCertificateVerification1_Online) { Client cli("https://google.com"); auto res = cli.Get("/"); From 5d717e6d917bd1074af1892d7f856b45ad291b3c Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Feb 2026 09:40:26 -1000 Subject: [PATCH 0612/1222] Resolve #2347 --- httplib.h | 6 +++-- test/Makefile | 2 +- test/test.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index a2b655b635..55dd9d6fce 100644 --- a/httplib.h +++ b/httplib.h @@ -9943,8 +9943,10 @@ inline bool Server::check_if_not_modified(const Request &req, Response &res, // simplified implementation requires exact matches. auto ret = detail::split_find(val.data(), val.data() + val.size(), ',', [&](const char *b, const char *e) { - return std::equal(b, e, "*") || - std::equal(b, e, etag.begin()); + auto seg_len = static_cast(e - b); + return (seg_len == 1 && *b == '*') || + (seg_len == etag.size() && + std::equal(b, e, etag.begin())); }); if (ret) { diff --git a/test/Makefile b/test/Makefile index b75eac9976..9269b3de6d 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ CXX = clang++ -CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) -DCPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address +CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) -DCPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO -fsanitize=address # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS ifneq ($(OS), Windows_NT) UNAME_S := $(shell uname -s) diff --git a/test/test.cc b/test/test.cc index 4636627374..4a219c129d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -13730,6 +13730,76 @@ TEST(ETagTest, StaticFileETagIfNoneMatchStarNotFound) { t.join(); } +TEST(ETagTest, IfNoneMatchBoundaryCheck) { + using namespace httplib; + + // Create a test file + const char *fname = "etag_boundary_testfile.txt"; + const char *content = "boundary-test"; + { + std::ofstream ofs(fname); + ofs << content; + ASSERT_TRUE(ofs.good()); + } + + Server svr; + svr.set_mount_point("/static", "."); + auto t = std::thread([&]() { svr.listen("localhost", PORT); }); + svr.wait_until_ready(); + + Client cli(HOST, PORT); + + // Get the actual ETag + auto res1 = cli.Get("/static/etag_boundary_testfile.txt"); + ASSERT_TRUE(res1); + ASSERT_EQ(200, res1->status); + ASSERT_TRUE(res1->has_header("ETag")); + std::string etag = res1->get_header_value("ETag"); + + // Test 1: Very long ETag value (longer than actual ETag) + // Should NOT match and return 200 (not trigger out-of-bounds read) + Headers h1 = {{"If-None-Match", "W/" + "\"very-long-etag-value-that-is-much-longer-" + "than-the-actual-etag-value\""}}; + auto res2 = cli.Get("/static/etag_boundary_testfile.txt", h1); + ASSERT_TRUE(res2); + EXPECT_EQ(200, res2->status); // Should not match + + // Test 2: Long string followed by wildcard + // Should match on "*" and return 304 (without out-of-bounds read on the long + // string) + Headers h2 = {{"If-None-Match", "W/\"another-very-long-value\", *"}}; + auto res3 = cli.Get("/static/etag_boundary_testfile.txt", h2); + ASSERT_TRUE(res3); + EXPECT_EQ(304, res3->status); // Should match on "*" + + // Test 3: Wildcard followed by long string + // Should match on "*" immediately and return 304 + Headers h3 = {{"If-None-Match", "*, W/\"long-value-after-wildcard\""}}; + auto res4 = cli.Get("/static/etag_boundary_testfile.txt", h3); + ASSERT_TRUE(res4); + EXPECT_EQ(304, res4->status); // Should match on "*" + + // Test 4: Multiple long non-matching values + // Should NOT match and return 200 (test that all comparisons are safe) + Headers h4 = {{"If-None-Match", "W/\"first-long-non-matching-value\", " + "W/\"second-long-non-matching-value\", " + "W/\"third-long-non-matching-value\""}}; + auto res5 = cli.Get("/static/etag_boundary_testfile.txt", h4); + ASSERT_TRUE(res5); + EXPECT_EQ(200, res5->status); // Should not match + + // Test 5: Single character that is not "*" (edge case) + Headers h5 = {{"If-None-Match", "X"}}; + auto res6 = cli.Get("/static/etag_boundary_testfile.txt", h5); + ASSERT_TRUE(res6); + EXPECT_EQ(200, res6->status); // Should not match + + svr.stop(); + t.join(); + std::remove(fname); +} + TEST(ETagTest, LastModifiedAndIfModifiedSince) { using namespace httplib; From 4e14bc89480606b1acbf8843ede5c4f883fa8e36 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Feb 2026 15:49:30 -1000 Subject: [PATCH 0613/1222] Fix memory leak (#2348) * Fix memory leak * Fix flaky errors --- .github/workflows/test.yaml | 4 ++++ test/Makefile | 2 +- test/lsan_suppressions.txt | 3 +++ test/test.cc | 5 +++-- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 test/lsan_suppressions.txt diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 418cac5144..31459e863d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -95,6 +95,8 @@ jobs: - name: build and run tests (OpenSSL) if: matrix.tls_backend == 'openssl' run: cd test && make + env: + LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (Mbed TLS) if: matrix.tls_backend == 'mbedtls' run: cd test && make test_split_mbedtls && make test_mbedtls && ./test_mbedtls @@ -122,6 +124,8 @@ jobs: - name: build and run tests (OpenSSL) if: matrix.tls_backend == 'openssl' run: cd test && make + env: + LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (Mbed TLS) if: matrix.tls_backend == 'mbedtls' run: cd test && make test_split_mbedtls && make test_mbedtls && ./test_mbedtls diff --git a/test/Makefile b/test/Makefile index 9269b3de6d..9b2d1e75cf 100644 --- a/test/Makefile +++ b/test/Makefile @@ -54,7 +54,7 @@ STYLE_CHECK_FILES = $(filter-out httplib.h httplib.cc, \ $(wildcard example/*.h example/*.cc fuzzing/*.h fuzzing/*.cc *.h *.cc ../httplib.h)) all : test test_split - ./test + LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test proxy : test_proxy @echo "Starting proxy server..." diff --git a/test/lsan_suppressions.txt b/test/lsan_suppressions.txt new file mode 100644 index 0000000000..1e177a65fe --- /dev/null +++ b/test/lsan_suppressions.txt @@ -0,0 +1,3 @@ +# OpenSSL 3.x internal caches (provider, cipher, keymgmt) are allocated +# lazily and intentionally kept until process exit. These are not real leaks. +leak:libcrypto diff --git a/test/test.cc b/test/test.cc index 4a219c129d..845c57ab56 100644 --- a/test/test.cc +++ b/test/test.cc @@ -4428,7 +4428,7 @@ TEST_F(ServerTest, TooLongRequest) { ASSERT_TRUE(res); EXPECT_EQ(StatusCode::UriTooLong_414, res->status); - EXPECT_LE(elapsed, 100); + EXPECT_LE(elapsed, 1000); EXPECT_EQ("close", res->get_header_value("Connection")); EXPECT_FALSE(cli_.is_socket_open()); } @@ -4513,7 +4513,7 @@ TEST_F(ServerTest, LongQueryValue) { ASSERT_TRUE(res); EXPECT_EQ(StatusCode::UriTooLong_414, res->status); - EXPECT_LE(elapsed, 100); + EXPECT_LE(elapsed, 1000); EXPECT_EQ("close", res->get_header_value("Connection")); EXPECT_FALSE(cli_.is_socket_open()); } @@ -15106,6 +15106,7 @@ TEST(BindServerTest, UpdateCerts) { ASSERT_TRUE(svr.is_valid()); svr.stop(); + X509_STORE_free(cert_store); X509_free(cert); X509_free(ca_cert); EVP_PKEY_free(key); From 2867b74f131200979b2b885683a189248d80e0f4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 8 Feb 2026 16:04:29 -1000 Subject: [PATCH 0614/1222] Release v0.31.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 55dd9d6fce..acb91fc088 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.30.2" -#define CPPHTTPLIB_VERSION_NUM "0x001E02" +#define CPPHTTPLIB_VERSION "0.31.0" +#define CPPHTTPLIB_VERSION_NUM "0x001F00" /* * Platform compatibility check From f0b7d4161dbc56da425e43b0aec34ca4b8d96ce0 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Feb 2026 15:12:12 -1000 Subject: [PATCH 0615/1222] Update justfile --- justfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index 4ff3e420d4..c1153b471a 100644 --- a/justfile +++ b/justfile @@ -6,11 +6,11 @@ list: @just --list --unsorted openssl: - @(cd test && make test && ./test) + @(cd test && make test && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test) @(cd test && make proxy) mbedtls: - @(cd test && make test_mbedtls && ./test_mbedtls) + @(cd test && make test_mbedtls && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test_mbedtls) @(cd test && make proxy_mbedtls) fuzz: From a8752921534502b1728ba2e42e7ef02d68ef2557 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Feb 2026 16:41:49 -1000 Subject: [PATCH 0616/1222] Move `stream` and `sse` implementations from the decl area to the impl area. (#2352) --- httplib.h | 674 +++++++++++++++++++++++++++++------------------------- 1 file changed, 362 insertions(+), 312 deletions(-) diff --git a/httplib.h b/httplib.h index acb91fc088..547028560f 100644 --- a/httplib.h +++ b/httplib.h @@ -2975,98 +2975,30 @@ namespace stream { class Result { public: - Result() : chunk_size_(8192) {} - - explicit Result(ClientImpl::StreamHandle &&handle, size_t chunk_size = 8192) - : handle_(std::move(handle)), chunk_size_(chunk_size) {} - - Result(Result &&other) noexcept - : handle_(std::move(other.handle_)), buffer_(std::move(other.buffer_)), - current_size_(other.current_size_), chunk_size_(other.chunk_size_), - finished_(other.finished_) { - other.current_size_ = 0; - other.finished_ = true; - } - - Result &operator=(Result &&other) noexcept { - if (this != &other) { - handle_ = std::move(other.handle_); - buffer_ = std::move(other.buffer_); - current_size_ = other.current_size_; - chunk_size_ = other.chunk_size_; - finished_ = other.finished_; - other.current_size_ = 0; - other.finished_ = true; - } - return *this; - } - + Result(); + explicit Result(ClientImpl::StreamHandle &&handle, size_t chunk_size = 8192); + Result(Result &&other) noexcept; + Result &operator=(Result &&other) noexcept; Result(const Result &) = delete; Result &operator=(const Result &) = delete; - // Check if the result is valid (connection succeeded and response received) - bool is_valid() const { return handle_.is_valid(); } - explicit operator bool() const { return is_valid(); } - - // Response status code - int status() const { - return handle_.response ? handle_.response->status : -1; - } - - // Response headers - const Headers &headers() const { - static const Headers empty_headers; - return handle_.response ? handle_.response->headers : empty_headers; - } - + // Response info + bool is_valid() const; + explicit operator bool() const; + int status() const; + const Headers &headers() const; std::string get_header_value(const std::string &key, - const char *def = "") const { - return handle_.response ? handle_.response->get_header_value(key, def) - : def; - } - - bool has_header(const std::string &key) const { - return handle_.response ? handle_.response->has_header(key) : false; - } - - // Error information - Error error() const { return handle_.error; } - Error read_error() const { return handle_.get_read_error(); } - bool has_read_error() const { return handle_.has_read_error(); } - - // Streaming iteration API - // Call next() to read the next chunk, then access data via data()/size() - // Returns true if data was read, false when stream is exhausted - bool next() { - if (!handle_.is_valid() || finished_) { return false; } - - if (buffer_.size() < chunk_size_) { buffer_.resize(chunk_size_); } - - ssize_t n = handle_.read(&buffer_[0], chunk_size_); - if (n > 0) { - current_size_ = static_cast(n); - return true; - } - - current_size_ = 0; - finished_ = true; - return false; - } - - // Pointer to current chunk data (valid after next() returns true) - const char *data() const { return buffer_.data(); } - - // Size of current chunk (valid after next() returns true) - size_t size() const { return current_size_; } + const char *def = "") const; + bool has_header(const std::string &key) const; + Error error() const; + Error read_error() const; + bool has_read_error() const; - // Convenience method: read all remaining data into a string - std::string read_all() { - std::string result; - while (next()) { - result.append(data(), size()); - } - return result; - } + // Stream reading + bool next(); + const char *data() const; + size_t size() const; + std::string read_all(); private: ClientImpl::StreamHandle handle_; @@ -3331,13 +3263,8 @@ struct SSEMessage { std::string data; // Event payload std::string id; // Event ID for Last-Event-ID header - SSEMessage() : event("message") {} - - void clear() { - event = "message"; - data.clear(); - id.clear(); - } + SSEMessage(); + void clear(); }; class SSEClient { @@ -3346,288 +3273,411 @@ class SSEClient { using ErrorHandler = std::function; using OpenHandler = std::function; - SSEClient(Client &client, const std::string &path) - : client_(client), path_(path) {} - - SSEClient(Client &client, const std::string &path, const Headers &headers) - : client_(client), path_(path), headers_(headers) {} - - ~SSEClient() { stop(); } + SSEClient(Client &client, const std::string &path); + SSEClient(Client &client, const std::string &path, const Headers &headers); + ~SSEClient(); SSEClient(const SSEClient &) = delete; SSEClient &operator=(const SSEClient &) = delete; // Event handlers - SSEClient &on_message(MessageHandler handler) { - on_message_ = std::move(handler); - return *this; - } + SSEClient &on_message(MessageHandler handler); + SSEClient &on_event(const std::string &type, MessageHandler handler); + SSEClient &on_open(OpenHandler handler); + SSEClient &on_error(ErrorHandler handler); + SSEClient &set_reconnect_interval(int ms); + SSEClient &set_max_reconnect_attempts(int n); - SSEClient &on_event(const std::string &type, MessageHandler handler) { - event_handlers_[type] = std::move(handler); - return *this; - } + // State accessors + bool is_connected() const; + const std::string &last_event_id() const; - SSEClient &on_open(OpenHandler handler) { - on_open_ = std::move(handler); - return *this; - } + // Blocking start - runs event loop with auto-reconnect + void start(); - SSEClient &on_error(ErrorHandler handler) { - on_error_ = std::move(handler); - return *this; - } + // Non-blocking start - runs in background thread + void start_async(); - SSEClient &set_reconnect_interval(int ms) { - reconnect_interval_ms_ = ms; - return *this; - } + // Stop the client (thread-safe) + void stop(); - SSEClient &set_max_reconnect_attempts(int n) { - max_reconnect_attempts_ = n; - return *this; - } +private: + bool parse_sse_line(const std::string &line, SSEMessage &msg, int &retry_ms); + void run_event_loop(); + void dispatch_event(const SSEMessage &msg); + bool should_reconnect(int count) const; + void wait_for_reconnect(); - // State accessors - bool is_connected() const { return connected_.load(); } - const std::string &last_event_id() const { return last_event_id_; } + // Client and path + Client &client_; + std::string path_; + Headers headers_; - // Blocking start - runs event loop with auto-reconnect - void start() { - running_.store(true); - run_event_loop(); - } + // Callbacks + MessageHandler on_message_; + std::map event_handlers_; + OpenHandler on_open_; + ErrorHandler on_error_; - // Non-blocking start - runs in background thread - void start_async() { - running_.store(true); - async_thread_ = std::thread([this]() { run_event_loop(); }); + // Configuration + int reconnect_interval_ms_ = 3000; + int max_reconnect_attempts_ = 0; // 0 = unlimited + + // State + std::atomic running_{false}; + std::atomic connected_{false}; + std::string last_event_id_; + + // Async support + std::thread async_thread_; +}; + +} // namespace sse + +// ---------------------------------------------------------------------------- + +/* + * Implementation that will be part of the .cc file if split into .h + .cc. + */ + +namespace stream { + +// stream::Result implementations +inline Result::Result() : chunk_size_(8192) {} + +inline Result::Result(ClientImpl::StreamHandle &&handle, size_t chunk_size) + : handle_(std::move(handle)), chunk_size_(chunk_size) {} + +inline Result::Result(Result &&other) noexcept + : handle_(std::move(other.handle_)), buffer_(std::move(other.buffer_)), + current_size_(other.current_size_), chunk_size_(other.chunk_size_), + finished_(other.finished_) { + other.current_size_ = 0; + other.finished_ = true; +} + +inline Result &Result::operator=(Result &&other) noexcept { + if (this != &other) { + handle_ = std::move(other.handle_); + buffer_ = std::move(other.buffer_); + current_size_ = other.current_size_; + chunk_size_ = other.chunk_size_; + finished_ = other.finished_; + other.current_size_ = 0; + other.finished_ = true; } + return *this; +} - // Stop the client (thread-safe) - void stop() { - running_.store(false); - client_.stop(); // Cancel any pending operations - if (async_thread_.joinable()) { async_thread_.join(); } +inline bool Result::is_valid() const { return handle_.is_valid(); } +inline Result::operator bool() const { return is_valid(); } + +inline int Result::status() const { + return handle_.response ? handle_.response->status : -1; +} + +inline const Headers &Result::headers() const { + static const Headers empty_headers; + return handle_.response ? handle_.response->headers : empty_headers; +} + +inline std::string Result::get_header_value(const std::string &key, + const char *def) const { + return handle_.response ? handle_.response->get_header_value(key, def) : def; +} + +inline bool Result::has_header(const std::string &key) const { + return handle_.response ? handle_.response->has_header(key) : false; +} + +inline Error Result::error() const { return handle_.error; } +inline Error Result::read_error() const { return handle_.get_read_error(); } +inline bool Result::has_read_error() const { return handle_.has_read_error(); } + +inline bool Result::next() { + if (!handle_.is_valid() || finished_) { return false; } + + if (buffer_.size() < chunk_size_) { buffer_.resize(chunk_size_); } + + ssize_t n = handle_.read(&buffer_[0], chunk_size_); + if (n > 0) { + current_size_ = static_cast(n); + return true; } -private: - // Parse a single SSE field line - // Returns true if this line ends an event (blank line) - bool parse_sse_line(const std::string &line, SSEMessage &msg, int &retry_ms) { - // Blank line signals end of event - if (line.empty() || line == "\r") { return true; } - - // Lines starting with ':' are comments (ignored) - if (!line.empty() && line[0] == ':') { return false; } - - // Find the colon separator - auto colon_pos = line.find(':'); - if (colon_pos == std::string::npos) { - // Line with no colon is treated as field name with empty value - return false; - } + current_size_ = 0; + finished_ = true; + return false; +} - auto field = line.substr(0, colon_pos); - std::string value; - - // Value starts after colon, skip optional single space - if (colon_pos + 1 < line.size()) { - auto value_start = colon_pos + 1; - if (line[value_start] == ' ') { value_start++; } - value = line.substr(value_start); - // Remove trailing \r if present - if (!value.empty() && value.back() == '\r') { value.pop_back(); } - } - - // Handle known fields - if (field == "event") { - msg.event = value; - } else if (field == "data") { - // Multiple data lines are concatenated with newlines - if (!msg.data.empty()) { msg.data += "\n"; } - msg.data += value; - } else if (field == "id") { - // Empty id is valid (clears the last event ID) - msg.id = value; - } else if (field == "retry") { - // Parse retry interval in milliseconds - { - int v = 0; - auto res = - detail::from_chars(value.data(), value.data() + value.size(), v); - if (res.ec == std::errc{}) { retry_ms = v; } - } - } - // Unknown fields are ignored per SSE spec +inline const char *Result::data() const { return buffer_.data(); } +inline size_t Result::size() const { return current_size_; } - return false; +inline std::string Result::read_all() { + std::string result; + while (next()) { + result.append(data(), size()); } + return result; +} - // Main event loop with auto-reconnect - void run_event_loop() { - auto reconnect_count = 0; +} // namespace stream - while (running_.load()) { - // Build headers, including Last-Event-ID if we have one - auto request_headers = headers_; - if (!last_event_id_.empty()) { - request_headers.emplace("Last-Event-ID", last_event_id_); - } +namespace sse { - // Open streaming connection - auto result = stream::Get(client_, path_, request_headers); +// SSEMessage implementations +inline SSEMessage::SSEMessage() : event("message") {} - // Connection error handling - if (!result) { - connected_.store(false); - if (on_error_) { on_error_(result.error()); } +inline void SSEMessage::clear() { + event = "message"; + data.clear(); + id.clear(); +} - if (!should_reconnect(reconnect_count)) { break; } - wait_for_reconnect(); - reconnect_count++; - continue; - } +// SSEClient implementations +inline SSEClient::SSEClient(Client &client, const std::string &path) + : client_(client), path_(path) {} - if (result.status() != 200) { - connected_.store(false); - // For certain errors, don't reconnect - if (result.status() == 204 || // No Content - server wants us to stop - result.status() == 404 || // Not Found - result.status() == 401 || // Unauthorized - result.status() == 403) { // Forbidden - if (on_error_) { on_error_(Error::Connection); } - break; - } +inline SSEClient::SSEClient(Client &client, const std::string &path, + const Headers &headers) + : client_(client), path_(path), headers_(headers) {} - if (on_error_) { on_error_(Error::Connection); } +inline SSEClient::~SSEClient() { stop(); } - if (!should_reconnect(reconnect_count)) { break; } - wait_for_reconnect(); - reconnect_count++; - continue; - } +inline SSEClient &SSEClient::on_message(MessageHandler handler) { + on_message_ = std::move(handler); + return *this; +} - // Connection successful - connected_.store(true); - reconnect_count = 0; - if (on_open_) { on_open_(); } +inline SSEClient &SSEClient::on_event(const std::string &type, + MessageHandler handler) { + event_handlers_[type] = std::move(handler); + return *this; +} - // Event receiving loop - std::string buffer; - SSEMessage current_msg; +inline SSEClient &SSEClient::on_open(OpenHandler handler) { + on_open_ = std::move(handler); + return *this; +} - while (running_.load() && result.next()) { - buffer.append(result.data(), result.size()); +inline SSEClient &SSEClient::on_error(ErrorHandler handler) { + on_error_ = std::move(handler); + return *this; +} - // Process complete lines in the buffer - size_t line_start = 0; - size_t newline_pos; +inline SSEClient &SSEClient::set_reconnect_interval(int ms) { + reconnect_interval_ms_ = ms; + return *this; +} - while ((newline_pos = buffer.find('\n', line_start)) != - std::string::npos) { - auto line = buffer.substr(line_start, newline_pos - line_start); - line_start = newline_pos + 1; +inline SSEClient &SSEClient::set_max_reconnect_attempts(int n) { + max_reconnect_attempts_ = n; + return *this; +} - // Parse the line and check if event is complete - auto event_complete = - parse_sse_line(line, current_msg, reconnect_interval_ms_); +inline bool SSEClient::is_connected() const { return connected_.load(); } - if (event_complete && !current_msg.data.empty()) { - // Update last_event_id for reconnection - if (!current_msg.id.empty()) { last_event_id_ = current_msg.id; } +inline const std::string &SSEClient::last_event_id() const { + return last_event_id_; +} - // Dispatch event to appropriate handler - dispatch_event(current_msg); +inline void SSEClient::start() { + running_.store(true); + run_event_loop(); +} - current_msg.clear(); - } - } +inline void SSEClient::start_async() { + running_.store(true); + async_thread_ = std::thread([this]() { run_event_loop(); }); +} - // Keep unprocessed data in buffer - buffer.erase(0, line_start); - } +inline void SSEClient::stop() { + running_.store(false); + client_.stop(); // Cancel any pending operations + if (async_thread_.joinable()) { async_thread_.join(); } +} + +inline bool SSEClient::parse_sse_line(const std::string &line, SSEMessage &msg, + int &retry_ms) { + // Blank line signals end of event + if (line.empty() || line == "\r") { return true; } + + // Lines starting with ':' are comments (ignored) + if (!line.empty() && line[0] == ':') { return false; } + + // Find the colon separator + auto colon_pos = line.find(':'); + if (colon_pos == std::string::npos) { + // Line with no colon is treated as field name with empty value + return false; + } + + auto field = line.substr(0, colon_pos); + std::string value; + + // Value starts after colon, skip optional single space + if (colon_pos + 1 < line.size()) { + auto value_start = colon_pos + 1; + if (line[value_start] == ' ') { value_start++; } + value = line.substr(value_start); + // Remove trailing \r if present + if (!value.empty() && value.back() == '\r') { value.pop_back(); } + } + + // Handle known fields + if (field == "event") { + msg.event = value; + } else if (field == "data") { + // Multiple data lines are concatenated with newlines + if (!msg.data.empty()) { msg.data += "\n"; } + msg.data += value; + } else if (field == "id") { + // Empty id is valid (clears the last event ID) + msg.id = value; + } else if (field == "retry") { + // Parse retry interval in milliseconds + { + int v = 0; + auto res = + detail::from_chars(value.data(), value.data() + value.size(), v); + if (res.ec == std::errc{}) { retry_ms = v; } + } + } + // Unknown fields are ignored per SSE spec + + return false; +} + +inline void SSEClient::run_event_loop() { + auto reconnect_count = 0; + + while (running_.load()) { + // Build headers, including Last-Event-ID if we have one + auto request_headers = headers_; + if (!last_event_id_.empty()) { + request_headers.emplace("Last-Event-ID", last_event_id_); + } + + // Open streaming connection + auto result = stream::Get(client_, path_, request_headers); - // Connection ended + // Connection error handling + if (!result) { connected_.store(false); + if (on_error_) { on_error_(result.error()); } - if (!running_.load()) { break; } + if (!should_reconnect(reconnect_count)) { break; } + wait_for_reconnect(); + reconnect_count++; + continue; + } - // Check for read errors - if (result.has_read_error()) { - if (on_error_) { on_error_(result.read_error()); } + if (result.status() != 200) { + connected_.store(false); + // For certain errors, don't reconnect + if (result.status() == 204 || // No Content - server wants us to stop + result.status() == 404 || // Not Found + result.status() == 401 || // Unauthorized + result.status() == 403) { // Forbidden + if (on_error_) { on_error_(Error::Connection); } + break; } + if (on_error_) { on_error_(Error::Connection); } + if (!should_reconnect(reconnect_count)) { break; } wait_for_reconnect(); reconnect_count++; + continue; } - connected_.store(false); - } + // Connection successful + connected_.store(true); + reconnect_count = 0; + if (on_open_) { on_open_(); } - // Dispatch event to appropriate handler - void dispatch_event(const SSEMessage &msg) { - // Check for specific event type handler first - auto it = event_handlers_.find(msg.event); - if (it != event_handlers_.end()) { - it->second(msg); - return; + // Event receiving loop + std::string buffer; + SSEMessage current_msg; + + while (running_.load() && result.next()) { + buffer.append(result.data(), result.size()); + + // Process complete lines in the buffer + size_t line_start = 0; + size_t newline_pos; + + while ((newline_pos = buffer.find('\n', line_start)) != + std::string::npos) { + auto line = buffer.substr(line_start, newline_pos - line_start); + line_start = newline_pos + 1; + + // Parse the line and check if event is complete + auto event_complete = + parse_sse_line(line, current_msg, reconnect_interval_ms_); + + if (event_complete && !current_msg.data.empty()) { + // Update last_event_id for reconnection + if (!current_msg.id.empty()) { last_event_id_ = current_msg.id; } + + // Dispatch event to appropriate handler + dispatch_event(current_msg); + + current_msg.clear(); + } + } + + // Keep unprocessed data in buffer + buffer.erase(0, line_start); } - // Fall back to generic message handler - if (on_message_) { on_message_(msg); } - } + // Connection ended + connected_.store(false); - // Check if we should attempt to reconnect - bool should_reconnect(int count) const { - if (!running_.load()) { return false; } - if (max_reconnect_attempts_ == 0) { return true; } // unlimited - return count < max_reconnect_attempts_; - } + if (!running_.load()) { break; } - // Wait for reconnect interval - void wait_for_reconnect() { - // Use small increments to check running_ flag frequently - auto waited = 0; - while (running_.load() && waited < reconnect_interval_ms_) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - waited += 100; + // Check for read errors + if (result.has_read_error()) { + if (on_error_) { on_error_(result.read_error()); } } + + if (!should_reconnect(reconnect_count)) { break; } + wait_for_reconnect(); + reconnect_count++; } - // Client and path - Client &client_; - std::string path_; - Headers headers_; + connected_.store(false); +} - // Callbacks - MessageHandler on_message_; - std::map event_handlers_; - OpenHandler on_open_; - ErrorHandler on_error_; +inline void SSEClient::dispatch_event(const SSEMessage &msg) { + // Check for specific event type handler first + auto it = event_handlers_.find(msg.event); + if (it != event_handlers_.end()) { + it->second(msg); + return; + } - // Configuration - int reconnect_interval_ms_ = 3000; - int max_reconnect_attempts_ = 0; // 0 = unlimited + // Fall back to generic message handler + if (on_message_) { on_message_(msg); } +} - // State - std::atomic running_{false}; - std::atomic connected_{false}; - std::string last_event_id_; +inline bool SSEClient::should_reconnect(int count) const { + if (!running_.load()) { return false; } + if (max_reconnect_attempts_ == 0) { return true; } // unlimited + return count < max_reconnect_attempts_; +} - // Async support - std::thread async_thread_; -}; +inline void SSEClient::wait_for_reconnect() { + // Use small increments to check running_ flag frequently + auto waited = 0; + while (running_.load() && waited < reconnect_interval_ms_) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + waited += 100; + } +} } // namespace sse -// ---------------------------------------------------------------------------- - -/* - * Implementation that will be part of the .cc file if split into .h + .cc. - */ - #ifdef CPPHTTPLIB_SSL_ENABLED /* * TLS abstraction layer - internal function declarations From 1f1a799d13d6e482279b846c3555f6b3dbebf9b4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 9 Feb 2026 16:43:02 -1000 Subject: [PATCH 0617/1222] Fix #2351 --- test/test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.cc b/test/test.cc index 845c57ab56..80c944676e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8902,7 +8902,7 @@ TEST(HostAndPortPropertiesTest, SSL) { ASSERT_EQ(443, cli.port()); } -TEST(SSLClientTest, UpdateCAStoreWithPem) { +TEST(SSLClientTest, UpdateCAStoreWithPem_Online) { // Test updating CA store multiple times using PEM-based load_ca_cert_store std::string cert; read_file(CA_CERT_FILE, cert); From 8c4370247ad9355182e3fd5ea7d400fe1374cdcd Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Tue, 10 Feb 2026 19:51:37 +0000 Subject: [PATCH 0618/1222] Add support for mbedtls to meson (#2350) Related: #2345 --- meson.build | 28 +++++++++++++++++++++++----- meson_options.txt | 4 +++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index 5b97168fef..0a11c6b28d 100644 --- a/meson.build +++ b/meson.build @@ -39,11 +39,29 @@ endif deps = [dependency('threads')] args = [] -openssl_dep = dependency('openssl', version: '>=3.0.0', required: get_option('openssl')) -if openssl_dep.found() - deps += openssl_dep - args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' - if host_machine.system() == 'darwin' +if get_option('tls').allowed() + tls_found = false + if get_option('tls_backend') == 'openssl' + openssl_dep = dependency('openssl', version: '>=3.0.0', required: get_option('tls')) + if openssl_dep.found() + deps += openssl_dep + args += '-DCPPHTTPLIB_OPENSSL_SUPPORT' + tls_found = true + endif + else + mbedtls_dep = dependency('mbedtls', required: get_option('tls')) + mbedtlsx509_dep = dependency('mbedx509', required: get_option('tls')) + mbedtlscrypto_dep = dependency('mbedcrypto', required: get_option('tls')) + if mbedtls_dep.found() and mbedtlsx509_dep.found() and mbedtlscrypto_dep.found() + deps += mbedtls_dep + deps += mbedtlsx509_dep + deps += mbedtlscrypto_dep + args += '-DCPPHTTPLIB_MBEDTLS_SUPPORT' + tls_found = true + endif + endif + + if tls_found and host_machine.system() == 'darwin' macosx_keychain_dep = dependency('appleframeworks', modules: ['CFNetwork', 'CoreFoundation', 'Security'], required: get_option('macosx_keychain')) if macosx_keychain_dep.found() deps += macosx_keychain_dep diff --git a/meson_options.txt b/meson_options.txt index 7447d8d1c2..05c4e19a1e 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,7 +2,8 @@ # # SPDX-License-Identifier: MIT -option('openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support') +option('tls', type: 'feature', description: 'Enable TLS support') +option('tls_backend', type: 'combo', choices: ['openssl', 'mbedtls'], value: 'openssl', description: 'Which TLS library to use') option('zlib', type: 'feature', value: 'auto', description: 'Enable zlib support') option('brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support') option('zstd', type: 'feature', value: 'auto', description: 'Enable zstd support') @@ -12,6 +13,7 @@ option('compile', type: 'boolean', value: false, description: 'Split the header option('test', type: 'boolean', value: false, description: 'Build tests') # Old option names +option('openssl', type: 'feature', deprecated: 'tls') option('cpp-httplib_openssl', type: 'feature', deprecated: 'openssl') option('cpp-httplib_zlib', type: 'feature', deprecated: 'zlib') option('cpp-httplib_brotli', type: 'feature', deprecated: 'brotli') From 02e4c5368539cb0655fa6396ccf7be8fc45f6fb3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 10 Feb 2026 23:21:43 -1000 Subject: [PATCH 0619/1222] Fix 'no TLS' problem with RequestWithoutContentLengthOrTransferEncoding --- httplib.h | 31 ++++++++++++++++++------------- justfile | 3 +++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index 547028560f..b909f3925a 100644 --- a/httplib.h +++ b/httplib.h @@ -9869,20 +9869,25 @@ inline bool Server::read_content_core( payload_max_length_ < (std::numeric_limits::max)()) { socket_t s = strm.socket(); if (s != INVALID_SOCKET) { - // Peek to check if there is any pending data - char peekbuf[1]; - ssize_t n = ::recv(s, peekbuf, 1, MSG_PEEK); - if (n > 0) { - // There is data, so read it with payload limit enforcement - auto result = detail::read_content_without_length( - strm, payload_max_length_, out); - if (result == detail::ReadContentResult::PayloadTooLarge) { - res.status = StatusCode::PayloadTooLarge_413; - return false; - } else if (result != detail::ReadContentResult::Success) { - return false; + // Use a non-blocking check to see if there is any pending data. + // A blocking recv(MSG_PEEK) would deadlock when the client is + // waiting for the response (e.g. POST with Connection: close and + // no body). + if (detail::select_read(s, 0, 0) > 0) { + char peekbuf[1]; + ssize_t n = ::recv(s, peekbuf, 1, MSG_PEEK); + if (n > 0) { + // There is data, so read it with payload limit enforcement + auto result = detail::read_content_without_length( + strm, payload_max_length_, out); + if (result == detail::ReadContentResult::PayloadTooLarge) { + res.status = StatusCode::PayloadTooLarge_413; + return false; + } else if (result != detail::ReadContentResult::Success) { + return false; + } + return true; } - return true; } } } diff --git a/justfile b/justfile index c1153b471a..0745a781bf 100644 --- a/justfile +++ b/justfile @@ -13,6 +13,9 @@ mbedtls: @(cd test && make test_mbedtls && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test_mbedtls) @(cd test && make proxy_mbedtls) +no_tls: + @(cd test && make test_no_tls && ./test_no_tls) + fuzz: @(cd test && make fuzz_test) From f69737a838dd1dc8c04308db6b5bdf4e1796a148 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 10 Feb 2026 23:38:57 -1000 Subject: [PATCH 0620/1222] Fix problem with PayloadMaxLengthTest.NoContentLengthPayloadLimit --- httplib.h | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/httplib.h b/httplib.h index b909f3925a..3766f2128b 100644 --- a/httplib.h +++ b/httplib.h @@ -9855,41 +9855,40 @@ inline bool Server::read_content_core( // are true (no Transfer-Encoding and no Content-Length), then the message // body length is zero (no message body is present). // - // For non-SSL builds, peek into the socket to detect clients that send a - // body without a Content-Length header (raw HTTP over TCP). If there is - // pending data that exceeds the configured payload limit, treat this as an - // oversized request and fail early (causing connection close). For SSL - // builds we cannot reliably peek the decrypted application bytes, so keep - // the original behaviour. + // For non-SSL builds, detect clients that send a body without a + // Content-Length header (raw HTTP over TCP). Check both the stream's + // internal read buffer (data already read from the socket during header + // parsing) and the socket itself for pending data. If data is found and + // exceeds the configured payload limit, reject with 413. + // For SSL builds we cannot reliably peek the decrypted application bytes, + // so keep the original behaviour. #if !defined(CPPHTTPLIB_SSL_ENABLED) if (!req.has_header("Content-Length") && !detail::is_chunked_transfer_encoding(req.headers)) { - // Only peek if payload_max_length is set to a finite value + // Only check if payload_max_length is set to a finite value if (payload_max_length_ > 0 && payload_max_length_ < (std::numeric_limits::max)()) { - socket_t s = strm.socket(); - if (s != INVALID_SOCKET) { - // Use a non-blocking check to see if there is any pending data. - // A blocking recv(MSG_PEEK) would deadlock when the client is - // waiting for the response (e.g. POST with Connection: close and - // no body). - if (detail::select_read(s, 0, 0) > 0) { - char peekbuf[1]; - ssize_t n = ::recv(s, peekbuf, 1, MSG_PEEK); - if (n > 0) { - // There is data, so read it with payload limit enforcement - auto result = detail::read_content_without_length( - strm, payload_max_length_, out); - if (result == detail::ReadContentResult::PayloadTooLarge) { - res.status = StatusCode::PayloadTooLarge_413; - return false; - } else if (result != detail::ReadContentResult::Success) { - return false; - } - return true; - } + // Check if there is data already buffered in the stream (read during + // header parsing) or pending on the socket. Use a non-blocking socket + // check to avoid deadlock when the client sends no body. + bool has_data = strm.is_readable(); + if (!has_data) { + socket_t s = strm.socket(); + if (s != INVALID_SOCKET) { + has_data = detail::select_read(s, 0, 0) > 0; } } + if (has_data) { + auto result = + detail::read_content_without_length(strm, payload_max_length_, out); + if (result == detail::ReadContentResult::PayloadTooLarge) { + res.status = StatusCode::PayloadTooLarge_413; + return false; + } else if (result != detail::ReadContentResult::Success) { + return false; + } + return true; + } } return true; } From 77d945d3b6c828a590a9d8b4d207c3e98b3c3e1e Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 11 Feb 2026 12:54:35 -0500 Subject: [PATCH 0621/1222] Correct sign comparison error with sk_X509_OBJECT_num (#2355) * Correct sign comparison error with sk_X509_OBJECT_num In some build configurations, sk_X509_OBJECT_num (from BoringSSL) returns a size_t. Comparing this directly against a signed int loop counter triggers -Werror,-Wsign-compare. Instead, move the count into a local int variable so the compiler uses implicit conversion to ensure type consistency during the loop comparison. * Update httplib.h * Update httplib.h Missed a s/int/decltype(count) --- httplib.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 3766f2128b..420c724c95 100644 --- a/httplib.h +++ b/httplib.h @@ -14743,7 +14743,8 @@ inline std::string x509_store_to_pem(X509_STORE *store) { std::string pem; auto objs = X509_STORE_get0_objects(store); if (!objs) return {}; - for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { + auto count = sk_X509_OBJECT_num(objs); + for (decltype(count) i = 0; i < count; i++) { auto obj = sk_X509_OBJECT_value(objs, i); if (X509_OBJECT_get_type(obj) == X509_LU_X509) { auto cert = X509_OBJECT_get0_X509(obj); @@ -14810,7 +14811,8 @@ inline STACK_OF(X509_NAME) * return nullptr; } - for (int i = 0; i < sk_X509_OBJECT_num(objs); i++) { + auto count = sk_X509_OBJECT_num(objs); + for (decltype(count) i = 0; i < count; i++) { auto obj = sk_X509_OBJECT_value(objs, i); if (X509_OBJECT_get_type(obj) == X509_LU_X509) { auto cert = X509_OBJECT_get0_X509(obj); @@ -15570,8 +15572,8 @@ inline size_t get_ca_certs(ctx_t ctx, std::vector &certs) { auto objs = X509_STORE_get0_objects(store); if (!objs) { return 0; } - int count = sk_X509_OBJECT_num(objs); - for (int i = 0; i < count; i++) { + auto count = sk_X509_OBJECT_num(objs); + for (decltype(count) i = 0; i < count; i++) { auto obj = sk_X509_OBJECT_value(objs, i); if (!obj) { continue; } if (X509_OBJECT_get_type(obj) == X509_LU_X509) { @@ -15597,8 +15599,8 @@ inline std::vector get_ca_names(ctx_t ctx) { auto objs = X509_STORE_get0_objects(store); if (!objs) { return names; } - int count = sk_X509_OBJECT_num(objs); - for (int i = 0; i < count; i++) { + auto count = sk_X509_OBJECT_num(objs); + for (decltype(count) i = 0; i < count; i++) { auto obj = sk_X509_OBJECT_value(objs, i); if (!obj) { continue; } if (X509_OBJECT_get_type(obj) == X509_LU_X509) { From 4e75a84b399c6826706037eb1e4e5cfe7096e5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Wed, 11 Feb 2026 18:55:07 +0100 Subject: [PATCH 0622/1222] Fix compilation on BoringSSL by replacing ASN1_TIME_to_tm (#2354) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix compilation on BoringSSL by replacing ASN1_TIME_to_tm BoringSSL doesn't expose `ASN1_TIME_to_tm`. This patch switches to using `ASN1_TIME_diff` to calculate `time_t`. This is supported by OpenSSL, LibreSSL, and BoringSSL, and also avoids the platform-specific `timegm` vs `_mkgmtime` logic. Signed-off-by: Adrien Gallouët * Format code Signed-off-by: Adrien Gallouët * Use detail::scope_exit Signed-off-by: Adrien Gallouët --------- Signed-off-by: Adrien Gallouët --- httplib.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/httplib.h b/httplib.h index 420c724c95..cdbd662a47 100644 --- a/httplib.h +++ b/httplib.h @@ -15461,18 +15461,20 @@ inline bool get_cert_validity(cert_t cert, time_t ¬_before, auto na = X509_get0_notAfter(x509); if (!nb || !na) return false; - // Convert ASN1_TIME to time_t - struct tm tm_nb = {}, tm_na = {}; - if (ASN1_TIME_to_tm(nb, &tm_nb) != 1) return false; - if (ASN1_TIME_to_tm(na, &tm_na) != 1) return false; + ASN1_TIME *epoch = ASN1_TIME_new(); + if (!epoch) return false; + auto se = detail::scope_exit([&] { ASN1_TIME_free(epoch); }); + + if (!ASN1_TIME_set(epoch, 0)) return false; + + int pday, psec; + + if (!ASN1_TIME_diff(&pday, &psec, epoch, nb)) return false; + not_before = 86400 * (time_t)pday + psec; + + if (!ASN1_TIME_diff(&pday, &psec, epoch, na)) return false; + not_after = 86400 * (time_t)pday + psec; -#ifdef _WIN32 - not_before = _mkgmtime(&tm_nb); - not_after = _mkgmtime(&tm_na); -#else - not_before = timegm(&tm_nb); - not_after = timegm(&tm_na); -#endif return true; } From f80864ca031932351abef49b74097c67f14719c6 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 11 Feb 2026 14:25:27 -1000 Subject: [PATCH 0623/1222] Resolve #2359 --- httplib.h | 11 +- test/test.cc | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 343 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index cdbd662a47..0863014e77 100644 --- a/httplib.h +++ b/httplib.h @@ -1956,6 +1956,7 @@ class ClientImpl { bool decompress_ = true; size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; + bool has_payload_max_length_ = false; std::string interface_; @@ -10758,6 +10759,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { compress_ = rhs.compress_; decompress_ = rhs.decompress_; payload_max_length_ = rhs.payload_max_length_; + has_payload_max_length_ = rhs.has_payload_max_length_; interface_ = rhs.interface_; proxy_host_ = rhs.proxy_host_; proxy_port_ = rhs.proxy_port_; @@ -12147,7 +12149,10 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (res.status != StatusCode::NotModified_304) { int dummy_status; - if (!detail::read_content(strm, res, payload_max_length_, dummy_status, + auto max_length = (!has_payload_max_length_ && req.content_receiver) + ? (std::numeric_limits::max)() + : payload_max_length_; + if (!detail::read_content(strm, res, max_length, dummy_status, std::move(progress), std::move(out), decompress_)) { if (error != Error::Canceled) { error = Error::Read; } @@ -13098,6 +13103,7 @@ inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } inline void ClientImpl::set_payload_max_length(size_t length) { payload_max_length_ = length; + has_payload_max_length_ = true; } inline void ClientImpl::set_interface(const std::string &intf) { @@ -15257,6 +15263,9 @@ inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) { } auto ssl = static_cast(session); + constexpr auto max_len = + static_cast((std::numeric_limits::max)()); + if (len > max_len) { len = max_len; } auto ret = SSL_read(ssl, buf, static_cast(len)); if (ret > 0) { diff --git a/test/test.cc b/test/test.cc index 80c944676e..7d16ddbb9d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -8752,6 +8752,339 @@ TEST(ClientVulnerabilityTest, PayloadMaxLengthZeroMeansNoLimit) { << " bytes without truncation, but only read " << total_read << " bytes."; } +// Verify that content_receiver bypasses the default payload_max_length, +// allowing streaming downloads larger than 100MB without requiring an explicit +// set_payload_max_length call. +TEST(ClientVulnerabilityTest, ContentReceiverBypassesDefaultPayloadMaxLength) { + static constexpr size_t DATA_SIZE = 200 * 1024 * 1024; // 200MB from server + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + auto server_thread = std::thread([] { + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 2); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&opt), +#else + &opt, +#endif + sizeof(opt)); + + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + if (cli != INVALID_SOCKET) { + char buf[4096]; + ::recv(cli, buf, sizeof(buf), 0); + + // Response with Content-Length larger than default 100MB limit + auto content_length = std::to_string(DATA_SIZE); + std::string response_header = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + + content_length + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + ::send(cli, +#ifdef _WIN32 + static_cast(response_header.c_str()), + static_cast(response_header.size()), +#else + response_header.c_str(), response_header.size(), +#endif + 0); + + std::string chunk(64 * 1024, 'A'); + size_t total_sent = 0; + + while (total_sent < DATA_SIZE) { + auto to_send = std::min(chunk.size(), DATA_SIZE - total_sent); + auto sent = ::send(cli, +#ifdef _WIN32 + static_cast(chunk.c_str()), + static_cast(to_send), +#else + chunk.c_str(), to_send, +#endif + 0); + if (sent <= 0) break; + total_sent += static_cast(sent); + } + + detail::close_socket(cli); + } + detail::close_socket(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + size_t total_received = 0; + + { + Client cli("127.0.0.1", PORT + 2); + cli.set_read_timeout(10, 0); + // Do NOT call set_payload_max_length — use the default 100MB limit + + auto res = + cli.Get("/large", [&](const char * /*data*/, size_t data_length) { + total_received += data_length; + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + } + + server_thread.join(); + + EXPECT_EQ(total_received, DATA_SIZE) + << "With content_receiver, the client should read all " << DATA_SIZE + << " bytes despite the default 100MB payload_max_length, but only read " + << total_received << " bytes."; +} + +// Verify that an explicit set_payload_max_length smaller than the response is +// enforced even when a content_receiver is used. +TEST(ClientVulnerabilityTest, + ContentReceiverRespectsExplicitPayloadMaxLength150MB) { + static constexpr size_t DATA_SIZE = 200 * 1024 * 1024; // 200MB from server + static constexpr size_t EXPLICIT_LIMIT = 150 * 1024 * 1024; // 150MB limit + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + auto server_thread = std::thread([] { + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 2); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&opt), +#else + &opt, +#endif + sizeof(opt)); + + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + if (cli != INVALID_SOCKET) { + char buf[4096]; + ::recv(cli, buf, sizeof(buf), 0); + + auto content_length = std::to_string(DATA_SIZE); + std::string response_header = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + + content_length + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + ::send(cli, +#ifdef _WIN32 + static_cast(response_header.c_str()), + static_cast(response_header.size()), +#else + response_header.c_str(), response_header.size(), +#endif + 0); + + std::string chunk(64 * 1024, 'A'); + size_t total_sent = 0; + + while (total_sent < DATA_SIZE) { + auto to_send = std::min(chunk.size(), DATA_SIZE - total_sent); + auto sent = ::send(cli, +#ifdef _WIN32 + static_cast(chunk.c_str()), + static_cast(to_send), +#else + chunk.c_str(), to_send, +#endif + 0); + if (sent <= 0) break; + total_sent += static_cast(sent); + } + + detail::close_socket(cli); + } + detail::close_socket(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + size_t total_received = 0; + + { + Client cli("127.0.0.1", PORT + 2); + cli.set_read_timeout(10, 0); + cli.set_payload_max_length(EXPLICIT_LIMIT); // Explicit 150MB limit + + auto res = + cli.Get("/large", [&](const char * /*data*/, size_t data_length) { + total_received += data_length; + return true; + }); + + // Should fail because 200MB exceeds the explicit 150MB limit + EXPECT_FALSE(res); + } + + server_thread.join(); + + EXPECT_LE(total_received, EXPLICIT_LIMIT) + << "Client with content_receiver should respect the explicit " + << "payload_max_length of " << EXPLICIT_LIMIT << " bytes, but read " + << total_received << " bytes."; +} + +// Verify that an explicit set_payload_max_length larger than the response +// allows the content_receiver to read all data successfully. +TEST(ClientVulnerabilityTest, + ContentReceiverRespectsExplicitPayloadMaxLength250MB) { + static constexpr size_t DATA_SIZE = 200 * 1024 * 1024; // 200MB from server + static constexpr size_t EXPLICIT_LIMIT = 250 * 1024 * 1024; // 250MB limit + +#ifndef _WIN32 + signal(SIGPIPE, SIG_IGN); +#endif + + auto server_thread = std::thread([] { + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 2); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, +#ifdef _WIN32 + reinterpret_cast(&opt), +#else + &opt, +#endif + sizeof(opt)); + + ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); + ::listen(srv, 1); + + sockaddr_in cli_addr{}; + socklen_t cli_len = sizeof(cli_addr); + auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); + + if (cli != INVALID_SOCKET) { + char buf[4096]; + ::recv(cli, buf, sizeof(buf), 0); + + auto content_length = std::to_string(DATA_SIZE); + std::string response_header = "HTTP/1.1 200 OK\r\n" + "Content-Length: " + + content_length + + "\r\n" + "Connection: close\r\n" + "\r\n"; + + ::send(cli, +#ifdef _WIN32 + static_cast(response_header.c_str()), + static_cast(response_header.size()), +#else + response_header.c_str(), response_header.size(), +#endif + 0); + + std::string chunk(64 * 1024, 'A'); + size_t total_sent = 0; + + while (total_sent < DATA_SIZE) { + auto to_send = std::min(chunk.size(), DATA_SIZE - total_sent); + auto sent = ::send(cli, +#ifdef _WIN32 + static_cast(chunk.c_str()), + static_cast(to_send), +#else + chunk.c_str(), to_send, +#endif + 0); + if (sent <= 0) break; + total_sent += static_cast(sent); + } + +#ifdef _WIN32 + ::shutdown(cli, SD_SEND); +#else + ::shutdown(cli, SHUT_WR); +#endif + char drain[1024]; + while (::recv(cli, drain, sizeof(drain), 0) > 0) {} + detail::close_socket(cli); + } + detail::close_socket(srv); + }); + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + + size_t total_received = 0; + + { + Client cli("127.0.0.1", PORT + 2); + cli.set_read_timeout(10, 0); + cli.set_payload_max_length(EXPLICIT_LIMIT); // Explicit 250MB limit + + auto res = + cli.Get("/large", [&](const char * /*data*/, size_t data_length) { + total_received += data_length; + return true; + }); + + ASSERT_TRUE(res); + EXPECT_EQ(StatusCode::OK_200, res->status); + } + + server_thread.join(); + + EXPECT_EQ(total_received, DATA_SIZE) + << "With explicit payload_max_length of " << EXPLICIT_LIMIT + << " bytes (larger than " << DATA_SIZE + << " bytes response), content_receiver should read all data, but only " + "read " + << total_received << " bytes."; +} + #if defined(CPPHTTPLIB_ZLIB_SUPPORT) && !defined(_WIN32) // Regression test for "zip bomb" attack on the client side: a malicious server // sends a small gzip-compressed response that decompresses to a huge payload. From c0adbb4b20f09cfca6fbf8468c91505df74894a4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Feb 2026 15:24:12 -0500 Subject: [PATCH 0624/1222] Release v0.32.0 --- httplib.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 0863014e77..7ffb753094 100644 --- a/httplib.h +++ b/httplib.h @@ -8,8 +8,8 @@ #ifndef CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H -#define CPPHTTPLIB_VERSION "0.31.0" -#define CPPHTTPLIB_VERSION_NUM "0x001F00" +#define CPPHTTPLIB_VERSION "0.32.0" +#define CPPHTTPLIB_VERSION_NUM "0x002000" /* * Platform compatibility check From 14e37bd75b52a31d7e68ccd20bc4e1b505257b6e Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Feb 2026 11:31:23 -1000 Subject: [PATCH 0625/1222] Offline test (Resolve #2356) (#2358) * Offline test * Disabled network * Removed MbedTLS --- .github/workflows/test_offline.yaml | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 .github/workflows/test_offline.yaml diff --git a/.github/workflows/test_offline.yaml b/.github/workflows/test_offline.yaml new file mode 100644 index 0000000000..64e86177b1 --- /dev/null +++ b/.github/workflows/test_offline.yaml @@ -0,0 +1,51 @@ +name: test_offline + +on: + push: + pull_request: + workflow_dispatch: + inputs: + test_linux: + description: 'Test on Linux' + type: boolean + default: true + +concurrency: + group: ${{ github.workflow }}-${{ github.ref || github.run_id }} + cancel-in-progress: true + +env: + GTEST_FILTER: "-*.*_Online" + +jobs: + ubuntu: + runs-on: ubuntu-latest + if: > + (github.event_name == 'push') || + (github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name) || + (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true') + strategy: + matrix: + tls_backend: [openssl, no-tls] + name: ubuntu (${{ matrix.tls_backend }}) + steps: + - name: checkout + uses: actions/checkout@v4 + - name: install common libraries + run: | + sudo apt-get update + sudo apt-get install -y libcurl4-openssl-dev zlib1g-dev libbrotli-dev libzstd-dev + - name: install OpenSSL + if: matrix.tls_backend == 'openssl' + run: sudo apt-get install -y libssl-dev + - name: disable network + run: sudo sh -c 'echo > /etc/resolv.conf' + - name: build and run tests (OpenSSL) + if: matrix.tls_backend == 'openssl' + run: cd test && make + env: + LSAN_OPTIONS: suppressions=lsan_suppressions.txt + - name: build and run tests (No TLS) + if: matrix.tls_backend == 'no-tls' + run: cd test && make test_no_tls && ./test_no_tls From c1ee85d89ea2d857509164df6b759e36342ef2e4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Feb 2026 17:46:26 -1000 Subject: [PATCH 0626/1222] Use iptables to disable network (#2363) * Use iptables to disable network * Fix race condition problem * Enable network after test finishes --- .github/workflows/test_offline.yaml | 13 ++++++++- test/test.cc | 44 +++++++++++++++-------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/.github/workflows/test_offline.yaml b/.github/workflows/test_offline.yaml index 64e86177b1..8814877e61 100644 --- a/.github/workflows/test_offline.yaml +++ b/.github/workflows/test_offline.yaml @@ -40,7 +40,13 @@ jobs: if: matrix.tls_backend == 'openssl' run: sudo apt-get install -y libssl-dev - name: disable network - run: sudo sh -c 'echo > /etc/resolv.conf' + run: | + sudo iptables -A OUTPUT -o lo -j ACCEPT + sudo iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT + sudo iptables -A OUTPUT -j REJECT + sudo ip6tables -A OUTPUT -o lo -j ACCEPT + sudo ip6tables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT + sudo ip6tables -A OUTPUT -j REJECT - name: build and run tests (OpenSSL) if: matrix.tls_backend == 'openssl' run: cd test && make @@ -49,3 +55,8 @@ jobs: - name: build and run tests (No TLS) if: matrix.tls_backend == 'no-tls' run: cd test && make test_no_tls && ./test_no_tls + - name: restore network + if: always() + run: | + sudo iptables -F OUTPUT + sudo ip6tables -F OUTPUT diff --git a/test/test.cc b/test/test.cc index 7d16ddbb9d..010b9de8b2 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9114,29 +9114,31 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) { signal(SIGPIPE, SIG_IGN); #endif - auto server_thread = std::thread([&compressed] { - auto srv = ::socket(AF_INET, SOCK_STREAM, 0); - default_socket_options(srv); - detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); - detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); - - sockaddr_in addr{}; - addr.sin_family = AF_INET; - addr.sin_port = htons(PORT + 3); - ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); + // Set up the listening socket in the main thread so the server is guaranteed + // to be ready before the client connects (eliminates race condition). + auto srv = ::socket(AF_INET, SOCK_STREAM, 0); + default_socket_options(srv); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_RCVTIMEO, 5, 0); + detail::set_socket_opt_time(srv, SOL_SOCKET, SO_SNDTIMEO, 5, 0); + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(PORT + 3); + ::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); - int opt = 1; - ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, + int opt = 1; + ::setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, #ifdef _WIN32 - reinterpret_cast(&opt), + reinterpret_cast(&opt), #else - &opt, + &opt, #endif - sizeof(opt)); + sizeof(opt)); - ::bind(srv, reinterpret_cast(&addr), sizeof(addr)); - ::listen(srv, 1); + ASSERT_EQ(0, ::bind(srv, reinterpret_cast(&addr), sizeof(addr))); + ASSERT_EQ(0, ::listen(srv, 1)); + auto server_thread = std::thread([&compressed, srv] { sockaddr_in cli_addr{}; socklen_t cli_len = sizeof(cli_addr); auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len); @@ -9180,10 +9182,12 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) { detail::close_socket(cli); } - detail::close_socket(srv); }); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + auto se = detail::scope_exit([&] { + detail::close_socket(srv); + server_thread.join(); + }); size_t total_decompressed = 0; @@ -9204,8 +9208,6 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) { } } - server_thread.join(); - // The decompressed size must be capped by payload_max_length. Without // protection, the client would decompress the full 10MB from a tiny // compressed payload, enabling a zip bomb DoS attack. From ed5c5d325b14c0bfc7fae578a73b9deb5c0f0f3b Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 12 Feb 2026 20:55:30 -1000 Subject: [PATCH 0627/1222] Parallel test on CI (#2364) * Parallel test on CI * Fix problem with Windows * Use cache for vcpkg * Parallel 'No Exception' test * Use one job to run all shards --- .github/workflows/test.yaml | 44 +++++++- .github/workflows/test_no_exceptions.yaml | 2 +- .github/workflows/test_offline.yaml | 4 +- .gitignore | 1 + justfile | 9 ++ test/Makefile | 38 ++++++- test/test.cc | 119 ++++++++++++++-------- 7 files changed, 166 insertions(+), 51 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 31459e863d..027eda56e7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -94,12 +94,12 @@ jobs: run: sudo apt-get install -y libmbedtls-dev - name: build and run tests (OpenSSL) if: matrix.tls_backend == 'openssl' - run: cd test && make + run: cd test && make test_split && make test_openssl_parallel env: LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (Mbed TLS) if: matrix.tls_backend == 'mbedtls' - run: cd test && make test_split_mbedtls && make test_mbedtls && ./test_mbedtls + run: cd test && make test_split_mbedtls && make test_mbedtls_parallel - name: run fuzz test target if: matrix.tls_backend == 'openssl' run: cd test && make fuzz_test @@ -123,12 +123,12 @@ jobs: run: brew install mbedtls@3 - name: build and run tests (OpenSSL) if: matrix.tls_backend == 'openssl' - run: cd test && make + run: cd test && make test_split && make test_openssl_parallel env: LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (Mbed TLS) if: matrix.tls_backend == 'mbedtls' - run: cd test && make test_split_mbedtls && make test_mbedtls && ./test_mbedtls + run: cd test && make test_split_mbedtls && make test_mbedtls_parallel - name: run fuzz test target if: matrix.tls_backend == 'openssl' run: cd test && make fuzz_test @@ -171,7 +171,14 @@ jobs: core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || ''); - name: Setup msbuild on windows uses: microsoft/setup-msbuild@v2 + - name: Cache vcpkg packages + id: vcpkg-cache + uses: actions/cache@v4 + with: + path: C:/vcpkg/installed + key: vcpkg-installed-windows-gtest-curl-zlib-brotli-zstd - name: Install vcpkg dependencies + if: steps.vcpkg-cache.outputs.cache-hit != 'true' run: vcpkg install gtest curl zlib brotli zstd - name: Install OpenSSL if: ${{ matrix.config.with_ssl }} @@ -192,7 +199,34 @@ jobs: run: cmake --build build --config Release -- /v:m /clp:ShowCommandLine - name: Run tests ${{ matrix.config.name }} if: ${{ matrix.config.run_tests }} - run: ctest --output-on-failure --test-dir build -C Release -E "BenchmarkTest" + shell: pwsh + working-directory: build/test + run: | + $shards = 4 + $procs = @() + for ($i = 0; $i -lt $shards; $i++) { + $log = "shard_${i}.log" + $procs += Start-Process -FilePath ./Release/httplib-test.exe ` + -ArgumentList "--gtest_color=yes","--gtest_filter=${{ github.event.inputs.gtest_filter || '*' }}-*BenchmarkTest*" ` + -NoNewWindow -PassThru -RedirectStandardOutput $log -RedirectStandardError "${log}.err" ` + -Environment @{ GTEST_TOTAL_SHARDS="$shards"; GTEST_SHARD_INDEX="$i" } + } + $procs | Wait-Process + $failed = $false + for ($i = 0; $i -lt $shards; $i++) { + $log = "shard_${i}.log" + if (Select-String -Path $log -Pattern "\[ PASSED \]" -Quiet) { + $passed = (Select-String -Path $log -Pattern "\[ PASSED \]").Line + Write-Host "Shard ${i}: $passed" + } else { + Write-Host "=== Shard $i FAILED ===" + Get-Content $log + if (Test-Path "${log}.err") { Get-Content "${log}.err" } + $failed = $true + } + } + if ($failed) { exit 1 } + Write-Host "All shards passed." - name: Run benchmark tests with retry ${{ matrix.config.name }} if: ${{ matrix.config.run_tests }} run: ctest --output-on-failure --test-dir build -C Release -R "BenchmarkTest" --repeat until-pass:5 diff --git a/.github/workflows/test_no_exceptions.yaml b/.github/workflows/test_no_exceptions.yaml index 7e71635f6c..f525297158 100644 --- a/.github/workflows/test_no_exceptions.yaml +++ b/.github/workflows/test_no_exceptions.yaml @@ -17,4 +17,4 @@ jobs: - name: Run tests with CPPHTTPLIB_NO_EXCEPTIONS run: | - cd test && make EXTRA_CXXFLAGS="-fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS" + cd test && make test_split EXTRA_CXXFLAGS="-fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS" && make test_openssl_parallel EXTRA_CXXFLAGS="-fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS" diff --git a/.github/workflows/test_offline.yaml b/.github/workflows/test_offline.yaml index 8814877e61..42c4495911 100644 --- a/.github/workflows/test_offline.yaml +++ b/.github/workflows/test_offline.yaml @@ -49,12 +49,12 @@ jobs: sudo ip6tables -A OUTPUT -j REJECT - name: build and run tests (OpenSSL) if: matrix.tls_backend == 'openssl' - run: cd test && make + run: cd test && make test_split && make test_openssl_parallel env: LSAN_OPTIONS: suppressions=lsan_suppressions.txt - name: build and run tests (No TLS) if: matrix.tls_backend == 'no-tls' - run: cd test && make test_no_tls && ./test_no_tls + run: cd test && make test_no_tls_parallel - name: restore network if: always() run: | diff --git a/.gitignore b/.gitignore index 80278dc2d0..c2590a0e28 100644 --- a/.gitignore +++ b/.gitignore @@ -46,6 +46,7 @@ test/test.xcodeproj/*/xcuser* test/*.o test/*.pem test/*.srl +test/*.log test/_build_* work/ benchmark/server* diff --git a/justfile b/justfile index 0745a781bf..6b4c5f07fc 100644 --- a/justfile +++ b/justfile @@ -9,13 +9,22 @@ openssl: @(cd test && make test && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test) @(cd test && make proxy) +openssl_parallel: + @(cd test && make test_openssl_parallel) + mbedtls: @(cd test && make test_mbedtls && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test_mbedtls) @(cd test && make proxy_mbedtls) +mbedtls_parallel: + @(cd test && make test_mbedtls_parallel) + no_tls: @(cd test && make test_no_tls && ./test_no_tls) +no_tls_parallel: + @(cd test && make test_no_tls_parallel) + fuzz: @(cd test && make fuzz_test) diff --git a/test/Makefile b/test/Makefile index 9b2d1e75cf..b8f5babee0 100644 --- a/test/Makefile +++ b/test/Makefile @@ -56,6 +56,42 @@ STYLE_CHECK_FILES = $(filter-out httplib.h httplib.cc, \ all : test test_split LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test +SHARDS ?= 4 + +define run_parallel + @echo "Running $(1) with $(SHARDS) shards in parallel..." + @fail=0; \ + for i in $$(seq 0 $$(($(SHARDS) - 1))); do \ + GTEST_TOTAL_SHARDS=$(SHARDS) GTEST_SHARD_INDEX=$$i \ + LSAN_OPTIONS=suppressions=lsan_suppressions.txt \ + ./$(1) --gtest_color=yes > $(1)_shard_$$i.log 2>&1 & \ + done; \ + wait; \ + for i in $$(seq 0 $$(($(SHARDS) - 1))); do \ + if ! grep -q "\[ PASSED \]" $(1)_shard_$$i.log; then \ + echo "=== Shard $$i FAILED ==="; \ + cat $(1)_shard_$$i.log; \ + fail=1; \ + else \ + passed=$$(grep "\[ PASSED \]" $(1)_shard_$$i.log); \ + echo "Shard $$i: $$passed"; \ + fi; \ + done; \ + if [ $$fail -ne 0 ]; then exit 1; fi; \ + echo "All shards passed." +endef + +.PHONY: test_openssl_parallel test_mbedtls_parallel test_no_tls_parallel + +test_openssl_parallel : test + $(call run_parallel,test) + +test_mbedtls_parallel : test_mbedtls + $(call run_parallel,test_mbedtls) + +test_no_tls_parallel : test_no_tls + $(call run_parallel,test_no_tls) + proxy : test_proxy @echo "Starting proxy server..." cd proxy && \ @@ -172,5 +208,5 @@ cert.pem: ./gen-certs.sh clean: - rm -rf test test_split test_mbedtls test_split_mbedtls test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM + rm -rf test test_split test_mbedtls test_split_mbedtls test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM *_shard_*.log diff --git a/test/test.cc b/test/test.cc index 010b9de8b2..de29274457 100644 --- a/test/test.cc +++ b/test/test.cc @@ -55,7 +55,13 @@ using namespace std; using namespace httplib; const char *HOST = "localhost"; -const int PORT = 1234; + +static int get_base_port() { + const char *shard = getenv("GTEST_SHARD_INDEX"); + return shard ? 11234 + std::atoi(shard) * 100 : 1234; +} + +const int PORT = get_base_port(); const string LONG_QUERY_VALUE = string(25000, '@'); const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE; @@ -178,7 +184,12 @@ class UnixSocketTest : public ::testing::Test { EXPECT_EQ(resp.body, content_); } - const std::string pathname_{"./httplib-server.sock"}; + static std::string make_sock_path() { + const char *shard = getenv("GTEST_SHARD_INDEX"); + return shard ? std::string("./httplib-server-") + shard + ".sock" + : "./httplib-server.sock"; + } + const std::string pathname_{make_sock_path()}; const std::string pattern_{"/hi"}; const std::string content_{"Hello World!"}; }; @@ -2370,28 +2381,32 @@ TEST(RedirectFromPageWithContent, Redirect) { TEST(RedirectFromPageWithContentIP6, Redirect) { Server svr; + auto port_str = std::to_string(PORT); + auto redirect_url = "http://[::1]:" + port_str + "/2"; + auto expected_host = "[::1]:" + port_str; + svr.Get("/1", [&](const Request & /*req*/, Response &res) { res.set_content("___", "text/plain"); // res.set_redirect("/2"); - res.set_redirect("http://[::1]:1234/2"); + res.set_redirect(redirect_url); }); svr.Get("/2", [&](const Request &req, Response &res) { auto host_header = req.headers.find("Host"); ASSERT_TRUE(host_header != req.headers.end()); - EXPECT_EQ("[::1]:1234", host_header->second); + EXPECT_EQ(expected_host, host_header->second); res.set_content("Hello World!", "text/plain"); }); - auto th = std::thread([&]() { svr.listen("::1", 1234); }); + auto th = std::thread([&]() { svr.listen("::1", PORT); }); auto se = detail::scope_exit([&] { svr.stop(); th.join(); ASSERT_FALSE(svr.is_running()); }); - // When IPV6 support isn't available svr.listen("::1", 1234) never + // When IPV6 support isn't available svr.listen("::1", PORT) never // actually starts anything, so the condition !svr.is_running() will // always remain true, and the loop never stops. // This basically counts how many milliseconds have passed since the @@ -2403,7 +2418,7 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { } { - Client cli("http://[::1]:1234"); + Client cli("::1", PORT); cli.set_follow_location(true); std::string body; @@ -2418,7 +2433,7 @@ TEST(RedirectFromPageWithContentIP6, Redirect) { } { - Client cli("http://[::1]:1234"); + Client cli("::1", PORT); std::string body; auto res = cli.Get("/1", [&](const char *data, size_t data_length) { @@ -10471,7 +10486,8 @@ TEST(ClientImplMethods, GetSocketTest) { res.status = StatusCode::OK_200; }); - auto thread = std::thread([&]() { svr.listen("127.0.0.1", 3333); }); + auto port = svr.bind_to_any_port("127.0.0.1"); + auto thread = std::thread([&]() { svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { svr.stop(); thread.join(); @@ -10481,7 +10497,7 @@ TEST(ClientImplMethods, GetSocketTest) { svr.wait_until_ready(); { - httplib::Client cli("http://127.0.0.1:3333"); + httplib::Client cli("127.0.0.1", port); cli.set_keep_alive(true); // Use the behavior of cpp-httplib of opening the connection @@ -10640,10 +10656,13 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) { } TEST(HttpToHttpsRedirectTest, CertFile) { + auto ssl_port = PORT + 1; + Server svr; ASSERT_TRUE(svr.is_valid()); svr.Get("/index", [&](const Request &, Response &res) { - res.set_redirect("https://127.0.0.1:1235/index"); + res.set_redirect("https://127.0.0.1:" + std::to_string(ssl_port) + + "/index"); svr.stop(); }); @@ -10655,7 +10674,8 @@ TEST(HttpToHttpsRedirectTest, CertFile) { }); thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); }); - thread t2 = thread([&]() { ASSERT_TRUE(ssl_svr.listen("127.0.0.1", 1235)); }); + thread t2 = + thread([&]() { ASSERT_TRUE(ssl_svr.listen("127.0.0.1", ssl_port)); }); auto se = detail::scope_exit([&] { t2.join(); t.join(); @@ -10677,10 +10697,13 @@ TEST(HttpToHttpsRedirectTest, CertFile) { } TEST(SSLClientRedirectTest, CertFile) { + auto ssl_port = PORT + 1; + SSLServer ssl_svr1(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE); ASSERT_TRUE(ssl_svr1.is_valid()); ssl_svr1.Get("/index", [&](const Request &, Response &res) { - res.set_redirect("https://127.0.0.1:1235/index"); + res.set_redirect("https://127.0.0.1:" + std::to_string(ssl_port) + + "/index"); ssl_svr1.stop(); }); @@ -10693,7 +10716,7 @@ TEST(SSLClientRedirectTest, CertFile) { thread t = thread([&]() { ASSERT_TRUE(ssl_svr1.listen("127.0.0.1", PORT)); }); thread t2 = - thread([&]() { ASSERT_TRUE(ssl_svr2.listen("127.0.0.1", 1235)); }); + thread([&]() { ASSERT_TRUE(ssl_svr2.listen("127.0.0.1", ssl_port)); }); auto se = detail::scope_exit([&] { t2.join(); t.join(); @@ -10774,7 +10797,8 @@ TEST(MultipartFormDataTest, LargeData) { } }); - auto t = std::thread([&]() { svr.listen(HOST, 8080); }); + auto port = svr.bind_to_any_port(HOST); + auto t = std::thread([&]() { svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -10788,7 +10812,7 @@ TEST(MultipartFormDataTest, LargeData) { std::stringstream buffer; buffer << data; - Client cli("https://localhost:8080"); + SSLClient cli(HOST, port); cli.enable_server_certificate_verification(false); UploadFormDataItems items{ @@ -10921,7 +10945,8 @@ TEST(MultipartFormDataTest, DataProviderItems) { EXPECT_EQ(items[3].content_type, ""); }); - auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto port = svr.bind_to_any_port("localhost"); + auto t = std::thread([&]() { svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -10931,7 +10956,7 @@ TEST(MultipartFormDataTest, DataProviderItems) { svr.wait_until_ready(); { - Client cli("https://localhost:8080"); + SSLClient cli("localhost", port); cli.enable_server_certificate_verification(false); UploadFormDataItems items{ @@ -11122,7 +11147,8 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { } }); - auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto port = svr.bind_to_any_port("localhost"); + auto t = std::thread([&]() { svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -11136,7 +11162,7 @@ TEST(MultipartFormDataTest, PostCustomBoundary) { std::stringstream buffer; buffer << data; - Client cli("https://localhost:8080"); + SSLClient cli("localhost", port); cli.enable_server_certificate_verification(false); UploadFormDataItems items{ @@ -11205,7 +11231,8 @@ TEST(MultipartFormDataTest, PutFormData) { } }); - auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto port = svr.bind_to_any_port("localhost"); + auto t = std::thread([&]() { svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -11219,7 +11246,7 @@ TEST(MultipartFormDataTest, PutFormData) { std::stringstream buffer; buffer << data; - Client cli("https://localhost:8080"); + SSLClient cli("localhost", port); cli.enable_server_certificate_verification(false); UploadFormDataItems items{ @@ -11269,7 +11296,8 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { } }); - auto t = std::thread([&]() { svr.listen("localhost", 8080); }); + auto port = svr.bind_to_any_port("localhost"); + auto t = std::thread([&]() { svr.listen_after_bind(); }); auto se = detail::scope_exit([&] { svr.stop(); t.join(); @@ -11283,7 +11311,7 @@ TEST(MultipartFormDataTest, PutFormDataCustomBoundary) { std::stringstream buffer; buffer << data; - Client cli("https://localhost:8080"); + SSLClient cli("localhost", port); cli.enable_server_certificate_verification(false); UploadFormDataItems items{ @@ -13176,7 +13204,8 @@ class OpenStreamTest : public ::testing::Test { } res.set_content(body, "text/plain"); }); - thread_ = std::thread([this]() { svr_.listen("127.0.0.1", 8787); }); + port_ = svr_.bind_to_any_port("127.0.0.1"); + thread_ = std::thread([this]() { svr_.listen_after_bind(); }); svr_.wait_until_ready(); } void TearDown() override { @@ -13185,17 +13214,18 @@ class OpenStreamTest : public ::testing::Test { } Server svr_; std::thread thread_; + int port_ = 0; }; TEST_F(OpenStreamTest, Basic) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/hello"); EXPECT_TRUE(handle.is_valid()); EXPECT_EQ("Hello World!", read_all(handle)); } TEST_F(OpenStreamTest, SmallBuffer) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/hello"); std::string result; char buf[4]; @@ -13206,14 +13236,15 @@ TEST_F(OpenStreamTest, SmallBuffer) { } TEST_F(OpenStreamTest, DefaultHeaders) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); // open_stream GET should include Host, User-Agent and Accept-Encoding { auto handle = cli.open_stream("GET", "/echo-headers"); ASSERT_TRUE(handle.is_valid()); auto body = read_all(handle); - EXPECT_NE(body.find("Host:127.0.0.1:8787"), std::string::npos); + EXPECT_NE(body.find("Host:127.0.0.1:" + std::to_string(port_)), + std::string::npos); EXPECT_NE(body.find("User-Agent:cpp-httplib/" CPPHTTPLIB_VERSION), std::string::npos); EXPECT_NE(body.find("Accept-Encoding:"), std::string::npos); @@ -13251,7 +13282,7 @@ TEST_F(OpenStreamTest, DefaultHeaders) { } TEST_F(OpenStreamTest, Large) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/large"); EXPECT_EQ(10000u, read_all(handle).size()); } @@ -13263,7 +13294,7 @@ TEST_F(OpenStreamTest, ConnectionError) { } TEST_F(OpenStreamTest, Chunked) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/chunked"); EXPECT_TRUE(handle.response && handle.response->get_header_value( "Transfer-Encoding") == "chunked"); @@ -13271,7 +13302,7 @@ TEST_F(OpenStreamTest, Chunked) { } TEST_F(OpenStreamTest, ProhibitedTrailersAreIgnored_Stream) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/streamed-chunked-with-prohibited-trailer"); ASSERT_TRUE(handle.is_valid()); @@ -13304,7 +13335,7 @@ TEST_F(OpenStreamTest, ProhibitedTrailersAreIgnored_Stream) { #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(OpenStreamTest, Gzip) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/compressible", {}, {{"Accept-Encoding", "gzip"}}); EXPECT_EQ("gzip", handle.response->get_header_value("Content-Encoding")); @@ -13314,7 +13345,7 @@ TEST_F(OpenStreamTest, Gzip) { #ifdef CPPHTTPLIB_BROTLI_SUPPORT TEST_F(OpenStreamTest, Brotli) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/compressible", {}, {{"Accept-Encoding", "br"}}); EXPECT_EQ("br", handle.response->get_header_value("Content-Encoding")); @@ -13324,7 +13355,7 @@ TEST_F(OpenStreamTest, Brotli) { #ifdef CPPHTTPLIB_ZSTD_SUPPORT TEST_F(OpenStreamTest, Zstd) { - Client cli("127.0.0.1", 8787); + Client cli("127.0.0.1", port_); auto handle = cli.open_stream("GET", "/compressible", {}, {{"Accept-Encoding", "zstd"}}); EXPECT_EQ("zstd", handle.response->get_header_value("Content-Encoding")); @@ -13365,7 +13396,8 @@ class SSLOpenStreamTest : public ::testing::Test { return true; }); }); - thread_ = std::thread([this]() { svr_.listen("127.0.0.1", 8788); }); + port_ = svr_.bind_to_any_port("127.0.0.1"); + thread_ = std::thread([this]() { svr_.listen_after_bind(); }); svr_.wait_until_ready(); } void TearDown() override { @@ -13374,10 +13406,11 @@ class SSLOpenStreamTest : public ::testing::Test { } SSLServer svr_; std::thread thread_; + int port_ = 0; }; TEST_F(SSLOpenStreamTest, Basic) { - SSLClient cli("127.0.0.1", 8788); + SSLClient cli("127.0.0.1", port_); cli.enable_server_certificate_verification(false); auto handle = cli.open_stream("GET", "/hello"); ASSERT_TRUE(handle.is_valid()); @@ -13385,7 +13418,7 @@ TEST_F(SSLOpenStreamTest, Basic) { } TEST_F(SSLOpenStreamTest, Chunked) { - SSLClient cli("127.0.0.1", 8788); + SSLClient cli("127.0.0.1", port_); cli.enable_server_certificate_verification(false); auto handle = cli.open_stream("GET", "/chunked"); @@ -13399,7 +13432,7 @@ TEST_F(SSLOpenStreamTest, Chunked) { } TEST_F(SSLOpenStreamTest, Post) { - SSLClient cli("127.0.0.1", 8788); + SSLClient cli("127.0.0.1", port_); cli.enable_server_certificate_verification(false); auto handle = @@ -13413,7 +13446,7 @@ TEST_F(SSLOpenStreamTest, Post) { } TEST_F(SSLOpenStreamTest, PostChunked) { - SSLClient cli("127.0.0.1", 8788); + SSLClient cli("127.0.0.1", port_); cli.enable_server_certificate_verification(false); auto handle = cli.open_stream("POST", "/chunked-response", {}, {}, @@ -13738,7 +13771,8 @@ class SSLStreamApiTest : public ::testing::Test { svr_.Post("/echo", [](const httplib::Request &req, httplib::Response &res) { res.set_content(req.body, "text/plain"); }); - thread_ = std::thread([this]() { svr_.listen("127.0.0.1", 8803); }); + port_ = svr_.bind_to_any_port("127.0.0.1"); + thread_ = std::thread([this]() { svr_.listen_after_bind(); }); svr_.wait_until_ready(); } void TearDown() override { @@ -13747,10 +13781,11 @@ class SSLStreamApiTest : public ::testing::Test { } httplib::SSLServer svr_{"cert.pem", "key.pem"}; std::thread thread_; + int port_ = 0; }; TEST_F(SSLStreamApiTest, GetAndPost) { - httplib::SSLClient cli("127.0.0.1", 8803); + httplib::SSLClient cli("127.0.0.1", port_); cli.enable_server_certificate_verification(false); auto get = httplib::stream::Get(cli, "/hello"); EXPECT_EQ("Hello SSL!", read_body(get)); From 8d225afc8cbd9b82fef3543dcb52fe504a2351ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Fri, 13 Feb 2026 19:44:06 +0100 Subject: [PATCH 0628/1222] Remove macOS select() fallback (#2365) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove macOS select() fallback macOS has supported `poll` for a long time now, so there's no need for the specific `select` code paths. With this commit, we can successfully build on visionOS. Signed-off-by: Adrien Gallouët * Fix coding style Signed-off-by: Adrien Gallouët --------- Signed-off-by: Adrien Gallouët --- httplib.h | 72 ++++++------------------------------------------------- 1 file changed, 7 insertions(+), 65 deletions(-) diff --git a/httplib.h b/httplib.h index 7ffb753094..4665957864 100644 --- a/httplib.h +++ b/httplib.h @@ -4600,78 +4600,32 @@ inline int poll_wrapper(struct pollfd *fds, nfds_t nfds, int timeout) { #endif } -template -inline ssize_t select_impl(socket_t sock, time_t sec, time_t usec) { -#ifdef __APPLE__ - if (sock >= FD_SETSIZE) { return -1; } - - fd_set fds, *rfds, *wfds; - FD_ZERO(&fds); - FD_SET(sock, &fds); - rfds = (Read ? &fds : nullptr); - wfds = (Read ? nullptr : &fds); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - return handle_EINTR([&]() { - return select(static_cast(sock + 1), rfds, wfds, nullptr, &tv); - }); -#else +inline ssize_t select_impl(socket_t sock, short events, time_t sec, + time_t usec) { struct pollfd pfd; pfd.fd = sock; - pfd.events = (Read ? POLLIN : POLLOUT); + pfd.events = events; + pfd.revents = 0; auto timeout = static_cast(sec * 1000 + usec / 1000); return handle_EINTR([&]() { return poll_wrapper(&pfd, 1, timeout); }); -#endif } inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { - return select_impl(sock, sec, usec); + return select_impl(sock, POLLIN, sec, usec); } inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { - return select_impl(sock, sec, usec); + return select_impl(sock, POLLOUT, sec, usec); } inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { -#ifdef __APPLE__ - if (sock >= FD_SETSIZE) { return Error::Connection; } - - fd_set fdsr, fdsw; - FD_ZERO(&fdsr); - FD_ZERO(&fdsw); - FD_SET(sock, &fdsr); - FD_SET(sock, &fdsw); - - timeval tv; - tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); - - auto ret = handle_EINTR([&]() { - return select(static_cast(sock + 1), &fdsr, &fdsw, nullptr, &tv); - }); - - if (ret == 0) { return Error::ConnectionTimeout; } - - if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { - auto error = 0; - socklen_t len = sizeof(error); - auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); - auto successful = res >= 0 && !error; - return successful ? Error::Success : Error::Connection; - } - - return Error::Connection; -#else struct pollfd pfd_read; pfd_read.fd = sock; pfd_read.events = POLLIN | POLLOUT; + pfd_read.revents = 0; auto timeout = static_cast(sec * 1000 + usec / 1000); @@ -4690,7 +4644,6 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, } return Error::Connection; -#endif } inline bool is_socket_alive(socket_t sock) { @@ -10474,17 +10427,6 @@ Server::process_request(Stream &strm, const std::string &remote_addr, res.version = "HTTP/1.1"; res.headers = default_headers_; -#ifdef __APPLE__ - // Socket file descriptor exceeded FD_SETSIZE... - if (strm.socket() >= FD_SETSIZE) { - Headers dummy; - detail::read_headers(strm, dummy); - res.status = StatusCode::InternalServerError_500; - output_error_log(Error::ExceedMaxSocketDescriptorCount, &req); - return write_response(strm, close_connection, req, res); - } -#endif - // Request line and headers if (!parse_request_line(line_reader.ptr(), req)) { res.status = StatusCode::BadRequest_400; From d4180e923f846b44a3d30acd938438d6e64fc9f6 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Fri, 13 Feb 2026 23:25:59 +0100 Subject: [PATCH 0629/1222] Fix comparison of integers of different signs warning when compiling with BoringSSL. (#2367) --- httplib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 4665957864..a35a1ae2e5 100644 --- a/httplib.h +++ b/httplib.h @@ -15341,7 +15341,7 @@ inline bool get_cert_sans(cert_t cert, std::vector &sans) { if (!names) return true; // No SANs is valid auto count = sk_GENERAL_NAME_num(names); - for (int i = 0; i < count; i++) { + for (decltype(count) i = 0; i < count; i++) { auto gen = sk_GENERAL_NAME_value(names, i); if (!gen) continue; From 464867a9cedef2b6185e25e3e912d3f796fe58fd Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 14 Feb 2026 17:44:49 -0500 Subject: [PATCH 0630/1222] WebSocket and Dynamic Thread Pool support (#2368) * WebSocket support * Validate selected subprotocol in WebSocket handshake * Fix problem with a Unit test * Dynamic Thread Pool support * Fix race condition in new Dynamic ThreadPool --- .github/workflows/test.yaml | 10 + .gitignore | 4 + README-websocket.md | 391 ++++++++++ README.md | 53 +- example/Makefile | 7 +- example/wsecho.cc | 135 ++++ httplib.h | 1252 ++++++++++++++++++++++++++++-- test/Makefile | 8 + test/test.cc | 772 +++++++++++++++++- test/test_thread_pool.cc | 228 ++++++ test/test_websocket_heartbeat.cc | 77 ++ 11 files changed, 2875 insertions(+), 62 deletions(-) create mode 100644 README-websocket.md create mode 100644 example/wsecho.cc create mode 100644 test/test_thread_pool.cc create mode 100644 test/test_websocket_heartbeat.cc diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 027eda56e7..69158d3f7d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -103,6 +103,11 @@ jobs: - name: run fuzz test target if: matrix.tls_backend == 'openssl' run: cd test && make fuzz_test + - name: build and run WebSocket heartbeat test + if: matrix.tls_backend == 'openssl' + run: cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat + - name: build and run ThreadPool test + run: cd test && make test_thread_pool && ./test_thread_pool macos: runs-on: macos-latest @@ -132,6 +137,11 @@ jobs: - name: run fuzz test target if: matrix.tls_backend == 'openssl' run: cd test && make fuzz_test + - name: build and run WebSocket heartbeat test + if: matrix.tls_backend == 'openssl' + run: cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat + - name: build and run ThreadPool test + run: cd test && make test_thread_pool && ./test_thread_pool windows: runs-on: windows-latest diff --git a/.gitignore b/.gitignore index c2590a0e28..04d7498fd9 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ example/server_and_client !example/server_and_client.* example/accept_header !example/accept_header.* +example/wsecho +!example/wsecho.* example/*.pem test/httplib.cc test/httplib.h @@ -41,6 +43,8 @@ test/test_proxy_mbedtls test/test_split test/test_split_mbedtls test/test_split_no_tls +test/test_websocket_heartbeat +test/test_thread_pool test/test.xcodeproj/xcuser* test/test.xcodeproj/*/xcuser* test/*.o diff --git a/README-websocket.md b/README-websocket.md new file mode 100644 index 0000000000..31ed826354 --- /dev/null +++ b/README-websocket.md @@ -0,0 +1,391 @@ +# WebSocket - RFC 6455 WebSocket Support + +A simple, blocking WebSocket implementation for C++11. + +> [!IMPORTANT] +> This is a blocking I/O WebSocket implementation using a thread-per-connection model. If you need high-concurrency WebSocket support with non-blocking/async I/O (e.g., thousands of simultaneous connections), this is not the one that you want. + +## Features + +- **RFC 6455 compliant**: Full WebSocket protocol support +- **Server and Client**: Both sides included +- **SSL/TLS support**: `wss://` scheme for secure connections +- **Text and Binary**: Both message types supported +- **Automatic heartbeat**: Periodic Ping/Pong keeps connections alive +- **Subprotocol negotiation**: `Sec-WebSocket-Protocol` support for GraphQL, MQTT, etc. + +## Quick Start + +### Server + +```cpp +httplib::Server svr; + +svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) { + std::string msg; + while (ws.read(msg)) { + ws.send("echo: " + msg); + } +}); + +svr.listen("localhost", 8080); +``` + +### Client + +```cpp +httplib::ws::WebSocketClient ws("ws://localhost:8080/ws"); + +if (ws.connect()) { + ws.send("hello"); + + std::string msg; + if (ws.read(msg)) { + std::cout << msg << std::endl; // "echo: hello" + } + ws.close(); +} +``` + +## API Reference + +### ReadResult + +```cpp +enum ReadResult : int { + Fail = 0, // Connection closed or error + Text = 1, // UTF-8 text message + Binary = 2, // Binary message +}; +``` + +Returned by `read()`. Since `Fail` is `0`, the result works naturally in boolean contexts — `while (ws.read(msg))` continues until the connection closes. When you need to distinguish text from binary, check the return value directly. + +### CloseStatus + +```cpp +enum class CloseStatus : uint16_t { + Normal = 1000, + GoingAway = 1001, + ProtocolError = 1002, + UnsupportedData = 1003, + NoStatus = 1005, + Abnormal = 1006, + InvalidPayload = 1007, + PolicyViolation = 1008, + MessageTooBig = 1009, + MandatoryExtension = 1010, + InternalError = 1011, +}; +``` + +### Server Registration + +```cpp +// Basic handler +Server &WebSocket(const std::string &pattern, WebSocketHandler handler); + +// With subprotocol negotiation +Server &WebSocket(const std::string &pattern, WebSocketHandler handler, + SubProtocolSelector sub_protocol_selector); +``` + +**Type aliases:** + +```cpp +using WebSocketHandler = + std::function; +using SubProtocolSelector = + std::function &protocols)>; +``` + +The `SubProtocolSelector` receives the list of subprotocols proposed by the client (from the `Sec-WebSocket-Protocol` header) and returns the selected one. Return an empty string to decline all proposed subprotocols. + +### WebSocket (Server-side) + +Passed to the handler registered with `Server::WebSocket()`. The handler runs in a dedicated thread per connection. + +```cpp +// Read next message (blocks until received, returns Fail/Text/Binary) +ReadResult read(std::string &msg); + +// Send messages +bool send(const std::string &data); // Text +bool send(const char *data, size_t len); // Binary + +// Close the connection +void close(CloseStatus status = CloseStatus::Normal, + const std::string &reason = ""); + +// Access the original HTTP upgrade request +const Request &request() const; + +// Check if the connection is still open +bool is_open() const; +``` + +### WebSocketClient + +```cpp +// Constructor - accepts ws:// or wss:// URL +explicit WebSocketClient(const std::string &scheme_host_port_path, + const Headers &headers = {}); + +// Check if the URL was parsed successfully +bool is_valid() const; + +// Connect (performs HTTP upgrade handshake) +bool connect(); + +// Get the subprotocol selected by the server (empty if none) +const std::string &subprotocol() const; + +// Read/Send/Close (same as server-side WebSocket) +ReadResult read(std::string &msg); +bool send(const std::string &data); +bool send(const char *data, size_t len); +void close(CloseStatus status = CloseStatus::Normal, + const std::string &reason = ""); +bool is_open() const; + +// Timeouts +void set_read_timeout(time_t sec, time_t usec = 0); +void set_write_timeout(time_t sec, time_t usec = 0); + +// SSL configuration (wss:// only, requires CPPHTTPLIB_OPENSSL_SUPPORT) +void set_ca_cert_path(const std::string &path); +void set_ca_cert_store(tls::ca_store_t store); +void enable_server_certificate_verification(bool enabled); +``` + +## Examples + +### Echo Server with Connection Logging + +```cpp +httplib::Server svr; + +svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) { + std::cout << "Connected from " << req.remote_addr << std::endl; + + std::string msg; + while (ws.read(msg)) { + ws.send("echo: " + msg); + } + + std::cout << "Disconnected" << std::endl; +}); + +svr.listen("localhost", 8080); +``` + +### Client: Continuous Read Loop + +```cpp +httplib::ws::WebSocketClient ws("ws://localhost:8080/ws"); + +if (ws.connect()) { + ws.send("hello"); + ws.send("world"); + + std::string msg; + while (ws.read(msg)) { // blocks until a message arrives + std::cout << msg << std::endl; // "echo: hello", "echo: world" + } + // read() returns false when the server closes the connection +} +``` + +### Text and Binary Messages + +Check the `ReadResult` return value to distinguish between text and binary: + +```cpp +// Server +svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) { + std::string msg; + httplib::ws::ReadResult ret; + while ((ret = ws.read(msg))) { + if (ret == httplib::ws::Text) { + ws.send("echo: " + msg); + } else { + ws.send(msg.data(), msg.size()); // Binary echo + } + } +}); + +// Client +httplib::ws::WebSocketClient ws("ws://localhost:8080/ws"); +if (ws.connect()) { + // Send binary data + const char binary[] = {0x00, 0x01, 0x02, 0x03}; + ws.send(binary, sizeof(binary)); + + // Receive and check the type + std::string msg; + if (ws.read(msg) == httplib::ws::Binary) { + // Process binary data in msg + } + ws.close(); +} +``` + +### SSL Client + +```cpp +httplib::ws::WebSocketClient ws("wss://echo.example.com/ws"); + +if (ws.connect()) { + ws.send("hello over TLS"); + + std::string msg; + if (ws.read(msg)) { + std::cout << msg << std::endl; + } + ws.close(); +} +``` + +### Close with Status + +```cpp +// Client-side: close with a specific status code and reason +ws.close(httplib::ws::CloseStatus::GoingAway, "shutting down"); + +// Server-side: close with a policy violation status +ws.close(httplib::ws::CloseStatus::PolicyViolation, "forbidden"); +``` + +### Accessing the Upgrade Request + +```cpp +svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) { + // Access headers from the original HTTP upgrade request + auto auth = req.get_header_value("Authorization"); + if (auth.empty()) { + ws.close(httplib::ws::CloseStatus::PolicyViolation, "unauthorized"); + return; + } + + std::string msg; + while (ws.read(msg)) { + ws.send("echo: " + msg); + } +}); +``` + +### Custom Headers and Timeouts + +```cpp +httplib::Headers headers = { + {"Authorization", "Bearer token123"} +}; + +httplib::ws::WebSocketClient ws("ws://localhost:8080/ws", headers); +ws.set_read_timeout(30, 0); // 30 seconds +ws.set_write_timeout(10, 0); // 10 seconds + +if (ws.connect()) { + std::string msg; + while (ws.read(msg)) { + std::cout << msg << std::endl; + } +} +``` + +### Subprotocol Negotiation + +The server can negotiate a subprotocol with the client using `Sec-WebSocket-Protocol`. This is required for protocols like GraphQL over WebSocket (`graphql-ws`) and MQTT. + +```cpp +// Server: register a handler with a subprotocol selector +svr.WebSocket( + "/ws", + [](const httplib::Request &req, httplib::ws::WebSocket &ws) { + std::string msg; + while (ws.read(msg)) { + ws.send("echo: " + msg); + } + }, + [](const std::vector &protocols) -> std::string { + // The client proposed a list of subprotocols; pick one + for (const auto &p : protocols) { + if (p == "graphql-ws" || p == "graphql-transport-ws") { + return p; + } + } + return ""; // Decline all + }); + +// Client: propose subprotocols via Sec-WebSocket-Protocol header +httplib::Headers headers = { + {"Sec-WebSocket-Protocol", "graphql-ws, graphql-transport-ws"} +}; +httplib::ws::WebSocketClient ws("ws://localhost:8080/ws", headers); + +if (ws.connect()) { + // Check which subprotocol the server selected + std::cout << "Subprotocol: " << ws.subprotocol() << std::endl; + // => "graphql-ws" + ws.close(); +} +``` + +### SSL Client with Certificate Configuration + +```cpp +httplib::ws::WebSocketClient ws("wss://example.com/ws"); +ws.set_ca_cert_path("/path/to/ca-bundle.crt"); +ws.enable_server_certificate_verification(true); + +if (ws.connect()) { + ws.send("secure message"); + ws.close(); +} +``` + +## Configuration + +| Macro | Default | Description | +|---------------------------------------------|-------------------|----------------------------------------------------------| +| `CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH` | `16777216` (16MB) | Maximum payload size per message | +| `CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND` | `300` | Read timeout for WebSocket connections (seconds) | +| `CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND` | `5` | Timeout for waiting peer's Close response (seconds) | +| `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND` | `30` | Automatic Ping interval for heartbeat (seconds) | + +## Threading Model + +WebSocket connections share the same thread pool as HTTP requests. Each WebSocket connection occupies one thread for its entire lifetime. The default thread pool size is `CPPHTTPLIB_THREAD_POOL_COUNT` (8). + +This means that if you have 8 simultaneous WebSocket connections with the default settings, **no more HTTP requests can be processed** until a WebSocket connection closes and frees up a thread. + +If your application uses WebSocket, you should increase the thread pool size: + +```cpp +httplib::Server svr; + +svr.new_task_queue = [] { + return new httplib::ThreadPool(128); // Increase from default 8 +}; +``` + +Choose a size that accounts for both your expected HTTP load and the maximum number of simultaneous WebSocket connections. + +## Protocol + +The implementation follows [RFC 6455](https://tools.ietf.org/html/rfc6455): + +- Handshake via HTTP Upgrade with `Sec-WebSocket-Key` / `Sec-WebSocket-Accept` +- Subprotocol negotiation via `Sec-WebSocket-Protocol` +- Frame masking (client-to-server) +- Control frames: Close, Ping, Pong +- Message fragmentation and reassembly +- Close handshake with status codes + +## Browser Test + +Run the echo server example and open `http://localhost:8080` in a browser: + +```bash +cd example && make wsecho && ./wsecho +``` diff --git a/README.md b/README.md index 52047c1be1..2ba98b7104 100644 --- a/README.md +++ b/README.md @@ -711,20 +711,24 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e ### Default thread pool support -`ThreadPool` is used as the **default** task queue, with a default thread count of 8 or `std::thread::hardware_concurrency() - 1`, whichever is greater. You can change it with `CPPHTTPLIB_THREAD_POOL_COUNT`. +`ThreadPool` is used as the **default** task queue, with dynamic scaling support. By default, it maintains a base thread count of 8 or `std::thread::hardware_concurrency() - 1` (whichever is greater), and can scale up to 4x that count under load. You can change these with `CPPHTTPLIB_THREAD_POOL_COUNT` and `CPPHTTPLIB_THREAD_POOL_MAX_COUNT`. -If you want to set the thread count at runtime, there is no convenient way... But here is how. +When all threads are busy and a new task arrives, a temporary thread is spawned (up to the maximum). When a dynamic thread finishes its task and the queue is empty, or after an idle timeout, it exits automatically. The idle timeout defaults to 3 seconds, configurable via `CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT`. + +If you want to set the thread counts at runtime: ```cpp -svr.new_task_queue = [] { return new ThreadPool(12); }; +svr.new_task_queue = [] { return new ThreadPool(/*base_threads=*/8, /*max_threads=*/64); }; ``` +#### Max queued requests + You can also provide an optional parameter to limit the maximum number of pending requests, i.e. requests `accept()`ed by the listener but still waiting to be serviced by worker threads. ```cpp -svr.new_task_queue = [] { return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); }; +svr.new_task_queue = [] { return new ThreadPool(/*base_threads=*/12, /*max_threads=*/0, /*max_queued_requests=*/18); }; ``` Default limit is 0 (unlimited). Once the limit is reached, the listener @@ -1344,6 +1348,47 @@ int main() { See [README-sse.md](README-sse.md) for more details. +WebSocket +--------- + +```cpp +// Server +httplib::Server svr; + +svr.WebSocket("/ws", [](const httplib::Request &req, httplib::ws::WebSocket &ws) { + httplib::ws::Message msg; + while (ws.read(msg)) { + if (msg.is_text()) { + ws.send("Echo: " + msg.data); + } + } +}); + +svr.listen("localhost", 8080); +``` + +```cpp +// Client +httplib::ws::WebSocketClient ws("ws://localhost:8080/ws"); + +if (ws.connect()) { + ws.send("Hello, WebSocket!"); + + std::string msg; + if (ws.read(msg)) { + std::cout << "Received: " << msg << std::endl; + } + + ws.close(); +} +``` + +SSL is also supported via `wss://` scheme (e.g. `WebSocketClient("wss://example.com/ws")`). Subprotocol negotiation (`Sec-WebSocket-Protocol`) is supported via `SubProtocolSelector` callback. + +> **Note:** WebSocket connections occupy a thread for their entire lifetime. If you plan to handle many simultaneous WebSocket connections, consider using a dynamic thread pool: `svr.new_task_queue = [] { return new ThreadPool(8, 64); };` + +See [README-websocket.md](README-websocket.md) for more details. + Split httplib.h into .h and .cc ------------------------------- diff --git a/example/Makefile b/example/Makefile index 3082b88014..64ce3aaaaf 100644 --- a/example/Makefile +++ b/example/Makefile @@ -18,7 +18,7 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz BROTLI_DIR = $(PREFIX)/opt/brotli BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec -all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli wsecho benchmark one_time_request server_and_client accept_header server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -47,6 +47,9 @@ ssesvr : ssesvr.cc ../httplib.h Makefile ssecli : ssecli.cc ../httplib.h Makefile $(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) +wsecho : wsecho.cc ../httplib.h Makefile + $(CXX) -o wsecho $(CXXFLAGS) wsecho.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) + benchmark : benchmark.cc ../httplib.h Makefile $(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) @@ -64,4 +67,4 @@ pem: openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client accept_header *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr ssecli wsecho benchmark one_time_request server_and_client accept_header *.pem diff --git a/example/wsecho.cc b/example/wsecho.cc new file mode 100644 index 0000000000..c03d2f3091 --- /dev/null +++ b/example/wsecho.cc @@ -0,0 +1,135 @@ +#include +#include + +using namespace httplib; + +const auto html = R"HTML( + + + + +WebSocket Demo + + + +

WebSocket Demo

+

Server accepts subprotocols: echo, chat (or none)

+ +
+ + + + +
+ +
+ + +
+ +
+ + + +
+ +

+
+
+
+
+)HTML";
+
+int main(void) {
+  Server svr;
+
+  svr.Get("/", [&](const Request & /*req*/, Response &res) {
+    res.set_content(html, "text/html");
+  });
+
+  svr.WebSocket(
+      "/ws",
+      [](const Request &req, ws::WebSocket &ws) {
+        std::cout << "WebSocket connected from " << req.remote_addr
+                  << std::endl;
+
+        std::string msg;
+        while (ws.read(msg)) {
+          std::cout << "Received: " << msg << std::endl;
+          ws.send("echo: " + msg);
+        }
+
+        std::cout << "WebSocket disconnected" << std::endl;
+      },
+      [](const std::vector &protocols) -> std::string {
+        for (const auto &p : protocols) {
+          if (p == "echo" || p == "chat") { return p; }
+        }
+        return "";
+      });
+
+  std::cout << "Listening on http://localhost:8080" << std::endl;
+  svr.listen("localhost", 8080);
+}
diff --git a/httplib.h b/httplib.h
index a35a1ae2e5..06cb9878e2 100644
--- a/httplib.h
+++ b/httplib.h
@@ -185,6 +185,14 @@
                       : 0))
 #endif
 
+#ifndef CPPHTTPLIB_THREAD_POOL_MAX_COUNT
+#define CPPHTTPLIB_THREAD_POOL_MAX_COUNT (CPPHTTPLIB_THREAD_POOL_COUNT * 4)
+#endif
+
+#ifndef CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT
+#define CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT 3 // seconds
+#endif
+
 #ifndef CPPHTTPLIB_RECV_FLAGS
 #define CPPHTTPLIB_RECV_FLAGS 0
 #endif
@@ -201,6 +209,22 @@
 #define CPPHTTPLIB_MAX_LINE_LENGTH 32768
 #endif
 
+#ifndef CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH
+#define CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH 16777216
+#endif
+
+#ifndef CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND
+#define CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND 300
+#endif
+
+#ifndef CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND
+#define CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND 5
+#endif
+
+#ifndef CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND
+#define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 30
+#endif
+
 /*
  * Headers
  */
@@ -440,6 +464,10 @@ using socket_t = int;
  */
 namespace httplib {
 
+namespace ws {
+class WebSocket;
+} // namespace ws
+
 namespace detail {
 
 /*
@@ -1124,6 +1152,11 @@ class Stream {
 
   virtual time_t duration() const = 0;
 
+  virtual void set_read_timeout(time_t sec, time_t usec = 0) {
+    (void)sec;
+    (void)usec;
+  }
+
   ssize_t write(const char *ptr);
   ssize_t write(const std::string &s);
 
@@ -1146,7 +1179,7 @@ class TaskQueue {
 
 class ThreadPool final : public TaskQueue {
 public:
-  explicit ThreadPool(size_t n, size_t mqr = 0);
+  explicit ThreadPool(size_t n, size_t max_n = 0, size_t mqr = 0);
   ThreadPool(const ThreadPool &) = delete;
   ~ThreadPool() override = default;
 
@@ -1154,20 +1187,22 @@ class ThreadPool final : public TaskQueue {
   void shutdown() override;
 
 private:
-  struct worker {
-    explicit worker(ThreadPool &pool);
+  void worker(bool is_dynamic);
+  void move_to_finished(std::thread::id id);
+  void cleanup_finished_threads();
 
-    void operator()();
+  size_t base_thread_count_;
+  size_t max_thread_count_;
+  size_t max_queued_requests_;
+  size_t idle_thread_count_;
 
-    ThreadPool &pool_;
-  };
-  friend struct worker;
+  bool shutdown_;
 
-  std::vector threads_;
   std::list> jobs_;
-
-  bool shutdown_;
-  size_t max_queued_requests_ = 0;
+  std::vector threads_;       // base threads
+  std::list dynamic_threads_; // dynamic threads
+  std::vector
+      finished_threads_; // exited dynamic threads awaiting join
 
   std::condition_variable cond_;
   std::mutex mutex_;
@@ -1294,6 +1329,11 @@ class Server {
   using Expect100ContinueHandler =
       std::function;
 
+  using WebSocketHandler =
+      std::function;
+  using SubProtocolSelector =
+      std::function &protocols)>;
+
   Server();
 
   virtual ~Server();
@@ -1311,6 +1351,10 @@ class Server {
   Server &Delete(const std::string &pattern, HandlerWithContentReader handler);
   Server &Options(const std::string &pattern, Handler handler);
 
+  Server &WebSocket(const std::string &pattern, WebSocketHandler handler);
+  Server &WebSocket(const std::string &pattern, WebSocketHandler handler,
+                    SubProtocolSelector sub_protocol_selector);
+
   bool set_base_dir(const std::string &dir,
                     const std::string &mount_point = std::string());
   bool set_mount_point(const std::string &mount_point, const std::string &dir,
@@ -1386,7 +1430,8 @@ class Server {
                        int remote_port, const std::string &local_addr,
                        int local_port, bool close_connection,
                        bool &connection_closed,
-                       const std::function &setup_request);
+                       const std::function &setup_request,
+                       bool *websocket_upgraded = nullptr);
 
   std::atomic svr_sock_{INVALID_SOCKET};
 
@@ -1488,6 +1533,14 @@ class Server {
   HandlersForContentReader delete_handlers_for_content_reader_;
   Handlers options_handlers_;
 
+  struct WebSocketHandlerEntry {
+    std::unique_ptr matcher;
+    WebSocketHandler handler;
+    SubProtocolSelector sub_protocol_selector;
+  };
+  using WebSocketHandlers = std::vector;
+  WebSocketHandlers websocket_handlers_;
+
   HandlerWithResponse error_handler_;
   ExceptionHandler exception_handler_;
   HandlerWithResponse pre_routing_handler_;
@@ -3335,6 +3388,143 @@ class SSEClient {
 
 } // namespace sse
 
+namespace ws {
+
+enum class Opcode : uint8_t {
+  Continuation = 0x0,
+  Text = 0x1,
+  Binary = 0x2,
+  Close = 0x8,
+  Ping = 0x9,
+  Pong = 0xA,
+};
+
+enum class CloseStatus : uint16_t {
+  Normal = 1000,
+  GoingAway = 1001,
+  ProtocolError = 1002,
+  UnsupportedData = 1003,
+  NoStatus = 1005,
+  Abnormal = 1006,
+  InvalidPayload = 1007,
+  PolicyViolation = 1008,
+  MessageTooBig = 1009,
+  MandatoryExtension = 1010,
+  InternalError = 1011,
+};
+
+enum ReadResult : int { Fail = 0, Text = 1, Binary = 2 };
+
+class WebSocket {
+public:
+  WebSocket(const WebSocket &) = delete;
+  WebSocket &operator=(const WebSocket &) = delete;
+  ~WebSocket();
+
+  ReadResult read(std::string &msg);
+  bool send(const std::string &data);
+  bool send(const char *data, size_t len);
+  void close(CloseStatus status = CloseStatus::Normal,
+             const std::string &reason = "");
+  const Request &request() const;
+  bool is_open() const;
+
+private:
+  friend class httplib::Server;
+  friend class WebSocketClient;
+
+  WebSocket(Stream &strm, const Request &req, bool is_server)
+      : strm_(strm), req_(req), is_server_(is_server) {
+    start_heartbeat();
+  }
+
+  WebSocket(std::unique_ptr &&owned_strm, const Request &req,
+            bool is_server)
+      : strm_(*owned_strm), owned_strm_(std::move(owned_strm)), req_(req),
+        is_server_(is_server) {
+    start_heartbeat();
+  }
+
+  void start_heartbeat();
+  bool send_frame(Opcode op, const char *data, size_t len, bool fin = true);
+
+  Stream &strm_;
+  std::unique_ptr owned_strm_;
+  Request req_;
+  bool is_server_;
+  std::atomic closed_{false};
+  std::mutex write_mutex_;
+  std::thread ping_thread_;
+  std::mutex ping_mutex_;
+  std::condition_variable ping_cv_;
+};
+
+class WebSocketClient {
+public:
+  explicit WebSocketClient(const std::string &scheme_host_port_path,
+                           const Headers &headers = {});
+
+  ~WebSocketClient();
+  WebSocketClient(const WebSocketClient &) = delete;
+  WebSocketClient &operator=(const WebSocketClient &) = delete;
+
+  bool is_valid() const;
+
+  bool connect();
+  ReadResult read(std::string &msg);
+  bool send(const std::string &data);
+  bool send(const char *data, size_t len);
+  void close(CloseStatus status = CloseStatus::Normal,
+             const std::string &reason = "");
+  bool is_open() const;
+  const std::string &subprotocol() const;
+  void set_read_timeout(time_t sec, time_t usec = 0);
+  void set_write_timeout(time_t sec, time_t usec = 0);
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+  void set_ca_cert_path(const std::string &path);
+  void set_ca_cert_store(tls::ca_store_t store);
+  void enable_server_certificate_verification(bool enabled);
+#endif
+
+private:
+  void shutdown_and_close();
+  bool create_stream(std::unique_ptr &strm);
+
+  std::string host_;
+  int port_;
+  std::string path_;
+  Headers headers_;
+  std::string subprotocol_;
+  bool is_valid_ = false;
+  socket_t sock_ = INVALID_SOCKET;
+  std::unique_ptr ws_;
+  time_t read_timeout_sec_ = CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND;
+  time_t read_timeout_usec_ = 0;
+  time_t write_timeout_sec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_SECOND;
+  time_t write_timeout_usec_ = CPPHTTPLIB_CLIENT_WRITE_TIMEOUT_USECOND;
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+  bool is_ssl_ = false;
+  tls::ctx_t tls_ctx_ = nullptr;
+  tls::session_t tls_session_ = nullptr;
+  std::string ca_cert_file_path_;
+  tls::ca_store_t ca_cert_store_ = nullptr;
+  bool server_certificate_verification_ = true;
+#endif
+};
+
+namespace impl {
+
+bool is_valid_utf8(const std::string &s);
+
+bool read_websocket_frame(Stream &strm, Opcode &opcode, std::string &payload,
+                          bool &fin, bool expect_masked, size_t max_len);
+
+} // namespace impl
+
+} // namespace ws
+
 // ----------------------------------------------------------------------------
 
 /*
@@ -3966,6 +4156,56 @@ inline size_t to_utf8(int code, char *buff) {
   return 0;
 }
 
+} // namespace detail
+
+namespace ws {
+namespace impl {
+
+inline bool is_valid_utf8(const std::string &s) {
+  size_t i = 0;
+  auto n = s.size();
+  while (i < n) {
+    auto c = static_cast(s[i]);
+    size_t len;
+    uint32_t cp;
+    if (c < 0x80) {
+      i++;
+      continue;
+    } else if ((c & 0xE0) == 0xC0) {
+      len = 2;
+      cp = c & 0x1F;
+    } else if ((c & 0xF0) == 0xE0) {
+      len = 3;
+      cp = c & 0x0F;
+    } else if ((c & 0xF8) == 0xF0) {
+      len = 4;
+      cp = c & 0x07;
+    } else {
+      return false;
+    }
+    if (i + len > n) { return false; }
+    for (size_t j = 1; j < len; j++) {
+      auto b = static_cast(s[i + j]);
+      if ((b & 0xC0) != 0x80) { return false; }
+      cp = (cp << 6) | (b & 0x3F);
+    }
+    // Overlong encoding check
+    if (len == 2 && cp < 0x80) { return false; }
+    if (len == 3 && cp < 0x800) { return false; }
+    if (len == 4 && cp < 0x10000) { return false; }
+    // Surrogate halves (U+D800..U+DFFF) and beyond U+10FFFF are invalid
+    if (cp >= 0xD800 && cp <= 0xDFFF) { return false; }
+    if (cp > 0x10FFFF) { return false; }
+    i += len;
+  }
+  return true;
+}
+
+} // namespace impl
+} // namespace ws
+
+namespace detail {
+
 // NOTE: This code came up with the following stackoverflow post:
 // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
 inline std::string base64_encode(const std::string &in) {
@@ -3996,6 +4236,281 @@ inline std::string base64_encode(const std::string &in) {
   return out;
 }
 
+inline std::string sha1(const std::string &input) {
+  // RFC 3174 SHA-1 implementation
+  auto left_rotate = [](uint32_t x, uint32_t n) -> uint32_t {
+    return (x << n) | (x >> (32 - n));
+  };
+
+  uint32_t h0 = 0x67452301;
+  uint32_t h1 = 0xEFCDAB89;
+  uint32_t h2 = 0x98BADCFE;
+  uint32_t h3 = 0x10325476;
+  uint32_t h4 = 0xC3D2E1F0;
+
+  // Pre-processing: adding padding bits
+  std::string msg = input;
+  uint64_t original_bit_len = static_cast(msg.size()) * 8;
+  msg.push_back(static_cast(0x80));
+  while (msg.size() % 64 != 56) {
+    msg.push_back(0);
+  }
+
+  // Append original length in bits as 64-bit big-endian
+  for (int i = 56; i >= 0; i -= 8) {
+    msg.push_back(static_cast((original_bit_len >> i) & 0xFF));
+  }
+
+  // Process each 512-bit chunk
+  for (size_t offset = 0; offset < msg.size(); offset += 64) {
+    uint32_t w[80];
+
+    for (size_t i = 0; i < 16; i++) {
+      w[i] =
+          (static_cast(static_cast(msg[offset + i * 4]))
+           << 24) |
+          (static_cast(static_cast(msg[offset + i * 4 + 1]))
+           << 16) |
+          (static_cast(static_cast(msg[offset + i * 4 + 2]))
+           << 8) |
+          (static_cast(
+              static_cast(msg[offset + i * 4 + 3])));
+    }
+
+    for (int i = 16; i < 80; i++) {
+      w[i] = left_rotate(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
+    }
+
+    uint32_t a = h0, b = h1, c = h2, d = h3, e = h4;
+
+    for (int i = 0; i < 80; i++) {
+      uint32_t f, k;
+      if (i < 20) {
+        f = (b & c) | ((~b) & d);
+        k = 0x5A827999;
+      } else if (i < 40) {
+        f = b ^ c ^ d;
+        k = 0x6ED9EBA1;
+      } else if (i < 60) {
+        f = (b & c) | (b & d) | (c & d);
+        k = 0x8F1BBCDC;
+      } else {
+        f = b ^ c ^ d;
+        k = 0xCA62C1D6;
+      }
+
+      uint32_t temp = left_rotate(a, 5) + f + e + k + w[i];
+      e = d;
+      d = c;
+      c = left_rotate(b, 30);
+      b = a;
+      a = temp;
+    }
+
+    h0 += a;
+    h1 += b;
+    h2 += c;
+    h3 += d;
+    h4 += e;
+  }
+
+  // Produce the final hash as a 20-byte binary string
+  std::string hash(20, '\0');
+  for (size_t i = 0; i < 4; i++) {
+    hash[i] = static_cast((h0 >> (24 - i * 8)) & 0xFF);
+    hash[4 + i] = static_cast((h1 >> (24 - i * 8)) & 0xFF);
+    hash[8 + i] = static_cast((h2 >> (24 - i * 8)) & 0xFF);
+    hash[12 + i] = static_cast((h3 >> (24 - i * 8)) & 0xFF);
+    hash[16 + i] = static_cast((h4 >> (24 - i * 8)) & 0xFF);
+  }
+  return hash;
+}
+
+inline std::string websocket_accept_key(const std::string &client_key) {
+  const std::string magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
+  return base64_encode(sha1(client_key + magic));
+}
+
+inline bool is_websocket_upgrade(const Request &req) {
+  if (req.method != "GET") { return false; }
+
+  // Check Upgrade: websocket (case-insensitive)
+  auto upgrade_it = req.headers.find("Upgrade");
+  if (upgrade_it == req.headers.end()) { return false; }
+  auto upgrade_val = upgrade_it->second;
+  std::transform(upgrade_val.begin(), upgrade_val.end(), upgrade_val.begin(),
+                 ::tolower);
+  if (upgrade_val != "websocket") { return false; }
+
+  // Check Connection header contains "Upgrade"
+  auto connection_it = req.headers.find("Connection");
+  if (connection_it == req.headers.end()) { return false; }
+  auto connection_val = connection_it->second;
+  std::transform(connection_val.begin(), connection_val.end(),
+                 connection_val.begin(), ::tolower);
+  if (connection_val.find("upgrade") == std::string::npos) { return false; }
+
+  // Check Sec-WebSocket-Key is a valid base64-encoded 16-byte value (24 chars)
+  // RFC 6455 Section 4.2.1
+  auto ws_key = req.get_header_value("Sec-WebSocket-Key");
+  if (ws_key.size() != 24 || ws_key[22] != '=' || ws_key[23] != '=') {
+    return false;
+  }
+  static const std::string b64chars =
+      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+  for (size_t i = 0; i < 22; i++) {
+    if (b64chars.find(ws_key[i]) == std::string::npos) { return false; }
+  }
+
+  // Check Sec-WebSocket-Version: 13
+  auto version = req.get_header_value("Sec-WebSocket-Version");
+  if (version != "13") { return false; }
+
+  return true;
+}
+
+inline bool write_websocket_frame(Stream &strm, ws::Opcode opcode,
+                                  const char *data, size_t len, bool fin,
+                                  bool mask) {
+  // First byte: FIN + opcode
+  uint8_t header[2];
+  header[0] = static_cast((fin ? 0x80 : 0x00) |
+                                   (static_cast(opcode) & 0x0F));
+
+  // Second byte: MASK + payload length
+  if (len < 126) {
+    header[1] = static_cast(len);
+    if (mask) { header[1] |= 0x80; }
+    if (strm.write(reinterpret_cast(header), 2) < 0) { return false; }
+  } else if (len <= 0xFFFF) {
+    header[1] = 126;
+    if (mask) { header[1] |= 0x80; }
+    if (strm.write(reinterpret_cast(header), 2) < 0) { return false; }
+    uint8_t ext[2];
+    ext[0] = static_cast((len >> 8) & 0xFF);
+    ext[1] = static_cast(len & 0xFF);
+    if (strm.write(reinterpret_cast(ext), 2) < 0) { return false; }
+  } else {
+    header[1] = 127;
+    if (mask) { header[1] |= 0x80; }
+    if (strm.write(reinterpret_cast(header), 2) < 0) { return false; }
+    uint8_t ext[8];
+    for (int i = 7; i >= 0; i--) {
+      ext[7 - i] = static_cast((len >> (i * 8)) & 0xFF);
+    }
+    if (strm.write(reinterpret_cast(ext), 8) < 0) { return false; }
+  }
+
+  if (mask) {
+    // Generate random mask key
+    thread_local std::mt19937 rng(std::random_device{}());
+    uint8_t mask_key[4];
+    auto r = rng();
+    std::memcpy(mask_key, &r, 4);
+    if (strm.write(reinterpret_cast(mask_key), 4) < 0) { return false; }
+
+    // Write masked payload in chunks
+    const size_t chunk_size = 4096;
+    std::vector buf((std::min)(len, chunk_size));
+    for (size_t offset = 0; offset < len; offset += chunk_size) {
+      size_t n = (std::min)(chunk_size, len - offset);
+      for (size_t i = 0; i < n; i++) {
+        buf[i] =
+            data[offset + i] ^ static_cast(mask_key[(offset + i) % 4]);
+      }
+      if (strm.write(buf.data(), n) < 0) { return false; }
+    }
+  } else {
+    if (len > 0) {
+      if (strm.write(data, len) < 0) { return false; }
+    }
+  }
+
+  return true;
+}
+
+} // namespace detail
+
+namespace ws {
+namespace impl {
+
+inline bool read_websocket_frame(Stream &strm, Opcode &opcode,
+                                 std::string &payload, bool &fin,
+                                 bool expect_masked, size_t max_len) {
+  // Read first 2 bytes
+  uint8_t header[2];
+  if (strm.read(reinterpret_cast(header), 2) != 2) { return false; }
+
+  fin = (header[0] & 0x80) != 0;
+
+  // RSV1, RSV2, RSV3 must be 0 when no extension is negotiated
+  if (header[0] & 0x70) { return false; }
+
+  opcode = static_cast(header[0] & 0x0F);
+  bool masked = (header[1] & 0x80) != 0;
+  uint64_t payload_len = header[1] & 0x7F;
+
+  // RFC 6455 Section 5.5: control frames MUST NOT be fragmented and
+  // MUST have a payload length of 125 bytes or less
+  bool is_control = (static_cast(opcode) & 0x08) != 0;
+  if (is_control) {
+    if (!fin) { return false; }
+    if (payload_len > 125) { return false; }
+  }
+
+  if (masked != expect_masked) { return false; }
+
+  // Extended payload length
+  if (payload_len == 126) {
+    uint8_t ext[2];
+    if (strm.read(reinterpret_cast(ext), 2) != 2) { return false; }
+    payload_len = (static_cast(ext[0]) << 8) | ext[1];
+  } else if (payload_len == 127) {
+    uint8_t ext[8];
+    if (strm.read(reinterpret_cast(ext), 8) != 8) { return false; }
+    // RFC 6455 Section 5.2: the most significant bit MUST be 0
+    if (ext[0] & 0x80) { return false; }
+    payload_len = 0;
+    for (int i = 0; i < 8; i++) {
+      payload_len = (payload_len << 8) | ext[i];
+    }
+  }
+
+  if (payload_len > max_len) { return false; }
+
+  // Read mask key if present
+  uint8_t mask_key[4] = {0};
+  if (masked) {
+    if (strm.read(reinterpret_cast(mask_key), 4) != 4) { return false; }
+  }
+
+  // Read payload
+  payload.resize(static_cast(payload_len));
+  if (payload_len > 0) {
+    size_t total_read = 0;
+    while (total_read < payload_len) {
+      auto n = strm.read(&payload[total_read],
+                         static_cast(payload_len - total_read));
+      if (n <= 0) { return false; }
+      total_read += static_cast(n);
+    }
+  }
+
+  // Unmask if needed
+  if (masked) {
+    for (size_t i = 0; i < payload.size(); i++) {
+      payload[i] ^= static_cast(mask_key[i % 4]);
+    }
+  }
+
+  return true;
+}
+
+} // namespace impl
+} // namespace ws
+
+namespace detail {
+
 inline bool is_valid_path(const std::string &path) {
   size_t level = 0;
   size_t i = 0;
@@ -4675,6 +5190,7 @@ class SocketStream final : public Stream {
   void get_local_ip_and_port(std::string &ip, int &port) const override;
   socket_t socket() const override;
   time_t duration() const override;
+  void set_read_timeout(time_t sec, time_t usec = 0) override;
 
 private:
   socket_t sock_;
@@ -5989,6 +6505,50 @@ inline bool read_headers(Stream &strm, Headers &headers) {
   return true;
 }
 
+inline bool read_websocket_upgrade_response(Stream &strm,
+                                            const std::string &expected_accept,
+                                            std::string &selected_subprotocol) {
+  // Read status line
+  const auto bufsiz = 2048;
+  char buf[bufsiz];
+  stream_line_reader line_reader(strm, buf, bufsiz);
+  if (!line_reader.getline()) { return false; }
+
+  // Check for "HTTP/1.1 101"
+  auto line = std::string(line_reader.ptr(), line_reader.size());
+  if (line.find("HTTP/1.1 101") == std::string::npos) { return false; }
+
+  // Parse headers using existing read_headers
+  Headers headers;
+  if (!read_headers(strm, headers)) { return false; }
+
+  // Verify Upgrade: websocket (case-insensitive)
+  auto upgrade_it = headers.find("Upgrade");
+  if (upgrade_it == headers.end()) { return false; }
+  auto upgrade_val = upgrade_it->second;
+  std::transform(upgrade_val.begin(), upgrade_val.end(), upgrade_val.begin(),
+                 ::tolower);
+  if (upgrade_val != "websocket") { return false; }
+
+  // Verify Connection header contains "Upgrade" (case-insensitive)
+  auto connection_it = headers.find("Connection");
+  if (connection_it == headers.end()) { return false; }
+  auto connection_val = connection_it->second;
+  std::transform(connection_val.begin(), connection_val.end(),
+                 connection_val.begin(), ::tolower);
+  if (connection_val.find("upgrade") == std::string::npos) { return false; }
+
+  // Verify Sec-WebSocket-Accept header value
+  auto it = headers.find("Sec-WebSocket-Accept");
+  if (it == headers.end() || it->second != expected_accept) { return false; }
+
+  // Extract negotiated subprotocol
+  auto proto_it = headers.find("Sec-WebSocket-Protocol");
+  if (proto_it != headers.end()) { selected_subprotocol = proto_it->second; }
+
+  return true;
+}
+
 enum class ReadContentResult {
   Success,         // Successfully read the content
   PayloadTooLarge, // The content exceeds the specified payload limit
@@ -7356,15 +7916,6 @@ inline bool expect_content(const Request &req) {
   return false;
 }
 
-inline bool has_crlf(const std::string &s) {
-  auto p = s.c_str();
-  while (*p) {
-    if (*p == '\r' || *p == '\n') { return true; }
-    p++;
-  }
-  return false;
-}
-
 #ifdef _WIN32
 class WSInit {
 public:
@@ -7484,6 +8035,52 @@ inline bool is_field_content(const std::string &s) {
 inline bool is_field_value(const std::string &s) { return is_field_content(s); }
 
 } // namespace fields
+
+inline bool perform_websocket_handshake(Stream &strm, const std::string &host,
+                                        int port, const std::string &path,
+                                        const Headers &headers,
+                                        std::string &selected_subprotocol) {
+  // Validate path and host
+  if (!fields::is_field_value(path) || !fields::is_field_value(host)) {
+    return false;
+  }
+
+  // Validate user-provided headers
+  for (const auto &h : headers) {
+    if (!fields::is_field_name(h.first) || !fields::is_field_value(h.second)) {
+      return false;
+    }
+  }
+
+  // Generate random Sec-WebSocket-Key
+  thread_local std::mt19937 rng(std::random_device{}());
+  std::string key_bytes(16, '\0');
+  for (size_t i = 0; i < 16; i += 4) {
+    auto r = rng();
+    std::memcpy(&key_bytes[i], &r, (std::min)(size_t(4), size_t(16 - i)));
+  }
+  auto client_key = base64_encode(key_bytes);
+
+  // Build upgrade request
+  std::string req_str = "GET " + path + " HTTP/1.1\r\n";
+  req_str += "Host: " + host + ":" + std::to_string(port) + "\r\n";
+  req_str += "Upgrade: websocket\r\n";
+  req_str += "Connection: Upgrade\r\n";
+  req_str += "Sec-WebSocket-Key: " + client_key + "\r\n";
+  req_str += "Sec-WebSocket-Version: 13\r\n";
+  for (const auto &h : headers) {
+    req_str += h.first + ": " + h.second + "\r\n";
+  }
+  req_str += "\r\n";
+
+  if (strm.write(req_str.data(), req_str.size()) < 0) { return false; }
+
+  // Verify 101 response and Sec-WebSocket-Accept header
+  auto expected_accept = websocket_accept_key(client_key);
+  return read_websocket_upgrade_response(strm, expected_accept,
+                                         selected_subprotocol);
+}
+
 } // namespace detail
 
 /*
@@ -7512,6 +8109,7 @@ class SSLSocketStream final : public Stream {
   void get_local_ip_and_port(std::string &ip, int &port) const override;
   socket_t socket() const override;
   time_t duration() const override;
+  void set_read_timeout(time_t sec, time_t usec = 0) override;
 
 private:
   socket_t sock_;
@@ -7846,6 +8444,53 @@ inline bool verify_cert_with_windows_schannel(
 }
 #endif // _WIN32
 
+inline bool setup_client_tls_session(const std::string &host, tls::ctx_t &ctx,
+                                     tls::session_t &session, socket_t sock,
+                                     bool server_certificate_verification,
+                                     const std::string &ca_cert_file_path,
+                                     tls::ca_store_t ca_cert_store,
+                                     time_t timeout_sec, time_t timeout_usec) {
+  using namespace tls;
+
+  ctx = create_client_context();
+  if (!ctx) { return false; }
+
+  if (server_certificate_verification) {
+    if (!ca_cert_file_path.empty()) {
+      load_ca_file(ctx, ca_cert_file_path.c_str());
+    }
+    if (ca_cert_store) { set_ca_store(ctx, ca_cert_store); }
+    load_system_certs(ctx);
+  }
+
+  bool is_ip = is_ip_address(host);
+
+#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT
+  if (is_ip && server_certificate_verification) {
+    set_verify_client(ctx, false);
+  } else {
+    set_verify_client(ctx, server_certificate_verification);
+  }
+#endif
+
+  session = create_session(ctx, sock);
+  if (!session) { return false; }
+
+  // RFC 6066: SNI must not be set for IP addresses
+  if (!is_ip) { set_sni(session, host.c_str()); }
+  if (server_certificate_verification) { set_hostname(session, host.c_str()); }
+
+  if (!connect_nonblocking(session, sock, timeout_sec, timeout_usec, nullptr)) {
+    return false;
+  }
+
+  if (server_certificate_verification) {
+    if (get_verify_result(session) != 0) { return false; }
+  }
+
+  return true;
+}
+
 } // namespace detail
 #endif // CPPHTTPLIB_SSL_ENABLED
 
@@ -8663,22 +9308,37 @@ inline ssize_t detail::BodyReader::read(char *buf, size_t len) {
 }
 
 // ThreadPool implementation
-inline ThreadPool::ThreadPool(size_t n, size_t mqr)
-    : shutdown_(false), max_queued_requests_(mqr) {
-  threads_.reserve(n);
-  while (n) {
-    threads_.emplace_back(worker(*this));
-    n--;
+inline ThreadPool::ThreadPool(size_t n, size_t max_n, size_t mqr)
+    : base_thread_count_(n), max_queued_requests_(mqr), idle_thread_count_(0),
+      shutdown_(false) {
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
+  if (max_n != 0 && max_n < n) {
+    std::string msg = "max_threads must be >= base_threads";
+    throw std::invalid_argument(msg);
+  }
+#endif
+  max_thread_count_ = max_n == 0 ? n : max_n;
+  threads_.reserve(base_thread_count_);
+  for (size_t i = 0; i < base_thread_count_; i++) {
+    threads_.emplace_back(std::thread([this]() { worker(false); }));
   }
 }
 
 inline bool ThreadPool::enqueue(std::function fn) {
   {
     std::unique_lock lock(mutex_);
+    if (shutdown_) { return false; }
     if (max_queued_requests_ > 0 && jobs_.size() >= max_queued_requests_) {
       return false;
     }
     jobs_.push_back(std::move(fn));
+
+    // Spawn a dynamic thread if no idle threads and under max
+    if (idle_thread_count_ == 0 &&
+        threads_.size() + dynamic_threads_.size() < max_thread_count_) {
+      cleanup_finished_threads();
+      dynamic_threads_.emplace_back(std::thread([this]() { worker(true); }));
+    }
   }
 
   cond_.notify_one();
@@ -8686,7 +9346,6 @@ inline bool ThreadPool::enqueue(std::function fn) {
 }
 
 inline void ThreadPool::shutdown() {
-  // Stop all worker threads...
   {
     std::unique_lock lock(mutex_);
     shutdown_ = true;
@@ -8694,31 +9353,84 @@ inline void ThreadPool::shutdown() {
 
   cond_.notify_all();
 
-  // Join...
   for (auto &t : threads_) {
-    t.join();
+    if (t.joinable()) { t.join(); }
+  }
+
+  // Move dynamic_threads_ to a local list under the lock to avoid racing
+  // with worker threads that call move_to_finished() concurrently.
+  std::list remaining_dynamic;
+  {
+    std::unique_lock lock(mutex_);
+    remaining_dynamic = std::move(dynamic_threads_);
+  }
+  for (auto &t : remaining_dynamic) {
+    if (t.joinable()) { t.join(); }
+  }
+
+  std::unique_lock lock(mutex_);
+  cleanup_finished_threads();
+}
+
+inline void ThreadPool::move_to_finished(std::thread::id id) {
+  // Must be called with mutex_ held
+  for (auto it = dynamic_threads_.begin(); it != dynamic_threads_.end(); ++it) {
+    if (it->get_id() == id) {
+      finished_threads_.push_back(std::move(*it));
+      dynamic_threads_.erase(it);
+      return;
+    }
   }
 }
 
-inline ThreadPool::worker::worker(ThreadPool &pool) : pool_(pool) {}
+inline void ThreadPool::cleanup_finished_threads() {
+  // Must be called with mutex_ held
+  for (auto &t : finished_threads_) {
+    if (t.joinable()) { t.join(); }
+  }
+  finished_threads_.clear();
+}
 
-inline void ThreadPool::worker::operator()() {
+inline void ThreadPool::worker(bool is_dynamic) {
   for (;;) {
     std::function fn;
     {
-      std::unique_lock lock(pool_.mutex_);
+      std::unique_lock lock(mutex_);
+      idle_thread_count_++;
+
+      if (is_dynamic) {
+        auto has_work = cond_.wait_for(
+            lock, std::chrono::seconds(CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT),
+            [&] { return !jobs_.empty() || shutdown_; });
+        if (!has_work) {
+          // Timed out with no work - exit this dynamic thread
+          idle_thread_count_--;
+          move_to_finished(std::this_thread::get_id());
+          break;
+        }
+      } else {
+        cond_.wait(lock, [&] { return !jobs_.empty() || shutdown_; });
+      }
 
-      pool_.cond_.wait(lock,
-                       [&] { return !pool_.jobs_.empty() || pool_.shutdown_; });
+      idle_thread_count_--;
 
-      if (pool_.shutdown_ && pool_.jobs_.empty()) { break; }
+      if (shutdown_ && jobs_.empty()) { break; }
 
-      fn = pool_.jobs_.front();
-      pool_.jobs_.pop_front();
+      fn = std::move(jobs_.front());
+      jobs_.pop_front();
     }
 
     assert(true == static_cast(fn));
     fn();
+
+    // Dynamic thread: exit if queue is empty after task completion
+    if (is_dynamic) {
+      std::unique_lock lock(mutex_);
+      if (jobs_.empty()) {
+        move_to_finished(std::this_thread::get_id());
+        break;
+      }
+    }
   }
 
 #if defined(CPPHTTPLIB_OPENSSL_SUPPORT) && !defined(OPENSSL_IS_BORINGSSL) &&   \
@@ -8876,6 +9588,11 @@ inline time_t SocketStream::duration() const {
       .count();
 }
 
+inline void SocketStream::set_read_timeout(time_t sec, time_t usec) {
+  read_timeout_sec_ = sec;
+  read_timeout_usec_ = usec;
+}
+
 // Buffer stream implementation
 inline bool BufferStream::is_readable() const { return true; }
 
@@ -9201,6 +9918,11 @@ inline time_t SSLSocketStream::duration() const {
       .count();
 }
 
+inline void SSLSocketStream::set_read_timeout(time_t sec, time_t usec) {
+  read_timeout_sec_ = sec;
+  read_timeout_usec_ = usec;
+}
+
 } // namespace detail
 #endif // CPPHTTPLIB_SSL_ENABLED
 
@@ -9210,8 +9932,10 @@ inline time_t SSLSocketStream::duration() const {
 
 // HTTP server implementation
 inline Server::Server()
-    : new_task_queue(
-          [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }) {
+    : new_task_queue([] {
+        return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT,
+                              CPPHTTPLIB_THREAD_POOL_MAX_COUNT);
+      }) {
 #ifndef _WIN32
   signal(SIGPIPE, SIG_IGN);
 #endif
@@ -9286,6 +10010,21 @@ inline Server &Server::Options(const std::string &pattern, Handler handler) {
   return *this;
 }
 
+inline Server &Server::WebSocket(const std::string &pattern,
+                                 WebSocketHandler handler) {
+  websocket_handlers_.push_back(
+      {make_matcher(pattern), std::move(handler), nullptr});
+  return *this;
+}
+
+inline Server &Server::WebSocket(const std::string &pattern,
+                                 WebSocketHandler handler,
+                                 SubProtocolSelector sub_protocol_selector) {
+  websocket_handlers_.push_back({make_matcher(pattern), std::move(handler),
+                                 std::move(sub_protocol_selector)});
+  return *this;
+}
+
 inline bool Server::set_base_dir(const std::string &dir,
                                  const std::string &mount_point) {
   return set_mount_point(mount_point, dir);
@@ -10408,7 +11147,8 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
                         int remote_port, const std::string &local_addr,
                         int local_port, bool close_connection,
                         bool &connection_closed,
-                        const std::function &setup_request) {
+                        const std::function &setup_request,
+                        bool *websocket_upgraded) {
   std::array buf{};
 
   detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
@@ -10511,6 +11251,77 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
     return !detail::is_socket_alive(sock);
   };
 
+  // WebSocket upgrade
+  // Check pre_routing_handler_ before upgrading so that authentication
+  // and other middleware can reject the request with an HTTP response
+  // (e.g., 401) before the protocol switches.
+  if (detail::is_websocket_upgrade(req)) {
+    if (pre_routing_handler_ &&
+        pre_routing_handler_(req, res) == HandlerResponse::Handled) {
+      if (res.status == -1) { res.status = StatusCode::OK_200; }
+      return write_response(strm, close_connection, req, res);
+    }
+    // Find matching WebSocket handler
+    for (const auto &entry : websocket_handlers_) {
+      if (entry.matcher->match(req)) {
+        // Compute accept key
+        auto client_key = req.get_header_value("Sec-WebSocket-Key");
+        auto accept_key = detail::websocket_accept_key(client_key);
+
+        // Negotiate subprotocol
+        std::string selected_subprotocol;
+        if (entry.sub_protocol_selector) {
+          auto protocol_header = req.get_header_value("Sec-WebSocket-Protocol");
+          if (!protocol_header.empty()) {
+            std::vector protocols;
+            std::istringstream iss(protocol_header);
+            std::string token;
+            while (std::getline(iss, token, ',')) {
+              // Trim whitespace
+              auto start = token.find_first_not_of(' ');
+              auto end = token.find_last_not_of(' ');
+              if (start != std::string::npos) {
+                protocols.push_back(token.substr(start, end - start + 1));
+              }
+            }
+            selected_subprotocol = entry.sub_protocol_selector(protocols);
+          }
+        }
+
+        // Send 101 Switching Protocols
+        std::string handshake_response = "HTTP/1.1 101 Switching Protocols\r\n"
+                                         "Upgrade: websocket\r\n"
+                                         "Connection: Upgrade\r\n"
+                                         "Sec-WebSocket-Accept: " +
+                                         accept_key + "\r\n";
+        if (!selected_subprotocol.empty()) {
+          if (!detail::fields::is_field_value(selected_subprotocol)) {
+            return false;
+          }
+          handshake_response +=
+              "Sec-WebSocket-Protocol: " + selected_subprotocol + "\r\n";
+        }
+        handshake_response += "\r\n";
+        if (strm.write(handshake_response.data(), handshake_response.size()) <
+            0) {
+          return false;
+        }
+
+        connection_closed = true;
+        if (websocket_upgraded) { *websocket_upgraded = true; }
+
+        {
+          // Use WebSocket-specific read timeout instead of HTTP timeout
+          strm.set_read_timeout(CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND, 0);
+          ws::WebSocket ws(strm, req, true);
+          entry.handler(req, ws);
+        }
+        return true;
+      }
+    }
+    // No matching handler - fall through to 404
+  }
+
   // Routing
   auto routed = false;
 #ifdef CPPHTTPLIB_NO_EXCEPTIONS
@@ -10607,6 +11418,7 @@ inline bool Server::process_and_close_socket(socket_t sock) {
   int local_port = 0;
   detail::get_local_ip_and_port(sock, local_addr, local_port);
 
+  bool websocket_upgraded = false;
   auto ret = detail::process_server_socket(
       svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
       read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
@@ -10614,7 +11426,7 @@ inline bool Server::process_and_close_socket(socket_t sock) {
       [&](Stream &strm, bool close_connection, bool &connection_closed) {
         return process_request(strm, remote_addr, remote_port, local_addr,
                                local_port, close_connection, connection_closed,
-                               nullptr);
+                               nullptr, &websocket_upgraded);
       });
 
   detail::shutdown_socket(sock);
@@ -13915,9 +14727,9 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
   // Use scope_exit to ensure cleanup on all paths (including exceptions)
   bool handshake_done = false;
   bool ret = false;
+  bool websocket_upgraded = false;
   auto cleanup = detail::scope_exit([&] {
-    // Shutdown gracefully if handshake succeeded and processing was successful
-    if (handshake_done) { shutdown(session, ret); }
+    if (handshake_done) { shutdown(session, !websocket_upgraded && ret); }
     free_session(session);
     detail::shutdown_socket(sock);
     detail::close_socket(sock);
@@ -13957,9 +14769,10 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
       read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
       write_timeout_usec_,
       [&](Stream &strm, bool close_connection, bool &connection_closed) {
-        return process_request(strm, remote_addr, remote_port, local_addr,
-                               local_port, close_connection, connection_closed,
-                               [&](Request &req) { req.ssl = session; });
+        return process_request(
+            strm, remote_addr, remote_port, local_addr, local_port,
+            close_connection, connection_closed,
+            [&](Request &req) { req.ssl = session; }, &websocket_upgraded);
       });
 
   return ret;
@@ -17213,6 +18026,351 @@ inline std::string verify_error_string(long error_code) {
 
 #endif // CPPHTTPLIB_MBEDTLS_SUPPORT
 
+// WebSocket implementation
+namespace ws {
+
+inline bool WebSocket::send_frame(Opcode op, const char *data, size_t len,
+                                  bool fin) {
+  std::lock_guard lock(write_mutex_);
+  if (closed_) { return false; }
+  return detail::write_websocket_frame(strm_, op, data, len, fin, !is_server_);
+}
+
+inline ReadResult WebSocket::read(std::string &msg) {
+  while (!closed_) {
+    Opcode opcode;
+    std::string payload;
+    bool fin;
+
+    if (!impl::read_websocket_frame(strm_, opcode, payload, fin, is_server_,
+                                    CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH)) {
+      closed_ = true;
+      return Fail;
+    }
+
+    switch (opcode) {
+    case Opcode::Ping: {
+      std::lock_guard lock(write_mutex_);
+      detail::write_websocket_frame(strm_, Opcode::Pong, payload.data(),
+                                    payload.size(), true, !is_server_);
+      continue;
+    }
+    case Opcode::Pong: continue;
+    case Opcode::Close: {
+      if (!closed_.exchange(true)) {
+        // Echo close frame back
+        std::lock_guard lock(write_mutex_);
+        detail::write_websocket_frame(strm_, Opcode::Close, payload.data(),
+                                      payload.size(), true, !is_server_);
+      }
+      return Fail;
+    }
+    case Opcode::Text:
+    case Opcode::Binary: {
+      auto result = opcode == Opcode::Text ? Text : Binary;
+      msg = std::move(payload);
+
+      // Handle fragmentation
+      if (!fin) {
+        while (true) {
+          Opcode cont_opcode;
+          std::string cont_payload;
+          bool cont_fin;
+          if (!impl::read_websocket_frame(
+                  strm_, cont_opcode, cont_payload, cont_fin, is_server_,
+                  CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH)) {
+            closed_ = true;
+            return Fail;
+          }
+          if (cont_opcode == Opcode::Ping) {
+            std::lock_guard lock(write_mutex_);
+            detail::write_websocket_frame(
+                strm_, Opcode::Pong, cont_payload.data(), cont_payload.size(),
+                true, !is_server_);
+            continue;
+          }
+          if (cont_opcode == Opcode::Pong) { continue; }
+          if (cont_opcode == Opcode::Close) {
+            if (!closed_.exchange(true)) {
+              std::lock_guard lock(write_mutex_);
+              detail::write_websocket_frame(
+                  strm_, Opcode::Close, cont_payload.data(),
+                  cont_payload.size(), true, !is_server_);
+            }
+            return Fail;
+          }
+          // RFC 6455: continuation frames must use opcode 0x0
+          if (cont_opcode != Opcode::Continuation) {
+            closed_ = true;
+            return Fail;
+          }
+          msg += cont_payload;
+          if (msg.size() > CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH) {
+            closed_ = true;
+            return Fail;
+          }
+          if (cont_fin) { break; }
+        }
+      }
+      // RFC 6455 Section 5.6: text frames must contain valid UTF-8
+      if (result == Text && !impl::is_valid_utf8(msg)) {
+        close(CloseStatus::InvalidPayload, "invalid UTF-8");
+        return Fail;
+      }
+      return result;
+    }
+    default: closed_ = true; return Fail;
+    }
+  }
+  return Fail;
+}
+
+inline bool WebSocket::send(const std::string &data) {
+  return send_frame(Opcode::Text, data.data(), data.size());
+}
+
+inline bool WebSocket::send(const char *data, size_t len) {
+  return send_frame(Opcode::Binary, data, len);
+}
+
+inline void WebSocket::close(CloseStatus status, const std::string &reason) {
+  if (closed_.exchange(true)) { return; }
+  ping_cv_.notify_all();
+  std::string payload;
+  auto code = static_cast(status);
+  payload.push_back(static_cast((code >> 8) & 0xFF));
+  payload.push_back(static_cast(code & 0xFF));
+  // RFC 6455 Section 5.5: control frame payload must not exceed 125 bytes
+  // Close frame has 2-byte status code, so reason is limited to 123 bytes
+  payload += reason.substr(0, 123);
+  {
+    std::lock_guard lock(write_mutex_);
+    detail::write_websocket_frame(strm_, Opcode::Close, payload.data(),
+                                  payload.size(), true, !is_server_);
+  }
+
+  // RFC 6455 Section 7.1.1: after sending a Close frame, wait for the peer's
+  // Close response before closing the TCP connection. Use a short timeout to
+  // avoid hanging if the peer doesn't respond.
+  strm_.set_read_timeout(CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND, 0);
+  Opcode op;
+  std::string resp;
+  bool fin;
+  while (impl::read_websocket_frame(strm_, op, resp, fin, is_server_, 125)) {
+    if (op == Opcode::Close) { break; }
+  }
+}
+
+inline WebSocket::~WebSocket() {
+  {
+    std::lock_guard lock(ping_mutex_);
+    closed_ = true;
+  }
+  ping_cv_.notify_all();
+  if (ping_thread_.joinable()) { ping_thread_.join(); }
+}
+
+inline void WebSocket::start_heartbeat() {
+  ping_thread_ = std::thread([this]() {
+    std::unique_lock lock(ping_mutex_);
+    while (!closed_) {
+      ping_cv_.wait_for(lock, std::chrono::seconds(
+                                  CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND));
+      if (closed_) { break; }
+      lock.unlock();
+      if (!send_frame(Opcode::Ping, nullptr, 0)) {
+        closed_ = true;
+        break;
+      }
+      lock.lock();
+    }
+  });
+}
+
+inline const Request &WebSocket::request() const { return req_; }
+
+inline bool WebSocket::is_open() const { return !closed_; }
+
+// WebSocketClient implementation
+inline WebSocketClient::WebSocketClient(
+    const std::string &scheme_host_port_path, const Headers &headers)
+    : headers_(headers) {
+  const static std::regex re(
+      R"(([a-z]+):\/\/(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?(\/.*))");
+
+  std::smatch m;
+  if (std::regex_match(scheme_host_port_path, m, re)) {
+    auto scheme = m[1].str();
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+    if (scheme != "ws" && scheme != "wss") {
+#else
+    if (scheme != "ws") {
+#endif
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
+      std::string msg = "'" + scheme + "' scheme is not supported.";
+      throw std::invalid_argument(msg);
+#endif
+      return;
+    }
+
+    auto is_ssl = scheme == "wss";
+
+    host_ = m[2].str();
+    if (host_.empty()) { host_ = m[3].str(); }
+
+    auto port_str = m[4].str();
+    port_ = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80);
+
+    path_ = m[5].str();
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+    is_ssl_ = is_ssl;
+#else
+    if (is_ssl) { return; }
+#endif
+
+    is_valid_ = true;
+  }
+}
+
+inline WebSocketClient::~WebSocketClient() { shutdown_and_close(); }
+
+inline bool WebSocketClient::is_valid() const { return is_valid_; }
+
+inline void WebSocketClient::shutdown_and_close() {
+#ifdef CPPHTTPLIB_SSL_ENABLED
+  if (is_ssl_) {
+    if (tls_session_) {
+      tls::shutdown(tls_session_, true);
+      tls::free_session(tls_session_);
+      tls_session_ = nullptr;
+    }
+    if (tls_ctx_) {
+      tls::free_context(tls_ctx_);
+      tls_ctx_ = nullptr;
+    }
+  }
+#endif
+  if (ws_ && ws_->is_open()) { ws_->close(); }
+  ws_.reset();
+  if (sock_ != INVALID_SOCKET) {
+    detail::shutdown_socket(sock_);
+    detail::close_socket(sock_);
+    sock_ = INVALID_SOCKET;
+  }
+}
+
+inline bool WebSocketClient::create_stream(std::unique_ptr &strm) {
+#ifdef CPPHTTPLIB_SSL_ENABLED
+  if (is_ssl_) {
+    if (!detail::setup_client_tls_session(
+            host_, tls_ctx_, tls_session_, sock_,
+            server_certificate_verification_, ca_cert_file_path_,
+            ca_cert_store_, read_timeout_sec_, read_timeout_usec_)) {
+      return false;
+    }
+
+    strm = std::unique_ptr(new detail::SSLSocketStream(
+        sock_, tls_session_, read_timeout_sec_, read_timeout_usec_,
+        write_timeout_sec_, write_timeout_usec_));
+    return true;
+  }
+#endif
+  strm = std::unique_ptr(
+      new detail::SocketStream(sock_, read_timeout_sec_, read_timeout_usec_,
+                               write_timeout_sec_, write_timeout_usec_));
+  return true;
+}
+
+inline bool WebSocketClient::connect() {
+  if (!is_valid_) { return false; }
+  shutdown_and_close();
+
+  Error error;
+  sock_ = detail::create_client_socket(
+      host_, std::string(), port_, AF_UNSPEC, false, false, nullptr, 5, 0,
+      read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
+      write_timeout_usec_, std::string(), error);
+
+  if (sock_ == INVALID_SOCKET) { return false; }
+
+  std::unique_ptr strm;
+  if (!create_stream(strm)) {
+    shutdown_and_close();
+    return false;
+  }
+
+  std::string selected_subprotocol;
+  if (!detail::perform_websocket_handshake(*strm, host_, port_, path_, headers_,
+                                           selected_subprotocol)) {
+    shutdown_and_close();
+    return false;
+  }
+  subprotocol_ = std::move(selected_subprotocol);
+
+  Request req;
+  req.method = "GET";
+  req.path = path_;
+  ws_ = std::unique_ptr(new WebSocket(std::move(strm), req, false));
+  return true;
+}
+
+inline ReadResult WebSocketClient::read(std::string &msg) {
+  if (!ws_) { return Fail; }
+  return ws_->read(msg);
+}
+
+inline bool WebSocketClient::send(const std::string &data) {
+  if (!ws_) { return false; }
+  return ws_->send(data);
+}
+
+inline bool WebSocketClient::send(const char *data, size_t len) {
+  if (!ws_) { return false; }
+  return ws_->send(data, len);
+}
+
+inline void WebSocketClient::close(CloseStatus status,
+                                   const std::string &reason) {
+  if (ws_) { ws_->close(status, reason); }
+}
+
+inline bool WebSocketClient::is_open() const { return ws_ && ws_->is_open(); }
+
+inline const std::string &WebSocketClient::subprotocol() const {
+  return subprotocol_;
+}
+
+inline void WebSocketClient::set_read_timeout(time_t sec, time_t usec) {
+  read_timeout_sec_ = sec;
+  read_timeout_usec_ = usec;
+}
+
+inline void WebSocketClient::set_write_timeout(time_t sec, time_t usec) {
+  write_timeout_sec_ = sec;
+  write_timeout_usec_ = usec;
+}
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+
+inline void WebSocketClient::set_ca_cert_path(const std::string &path) {
+  ca_cert_file_path_ = path;
+}
+
+inline void WebSocketClient::set_ca_cert_store(tls::ca_store_t store) {
+  ca_cert_store_ = store;
+}
+
+inline void
+WebSocketClient::enable_server_certificate_verification(bool enabled) {
+  server_certificate_verification_ = enabled;
+}
+
+#endif // CPPHTTPLIB_SSL_ENABLED
+
+} // namespace ws
+
 // ----------------------------------------------------------------------------
 
 } // namespace httplib
diff --git a/test/Makefile b/test/Makefile
index b8f5babee0..be9e567a9f 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -155,6 +155,10 @@ test_no_tls : test.cc include_httplib.cc ../httplib.h Makefile
 test_split_no_tls : test.cc ../httplib.h httplib.cc Makefile
 	$(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_NO_TLS)
 
+# ThreadPool unit tests (no TLS, no compression needed)
+test_thread_pool : test_thread_pool.cc ../httplib.h Makefile
+	$(CXX) -o $@ -I.. $(CXXFLAGS) test_thread_pool.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include -lpthread
+
 check_abi:
 	@./check-shared-library-abi-compatibility.sh
 
@@ -180,6 +184,10 @@ style_check: $(STYLE_CHECK_FILES)
 		echo "All files are properly formatted."; \
 	fi
 
+test_websocket_heartbeat : test_websocket_heartbeat.cc ../httplib.h Makefile
+	$(CXX) -o $@ -I.. $(CXXFLAGS) test_websocket_heartbeat.cc $(TEST_ARGS)
+	@file $@
+
 test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem
 	$(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS)
 
diff --git a/test/test.cc b/test/test.cc
index de29274457..0ba60fdc1a 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -4627,15 +4627,9 @@ TEST_F(ServerTest, HeaderCountExceedsLimit) {
   cli_.set_keep_alive(true);
   auto res = cli_.Get("/hi", headers);
 
-  // The request should either fail or return 400 Bad Request
-  if (res) {
-    // If we get a response, it should be 400 Bad Request
-    EXPECT_EQ(StatusCode::BadRequest_400, res->status);
-  } else {
-    // Or the request should fail entirely
-    EXPECT_FALSE(res);
-  }
-
+  // The server should respond with 400 Bad Request
+  ASSERT_TRUE(res);
+  EXPECT_EQ(StatusCode::BadRequest_400, res->status);
   EXPECT_EQ("close", res->get_header_value("Connection"));
   EXPECT_FALSE(cli_.is_socket_open());
 }
@@ -15704,3 +15698,763 @@ TEST(SSLClientServerTest, CustomizeServerSSLCtxMbedTLS) {
   ASSERT_EQ(StatusCode::OK_200, res->status);
 }
 #endif
+
+// WebSocket Tests
+
+TEST(WebSocketTest, RSVBitsMustBeZero) {
+  // RFC 6455 Section 5.2: RSV1, RSV2, RSV3 MUST be 0 unless an extension
+  // defining the meaning of these bits has been negotiated.
+  auto make_frame = [](uint8_t first_byte) {
+    std::string frame;
+    frame += static_cast(first_byte); // FIN + RSV + opcode
+    frame += static_cast(0x05);       // mask=0, payload_len=5
+    frame += "Hello";
+    return frame;
+  };
+
+  // RSV1 set (0x40)
+  {
+    detail::BufferStream strm;
+    strm.write(make_frame(0x81 | 0x40).data(), 8); // FIN + RSV1 + Text
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // RSV2 set (0x20)
+  {
+    detail::BufferStream strm;
+    strm.write(make_frame(0x81 | 0x20).data(), 8); // FIN + RSV2 + Text
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // RSV3 set (0x10)
+  {
+    detail::BufferStream strm;
+    strm.write(make_frame(0x81 | 0x10).data(), 8); // FIN + RSV3 + Text
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // No RSV bits set - should succeed
+  {
+    detail::BufferStream strm;
+    strm.write(make_frame(0x81).data(), 8); // FIN + Text, no RSV
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_TRUE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                               false, 1024));
+    EXPECT_EQ(ws::Opcode::Text, opcode);
+    EXPECT_EQ("Hello", payload);
+    EXPECT_TRUE(fin);
+  }
+}
+
+TEST(WebSocketTest, ControlFrameValidation) {
+  // RFC 6455 Section 5.5: control frames MUST have FIN=1 and
+  // payload length <= 125.
+
+  // Ping with FIN=0 - must be rejected
+  {
+    detail::BufferStream strm;
+    std::string frame;
+    frame += static_cast(0x09); // FIN=0, opcode=Ping
+    frame += static_cast(0x00); // mask=0, payload_len=0
+    strm.write(frame.data(), frame.size());
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // Close with FIN=0 - must be rejected
+  {
+    detail::BufferStream strm;
+    std::string frame;
+    frame += static_cast(0x08); // FIN=0, opcode=Close
+    frame += static_cast(0x00); // mask=0, payload_len=0
+    strm.write(frame.data(), frame.size());
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // Ping with payload_len=126 (extended length) - must be rejected
+  {
+    detail::BufferStream strm;
+    std::string frame;
+    frame += static_cast(0x89); // FIN=1, opcode=Ping
+    frame += static_cast(126);  // payload_len=126 (>125)
+    frame += static_cast(0x00); // extended length high byte
+    frame += static_cast(126);  // extended length low byte
+    frame += std::string(126, 'x');
+    strm.write(frame.data(), frame.size());
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // Ping with FIN=1 and payload_len=125 - should succeed
+  {
+    detail::BufferStream strm;
+    std::string frame;
+    frame += static_cast(0x89); // FIN=1, opcode=Ping
+    frame += static_cast(125);  // payload_len=125
+    frame += std::string(125, 'x');
+    strm.write(frame.data(), frame.size());
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_TRUE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                               false, 1024));
+    EXPECT_EQ(ws::Opcode::Ping, opcode);
+    EXPECT_EQ(125u, payload.size());
+    EXPECT_TRUE(fin);
+  }
+}
+
+TEST(WebSocketTest, PayloadLength64BitMSBMustBeZero) {
+  // RFC 6455 Section 5.2: the most significant bit of a 64-bit payload
+  // length MUST be 0.
+
+  // MSB set - must be rejected
+  {
+    detail::BufferStream strm;
+    std::string frame;
+    frame += static_cast(0x81); // FIN=1, opcode=Text
+    frame += static_cast(127);  // 64-bit extended length
+    frame += static_cast(0x80); // MSB set (invalid)
+    frame += std::string(7, '\0');    // remaining 7 bytes of length
+    strm.write(frame.data(), frame.size());
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_FALSE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                                false, 1024));
+  }
+
+  // MSB clear - should pass length parsing (will be rejected by max_len,
+  // but that's a different check; use a small length to verify)
+  {
+    detail::BufferStream strm;
+    std::string frame;
+    frame += static_cast(0x81); // FIN=1, opcode=Text
+    frame += static_cast(127);  // 64-bit extended length
+    frame += std::string(7, '\0');    // high bytes = 0
+    frame += static_cast(0x03); // length = 3
+    frame += "abc";
+    strm.write(frame.data(), frame.size());
+    ws::Opcode opcode;
+    std::string payload;
+    bool fin;
+    EXPECT_TRUE(ws::impl::read_websocket_frame(strm, opcode, payload, fin,
+                                               false, 1024));
+    EXPECT_EQ(ws::Opcode::Text, opcode);
+    EXPECT_EQ("abc", payload);
+  }
+}
+
+TEST(WebSocketTest, InvalidUTF8TextFrame) {
+  // RFC 6455 Section 5.6: text frames must contain valid UTF-8.
+
+  // Valid UTF-8
+  EXPECT_TRUE(ws::impl::is_valid_utf8("Hello"));
+  EXPECT_TRUE(ws::impl::is_valid_utf8("\xC3\xA9"));         // é (U+00E9)
+  EXPECT_TRUE(ws::impl::is_valid_utf8("\xE3\x81\x82"));     // あ (U+3042)
+  EXPECT_TRUE(ws::impl::is_valid_utf8("\xF0\x9F\x98\x80")); // 😀 (U+1F600)
+  EXPECT_TRUE(ws::impl::is_valid_utf8(""));
+
+  // Invalid UTF-8
+  EXPECT_FALSE(ws::impl::is_valid_utf8("\x80"));     // Invalid start byte
+  EXPECT_FALSE(ws::impl::is_valid_utf8("\xC3\x28")); // Bad continuation
+  EXPECT_FALSE(ws::impl::is_valid_utf8("\xC0\xAF")); // Overlong encoding
+  EXPECT_FALSE(
+      ws::impl::is_valid_utf8("\xED\xA0\x80")); // Surrogate half U+D800
+  EXPECT_FALSE(ws::impl::is_valid_utf8("\xF4\x90\x80\x80")); // Beyond U+10FFFF
+}
+
+TEST(WebSocketTest, ConnectAndDisconnect) {
+  Server svr;
+  svr.WebSocket("/ws", [](const Request &, ws::WebSocket &ws) {
+    std::string msg;
+    while (ws.read(msg)) {}
+  });
+
+  auto port = svr.bind_to_any_port(HOST);
+  std::thread t([&]() { svr.listen_after_bind(); });
+  svr.wait_until_ready();
+
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port) + "/ws");
+  ASSERT_TRUE(client.connect());
+  EXPECT_TRUE(client.is_open());
+  client.close();
+  EXPECT_FALSE(client.is_open());
+
+  svr.stop();
+  t.join();
+}
+
+TEST(WebSocketTest, ValidURL) {
+  ws::WebSocketClient ws1("ws://localhost:8080/path");
+  EXPECT_TRUE(ws1.is_valid());
+
+  ws::WebSocketClient ws2("ws://example.com/path");
+  EXPECT_TRUE(ws2.is_valid());
+
+  ws::WebSocketClient ws3("ws://example.com:9090/path/to/endpoint");
+  EXPECT_TRUE(ws3.is_valid());
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+  ws::WebSocketClient wss1("wss://example.com/path");
+  EXPECT_TRUE(wss1.is_valid());
+
+  ws::WebSocketClient wss2("wss://example.com:443/path");
+  EXPECT_TRUE(wss2.is_valid());
+#endif
+}
+
+TEST(WebSocketTest, InvalidURL) {
+  // No scheme
+  ws::WebSocketClient ws1("localhost:8080/path");
+  EXPECT_FALSE(ws1.is_valid());
+
+  // No path
+  ws::WebSocketClient ws2("ws://localhost:8080");
+  EXPECT_FALSE(ws2.is_valid());
+
+  // Empty string
+  ws::WebSocketClient ws3("");
+  EXPECT_FALSE(ws3.is_valid());
+
+  // Missing host
+  ws::WebSocketClient ws4("ws://:8080/path");
+  EXPECT_FALSE(ws4.is_valid());
+}
+
+TEST(WebSocketTest, UnsupportedScheme) {
+#ifdef CPPHTTPLIB_NO_EXCEPTIONS
+  ws::WebSocketClient ws1("http://localhost:8080/path");
+  EXPECT_FALSE(ws1.is_valid());
+
+  ws::WebSocketClient ws2("https://localhost:8080/path");
+  EXPECT_FALSE(ws2.is_valid());
+
+  ws::WebSocketClient ws3("ftp://localhost:8080/path");
+  EXPECT_FALSE(ws3.is_valid());
+#else
+  EXPECT_THROW(ws::WebSocketClient("http://localhost:8080/path"),
+               std::invalid_argument);
+
+  EXPECT_THROW(ws::WebSocketClient("ftp://localhost:8080/path"),
+               std::invalid_argument);
+#endif
+}
+
+TEST(WebSocketTest, ConnectWhenInvalid) {
+  ws::WebSocketClient ws("not a valid url");
+  EXPECT_FALSE(ws.is_valid());
+  EXPECT_FALSE(ws.connect());
+}
+
+TEST(WebSocketTest, DefaultPort) {
+  ws::WebSocketClient ws1("ws://example.com/path");
+  EXPECT_TRUE(ws1.is_valid());
+  // ws:// defaults to port 80 (verified by successful parse)
+
+#ifdef CPPHTTPLIB_SSL_ENABLED
+  ws::WebSocketClient ws2("wss://example.com/path");
+  EXPECT_TRUE(ws2.is_valid());
+  // wss:// defaults to port 443 (verified by successful parse)
+#endif
+}
+
+TEST(WebSocketTest, IPv6LiteralAddress) {
+  ws::WebSocketClient ws1("ws://[::1]:8080/path");
+  EXPECT_TRUE(ws1.is_valid());
+
+  ws::WebSocketClient ws2("ws://[fe80::1]:3000/ws");
+  EXPECT_TRUE(ws2.is_valid());
+}
+
+TEST(WebSocketTest, ComplexPath) {
+  ws::WebSocketClient ws1("ws://localhost:8080/path/to/endpoint");
+  EXPECT_TRUE(ws1.is_valid());
+
+  ws::WebSocketClient ws2("ws://localhost:8080/");
+  EXPECT_TRUE(ws2.is_valid());
+}
+
+class WebSocketIntegrationTest : public ::testing::Test {
+protected:
+  void SetUp() override {
+    server_ = httplib::detail::make_unique();
+    setup_server();
+    start_server();
+  }
+
+  void TearDown() override {
+    server_->stop();
+    if (server_thread_.joinable()) { server_thread_.join(); }
+  }
+
+  void setup_server() {
+    server_->WebSocket("/ws-echo", [](const Request &, ws::WebSocket &ws) {
+      std::string msg;
+      ws::ReadResult ret;
+      while ((ret = ws.read(msg))) {
+        if (ret == ws::Binary) {
+          ws.send(msg.data(), msg.size());
+        } else {
+          ws.send(msg);
+        }
+      }
+    });
+
+    server_->WebSocket("/ws-echo-string",
+                       [](const Request &, ws::WebSocket &ws) {
+                         std::string msg;
+                         while (ws.read(msg)) {
+                           ws.send("echo: " + msg);
+                         }
+                       });
+
+    server_->WebSocket(
+        "/ws-request-info", [](const Request &req, ws::WebSocket &ws) {
+          // Echo back request metadata
+          ws.send("path:" + req.path);
+          ws.send("header:" + req.get_header_value("X-Test-Header"));
+          std::string msg;
+          while (ws.read(msg)) {}
+        });
+
+    server_->WebSocket("/ws-close", [](const Request &, ws::WebSocket &ws) {
+      std::string msg;
+      ws.read(msg); // wait for a message
+      ws.close();
+    });
+
+    server_->WebSocket("/ws-close-status",
+                       [](const Request &, ws::WebSocket &ws) {
+                         std::string msg;
+                         ws.read(msg); // wait for a message
+                         ws.close(ws::CloseStatus::GoingAway, "shutting down");
+                       });
+
+    server_->WebSocket(
+        "/ws-subprotocol",
+        [](const Request &, ws::WebSocket &ws) {
+          std::string msg;
+          while (ws.read(msg)) {
+            ws.send(msg);
+          }
+        },
+        [](const std::vector &protocols) -> std::string {
+          for (const auto &p : protocols) {
+            if (p == "graphql-ws") { return p; }
+          }
+          return "";
+        });
+  }
+
+  void start_server() {
+    port_ = server_->bind_to_any_port(HOST);
+    server_thread_ = std::thread([this]() { server_->listen_after_bind(); });
+    server_->wait_until_ready();
+  }
+
+  std::unique_ptr server_;
+  std::thread server_thread_;
+  int port_ = 0;
+};
+
+TEST_F(WebSocketIntegrationTest, TextEcho) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+  ASSERT_TRUE(client.is_open());
+
+  ASSERT_TRUE(client.send("Hello WebSocket"));
+  std::string msg;
+  EXPECT_EQ(ws::Text, client.read(msg));
+  EXPECT_EQ("Hello WebSocket", msg);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, BinaryEcho) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+
+  std::string binary_data = {'\x00', '\x01', '\x02', '\xFF', '\xFE'};
+  ASSERT_TRUE(client.send(binary_data.data(), binary_data.size()));
+
+  std::string msg;
+  EXPECT_EQ(ws::Binary, client.read(msg));
+  EXPECT_EQ(binary_data, msg);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, MultipleMessages) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+
+  for (int i = 0; i < 10; i++) {
+    auto text = "message " + std::to_string(i);
+    ASSERT_TRUE(client.send(text));
+    std::string msg;
+    ASSERT_TRUE(client.read(msg));
+    EXPECT_EQ(text, msg);
+  }
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, CloseHandshake) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-close");
+  ASSERT_TRUE(client.connect());
+
+  // Send a message to trigger the server to close
+  ASSERT_TRUE(client.send("trigger close"));
+
+  // The server will close, so read should return false
+  std::string msg;
+  EXPECT_FALSE(client.read(msg));
+  EXPECT_FALSE(client.is_open());
+}
+
+TEST_F(WebSocketIntegrationTest, LargeMessage) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+
+  // 128KB message
+  std::string large_data(128 * 1024, 'X');
+  ASSERT_TRUE(client.send(large_data));
+  std::string msg;
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ(large_data, msg);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, ConcurrentSend) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+
+  const int num_threads = 4;
+  std::vector threads;
+  std::atomic send_count{0};
+
+  for (int t = 0; t < num_threads; t++) {
+    threads.emplace_back([&client, &send_count, t]() {
+      for (int i = 0; i < 5; i++) {
+        auto text = "thread" + std::to_string(t) + "_msg" + std::to_string(i);
+        if (client.send(text)) { send_count++; }
+      }
+    });
+  }
+
+  for (auto &th : threads) {
+    th.join();
+  }
+
+  int received = 0;
+  std::string msg;
+  while (received < send_count.load()) {
+    if (!client.read(msg)) { break; }
+    received++;
+  }
+  EXPECT_EQ(send_count.load(), received);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, ReadString) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo-string");
+  ASSERT_TRUE(client.connect());
+
+  ASSERT_TRUE(client.send("hello"));
+  std::string msg;
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("echo: hello", msg);
+
+  ASSERT_TRUE(client.send("world"));
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("echo: world", msg);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, RequestAccess) {
+  Headers headers = {{"X-Test-Header", "test-value"}};
+  ws::WebSocketClient client(
+      "ws://localhost:" + std::to_string(port_) + "/ws-request-info", headers);
+  ASSERT_TRUE(client.connect());
+
+  std::string msg;
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("path:/ws-request-info", msg);
+
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("header:test-value", msg);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, ReadTimeout) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  client.set_read_timeout(1, 0); // 1 second
+  ASSERT_TRUE(client.connect());
+
+  // Don't send anything — server echo handler waits for a message,
+  // so read() should time out and return false.
+  std::string msg;
+  EXPECT_FALSE(client.read(msg));
+}
+
+TEST_F(WebSocketIntegrationTest, MaxPayloadExceeded) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  client.set_read_timeout(5, 0);
+  ASSERT_TRUE(client.connect());
+
+  // Send a message exceeding CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH (16MB).
+  // The server should reject it and close the connection.
+  std::string oversized(CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH + 1, 'A');
+  client.send(oversized);
+
+  // The server's read() should have failed due to payload limit,
+  // so our read() should return false (connection closed).
+  std::string msg;
+  EXPECT_FALSE(client.read(msg));
+}
+
+TEST_F(WebSocketIntegrationTest, MaxPayloadAtLimit) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  client.set_read_timeout(10, 0);
+  ASSERT_TRUE(client.connect());
+
+  // Send a message exactly at CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH (16MB).
+  // This should succeed.
+  std::string at_limit(CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH, 'B');
+  ASSERT_TRUE(client.send(at_limit));
+
+  std::string msg;
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ(at_limit.size(), msg.size());
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, ConnectToInvalidPath) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/nonexistent");
+  EXPECT_FALSE(client.connect());
+  EXPECT_FALSE(client.is_open());
+}
+
+TEST_F(WebSocketIntegrationTest, EmptyMessage) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+
+  ASSERT_TRUE(client.send(""));
+  std::string msg;
+  EXPECT_EQ(ws::Text, client.read(msg));
+  EXPECT_EQ("", msg);
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, Reconnect) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+
+  // First connection
+  ASSERT_TRUE(client.connect());
+  ASSERT_TRUE(client.send("first"));
+  std::string msg;
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("first", msg);
+  client.close();
+  EXPECT_FALSE(client.is_open());
+
+  // Reconnect using the same client object
+  ASSERT_TRUE(client.connect());
+  ASSERT_TRUE(client.is_open());
+  ASSERT_TRUE(client.send("second"));
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("second", msg);
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, CloseWithStatus) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-close-status");
+  ASSERT_TRUE(client.connect());
+
+  // Trigger the server to close with GoingAway status
+  ASSERT_TRUE(client.send("trigger"));
+
+  // read() should return false after receiving the close frame
+  std::string msg;
+  EXPECT_FALSE(client.read(msg));
+  EXPECT_FALSE(client.is_open());
+}
+
+TEST_F(WebSocketIntegrationTest, ClientCloseWithStatus) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  ASSERT_TRUE(client.connect());
+
+  client.close(ws::CloseStatus::GoingAway, "client leaving");
+  EXPECT_FALSE(client.is_open());
+}
+
+TEST_F(WebSocketIntegrationTest, SubProtocolNegotiation) {
+  Headers headers = {{"Sec-WebSocket-Protocol", "mqtt, graphql-ws"}};
+  ws::WebSocketClient client(
+      "ws://localhost:" + std::to_string(port_) + "/ws-subprotocol", headers);
+  ASSERT_TRUE(client.connect());
+
+  // Server should have selected graphql-ws
+  EXPECT_EQ("graphql-ws", client.subprotocol());
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, SubProtocolNoMatch) {
+  Headers headers = {{"Sec-WebSocket-Protocol", "mqtt, wamp"}};
+  ws::WebSocketClient client(
+      "ws://localhost:" + std::to_string(port_) + "/ws-subprotocol", headers);
+  ASSERT_TRUE(client.connect());
+
+  // Server should not have selected any subprotocol
+  EXPECT_TRUE(client.subprotocol().empty());
+
+  client.close();
+}
+
+TEST_F(WebSocketIntegrationTest, SubProtocolNotRequested) {
+  // Connect without requesting any subprotocol
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) +
+                             "/ws-subprotocol");
+  ASSERT_TRUE(client.connect());
+
+  EXPECT_TRUE(client.subprotocol().empty());
+
+  client.close();
+}
+
+TEST(WebSocketPreRoutingTest, RejectWithoutAuth) {
+  Server svr;
+
+  svr.set_pre_routing_handler([](const Request &req, Response &res) {
+    if (!req.has_header("Authorization")) {
+      res.status = StatusCode::Unauthorized_401;
+      res.set_content("Unauthorized", "text/plain");
+      return Server::HandlerResponse::Handled;
+    }
+    return Server::HandlerResponse::Unhandled;
+  });
+
+  svr.WebSocket("/ws", [](const Request &, ws::WebSocket &ws) {
+    std::string msg;
+    while (ws.read(msg)) {
+      ws.send(msg);
+    }
+  });
+
+  auto port = svr.bind_to_any_port("localhost");
+  std::thread t([&]() { svr.listen_after_bind(); });
+  svr.wait_until_ready();
+
+  // Without Authorization header - should be rejected before upgrade
+  ws::WebSocketClient client1("ws://localhost:" + std::to_string(port) + "/ws");
+  EXPECT_FALSE(client1.connect());
+
+  // With Authorization header - should succeed
+  Headers headers = {{"Authorization", "Bearer token123"}};
+  ws::WebSocketClient client2("ws://localhost:" + std::to_string(port) + "/ws",
+                              headers);
+  ASSERT_TRUE(client2.connect());
+  ASSERT_TRUE(client2.send("hello"));
+  std::string msg;
+  ASSERT_TRUE(client2.read(msg));
+  EXPECT_EQ("hello", msg);
+  client2.close();
+
+  svr.stop();
+  t.join();
+}
+
+#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
+class WebSocketSSLIntegrationTest : public ::testing::Test {
+protected:
+  void SetUp() override {
+    server_ = httplib::detail::make_unique(SERVER_CERT_FILE,
+                                                      SERVER_PRIVATE_KEY_FILE);
+    server_->WebSocket("/ws-echo", [](const Request &, ws::WebSocket &ws) {
+      std::string msg;
+      ws::ReadResult ret;
+      while ((ret = ws.read(msg))) {
+        if (ret == ws::Binary) {
+          ws.send(msg.data(), msg.size());
+        } else {
+          ws.send(msg);
+        }
+      }
+    });
+    port_ = server_->bind_to_any_port(HOST);
+    server_thread_ = std::thread([this]() { server_->listen_after_bind(); });
+    server_->wait_until_ready();
+  }
+
+  void TearDown() override {
+    server_->stop();
+    if (server_thread_.joinable()) { server_thread_.join(); }
+  }
+
+  std::unique_ptr server_;
+  std::thread server_thread_;
+  int port_ = 0;
+};
+
+TEST_F(WebSocketSSLIntegrationTest, TextEcho) {
+  ws::WebSocketClient client("wss://localhost:" + std::to_string(port_) +
+                             "/ws-echo");
+  client.enable_server_certificate_verification(false);
+  ASSERT_TRUE(client.connect());
+  ASSERT_TRUE(client.is_open());
+
+  ASSERT_TRUE(client.send("Hello WSS"));
+  std::string msg;
+  EXPECT_EQ(ws::Text, client.read(msg));
+  EXPECT_EQ("Hello WSS", msg);
+
+  client.close();
+}
+#endif
diff --git a/test/test_thread_pool.cc b/test/test_thread_pool.cc
new file mode 100644
index 0000000000..1f08c7f2c4
--- /dev/null
+++ b/test/test_thread_pool.cc
@@ -0,0 +1,228 @@
+// ThreadPool unit tests
+// Set a short idle timeout for faster shrink tests
+#define CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT 1
+
+#include 
+
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+using namespace httplib;
+
+TEST(ThreadPoolTest, BasicTaskExecution) {
+  ThreadPool pool(4);
+  std::atomic count(0);
+
+  for (int i = 0; i < 10; i++) {
+    pool.enqueue([&count]() { count++; });
+  }
+
+  pool.shutdown();
+  EXPECT_EQ(10, count.load());
+}
+
+TEST(ThreadPoolTest, FixedPoolWhenMaxEqualsBase) {
+  // max_n == 0 means max = base (fixed pool behavior)
+  ThreadPool pool(4);
+  std::atomic count(0);
+
+  for (int i = 0; i < 100; i++) {
+    pool.enqueue([&count]() { count++; });
+  }
+
+  pool.shutdown();
+  EXPECT_EQ(100, count.load());
+}
+
+TEST(ThreadPoolTest, DynamicScaleUp) {
+  // base=2, max=8: block 2 base threads, then enqueue more tasks
+  ThreadPool pool(2, 8);
+
+  std::atomic active(0);
+  std::atomic max_active(0);
+  std::atomic completed(0);
+  std::mutex barrier_mutex;
+  std::condition_variable barrier_cv;
+  bool release = false;
+
+  // Occupy all base threads with blocking tasks
+  for (int i = 0; i < 2; i++) {
+    pool.enqueue([&]() {
+      active++;
+      {
+        std::unique_lock lock(barrier_mutex);
+        barrier_cv.wait(lock, [&] { return release; });
+      }
+      active--;
+      completed++;
+    });
+  }
+
+  // Wait for base threads to be occupied
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+  // These should trigger dynamic thread creation
+  for (int i = 0; i < 4; i++) {
+    pool.enqueue([&]() {
+      int cur = ++active;
+      // Track peak active count
+      int prev = max_active.load();
+      while (cur > prev && !max_active.compare_exchange_weak(prev, cur)) {}
+      std::this_thread::sleep_for(std::chrono::milliseconds(50));
+      active--;
+      completed++;
+    });
+  }
+
+  // Wait for dynamic tasks to complete
+  std::this_thread::sleep_for(std::chrono::milliseconds(500));
+
+  // Release the blocking tasks
+  {
+    std::unique_lock lock(barrier_mutex);
+    release = true;
+  }
+  barrier_cv.notify_all();
+
+  pool.shutdown();
+  EXPECT_EQ(6, completed.load());
+  // More than 2 threads were active simultaneously
+  EXPECT_GT(max_active.load(), 2);
+}
+
+TEST(ThreadPoolTest, DynamicShrinkAfterIdle) {
+  // CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT is set to 1 second
+  ThreadPool pool(2, 8);
+
+  std::atomic completed(0);
+
+  // Enqueue tasks that require dynamic threads
+  for (int i = 0; i < 8; i++) {
+    pool.enqueue([&]() {
+      std::this_thread::sleep_for(std::chrono::milliseconds(100));
+      completed++;
+    });
+  }
+
+  // Wait for all tasks to complete + idle timeout + margin
+  std::this_thread::sleep_for(std::chrono::milliseconds(2500));
+
+  // Now enqueue a simple task to verify the pool still works
+  // (base threads are still alive)
+  std::atomic final_task_done(false);
+  pool.enqueue([&]() { final_task_done = true; });
+
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+  pool.shutdown();
+  EXPECT_EQ(8, completed.load());
+  EXPECT_TRUE(final_task_done.load());
+}
+
+TEST(ThreadPoolTest, ShutdownWithActiveDynamicThreads) {
+  ThreadPool pool(2, 8);
+
+  std::atomic started(0);
+
+  std::mutex block_mutex;
+  std::condition_variable block_cv;
+  bool release = false;
+
+  // Start tasks on dynamic threads that block until released
+  for (int i = 0; i < 6; i++) {
+    pool.enqueue([&]() {
+      started++;
+      std::unique_lock lock(block_mutex);
+      block_cv.wait(lock, [&] { return release; });
+    });
+  }
+
+  // Wait for tasks to start
+  std::this_thread::sleep_for(std::chrono::milliseconds(200));
+  EXPECT_GE(started.load(), 2);
+
+  // Release all blocked threads, then shutdown
+  {
+    std::unique_lock lock(block_mutex);
+    release = true;
+  }
+  block_cv.notify_all();
+
+  pool.shutdown();
+}
+
+TEST(ThreadPoolTest, MaxQueuedRequests) {
+  // base=2, max=2 (fixed), mqr=3
+  ThreadPool pool(2, 2, 3);
+
+  std::mutex block_mutex;
+  std::condition_variable block_cv;
+  bool release = false;
+
+  // Block both threads
+  for (int i = 0; i < 2; i++) {
+    EXPECT_TRUE(pool.enqueue([&]() {
+      std::unique_lock lock(block_mutex);
+      block_cv.wait(lock, [&] { return release; });
+    }));
+  }
+
+  std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+  // Fill the queue up to max_queued_requests
+  EXPECT_TRUE(pool.enqueue([]() {}));
+  EXPECT_TRUE(pool.enqueue([]() {}));
+  EXPECT_TRUE(pool.enqueue([]() {}));
+
+  // This should fail - queue is full
+  EXPECT_FALSE(pool.enqueue([]() {}));
+
+  // Release blocked threads
+  {
+    std::unique_lock lock(block_mutex);
+    release = true;
+  }
+  block_cv.notify_all();
+
+  pool.shutdown();
+}
+
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
+TEST(ThreadPoolTest, InvalidMaxThreadsThrows) {
+  // max_n < n should throw
+  EXPECT_THROW(ThreadPool(8, 4), std::invalid_argument);
+}
+#endif
+
+TEST(ThreadPoolTest, EnqueueAfterShutdownReturnsFalse) {
+  ThreadPool pool(2);
+  pool.shutdown();
+  EXPECT_FALSE(pool.enqueue([]() {}));
+}
+
+TEST(ThreadPoolTest, ConcurrentEnqueue) {
+  ThreadPool pool(4, 16);
+  std::atomic count(0);
+  const int num_producers = 4;
+  const int tasks_per_producer = 100;
+
+  std::vector producers;
+  for (int p = 0; p < num_producers; p++) {
+    producers.emplace_back([&]() {
+      for (int i = 0; i < tasks_per_producer; i++) {
+        pool.enqueue([&count]() { count++; });
+      }
+    });
+  }
+
+  for (auto &t : producers) {
+    t.join();
+  }
+
+  pool.shutdown();
+  EXPECT_EQ(num_producers * tasks_per_producer, count.load());
+}
diff --git a/test/test_websocket_heartbeat.cc b/test/test_websocket_heartbeat.cc
new file mode 100644
index 0000000000..e5851ebf1f
--- /dev/null
+++ b/test/test_websocket_heartbeat.cc
@@ -0,0 +1,77 @@
+// Standalone test for WebSocket automatic heartbeat.
+// Compiled with a 1-second ping interval so we can verify heartbeat behavior
+// without waiting 30 seconds.
+
+#define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 1
+#define CPPHTTPLIB_WEBSOCKET_READ_TIMEOUT_SECOND 3
+#include 
+
+#include "gtest/gtest.h"
+
+using namespace httplib;
+
+class WebSocketHeartbeatTest : public ::testing::Test {
+protected:
+  void SetUp() override {
+    svr_.WebSocket("/ws", [](const Request &, ws::WebSocket &ws) {
+      std::string msg;
+      while (ws.read(msg)) {
+        ws.send(msg);
+      }
+    });
+
+    port_ = svr_.bind_to_any_port("localhost");
+    thread_ = std::thread([this]() { svr_.listen_after_bind(); });
+    svr_.wait_until_ready();
+  }
+
+  void TearDown() override {
+    svr_.stop();
+    thread_.join();
+  }
+
+  Server svr_;
+  int port_;
+  std::thread thread_;
+};
+
+// Verify that an idle connection stays alive beyond the read timeout
+// thanks to automatic heartbeat pings.
+TEST_F(WebSocketHeartbeatTest, IdleConnectionStaysAlive) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) + "/ws");
+  ASSERT_TRUE(client.connect());
+
+  // Sleep longer than read timeout (3s). Without heartbeat, the connection
+  // would time out. With heartbeat pings every 1s, it stays alive.
+  std::this_thread::sleep_for(std::chrono::seconds(5));
+
+  // Connection should still be open
+  ASSERT_TRUE(client.is_open());
+
+  // Verify we can still exchange messages
+  ASSERT_TRUE(client.send("hello after idle"));
+  std::string msg;
+  ASSERT_TRUE(client.read(msg));
+  EXPECT_EQ("hello after idle", msg);
+
+  client.close();
+}
+
+// Verify that multiple heartbeat cycles work
+TEST_F(WebSocketHeartbeatTest, MultipleHeartbeatCycles) {
+  ws::WebSocketClient client("ws://localhost:" + std::to_string(port_) + "/ws");
+  ASSERT_TRUE(client.connect());
+
+  // Wait through several heartbeat cycles
+  for (int i = 0; i < 3; i++) {
+    std::this_thread::sleep_for(std::chrono::milliseconds(1500));
+    ASSERT_TRUE(client.is_open());
+    std::string text = "msg" + std::to_string(i);
+    ASSERT_TRUE(client.send(text));
+    std::string msg;
+    ASSERT_TRUE(client.read(msg));
+    EXPECT_EQ(text, msg);
+  }
+
+  client.close();
+}

From b2430249d2955263d2624e65353a3afaf479699b Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 14 Feb 2026 17:59:15 -0500
Subject: [PATCH 0631/1222] Update README-websocket.md and justfile

---
 README-websocket.md | 10 +++++-----
 justfile            |  9 +++++++++
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/README-websocket.md b/README-websocket.md
index 31ed826354..b063e8908a 100644
--- a/README-websocket.md
+++ b/README-websocket.md
@@ -355,21 +355,21 @@ if (ws.connect()) {
 
 ## Threading Model
 
-WebSocket connections share the same thread pool as HTTP requests. Each WebSocket connection occupies one thread for its entire lifetime. The default thread pool size is `CPPHTTPLIB_THREAD_POOL_COUNT` (8).
+WebSocket connections share the same thread pool as HTTP requests. Each WebSocket connection occupies one thread for its entire lifetime.
 
-This means that if you have 8 simultaneous WebSocket connections with the default settings, **no more HTTP requests can be processed** until a WebSocket connection closes and frees up a thread.
+The default thread pool uses dynamic scaling: it maintains a base thread count of `CPPHTTPLIB_THREAD_POOL_COUNT` (8 or `std::thread::hardware_concurrency() - 1`, whichever is greater) and can scale up to 4x that count under load (`CPPHTTPLIB_THREAD_POOL_MAX_COUNT`). When all base threads are busy, temporary threads are spawned automatically up to the maximum. These dynamic threads exit after an idle timeout (`CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT`, default 3 seconds).
 
-If your application uses WebSocket, you should increase the thread pool size:
+This dynamic scaling helps accommodate WebSocket connections alongside HTTP requests. However, if you expect many simultaneous WebSocket connections, you should configure the thread pool accordingly:
 
 ```cpp
 httplib::Server svr;
 
 svr.new_task_queue = [] {
-  return new httplib::ThreadPool(128); // Increase from default 8
+  return new httplib::ThreadPool(/*base_threads=*/8, /*max_threads=*/128);
 };
 ```
 
-Choose a size that accounts for both your expected HTTP load and the maximum number of simultaneous WebSocket connections.
+Choose sizes that account for both your expected HTTP load and the maximum number of simultaneous WebSocket connections.
 
 ## Protocol
 
diff --git a/justfile b/justfile
index 6b4c5f07fc..15dffcba9c 100644
--- a/justfile
+++ b/justfile
@@ -28,6 +28,15 @@ no_tls_parallel:
 fuzz:
     @(cd test && make fuzz_test)
 
+proxy:
+    @(cd test && make proxy)
+
+websocket_heartbeat:
+    @(cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat)
+
+thread_pool:
+    @(cd test && make test_thread_pool && ./test_thread_pool)
+
 build:
     @(cd test && make test_split)
     @(cd test && make test_split_mbedtls)

From ab96f72b96870f70a78a2bae659586a3c6c49cca Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 14 Feb 2026 20:06:01 -0500
Subject: [PATCH 0632/1222] Update README

---
 README.md | 119 ++++++++++++++++++++++++++----------------------------
 1 file changed, 58 insertions(+), 61 deletions(-)

diff --git a/README.md b/README.md
index 2ba98b7104..f0b6dff4ed 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,4 @@
-cpp-httplib
-===========
+# cpp-httplib
 
 [![](https://github.com/yhirose/cpp-httplib/workflows/test/badge.svg)](https://github.com/yhirose/cpp-httplib/actions)
 
@@ -10,10 +9,16 @@ It's extremely easy to set up. Just include the **httplib.h** file in your code!
 > [!IMPORTANT]
 > This library uses 'blocking' socket I/O. If you are looking for a library with 'non-blocking' socket I/O, this is not the one that you want.
 
-Simple examples
----------------
+## Features
 
-#### Server (Multi-threaded)
+- HTTP Server/Client
+- TLS support (OpenSSL, MbedTLS)
+- WebSocket
+- Content compression (gzip, Brotli, zstd)
+
+## Simple examples
+
+### Server
 
 ```c++
 #define CPPHTTPLIB_OPENSSL_SUPPORT
@@ -32,7 +37,7 @@ svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) {
 svr.listen("0.0.0.0", 8080);
 ```
 
-#### Client
+### Client
 
 ```c++
 #define CPPHTTPLIB_OPENSSL_SUPPORT
@@ -50,8 +55,7 @@ if (auto res = cli.Get("/hi")) {
 }
 ```
 
-SSL/TLS Support
----------------
+## SSL/TLS Support
 
 cpp-httplib supports multiple TLS backends through an abstraction layer:
 
@@ -92,30 +96,6 @@ cli.enable_server_certificate_verification(false);
 cli.enable_server_hostname_verification(false);
 ```
 
-### Windows Certificate Verification
-
-On Windows, cpp-httplib automatically performs additional certificate verification using the Windows certificate store via CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`). This works with both OpenSSL and Mbed TLS backends, providing:
-
-- Real-time certificate validation integrated with Windows Update
-- Certificate revocation checking
-- SSL/TLS policy verification using the system certificate store (ROOT and CA)
-
-This feature is enabled by default and can be controlled at runtime:
-
-```c++
-// Disable Windows certificate verification (use only OpenSSL/Mbed TLS verification)
-cli.enable_windows_certificate_verification(false);
-```
-
-To disable this feature at compile time, define:
-
-```c++
-#define CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
-```
-
-> [!NOTE]
-> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself.
-
 ### SSL Error Handling
 
 When SSL operations fail, cpp-httplib provides detailed error information through `ssl_error()` and `ssl_backend_error()`:
@@ -205,8 +185,31 @@ svr.Get("/", [](const httplib::Request &req, httplib::Response &res) {
 });
 ```
 
-Server
-------
+### Windows Certificate Verification
+
+On Windows, cpp-httplib automatically performs additional certificate verification using the Windows certificate store via CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`). This works with both OpenSSL and Mbed TLS backends, providing:
+
+- Real-time certificate validation integrated with Windows Update
+- Certificate revocation checking
+- SSL/TLS policy verification using the system certificate store (ROOT and CA)
+
+This feature is enabled by default and can be controlled at runtime:
+
+```c++
+// Disable Windows certificate verification (use only OpenSSL/Mbed TLS verification)
+cli.enable_windows_certificate_verification(false);
+```
+
+To disable this feature at compile time, define:
+
+```c++
+#define CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
+```
+
+> [!NOTE]
+> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself.
+
+## Server
 
 ```c++
 #include 
@@ -764,8 +767,7 @@ svr.new_task_queue = [] {
 };
 ```
 
-Client
-------
+## Client
 
 ```c++
 #include 
@@ -894,11 +896,15 @@ httplib::Headers headers = {
 };
 auto res = cli.Get("/hi", headers);
 ```
+
 or
+
 ```c++
 auto res = cli.Get("/hi", {{"Hello", "World!"}});
 ```
+
 or
+
 ```c++
 cli.set_default_headers({
   { "Hello", "World!" }
@@ -1189,23 +1195,21 @@ httplib::Server svr;
 svr.listen("127.0.0.1", 8080);
 ```
 
-Payload Limit
--------------
+## Payload Limit
 
 The maximum payload body size is limited to 100MB by default for both server and client. You can change it with `set_payload_max_length()` or by defining `CPPHTTPLIB_PAYLOAD_MAX_LENGTH` at compile time. Setting it to `0` disables the limit entirely.
 
-Compression
------------
+## Compression
 
 The server can apply compression to the following MIME type contents:
 
-  * all text types except text/event-stream
-  * image/svg+xml
-  * application/javascript
-  * application/json
-  * application/xml
-  * application/protobuf
-  * application/xhtml+xml
+- all text types except text/event-stream
+- image/svg+xml
+- application/javascript
+- application/json
+- application/xml
+- application/protobuf
+- application/xhtml+xml
 
 ### Zlib Support
 
@@ -1298,8 +1302,7 @@ std::string decoded_component = httplib::decode_uri_component(encoded_component)
 
 Use `encode_uri()` for full URLs and `encode_uri_component()` for individual query parameters or path segments.
 
-Stream API
-----------
+## Stream API
 
 Process large responses without loading everything into memory.
 
@@ -1326,8 +1329,7 @@ All HTTP methods are supported: `stream::Get`, `Post`, `Put`, `Patch`, `Delete`,
 
 See [README-stream.md](README-stream.md) for more details.
 
-SSE Client
-----------
+## SSE Client
 
 ```cpp
 #include 
@@ -1348,8 +1350,7 @@ int main() {
 
 See [README-sse.md](README-sse.md) for more details.
 
-WebSocket
----------
+## WebSocket
 
 ```cpp
 // Server
@@ -1389,8 +1390,7 @@ SSL is also supported via `wss://` scheme (e.g. `WebSocketClient("wss://example.
 
 See [README-websocket.md](README-websocket.md) for more details.
 
-Split httplib.h into .h and .cc
--------------------------------
+## Split httplib.h into .h and .cc
 
 ```console
 $ ./split.py -h
@@ -1408,8 +1408,7 @@ $ ./split.py
 Wrote out/httplib.h and out/httplib.cc
 ```
 
-Dockerfile for Static HTTP Server
----------------------------------
+## Dockerfile for Static HTTP Server
 
 Dockerfile for static HTTP server is available. Port number of this HTTP server is 80, and it serves static files from `/html` directory in the container.
 
@@ -1480,12 +1479,10 @@ Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32
 > [!NOTE]
 > Windows 8 or lower, Visual Studio 2015 or lower, and Cygwin and MSYS2 including MinGW are neither supported nor tested.
 
-License
--------
+## License
 
 MIT license (© 2026 Yuji Hirose)
 
-Special Thanks To
------------------
+## Special Thanks To
 
 [These folks](https://github.com/yhirose/cpp-httplib/graphs/contributors) made great contributions to polish this library to totally another level from a simple toy!

From c41c5fb8a949c986cbf30939a2b426d0a907d513 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 14 Feb 2026 23:22:42 -0500
Subject: [PATCH 0633/1222] Revise README features section and header

Updated section headers and improved feature list formatting.
---
 README.md | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index f0b6dff4ed..d5e75bd168 100644
--- a/README.md
+++ b/README.md
@@ -9,12 +9,13 @@ It's extremely easy to set up. Just include the **httplib.h** file in your code!
 > [!IMPORTANT]
 > This library uses 'blocking' socket I/O. If you are looking for a library with 'non-blocking' socket I/O, this is not the one that you want.
 
-## Features
+## Main Features
 
 - HTTP Server/Client
-- TLS support (OpenSSL, MbedTLS)
-- WebSocket
-- Content compression (gzip, Brotli, zstd)
+- SSL/TLS support (OpenSSL, MbedTLS)
+- [Stream API](README-stream.md)
+- [Server-Sent Events](README-sse.md)
+- [WebSocket](README-websocket.md)
 
 ## Simple examples
 

From 718d7d92b993d8fdf6a74d82af8572b96787a909 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Mon, 16 Feb 2026 06:21:40 -0500
Subject: [PATCH 0634/1222] Fix problems in Unit tests

---
 test/test.cc | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/test/test.cc b/test/test.cc
index 0ba60fdc1a..257b78eb99 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -9153,8 +9153,19 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) {
     auto cli = ::accept(srv, reinterpret_cast(&cli_addr), &cli_len);
 
     if (cli != INVALID_SOCKET) {
+      // Read the full HTTP request (until \r\n\r\n)
       char buf[4096];
-      ::recv(cli, buf, sizeof(buf), 0);
+      size_t total = 0;
+      while (total < sizeof(buf)) {
+        auto n = ::recv(cli, buf + total, sizeof(buf) - total, 0);
+        if (n <= 0) break;
+        total += static_cast(n);
+        // Check for end of headers
+        if (total >= 4) {
+          std::string req(buf, total);
+          if (req.find("\r\n\r\n") != std::string::npos) break;
+        }
+      }
 
       // Malicious response: gzip-compressed body, no Content-Length
       std::string response_header = "HTTP/1.1 200 OK\r\n"
@@ -11633,7 +11644,7 @@ TEST(TaskQueueTest, IncreaseAtomicIntegerWithQueueLimit) {
   unsigned int queued_count{0};
   std::atomic_uint count{0};
   std::unique_ptr task_queue{
-      new ThreadPool{/*num_threads=*/1, qlimit}};
+      new ThreadPool{/*num_threads=*/1, /*max_threads=*/1, qlimit}};
 
   for (unsigned int i = 0; i < number_of_tasks; ++i) {
     if (task_queue->enqueue(
@@ -11654,7 +11665,7 @@ TEST(TaskQueueTest, IncreaseAtomicIntegerWithQueueLimit) {
 
 TEST(TaskQueueTest, MaxQueuedRequests) {
   static constexpr unsigned int qlimit{3};
-  std::unique_ptr task_queue{new ThreadPool{1, qlimit}};
+  std::unique_ptr task_queue{new ThreadPool{1, 1, qlimit}};
   std::condition_variable sem_cv;
   std::mutex sem_mtx;
   int credits = 0;

From 0d5bf55c73cf42ba5e58f2e55d9edcbc70d097a3 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Fri, 20 Feb 2026 15:42:45 -0500
Subject: [PATCH 0635/1222] Add wolfSSL support (#2370)

* Add wolfSSL support

* Update CI

* Fix build error

* Revert "Fix build error"

This reverts commit d48096277fd53777988d23dfdc53d9ce6bbc334c.

* Fix build errors

* Build errors on ubuntu

* Update README

* Refactoring

* Fix wolfSSL issues
---
 .github/workflows/test.yaml |   16 +-
 .gitignore                  |    2 +
 README.md                   |   21 +-
 httplib.h                   | 1463 ++++++++++++++++++++++++++++++++---
 justfile                    |   19 +-
 test/Makefile               |   42 +-
 6 files changed, 1455 insertions(+), 108 deletions(-)

diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml
index 69158d3f7d..378c0ce3d9 100644
--- a/.github/workflows/test.yaml
+++ b/.github/workflows/test.yaml
@@ -77,7 +77,7 @@ jobs:
       (github.event_name == 'workflow_dispatch' && github.event.inputs.test_linux == 'true')
     strategy:
       matrix:
-        tls_backend: [openssl, mbedtls]
+        tls_backend: [openssl, mbedtls, wolfssl]
     name: ubuntu (${{ matrix.tls_backend }})
     steps:
       - name: checkout
@@ -92,6 +92,9 @@ jobs:
       - name: install Mbed TLS
         if: matrix.tls_backend == 'mbedtls'
         run: sudo apt-get install -y libmbedtls-dev
+      - name: install wolfSSL
+        if: matrix.tls_backend == 'wolfssl'
+        run: sudo apt-get install -y libwolfssl-dev
       - name: build and run tests (OpenSSL)
         if: matrix.tls_backend == 'openssl'
         run: cd test && make test_split && make test_openssl_parallel
@@ -100,6 +103,9 @@ jobs:
       - name: build and run tests (Mbed TLS)
         if: matrix.tls_backend == 'mbedtls'
         run: cd test && make test_split_mbedtls && make test_mbedtls_parallel
+      - name: build and run tests (wolfSSL)
+        if: matrix.tls_backend == 'wolfssl'
+        run: cd test && make test_split_wolfssl && make test_wolfssl_parallel
       - name: run fuzz test target
         if: matrix.tls_backend == 'openssl'
         run: cd test && make fuzz_test
@@ -118,7 +124,7 @@ jobs:
       (github.event_name == 'workflow_dispatch' && github.event.inputs.test_macos == 'true')
     strategy:
       matrix:
-        tls_backend: [openssl, mbedtls]
+        tls_backend: [openssl, mbedtls, wolfssl]
     name: macos (${{ matrix.tls_backend }})
     steps:
       - name: checkout
@@ -126,6 +132,9 @@ jobs:
       - name: install Mbed TLS
         if: matrix.tls_backend == 'mbedtls'
         run: brew install mbedtls@3
+      - name: install wolfSSL
+        if: matrix.tls_backend == 'wolfssl'
+        run: brew install wolfssl
       - name: build and run tests (OpenSSL)
         if: matrix.tls_backend == 'openssl'
         run: cd test && make test_split && make test_openssl_parallel
@@ -134,6 +143,9 @@ jobs:
       - name: build and run tests (Mbed TLS)
         if: matrix.tls_backend == 'mbedtls'
         run: cd test && make test_split_mbedtls && make test_mbedtls_parallel
+      - name: build and run tests (wolfSSL)
+        if: matrix.tls_backend == 'wolfssl'
+        run: cd test && make test_split_wolfssl && make test_wolfssl_parallel
       - name: run fuzz test target
         if: matrix.tls_backend == 'openssl'
         run: cd test && make fuzz_test
diff --git a/.gitignore b/.gitignore
index 04d7498fd9..01cc6cfd54 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,12 +36,14 @@ test/httplib.cc
 test/httplib.h
 test/test
 test/test_mbedtls
+test/test_wolfssl
 test/test_no_tls
 test/server_fuzzer
 test/test_proxy
 test/test_proxy_mbedtls
 test/test_split
 test/test_split_mbedtls
+test/test_split_wolfssl
 test/test_split_no_tls
 test/test_websocket_heartbeat
 test/test_thread_pool
diff --git a/README.md b/README.md
index d5e75bd168..f40893c34c 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ It's extremely easy to set up. Just include the **httplib.h** file in your code!
 ## Main Features
 
 - HTTP Server/Client
-- SSL/TLS support (OpenSSL, MbedTLS)
+- SSL/TLS support (OpenSSL, MbedTLS, wolfSSL)
 - [Stream API](README-stream.md)
 - [Server-Sent Events](README-sse.md)
 - [WebSocket](README-websocket.md)
@@ -64,6 +64,7 @@ cpp-httplib supports multiple TLS backends through an abstraction layer:
 | :------ | :----- | :-------- |
 | OpenSSL | `CPPHTTPLIB_OPENSSL_SUPPORT` | `libssl`, `libcrypto` |
 | Mbed TLS | `CPPHTTPLIB_MBEDTLS_SUPPORT` | `libmbedtls`, `libmbedx509`, `libmbedcrypto` |
+| wolfSSL | `CPPHTTPLIB_WOLFSSL_SUPPORT` | `libwolfssl` |
 
 > [!NOTE]
 > OpenSSL 3.0 or later is required. Please see [this page](https://www.openssl.org/policies/releasestrat.html) for more information.
@@ -71,12 +72,18 @@ cpp-httplib supports multiple TLS backends through an abstraction layer:
 > [!NOTE]
 > Mbed TLS 2.x and 3.x are supported. The library automatically detects the version and uses the appropriate API.
 
+> [!NOTE]
+> wolfSSL must be built with OpenSSL compatibility layer enabled (`--enable-opensslall`). wolfSSL 5.x is supported.
+
+> [!NOTE]
+> **Mbed TLS / wolfSSL limitation:** `get_ca_certs()` and `get_ca_names()` only reflect CA certificates loaded via `load_ca_cert_store()` or `load_ca_cert_store(pem, size)`. Certificates loaded through `set_ca_cert_path()` or system certificates (`load_system_certs`) are not enumerable with these backends.
+
 > [!TIP]
 > For macOS: cpp-httplib can use system certs with `CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN`. `CoreFoundation` and `Security` should be linked with `-framework`.
 
 ```c++
-// Use either OpenSSL or Mbed TLS
-#define CPPHTTPLIB_OPENSSL_SUPPORT   // or CPPHTTPLIB_MBEDTLS_SUPPORT
+// Use either OpenSSL, Mbed TLS, or wolfSSL
+#define CPPHTTPLIB_OPENSSL_SUPPORT   // or CPPHTTPLIB_MBEDTLS_SUPPORT or CPPHTTPLIB_WOLFSSL_SUPPORT
 #include "path/to/httplib.h"
 
 // Server
@@ -102,10 +109,10 @@ cli.enable_server_hostname_verification(false);
 When SSL operations fail, cpp-httplib provides detailed error information through `ssl_error()` and `ssl_backend_error()`:
 
 - `ssl_error()` - Returns the TLS-level error code (e.g., `SSL_ERROR_SSL` for OpenSSL)
-- `ssl_backend_error()` - Returns the backend-specific error code (e.g., `ERR_get_error()` for OpenSSL, return value for Mbed TLS)
+- `ssl_backend_error()` - Returns the backend-specific error code (e.g., `ERR_get_error()` for OpenSSL/wolfSSL, return value for Mbed TLS)
 
 ```c++
-#define CPPHTTPLIB_OPENSSL_SUPPORT  // or CPPHTTPLIB_MBEDTLS_SUPPORT
+#define CPPHTTPLIB_OPENSSL_SUPPORT  // or CPPHTTPLIB_MBEDTLS_SUPPORT or CPPHTTPLIB_WOLFSSL_SUPPORT
 #include "path/to/httplib.h"
 
 httplib::Client cli("https://example.com");
@@ -188,7 +195,7 @@ svr.Get("/", [](const httplib::Request &req, httplib::Response &res) {
 
 ### Windows Certificate Verification
 
-On Windows, cpp-httplib automatically performs additional certificate verification using the Windows certificate store via CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`). This works with both OpenSSL and Mbed TLS backends, providing:
+On Windows, cpp-httplib automatically performs additional certificate verification using the Windows certificate store via CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`). This works with all TLS backends (OpenSSL, Mbed TLS, and wolfSSL), providing:
 
 - Real-time certificate validation integrated with Windows Update
 - Certificate revocation checking
@@ -197,7 +204,7 @@ On Windows, cpp-httplib automatically performs additional certificate verificati
 This feature is enabled by default and can be controlled at runtime:
 
 ```c++
-// Disable Windows certificate verification (use only OpenSSL/Mbed TLS verification)
+// Disable Windows certificate verification (use only OpenSSL/Mbed TLS/wolfSSL verification)
 cli.enable_windows_certificate_verification(false);
 ```
 
diff --git a/httplib.h b/httplib.h
index 06cb9878e2..a260c1f363 100644
--- a/httplib.h
+++ b/httplib.h
@@ -439,10 +439,46 @@ using socket_t = int;
 
 #endif // CPPHTTPLIB_MBEDTLS_SUPPORT
 
+#ifdef CPPHTTPLIB_WOLFSSL_SUPPORT
+#include 
+
+#include 
+
+// Fallback definitions for older wolfSSL versions (e.g., 5.6.6)
+#ifndef WOLFSSL_GEN_EMAIL
+#define WOLFSSL_GEN_EMAIL 1
+#endif
+#ifndef WOLFSSL_GEN_DNS
+#define WOLFSSL_GEN_DNS 2
+#endif
+#ifndef WOLFSSL_GEN_URI
+#define WOLFSSL_GEN_URI 6
+#endif
+#ifndef WOLFSSL_GEN_IPADD
+#define WOLFSSL_GEN_IPADD 7
+#endif
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#ifdef _WIN32
+#include 
+#ifdef _MSC_VER
+#pragma comment(lib, "crypt32.lib")
+#endif
+#endif // _WIN32
+#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
+#if TARGET_OS_MAC
+#include 
+#endif
+#endif // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN
+#endif // CPPHTTPLIB_WOLFSSL_SUPPORT
+
 // Define CPPHTTPLIB_SSL_ENABLED if any SSL backend is available
-// This simplifies conditional compilation when adding new backends (e.g.,
-// wolfSSL)
-#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) || defined(CPPHTTPLIB_MBEDTLS_SUPPORT)
+#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) ||                                     \
+    defined(CPPHTTPLIB_MBEDTLS_SUPPORT) || defined(CPPHTTPLIB_WOLFSSL_SUPPORT)
 #define CPPHTTPLIB_SSL_ENABLED
 #endif
 
@@ -3023,6 +3059,36 @@ struct MbedTlsContext {
 } // namespace tls
 #endif
 
+#ifdef CPPHTTPLIB_WOLFSSL_SUPPORT
+namespace tls {
+namespace impl {
+
+// wolfSSL context wrapper (holds WOLFSSL_CTX and related state).
+// This struct is accessible via tls::impl for use in SSL context
+// setup callbacks (cast ctx_t to tls::impl::WolfSSLContext*).
+struct WolfSSLContext {
+  WOLFSSL_CTX *ctx = nullptr;
+  bool is_server = false;
+  bool verify_client = false;
+  bool has_verify_callback = false;
+  std::string ca_pem_data_; // accumulated PEM for get_ca_names/get_ca_certs
+
+  WolfSSLContext();
+  ~WolfSSLContext();
+
+  WolfSSLContext(const WolfSSLContext &) = delete;
+  WolfSSLContext &operator=(const WolfSSLContext &) = delete;
+};
+
+// CA store for wolfSSL: holds raw PEM bytes to allow reloading into any ctx
+struct WolfSSLCAStore {
+  std::string pem_data;
+};
+
+} // namespace impl
+} // namespace tls
+#endif
+
 #endif // CPPHTTPLIB_SSL_ENABLED
 
 namespace stream {
@@ -8202,6 +8268,39 @@ inline std::string SHA_512(const std::string &s) {
 #endif
   return hash_to_hex(hash);
 }
+#elif defined(CPPHTTPLIB_WOLFSSL_SUPPORT)
+namespace {
+template 
+inline std::string hash_to_hex(const unsigned char (&hash)[N]) {
+  std::stringstream ss;
+  for (size_t i = 0; i < N; ++i) {
+    ss << std::hex << std::setw(2) << std::setfill('0')
+       << static_cast(hash[i]);
+  }
+  return ss.str();
+}
+} // namespace
+
+inline std::string MD5(const std::string &s) {
+  unsigned char hash[WC_MD5_DIGEST_SIZE];
+  wc_Md5Hash(reinterpret_cast(s.c_str()),
+             static_cast(s.size()), hash);
+  return hash_to_hex(hash);
+}
+
+inline std::string SHA_256(const std::string &s) {
+  unsigned char hash[WC_SHA256_DIGEST_SIZE];
+  wc_Sha256Hash(reinterpret_cast(s.c_str()),
+                static_cast(s.size()), hash);
+  return hash_to_hex(hash);
+}
+
+inline std::string SHA_512(const std::string &s) {
+  unsigned char hash[WC_SHA512_DIGEST_SIZE];
+  wc_Sha512Hash(reinterpret_cast(s.c_str()),
+                static_cast(s.size()), hash);
+  return hash_to_hex(hash);
+}
 #endif
 
 inline bool is_ip_address(const std::string &host) {
@@ -15078,11 +15177,11 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
 
   bool is_ip = detail::is_ip_address(host_);
 
-#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT
-  // MbedTLS needs explicit verification mode (OpenSSL uses SSL_VERIFY_NONE
-  // by default and performs all verification post-handshake).
+#if defined(CPPHTTPLIB_MBEDTLS_SUPPORT) || defined(CPPHTTPLIB_WOLFSSL_SUPPORT)
+  // MbedTLS/wolfSSL need explicit verification mode (OpenSSL uses
+  // SSL_VERIFY_NONE by default and performs all verification post-handshake).
   // For IP addresses with verification enabled, use OPTIONAL mode since
-  // MbedTLS requires hostname for VERIFY_REQUIRED.
+  // these backends require hostname for strict verification.
   if (is_ip && server_certificate_verification_) {
     set_verify_client(ctx_, false);
   } else {
@@ -15303,6 +15402,107 @@ inline VerifyCallback &get_mbedtls_verify_callback() {
   return callback;
 }
 
+// Check if a string is an IPv4 address
+inline bool is_ipv4_address(const std::string &str) {
+  int dots = 0;
+  for (char c : str) {
+    if (c == '.') {
+      dots++;
+    } else if (!isdigit(static_cast(c))) {
+      return false;
+    }
+  }
+  return dots == 3;
+}
+
+// Parse IPv4 address string to bytes
+inline bool parse_ipv4(const std::string &str, unsigned char *out) {
+  int parts[4];
+  if (sscanf(str.c_str(), "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2],
+             &parts[3]) != 4) {
+    return false;
+  }
+  for (int i = 0; i < 4; i++) {
+    if (parts[i] < 0 || parts[i] > 255) return false;
+    out[i] = static_cast(parts[i]);
+  }
+  return true;
+}
+
+#ifdef _WIN32
+// Enumerate Windows system certificates and call callback with DER data
+template 
+inline bool enumerate_windows_system_certs(Callback cb) {
+  bool loaded = false;
+  static const wchar_t *store_names[] = {L"ROOT", L"CA"};
+  for (auto store_name : store_names) {
+    HCERTSTORE hStore = CertOpenSystemStoreW(0, store_name);
+    if (hStore) {
+      PCCERT_CONTEXT pContext = nullptr;
+      while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) !=
+             nullptr) {
+        if (cb(pContext->pbCertEncoded, pContext->cbCertEncoded)) {
+          loaded = true;
+        }
+      }
+      CertCloseStore(hStore, 0);
+    }
+  }
+  return loaded;
+}
+#endif
+
+#if defined(__APPLE__) && defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
+// Enumerate macOS Keychain certificates and call callback with DER data
+template 
+inline bool enumerate_macos_keychain_certs(Callback cb) {
+  bool loaded = false;
+  CFArrayRef certs = nullptr;
+  OSStatus status = SecTrustCopyAnchorCertificates(&certs);
+  if (status == errSecSuccess && certs) {
+    CFIndex count = CFArrayGetCount(certs);
+    for (CFIndex i = 0; i < count; i++) {
+      SecCertificateRef cert =
+          (SecCertificateRef)CFArrayGetValueAtIndex(certs, i);
+      CFDataRef data = SecCertificateCopyData(cert);
+      if (data) {
+        if (cb(CFDataGetBytePtr(data),
+               static_cast(CFDataGetLength(data)))) {
+          loaded = true;
+        }
+        CFRelease(data);
+      }
+    }
+    CFRelease(certs);
+  }
+  return loaded;
+}
+#endif
+
+#if !defined(_WIN32) && !(defined(__APPLE__) &&                                \
+                          defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN))
+// Common CA certificate file paths on Linux/Unix
+inline const char **system_ca_paths() {
+  static const char *paths[] = {
+      "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu
+      "/etc/pki/tls/certs/ca-bundle.crt",   // RHEL/CentOS
+      "/etc/ssl/ca-bundle.pem",             // OpenSUSE
+      "/etc/pki/tls/cacert.pem",            // OpenELEC
+      "/etc/ssl/cert.pem",                  // Alpine, FreeBSD
+      nullptr};
+  return paths;
+}
+
+// Common CA certificate directory paths on Linux/Unix
+inline const char **system_ca_dirs() {
+  static const char *dirs[] = {"/etc/ssl/certs",             // Debian/Ubuntu
+                               "/etc/pki/tls/certs",         // RHEL/CentOS
+                               "/usr/share/ca-certificates", // Other
+                               nullptr};
+  return dirs;
+}
+#endif
+
 } // namespace impl
 
 inline bool set_client_ca_file(ctx_t ctx, const char *ca_file,
@@ -16879,33 +17079,6 @@ inline int mbedtls_sni_callback(void *p_ctx, mbedtls_ssl_context *ssl,
 inline int mbedtls_verify_callback(void *data, mbedtls_x509_crt *crt,
                                    int cert_depth, uint32_t *flags);
 
-// Check if a string is an IPv4 address
-inline bool is_ipv4_address(const std::string &str) {
-  int dots = 0;
-  for (char c : str) {
-    if (c == '.') {
-      dots++;
-    } else if (!isdigit(static_cast(c))) {
-      return false;
-    }
-  }
-  return dots == 3;
-}
-
-// Parse IPv4 address string to bytes
-inline bool parse_ipv4(const std::string &str, unsigned char *out) {
-  int parts[4];
-  if (sscanf(str.c_str(), "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2],
-             &parts[3]) != 4) {
-    return false;
-  }
-  for (int i = 0; i < 4; i++) {
-    if (parts[i] < 0 || parts[i] > 255) return false;
-    out[i] = static_cast(parts[i]);
-  }
-  return true;
-}
-
 // MbedTLS verify callback wrapper
 inline int mbedtls_verify_callback(void *data, mbedtls_x509_crt *crt,
                                    int cert_depth, uint32_t *flags) {
@@ -17120,68 +17293,26 @@ inline bool load_system_certs(ctx_t ctx) {
   bool loaded = false;
 
 #ifdef _WIN32
-  // Load from Windows certificate store (ROOT and CA)
-  static const wchar_t *store_names[] = {L"ROOT", L"CA"};
-  for (auto store_name : store_names) {
-    HCERTSTORE hStore = CertOpenSystemStoreW(0, store_name);
-    if (hStore) {
-      PCCERT_CONTEXT pContext = nullptr;
-      while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) !=
-             nullptr) {
-        int ret = mbedtls_x509_crt_parse_der(
-            &mctx->ca_chain, pContext->pbCertEncoded, pContext->cbCertEncoded);
-        if (ret == 0) { loaded = true; }
-      }
-      CertCloseStore(hStore, 0);
-    }
-  }
+  loaded = impl::enumerate_windows_system_certs(
+      [&](const unsigned char *data, size_t len) {
+        return mbedtls_x509_crt_parse_der(&mctx->ca_chain, data, len) == 0;
+      });
 #elif defined(__APPLE__) && defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
-  // Load from macOS Keychain
-  CFArrayRef certs = nullptr;
-  OSStatus status = SecTrustCopyAnchorCertificates(&certs);
-  if (status == errSecSuccess && certs) {
-    CFIndex count = CFArrayGetCount(certs);
-    for (CFIndex i = 0; i < count; i++) {
-      SecCertificateRef cert =
-          (SecCertificateRef)CFArrayGetValueAtIndex(certs, i);
-      CFDataRef data = SecCertificateCopyData(cert);
-      if (data) {
-        int ret = mbedtls_x509_crt_parse_der(
-            &mctx->ca_chain, CFDataGetBytePtr(data),
-            static_cast(CFDataGetLength(data)));
-        if (ret == 0) { loaded = true; }
-        CFRelease(data);
-      }
-    }
-    CFRelease(certs);
-  }
+  loaded = impl::enumerate_macos_keychain_certs(
+      [&](const unsigned char *data, size_t len) {
+        return mbedtls_x509_crt_parse_der(&mctx->ca_chain, data, len) == 0;
+      });
 #else
-  // Try common CA certificate locations on Linux/Unix
-  static const char *ca_paths[] = {
-      "/etc/ssl/certs/ca-certificates.crt", // Debian/Ubuntu
-      "/etc/pki/tls/certs/ca-bundle.crt",   // RHEL/CentOS
-      "/etc/ssl/ca-bundle.pem",             // OpenSUSE
-      "/etc/pki/tls/cacert.pem",            // OpenELEC
-      "/etc/ssl/cert.pem",                  // Alpine, FreeBSD
-      nullptr};
-
-  for (const char **path = ca_paths; *path; ++path) {
-    int ret = mbedtls_x509_crt_parse_file(&mctx->ca_chain, *path);
-    if (ret >= 0) {
+  for (auto path = impl::system_ca_paths(); *path; ++path) {
+    if (mbedtls_x509_crt_parse_file(&mctx->ca_chain, *path) >= 0) {
       loaded = true;
       break;
     }
   }
 
-  // Also try the CA directory
   if (!loaded) {
-    static const char *ca_dirs[] = {"/etc/ssl/certs",     // Debian/Ubuntu
-                                    "/etc/pki/tls/certs", // RHEL/CentOS
-                                    "/usr/share/ca-certificates", nullptr};
-
-    for (const char **dir = ca_dirs; *dir; ++dir) {
-      int ret = mbedtls_x509_crt_parse_path(&mctx->ca_chain, *dir);
-      if (ret >= 0) {
+    for (auto dir = impl::system_ca_dirs(); *dir; ++dir) {
+      if (mbedtls_x509_crt_parse_path(&mctx->ca_chain, *dir) >= 0) {
         loaded = true;
         break;
       }
@@ -17232,6 +17363,18 @@ inline bool set_client_cert_pem(ctx_t ctx, const char *cert, const char *key,
     return false;
   }
 
+  // Verify that the certificate and private key match
+#ifdef CPPHTTPLIB_MBEDTLS_V3
+  ret = mbedtls_pk_check_pair(&mctx->own_cert.pk, &mctx->own_key,
+                              mbedtls_ctr_drbg_random, &mctx->ctr_drbg);
+#else
+  ret = mbedtls_pk_check_pair(&mctx->own_cert.pk, &mctx->own_key);
+#endif
+  if (ret != 0) {
+    impl::mbedtls_last_error() = ret;
+    return false;
+  }
+
   ret = mbedtls_ssl_conf_own_cert(&mctx->conf, &mctx->own_cert, &mctx->own_key);
   if (ret != 0) {
     impl::mbedtls_last_error() = ret;
@@ -17265,6 +17408,18 @@ inline bool set_client_cert_file(ctx_t ctx, const char *cert_path,
     return false;
   }
 
+  // Verify that the certificate and private key match
+#ifdef CPPHTTPLIB_MBEDTLS_V3
+  ret = mbedtls_pk_check_pair(&mctx->own_cert.pk, &mctx->own_key,
+                              mbedtls_ctr_drbg_random, &mctx->ctr_drbg);
+#else
+  ret = mbedtls_pk_check_pair(&mctx->own_cert.pk, &mctx->own_key);
+#endif
+  if (ret != 0) {
+    impl::mbedtls_last_error() = ret;
+    return false;
+  }
+
   ret = mbedtls_ssl_conf_own_cert(&mctx->conf, &mctx->own_cert, &mctx->own_key);
   if (ret != 0) {
     impl::mbedtls_last_error() = ret;
@@ -18026,6 +18181,1138 @@ inline std::string verify_error_string(long error_code) {
 
 #endif // CPPHTTPLIB_MBEDTLS_SUPPORT
 
+/*
+ * Group 10: TLS abstraction layer - wolfSSL backend
+ */
+
+/*
+ * wolfSSL Backend Implementation
+ */
+
+#ifdef CPPHTTPLIB_WOLFSSL_SUPPORT
+namespace tls {
+
+namespace impl {
+
+// wolfSSL session wrapper
+struct WolfSSLSession {
+  WOLFSSL *ssl = nullptr;
+  socket_t sock = INVALID_SOCKET;
+  std::string hostname;     // For client: set via set_sni
+  std::string sni_hostname; // For server: received from client via SNI callback
+
+  WolfSSLSession() = default;
+
+  ~WolfSSLSession() {
+    if (ssl) { wolfSSL_free(ssl); }
+  }
+
+  WolfSSLSession(const WolfSSLSession &) = delete;
+  WolfSSLSession &operator=(const WolfSSLSession &) = delete;
+};
+
+// Thread-local error code accessor for wolfSSL
+inline uint64_t &wolfssl_last_error() {
+  static thread_local uint64_t err = 0;
+  return err;
+}
+
+// Helper to map wolfSSL error to ErrorCode.
+// ssl_error is the value from wolfSSL_get_error().
+// raw_ret is the raw return value from the wolfSSL call (for low-level error).
+inline ErrorCode map_wolfssl_error(WOLFSSL *ssl, int ssl_error,
+                                   int &out_errno) {
+  switch (ssl_error) {
+  case SSL_ERROR_NONE: return ErrorCode::Success;
+  case SSL_ERROR_WANT_READ: return ErrorCode::WantRead;
+  case SSL_ERROR_WANT_WRITE: return ErrorCode::WantWrite;
+  case SSL_ERROR_ZERO_RETURN: return ErrorCode::PeerClosed;
+  case SSL_ERROR_SYSCALL: out_errno = errno; return ErrorCode::SyscallError;
+  default:
+    if (ssl) {
+      // wolfSSL stores the low-level error code as a negative value.
+      // DOMAIN_NAME_MISMATCH (-322) indicates hostname verification failure.
+      int low_err = ssl_error; // wolfSSL_get_error returns the low-level code
+      if (low_err == DOMAIN_NAME_MISMATCH) {
+        return ErrorCode::HostnameMismatch;
+      }
+      // Check verify result to distinguish cert verification from generic SSL
+      // errors.
+      long vr = wolfSSL_get_verify_result(ssl);
+      if (vr != 0) { return ErrorCode::CertVerifyFailed; }
+    }
+    return ErrorCode::Fatal;
+  }
+}
+
+// WolfSSLContext constructor/destructor implementations
+inline WolfSSLContext::WolfSSLContext() { wolfSSL_Init(); }
+
+inline WolfSSLContext::~WolfSSLContext() {
+  if (ctx) { wolfSSL_CTX_free(ctx); }
+}
+
+// Thread-local storage for SNI captured during handshake
+inline std::string &wolfssl_pending_sni() {
+  static thread_local std::string sni;
+  return sni;
+}
+
+// SNI callback for wolfSSL server to capture client's SNI hostname
+inline int wolfssl_sni_callback(WOLFSSL *ssl, int *ret, void *exArg) {
+  (void)ret;
+  (void)exArg;
+
+  void *name_data = nullptr;
+  unsigned short name_len =
+      wolfSSL_SNI_GetRequest(ssl, WOLFSSL_SNI_HOST_NAME, &name_data);
+
+  if (name_data && name_len > 0) {
+    wolfssl_pending_sni().assign(static_cast(name_data),
+                                 name_len);
+  } else {
+    wolfssl_pending_sni().clear();
+  }
+  return 0; // Continue regardless
+}
+
+// wolfSSL verify callback wrapper
+inline int wolfssl_verify_callback(int preverify_ok,
+                                   WOLFSSL_X509_STORE_CTX *x509_ctx) {
+  auto &callback = get_verify_callback();
+  if (!callback) { return preverify_ok; }
+
+  WOLFSSL_X509 *cert = wolfSSL_X509_STORE_CTX_get_current_cert(x509_ctx);
+  int depth = wolfSSL_X509_STORE_CTX_get_error_depth(x509_ctx);
+  int err = wolfSSL_X509_STORE_CTX_get_error(x509_ctx);
+
+  // Get the WOLFSSL object from the X509_STORE_CTX
+  WOLFSSL *ssl = static_cast(wolfSSL_X509_STORE_CTX_get_ex_data(
+      x509_ctx, wolfSSL_get_ex_data_X509_STORE_CTX_idx()));
+
+  VerifyContext verify_ctx;
+  verify_ctx.session = static_cast(ssl);
+  verify_ctx.cert = static_cast(cert);
+  verify_ctx.depth = depth;
+  verify_ctx.preverify_ok = (preverify_ok != 0);
+  verify_ctx.error_code = static_cast(err);
+
+  if (err != 0) {
+    verify_ctx.error_string = wolfSSL_X509_verify_cert_error_string(err);
+  } else {
+    verify_ctx.error_string = nullptr;
+  }
+
+  bool accepted = callback(verify_ctx);
+  return accepted ? 1 : 0;
+}
+
+inline void set_wolfssl_password_cb(WOLFSSL_CTX *ctx, const char *password) {
+  wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, const_cast(password));
+  wolfSSL_CTX_set_default_passwd_cb(
+      ctx, [](char *buf, int size, int /*rwflag*/, void *userdata) -> int {
+        auto *pwd = static_cast(userdata);
+        if (!pwd) return 0;
+        auto len = static_cast(strlen(pwd));
+        if (len > size) len = size;
+        memcpy(buf, pwd, static_cast(len));
+        return len;
+      });
+}
+
+} // namespace impl
+
+inline ctx_t create_client_context() {
+  auto ctx = new (std::nothrow) impl::WolfSSLContext();
+  if (!ctx) { return nullptr; }
+
+  ctx->is_server = false;
+
+  WOLFSSL_METHOD *method = wolfTLSv1_2_client_method();
+  if (!method) {
+    delete ctx;
+    return nullptr;
+  }
+
+  ctx->ctx = wolfSSL_CTX_new(method);
+  if (!ctx->ctx) {
+    delete ctx;
+    return nullptr;
+  }
+
+  // Default: verify peer certificate
+  wolfSSL_CTX_set_verify(ctx->ctx, SSL_VERIFY_PEER, nullptr);
+
+  return static_cast(ctx);
+}
+
+inline ctx_t create_server_context() {
+  auto ctx = new (std::nothrow) impl::WolfSSLContext();
+  if (!ctx) { return nullptr; }
+
+  ctx->is_server = true;
+
+  WOLFSSL_METHOD *method = wolfTLSv1_2_server_method();
+  if (!method) {
+    delete ctx;
+    return nullptr;
+  }
+
+  ctx->ctx = wolfSSL_CTX_new(method);
+  if (!ctx->ctx) {
+    delete ctx;
+    return nullptr;
+  }
+
+  // Default: don't verify client
+  wolfSSL_CTX_set_verify(ctx->ctx, SSL_VERIFY_NONE, nullptr);
+
+  // Enable SNI on server
+  wolfSSL_CTX_SNI_SetOptions(ctx->ctx, WOLFSSL_SNI_HOST_NAME,
+                             WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
+
+  return static_cast(ctx);
+}
+
+inline void free_context(ctx_t ctx) {
+  if (ctx) { delete static_cast(ctx); }
+}
+
+inline bool set_min_version(ctx_t ctx, Version version) {
+  if (!ctx) { return false; }
+  auto wctx = static_cast(ctx);
+
+  int min_ver = WOLFSSL_TLSV1_2;
+  if (version >= Version::TLS1_3) { min_ver = WOLFSSL_TLSV1_3; }
+
+  return wolfSSL_CTX_SetMinVersion(wctx->ctx, min_ver) == WOLFSSL_SUCCESS;
+}
+
+inline bool load_ca_pem(ctx_t ctx, const char *pem, size_t len) {
+  if (!ctx || !pem) { return false; }
+  auto wctx = static_cast(ctx);
+
+  int ret = wolfSSL_CTX_load_verify_buffer(
+      wctx->ctx, reinterpret_cast(pem),
+      static_cast(len), SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+  wctx->ca_pem_data_.append(pem, len);
+  return true;
+}
+
+inline bool load_ca_file(ctx_t ctx, const char *file_path) {
+  if (!ctx || !file_path) { return false; }
+  auto wctx = static_cast(ctx);
+
+  int ret = wolfSSL_CTX_load_verify_locations(wctx->ctx, file_path, nullptr);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+  return true;
+}
+
+inline bool load_ca_dir(ctx_t ctx, const char *dir_path) {
+  if (!ctx || !dir_path) { return false; }
+  auto wctx = static_cast(ctx);
+
+  int ret = wolfSSL_CTX_load_verify_locations(wctx->ctx, nullptr, dir_path);
+  // wolfSSL may fail if the directory doesn't contain properly hashed certs.
+  // Unlike OpenSSL which lazily loads certs from directories, wolfSSL scans
+  // immediately. Return true even on failure since the CA file may have
+  // already been loaded, matching OpenSSL's lenient behavior.
+  (void)ret;
+  return true;
+}
+
+inline bool load_system_certs(ctx_t ctx) {
+  if (!ctx) { return false; }
+  auto wctx = static_cast(ctx);
+  bool loaded = false;
+
+#ifdef _WIN32
+  loaded = impl::enumerate_windows_system_certs(
+      [&](const unsigned char *data, size_t len) {
+        return wolfSSL_CTX_load_verify_buffer(wctx->ctx, data,
+                                              static_cast(len),
+                                              SSL_FILETYPE_ASN1) == SSL_SUCCESS;
+      });
+#elif defined(__APPLE__) && defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
+  loaded = impl::enumerate_macos_keychain_certs(
+      [&](const unsigned char *data, size_t len) {
+        return wolfSSL_CTX_load_verify_buffer(wctx->ctx, data,
+                                              static_cast(len),
+                                              SSL_FILETYPE_ASN1) == SSL_SUCCESS;
+      });
+#else
+  for (auto path = impl::system_ca_paths(); *path; ++path) {
+    if (wolfSSL_CTX_load_verify_locations(wctx->ctx, *path, nullptr) ==
+        SSL_SUCCESS) {
+      loaded = true;
+      break;
+    }
+  }
+
+  if (!loaded) {
+    for (auto dir = impl::system_ca_dirs(); *dir; ++dir) {
+      if (wolfSSL_CTX_load_verify_locations(wctx->ctx, nullptr, *dir) ==
+          SSL_SUCCESS) {
+        loaded = true;
+        break;
+      }
+    }
+  }
+#endif
+
+  return loaded;
+}
+
+inline bool set_client_cert_pem(ctx_t ctx, const char *cert, const char *key,
+                                const char *password) {
+  if (!ctx || !cert || !key) { return false; }
+  auto wctx = static_cast(ctx);
+
+  // Load certificate
+  int ret = wolfSSL_CTX_use_certificate_buffer(
+      wctx->ctx, reinterpret_cast(cert),
+      static_cast(strlen(cert)), SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  // Set password callback if password is provided
+  if (password) { impl::set_wolfssl_password_cb(wctx->ctx, password); }
+
+  // Load private key
+  ret = wolfSSL_CTX_use_PrivateKey_buffer(
+      wctx->ctx, reinterpret_cast(key),
+      static_cast(strlen(key)), SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  // Verify that the certificate and private key match
+  return wolfSSL_CTX_check_private_key(wctx->ctx) == SSL_SUCCESS;
+}
+
+inline bool set_client_cert_file(ctx_t ctx, const char *cert_path,
+                                 const char *key_path, const char *password) {
+  if (!ctx || !cert_path || !key_path) { return false; }
+  auto wctx = static_cast(ctx);
+
+  // Load certificate file
+  int ret =
+      wolfSSL_CTX_use_certificate_file(wctx->ctx, cert_path, SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  // Set password callback if password is provided
+  if (password) { impl::set_wolfssl_password_cb(wctx->ctx, password); }
+
+  // Load private key file
+  ret = wolfSSL_CTX_use_PrivateKey_file(wctx->ctx, key_path, SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  // Verify that the certificate and private key match
+  return wolfSSL_CTX_check_private_key(wctx->ctx) == SSL_SUCCESS;
+}
+
+inline void set_verify_client(ctx_t ctx, bool require) {
+  if (!ctx) { return; }
+  auto wctx = static_cast(ctx);
+  wctx->verify_client = require;
+  if (require) {
+    wolfSSL_CTX_set_verify(
+        wctx->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
+        wctx->has_verify_callback ? impl::wolfssl_verify_callback : nullptr);
+  } else {
+    if (wctx->has_verify_callback) {
+      wolfSSL_CTX_set_verify(wctx->ctx, SSL_VERIFY_PEER,
+                             impl::wolfssl_verify_callback);
+    } else {
+      wolfSSL_CTX_set_verify(wctx->ctx, SSL_VERIFY_NONE, nullptr);
+    }
+  }
+}
+
+inline session_t create_session(ctx_t ctx, socket_t sock) {
+  if (!ctx || sock == INVALID_SOCKET) { return nullptr; }
+  auto wctx = static_cast(ctx);
+
+  auto session = new (std::nothrow) impl::WolfSSLSession();
+  if (!session) { return nullptr; }
+
+  session->sock = sock;
+  session->ssl = wolfSSL_new(wctx->ctx);
+  if (!session->ssl) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    delete session;
+    return nullptr;
+  }
+
+  wolfSSL_set_fd(session->ssl, static_cast(sock));
+
+  // Set up SNI callback for server
+  if (wctx->is_server) {
+    wolfSSL_CTX_set_servername_callback(wctx->ctx, impl::wolfssl_sni_callback);
+  }
+
+  return static_cast(session);
+}
+
+inline void free_session(session_t session) {
+  if (session) { delete static_cast(session); }
+}
+
+inline bool set_sni(session_t session, const char *hostname) {
+  if (!session || !hostname) { return false; }
+  auto wsession = static_cast(session);
+
+  int ret = wolfSSL_UseSNI(wsession->ssl, WOLFSSL_SNI_HOST_NAME, hostname,
+                           static_cast(strlen(hostname)));
+  if (ret != WOLFSSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  // Also set hostname for verification
+  wolfSSL_check_domain_name(wsession->ssl, hostname);
+
+  wsession->hostname = hostname;
+  return true;
+}
+
+inline bool set_hostname(session_t session, const char *hostname) {
+  // In wolfSSL, set_hostname also sets up hostname verification
+  return set_sni(session, hostname);
+}
+
+inline TlsError connect(session_t session) {
+  TlsError err;
+  if (!session) {
+    err.code = ErrorCode::Fatal;
+    return err;
+  }
+
+  auto wsession = static_cast(session);
+  int ret = wolfSSL_connect(wsession->ssl);
+
+  if (ret == SSL_SUCCESS) {
+    err.code = ErrorCode::Success;
+  } else {
+    int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+    err.code = impl::map_wolfssl_error(wsession->ssl, ssl_error, err.sys_errno);
+    err.backend_code = static_cast(ssl_error);
+    impl::wolfssl_last_error() = err.backend_code;
+  }
+
+  return err;
+}
+
+inline TlsError accept(session_t session) {
+  TlsError err;
+  if (!session) {
+    err.code = ErrorCode::Fatal;
+    return err;
+  }
+
+  auto wsession = static_cast(session);
+  int ret = wolfSSL_accept(wsession->ssl);
+
+  if (ret == SSL_SUCCESS) {
+    err.code = ErrorCode::Success;
+    // Capture SNI from thread-local storage after successful handshake
+    wsession->sni_hostname = std::move(impl::wolfssl_pending_sni());
+    impl::wolfssl_pending_sni().clear();
+  } else {
+    int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+    err.code = impl::map_wolfssl_error(wsession->ssl, ssl_error, err.sys_errno);
+    err.backend_code = static_cast(ssl_error);
+    impl::wolfssl_last_error() = err.backend_code;
+  }
+
+  return err;
+}
+
+inline bool connect_nonblocking(session_t session, socket_t sock,
+                                time_t timeout_sec, time_t timeout_usec,
+                                TlsError *err) {
+  if (!session) {
+    if (err) { err->code = ErrorCode::Fatal; }
+    return false;
+  }
+
+  auto wsession = static_cast(session);
+
+  // Set socket to non-blocking mode
+  detail::set_nonblocking(sock, true);
+  auto cleanup =
+      detail::scope_exit([&]() { detail::set_nonblocking(sock, false); });
+
+  int ret;
+  while ((ret = wolfSSL_connect(wsession->ssl)) != SSL_SUCCESS) {
+    int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+    if (ssl_error == SSL_ERROR_WANT_READ) {
+      if (detail::select_read(sock, timeout_sec, timeout_usec) > 0) {
+        continue;
+      }
+    } else if (ssl_error == SSL_ERROR_WANT_WRITE) {
+      if (detail::select_write(sock, timeout_sec, timeout_usec) > 0) {
+        continue;
+      }
+    }
+
+    // Error or timeout
+    if (err) {
+      err->code =
+          impl::map_wolfssl_error(wsession->ssl, ssl_error, err->sys_errno);
+      err->backend_code = static_cast(ssl_error);
+    }
+    impl::wolfssl_last_error() = static_cast(ssl_error);
+    return false;
+  }
+
+  if (err) { err->code = ErrorCode::Success; }
+  return true;
+}
+
+inline bool accept_nonblocking(session_t session, socket_t sock,
+                               time_t timeout_sec, time_t timeout_usec,
+                               TlsError *err) {
+  if (!session) {
+    if (err) { err->code = ErrorCode::Fatal; }
+    return false;
+  }
+
+  auto wsession = static_cast(session);
+
+  // Set socket to non-blocking mode
+  detail::set_nonblocking(sock, true);
+  auto cleanup =
+      detail::scope_exit([&]() { detail::set_nonblocking(sock, false); });
+
+  int ret;
+  while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) {
+    int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+    if (ssl_error == SSL_ERROR_WANT_READ) {
+      if (detail::select_read(sock, timeout_sec, timeout_usec) > 0) {
+        continue;
+      }
+    } else if (ssl_error == SSL_ERROR_WANT_WRITE) {
+      if (detail::select_write(sock, timeout_sec, timeout_usec) > 0) {
+        continue;
+      }
+    }
+
+    // Error or timeout
+    if (err) {
+      err->code =
+          impl::map_wolfssl_error(wsession->ssl, ssl_error, err->sys_errno);
+      err->backend_code = static_cast(ssl_error);
+    }
+    impl::wolfssl_last_error() = static_cast(ssl_error);
+    return false;
+  }
+
+  if (err) { err->code = ErrorCode::Success; }
+
+  // Capture SNI from thread-local storage after successful handshake
+  wsession->sni_hostname = std::move(impl::wolfssl_pending_sni());
+  impl::wolfssl_pending_sni().clear();
+
+  return true;
+}
+
+inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) {
+  if (!session || !buf) {
+    err.code = ErrorCode::Fatal;
+    return -1;
+  }
+
+  auto wsession = static_cast(session);
+  int ret = wolfSSL_read(wsession->ssl, buf, static_cast(len));
+
+  if (ret > 0) {
+    err.code = ErrorCode::Success;
+    return static_cast(ret);
+  }
+
+  if (ret == 0) {
+    err.code = ErrorCode::PeerClosed;
+    return 0;
+  }
+
+  int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+  err.code = impl::map_wolfssl_error(wsession->ssl, ssl_error, err.sys_errno);
+  err.backend_code = static_cast(ssl_error);
+  impl::wolfssl_last_error() = err.backend_code;
+  return -1;
+}
+
+inline ssize_t write(session_t session, const void *buf, size_t len,
+                     TlsError &err) {
+  if (!session || !buf) {
+    err.code = ErrorCode::Fatal;
+    return -1;
+  }
+
+  auto wsession = static_cast(session);
+  int ret = wolfSSL_write(wsession->ssl, buf, static_cast(len));
+
+  if (ret > 0) {
+    err.code = ErrorCode::Success;
+    return static_cast(ret);
+  }
+
+  // wolfSSL_write returns 0 when the peer has sent a close_notify.
+  // Treat this as an error (return -1) so callers don't spin in a
+  // write loop adding zero to the offset.
+  if (ret == 0) {
+    err.code = ErrorCode::PeerClosed;
+    return -1;
+  }
+
+  int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+  err.code = impl::map_wolfssl_error(wsession->ssl, ssl_error, err.sys_errno);
+  err.backend_code = static_cast(ssl_error);
+  impl::wolfssl_last_error() = err.backend_code;
+  return -1;
+}
+
+inline int pending(const_session_t session) {
+  if (!session) { return 0; }
+  auto wsession =
+      static_cast(const_cast(session));
+  return wolfSSL_pending(wsession->ssl);
+}
+
+inline void shutdown(session_t session, bool graceful) {
+  if (!session) { return; }
+  auto wsession = static_cast(session);
+
+  if (graceful) {
+    int ret;
+    int attempts = 0;
+    while ((ret = wolfSSL_shutdown(wsession->ssl)) != SSL_SUCCESS &&
+           attempts < 3) {
+      int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+      if (ssl_error != SSL_ERROR_WANT_READ &&
+          ssl_error != SSL_ERROR_WANT_WRITE) {
+        break;
+      }
+      attempts++;
+    }
+  } else {
+    wolfSSL_shutdown(wsession->ssl);
+  }
+}
+
+inline bool is_peer_closed(session_t session, socket_t sock) {
+  if (!session || sock == INVALID_SOCKET) { return true; }
+  auto wsession = static_cast(session);
+
+  // Check if there's already decrypted data available
+  if (wolfSSL_pending(wsession->ssl) > 0) { return false; }
+
+  // Set socket to non-blocking to avoid blocking on read
+  detail::set_nonblocking(sock, true);
+  auto cleanup =
+      detail::scope_exit([&]() { detail::set_nonblocking(sock, false); });
+
+  // Try a 1-byte read to check connection status
+  unsigned char buf;
+  int ret = wolfSSL_read(wsession->ssl, &buf, 1);
+
+  // If we got data or WANT_READ (would block), connection is alive
+  if (ret > 0) { return false; }
+
+  int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
+  if (ssl_error == SSL_ERROR_WANT_READ) { return false; }
+
+  return ssl_error == SSL_ERROR_ZERO_RETURN || ssl_error == SSL_ERROR_SYSCALL ||
+         ret == 0;
+}
+
+inline cert_t get_peer_cert(const_session_t session) {
+  if (!session) { return nullptr; }
+  auto wsession =
+      static_cast(const_cast(session));
+
+  WOLFSSL_X509 *cert = wolfSSL_get_peer_certificate(wsession->ssl);
+  return static_cast(cert);
+}
+
+inline void free_cert(cert_t cert) {
+  if (cert) { wolfSSL_X509_free(static_cast(cert)); }
+}
+
+inline bool verify_hostname(cert_t cert, const char *hostname) {
+  if (!cert || !hostname) { return false; }
+  auto x509 = static_cast(cert);
+  std::string host_str(hostname);
+
+  // Check if hostname is an IP address
+  bool is_ip = impl::is_ipv4_address(host_str);
+  unsigned char ip_bytes[4];
+  if (is_ip) { impl::parse_ipv4(host_str, ip_bytes); }
+
+  // Check Subject Alternative Names
+  auto *san_names = static_cast(
+      wolfSSL_X509_get_ext_d2i(x509, NID_subject_alt_name, nullptr, nullptr));
+
+  if (san_names) {
+    int san_count = wolfSSL_sk_num(san_names);
+    for (int i = 0; i < san_count; i++) {
+      auto *names =
+          static_cast(wolfSSL_sk_value(san_names, i));
+      if (!names) continue;
+
+      if (!is_ip && names->type == WOLFSSL_GEN_DNS) {
+        // DNS name
+        unsigned char *dns_name = nullptr;
+        int dns_len = wolfSSL_ASN1_STRING_to_UTF8(&dns_name, names->d.dNSName);
+        if (dns_name && dns_len > 0) {
+          std::string san_name(reinterpret_cast(dns_name),
+                               static_cast(dns_len));
+          XFREE(dns_name, nullptr, DYNAMIC_TYPE_OPENSSL);
+          if (detail::match_hostname(san_name, host_str)) {
+            wolfSSL_sk_free(san_names);
+            return true;
+          }
+        }
+      } else if (is_ip && names->type == WOLFSSL_GEN_IPADD) {
+        // IP address
+        unsigned char *ip_data = wolfSSL_ASN1_STRING_data(names->d.iPAddress);
+        int ip_len = wolfSSL_ASN1_STRING_length(names->d.iPAddress);
+        if (ip_data && ip_len == 4 && memcmp(ip_data, ip_bytes, 4) == 0) {
+          wolfSSL_sk_free(san_names);
+          return true;
+        }
+      }
+    }
+    wolfSSL_sk_free(san_names);
+  }
+
+  // Fallback: Check Common Name (CN) in subject
+  WOLFSSL_X509_NAME *subject = wolfSSL_X509_get_subject_name(x509);
+  if (subject) {
+    char cn[256] = {};
+    int cn_len = wolfSSL_X509_NAME_get_text_by_NID(subject, NID_commonName, cn,
+                                                   sizeof(cn));
+    if (cn_len > 0) {
+      std::string cn_str(cn, static_cast(cn_len));
+      if (detail::match_hostname(cn_str, host_str)) { return true; }
+    }
+  }
+
+  return false;
+}
+
+inline uint64_t hostname_mismatch_code() {
+  return static_cast(DOMAIN_NAME_MISMATCH);
+}
+
+inline long get_verify_result(const_session_t session) {
+  if (!session) { return -1; }
+  auto wsession =
+      static_cast(const_cast(session));
+  long result = wolfSSL_get_verify_result(wsession->ssl);
+  return result;
+}
+
+inline std::string get_cert_subject_cn(cert_t cert) {
+  if (!cert) return "";
+  auto x509 = static_cast(cert);
+
+  WOLFSSL_X509_NAME *subject = wolfSSL_X509_get_subject_name(x509);
+  if (!subject) return "";
+
+  char cn[256] = {};
+  int cn_len = wolfSSL_X509_NAME_get_text_by_NID(subject, NID_commonName, cn,
+                                                 sizeof(cn));
+  if (cn_len <= 0) return "";
+  return std::string(cn, static_cast(cn_len));
+}
+
+inline std::string get_cert_issuer_name(cert_t cert) {
+  if (!cert) return "";
+  auto x509 = static_cast(cert);
+
+  WOLFSSL_X509_NAME *issuer = wolfSSL_X509_get_issuer_name(x509);
+  if (!issuer) return "";
+
+  char *name_str = wolfSSL_X509_NAME_oneline(issuer, nullptr, 0);
+  if (!name_str) return "";
+
+  std::string result(name_str);
+  XFREE(name_str, nullptr, DYNAMIC_TYPE_OPENSSL);
+  return result;
+}
+
+inline bool get_cert_sans(cert_t cert, std::vector &sans) {
+  sans.clear();
+  if (!cert) return false;
+  auto x509 = static_cast(cert);
+
+  auto *san_names = static_cast(
+      wolfSSL_X509_get_ext_d2i(x509, NID_subject_alt_name, nullptr, nullptr));
+  if (!san_names) return true; // No SANs is not an error
+
+  int count = wolfSSL_sk_num(san_names);
+  for (int i = 0; i < count; i++) {
+    auto *name =
+        static_cast(wolfSSL_sk_value(san_names, i));
+    if (!name) continue;
+
+    SanEntry entry;
+    switch (name->type) {
+    case WOLFSSL_GEN_DNS: {
+      entry.type = SanType::DNS;
+      unsigned char *dns_name = nullptr;
+      int dns_len = wolfSSL_ASN1_STRING_to_UTF8(&dns_name, name->d.dNSName);
+      if (dns_name && dns_len > 0) {
+        entry.value = std::string(reinterpret_cast(dns_name),
+                                  static_cast(dns_len));
+        XFREE(dns_name, nullptr, DYNAMIC_TYPE_OPENSSL);
+      }
+      break;
+    }
+    case WOLFSSL_GEN_IPADD: {
+      entry.type = SanType::IP;
+      unsigned char *ip_data = wolfSSL_ASN1_STRING_data(name->d.iPAddress);
+      int ip_len = wolfSSL_ASN1_STRING_length(name->d.iPAddress);
+      if (ip_data && ip_len == 4) {
+        char buf[16];
+        snprintf(buf, sizeof(buf), "%d.%d.%d.%d", ip_data[0], ip_data[1],
+                 ip_data[2], ip_data[3]);
+        entry.value = buf;
+      } else if (ip_data && ip_len == 16) {
+        char buf[64];
+        snprintf(buf, sizeof(buf),
+                 "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
+                 "%02x%02x:%02x%02x:%02x%02x:%02x%02x",
+                 ip_data[0], ip_data[1], ip_data[2], ip_data[3], ip_data[4],
+                 ip_data[5], ip_data[6], ip_data[7], ip_data[8], ip_data[9],
+                 ip_data[10], ip_data[11], ip_data[12], ip_data[13],
+                 ip_data[14], ip_data[15]);
+        entry.value = buf;
+      }
+      break;
+    }
+    case WOLFSSL_GEN_EMAIL:
+      entry.type = SanType::EMAIL;
+      {
+        unsigned char *email = nullptr;
+        int email_len = wolfSSL_ASN1_STRING_to_UTF8(&email, name->d.rfc822Name);
+        if (email && email_len > 0) {
+          entry.value = std::string(reinterpret_cast(email),
+                                    static_cast(email_len));
+          XFREE(email, nullptr, DYNAMIC_TYPE_OPENSSL);
+        }
+      }
+      break;
+    case WOLFSSL_GEN_URI:
+      entry.type = SanType::URI;
+      {
+        unsigned char *uri = nullptr;
+        int uri_len = wolfSSL_ASN1_STRING_to_UTF8(
+            &uri, name->d.uniformResourceIdentifier);
+        if (uri && uri_len > 0) {
+          entry.value = std::string(reinterpret_cast(uri),
+                                    static_cast(uri_len));
+          XFREE(uri, nullptr, DYNAMIC_TYPE_OPENSSL);
+        }
+      }
+      break;
+    default: entry.type = SanType::OTHER; break;
+    }
+
+    if (!entry.value.empty()) { sans.push_back(std::move(entry)); }
+  }
+  wolfSSL_sk_free(san_names);
+  return true;
+}
+
+inline bool get_cert_validity(cert_t cert, time_t ¬_before,
+                              time_t ¬_after) {
+  if (!cert) return false;
+  auto x509 = static_cast(cert);
+
+  const WOLFSSL_ASN1_TIME *nb = wolfSSL_X509_get_notBefore(x509);
+  const WOLFSSL_ASN1_TIME *na = wolfSSL_X509_get_notAfter(x509);
+
+  if (!nb || !na) return false;
+
+  // wolfSSL_ASN1_TIME_to_tm is available
+  struct tm tm_nb = {}, tm_na = {};
+  if (wolfSSL_ASN1_TIME_to_tm(nb, &tm_nb) != WOLFSSL_SUCCESS) return false;
+  if (wolfSSL_ASN1_TIME_to_tm(na, &tm_na) != WOLFSSL_SUCCESS) return false;
+
+#ifdef _WIN32
+  not_before = _mkgmtime(&tm_nb);
+  not_after = _mkgmtime(&tm_na);
+#else
+  not_before = timegm(&tm_nb);
+  not_after = timegm(&tm_na);
+#endif
+  return true;
+}
+
+inline std::string get_cert_serial(cert_t cert) {
+  if (!cert) return "";
+  auto x509 = static_cast(cert);
+
+  WOLFSSL_ASN1_INTEGER *serial_asn1 = wolfSSL_X509_get_serialNumber(x509);
+  if (!serial_asn1) return "";
+
+  // Get the serial number data
+  int len = serial_asn1->length;
+  unsigned char *data = serial_asn1->data;
+  if (!data || len <= 0) return "";
+
+  std::string result;
+  result.reserve(static_cast(len) * 2);
+  for (int i = 0; i < len; i++) {
+    char hex[3];
+    snprintf(hex, sizeof(hex), "%02X", data[i]);
+    result += hex;
+  }
+  return result;
+}
+
+inline bool get_cert_der(cert_t cert, std::vector &der) {
+  if (!cert) return false;
+  auto x509 = static_cast(cert);
+
+  int der_len = 0;
+  const unsigned char *der_data = wolfSSL_X509_get_der(x509, &der_len);
+  if (!der_data || der_len <= 0) return false;
+
+  der.assign(der_data, der_data + der_len);
+  return true;
+}
+
+inline const char *get_sni(const_session_t session) {
+  if (!session) return nullptr;
+  auto wsession = static_cast(session);
+
+  // For server: return SNI received from client during handshake
+  if (!wsession->sni_hostname.empty()) {
+    return wsession->sni_hostname.c_str();
+  }
+
+  // For client: return the hostname set via set_sni
+  if (!wsession->hostname.empty()) { return wsession->hostname.c_str(); }
+
+  return nullptr;
+}
+
+inline uint64_t peek_error() {
+  return static_cast(wolfSSL_ERR_peek_last_error());
+}
+
+inline uint64_t get_error() {
+  uint64_t err = impl::wolfssl_last_error();
+  impl::wolfssl_last_error() = 0;
+  return err;
+}
+
+inline std::string error_string(uint64_t code) {
+  char buf[256];
+  wolfSSL_ERR_error_string(static_cast(code), buf);
+  return std::string(buf);
+}
+
+inline ca_store_t create_ca_store(const char *pem, size_t len) {
+  if (!pem || len == 0) { return nullptr; }
+  // Validate by attempting to load into a temporary ctx
+  WOLFSSL_CTX *tmp_ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
+  if (!tmp_ctx) { return nullptr; }
+  int ret = wolfSSL_CTX_load_verify_buffer(
+      tmp_ctx, reinterpret_cast(pem),
+      static_cast(len), SSL_FILETYPE_PEM);
+  wolfSSL_CTX_free(tmp_ctx);
+  if (ret != SSL_SUCCESS) { return nullptr; }
+  return static_cast(
+      new impl::WolfSSLCAStore{std::string(pem, len)});
+}
+
+inline void free_ca_store(ca_store_t store) {
+  delete static_cast(store);
+}
+
+inline bool set_ca_store(ctx_t ctx, ca_store_t store) {
+  if (!ctx || !store) { return false; }
+  auto *wctx = static_cast(ctx);
+  auto *ca = static_cast(store);
+  int ret = wolfSSL_CTX_load_verify_buffer(
+      wctx->ctx, reinterpret_cast(ca->pem_data.data()),
+      static_cast(ca->pem_data.size()), SSL_FILETYPE_PEM);
+  if (ret == SSL_SUCCESS) { wctx->ca_pem_data_ += ca->pem_data; }
+  return ret == SSL_SUCCESS;
+}
+
+inline size_t get_ca_certs(ctx_t ctx, std::vector &certs) {
+  certs.clear();
+  if (!ctx) { return 0; }
+  auto *wctx = static_cast(ctx);
+  if (wctx->ca_pem_data_.empty()) { return 0; }
+
+  const std::string &pem = wctx->ca_pem_data_;
+  const std::string begin_marker = "-----BEGIN CERTIFICATE-----";
+  const std::string end_marker = "-----END CERTIFICATE-----";
+  size_t pos = 0;
+  while ((pos = pem.find(begin_marker, pos)) != std::string::npos) {
+    size_t end_pos = pem.find(end_marker, pos);
+    if (end_pos == std::string::npos) { break; }
+    end_pos += end_marker.size();
+    std::string cert_pem = pem.substr(pos, end_pos - pos);
+    WOLFSSL_X509 *x509 = wolfSSL_X509_load_certificate_buffer(
+        reinterpret_cast(cert_pem.data()),
+        static_cast(cert_pem.size()), WOLFSSL_FILETYPE_PEM);
+    if (x509) { certs.push_back(static_cast(x509)); }
+    pos = end_pos;
+  }
+  return certs.size();
+}
+
+inline std::vector get_ca_names(ctx_t ctx) {
+  std::vector names;
+  if (!ctx) { return names; }
+  auto *wctx = static_cast(ctx);
+  if (wctx->ca_pem_data_.empty()) { return names; }
+
+  const std::string &pem = wctx->ca_pem_data_;
+  const std::string begin_marker = "-----BEGIN CERTIFICATE-----";
+  const std::string end_marker = "-----END CERTIFICATE-----";
+  size_t pos = 0;
+  while ((pos = pem.find(begin_marker, pos)) != std::string::npos) {
+    size_t end_pos = pem.find(end_marker, pos);
+    if (end_pos == std::string::npos) { break; }
+    end_pos += end_marker.size();
+    std::string cert_pem = pem.substr(pos, end_pos - pos);
+    WOLFSSL_X509 *x509 = wolfSSL_X509_load_certificate_buffer(
+        reinterpret_cast(cert_pem.data()),
+        static_cast(cert_pem.size()), WOLFSSL_FILETYPE_PEM);
+    if (x509) {
+      WOLFSSL_X509_NAME *subject = wolfSSL_X509_get_subject_name(x509);
+      if (subject) {
+        char *name_str = wolfSSL_X509_NAME_oneline(subject, nullptr, 0);
+        if (name_str) {
+          names.push_back(name_str);
+          XFREE(name_str, nullptr, DYNAMIC_TYPE_OPENSSL);
+        }
+      }
+      wolfSSL_X509_free(x509);
+    }
+    pos = end_pos;
+  }
+  return names;
+}
+
+inline bool update_server_cert(ctx_t ctx, const char *cert_pem,
+                               const char *key_pem, const char *password) {
+  if (!ctx || !cert_pem || !key_pem) { return false; }
+  auto *wctx = static_cast(ctx);
+
+  // Load new certificate
+  int ret = wolfSSL_CTX_use_certificate_buffer(
+      wctx->ctx, reinterpret_cast(cert_pem),
+      static_cast(strlen(cert_pem)), SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  // Set password if provided
+  if (password) { impl::set_wolfssl_password_cb(wctx->ctx, password); }
+
+  // Load new private key
+  ret = wolfSSL_CTX_use_PrivateKey_buffer(
+      wctx->ctx, reinterpret_cast(key_pem),
+      static_cast(strlen(key_pem)), SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+
+  return true;
+}
+
+inline bool update_server_client_ca(ctx_t ctx, const char *ca_pem) {
+  if (!ctx || !ca_pem) { return false; }
+  auto *wctx = static_cast(ctx);
+
+  int ret = wolfSSL_CTX_load_verify_buffer(
+      wctx->ctx, reinterpret_cast(ca_pem),
+      static_cast(strlen(ca_pem)), SSL_FILETYPE_PEM);
+  if (ret != SSL_SUCCESS) {
+    impl::wolfssl_last_error() =
+        static_cast(wolfSSL_ERR_peek_last_error());
+    return false;
+  }
+  return true;
+}
+
+inline bool set_verify_callback(ctx_t ctx, VerifyCallback callback) {
+  if (!ctx) { return false; }
+  auto *wctx = static_cast(ctx);
+
+  impl::get_verify_callback() = std::move(callback);
+  wctx->has_verify_callback = static_cast(impl::get_verify_callback());
+
+  if (wctx->has_verify_callback) {
+    wolfSSL_CTX_set_verify(wctx->ctx, SSL_VERIFY_PEER,
+                           impl::wolfssl_verify_callback);
+  } else {
+    wolfSSL_CTX_set_verify(
+        wctx->ctx,
+        wctx->verify_client
+            ? (SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
+            : SSL_VERIFY_NONE,
+        nullptr);
+  }
+  return true;
+}
+
+inline long get_verify_error(const_session_t session) {
+  if (!session) { return -1; }
+  auto *wsession =
+      static_cast(const_cast(session));
+  return wolfSSL_get_verify_result(wsession->ssl);
+}
+
+inline std::string verify_error_string(long error_code) {
+  if (error_code == 0) { return ""; }
+  const char *str =
+      wolfSSL_X509_verify_cert_error_string(static_cast(error_code));
+  return str ? std::string(str) : std::string();
+}
+
+} // namespace tls
+
+#endif // CPPHTTPLIB_WOLFSSL_SUPPORT
+
 // WebSocket implementation
 namespace ws {
 
diff --git a/justfile b/justfile
index 15dffcba9c..321365fa11 100644
--- a/justfile
+++ b/justfile
@@ -6,7 +6,7 @@ list:
     @just --list --unsorted
 
 openssl:
-    @(cd test && make test && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test)
+    @(cd test && LSAN_OPTIONS=suppressions=lsan_suppressions.txt make)
     @(cd test && make proxy)
 
 openssl_parallel:
@@ -19,24 +19,25 @@ mbedtls:
 mbedtls_parallel:
     @(cd test && make test_mbedtls_parallel)
 
+wolfssl:
+    @(cd test && make test_wolfssl && LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test_wolfssl)
+    @(cd test && make proxy_wolfssl)
+
+wolfssl_parallel:
+    @(cd test && make test_wolfssl_parallel)
+
 no_tls:
     @(cd test && make test_no_tls && ./test_no_tls)
 
 no_tls_parallel:
     @(cd test && make test_no_tls_parallel)
 
-fuzz:
+others:
     @(cd test && make fuzz_test)
-
-proxy:
-    @(cd test && make proxy)
-
-websocket_heartbeat:
     @(cd test && make test_websocket_heartbeat && ./test_websocket_heartbeat)
-
-thread_pool:
     @(cd test && make test_thread_pool && ./test_thread_pool)
 
 build:
     @(cd test && make test_split)
     @(cd test && make test_split_mbedtls)
+    @(cd test && make test_split_wolfssl)
diff --git a/test/Makefile b/test/Makefile
index be9e567a9f..4060466c0b 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -11,9 +11,13 @@ ifneq ($(OS), Windows_NT)
 		MBEDTLS_DIR ?= $(shell brew --prefix mbedtls@3)
 		MBEDTLS_SUPPORT = -DCPPHTTPLIB_MBEDTLS_SUPPORT -I$(MBEDTLS_DIR)/include -L$(MBEDTLS_DIR)/lib -lmbedtls -lmbedx509 -lmbedcrypto
 		MBEDTLS_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security
+		WOLFSSL_DIR ?= $(shell brew --prefix wolfssl)
+		WOLFSSL_SUPPORT = -DCPPHTTPLIB_WOLFSSL_SUPPORT -I$(WOLFSSL_DIR)/include -I$(WOLFSSL_DIR)/include/wolfssl -L$(WOLFSSL_DIR)/lib -lwolfssl
+		WOLFSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security
 	else
 		OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -lssl -lcrypto
 		MBEDTLS_SUPPORT = -DCPPHTTPLIB_MBEDTLS_SUPPORT -lmbedtls -lmbedx509 -lmbedcrypto
+		WOLFSSL_SUPPORT = -DCPPHTTPLIB_WOLFSSL_SUPPORT -lwolfssl
 	endif
 endif
 
@@ -38,6 +42,7 @@ endif
 
 TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
 TEST_ARGS_MBEDTLS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(MBEDTLS_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
+TEST_ARGS_WOLFSSL = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(WOLFSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
 TEST_ARGS_NO_TLS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
 
 # By default, use standalone_fuzz_target_runner.
@@ -81,7 +86,7 @@ define run_parallel
 	echo "All shards passed."
 endef
 
-.PHONY: test_openssl_parallel test_mbedtls_parallel test_no_tls_parallel
+.PHONY: test_openssl_parallel test_mbedtls_parallel test_wolfssl_parallel test_no_tls_parallel
 
 test_openssl_parallel : test
 	$(call run_parallel,test)
@@ -89,6 +94,9 @@ test_openssl_parallel : test
 test_mbedtls_parallel : test_mbedtls
 	$(call run_parallel,test_mbedtls)
 
+test_wolfssl_parallel : test_wolfssl
+	$(call run_parallel,test_wolfssl)
+
 test_no_tls_parallel : test_no_tls
 	$(call run_parallel,test_no_tls)
 
@@ -130,6 +138,25 @@ proxy_mbedtls : test_proxy_mbedtls
 	cd proxy && docker compose down; \
 	exit $$exit_code
 
+proxy_wolfssl : test_proxy_wolfssl
+	@echo "Starting proxy server..."
+	cd proxy && \
+	docker compose up -d
+	@echo "Waiting for proxy to be ready..."
+	@until nc -z localhost 3128 && nc -z localhost 3129; do sleep 1; done
+	@echo "Proxy servers are ready, waiting additional 5 seconds for full startup..."
+	@sleep 5
+	@echo "Checking proxy server status..."
+	@cd proxy && docker compose ps
+	@echo "Checking proxy server logs..."
+	@cd proxy && docker compose logs --tail=20
+	@echo "Running proxy tests (wolfSSL)..."
+	./test_proxy_wolfssl; \
+	exit_code=$$?; \
+	echo "Stopping proxy server..."; \
+	cd proxy && docker compose down; \
+	exit $$exit_code
+
 test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
 	$(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS)
 	@file $@
@@ -147,6 +174,14 @@ test_mbedtls : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
 test_split_mbedtls : test.cc ../httplib.h httplib.cc Makefile cert.pem
 	$(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_MBEDTLS)
 
+# wolfSSL backend targets
+test_wolfssl : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
+	$(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_WOLFSSL)
+	@file $@
+
+test_split_wolfssl : test.cc ../httplib.h httplib.cc Makefile cert.pem
+	$(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_WOLFSSL)
+
 # No TLS
 test_no_tls : test.cc include_httplib.cc ../httplib.h Makefile
 	$(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_NO_TLS)
@@ -194,6 +229,9 @@ test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem
 test_proxy_mbedtls : test_proxy.cc ../httplib.h Makefile cert.pem
 	$(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS_MBEDTLS)
 
+test_proxy_wolfssl : test_proxy.cc ../httplib.h Makefile cert.pem
+	$(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS_WOLFSSL)
+
 # Runs server_fuzzer.cc based on value of $(LIB_FUZZING_ENGINE).
 # Usage: make fuzz_test LIB_FUZZING_ENGINE=/path/to/libFuzzer
 fuzz_test: server_fuzzer
@@ -216,5 +254,5 @@ cert.pem:
 	./gen-certs.sh
 
 clean:
-	rm -rf test test_split test_mbedtls test_split_mbedtls test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM *_shard_*.log
+	rm -rf test test_split test_mbedtls test_split_mbedtls test_wolfssl test_split_wolfssl test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls test_proxy_wolfssl server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM *_shard_*.log
 

From 4f17fbaa035310eba41489d466c9f81e6dc7bb50 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Fri, 20 Feb 2026 15:44:31 -0500
Subject: [PATCH 0636/1222] Missing change for wolfSSL support

---
 httplib.h | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/httplib.h b/httplib.h
index a260c1f363..32892cb8b1 100644
--- a/httplib.h
+++ b/httplib.h
@@ -18370,6 +18370,7 @@ inline ctx_t create_server_context() {
   // Enable SNI on server
   wolfSSL_CTX_SNI_SetOptions(ctx->ctx, WOLFSSL_SNI_HOST_NAME,
                              WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
+  wolfSSL_CTX_set_servername_callback(ctx->ctx, impl::wolfssl_sni_callback);
 
   return static_cast(ctx);
 }
@@ -18569,11 +18570,6 @@ inline session_t create_session(ctx_t ctx, socket_t sock) {
 
   wolfSSL_set_fd(session->ssl, static_cast(sock));
 
-  // Set up SNI callback for server
-  if (wctx->is_server) {
-    wolfSSL_CTX_set_servername_callback(wctx->ctx, impl::wolfssl_sni_callback);
-  }
-
   return static_cast(session);
 }
 
@@ -18837,9 +18833,9 @@ inline bool is_peer_closed(session_t session, socket_t sock) {
   auto cleanup =
       detail::scope_exit([&]() { detail::set_nonblocking(sock, false); });
 
-  // Try a 1-byte read to check connection status
+  // Peek 1 byte to check connection status without consuming data
   unsigned char buf;
-  int ret = wolfSSL_read(wsession->ssl, &buf, 1);
+  int ret = wolfSSL_peek(wsession->ssl, &buf, 1);
 
   // If we got data or WANT_READ (would block), connection is alive
   if (ret > 0) { return false; }

From 17f301877f4a9614390b324e10457bf428f7fa46 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Fri, 20 Feb 2026 15:45:04 -0500
Subject: [PATCH 0637/1222] Add `bench` command in justfile

---
 justfile | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/justfile b/justfile
index 321365fa11..c6fe58e922 100644
--- a/justfile
+++ b/justfile
@@ -41,3 +41,6 @@ build:
     @(cd test && make test_split)
     @(cd test && make test_split_mbedtls)
     @(cd test && make test_split_wolfssl)
+
+bench:
+    @(cd benchmark && make bench-all)

From 43cf1822c6b2521bf863afdf7f9f48acf18a2628 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Fri, 20 Feb 2026 16:22:01 -0500
Subject: [PATCH 0638/1222] Resolve #2369

---
 .gitignore   |   1 +
 README.md    |  28 +++++++++++
 httplib.h    | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test.cc | 107 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 272 insertions(+)

diff --git a/.gitignore b/.gitignore
index 01cc6cfd54..1ea2142f62 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,6 +41,7 @@ test/test_no_tls
 test/server_fuzzer
 test/test_proxy
 test/test_proxy_mbedtls
+test/test_proxy_wolfssl
 test/test_split
 test/test_split_mbedtls
 test/test_split_wolfssl
diff --git a/README.md b/README.md
index f40893c34c..80d6fc48a7 100644
--- a/README.md
+++ b/README.md
@@ -468,6 +468,34 @@ svr.set_pre_request_handler([](const auto& req, auto& res) {
 });
 ```
 
+### Response user data
+
+`res.user_data` is a `std::map` that lets pre-routing or pre-request handlers pass arbitrary data to route handlers.
+
+```cpp
+struct AuthContext {
+  std::string user_id;
+  std::string role;
+};
+
+svr.set_pre_routing_handler([](const auto& req, auto& res) {
+  auto token = req.get_header_value("Authorization");
+  res.user_data["auth"] = AuthContext{decode_token(token)};
+  return Server::HandlerResponse::Unhandled;
+});
+
+svr.Get("/me", [](const auto& /*req*/, auto& res) {
+  auto* ctx = httplib::any_cast(&res.user_data["auth"]);
+  if (!ctx) {
+    res.status = StatusCode::Unauthorized_401;
+    return;
+  }
+  res.set_content("Hello " + ctx->user_id, "text/plain");
+});
+```
+
+`httplib::any` mirrors the C++17 `std::any` API. On C++17 and later it is an alias for `std::any`; on C++11/14 a compatible implementation is provided.
+
 ### Form data handling
 
 #### URL-encoded form data ('application/x-www-form-urlencoded')
diff --git a/httplib.h b/httplib.h
index 32892cb8b1..8de27198e6 100644
--- a/httplib.h
+++ b/httplib.h
@@ -352,6 +352,9 @@ using socket_t = int;
 #include 
 #include 
 #include 
+#if __cplusplus >= 201703L
+#include 
+#endif
 
 #if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) ||                        \
     defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
@@ -775,6 +778,135 @@ using Match = std::smatch;
 using DownloadProgress = std::function;
 using UploadProgress = std::function;
 
+// ----------------------------------------------------------------------------
+// httplib::any — type-erased value container (C++11 compatible)
+// On C++17+ builds, thin wrappers around std::any are provided.
+// ----------------------------------------------------------------------------
+
+#if __cplusplus >= 201703L
+
+using any = std::any;
+using bad_any_cast = std::bad_any_cast;
+
+template  T any_cast(const any &a) { return std::any_cast(a); }
+template  T any_cast(any &a) { return std::any_cast(a); }
+template  T any_cast(any &&a) {
+  return std::any_cast(std::move(a));
+}
+template  const T *any_cast(const any *a) noexcept {
+  return std::any_cast(a);
+}
+template  T *any_cast(any *a) noexcept {
+  return std::any_cast(a);
+}
+
+#else // C++11/14 implementation
+
+class bad_any_cast : public std::bad_cast {
+public:
+  const char *what() const noexcept override { return "bad any_cast"; }
+};
+
+namespace detail {
+
+using any_type_id = const void *;
+
+// Returns a unique per-type ID without RTTI.
+// The static address is stable across TUs because function templates are
+// implicitly inline and the ODR merges their statics into one.
+template  any_type_id any_typeid() noexcept {
+  static const char id = 0;
+  return &id;
+}
+
+struct any_storage {
+  virtual ~any_storage() = default;
+  virtual std::unique_ptr clone() const = 0;
+  virtual any_type_id type_id() const noexcept = 0;
+};
+
+template  struct any_value final : any_storage {
+  T value;
+  template  explicit any_value(U &&v) : value(std::forward(v)) {}
+  std::unique_ptr clone() const override {
+    return std::unique_ptr(new any_value(value));
+  }
+  any_type_id type_id() const noexcept override { return any_typeid(); }
+};
+
+} // namespace detail
+
+class any {
+  std::unique_ptr storage_;
+
+public:
+  any() noexcept = default;
+  any(const any &o) : storage_(o.storage_ ? o.storage_->clone() : nullptr) {}
+  any(any &&) noexcept = default;
+  any &operator=(const any &o) {
+    storage_ = o.storage_ ? o.storage_->clone() : nullptr;
+    return *this;
+  }
+  any &operator=(any &&) noexcept = default;
+
+  template <
+      typename T, typename D = typename std::decay::type,
+      typename std::enable_if::value, int>::type = 0>
+  any(T &&v) : storage_(new detail::any_value(std::forward(v))) {}
+
+  template <
+      typename T, typename D = typename std::decay::type,
+      typename std::enable_if::value, int>::type = 0>
+  any &operator=(T &&v) {
+    storage_.reset(new detail::any_value(std::forward(v)));
+    return *this;
+  }
+
+  bool has_value() const noexcept { return storage_ != nullptr; }
+  void reset() noexcept { storage_.reset(); }
+
+  template  friend T *any_cast(any *a) noexcept;
+  template  friend const T *any_cast(const any *a) noexcept;
+};
+
+template  T *any_cast(any *a) noexcept {
+  if (!a || !a->storage_) { return nullptr; }
+  if (a->storage_->type_id() != detail::any_typeid()) { return nullptr; }
+  return &static_cast *>(a->storage_.get())->value;
+}
+
+template  const T *any_cast(const any *a) noexcept {
+  if (!a || !a->storage_) { return nullptr; }
+  if (a->storage_->type_id() != detail::any_typeid()) { return nullptr; }
+  return &static_cast *>(a->storage_.get())->value;
+}
+
+template  T any_cast(const any &a) {
+  using U =
+      typename std::remove_cv::type>::type;
+  const U *p = any_cast(&a);
+  if (!p) { throw bad_any_cast{}; }
+  return static_cast(*p);
+}
+
+template  T any_cast(any &a) {
+  using U =
+      typename std::remove_cv::type>::type;
+  U *p = any_cast(&a);
+  if (!p) { throw bad_any_cast{}; }
+  return static_cast(*p);
+}
+
+template  T any_cast(any &&a) {
+  using U =
+      typename std::remove_cv::type>::type;
+  U *p = any_cast(&a);
+  if (!p) { throw bad_any_cast{}; }
+  return static_cast(std::move(*p));
+}
+
+#endif // __cplusplus >= 201703L
+
 struct Response;
 using ResponseHandler = std::function;
 
@@ -1074,6 +1206,10 @@ struct Response {
   std::string body;
   std::string location; // Redirect location
 
+  // User-defined context — set by pre-routing/pre-request handlers and read
+  // by route handlers to pass arbitrary data (e.g. decoded auth tokens).
+  std::map user_data;
+
   bool has_header(const std::string &key) const;
   std::string get_header_value(const std::string &key, const char *def = "",
                                size_t id = 0) const;
diff --git a/test/test.cc b/test/test.cc
index 257b78eb99..3ae89a00f4 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -3000,6 +3000,113 @@ TEST(RequestHandlerTest, PreRequestHandler) {
   }
 }
 
+TEST(AnyTest, BasicOperations) {
+  // Default construction
+  httplib::any a;
+  EXPECT_FALSE(a.has_value());
+
+  // Value construction and any_cast (pointer form, noexcept)
+  httplib::any b(42);
+  EXPECT_TRUE(b.has_value());
+  auto *p = httplib::any_cast(&b);
+  ASSERT_NE(nullptr, p);
+  EXPECT_EQ(42, *p);
+
+  // Type mismatch → nullptr
+  auto *q = httplib::any_cast(&b);
+  EXPECT_EQ(nullptr, q);
+
+  // any_cast (value form) succeeds
+  EXPECT_EQ(42, httplib::any_cast(b));
+
+  // any_cast (value form) throws on type mismatch
+  EXPECT_THROW(httplib::any_cast(b), httplib::bad_any_cast);
+
+  // Copy
+  httplib::any c = b;
+  EXPECT_EQ(42, httplib::any_cast(c));
+
+  // Move
+  httplib::any d = std::move(c);
+  EXPECT_EQ(42, httplib::any_cast(d));
+
+  // Assignment with different type
+  b = std::string("hello");
+  EXPECT_EQ("hello", httplib::any_cast(b));
+
+  // Reset
+  b.reset();
+  EXPECT_FALSE(b.has_value());
+}
+
+TEST(RequestHandlerTest, ResponseUserDataInPreRouting) {
+  struct AuthCtx {
+    std::string user_id;
+  };
+
+  Server svr;
+
+  svr.set_pre_routing_handler([](const Request & /*req*/, Response &res) {
+    res.user_data["auth"] = AuthCtx{"alice"};
+    return Server::HandlerResponse::Unhandled;
+  });
+
+  svr.Get("/me", [](const Request & /*req*/, Response &res) {
+    auto *ctx = httplib::any_cast(&res.user_data["auth"]);
+    ASSERT_NE(nullptr, ctx);
+    res.set_content("Hello " + ctx->user_id, "text/plain");
+  });
+
+  auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    thread.join();
+    ASSERT_FALSE(svr.is_running());
+  });
+
+  svr.wait_until_ready();
+
+  Client cli(HOST, PORT);
+  auto res = cli.Get("/me");
+  ASSERT_TRUE(res);
+  EXPECT_EQ(StatusCode::OK_200, res->status);
+  EXPECT_EQ("Hello alice", res->body);
+}
+
+TEST(RequestHandlerTest, ResponseUserDataInPreRequest) {
+  struct RoleCtx {
+    std::string role;
+  };
+
+  Server svr;
+
+  svr.set_pre_request_handler([](const Request & /*req*/, Response &res) {
+    res.user_data["role"] = RoleCtx{"admin"};
+    return Server::HandlerResponse::Unhandled;
+  });
+
+  svr.Get("/role", [](const Request & /*req*/, Response &res) {
+    auto *ctx = httplib::any_cast(&res.user_data["role"]);
+    ASSERT_NE(nullptr, ctx);
+    res.set_content(ctx->role, "text/plain");
+  });
+
+  auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    thread.join();
+    ASSERT_FALSE(svr.is_running());
+  });
+
+  svr.wait_until_ready();
+
+  Client cli(HOST, PORT);
+  auto res = cli.Get("/role");
+  ASSERT_TRUE(res);
+  EXPECT_EQ(StatusCode::OK_200, res->status);
+  EXPECT_EQ("admin", res->body);
+}
+
 TEST(InvalidFormatTest, StatusCode) {
   Server svr;
 

From 2280f1d191cd9d02b87b1adb5bc5f9a6afe94ed4 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Fri, 20 Feb 2026 23:15:01 -0500
Subject: [PATCH 0639/1222] Improvement for Multipart Form Data

---
 README.md    |  11 +++++
 httplib.h    | 123 ++++++++++++++++++++++++++++++++++++++++++++++-----
 test/test.cc |  94 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 216 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index 80d6fc48a7..b9887917cb 100644
--- a/README.md
+++ b/README.md
@@ -989,6 +989,17 @@ httplib::UploadFormDataItems items = {
 auto res = cli.Post("/multipart", items);
 ```
 
+To upload files from disk without loading them entirely into memory, use `make_file_provider`. The file is read and sent in chunks with a correct `Content-Length` header.
+
+```cpp
+httplib::FormDataProviderItems providers = {
+  httplib::make_file_provider("file1", "/path/to/large.bin", "large.bin", "application/octet-stream"),
+  httplib::make_file_provider("avatar", "/path/to/photo.jpg", "photo.jpg", "image/jpeg"),
+};
+
+auto res = cli.Post("/upload", {}, {}, providers);
+```
+
 ### PUT
 
 ```c++
diff --git a/httplib.h b/httplib.h
index 8de27198e6..2b61c2483b 100644
--- a/httplib.h
+++ b/httplib.h
@@ -334,6 +334,7 @@ using socket_t = int;
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1001,6 +1002,34 @@ struct FormDataProvider {
 };
 using FormDataProviderItems = std::vector;
 
+inline FormDataProvider
+make_file_provider(const std::string &name, const std::string &filepath,
+                   const std::string &filename = std::string(),
+                   const std::string &content_type = std::string()) {
+  FormDataProvider fdp;
+  fdp.name = name;
+  fdp.filename = filename.empty() ? filepath : filename;
+  fdp.content_type = content_type;
+  fdp.provider = [filepath](size_t offset, DataSink &sink) -> bool {
+    std::ifstream f(filepath, std::ios::binary);
+    if (!f) { return false; }
+    if (offset > 0) {
+      f.seekg(static_cast(offset));
+      if (!f.good()) {
+        sink.done();
+        return true;
+      }
+    }
+    char buf[8192];
+    f.read(buf, sizeof(buf));
+    auto n = static_cast(f.gcount());
+    if (n > 0) { return sink.write(buf, n); }
+    sink.done(); // EOF
+    return true;
+  };
+  return fdp;
+}
+
 using ContentReceiverWithProgress = std::function;
 
@@ -7870,6 +7899,64 @@ serialize_multipart_formdata(const UploadFormDataItems &items,
   return body;
 }
 
+inline size_t get_multipart_content_length(const UploadFormDataItems &items,
+                                           const std::string &boundary) {
+  size_t total = 0;
+  for (const auto &item : items) {
+    total += serialize_multipart_formdata_item_begin(item, boundary).size();
+    total += item.content.size();
+    total += serialize_multipart_formdata_item_end().size();
+  }
+  total += serialize_multipart_formdata_finish(boundary).size();
+  return total;
+}
+
+struct MultipartSegment {
+  const char *data;
+  size_t size;
+};
+
+// NOTE: items must outlive the returned ContentProvider
+//       (safe for synchronous use inside Post/Put/Patch)
+inline ContentProvider
+make_multipart_content_provider(const UploadFormDataItems &items,
+                                const std::string &boundary) {
+  // Own the per-item header strings and the finish string
+  std::vector owned;
+  owned.reserve(items.size() + 1);
+  for (const auto &item : items)
+    owned.push_back(serialize_multipart_formdata_item_begin(item, boundary));
+  owned.push_back(serialize_multipart_formdata_finish(boundary));
+
+  // Flat segment list: [header, content, "\r\n"] * N + [finish]
+  std::vector segs;
+  segs.reserve(items.size() * 3 + 1);
+  static const char crlf[] = "\r\n";
+  for (size_t i = 0; i < items.size(); i++) {
+    segs.push_back({owned[i].data(), owned[i].size()});
+    segs.push_back({items[i].content.data(), items[i].content.size()});
+    segs.push_back({crlf, 2});
+  }
+  segs.push_back({owned.back().data(), owned.back().size()});
+
+  return [owned = std::move(owned), segs = std::move(segs)](
+             size_t offset, size_t length, DataSink &sink) -> bool {
+    size_t pos = 0;
+    for (const auto &seg : segs) {
+      // Loop invariant: pos <= offset (proven by advancing pos only when
+      // offset - pos >= seg.size, i.e., the segment doesn't contain offset)
+      if (seg.size > 0 && offset - pos < seg.size) {
+        size_t seg_offset = offset - pos;
+        size_t available = seg.size - seg_offset;
+        size_t to_write = std::min(available, length);
+        return sink.write(seg.data + seg_offset, to_write);
+      }
+      pos += seg.size;
+    }
+    return true; // past end (shouldn't be reached when content_length is exact)
+  };
+}
+
 inline void coalesce_ranges(Ranges &ranges, size_t content_length) {
   if (ranges.size() <= 1) return;
 
@@ -13402,8 +13489,10 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers,
   const auto &boundary = detail::make_multipart_data_boundary();
   const auto &content_type =
       detail::serialize_multipart_formdata_get_content_type(boundary);
-  const auto &body = detail::serialize_multipart_formdata(items, boundary);
-  return Post(path, headers, body, content_type, progress);
+  auto content_length = detail::get_multipart_content_length(items, boundary);
+  return Post(path, headers, content_length,
+              detail::make_multipart_content_provider(items, boundary),
+              content_type, progress);
 }
 
 inline Result ClientImpl::Post(const std::string &path, const Headers &headers,
@@ -13416,8 +13505,10 @@ inline Result ClientImpl::Post(const std::string &path, const Headers &headers,
 
   const auto &content_type =
       detail::serialize_multipart_formdata_get_content_type(boundary);
-  const auto &body = detail::serialize_multipart_formdata(items, boundary);
-  return Post(path, headers, body, content_type, progress);
+  auto content_length = detail::get_multipart_content_length(items, boundary);
+  return Post(path, headers, content_length,
+              detail::make_multipart_content_provider(items, boundary),
+              content_type, progress);
 }
 
 inline Result ClientImpl::Post(const std::string &path, const Headers &headers,
@@ -13595,8 +13686,10 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers,
   const auto &boundary = detail::make_multipart_data_boundary();
   const auto &content_type =
       detail::serialize_multipart_formdata_get_content_type(boundary);
-  const auto &body = detail::serialize_multipart_formdata(items, boundary);
-  return Put(path, headers, body, content_type, progress);
+  auto content_length = detail::get_multipart_content_length(items, boundary);
+  return Put(path, headers, content_length,
+             detail::make_multipart_content_provider(items, boundary),
+             content_type, progress);
 }
 
 inline Result ClientImpl::Put(const std::string &path, const Headers &headers,
@@ -13609,8 +13702,10 @@ inline Result ClientImpl::Put(const std::string &path, const Headers &headers,
 
   const auto &content_type =
       detail::serialize_multipart_formdata_get_content_type(boundary);
-  const auto &body = detail::serialize_multipart_formdata(items, boundary);
-  return Put(path, headers, body, content_type, progress);
+  auto content_length = detail::get_multipart_content_length(items, boundary);
+  return Put(path, headers, content_length,
+             detail::make_multipart_content_provider(items, boundary),
+             content_type, progress);
 }
 
 inline Result ClientImpl::Put(const std::string &path, const Headers &headers,
@@ -13790,8 +13885,10 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers,
   const auto &boundary = detail::make_multipart_data_boundary();
   const auto &content_type =
       detail::serialize_multipart_formdata_get_content_type(boundary);
-  const auto &body = detail::serialize_multipart_formdata(items, boundary);
-  return Patch(path, headers, body, content_type, progress);
+  auto content_length = detail::get_multipart_content_length(items, boundary);
+  return Patch(path, headers, content_length,
+               detail::make_multipart_content_provider(items, boundary),
+               content_type, progress);
 }
 
 inline Result ClientImpl::Patch(const std::string &path, const Headers &headers,
@@ -13804,8 +13901,10 @@ inline Result ClientImpl::Patch(const std::string &path, const Headers &headers,
 
   const auto &content_type =
       detail::serialize_multipart_formdata_get_content_type(boundary);
-  const auto &body = detail::serialize_multipart_formdata(items, boundary);
-  return Patch(path, headers, body, content_type, progress);
+  auto content_length = detail::get_multipart_content_length(items, boundary);
+  return Patch(path, headers, content_length,
+               detail::make_multipart_content_provider(items, boundary),
+               content_type, progress);
 }
 
 inline Result ClientImpl::Patch(const std::string &path, const Headers &headers,
diff --git a/test/test.cc b/test/test.cc
index 3ae89a00f4..9eb0c0235b 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -11725,6 +11725,100 @@ TEST(MultipartFormDataTest, LargeHeader) {
   ASSERT_EQ("200", response.substr(9, 3));
 }
 
+TEST(MultipartFormDataTest, UploadItemsHasContentLength) {
+  // Verify that Post(path, headers, UploadFormDataItems) sends Content-Length
+  // (not chunked Transfer-Encoding) after the streaming refactor.
+  auto handled = false;
+
+  Server svr;
+  svr.Post("/upload", [&](const Request &req, Response &res) {
+    auto cl_it = req.headers.find("Content-Length");
+    EXPECT_TRUE(cl_it != req.headers.end());
+    auto te_it = req.headers.find("Transfer-Encoding");
+    EXPECT_TRUE(te_it == req.headers.end());
+    EXPECT_EQ(2u, req.form.fields.size() + req.form.files.size());
+    res.set_content("ok", "text/plain");
+    handled = true;
+  });
+
+  auto port = svr.bind_to_any_port(HOST);
+  auto t = thread([&] { svr.listen_after_bind(); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    t.join();
+    ASSERT_FALSE(svr.is_running());
+    ASSERT_TRUE(handled);
+  });
+
+  svr.wait_until_ready();
+
+  UploadFormDataItems items = {
+      {"field1", "hello", "", "text/plain"},
+      {"file1", "world", "test.txt", "application/octet-stream"},
+  };
+
+  Client cli(HOST, port);
+  auto res = cli.Post("/upload", {}, items);
+  ASSERT_TRUE(res);
+  EXPECT_EQ(StatusCode::OK_200, res->status);
+}
+
+TEST(MultipartFormDataTest, MakeFileProvider) {
+  // Verify make_file_provider sends a file's contents correctly.
+  const std::string file_content(4096, 'Z');
+  const std::string tmp_path = "/tmp/httplib_test_make_file_provider.bin";
+  {
+    std::ofstream ofs(tmp_path, std::ios::binary);
+    ofs.write(file_content.data(),
+              static_cast(file_content.size()));
+  }
+
+  auto handled = false;
+
+  Server svr;
+  svr.Post("/upload", [&](const Request &req, Response & /*res*/,
+                          const ContentReader &content_reader) {
+    ASSERT_TRUE(req.is_multipart_form_data());
+    std::vector items;
+    content_reader(
+        [&](const FormData &file) {
+          items.push_back(file);
+          return true;
+        },
+        [&](const char *data, size_t data_length) {
+          items.back().content.append(data, data_length);
+          return true;
+        });
+    ASSERT_EQ(1u, items.size());
+    EXPECT_EQ("myfile", items[0].name);
+    EXPECT_EQ("data.bin", items[0].filename);
+    EXPECT_EQ("application/octet-stream", items[0].content_type);
+    EXPECT_EQ(file_content, items[0].content);
+    handled = true;
+  });
+
+  auto port = svr.bind_to_any_port(HOST);
+  auto t = thread([&] { svr.listen_after_bind(); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    t.join();
+    ASSERT_FALSE(svr.is_running());
+    ASSERT_TRUE(handled);
+    std::remove(tmp_path.c_str());
+  });
+
+  svr.wait_until_ready();
+
+  FormDataProviderItems providers;
+  providers.push_back(make_file_provider("myfile", tmp_path, "data.bin",
+                                         "application/octet-stream"));
+
+  Client cli(HOST, port);
+  auto res = cli.Post("/upload", {}, {}, providers);
+  ASSERT_TRUE(res);
+  EXPECT_EQ(StatusCode::OK_200, res->status);
+}
+
 TEST(TaskQueueTest, IncreaseAtomicInteger) {
   static constexpr unsigned int number_of_tasks{1000000};
   std::atomic_uint count{0};

From 9b9bda6b6e6c2875b59644d75b3ee29ce460973a Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 21 Feb 2026 08:14:47 -0500
Subject: [PATCH 0640/1222] Fix CI build errors

---
 httplib.h    | 14 +++++++++++++-
 test/test.cc |  2 ++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/httplib.h b/httplib.h
index 2b61c2483b..94c0e3cffe 100644
--- a/httplib.h
+++ b/httplib.h
@@ -886,7 +886,11 @@ template  T any_cast(const any &a) {
   using U =
       typename std::remove_cv::type>::type;
   const U *p = any_cast(&a);
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
   if (!p) { throw bad_any_cast{}; }
+#else
+  if (!p) { std::abort(); }
+#endif
   return static_cast(*p);
 }
 
@@ -894,7 +898,11 @@ template  T any_cast(any &a) {
   using U =
       typename std::remove_cv::type>::type;
   U *p = any_cast(&a);
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
   if (!p) { throw bad_any_cast{}; }
+#else
+  if (!p) { std::abort(); }
+#endif
   return static_cast(*p);
 }
 
@@ -902,7 +910,11 @@ template  T any_cast(any &&a) {
   using U =
       typename std::remove_cv::type>::type;
   U *p = any_cast(&a);
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
   if (!p) { throw bad_any_cast{}; }
+#else
+  if (!p) { std::abort(); }
+#endif
   return static_cast(std::move(*p));
 }
 
@@ -7948,7 +7960,7 @@ make_multipart_content_provider(const UploadFormDataItems &items,
       if (seg.size > 0 && offset - pos < seg.size) {
         size_t seg_offset = offset - pos;
         size_t available = seg.size - seg_offset;
-        size_t to_write = std::min(available, length);
+        size_t to_write = (std::min)(available, length);
         return sink.write(seg.data + seg_offset, to_write);
       }
       pos += seg.size;
diff --git a/test/test.cc b/test/test.cc
index 9eb0c0235b..4e75f6014f 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -3020,7 +3020,9 @@ TEST(AnyTest, BasicOperations) {
   EXPECT_EQ(42, httplib::any_cast(b));
 
   // any_cast (value form) throws on type mismatch
+#ifndef CPPHTTPLIB_NO_EXCEPTIONS
   EXPECT_THROW(httplib::any_cast(b), httplib::bad_any_cast);
+#endif
 
   // Copy
   httplib::any c = b;

From f1f8ff53d53096946a97416525a1079dd9e8a5b1 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 21 Feb 2026 08:43:49 -0500
Subject: [PATCH 0641/1222] Release v0.33.0

---
 httplib.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/httplib.h b/httplib.h
index 94c0e3cffe..a17629fd20 100644
--- a/httplib.h
+++ b/httplib.h
@@ -8,8 +8,8 @@
 #ifndef CPPHTTPLIB_HTTPLIB_H
 #define CPPHTTPLIB_HTTPLIB_H
 
-#define CPPHTTPLIB_VERSION "0.32.0"
-#define CPPHTTPLIB_VERSION_NUM "0x002000"
+#define CPPHTTPLIB_VERSION "0.33.0"
+#define CPPHTTPLIB_VERSION_NUM "0x002100"
 
 /*
  * Platform compatibility check

From 33dbe00cce793988c900fad2ee87997b1db34b22 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 21 Feb 2026 08:52:50 -0500
Subject: [PATCH 0642/1222] Fix compiple problem with C++11 compiler

---
 httplib.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/httplib.h b/httplib.h
index a17629fd20..91d1490d26 100644
--- a/httplib.h
+++ b/httplib.h
@@ -7951,10 +7951,19 @@ make_multipart_content_provider(const UploadFormDataItems &items,
   }
   segs.push_back({owned.back().data(), owned.back().size()});
 
-  return [owned = std::move(owned), segs = std::move(segs)](
-             size_t offset, size_t length, DataSink &sink) -> bool {
+  struct MultipartState {
+    std::vector owned;
+    std::vector segs;
+  };
+  auto state = std::make_shared();
+  state->owned = std::move(owned);
+  // `segs` holds raw pointers into owned strings; std::string move preserves
+  // the data pointer, so these pointers remain valid after the move above.
+  state->segs = std::move(segs);
+
+  return [state](size_t offset, size_t length, DataSink &sink) -> bool {
     size_t pos = 0;
-    for (const auto &seg : segs) {
+    for (const auto &seg : state->segs) {
       // Loop invariant: pos <= offset (proven by advancing pos only when
       // offset - pos >= seg.size, i.e., the segment doesn't contain offset)
       if (seg.size > 0 && offset - pos < seg.size) {

From e61a8bcec7119f0eaa63fc2642344b6c182aa9b0 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 21 Feb 2026 08:55:06 -0500
Subject: [PATCH 0643/1222] Release v0.33.1

---
 httplib.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/httplib.h b/httplib.h
index 91d1490d26..e4d461a7b4 100644
--- a/httplib.h
+++ b/httplib.h
@@ -8,8 +8,8 @@
 #ifndef CPPHTTPLIB_HTTPLIB_H
 #define CPPHTTPLIB_HTTPLIB_H
 
-#define CPPHTTPLIB_VERSION "0.33.0"
-#define CPPHTTPLIB_VERSION_NUM "0x002100"
+#define CPPHTTPLIB_VERSION "0.33.1"
+#define CPPHTTPLIB_VERSION_NUM "0x002101"
 
 /*
  * Platform compatibility check

From b4d16a582dfad234f859e7554d45cdc9deb76a49 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sat, 21 Feb 2026 23:27:16 -0500
Subject: [PATCH 0644/1222] Update `latest` tag when new release

---
 .github/workflows/tag-latest.yml | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 .github/workflows/tag-latest.yml

diff --git a/.github/workflows/tag-latest.yml b/.github/workflows/tag-latest.yml
new file mode 100644
index 0000000000..9cca61603d
--- /dev/null
+++ b/.github/workflows/tag-latest.yml
@@ -0,0 +1,19 @@
+name: Update latest tag
+
+on:
+  release:
+    types: [published]
+
+jobs:
+  tag-latest:
+    runs-on: ubuntu-latest
+    permissions:
+      contents: write
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v4
+
+      - name: Force-update latest tag
+        run: |
+          git tag -f latest
+          git push origin latest --force

From c53d93d145d880f641b604479fcd8edc90f4baab Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sun, 22 Feb 2026 01:15:21 -0500
Subject: [PATCH 0645/1222] Add make_file_body

---
 README.md    | 11 ++++++++++-
 httplib.h    | 26 ++++++++++++++++++++++++++
 test/test.cc | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index b9887917cb..a60ee22e80 100644
--- a/README.md
+++ b/README.md
@@ -989,7 +989,7 @@ httplib::UploadFormDataItems items = {
 auto res = cli.Post("/multipart", items);
 ```
 
-To upload files from disk without loading them entirely into memory, use `make_file_provider`. The file is read and sent in chunks with a correct `Content-Length` header.
+To upload files from disk without loading them entirely into memory, use `make_file_provider`. The file is sent with chunked transfer encoding.
 
 ```cpp
 httplib::FormDataProviderItems providers = {
@@ -1000,6 +1000,15 @@ httplib::FormDataProviderItems providers = {
 auto res = cli.Post("/upload", {}, {}, providers);
 ```
 
+### POST with a file body
+
+To POST a file as a raw binary body with `Content-Length`, use `make_file_body`.
+
+```cpp
+auto [size, provider] = httplib::make_file_body("/path/to/data.bin");
+auto res = cli.Post("/upload", size, provider, "application/octet-stream");
+```
+
 ### PUT
 
 ```c++
diff --git a/httplib.h b/httplib.h
index e4d461a7b4..9f4ea3c714 100644
--- a/httplib.h
+++ b/httplib.h
@@ -1042,6 +1042,32 @@ make_file_provider(const std::string &name, const std::string &filepath,
   return fdp;
 }
 
+inline std::pair
+make_file_body(const std::string &filepath) {
+  std::ifstream f(filepath, std::ios::binary | std::ios::ate);
+  if (!f) { return {0, ContentProvider{}}; }
+  auto size = static_cast(f.tellg());
+
+  ContentProvider provider = [filepath](size_t offset, size_t length,
+                                        DataSink &sink) -> bool {
+    std::ifstream f(filepath, std::ios::binary);
+    if (!f) { return false; }
+    f.seekg(static_cast(offset));
+    if (!f.good()) { return false; }
+    char buf[8192];
+    while (length > 0) {
+      auto to_read = (std::min)(sizeof(buf), length);
+      f.read(buf, static_cast(to_read));
+      auto n = static_cast(f.gcount());
+      if (n == 0) { break; }
+      if (!sink.write(buf, n)) { return false; }
+      length -= n;
+    }
+    return true;
+  };
+  return {size, std::move(provider)};
+}
+
 using ContentReceiverWithProgress = std::function;
 
diff --git a/test/test.cc b/test/test.cc
index 4e75f6014f..f25ca3dce5 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -11821,6 +11821,46 @@ TEST(MultipartFormDataTest, MakeFileProvider) {
   EXPECT_EQ(StatusCode::OK_200, res->status);
 }
 
+TEST(MakeFileBodyTest, Basic) {
+  const std::string file_content(4096, 'Z');
+  const std::string tmp_path = "/tmp/httplib_test_make_file_body.bin";
+  {
+    std::ofstream ofs(tmp_path, std::ios::binary);
+    ofs.write(file_content.data(),
+              static_cast(file_content.size()));
+  }
+
+  auto handled = false;
+
+  Server svr;
+  svr.Post("/upload", [&](const Request &req, Response &res) {
+    EXPECT_EQ(file_content, req.body);
+    handled = true;
+    res.status = StatusCode::OK_200;
+  });
+
+  auto port = svr.bind_to_any_port(HOST);
+  auto t = thread([&] { svr.listen_after_bind(); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    t.join();
+    ASSERT_FALSE(svr.is_running());
+    ASSERT_TRUE(handled);
+    std::remove(tmp_path.c_str());
+  });
+
+  svr.wait_until_ready();
+
+  auto fb = make_file_body(tmp_path);
+  ASSERT_GT(fb.first, 0u);
+
+  Client cli(HOST, port);
+  auto res =
+      cli.Post("/upload", fb.first, fb.second, "application/octet-stream");
+  ASSERT_TRUE(res);
+  EXPECT_EQ(StatusCode::OK_200, res->status);
+}
+
 TEST(TaskQueueTest, IncreaseAtomicInteger) {
   static constexpr unsigned int number_of_tasks{1000000};
   std::atomic_uint count{0};

From f29bb15f9d8639a417be3a506a4d60855132c0c6 Mon Sep 17 00:00:00 2001
From: yhirose 
Date: Sun, 22 Feb 2026 05:29:54 -0500
Subject: [PATCH 0646/1222] Performance improvement!

---
 benchmark/Makefile        |    13 +-
 benchmark/crow/crow_all.h | 24154 ++++++++++++++++++------------------
 benchmark/crow/main.cpp   |     2 +-
 httplib.h                 |    40 +-
 4 files changed, 11844 insertions(+), 12365 deletions(-)

diff --git a/benchmark/Makefile b/benchmark/Makefile
index 6dc93f05dd..6fdd5281ea 100644
--- a/benchmark/Makefile
+++ b/benchmark/Makefile
@@ -1,4 +1,7 @@
-CXXFLAGS = -std=c++11 -O2 -I..
+CXXFLAGS = -O2 -I..
+
+CPPHTTPLIB_CXXFLAGS = -std=c++11
+CROW_CXXFLAGS = -std=c++17
 
 CPPHTTPLIB_FLAGS = -DCPPHTTPLIB_THREAD_POOL_COUNT=16
 
@@ -18,11 +21,11 @@ run : server
 	@./server
 
 server : cpp-httplib/main.cpp ../httplib.h
-	@g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp
+	@g++ -o $@ $(CXXFLAGS) $(CPPHTTPLIB_CXXFLAGS) $(CPPHTTPLIB_FLAGS) cpp-httplib/main.cpp
 
 # crow
 bench-crow: server-crow
-	@echo "-------------\n Crow v1.2.0\n-------------\n"
+	@echo "-------------\n Crow v1.3.1\n-------------\n"
 	@./server-crow & export PID=$$!; $(BENCH); kill $${PID}
 	@echo ""
 
@@ -32,8 +35,8 @@ monitor-crow: server-crow
 run-crow : server-crow
 	@./server-crow
 
-server-crow : crow/main.cpp
-	@g++ -o $@ $(CXXFLAGS) crow/main.cpp
+server-crow : crow/main.cpp crow/crow_all.h
+	@g++ -o $@ $(CXXFLAGS) $(CROW_CXXFLAGS) crow/main.cpp
 
 # misc
 build: server server-crow
diff --git a/benchmark/crow/crow_all.h b/benchmark/crow/crow_all.h
index 0465f96dc8..6de0dac422 100644
--- a/benchmark/crow/crow_all.h
+++ b/benchmark/crow/crow_all.h
@@ -2,7 +2,7 @@
 /*BSD 3-Clause License
 
 Copyright (c) 2014-2017, ipkn
-              2020-2022, CrowCpp
+              2020-2026, CrowCpp
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -30,965 +30,921 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-The Crow logo and other graphic material (excluding third party logos) used are under exclusive Copyright (c) 2021-2022, Farook Al-Sammarraie (The-EDev), All rights reserved.
+The Crow logo and other graphic material (excluding third party logos) used are
+under exclusive Copyright (c) 2021-2022, Farook Al-Sammarraie (The-EDev), All
+rights reserved.
 */
 #pragma once
-// This file is generated from nginx/conf/mime.types using nginx_mime2cpp.py on 2021-12-03.
-#include 
+#ifdef CROW_ENABLE_COMPRESSION
+
 #include 
+#include 
 
-namespace crow
+// http://zlib.net/manual.html
+namespace crow // NOTE: Already documented in "crow/app.h"
 {
-    const std::unordered_map mime_types{
-      {"shtml", "text/html"},
-      {"htm", "text/html"},
-      {"html", "text/html"},
-      {"css", "text/css"},
-      {"xml", "text/xml"},
-      {"gif", "image/gif"},
-      {"jpg", "image/jpeg"},
-      {"jpeg", "image/jpeg"},
-      {"js", "application/javascript"},
-      {"atom", "application/atom+xml"},
-      {"rss", "application/rss+xml"},
-      {"mml", "text/mathml"},
-      {"txt", "text/plain"},
-      {"jad", "text/vnd.sun.j2me.app-descriptor"},
-      {"wml", "text/vnd.wap.wml"},
-      {"htc", "text/x-component"},
-      {"avif", "image/avif"},
-      {"png", "image/png"},
-      {"svgz", "image/svg+xml"},
-      {"svg", "image/svg+xml"},
-      {"tiff", "image/tiff"},
-      {"tif", "image/tiff"},
-      {"wbmp", "image/vnd.wap.wbmp"},
-      {"webp", "image/webp"},
-      {"ico", "image/x-icon"},
-      {"jng", "image/x-jng"},
-      {"bmp", "image/x-ms-bmp"},
-      {"woff", "font/woff"},
-      {"woff2", "font/woff2"},
-      {"ear", "application/java-archive"},
-      {"war", "application/java-archive"},
-      {"jar", "application/java-archive"},
-      {"json", "application/json"},
-      {"hqx", "application/mac-binhex40"},
-      {"doc", "application/msword"},
-      {"pdf", "application/pdf"},
-      {"ai", "application/postscript"},
-      {"eps", "application/postscript"},
-      {"ps", "application/postscript"},
-      {"rtf", "application/rtf"},
-      {"m3u8", "application/vnd.apple.mpegurl"},
-      {"kml", "application/vnd.google-earth.kml+xml"},
-      {"kmz", "application/vnd.google-earth.kmz"},
-      {"xls", "application/vnd.ms-excel"},
-      {"eot", "application/vnd.ms-fontobject"},
-      {"ppt", "application/vnd.ms-powerpoint"},
-      {"odg", "application/vnd.oasis.opendocument.graphics"},
-      {"odp", "application/vnd.oasis.opendocument.presentation"},
-      {"ods", "application/vnd.oasis.opendocument.spreadsheet"},
-      {"odt", "application/vnd.oasis.opendocument.text"},
-      {"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
-      {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
-      {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
-      {"wmlc", "application/vnd.wap.wmlc"},
-      {"wasm", "application/wasm"},
-      {"7z", "application/x-7z-compressed"},
-      {"cco", "application/x-cocoa"},
-      {"jardiff", "application/x-java-archive-diff"},
-      {"jnlp", "application/x-java-jnlp-file"},
-      {"run", "application/x-makeself"},
-      {"pm", "application/x-perl"},
-      {"pl", "application/x-perl"},
-      {"pdb", "application/x-pilot"},
-      {"prc", "application/x-pilot"},
-      {"rar", "application/x-rar-compressed"},
-      {"rpm", "application/x-redhat-package-manager"},
-      {"sea", "application/x-sea"},
-      {"swf", "application/x-shockwave-flash"},
-      {"sit", "application/x-stuffit"},
-      {"tk", "application/x-tcl"},
-      {"tcl", "application/x-tcl"},
-      {"crt", "application/x-x509-ca-cert"},
-      {"pem", "application/x-x509-ca-cert"},
-      {"der", "application/x-x509-ca-cert"},
-      {"xpi", "application/x-xpinstall"},
-      {"xhtml", "application/xhtml+xml"},
-      {"xspf", "application/xspf+xml"},
-      {"zip", "application/zip"},
-      {"dll", "application/octet-stream"},
-      {"exe", "application/octet-stream"},
-      {"bin", "application/octet-stream"},
-      {"deb", "application/octet-stream"},
-      {"dmg", "application/octet-stream"},
-      {"img", "application/octet-stream"},
-      {"iso", "application/octet-stream"},
-      {"msm", "application/octet-stream"},
-      {"msp", "application/octet-stream"},
-      {"msi", "application/octet-stream"},
-      {"kar", "audio/midi"},
-      {"midi", "audio/midi"},
-      {"mid", "audio/midi"},
-      {"mp3", "audio/mpeg"},
-      {"ogg", "audio/ogg"},
-      {"m4a", "audio/x-m4a"},
-      {"ra", "audio/x-realaudio"},
-      {"3gp", "video/3gpp"},
-      {"3gpp", "video/3gpp"},
-      {"ts", "video/mp2t"},
-      {"mp4", "video/mp4"},
-      {"mpg", "video/mpeg"},
-      {"mpeg", "video/mpeg"},
-      {"mov", "video/quicktime"},
-      {"webm", "video/webm"},
-      {"flv", "video/x-flv"},
-      {"m4v", "video/x-m4v"},
-      {"mng", "video/x-mng"},
-      {"asf", "video/x-ms-asf"},
-      {"asx", "video/x-ms-asf"},
-      {"wmv", "video/x-ms-wmv"},
-      {"avi", "video/x-msvideo"}};
-}
+namespace compression {
+// Values used in the 'windowBits' parameter for deflateInit2.
+enum algorithm {
+  // 15 is the default value for deflate
+  DEFLATE = 15,
+  // windowBits can also be greater than 15 for optional gzip encoding.
+  // Add 16 to windowBits to write a simple gzip header and trailer around the
+  // compressed data instead of a zlib wrapper.
+  GZIP = 15 | 16,
+};
 
+inline std::string compress_string(std::string const &str, algorithm algo) {
+  std::string compressed_str;
+  z_stream stream{};
+  // Initialize with the default values
+  if (::deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, algo, 8,
+                     Z_DEFAULT_STRATEGY) == Z_OK) {
+    char buffer[8192];
+
+    stream.avail_in = str.size();
+    // zlib does not take a const pointer. The data is not altered.
+    stream.next_in =
+        const_cast(reinterpret_cast(str.c_str()));
+
+    int code = Z_OK;
+    do {
+      stream.avail_out = sizeof(buffer);
+      stream.next_out = reinterpret_cast(&buffer[0]);
+
+      code = ::deflate(&stream, Z_FINISH);
+      // Successful and non-fatal error code returned by deflate when used with
+      // Z_FINISH flush
+      if (code == Z_OK || code == Z_STREAM_END) {
+        std::copy(&buffer[0], &buffer[sizeof(buffer) - stream.avail_out],
+                  std::back_inserter(compressed_str));
+      }
 
-#include 
+    } while (code == Z_OK);
 
-namespace crow
-{
-    /// An abstract class that allows any other class to be returned by a handler.
-    struct returnable
-    {
-        std::string content_type;
-        virtual std::string dump() const = 0;
+    if (code != Z_STREAM_END) compressed_str.clear();
 
-        returnable(std::string ctype):
-          content_type{ctype}
-        {}
+    ::deflateEnd(&stream);
+  }
 
-        virtual ~returnable(){};
-    };
+  return compressed_str;
+}
+
+inline std::string decompress_string(std::string const &deflated_string) {
+  std::string inflated_string;
+  Bytef tmp[8192];
+
+  z_stream zstream{};
+  zstream.avail_in = deflated_string.size();
+  // Nasty const_cast but zlib won't alter its contents
+  zstream.next_in = const_cast(
+      reinterpret_cast(deflated_string.c_str()));
+  // Initialize with automatic header detection, for gzip support
+  if (::inflateInit2(&zstream, MAX_WBITS | 32) == Z_OK) {
+    do {
+      zstream.avail_out = sizeof(tmp);
+      zstream.next_out = &tmp[0];
+
+      auto ret = ::inflate(&zstream, Z_NO_FLUSH);
+      if (ret == Z_OK || ret == Z_STREAM_END) {
+        std::copy(&tmp[0], &tmp[sizeof(tmp) - zstream.avail_out],
+                  std::back_inserter(inflated_string));
+      } else {
+        // Something went wrong with inflate; make sure we return an empty
+        // string
+        inflated_string.clear();
+        break;
+      }
+
+    } while (zstream.avail_out == 0);
+
+    // Free zlib's internal memory
+    ::inflateEnd(&zstream);
+  }
+
+  return inflated_string;
+}
+} // namespace compression
 } // namespace crow
 
+#endif
+
+namespace crow {
+constexpr const char VERSION[] = "master";
+}
+
+/*
+ * SHA1 Wikipedia Page: http://en.wikipedia.org/wiki/SHA-1
+ *
+ * Copyright (c) 2012-22 SAURAV MOHAPATRA 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/**
+ * \file TinySHA1.hpp
+ * \author SAURAV MOHAPATRA 
+ * \date 2012-22
+ * \brief TinySHA1 - a header only implementation of the SHA1 algorithm in C++.
+ * Based on the implementation in boost::uuid::details.
+ *
+ * In this file are defined:
+ * - sha1::SHA1
+ */
+#ifndef _TINY_SHA1_HPP_
+#define _TINY_SHA1_HPP_
+#include 
+#include 
+#include 
+#include 
+
+/**
+ * \namespace sha1
+ * \brief Here is defined the SHA1 class
+ */
+namespace sha1 {
+/**
+ * \class SHA1
+ * \brief A tiny SHA1 algorithm implementation used internally in the
+ * Crow server (specifically in crow/websocket.h).
+ */
+class SHA1 {
+public:
+  typedef uint32_t digest32_t[5];
+  typedef uint8_t digest8_t[20];
+  inline static uint32_t LeftRotate(uint32_t value, size_t count) {
+    return (value << count) ^ (value >> (32 - count));
+  }
+  SHA1() { reset(); }
+  virtual ~SHA1() {}
+  SHA1(const SHA1 &s) { *this = s; }
+  const SHA1 &operator=(const SHA1 &s) {
+    memcpy(m_digest, s.m_digest, 5 * sizeof(uint32_t));
+    memcpy(m_block, s.m_block, 64);
+    m_blockByteIndex = s.m_blockByteIndex;
+    m_byteCount = s.m_byteCount;
+    return *this;
+  }
+  SHA1 &reset() {
+    m_digest[0] = 0x67452301;
+    m_digest[1] = 0xEFCDAB89;
+    m_digest[2] = 0x98BADCFE;
+    m_digest[3] = 0x10325476;
+    m_digest[4] = 0xC3D2E1F0;
+    m_blockByteIndex = 0;
+    m_byteCount = 0;
+    return *this;
+  }
+  SHA1 &processByte(uint8_t octet) {
+    this->m_block[this->m_blockByteIndex++] = octet;
+    ++this->m_byteCount;
+    if (m_blockByteIndex == 64) {
+      this->m_blockByteIndex = 0;
+      processBlock();
+    }
+    return *this;
+  }
+  SHA1 &processBlock(const void *const start, const void *const end) {
+    const uint8_t *begin = static_cast(start);
+    const uint8_t *finish = static_cast(end);
+    while (begin != finish) {
+      processByte(*begin);
+      begin++;
+    }
+    return *this;
+  }
+  SHA1 &processBytes(const void *const data, size_t len) {
+    const uint8_t *block = static_cast(data);
+    processBlock(block, block + len);
+    return *this;
+  }
+  const uint32_t *getDigest(digest32_t digest) {
+    size_t bitCount = this->m_byteCount * 8;
+    processByte(0x80);
+    if (this->m_blockByteIndex > 56) {
+      while (m_blockByteIndex != 0) {
+        processByte(0);
+      }
+      while (m_blockByteIndex < 56) {
+        processByte(0);
+      }
+    } else {
+      while (m_blockByteIndex < 56) {
+        processByte(0);
+      }
+    }
+    processByte(0);
+    processByte(0);
+    processByte(0);
+    processByte(0);
+    processByte(static_cast((bitCount >> 24) & 0xFF));
+    processByte(static_cast((bitCount >> 16) & 0xFF));
+    processByte(static_cast((bitCount >> 8) & 0xFF));
+    processByte(static_cast((bitCount) & 0xFF));
+
+    memcpy(digest, m_digest, 5 * sizeof(uint32_t));
+    return digest;
+  }
+  const uint8_t *getDigestBytes(digest8_t digest) {
+    digest32_t d32;
+    getDigest(d32);
+    size_t di = 0;
+    digest[di++] = ((d32[0] >> 24) & 0xFF);
+    digest[di++] = ((d32[0] >> 16) & 0xFF);
+    digest[di++] = ((d32[0] >> 8) & 0xFF);
+    digest[di++] = ((d32[0]) & 0xFF);
+
+    digest[di++] = ((d32[1] >> 24) & 0xFF);
+    digest[di++] = ((d32[1] >> 16) & 0xFF);
+    digest[di++] = ((d32[1] >> 8) & 0xFF);
+    digest[di++] = ((d32[1]) & 0xFF);
+
+    digest[di++] = ((d32[2] >> 24) & 0xFF);
+    digest[di++] = ((d32[2] >> 16) & 0xFF);
+    digest[di++] = ((d32[2] >> 8) & 0xFF);
+    digest[di++] = ((d32[2]) & 0xFF);
+
+    digest[di++] = ((d32[3] >> 24) & 0xFF);
+    digest[di++] = ((d32[3] >> 16) & 0xFF);
+    digest[di++] = ((d32[3] >> 8) & 0xFF);
+    digest[di++] = ((d32[3]) & 0xFF);
+
+    digest[di++] = ((d32[4] >> 24) & 0xFF);
+    digest[di++] = ((d32[4] >> 16) & 0xFF);
+    digest[di++] = ((d32[4] >> 8) & 0xFF);
+    digest[di++] = ((d32[4]) & 0xFF);
+    return digest;
+  }
+
+protected:
+  void processBlock() {
+    uint32_t w[80];
+    for (size_t i = 0; i < 16; i++) {
+      w[i] = (m_block[i * 4 + 0] << 24);
+      w[i] |= (m_block[i * 4 + 1] << 16);
+      w[i] |= (m_block[i * 4 + 2] << 8);
+      w[i] |= (m_block[i * 4 + 3]);
+    }
+    for (size_t i = 16; i < 80; i++) {
+      w[i] = LeftRotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1);
+    }
+
+    uint32_t a = m_digest[0];
+    uint32_t b = m_digest[1];
+    uint32_t c = m_digest[2];
+    uint32_t d = m_digest[3];
+    uint32_t e = m_digest[4];
+
+    for (std::size_t i = 0; i < 80; ++i) {
+      uint32_t f = 0;
+      uint32_t k = 0;
+
+      if (i < 20) {
+        f = (b & c) | (~b & d);
+        k = 0x5A827999;
+      } else if (i < 40) {
+        f = b ^ c ^ d;
+        k = 0x6ED9EBA1;
+      } else if (i < 60) {
+        f = (b & c) | (b & d) | (c & d);
+        k = 0x8F1BBCDC;
+      } else {
+        f = b ^ c ^ d;
+        k = 0xCA62C1D6;
+      }
+      uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i];
+      e = d;
+      d = c;
+      c = LeftRotate(b, 30);
+      b = a;
+      a = temp;
+    }
+
+    m_digest[0] += a;
+    m_digest[1] += b;
+    m_digest[2] += c;
+    m_digest[3] += d;
+    m_digest[4] += e;
+  }
+
+private:
+  digest32_t m_digest;
+  uint8_t m_block[64];
+  size_t m_blockByteIndex;
+  size_t m_byteCount;
+};
+} // namespace sha1
+#endif
 
+#include 
+#include 
 #include 
 #include 
 #include 
-#include 
 #include 
-#include 
-#include 
+#include 
 
-namespace crow
-{
+namespace crow {
 
 // ----------------------------------------------------------------------------
 // qs_parse (modified)
 // https://github.com/bartgrantham/qs_parse
 // ----------------------------------------------------------------------------
 /*  Similar to strncmp, but handles URL-encoding for either string  */
-int qs_strncmp(const char* s, const char* qs, size_t n);
-
+int qs_strncmp(const char *s, const char *qs, size_t n);
 
 /*  Finds the beginning of each key/value pair and stores a pointer in qs_kv.
  *  Also decodes the value portion of the k/v pair *in-place*.  In a future
  *  enhancement it will also have a compile-time option of sorting qs_kv
  *  alphabetically by key.  */
-size_t qs_parse(char* qs, char* qs_kv[], size_t qs_kv_size, bool parse_url);
-
+size_t qs_parse(char *qs, char *qs_kv[], size_t qs_kv_size, bool parse_url);
 
 /*  Used by qs_parse to decode the value portion of a k/v pair  */
-int qs_decode(char * qs);
-
+int qs_decode(char *qs);
 
 /*  Looks up the value according to the key on a pre-processed query string
  *  A future enhancement will be a compile-time option to look up the key
  *  in a pre-sorted qs_kv array via a binary search.  */
-//char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size);
- char * qs_k2v(const char * key, char * const * qs_kv, size_t qs_kv_size, int nth);
-
+// char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size);
+char *qs_k2v(const char *key, char *const *qs_kv, size_t qs_kv_size, int nth);
 
 /*  Non-destructive lookup of value, based on key.  User provides the
  *  destinaton string and length.  */
-char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len);
+char *qs_scanvalue(const char *key, const char *qs, char *val, size_t val_len);
 
 // TODO: implement sorting of the qs_kv array; for now ensure it's not compiled
 #undef _qsSORTING
 
 // isxdigit _is_ available in , but let's avoid another header instead
-#define CROW_QS_ISHEX(x)    ((((x)>='0'&&(x)<='9') || ((x)>='A'&&(x)<='F') || ((x)>='a'&&(x)<='f')) ? 1 : 0)
-#define CROW_QS_HEX2DEC(x)  (((x)>='0'&&(x)<='9') ? (x)-48 : ((x)>='A'&&(x)<='F') ? (x)-55 : ((x)>='a'&&(x)<='f') ? (x)-87 : 0)
-#define CROW_QS_ISQSCHR(x) ((((x)=='=')||((x)=='#')||((x)=='&')||((x)=='\0')) ? 0 : 1)
-
-inline int qs_strncmp(const char * s, const char * qs, size_t n)
-{
-    unsigned char u1, u2, unyb, lnyb;
-
-    while(n-- > 0)
+#define CROW_QS_ISHEX(x)                                                       \
+  ((((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F') ||                \
+    ((x) >= 'a' && (x) <= 'f'))                                                \
+       ? 1                                                                     \
+       : 0)
+#define CROW_QS_HEX2DEC(x)                                                     \
+  (((x) >= '0' && (x) <= '9')   ? (x) - 48                                     \
+   : ((x) >= 'A' && (x) <= 'F') ? (x) - 55                                     \
+   : ((x) >= 'a' && (x) <= 'f') ? (x) - 87                                     \
+                                : 0)
+#define CROW_QS_ISQSCHR(x)                                                     \
+  ((((x) == '=') || ((x) == '#') || ((x) == '&') || ((x) == '\0')) ? 0 : 1)
+
+inline int qs_strncmp(const char *s, const char *qs, size_t n) {
+  unsigned char u1, u2, unyb, lnyb;
+
+  while (n-- > 0) {
+    u1 = static_cast(*s++);
+    u2 = static_cast(*qs++);
+
+    if (!CROW_QS_ISQSCHR(u1)) { u1 = '\0'; }
+    if (!CROW_QS_ISQSCHR(u2)) { u2 = '\0'; }
+
+    if (u1 == '+') { u1 = ' '; }
+    if (u1 == '%') // easier/safer than scanf
     {
-        u1 = static_cast(*s++);
-        u2 = static_cast(*qs++);
-
-        if ( ! CROW_QS_ISQSCHR(u1) ) {  u1 = '\0';  }
-        if ( ! CROW_QS_ISQSCHR(u2) ) {  u2 = '\0';  }
-
-        if ( u1 == '+' ) {  u1 = ' ';  }
-        if ( u1 == '%' ) // easier/safer than scanf
-        {
-            unyb = static_cast(*s++);
-            lnyb = static_cast(*s++);
-            if ( CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb) )
-                u1 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb);
-            else
-                u1 = '\0';
-        }
-
-        if ( u2 == '+' ) {  u2 = ' ';  }
-        if ( u2 == '%' ) // easier/safer than scanf
-        {
-            unyb = static_cast(*qs++);
-            lnyb = static_cast(*qs++);
-            if ( CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb) )
-                u2 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb);
-            else
-                u2 = '\0';
-        }
+      unyb = static_cast(*s++);
+      lnyb = static_cast(*s++);
+      if (CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb))
+        u1 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb);
+      else
+        u1 = '\0';
+    }
 
-        if ( u1 != u2 )
-            return u1 - u2;
-        if ( u1 == '\0' )
-            return 0;
+    if (u2 == '+') { u2 = ' '; }
+    if (u2 == '%') // easier/safer than scanf
+    {
+      unyb = static_cast(*qs++);
+      lnyb = static_cast(*qs++);
+      if (CROW_QS_ISHEX(unyb) && CROW_QS_ISHEX(lnyb))
+        u2 = (CROW_QS_HEX2DEC(unyb) * 16) + CROW_QS_HEX2DEC(lnyb);
+      else
+        u2 = '\0';
     }
-    if ( CROW_QS_ISQSCHR(*qs) )
-        return -1;
-    else
-        return 0;
-}
 
+    if (u1 != u2) return u1 - u2;
+    if (u1 == '\0') return 0;
+  }
+  if (CROW_QS_ISQSCHR(*qs))
+    return -1;
+  else
+    return 0;
+}
 
-inline size_t qs_parse(char* qs, char* qs_kv[], size_t qs_kv_size, bool parse_url = true)
-{
-    size_t i, j;
-    char * substr_ptr;
+inline size_t qs_parse(char *qs, char *qs_kv[], size_t qs_kv_size,
+                       bool parse_url = true) {
+  size_t i, j;
+  char *substr_ptr;
 
-    for(i=0; i means x iterations of this loop -> means *x+1* k/v pairs
-        substr_ptr += j + 1;
-        i++;
-    }
+  i = 0;
+  while (i < qs_kv_size) {
+    qs_kv[i] = substr_ptr;
+    j = strcspn(substr_ptr, "&");
+    if (substr_ptr[j] == '\0') {
+      i++;
+      break;
+    } // x &'s -> means x iterations of this loop -> means *x+1* k/v pairs
+    substr_ptr += j + 1;
+    i++;
+  }
 
-    // we only decode the values in place, the keys could have '='s in them
-    // which will hose our ability to distinguish keys from values later
-    for(j=0; j> qs_dict_name2kv(const char * dict_name, char * const * qs_kv, size_t qs_kv_size, int nth = 0)
-{
-    size_t i;
-    size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close;
+inline std::unique_ptr>
+qs_dict_name2kv(const char *dict_name, char *const *qs_kv, size_t qs_kv_size,
+                int nth = 0) {
+  size_t i;
+  size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close;
 
-    name_len = strlen(dict_name);
+  name_len = strlen(dict_name);
 
 #ifdef _qsSORTING
 // TODO: binary search for key in the sorted qs_kv
 #else  // _qsSORTING
-    for(i=0; i 0 &&
-                 skip_to_brace_close > 0 &&
-                 nth == 0 )
-            {
-                auto key = std::string(qs_kv[i] + skip_to_brace_open, skip_to_brace_close - skip_to_brace_open);
-                auto value = std::string(qs_kv[i] + skip_to_eq);
-                return std::unique_ptr>(new std::pair(key, value));
-            }
-            else
-            {
-                --nth;
-            }
-        }
+  for (i = 0; i < qs_kv_size; i++) {
+    if (strncmp(dict_name, qs_kv[i], name_len) == 0) {
+      skip_to_eq = strcspn(qs_kv[i], "=");
+      if (qs_kv[i][skip_to_eq] == '=') skip_to_eq++;
+      skip_to_brace_open = strcspn(qs_kv[i], "[");
+      if (qs_kv[i][skip_to_brace_open] == '[') skip_to_brace_open++;
+      skip_to_brace_close = strcspn(qs_kv[i], "]");
+
+      if (skip_to_brace_open <= skip_to_brace_close && skip_to_brace_open > 0 &&
+          skip_to_brace_close > 0 && nth == 0) {
+        auto key = std::string(qs_kv[i] + skip_to_brace_open,
+                               skip_to_brace_close - skip_to_brace_open);
+        auto value = std::string(qs_kv[i] + skip_to_eq);
+        return std::unique_ptr>(
+            new std::pair(key, value));
+      } else {
+        --nth;
+      }
     }
-#endif  // _qsSORTING
+  }
+#endif // _qsSORTING
 
-    return nullptr;
+  return nullptr;
 }
 
+inline char *qs_scanvalue(const char *key, const char *qs, char *val,
+                          size_t val_len) {
+  const char *tmp = strchr(qs, '?');
 
-inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len)
-{
-    size_t i, key_len;
-    const char * tmp;
-
-    // find the beginning of the k/v substrings
-    if ( (tmp = strchr(qs, '?')) != NULL )
-        qs = tmp + 1;
+  // find the beginning of the k/v substrings
+  if (tmp != nullptr) qs = tmp + 1;
 
-    key_len = strlen(key);
-    while(qs[0] != '#' && qs[0] != '\0')
-    {
-        if ( qs_strncmp(key, qs, key_len) == 0 )
-            break;
-        qs += strcspn(qs, "&") + 1;
-    }
+  const size_t key_len = strlen(key);
+  while (*qs != '#' && *qs != '\0') {
+    if (qs_strncmp(key, qs, key_len) == 0) break;
+    qs += strcspn(qs, "&");
+    if (*qs == '&') qs++;
+  }
 
-    if ( qs[0] == '\0' ) return NULL;
+  if (qs[0] == '\0') return nullptr;
 
-    qs += strcspn(qs, "=&#");
-    if ( qs[0] == '=' )
-    {
-        qs++;
-        i = strcspn(qs, "&=#");
+  qs += strcspn(qs, "=&#");
+  if (qs[0] == '=') {
+    qs++;
+    size_t i = strcspn(qs, "&=#");
 #ifdef _MSC_VER
-        strncpy_s(val, val_len, qs, (val_len - 1)<(i + 1) ? (val_len - 1) : (i + 1));
+    strncpy_s(val, val_len, qs,
+              (val_len - 1) < (i + 1) ? (val_len - 1) : (i + 1));
 #else
-        strncpy(val, qs, (val_len - 1)<(i + 1) ? (val_len - 1) : (i + 1));
+    strncpy(val, qs, (val_len - 1) < (i + 1) ? (val_len - 1) : (i + 1));
 #endif
-		qs_decode(val);
-    }
-    else
-    {
-        if ( val_len > 0 )
-            val[0] = '\0';
-    }
+    qs_decode(val);
+  } else {
+    if (val_len > 0) val[0] = '\0';
+  }
 
-    return val;
-}
+  return val;
 }
+} // namespace crow
 // ----------------------------------------------------------------------------
 
+namespace crow {
+struct request;
+/// A class to represent any data coming after the `?` in the request URL into
+/// key-value pairs.
+class query_string {
+public:
+  static const int MAX_KEY_VALUE_PAIRS_COUNT = 256;
 
-namespace crow
-{
-    struct request;
-    /// A class to represent any data coming after the `?` in the request URL into key-value pairs.
-    class query_string
-    {
-    public:
-        static const int MAX_KEY_VALUE_PAIRS_COUNT = 256;
+  query_string() = default;
 
-        query_string() = default;
+  query_string(const query_string &qs) : url_(qs.url_) {
+    for (auto p : qs.key_value_pairs_) {
+      key_value_pairs_.push_back((char *)(p - qs.url_.c_str() + url_.c_str()));
+    }
+  }
 
-        query_string(const query_string& qs):
-          url_(qs.url_)
-        {
-            for (auto p : qs.key_value_pairs_)
-            {
-                key_value_pairs_.push_back((char*)(p - qs.url_.c_str() + url_.c_str()));
-            }
-        }
+  query_string &operator=(const query_string &qs) {
+    url_ = qs.url_;
+    key_value_pairs_.clear();
+    for (auto p : qs.key_value_pairs_) {
+      key_value_pairs_.push_back((char *)(p - qs.url_.c_str() + url_.c_str()));
+    }
+    return *this;
+  }
 
-        query_string& operator=(const query_string& qs)
-        {
-            url_ = qs.url_;
-            key_value_pairs_.clear();
-            for (auto p : qs.key_value_pairs_)
-            {
-                key_value_pairs_.push_back((char*)(p - qs.url_.c_str() + url_.c_str()));
-            }
-            return *this;
-        }
+  query_string &operator=(query_string &&qs) noexcept {
+    key_value_pairs_ = std::move(qs.key_value_pairs_);
+    char *old_data = (char *)qs.url_.c_str();
+    url_ = std::move(qs.url_);
+    for (auto &p : key_value_pairs_) {
+      p += (char *)url_.c_str() - old_data;
+    }
+    return *this;
+  }
 
-        query_string& operator=(query_string&& qs) noexcept
-        {
-            key_value_pairs_ = std::move(qs.key_value_pairs_);
-            char* old_data = (char*)qs.url_.c_str();
-            url_ = std::move(qs.url_);
-            for (auto& p : key_value_pairs_)
-            {
-                p += (char*)url_.c_str() - old_data;
-            }
-            return *this;
-        }
+  query_string(std::string params, bool url = true) : url_(std::move(params)) {
+    if (url_.empty()) return;
 
+    key_value_pairs_.resize(MAX_KEY_VALUE_PAIRS_COUNT);
+    size_t count = qs_parse(&url_[0], &key_value_pairs_[0],
+                            MAX_KEY_VALUE_PAIRS_COUNT, url);
 
-        query_string(std::string params, bool url = true):
-          url_(std::move(params))
-        {
-            if (url_.empty())
-                return;
+    key_value_pairs_.resize(count);
+    key_value_pairs_.shrink_to_fit();
+  }
 
-            key_value_pairs_.resize(MAX_KEY_VALUE_PAIRS_COUNT);
-            size_t count = qs_parse(&url_[0], &key_value_pairs_[0], MAX_KEY_VALUE_PAIRS_COUNT, url);
+  void clear() {
+    key_value_pairs_.clear();
+    url_.clear();
+  }
 
-            key_value_pairs_.resize(count);
-            key_value_pairs_.shrink_to_fit();
-        }
+  friend std::ostream &operator<<(std::ostream &os, const query_string &qs) {
+    os << "[ ";
+    for (size_t i = 0; i < qs.key_value_pairs_.size(); ++i) {
+      if (i) os << ", ";
+      os << qs.key_value_pairs_[i];
+    }
+    os << " ]";
+    return os;
+  }
 
-        void clear()
-        {
-            key_value_pairs_.clear();
-            url_.clear();
-        }
+  /// Get a value from a name, used for `?name=value`.
 
-        friend std::ostream& operator<<(std::ostream& os, const query_string& qs)
-        {
-            os << "[ ";
-            for (size_t i = 0; i < qs.key_value_pairs_.size(); ++i)
-            {
-                if (i)
-                    os << ", ";
-                os << qs.key_value_pairs_[i];
-            }
-            os << " ]";
-            return os;
+  ///
+  /// Note: this method returns the value of the first occurrence of the key
+  /// only, to return all occurrences, see \ref get_list().
+  char *get(const std::string &name) const {
+    char *ret =
+        qs_k2v(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size());
+    return ret;
+  }
+
+  /// Works similar to \ref get() except it removes the item from the query
+  /// string.
+  char *pop(const std::string &name) {
+    char *ret = get(name);
+    if (ret != nullptr) {
+      const std::string key_name = name + '=';
+      for (unsigned int i = 0; i < key_value_pairs_.size(); i++) {
+        std::string str_item(key_value_pairs_[i]);
+        if (str_item.find(key_name) == 0) {
+          key_value_pairs_.erase(key_value_pairs_.begin() + i);
+          break;
         }
+      }
+    }
+    return ret;
+  }
 
-        /// Get a value from a name, used for `?name=value`.
+  /// Returns a list of values, passed as
+  /// `?name[]=value1&name[]=value2&...name[]=valuen` with n being the size of
+  /// the list.
+
+  ///
+  /// Note: Square brackets in the above example are controlled by
+  /// `use_brackets` boolean (true by default). If set to false, the example
+  /// becomes `?name=value1,name=value2...name=valuen`
+  std::vector get_list(const std::string &name,
+                               bool use_brackets = true) const {
+    std::vector ret;
+    std::string plus = name + (use_brackets ? "[]" : "");
+    char *element = nullptr;
+
+    int count = 0;
+    while (1) {
+      element = qs_k2v(plus.c_str(), key_value_pairs_.data(),
+                       key_value_pairs_.size(), count++);
+      if (!element) break;
+      ret.push_back(element);
+    }
+    return ret;
+  }
 
-        ///
-        /// Note: this method returns the value of the first occurrence of the key only, to return all occurrences, see \ref get_list().
-        char* get(const std::string& name) const
-        {
-            char* ret = qs_k2v(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size());
-            return ret;
+  /// Similar to \ref get_list() but it removes the
+  std::vector pop_list(const std::string &name,
+                               bool use_brackets = true) {
+    std::vector ret = get_list(name, use_brackets);
+    const size_t name_len = name.length();
+    if (!ret.empty()) {
+      for (unsigned int i = 0; i < key_value_pairs_.size(); i++) {
+        std::string str_item(key_value_pairs_[i]);
+        if (str_item.find(name) == 0) {
+          if (use_brackets && str_item.find("[]=", name_len) == name_len) {
+            key_value_pairs_.erase(key_value_pairs_.begin() + i--);
+          } else if (!use_brackets &&
+                     str_item.find('=', name_len) == name_len) {
+            key_value_pairs_.erase(key_value_pairs_.begin() + i--);
+          }
         }
+      }
+    }
+    return ret;
+  }
 
-        /// Works similar to \ref get() except it removes the item from the query string.
-        char* pop(const std::string& name)
-        {
-            char* ret = get(name);
-            if (ret != nullptr)
-            {
-                for (unsigned int i = 0; i < key_value_pairs_.size(); i++)
-                {
-                    std::string str_item(key_value_pairs_[i]);
-                    if (str_item.substr(0, name.size() + 1) == name + '=')
-                    {
-                        key_value_pairs_.erase(key_value_pairs_.begin() + i);
-                        break;
-                    }
-                }
-            }
-            return ret;
+  /// Works similar to \ref get_list() except the brackets are mandatory must
+  /// not be empty.
+
+  ///
+  /// For example calling `get_dict(yourname)` on
+  /// `?yourname[sub1]=42&yourname[sub2]=84` would give a map containing `{sub1
+  /// : 42, sub2 : 84}`.
+  ///
+  /// if your query string has both empty brackets and ones with a key inside,
+  /// use pop_list() to get all the values without a key before running this
+  /// method.
+  std::unordered_map
+  get_dict(const std::string &name) const {
+    std::unordered_map ret;
+
+    int count = 0;
+    while (1) {
+      if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(),
+                                         key_value_pairs_.size(), count++))
+        ret.insert(*element);
+      else
+        break;
+    }
+    return ret;
+  }
+
+  /// Works the same as \ref get_dict() but removes the values from the query
+  /// string.
+  std::unordered_map
+  pop_dict(const std::string &name) {
+    const std::string name_value = name + '[';
+    std::unordered_map ret = get_dict(name);
+    if (!ret.empty()) {
+      for (unsigned int i = 0; i < key_value_pairs_.size(); i++) {
+        std::string str_item(key_value_pairs_[i]);
+        if (str_item.find(name_value) == 0) {
+          key_value_pairs_.erase(key_value_pairs_.begin() + i--);
         }
+      }
+    }
+    return ret;
+  }
 
-        /// Returns a list of values, passed as `?name[]=value1&name[]=value2&...name[]=valuen` with n being the size of the list.
+  std::vector keys() const {
+    std::vector keys;
+    keys.reserve(key_value_pairs_.size());
 
-        ///
-        /// Note: Square brackets in the above example are controlled by `use_brackets` boolean (true by default). If set to false, the example becomes `?name=value1,name=value2...name=valuen`
-        std::vector get_list(const std::string& name, bool use_brackets = true) const
-        {
-            std::vector ret;
-            std::string plus = name + (use_brackets ? "[]" : "");
-            char* element = nullptr;
+    for (const char *const element : key_value_pairs_) {
+      const char *delimiter = strchr(element, '=');
+      if (delimiter)
+        keys.emplace_back(element, delimiter);
+      else
+        keys.emplace_back(element);
+    }
 
-            int count = 0;
-            while (1)
-            {
-                element = qs_k2v(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++);
-                if (!element)
-                    break;
-                ret.push_back(element);
-            }
-            return ret;
-        }
+    return keys;
+  }
 
-        /// Similar to \ref get_list() but it removes the
-        std::vector pop_list(const std::string& name, bool use_brackets = true)
-        {
-            std::vector ret = get_list(name, use_brackets);
-            if (!ret.empty())
-            {
-                for (unsigned int i = 0; i < key_value_pairs_.size(); i++)
-                {
-                    std::string str_item(key_value_pairs_[i]);
-                    if ((use_brackets ? (str_item.substr(0, name.size() + 3) == name + "[]=") : (str_item.substr(0, name.size() + 1) == name + '=')))
-                    {
-                        key_value_pairs_.erase(key_value_pairs_.begin() + i--);
-                    }
-                }
-            }
-            return ret;
-        }
+private:
+  std::string url_;
+  std::vector key_value_pairs_;
+};
 
-        /// Works similar to \ref get_list() except the brackets are mandatory must not be empty.
+} // namespace crow
 
-        ///
-        /// For example calling `get_dict(yourname)` on `?yourname[sub1]=42&yourname[sub2]=84` would give a map containing `{sub1 : 42, sub2 : 84}`.
-        ///
-        /// if your query string has both empty brackets and ones with a key inside, use pop_list() to get all the values without a key before running this method.
-        std::unordered_map get_dict(const std::string& name) const
-        {
-            std::unordered_map ret;
+// This file is generated from nginx/conf/mime.types using nginx_mime2cpp.py on
+// 2021-12-03.
+#include 
+#include 
 
-            int count = 0;
-            while (1)
-            {
-                if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++))
-                    ret.insert(*element);
-                else
-                    break;
-            }
-            return ret;
-        }
-
-        /// Works the same as \ref get_dict() but removes the values from the query string.
-        std::unordered_map pop_dict(const std::string& name)
-        {
-            std::unordered_map ret = get_dict(name);
-            if (!ret.empty())
-            {
-                for (unsigned int i = 0; i < key_value_pairs_.size(); i++)
-                {
-                    std::string str_item(key_value_pairs_[i]);
-                    if (str_item.substr(0, name.size() + 1) == name + '[')
-                    {
-                        key_value_pairs_.erase(key_value_pairs_.begin() + i--);
-                    }
-                }
-            }
-            return ret;
-        }
-
-        std::vector keys() const
-        {
-            std::vector keys;
-            keys.reserve(key_value_pairs_.size());
-
-            for (const char* const element : key_value_pairs_)
-            {
-                const char* delimiter = strchr(element, '=');
-                if (delimiter)
-                    keys.emplace_back(element, delimiter);
-                else
-                    keys.emplace_back(element);
-            }
-
-            return keys;
-        }
-
-    private:
-        std::string url_;
-        std::vector key_value_pairs_;
-    };
-
-} // namespace crow
-
-#ifdef CROW_ENABLE_COMPRESSION
-
-#include 
-#include 
-
-// http://zlib.net/manual.html
-namespace crow // NOTE: Already documented in "crow/app.h"
-{
-    namespace compression
-    {
-        // Values used in the 'windowBits' parameter for deflateInit2.
-        enum algorithm
-        {
-            // 15 is the default value for deflate
-            DEFLATE = 15,
-            // windowBits can also be greater than 15 for optional gzip encoding.
-            // Add 16 to windowBits to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
-            GZIP = 15 | 16,
-        };
-
-        inline std::string compress_string(std::string const& str, algorithm algo)
-        {
-            std::string compressed_str;
-            z_stream stream{};
-            // Initialize with the default values
-            if (::deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, algo, 8, Z_DEFAULT_STRATEGY) == Z_OK)
-            {
-                char buffer[8192];
-
-                stream.avail_in = str.size();
-                // zlib does not take a const pointer. The data is not altered.
-                stream.next_in = const_cast(reinterpret_cast(str.c_str()));
-
-                int code = Z_OK;
-                do
-                {
-                    stream.avail_out = sizeof(buffer);
-                    stream.next_out = reinterpret_cast(&buffer[0]);
-
-                    code = ::deflate(&stream, Z_FINISH);
-                    // Successful and non-fatal error code returned by deflate when used with Z_FINISH flush
-                    if (code == Z_OK || code == Z_STREAM_END)
-                    {
-                        std::copy(&buffer[0], &buffer[sizeof(buffer) - stream.avail_out], std::back_inserter(compressed_str));
-                    }
-
-                } while (code == Z_OK);
-
-                if (code != Z_STREAM_END)
-                    compressed_str.clear();
-
-                ::deflateEnd(&stream);
-            }
-
-            return compressed_str;
-        }
-
-        inline std::string decompress_string(std::string const& deflated_string)
-        {
-            std::string inflated_string;
-            Bytef tmp[8192];
-
-            z_stream zstream{};
-            zstream.avail_in = deflated_string.size();
-            // Nasty const_cast but zlib won't alter its contents
-            zstream.next_in = const_cast(reinterpret_cast(deflated_string.c_str()));
-            // Initialize with automatic header detection, for gzip support
-            if (::inflateInit2(&zstream, MAX_WBITS | 32) == Z_OK)
-            {
-                do
-                {
-                    zstream.avail_out = sizeof(tmp);
-                    zstream.next_out = &tmp[0];
-
-                    auto ret = ::inflate(&zstream, Z_NO_FLUSH);
-                    if (ret == Z_OK || ret == Z_STREAM_END)
-                    {
-                        std::copy(&tmp[0], &tmp[sizeof(tmp) - zstream.avail_out], std::back_inserter(inflated_string));
-                    }
-                    else
-                    {
-                        // Something went wrong with inflate; make sure we return an empty string
-                        inflated_string.clear();
-                        break;
-                    }
-
-                } while (zstream.avail_out == 0);
-
-                // Free zlib's internal memory
-                ::inflateEnd(&zstream);
-            }
-
-            return inflated_string;
-        }
-    } // namespace compression
-} // namespace crow
-
-#endif
-
-/*
- * SHA1 Wikipedia Page: http://en.wikipedia.org/wiki/SHA-1
- * 
- * Copyright (c) 2012-22 SAURAV MOHAPATRA 
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/** 
- * \file TinySHA1.hpp
- * \author SAURAV MOHAPATRA 
- * \date 2012-22
- * \brief TinySHA1 - a header only implementation of the SHA1 algorithm in C++. Based
- * on the implementation in boost::uuid::details.
- *
- * In this file are defined:
- * - sha1::SHA1
- */
-#ifndef _TINY_SHA1_HPP_
-#define _TINY_SHA1_HPP_
-#include 
-#include 
-#include 
-#include 
-
-/**
- * \namespace sha1
- * \brief Here is defined the SHA1 class
- */
-namespace sha1
-{
-	/**
-	 * \class SHA1
-	 * \brief A tiny SHA1 algorithm implementation used internally in the
-	 * Crow server (specifically in crow/websocket.h).
-	 */
-	class SHA1
-	{
-	public:
-		typedef uint32_t digest32_t[5];
-		typedef uint8_t digest8_t[20];
-		inline static uint32_t LeftRotate(uint32_t value, size_t count) {
-			return (value << count) ^ (value >> (32-count));
-		}
-		SHA1(){ reset(); }
-		virtual ~SHA1() {}
-		SHA1(const SHA1& s) { *this = s; }
-		const SHA1& operator = (const SHA1& s) {
-			memcpy(m_digest, s.m_digest, 5 * sizeof(uint32_t));
-			memcpy(m_block, s.m_block, 64);
-			m_blockByteIndex = s.m_blockByteIndex;
-			m_byteCount = s.m_byteCount;
-			return *this;
-		}
-		SHA1& reset() {
-			m_digest[0] = 0x67452301;
-			m_digest[1] = 0xEFCDAB89;
-			m_digest[2] = 0x98BADCFE;
-			m_digest[3] = 0x10325476;
-			m_digest[4] = 0xC3D2E1F0;
-			m_blockByteIndex = 0;
-			m_byteCount = 0;
-			return *this;
-		}
-		SHA1& processByte(uint8_t octet) {
-			this->m_block[this->m_blockByteIndex++] = octet;
-			++this->m_byteCount;
-			if(m_blockByteIndex == 64) {
-				this->m_blockByteIndex = 0;
-				processBlock();
-			}
-			return *this;
-		}
-		SHA1& processBlock(const void* const start, const void* const end) {
-			const uint8_t* begin = static_cast(start);
-			const uint8_t* finish = static_cast(end);
-			while(begin != finish) {
-				processByte(*begin);
-				begin++;
-			}
-			return *this;
-		}
-		SHA1& processBytes(const void* const data, size_t len) {
-			const uint8_t* block = static_cast(data);
-			processBlock(block, block + len);
-			return *this;
-		}
-		const uint32_t* getDigest(digest32_t digest) {
-			size_t bitCount = this->m_byteCount * 8;
-			processByte(0x80);
-			if (this->m_blockByteIndex > 56) {
-				while (m_blockByteIndex != 0) {
-					processByte(0);
-				}
-				while (m_blockByteIndex < 56) {
-					processByte(0);
-				}
-			} else {
-				while (m_blockByteIndex < 56) {
-					processByte(0);
-				}
-			}
-			processByte(0);
-			processByte(0);
-			processByte(0);
-			processByte(0);
-			processByte( static_cast((bitCount>>24) & 0xFF));
-			processByte( static_cast((bitCount>>16) & 0xFF));
-			processByte( static_cast((bitCount>>8 ) & 0xFF));
-			processByte( static_cast((bitCount)     & 0xFF));
-	
-			memcpy(digest, m_digest, 5 * sizeof(uint32_t));
-			return digest;
-		}
-		const uint8_t* getDigestBytes(digest8_t digest) {
-			digest32_t d32;
-			getDigest(d32);
-			size_t di = 0;
-			digest[di++] = ((d32[0] >> 24) & 0xFF);
-			digest[di++] = ((d32[0] >> 16) & 0xFF);
-			digest[di++] = ((d32[0] >> 8) & 0xFF);
-			digest[di++] = ((d32[0]) & 0xFF);
-			
-			digest[di++] = ((d32[1] >> 24) & 0xFF);
-			digest[di++] = ((d32[1] >> 16) & 0xFF);
-			digest[di++] = ((d32[1] >> 8) & 0xFF);
-			digest[di++] = ((d32[1]) & 0xFF);
-			
-			digest[di++] = ((d32[2] >> 24) & 0xFF);
-			digest[di++] = ((d32[2] >> 16) & 0xFF);
-			digest[di++] = ((d32[2] >> 8) & 0xFF);
-			digest[di++] = ((d32[2]) & 0xFF);
-			
-			digest[di++] = ((d32[3] >> 24) & 0xFF);
-			digest[di++] = ((d32[3] >> 16) & 0xFF);
-			digest[di++] = ((d32[3] >> 8) & 0xFF);
-			digest[di++] = ((d32[3]) & 0xFF);
-			
-			digest[di++] = ((d32[4] >> 24) & 0xFF);
-			digest[di++] = ((d32[4] >> 16) & 0xFF);
-			digest[di++] = ((d32[4] >> 8) & 0xFF);
-			digest[di++] = ((d32[4]) & 0xFF);
-			return digest;
-		}
-	
-	protected:
-		void processBlock() {
-			uint32_t w[80];
-			for (size_t i = 0; i < 16; i++) {
-				w[i]  = (m_block[i*4 + 0] << 24);
-				w[i] |= (m_block[i*4 + 1] << 16);
-				w[i] |= (m_block[i*4 + 2] << 8);
-				w[i] |= (m_block[i*4 + 3]);
-			}
-			for (size_t i = 16; i < 80; i++) {
-				w[i] = LeftRotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
-			}
-	
-			uint32_t a = m_digest[0];
-			uint32_t b = m_digest[1];
-			uint32_t c = m_digest[2];
-			uint32_t d = m_digest[3];
-			uint32_t e = m_digest[4];
-	
-			for (std::size_t i=0; i<80; ++i) {
-				uint32_t f = 0;
-				uint32_t k = 0;
-	
-				if (i<20) {
-					f = (b & c) | (~b & d);
-					k = 0x5A827999;
-				} else if (i<40) {
-					f = b ^ c ^ d;
-					k = 0x6ED9EBA1;
-				} else if (i<60) {
-					f = (b & c) | (b & d) | (c & d);
-					k = 0x8F1BBCDC;
-				} else {
-					f = b ^ c ^ d;
-					k = 0xCA62C1D6;
-				}
-				uint32_t temp = LeftRotate(a, 5) + f + e + k + w[i];
-				e = d;
-				d = c;
-				c = LeftRotate(b, 30);
-				b = a;
-				a = temp;
-			}
-	
-			m_digest[0] += a;
-			m_digest[1] += b;
-			m_digest[2] += c;
-			m_digest[3] += d;
-			m_digest[4] += e;
-		}
-	private:
-		digest32_t m_digest;
-		uint8_t m_block[64];
-		size_t m_blockByteIndex;
-		size_t m_byteCount;
-	};
+namespace crow {
+const std::unordered_map mime_types{
+    {"gz", "application/gzip"},
+    {"shtml", "text/html"},
+    {"htm", "text/html"},
+    {"html", "text/html"},
+    {"css", "text/css"},
+    {"xml", "text/xml"},
+    {"gif", "image/gif"},
+    {"jpg", "image/jpeg"},
+    {"jpeg", "image/jpeg"},
+    {"js", "application/javascript"},
+    {"atom", "application/atom+xml"},
+    {"rss", "application/rss+xml"},
+    {"mml", "text/mathml"},
+    {"txt", "text/plain"},
+    {"jad", "text/vnd.sun.j2me.app-descriptor"},
+    {"wml", "text/vnd.wap.wml"},
+    {"htc", "text/x-component"},
+    {"avif", "image/avif"},
+    {"png", "image/png"},
+    {"svgz", "image/svg+xml"},
+    {"svg", "image/svg+xml"},
+    {"tiff", "image/tiff"},
+    {"tif", "image/tiff"},
+    {"wbmp", "image/vnd.wap.wbmp"},
+    {"webp", "image/webp"},
+    {"ico", "image/x-icon"},
+    {"jng", "image/x-jng"},
+    {"bmp", "image/x-ms-bmp"},
+    {"woff", "font/woff"},
+    {"woff2", "font/woff2"},
+    {"ear", "application/java-archive"},
+    {"war", "application/java-archive"},
+    {"jar", "application/java-archive"},
+    {"json", "application/json"},
+    {"hqx", "application/mac-binhex40"},
+    {"doc", "application/msword"},
+    {"pdf", "application/pdf"},
+    {"ai", "application/postscript"},
+    {"eps", "application/postscript"},
+    {"ps", "application/postscript"},
+    {"rtf", "application/rtf"},
+    {"m3u8", "application/vnd.apple.mpegurl"},
+    {"kml", "application/vnd.google-earth.kml+xml"},
+    {"kmz", "application/vnd.google-earth.kmz"},
+    {"xls", "application/vnd.ms-excel"},
+    {"eot", "application/vnd.ms-fontobject"},
+    {"ppt", "application/vnd.ms-powerpoint"},
+    {"odg", "application/vnd.oasis.opendocument.graphics"},
+    {"odp", "application/vnd.oasis.opendocument.presentation"},
+    {"ods", "application/vnd.oasis.opendocument.spreadsheet"},
+    {"odt", "application/vnd.oasis.opendocument.text"},
+    {"pptx", "application/"
+             "vnd.openxmlformats-officedocument.presentationml.presentation"},
+    {"xlsx",
+     "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
+    {"docx",
+     "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
+    {"wmlc", "application/vnd.wap.wmlc"},
+    {"wasm", "application/wasm"},
+    {"7z", "application/x-7z-compressed"},
+    {"cco", "application/x-cocoa"},
+    {"jardiff", "application/x-java-archive-diff"},
+    {"jnlp", "application/x-java-jnlp-file"},
+    {"run", "application/x-makeself"},
+    {"pm", "application/x-perl"},
+    {"pl", "application/x-perl"},
+    {"pdb", "application/x-pilot"},
+    {"prc", "application/x-pilot"},
+    {"rar", "application/x-rar-compressed"},
+    {"rpm", "application/x-redhat-package-manager"},
+    {"sea", "application/x-sea"},
+    {"swf", "application/x-shockwave-flash"},
+    {"sit", "application/x-stuffit"},
+    {"tk", "application/x-tcl"},
+    {"tcl", "application/x-tcl"},
+    {"crt", "application/x-x509-ca-cert"},
+    {"pem", "application/x-x509-ca-cert"},
+    {"der", "application/x-x509-ca-cert"},
+    {"xpi", "application/x-xpinstall"},
+    {"xhtml", "application/xhtml+xml"},
+    {"xspf", "application/xspf+xml"},
+    {"zip", "application/zip"},
+    {"dll", "application/octet-stream"},
+    {"exe", "application/octet-stream"},
+    {"bin", "application/octet-stream"},
+    {"deb", "application/octet-stream"},
+    {"dmg", "application/octet-stream"},
+    {"img", "application/octet-stream"},
+    {"iso", "application/octet-stream"},
+    {"msm", "application/octet-stream"},
+    {"msp", "application/octet-stream"},
+    {"msi", "application/octet-stream"},
+    {"kar", "audio/midi"},
+    {"midi", "audio/midi"},
+    {"mid", "audio/midi"},
+    {"mp3", "audio/mpeg"},
+    {"ogg", "audio/ogg"},
+    {"m4a", "audio/x-m4a"},
+    {"ra", "audio/x-realaudio"},
+    {"3gp", "video/3gpp"},
+    {"3gpp", "video/3gpp"},
+    {"ts", "video/mp2t"},
+    {"mp4", "video/mp4"},
+    {"mpg", "video/mpeg"},
+    {"mpeg", "video/mpeg"},
+    {"mov", "video/quicktime"},
+    {"webm", "video/webm"},
+    {"flv", "video/x-flv"},
+    {"m4v", "video/x-m4v"},
+    {"mng", "video/x-mng"},
+    {"asf", "video/x-ms-asf"},
+    {"asx", "video/x-ms-asf"},
+    {"wmv", "video/x-ms-wmv"},
+    {"avi", "video/x-msvideo"}};
 }
-#endif
 
 // settings for crow
 // TODO(ipkn) replace with runtime config. libucl?
 
 /* #ifdef - enables debug mode */
-//#define CROW_ENABLE_DEBUG
+// #define CROW_ENABLE_DEBUG
 
 /* #ifdef - enables logging */
 #define CROW_ENABLE_LOGGING
 
-/* #ifdef - enforces section 5.2 and 6.1 of RFC6455 (only accepting masked messages from clients) */
-//#define CROW_ENFORCE_WS_SPEC
+/* #ifdef - enforces section 5.2 and 6.1 of RFC6455 (only accepting masked
+ * messages from clients) */
+// #define CROW_ENFORCE_WS_SPEC
 
 /* #define - specifies log level */
 /*
@@ -1012,22 +968,6 @@ namespace sha1
 #endif
 
 // compiler flags
-#if defined(_MSVC_LANG) && _MSVC_LANG >= 201402L
-#define CROW_CAN_USE_CPP14
-#endif
-#if __cplusplus >= 201402L
-#define CROW_CAN_USE_CPP14
-#endif
-
-#if defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
-#define CROW_CAN_USE_CPP17
-#endif
-#if __cplusplus >= 201703L
-#define CROW_CAN_USE_CPP17
-#if defined(__GNUC__) && __GNUC__ < 8
-#define CROW_FILESYSTEM_IS_EXPERIMENTAL
-#endif
-#endif
 
 #if defined(_MSC_VER)
 #if _MSC_VER < 1900
@@ -1037,15 +977,6 @@ namespace sha1
 #endif
 #endif
 
-#if defined(__GNUC__) && __GNUC__ == 8 && __GNUC_MINOR__ < 4
-#if __cplusplus > 201103L
-#define CROW_GCC83_WORKAROUND
-#else
-#error "GCC 8.1 - 8.3 has a bug that prevents Crow from compiling with C++11. Please update GCC to > 8.3 or use C++ > 11."
-#endif
-#endif
-
-
 #ifdef CROW_USE_BOOST
 #include 
 #include 
@@ -1063,9463 +994,9072 @@ namespace sha1
 #endif
 #endif
 
-#if (CROW_USE_BOOST && BOOST_VERSION >= 107000) || (ASIO_VERSION >= 101300)
-#define GET_IO_SERVICE(s) ((asio::io_context&)(s).get_executor().context())
+#if (defined(CROW_USE_BOOST) && BOOST_VERSION >= 107000) ||                    \
+    (ASIO_VERSION >= 101008)
+#define GET_IO_CONTEXT(s) ((asio::io_context &)(s).get_executor().context())
 #else
-#define GET_IO_SERVICE(s) ((s).get_io_service())
+#define GET_IO_CONTEXT(s) ((s).get_io_service())
 #endif
 
-namespace crow
-{
+namespace crow {
 #ifdef CROW_USE_BOOST
-    namespace asio = boost::asio;
-    using error_code = boost::system::error_code;
+namespace asio = boost::asio;
+using error_code = boost::system::error_code;
 #else
-    using error_code = asio::error_code;
+using error_code = asio::error_code;
 #endif
-    using tcp = asio::ip::tcp;
+using tcp = asio::ip::tcp;
+using stream_protocol = asio::local::stream_protocol;
 
-    /// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream
-    struct SocketAdaptor
-    {
-        using context = void;
-        SocketAdaptor(asio::io_service& io_service, context*):
-          socket_(io_service)
-        {}
+/// A wrapper for the asio::ip::tcp::socket and asio::ssl::stream
+struct SocketAdaptor {
+  using context = void;
+  SocketAdaptor(asio::io_context &io_context, context *)
+      : socket_(io_context) {}
 
-        asio::io_service& get_io_service()
-        {
-            return GET_IO_SERVICE(socket_);
-        }
+  asio::io_context &get_io_context() { return GET_IO_CONTEXT(socket_); }
 
-        /// Get the TCP socket handling data trasfers, regardless of what layer is handling transfers on top of the socket.
-        tcp::socket& raw_socket()
-        {
-            return socket_;
-        }
+  /// Get the TCP socket handling data transfers, regardless of what layer is
+  /// handling transfers on top of the socket.
+  tcp::socket &raw_socket() { return socket_; }
 
-        /// Get the object handling data transfers, this can be either a TCP socket or an SSL stream (if SSL is enabled).
-        tcp::socket& socket()
-        {
-            return socket_;
-        }
+  /// Get the object handling data transfers, this can be either a TCP socket or
+  /// an SSL stream (if SSL is enabled).
+  tcp::socket &socket() { return socket_; }
 
-        tcp::endpoint remote_endpoint()
-        {
-            return socket_.remote_endpoint();
-        }
+  tcp::endpoint remote_endpoint() const { return socket_.remote_endpoint(); }
 
-        bool is_open()
-        {
-            return socket_.is_open();
-        }
+  std::string address() const {
+    return socket_.remote_endpoint().address().to_string();
+  }
 
-        void close()
-        {
-            error_code ec;
-            socket_.close(ec);
-        }
+  bool is_open() const { return socket_.is_open(); }
 
-        void shutdown_readwrite()
-        {
-            error_code ec;
-            socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
-        }
+  void close() {
+    error_code ec;
+    socket_.close(ec);
+  }
 
-        void shutdown_write()
-        {
-            error_code ec;
-            socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
-        }
+  void shutdown_readwrite() {
+    error_code ec;
+    socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
+  }
 
-        void shutdown_read()
-        {
-            error_code ec;
-            socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
-        }
+  void shutdown_write() {
+    error_code ec;
+    socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
+  }
 
-        template
-        void start(F f)
-        {
-            f(error_code());
-        }
+  void shutdown_read() {
+    error_code ec;
+    socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
+  }
 
-        tcp::socket socket_;
-    };
+  template  void start(F f) { f(error_code()); }
 
-#ifdef CROW_ENABLE_SSL
-    struct SSLAdaptor
-    {
-        using context = asio::ssl::context;
-        using ssl_socket_t = asio::ssl::stream;
-        SSLAdaptor(asio::io_service& io_service, context* ctx):
-          ssl_socket_(new ssl_socket_t(io_service, *ctx))
-        {}
+  tcp::socket socket_;
+};
 
-        asio::ssl::stream& socket()
-        {
-            return *ssl_socket_;
-        }
+struct UnixSocketAdaptor {
+  using context = void;
+  UnixSocketAdaptor(asio::io_context &io_context, context *)
+      : socket_(io_context) {}
 
-        tcp::socket::lowest_layer_type&
-          raw_socket()
-        {
-            return ssl_socket_->lowest_layer();
-        }
+  asio::io_context &get_io_context() { return GET_IO_CONTEXT(socket_); }
 
-        tcp::endpoint remote_endpoint()
-        {
-            return raw_socket().remote_endpoint();
-        }
+  stream_protocol::socket &raw_socket() { return socket_; }
 
-        bool is_open()
-        {
-            return ssl_socket_ ? raw_socket().is_open() : false;
-        }
+  stream_protocol::socket &socket() { return socket_; }
 
-        void close()
-        {
-            if (is_open())
-            {
-                error_code ec;
-                raw_socket().close(ec);
-            }
-        }
+  stream_protocol::endpoint remote_endpoint() {
+    return socket_.local_endpoint();
+  }
 
-        void shutdown_readwrite()
-        {
-            if (is_open())
-            {
-                error_code ec;
-                raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
-            }
-        }
+  std::string address() const { return ""; }
 
-        void shutdown_write()
-        {
-            if (is_open())
-            {
-                error_code ec;
-                raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
-            }
-        }
+  bool is_open() { return socket_.is_open(); }
 
-        void shutdown_read()
-        {
-            if (is_open())
-            {
-                error_code ec;
-                raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
-            }
-        }
+  void close() {
+    error_code ec;
+    socket_.close(ec);
+  }
 
-        asio::io_service& get_io_service()
-        {
-            return GET_IO_SERVICE(raw_socket());
-        }
+  void shutdown_readwrite() {
+    error_code ec;
+    socket_.shutdown(asio::socket_base::shutdown_type::shutdown_both, ec);
+  }
 
-        template
-        void start(F f)
-        {
-            ssl_socket_->async_handshake(asio::ssl::stream_base::server,
-                                         [f](const error_code& ec) {
-                                             f(ec);
-                                         });
-        }
+  void shutdown_write() {
+    error_code ec;
+    socket_.shutdown(asio::socket_base::shutdown_type::shutdown_send, ec);
+  }
 
-        std::unique_ptr> ssl_socket_;
-    };
-#endif
-} // namespace crow
+  void shutdown_read() {
+    error_code ec;
+    socket_.shutdown(asio::socket_base::shutdown_type::shutdown_receive, ec);
+  }
 
+  template  void start(F f) { f(error_code()); }
 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
+  stream_protocol::socket socket_;
+};
 
+#ifdef CROW_ENABLE_SSL
+struct SSLAdaptor {
+  using context = asio::ssl::context;
+  using ssl_socket_t = asio::ssl::stream;
+  SSLAdaptor(asio::io_context &io_context, context *ctx)
+      : ssl_socket_(new ssl_socket_t(io_context, *ctx)) {}
 
-#if defined(CROW_CAN_USE_CPP17) && !defined(CROW_FILESYSTEM_IS_EXPERIMENTAL)
-#include 
-#endif
+  asio::ssl::stream &socket() { return *ssl_socket_; }
 
-// TODO(EDev): Adding C++20's [[likely]] and [[unlikely]] attributes might be useful
-#if defined(__GNUG__) || defined(__clang__)
-#define CROW_LIKELY(X) __builtin_expect(!!(X), 1)
-#define CROW_UNLIKELY(X) __builtin_expect(!!(X), 0)
-#else
-#define CROW_LIKELY(X) (X)
-#define CROW_UNLIKELY(X) (X)
-#endif
+  tcp::socket::lowest_layer_type &raw_socket() {
+    return ssl_socket_->lowest_layer();
+  }
 
-namespace crow
-{
-    /// @cond SKIP
-    namespace black_magic
-    {
-#ifndef CROW_MSVC_WORKAROUND
-        /// Out of Range Exception for const_str
-        struct OutOfRange
-        {
-            OutOfRange(unsigned /*pos*/, unsigned /*length*/) {}
-        };
-        /// Helper function to throw an exception if i is larger than len
-        constexpr unsigned requires_in_range(unsigned i, unsigned len)
-        {
-            return i >= len ? throw OutOfRange(i, len) : i;
-        }
+  tcp::endpoint remote_endpoint() { return raw_socket().remote_endpoint(); }
 
-        /// A constant string implementation.
-        class const_str
-        {
-            const char* const begin_;
-            unsigned size_;
+  std::string address() const {
+    return ssl_socket_->lowest_layer().remote_endpoint().address().to_string();
+  }
 
-        public:
-            template
-            constexpr const_str(const char (&arr)[N]):
-              begin_(arr), size_(N - 1)
-            {
-                static_assert(N >= 1, "not a string literal");
-            }
-            constexpr char operator[](unsigned i) const
-            {
-                return requires_in_range(i, size_), begin_[i];
-            }
+  bool is_open() { return ssl_socket_ ? raw_socket().is_open() : false; }
 
-            constexpr operator const char*() const
-            {
-                return begin_;
-            }
+  void close() {
+    if (is_open()) {
+      error_code ec;
+      raw_socket().close(ec);
+    }
+  }
 
-            constexpr const char* begin() const { return begin_; }
-            constexpr const char* end() const { return begin_ + size_; }
+  void shutdown_readwrite() {
+    if (is_open()) {
+      error_code ec;
+      raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_both,
+                            ec);
+    }
+  }
 
-            constexpr unsigned size() const
-            {
-                return size_;
-            }
-        };
+  void shutdown_write() {
+    if (is_open()) {
+      error_code ec;
+      raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_send,
+                            ec);
+    }
+  }
 
-        constexpr unsigned find_closing_tag(const_str s, unsigned p)
-        {
-            return s[p] == '>' ? p : find_closing_tag(s, p + 1);
-        }
+  void shutdown_read() {
+    if (is_open()) {
+      error_code ec;
+      raw_socket().shutdown(asio::socket_base::shutdown_type::shutdown_receive,
+                            ec);
+    }
+  }
 
-        /// Check that the CROW_ROUTE string is valid
-        constexpr bool is_valid(const_str s, unsigned i = 0, int f = 0)
-        {
-            return i == s.size()   ? f == 0 :
-                   f < 0 || f >= 2 ? false :
-                   s[i] == '<'     ? is_valid(s, i + 1, f + 1) :
-                   s[i] == '>'     ? is_valid(s, i + 1, f - 1) :
-                                     is_valid(s, i + 1, f);
-        }
+  asio::io_context &get_io_context() { return GET_IO_CONTEXT(raw_socket()); }
 
-        constexpr bool is_equ_p(const char* a, const char* b, unsigned n)
-        {
-            return *a == 0 && *b == 0 && n == 0 ? true :
-                   (*a == 0 || *b == 0)         ? false :
-                   n == 0                       ? true :
-                   *a != *b                     ? false :
-                                                  is_equ_p(a + 1, b + 1, n - 1);
-        }
+  template  void start(F f) {
+    ssl_socket_->async_handshake(asio::ssl::stream_base::server,
+                                 [f](const error_code &ec) { f(ec); });
+  }
 
-        constexpr bool is_equ_n(const_str a, unsigned ai, const_str b, unsigned bi, unsigned n)
-        {
-            return ai + n > a.size() || bi + n > b.size() ? false :
-                   n == 0                                 ? true :
-                   a[ai] != b[bi]                         ? false :
-                                                            is_equ_n(a, ai + 1, b, bi + 1, n - 1);
-        }
+  std::unique_ptr> ssl_socket_;
+};
+#endif
+} // namespace crow
 
-        constexpr bool is_int(const_str s, unsigned i)
-        {
-            return is_equ_n(s, i, "", 0, 5);
-        }
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
-        constexpr bool is_uint(const_str s, unsigned i)
-        {
-            return is_equ_n(s, i, "", 0, 6);
-        }
+namespace crow {
+enum class LogLevel {
+#ifndef ERROR
+#ifndef DEBUG
+  DEBUG = 0,
+  INFO,
+  WARNING,
+  ERROR,
+  CRITICAL,
+#endif
+#endif
 
-        constexpr bool is_float(const_str s, unsigned i)
-        {
-            return is_equ_n(s, i, "", 0, 7) ||
-                   is_equ_n(s, i, "", 0, 8);
-        }
+  Debug = 0,
+  Info,
+  Warning,
+  Error,
+  Critical,
+};
 
-        constexpr bool is_str(const_str s, unsigned i)
-        {
-            return is_equ_n(s, i, "", 0, 5) ||
-                   is_equ_n(s, i, "", 0, 8);
-        }
+class ILogHandler {
+public:
+  virtual ~ILogHandler() = default;
 
-        constexpr bool is_path(const_str s, unsigned i)
-        {
-            return is_equ_n(s, i, "", 0, 6);
-        }
-#endif
-        template
-        struct parameter_tag
-        {
-            static const int value = 0;
-        };
-#define CROW_INTERNAL_PARAMETER_TAG(t, i) \
-    template<>                            \
-    struct parameter_tag               \
-    {                                     \
-        static const int value = i;       \
-    }
-        CROW_INTERNAL_PARAMETER_TAG(int, 1);
-        CROW_INTERNAL_PARAMETER_TAG(char, 1);
-        CROW_INTERNAL_PARAMETER_TAG(short, 1);
-        CROW_INTERNAL_PARAMETER_TAG(long, 1);
-        CROW_INTERNAL_PARAMETER_TAG(long long, 1);
-        CROW_INTERNAL_PARAMETER_TAG(unsigned int, 2);
-        CROW_INTERNAL_PARAMETER_TAG(unsigned char, 2);
-        CROW_INTERNAL_PARAMETER_TAG(unsigned short, 2);
-        CROW_INTERNAL_PARAMETER_TAG(unsigned long, 2);
-        CROW_INTERNAL_PARAMETER_TAG(unsigned long long, 2);
-        CROW_INTERNAL_PARAMETER_TAG(double, 3);
-        CROW_INTERNAL_PARAMETER_TAG(std::string, 4);
-#undef CROW_INTERNAL_PARAMETER_TAG
-        template
-        struct compute_parameter_tag_from_args_list;
+  virtual void log(const std::string &message, LogLevel level) = 0;
+};
 
-        template<>
-        struct compute_parameter_tag_from_args_list<>
-        {
-            static const int value = 0;
-        };
+class CerrLogHandler : public ILogHandler {
+public:
+  void log(const std::string &message, LogLevel level) override {
+    std::string log_msg;
+    log_msg.reserve(message.length() + 1 + 32 + 3 + 8 + 2);
+    log_msg.append("(").append(timestamp()).append(") [");
+
+    switch (level) {
+    case LogLevel::Debug: log_msg.append("DEBUG   "); break;
+    case LogLevel::Info: log_msg.append("INFO    "); break;
+    case LogLevel::Warning: log_msg.append("WARNING "); break;
+    case LogLevel::Error: log_msg.append("ERROR   "); break;
+    case LogLevel::Critical: log_msg.append("CRITICAL"); break;
+    }
 
-        template
-        struct compute_parameter_tag_from_args_list
-        {
-            static const int sub_value =
-              compute_parameter_tag_from_args_list::value;
-            static const int value =
-              parameter_tag::type>::value ? sub_value * 6 + parameter_tag::type>::value : sub_value;
-        };
+    log_msg.append("] ").append(message);
 
-        static inline bool is_parameter_tag_compatible(uint64_t a, uint64_t b)
-        {
-            if (a == 0)
-                return b == 0;
-            if (b == 0)
-                return a == 0;
-            int sa = a % 6;
-            int sb = a % 6;
-            if (sa == 5) sa = 4;
-            if (sb == 5) sb = 4;
-            if (sa != sb)
-                return false;
-            return is_parameter_tag_compatible(a / 6, b / 6);
-        }
+    std::cerr << log_msg << std::endl;
+  }
 
-        static inline unsigned find_closing_tag_runtime(const char* s, unsigned p)
-        {
-            return s[p] == 0   ? throw std::runtime_error("unmatched tag <") :
-                   s[p] == '>' ? p :
-                                 find_closing_tag_runtime(s, p + 1);
-        }
+private:
+  static std::string timestamp() {
+    char date[32];
+    time_t t = time(0);
 
-        static inline uint64_t get_parameter_tag_runtime(const char* s, unsigned p = 0)
-        {
-            return s[p] == 0   ? 0 :
-                   s[p] == '<' ? (
-                                   std::strncmp(s + p, "", 5) == 0  ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 1 :
-                                   std::strncmp(s + p, "", 6) == 0 ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 2 :
-                                   (std::strncmp(s + p, "", 7) == 0 ||
-                                    std::strncmp(s + p, "", 8) == 0) ?
-                                                                           get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 3 :
-                                   (std::strncmp(s + p, "", 5) == 0 ||
-                                    std::strncmp(s + p, "", 8) == 0) ?
-                                                                           get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 4 :
-                                   std::strncmp(s + p, "", 6) == 0 ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 5 :
-                                                                           throw std::runtime_error("invalid parameter type")) :
-                                 get_parameter_tag_runtime(s, p + 1);
-        }
-#ifndef CROW_MSVC_WORKAROUND
-        constexpr uint64_t get_parameter_tag(const_str s, unsigned p = 0)
-        {
-            return p == s.size() ? 0 :
-                   s[p] == '<'   ? (
-                                   is_int(s, p)   ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1 :
-                                     is_uint(s, p)  ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 2 :
-                                     is_float(s, p) ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 3 :
-                                     is_str(s, p)   ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 4 :
-                                     is_path(s, p)  ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 5 :
-                                                      throw std::runtime_error("invalid parameter type")) :
-                                 get_parameter_tag(s, p + 1);
-        }
+    tm my_tm;
+
+#if defined(_MSC_VER) || defined(__MINGW32__)
+#ifdef CROW_USE_LOCALTIMEZONE
+    localtime_s(&my_tm, &t);
+#else
+    gmtime_s(&my_tm, &t);
+#endif
+#else
+#ifdef CROW_USE_LOCALTIMEZONE
+    localtime_r(&t, &my_tm);
+#else
+    gmtime_r(&t, &my_tm);
+#endif
 #endif
 
-        template
-        struct S
-        {
-            template
-            using push = S;
-            template
-            using push_back = S;
-            template class U>
-            using rebind = U;
-        };
+    size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm);
+    return std::string(date, date + sz);
+  }
+};
 
-        // Check whether the template function can be called with specific arguments
-        template
-        struct CallHelper;
-        template
-        struct CallHelper>
-        {
-            template()(std::declval()...))>
-            static char __test(int);
+class logger {
+public:
+  logger(LogLevel level) : level_(level) {}
+  ~logger() {
+#ifdef CROW_ENABLE_LOGGING
+    if (level_ >= get_current_log_level()) {
+      get_handler_ref()->log(stringstream_.str(), level_);
+    }
+#endif
+  }
 
-            template
-            static int __test(...);
+  //
+  template  logger &operator<<(T const &value) {
+#ifdef CROW_ENABLE_LOGGING
+    if (level_ >= get_current_log_level()) { stringstream_ << value; }
+#endif
+    return *this;
+  }
 
-            static constexpr bool value = sizeof(__test(0)) == sizeof(char);
-        };
+  //
+  static void setLogLevel(LogLevel level) { get_log_level_ref() = level; }
 
-        // Check Tuple contains type T
-        template
-        struct has_type;
+  static void setHandler(ILogHandler *handler) { get_handler_ref() = handler; }
+
+  static LogLevel get_current_log_level() { return get_log_level_ref(); }
+
+private:
+  //
+  static LogLevel &get_log_level_ref() {
+    static LogLevel current_level = static_cast(CROW_LOG_LEVEL);
+    return current_level;
+  }
+  static ILogHandler *&get_handler_ref() {
+    static CerrLogHandler default_handler;
+    static ILogHandler *current_handler = &default_handler;
+    return current_handler;
+  }
 
-        template
-        struct has_type> : std::false_type
-        {};
+  //
+  std::ostringstream stringstream_;
+  LogLevel level_;
+};
+} // namespace crow
 
-        template
-        struct has_type> : has_type>
-        {};
+#define CROW_LOG_CRITICAL                                                      \
+  if (crow::logger::get_current_log_level() <= crow::LogLevel::Critical)       \
+  crow::logger(crow::LogLevel::Critical)
+#define CROW_LOG_ERROR                                                         \
+  if (crow::logger::get_current_log_level() <= crow::LogLevel::Error)          \
+  crow::logger(crow::LogLevel::Error)
+#define CROW_LOG_WARNING                                                       \
+  if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning)        \
+  crow::logger(crow::LogLevel::Warning)
+#define CROW_LOG_INFO                                                          \
+  if (crow::logger::get_current_log_level() <= crow::LogLevel::Info)           \
+  crow::logger(crow::LogLevel::Info)
+#define CROW_LOG_DEBUG                                                         \
+  if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug)          \
+  crow::logger(crow::LogLevel::Debug)
 
-        template
-        struct has_type> : std::true_type
-        {};
+#include 
 
-        // Find index of type in tuple
-        template
-        struct tuple_index;
+namespace crow {
+/// An abstract class that allows any other class to be returned by a handler.
+struct returnable {
+  std::string content_type;
+  virtual std::string dump() const = 0;
 
-        template
-        struct tuple_index>
-        {
-            static const int value = 0;
-        };
+  returnable(std::string ctype) : content_type{ctype} {}
 
-        template
-        struct tuple_index>
-        {
-            static const int value = 1 + tuple_index>::value;
-        };
+  virtual ~returnable() {}
+};
+} // namespace crow
 
-        // Extract element from forward tuple or get default
-#ifdef CROW_CAN_USE_CPP14
-        template
-        typename std::enable_if::value, typename std::decay::type&&>::type
-          tuple_extract(Tup& tup)
-        {
-            return std::move(std::get(tup));
-        }
+#ifdef CROW_USE_BOOST
+#include 
+#ifdef CROW_ENABLE_SSL
+#include 
+#endif
 #else
-        template
-        typename std::enable_if::value, T&&>::type
-          tuple_extract(Tup& tup)
-        {
-            return std::move(std::get::value>(tup));
-        }
+#ifndef ASIO_STANDALONE
+#define ASIO_STANDALONE
+#endif
+#include 
+#ifdef CROW_ENABLE_SSL
+#include 
+#endif
 #endif
 
-        template
-        typename std::enable_if::value, T>::type
-          tuple_extract(Tup&)
-        {
-            return T{};
-        }
+namespace crow {
+#ifdef CROW_USE_BOOST
+namespace asio = boost::asio;
+using error_code = boost::system::error_code;
+#else
+using error_code = asio::error_code;
+#endif
+using tcp = asio::ip::tcp;
+using stream_protocol = asio::local::stream_protocol;
 
-        // Kind of fold expressions in C++11
-        template
-        struct bool_pack;
-        template
-        using all_true = std::is_same, bool_pack>;
+struct TCPAcceptor {
+  using endpoint = tcp::endpoint;
+  tcp::acceptor acceptor_;
+  TCPAcceptor(asio::io_context &io_context) : acceptor_(io_context) {}
 
-        template
-        struct single_tag_to_type
-        {};
+  int16_t port() const { return acceptor_.local_endpoint().port(); }
+  std::string address() const {
+    return acceptor_.local_endpoint().address().to_string();
+  }
+  std::string url_display(bool ssl_used) const {
+    auto address = acceptor_.local_endpoint().address();
+    return (ssl_used ? "https://" : "http://") +
+           (address.is_v4() ? address.to_string()
+                            : "[" + address.to_string() + "]") +
+           ":" + std::to_string(acceptor_.local_endpoint().port());
+  }
+  tcp::acceptor &raw_acceptor() { return acceptor_; }
+  endpoint local_endpoint() const { return acceptor_.local_endpoint(); }
+  inline static tcp::acceptor::reuse_address reuse_address_option() {
+    return tcp::acceptor::reuse_address(true);
+  }
+};
 
-        template<>
-        struct single_tag_to_type<1>
-        {
-            using type = int64_t;
-        };
+struct UnixSocketAcceptor {
+  using endpoint = stream_protocol::endpoint;
+  stream_protocol::acceptor acceptor_;
+  UnixSocketAcceptor(asio::io_context &io_context) : acceptor_(io_context) {}
 
-        template<>
-        struct single_tag_to_type<2>
-        {
-            using type = uint64_t;
-        };
+  int16_t port() const { return 0; }
+  std::string address() const { return acceptor_.local_endpoint().path(); }
+  std::string url_display(bool) const {
+    return acceptor_.local_endpoint().path();
+  }
+  stream_protocol::acceptor &raw_acceptor() { return acceptor_; }
+  endpoint local_endpoint() const { return acceptor_.local_endpoint(); }
+  inline static stream_protocol::acceptor::reuse_address
+  reuse_address_option() {
+    // reuse addr must be false
+    // (https://github.com/chriskohlhoff/asio/issues/622)
+    return stream_protocol::acceptor::reuse_address(false);
+  }
+};
+} // namespace crow
 
-        template<>
-        struct single_tag_to_type<3>
-        {
-            using type = double;
-        };
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
-        template<>
-        struct single_tag_to_type<4>
-        {
-            using type = std::string;
-        };
+#include 
 
-        template<>
-        struct single_tag_to_type<5>
-        {
-            using type = std::string;
-        };
+// TODO(EDev): Adding C++20's [[likely]] and [[unlikely]] attributes might be
+// useful
+#if defined(__GNUG__) || defined(__clang__)
+#define CROW_LIKELY(X) __builtin_expect(!!(X), 1)
+#define CROW_UNLIKELY(X) __builtin_expect(!!(X), 0)
+#else
+#define CROW_LIKELY(X) (X)
+#define CROW_UNLIKELY(X) (X)
+#endif
 
+namespace crow {
+/// @cond SKIP
+namespace black_magic {
+#ifndef CROW_MSVC_WORKAROUND
+/// Out of Range Exception for const_str
+struct OutOfRange {
+  OutOfRange(unsigned /*pos*/, unsigned /*length*/) {}
+};
+/// Helper function to throw an exception if i is larger than len
+constexpr unsigned requires_in_range(unsigned i, unsigned len) {
+  return i >= len ? throw OutOfRange(i, len) : i;
+}
 
-        template
-        struct arguments
-        {
-            using subarguments = typename arguments::type;
-            using type =
-              typename subarguments::template push::type>;
-        };
+/// A constant string implementation.
+class const_str {
+  const char *const begin_;
+  unsigned size_;
 
-        template<>
-        struct arguments<0>
-        {
-            using type = S<>;
-        };
+public:
+  template 
+  constexpr const_str(const char (&arr)[N]) : begin_(arr), size_(N - 1) {
+    static_assert(N >= 1, "not a string literal");
+  }
+  constexpr char operator[](unsigned i) const {
+    return requires_in_range(i, size_), begin_[i];
+  }
 
-        template
-        struct last_element_type
-        {
-            using type = typename std::tuple_element>::type;
-        };
+  constexpr operator const char *() const { return begin_; }
 
+  constexpr const char *begin() const { return begin_; }
+  constexpr const char *end() const { return begin_ + size_; }
 
-        template<>
-        struct last_element_type<>
-        {};
+  constexpr unsigned size() const { return size_; }
+};
 
+constexpr unsigned find_closing_tag(const_str s, unsigned p) {
+  return s[p] == '>' ? p : find_closing_tag(s, p + 1);
+}
 
-        // from http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth
-        template
-        using Invoke = typename T::type;
+/// Check that the CROW_ROUTE string is valid
+constexpr bool is_valid(const_str s, unsigned i = 0, int f = 0) {
+  return i == s.size()     ? f == 0
+         : f < 0 || f >= 2 ? false
+         : s[i] == '<'     ? is_valid(s, i + 1, f + 1)
+         : s[i] == '>'     ? is_valid(s, i + 1, f - 1)
+                           : is_valid(s, i + 1, f);
+}
 
-        template
-        struct seq
-        {
-            using type = seq;
-        };
+constexpr bool is_equ_p(const char *a, const char *b, unsigned n) {
+  return *a == 0 && *b == 0 && n == 0 ? true
+         : (*a == 0 || *b == 0)       ? false
+         : n == 0                     ? true
+         : *a != *b                   ? false
+                                      : is_equ_p(a + 1, b + 1, n - 1);
+}
 
-        template
-        struct concat;
+constexpr bool is_equ_n(const_str a, unsigned ai, const_str b, unsigned bi,
+                        unsigned n) {
+  return ai + n > a.size() || bi + n > b.size() ? false
+         : n == 0                               ? true
+         : a[ai] != b[bi]                       ? false
+                          : is_equ_n(a, ai + 1, b, bi + 1, n - 1);
+}
 
-        template
-        struct concat, seq> : seq
-        {};
+constexpr bool is_int(const_str s, unsigned i) {
+  return is_equ_n(s, i, "", 0, 5);
+}
 
-        template
-        using Concat = Invoke>;
+constexpr bool is_uint(const_str s, unsigned i) {
+  return is_equ_n(s, i, "", 0, 6);
+}
 
-        template
-        struct gen_seq;
-        template
-        using GenSeq = Invoke>;
+constexpr bool is_float(const_str s, unsigned i) {
+  return is_equ_n(s, i, "", 0, 7) || is_equ_n(s, i, "", 0, 8);
+}
 
-        template
-        struct gen_seq : Concat, GenSeq>
-        {};
+constexpr bool is_str(const_str s, unsigned i) {
+  return is_equ_n(s, i, "", 0, 5) || is_equ_n(s, i, "", 0, 8);
+}
 
-        template<>
-        struct gen_seq<0> : seq<>
-        {};
-        template<>
-        struct gen_seq<1> : seq<0>
-        {};
+constexpr bool is_path(const_str s, unsigned i) {
+  return is_equ_n(s, i, "", 0, 6);
+}
+#endif
+template  struct parameter_tag {
+  static const int value = 0;
+};
+#define CROW_INTERNAL_PARAMETER_TAG(t, i)                                      \
+  template <> struct parameter_tag {                                        \
+    static const int value = i;                                                \
+  }
+CROW_INTERNAL_PARAMETER_TAG(int, 1);
+CROW_INTERNAL_PARAMETER_TAG(char, 1);
+CROW_INTERNAL_PARAMETER_TAG(short, 1);
+CROW_INTERNAL_PARAMETER_TAG(long, 1);
+CROW_INTERNAL_PARAMETER_TAG(long long, 1);
+CROW_INTERNAL_PARAMETER_TAG(unsigned int, 2);
+CROW_INTERNAL_PARAMETER_TAG(unsigned char, 2);
+CROW_INTERNAL_PARAMETER_TAG(unsigned short, 2);
+CROW_INTERNAL_PARAMETER_TAG(unsigned long, 2);
+CROW_INTERNAL_PARAMETER_TAG(unsigned long long, 2);
+CROW_INTERNAL_PARAMETER_TAG(double, 3);
+CROW_INTERNAL_PARAMETER_TAG(std::string, 4);
+#undef CROW_INTERNAL_PARAMETER_TAG
+template  struct compute_parameter_tag_from_args_list;
 
-        template
-        struct pop_back_helper;
+template <> struct compute_parameter_tag_from_args_list<> {
+  static const int value = 0;
+};
 
-        template
-        struct pop_back_helper, Tuple>
-        {
-            template class U>
-            using rebind = U::type...>;
-        };
+template 
+struct compute_parameter_tag_from_args_list {
+  static const int sub_value =
+      compute_parameter_tag_from_args_list::value;
+  static const int value =
+      parameter_tag::type>::value
+          ? sub_value * 6 + parameter_tag::type>::value
+          : sub_value;
+};
 
-        template
-        struct pop_back //: public pop_back_helper::type, std::tuple>
-        {
-            template class U>
-            using rebind = typename pop_back_helper::type, std::tuple>::template rebind;
-        };
+static inline bool is_parameter_tag_compatible(uint64_t a, uint64_t b) {
+  if (a == 0) return b == 0;
+  if (b == 0) return a == 0;
+  int sa = a % 6;
+  int sb = a % 6;
+  if (sa == 5) sa = 4;
+  if (sb == 5) sb = 4;
+  if (sa != sb) return false;
+  return is_parameter_tag_compatible(a / 6, b / 6);
+}
 
-        template<>
-        struct pop_back<>
-        {
-            template class U>
-            using rebind = U<>;
-        };
+static inline unsigned find_closing_tag_runtime(const char *s, unsigned p) {
+  return s[p] == 0     ? throw std::runtime_error("unmatched tag <")
+         : s[p] == '>' ? p
+                       : find_closing_tag_runtime(s, p + 1);
+}
 
-        // from http://stackoverflow.com/questions/2118541/check-if-c0x-parameter-pack-contains-a-type
-        template
-        struct contains : std::true_type
-        {};
+static inline uint64_t get_parameter_tag_runtime(const char *s,
+                                                 unsigned p = 0) {
+  return s[p] == 0 ? 0
+         : s[p] == '<'
+             ? (std::strncmp(s + p, "", 5) == 0
+                    ? get_parameter_tag_runtime(
+                          s, find_closing_tag_runtime(s, p)) *
+                              6 +
+                          1
+                : std::strncmp(s + p, "", 6) == 0
+                    ? get_parameter_tag_runtime(
+                          s, find_closing_tag_runtime(s, p)) *
+                              6 +
+                          2
+                : (std::strncmp(s + p, "", 7) == 0 ||
+                   std::strncmp(s + p, "", 8) == 0)
+                    ? get_parameter_tag_runtime(
+                          s, find_closing_tag_runtime(s, p)) *
+                              6 +
+                          3
+                : (std::strncmp(s + p, "", 5) == 0 ||
+                   std::strncmp(s + p, "", 8) == 0)
+                    ? get_parameter_tag_runtime(
+                          s, find_closing_tag_runtime(s, p)) *
+                              6 +
+                          4
+                : std::strncmp(s + p, "", 6) == 0
+                    ? get_parameter_tag_runtime(
+                          s, find_closing_tag_runtime(s, p)) *
+                              6 +
+                          5
+                    : throw std::runtime_error("invalid parameter type"))
+             : get_parameter_tag_runtime(s, p + 1);
+}
+#ifndef CROW_MSVC_WORKAROUND
+constexpr uint64_t get_parameter_tag(const_str s, unsigned p = 0) {
+  return p == s.size() ? 0
+         : s[p] == '<'
+             ? (is_int(s, p)
+                    ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1
+                : is_uint(s, p)
+                    ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 2
+                : is_float(s, p)
+                    ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 3
+                : is_str(s, p)
+                    ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 4
+                : is_path(s, p)
+                    ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 5
+                    : throw std::runtime_error("invalid parameter type"))
+             : get_parameter_tag(s, p + 1);
+}
+#endif
 
-        template
-        struct contains : std::conditional::value, std::true_type, contains>::type
-        {};
+template  struct S {
+  template  using push = S;
+  template  using push_back = S;
+  template